【C语言】 C语言之调试

1  简介


       大家都知道编写程序后,会有一项重要的工作——调试程序,那么如何在调试程序时高效而便捷,就需要编写适合自己的调试部件。本文适合于C51、ARM等嵌入式平台。


2  版权声明

博主:E-Mouse

声明:饮水思源,转载请表明来源。

           如涉及作品内容、版权和其他问题,请在30日内与博主联系。

联系方式:519632131@qq.com

原文地址:http://blog.csdn.net/dianzilaoshu


3  工具/原料

软件:KEIL5 C51

源码:My_Debug.h


4  方法/步骤

4.1  My_Debug.h源码

/*******************************************************************************
  * Copyright (C), 2017 E-Mouse工作室 519632131@qq.com
  ******************************************************************************
  * @file    My_Debug.c
  * @author  Jon
  * @E-maill 519632131@qq.com
  * @version V1.0.0
  * @date    26-June-2017
  * @brief   这个文件是程序调试文件。
  ****************************************************************************** 
  * @attention
  *
  * 输入接口:printf函数
  *           系统选项
  * 
  * 输出接口:MY_ASSERT(message, assertion);
  *           MY_ERROR(message, expression, handler);  
  *           MY_DEBUGF(debug, message);
  * 
  ******************************************************************************
  * @update
  *
  * (Jon 20170626)
  * 创建此文件
  * 添加调试部件
  * 
  *
  ******************************************************************************
  */
#ifndef __MY_DEBUG_H__
#define __MY_DEBUG_H__

#include "sys_opt.h"

#include "stdio.h"
     
/** lower two bits indicate debug level
 * - 0 all
 * - 1 warning
 * - 2 serious
 * - 3 severe
 */
#define MY_DBG_LEVEL_ALL     0x00
#define MY_DBG_LEVEL_OFF     MY_DBG_LEVEL_ALL /* compatibility define only */
#define MY_DBG_LEVEL_WARNING 0x01 /* bad checksums, dropped packets, ... */
#define MY_DBG_LEVEL_SERIOUS 0x02 /* memory allocation failures, ... */
#define MY_DBG_LEVEL_SEVERE  0x03
#define MY_DBG_MASK_LEVEL    0x03

/** flag for MY_DEBUGF to enable that debug message */
#define MY_DBG_ON            0x80U
/** flag for MY_DEBUGF to disable that debug message */
#define MY_DBG_OFF           0x00U

/** flag for MY_DEBUGF indicating a tracing message (to follow program flow) */
#define MY_DBG_TRACE         0x40U
/** flag for MY_DEBUGF indicating a state debug message (to follow module states) */
#define MY_DBG_STATE         0x20U
/** flag for MY_DEBUGF indicating newly added code, not thoroughly tested yet */
#define MY_DBG_FRESH         0x10U
/** flag for MY_DEBUGF to halt after printing this debug message */
#define MY_DBG_HALT          0x08U


/*打印接口*/
#ifndef MY_PLATFORM_ASSERT
#define MY_PLATFORM_ASSERT(x) \
    do \
    {   printf("Assertion \"%s\" failed at line %d in %s\r\n", x, __LINE__, __FILE__); \
    } while(0)
#endif

#ifndef MY_PLATFORM_DIAG
#define MY_PLATFORM_DIAG(x) do {printf x;} while(0)
#endif

/*断言*/
#ifndef MY_ASSERT
#define MY_ASSERT(message, assertion) do { if(!(assertion)) \
  MY_PLATFORM_ASSERT(message); } while(0)
#else  /* MY_NOASSERT */
#define MY_ASSERT(message, assertion) 
#endif /* MY_NOASSERT */

/*错误调试*/
/** if "expression" isn't true, then print "message" and execute "handler" expression */
#ifndef MY_ERROR
#define MY_ERROR(message, expression, handler) do { if (!(expression)) { \
  MY_PLATFORM_ASSERT(message); handler;}} while(0)
#else
#define MY_ERROR(message, expression, handler)
#endif /* MY_ERROR */

/*警告调试*/
#ifndef MY_DEBUG
/** print debug message only if debug message type is enabled...
 *  AND is of correct type AND is at least MY_DBG_LEVEL
 */
#define MY_DEBUGF(debug, message) do { \
                                if ( \
                                    ((debug) & MY_DBG_ON) && \
                                    ((debug) & MY_DBG_TYPES_ON) && \
                                    ((signed short int)((debug) & MY_DBG_MASK_LEVEL) >= MY_DBG_MIN_LEVEL)) { \
                                    MY_PLATFORM_DIAG(message); \
                                if ((debug) & MY_DBG_HALT) { \
                                while(1); \
                                 } \
                               } \
                             } while(0)
#else  /* MY_DEBUG */
#define MY_DEBUGF(debug, message) 
#endif /* MY_DEBUG */

#else
//#define MY_ASSERT(message, assertion)
//#define MY_ERROR(message, expression, handler)
//#define MY_DEBUGF(debug, message)
#endif /* __MY_DEBUG_H__ */

/************* (C) COPYRIGHT 2017 E-Mouse工作室 *****END OF FILE****************/


4.2  调用说明

4.2.1 My_Debug.h输入API

        ① 调用printf函数打印出相关信息,printf打印驱动接口因不同的平台而各异,这里不阐述。住:本人是采用51内核的单片机串口打印至PC进行调试。

② 系统选项应个人而定,比如在系统选项中加入工程上需要调试的文件的开关。

4.2.1 My_Debug.h输出API

请阅读相关代码,一读便知!


4.2  调用例子

MY_ASSERT("PCB must be deallocated outside this function", conn->pcb.tcp == NULL);
MY_ERROR("igmp_joingroup: attempt to join non-multicast address", ip_addr_ismulticast(groupaddr), return ERR_VAL;);
MY_DEBUGF(API_LIB_DEBUG, ("netconn_recv_data: received %p, len=%"U16_F"\n", buf, len));

/*打印代码当前位置*/
MY_DEBUGF(MY_DBG_ON, ("Current code at line %d in %s!\r\n",(unsigned short)__LINE__, __FILE__));

4  注意事项

在keil5 c51中打印当前位置的行号,需要强制转换类型,否则当前出来的数据与预期不同。

  • 10
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值