关于 Linux 驱动的调试方法
(2011-12-25 15:50:38)
标签:
armx86linux驱动oops调试it |
分类: iLinuxTek |
注:这些关于Linux内核的博文都是本人给一些培训机构的学生上课的课件内容
以前在写 Linux 驱动的时候 ,遇到比较多的kernel panic oops
问题,然后
问一些 同事 ,比较多的回答都是加 printk,其实用 GDB 的中的一个功能是能很方便地debug 这些问题的。本文是以前给一个 Linux 驱动培训班上课的课件,也贴在CSDN 上很久了,这次贴在这里.
问一些 同事 ,比较多的回答都是加 printk,其实用 GDB 的中的一个功能是能很方便地debug 这些问题的。本文是以前给一个 Linux 驱动培训班上课的课件,也贴在CSDN 上很久了,这次贴在这里.
将ARM ,X86 的Linux driver
OOPS 调试方法共享如下:
•
•1.1.
•
•1
•
•2
•3) as,ld,objdump,etc
•4) glibc/uclibc
•5) make
•6) gdb
•7
•8
•下用
•
•1.2
•在内核中
•#define
•#define
•#define
•#define
•#define
•#define
•#define
•#define
•通过
•通过如下命令可以使得
•#echo 8 > /proc/sys/kernel/printk
•同时设置
•在
•
•1.3 oops
•1.3.1 API oops DEBUG
•
•1.3.1.1.
•示例:
•
•#include <stdio.h>
•#include <stdlib.h>
•Const char array[]="/x6b/xc0 ";
•int main(int argc, char *argv[])
•{
•
•
•}
•
•1.
•[root@localhost ~]# gcc -g -o apioops apioops.c
•
•
•2
•[root@localhost ~]# ./apioops
•
•显示屏输出信息
•0x4005e0
•Segmentation fault
•[root@localhost ~]#
•
•串口输出信息:
•apioops[28910]: segfault at 0000000000000000 rip 00000000004004c0 rsp 00007fff22e15760 error 6
•rip 00000000004004c0
•
•EIP RIP
•
3
•[root@localhost ~]# gdb apioops
•GNU gdb Fedora (6.8-27.el5)
•Copyright (C) 2008 Free Software Foundation, Inc.
•License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
•This is free software: you are free to change and redistribute it.
•There is NO WARRANTY, to the extent permitted by law.
•and "show warranty" for details.
•This GDB was configured as "x86_64-redhat-linux-gnu"...
•(
•
•1.list
•(gdb) l*0x4004c0
•0x4004c0 is in main (apioops.c:11).
•6
•7
•8
•9
•10
•11
•12
•(gdb)
•
•2. run apioops
•(gdb) r
•Starting program: /root/apioops
•0x4005c8
•Program received signal SIGSEGV, Segmentation fault.
•0x00000000004004c0 in main (argc=1, argv=0x7fffa7020a28) at oops.c:11
•11
•(gdb)
•
•3)
•1.
•(gdb) r
•Starting program: /root/apioops
•(no debugging symbols found)
•(no debugging symbols found)
•0x4005c8
•Program received signal SIGSEGV, Segmentation fault.
•0x00000000004004c0 in main ()
•(gdb)
•
•
•2.
•(
•Dump of assembler code for function main:
•0x0000000000400498 <main+0>:
•0x0000000000400499 <main+1>:
•0x000000000040049c <main+4>:
•0x00000000004004a0 <main+8>:
•0x00000000004004a3 <main+11>:
•0x00000000004004a7 <main+15>:
•0x00000000004004ac <main+20>:
•0x00000000004004b1 <main+25>:
•0x00000000004004b6 <main+30>:
•0x00000000004004bb <main+35>:
•0x00000000004004c0 <main+40>:
•0x00000000004004c6 <main+46>:
•0x00000000004004c7 <main+47>:
•End of assembler dump.
•(
•可以看到出错地址
•0x00000000004004c0 <main+40>:
•表明是这个地址的代码访问了空指针
•
•3.
•[root@localhost ~]# objdump -d apioops > log
•………………………………….
•0000000000400498 <main>:
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•…………………………………………
•可以看到出错地址
•0x00000000004004c0 <main+40>:
•
•1.3.2.kernel oops debug
•示例代码:
•注意编译的时候代开
•编译:
•创建设备节点:
•写操作:
•[root@localhost test]# echo 1 > /dev/oopsexam
•从串口拿到的
•
•<6>Enter oopsexam_write
•Unable to handle kernel NULL pointer dereference at 0000000000000000 RIP:
•
•PGD 50ccd067 PUD 50ccc067 PMD 0
•Oops: 0002 [1] SMP
•last sysfs file: /block/sda/dev
•CPU 0
•Modules linked in: oops(FU) ipv6 xfrm_nalgo crypto_api autofs4 hidp rfcomm l2cap bluetooth sunrpc dm_mirror dm_multipath scsi_dh video hwmon backlight sbs i2c_ec button battery asus_acpi acpi_memhotplug ac parport_pc lp parport floppy sg pcspkr i3000_edac edac_mc i2c_i801 i2c_core e1000 serio_raw e1000e dm_raid45 dm_message dm_region_hash dm_log dm_mod dm_mem_cache ata_piix libata shpchp mptsas mptscsih mptbase scsi_transport_sas sd_mod scsi_mod ext3 jbd uhci_hcd ohci_hcd ehci_hcd
•Pid: 28853, comm: bash Tainted: GF
•
•
•Pid: 28853, comm: bash Tainted: GF
•RIP: 0010:[<ffffffff8848401c>]
•RSP: 0018:ffff8100514dbf08
•RAX: 0000000000000002 RBX: 0000000000000002 RCX: ffffffff802f7aa8
•RDX: ffffffff802f7aa8 RSI: 0000000000000000 RDI: ffffffff802f7aa0
•RBP: 0000000000000002 R08: ffffffff802f7aa8 R09: 0000000000000046
•R10: ffff8100514dbc98 R11: ffffffff80161742 R12: 00002ba383367000
•R13: ffff8100514dbf50 R14: 0000000000000000 R15: 0000000000000000
•FS:
•CS:
•CR2: 0000000000000000 CR3: 00000000508c4000 CR4: 00000000000006e0
•Process bash (pid: 28853, threadinfo ffff8100514da000, task ffff810051eb97a0)
•Stack:
•
•
•Call Trace:
•
•
•
•Code: c7 04 25 00 00 00 00 01 00 00 00 5b c3 53 48 c7 c6 a7 40 48
•RIP
•
•CR2: 0000000000000000
•
•
•很关键的地方:
•RIP
调试:
•[root@localhost test]# gdb oops.ko
•GNU gdb Fedora (6.8-27.el5)
•Copyright (C) 2008 Free Software Foundation, Inc.
•License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
•This is free software: you are free to change and redistribute it.
•There is NO WARRANTY, to the extent permitted by law.
•and "show warranty" for details.
•This GDB was configured as "x86_64-redhat-linux-gnu"...
•(gdb)
•1. list
•(gdb) l*oopsexam_write+0x1c
•0x1c is in oopsexam_write (/home/test/examoops.c:73).
•warning: Source file is more recent than executable.
•68
•69
•70
•71
•72
•73
•74
•75
•76
•77
•(gdb)
•
•
•
•开发板上
•
•[root@utu-linux /test]# echo 1 >/dev/oopsexam
•<6>Enter oopsexam_write
•Unable to handle kernel NULL pointer dereference at virtual address 00000000
•pgd = c3ca4000
•[00000000] *pgd=33c96031, *pte=00000000, *ppte=00000000
•Internal error: Oops: 817 [#1]
•Modules linked in: oopsexam utuled button
•CPU: 0
•PC is at oopsexam_write+0x28/0x38 [oopsexam]
•LR is at 0x1
•pc : [<bf004060>]
•sp : c3c29f3c
•r10: c3c29f78
•r7 : 40177000
•r3 : 00000000
•Flags: nZCv
•Control: C000717F
•Process echo (pid: 829, stack limit = 0xc3c28194)
•Stack: (0xc3c29f3c to 0xc3c2a000)
•
•Stack: (0xc3c29f3c to 0xc3c2a000)
•9f20:
•9f40: c3c29f74 c3c29f50 c0070d84 bf004048 c0628324 c0628300 c3c29f78 00000000
•9f60: 00000000 401736bc c3c29fa4 c3c29f78 c0070ebc c0070cd4 00000000 00000000
•9f80: 00000000 00000002 40177000 40171c8c 00000004 c0022024 00000000 c3c29fa8
•9fa0: c0021ea0 c0070e80 00000002 c002877c 00000001 40177000 00000002 00000000
•9fc0: 00000002 40177000 40171c8c 40177000 00000002 0000c06c 401736bc bec36e68
•9fe0: 00000000 bec36dc4 00001920 40113490 60000010 00000001 00646f6d 00000000
•Backtrace:
•[<bf004038>] (oopsexam_write+0x0/0x38 [oopsexam]) from [<c0070d84>] (vfs_write+0
•xc0/0x138)
•
•[<c0070cc4>] (vfs_write+0x0/0x138) from [<c0070ebc>] (sys_write+0x4c/0x74)
•[<c0070e70>] (sys_write+0x0/0x74) from [<c0021ea0>] (ret_fast_syscall+0x0/0x2c)
•
•
•Code: eb40d1be e3a02001 e3a03000 e1a00004 (e5832000)
•
•
•2. objdump
•Objdump –d oopsexam.ko > log
•Vim log :
•0000000000000000 <oopsexam_write>:
•
•
•
•
•
•
•
•
•
•
•
•很明显,往一个空指针 赋值
•
•如何找到对应的代码,那就的对应着代码看汇编。

加载中…