Enable and disable gprof at runtime?
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.