Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support arguments (-a) for dlopen #1981

Open
vimkim opened this issue Nov 25, 2024 · 2 comments
Open

Support arguments (-a) for dlopen #1981

vimkim opened this issue Nov 25, 2024 · 2 comments

Comments

@vimkim
Copy link
Contributor

vimkim commented Nov 25, 2024

Is there a way to show arguments for functions inside a dynamically loaded libraries using
dlopen?

How to Reproduce

libexample.c

#include <stdio.h>

void say_hello(int x) {
    printf("Hello from the shared library! x = %d\n", x);
}

main.c

#include <stdio.h>
#include <dlfcn.h>

void say_hello_no_dlopen(int x){
  printf("Hello, World! x = %d\n", x);
  printf("This function is not a dynamically loaded function\n\n");
}

int main() {

    int y = 333;

    say_hello_no_dlopen(y);

    // Load the shared library
    void *handle = dlopen("./libexample.so", RTLD_LAZY);
    if (!handle) {
        fprintf(stderr, "Error: %s\n", dlerror());
        return 1;
    }

    // Clear any existing error
    dlerror();

    // Get the function pointer
    void (*say_hello)(int) = (void (*)(int))dlsym(handle, "say_hello");
    const char *error = dlerror();
    if (error) {
        fprintf(stderr, "Error: %s\n", error);
        dlclose(handle);
        return 1;
    }

    // Call the function
    say_hello(777);

    // Close the shared library
    dlclose(handle);

    return 0;
}

Compile and Run:

gcc -shared -finstrument-functions -fPIC -pg -g -o libexample.so libexample.c
gcc -finstrument-functions -g -pg -o main main.c -ldl
./main

Results:

Hello, World! x = 333
This function is not a dynamically loaded function

Hello from the shared library! x = 777

Uftrace:

uftrace -a ./main

Results:
image

As you can see,
the function named 'say_hello_no_dlopen()' prints the argument (of int 333) just fine,
while the dlopened function say_hello prints no argument.

Do you know how to fix this issue?

@honggyukim
Copy link
Collaborator

Hi @vimkim, this is duplicate with #842 so please have a look.

@namhyung
Copy link
Owner

Btw, please don't use -finstrument-functions and -pg together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants