如何告诉gcc使用我自己的函数调用每个_line_代码来检测代码?

Vi.*_*Vi. 5 c instrumentation gcc profiling

例如,有来源:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

它应编译为:

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);
}
Run Code Online (Sandbox Code Playgroud)

当然,my_special_debugging_function应该能够使用backtrace函数.

有没有选择gcc来做到这一点?或者是否有工具在源代码级别执行此操作?(例如用我的函数生成其他C源)

@related 如何用我的字符串"交错"C/C++ 源码(只在适当的地方内部函数)?

@related 应该使用什么探查器来测量_real_ time(包括等待系统调用)花在这个函数上,而不是_CPU_一个

jil*_*les 5

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

  • `-finstrument-functions` 在函数级别工作。如何使其按线路工作? (3认同)