0

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?

4

1 回答 1

2

它仍然需要进行相当多的设置,但您可以通过定义一个打印文件/行的宏并在您的代码中点缀它,从而使追踪崩溃的位置变得不那么痛苦

#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}
于 2013-04-25T13:45:48.400 回答