编译glibc源码

发布时间:2024年01月11日

前言

有时候基于好奇或遇到了BUG,想看看glibc中某些功能怎么具体实现的。除了调试汇编,还可以编译glibc源码然后调试。在上节《Modern C++ std::mutex底层原理-CSDN博客》中,我们用汇编调试了glibc,本节我们编译其源码来直接调试。

编译流程

$ ldd --version
ldd (GNU libc) 2.17
$ wget https://ftp.gnu.org/gnu/glibc/glibc-2.17.tar.gz
$ tar -xvzf *.gz
$ mkdir glibc-2.17-build
$ cd glibc-2.17-build
$ export CFLAGS="-g3 -O1"           for macro expansion, glibc requires optimization.
$ ../glibc-2.17/configure --prefix=/usr/local/glibc-2.17  --disable-profile --disable-werror
$ make
$ sudo make install 

先确定glibc版本,然后下载对应的源码,解压,编译&安装到/usr/local/glibc-2.17

为了不破坏我机器的环境,我没设置环境变量,而是利用编译选项让它从/usr/local/glibc-2.17下找lib:

[mzhai@lock]$ cat mutex_1thread.cpp
#include <iostream>       // std::cout
#include <thread>         // std::thread
#include <mutex>          // std::mutex, std::lock_guard, std::adopt_lock

std::mutex mtx;           // mutex for critical section
int total=0;

void print_thread_id(int id) {
        mtx.lock();
        for(int i=0;i<10000;i++) total++;
        mtx.unlock();
}

int main()
{
        print_thread_id(1);
        print_thread_id(2);
        std::cout<<"total="<<total<<std::endl;
        return 0;
}
[mzhai@lock]$ g++ mutex_1thread.cpp  -I/usr/local/glibc-2.17/include -L/usr/local/glibc-2.17/lib -Wl,--rpath=/usr/local/glibc-2.17/lib -Wl,--rpath=/lib64 -Wl,--dynamic-linker=/usr/local/glibc-2.17/lib/ld-2.17.so -g -std=c++11 -lpthread
[mzhai@lock]$ ldd ./a.out
        linux-vdso.so.1 =>  (0x00007ffc46970000)
        libpthread.so.0 => /usr/local/glibc-2.17/lib/libpthread.so.0 (0x00007f147874d000)
        libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f1478445000)
        libm.so.6 => /usr/local/glibc-2.17/lib/libm.so.6 (0x00007f1478148000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f1477f32000)
        libc.so.6 => /usr/local/glibc-2.17/lib/libc.so.6 (0x00007f1477b90000)
        /usr/local/glibc-2.17/lib/ld-2.17.so => /lib64/ld-linux-x86-64.so.2 (0x00007f1478969000)

?可以安心的调试了

[mzhai@lock]$ gdb ./a.out
(gdb) b main
Breakpoint 1 at 0x400ace: file mutex_1thread.cpp, line 16.
(gdb) r
Starting program: /home/mzhai/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Breakpoint 1, main () at mutex_1thread.cpp:16
16              print_thread_id(1);
Missing separate debuginfos, use: debuginfo-install libgcc-4.8.5-44.el7.x86_64 libstdc++-4.8.5-44.el7.x86_64
(gdb) n
17              print_thread_id(2);
(gdb) s
print_thread_id (id=2) at mutex_1thread.cpp:9
9               mtx.lock();
(gdb) s
std::mutex::lock (this=0x6021c0 <mtx>) at /usr/include/c++/4.8.2/mutex:134
134           int __e = __gthread_mutex_lock(&_M_mutex);
(gdb) s
__gthread_mutex_lock (__mutex=0x6021c0 <mtx>) at /usr/include/c++/4.8.2/x86_64-redhat-linux/bits/gthr-default.h:747
747       if (__gthread_active_p ())
(gdb) n
748         return __gthrw_(pthread_mutex_lock) (__mutex);
(gdb) s
__GI___pthread_mutex_lock (mutex=0x6021c0 <mtx>) at pthread_mutex_lock.c:47
47      {
(gdb) s
50        unsigned int type = PTHREAD_MUTEX_TYPE (mutex);
(gdb)
54        if (__builtin_expect (type & ~PTHREAD_MUTEX_KIND_MASK_NP, 0))
(gdb)
57        pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
(gdb)
59        if (__builtin_expect (type, PTHREAD_MUTEX_TIMED_NP)
(gdb)
64            LLL_MUTEX_LOCK (mutex);
(gdb) info macro LLL_MUTEX_LOCK
Defined at /min/glibc/glibc-2.17/nptl/pthread_mutex_lock.c:30
#define LLL_MUTEX_LOCK(mutex) lll_lock ((mutex)->__data.__lock, PTHREAD_MUTEX_PSHARED (mutex))
(gdb) info macro lll_lock
Defined at /min/glibc/glibc-2.17/nptl/../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.h:296
  included at /min/glibc/glibc-2.17/nptl/../nptl/descr.h:30
  included at /min/glibc/glibc-2.17/nptl/../nptl/sysdeps/x86_64/tls.h:95
  included at /min/glibc/glibc-2.17/nptl/../include/errno.h:22
  included at /min/glibc/glibc-2.17/nptl/pthread_mutex_lock.c:20
#define lll_lock(futex, private) (void) ({ int ignore1, ignore2, ignore3; if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) __asm __volatile (__lll_lock_asm_start ".subsection 1\n\t" ".type _L_lock_%=, @function\n" "_L_lock_%=:\n" "1:\tlea %2, %%" RDI_LP "\n" "2:\tsub $128, %%" RSP_LP "\n" "3:\tcallq __lll_lock_wait_private\n" "4:\tadd $128, %%" RSP_LP "\n" "5:\tjmp 24f\n" "6:\t.size _L_lock_%=, 6b-1b\n\t" ".previous\n" LLL_STUB_UNWIND_INFO_5 "24:" : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), "=a" (ignore3) : "0" (1), "m" (futex), "3" (0) : "cx", "r11", "cc", "memory"); else __asm __volatile (__lll_lock_asm_start ".subsection 1\n\t" ".type _L_lock_%=, @function\n" "_L_lock_%=:\n" "1:\tlea %2, %%" RDI_LP "\n" "2:\tsub $128, %%" RSP_LP "\n" "3:\tcallq __lll_lock_wait\n" "4:\tadd $128, %%" RSP_LP "\n" "5:\tjmp 24f\n" "6:\t.size _L_lock_%=, 6b-1b\n\t" ".previous\n" LLL_STUB_UNWIND_INFO_5 "24:" : "=S" (ignore1), "=D" (ignore2), "=m" (futex), "=a" (ignore3) : "1" (1), "m" (futex), "3" (0), "0" (private) : "cx", "r11", "cc", "memory"); })
(gdb)


?

文章来源:https://blog.csdn.net/zhaiminlove/article/details/135538528
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。