5
votes

I have two functions, func1 and func2, each with a breakpoint set.

Is it possible to have GDB stop on the func2 breakpoint iff the previous breakpoint hit was func1?

2
Do you have a small code snippet that would demonstrate what you are trying to accomplish? - ZombieCode
There's no need for a code snippet; the question is crystal clear. - Jim Balter
Breakpoint command lists are your friends. You can give the breakpoint of func1 the command to set the breakpoint for func2 iff your program stops due to the first breakpoint. ofb.net/gnu/gdb/gdb_35.html#SEC35 - halex

2 Answers

2
votes

The best way to do this is to use commands in breakpoints.

You can direct GDB to execute certain commands (like, to increment a counter) when the two breakpoints are hit. The execution is halted conditionally based on the count of these variables/flags.

I found this information on this link. Please refer the same for further details. The article is very well-written with proper examples. Hope this helps.

0
votes

Have one breakpoint set the other breakpoint. To avoid gdb spaghetti use of define to create functions is recommended.

main.cpp

int c1=0, c2=0;

void func1(){
    c1++;
}

void func2(){
    c2++;
}

int main(){

    // we shouldn't see a breakpoint here
    for(int i=0; i < 5; i++)
        func1();

    func2();

    // get a breakpoint
    func1();

    return 0;
}

compile and run gdb

clang++ main.cpp -o main.exe -g
gdb --args ./main.exe

gdb commands

break func2
commands
    break func1
    # run a few commands when we hit func1()
    commands
    print c1
    backtrace
    end
    # continue to func1() breakpoint
    continue
end
run