当我编写一个要与 一起使用的库时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'
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
观察到该preflight …