How can I include/view the source code of malloc
in gdb?
I want to do a step by step execution in gdb
, and step into malloc.c
The following worked for me. Not sure whether there is a better way.
sudo apt-get install libc6-dbg
sudo apt-get install eglibc-source
./usr/src/glibc $ sudo tar xvf eglibc-2.19.tar.xz
(gdb) dir /usr/src/glibc/eglibc-2.19/malloc
(gdb) n
13 char *c = malloc(100);
(gdb) s
__GI___libc_malloc (bytes=100) at malloc.c:2876 2876
{
(gdb)
Gdb can only show the source codes because the debug-compiled binaries contain references between the binary code and the source files.
malloc()
is in the C library. On normal systems, it is not compiled with debug metadata, and its sources are also not installed in the system.
But they are reachable, you only need to install the debug versions of these libraries. For example, on debian an apt-get install glibc-debug
or similar will do it. On SuSE, a zipper in libc6-debug
(afaik, maybe the exact package names could be a little bit differ).