PATMR3Dbg.cpp revision e3ddc294f1a80edb513f054d4c24ab79d7dfd3dd
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * PATM - Dynamic Guest OS Patching Manager, Debugger Related Parts.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * Copyright (C) 2006-2013 Oracle Corporation
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * available from http://www.virtualbox.org. This file is free software;
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * General Public License (GPL) as published by the Free Software
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync/*******************************************************************************
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync* Header Files *
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync*******************************************************************************/
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync/*******************************************************************************
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync* Defined Constants And Macros *
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync*******************************************************************************/
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync/** Adds a structure member to a debug (pseudo) module as a symbol. */
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync#define ADD_MEMBER(a_hDbgMod, a_Struct, a_Member, a_pszName) \
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync rc = RTDbgModSymbolAdd(hDbgMod, a_pszName, 0 /*iSeg*/, RT_OFFSETOF(a_Struct, a_Member), \
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync RT_SIZEOFMEMB(a_Struct, a_Member), 0 /*fFlags*/, NULL /*piOrdinal*/); \
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync } while (0)
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync/** Adds a structure member to a debug (pseudo) module as a symbol. */
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync#define ADD_FUNC(a_hDbgMod, a_BaseRCPtr, a_FuncRCPtr, a_cbFunc, a_pszName) \
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync int rcAddFunc = RTDbgModSymbolAdd(hDbgMod, a_pszName, 0 /*iSeg*/, \
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync (RTRCUINTPTR)a_FuncRCPtr - (RTRCUINTPTR)(a_BaseRCPtr), \
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync } while (0)
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * Called by PATMR3Init.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param pVM The cross context VM structure.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * Called by PATMR3Term.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param pVM The cross context VM structure.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * Called by when the patch memory is reinitialized.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param pVM The cross context VM structure.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync RTDbgModRemoveAll(pVM->patm.s.hDbgModPatchMem, true);
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsyncstatic size_t patmR3DbgDescribePatchAsSymbol(PPATMPATCHREC pPatchRec, char *pszName, size_t cbLeft)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync } while (0)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync /* Start the name off with the address of the guest code. */
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync size_t cch = RTStrPrintf(pszName, cbLeft, "Patch_%#08x", pPatchRec->patch.pPrivInstrGC);
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync /* Append flags. */
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * Called when a new patch is added or when first populating the address space.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param pVM The cross context VM structure.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param pPatchRec The patch record.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsyncvoid patmR3DbgAddPatch(PVM pVM, PPATMPATCHREC pPatchRec)
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync && !(pPatchRec->patch.flags & PATMFL_GLOBAL_FUNCTIONS))
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync /** @todo find a cheap way of checking whether we've already added the patch.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * Using a flag would be nice, except I don't want to consider saved
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * state considerations right now (I don't recall if we're still
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * depending on structure layout there or not). */
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync size_t off = patmR3DbgDescribePatchAsSymbol(pPatchRec, szName, sizeof(szName));
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync /* If we have a symbol near the guest address, append that. */
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync int rc = DBGFR3SymbolByAddr(pVM, pPatchRec->patch.pPrivInstrGC, &offDisp, &Symbol);
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync RTStrCopy(&szName[off], sizeof(szName) - off, Symbol.szName);
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync /* Add it (may fail due to enable/disable patches). */
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync RTDbgModSymbolAdd(pVM->patm.s.hDbgModPatchMem, szName, 0 /*iSeg*/,
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * Enumeration callback used by patmR3DbgAddPatches
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @returns 0 (continue enum)
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param pNode The patch record node.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param pvUser The cross context VM structure.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsyncstatic DECLCALLBACK(int) patmR3DbgAddPatchCallback(PAVLOU32NODECORE pNode, void *pvUser)
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync patmR3DbgAddPatch((PVM)pvUser, (PPATMPATCHREC)pNode);
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * Populates an empty "patches" (hDbgModPatchMem) module with patch symbols.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param pVM The cross context VM structure.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * @param hDbgMod The debug module handle.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsyncstatic void patmR3DbgAddPatches(PVM pVM, RTDBGMOD hDbgMod)
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * Global functions.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperCallGC, PATMLookupAndCallRecord.size, "PATMLookupAndCall");
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperRetGC, PATMRetFunctionRecord.size, "PATMRetFunction");
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperJumpGC, PATMLookupAndJumpRecord.size, "PATMLookupAndJump");
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync ADD_FUNC(hDbgMod, pVM->patm.s.pPatchMemGC, pVM->patm.s.pfnHelperIretGC, PATMIretFunctionRecord.size, "PATMIretFunction");
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync * The patches.
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync RTAvloU32DoWithAll(&pVM->patm.s.PatchLookupTreeHC->PatchTree, true /*fFromLeft*/, patmR3DbgAddPatchCallback, pVM);
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * Populate DBGF_AS_RC with PATM symbols.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * Called by dbgfR3AsLazyPopulate when DBGF_AS_RC or DBGF_AS_RC_AND_GC_GLOBAL is
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * accessed for the first time.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * @param pVM The cross context VM structure.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * @param hDbgAs The DBGF_AS_RC address space handle.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsyncVMMR3_INT_DECL(void) PATMR3DbgPopulateAddrSpace(PVM pVM, RTDBGAS hDbgAs)
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * Add a fake debug module for the PATMGCSTATE structure.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync int rc = RTDbgModCreate(&hDbgMod, "patmgcstate", sizeof(PATMGCSTATE), 0 /*fFlags*/);
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, uVMFlags, "uVMFlags");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, uPendingAction, "uPendingAction");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, uPatchCalls, "uPatchCalls");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, uScratch, "uScratch");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, uIretEFlags, "uIretEFlags");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, uIretCS, "uIretCS");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, uIretEIP, "uIretEIP");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, GCPtrInhibitInterrupts, "GCPtrInhibitInterrupts");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, GCCallPatchTargetAddr, "GCCallPatchTargetAddr");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, GCCallReturnAddr, "GCCallReturnAddr");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uEAX, "Restore.uEAX");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uECX, "Restore.uECX");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uEDI, "Restore.uEDI");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.eFlags, "Restore.eFlags");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync ADD_MEMBER(hDbgMod, PATMGCSTATE, Restore.uFlags, "Restore.uFlags");
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync rc = RTDbgAsModuleLink(hDbgAs, hDbgMod, pVM->patm.s.pGCStateGC, 0 /*fFlags/*/);
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync * Add a fake debug module for the patches.
4d8891c509dc8abc8a8040f347e33afa9c390f8dvboxsync rc = RTDbgModCreate(&hDbgMod, "patches", pVM->patm.s.cbPatchMem, 0 /*fFlags*/);
acb0ddeac7f624f263afe1c42c8e35655aa59181vboxsync rc = RTDbgAsModuleLink(hDbgAs, hDbgMod, pVM->patm.s.pPatchMemGC, 0 /*fFlags/*/);
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * Annotates an instruction if patched.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * @param pVM The VM handle.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * @param RCPtr The instruction address.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * @param cbInstr The instruction length.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * @param pszBuf The output buffer. This will be an empty string
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * if the instruction wasn't patched. If it's
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * patched, it will hold a symbol-like string
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * describing the patch.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * @param cbBuf The size of the output buffer.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsyncVMMR3_INT_DECL(void) PATMR3DbgAnnotatePatchedInstruction(PVM pVM, RTRCPTR RCPtr, uint8_t cbInstr, char *pszBuf, size_t cbBuf)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * Always zero the buffer.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * Drop out immediately if it cannot be a patched instruction.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * Look for a patch record covering any part of the instruction.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * The first query results in a patched less or equal to RCPtr. While the
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * second results in one that's greater than RCPtr.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync pPatchRec = (PPATMPATCHREC)RTAvloU32GetBestFit(&pVM->patm.s.PatchLookupTreeHC->PatchTree, RCPtr, false /*fFromAbove*/);
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync || RCPtr - pPatchRec->patch.pPrivInstrGC > pPatchRec->patch.cbPrivInstr)
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync pPatchRec = (PPATMPATCHREC)RTAvloU32GetBestFit(&pVM->patm.s.PatchLookupTreeHC->PatchTree, RCPtr, true /*fFromAbove*/);
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync || (RTRCPTR)(RCPtr + cbInstr) < pPatchRec->patch.pPrivInstrGC )
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync * Lazy bird uses the symbol name generation code for describing the patch.
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync size_t off = patmR3DbgDescribePatchAsSymbol(pPatchRec, pszBuf, cbBuf);
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync const char *pszState;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync case PATCH_DISABLE_PENDING: pszState = "DisablePending"; break;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync default: pszState = "State???"; AssertFailed(); break;
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync off += RTStrPrintf(&pszBuf[off], cbBuf - off, " - %s (%u b) - %#x LB %#x",
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync pPatchRec->patch.pPatchBlockOffset + pVM->patm.s.pPatchMemGC,
e3ddc294f1a80edb513f054d4c24ab79d7dfd3ddvboxsync off += RTStrPrintf(&pszBuf[off], cbBuf - off, " - %s (%u b)", pszState, pPatchRec->patch.cbPatchJump);