tk.lee
tk.lee

Reputation: 305

gdb: conditionally break on function only if the caller function is not equal certain value

In my project, I have my_malloc() that will call malloc().

I like to set up the conditional breakpoint in gdb such that gdb will break into "gdb>" only when the caller function of malloc() is not equal to my_mallc().

Is it possible?

The goal is to id all that code that is calling malloc() directly and didn't go thru my_malloc().

Upvotes: 8

Views: 1199

Answers (1)

Employed Russian
Employed Russian

Reputation: 213526

I like to set up the conditional breakpoint in gdb such that gdb will break into "gdb>" only when the caller function of malloc() is not equal to my_mallc().

In other words, you want to break on malloc when it is not called by my_malloc.

One way to do that is to set three breakpoints: one on malloc, one on my_malloc entry, and one on my_malloc return. Then (assuming the breakpoints are 1, 2 and 3 respectively).

(gdb) commands 2
silent                # don't announce hitting breakpoint #2
disable 1             # don't stop when malloc is called within my_malloc
continue              # continue execution when BP#2 is hit
end

(gdb) commands 3
silent
enable 1              # re-enable malloc breakpoint
continue
end

This technique only works for single-threaded applications.

Upvotes: 5

Related Questions