VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/DBGFStack.cpp@ 97178

Last change on this file since 97178 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 45.4 KB
Line 
1/* $Id: DBGFStack.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, Call Stack Analyser.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_DBGF
33#include <VBox/vmm/dbgf.h>
34#include <VBox/vmm/selm.h>
35#include <VBox/vmm/mm.h>
36#include "DBGFInternal.h"
37#include <VBox/vmm/vm.h>
38#include <VBox/vmm/uvm.h>
39#include <VBox/err.h>
40#include <VBox/log.h>
41#include <iprt/param.h>
42#include <iprt/assert.h>
43#include <iprt/alloca.h>
44#include <iprt/mem.h>
45#include <iprt/string.h>
46#include <iprt/formats/pecoff.h>
47
48
49/*********************************************************************************************************************************
50* Structures and Typedefs *
51*********************************************************************************************************************************/
52static DECLCALLBACK(int) dbgfR3StackReadCallback(PRTDBGUNWINDSTATE pThis, RTUINTPTR uSp, size_t cbToRead, void *pvDst);
53
54/**
55 * Unwind context.
56 *
57 * @note Using a constructor and destructor here for simple+safe cleanup.
58 */
59typedef struct DBGFUNWINDCTX
60{
61 PUVM m_pUVM;
62 VMCPUID m_idCpu;
63 RTDBGAS m_hAs;
64 PCCPUMCTX m_pInitialCtx;
65 bool m_fIsHostRing0;
66 uint64_t m_uOsScratch; /**< For passing to DBGFOSREG::pfnStackUnwindAssist. */
67
68 RTDBGMOD m_hCached;
69 RTUINTPTR m_uCachedMapping;
70 RTUINTPTR m_cbCachedMapping;
71 RTDBGSEGIDX m_idxCachedSegMapping;
72
73 RTDBGUNWINDSTATE m_State;
74
75 DBGFUNWINDCTX(PUVM pUVM, VMCPUID idCpu, PCCPUMCTX pInitialCtx, RTDBGAS hAs)
76 {
77 m_State.u32Magic = RTDBGUNWINDSTATE_MAGIC;
78 m_State.enmArch = RTLDRARCH_AMD64;
79 m_State.pfnReadStack = dbgfR3StackReadCallback;
80 m_State.pvUser = this;
81 RT_ZERO(m_State.u);
82 if (pInitialCtx)
83 {
84 m_State.u.x86.auRegs[X86_GREG_xAX] = pInitialCtx->rax;
85 m_State.u.x86.auRegs[X86_GREG_xCX] = pInitialCtx->rcx;
86 m_State.u.x86.auRegs[X86_GREG_xDX] = pInitialCtx->rdx;
87 m_State.u.x86.auRegs[X86_GREG_xBX] = pInitialCtx->rbx;
88 m_State.u.x86.auRegs[X86_GREG_xSP] = pInitialCtx->rsp;
89 m_State.u.x86.auRegs[X86_GREG_xBP] = pInitialCtx->rbp;
90 m_State.u.x86.auRegs[X86_GREG_xSI] = pInitialCtx->rsi;
91 m_State.u.x86.auRegs[X86_GREG_xDI] = pInitialCtx->rdi;
92 m_State.u.x86.auRegs[X86_GREG_x8 ] = pInitialCtx->r8;
93 m_State.u.x86.auRegs[X86_GREG_x9 ] = pInitialCtx->r9;
94 m_State.u.x86.auRegs[X86_GREG_x10] = pInitialCtx->r10;
95 m_State.u.x86.auRegs[X86_GREG_x11] = pInitialCtx->r11;
96 m_State.u.x86.auRegs[X86_GREG_x12] = pInitialCtx->r12;
97 m_State.u.x86.auRegs[X86_GREG_x13] = pInitialCtx->r13;
98 m_State.u.x86.auRegs[X86_GREG_x14] = pInitialCtx->r14;
99 m_State.u.x86.auRegs[X86_GREG_x15] = pInitialCtx->r15;
100 m_State.uPc = pInitialCtx->rip;
101 m_State.u.x86.uRFlags = pInitialCtx->rflags.u;
102 m_State.u.x86.auSegs[X86_SREG_ES] = pInitialCtx->es.Sel;
103 m_State.u.x86.auSegs[X86_SREG_CS] = pInitialCtx->cs.Sel;
104 m_State.u.x86.auSegs[X86_SREG_SS] = pInitialCtx->ss.Sel;
105 m_State.u.x86.auSegs[X86_SREG_DS] = pInitialCtx->ds.Sel;
106 m_State.u.x86.auSegs[X86_SREG_GS] = pInitialCtx->gs.Sel;
107 m_State.u.x86.auSegs[X86_SREG_FS] = pInitialCtx->fs.Sel;
108 m_State.u.x86.fRealOrV86 = CPUMIsGuestInRealOrV86ModeEx(pInitialCtx);
109 }
110 else if (hAs == DBGF_AS_R0)
111 VMMR3InitR0StackUnwindState(pUVM, idCpu, &m_State);
112
113 m_pUVM = pUVM;
114 m_idCpu = idCpu;
115 m_hAs = DBGFR3AsResolveAndRetain(pUVM, hAs);
116 m_pInitialCtx = pInitialCtx;
117 m_fIsHostRing0 = hAs == DBGF_AS_R0;
118 m_uOsScratch = 0;
119
120 m_hCached = NIL_RTDBGMOD;
121 m_uCachedMapping = 0;
122 m_cbCachedMapping = 0;
123 m_idxCachedSegMapping = NIL_RTDBGSEGIDX;
124 }
125
126 ~DBGFUNWINDCTX();
127
128} DBGFUNWINDCTX;
129/** Pointer to unwind context. */
130typedef DBGFUNWINDCTX *PDBGFUNWINDCTX;
131
132
133static void dbgfR3UnwindCtxFlushCache(PDBGFUNWINDCTX pUnwindCtx)
134{
135 if (pUnwindCtx->m_hCached != NIL_RTDBGMOD)
136 {
137 RTDbgModRelease(pUnwindCtx->m_hCached);
138 pUnwindCtx->m_hCached = NIL_RTDBGMOD;
139 }
140 pUnwindCtx->m_cbCachedMapping = 0;
141 pUnwindCtx->m_idxCachedSegMapping = NIL_RTDBGSEGIDX;
142}
143
144
145DBGFUNWINDCTX::~DBGFUNWINDCTX()
146{
147 dbgfR3UnwindCtxFlushCache(this);
148 if (m_hAs != NIL_RTDBGAS)
149 {
150 RTDbgAsRelease(m_hAs);
151 m_hAs = NIL_RTDBGAS;
152 }
153}
154
155
156/**
157 * @interface_method_impl{RTDBGUNWINDSTATE,pfnReadStack}
158 */
159static DECLCALLBACK(int) dbgfR3StackReadCallback(PRTDBGUNWINDSTATE pThis, RTUINTPTR uSp, size_t cbToRead, void *pvDst)
160{
161 Assert( pThis->enmArch == RTLDRARCH_AMD64
162 || pThis->enmArch == RTLDRARCH_X86_32);
163
164 PDBGFUNWINDCTX pUnwindCtx = (PDBGFUNWINDCTX)pThis->pvUser;
165 DBGFADDRESS SrcAddr;
166 int rc = VINF_SUCCESS;
167 if (pUnwindCtx->m_fIsHostRing0)
168 DBGFR3AddrFromHostR0(&SrcAddr, uSp);
169 else
170 {
171 if ( pThis->enmArch == RTLDRARCH_X86_32
172 || pThis->enmArch == RTLDRARCH_X86_16)
173 {
174 if (!pThis->u.x86.fRealOrV86)
175 rc = DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &SrcAddr, pThis->u.x86.auSegs[X86_SREG_SS], uSp);
176 else
177 DBGFR3AddrFromFlat(pUnwindCtx->m_pUVM, &SrcAddr, uSp + ((uint32_t)pThis->u.x86.auSegs[X86_SREG_SS] << 4));
178 }
179 else
180 DBGFR3AddrFromFlat(pUnwindCtx->m_pUVM, &SrcAddr, uSp);
181 }
182 if (RT_SUCCESS(rc))
183 rc = DBGFR3MemRead(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &SrcAddr, pvDst, cbToRead);
184 if (RT_SUCCESS(rc))
185 return rc;
186 return -rc; /* Ignore read errors. */
187}
188
189
190/**
191 * Sets PC and SP.
192 *
193 * @returns true.
194 * @param pUnwindCtx The unwind context.
195 * @param pAddrPC The program counter (PC) value to set.
196 * @param pAddrStack The stack pointer (SP) value to set.
197 */
198static bool dbgfR3UnwindCtxSetPcAndSp(PDBGFUNWINDCTX pUnwindCtx, PCDBGFADDRESS pAddrPC, PCDBGFADDRESS pAddrStack)
199{
200 Assert( pUnwindCtx->m_State.enmArch == RTLDRARCH_AMD64
201 || pUnwindCtx->m_State.enmArch == RTLDRARCH_X86_32);
202
203 if (!DBGFADDRESS_IS_FAR(pAddrPC))
204 pUnwindCtx->m_State.uPc = pAddrPC->FlatPtr;
205 else
206 {
207 pUnwindCtx->m_State.uPc = pAddrPC->off;
208 pUnwindCtx->m_State.u.x86.auSegs[X86_SREG_CS] = pAddrPC->Sel;
209 }
210 if (!DBGFADDRESS_IS_FAR(pAddrStack))
211 pUnwindCtx->m_State.u.x86.auRegs[X86_GREG_xSP] = pAddrStack->FlatPtr;
212 else
213 {
214 pUnwindCtx->m_State.u.x86.auRegs[X86_GREG_xSP] = pAddrStack->off;
215 pUnwindCtx->m_State.u.x86.auSegs[X86_SREG_SS] = pAddrStack->Sel;
216 }
217 return true;
218}
219
220
221/**
222 * Tries to unwind one frame using unwind info.
223 *
224 * @returns true on success, false on failure.
225 * @param pUnwindCtx The unwind context.
226 */
227static bool dbgfR3UnwindCtxDoOneFrame(PDBGFUNWINDCTX pUnwindCtx)
228{
229 /*
230 * Need to load it into the cache?
231 */
232 RTUINTPTR offCache = pUnwindCtx->m_State.uPc - pUnwindCtx->m_uCachedMapping;
233 if (offCache >= pUnwindCtx->m_cbCachedMapping)
234 {
235 RTDBGMOD hDbgMod = NIL_RTDBGMOD;
236 RTUINTPTR uBase = 0;
237 RTDBGSEGIDX idxSeg = NIL_RTDBGSEGIDX;
238 int rc = RTDbgAsModuleByAddr(pUnwindCtx->m_hAs, pUnwindCtx->m_State.uPc, &hDbgMod, &uBase, &idxSeg);
239 if (RT_SUCCESS(rc))
240 {
241 dbgfR3UnwindCtxFlushCache(pUnwindCtx);
242 pUnwindCtx->m_hCached = hDbgMod;
243 pUnwindCtx->m_uCachedMapping = uBase;
244 pUnwindCtx->m_idxCachedSegMapping = idxSeg;
245 pUnwindCtx->m_cbCachedMapping = idxSeg == NIL_RTDBGSEGIDX ? RTDbgModImageSize(hDbgMod)
246 : RTDbgModSegmentSize(hDbgMod, idxSeg);
247 offCache = pUnwindCtx->m_State.uPc - uBase;
248 }
249 else
250 return false;
251 }
252
253 /*
254 * Do the lookup.
255 */
256 AssertCompile(UINT32_MAX == NIL_RTDBGSEGIDX);
257 int rc = RTDbgModUnwindFrame(pUnwindCtx->m_hCached, pUnwindCtx->m_idxCachedSegMapping, offCache, &pUnwindCtx->m_State);
258 if (RT_SUCCESS(rc))
259 return true;
260 return false;
261}
262
263
264/**
265 * Read stack memory, will init entire buffer.
266 */
267DECLINLINE(int) dbgfR3StackRead(PUVM pUVM, VMCPUID idCpu, void *pvBuf, PCDBGFADDRESS pSrcAddr, size_t cb, size_t *pcbRead)
268{
269 int rc = DBGFR3MemRead(pUVM, idCpu, pSrcAddr, pvBuf, cb);
270 if (RT_FAILURE(rc))
271 {
272 /* fallback: byte by byte and zero the ones we fail to read. */
273 size_t cbRead;
274 for (cbRead = 0; cbRead < cb; cbRead++)
275 {
276 DBGFADDRESS Addr = *pSrcAddr;
277 rc = DBGFR3MemRead(pUVM, idCpu, DBGFR3AddrAdd(&Addr, cbRead), (uint8_t *)pvBuf + cbRead, 1);
278 if (RT_FAILURE(rc))
279 break;
280 }
281 if (cbRead)
282 rc = VINF_SUCCESS;
283 memset((char *)pvBuf + cbRead, 0, cb - cbRead);
284 *pcbRead = cbRead;
285 }
286 else
287 *pcbRead = cb;
288 return rc;
289}
290
291/**
292 * Collects sure registers on frame exit.
293 *
294 * @returns VINF_SUCCESS or VERR_NO_MEMORY.
295 * @param pUVM The user mode VM handle for the allocation.
296 * @param pFrame The frame in question.
297 * @param pState The unwind state.
298 */
299static int dbgfR3StackWalkCollectRegisterChanges(PUVM pUVM, PDBGFSTACKFRAME pFrame, PRTDBGUNWINDSTATE pState)
300{
301 pFrame->cSureRegs = 0;
302 pFrame->paSureRegs = NULL;
303
304 if ( pState->enmArch == RTLDRARCH_AMD64
305 || pState->enmArch == RTLDRARCH_X86_32
306 || pState->enmArch == RTLDRARCH_X86_16)
307 {
308 if (pState->u.x86.Loaded.fAll)
309 {
310 /*
311 * Count relevant registers.
312 */
313 uint32_t cRegs = 0;
314 if (pState->u.x86.Loaded.s.fRegs)
315 for (uint32_t f = 1; f < RT_BIT_32(RT_ELEMENTS(pState->u.x86.auRegs)); f <<= 1)
316 if (pState->u.x86.Loaded.s.fRegs & f)
317 cRegs++;
318 if (pState->u.x86.Loaded.s.fSegs)
319 for (uint32_t f = 1; f < RT_BIT_32(RT_ELEMENTS(pState->u.x86.auSegs)); f <<= 1)
320 if (pState->u.x86.Loaded.s.fSegs & f)
321 cRegs++;
322 if (pState->u.x86.Loaded.s.fRFlags)
323 cRegs++;
324 if (pState->u.x86.Loaded.s.fErrCd)
325 cRegs++;
326 if (cRegs > 0)
327 {
328 /*
329 * Allocate the arrays.
330 */
331 PDBGFREGVALEX paSureRegs = (PDBGFREGVALEX)MMR3HeapAllocZU(pUVM, MM_TAG_DBGF_STACK, sizeof(DBGFREGVALEX) * cRegs);
332 AssertReturn(paSureRegs, VERR_NO_MEMORY);
333 pFrame->paSureRegs = paSureRegs;
334 pFrame->cSureRegs = cRegs;
335
336 /*
337 * Popuplate the arrays.
338 */
339 uint32_t iReg = 0;
340 if (pState->u.x86.Loaded.s.fRegs)
341 for (uint32_t i = 0; i < RT_ELEMENTS(pState->u.x86.auRegs); i++)
342 if (pState->u.x86.Loaded.s.fRegs & RT_BIT(i))
343 {
344 paSureRegs[iReg].Value.u64 = pState->u.x86.auRegs[i];
345 paSureRegs[iReg].enmType = DBGFREGVALTYPE_U64;
346 paSureRegs[iReg].enmReg = (DBGFREG)(DBGFREG_RAX + i);
347 iReg++;
348 }
349
350 if (pState->u.x86.Loaded.s.fSegs)
351 for (uint32_t i = 0; i < RT_ELEMENTS(pState->u.x86.auSegs); i++)
352 if (pState->u.x86.Loaded.s.fSegs & RT_BIT(i))
353 {
354 paSureRegs[iReg].Value.u16 = pState->u.x86.auSegs[i];
355 paSureRegs[iReg].enmType = DBGFREGVALTYPE_U16;
356 switch (i)
357 {
358 case X86_SREG_ES: paSureRegs[iReg].enmReg = DBGFREG_ES; break;
359 case X86_SREG_CS: paSureRegs[iReg].enmReg = DBGFREG_CS; break;
360 case X86_SREG_SS: paSureRegs[iReg].enmReg = DBGFREG_SS; break;
361 case X86_SREG_DS: paSureRegs[iReg].enmReg = DBGFREG_DS; break;
362 case X86_SREG_FS: paSureRegs[iReg].enmReg = DBGFREG_FS; break;
363 case X86_SREG_GS: paSureRegs[iReg].enmReg = DBGFREG_GS; break;
364 default: AssertFailedBreak();
365 }
366 iReg++;
367 }
368
369 if (iReg < cRegs)
370 {
371 if (pState->u.x86.Loaded.s.fRFlags)
372 {
373 paSureRegs[iReg].Value.u64 = pState->u.x86.uRFlags;
374 paSureRegs[iReg].enmType = DBGFREGVALTYPE_U64;
375 paSureRegs[iReg].enmReg = DBGFREG_RFLAGS;
376 iReg++;
377 }
378 if (pState->u.x86.Loaded.s.fErrCd)
379 {
380 paSureRegs[iReg].Value.u64 = pState->u.x86.uErrCd;
381 paSureRegs[iReg].enmType = DBGFREGVALTYPE_U64;
382 paSureRegs[iReg].enmReg = DBGFREG_END;
383 paSureRegs[iReg].pszName = "trap-errcd";
384 iReg++;
385 }
386 }
387 Assert(iReg == cRegs);
388 }
389 }
390 }
391
392 return VINF_SUCCESS;
393}
394
395
396/**
397 * Internal worker routine.
398 *
399 * On x86 the typical stack frame layout is like this:
400 * .. ..
401 * 16 parameter 2
402 * 12 parameter 1
403 * 8 parameter 0
404 * 4 return address
405 * 0 old ebp; current ebp points here
406 */
407DECL_NO_INLINE(static, int) dbgfR3StackWalk(PDBGFUNWINDCTX pUnwindCtx, PDBGFSTACKFRAME pFrame, bool fFirst)
408{
409 /*
410 * Stop if we got a read error in the previous run.
411 */
412 if (pFrame->fFlags & DBGFSTACKFRAME_FLAGS_LAST)
413 return VERR_NO_MORE_FILES;
414
415 /*
416 * Advance the frame (except for the first).
417 */
418 if (!fFirst) /** @todo we can probably eliminate this fFirst business... */
419 {
420 /* frame, pc and stack is taken from the existing frames return members. */
421 pFrame->AddrFrame = pFrame->AddrReturnFrame;
422 pFrame->AddrPC = pFrame->AddrReturnPC;
423 pFrame->pSymPC = pFrame->pSymReturnPC;
424 pFrame->pLinePC = pFrame->pLineReturnPC;
425
426 /* increment the frame number. */
427 pFrame->iFrame++;
428
429 /* UNWIND_INFO_RET -> USED_UNWIND; return type */
430 if (!(pFrame->fFlags & DBGFSTACKFRAME_FLAGS_UNWIND_INFO_RET))
431 pFrame->fFlags &= ~DBGFSTACKFRAME_FLAGS_USED_UNWIND_INFO;
432 else
433 {
434 pFrame->fFlags |= DBGFSTACKFRAME_FLAGS_USED_UNWIND_INFO;
435 pFrame->fFlags &= ~DBGFSTACKFRAME_FLAGS_UNWIND_INFO_RET;
436 if (pFrame->enmReturnFrameReturnType != RTDBGRETURNTYPE_INVALID)
437 {
438 pFrame->enmReturnType = pFrame->enmReturnFrameReturnType;
439 pFrame->enmReturnFrameReturnType = RTDBGRETURNTYPE_INVALID;
440 }
441 }
442 pFrame->fFlags &= ~DBGFSTACKFRAME_FLAGS_TRAP_FRAME;
443 }
444
445 /*
446 * Figure the return address size and use the old PC to guess stack item size.
447 */
448 /** @todo this is bogus... */
449 unsigned cbRetAddr = RTDbgReturnTypeSize(pFrame->enmReturnType);
450 unsigned cbStackItem;
451 switch (pFrame->AddrPC.fFlags & DBGFADDRESS_FLAGS_TYPE_MASK)
452 {
453 case DBGFADDRESS_FLAGS_FAR16: cbStackItem = 2; break;
454 case DBGFADDRESS_FLAGS_FAR32: cbStackItem = 4; break;
455 case DBGFADDRESS_FLAGS_FAR64: cbStackItem = 8; break;
456 case DBGFADDRESS_FLAGS_RING0: cbStackItem = sizeof(RTHCUINTPTR); break;
457 default:
458 switch (pFrame->enmReturnType)
459 {
460 case RTDBGRETURNTYPE_FAR16:
461 case RTDBGRETURNTYPE_IRET16:
462 case RTDBGRETURNTYPE_IRET32_V86:
463 case RTDBGRETURNTYPE_NEAR16: cbStackItem = 2; break;
464
465 case RTDBGRETURNTYPE_FAR32:
466 case RTDBGRETURNTYPE_IRET32:
467 case RTDBGRETURNTYPE_IRET32_PRIV:
468 case RTDBGRETURNTYPE_NEAR32: cbStackItem = 4; break;
469
470 case RTDBGRETURNTYPE_FAR64:
471 case RTDBGRETURNTYPE_IRET64:
472 case RTDBGRETURNTYPE_NEAR64: cbStackItem = 8; break;
473
474 default:
475 AssertMsgFailed(("%d\n", pFrame->enmReturnType));
476 cbStackItem = 4;
477 break;
478 }
479 }
480
481 /*
482 * Read the raw frame data.
483 * We double cbRetAddr in case we have a far return.
484 */
485 union
486 {
487 uint64_t *pu64;
488 uint32_t *pu32;
489 uint16_t *pu16;
490 uint8_t *pb;
491 void *pv;
492 } u, uRet, uArgs, uBp;
493 size_t cbRead = cbRetAddr*2 + cbStackItem + sizeof(pFrame->Args);
494 u.pv = alloca(cbRead);
495 uBp = u;
496 uRet.pb = u.pb + cbStackItem;
497 uArgs.pb = u.pb + cbStackItem + cbRetAddr;
498
499 Assert(DBGFADDRESS_IS_VALID(&pFrame->AddrFrame));
500 int rc = dbgfR3StackRead(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, u.pv, &pFrame->AddrFrame, cbRead, &cbRead);
501 if ( RT_FAILURE(rc)
502 || cbRead < cbRetAddr + cbStackItem)
503 pFrame->fFlags |= DBGFSTACKFRAME_FLAGS_LAST;
504
505 /*
506 * Return Frame address.
507 *
508 * If we used unwind info to get here, the unwind register context will be
509 * positioned after the return instruction has been executed. We start by
510 * picking up the rBP register here for return frame and will try improve
511 * on it further down by using unwind info.
512 */
513 pFrame->AddrReturnFrame = pFrame->AddrFrame;
514 if (pFrame->fFlags & DBGFSTACKFRAME_FLAGS_USED_UNWIND_INFO)
515 {
516 if ( pFrame->enmReturnType == RTDBGRETURNTYPE_IRET32_PRIV
517 || pFrame->enmReturnType == RTDBGRETURNTYPE_IRET64)
518 DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &pFrame->AddrReturnFrame,
519 pUnwindCtx->m_State.u.x86.auSegs[X86_SREG_SS], pUnwindCtx->m_State.u.x86.auRegs[X86_GREG_xBP]);
520 else if (pFrame->enmReturnType == RTDBGRETURNTYPE_IRET32_V86)
521 DBGFR3AddrFromFlat(pUnwindCtx->m_pUVM, &pFrame->AddrReturnFrame,
522 ((uint32_t)pUnwindCtx->m_State.u.x86.auSegs[X86_SREG_SS] << 4)
523 + pUnwindCtx->m_State.u.x86.auRegs[X86_GREG_xBP]);
524 else
525 {
526 pFrame->AddrReturnFrame.off = pUnwindCtx->m_State.u.x86.auRegs[X86_GREG_xBP];
527 pFrame->AddrReturnFrame.FlatPtr += pFrame->AddrReturnFrame.off - pFrame->AddrFrame.off;
528 }
529 }
530 else
531 {
532 switch (cbStackItem)
533 {
534 case 2: pFrame->AddrReturnFrame.off = *uBp.pu16; break;
535 case 4: pFrame->AddrReturnFrame.off = *uBp.pu32; break;
536 case 8: pFrame->AddrReturnFrame.off = *uBp.pu64; break;
537 default: AssertMsgFailedReturn(("cbStackItem=%d\n", cbStackItem), VERR_DBGF_STACK_IPE_1);
538 }
539
540 /* Watcom tries to keep the frame pointer odd for far returns. */
541 if ( cbStackItem <= 4
542 && !(pFrame->fFlags & DBGFSTACKFRAME_FLAGS_USED_UNWIND_INFO))
543 {
544 if (pFrame->AddrReturnFrame.off & 1)
545 {
546 pFrame->AddrReturnFrame.off &= ~(RTGCUINTPTR)1;
547 if (pFrame->enmReturnType == RTDBGRETURNTYPE_NEAR16)
548 {
549 pFrame->fFlags |= DBGFSTACKFRAME_FLAGS_USED_ODD_EVEN;
550 pFrame->enmReturnType = RTDBGRETURNTYPE_FAR16;
551 cbRetAddr = 4;
552 }
553 else if (pFrame->enmReturnType == RTDBGRETURNTYPE_NEAR32)
554 {
555#if 1
556 /* Assumes returning 32-bit code. */
557 pFrame->fFlags |= DBGFSTACKFRAME_FLAGS_USED_ODD_EVEN;
558 pFrame->enmReturnType = RTDBGRETURNTYPE_FAR32;
559 cbRetAddr = 8;
560#else
561 /* Assumes returning 16-bit code. */
562 pFrame->fFlags |= DBGFSTACKFRAME_FLAGS_USED_ODD_EVEN;
563 pFrame->enmReturnType = RTDBGRETURNTYPE_FAR16;
564 cbRetAddr = 4;
565#endif
566 }
567 }
568 else if (pFrame->fFlags & DBGFSTACKFRAME_FLAGS_USED_ODD_EVEN)
569 {
570 if (pFrame->enmReturnType == RTDBGRETURNTYPE_FAR16)
571 {
572 pFrame->enmReturnType = RTDBGRETURNTYPE_NEAR16;
573 cbRetAddr = 2;
574 }
575 else if (pFrame->enmReturnType == RTDBGRETURNTYPE_NEAR32)
576 {
577 pFrame->enmReturnType = RTDBGRETURNTYPE_FAR32;
578 cbRetAddr = 4;
579 }
580 pFrame->fFlags &= ~DBGFSTACKFRAME_FLAGS_USED_ODD_EVEN;
581 }
582 uArgs.pb = u.pb + cbStackItem + cbRetAddr;
583 }
584
585 pFrame->AddrReturnFrame.FlatPtr += pFrame->AddrReturnFrame.off - pFrame->AddrFrame.off;
586 }
587
588 /*
589 * Return Stack Address.
590 */
591 pFrame->AddrReturnStack = pFrame->AddrReturnFrame;
592 if (pFrame->fFlags & DBGFSTACKFRAME_FLAGS_USED_UNWIND_INFO)
593 {
594 if ( pFrame->enmReturnType == RTDBGRETURNTYPE_IRET32_PRIV
595 || pFrame->enmReturnType == RTDBGRETURNTYPE_IRET64)
596 DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &pFrame->AddrReturnStack,
597 pUnwindCtx->m_State.u.x86.auSegs[X86_SREG_SS], pUnwindCtx->m_State.u.x86.auRegs[X86_GREG_xSP]);
598 else if (pFrame->enmReturnType == RTDBGRETURNTYPE_IRET32_V86)
599 DBGFR3AddrFromFlat(pUnwindCtx->m_pUVM, &pFrame->AddrReturnStack,
600 ((uint32_t)pUnwindCtx->m_State.u.x86.auSegs[X86_SREG_SS] << 4)
601 + pUnwindCtx->m_State.u.x86.auRegs[X86_GREG_xSP]);
602 else
603 {
604 pFrame->AddrReturnStack.off = pUnwindCtx->m_State.u.x86.auRegs[X86_GREG_xSP];
605 pFrame->AddrReturnStack.FlatPtr += pFrame->AddrReturnStack.off - pFrame->AddrStack.off;
606 }
607 }
608 else
609 {
610 pFrame->AddrReturnStack.off += cbStackItem + cbRetAddr;
611 pFrame->AddrReturnStack.FlatPtr += cbStackItem + cbRetAddr;
612 }
613
614 /*
615 * Return PC.
616 */
617 pFrame->AddrReturnPC = pFrame->AddrPC;
618 if (pFrame->fFlags & DBGFSTACKFRAME_FLAGS_USED_UNWIND_INFO)
619 {
620 if (RTDbgReturnTypeIsNear(pFrame->enmReturnType))
621 {
622 pFrame->AddrReturnPC.off = pUnwindCtx->m_State.uPc;
623 pFrame->AddrReturnPC.FlatPtr += pFrame->AddrReturnPC.off - pFrame->AddrPC.off;
624 }
625 else
626 DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &pFrame->AddrReturnPC,
627 pUnwindCtx->m_State.u.x86.auSegs[X86_SREG_CS], pUnwindCtx->m_State.uPc);
628 }
629 else
630 {
631 int rc2;
632 switch (pFrame->enmReturnType)
633 {
634 case RTDBGRETURNTYPE_NEAR16:
635 if (DBGFADDRESS_IS_VALID(&pFrame->AddrReturnPC))
636 {
637 pFrame->AddrReturnPC.FlatPtr += *uRet.pu16 - pFrame->AddrReturnPC.off;
638 pFrame->AddrReturnPC.off = *uRet.pu16;
639 }
640 else
641 DBGFR3AddrFromFlat(pUnwindCtx->m_pUVM, &pFrame->AddrReturnPC, *uRet.pu16);
642 break;
643 case RTDBGRETURNTYPE_NEAR32:
644 if (DBGFADDRESS_IS_VALID(&pFrame->AddrReturnPC))
645 {
646 pFrame->AddrReturnPC.FlatPtr += *uRet.pu32 - pFrame->AddrReturnPC.off;
647 pFrame->AddrReturnPC.off = *uRet.pu32;
648 }
649 else
650 DBGFR3AddrFromFlat(pUnwindCtx->m_pUVM, &pFrame->AddrReturnPC, *uRet.pu32);
651 break;
652 case RTDBGRETURNTYPE_NEAR64:
653 if (DBGFADDRESS_IS_VALID(&pFrame->AddrReturnPC))
654 {
655 pFrame->AddrReturnPC.FlatPtr += *uRet.pu64 - pFrame->AddrReturnPC.off;
656 pFrame->AddrReturnPC.off = *uRet.pu64;
657 }
658 else
659 DBGFR3AddrFromFlat(pUnwindCtx->m_pUVM, &pFrame->AddrReturnPC, *uRet.pu64);
660 break;
661 case RTDBGRETURNTYPE_FAR16:
662 rc2 = DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &pFrame->AddrReturnPC, uRet.pu16[1], uRet.pu16[0]);
663 if (RT_SUCCESS(rc2))
664 break;
665 rc2 = DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &pFrame->AddrReturnPC, pFrame->AddrPC.Sel, uRet.pu16[0]);
666 if (RT_SUCCESS(rc2))
667 pFrame->enmReturnType = RTDBGRETURNTYPE_NEAR16;
668 else
669 DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &pFrame->AddrReturnPC, uRet.pu16[1], uRet.pu16[0]);
670 break;
671 case RTDBGRETURNTYPE_FAR32:
672 rc2 = DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
673 if (RT_SUCCESS(rc2))
674 break;
675 rc2 = DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &pFrame->AddrReturnPC, pFrame->AddrPC.Sel, uRet.pu32[0]);
676 if (RT_SUCCESS(rc2))
677 pFrame->enmReturnType = RTDBGRETURNTYPE_NEAR32;
678 else
679 DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
680 break;
681 case RTDBGRETURNTYPE_FAR64:
682 DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &pFrame->AddrReturnPC, uRet.pu16[4], uRet.pu64[0]);
683 break;
684 case RTDBGRETURNTYPE_IRET16:
685 DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &pFrame->AddrReturnPC, uRet.pu16[1], uRet.pu16[0]);
686 break;
687 case RTDBGRETURNTYPE_IRET32:
688 DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
689 break;
690 case RTDBGRETURNTYPE_IRET32_PRIV:
691 DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
692 break;
693 case RTDBGRETURNTYPE_IRET32_V86:
694 DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
695 break;
696 case RTDBGRETURNTYPE_IRET64:
697 DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &pFrame->AddrReturnPC, uRet.pu16[4], uRet.pu64[0]);
698 break;
699 default:
700 AssertMsgFailed(("enmReturnType=%d\n", pFrame->enmReturnType));
701 return VERR_INVALID_PARAMETER;
702 }
703 }
704
705
706 pFrame->pSymReturnPC = DBGFR3AsSymbolByAddrA(pUnwindCtx->m_pUVM, pUnwindCtx->m_hAs, &pFrame->AddrReturnPC,
707 RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL | RTDBGSYMADDR_FLAGS_SKIP_ABS_IN_DEFERRED,
708 NULL /*poffDisp*/, NULL /*phMod*/);
709 pFrame->pLineReturnPC = DBGFR3AsLineByAddrA(pUnwindCtx->m_pUVM, pUnwindCtx->m_hAs, &pFrame->AddrReturnPC,
710 NULL /*poffDisp*/, NULL /*phMod*/);
711
712 /*
713 * Frame bitness flag.
714 */
715 /** @todo use previous return type for this? */
716 pFrame->fFlags &= ~(DBGFSTACKFRAME_FLAGS_16BIT | DBGFSTACKFRAME_FLAGS_32BIT | DBGFSTACKFRAME_FLAGS_64BIT);
717 switch (cbStackItem)
718 {
719 case 2: pFrame->fFlags |= DBGFSTACKFRAME_FLAGS_16BIT; break;
720 case 4: pFrame->fFlags |= DBGFSTACKFRAME_FLAGS_32BIT; break;
721 case 8: pFrame->fFlags |= DBGFSTACKFRAME_FLAGS_64BIT; break;
722 default: AssertMsgFailedReturn(("cbStackItem=%d\n", cbStackItem), VERR_DBGF_STACK_IPE_2);
723 }
724
725 /*
726 * The arguments.
727 */
728 memcpy(&pFrame->Args, uArgs.pv, sizeof(pFrame->Args));
729
730 /*
731 * Collect register changes.
732 * Then call the OS layer to assist us (e.g. NT trap frames).
733 */
734 if (pFrame->fFlags & DBGFSTACKFRAME_FLAGS_USED_UNWIND_INFO)
735 {
736 rc = dbgfR3StackWalkCollectRegisterChanges(pUnwindCtx->m_pUVM, pFrame, &pUnwindCtx->m_State);
737 if (RT_FAILURE(rc))
738 return rc;
739
740 if ( pUnwindCtx->m_pInitialCtx
741 && pUnwindCtx->m_hAs != NIL_RTDBGAS)
742 {
743 rc = dbgfR3OSStackUnwindAssist(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, pFrame, &pUnwindCtx->m_State,
744 pUnwindCtx->m_pInitialCtx, pUnwindCtx->m_hAs, &pUnwindCtx->m_uOsScratch);
745 if (RT_FAILURE(rc))
746 return rc;
747 }
748 }
749
750 /*
751 * Try use unwind information to locate the return frame pointer (for the
752 * next loop iteration).
753 */
754 Assert(!(pFrame->fFlags & DBGFSTACKFRAME_FLAGS_UNWIND_INFO_RET));
755 pFrame->enmReturnFrameReturnType = RTDBGRETURNTYPE_INVALID;
756 if (!(pFrame->fFlags & DBGFSTACKFRAME_FLAGS_LAST))
757 {
758 /* Set PC and SP if we didn't unwind our way here (context will then point
759 and the return PC and SP already). */
760 if (!(pFrame->fFlags & DBGFSTACKFRAME_FLAGS_USED_UNWIND_INFO))
761 {
762 dbgfR3UnwindCtxSetPcAndSp(pUnwindCtx, &pFrame->AddrReturnPC, &pFrame->AddrReturnStack);
763 pUnwindCtx->m_State.u.x86.auRegs[X86_GREG_xBP] = pFrame->AddrReturnFrame.off;
764 }
765 /** @todo Reevaluate CS if the previous frame return type isn't near. */
766 if ( pUnwindCtx->m_State.enmArch == RTLDRARCH_AMD64
767 || pUnwindCtx->m_State.enmArch == RTLDRARCH_X86_32
768 || pUnwindCtx->m_State.enmArch == RTLDRARCH_X86_16)
769 pUnwindCtx->m_State.u.x86.Loaded.fAll = 0;
770 else
771 AssertFailed();
772 if (dbgfR3UnwindCtxDoOneFrame(pUnwindCtx))
773 {
774 if (pUnwindCtx->m_fIsHostRing0)
775 DBGFR3AddrFromHostR0(&pFrame->AddrReturnFrame, pUnwindCtx->m_State.u.x86.FrameAddr.off);
776 else
777 {
778 DBGFADDRESS AddrReturnFrame = pFrame->AddrReturnFrame;
779 rc = DBGFR3AddrFromSelOff(pUnwindCtx->m_pUVM, pUnwindCtx->m_idCpu, &AddrReturnFrame,
780 pUnwindCtx->m_State.u.x86.FrameAddr.sel, pUnwindCtx->m_State.u.x86.FrameAddr.off);
781 if (RT_SUCCESS(rc))
782 pFrame->AddrReturnFrame = AddrReturnFrame;
783 }
784 pFrame->enmReturnFrameReturnType = pUnwindCtx->m_State.enmRetType;
785 pFrame->fFlags |= DBGFSTACKFRAME_FLAGS_UNWIND_INFO_RET;
786 }
787 }
788
789 return VINF_SUCCESS;
790}
791
792
793/**
794 * Walks the entire stack allocating memory as we walk.
795 */
796static DECLCALLBACK(int) dbgfR3StackWalkCtxFull(PUVM pUVM, VMCPUID idCpu, PCCPUMCTX pCtx, RTDBGAS hAs,
797 DBGFCODETYPE enmCodeType,
798 PCDBGFADDRESS pAddrFrame,
799 PCDBGFADDRESS pAddrStack,
800 PCDBGFADDRESS pAddrPC,
801 RTDBGRETURNTYPE enmReturnType,
802 PCDBGFSTACKFRAME *ppFirstFrame)
803{
804 DBGFUNWINDCTX UnwindCtx(pUVM, idCpu, pCtx, hAs);
805
806 /* alloc first frame. */
807 PDBGFSTACKFRAME pCur = (PDBGFSTACKFRAME)MMR3HeapAllocZU(pUVM, MM_TAG_DBGF_STACK, sizeof(*pCur));
808 if (!pCur)
809 return VERR_NO_MEMORY;
810
811 /*
812 * Initialize the frame.
813 */
814 pCur->pNextInternal = NULL;
815 pCur->pFirstInternal = pCur;
816
817 int rc = VINF_SUCCESS;
818 if (pAddrPC)
819 pCur->AddrPC = *pAddrPC;
820 else if (enmCodeType != DBGFCODETYPE_GUEST)
821 DBGFR3AddrFromFlat(pUVM, &pCur->AddrPC, pCtx->rip);
822 else
823 rc = DBGFR3AddrFromSelOff(pUVM, idCpu, &pCur->AddrPC, pCtx->cs.Sel, pCtx->rip);
824 if (RT_SUCCESS(rc))
825 {
826 uint64_t fAddrMask;
827 if (enmCodeType == DBGFCODETYPE_RING0)
828 fAddrMask = HC_ARCH_BITS == 64 ? UINT64_MAX : UINT32_MAX;
829 else if (enmCodeType == DBGFCODETYPE_HYPER)
830 fAddrMask = UINT32_MAX;
831 else if (DBGFADDRESS_IS_FAR16(&pCur->AddrPC))
832 fAddrMask = UINT16_MAX;
833 else if (DBGFADDRESS_IS_FAR32(&pCur->AddrPC))
834 fAddrMask = UINT32_MAX;
835 else if (DBGFADDRESS_IS_FAR64(&pCur->AddrPC))
836 fAddrMask = UINT64_MAX;
837 else
838 {
839 PVMCPU pVCpu = VMMGetCpuById(pUVM->pVM, idCpu);
840 CPUMMODE enmCpuMode = CPUMGetGuestMode(pVCpu);
841 if (enmCpuMode == CPUMMODE_REAL)
842 {
843 fAddrMask = UINT16_MAX;
844 if (enmReturnType == RTDBGRETURNTYPE_INVALID)
845 pCur->enmReturnType = RTDBGRETURNTYPE_NEAR16;
846 }
847 else if ( enmCpuMode == CPUMMODE_PROTECTED
848 || !CPUMIsGuestIn64BitCode(pVCpu))
849 {
850 fAddrMask = UINT32_MAX;
851 if (enmReturnType == RTDBGRETURNTYPE_INVALID)
852 pCur->enmReturnType = RTDBGRETURNTYPE_NEAR32;
853 }
854 else
855 {
856 fAddrMask = UINT64_MAX;
857 if (enmReturnType == RTDBGRETURNTYPE_INVALID)
858 pCur->enmReturnType = RTDBGRETURNTYPE_NEAR64;
859 }
860 }
861
862 if (enmReturnType == RTDBGRETURNTYPE_INVALID)
863 switch (pCur->AddrPC.fFlags & DBGFADDRESS_FLAGS_TYPE_MASK)
864 {
865 case DBGFADDRESS_FLAGS_FAR16: pCur->enmReturnType = RTDBGRETURNTYPE_NEAR16; break;
866 case DBGFADDRESS_FLAGS_FAR32: pCur->enmReturnType = RTDBGRETURNTYPE_NEAR32; break;
867 case DBGFADDRESS_FLAGS_FAR64: pCur->enmReturnType = RTDBGRETURNTYPE_NEAR64; break;
868 case DBGFADDRESS_FLAGS_RING0:
869 pCur->enmReturnType = HC_ARCH_BITS == 64 ? RTDBGRETURNTYPE_NEAR64 : RTDBGRETURNTYPE_NEAR32;
870 break;
871 default:
872 pCur->enmReturnType = RTDBGRETURNTYPE_NEAR32;
873 break;
874 }
875
876
877 if (pAddrStack)
878 pCur->AddrStack = *pAddrStack;
879 else if (enmCodeType != DBGFCODETYPE_GUEST)
880 DBGFR3AddrFromFlat(pUVM, &pCur->AddrStack, pCtx->rsp & fAddrMask);
881 else
882 rc = DBGFR3AddrFromSelOff(pUVM, idCpu, &pCur->AddrStack, pCtx->ss.Sel, pCtx->rsp & fAddrMask);
883
884 Assert(!(pCur->fFlags & DBGFSTACKFRAME_FLAGS_USED_UNWIND_INFO));
885 if (pAddrFrame)
886 pCur->AddrFrame = *pAddrFrame;
887 else if (enmCodeType != DBGFCODETYPE_GUEST)
888 DBGFR3AddrFromFlat(pUVM, &pCur->AddrFrame, pCtx->rbp & fAddrMask);
889 else if (RT_SUCCESS(rc))
890 rc = DBGFR3AddrFromSelOff(pUVM, idCpu, &pCur->AddrFrame, pCtx->ss.Sel, pCtx->rbp & fAddrMask);
891
892 /*
893 * Try unwind and get a better frame pointer and state.
894 */
895 if ( RT_SUCCESS(rc)
896 && dbgfR3UnwindCtxSetPcAndSp(&UnwindCtx, &pCur->AddrPC, &pCur->AddrStack)
897 && dbgfR3UnwindCtxDoOneFrame(&UnwindCtx))
898 {
899 pCur->enmReturnType = UnwindCtx.m_State.enmRetType;
900 pCur->fFlags |= DBGFSTACKFRAME_FLAGS_USED_UNWIND_INFO;
901 if (!UnwindCtx.m_fIsHostRing0)
902 rc = DBGFR3AddrFromSelOff(UnwindCtx.m_pUVM, UnwindCtx.m_idCpu, &pCur->AddrFrame,
903 UnwindCtx.m_State.u.x86.FrameAddr.sel, UnwindCtx.m_State.u.x86.FrameAddr.off);
904 else
905 DBGFR3AddrFromHostR0(&pCur->AddrFrame, UnwindCtx.m_State.u.x86.FrameAddr.off);
906 }
907 /*
908 * The first frame.
909 */
910 if (RT_SUCCESS(rc))
911 {
912 if (DBGFADDRESS_IS_VALID(&pCur->AddrPC))
913 {
914 pCur->pSymPC = DBGFR3AsSymbolByAddrA(pUVM, hAs, &pCur->AddrPC,
915 RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL | RTDBGSYMADDR_FLAGS_SKIP_ABS_IN_DEFERRED,
916 NULL /*poffDisp*/, NULL /*phMod*/);
917 pCur->pLinePC = DBGFR3AsLineByAddrA(pUVM, hAs, &pCur->AddrPC, NULL /*poffDisp*/, NULL /*phMod*/);
918 }
919
920 rc = dbgfR3StackWalk(&UnwindCtx, pCur, true /*fFirst*/);
921 }
922 }
923 else
924 pCur->enmReturnType = enmReturnType;
925 if (RT_FAILURE(rc))
926 {
927 DBGFR3StackWalkEnd(pCur);
928 return rc;
929 }
930
931 /*
932 * The other frames.
933 */
934 DBGFSTACKFRAME Next = *pCur;
935 while (!(pCur->fFlags & (DBGFSTACKFRAME_FLAGS_LAST | DBGFSTACKFRAME_FLAGS_MAX_DEPTH | DBGFSTACKFRAME_FLAGS_LOOP)))
936 {
937 Next.cSureRegs = 0;
938 Next.paSureRegs = NULL;
939
940 /* try walk. */
941 rc = dbgfR3StackWalk(&UnwindCtx, &Next, false /*fFirst*/);
942 if (RT_FAILURE(rc))
943 break;
944
945 /* add the next frame to the chain. */
946 PDBGFSTACKFRAME pNext = (PDBGFSTACKFRAME)MMR3HeapAllocU(pUVM, MM_TAG_DBGF_STACK, sizeof(*pNext));
947 if (!pNext)
948 {
949 DBGFR3StackWalkEnd(pCur);
950 return VERR_NO_MEMORY;
951 }
952 *pNext = Next;
953 pCur->pNextInternal = pNext;
954 pCur = pNext;
955 Assert(pCur->pNextInternal == NULL);
956
957 /* check for loop */
958 for (PCDBGFSTACKFRAME pLoop = pCur->pFirstInternal;
959 pLoop && pLoop != pCur;
960 pLoop = pLoop->pNextInternal)
961 if (pLoop->AddrFrame.FlatPtr == pCur->AddrFrame.FlatPtr)
962 {
963 pCur->fFlags |= DBGFSTACKFRAME_FLAGS_LOOP;
964 break;
965 }
966
967 /* check for insane recursion */
968 if (pCur->iFrame >= 2048)
969 pCur->fFlags |= DBGFSTACKFRAME_FLAGS_MAX_DEPTH;
970 }
971
972 *ppFirstFrame = pCur->pFirstInternal;
973 return rc;
974}
975
976
977/**
978 * Common worker for DBGFR3StackWalkBeginGuestEx, DBGFR3StackWalkBeginHyperEx,
979 * DBGFR3StackWalkBeginGuest and DBGFR3StackWalkBeginHyper.
980 */
981static int dbgfR3StackWalkBeginCommon(PUVM pUVM,
982 VMCPUID idCpu,
983 DBGFCODETYPE enmCodeType,
984 PCDBGFADDRESS pAddrFrame,
985 PCDBGFADDRESS pAddrStack,
986 PCDBGFADDRESS pAddrPC,
987 RTDBGRETURNTYPE enmReturnType,
988 PCDBGFSTACKFRAME *ppFirstFrame)
989{
990 /*
991 * Validate parameters.
992 */
993 *ppFirstFrame = NULL;
994 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
995 PVM pVM = pUVM->pVM;
996 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
997 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
998 if (pAddrFrame)
999 AssertReturn(DBGFR3AddrIsValid(pUVM, pAddrFrame), VERR_INVALID_PARAMETER);
1000 if (pAddrStack)
1001 AssertReturn(DBGFR3AddrIsValid(pUVM, pAddrStack), VERR_INVALID_PARAMETER);
1002 if (pAddrPC)
1003 AssertReturn(DBGFR3AddrIsValid(pUVM, pAddrPC), VERR_INVALID_PARAMETER);
1004 AssertReturn(enmReturnType >= RTDBGRETURNTYPE_INVALID && enmReturnType < RTDBGRETURNTYPE_END, VERR_INVALID_PARAMETER);
1005
1006 /*
1007 * Get the CPUM context pointer and pass it on the specified EMT.
1008 */
1009 RTDBGAS hAs;
1010 PCCPUMCTX pCtx;
1011 switch (enmCodeType)
1012 {
1013 case DBGFCODETYPE_GUEST:
1014 pCtx = CPUMQueryGuestCtxPtr(VMMGetCpuById(pVM, idCpu));
1015 hAs = DBGF_AS_GLOBAL;
1016 break;
1017 case DBGFCODETYPE_HYPER:
1018 pCtx = CPUMQueryGuestCtxPtr(VMMGetCpuById(pVM, idCpu));
1019 hAs = DBGF_AS_RC_AND_GC_GLOBAL;
1020 break;
1021 case DBGFCODETYPE_RING0:
1022 pCtx = NULL; /* No valid context present. */
1023 hAs = DBGF_AS_R0;
1024 break;
1025 default:
1026 AssertFailedReturn(VERR_INVALID_PARAMETER);
1027 }
1028 return VMR3ReqPriorityCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3StackWalkCtxFull, 10,
1029 pUVM, idCpu, pCtx, hAs, enmCodeType,
1030 pAddrFrame, pAddrStack, pAddrPC, enmReturnType, ppFirstFrame);
1031}
1032
1033
1034/**
1035 * Begins a guest stack walk, extended version.
1036 *
1037 * This will walk the current stack, constructing a list of info frames which is
1038 * returned to the caller. The caller uses DBGFR3StackWalkNext to traverse the
1039 * list and DBGFR3StackWalkEnd to release it.
1040 *
1041 * @returns VINF_SUCCESS on success.
1042 * @returns VERR_NO_MEMORY if we're out of memory.
1043 *
1044 * @param pUVM The user mode VM handle.
1045 * @param idCpu The ID of the virtual CPU which stack we want to walk.
1046 * @param enmCodeType Code type
1047 * @param pAddrFrame Frame address to start at. (Optional)
1048 * @param pAddrStack Stack address to start at. (Optional)
1049 * @param pAddrPC Program counter to start at. (Optional)
1050 * @param enmReturnType The return address type. (Optional)
1051 * @param ppFirstFrame Where to return the pointer to the first info frame.
1052 */
1053VMMR3DECL(int) DBGFR3StackWalkBeginEx(PUVM pUVM,
1054 VMCPUID idCpu,
1055 DBGFCODETYPE enmCodeType,
1056 PCDBGFADDRESS pAddrFrame,
1057 PCDBGFADDRESS pAddrStack,
1058 PCDBGFADDRESS pAddrPC,
1059 RTDBGRETURNTYPE enmReturnType,
1060 PCDBGFSTACKFRAME *ppFirstFrame)
1061{
1062 return dbgfR3StackWalkBeginCommon(pUVM, idCpu, enmCodeType, pAddrFrame, pAddrStack, pAddrPC, enmReturnType, ppFirstFrame);
1063}
1064
1065
1066/**
1067 * Begins a guest stack walk.
1068 *
1069 * This will walk the current stack, constructing a list of info frames which is
1070 * returned to the caller. The caller uses DBGFR3StackWalkNext to traverse the
1071 * list and DBGFR3StackWalkEnd to release it.
1072 *
1073 * @returns VINF_SUCCESS on success.
1074 * @returns VERR_NO_MEMORY if we're out of memory.
1075 *
1076 * @param pUVM The user mode VM handle.
1077 * @param idCpu The ID of the virtual CPU which stack we want to walk.
1078 * @param enmCodeType Code type
1079 * @param ppFirstFrame Where to return the pointer to the first info frame.
1080 */
1081VMMR3DECL(int) DBGFR3StackWalkBegin(PUVM pUVM, VMCPUID idCpu, DBGFCODETYPE enmCodeType, PCDBGFSTACKFRAME *ppFirstFrame)
1082{
1083 return dbgfR3StackWalkBeginCommon(pUVM, idCpu, enmCodeType, NULL, NULL, NULL, RTDBGRETURNTYPE_INVALID, ppFirstFrame);
1084}
1085
1086/**
1087 * Gets the next stack frame.
1088 *
1089 * @returns Pointer to the info for the next stack frame.
1090 * NULL if no more frames.
1091 *
1092 * @param pCurrent Pointer to the current stack frame.
1093 *
1094 */
1095VMMR3DECL(PCDBGFSTACKFRAME) DBGFR3StackWalkNext(PCDBGFSTACKFRAME pCurrent)
1096{
1097 return pCurrent
1098 ? pCurrent->pNextInternal
1099 : NULL;
1100}
1101
1102
1103/**
1104 * Ends a stack walk process.
1105 *
1106 * This *must* be called after a successful first call to any of the stack
1107 * walker functions. If not called we will leak memory or other resources.
1108 *
1109 * @param pFirstFrame The frame returned by one of the begin functions.
1110 */
1111VMMR3DECL(void) DBGFR3StackWalkEnd(PCDBGFSTACKFRAME pFirstFrame)
1112{
1113 if ( !pFirstFrame
1114 || !pFirstFrame->pFirstInternal)
1115 return;
1116
1117 PDBGFSTACKFRAME pFrame = (PDBGFSTACKFRAME)pFirstFrame->pFirstInternal;
1118 while (pFrame)
1119 {
1120 PDBGFSTACKFRAME pCur = pFrame;
1121 pFrame = (PDBGFSTACKFRAME)pCur->pNextInternal;
1122 if (pFrame)
1123 {
1124 if (pCur->pSymReturnPC == pFrame->pSymPC)
1125 pFrame->pSymPC = NULL;
1126 if (pCur->pSymReturnPC == pFrame->pSymReturnPC)
1127 pFrame->pSymReturnPC = NULL;
1128
1129 if (pCur->pSymPC == pFrame->pSymPC)
1130 pFrame->pSymPC = NULL;
1131 if (pCur->pSymPC == pFrame->pSymReturnPC)
1132 pFrame->pSymReturnPC = NULL;
1133
1134 if (pCur->pLineReturnPC == pFrame->pLinePC)
1135 pFrame->pLinePC = NULL;
1136 if (pCur->pLineReturnPC == pFrame->pLineReturnPC)
1137 pFrame->pLineReturnPC = NULL;
1138
1139 if (pCur->pLinePC == pFrame->pLinePC)
1140 pFrame->pLinePC = NULL;
1141 if (pCur->pLinePC == pFrame->pLineReturnPC)
1142 pFrame->pLineReturnPC = NULL;
1143 }
1144
1145 RTDbgSymbolFree(pCur->pSymPC);
1146 RTDbgSymbolFree(pCur->pSymReturnPC);
1147 RTDbgLineFree(pCur->pLinePC);
1148 RTDbgLineFree(pCur->pLineReturnPC);
1149
1150 if (pCur->paSureRegs)
1151 {
1152 MMR3HeapFree(pCur->paSureRegs);
1153 pCur->paSureRegs = NULL;
1154 pCur->cSureRegs = 0;
1155 }
1156
1157 pCur->pNextInternal = NULL;
1158 pCur->pFirstInternal = NULL;
1159 pCur->fFlags = 0;
1160 MMR3HeapFree(pCur);
1161 }
1162}
1163
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette