0

There are 3 functions: f1, f2, f3:

void f1()
{
   f3();
}

void f2()
{
   f3();
}

void f3()
{
   ....
}

I want to put a breakpoint somewhere inside f3, but only if f3 was called from f1.

Dharmendra
  • 384
  • 1
  • 5
  • 22
  • Possible duplicate of [Is there any way to set a breakpoint in gdb that is conditional on the call stack](https://stackoverflow.com/questions/5336403/is-there-any-way-to-set-a-breakpoint-in-gdb-that-is-conditional-on-the-call-stac). – Mark Plotnick May 04 '16 at 14:07
  • 1
    Also related: [GDB break on exception thrown when called from specific function](http://stackoverflow.com/questions/26765176/gdb-break-on-exception-thrown-when-called-from-specific-function). With a recent version of gdb, you can do `b f3 if $_caller_is("f1")` – Mark Plotnick May 04 '16 at 14:47
  • And documentation for above function(s) is available at https://sourceware.org/gdb/onlinedocs/gdb/Convenience-Funs.html. – dbrank0 May 05 '16 at 07:50
  • Does this answer your question? [Is there any way to set a breakpoint in gdb that is conditional on the call stack?](https://stackoverflow.com/questions/5336403/is-there-any-way-to-set-a-breakpoint-in-gdb-that-is-conditional-on-the-call-stac) – jaggi Dec 03 '20 at 12:27

1 Answers1

1

A solution among others consists of setting a conditional breakpoint. the call of f3 is identified by int boolean

code:

#include <stdio.h>

int boolean =0;

void f3()
{

}

void f2()
{
    boolean = 1;
    f3();
}

void f1()
{
    boolean = 0;
    f3();
}


int main()
{

    f2();
    f1();
    f2();
    f1();   

    return 0;
}

In gdb to set a break in f3 only if you come from f2

(gdb)b f3 if boolean==1
Rami
  • 73
  • 6