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?
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.
Have one breakpoint set the other breakpoint. To avoid gdb spaghetti use of define to create functions is recommended.
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;
}
clang++ main.cpp -o main.exe -g
gdb --args ./main.exe
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
func1the command to set the breakpoint forfunc2iff your program stops due to the first breakpoint. ofb.net/gnu/gdb/gdb_35.html#SEC35 - halex