Skip to content

Instantly share code, notes, and snippets.

@ayosec
Created January 29, 2012 11:55
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ayosec/1698469 to your computer and use it in GitHub Desktop.
Save ayosec/1698469 to your computer and use it in GitHub Desktop.
GDB commands to trace calls to malloc/free

Attach to a running process with

  gdb -x trace-dyn-mem -p $PID

After every malloc the returned value (the allocated address) will be read from the RAX (64 bits) register.

After every free the last item in the backtrace (the free itself) will be shown. With the libc6-dbg package installed you can see the address passed as the first argument of free.

break malloc
commands
silent
bt 4
up
break
commands
silent
clear
print $rax
cont
end
cont
end
break free
commands
silent
bt 1
cont
end
cont
@jpalecek
Copy link

jpalecek commented Feb 7, 2024

This will litter your gdb session with tons of breakpoints

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment