8

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

tk.lee
  • 305
  • 1
  • 2
  • 11
  • Sounds like XY-problem to me. Why not `LD_PRELOAD` `malloc()` – EOF Nov 17 '14 at 23:25
  • @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 Nov 17 '14 at 23:28
  • @EOF - Could you give more details? I'm afraid your answer assumes more knowledge than many people have. – Craig S. Anderson Nov 17 '14 at 23:29
  • equal: https://stackoverflow.com/questions/5336403/is-there-any-way-to-set-a-breakpoint-in-gdb-that-is-conditional-on-the-call-stac – Ciro Santilli OurBigBook.com Jun 27 '17 at 04:24

1 Answers1

5

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.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Thanks. I will accept your answer. It give me an idea which is setting a global variable g_from_my_malloc=1 in my_malloc() before calling malloc and clear it after malloc return. This way, it is easy to break on malloc condition on if g_from_my_malloc!=1. If I guard it with mutex, it should work on multiple-thread app too. THANKS AGAIN!!! – tk.lee Nov 18 '14 at 19:33