VirtualBox

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

Last change on this file since 103583 was 103374, checked in by vboxsync, 10 months ago

VMM/PGM,DBGF,GIC: Parfait pointed out some potential NULL pointer use here and there. bugref:3409

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 45.8 KB
Line 
1/* $Id: DBGFStack.cpp 103374 2024-02-14 22:10:00Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, Call Stack Analyser.
4 */
5
6/*
7 * Copyright (C) 2006-2023 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 enmReturnType,
806 PCDBGFSTACKFRAME *ppFirstFrame)
807{
808 DBGFUNWINDCTX UnwindCtx(pUVM, idCpu, pCtx, hAs);
809
810 /* alloc first frame. */
811 PDBGFSTACKFRAME pCur = (PDBGFSTACKFRAME)MMR3HeapAllocZU(pUVM, MM_TAG_DBGF_STACK, sizeof(*pCur));
812 if (!pCur)
813 return VERR_NO_MEMORY;
814
815 /*
816 * Initialize the frame.
817 */
818 pCur->pNextInternal = NULL;
819 pCur->pFirstInternal = pCur;
820
821 int rc = VINF_SUCCESS;
822#if defined(VBOX_VMM_TARGET_ARMV8)
823 if (pAddrPC)
824 pCur->AddrPC = *pAddrPC;
825 else
826 DBGFR3AddrFromFlat(pUVM, &pCur->AddrPC, pCtx->Pc.u64);
827#else
828 if (pAddrPC)
829 pCur->AddrPC = *pAddrPC;
830 else if (enmCodeType != DBGFCODETYPE_GUEST)
831 DBGFR3AddrFromFlat(pUVM, &pCur->AddrPC, pCtx->rip);
832 else
833 rc = DBGFR3AddrFromSelOff(pUVM, idCpu, &pCur->AddrPC, pCtx->cs.Sel, pCtx->rip);
834#endif
835 if (RT_SUCCESS(rc))
836 {
837 uint64_t fAddrMask;
838 if (enmCodeType == DBGFCODETYPE_RING0)
839 fAddrMask = HC_ARCH_BITS == 64 ? UINT64_MAX : UINT32_MAX;
840 else if (enmCodeType == DBGFCODETYPE_HYPER)
841 fAddrMask = UINT32_MAX;
842 else if (DBGFADDRESS_IS_FAR16(&pCur->AddrPC))
843 fAddrMask = UINT16_MAX;
844 else if (DBGFADDRESS_IS_FAR32(&pCur->AddrPC))
845 fAddrMask = UINT32_MAX;
846 else if (DBGFADDRESS_IS_FAR64(&pCur->AddrPC))
847 fAddrMask = UINT64_MAX;
848 else
849 {
850 PVMCPU const pVCpu = pUVM->pVM->apCpusR3[idCpu];
851 CPUMMODE const enmCpuMode = CPUMGetGuestMode(pVCpu);
852 if (enmCpuMode == CPUMMODE_REAL)
853 {
854 fAddrMask = UINT16_MAX;
855 if (enmReturnType == RTDBGRETURNTYPE_INVALID)
856 pCur->enmReturnType = RTDBGRETURNTYPE_NEAR16;
857 }
858 else if ( enmCpuMode == CPUMMODE_PROTECTED
859 || !CPUMIsGuestIn64BitCode(pVCpu))
860 {
861 fAddrMask = UINT32_MAX;
862 if (enmReturnType == RTDBGRETURNTYPE_INVALID)
863 pCur->enmReturnType = RTDBGRETURNTYPE_NEAR32;
864 }
865 else
866 {
867 fAddrMask = UINT64_MAX;
868 if (enmReturnType == RTDBGRETURNTYPE_INVALID)
869 pCur->enmReturnType = RTDBGRETURNTYPE_NEAR64;
870 }
871 }
872
873 if (enmReturnType == RTDBGRETURNTYPE_INVALID)
874 switch (pCur->AddrPC.fFlags & DBGFADDRESS_FLAGS_TYPE_MASK)
875 {
876 case DBGFADDRESS_FLAGS_FAR16: pCur->enmReturnType = RTDBGRETURNTYPE_NEAR16; break;
877 case DBGFADDRESS_FLAGS_FAR32: pCur->enmReturnType = RTDBGRETURNTYPE_NEAR32; break;
878 case DBGFADDRESS_FLAGS_FAR64: pCur->enmReturnType = RTDBGRETURNTYPE_NEAR64; break;
879 case DBGFADDRESS_FLAGS_RING0:
880 pCur->enmReturnType = HC_ARCH_BITS == 64 ? RTDBGRETURNTYPE_NEAR64 : RTDBGRETURNTYPE_NEAR32;
881 break;
882 default:
883 pCur->enmReturnType = RTDBGRETURNTYPE_NEAR32;
884 break;
885 }
886
887
888#if defined(VBOX_VMM_TARGET_ARMV8)
889 RT_NOREF(pAddrFrame, pAddrStack);
890 AssertFailed();
891 rc = VERR_NOT_IMPLEMENTED;
892#else
893 if (pAddrStack)
894 pCur->AddrStack = *pAddrStack;
895 else if (enmCodeType != DBGFCODETYPE_GUEST)
896 DBGFR3AddrFromFlat(pUVM, &pCur->AddrStack, pCtx->rsp & fAddrMask);
897 else
898 rc = DBGFR3AddrFromSelOff(pUVM, idCpu, &pCur->AddrStack, pCtx->ss.Sel, pCtx->rsp & fAddrMask);
899
900 Assert(!(pCur->fFlags & DBGFSTACKFRAME_FLAGS_USED_UNWIND_INFO));
901 if (pAddrFrame)
902 pCur->AddrFrame = *pAddrFrame;
903 else if (enmCodeType != DBGFCODETYPE_GUEST)
904 DBGFR3AddrFromFlat(pUVM, &pCur->AddrFrame, pCtx->rbp & fAddrMask);
905 else if (RT_SUCCESS(rc))
906 rc = DBGFR3AddrFromSelOff(pUVM, idCpu, &pCur->AddrFrame, pCtx->ss.Sel, pCtx->rbp & fAddrMask);
907#endif
908
909 /*
910 * Try unwind and get a better frame pointer and state.
911 */
912 if ( RT_SUCCESS(rc)
913 && dbgfR3UnwindCtxSetPcAndSp(&UnwindCtx, &pCur->AddrPC, &pCur->AddrStack)
914 && dbgfR3UnwindCtxDoOneFrame(&UnwindCtx))
915 {
916 pCur->enmReturnType = UnwindCtx.m_State.enmRetType;
917 pCur->fFlags |= DBGFSTACKFRAME_FLAGS_USED_UNWIND_INFO;
918 if (!UnwindCtx.m_fIsHostRing0)
919 rc = DBGFR3AddrFromSelOff(UnwindCtx.m_pUVM, UnwindCtx.m_idCpu, &pCur->AddrFrame,
920 UnwindCtx.m_State.u.x86.FrameAddr.sel, UnwindCtx.m_State.u.x86.FrameAddr.off);
921 else
922 DBGFR3AddrFromHostR0(&pCur->AddrFrame, UnwindCtx.m_State.u.x86.FrameAddr.off);
923 }
924 /*
925 * The first frame.
926 */
927 if (RT_SUCCESS(rc))
928 {
929 if (DBGFADDRESS_IS_VALID(&pCur->AddrPC))
930 {
931 pCur->pSymPC = DBGFR3AsSymbolByAddrA(pUVM, hAs, &pCur->AddrPC,
932 RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL | RTDBGSYMADDR_FLAGS_SKIP_ABS_IN_DEFERRED,
933 NULL /*poffDisp*/, NULL /*phMod*/);
934 pCur->pLinePC = DBGFR3AsLineByAddrA(pUVM, hAs, &pCur->AddrPC, NULL /*poffDisp*/, NULL /*phMod*/);
935 }
936
937 rc = dbgfR3StackWalk(&UnwindCtx, pCur, true /*fFirst*/);
938 }
939 }
940 else
941 pCur->enmReturnType = enmReturnType;
942 if (RT_FAILURE(rc))
943 {
944 DBGFR3StackWalkEnd(pCur);
945 return rc;
946 }
947
948 /*
949 * The other frames.
950 */
951 DBGFSTACKFRAME Next = *pCur;
952 while (!(pCur->fFlags & (DBGFSTACKFRAME_FLAGS_LAST | DBGFSTACKFRAME_FLAGS_MAX_DEPTH | DBGFSTACKFRAME_FLAGS_LOOP)))
953 {
954 Next.cSureRegs = 0;
955 Next.paSureRegs = NULL;
956
957 /* try walk. */
958 rc = dbgfR3StackWalk(&UnwindCtx, &Next, false /*fFirst*/);
959 if (RT_FAILURE(rc))
960 break;
961
962 /* add the next frame to the chain. */
963 PDBGFSTACKFRAME pNext = (PDBGFSTACKFRAME)MMR3HeapAllocU(pUVM, MM_TAG_DBGF_STACK, sizeof(*pNext));
964 if (!pNext)
965 {
966 DBGFR3StackWalkEnd(pCur);
967 return VERR_NO_MEMORY;
968 }
969 *pNext = Next;
970 pCur->pNextInternal = pNext;
971 pCur = pNext;
972 Assert(pCur->pNextInternal == NULL);
973
974 /* check for loop */
975 for (PCDBGFSTACKFRAME pLoop = pCur->pFirstInternal;
976 pLoop && pLoop != pCur;
977 pLoop = pLoop->pNextInternal)
978 if (pLoop->AddrFrame.FlatPtr == pCur->AddrFrame.FlatPtr)
979 {
980 pCur->fFlags |= DBGFSTACKFRAME_FLAGS_LOOP;
981 break;
982 }
983
984 /* check for insane recursion */
985 if (pCur->iFrame >= 2048)
986 pCur->fFlags |= DBGFSTACKFRAME_FLAGS_MAX_DEPTH;
987 }
988
989 *ppFirstFrame = pCur->pFirstInternal;
990 return rc;
991}
992
993
994/**
995 * Common worker for DBGFR3StackWalkBeginGuestEx, DBGFR3StackWalkBeginHyperEx,
996 * DBGFR3StackWalkBeginGuest and DBGFR3StackWalkBeginHyper.
997 */
998static int dbgfR3StackWalkBeginCommon(PUVM pUVM,
999 VMCPUID idCpu,
1000 DBGFCODETYPE enmCodeType,
1001 PCDBGFADDRESS pAddrFrame,
1002 PCDBGFADDRESS pAddrStack,
1003 PCDBGFADDRESS pAddrPC,
1004 RTDBGRETURNTYPE enmReturnType,
1005 PCDBGFSTACKFRAME *ppFirstFrame)
1006{
1007 /*
1008 * Validate parameters.
1009 */
1010 *ppFirstFrame = NULL;
1011 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1012 PVM pVM = pUVM->pVM;
1013 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1014 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
1015 if (pAddrFrame)
1016 AssertReturn(DBGFR3AddrIsValid(pUVM, pAddrFrame), VERR_INVALID_PARAMETER);
1017 if (pAddrStack)
1018 AssertReturn(DBGFR3AddrIsValid(pUVM, pAddrStack), VERR_INVALID_PARAMETER);
1019 if (pAddrPC)
1020 AssertReturn(DBGFR3AddrIsValid(pUVM, pAddrPC), VERR_INVALID_PARAMETER);
1021 AssertReturn(enmReturnType >= RTDBGRETURNTYPE_INVALID && enmReturnType < RTDBGRETURNTYPE_END, VERR_INVALID_PARAMETER);
1022
1023 /*
1024 * Get the CPUM context pointer and pass it on the specified EMT.
1025 */
1026 RTDBGAS hAs;
1027 PCCPUMCTX pCtx;
1028 switch (enmCodeType)
1029 {
1030 case DBGFCODETYPE_GUEST:
1031 pCtx = CPUMQueryGuestCtxPtr(pVM->apCpusR3[idCpu]);
1032 hAs = DBGF_AS_GLOBAL;
1033 break;
1034 case DBGFCODETYPE_HYPER:
1035 pCtx = CPUMQueryGuestCtxPtr(pVM->apCpusR3[idCpu]);
1036 hAs = DBGF_AS_RC_AND_GC_GLOBAL;
1037 break;
1038 case DBGFCODETYPE_RING0:
1039 pCtx = NULL; /* No valid context present. */
1040 hAs = DBGF_AS_R0;
1041 break;
1042 default:
1043 AssertFailedReturn(VERR_INVALID_PARAMETER);
1044 }
1045 return VMR3ReqPriorityCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3StackWalkCtxFull, 10,
1046 pUVM, idCpu, pCtx, hAs, enmCodeType,
1047 pAddrFrame, pAddrStack, pAddrPC, enmReturnType, ppFirstFrame);
1048}
1049
1050
1051/**
1052 * Begins a guest stack walk, extended version.
1053 *
1054 * This will walk the current stack, constructing a list of info frames which is
1055 * returned to the caller. The caller uses DBGFR3StackWalkNext to traverse the
1056 * list and DBGFR3StackWalkEnd to release it.
1057 *
1058 * @returns VINF_SUCCESS on success.
1059 * @returns VERR_NO_MEMORY if we're out of memory.
1060 *
1061 * @param pUVM The user mode VM handle.
1062 * @param idCpu The ID of the virtual CPU which stack we want to walk.
1063 * @param enmCodeType Code type
1064 * @param pAddrFrame Frame address to start at. (Optional)
1065 * @param pAddrStack Stack address to start at. (Optional)
1066 * @param pAddrPC Program counter to start at. (Optional)
1067 * @param enmReturnType The return address type. (Optional)
1068 * @param ppFirstFrame Where to return the pointer to the first info frame.
1069 */
1070VMMR3DECL(int) DBGFR3StackWalkBeginEx(PUVM pUVM,
1071 VMCPUID idCpu,
1072 DBGFCODETYPE enmCodeType,
1073 PCDBGFADDRESS pAddrFrame,
1074 PCDBGFADDRESS pAddrStack,
1075 PCDBGFADDRESS pAddrPC,
1076 RTDBGRETURNTYPE enmReturnType,
1077 PCDBGFSTACKFRAME *ppFirstFrame)
1078{
1079 return dbgfR3StackWalkBeginCommon(pUVM, idCpu, enmCodeType, pAddrFrame, pAddrStack, pAddrPC, enmReturnType, ppFirstFrame);
1080}
1081
1082
1083/**
1084 * Begins a guest stack walk.
1085 *
1086 * This will walk the current stack, constructing a list of info frames which is
1087 * returned to the caller. The caller uses DBGFR3StackWalkNext to traverse the
1088 * list and DBGFR3StackWalkEnd to release it.
1089 *
1090 * @returns VINF_SUCCESS on success.
1091 * @returns VERR_NO_MEMORY if we're out of memory.
1092 *
1093 * @param pUVM The user mode VM handle.
1094 * @param idCpu The ID of the virtual CPU which stack we want to walk.
1095 * @param enmCodeType Code type
1096 * @param ppFirstFrame Where to return the pointer to the first info frame.
1097 */
1098VMMR3DECL(int) DBGFR3StackWalkBegin(PUVM pUVM, VMCPUID idCpu, DBGFCODETYPE enmCodeType, PCDBGFSTACKFRAME *ppFirstFrame)
1099{
1100 return dbgfR3StackWalkBeginCommon(pUVM, idCpu, enmCodeType, NULL, NULL, NULL, RTDBGRETURNTYPE_INVALID, ppFirstFrame);
1101}
1102
1103/**
1104 * Gets the next stack frame.
1105 *
1106 * @returns Pointer to the info for the next stack frame.
1107 * NULL if no more frames.
1108 *
1109 * @param pCurrent Pointer to the current stack frame.
1110 *
1111 */
1112VMMR3DECL(PCDBGFSTACKFRAME) DBGFR3StackWalkNext(PCDBGFSTACKFRAME pCurrent)
1113{
1114 return pCurrent
1115 ? pCurrent->pNextInternal
1116 : NULL;
1117}
1118
1119
1120/**
1121 * Ends a stack walk process.
1122 *
1123 * This *must* be called after a successful first call to any of the stack
1124 * walker functions. If not called we will leak memory or other resources.
1125 *
1126 * @param pFirstFrame The frame returned by one of the begin functions.
1127 */
1128VMMR3DECL(void) DBGFR3StackWalkEnd(PCDBGFSTACKFRAME pFirstFrame)
1129{
1130 if ( !pFirstFrame
1131 || !pFirstFrame->pFirstInternal)
1132 return;
1133
1134 PDBGFSTACKFRAME pFrame = (PDBGFSTACKFRAME)pFirstFrame->pFirstInternal;
1135 while (pFrame)
1136 {
1137 PDBGFSTACKFRAME pCur = pFrame;
1138 pFrame = (PDBGFSTACKFRAME)pCur->pNextInternal;
1139 if (pFrame)
1140 {
1141 if (pCur->pSymReturnPC == pFrame->pSymPC)
1142 pFrame->pSymPC = NULL;
1143 if (pCur->pSymReturnPC == pFrame->pSymReturnPC)
1144 pFrame->pSymReturnPC = NULL;
1145
1146 if (pCur->pSymPC == pFrame->pSymPC)
1147 pFrame->pSymPC = NULL;
1148 if (pCur->pSymPC == pFrame->pSymReturnPC)
1149 pFrame->pSymReturnPC = NULL;
1150
1151 if (pCur->pLineReturnPC == pFrame->pLinePC)
1152 pFrame->pLinePC = NULL;
1153 if (pCur->pLineReturnPC == pFrame->pLineReturnPC)
1154 pFrame->pLineReturnPC = NULL;
1155
1156 if (pCur->pLinePC == pFrame->pLinePC)
1157 pFrame->pLinePC = NULL;
1158 if (pCur->pLinePC == pFrame->pLineReturnPC)
1159 pFrame->pLineReturnPC = NULL;
1160 }
1161
1162 RTDbgSymbolFree(pCur->pSymPC);
1163 RTDbgSymbolFree(pCur->pSymReturnPC);
1164 RTDbgLineFree(pCur->pLinePC);
1165 RTDbgLineFree(pCur->pLineReturnPC);
1166
1167 if (pCur->paSureRegs)
1168 {
1169 MMR3HeapFree(pCur->paSureRegs);
1170 pCur->paSureRegs = NULL;
1171 pCur->cSureRegs = 0;
1172 }
1173
1174 pCur->pNextInternal = NULL;
1175 pCur->pFirstInternal = NULL;
1176 pCur->fFlags = 0;
1177 MMR3HeapFree(pCur);
1178 }
1179}
1180
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