6

例如,有来源:

void my_special_debugging_function(const char* function_name, const char* file_name, int line_number);

void func1() {
    func3();
    func4();
}

void foo() {
    func1();
    if(qqq) {
        func2();
    };
    func3();
    func4();
    for(...) {
        func5();
    }
}

它应该编译为:

void my_special_debugging_function(const char* function_name, const char* file_name, int line_number);

void func1() {
    my_special_debugging_function("func1", "prog.c", 3);
    func3();
    my_special_debugging_function("func1", "prog.c", 4);
    func4();
    my_special_debugging_function("func1", "prog.c", 5);
}

void foo() {
    my_special_debugging_function("foo", "prog.c", 8);
    func1();
    my_special_debugging_function("foo", "prog.c", 9);
    if(qqq) {
        my_special_debugging_function("foo", "prog.c", 10);
        func2();
        my_special_debugging_function("foo", "prog.c", 11);
    };
    my_special_debugging_function("foo", "prog.c", 12);
    func3();
    my_special_debugging_function("foo", "prog.c", 13);
    func4();
    my_special_debugging_function("foo", "prog.c", 14);
    for(...) {
        my_special_debugging_function("foo", "prog.c", 15);
        func5();
        my_special_debugging_function("foo", "prog.c", 16);
    }
    my_special_debugging_function("foo", "prog.c", 17);
}

当然,my_special_debugging_function 应该可以使用backtrace函数。

有 gcc 的选项吗?或者是否有工具可以在源代码级别执行此操作?(例如,用我的函数生成其他 C 源代码)

@related 如何用我的字符串“交错”C/C++ 源代码(仅在适当位置的函数内部)?

@related 我应该使用什么分析器来测量在这个函数中花费的_real_时间(包括等待系统调用),而不是_CPU_一个

4

4 回答 4

5

请参阅-finstrument-functionsGCC 文档。您可能希望dladdr()在调试功能中使用,这可能还需要与-Wl,-export-dynamic.

于 2010-10-20T22:41:35.337 回答
2

如果您使用的 gcc 版本 >=4.5,您可以编写一个 gcc 插件,以您喜欢的方式处理 AST。但是该解决方案将取决于编译器。

您还可以从 eclipse CDT 获取 AST并从该输入重新生成 C 代码。

于 2014-08-19T11:39:24.623 回答
0

如另一个答案所述,I don't think that there is any way to tell GCC to do that, without preprocessor tricks and edits to the source code. – nategoose

于 2010-10-30T02:33:47.077 回答
-1

您可以使用 aspectc++ 轻松完成。从aspectc.org获取此编译器这是一个满足您要求的简单方面。 追踪.ah

#ifndef __trace_ah__
#define __trace_ah__
#include <cstdio>
#include <iostream>
using namespace std;
template <int I> struct ArgPrinter
{
template <class JP> static inline void work (JP &tjp) {
ArgPrinter<I - 1>::work (tjp);
cout << *tjp.template arg<I - 1> () << " ";
}
};
template <> struct ArgPrinter<0>
{
 template <class JP> static inline void work (JP &tjp) {}
};


aspect trace {
int depth=-1;
pointcut virtual methods() = "% ...::%(...)";

template <class JP> void print_args (JP &tjp)
    {
         ArgPrinter<JP::ARGS>::work (tjp);
    }

advice execution (methods()) : before ()
{
    depth++;
    cout << "Enter -> " << JoinPoint::signature() <<" Depth:"<<depth<< " ARGS " << JoinPoint::ARGS <<" (";
    tjp->arg(0);
    print_args (*tjp);
    cout<<")"<<endl;
    cout<< "console--"<<endl;
}

advice execution("% ...::%(...)" && !"void ...::%(...)") : after()
{
    cout<< "--console"<<endl;
    JoinPoint::Result res = *tjp->result();
    cout << "Exit <- " << tjp->signature()<< " Depth: "<<depth<< " RET " << res<<endl;
    depth--;
}
advice execution("void ...::%(...)") : after()
{
    cout<< "--console"<<endl;
    cout << "Exit <- " << tjp->signature()<< " Depth: "<<depth<< " RET void"<<endl;
    depth--;
}
};
#endif

使用 ac++ 编译器将此方面与您的项目一起编译,然后运行您的程序。然后您应该在控制台中看到跟踪。追踪快乐!

于 2016-01-20T21:48:26.583 回答