[LINUX] dlopen () ltrace a function call in a shared library

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.

Example

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;
}

Run ltrace a.out

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) +++

Run ltrace -x hoge a .out

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) +++

Commentary

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

dlopen () ltrace a function call in a shared library
Create a function in Python
How to call a function
Call a Python function from p5.js.
Python-dynamically call a function from a string
A story about a build error in a shared library that references libusb
Attempt to extend a function in the library (add copy function to pathlib)
Draw a graph of a quadratic function in Python
To execute a Python enumerate function in JavaScript
Create a protein sequence mutation library in pandas
Call a Python script from Embedded Python in C ++ / C ++
[Go] How to write or call a function
How to Mock a Public function in Pytest
Duality in function
This is a sample of function application in dataframe.
[Python] How to call a c function from python (ctypes)
Publish / upload a library created in Python to PyPI
[Redash] Standard library cannot be used in python function
What does the last () in a function mean in Python?