在运行时启用和禁用gprof?

Har*_*ald 5 c performance gprof

我想知道是否有任何API gprof可以在运行时通过受监视的应用程序启用和禁用性能分析。我对禁用代码某些部分的概要分析并使之专注于我感兴趣的部分感兴趣。我的意思是,有办法避免这种情况吗?

int main (void)
{

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

  interesting_routine();
}
Run Code Online (Sandbox Code Playgroud)

链接从GCC网站指仪器选项似乎并不包括这个功能的任何引用。

Art*_*Art 4

有一种未记录且隐藏的方法可以在某些系统(至少是某些(如果不是全部)版本的 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
Run Code Online (Sandbox Code Playgroud)

Glibc 没有这个函数的原型或手册页,但它确实存在。