Skip to main content.

5.4.1. Debugging with printk

simple

#define DEBUG

#ifdef DEBUG
# define _DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt "\n", __FUNCTION__, ##args)
#else
# define _DBG(fmt, args...) do { } while(0);
#endif

/* example usage */
void read_this(int count)
{
   _DBG("reading count=%d bytes", count);
}

Debug levels:

#define DEBUG
#define DEBUG_LEVEL_LOW
#define DEBUG_LEVEL_MEDIUM
#define DEBUG_LEVEL_HIGH

static int debug = DEBUG_LEVEL_MEDIUM; /* change with ioctl or proc */

#ifdef DEBUG
# define _DBG(x, fmt, args...) do{ if (debug>=x) printk(KERN_CRIT "%s: " fmt "\n", __FUNCTION__, ##args); } while(0);
#else
# define _DBG(x, fmt, args...) do { } while(0);
#endif

/* example usage */
void function1()
{
        _DBG(DEBUG_LEVEL_LOW, "addr1=0x%x, addr2=0x%x", addr1, addr2);
}

void fatal_error()
{
   _DBG(DEBUG_LEVEL_HIGH, "this is the end");
}

Debug selective subsystems:

#define DEBUG

#define DEBUG_READ   (1<<0)
#define DEBUG_WRITE   (1<<1)
#define DEBUG_LOCKS   (1<<2)

static unsigned int debug_mask = 0;

#ifdef DEBUG
# define _DBG(x, fmt, args...) do{ if (debug_mask & x) printk(KERN_DEBUG "%s: " fmt "\n", __FUNCTION__, ##args); } while(0);
#else
# define _DBG(x, fmt, args...) do { } while(0);
#endif

/* example usage */
void read()
{
   _DBG(DEBUG_READ, "inside read function");
}

void write(int count)
{
   _DBG(DEBUG_WRITE, "writing count=%d bytes", count);
}

5.4. Debugging Techniques 1. Denx Training Topics 5.4.2. Debugging Oopses
Prev Home Next