views:

53

answers:

2

For example, there is the source:

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();
    }
}

It should compile as:

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);
}

Of course, my_special_debugging_function should be able to use backtrace function.

Is there option of gcc to do it? Or is there a tool to do it at souce code level? (e.g. generate other C souce with my function)

@related http://stackoverflow.com/questions/3989992/how-to-interleave-c-c-souce-with-my-string-only-inside-functions-at-appropri

@related http://stackoverflow.com/questions/3992587/what-profiler-should-i-use-to-measure-real-time-including-waiting-for-syscalls

+3  A: 

See -finstrument-functions in the GCC documentation. You may want to use dladdr() in the debugging function, which may also require linking with -Wl,-export-dynamic.

jilles
`-finstrument-functions` work at function level. How to make it to work line-wise?
Vi
@Vi: 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
@nategoose, OK, re-asking about tricks for source code: http://stackoverflow.com/questions/3989992/how-to-interleave-c-c-souce-with-my-string-only-inside-functions-at-appropri
Vi
A: 

As stated in the other answer, 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

Vi