Abhilash V R
Abhilash V R

Reputation: 257

C Linux Tracing all function calls including function inside library

i have program like

int main()
{
  char *ptr = malloc(2);
  free(ptr);
}

So i just want to trace of all function calls happening inside the program ,till system call

like

malloc
   |____ libc( sme_fn)
           |
           |__sme_system_call

Could you please tell some way to get this ?

Upvotes: 3

Views: 3279

Answers (2)

paulsm4
paulsm4

Reputation: 121599

As you know, "system calls" come in two flavors:

  • Calls directly to the operating system ("open", "close", "fork", "exec", "exit", etc)

  • Standard C runtime functions for the platform ("printf()", "malloc()", "free(), etc.)

You can view the former with "strace".

You can view (at least calls into) the latter with gdb.

You can look at the complete implementation, and all internals, directly from the source code:

Finally, if you're having issues with "malloc()", "valgrind" is (one of several) very, very useful tools to consider.

Upvotes: 2

Tim
Tim

Reputation: 5361

If you're using gcc, compile with -pg and then use the gprof command.

Or, if you're on Linux, you can use oprofile to do something similar without recompiling.

Both tools should give you call graphs, which is what you're looking for.

Upvotes: 1

Related Questions