宏定义编写技巧__调试技巧【原创】

带颜色打印:

printk("\033[1;33;40m misc.c InterIoctl() action=%d\033[0m\r\n", action);

方法一、

 1 #ifndef _PMU_DUMMY_
 2
 3 #define _PMU_DUMMY_
 4
 5
 6
 7 #define PAX_PMU_DUMMY_DEBUG_ENABLE
 8
 9
10
11 #ifdef PAX_PMU_DUMMY_DEBUG_ENABLE
12
13 #define PAX_PMU_DUMMY_DEBUG(x...)   do{printk("\r\n");printk(x);printk("\r\n");}while(0)
14
15 #else
16
17 #define PAX_PMU_DUMMY_DEBUG(x...)
18
19 #endif
20
21
22
23 #define REGULATOR_DUMMY        "dummy_regulator"
24
25
26
27 #endif

方法二、

 1 #define DEBUG_ENABLE   1
 2
 3
 4
 5 #if  DEBUG_ENABLE
 6
 7 #define PRINT(fmt, args...)  do{ 8
 9         printf("<%s>【*%s()*】(%d):", __FILE__, __func__, __LINE__);10
11         printf(fmt, ##args);12
13 }while(0)
14
15 #else
16
17 #define PRINT(fmt, args...)
18
19 #endif

方法三、

 1 // 带格式的打印调试
 2 int Serial_Printf(char *fmt, ...)
 3 {
 4   va_list args;
 5   int printed = 0;
 6
 7   uint8 printf_buf[MAX_PRINTF_LEN];
 8
 9   va_start(args, fmt);
10   printed = vsprintf((char*)printf_buf, fmt, args);
11   va_end(args);
12
13   printf_buf[MAX_PRINTF_LEN-1] = ‘\0‘;
14   SerialPrintString(printf_buf);
15
16   return printed;
17 }
1 /*
2 打印一个字符串
3 str不可以包含0x00,除非结尾
4 */
5 void SerialPrintString(uint8 str[])
6 {
7   HalUARTWrite (SBP_UART_PORT, str, osal_strlen((char*)str));
8 }
 1 //#define DEBUG
 2
 3 #ifdef DEBUG
 4 #define DEBUG_PRINT(format, ...)            Serial_Printf(format, ##__VA_ARGS__)
 5 #define DEBUG_PRINT_BUFF(title, buff, len, display_char)   6                                             Serial_PrintBuffer(title, buff, len, display_char)
 7 #else
 8 #define DEBUG_PRINT(format, ...)
 9 #define DEBUG_PRINT_BUFF(title, buff, len, display_char)
10 #endif  
方法四、
//console.c
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <pthread.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>

#include "Console.h"

/*----------------------------------------------------------*/
/*! \brief Structure which will be copied into shared memory, in order to synchronize console output from
 *         different processes and threads.
 */
/*----------------------------------------------------------*/
typedef struct {
    ///If set to true, there is an segmented print ongoing (Start, Continue, Exit).
    bool criticalSection;
    ///Handle of the shared mutex.
    pthread_mutex_t mutex;
} shared_data;

/*----------------------------------------------------------*/
/*! \brief Pointer to the shared memory instance.
 */
/*----------------------------------------------------------*/
static shared_data* data = NULL;

void show_read(unsigned char *str, unsigned char *buff, unsigned int len)
{
    ConsolePrintfStart("serial-RX: %s [", str);
    for (int i = 0; i < len; i++) {
        ConsolePrintfContinue("%02X ", buff[i]);
    }
    ConsolePrintfExit("]\r\n");
}

void ConsoleInit(void)
{
    pthread_mutexattr_t attr;
    int prot = PROT_READ | PROT_WRITE;
    int flags = MAP_SHARED | MAP_ANONYMOUS;
    data = (shared_data*)mmap(NULL, sizeof(shared_data), prot, flags, -1, 0);

    data->criticalSection = false;

    pthread_mutexattr_init(&attr);
    pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
    pthread_mutex_init(&data->mutex, &attr);
}

void ConsoleDeinit(void)
{
    munmap(data, sizeof(data));
    data = NULL;
}

void ConsolePrintf(const char *statement, ...)
{
    int err;
    if (NULL == data) {
        printf("ConsolePrintf data was null\n");
        while (true);
    }
    if (0 != (err = pthread_mutex_lock(&data->mutex))) {
        printf("ConsolePrintf, pthread_mutex_lock error: %d\n", err);
    }
    if (NULL != statement) {
        va_list args;
        va_start(args, statement);
        vprintf(statement, args);
        va_end(args);
    }
    if (0 != (err = pthread_mutex_unlock(&data->mutex))) {
        printf("ConsolePrintf, pthread_mutex_unlock error: %d\n", err);
        while (true);
    }
}

void ConsolePrintfStart(const char *statement, ...)
{
    int err;
    if (NULL == data) {
        printf("ConsolePrintfStart data was null\n");
        while (true);
    }
    if (0 != (err = pthread_mutex_lock(&data->mutex))) {
        printf("ConsolePrintfStart, pthread_mutex_lock error: %d\n", err);
    }
    data->criticalSection = true;

    if (NULL != statement) {
        va_list args;
        va_start(args, statement);
        vprintf(statement, args);
        va_end(args);
    }
}

void ConsolePrintfContinue(const char *statement, ...)
{
    if (NULL == data) {
        printf("ConsolePrintfContinue data was null\n");
        while (true);
    }
    if (!data->criticalSection) {
        printf("ConsolePrintfContinue not in critical section\n");
        while (true);
    }

    if (NULL != statement) {
        va_list args;
        va_start(args, statement);
        vprintf(statement, args);
        va_end(args);
    }
}

void ConsolePrintfExit(const char *statement, ...)
{
    int err;
    if (NULL == data) {
        printf("ConsolePrintfExit data was null\n");
        while (true);
    }
    if (!data->criticalSection) {
        printf("ConsolePrintfExit not in critical section\n");
        while (true);
    }
    if (NULL != statement) {
        va_list args;
        va_start(args, statement);
        vprintf(statement, args);
        va_end(args);
    }
    data->criticalSection = false;
    if (0 != (err = pthread_mutex_unlock(&data->mutex))) {
        printf("ConsolePrintfExit, pthread_mutex_unlock error: %d\n", err);
        while (true);
    }
}
//console.h
/*----------------------------------------------------------*/
/*! \file
 *  \brief This file contains C-functions starting with "Console" to provide
 *         process and thread safe access to the console output.
 */
/*----------------------------------------------------------*/
#ifndef _CONSOLE_H_
#define _CONSOLE_H_

#define RESETCOLOR "\033[0m"
#define GREEN      "\033[0;32m"
#define RED        "\033[0;31m"
#define YELLOW     "\033[1;33m"
#define BLUE       "\033[0;34m"

#ifdef __cplusplus
extern "C"
{
#endif

    void show_read(unsigned char *str, unsigned char *buff, unsigned int len);
    /*----------------------------------------------------------*/
    /*! \brief Initializes the resources needed to synchronize between processes and threads.
     *  \note This function must be called before any other function of this component.
     *
     */
    /*----------------------------------------------------------*/
    void ConsoleInit(void);

    /*----------------------------------------------------------*/
    /*! \brief Destroys the resources needed to synchronize between processes and threads.
     *  \note After this function, any other function (except ConsoleInit) must not be called.
     *
     */
    /*----------------------------------------------------------*/
    void ConsoleDeinit(void);

    /*----------------------------------------------------------*/
    /*! \brief Uses the board specific PRINT mechanism and provides thread and process safety.
     *
     */
    /*----------------------------------------------------------*/
    void ConsolePrintf(const char *statement, ...);

    /*----------------------------------------------------------*/
    /*! \brief Starts to print and stay blocked after exit of this function
     *
     */
    /*----------------------------------------------------------*/
    void ConsolePrintfStart(const char *statement, ...);

    /*----------------------------------------------------------*/
    /*! \brief Continue to print and stay blocked after exit of this function
     *  \note ConsolePrintfStart must be called before and when finished ConsolePrintfExit must be called.
     *  \note This function may be called multiple times.
     */
    /*----------------------------------------------------------*/
    void ConsolePrintfContinue(const char *statement, ...);

    /*----------------------------------------------------------*/
    /*! \brief Continue to print and unblock after finishing.
     *  \note ConsolePrintfStart must be called before. ConsolePrintfContinue may have been called before multiple times.
     */
    /*----------------------------------------------------------*/
    void ConsolePrintfExit(const char *statement, ...);

#ifdef __cplusplus
}
#endif

#endif //_CONSOLE_H_
#ifndef _BASE_H_
#define _BASE_H_

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <setjmp.h>
#include <sys/mman.h>

#include "uart.h"
#include "Console.h"

//#define DEBUG

#ifdef DEBUG
    #define DEBUG_PRINT(format, ...)    ConsolePrintf(format, ##__VA_ARGS__);
    #define SHOW_READ(format, ...)    show_read(format, ##__VA_ARGS__);
#else
    #define DEBUG_PRINT(format, ...)
    #define SHOW_READ(format, ...)
#endif

#define     FALSE        0
#define     TRUE        1

typedef unsigned char   BYTE;           //  8-bit
typedef unsigned short  WORD;           // 16-bit
typedef unsigned long   DWORD;          // 32-bit
typedef unsigned char        u8;
typedef unsigned short        u16;
typedef unsigned int        u32;
typedef unsigned long long    u64;
typedef unsigned char        uint8;
typedef unsigned short        uint16;
typedef unsigned int        uint32;
typedef unsigned long long    uint64;

/* takes a byte out of a uint32 : var - uint32,  ByteNum - byte to take out (0 - 3) */
#define BREAK_UINT32( var, ByteNum ) \
          (uint8)((uint32)(((var) >>((ByteNum) * 8)) & 0x00FF))

#define BUILD_UINT32(Byte0, Byte1, Byte2, Byte3) \
          ((uint32)((uint32)((Byte0) & 0x00FF)           + ((uint32)((Byte1) & 0x00FF) << 8)           + ((uint32)((Byte2) & 0x00FF) << 16)           + ((uint32)((Byte3) & 0x00FF) << 24)))

#define BUILD_UINT16(loByte, hiByte) \
          ((uint16)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)))

#define HI_UINT16(a) (((a) >> 8) & 0xFF)
#define LO_UINT16(a) ((a) & 0xFF)

#define BUILD_UINT8(hiByte, loByte) \
          ((uint8)(((loByte) & 0x0F) + (((hiByte) & 0x0F) << 4)))

#define HI_UINT8(a) (((a) >> 4) & 0x0F)
#define LO_UINT8(a) ((a) & 0x0F)

#endif
时间: 2024-10-12 17:15:23

宏定义编写技巧__调试技巧【原创】的相关文章

js调试技巧 Firefox调试技巧汇总

Firebug入门指南        :  http://www.ruanyifeng.com/blog/2008/06/firebug_tutorial.html Firebug控制台详解: http://www.cnblogs.com/see7di/archive/2011/11/21/2257442.html    http://www.cnblogs.com/leejersey/archive/2012/11/27/2790998.html debugger: js中调用console写

宏定义神级用法-调试信息便捷开关

大家编写程序的时候,一般都会有调试信息,这些调试信息一般长期伴随着,后来要发布的时候发那些不需要的调试信息给删除掉,如果是一个有丰富的经验人,是采用一个宏定义的开关来决定是否启动这些打印信息.代码如下: #include <stdio.h> /******************************************************************************** DEBUG: 0 关闭打印信息 1 打开打印信息 *********************

Visual Studio 2015中的常用调试技巧分享

.NET 技术交流群:337901356 欢迎您的加入! 为什么要学习调试? 调试(Debug)是作为一个程序员必须要学会的东西,学会调试可以极大的提高开发效率,排错时间,很多人不喜欢调试,但我认为这是一个很不可取的选择,调试的时候能让我们看到程序的执行顺序.步骤以及过程等,调试的时候可以让我们监视代码中各个变量的情况,调试让我们可以让我们快速的找出错误的根源.可见调试是至关重要的. 要学习好怎么调试,那么必须去了解VS 这个IDE中的各种调试技巧,下面我就讲讲我所经常在调试程序中所用到的技巧.

C语言宏定义使用技巧

写好C语言,漂亮的宏定义很重要,使用宏定义可以防止出错,提高可移植性,可读性,方便性 等等.下面列举一些成熟软件中常用得宏定义...... 1,防止一个头文件被重复包含 #ifndef COMDEF_H #define COMDEF_H //头文件内容 #endif 2,重新定义一些类型,防止由于各种平台和编译器的不同,而产生的类型字节数差异,方便移植. typedef  unsigned char      boolean;     /* Boolean value type. */ type

C语言宏定义技巧

网络出处:http://blog.chinaunix.net/uid-14022540-id-2849095.html 宏中"#"和"##"的用法 一.一般用法 我们使用#把宏参数变为一个字符串,用##把两个宏参数贴合在一起. #include <stdio.h> #define CONS(a,b) ((int)(a##e##b)) #define STR(s) #s int main() { printf("%s\n",STR(AB

iOS技巧,宏定义

1.NSlog  发布后不打印 #ifdef DEBUG// 如果有DEBUG这个宏就编译下面一句代码 #define DDLog(...) NSLog(__VA_ARGS__) #else // 如果没有DEBUG这个宏就编译下面一句代码 #define DDLog(...) #endif 2.三方库及其他整理 1.?AFNetworking   HTTP 网络请求库   2.?SDWebImage 图片缓存 3.?MBProgressHUD 加载中展示 4.MMDrawerControlle

嵌入式系统编程和调试技巧

嵌入式系统的开发,软件的运行稳定可靠是非常重要的.在芯片中,软件是没有质量的,但软件的质量可以决定一颗芯片的成败.芯片设计中,性能能否满足设计要求,除了硬件设计.软硬件配合的设计技巧,对于软件来说,编程的一些技术和技巧同样重要. 本文讲述我在芯片固件开发过程中使用的一些编程调试技巧.针对在嵌入式系统开发中常见的问题,如实时系统下的同步问题,动态内存分配的内存泄漏问题,如何在编程阶段预防BUG出现,调试阶段如何及时发现问题和定位问题.总结下经验,目的是开发一个稳定运行的固件,提高开发效率,提高运行

Visual Studio高级调试技巧

1. 设置软件断点,运行到目标位置启动调试器 方法①:使用汇编指令(注:x64 c++不支持汇编) _asm int 3 方法②:编译器提供的方法 __debugbreak(); 方法③:使用windows API DebugBreak(); WerFault.exe进程(Windows Error Reporting)弹出ConsoleTest.exe已停止工作: 要想出现“调试程序”选项,需要将Windows Error Reporting注册表信息设置成如下图所示(注:特别是红框的内容)

iOS开发调试技巧总结(持续更新中)

作者:乞力马扎罗的雪  原文 对于软件开发而言,调试是必须学会的技能,重要性不言而喻.对于调试的技能,基本上是可以迁移的,也就是说你以前在其他平台上掌握的很多调试技巧,很多也是可以用在iOS开发中.不同语言.不同IDE.不同平台的调试,有同性也有个性.今天我们就来学习一下iOS开发中的调试技巧,语言暂用为OC,IDE当然是强大的Xcode.首先说明下,Xcode已经为我们调试项目提供了极大的方便. [1.普通断点] 断点(Breakpoint)绝对是调试程序的第一大选择,也是掌握的基础技能.顾名