glibc 查看版本号
1 ldd 某可执行程序 查看链接情况
2 直接执行链接的libc.so文件 。

兄弟们可能会纳闷,SO文件怎么可以执行呢。 GLIBC耍了个trick。
-
#ifdef HAVE_ELF
-
/* This function is the entry point for the shared object.
-
Running the library as a program will get here. */
-
-
extern void __libc_main (void) __attribute__ ((noreturn));
-
void
-
__libc_main (void)
-
{
-
__libc_print_version ();
-
_exit (0);
-
}
-
#endif
-
static const char banner[] =
-
"GNU C Library "PKGVERSION RELEASE" release version "VERSION", by Roland McGrath et al.\n\
-
Copyright (C) 2012 Free Software Foundation, Inc.\n\
-
This is free software; see the source for copying conditions.\n\
-
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
-
PARTICULAR PURPOSE.\n\
-
Compiled by GNU CC version "__VERSION__".\n"
-
...
-
-
void
-
__libc_print_version (void)
-
{
-
__write (STDOUT_FILENO, banner, sizeof banner - 1);
- }
这个问题的解决,要感谢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/对应目录下。
第一步 安装符号表
- sudo apt-get install libc6-dbg
-
root@manu:/usr/lib/debug/lib/i386-linux-gnu# ll
-
总用量 10268
-
drwxr-xr-x 3 root root 4096 5月 2 16:21 ./
-
drwxr-xr-x 4 root root 4096 12月 2 15:16 ../
-
-rwxr-xr-x 1 root root 555181 1月 28 20:30 ld-2.15.so*
-
-rw-r--r-- 1 root root 49332 1月 28 20:30 libanl-2.15.so
-
-rw-r--r-- 1 root root 13544 1月 28 20:30 libBrokenLocale-2.15.so
-
-rwxr-xr-x 1 root root 6963435 1月 28 20:30 libc-2.15.so*
-
-rw-r--r-- 1 root root 62923 1月 28 20:30 libcidn-2.15.so
-
-rw-r--r-- 1 root root 68453 1月 28 20:30 libcrypt-2.15.so
- ...
-
-rw-r--r-- 1 root root 6149 1月 28 20:30 libpcprofile.so
-
-rwxr-xr-x 1 root root 565440 1月 28 20:30 libpthread-2.15.so*
-
-rw-r--r-- 1 root root 201563 1月 28 20:30 libresolv-2.15.so
-
-rw-r--r-- 1 root root 135143 1月 28 20:30 librt-2.15.so
-
- ....
这仅仅是有了符号表,但是看不到代码,调试的过程中,无法看到代码。很难受。
第二步:安装源码:
Ubuntu很贴心的提供了安装方法:
- root@manu:~/code/c/classical# sudo apt-get source libc6-dev
- drwxr-xr-x 72 root root 4096 5月 2 17:05 eglibc-2.15/
第三步:gdb 调试glibc
我的目的是通过调试pthread_create创建过程,了解glibc和内核是如何实现NPTL线程的。 我的程序里面有pthread_create,我想单步跟踪glibc里面的__pthread_create_2_1函数。
首先要告诉的gdb,源文件要去那个目录下寻找:
- (gdb) directory /home/manu/code/c/classical/eglibc-2.15/nptl

这部分代码,主要参考自用GDB追踪glibc代码执行过程。感谢作者的分享,让我可以继续我的NPTL 线程栈和TLS的分析。希望近期能完成这个NPTL的学习。
值得一提的是,分析Ubuntu提供的patch,可以看到,glibc查看版本号一节中的输出中含有Ubuntu EGLIC的原因:
- -rw-r--r-- 1 root root 10368576 1月 28 19:03 eglibc_2.15-0ubuntu10.4.diff

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