c - 如何告诉 gcc 通过在代码的每一行调用我自己的函数来检测代码?

标签 c gcc profiling instrumentation

比如有来源:

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 How to "interleave" C/C++ souce with my string (only inside functions at appropriate places)?

@related What profiler should I use to measure _real_ time (including waiting for syscalls) spend in this function, not _CPU_ one

最佳答案

请参阅 GCC 文档中的 -finstrument-functions。您可能想在调试函数中使用 dladdr(),这可能还需要与 -Wl,-export-dynamic 链接。

关于c - 如何告诉 gcc 通过在代码的每一行调用我自己的函数来检测代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3982868/

相关文章:

ruby-on-rails - 如何使用 rack-mini-profiler 分析返回 json 响应的 rails Controller ?

二叉树的缓存位置

c++ - 链接时不包含宏定义

创建一个静态库并链接到它

c++ - VS 2010,非托管 C++ 的性能向导

c - 测量代码的吞吐量和延迟

c - 是否可以从内存泄漏中获取数据?

c - 可能使某些应用程序崩溃的字符串的最小长度

c - 在C中将指针分配给结构体时出现问题

gcc - GCC 内联汇编中的寄存器可以使用漂亮的变量名吗?