VirtualBox

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

Last change on this file was 106061, checked in by vboxsync, 7 weeks ago

Copyright year updates by scm.

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