You can define the malloc exit point and put a conditional breakpoint there. For example:
(gdb) tbreak main Breakpoint 1 at 0x4005c4: file tc, line 13. (gdb) r Starting program: /var/tmp/a.out main () at tc:13 13 foo = malloc (64); (gdb) br *__libc_malloc+211 if $rax==0 Breakpoint 2 at 0x7f26d143ea93 (gdb) n 14 foo = malloc (GIGABYTE*64); (gdb) p foo $1 = (void *) 0x21dc010 (gdb) n Breakpoint 2, 0x00007f26d143ea93 in malloc () from /lib/libc.so.6
Note that I added a call to malloc , which succeeds first to illustrate that the breakpoint only starts for the return value NULL . The breakpoint address may be different in libc versions, I found it by going through malloc with nexti until I am in the ret command.
Jester
source share