Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Gdb how to set conditional breakpoint to a function's 3rd line

Tags:

gdb

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.

like image 445
Vereb Avatar asked Jun 10 '10 09:06

Vereb


2 Answers

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!

like image 68
Fanatic23 Avatar answered Nov 09 '22 13:11

Fanatic23


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

like image 44
TheCodeArtist Avatar answered Nov 09 '22 15:11

TheCodeArtist