使用gdb调试glibc

最近在研究signal的过程中,发现很多问题对源码进行一些简单的分析就可以迎刃而解。其实之前已经类似的想法了,能不能用gdb在调试程序的同时也对库函数进行调试。不过这种方法最深只能调试到系统调用,对于内核的运行情况,直接用gdb还是无法调试。就我现在看到的方法来说,跟踪调试内核的运行情况必须要虚拟机。

好了书归正传,今天让我们来看看如何使用gdb调试glibc。开始之前还是要先感谢我参考的原blog:http://blog.chinaunix.net/uid-24774106-id-3642925.html 最近我的很多文章都是参考这位大神的blog,在此也向大家安利以下这位大神的blog。

1. 首先安装glibc的符号表,命令如下:

sudo apt-get install libc6-dbg

其中libc6-dbg的内容是:“This package contains the detached debugging symbols for the GNU C library.” 翻译过来就是这个包的内容是被分离的GNU C库的调试符号。

对于libc6的其他包可参考如下网址:https://launchpad.net/ubuntu/+source/glibc/2.21-0ubuntu4

2. 再来是安装glibc的源文件,命令如下:

sudo apt-get source libc6-dev

其中libc6-dev的内容是:“Contains the symlinks, headers, and object files needed to compile and link programs which use the standard C library.” 翻译以下吧,这个包包含了symlinks(是啥不知道)、头文件与目标文件,以上文件是使用标准c库编译链接程序时所需,这个包最为关键,有了头文件、库文件我们写的程序就能运行了。

其中source选项是指下载该包的源代码,对于apt-get命令的其他选项,请见这篇文章:http://jingyan.baidu.com/article/22a299b51648e09e19376ae7.html

以上命令运行结果如下:

sudo apt-get source libc6-dev
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
选择 glibc 作为源代码包而非 libc6-dev
提示:glibc 的打包工作被维护于以下位置的 Svn 版本控制系统中:
svn://svn.debian.org/pkg-glibc/glibc-package/
需要下载 13.7 MB 的源代码包。
获取:1 http://cn.archive.ubuntu.com/ubuntu/ vivid/main glibc 2.21-0ubuntu4 (dsc) [8,672 B]
获取:2 http://cn.archive.ubuntu.com/ubuntu/ vivid/main glibc 2.21-0ubuntu4 (tar) [12.7 MB]
获取:3 http://cn.archive.ubuntu.com/ubuntu/ vivid/main glibc 2.21-0ubuntu4 (diff) [994 kB]
下载 13.7 MB,耗时 12秒 (1,068 kB/s)                                           
gpgv: 于 2015年03月26日 星期四 14时09分22秒 CST 创建的签名,使用 RSA,钥匙号 B1CDE58F
gpgv: 无法检查签名:找不到公钥
dpkg-source: 警告: 对 ./glibc_2.21-0ubuntu4.dsc 校验签名失败
dpkg-source: info: extracting glibc in glibc-2.21
dpkg-source: info: unpacking glibc_2.21.orig.tar.xz
dpkg-source: info: unpacking glibc_2.21-0ubuntu4.debian.tar.xz
dpkg-source: info: applying git-updates.diff
dpkg-source: info: applying locale/check-unknown-symbols.diff
dpkg-source: info: applying locale/fix-LC_COLLATE-rules.diff
dpkg-source: info: applying locale/preprocessor-collate-uli-sucks.diff
dpkg-source: info: applying locale/preprocessor-collate.diff
dpkg-source: info: applying locale/locale-print-LANGUAGE.diff
dpkg-source: info: applying locale/LC_IDENTIFICATION-optional-fields.diff
dpkg-source: info: applying locale/LC_COLLATE-keywords-ordering.diff
dpkg-source: info: applying localedata/local-all-no-archive.diff
dpkg-source: info: applying localedata/sort-UTF8-first.diff
dpkg-source: info: applying localedata/supported.diff
dpkg-source: info: applying localedata/locale-eo_EO.diff
dpkg-source: info: applying localedata/locale-eu_FR.diff
dpkg-source: info: applying localedata/new-valencian-locale.diff
dpkg-source: info: applying localedata/locale-ku_TR.diff
dpkg-source: info: applying localedata/tl_PH-yesexpr.diff
dpkg-source: info: applying localedata/fo_FO-date_fmt.diff
dpkg-source: info: applying localedata/locales_CH.diff
dpkg-source: info: applying localedata/locales-fr.diff
dpkg-source: info: applying localedata/locale-en_DK.diff
dpkg-source: info: applying localedata/locale-csb_PL.diff
dpkg-source: info: applying localedata/locale-zh_TW.diff
dpkg-source: info: applying localedata/locale-se_NO.diff
dpkg-source: info: applying localedata/tailor-iso14651_t1.diff
dpkg-source: info: applying localedata/locale-hsb_DE.diff
dpkg-source: info: applying localedata/tailor-iso14651_t1-common.diff
dpkg-source: info: applying localedata/fix-lang.diff
dpkg-source: info: applying localedata/fr_CA-first_weekday.diff
dpkg-source: info: applying localedata/fr_CH-first_weekday.diff
dpkg-source: info: applying localedata/locale-de_LI.diff
dpkg-source: info: applying localedata/submitted-bz9725-locale-sv_SE.diff
dpkg-source: info: applying localedata/first_weekday.diff
dpkg-source: info: applying localedata/locale-C.diff
dpkg-source: info: applying localedata/locale-nb_NO.diff
dpkg-source: info: applying localedata/submitted-en_AU-date_fmt.diff
dpkg-source: info: applying localedata/submitted-es_MX-decimal_point.diff
dpkg-source: info: applying localedata/submitted-locale-bo.diff
dpkg-source: info: applying localedata/submitted-it_IT-thousands_sep.diff
dpkg-source: info: applying alpha/local-gcc4.1.diff
dpkg-source: info: applying alpha/submitted-dl-support.diff
dpkg-source: info: applying alpha/submitted-rtld-fPIC.diff
dpkg-source: info: applying alpha/local-string-functions.diff
dpkg-source: info: applying amd64/local-blacklist-on-TSX-Haswell.diff
dpkg-source: info: applying arm/local-ioperm.diff
dpkg-source: info: applying arm/local-sigaction.diff
dpkg-source: info: applying arm/unsubmitted-ldconfig-cache-abi.diff
dpkg-source: info: applying arm/unsubmitted-ldso-abi-check.diff
dpkg-source: info: applying arm/local-soname-hack.diff
dpkg-source: info: applying arm/local-vfp-sysdeps.diff
dpkg-source: info: applying arm/unsubmitted-ldso-multilib.diff
dpkg-source: info: applying arm/cvs-arm-sfi_breg.diff
dpkg-source: info: applying arm/cvs-memcpy-memmove-always-bx.diff
dpkg-source: info: applying arm/local-arm-futex.diff
dpkg-source: info: applying hppa/local-inlining.diff
dpkg-source: info: applying hppa/local-stack-grows-up.diff
dpkg-source: info: applying hppa/local-elf-make-cflags.diff
dpkg-source: info: applying hppa/local-fcntl-osync.diff
dpkg-source: info: applying hppa/local-fpu.diff
dpkg-source: info: applying hppa/local-shmlba.diff
dpkg-source: info: applying hppa/local-pthread_spin_unlock.diff
dpkg-source: info: applying hurd-i386/local-enable-ldconfig.diff
dpkg-source: info: applying hurd-i386/tg-libpthread_depends.diff
dpkg-source: info: applying i386/local-biarch.diff
dpkg-source: info: applying i386/local-cmov.diff
dpkg-source: info: applying i386/local-cpuid-level2.diff
dpkg-source: info: applying i386/submitted-i686-timing.diff
dpkg-source: info: applying i386/unsubmitted-quiet-ldconfig.diff
dpkg-source: info: applying kfreebsd/submitted-waitid.diff
dpkg-source: info: applying kfreebsd/submitted-auxv.diff
dpkg-source: info: applying kfreebsd/local-config_h_in.patch
dpkg-source: info: applying kfreebsd/local-grantpt.diff
dpkg-source: info: applying kfreebsd/local-sysdeps.diff
dpkg-source: info: applying kfreebsd/local-fbtl.diff
dpkg-source: info: applying kfreebsd/local-fbtl-depends.diff
dpkg-source: info: applying kfreebsd/local-scripts.diff
dpkg-source: info: applying kfreebsd/local-getaddrinfo-freebsd-kernel.diff
dpkg-source: info: applying kfreebsd/local-no-execfn.diff
dpkg-source: info: applying m68k/local-dwarf2-buildfix.diff
dpkg-source: info: applying m68k/local-fpic.diff
dpkg-source: info: applying m68k/local-reloc.diff
dpkg-source: info: applying m68k/submitted-gcc34-seccomment.diff
dpkg-source: info: applying mips/local-r10k.diff
dpkg-source: info: applying mips/submitted-rld_map.diff
dpkg-source: info: applying powerpc/local-powerpc8xx-dcbz.diff
dpkg-source: info: applying powerpc/local-tune-power8.diff
dpkg-source: info: applying powerpc/cvs-ppc-sqrt.diff
dpkg-source: info: applying powerpc/cvs-ppc-sqrtf.diff
dpkg-source: info: applying powerpc/cvs-ppc-pow.diff
dpkg-source: info: applying powerpc/cvs-ppc-feraiseexcept.diff
dpkg-source: info: applying powerpc/cvs-power7-strncpy.diff
dpkg-source: info: applying powerpc/cvs-ppc-tabort-le.diff
dpkg-source: info: applying s390/submitted-nexttowardf.diff
dpkg-source: info: applying sh4/local-fpscr_values.diff
dpkg-source: info: applying sparc/local-sparcv9-target.diff
dpkg-source: info: applying sparc/submitted-timing.diff
dpkg-source: info: applying all/local-alias-UTF-8.diff
dpkg-source: info: applying all/local-alias-et_EE.diff
dpkg-source: info: applying all/local-remove-manual.diff
dpkg-source: info: applying all/local-ru_RU.diff
dpkg-source: info: applying all/local-ldd.diff
dpkg-source: info: applying all/local-nis-shadow.diff
dpkg-source: info: applying all/submitted-po-fr-fixes.diff
dpkg-source: info: applying any/local-rtlddir-cross.diff
dpkg-source: info: applying any/local-asserth-decls.diff
dpkg-source: info: applying any/local-bindresvport_blacklist.diff
dpkg-source: info: applying any/local-allocalim-header.diff
dpkg-source: info: applying any/local-fhs-linux-paths.diff
dpkg-source: info: applying any/local-fhs-nscd.diff
dpkg-source: info: applying any/local-ld-multiarch.diff
dpkg-source: info: applying any/local-ldso-disable-hwcap.diff
dpkg-source: info: applying any/local-ldconfig.diff
dpkg-source: info: applying any/local-ldconfig-fsync.diff
dpkg-source: info: applying any/local-libgcc-compat-main.diff
dpkg-source: info: applying any/local-libgcc-compat-ports.diff
dpkg-source: info: applying any/local-libgcc-compat-abilists.diff
dpkg-source: info: applying any/local-mktemp.diff
dpkg-source: info: applying any/local-no-pagesize.diff
dpkg-source: info: applying any/local-nss-upgrade.diff
dpkg-source: info: applying any/local-stubs_h.diff
dpkg-source: info: applying any/local-stdio-lock.diff
dpkg-source: info: applying any/local-tcsetaddr.diff
dpkg-source: info: applying any/local-disable-test-tgmath2.diff
dpkg-source: info: applying any/local-tst-mktime2.diff
dpkg-source: info: applying any/submitted-nis-netgrp.diff
dpkg-source: info: applying any/submitted-longdouble.diff
dpkg-source: info: applying any/local-nss-overflow.diff
dpkg-source: info: applying any/submitted-missing-etc-hosts.diff
dpkg-source: info: applying any/submitted-bits-fcntl_h-at.diff
dpkg-source: info: applying any/local-no-SOCK_NONBLOCK.diff
dpkg-source: info: applying any/submitted-nptl-invalid-td.patch
dpkg-source: info: applying any/local-dlfptr.diff
dpkg-source: info: applying any/submitted-string2-strcmp.diff
dpkg-source: info: applying any/local-ldconfig-multiarch.diff
dpkg-source: info: applying any/local-tst-writev.diff
dpkg-source: info: applying any/local-disable-libnss-db.diff
dpkg-source: info: applying any/local-revert-bz13979.diff
dpkg-source: info: applying any/local-tst-eintr1-eagain.diff
dpkg-source: info: applying any/unsubmitted-ldso-machine-mismatch.diff
dpkg-source: info: applying any/unsubmitted-dlopen-static-crash.diff
dpkg-source: info: applying any/unsubmitted-scanf-includes.diff
dpkg-source: info: applying any/local-ldconfig-ignore-ld.so.diff
dpkg-source: info: applying any/local-no-malloc-backtrace.diff
dpkg-source: info: applying any/local-dynamic-resolvconf.diff
dpkg-source: info: applying any/local-libpic.diff
dpkg-source: info: applying any/local-bootstrap-headers.diff
dpkg-source: info: applying any/submitted-argp-attribute.diff
dpkg-source: info: applying any/submitted-resolv-ipv6-nameservers.diff
dpkg-source: info: applying any/local-static-dlopen-search-path.diff
dpkg-source: info: applying any/cvs-ldconfig-aux-cache.diff
dpkg-source: info: applying any/cvs-vismain-pie.diff
dpkg-source: info: applying any/local-tester-gcc-4.9.diff
dpkg-source: info: applying any/local-xfail-stdlib-linkns.diff
dpkg-source: info: applying any/cvs-localplt-new-readelf.diff
dpkg-source: info: applying any/cvs-make-typo.diff
dpkg-source: info: applying any/cvs-logbl-accuracy.diff
dpkg-source: info: applying any/local-math-logb.diff
dpkg-source: info: applying ubuntu/lddebug-scopes.diff
dpkg-source: info: applying ubuntu/local-altlocaledir.diff
dpkg-source: info: applying ubuntu/local-disable-ld_audit.diff
dpkg-source: info: applying ubuntu/local-disable-nscd-netgroup-caching.diff
dpkg-source: info: applying ubuntu/submitted-no-sprintf-pre-truncate.diff
dpkg-source: info: applying ubuntu/submitted-no-stack-backtrace.diff
dpkg-source: info: applying ubuntu/unsubmitted-increase-dtv-surplus.diff
dpkg-source: info: applying ubuntu/local-warn-bsd-source.diff

内容挺多的,一条一条的看

glibc 2.21-0ubuntu4 (dsc)
dpkg-source: 警告: 对 ./glibc_2.21-0ubuntu4.dsc 校验签名失败

对比这两条输出,基本可以判定 .dsc  文件是dsc加密所需的文件。与源代码无关。

glibc 2.21-0ubuntu4 (tar) [12.7 MB]

从文件格式与大小判断,这就是glibc的源代码。

glibc 2.21-0ubuntu4 (diff) [994 kB]

这就是补丁文件,这个补丁文件不需要我们手动下载,它会随程序的源文件一起下载,关于diff文件,请参考这篇blog: http://www.cnblogs.com/cute/archive/2011/04/29/2033011.html 在下惭愧,这篇文章我还没看了 微笑

好了一切万事俱备,接下来该调试一下了。

还是使用咱们的signal程序,启动gdb进行调试:

gdb ./test_signal_2
start

遇到signal函数进入

<pre name="code" class="plain">(gdb) step
34	    if(signal(SIGINT,signal_handler) == SIG_ERR)
(gdb) step
__bsd_signal (sig=2, handler=0x40099b <signal_handler>)
    at ../sysdeps/posix/signal.c:36
36	../sysdeps/posix/signal.c: 没有那个文件或目录.

 

此时发现没有那个文件,使用directory命令加入

directory /home/glibc-2.21/sysdeps/posix/

之所以没有在一开始就使用directory命令,是因为directory命令无法对源文件所在目录进行递归搜索。但在很多时候我们无法知道某个函数的实现在哪个文件中,因此我采用这个方法:先由gdb定位文件,再使用directory命令。

以上。


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值