Linux Device Driver Article 4: Driver debugging methods, linux Article 4

Source: Internet
Author: User

Linux Device Driver Article 4: Driver debugging methods, linux Article 4

In the previous article, we probably talked about how to write a simple character device driver. We are not a god, but there will certainly be a problem when writing code. We need to continuously debug it during the code writing process. In common c applications, we often use printf to output information or use gdb to debug the program. How can we debug the driver? We know that the common problem encountered during program debugging is the problem caused by the out-of-bounds of the wild pointer or array. Running this program in the application will report the segmentation fault error, due to the special nature of the driver, such a situation may lead to system downtime and oops information. So how can we analyze oops information and even locate specific error code lines based on oops information? The following describes how to debug the driver based on a simple example.

How to locate code lines based on oops

We use the "hello world" program in the "build and run" module of the linux Device Driver to demonstrate the error. The "hello world" program containing the error code is as follows:

#include <linux/init.h>#include <linux/module.h>MODULE_LICENSE("Dual BSD/GPL");static int hello_init(void){        char *p = NULL;        memcpy(p, "test", 4);        printk(KERN_ALERT "Hello, world\n");        return 0;}static void hello_exit(void){        printk(KERN_ALERT "Goodbye, cruel world\n");}module_init(hello_init);module_exit(hello_exit);

The Makefile file is as follows:

ifneq ($(KERNELRELEASE),)obj-m := helloworld.oelseKERNELDIR ?= /lib/modules/$(shell uname -r)/buildPWD := $(shell pwd)default:        $(MAKE) -C $(KERNELDIR) M=$(PWD) modulesendifclean:        rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions modules.order  Module.symvers

Obviously, the first line of the above Code is a null pointer error. The following oops information appears after insmod:

[  459.516441] BUG: unable to handle kernel NULL pointer dereference at           (null)[  459.516445] [  459.516448] PGD 0 [  459.516450] Oops: 0002 [#1] SMP [  459.516452] Modules linked in: helloworld(OE+) vmw_vsock_vmci_transport vsock coretemp crct10dif_pclmul crc32_pclmul ghash_clmulni_intel aesni_intel vmw_balloon snd_ens1371 aes_x86_64 lrw snd_ac97_codec gf128mul glue_helper ablk_helper cryptd ac97_bus gameport snd_pcm serio_raw snd_seq_midi snd_seq_midi_event snd_rawmidi snd_seq snd_seq_device snd_timer vmwgfx btusb ttm snd drm_kms_helper drm soundcore shpchp vmw_vmci i2c_piix4 rfcomm bnep bluetooth 6lowpan_iphc parport_pc ppdev mac_hid lp parport hid_generic usbhid hid psmouse ahci libahci floppy e1000 vmw_pvscsi vmxnet3 mptspi mptscsih mptbase scsi_transport_spi pata_acpi [last unloaded: helloworld][  459.516476] CPU: 0 PID: 4531 Comm: insmod Tainted: G           OE 3.16.0-33-generic #44~14.04.1-Ubuntu[  459.516478] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 05/20/2014[  459.516479] task: ffff88003821f010 ti: ffff880038fa0000 task.ti: ffff880038fa0000[  459.516480] RIP: 0010:[<ffffffffc061400d>]  [<ffffffffc061400d>] hello_init+0xd/0x30 [helloworld][  459.516483] RSP: 0018:ffff880038fa3d40  EFLAGS: 00010246[  459.516484] RAX: ffff88000c31d901 RBX: ffffffff81c1a020 RCX: 000000000004b29f[  459.516485] RDX: 000000000004b29e RSI: 0000000000000017 RDI: ffffffffc0615024[  459.516485] RBP: ffff880038fa3db8 R08: 0000000000015e80 R09: ffff88003d615e80[  459.516486] R10: ffffea000030c740 R11: ffffffff81002138 R12: ffff88000c31d0c0[  459.516487] R13: 0000000000000000 R14: ffffffffc0614000 R15: ffffffffc0616000[  459.516488] FS:  00007f8a6fa86740(0000) GS:ffff88003d600000(0000) knlGS:0000000000000000[  459.516489] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033[  459.516490] CR2: 0000000000000000 CR3: 0000000038760000 CR4: 00000000003407f0[  459.516522] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000[  459.516524] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400[  459.516524] Stack:[  459.516537]  ffff880038fa3db8 ffffffff81002144 0000000000000001 0000000000000001[  459.516540]  0000000000000001 ffff880028ab5040 0000000000000001 ffff880038fa3da0[  459.516541]  ffffffff8119d0b2 ffffffffc0616018 00000000bd1141ac ffffffffc0616018[  459.516543] Call Trace:[  459.516548]  [<ffffffff81002144>] ? do_one_initcall+0xd4/0x210[  459.516550]  [<ffffffff8119d0b2>] ? __vunmap+0xb2/0x100[  459.516554]  [<ffffffff810ed9b1>] load_module+0x13c1/0x1b80[  459.516557]  [<ffffffff810e9560>] ? store_uevent+0x40/0x40[  459.516560]  [<ffffffff810ee2e6>] SyS_finit_module+0x86/0xb0[  459.516563]  [<ffffffff8176be6d>] system_call_fastpath+0x1a/0x1f[  459.516564] Code: <c7> 04 25 00 00 00 00 74 65 73 74 31 c0 48 89 e5 e8 a2 86 14 c1 31 [  459.516573] RIP  [<ffffffffc061400d>] hello_init+0xd/0x30 [helloworld][  459.516575]  RSP <ffff880038fa3d40>[  459.516576] CR2: 0000000000000000[  459.516578] ---[ end trace 7c52cc8624b7ea60 ]---

  

The following is a brief analysis of oops information.

BUG: unable to handle kernel NULL pointer dereference at (null) knows that the error is caused by the use of a NULL pointer. The highlighted part determines the specific error function. Modules linked in: helloworld indicates the specific module that causes oops problems. Call trace lists the call information of a function. The highlighted part of the information is the most useful. We can find the specific error code line based on the information. The following describes how to locate the specific error code line.

In the hosts file:

objdump helloworld.o -D > err.txt  

Err.txt contains the following content:

Helloworld. o: file format elf64-x86-64Disassembly of section. text: <span> coder_online, scan the QR code below or search for coder_online to follow, we can communicate online.

 

From: http://my.oschina.net/haomcu/blog/394945

 

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.