我有一个使用动态加载库来加载插件的 C 程序。我想跟踪库调用以调试插件的加载。
我看了看ltrace,但我似乎无法让它工作:
这是一个示例程序:
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main() {
int *a = malloc(sizeof(int));
void *handle;
double (*cosine)(double);
char *error;
handle = dlopen ("/usr/lib/x86_64-linux-gnu/libm.so.6", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}
cosine = dlsym(handle, "cos");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
exit(1);
}
printf ("%f\n", (*cosine)(2.0));
dlclose(handle);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译(并删除PIE,否则 ltrace 将看不到任何内容):
gcc main.c -pg -ldl -no-pie
跑步:ltrace ./a.out
输出
__monstartup(0x401170, 0x401431, 0x7fffe3875838, 0x7fffe3875838) = …Run Code Online (Sandbox Code Playgroud)