Wednesday, May 23, 2007

[off topic] Using GNU GDB for assembler

GNU GDB refuses to be good debugger for assembler. But still with some pain it is possible to get out of it something.

But generally, I would advise learning to debug your programs right in your brains while you typing them. Think that's too complicated? Then read on. By end of the post you might be not that sure about that.

0. Starting program. 'break *&_start' to set breakpoint at program entry point. 'run' as usual to actually load program.

1. 'list' - forget about it. It will never ever show assembler. 'set language asm'? - HA-HA!! Naively thought you can outsmart GDB?? How naive of you. 'list' will not work. Period.

2. But how to see the code then?? Answer: 'disass $pc $pc+36'. $pc is content of register 'pc', what under PowerPC is 'program counter' - pointer to next instruction. (Probably pc is portable, have no clue.) Second parameter - $pc+36 - tells to disassembler to stop at address +36 (thus disassembling next 8 instructions, stopping at 9th).

3. 'step' and 'next' will not work. I would have paid a premium to disable them altogether when I do use gdb for assembler, because they are screwing execution context in most unexpected way. You fingers slipped and typed 'n<Return>'? Restart everything again - that way it's faster.

4. 'stepi' and 'nexti' are used to walk instruction-wise.

5. Breakpoints: 'break *0xdeadbeef', where 0xXXXXXXXX is address where you want to set a break point.

Overall, you would hit snag at every corner. Yet, if you are courageous enough, my experience laid out here might serve as starting point.

P.S. Interesting article aboug GDB from IBM's developerWorks.

No comments: