Can GDB set a breakpoint on a sequence of function calls?
Asked Answered
C

1

7

I'd like to inspect some global variables before a crash happens. The issue only reproduces on a certain stack trace and setting a breakpoint on the innermost function (or any other from the stack) will not get me close enough.

Can I achieve the result of breaking only when the top of the stack contains something like this ?

#0 __GI_connect
#1 curl_connect
#2 get_file
#3 init_assets

Just doing

b init_assets
c
b get_file
c
...

doesn't work since init_assets is called multiple times and it doesn't call curl every time, so gdb will break in unrelated code that uses curl.

Later edit: another way is:

b inner_func
ignore 1 10000
r
# app crashes
info b

Breakpoint 1 has been hit 10 times.

Then you remove the breakpoint, add it back and only ignore 9 times. When you run the app again, gdb will stop on the 10th time -- when inner_func crashes.

You can also record the app in mozilla's rr if the execution varies too much.

Churchman answered 26/3, 2019 at 12:11 Comment(0)
O
16

You can use a conditional breakpoint with the the $_caller_is convenience function. Something like this:

(gdb) break connect
Breakpoint 1 at 0x7ffff7ee6820
(gdb) cond 1 $_caller_is("curl_connect") && $_caller_is("get_file", 2) && $_caller_is("init_assets", 3)
Organotherapy answered 26/3, 2019 at 13:31 Comment(2)
Wow! The _caller_is is convenient. Previous answer to this question: https://mcmap.net/q/442695/-is-there-any-way-to-set-a-breakpoint-in-gdb-that-is-conditional-on-the-call-stackPortable
Is it possible to use $_caller_is without debugging symbols, e.g. match it against the stack frame address?Literalminded

© 2022 - 2024 — McMap. All rights reserved.