通过gdb定位到oops具体行号

概述

  模拟异常,定位行号。

通过代码制造异常

oops.c

#include <linux/kernel.h> 
#include <linux/module.h> 
#include <linux/init.h> 
 
static void create_oops(void)
{ 
        *(int *)0 = 0; 
} 
 
static int __init my_oops_init(void)
{ 
        printk("oops from the module\n"); 
        create_oops(); 
       return (0); 
} 
static void __exit my_oops_exit(void)
{ 
        printk("Goodbye world\n"); 
} 
 
module_init(my_oops_init); 
module_exit(my_oops_exit);

makefile

CONFIG_MODULE_SIG = n

KVERS = $(shell uname -r)

#kernel modules
obj-m += oops.o

build: kernel_modules

kernel_modules:
    make -C /lib/modules/$(KVERS)/build M=$(CURDIR) CONFIG_DEBUG_INFO=1 modules

clean:
    make -C /lib/modules/$(KVERS)/build M=$(CURDIR) clean

  加粗、下划线部分编译时候开始 -g 选项。

产生异常

  sudo insmod oops.ko

[ 1146.005290] oops: loading out-of-tree module taints kernel.
[ 1146.005293] oops: module license 'unspecified' taints kernel.
[ 1146.005293] Disabling lock debugging due to kernel taint
[ 1146.005313] oops: module verification failed: signature and/or required key missing - tainting kernel
[ 1146.005564] oops from the module
[ 1146.005568] BUG: unable to handle kernel NULL pointer dereference at           (null)
[ 1146.005589] IP: [<ffffffffc051c010>] my_oops_init+0x10/0x1000 [oops]
[ 1146.005604] PGD 0 
[ 1146.005611] Oops: 0002 [#1] SMP 
[ 1146.005620] Modules linked in: oops(POE+) rndis_host cdc_ether usbnet input_leds snd_hda_intel intel_rapl x86_pkg_temp_thermal intel_powerclamp coretemp kvm_intel kvm snd_hda_codec snd_hda_core irqbypass crct10dif_pclmul crc32_pclmul snd_hwdep ghash_clmulni_intel aesni_intel snd_pcm aes_x86_64 lrw gf128mul glue_helper ablk_helper cryptd snd_seq_midi snd_seq_midi_event snd_rawmidi snd_seq snd_seq_device snd_timer serio_raw snd soundcore shpchp mei_me mei 8250_fintek acpi_pad mac_hid binfmt_misc parport_pc ppdev lp parport autofs4 hid_generic usbhid hid i915_bpo intel_ips i2c_algo_bit drm_kms_helper psmouse syscopyarea sysfillrect sysimgblt fb_sys_fops r8169 drm ahci mii libahci video fjes
[ 1146.005809] CPU: 0 PID: 3632 Comm: insmod Tainted: P           OE   4.4.0-155-generic #182-Ubuntu
[ 1146.005827] Hardware name: Gigabyte Technology Co., Ltd. B250M-HD3/B250M-HD3-CF, BIOS F7 07/06/2017
[ 1146.005845] task: ffff8802253f8000 ti: ffff8801da7f8000 task.ti: ffff8801da7f8000
[ 1146.005861] RIP: 0010:[<ffffffffc051c010>]  [<ffffffffc051c010>] my_oops_init+0x10/0x1000 [oops]
[ 1146.005880] RSP: 0018:ffff8801da7fbc88  EFLAGS: 00010282
[ 1146.005892] RAX: 0000000000000014 RBX: ffffffff81e13080 RCX: 0000000000000006
[ 1146.005906] RDX: 0000000000000000 RSI: 0000000000000246 RDI: ffff88022ec105d0
[ 1146.005921] RBP: ffff8801da7fbc88 R08: 000000000000000a R09: 0000000000000082
[ 1146.005935] R10: ffffea00025c95c0 R11: 0000000000000357 R12: ffff880097257d60
[ 1146.005950] R13: 0000000000000000 R14: ffffffffc051c000 R15: ffff880222840f00
[ 1146.005965] FS:  00007f2ed5317700(0000) GS:ffff88022ec00000(0000) knlGS:0000000000000000
[ 1146.005982] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 1146.005994] CR2: 0000000000000000 CR3: 00000001da7a8000 CR4: 0000000000360670
[ 1146.006008] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[ 1146.006023] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[ 1146.006037] Stack:
[ 1146.006042]  ffff8801da7fbd08 ffffffff81002135 96f6ee71bad93869 ffff8800972ade00
[ 1146.006061]  ffff8801da785c00 ffffffff811dd8e9 ffffffffc0519000 ffffffff811fb1c5
[ 1146.006079]  ffff8801da7fbcf0 ffffffff811fbdad 0000000000000018 96f6ee71bad93869
[ 1146.006098] Call Trace:
[ 1146.006107]  [<ffffffff81002135>] do_one_initcall+0xb5/0x200
[ 1146.006121]  [<ffffffff811dd8e9>] ? __vunmap+0xc9/0xf0
[ 1146.006133]  [<ffffffff811fb1c5>] ? kmem_cache_alloc_trace+0x185/0x1f0
[ 1146.006146]  [<ffffffff811fbdad>] ? kfree+0x13d/0x150
[ 1146.006159]  [<ffffffff81198ca5>] do_init_module+0x5f/0x1cf
[ 1146.006172]  [<ffffffff8111365e>] load_module+0x16ae/0x1c50
[ 1146.006185]  [<ffffffff8110fb60>] ? __symbol_put+0x60/0x60
[ 1146.006198]  [<ffffffff81223980>] ? kernel_read+0x50/0x80
[ 1146.006211]  [<ffffffff81113e44>] SYSC_finit_module+0xb4/0xe0
[ 1146.006224]  [<ffffffff81113e8e>] SyS_finit_module+0xe/0x10
[ 1146.006237]  [<ffffffff81864f1b>] entry_SYSCALL_64_fastpath+0x22/0xcb
[ 1146.006251] Code: <c7> 04 25 00 00 00 00 00 00 00 00 31 c0 5d c3 00 00 00 00 00 00 00 
[ 1146.006288] RIP  [<ffffffffc051c010>] my_oops_init+0x10/0x1000 [oops]
[ 1146.006304]  RSP <ffff8801da7fbc88>
[ 1146.006312] CR2: 0000000000000000
[ 1146.011603] ---[ end trace b3da19429be27ce0 ]---

 

 

定位异常到具体行号

rivsidn@rivsidn:~/demo/driver/oops$ gdb oops.ko
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from oops.ko...done.
(gdb) list *(my_oops_init+0x10)
0x34 is in my_oops_init (/home/rivsidn/demo/driver/oops/oops.c:7).
2    #include <linux/module.h> 
3    #include <linux/init.h> 
4     
5    static void create_oops(void)
6    { 
7            *(int *)0 = 0; 
8    } 
9     
10    static int __init my_oops_init(void)
11    { 

 

 

参考资料

 

posted on 2019-07-13 16:08  rivsidn  阅读(1158)  评论(0编辑  收藏  举报

导航