printf debugging to trace a function

769 views Asked by At

I'm trying to port software to a microcontroller (so I can't step through the code with e.g. gdb) and it crashes unpleasantly. To identify the reason for this, I want to insert a printf() before every statement, echoing said statement, e.g.

void foo(int c) {
    bar();
    for(int i=0; i<c; ++c) {
        baz(i);
    }
    very_long_function(&with, &arguments, \
                       on->several(lines)); 
}

Would become

void foo(int c) {
    printf("bar();\n");
    bar();
    printf("for(int i=0; i<c; ++c)\n");
    for(int i=0; i<c; ++c) {
        printf("baz(i)\n");
        baz(i);
    }
    printf("very_long_function(&with, &arguments, \
                       on->several(lines));\n");
    very_long_function(&with, &arguments, \
                       on->several(lines));
}

Is there already some script to do this?

1

There are 1 answers

0
simonc On BEST ANSWER

It still requires a fair bit of setup but you can make tracking down the location of a crash a bit less painful by defining a macro which prints file/line and dotting that through your code

#define FL printf("File %s, line %u\n", __FILE__, __LINE__);

void foo(int c) {
FL    bar();
FL    for(int i=0; i<c; ++c) {
FL        baz(i);
    }
FL    very_long_function(&with, &arguments, \
                       on->several(lines)); 
FL}