标签: gcc-attribute

如何调试 LD_PRELOAD 库中的构造函数?

当我编写一个要与 一起使用的库时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 …

c debugging gdb ld-preload gcc-attribute

3
推荐指数
1
解决办法
42
查看次数

标签 统计

c ×1

debugging ×1

gcc-attribute ×1

gdb ×1

ld-preload ×1