VirtualBox

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

Last change on this file since 11985 was 8155, checked in by vboxsync, 17 years ago

The Big Sun Rebranding Header Change

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.8 KB
Line 
1/* $Id: DBGFStack.cpp 8155 2008-04-18 15:16:47Z vboxsync $ */
2/** @file
3 * VMM 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 "DBGFInternal.h"
30#include <VBox/vm.h>
31#include <VBox/mm.h>
32#include <VBox/err.h>
33#include <VBox/log.h>
34#include <iprt/assert.h>
35#include <iprt/string.h>
36#include <iprt/alloca.h>
37
38
39
40/**
41 * Read stack memory.
42 */
43DECLINLINE(int) dbgfR3Read(PVM pVM, void *pvBuf, RTGCUINTPTR GCPtr, size_t cb, size_t *pcbRead)
44{
45 int rc = MMR3ReadGCVirt(pVM, pvBuf, GCPtr, cb);
46 if (VBOX_FAILURE(rc))
47 {
48 size_t cbRead;
49 for (cbRead = 0; cbRead < cb; cbRead++)
50 {
51 rc = MMR3ReadGCVirt(pVM, (uint8_t *)pvBuf + cbRead, GCPtr + cbRead, 1);
52 if (VBOX_FAILURE(rc))
53 break;
54 }
55 if (cbRead)
56 rc = VINF_SUCCESS;
57 memset((char *)pvBuf + cbRead, 0, cb - cbRead);
58 *pcbRead = cbRead;
59 }
60 else
61 *pcbRead = cb;
62 return rc;
63}
64
65
66
67
68/**
69 * Internal worker routine.
70 *
71 * On x86 the typical stack frame layout is like this:
72 * .. ..
73 * 16 parameter 2
74 * 12 parameter 1
75 * 8 parameter 0
76 * 4 return address
77 * 0 old ebp; current ebp points here
78 */
79static int dbgfR3StackWalk(PVM pVM, PDBGFSTACKFRAME pFrame)
80{
81 /*
82 * Stop if we got a read error in the previous run.
83 */
84 if (pFrame->fFlags & DBGFSTACKFRAME_FLAGS_LAST)
85 return VERR_NO_MORE_FILES;
86
87 /*
88 * Read the raw frame data.
89 */
90 const DBGFADDRESS AddrOldPC = pFrame->AddrPC;
91 const unsigned cbRetAddr = DBGFReturnTypeSize(pFrame->enmReturnType);
92 unsigned cbStackItem;
93 switch (AddrOldPC.fFlags & DBGFADDRESS_FLAGS_TYPE_MASK)
94 {
95 case DBGFADDRESS_FLAGS_FAR16: cbStackItem = 2; break;
96 case DBGFADDRESS_FLAGS_FAR32: cbStackItem = 4; break;
97 case DBGFADDRESS_FLAGS_FAR64: cbStackItem = 8; break;
98 default: cbStackItem = 4; break; /// @todo 64-bit guests.
99 }
100
101 union
102 {
103 uint64_t *pu64;
104 uint32_t *pu32;
105 uint16_t *pu16;
106 uint8_t *pb;
107 void *pv;
108 } u, uRet, uArgs, uBp;
109 size_t cbRead = cbRetAddr + cbStackItem + sizeof(pFrame->Args);
110 u.pv = alloca(cbRead);
111 uBp = u;
112 uRet.pb = u.pb + cbStackItem;
113 uArgs.pb = u.pb + cbStackItem + cbRetAddr;
114
115 Assert(DBGFADDRESS_IS_VALID(&pFrame->AddrFrame));
116 int rc = dbgfR3Read(pVM, u.pv,
117 pFrame->fFlags & DBGFSTACKFRAME_FLAGS_ALL_VALID
118 ? pFrame->AddrReturnFrame.FlatPtr
119 : pFrame->AddrFrame.FlatPtr,
120 cbRead, &cbRead);
121 if ( VBOX_FAILURE(rc)
122 || cbRead < cbRetAddr + cbStackItem)
123 pFrame->fFlags |= DBGFSTACKFRAME_FLAGS_LAST;
124
125 /*
126 * The first step is taken in a different way than the others.
127 */
128 if (!(pFrame->fFlags & DBGFSTACKFRAME_FLAGS_ALL_VALID))
129 {
130 pFrame->fFlags |= DBGFSTACKFRAME_FLAGS_ALL_VALID;
131 pFrame->iFrame = 0;
132
133 /* Current PC - set by caller, just find symbol & line. */
134 if (DBGFADDRESS_IS_VALID(&pFrame->AddrPC))
135 {
136 pFrame->pSymPC = DBGFR3SymbolByAddrAlloc(pVM, pFrame->AddrPC.FlatPtr, NULL);
137 pFrame->pLinePC = DBGFR3LineByAddrAlloc(pVM, pFrame->AddrPC.FlatPtr, NULL);
138 }
139 }
140 else /* 2nd and subsequent steps */
141 {
142 /* frame, pc and stack is taken from the existing frames return members. */
143 pFrame->AddrFrame = pFrame->AddrReturnFrame;
144 pFrame->AddrPC = pFrame->AddrReturnPC;
145 pFrame->pSymPC = pFrame->pSymReturnPC;
146 pFrame->pLinePC = pFrame->pLineReturnPC;
147
148 /* increment the frame number. */
149 pFrame->iFrame++;
150 }
151
152 /*
153 * Return Frame address.
154 */
155 pFrame->AddrReturnFrame = pFrame->AddrFrame;
156 switch (cbStackItem)
157 {
158 case 2: pFrame->AddrReturnFrame.off = *uBp.pu16; break;
159 case 4: pFrame->AddrReturnFrame.off = *uBp.pu32; break;
160 case 8: pFrame->AddrReturnFrame.off = *uBp.pu64; break;
161 default: AssertMsgFailed(("cbStackItem=%d\n", cbStackItem)); return VERR_INTERNAL_ERROR;
162 }
163 pFrame->AddrReturnFrame.FlatPtr += pFrame->AddrReturnFrame.off - pFrame->AddrFrame.off;
164
165 /*
166 * Return PC and Stack Addresses.
167 */
168 /** @todo AddrReturnStack is not correct for stdcall and pascal. (requires scope info) */
169 pFrame->AddrReturnStack = pFrame->AddrFrame;
170 pFrame->AddrReturnStack.off += cbStackItem + cbRetAddr;
171 pFrame->AddrReturnStack.FlatPtr += cbStackItem + cbRetAddr;
172
173 pFrame->AddrReturnPC = pFrame->AddrPC;
174 switch (pFrame->enmReturnType)
175 {
176 case DBGFRETURNTYPE_NEAR16:
177 if (DBGFADDRESS_IS_VALID(&pFrame->AddrReturnPC))
178 {
179 pFrame->AddrReturnPC.FlatPtr += *uRet.pu16 - pFrame->AddrReturnPC.off;
180 pFrame->AddrReturnPC.off = *uRet.pu16;
181 }
182 else
183 DBGFR3AddrFromFlat(pVM, &pFrame->AddrReturnPC, *uRet.pu16);
184 break;
185 case DBGFRETURNTYPE_NEAR32:
186 if (DBGFADDRESS_IS_VALID(&pFrame->AddrReturnPC))
187 {
188 pFrame->AddrReturnPC.FlatPtr += *uRet.pu32 - pFrame->AddrReturnPC.off;
189 pFrame->AddrReturnPC.off = *uRet.pu32;
190 }
191 else
192 DBGFR3AddrFromFlat(pVM, &pFrame->AddrReturnPC, *uRet.pu32);
193 break;
194 case DBGFRETURNTYPE_NEAR64:
195 if (DBGFADDRESS_IS_VALID(&pFrame->AddrReturnPC))
196 {
197 pFrame->AddrReturnPC.FlatPtr += *uRet.pu64 - pFrame->AddrReturnPC.off;
198 pFrame->AddrReturnPC.off = *uRet.pu64;
199 }
200 else
201 DBGFR3AddrFromFlat(pVM, &pFrame->AddrReturnPC, *uRet.pu64);
202 break;
203 case DBGFRETURNTYPE_FAR16:
204 DBGFR3AddrFromSelOff(pVM, &pFrame->AddrReturnPC, uRet.pu16[1], uRet.pu16[0]);
205 break;
206 case DBGFRETURNTYPE_FAR32:
207 DBGFR3AddrFromSelOff(pVM, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
208 break;
209 case DBGFRETURNTYPE_FAR64:
210 DBGFR3AddrFromSelOff(pVM, &pFrame->AddrReturnPC, uRet.pu16[4], uRet.pu64[0]);
211 break;
212 case DBGFRETURNTYPE_IRET16:
213 DBGFR3AddrFromSelOff(pVM, &pFrame->AddrReturnPC, uRet.pu16[1], uRet.pu16[0]);
214 break;
215 case DBGFRETURNTYPE_IRET32:
216 DBGFR3AddrFromSelOff(pVM, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
217 break;
218 case DBGFRETURNTYPE_IRET32_PRIV:
219 DBGFR3AddrFromSelOff(pVM, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
220 break;
221 case DBGFRETURNTYPE_IRET32_V86:
222 DBGFR3AddrFromSelOff(pVM, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
223 break;
224 case DBGFRETURNTYPE_IRET64:
225 DBGFR3AddrFromSelOff(pVM, &pFrame->AddrReturnPC, uRet.pu16[4], uRet.pu64[0]);
226 break;
227 default:
228 AssertMsgFailed(("enmReturnType=%d\n", pFrame->enmReturnType));
229 return VERR_INVALID_PARAMETER;
230 }
231
232 pFrame->pSymReturnPC = DBGFR3SymbolByAddrAlloc(pVM, pFrame->AddrReturnPC.FlatPtr, NULL);
233 pFrame->pLineReturnPC = DBGFR3LineByAddrAlloc(pVM, pFrame->AddrReturnPC.FlatPtr, NULL);
234
235 /*
236 * The arguments.
237 */
238 memcpy(&pFrame->Args, uArgs.pv, sizeof(pFrame->Args));
239
240 return VINF_SUCCESS;
241}
242
243
244/**
245 * Walks the entire stack allocating memory as we walk.
246 */
247static DECLCALLBACK(int) dbgfR3StackWalkCtxFull(PVM pVM, PDBGFSTACKFRAME pFrame, PCCPUMCTXCORE pCtxCore, bool fGuest)
248{
249 pFrame->pNext = NULL;
250 pFrame->pFirst = NULL;
251
252 /* alloc first frame. */
253 PDBGFSTACKFRAME pCur = (PDBGFSTACKFRAME)MMR3HeapAllocZ(pVM, MM_TAG_DBGF_STACK, sizeof(*pCur));
254 if (!pCur)
255 return VERR_NO_MEMORY;
256
257 /* copy input frame */
258 pCur->AddrFrame = pFrame->AddrFrame;
259 pCur->AddrStack = pFrame->AddrStack;
260 pCur->AddrPC = pFrame->AddrPC;
261 pCur->enmReturnType = pFrame->enmReturnType;
262 pCur->pNext = NULL;
263 pCur->pFirst = pCur;
264
265 int rc = VINF_SUCCESS;
266 if (!DBGFADDRESS_IS_VALID(&pCur->AddrPC))
267 rc = DBGFR3AddrFromSelOff(pVM, &pCur->AddrPC, pCtxCore->cs, pCtxCore->eip);
268 if (VBOX_SUCCESS(rc) /*&& pCur->enmReturnType == DBGFRETURNTYPE_INVALID*/)
269 {
270 switch (pCur->AddrPC.fFlags & DBGFADDRESS_FLAGS_TYPE_MASK)
271 {
272 case DBGFADDRESS_FLAGS_FAR16: pCur->enmReturnType = DBGFRETURNTYPE_NEAR16; break;
273 case DBGFADDRESS_FLAGS_FAR32: pCur->enmReturnType = DBGFRETURNTYPE_NEAR32; break;
274 case DBGFADDRESS_FLAGS_FAR64: pCur->enmReturnType = DBGFRETURNTYPE_NEAR64; break;
275 default: pCur->enmReturnType = DBGFRETURNTYPE_NEAR32; break; /// @todo 64-bit guests
276 }
277 }
278 uint64_t u64Mask = UINT64_MAX;
279 if (VBOX_SUCCESS(rc) && DBGFADDRESS_IS_FAR16(&pCur->AddrPC) && fGuest)
280 u64Mask = UINT16_MAX;
281 if (VBOX_SUCCESS(rc) && !DBGFADDRESS_IS_VALID(&pCur->AddrStack))
282 rc = DBGFR3AddrFromSelOff(pVM, &pCur->AddrStack, pCtxCore->ss, pCtxCore->esp & u64Mask);
283 if (VBOX_SUCCESS(rc) && !DBGFADDRESS_IS_VALID(&pCur->AddrFrame))
284 rc = DBGFR3AddrFromSelOff(pVM, &pCur->AddrFrame, pCtxCore->ss, pCtxCore->ebp & u64Mask);
285
286 /*
287 * The first frame.
288 */
289 if (VBOX_SUCCESS(rc))
290 rc = dbgfR3StackWalk(pVM, pCur);
291 if (VBOX_FAILURE(rc))
292 {
293 DBGFR3StackWalkEnd(pVM, pCur);
294 return rc;
295 }
296
297 /*
298 * The other frames.
299 */
300 DBGFSTACKFRAME Next = *pCur;
301 while (!(pCur->fFlags & (DBGFSTACKFRAME_FLAGS_LAST | DBGFSTACKFRAME_FLAGS_MAX_DEPTH | DBGFSTACKFRAME_FLAGS_LOOP)))
302 {
303 /* try walk. */
304 rc = dbgfR3StackWalk(pVM, &Next);
305 if (VBOX_FAILURE(rc))
306 break;
307
308 /* add the next frame to the chain. */
309 PDBGFSTACKFRAME pNext = (PDBGFSTACKFRAME)MMR3HeapAlloc(pVM, MM_TAG_DBGF_STACK, sizeof(*pNext));
310 if (!pNext)
311 {
312 DBGFR3StackWalkEnd(pVM, pCur);
313 return VERR_NO_MEMORY;
314 }
315 *pNext = Next;
316 pCur = pCur->pNext = pNext;
317 Assert(pCur->pNext == NULL);
318
319 /* check for loop */
320 for (PDBGFSTACKFRAME pLoop = pCur->pFirst; pLoop && pLoop != pCur; pLoop = pLoop->pNext)
321 if (pLoop->AddrFrame.FlatPtr == pCur->AddrFrame.FlatPtr)
322 {
323 pCur->fFlags |= DBGFSTACKFRAME_FLAGS_LOOP;
324 break;
325 }
326
327 /* check for insane recursion */
328 if (pCur->iFrame >= 2048)
329 pCur->fFlags |= DBGFSTACKFRAME_FLAGS_MAX_DEPTH;
330 }
331
332 *pFrame = *pCur->pFirst;
333 return rc;
334}
335
336
337/**
338 * Begins a stack walk.
339 * This will construct and obtain the first frame.
340 *
341 * @returns VINF_SUCCESS on success.
342 * @returns VERR_NO_MEMORY if we're out of memory.
343 * @param pVM The VM handle.
344 * @param pFrame The stack frame info structure.
345 * On input this structure must be memset to zero.
346 * If wanted, the AddrPC, AddrStack and AddrFrame fields may be set
347 * to valid addresses after memsetting it. Any of those fields not set
348 * will be fetched from the guest CPU state.
349 * On output the structure will contain all the information we were able to
350 * obtain about the stack frame.
351 */
352DBGFR3DECL(int) DBGFR3StackWalkBeginGuest(PVM pVM, PDBGFSTACKFRAME pFrame)
353{
354 pFrame->pFirst = NULL;
355 pFrame->pNext = NULL;
356
357 PVMREQ pReq;
358 int rc = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)dbgfR3StackWalkCtxFull, 4,
359 pVM, pFrame, CPUMGetGuestCtxCore(pVM), true);
360 if (VBOX_SUCCESS(rc))
361 rc = pReq->iStatus;
362 VMR3ReqFree(pReq);
363
364 return rc;
365}
366
367
368/**
369 * Begins a stack walk.
370 * This will construct and obtain the first frame.
371 *
372 * @returns VINF_SUCCESS on success.
373 * @returns VERR_NO_MEMORY if we're out of memory.
374 * @param pVM The VM handle.
375 * @param pFrame The stack frame info structure.
376 * On input this structure must be memset to zero.
377 * If wanted, the AddrPC, AddrStack and AddrFrame fields may be set
378 * to valid addresses after memsetting it. Any of those fields not set
379 * will be fetched from the hypervisor CPU state.
380 * On output the structure will contain all the information we were able to
381 * obtain about the stack frame.
382 */
383DBGFR3DECL(int) DBGFR3StackWalkBeginHyper(PVM pVM, PDBGFSTACKFRAME pFrame)
384{
385 pFrame->pFirst = NULL;
386 pFrame->pNext = NULL;
387
388 PVMREQ pReq;
389 int rc = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)dbgfR3StackWalkCtxFull, 4,
390 pVM, pFrame, CPUMGetHyperCtxCore(pVM), 4);
391 if (VBOX_SUCCESS(rc))
392 rc = pReq->iStatus;
393 VMR3ReqFree(pReq);
394
395 return rc;
396}
397
398
399/**
400 * Gets the next stack frame.
401 *
402 * @returns VINF_SUCCESS
403 * @returns VERR_NO_MORE_FILES if not more stack frames.
404 * @param pVM The VM handle.
405 * @param pFrame Pointer to the current frame on input, content is replaced with the next frame on successful return.
406 */
407DBGFR3DECL(int) DBGFR3StackWalkNext(PVM pVM, PDBGFSTACKFRAME pFrame)
408{
409 if (pFrame->pNext)
410 {
411 *pFrame = *pFrame->pNext;
412 return VINF_SUCCESS;
413 }
414 return VERR_NO_MORE_FILES;
415}
416
417
418/**
419 * Ends a stack walk process.
420 *
421 * This *must* be called after a successful first call to any of the stack
422 * walker functions. If not called we will leak memory or other resources.
423 *
424 * @param pVM The VM handle.
425 * @param pFrame The stackframe as returned by the last stack walk call.
426 */
427DBGFR3DECL(void) DBGFR3StackWalkEnd(PVM pVM, PDBGFSTACKFRAME pFrame)
428{
429 if (!pFrame || !pFrame->pFirst)
430 return;
431
432 pFrame = pFrame->pFirst;
433 while (pFrame)
434 {
435 PDBGFSTACKFRAME pCur = pFrame;
436 pFrame = pCur->pNext;
437 if (pFrame)
438 {
439 if (pCur->pSymReturnPC == pFrame->pSymPC)
440 pFrame->pSymPC = NULL;
441 if (pCur->pSymReturnPC == pFrame->pSymReturnPC)
442 pFrame->pSymReturnPC = NULL;
443
444 if (pCur->pSymPC == pFrame->pSymPC)
445 pFrame->pSymPC = NULL;
446 if (pCur->pSymPC == pFrame->pSymReturnPC)
447 pFrame->pSymReturnPC = NULL;
448
449 if (pCur->pLineReturnPC == pFrame->pLinePC)
450 pFrame->pLinePC = NULL;
451 if (pCur->pLineReturnPC == pFrame->pLineReturnPC)
452 pFrame->pLineReturnPC = NULL;
453
454 if (pCur->pLinePC == pFrame->pLinePC)
455 pFrame->pLinePC = NULL;
456 if (pCur->pLinePC == pFrame->pLineReturnPC)
457 pFrame->pLineReturnPC = NULL;
458 }
459
460 DBGFR3SymbolFree(pCur->pSymPC);
461 DBGFR3SymbolFree(pCur->pSymReturnPC);
462 DBGFR3LineFree(pCur->pLinePC);
463 DBGFR3LineFree(pCur->pLineReturnPC);
464
465 pCur->pNext = NULL;
466 pCur->pFirst = NULL;
467 pCur->fFlags = 0;
468 MMR3HeapFree(pCur);
469 }
470}
471
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