3

I wanted to step into the code of ld.so whenever it will be used in my normal c code. I am trying to code flow through GDB in the TUI mode where you can see both source code and assembly as you step over the code.

For this I also installed libc-dbg binutils-source package from ubuntu package manager. GDB can find the debug symbols for the ld.so itself and I can step at the instruction level that is using si but I cant step at the source level as GDB is not able to find the source for ld.so and shows NO Source Available.

How can I make GDB find the source for ld.so so that I can also see which line in the ld.so source is actually being executed.

I am using Ubuntu 12.10 64bit with GCC 4.8.2

Community
  • 1
  • 1
abhi
  • 3,476
  • 5
  • 41
  • 58

2 Answers2

4

If you've libc's source code available, you can add sources to gdb's source path with dir command: Source_Path

Edit: To debug libc related files (in a Ubuntu distro) you would need to:

  1. Get libc's debug information by installing libc6-dbg packet.
  2. Get libc's source code by enabling source repositories (by running software-sources and and checking "enable source code repositories") and running apt-get source libc6
  3. Add libc's debug information into LD_LIBRARY_PATH: export LD_LIBRARY_PATH=/usr/lib/debug or LD_LIBRARY_PATH=/usr/lib/debug gdb <application>
  4. Add full path to c file to gdb's source path, this is: dir directory_path_libc_source/stdio-common
jcm
  • 2,568
  • 14
  • 18
  • I tried downloading the source through apt but got this error apt-get source libc6 Reading package lists... Done Building dependency tree Reading state information... Done Picking 'eglibc' as source package instead of 'libc6' E: Unable to find a source package for eglibc. So I did a wget the libc source for the version I have. I set the source_path using set directories absolute path to the root of the libc source. But I still can not see the source code for libc in GDB while stepping – abhi Nov 21 '13 at 10:07
  • @abhi This means that you miss enabling source repositories. You can enable them from "software sources" (you can reach it from "software center"'s edit menu or, from command line, running `sofware-sources`), and you will see a "enable source code repositories" – jcm Nov 21 '13 at 10:20
  • I corrected the apt-get source issue as you directed and downloaded the libc6 source in a directory. Started the gdb with simple test program to debug which had only one printf() and on stepping the code I still cant see the source. See the screen shot after i step on the printf() https://www.dropbox.com/s/0z8btk672vfaxot/gdb.png – abhi Nov 21 '13 at 10:45
  • @abhi Have you added libc's source directory with `dir` command? I mean, from your capture, it seems that it's not in gdb's source patch. Can you also try running `info sources` to check it? – jcm Nov 21 '13 at 10:50
  • This is exactly what I did: 1. gdb ./test -> dir directory_path_libc_source(the path is shown in show dir) the set a breakpoint before printf() and run. On hitting Breakpoint I did info sources (https://www.dropbox.com/s/z7b2rcysxqoexju/gdb1.png) stepped but cant see the code. On hitting printf() it says _printf (format=0x400594 "ABHI") at printf.c:30 30 printf.c: No such file or directory. (gdb) s 34 in printf.c (gdb) s 30 in printf.c – abhi Nov 21 '13 at 11:09
  • @abhi Have you added libc's debuginfor files into LD_LIBRARY_PATH? – jcm Nov 21 '13 at 11:24
  • I tried with adding /usr/lib/debug/lib/ to LD_LIBRARY_PATH and it also did not help. Am I missing something else? Thanks for answering so many times. – abhi Nov 21 '13 at 11:42
  • @abhi OK, I've tested and I think I know the problem. You should add full path to c file, this is: dir directory_path_libc_source/stdio-common. This together with LD_LIBRARY_PATH when you run gdb: LD_LIBRARY_PATH=/usr/lib/debug gdb – jcm Nov 21 '13 at 14:40
0

Download the Glibc source and path the same with gdb before debugging. Check this ubuntu blog

Parthiban
  • 2,130
  • 2
  • 14
  • 27
  • I tried the command given for gdb in the link you provided and gave it the path of the dir where I have the glibc source. I used a test program which had only a main with printf("ASAS") but on reaching the printf() it did not step into the printf() code in libc. Am I missing something – abhi Nov 21 '13 at 10:26