9

Is there a way to set a breakpoint at every line in the code with GDB? Obviously I don't want to hit b *addr for every single line, so I'm wondering if there's a fast way to do this.

Edit Note that I am running a binary created by someone else and I do not have access to the source code. Unfortunately, that binary has not been compiled with the -g flag. Therefore, I cannot just single step through each line in the code.

Further Edit As Jason points out below, you can indeed single step through the code so long as you use si or ni, as opposed to just simply s (step) or n (next). n or s work fine, though, if the source code had been compiled with -g, but it steps through lines of source code, as opposed to stepping through every assembly instruction like ni or si do in a binary that was compiled without -g.

3 Answers3

7

Use si (stepi) to instruction step through the code. You can use ni (nexti) to step over library functions you're not interested in. If you accidentally step into one of them, finish should get you back to your original routine. People working at this level typically have gdb set to display the next few instructions that are about to be executed, e.g. disp/3i $pc.

Jason Molenda
  • 14,835
  • 1
  • 59
  • 61
  • Wow, such a simple solution. I was using n and s, instead of ni or si. Works perfectly. I'm curious as to how GDB interprets ni vs n. Also, disp/3i $pc is tremendously helpful. –  Nov 16 '12 at 08:24
  • 1
    `next` uses the source line information from the debug info to determine the address range of a source line. Line 10 might go from 0x100 to 0x116, so the debugger (behind the scenes), instruction steps every instruction until the pc is 0x116. Complications include stepping into another function or leaving the current function (return command, longjmp, exception thrown) in which case you want to stop execution. When the debugger steps into a subroutine call, it sets a breakpoint on your original function and continues. There's a lot of stuff going on behind the scenes. `ni` is lower level. – Jason Molenda Nov 16 '12 at 10:15
1

Can't you just place the breakpoint on the first line of execution and then step through each line ? This depends on what are you trying to achieve by setting breakpoints on each line. If you want to evaluate expressions, you can do it by following my logic (step through each line).

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
AYK
  • 3,312
  • 1
  • 17
  • 30
  • Good point. You are correct, but I forgot to say that I am running a binary that was not compiled with the -g flag. I don't have access to the source code, either, so I can't recompile with -g. Unfortunately, that does not let me single step. Question has been edited for clarity –  Nov 16 '12 at 05:35
1

PowerPC has hardware support for ranged breakpoints, and GCB offers:

break-range start end

in that arch. So I think you could just break on the entire memory address, or the entire text section (untested).

The command fails on x86.

Doc: https://sourceware.org/gdb/onlinedocs/gdb.html#index-break_002drange-1548

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985