DBGFStack.cpp revision c97989161fbe75bc14cea477a5443bbf474dd3ad
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * VMM DBGF - Debugger Facility, Call Stack Analyser.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * Copyright (C) 2006-2007 innotek GmbH
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * This file is part of VirtualBox Open Source Edition (OSE), as
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * available from http://www.virtualbox.org. This file is free software;
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * you can redistribute it and/or modify it under the terms of the GNU
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * General Public License as published by the Free Software Foundation,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * distribution. VirtualBox OSE is distributed in the hope that it will
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * be useful, but WITHOUT ANY WARRANTY of any kind.
94e25967cda41b886e33ec254b917d21df21a187Bob Halley * If you received this file as part of a commercial VirtualBox
28a8f5b0de57d269cf2845c69cb6abe18cbd3b3aMark Andrews * distribution, then only the terms of your commercial VirtualBox
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * license agreement apply instead of the previous paragraph.
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley/*******************************************************************************
94e25967cda41b886e33ec254b917d21df21a187Bob Halley* Header Files *
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence*******************************************************************************/
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Read stack memory.
6fa1cb5754695d550a58c6e8978fda65f5146af7David LawrenceDECLINLINE(int) dbgfR3Read(PVM pVM, void *pvBuf, RTGCUINTPTR GCPtr, size_t cb, size_t *pcbRead)
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence int rc = MMR3ReadGCVirt(pVM, pvBuf, GCPtr, cb);
df0f58959ed82a2a43ca8d816ce9592541df9f2fMark Andrews rc = MMR3ReadGCVirt(pVM, (uint8_t *)pvBuf + cbRead, GCPtr + cbRead, 1);
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews * Internal worker routine.
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews * On x86 the typical stack frame layout is like this:
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews * 16 parameter 2
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews * 12 parameter 1
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews * 8 parameter 0
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews * 4 return address
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews * 0 old ebp; current ebp points here
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrewsstatic int dbgfR3StackWalk(PVM pVM, PDBGFSTACKFRAME pFrame)
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews * Stop if we got a read error in the previous run.
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews if (pFrame->fFlags & DBGFSTACKFRAME_FLAGS_LAST)
3740b569ae76295b941d57a724a43beb75b533baBob Halley * Read the raw frame data.
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence const DBGFADDRESS AddrOldPC = pFrame->AddrPC;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley const unsigned cbRetAddr = DBGFReturnTypeSize(pFrame->enmReturnType);
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley switch (AddrOldPC.fFlags & DBGFADDRESS_FLAGS_TYPE_MASK)
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley case DBGFADDRESS_FLAGS_FAR16: cbStackItem = 2; break;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley case DBGFADDRESS_FLAGS_FAR32: cbStackItem = 4; break;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley case DBGFADDRESS_FLAGS_FAR64: cbStackItem = 8; break;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley default: cbStackItem = 4; break; /// @todo 64-bit guests.
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley size_t cbRead = cbRetAddr + cbStackItem + sizeof(pFrame->Args);
27809a2ee5db141b684e53bf1d94da26e9f92d3aMark Andrews Assert(DBGFADDRESS_IS_VALID(&pFrame->AddrFrame));
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence pFrame->fFlags & DBGFSTACKFRAME_FLAGS_ALL_VALID
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley * The first step is taken in a different way than the others.
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley if (!(pFrame->fFlags & DBGFSTACKFRAME_FLAGS_ALL_VALID))
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley pFrame->fFlags |= DBGFSTACKFRAME_FLAGS_ALL_VALID;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley /* Current PC - set by caller, just find symbol & line. */
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley pFrame->pSymPC = DBGFR3SymbolByAddrAlloc(pVM, pFrame->AddrPC.FlatPtr, NULL);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence pFrame->pLinePC = DBGFR3LineByAddrAlloc(pVM, pFrame->AddrPC.FlatPtr, NULL);
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley else /* 2nd and subsequent steps */
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley /* frame, pc and stack is taken from the existing frames return members. */
94e25967cda41b886e33ec254b917d21df21a187Bob Halley /* increment the frame number. */
94e25967cda41b886e33ec254b917d21df21a187Bob Halley * Return Frame address.
94e25967cda41b886e33ec254b917d21df21a187Bob Halley case 2: pFrame->AddrReturnFrame.off = *uBp.pu16; break;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence case 4: pFrame->AddrReturnFrame.off = *uBp.pu32; break;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence case 8: pFrame->AddrReturnFrame.off = *uBp.pu64; break;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence default: AssertMsgFailed(("cbStackItem=%d\n", cbStackItem)); return VERR_INTERNAL_ERROR;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence pFrame->AddrReturnFrame.FlatPtr += pFrame->AddrReturnFrame.off - pFrame->AddrFrame.off;
df0f58959ed82a2a43ca8d816ce9592541df9f2fMark Andrews * Return PC and Stack Addresses.
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews /** @todo AddrReturnStack is not correct for stdcall and pascal. (requires scope info) */
df0f58959ed82a2a43ca8d816ce9592541df9f2fMark Andrews pFrame->AddrReturnStack.off += cbStackItem + cbRetAddr;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence pFrame->AddrReturnStack.FlatPtr += cbStackItem + cbRetAddr;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence if (DBGFADDRESS_IS_VALID(&pFrame->AddrReturnPC))
5eb91bd90e3ad3426e5e3213031556a737cf3809Mark Andrews pFrame->AddrReturnPC.FlatPtr += *uRet.pu16 - pFrame->AddrReturnPC.off;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence DBGFR3AddrFromFlat(pVM, &pFrame->AddrReturnPC, *uRet.pu16);
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley pFrame->AddrReturnPC.FlatPtr += *uRet.pu32 - pFrame->AddrReturnPC.off;
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley DBGFR3AddrFromFlat(pVM, &pFrame->AddrReturnPC, *uRet.pu32);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence if (DBGFADDRESS_IS_VALID(&pFrame->AddrReturnPC))
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley pFrame->AddrReturnPC.FlatPtr += *uRet.pu64 - pFrame->AddrReturnPC.off;
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley DBGFR3AddrFromFlat(pVM, &pFrame->AddrReturnPC, *uRet.pu64);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence DBGFR3AddrFromSelOff(pVM, &pFrame->AddrReturnPC, uRet.pu16[1], uRet.pu16[0]);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence DBGFR3AddrFromSelOff(pVM, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews DBGFR3AddrFromSelOff(pVM, &pFrame->AddrReturnPC, uRet.pu16[4], uRet.pu64[0]);
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews DBGFR3AddrFromSelOff(pVM, &pFrame->AddrReturnPC, uRet.pu16[1], uRet.pu16[0]);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence DBGFR3AddrFromSelOff(pVM, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence DBGFR3AddrFromSelOff(pVM, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence DBGFR3AddrFromSelOff(pVM, &pFrame->AddrReturnPC, uRet.pu16[2], uRet.pu32[0]);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence DBGFR3AddrFromSelOff(pVM, &pFrame->AddrReturnPC, uRet.pu16[4], uRet.pu64[0]);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence AssertMsgFailed(("enmReturnType=%d\n", pFrame->enmReturnType));
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley pFrame->pSymReturnPC = DBGFR3SymbolByAddrAlloc(pVM, pFrame->AddrReturnPC.FlatPtr, NULL);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence pFrame->pLineReturnPC = DBGFR3LineByAddrAlloc(pVM, pFrame->AddrReturnPC.FlatPtr, NULL);
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley * The arguments.
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley memcpy(&pFrame->Args, uArgs.pv, sizeof(pFrame->Args));
94e25967cda41b886e33ec254b917d21df21a187Bob Halley * Walks the entire stack allocating memory as we walk.
94e25967cda41b886e33ec254b917d21df21a187Bob Halleystatic DECLCALLBACK(int) dbgfR3StackWalkCtxFull(PVM pVM, PDBGFSTACKFRAME pFrame, PCCPUMCTXCORE pCtxCore, bool fGuest)
94e25967cda41b886e33ec254b917d21df21a187Bob Halley /* alloc first frame. */
94e25967cda41b886e33ec254b917d21df21a187Bob Halley PDBGFSTACKFRAME pCur = (PDBGFSTACKFRAME)MMR3HeapAllocZ(pVM, MM_TAG_DBGF_STACK, sizeof(*pCur));
26f327f1f53afdb8256affa1c197ed138bf3cb2fAndreas Gustafsson /* copy input frame */
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence rc = DBGFR3AddrFromSelOff(pVM, &pCur->AddrPC, pCtxCore->cs, pCtxCore->eip);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence if (VBOX_SUCCESS(rc) /*&& pCur->enmReturnType == DBGFRETURNTYPE_INVALID*/)
94e25967cda41b886e33ec254b917d21df21a187Bob Halley switch (pCur->AddrPC.fFlags & DBGFADDRESS_FLAGS_TYPE_MASK)
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley case DBGFADDRESS_FLAGS_FAR16: pCur->enmReturnType = DBGFRETURNTYPE_NEAR16; break;
c89ac488df58cf6a37918cd00236eedf015830f8Andreas Gustafsson case DBGFADDRESS_FLAGS_FAR32: pCur->enmReturnType = DBGFRETURNTYPE_NEAR32; break;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley case DBGFADDRESS_FLAGS_FAR64: pCur->enmReturnType = DBGFRETURNTYPE_NEAR64; break;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence default: pCur->enmReturnType = DBGFRETURNTYPE_NEAR32; break; /// @todo 64-bit guests
94e25967cda41b886e33ec254b917d21df21a187Bob Halley if (VBOX_SUCCESS(rc) && DBGFADDRESS_IS_FAR16(&pCur->AddrPC) && fGuest)
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence if (VBOX_SUCCESS(rc) && !DBGFADDRESS_IS_VALID(&pCur->AddrStack))
26f327f1f53afdb8256affa1c197ed138bf3cb2fAndreas Gustafsson rc = DBGFR3AddrFromSelOff(pVM, &pCur->AddrStack, pCtxCore->ss, pCtxCore->esp & u64Mask);
26f327f1f53afdb8256affa1c197ed138bf3cb2fAndreas Gustafsson if (VBOX_SUCCESS(rc) && !DBGFADDRESS_IS_VALID(&pCur->AddrFrame))
26f327f1f53afdb8256affa1c197ed138bf3cb2fAndreas Gustafsson rc = DBGFR3AddrFromSelOff(pVM, &pCur->AddrFrame, pCtxCore->ss, pCtxCore->ebp & u64Mask);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * The first frame.
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley * The other frames.
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence while (!(pCur->fFlags & (DBGFSTACKFRAME_FLAGS_LAST | DBGFSTACKFRAME_FLAGS_MAX_DEPTH | DBGFSTACKFRAME_FLAGS_LOOP)))
94e25967cda41b886e33ec254b917d21df21a187Bob Halley /* try walk. */
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence /* add the next frame to the chain. */
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence PDBGFSTACKFRAME pNext = (PDBGFSTACKFRAME)MMR3HeapAlloc(pVM, MM_TAG_DBGF_STACK, sizeof(*pNext));
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence /* check for loop */
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence for (PDBGFSTACKFRAME pLoop = pCur->pFirst; pLoop && pLoop != pCur; pLoop = pLoop->pNext)
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence if (pLoop->AddrFrame.FlatPtr == pCur->AddrFrame.FlatPtr)
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence /* check for insane recursion */
ae5df22719a9e2c252ea1fcccd2cadb44c8bd8d4Mark Andrews pCur->fFlags |= DBGFSTACKFRAME_FLAGS_MAX_DEPTH;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * Begins a stack walk.
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * This will construct and obtain the first frame.
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * @returns VINF_SUCCESS on success.
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * @returns VERR_NO_MEMORY if we're out of memory.
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * @param pVM The VM handle.
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * @param pFrame The stack frame info structure.
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * On input this structure must be memset to zero.
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * If wanted, the AddrPC, AddrStack and AddrFrame fields may be set
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * to valid addresses after memsetting it. Any of those fields not set
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * will be fetched from the guest CPU state.
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * On output the structure will contain all the information we were able to
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * obtain about the stack frame.
6fa1cb5754695d550a58c6e8978fda65f5146af7David LawrenceDBGFR3DECL(int) DBGFR3StackWalkBeginGuest(PVM pVM, PDBGFSTACKFRAME pFrame)
269c07173e24d7811e2fd09304023e3104fcbe0bMark Andrews int rc = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)dbgfR3StackWalkCtxFull, 4,
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence pVM, pFrame, CPUMGetGuestCtxCore(pVM), true);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * Begins a stack walk.
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * This will construct and obtain the first frame.
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * @returns VINF_SUCCESS on success.
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence * @returns VERR_NO_MEMORY if we're out of memory.
26f327f1f53afdb8256affa1c197ed138bf3cb2fAndreas Gustafsson * @param pVM The VM handle.
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence * @param pFrame The stack frame info structure.
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence * On input this structure must be memset to zero.
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * If wanted, the AddrPC, AddrStack and AddrFrame fields may be set
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence * to valid addresses after memsetting it. Any of those fields not set
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence * will be fetched from the hypervisor CPU state.
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence * On output the structure will contain all the information we were able to
c8563aaf86c04f0e2284bcc8e444a0651c157ea0Andreas Gustafsson * obtain about the stack frame.
c8563aaf86c04f0e2284bcc8e444a0651c157ea0Andreas GustafssonDBGFR3DECL(int) DBGFR3StackWalkBeginHyper(PVM pVM, PDBGFSTACKFRAME pFrame)
c8563aaf86c04f0e2284bcc8e444a0651c157ea0Andreas Gustafsson int rc = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)dbgfR3StackWalkCtxFull, 4,
186e7f37c9fc985a7a7264cc8170e48a25bed434Mark Andrews * Gets the next stack frame.
186e7f37c9fc985a7a7264cc8170e48a25bed434Mark Andrews * @returns VINF_SUCCESS
186e7f37c9fc985a7a7264cc8170e48a25bed434Mark Andrews * @returns VERR_NO_MORE_FILES if not more stack frames.
186e7f37c9fc985a7a7264cc8170e48a25bed434Mark Andrews * @param pVM The VM handle.
186e7f37c9fc985a7a7264cc8170e48a25bed434Mark Andrews * @param pFrame Pointer to the current frame on input, content is replaced with the next frame on successful return.
186e7f37c9fc985a7a7264cc8170e48a25bed434Mark AndrewsDBGFR3DECL(int) DBGFR3StackWalkNext(PVM pVM, PDBGFSTACKFRAME pFrame)
903247531a10d699ef239a7351554ba0a1e3cd22Evan Hunt * Ends a stack walk process.
903247531a10d699ef239a7351554ba0a1e3cd22Evan Hunt * This *must* be called after a successful first call to any of the stack
903247531a10d699ef239a7351554ba0a1e3cd22Evan Hunt * walker functions. If not called we will leak memory or other resources.
903247531a10d699ef239a7351554ba0a1e3cd22Evan Hunt * @param pVM The VM handle.
903247531a10d699ef239a7351554ba0a1e3cd22Evan Hunt * @param pFrame The stackframe as returned by the last stack walk call.