链接器 – 使用gdb将断点设置为LibC

为什么我不能在LibC中的导出函数中设置断点(使用gdb)?由于Libc是动态链接的,它必须包含它导出的函数的符号.我不应该能够为这些功能中的任何一个设置断点吗?

我只是试图做:

(gdb) b _IO_vfprintf@@GLIBC_2.2.5
Function "_IO_vfprintf@@GLIBC_2.2.5" not defined.

但是查看ELF文件中的dynamyc-symbols表,符号确实存在:

 127: 0000000000049cf0 20904 FUNC    GLOBAL DEFAULT   12 _IO_vfprintf@@GLIBC_2.2.5

最佳答案 我不知道你是如何想出你正在使用的符号名称,但这是我在我的系统上看到的(Ubuntu 14.04.1):

$objdump --dynamic-syms /lib/x86_64-linux-gnu/libc.so.6 |grep vfprintf
0000000000049cf0 g    DF .text  00000000000051a8  GLIBC_2.2.5 _IO_vfprintf
00000000001097e0 g    DF .text  0000000000000111  GLIBC_2.3.4 __vfprintf_chk
0000000000049cf0 g    DF .text  00000000000051a8  GLIBC_2.2.5 vfprintf

这是一个演示程序:

   #include <stdio.h>
   #include <stdarg.h>

int myprintf( const char *format, ... )
   {
   va_list ap;
   va_start( ap, format );
   int result = _IO_vfprintf( stderr, format, ap );
   va_end(ap);
   return result;
   }

int main()
   {
   myprintf( "hello world! %s %s %s\n", "abc", "def", "ghi" );
   myprintf( "goodbye world! %d %d\n", 123, 456 );
   return 0;
   }

我发现如果我第一次运行到main(),然后用b _IO_vfprintf设置一个断点,它就会少抱怨.

$make CFLAGS="-Wall -Werror -g" test && ./test

$objdump --disassemble test |grep vfprintf ## verify call isn't inlined
0000000000400480 <_IO_vfprintf@plt>:
  40061e:   e8 5d fe ff ff          callq  400480 <_IO_vfprintf@plt>

$gdb --quiet ./test
Reading symbols from ./test...done.

(gdb) b main
Breakpoint 1 at 0x400635: file test.c, line 16.

(gdb) run
Starting program: .../test 

Breakpoint 1, main () at test.c:16
16     myprintf( "hello world! %s %s %s\n", "abc", "def", "ghi" );

(gdb) b _IO_vfprintf
Breakpoint 2 at 0x7ffff7a5ecf4

(gdb) cont
Continuing.

Breakpoint 2, 0x00007ffff7a5ecf4 in vfprintf () from /lib/x86_64-linux-gnu/libc.so.6

是的,它有效……

将它提升到一个新的水平 – 您可以通过应用以下命令逐步执行libc源代码…

$sudo apt-get install libc6-dbg ## get the debug symbols
$apt-get source libc-dev-bin ## download the source (on Ubuntu or similar)
$gdb --quiet --directory ./eglibc-2.19/stdio-common ./test

相关说明here.

点赞