0

I am trying to write a Python script for GDB to trace a function.

The idea is to set a breakpoint on an address location, let the program run and then, when it breaks, log to file registers, vectors and stack and find out what address the next instruction will be, set a breakpoint on that location and rinse and repeat.

I read through the documentation and I'm pretty confident registers, vectors and memory locations can be easily dumped. The actual problem is finding what the next instruction location will be as it requires to analyze the disassembly of the current instruction to determine where the next breakpoint should be placed.

Update

I am doing all this without using stepi or nexti because the target I'm debugging works only with hardware breakpoints and as far as I know those commands use software breakpoints to break at the next instruction

Is there anything like that in GDB?

1 Answer 1

0

Yes, you can do this in gdb. Rather than trying to set a breakpoint on the next instruction, you can instead use the si command to single-step to the next instruction.

Sign up to request clarification or add additional context in comments.

6 Comments

I forgot to add that I cannot use software breakpoints for the target I'm debugging. I need to do it with hardware breakpoints. And as far as I can tell si uses software breakpoints to stop at the next instruction.
You don't mention the target. On Linux a single step is just a ptrace request, I think typically in the end implemented using debug registers on the chip. Anyway, if this doesn't work for you, then you're on your own. gdb does have some logic in this area (for reverse primarily, but also for "out of line" stepping), but this information isn't accessible from Python.
The target is Android, so it's still a Linux under the hood. I should have a look at the gdbserver implementation in the Android NDK to check what they're using. It would really help to be able to extract some info through Python and eventually pipe that to Capstone to do the hard work of figuring out the next instruction.
I don't know what capstone needs, but it's easy to read memory from Python and hand that to whatever you like.
Yes, I guess I'll give a shot at reading memory and exporting the dump to capstone to have the list of instructions back. Capstone should be able to find out what the next instruction address is, given enough context. BTW do you know why hardware breakpoints haven't been exported to Python?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.