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.
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.
The accepted answer is correct. Just wanted to remind C++ programmers to use
extern "C" void moncontrol(int);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With