Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

linux

gdb

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

like image 383
tk.lee Avatar asked Nov 17 '14 23:11

tk.lee


1 Answers

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.

like image 155
Employed Russian Avatar answered Sep 30 '22 23:09

Employed Russian