博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
调试技巧之 gcc/g++ -instrument-functions 参数
阅读量:6071 次
发布时间:2019-06-20

本文共 4036 字,大约阅读时间需要 13 分钟。

参考

     

       

     

     

       官方参数说明


索引

  •      
  •      
  •      
  •      

     


       因为项目太大,线程太多,gprof不可靠,于是找到了finstrument-functions

      -finstrument-functions 會在每次進入與退出函式前呼叫 "__cyg_profile_func_enter" 與 "__cyg_profile_func_exit" 這兩個 hook function。等等,「進入」與「退出」是又何解?C Programming Language 最經典之處在於,雖然沒有定義語言實做的方式,但實際上 function call 皆以 stack frame 的形式存在,去年在「深入淺出 Hello World」Part II 有提過。所以上述那一大段英文就是說,如果我們不透過 GCC 內建函式 "__builtin_return_address" 取得 caller 與 callee 相關的動態位址,那麼仍可透過 -finstrument-functions,讓 GCC 合成相關的處理指令,讓我們得以追蹤。而看到 __cyg 開頭的函式,就知道是來自 Cygnus 的貢獻,在 gcc 2.x 內部設計可瞥見不少。


hello.c

#include 
#define DUMP(func, call) printf("%s: func = %p, called by = %p/n", __FUNCTION__, func, call)void __attribute__((__no_instrument_function__))__cyg_profile_func_enter(void *this_func, void *call_site){ DUMP(this_func, call_site);}void __attribute__((__no_instrument_function__))__cyg_profile_func_exit(void *this_func, void *call_site){ DUMP(this_func, call_site);}main(){ puts("Hello World!"); return 0;}

makefile

all:hello.o    gcc -finstrument-functions -finstrument-functions hello.o -o hellohello.o:    gcc  -finstrument-functions  -c hello.cclean:    rm *.o hello -rf

运行:

./hello__cyg_profile_func_enter: func = 0x8048438, called by = 0x658dec/nHello World!__cyg_profile_func_exit: func = 0x8048438, called by = 0x658dec/n

 


c++

hello.cc

#include        
#include
#include
#include
#include
using namespace std;#define DUMP(func, call) printf("%s: func = %p, called by = %p\n", __FUNCTION__, func, call)#ifdef __cplusplusextern "C" {#endif/* Static functions. */static FILE *openlogfile (const char *filename) __attribute__ ((no_instrument_function));static void closelogfile (void) __attribute__ ((no_instrument_function));/* Note that these are linked internally by the compiler. * Don't call them directly! */void __cyg_profile_func_enter (void *this_fn, void *call_site) __attribute__ ((no_instrument_function));void __cyg_profile_func_exit (void *this_fn, void *call_site) __attribute__ ((no_instrument_function));#ifdef __cplusplus};#endifvoid__cyg_profile_func_enter (void *this_fn, void *call_site){ DUMP(this_fn, call_site);}void__cyg_profile_func_exit (void *this_fn, void *call_site){ DUMP(this_fn, call_site);} intmain ( int argc, char *argv[] ){ cout << "\nProgram " << argv[0] << endl << endl; return EXIT_SUCCESS;} // ---------- end of function main ----------

makefile:

all:    g++ -finstrument-functions  hello.cc

运行:

./a.out__cyg_profile_func_enter: func = 0x80487ae, called by = 0x8048a3b__cyg_profile_func_enter: func = 0x8048714, called by = 0x80487d7__cyg_profile_func_exit: func = 0x8048714, called by = 0x80487d7__cyg_profile_func_exit: func = 0x80487ae, called by = 0x8048a3b__cyg_profile_func_enter: func = 0x8048882, called by = 0x658decProgram ./a.out__cyg_profile_func_exit: func = 0x8048882, called by = 0x658dec__cyg_profile_func_enter: func = 0x804881a, called by = 0x66e7f9__cyg_profile_func_exit: func = 0x804881a, called by = 0x66e7f9

 


 

hello.c

#include 
#include "test.h"#define DUMP(func, call) printf("%s: func = %p, called by = %p\n", __FUNCTION__, func, call)void __attribute__((__no_instrument_function__))__cyg_profile_func_enter(void *this_func, void *call_site){ DUMP(this_func, call_site);}void __attribute__((__no_instrument_function__))__cyg_profile_func_exit(void *this_func, void *call_site){ DUMP(this_func, call_site);}void test(){ printf("nihao\n");}main(){ test(); puts("Hello World!"); return 0;}

makefile

CC=gcc44all:hello.o        $(CC) -g -finstrument-functions  hello.o -o hellohello.o:        $(CC) -g -finstrument-functions -finstrument-functions-exclude-function-list=test -c hello.cclean:        rm *.o hello -rf

运行:

./hello__cyg_profile_func_enter: func = 0x804847e, called by = 0xb7e6adecnihaoHello World!__cyg_profile_func_exit: func = 0x804847e, called by = 0xb7e6adec

 

 

转载地址:http://vyigx.baihongyu.com/

你可能感兴趣的文章
Qt的语法高亮类(注释方式)
查看>>
Hadoop实战(9)_Hive进阶及UDF开发
查看>>
小游戏一:win32贴图——TransparentBlt
查看>>
【windows8开发】C++开发Metro风格App
查看>>
Spring Cloud云服务- HongHu云架构common-service代码结构分析
查看>>
【BZOJ】1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害
查看>>
Java开发5年只会curd,被新来开发一年的小子说成是混吃等死...
查看>>
CentOS 7 Docker
查看>>
Telegraf+Influxdb+Grafana构建监控平台
查看>>
死磕Tomcat系列(1)——整体架构
查看>>
Spring AOP
查看>>
The following users do not have email address specified
查看>>
Docker Data Center系列(一)- 快速搭建云原生架构的实践环境
查看>>
ESLint 禁止检验
查看>>
用了那么多年MySQL不知道Explain?怪不得性能那么差!
查看>>
java 字符流 字节流
查看>>
mysql实战14 | count(*)这么慢,我该怎么办?
查看>>
wireshark的ubuntu更新ppa源
查看>>
AngularJs 学习 (二)
查看>>
0302感想
查看>>