Include source code of malloc.c in gdb?

后端 未结 2 1097
囚心锁ツ
囚心锁ツ 2020-12-02 14:02

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

相关标签:
2条回答
  • 2020-12-02 14:24

    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)

    0 讨论(0)
  • 2020-12-02 14:31

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

    0 讨论(0)
提交回复
热议问题