Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I trace all local function calls and exits, and record it to a file to review

I want to trace a C program under Linux, and record all function calls and returns in a format of tree. For example, the source code:

void a ()
{
    printf("a\n");
}
void b ()
{
    printf("b\n");
}

void c ()
{
    a();
    b();
}

int main()
{
    a();
    b();
    c();
}

And I want a output like the following:

call main
    call a
    exit a
    call b
    exit b
    call c
        call a
        exit a
        call b
        exit b
    exit c
exit main

It is a idealistic output. I just want to get all the process of local function calls and returns, so the similar output is also welcome.

like image 558
river Avatar asked Mar 31 '26 00:03

river


1 Answers

gcc -finstrument-functions does my job

like image 122
river Avatar answered Apr 02 '26 15:04

river



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!