7
votes

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().

1
Sounds like XY-problem to me. Why not LD_PRELOAD malloc() - EOF
@EOF, my_mallc() is called from a C Macro that add addition info such as FILE, LINE such that I can keep track of alloc/free amount for each file:lineno entry. If I use LD_PRELOAD, I loss the FILE, LINE info from the preprocessor. - tk.lee
@EOF - Could you give more details? I'm afraid your answer assumes more knowledge than many people have. - Craig S. Anderson

1 Answers

5
votes

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.