Linux Device Driver Learning (2)-debugging technology

Source: Internet
Author: User
Tags echo 7
Go to Chapter 4 debugging technology of Linux Driver (version 3rd. I. kernel debugging support has been suggested earlier: to learn how to write a driver, you need to build and install your own kernel (Standard main kernel ). One of the most important reasons is that kernel developers have already set up a number of debugging functions. However, as these functions cause additional output and decrease, the release vendors usually disable the debugging functions in the release kernel.
To implement kernel debugging, I have added several kernel configurations:
  Kernel hacking  --->             [*] Magic SysRq key        [*] Kernel debugging        [*]   Debug slab memory allocations          [*]   Spinlock and rw-lock debugging: basic checks         [*]   Spinlock debugging: sleep-inside-spinlock checking        [*]   Compile the kernel with debug info          [*] Magic SysRq key Device Drivers  --->          Generic Driver Options  --->          [*] Driver Core verbose debug messages General setup  --->       [*] Configure standard kernel features (for small systems)  --->          [*]   Load all symbols for debugging/ksymoops
There are other configurations introduced in the book. Some of them are not needed, or they are not supported by S3C2440, and cannot be seen in the menu. Ii. Print and debug

(1) printk

First, printk has eight loglevels, which are defined in <Linux/kernel. h>:
#define    KERN_EMERG    "<0>" /* system is unusable           */#define    KERN_ALERT    "<1>" /* action must be taken immediately*/#define    KERN_CRIT    "<2>" /* critical conditions    */#define    KERN_ERR    "<3>" /* error conditions            */#define    KERN_WARNING    "<4>" /* warning conditions   */#define    KERN_NOTICE    "<5>" /* normal but significant condition */#define    KERN_INFO    "<6>" /* informational            */#define    KERN_DEBUG    "<7>" /* debug-level messages   */
The default level of unspecified priority is defined in/kernel/printk. C:
#define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
Information can be displayed only when the priority value is smaller than the value of the integer variable lele_loglevel. The initial value of console_loglevel default_console_loglevel is also defined in/kernel/printk. C:
#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
While the program that changes the console_loglevel is running (provided in Linux Device Driver (version 3rd) as follows:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#define __LIBRARY__ /* _syscall3 and friends are only available through this */#include <linux/unistd.h>/* define the system call, to override the library function */_syscall3(int, syslog, int, type, char *, bufp, int, len);int main(int argc, char **argv){int level;if (argc==2) {    level = atoi(argv[1]); /* the chosen console */} else {fprintf(stderr, "%s: need a single arg\n",argv[0]); exit(1);}if (syslog(8,NULL,level) < 0) {fprintf(stderr,"%s: syslog(setlevel): %s\n",                argv[0],strerror(errno));exit(1);}exit(0);}
The most critical “syslog(8,NULL,level)”I don't understand the statement. I didn't find any relevant information. However, the experiment on the arm9-board shows that the program is OK! I used the hello World module for an experiment. The phenomenon is the same as that in the book.

[Tekkaman2440@SBC2440V4]#cd /tmp/[Tekkaman2440@SBC2440V4]#./setlevel 1[Tekkaman2440@SBC2440V4]#cd /lib/modules/[Tekkaman2440@SBC2440V4]#insmod hello.ko[Tekkaman2440@SBC2440V4]#rmmod hello[Tekkaman2440@SBC2440V4]#cd /tmp/[Tekkaman2440@SBC2440V4]#./setlevel 7[Tekkaman2440@SBC2440V4]#cd /lib/modules/[Tekkaman2440@SBC2440V4]#insmod hello.koHello, Tekkaman Ninja ![Tekkaman2440@SBC2440V4]#rmmod helloGoodbye, Tekkaman Ninja ! Love Linux !Love ARM ! Love KeKe ![Tekkaman2440@SBC2440V4]# 
And through /Proc/sys/kernel/printk access to changeLele_loglevel value:
[Tekkaman2440@SBC2440V4]#echo 1 > /proc/sys/kernel/printk[Tekkaman2440@SBC2440V4]#cat /proc/sys/kernel/printk1 4       1       7[Tekkaman2440@SBC2440V4]#insmod hello.ko[Tekkaman2440@SBC2440V4]#rmmod hello[Tekkaman2440@SBC2440V4]#echo 7 > /proc/sys/kernel/printk[Tekkaman2440@SBC2440V4]#cat /proc/sys/kernel/printk7  4       1       7[Tekkaman2440@SBC2440V4]#insmod hello.koHello, Tekkaman Ninja ![Tekkaman2440@SBC2440V4]#rmmod helloGoodbye, Tekkaman Ninja ! Love Linux !Love ARM ! Love KeKe !

Meanings of the four numbers: Current loglevel, default loglevel, minimum allowed loglevel, and default loglevel during boot.

To enable and disable debugging information conveniently, the Linux Device Driver (version 3rd) provides the following source code:
/* Macros to help debugging */#undef PDEBUG /* undef it, just in case */#ifdef SCULL_DEBUG# ifdef __KERNEL__/* This one if debugging is on, and kernel space */# define PDEBUG(fmt, args...) printk( KERN_DEBUG "scull: " fmt, ## args)# else /* This one for user space */# define PDEBUG(fmt, args...) fprintf(stderr, fmt, ## args)# endif#else# define PDEBUG(fmt, args...) /* not debugging: nothing */#endif#undef PDEBUGG#define PDEBUGG(fmt, args...) /* nothing: it's a placeholder */
The statement to be added in makefile:
# Comment/uncomment the following line to disable/enable debuggingDEBUG = y# Add your debugging flag (or not) to CFLAGSifeq ($(DEBUG),y)  DEBFLAGS = -O -g -DSCULL_DEBUG # "-O" is needed to expand inlineselse  DEBFLAGS = -O2endifCFLAGS += $(DEBFLAGS)
To avoid too fast duplicate printk output and blocking the system, the kernel uses the following function to skip part of the output:
int printk_ratelimit(void);
Typical applications are as follows:
if (printk_ratelimit( ))    printk(KERN_NOTICE "The printer is still on fire\n");
You can modify/proc/sys/kernel/printk_ratelimit (the number of seconds to wait before re-opening the information) and/proc/sys/kernel/printk_ratelimit_burst (the number of messages acceptable before the speed limit) to customize the printk_ratelimit behavior. Linux also provides macros for printing device numbers (defined in <Linux/kdev_t.h> ): int print_dev_t(char *buffer, dev_t dev);

char *format_dev_t(char *buffer, dev_t dev);
The only difference between the two functions is: print_dev_t returns the number of printed characters, and format_dev_t returns the buffer pointer. Note that the size of the buffer char * buffer should be at least 20 B. Iii. query and debugging

In most cases, the best way to obtain relevant information is to query the system information as needed, rather than continuously generating data.

Use the/proc file system

The/proc file system is a special file system created by software. The kernel uses it to export information to the outside world. Each file under/proc is bound to a kernel function. When you read the file, the function dynamically generates the file content. As previously used:
[Tekkaman2440@SBC2440V4]#cat /proc/devicesCharacter devices:  1 mem  2 pty  3 ttyp  4 /dev/vc/0  4 tty  4 ttyS  5 /dev/tty  5 /dev/console  5 /dev/ptmx  7 vcs 10 misc 13 input 14 sound 81 video4linux 89 i2c 90 mtd116 alsa128 ptm136 pts180 usb189 usb_device204 s3c2410_serial252 scull253 usb_endpoint254 rtcBlock devices:  1 ramdisk256 rfd  7 loop 31 mtdblock 93 nftl 96 inftl179 mmc
The/proc module must contain <Linux/proc_fs.h>, and the seq_file interface must contain <Linux/seq_file.h>.

For specific application methods, the source program and experiment are more effective. For other debugging methods, such as GDB, LTT, and sysrq, in other books, such: detailed description of Embedded Linux System Development Technology-Based on ARM, building embedded Linux system, etc.

4. Source Code Experiment

Module Program link: Module Program

Module test program Link: module test program

Lab symptom:

[Tekkaman2440@SBC2440V4]#cd /lib/modules/[Tekkaman2440@SBC2440V4]#insmod scull_debug.ko scull_nr_devs=1 scull_quantum=6 scull_qset=2[Tekkaman2440@SBC2440V4]#cd /tmp/[Tekkaman2440@SBC2440V4]#./scull_testwrite code=6write code=6write code=6write code=2read code=6read code=6read code=6read code=2[0]=0 [1]=1 [2]=2 [3]=3 [4]=4[5]=5 [6]=6 [7]=7 [8]=8 [9]=9[10]=10 [11]=11 [12]=12 [13]=13 [14]=14[15]=15 [16]=16 [17]=17 [18]=18 [19]=19[Tekkaman2440@SBC2440V4]#cd /proc/[Tekkaman2440@SBC2440V4]#ls1              751            cmdline        kallsyms       stat2              769            cpu            kmsg           swaps3              77             cpuinfo        loadavg        sys4              778            crypto         locks          sysrq-trigger5              779            devices        meminfo        sysvipc59             78             diskstats      misc           timer_list6              781            driver         modules        tty60             783            execdomains    mounts         uptime63             785            filesystems    mtd            version65             79             fs             net            vmstat707            80             ide            partitions     yaffs708            819            interrupts     scullmem       zoneinfo709            asound         iomem          scullseq710            buddyinfo      ioports        self742            bus            irq            slabinfo[Tekkaman2440@SBC2440V4]#cat scullmemDevice 0: qset 2, q 6, sz 20  item at c071ebd4, qset at c071ef7c  item at c071ef14, qset at c071eee0       0: c071eeac       1: c071ee78[Tekkaman2440@SBC2440V4]#cat scullseqDevice 0: qset 2, q 6, sz 20  item at c071ebd4, qset at c071ef7c  item at c071ef14, qset at c071eee0       0: c071eeac       1: c071ee78[Tekkaman2440@SBC2440V4]#rmmod scull_debug[Tekkaman2440@SBC2440V4]#ls1              742            buddyinfo      iomem          self2              751            bus            ioports        slabinfo3              769            cmdline        irq            stat4              77             cpu            kallsyms       swaps5              778            cpuinfo        kmsg           sys59             779            crypto         loadavg        sysrq-trigger6              78             devices        locks          sysvipc60             781            diskstats      meminfo        timer_list63             783            driver         misc           tty65             785            execdomains    modules        uptime707            79             filesystems    mounts         version708            80             fs             mtd            vmstat709            824            ide            net            yaffs710            asound         interrupts     partitions     zoneinfo

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.