Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting a breakpoint in a specific line inside a function with 'gdb'

Tags:

I am trying to set a breakpoint to the fifth line inside a member function of a class(a class that I created) with 'gdb'.

From here I understood just how to set a breakpoint at the begining of the the function,but I want to set it on a specific line inside the function, or a specific offset from the begining of this function.

In general is there a way in 'gdb' to set a breakpoint to a line by setting an offset from another breakpoint I already have ?

Thanks !

like image 865
nadavgam Avatar asked Dec 09 '15 22:12

nadavgam


People also ask

How do you set a breakpoint at a specific line in GDB?

Setting breakpoints A breakpoint is like a stop sign in your code -- whenever gdb gets to a breakpoint it halts execution of your program and allows you to examine it. To set breakpoints, type "break [filename]:[linenumber]". For example, if you wanted to set a breakpoint at line 55 of main.

How do you set a breakpoint in a function?

You can set a breakpoint at a line number, using the stop at command, where n is a source code line number and filename is an optional program file name qualifier. If the line specified is not an executable line of source code, dbx sets the breakpoint at the next executable line.

What is the GDB command to set a breakpoint in line 7?

c file listed in Example 7.1, “Compiling a C Program With Debugging Information” with debugging information, you can set a new breakpoint at line 7 by running the following command: (gdb) break 7 Breakpoint 2 at 0x4004e3: file fibonacci.

What is a break point in GDB?

A breakpoint makes your program stop whenever a certain point in the program is reached. For each breakpoint, you can add conditions to control in finer detail whether your program stops.


2 Answers

You can create a breakpoint at an offset from the current stopped position with gdb breakpoint +<offset>.

You can also create a breakpoint on a specific line number using either gdb break <linenumber> (for the current source file) or gdb break <filename>:<linenumber> (for a file other than the current file).

More details in the docs.

like image 102
lsowen Avatar answered Oct 09 '22 03:10

lsowen


There isn't a way to set a breakpoint relative to the start of a function such that it will retain its relative position if the source file is modified. This would sometimes be useful; but it is just a feature that nobody has added to gdb.

It could maybe be emulated from Python, though it couldn't work exactly the way that ordinary breakpoints work, because Python doesn't have access to the breakpoint resetting mechanism inside gdb.

A one-shot solution can be done either as shown in the other answer or from Python.

When I have needed this sort of functionality -- a breakpoint mid-function that is reasonably robust against source changes -- I have used "SDT" static probe points. These let you name such spots in your source.

like image 29
Tom Tromey Avatar answered Oct 09 '22 04:10

Tom Tromey