13

Having trouble stepping into string.h in GDB 7.5. Here's a simple example program:

Source code:

#include <stdio.h>
#include <string.h>

int main() {
    char str1[20];
    strcpy(str1, "STEP INTO ME\n");
    printf(str1);
}

Compiled: ~$ gcc -g foo.c

Invoked: ~$ gdb -q ./a.out

GDB:

(gdb) break 5
Breakpoint 1 at 0x8048471: file foo.c, line 6.
(gdb) break strcpy
Function "strcpy" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y

Breakpoint 2 (strcpy) pending.
(gdb) run 
Starting program: /home/user/a.out 

Breakpoint 1, main () at foo.c:6
6               strcpy(str_a, "Hello, world!\n");
(gdb) step
7               printf(str_a);

Shouldn't I be in the string library at this point? Instead it continues to the printf().


EDIT:

Scott's suggestion "worked", but not in the expected manner.

Breakpoint 1, main () at foo.c:6
6               strcpy(str_a, "Hello, world!\n");
(gdb) i r $eip
eip            0x80484a1        0x80484a1 <main+21>
(gdb) step

Breakpoint 2, __strcpy_ssse3 () at ../sysdeps/i386/i686/multiarch/strcpy-ssse3.S:78
78      ../sysdeps/i386/i686/multiarch/strcpy-ssse3.S: No such file or directory.
(gdb) i r $eip
eip            0xb7e9c820       0xb7e9c820 <__strcpy_ssse3> 

I am surprised at the directory in 78... expected something like: /lib/.../cmov/libc.so.6. And the claim that there is no such file or directory.

3
  • @skirkpatrick: This is wrong, at least on most systems (OS X, Debian, Ubuntu, Fedora...). Shared libraries are compiled with debugging symbols. The real question is whether those symbols are installed. Mar 9, 2013 at 1:33
  • @DietrichEpp: After a quick search, it looks like you're right. Sorry for the misinformation; I'll remove my comment. Mar 9, 2013 at 1:38
  • The debug symbols are not part of the .so which reside in your LD_LIBRARY_PATH. Instead they reside in a directory which gdb searches such as /usr/lib/debug. This info can be obtained by running show debug-file-directory in gdb. These files contain only debug information without any .text or .data sections of the .so. And yes you need to install them separately.
    – Tuxdude
    Mar 9, 2013 at 1:49

3 Answers 3

18

Recompile your code with gcc -fno-builtin -g foo.c and the gdb step command will work. (See -fno-builtin documentation). Otherwise small strcpy(), memcpy() calls would often be translated into open coded data movement instructions, e.g. on x86-64:

4   int main() {
   0x000000000040052c <+0>: push   %rbp
   0x000000000040052d <+1>: mov    %rsp,%rbp
   0x0000000000400530 <+4>: sub    $0x20,%rsp

5       char str1[20];
6       strcpy(str1, "STEP INTO ME\n");
   0x0000000000400534 <+8>: lea    -0x20(%rbp),%rax
   0x0000000000400538 <+12>:    movl   $0x50455453,(%rax)
   0x000000000040053e <+18>:    movl   $0x544e4920,0x4(%rax)
   0x0000000000400545 <+25>:    movl   $0x454d204f,0x8(%rax)
   0x000000000040054c <+32>:    movw   $0xa,0xc(%rax)

7       printf(str1);
   0x0000000000400552 <+38>:    lea    -0x20(%rbp),%rax
   0x0000000000400556 <+42>:    mov    %rax,%rdi
   0x0000000000400559 <+45>:    mov    $0x0,%eax
   0x000000000040055e <+50>:    callq  0x400410 <printf@plt>

8   }
   0x0000000000400563 <+55>:    leaveq 
   0x0000000000400564 <+56>:    retq

You can see the strpcy() call being compiled into multiple MOV instructions.

gcc -fno-builtin compiles the same program into:

4   int main() {
   0x000000000040057c <+0>: push   %rbp
   0x000000000040057d <+1>: mov    %rsp,%rbp
   0x0000000000400580 <+4>: sub    $0x20,%rsp

5       char str1[20];
6       strcpy(str1, "STEP INTO ME\n");
   0x0000000000400584 <+8>: lea    -0x20(%rbp),%rax
   0x0000000000400588 <+12>:    mov    $0x400660,%esi
   0x000000000040058d <+17>:    mov    %rax,%rdi
   0x0000000000400590 <+20>:    callq  0x400450 <strcpy@plt>

7       printf(str1);
   0x0000000000400595 <+25>:    lea    -0x20(%rbp),%rax
   0x0000000000400599 <+29>:    mov    %rax,%rdi
   0x000000000040059c <+32>:    mov    $0x0,%eax
   0x00000000004005a1 <+37>:    callq  0x400460 <printf@plt>

8   }
   0x00000000004005a6 <+42>:    leaveq 
   0x00000000004005a7 <+43>:    retq 

and you can see the call to <strcpy@plt>.

Assuming you wanted to step into strcpy() to study its implementation, you'd want to have debug info for libc.so installed. Unfortunately the way to get debug info differs between Linux distros. On Fedora it's as simple as debuginfo-install glibc. It takes more steps on Ubuntu and Debian. This RPM DPKG Rosetta Stone page have links to instructions for Fedora, Ubuntu and Debian (search for debuginfo).

Since you're on Ubuntu 12.10 and actually want to see the strcpy() assembly source code:

$ sudo apt-get install libc6-dbg
$ sudo apt-get source libc6-dev
$ gdb ./a.out
(gdb) directory eglibc-2.15/sysdeps
Source directories searched: /home/scottt/eglibc-2.15/sysdeps:$cdir:$cwd
(gdb) break strcpy
Breakpoint 1 at 0x400450
(gdb) run
Starting program: /home/scottt/a.out 

Breakpoint 1, __strcpy_sse2 () at ../sysdeps/x86_64/multiarch/../strcpy.S:32
32      movq %rsi, %rcx     /* Source register. */
9
  • Is t.c supposed to represent foo.c (in this case) or is that a file I am supposed to link with?
    – d0rmLife
    Mar 9, 2013 at 1:41
  • I tried this. Steps in, but it goes to an unexpected directory and says No such file or directory. I'll post the results in my question... the formatting will be brutal in a comment.
    – d0rmLife
    Mar 9, 2013 at 1:50
  • @d0rmLife: it looks like strcpy is implemented in assembly in glibc
    – teppic
    Mar 9, 2013 at 1:52
  • @d0rmLife, t.c was indeed just my shorter name for foo.c :) After you step into an optimized implementation of strcpy() is where you really want to have debug info for libc.so installed to make sense of things.
    – scottt
    Mar 9, 2013 at 1:53
  • I wanted to make sure it wasn't some unusual file! :) ...posted the output in my question. I would appreciate any ideas you might have on the directory.
    – d0rmLife
    Mar 9, 2013 at 1:56
3

You tried to set a breakpoint for a function defined in the string library usually part of the standard C library - libc.so

And as gdb informs you:

(gdb) break strcpy
Function "strcpy" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y

Breakpoint 2 (strcpy) pending.

the library is not loaded yet.

But the real problem is, even when the library is loaded, if the library i.e. libc.so does not have debug symbols in it, you would not be able to step through the code within the library using gdb.

You could enable verbose mode to see which symbols, gdb is able to load:

(gdb) b main
Breakpoint 1 at 0x400914: file test.cpp, line 7.
(gdb) set verbose on
(gdb) run
Starting program: /home/agururaghave/.scratch/gdb-test/test 
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Reading symbols from system-supplied DSO at 0x7ffff7ffb000...(no debugging symbols found)...done.
Reading symbols from /usr/lib64/libstdc++.so.6...(no debugging symbols found)...done.
Registering libstdc++-v6 pretty-printer for /usr/lib64/libstdc++.so.6 ...
Loaded symbols for /usr/lib64/libstdc++.so.6
Reading symbols from /lib64/libm.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libm.so.6
Reading symbols from /lib64/libgcc_s.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/libgcc_s.so.1
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6

Breakpoint 1, main () at test.cpp:7
7       bool result = myObj1 < myObj2;

This line for example tells you whether it was able to get the symbols for libc.so:

Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.

You could then figure out where the debug symbols are picked up from using show debug-file-directory:

(gdb) show debug-file-directory 
The directory where separate debug symbols are searched for is "/usr/lib/debug".

As you see /usr/lib/debug here does not contain the full .so with debug symbols. Instead it only has the debug info without any .text or .data sections of the actual libc.so which the program uses for execution.

The solution to install the debug info for libraries would be distro specific.

I think the package is called libc6-dbg on the debian based distros. On my openSUSE machine, it seems to be called glibc-debuginfo

BTW, +1 on scottt's suggestion of using -fno-builtin so that gcc does not use its built-in methods for functions like strcpy and other standard ones defined as part of C standard.

7
  • Whether libc.so has debug info is not the problem here. Since the program is dynamically linked, if there exists a call to strcpy() that goes through the PLT, gdb would catch it. The problem here is that the gcc option -fno-buitin was not passed, and the call to strcpy() in the source was translated into store instructions.
    – scottt
    Mar 9, 2013 at 1:38
  • Any idea what the package might be called for Ubuntu 12?
    – d0rmLife
    Mar 9, 2013 at 1:40
  • Try sudo apt-get install libc6-dbg
    – Tuxdude
    Mar 9, 2013 at 1:40
  • @scottt - Good catch, you're actually right. I didn't know gcc uses builtins for functions like strcpy.
    – Tuxdude
    Mar 9, 2013 at 1:44
  • Got the package, still not stepping in. Does it require special options in compiling/invoking?
    – d0rmLife
    Mar 9, 2013 at 1:45
0

You probably don't have any symbols for your C library. Try stepi, but be prepared to see only assembly instructions.

4
  • @d0rmLife, are you sure? How did you check?
    – Carl Norum
    Mar 9, 2013 at 1:33
  • By running GDB and using stepi. No, I guess I'm not positive but it didn't seem to. At least, not in the usual way...
    – d0rmLife
    Mar 9, 2013 at 1:36
  • @d0rmLife, and the PC didn't change when you did that? That surprises me.
    – Carl Norum
    Mar 9, 2013 at 1:37
  • Output was different, but it didn't acknowledge being in a different library (expected something like /lib/...libc.so...)
    – d0rmLife
    Mar 9, 2013 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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