From 9becda82eff58c7d66700c2ade18be786981529e Mon Sep 17 00:00:00 2001 From: Rene Gollent Date: Tue, 25 Dec 2012 09:34:28 -0500 Subject: [PATCH] Implement ArchitectureX8664::UpdateStackFrameCpuState(). --- .../arch/x86_64/ArchitectureX8664.cpp | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/apps/debugger/arch/x86_64/ArchitectureX8664.cpp b/src/apps/debugger/arch/x86_64/ArchitectureX8664.cpp index 747ee12424..21525627e7 100644 --- a/src/apps/debugger/arch/x86_64/ArchitectureX8664.cpp +++ b/src/apps/debugger/arch/x86_64/ArchitectureX8664.cpp @@ -303,7 +303,39 @@ ArchitectureX8664::UpdateStackFrameCpuState(const StackFrame* frame, Image* previousImage, FunctionDebugInfo* previousFunction, CpuState* previousCpuState) { - fprintf(stderr, "ArchitectureX8664::UpdateStackFrameCpuState: TODO\n"); + // This is not a top frame, so we want to offset rip to the previous + // (calling) instruction. + CpuStateX8664* cpuState = dynamic_cast(previousCpuState); + + // get rip + uint64 rip = cpuState->IntRegisterValue(X86_64_REGISTER_RIP); + if (previousFunction == NULL || rip <= previousFunction->Address()) + return; + target_addr_t functionAddress = previousFunction->Address(); + + // allocate a buffer for the function code to disassemble + size_t bufferSize = rip - functionAddress; + void* buffer = malloc(bufferSize); + if (buffer == NULL) + return; + MemoryDeleter bufferDeleter(buffer); + + // read the code + ssize_t bytesRead = fTeamMemory->ReadMemory(functionAddress, buffer, + bufferSize); + if (bytesRead != (ssize_t)bufferSize) + return; + + // disassemble to get the previous instruction + DisassemblerX8664 disassembler; + target_addr_t instructionAddress; + target_size_t instructionSize; + if (disassembler.Init(functionAddress, buffer, bufferSize) == B_OK + && disassembler.GetPreviousInstruction(rip, instructionAddress, + instructionSize) == B_OK) { + rip -= instructionSize; + cpuState->SetIntRegister(X86_64_REGISTER_RIP, rip); + } }