2

I am trying to run some profiles on DBMSs written in C, primarily the postgres binary. I would like to be able to use a utility to print out the sequence of function calls made made by the program. As a simple example, take this program:

void func1 () { 
  printf("x\n");
}

void func2 () {
  printf("y\n");
  func1();
}

int main () {
  func2();
  func1();
  return 0;
}

When compiled and executed with this "utility", I would like to see something along the lines of this:

-> main
  -> func2
    -> func1
  -> func1
<- 

Also, I cannot modify either the source code or the makefile, however -g is already enabled.

I know that I have used a profiler in the past that did something similar to this, but I cannot remember which one. I did some googling, and I could't find a good solution that did not require me to change either the source or the makefile.

What profiling tool can I use to accomplish this? Or does one not exist?

Thanks.

bddicken
  • 1,412
  • 1
  • 15
  • 16
  • I don't think that there's a standard command that works across all operating-systems that does what you want. – dcaswell Aug 30 '13 at 23:58
  • Are you referring to a static profile (showing function call hierarchy) or dynamic profile (showing actual function calls based upon execution)? – lurker Aug 31 '13 at 00:08
  • @mbratch I am referring to a dynamic profile. – bddicken Aug 31 '13 at 00:24

2 Answers2

1

I know of no tools to do this directly, but you could use GDB breakpoint commands to get the stack traces along with regex break to break on the functions you're interested in. At that point you should be able to postprocess it to get the output in a format you desire.

For example, you could do something like this (edited for brevity):

$ gdb ./program
(gdb) rbreak program.c:.
...
(gdb) commands
>silent
>bt
>cont
>end
(gdb) run
...
#0  main () at program.c:22
...
#0  foo (number=113383) at program.c:4
#1  main () at program.c:22
...
Program exited normally.
(gdb)
Kyle Lemons
  • 4,716
  • 1
  • 19
  • 23
0

What you want is "tracing", where entry and exit from "every" function is recorded in a file. This is provided in some interpreted languages, but not in compiled, that I know of. I don't know of any profilers that do this.

I quoted "every" because there are huge layers of function calls I'm guessing you don't want, like I/O, memory allocation and freeing, etc. In fact, almost every single machine instruction is in effect a function call into the micro-code or internal functioning of the CPU chip. I suppose you don't care to see those :)

For any but the tiniest programs, such traces are way too long for any human to read. If what you're really after is to understand what the program's doing with respect to time, the subject has been very much discussed, and this is my contribution.

Community
  • 1
  • 1
Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135