Enable and disable gprof at runtime?

I wonder if there is any API inside gprofto enable and disable profiling at runtime by a controlled application. I’m interested in disabling profiling for certain parts of the code and letting it focus on those that interest me. I mean, is there a way to avoid this?

int main (void)
{

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

  interesting_routine();
}

This link from the GCC website, linking to toolkit parameters, does not seem to contain links to this functionality.

+4
source share
1 answer

, ( , , , glibc BSD).

$ 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 man- , .

+2

Source: https://habr.com/ru/post/1656343/


All Articles