Skip to content

Instantly share code, notes, and snippets.

@zhangw
Forked from ayosec/README.md
Created June 14, 2023 08:10
Show Gist options
  • Save zhangw/587ed0d0026a57143f0f68d48e81a7b2 to your computer and use it in GitHub Desktop.
Save zhangw/587ed0d0026a57143f0f68d48e81a7b2 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment