Code Debugging with GDB - part 4: Basic Debug Glibc Source Code
Abstract
This article describes the preparations needed to debug glibc source code, including: how to install the glibc shared library with debugging information, how to get the glibc source code that matches the system, how to use the glibc source code in gdb, and through a simple Examples show this process.
Preparation for debugging glibc source code
Install libc-dbg
1 | sudo apt-get install libc-dbg |
Note: libc-dbg contains glibc shared libraries such as ld-X.X.so
, libc-X.X.so
and libm-X.X.so
with debugging information.
After installing libc-dbg, these shared libraries with debugging information are located in the /usr/lib/debug/lib/x86_64-linux-gnu
directory, as follows:
1 | ls /usr/lib/debug/lib/x86_64-linux-gnu |
We can use the readelf command to verify that these shared libraries are indeed with debugging information. for example:
1 | readelf -S /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.31.so | grep debug |
If there are sections beginning with .debug
in the above output, it means that there are debugging information.
Install the matching glibc source code
Note: If the glibc library that comes with the system, for example, the version of lib.c.so
is 2.31. Then, the source code of glibc 2.31 version downloaded directly from GNU official website may not match. The word “may” here means that if ubuntu does not modify the source code you want to debug, then it matches; otherwise, it does not match.
Install the matching glibc source code
1 | sudo apt-get install glibc-source |
Decompress the matching glibc source code
the installed glibc source code is located in the /usr/src/glibc/ directory, as follows:
1 | ls /usr/src/glibc |
After the installation is complete, you need to unzip it before you can use it. Suppose you want to extract it to the /home
directory, then execute the following command:
1 | sudo tar xvf /usr/src/glibc/glibc-2.31.tar.xz -C /home/ethanol/Desktop |
How to determine whether the glibc source code is compatible
If you use gdb to debug the glibc source code, a warning: Source file is more recent than executable
appears. Well, it usually means that the glibc source code is not compatible. At this point, we must install the supporting glibc source code before proceeding with source code debugging.
Debug glibc source code
Step 1: Prepare the sample program and generate an executable target file.
1 |
|
1 | g++ -o hello hello.cpp -g |
Step 2: Debug glibc
- Directory of debugging shared library.
1 | show debug-file-directory |
- Manually set the directory of debugging shared library.
1 | set debug-file-directory /usr/lib/debug |
- Specify the directory where the glibc source is located.
1 | # Start debug |