By Nathan Fellman

2011-03-17 08:04:12 8 Comments

I am debugging C++ in gdb 7.1 on Linux.

I have a function a() that is called in many places in the code. I want to set a breakpoint in it, but only if it was called from b(). Is there any way to do it?

Is there any way to do it only if b() was called from c(), and so on ad infinitum?


@Employed Russian 2011-03-21 00:44:47

Update: There is now a better answer to this question: use GDB _is_caller convenience function.

The need you describe comes up quite often, usually in the context of some_utility_fn being called a lot, but you only are interested in the call which comes from some_other_fn.

You could probably script this entire interaction using the new embedded Python support in GDB from CVS trunk.

Without Python, you are limited in what you can do, but the usual technique is to have a disabled breakpoint on a(), and enable it from a command, attached to a breakpoint on b().

Here is an example:

int a(int x)
  return x + 1;

int b()
  return a(1);

int call_a_lots()
  int i, sum = 0;
  for (i = 0; i < 100; i++)
    sum += a(i);

int main()
  return b();

gcc -g t.c
gdb -q ./a.out
Reading symbols from /tmp/a.out...done.
(gdb) break a
Breakpoint 1 at 0x4004cb: file t.c, line 3.
(gdb) disable 1
(gdb) break b
Breakpoint 2 at 0x4004d7: file t.c, line 8.
(gdb) command 2
>enable 1
(gdb) run

Breakpoint 1, a (x=1) at t.c:3
3     return x + 1;
(gdb) bt
#0  a (x=1) at t.c:3
#1  0x00000000004004e1 in b () at t.c:8
#2  0x000000000040052c in main () at t.c:21
(gdb) q

Voila: we've stopped on a() called from b(), ignoring previous 100 calls to a().

@Ciro Santilli 冠状病毒审查六四事件法轮功 2016-12-30 14:06:29

Can this be automated without Python?…

@Jeff Trull 2018-03-01 19:10:23

gdb can handle this directly now without any need for Python. Just do this:

b a if $_caller_is("b")

@owenwater 2011-03-20 02:05:41

not sure how to do it by gdb.
But you can declare global variable like:

bool call_a = false;

and when b calling a

call_a = true;   

and set call_a to false when other function call a() or after your breakpoint

then use condition break-point

break [line-number] if call_a == true

@rix0rrr 2016-05-02 13:29:16

A simpler solution than Python scripting is using a temporary breakpoint.

It looks like this:

b ParentFunction
command 1
  tb FunctionImInterestedIn

Every time you break in ParentFunction, you'll set a one-time breakpoint on the function you're actually interested in, then continue running (presumably until you hit that breakpoint).

Since you'll break exactly once on FunctionImInterestedIn, this won't work if FunctionImInterestedIn is called multiple times in the context of ParentFunction and you want to break on each invocation.

@Nathan Fellman 2016-05-02 14:00:11

It looks like your last sentence was cut. What did you mean to write?

@Ciro Santilli 冠状病毒审查六四事件法轮功 2016-12-30 14:06:55

Can this be automated without Python?…

@AppSpec 2015-04-15 01:46:48

An easy one for arm is:

Set the breakpoint in the function you are interested.

break a

Attach an gdb command to that breakpoint.

command 1
 up 1
 if $lr == 0x12345678
  echo match \n
  down 1
  echo no match \n
  echo $lr \n
  down 1

When ever you arrive in the function a(), the command temporarily pops up one stack frame thus updating the link register. The callers link register value can then be used continue when the caller is not the execution path you need.


@user184968 2013-11-26 06:22:35

I have tested this on gdb 7.6 that is already available but it does not work on gdb 7.2 and probably on gdb 7.1:

So this is main.cpp:

int a()
  int p = 0;
  p = p +1;
  return  p;

int b()
  return a();

int c()
  return a();

int main()
  return 0;

Then g++ -g main.cpp

This is

class MyBreakpoint (gdb.Breakpoint):
    def stop (self):
        if gdb.selected_frame().older().name()=="b":
          return True
          return False


And this is how it works:

4>gdb -q -x ./a.out
Reading symbols from /home/a.out...done.
Breakpoint 1 at 0x400540: file main.cpp, line 3.
(gdb) r
Starting program: /home/a.out
#0  a () at main.cpp:3
#1  0x0000000000400559 in b () at main.cpp:10
#2  0x0000000000400574 in main () at main.cpp:21

Breakpoint 1, a () at main.cpp:3
3         int p = 0;
(gdb) c
[Inferior 1 (process 16739) exited normally]
(gdb) quit

@Ciro Santilli 冠状病毒审查六四事件法轮功 2016-12-30 13:59:21

We should make the Python function take a and b as CLI arguments.

@Ciro Santilli 冠状病毒审查六四事件法轮功 2016-12-30 17:30:56

Started a sketch at:… but got lazy. Note that temporary breakpoints are possible in pure Python with gdb.Breakpoint(temporary=True) and that it would be cool to go up the stack .older() chain all the way.

Related Questions

Sponsored Content

39 Answered Questions

10 Answered Questions

21 Answered Questions

[SOLVED] Why should I use a pointer rather than the object itself?

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2357919 View
  • 4243 Score
  • 1 Answer
  • Tags:   c++ c++-faq

5 Answered Questions

[SOLVED] What is the copy-and-swap idiom?

17 Answered Questions

[SOLVED] Why can templates only be implemented in the header file?

5 Answered Questions

[SOLVED] Can I set a breakpoint on 'memory access' in GDB?

7 Answered Questions

Sponsored Content