TLDR
** The story on x86_64. Not confirmed on other architectures. ** **
You can use the ltrace command to trace calls to functions in a shared library. However, simply using ltrace a.out
will trace the function calls in the library specified at build time, such as -lhoge
, but not the function calls in the dlopen () library. To do this, you need to specify the function name, such as ltrace -x hoge a.out
.
hoge.c
// gcc -W -Wall -fPIC -shared hoge.c -o libhoge.so
int hoge(int a, int b)
{
return 10 + a * b;
}
main.c
// gcc -W -Wall -ldl main.c -o a.out
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main()
{
void* lib = dlopen("./libhoge.so", RTLD_LAZY);
if (lib == NULL) {
printf("%s\n", dlerror());
exit(1);
}
int (*f)(int, int);
f = (int (*)(int, int))dlsym(lib, "hoge");
int x = f(5, 9);
printf("x = %d\n", x);
dlclose(lib);
return 0;
}
The call to hoge () is not displayed.
$ ltrace ./a.out
dlopen("./libhoge.so", 1) = 0x55cd42d67280
dlsym(0x55cd42d67280, "hoge") = 0x7f0bc72cc5aa
printf("x = %d\n", 55x = 55
) = 7
dlclose(0x55cd42d67280) = 0
+++ exited (status 0) +++
The line [email protected] (5, 9, 1, 0)
is displayed.
$ ltrace -x hoge ./a.out
dlopen("./libhoge.so", 1) = 0x564a38c5d280
dlsym(0x564a38c5d280, "hoge") = 0x7f8ffa51b5aa
[email protected](5, 9, 1, 0) = 55
printf("x = %d\n", 55x = 55
) = 7
dlclose(0x564a38c5d280) = 0
+++ exited (status 0) +++
It seems that ltrace's dlopen () support was implemented around 2009. https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=537781 The original patch and its description can be found on this page. http://timetobleed.com/extending-ltrace-to-make-your-rubypythonperlphp-apps-faster/
Recommended Posts