Debugging techniques of embedded C language code

Source: Internet
Author: User

In the process of project development, it is unavoidable to encounter the debugging code situation.

When we first started to write code, we wanted to see where the specific execution went, often:

printf ("* * * * Code is here! \ n ");

As debugging information continues to grow, the printf () functions in our project are everywhere, so when debugging is done, it is very painful to find that you need to comment out all the debug code. As a result, the code is transformed by a macro definition:

First, define the Debug macro:

#define _debug_is_my

Then, add the following code where you want to add the debug code:

#ifdef _debug_is_my

printf ("* * * * Code is here! \ r \ n ");

#endif

This seems to solve the problem, but it makes the project code look bloated and too much repetitive code (#ifdef _debug_is_my ... #endif), which programmers can't tolerate.

Therefore, referring to a lot of low-level code and network materials, see that there is indeed a very good and mature wording:

/*
* Custom Debug macros.
* 1. Quickly switch between debug version and Relese version project
* 2. Simplify the writing of printing and debugging statements, improve the efficiency of development debugging
*/
#define _MY_DEBUG_ 1

#ifdef _my_debug_
#define MY_DEBUG_PRINT_INFO (...) printf (__va_args__)
#define MY_DEBUG_PRINT_VAR (X, ...) printf ("File:" __file__ ", Line:%d:" X "\ r \ n", __line__,# #__VA_ARGS__)
#else
#define MY_DEBUG_PRINT_INFO (...)
#define MY_DEBUG_PRINT_VAR (X, ...)
#endif

In the program you need to add debugging code, call directly:

My_debug_print_info ("code is here!");

Or

My_debug_print_var ("code is here!");

Or

My_debug_print_var ("Recev_flag =%d.", Recev_flag);

The debug information can be implemented as shown in the normal output:

  

Attention:

1, the carriage return line under Windows is "\ r \ n".

2. Predefined macros are used, which provide information about the current compilation and compiler itself.

__LINE__: Inserts the current source code line number in the source code;

__FILE__: Inserts the current source filename in the source file;

The above two macros can also be used as error detection. For example the problem was removed by 0.

#define Check_zero (divisor) \

if (0 = = divisor) \

printf ("Attemp to divide by 0 on line%d of file%s ***\r\n", __line__, __file__);

  The Check_zero macro should be called before the division operation.

  Check_zero (j);

K = i/j;

If J is 0, the above prompt will be printed.

Refer to the following blog:

1, http://www.360doc.com/content/14/0703/10/7324690_391662898.shtml

2, http://blog.csdn.net/aobai219/article/details/6092292

3, http://blog.csdn.net/cp1300/article/details/7773239

4. The modern method of C language programming

  

  

        

Debugging techniques of embedded C language code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.