VirtualBox

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

Last change on this file since 85416 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

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