2017-12-03  23:59:16

参考 如何快速地在每个函数入口处加入相同的语句?

https://www.zhihu.com/question/56132218

做个存档

 scj@scjComputer:~/THpro/func_trace$ tree .
.
├── func_trace.c
├── func_trace.sh
├── imple
│   ├── bar.c
│   └── bar.h
├── main.c
└── 步骤
func_trace.c
 #include <stdio.h>

 static FILE *fp_trace;

 void __attribute__((constructor)) traceBegin(void) {
fp_trace = fopen("func_trace.out", "w");
} void __attribute__((destructor)) traceEnd(void) {
if (fp_trace != NULL) {
fclose(fp_trace);
}
} void __cyg_profile_func_enter(void *func, void *caller) {
if (fp_trace != NULL) {
fprintf(fp_trace, "entry %p %p\n", func, caller);
}
} void __cyg_profile_func_exit(void *func, void *caller) {
if (fp_trace != NULL) {
fprintf(fp_trace, "exit %p %p\n", func, caller);
}
}
func_trace.sh

 #!/bin/bash
EXECUTABLE="$1"
TRACELOG="$2" while read TRACEFLAG FADDR CADDR; do
FNAME="$(addr2line -f -e ${EXECUTABLE} ${FADDR}|head -1)" if test "${TRACEFLAG}" = "entry"
then
CNAME="$(addr2line -f -e ${EXECUTABLE} ${CADDR}|head -1)"
CLINE="$(addr2line -s -e ${EXECUTABLE} ${CADDR})"
echo "Enter ${FNAME} called from ${CNAME} (${CLINE})"
fi if test "${TRACEFLAG}" = "exit"
then
echo "Exit ${FNAME}"
fi done < "${TRACELOG}"

bar.c

 #include "bar.h"

 int bar(void) {
zoo();
return ;
} int foo(void) {
return ;
} void zoo(void) {
foo();
}

bar.h

 #ifndef bar_h
#define bar_h int bar(void);
int foo(void);
void zoo(void); #endif

main.c

 #include <stdio.h>
#include "./imple/bar.h" int main(int argc, char **argv) {
bar();
}

然后按照如下顺序执行:

 gcc func_trace.c -c
gcc main.c ./imple/*.c func_trace.o -finstrument-functions
./a.out
./func_trace.sh a.out func_trace.out
可以直接编译好多源文件的程序了。
直接把main.c以外的文件放进imple文件夹里头就可以 如果第二步有问题,用
gcc main.c ./source/*.c func_trace.o -finstrument-functions 2>trace_log.txt

导出编译信息

imple文件夹里头的东西封装成so后反而看不到调用过程。

finstrument-functions的更多相关文章

  1. asp.net MVC helper 和自定义函数@functions小结

    asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的切换,大大提升了我们的开发效率.但是Razor语法还是有一些棉花糖值得我们了解一下,可以更加强劲的提升我们 ...

  2. 【跟着子迟品 underscore】Array Functions 相关源码拾遗 & 小结

    Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...

  3. 【跟着子迟品 underscore】Object Functions 相关源码拾遗 & 小结

    Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...

  4. ajax的使用:(ajaxReturn[ajax的返回方法]),(eval返回字符串);分页;第三方类(page.class.php)如何载入;自动加载函数库(functions);session如何防止跳过登录访问(构造函数说明)

    一.ajax例子:ajaxReturn("ok","eval")->thinkphp中ajax的返回值的方法,返回参数为ok,返回类型为eval(字符串) ...

  5. QM模块包含主数据(Master data)和功能(functions)

    QM模块包含主数据(Master data)和功能(functions)   QM主数据   QM主数据 1 Material   Master MM01/MM02/MM50待测 物料主数据 2 Sa ...

  6. jQuery String Functions

    In today's post, I have put together all jQuery String Functions. Well, I should say that these are ...

  7. 2-4. Using auto with Functions

    在C++14中允许使用type deduction用于函数参数和函数返回值 Return Type Deduction in C++11 #include <iostream> using ...

  8. [Python] Pitfalls: About Default Parameter Values in Functions

    Today an interesting bug (pitfall) is found when I was trying debug someone's code. There is a funct ...

  9. Kernel Functions for Machine Learning Applications

    In recent years, Kernel methods have received major attention, particularly due to the increased pop ...

  10. Execution Order of Event Functions

    In Unity scripting, there are a number of event functions that get executed in a predetermined order ...

随机推荐

  1. mfs windows客户端

    之前用moosefs,苦于没有Windows客户端, 本来想在Linux上mfsmount一个目录然后用samba做共享目录,但是这样太不简洁了. 当然豆瓣上有的小伙伴也说自己开发了moosefs的W ...

  2. 如何使用require.js?

    最近几天在学习一个javascript库require.js,也看了一些相关的教学视频,这里推荐一下幕课网阿当老师的<阿当大话西游之Web组件>的教学视频,一整套看下来,参照视频里面的de ...

  3. learning makefile 定义命令包

  4. react+classnames

    之前做项目的时候一直不知道有不知道有classnames这个东西,一直用的都是字符串拼接,感觉用的很别扭. classnames用法和angular1.x及vue差不多,所以用起来会比较顺手 1)安装 ...

  5. MCNN多层神经网络论文笔记

    论文原文 https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Zhang_Single-Image_Crowd_Coun ...

  6. Kruskal算法(题目还是:畅通工程)

    那还是先把题目丢出来,是HDU上的一道题 畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  7. 阶段01Java基础day16集合框架02

    16.01_集合框架(去除ArrayList中重复字符串元素方式1) A:案例演示 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同) 思路:创建新集合方式 /** * A:案例演示 ...

  8. Jsの练习-数组常用方法 -splice()

    splice() 功能:1.可以实现删除,插入和替换. 删除:可以删除任意数量的项,只需指定2个参数:要删除的第一项的位置和要删除的项数. 例如:splice(0,2)会删除数组中的前2项. 插入:可 ...

  9. nginx——Nginx 防爬虫优化

    if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediap ...

  10. __FILES__

    _FILE_ :被称为PHP魔术常量 ,返回当前执行PHP脚本的完整路径和文件名,包含一个绝对路径 1)dirname(__FILE___) 函数返回的是脚本所在在的路径.   比如文件 b.php ...