Skip to content

谈谈Debug的方法 #9

@clarazhang

Description

@clarazhang

我先抛砖引玉。
我主要用printf来进行调试,一般会定义如下的宏:

#ifndef NDEBUG1
#define debug(b, fmt, args...)\
    if (b) fprintf(stderr, "[debug] %s:%d: \n\t"fmt"\n", \
        __FILE__, __LINE__, ## args)
#else
#define debug(b, ...)   // do nothing
#endif
#define __DBG__         printf("***%s:%d\n",__FILE__, __LINE__);

这样,可以
1)为试图定位程序执行到什么地方出错,可以在被调试的程序中按程序执行的流程,在需要的地方安插若干__DBG__,编译运行程序,看能输出程序执行到哪些位置;

2)使用debug(...) 对程序中重要的位置输出执行到该位置时的相关信息。采用这种方式安插的debug调用在代码调通后不必删掉,只需定义宏NDEBUG1来编译程序即可。

欢迎各位同学在此Issue下补充你们调试遇到的问题和方法。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions