如何以编程方式在c文件的每个函数中添加打印语句? [英] How to add print statements in every function in c files right programmatically?

查看:74
本文介绍了如何以编程方式在c文件的每个函数中添加打印语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究嵌入式代码,现在完全依靠函数内部的打印来确定执行流程(没有可用的堆栈跟踪功能)。

I am working on embedded code and for now totally reliant on prints from within functions to figure out the flow of execution (there is no stack trace functionality available).

经常发生的情况是,我放了一堆打印语句,构建代码并运行它,只是意识到我也应该在其他十几个地方放打印件。然后重新开始一个小时的过程。

It often happens that I put a bunch of print statements, build my code and run it only to realize I should've put prints in a dozen other places too. And then start the hour long process again.

是否有一种简单的方法来提取我想分析的5或6个c文件并运行一些将要使用的工具并在每个函数中添加打印语句? (显然,这必须在变量声明之后,因为它在C中)

Is there an easy way to take my 5 or 6 c files that I want to analyze and run some tool that will go in and add a print statement in each function? (this will obviously have to be after the variable declarations as this is in C)

更好的做法是,每当有一个if / else或switch时都进行打印/ case ..基本上是任何条件语句。

Even better would be to have a print each time there is an if/ else or switch/ case ..basically any conditional statements.

推荐答案

您没有说明正在使用的编译器,但是gcc有一个非常重要的条件。方便的开关:

You don't state the compiler you are using, but gcc has a very handy switch:

-finstrument-functions

在每个函数入口和出口插入一个特殊的调用。通过这种调整,您可以只编译相关文件,而无需修改源代码。

which inserts a special call at each function entry and exit. You could compile only the relevant files with this tweak, no need to modify the source code.

对您应创建的两个函数进行了调用:

The calls are made to two functions you should create:

      void __cyg_profile_func_enter (void *this_fn,
                                     void *call_site);
      void __cyg_profile_func_exit  (void *this_fn,
                                     void *call_site);

Etrace是一个用于利用此工具创建呼叫跟踪的工具,请参见 http://ndevilla.free.fr/etrace/

Etrace is a tool designed for exploiting this to create call traces, see http://ndevilla.free.fr/etrace/

这篇关于如何以编程方式在c文件的每个函数中添加打印语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆