How to Set a Breakpoint in Gdb Where the Function Returns

How to set a breakpoint in GDB where the function returns?

break without arguments stops execution at the next instruction in the currently selected stack frame. You select strack frames via the frame or up and down commands. If you want to debug the point where you are actually leaving the current function, select the next outer frame and break there.

gdb: make a breakpoint on a class function in c++

Add one more line to your main():

std::cout << cA.getA(2,3) << std::endl;

Now, repeat your original experiment. Your results will be different, now:

(gdb) b a::getA
Breakpoint 1 at 0x40089d: a::getA. (2 locations)

"2 locations" is gdb telling you that it now injected breakpoints for both overloaded functions. Stepping through the code will verify this.

If a symbol resolved to multiply-overloaded functions, the b command sets a breakpoint at each one.

But because the 2nd overloaded function was an inline function and it was never called in your original code, gcc didn't even compile it, and there was nothing for gdb to set a breakpoint at.

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

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.

How to put breakpoint at function return

You can use reverse debugging using this one:

(gdb) fin
(gdb) reverse-step

How can I use gdb to catch the moment when a function returns false?

you can try with a bit of Python code:

import gdb
class FunctionFinishBreakpoint (gdb.FinishBreakpoint):
def __init__ (self):
gdb.FinishBreakpoint.__init__(self, gdb.newest_frame(),
internal=True)
self.silent = True

def stop(self):
#print("after: {}".format(int(self.return_value)))
return not int(self.return_value)

class FunctionBreakpoint(gdb.Breakpoint):
def __init__ (self, spec):
gdb.Breakpoint.__init__(self, spec)
self.silent = True

def stop (self):
#print("before")
FunctionFinishBreakpoint() # set breakpoint on function return

return False # do not stop at function entry

FunctionBreakpoint("test")

Save that in a finish.py file, edit it to your needs and source it from GDB, or run it between python ... end or in python-interactive (pi).

This code creates a FunctionBreakpoint, that triggers FunctionBreakpoint.stop eachtime function test is hit. The callback is silent, and only creates a FunctionFinishBreakpoint, that stops at the end of the current frame (ie, at the end of your function). That second stop calls FunctionFinishBreakpoint.stop, which tests if the return value evaluates to true or false. If it is "not true", it tells GDB to stop the execution.

Documentation references:

  • Manipulating breakpoints using Python
  • Finish Breakpoints

(gdb.FinishBreakpoint was added to GDB Python interface for that very purpose, by myself :-)

(last time I checked, there was an efficiency problem with these FinishBreakpoint, you may notice it if your function is called very often)

GDB breakpoint on function arguments

Sure, use "break if"

break foo if i == 5

If you have multiple variable if need to break on, just use classic if syntax :

break foo if i == 5 && j == 3

How to debug a function with gdb

You managed to miss part of the message from GDB. Here is my sample program:

int
foo (int arg)
{
return arg + 3;
}

int
main ()
{
return foo (-3);
}

And here is my GDB session:

(gdb) start
Temporary breakpoint 1 at 0x401119: file eval.c, line 10.
Starting program: eval.x

Temporary breakpoint 1, main () at eval.c:10
10 return foo (-3);
(gdb) break foo
Breakpoint 2 at 0x40110d: file eval.c, line 4.
(gdb) print foo (2)

Breakpoint 2, foo (arg=2) at eval.c:4
4 return arg + 3;
The program being debugged stopped while in a function called from GDB.
Evaluation of the expression containing the function
(foo) will be abandoned.
When the function is done executing, GDB will silently stop.
(gdb) bt
#0 foo (arg=2) at eval.c:4
#1 <function called from gdb>
#2 main () at eval.c:10
(gdb)

Notice the last line of the message from GDB: When the function is done executing, GDB will silently stop. So GDB is still inside the called function with the arguments you passed. The can be seen in the backtrace with <function called from GDB>.

So you can continue stepping through the function to see how it behaves. What you don't get is the printing of the result when the function returns, GDB has lost track of the fact that this is what you wanted, so instead, when the function returns GDB will just drop you back to the prompt. What this means is that you should inspect the return value inside the function before it returns.

How to set a breakpoint in function which can be detected by gdb

I tried to set an infinite loop, but the system seems to have optmised it and moved forward.

If you are going to be effective at debugging, first thing you need to learn is to confirm your assumptions.

The "the system seems to have ..." is not an acceptable assumption, you need to confirm that this is indeed what has happened, using GDB disas command.

It is also impossible for compiler to optimize out an infinite loop like this:

volatile int gdb_attached = 0;
while (!gdb_attached) {
printf("Waiting for gdb to attach\n");
sleep(1);
}

If you put a loop like above into your function, it is guaranteed (assuming your compiler is not exceptionally buggy) that your function (if it is called at all) will not return until you attach GDB to your process, and set gdb_attached to non-zero value from GDB.



Related Topics



Leave a reply



Submit