Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable and disable gprof at runtime?

I wonder if there's any API within gprof to enable and disable profiling at runtime by the monitored application. I'm interested on disabling the profiling of certain parts of the code and enabling it to focus on those that are interesting to me. I mean, is there a way to avoid do this?

int main (void)
{

  // disable gprof ?
  uninteresting_routine();
  // enable gprof ?

  interesting_routine();
}

This link from the GCC website referring the instrumentation options does not seem to include any reference to this functionality.

like image 982
Harald Avatar asked Sep 30 '16 10:09

Harald


2 Answers

There's an undocumented and hidden way of doing this that works on some systems (at least some, if not all, versions of glibc and some BSDs).

$ cat foo.c
extern void moncontrol(int);

static void
foo(void)
{
}

static void
bar(void)
{
}

int
main(int argc, char **argv)
{
    moncontrol(0);
    foo();
    moncontrol(1);
    bar();
    return 0;
}
$ cc -o foo -pg foo.c && ./foo
$ gprof foo | egrep 'foo|bar'
  0.00      0.00     0.00        1     0.00     0.00  bar
[1]      0.0    0.00    0.00       1         bar [1]
   [1] bar

Glibc doesn't have a prototype or man-page for this function, but it does exist.

like image 70
Art Avatar answered Oct 10 '22 03:10

Art


The accepted answer is correct. Just wanted to remind C++ programmers to use

extern "C" void moncontrol(int);
like image 43
Sal Mangano Avatar answered Oct 10 '22 05:10

Sal Mangano