21

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 source code when any of the malloc functions is called.

Currently what gdb says is: malloc.c: No such file or directory.

This guy here faced the same problem, but they do not mention a solution, ie how to actually step into the source code of malloc.

I am on Ubuntu server 14.04, and I have already tried to install the following: libc6-dbg, libc6-dev, and libc6-dbgsym. I don't even know if one of these packages might help, but installing the libc-dbgsym gives me the following error:

dpkg: error processing archive /var/cache/apt/archives/libc6-dbgsym_2.19-0ubuntu6.6_amd64.ddeb (--unpack):  trying to overwrite
    '/usr/lib/debug/usr/lib/x86_64-linux-gnu/audit/sotruss-lib.so', which
    is also in package libc6-dbg:amd64 2.19-0ubuntu6.6 dpkg-deb: error:
    subprocess paste was killed by signal (Broken pipe)
2
  • You need to find, or compile, a debug version of the libraries. Apr 29, 2015 at 22:26
  • @MartinJames that's the part I am failing at! :(
    – Paschalis
    Apr 29, 2015 at 22:41

2 Answers 2

31

The following worked for me. Not sure whether there is a better way.

  1. Install libc6-dbg (which you have already done): sudo apt-get install libc6-dbg
  2. Install the eglibc-source package (ubuntu actually uses eglibc): sudo apt-get install eglibc-source.
  3. Unpack the tar file that was installed in /usr/src/glibc: /usr/src/glibc $ sudo tar xvf eglibc-2.19.tar.xz
  4. Crank up gdb and add in the path to the malloc source: (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)

3
  • Works like charm! Also, step 4 can be added in .gdbinit!
    – Paschalis
    Apr 29, 2015 at 23:26
  • 1
    Great! One somewhat related gotcha (which it sounds like you know about). But for future readers: Don't break on malloc itself as libc does all sorts of symbol gymnastics so that the malloc symbol is not the actual malloc that is invoked. Instead break in the caller and step into the malloc call.
    – kaylum
    Apr 29, 2015 at 23:40
  • 7
    For Ubuntu 18.04 users, seems that eglibc-source isn't a thing anymore. Seems to just be glibc-source now, based on my search through the repos. Jun 17, 2019 at 1:32
3

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).

6
  • I should have downvoted just because you said "gdb is not a wizard"!
    – Paschalis
    Apr 29, 2015 at 22:33
  • Jokes aside, I think I did that. I am on Ubuntu, and I believe libc6-dbg is the equivalent of what you suggested!
    – Paschalis
    Apr 29, 2015 at 22:34
  • @Paschalis I am sorry, I didn't have any negative intention with that. I removed this from the answer.
    – peterh
    Apr 29, 2015 at 22:34
  • I was joking :) I 've installed a debug version of the libc. Still does not work! I 've updated my question a while ago!
    – Paschalis
    Apr 29, 2015 at 22:40
  • @Paschalis Is the glibc source also on your system? Can you see its files? It is big.
    – peterh
    Apr 29, 2015 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.