tags:

views:

192

answers:

2

How can I set a conditional breakpoint to a function's 3rd line in GDB?

I know I could give the file name + line number but this is not working for me.
I have to store the gdb commands as a script to repeat it later and I don't want to change the script every time the source file is changing.

+1  A: 

Conditional Breakpoints


Type in the following,
assuming You have to apply a breakpoint at the line inside the function

(gdb) break <function-name>

GDB will reply with as follows Breakpoint 1 at 0xaddr: filename , linenumber.

Note the number of the breakpoint
( in this case it is 1 )

Enter the following command
( replace the breakpoint-number appropriately )

(gdb) condition 1 <condition>

During run-time you can skip (step-over) to the 3rd line using the next command twice

(gdb) next
(gdb) next

..and you are at the function’s 3rd line!!

UPDATE:

Additionally, to break when the variable changes value use this:

(gdb) watch variable==value

For more details please refer :
- GDB:Conditional Breakpoints
- Hacking into an executable using GDB


GoodLUCK!!
- CVS

CVS-2600Hertz
Thanks, the problem is that the condition is only valid in the third line since it uses the functions local variable.
Vereb
updated the answer. Use a Watchpoint.
CVS-2600Hertz
A: 

How about defining your own command file for gdb and then just sourcing it during debug? That would help reduce a fair bit of typing. Try this:

Create a file, let's call it mydef and here's its contents:

define cmd
  b function-where-you-want-to-break
  r
  b +2
  c
end

Once you load the executable in gdb, type in source mydef in gdb prompt followed by cmd

Now you are into the 2nd line from where function began. :) Hope this helps!

Fanatic23