How to set conditional breakpoint if malloc returns NULL via gdb

Source code example:

#include <stdio.h> #include <stdlib.h> #include <errno.h> #define GIGABYTE 1024*1024*1024 int main (void) { void *foo; int result; foo = (void *) malloc (GIGABYTE*5); result = errno; if (foo != NULL) { return 2; } else { fprintf (stderr, "ERROR: %d\n", result); return 1; } return 0; } 

Question:

  • How to tell gdb ( # gdb -silent ./huge_malloc ) to stop / stop if malloc() returns 0x0 , without if foo is 0x0
+7
source share
2 answers

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.

+6
source

Could you just write a wrapper around malloc that will save the return value, and then set a conditional breakpoint to this value?

+2
source

All Articles