gdbsegmentation-faultsbrk

Can I get the current sbrk() limit in gdb?


I would like to know what the current sbrk() limit is to debug a SEGV I'm having a hard time with. My code looks good and works in most cases (only one case out of quite many breaks with SEGV.)

I'm thinking it may be in link with the fact that in this case we use a multi-threaded application, but I just can't pinpoint the problem right now. I'd like to compare the SEGV address with the sbrk() limit to see how off the address is. I'm thinking it could be a mmap() or some similar address which gets removed under my feet.


Solution

  • If the process still has a valid stack, you can call the sbrk function from libc:

    (gdb) print ((void *(*) (unsigned long)) sbrk)(0)
    $1 = (void *) 0x55555580e000
    

    The cast is not necessary if GDB was able to load debugging information for libc.

    (This may not work for statically linked binaries if sbrk is not linked into the program at all.)

    In principle it works with any function for which GDB can find a symbol. But whether it's safe to call a particular function from GDB depends on where exactly the program has stopped (e.g., calling malloc from within malloc is usually a bad idea).