Another hint (in addition of Oli's answer), when chasing memory bugs with the gdb
debugger, is to disable address space layout randomization, with e.g.
echo 0 > /proc/sys/kernel/randomize_va_space
After doing that, two consecutive runs of the same deterministic program will usually mmap
regions at the same addresses (from one run to another), and this helps a lot debugging with gdb
(because then malloc
usually gives the same result from one run to another, at the same given location in the run).
You can also use the watch
command of gdb
. In particular, if in a first run (with ASLR disabled) you figure that the location 0x123456 is changing unexepectedly, you could give gdb
the following command in its second run:
watch * (void**) 0x123456
Then gdb
will break when this location changes (sadly, it has to be mmap
-ed already).