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?

Valerio Santinelli
  • 1,592
  • 2
  • 27
  • 45

1 Answers1

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.

Tom Tromey
  • 21,507
  • 2
  • 45
  • 63
  • 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. – Valerio Santinelli Oct 03 '16 at 20:08
  • 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. – Tom Tromey Oct 03 '16 at 21:46
  • 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. – Valerio Santinelli Oct 04 '16 at 11:24
  • I don't know what capstone needs, but it's easy to read memory from Python and hand that to whatever you like. – Tom Tromey Oct 04 '16 at 13:58
  • 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? – Valerio Santinelli Oct 04 '16 at 19:25
  • There's no deep reason, just nobody has written a patch. – Tom Tromey Oct 04 '16 at 20:35