1 | /* $Id: DBGFStack.cpp 19687 2009-05-14 10:27:03Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DBGF - Debugger Facility, Call Stack Analyser.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_DBGF
|
---|
27 | #include <VBox/dbgf.h>
|
---|
28 | #include <VBox/selm.h>
|
---|
29 | #include <VBox/mm.h>
|
---|
30 | #include "DBGFInternal.h"
|
---|
31 | #include <VBox/vm.h>
|
---|
32 | #include <VBox/err.h>
|
---|
33 | #include <VBox/log.h>
|
---|
34 | #include <iprt/param.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/alloca.h>
|
---|
38 |
|
---|
39 |
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Read stack memory.
|
---|
43 | */
|
---|
44 | DECLINLINE(int) dbgfR3Read(PVM pVM, VMCPUID idCpu, void *pvBuf, PCDBGFADDRESS pSrcAddr, size_t cb, size_t *pcbRead)
|
---|
45 | {
|
---|
46 | int rc = DBGFR3MemRead(pVM, idCpu, pSrcAddr, pvBuf, cb);
|
---|
47 | if (RT_FAILURE(rc))
|
---|
48 | {
|
---|
49 | /* fallback: byte by byte and zero the ones we fail to read. */
|
---|
50 | size_t cbRead;
|
---|
51 | for (cbRead = 0; cbRead < cb; cbRead++)
|
---|
52 | {
|
---|
53 | DBGFADDRESS Addr = *pSrcAddr;
|
---|
54 | rc = DBGFR3MemRead(pVM, idCpu, DBGFR3AddrAdd(&Addr, cbRead), (uint8_t *)pvBuf + cbRead, 1);
|
---|
55 | if (RT_FAILURE(rc))
|
---|
56 | break;
|
---|
57 | }
|
---|
58 | if (cbRead)
|
---|
59 | rc = VINF_SUCCESS;
|
---|
60 | memset((char *)pvBuf + cbRead, 0, cb - cbRead);
|
---|
61 | *pcbRead = cbRead;
|
---|
62 | }
|
---|
63 | else
|
---|
64 | *pcbRead = cb;
|
---|
65 | return rc;
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Internal worker routine.
|
---|
71 | *
|
---|
72 | * On x86 the typical stack frame layout is like this:
|
---|
73 | * .. ..
|
---|
74 | * 16 parameter 2
|
---|
75 | * 12 parameter 1
|
---|
76 | * 8 parameter 0
|
---|
77 | * 4 return address
|
---|
78 | * 0 old ebp; current ebp points here
|
---|
79 | *
|
---|
80 | * @todo Add AMD64 support (needs teaming up with the module management for
|
---|
81 | * unwind tables).
|
---|
82 | */
|
---|
83 | static int dbgfR3StackWalk(PVM pVM, VMCPUID idCpu, PDBGFSTACKFRAME pFrame)
|
---|
84 | {
|
---|
85 | /*
|
---|
86 | * Stop if we got a read error in the previous run.
|
---|
87 | */
|
---|
88 | if (pFrame->fFlags & DBGFSTACKFRAME_FLAGS_LAST)
|
---|
89 | return VERR_NO_MORE_FILES;
|
---|
90 |
|
---|
91 | /*
|
---|
92 | * Read the raw frame data.
|
---|
93 | */
|
---|
94 | const DBGFADDRESS AddrOldPC = pFrame->AddrPC;
|
---|
95 | const unsigned cbRetAddr = DBGFReturnTypeSize(pFrame->enmReturnType);
|
---|
96 | unsigned cbStackItem;
|
---|
97 | switch (AddrOldPC.fFlags & DBGFADDRESS_FLAGS_TYPE_MASK)
|
---|
98 | {
|
---|
99 | case DBGFADDRESS_FLAGS_FAR16: cbStackItem = 2; break;
|
---|
100 | case DBGFADDRESS_FLAGS_FAR32: cbStackItem = 4; break;
|
---|
101 | case DBGFADDRESS_FLAGS_FAR64: cbStackItem = 8; break;
|
---|
102 | case DBGFADDRESS_FLAGS_RING0: cbStackItem = sizeof(RTHCUINTPTR); break;
|
---|
103 | default: cbStackItem = 4; break; /// @todo 64-bit guests.
|
---|
104 | }
|
---|
105 |
|
---|
106 | union
|
---|
107 | {
|
---|
108 | uint64_t *pu64;
|
---|
109 | uint32_t *pu32;
|
---|
110 | uint16_t *pu16;
|
---|
111 | uint8_t *pb;
|
---|
112 | void *pv;
|
---|
113 | } u, uRet, uArgs, uBp;
|
---|
114 | size_t cbRead = cbRetAddr + cbStackItem + sizeof(pFrame->Args);
|
---|
115 | u.pv = alloca(cbRead);
|
---|
116 | uBp = u;
|
---|
117 | uRet.pb = u.pb + cbStackItem;
|
---|
118 | uArgs.pb = u.pb + cbStackItem + cbRetAddr;
|
---|
119 |
|
---|
120 | Assert(DBGFADDRESS_IS_VALID(&pFrame->AddrFrame));
|
---|
121 | int rc = dbgfR3Read(pVM, idCpu, u.pv,
|
---|
122 | pFrame->fFlags & DBGFSTACKFRAME_FLAGS_ALL_VALID
|
---|
123 | ? &pFrame->AddrReturnFrame
|
---|
124 | : &pFrame->AddrFrame,
|
---|
125 | cbRead, &cbRead);
|
---|
126 | if ( RT_FAILURE(rc)
|
---|
127 | || cbRead < cbRetAddr + cbStackItem)
|
---|
128 | pFrame->fFlags |= DBGFSTACKFRAME_FLAGS_LAST;
|
---|
129 |
|
---|
130 | /*
|
---|
131 | * The first step is taken in a different way than the others.
|
---|
132 | */
|
---|
133 | if (!(pFrame->fFlags & DBGFSTACKFRAME_FLAGS_ALL_VALID))
|
---|
134 | {
|
---|
135 | pFrame->fFlags |= DBGFSTACKFRAME_FLAGS_ALL_VALID;
|
---|
136 | pFrame->iFrame = 0;
|
---|
137 |
|
---|
138 | /* Current PC - set by caller, just find symbol & line. */
|
---|
139 | if (DBGFADDRESS_IS_VALID(&pFrame->AddrPC))
|
---|
140 | {
|
---|
141 | pFrame->pSymPC = DBGFR3SymbolByAddrAlloc(pVM, pFrame->AddrPC.FlatPtr, NULL);
|
---|
142 | pFrame->pLinePC = DBGFR3LineByAddrAlloc(pVM, pFrame->AddrPC.FlatPtr, NULL);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | else /* 2nd and subsequent steps */
|
---|
146 | {
|
---|
147 | /* frame, pc and stack is taken from the existing frames return members. */
|
---|
148 | pFrame->AddrFrame = pFrame->AddrReturnFrame;
|
---|
149 | pFrame->AddrPC = pFrame->AddrReturnPC;
|
---|
150 | pFrame->pSymPC = pFrame->pSymReturnPC;
|
---|
151 | pFrame->pLinePC = pFrame->pLineReturnPC;
|
---|
152 |
|
---|
153 | /* increment the frame number. */
|
---|
154 | pFrame->iFrame++;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Return Frame address.
|
---|
159 | */
|
---|
160 | pFrame->AddrReturnFrame = pFrame->AddrFrame;
|
---|
161 | switch (cbStackItem)
|
---|
162 | {
|
---|
163 | case 2: pFrame->AddrReturnFrame.off = *uBp.pu16; break;
|
---|
164 | case 4: pFrame->AddrReturnFrame.off = *uBp.pu32; break;
|
---|
165 | case 8: pFrame->AddrReturnFrame.off = *uBp.pu64; break;
|
---|
166 | default: AssertMsgFailed(("cbStackItem=%d\n", cbStackItem)); return VERR_INTERNAL_ERROR;
|
---|
167 | }
|
---|
168 | pFrame->AddrReturnFrame.FlatPtr += pFrame->AddrReturnFrame.off - pFrame->AddrFrame.off;
|
---|
169 |
|
---|
170 | /*
|
---|
171 | * Return PC and Stack Addresses.
|
---|
172 | */
|
---|
173 | /** @todo AddrReturnStack is not correct for stdcall and pascal. (requires scope info) */
|
---|
174 | pFrame->AddrReturnStack = pFrame->AddrFrame;
|
---|
175 | pFrame->AddrReturnStack.off += cbStackItem + cbRetAddr;
|
---|
176 | pFrame->AddrReturnStack.FlatPtr += cbStackItem + cbRetAddr;
|
---|
177 |
|
---|
178 | pFrame->AddrReturnPC = pFrame->AddrPC;
|
---|
179 | switch (pFrame->enmReturnType)
|
---|
180 | {
|
---|
181 | case DBGFRETURNTYPE_NEAR16:
|
---|
182 | if (DBGFADDRESS_IS_VALID(&pFrame->AddrReturnPC))
|
---|
183 | {
|
---|
184 | pFrame->AddrReturnPC.FlatPtr += *uRet.pu16 - pFrame->AddrReturnPC.off;
|
---|
185 | pFrame->AddrReturnPC.off = *uRet.pu16;
|
---|
186 | }
|
---|
187 | else
|
---|
188 | DBGFR3AddrFromFlat(pVM, &pFrame->AddrReturnPC, *uRet.pu16);
|
---|
189 | break;
|
---|
190 | case DBGFRETURNTYPE_NEAR32:
|
---|
191 | if (DBGFADDRESS_IS_VALID(&pFrame->AddrReturnPC))
|
---|
192 | {
|
---|
193 | pFrame->AddrReturnPC.FlatPtr += *uRet.pu32 - pFrame->AddrReturnPC.off;
|
---|
194 | pFrame->AddrReturnPC.off = *uRet.pu32;
|
---|
195 | }
|
---|
196 | else
|
---|
197 | DBGFR3AddrFromFlat(pVM, &pFrame->AddrReturnPC, *uRet.pu32);
|
---|
198 | break;
|
---|
199 | case DBGFRETURNTYPE_NEAR64:
|
---|
200 | if (DBGFADDRESS_IS_VALID(&pFrame->AddrReturnPC))
|
---|
201 | {
|
---|
202 | pFrame->AddrReturnPC.FlatPtr += *uRet.pu64 - pFrame->AddrReturnPC.off;
|
---|
203 | pFrame->AddrReturnPC.off = *uRet.pu64;
|
---|
204 | }
|
---|
205 | else
|
---|
206 | DBGFR3AddrFromFlat(pVM, &pFrame->AddrReturnPC, *uRet.pu64);
|
---|
207 | break;
|
---|
208 | case DBGFRETURNTYPE_FAR16:
|
---|
209 | DBGFR3AddrFromSelOff(pVM, idCpu, &pFrame->AddrReturnPC, uRet.pu16[1], uRet.pu16[0]);
|
---|
210 | break;
|
---|
211 | case DBGFRETURNTYPE_FAR32:
|
---|
212 | DBGFR3AddrFromSelOff(pVM, idCpu, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
|
---|
213 | break;
|
---|
214 | case DBGFRETURNTYPE_FAR64:
|
---|
215 | DBGFR3AddrFromSelOff(pVM, idCpu, &pFrame->AddrReturnPC, uRet.pu16[4], uRet.pu64[0]);
|
---|
216 | break;
|
---|
217 | case DBGFRETURNTYPE_IRET16:
|
---|
218 | DBGFR3AddrFromSelOff(pVM, idCpu, &pFrame->AddrReturnPC, uRet.pu16[1], uRet.pu16[0]);
|
---|
219 | break;
|
---|
220 | case DBGFRETURNTYPE_IRET32:
|
---|
221 | DBGFR3AddrFromSelOff(pVM, idCpu, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
|
---|
222 | break;
|
---|
223 | case DBGFRETURNTYPE_IRET32_PRIV:
|
---|
224 | DBGFR3AddrFromSelOff(pVM, idCpu, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
|
---|
225 | break;
|
---|
226 | case DBGFRETURNTYPE_IRET32_V86:
|
---|
227 | DBGFR3AddrFromSelOff(pVM, idCpu, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
|
---|
228 | break;
|
---|
229 | case DBGFRETURNTYPE_IRET64:
|
---|
230 | DBGFR3AddrFromSelOff(pVM, idCpu, &pFrame->AddrReturnPC, uRet.pu16[4], uRet.pu64[0]);
|
---|
231 | break;
|
---|
232 | default:
|
---|
233 | AssertMsgFailed(("enmReturnType=%d\n", pFrame->enmReturnType));
|
---|
234 | return VERR_INVALID_PARAMETER;
|
---|
235 | }
|
---|
236 |
|
---|
237 | pFrame->pSymReturnPC = DBGFR3SymbolByAddrAlloc(pVM, pFrame->AddrReturnPC.FlatPtr, NULL);
|
---|
238 | pFrame->pLineReturnPC = DBGFR3LineByAddrAlloc(pVM, pFrame->AddrReturnPC.FlatPtr, NULL);
|
---|
239 |
|
---|
240 | /*
|
---|
241 | * The arguments.
|
---|
242 | */
|
---|
243 | memcpy(&pFrame->Args, uArgs.pv, sizeof(pFrame->Args));
|
---|
244 |
|
---|
245 | return VINF_SUCCESS;
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Walks the entire stack allocating memory as we walk.
|
---|
251 | */
|
---|
252 | static DECLCALLBACK(int) dbgfR3StackWalkCtxFull(PVM pVM, VMCPUID idCpu, PCCPUMCTXCORE pCtxCore,
|
---|
253 | DBGFCODETYPE enmCodeType,
|
---|
254 | PCDBGFADDRESS pAddrFrame,
|
---|
255 | PCDBGFADDRESS pAddrStack,
|
---|
256 | PCDBGFADDRESS pAddrPC,
|
---|
257 | DBGFRETURNTYPE enmReturnType,
|
---|
258 | PCDBGFSTACKFRAME *ppFirstFrame)
|
---|
259 | {
|
---|
260 | /* alloc first frame. */
|
---|
261 | PDBGFSTACKFRAME pCur = (PDBGFSTACKFRAME)MMR3HeapAllocZ(pVM, MM_TAG_DBGF_STACK, sizeof(*pCur));
|
---|
262 | if (!pCur)
|
---|
263 | return VERR_NO_MEMORY;
|
---|
264 |
|
---|
265 | /*
|
---|
266 | * Initialize the frame.
|
---|
267 | */
|
---|
268 | pCur->pNextInternal = NULL;
|
---|
269 | pCur->pFirstInternal = pCur;
|
---|
270 |
|
---|
271 | int rc = VINF_SUCCESS;
|
---|
272 | if (pAddrPC)
|
---|
273 | pCur->AddrPC = *pAddrPC;
|
---|
274 | else
|
---|
275 | rc = DBGFR3AddrFromSelOff(pVM, idCpu, &pCur->AddrPC, pCtxCore->cs, pCtxCore->rip);
|
---|
276 | if (RT_SUCCESS(rc))
|
---|
277 | {
|
---|
278 | if (enmReturnType == DBGFRETURNTYPE_INVALID)
|
---|
279 | switch (pCur->AddrPC.fFlags & DBGFADDRESS_FLAGS_TYPE_MASK)
|
---|
280 | {
|
---|
281 | case DBGFADDRESS_FLAGS_FAR16: pCur->enmReturnType = DBGFRETURNTYPE_NEAR16; break;
|
---|
282 | case DBGFADDRESS_FLAGS_FAR32: pCur->enmReturnType = DBGFRETURNTYPE_NEAR32; break;
|
---|
283 | case DBGFADDRESS_FLAGS_FAR64: pCur->enmReturnType = DBGFRETURNTYPE_NEAR64; break;
|
---|
284 | case DBGFADDRESS_FLAGS_RING0: pCur->enmReturnType = (HC_ARCH_BITS == 64) ? DBGFRETURNTYPE_NEAR64 : DBGFRETURNTYPE_NEAR32; break;
|
---|
285 | default: pCur->enmReturnType = DBGFRETURNTYPE_NEAR32; break; /// @todo 64-bit guests
|
---|
286 | }
|
---|
287 |
|
---|
288 | uint64_t fAddrMask = UINT64_MAX;
|
---|
289 | if (enmCodeType == DBGFCODETYPE_RING0)
|
---|
290 | fAddrMask = (HC_ARCH_BITS == 64) ? UINT64_MAX : UINT32_MAX;
|
---|
291 | else
|
---|
292 | if (enmCodeType == DBGFCODETYPE_HYPER)
|
---|
293 | fAddrMask = UINT32_MAX;
|
---|
294 | else if (DBGFADDRESS_IS_FAR16(&pCur->AddrPC))
|
---|
295 | fAddrMask = UINT16_MAX;
|
---|
296 | else if (DBGFADDRESS_IS_FAR32(&pCur->AddrPC))
|
---|
297 | fAddrMask = UINT32_MAX;
|
---|
298 | else if (DBGFADDRESS_IS_FLAT(&pCur->AddrPC))
|
---|
299 | {
|
---|
300 | CPUMMODE CpuMode = CPUMGetGuestMode(VMMGetCpuById(pVM, idCpu));
|
---|
301 | if (CpuMode == CPUMMODE_REAL)
|
---|
302 | fAddrMask = UINT16_MAX;
|
---|
303 | else if (CpuMode == CPUMMODE_PROTECTED)
|
---|
304 | fAddrMask = UINT32_MAX;
|
---|
305 | }
|
---|
306 |
|
---|
307 | if (pAddrStack)
|
---|
308 | pCur->AddrStack = *pAddrStack;
|
---|
309 | else
|
---|
310 | rc = DBGFR3AddrFromSelOff(pVM, idCpu, &pCur->AddrStack, pCtxCore->ss, pCtxCore->rsp & fAddrMask);
|
---|
311 |
|
---|
312 | if (pAddrFrame)
|
---|
313 | pCur->AddrFrame = *pAddrFrame;
|
---|
314 | else if (RT_SUCCESS(rc))
|
---|
315 | rc = DBGFR3AddrFromSelOff(pVM, idCpu, &pCur->AddrFrame, pCtxCore->ss, pCtxCore->rbp & fAddrMask);
|
---|
316 | }
|
---|
317 | else
|
---|
318 | pCur->enmReturnType = enmReturnType;
|
---|
319 |
|
---|
320 | /*
|
---|
321 | * The first frame.
|
---|
322 | */
|
---|
323 | if (RT_SUCCESS(rc))
|
---|
324 | rc = dbgfR3StackWalk(pVM, idCpu, pCur);
|
---|
325 | if (RT_FAILURE(rc))
|
---|
326 | {
|
---|
327 | DBGFR3StackWalkEnd(pCur);
|
---|
328 | return rc;
|
---|
329 | }
|
---|
330 |
|
---|
331 | /*
|
---|
332 | * The other frames.
|
---|
333 | */
|
---|
334 | DBGFSTACKFRAME Next = *pCur;
|
---|
335 | while (!(pCur->fFlags & (DBGFSTACKFRAME_FLAGS_LAST | DBGFSTACKFRAME_FLAGS_MAX_DEPTH | DBGFSTACKFRAME_FLAGS_LOOP)))
|
---|
336 | {
|
---|
337 | /* try walk. */
|
---|
338 | rc = dbgfR3StackWalk(pVM, idCpu, &Next);
|
---|
339 | if (RT_FAILURE(rc))
|
---|
340 | break;
|
---|
341 |
|
---|
342 | /* add the next frame to the chain. */
|
---|
343 | PDBGFSTACKFRAME pNext = (PDBGFSTACKFRAME)MMR3HeapAlloc(pVM, MM_TAG_DBGF_STACK, sizeof(*pNext));
|
---|
344 | if (!pNext)
|
---|
345 | {
|
---|
346 | DBGFR3StackWalkEnd(pCur);
|
---|
347 | return VERR_NO_MEMORY;
|
---|
348 | }
|
---|
349 | *pNext = Next;
|
---|
350 | pCur->pNextInternal = pNext;
|
---|
351 | pCur = pNext;
|
---|
352 | Assert(pCur->pNextInternal == NULL);
|
---|
353 |
|
---|
354 | /* check for loop */
|
---|
355 | for (PCDBGFSTACKFRAME pLoop = pCur->pFirstInternal;
|
---|
356 | pLoop && pLoop != pCur;
|
---|
357 | pLoop = pLoop->pNextInternal)
|
---|
358 | if (pLoop->AddrFrame.FlatPtr == pCur->AddrFrame.FlatPtr)
|
---|
359 | {
|
---|
360 | pCur->fFlags |= DBGFSTACKFRAME_FLAGS_LOOP;
|
---|
361 | break;
|
---|
362 | }
|
---|
363 |
|
---|
364 | /* check for insane recursion */
|
---|
365 | if (pCur->iFrame >= 2048)
|
---|
366 | pCur->fFlags |= DBGFSTACKFRAME_FLAGS_MAX_DEPTH;
|
---|
367 | }
|
---|
368 |
|
---|
369 | *ppFirstFrame = pCur->pFirstInternal;
|
---|
370 | return rc;
|
---|
371 | }
|
---|
372 |
|
---|
373 |
|
---|
374 | /**
|
---|
375 | * Common worker for DBGFR3StackWalkBeginGuestEx, DBGFR3StackWalkBeginHyperEx,
|
---|
376 | * DBGFR3StackWalkBeginGuest and DBGFR3StackWalkBeginHyper.
|
---|
377 | */
|
---|
378 | static int dbgfR3StackWalkBeginCommon(PVM pVM,
|
---|
379 | VMCPUID idCpu,
|
---|
380 | DBGFCODETYPE enmCodeType,
|
---|
381 | PCDBGFADDRESS pAddrFrame,
|
---|
382 | PCDBGFADDRESS pAddrStack,
|
---|
383 | PCDBGFADDRESS pAddrPC,
|
---|
384 | DBGFRETURNTYPE enmReturnType,
|
---|
385 | PCDBGFSTACKFRAME *ppFirstFrame)
|
---|
386 | {
|
---|
387 | #if HC_ARCH_BITS == 64
|
---|
388 | /** @todo Not implemented for 64 bits hosts yet */
|
---|
389 | if (enmCodeType == DBGFCODETYPE_RING0)
|
---|
390 | return VINF_SUCCESS;
|
---|
391 | #endif
|
---|
392 | /*
|
---|
393 | * Validate parameters.
|
---|
394 | */
|
---|
395 | *ppFirstFrame = NULL;
|
---|
396 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
397 | AssertReturn(idCpu < pVM->cCPUs, VERR_INVALID_CPU_ID);
|
---|
398 | if (pAddrFrame)
|
---|
399 | AssertReturn(DBGFR3AddrIsValid(pVM, pAddrFrame), VERR_INVALID_PARAMETER);
|
---|
400 | if (pAddrStack)
|
---|
401 | AssertReturn(DBGFR3AddrIsValid(pVM, pAddrStack), VERR_INVALID_PARAMETER);
|
---|
402 | if (pAddrPC)
|
---|
403 | AssertReturn(DBGFR3AddrIsValid(pVM, pAddrPC), VERR_INVALID_PARAMETER);
|
---|
404 | AssertReturn(enmReturnType >= DBGFRETURNTYPE_INVALID && enmReturnType < DBGFRETURNTYPE_END, VERR_INVALID_PARAMETER);
|
---|
405 |
|
---|
406 | /*
|
---|
407 | * Get the CPUM context pointer and pass it on the specified EMT.
|
---|
408 | */
|
---|
409 | PCCPUMCTXCORE pCtxCore;
|
---|
410 | switch (enmCodeType)
|
---|
411 | {
|
---|
412 | case DBGFCODETYPE_GUEST:
|
---|
413 | pCtxCore = CPUMGetGuestCtxCore(VMMGetCpuById(pVM, idCpu));
|
---|
414 | break;
|
---|
415 | case DBGFCODETYPE_HYPER:
|
---|
416 | pCtxCore = CPUMGetHyperCtxCore(VMMGetCpuById(pVM, idCpu));
|
---|
417 | break;
|
---|
418 | case DBGFCODETYPE_RING0:
|
---|
419 | pCtxCore = NULL; /* No valid context present. */
|
---|
420 | break;
|
---|
421 | default:
|
---|
422 | AssertFailed();
|
---|
423 | }
|
---|
424 | PVMREQ pReq;
|
---|
425 | int rc = VMR3ReqCall(pVM, idCpu, &pReq, RT_INDEFINITE_WAIT,
|
---|
426 | (PFNRT)dbgfR3StackWalkCtxFull, 9,
|
---|
427 | pVM, idCpu, pCtxCore, enmCodeType,
|
---|
428 | pAddrFrame, pAddrStack, pAddrPC, enmReturnType, ppFirstFrame);
|
---|
429 | if (RT_SUCCESS(rc))
|
---|
430 | rc = pReq->iStatus;
|
---|
431 | VMR3ReqFree(pReq);
|
---|
432 |
|
---|
433 | return rc;
|
---|
434 |
|
---|
435 | }
|
---|
436 |
|
---|
437 |
|
---|
438 | /**
|
---|
439 | * Begins a guest stack walk, extended version.
|
---|
440 | *
|
---|
441 | * This will walk the current stack, constructing a list of info frames which is
|
---|
442 | * returned to the caller. The caller uses DBGFR3StackWalkNext to traverse the
|
---|
443 | * list and DBGFR3StackWalkEnd to release it.
|
---|
444 | *
|
---|
445 | * @returns VINF_SUCCESS on success.
|
---|
446 | * @returns VERR_NO_MEMORY if we're out of memory.
|
---|
447 | *
|
---|
448 | * @param pVM The VM handle.
|
---|
449 | * @param idCpu The ID of the virtual CPU which stack we want to walk.
|
---|
450 | * @param enmCodeType Code type
|
---|
451 | * @param pAddrFrame Frame address to start at. (Optional)
|
---|
452 | * @param pAddrStack Stack address to start at. (Optional)
|
---|
453 | * @param pAddrPC Program counter to start at. (Optional)
|
---|
454 | * @param enmReturnType The return address type. (Optional)
|
---|
455 | * @param ppFirstFrame Where to return the pointer to the first info frame.
|
---|
456 | */
|
---|
457 | VMMR3DECL(int) DBGFR3StackWalkBeginEx(PVM pVM,
|
---|
458 | VMCPUID idCpu,
|
---|
459 | DBGFCODETYPE enmCodeType,
|
---|
460 | PCDBGFADDRESS pAddrFrame,
|
---|
461 | PCDBGFADDRESS pAddrStack,
|
---|
462 | PCDBGFADDRESS pAddrPC,
|
---|
463 | DBGFRETURNTYPE enmReturnType,
|
---|
464 | PCDBGFSTACKFRAME *ppFirstFrame)
|
---|
465 | {
|
---|
466 | return dbgfR3StackWalkBeginCommon(pVM, idCpu, enmCodeType, pAddrFrame, pAddrStack, pAddrPC, enmReturnType, ppFirstFrame);
|
---|
467 | }
|
---|
468 |
|
---|
469 |
|
---|
470 | /**
|
---|
471 | * Begins a guest stack walk.
|
---|
472 | *
|
---|
473 | * This will walk the current stack, constructing a list of info frames which is
|
---|
474 | * returned to the caller. The caller uses DBGFR3StackWalkNext to traverse the
|
---|
475 | * list and DBGFR3StackWalkEnd to release it.
|
---|
476 | *
|
---|
477 | * @returns VINF_SUCCESS on success.
|
---|
478 | * @returns VERR_NO_MEMORY if we're out of memory.
|
---|
479 | *
|
---|
480 | * @param pVM The VM handle.
|
---|
481 | * @param idCpu The ID of the virtual CPU which stack we want to walk.
|
---|
482 | * @param enmCodeType Code type
|
---|
483 | * @param ppFirstFrame Where to return the pointer to the first info frame.
|
---|
484 | */
|
---|
485 | VMMR3DECL(int) DBGFR3StackWalkBegin(PVM pVM, VMCPUID idCpu, DBGFCODETYPE enmCodeType, PCDBGFSTACKFRAME *ppFirstFrame)
|
---|
486 | {
|
---|
487 | return dbgfR3StackWalkBeginCommon(pVM, idCpu, enmCodeType, NULL, NULL, NULL, DBGFRETURNTYPE_INVALID, ppFirstFrame);
|
---|
488 | }
|
---|
489 |
|
---|
490 | /**
|
---|
491 | * Gets the next stack frame.
|
---|
492 | *
|
---|
493 | * @returns Pointer to the info for the next stack frame.
|
---|
494 | * NULL if no more frames.
|
---|
495 | *
|
---|
496 | * @param pCurrent Pointer to the current stack frame.
|
---|
497 | *
|
---|
498 | */
|
---|
499 | VMMR3DECL(PCDBGFSTACKFRAME) DBGFR3StackWalkNext(PCDBGFSTACKFRAME pCurrent)
|
---|
500 | {
|
---|
501 | return pCurrent
|
---|
502 | ? pCurrent->pNextInternal
|
---|
503 | : NULL;
|
---|
504 | }
|
---|
505 |
|
---|
506 |
|
---|
507 | /**
|
---|
508 | * Ends a stack walk process.
|
---|
509 | *
|
---|
510 | * This *must* be called after a successful first call to any of the stack
|
---|
511 | * walker functions. If not called we will leak memory or other resources.
|
---|
512 | *
|
---|
513 | * @param pFirstFrame The frame returned by one of the the begin
|
---|
514 | * functions.
|
---|
515 | */
|
---|
516 | VMMR3DECL(void) DBGFR3StackWalkEnd(PCDBGFSTACKFRAME pFirstFrame)
|
---|
517 | {
|
---|
518 | if ( !pFirstFrame
|
---|
519 | || !pFirstFrame->pFirstInternal)
|
---|
520 | return;
|
---|
521 |
|
---|
522 | PDBGFSTACKFRAME pFrame = (PDBGFSTACKFRAME)pFirstFrame->pFirstInternal;
|
---|
523 | while (pFrame)
|
---|
524 | {
|
---|
525 | PDBGFSTACKFRAME pCur = pFrame;
|
---|
526 | pFrame = (PDBGFSTACKFRAME)pCur->pNextInternal;
|
---|
527 | if (pFrame)
|
---|
528 | {
|
---|
529 | if (pCur->pSymReturnPC == pFrame->pSymPC)
|
---|
530 | pFrame->pSymPC = NULL;
|
---|
531 | if (pCur->pSymReturnPC == pFrame->pSymReturnPC)
|
---|
532 | pFrame->pSymReturnPC = NULL;
|
---|
533 |
|
---|
534 | if (pCur->pSymPC == pFrame->pSymPC)
|
---|
535 | pFrame->pSymPC = NULL;
|
---|
536 | if (pCur->pSymPC == pFrame->pSymReturnPC)
|
---|
537 | pFrame->pSymReturnPC = NULL;
|
---|
538 |
|
---|
539 | if (pCur->pLineReturnPC == pFrame->pLinePC)
|
---|
540 | pFrame->pLinePC = NULL;
|
---|
541 | if (pCur->pLineReturnPC == pFrame->pLineReturnPC)
|
---|
542 | pFrame->pLineReturnPC = NULL;
|
---|
543 |
|
---|
544 | if (pCur->pLinePC == pFrame->pLinePC)
|
---|
545 | pFrame->pLinePC = NULL;
|
---|
546 | if (pCur->pLinePC == pFrame->pLineReturnPC)
|
---|
547 | pFrame->pLineReturnPC = NULL;
|
---|
548 | }
|
---|
549 |
|
---|
550 | DBGFR3SymbolFree(pCur->pSymPC);
|
---|
551 | DBGFR3SymbolFree(pCur->pSymReturnPC);
|
---|
552 | DBGFR3LineFree(pCur->pLinePC);
|
---|
553 | DBGFR3LineFree(pCur->pLineReturnPC);
|
---|
554 |
|
---|
555 | pCur->pNextInternal = NULL;
|
---|
556 | pCur->pFirstInternal = NULL;
|
---|
557 | pCur->fFlags = 0;
|
---|
558 | MMR3HeapFree(pCur);
|
---|
559 | }
|
---|
560 | }
|
---|
561 |
|
---|