当我编写一个要与LD_PRELOAD
一起使用的库时,我如何调试它的__attribute__((__constructor__))
个函数?它们似乎总是在gdb停止进程之前运行.作为MCVE,运行以下命令:
cat > preflight.c <<EOF
#include <stdio.h>
#include <stdlib.h>
__attribute__((__constructor__))
void preflight(void) {
puts("Exiting from preflight");
exit(42);
}
EOF
gcc -g -fPIC -shared preflight.c -o preflight.so
gdb /bin/true -ex 'set environment LD_PRELOAD ./preflight.so' \
-ex 'set breakpoint pending on' \
-ex 'break preflight' \
-ex 'starti'
Gdb输出的结尾如下所示:
Function "preflight" not defined.
Breakpoint 1 (preflight) pending.
Starting program: /usr/bin/true
Exiting from preflight
During startup program exited with code 42.
(gdb)
注意到preflight
函数在gdb停止程序之前运行,尽管我试图在它上面设置一个断点,并且我使用了starti
,它应该在第一条指令时中断.我必须做什么不同的操作才能让gdb突破preflight
函数?