Chinaunix首页 | 论坛 | 博客
  • 博客访问: 622925
  • 博文数量: 108
  • 博客积分: 46
  • 博客等级: 民兵
  • 技术积分: 1279
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-16 11:36
个人简介

坐井以观天

文章分类

全部博文(108)

文章存档

2014年(13)

2013年(90)

2012年(6)

分类: LINUX

2013-05-07 14:01:15

原文地址:使用gdb调试glibc 作者:Bean_lee

    我在学习glibc 在main函数之前的时候,其实已经有类似的需求了,只不过当时不够激进,没有想到调试glibc 。 这两天在学习NPTL线程栈及TLS相关的东西,实在是比较复杂,才动了调试glibc,单步跟踪一窥究竟的念头。在网上找了一些资料,解决了这个问题。中间遇到的很有意思的东西比较有价值的东西我都记录下来了,这篇文章不能算原创,基本来源于参考文献的两篇文章。向这两位同学致谢。
  glibc 查看版本号
   1 ldd 某可执行程序 查看链接情况
   2 直接执行链接的libc.so文件 。
  

 兄弟们可能会纳闷,SO文件怎么可以执行呢。 GLIBC耍了个trick。
  1. #ifdef HAVE_ELF
  2. /* This function is the entry point for the shared object.
  3.    Running the library as a program will get here. */

  4. extern void __libc_main (void) __attribute__ ((noreturn));
  5. void
  6. __libc_main (void)
  7. {
  8.   __libc_print_version ();
  9.   _exit (0);
  10. }
  11. #endif

  1. static const char banner[] =
  2. "GNU C Library "PKGVERSION RELEASE" release version "VERSION", by Roland McGrath et al.\n\
  3. Copyright (C) 2012 Free Software Foundation, Inc.\n\
  4. This is free software; see the source for copying conditions.\n\
  5. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
  6. PARTICULAR PURPOSE.\n\
  7. Compiled by GNU CC version "__VERSION__".\n"
  8. ...

  9. void
  10. __libc_print_version (void)
  11. {
  12.   __write (STDOUT_FILENO, banner, sizeof banner - 1);
  13. }
    glibc应该是和Ubuntu没啥关系,可是我们从上图中居然看到Ubuntu EGLIBC的字样,原因很简单,Ubuntu发行给glibc-2.15打了patch。
   这个问题的解决,要感谢CSDN的blog of tony 。再次向这位前辈致敬。

   gdb调试glibc
   原本查看版本好的目的是为了下载对应源码,开始用gdb调试glibc,可是上面截图中的Ubuntu EGLIBC字样给我泼了一盆冷水。何哉?我虽然有glibc-2.15的source code,无奈Ubuntu给glibc打了patch。 继续找办法。
   在Ubuntu下,首先是安装符号表。 我们知道,大多数的动态库DSO ,都是strip过的,没有任何调试信息,第一步是要给glibc添加符号表。保存在/usr/lib/debug/对应目录下。
   第一步 安装符号表
  1. sudo apt-get install libc6-dbg
安装完后,我们在可以看到如下:
  1. root@manu:/usr/lib/debug/lib/i386-linux-gnu# ll
  2. 总用量 10268
  3. drwxr-xr-x 3 root root    4096  5月 2 16:21 ./
  4. drwxr-xr-x 4 root root    4096 12月 2 15:16 ../
  5. -rwxr-xr-x 1 root root  555181 1月 28 20:30 ld-2.15.so*
  6. -rw-r--r-- 1 root root   49332 1月 28 20:30 libanl-2.15.so
  7. -rw-r--r-- 1 root root   13544 1月 28 20:30 libBrokenLocale-2.15.so
  8. -rwxr-xr-x 1 root root 6963435 1月 28 20:30 libc-2.15.so*
  9. -rw-r--r-- 1 root root   62923 1月 28 20:30 libcidn-2.15.so
  10. -rw-r--r-- 1 root root   68453 1月 28 20:30 libcrypt-2.15.so
  11. ...
  12. -rw-r--r-- 1 root root    6149 1月 28 20:30 libpcprofile.so
  13. -rwxr-xr-x 1 root root  565440 1月 28 20:30 libpthread-2.15.so*
  14. -rw-r--r-- 1 root root  201563 1月 28 20:30 libresolv-2.15.so
  15. -rw-r--r-- 1 root root  135143 1月 28 20:30 librt-2.15.so

  16. ....
    不太清楚为何某人安装/usr/lib/debug/lib/i386-linux-gnu的筒子可以阅读我的另一篇博文:程序减肥,strip,eu-strip 及其符号表 
   这仅仅是有了符号表,但是看不到代码,调试的过程中,无法看到代码。很难受。
   第二步:安装源码:
   Ubuntu很贴心的提供了安装方法:
  1. root@manu:~/code/c/classical# sudo apt-get source libc6-dev
    在我的当前路径下下载好了源码包,patch包,最终生成了eglibc-2.15路径。所有源码都在该目录下:
  1. drwxr-xr-x 72 root root 4096 5月 2 17:05 eglibc-2.15/
    精确的源码我们也有了,现在可以用gdb来调试glibc了,终于走到了这一步:
    第三步:gdb 调试glibc
   
我的目的是通过调试pthread_create创建过程,了解glibc和内核是如何实现NPTL线程的。 我的程序里面有pthread_create,我想单步跟踪glibc里面的__pthread_create_2_1函数。
   首先要告诉的gdb,源文件要去那个目录下寻找:
  1. (gdb) directory /home/manu/code/c/classical/eglibc-2.15/nptl
    OK,我们可以看下截图:
    
       
    这部分代码,主要参考自用GDB追踪glibc代码执行过程。感谢作者的分享,让我可以继续我的NPTL 线程栈和TLS的分析。希望近期能完成这个NPTL的学习。
   值得一提的是,分析Ubuntu提供的patch,可以看到,glibc查看版本号一节中的输出中含有Ubuntu EGLIC的原因:
  1. -rw-r--r-- 1 root root 10368576 1月 28 19:03 eglibc_2.15-0ubuntu10.4.diff
     

   
参考文献
用GDB追踪glibc代码执行过程
如何查看GLIBC的版本


阅读(914) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~