NationalSecurityAgency / ghidra

Ghidra is a software reverse engineering (SRE) framework
https://www.nsa.gov/ghidra
Apache License 2.0
51.37k stars 5.85k forks source link

Emulater Get Register Value Always Returning 0? #681

Closed VGKintsugi closed 3 years ago

VGKintsugi commented 5 years ago

I'm testing my processor module with sleighexample.cc. I have disassembly working but I'm running into issues with emulation, specifically when I'm trying to query register values. Whenever I attempt to read the value of a register the emulator always returns 0. I know I am using the correct name of the register because if I specify an invalid register I get "terminate called after throwing an instance of 'SleighError'".

Here is my callback code called from DoEmulate():

`bool PrintfCallBack::addressCallback(const Address &addr) {
MemoryState mem = static_cast<EmulateMemory >(emulate)->getMemoryState(); uint4 pc = mem->getValue("pc");

printf("PC: 0x%08x 0x%08x 0x%08x\n", addr.getOffset(), emulate->getExecuteAddress().getOffset(), pc );

return true; }`

The code prints: PC: 0x00004008 0x00004008 0x00000000 The first two values are correct, the 3rd is always zero. I expect to be 0x00004008. Any advice on how to debug this? Thanks in advance.

VGKintsugi commented 5 years ago

I've made some progress myself and it looks like there are two issues:

1) My printf() format specifiers are incorrect. I should be using "0x%08zx" or something similar for "long unsigned int". 2) mem->getValue("pc") is still always returning 0. Other registers work. I'm assuming this is because I don't actually reference pc in my SLEIGH for most instructions? I assuming if I had a "pc = inst_next;" line in every single SLEIGH definition pc would get updated correctly.