test.c
如果当前是纯终端则会将oops信息和适当优先级的信息打印至终端
可通过echo 8 > /proc/sys/kernel/printk修改优先级则可以打印所有信息
可通过修改/etc/inittab将默认值从5改为3,reboot进入纯终端
所有oops信息和所有优先级信息都会出现在/var/log/messages
看一下反汇编
*p=3;
23: c6 05 00 00 00 00 03 movb $0x3,0x0
可以另开一个终端 去监视内核信息cat /proc/kmesg
dmesg 即cat /var/log/messages
**********************************************************************************************************************************************************
micro2440下测试,
改一下Makefile,KDIR := /opt/FriendlyARM/mini2440/linux-2.6.32.2/
板子默认printk优先级是4,当前控制台优先级是7,
char *p=NULL;
*p=3;
24: e3a00000 mov r0, #0 ; 0x0
28: e3a03003 mov r3, #3 ; 0x3
2c: e5c03000 strb r3, [r0]
#include <linux/kernel.h>
#include <linux/module.h>
void test_func()
{
char *p=NULL;
*p=3;
}
static int __init mini2440_hello_module_init(void)
{
printk(KERN_INFO"Hello, KERN_INFO !\n");
printk(KERN_ALERT"Hello, KERN_ALERT !\n");
printk("<1>Hello, KERN_ALERT 1 !\n");
test_func();
return 0;
}
static void __exit mini2440_hello_module_cleanup(void)
{
printk("<4>Good-bye, Mini2440 module was removed!\n");
}
module_init(mini2440_hello_module_init);
module_exit(mini2440_hello_module_cleanup);
MODULE_LICENSE("GPL");
Makefile
ifneq ($(KERNELRELEASE),)
obj-m := test.o
else
KDIR := /lib/modules/$(shell uname -r)/build
#KDIR := /opt/FriendlyARM/mini2440/linux-2.6.32.2/
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.ko *.o *.mod.o *.mod.c *.symvers
endif
x86下测试
[root@localhost test]# make
[root@localhost test]# insmod test.ko
Killed
先看一下打印优先级
[root@localhost test]# cat /proc/sys/kernel/printk
4 4 1 7
/*
第一个4是console_loglevel,当前控制台优先级,凡是printk使用的优先级>=4(数字<=4),的信息都会在屏幕上打印出来(仅对于纯终端)
第二个4是default_message_loglevel,printk默认使用的优先级
第三个mininum_console_level
第四个defalut_console_loglevel
*/
由于是x窗口,rsyslog在运行,打印结果不会出现在终端,而出现在/var/log/messages(包括所有oops和优先级的信息)
如果当前是纯终端则会将oops信息和适当优先级的信息打印至终端
可通过echo 8 > /proc/sys/kernel/printk修改优先级则可以打印所有信息
可通过修改/etc/inittab将默认值从5改为3,reboot进入纯终端
所有oops信息和所有优先级信息都会出现在/var/log/messages
[root@localhost test]# cat /var/log/messages
......
Sep 11 09:03:05 localhost kernel: [ 114.497566] Hello, KERN_INFO !
Sep 11 09:03:05 localhost kernel: [ 114.497592] Hello, KERN_ALERT !
Sep 11 09:03:05 localhost kernel: [ 114.497995] Hello, KERN_ALERT 1 !
Sep 11 09:03:05 localhost kernel: [ 114.502025] BUG: unable to handle kernel NULL pointer dereference at (null)
Sep 11 09:03:05 localhost kernel: [ 114.502025] IP: [<f788e023>] mini2440_hello_module_init+0x23/0x2c [test]
Sep 11 09:03:05 localhost kernel: [ 114.502025] *pde = 00000000
Sep 11 09:03:05 localhost kernel: [ 114.502025] Oops: 0002 [#1] SMP
Sep 11 09:03:05 localhost kernel: [ 114.502025] last sysfs file: /sys/kernel/mm/ksm/run
Sep 11 09:03:05 localhost kernel: [ 114.502025] Modules linked in: test(+) fuse ebtable_nat ebtables ipt_MASQUERADE iptable_nat nf_nat bridge stp llc rmd160 crypto_null camellia lzo lzo_compress cast6 cast5 deflate zlib_deflate cts ctr gcm ccm serpent blowfish twofish twofish_common ecb xcbc cbc sha256_generic sha512_generic des_generic aes_i586 geode_aes aes_generic ah6 ah4 esp6 esp4 xfrm4_mode_beet xfrm4_tunnel tunnel4 xfrm4_mode_tunnel xfrm4_mode_transport xfrm6_mode_transport xfrm6_mode_ro xfrm6_mode_beet xfrm6_mode_tunnel ipcomp ipcomp6 xfrm_ipcomp xfrm6_tunnel tunnel6 af_key sunrpc lp vmblock vsock capi capifs kernelcapi vmhgfs ip6t_REJECT nf_conntrack_ipv6 ip6table_filter ip6_tables ipv6 uinput snd_ens1371 gameport snd_rawmidi snd_ac97_codec ac97_bus snd_seq pcnet32 snd_seq_device snd_pcm ppdev parport_pc vmxnet snd_timer parport snd mii i2c_piix4 soundcore microcode snd_page_alloc vmware_balloon vmci i2c_core mptspi mptscsih mptbase scsi_transport_spi [last unloaded: mperf]
Sep 11 09:03:05 localhost kernel: [ 114.502025]
Sep 11 09:03:05 localhost kernel: [ 114.502025] Pid: 2810, comm: insmod Not tainted 2.6.35.6-45.fc14.i686 #1 440BX Desktop Reference Platform/VMware Virtual Platform
Sep 11 09:03:05 localhost kernel: [ 114.502025] EIP: 0060:[<f788e023>] EFLAGS: 00010246 CPU: 0
Sep 11 09:03:05 localhost kernel: [ 114.502025] EIP is at mini2440_hello_module_init+0x23/0x2c [test]
Sep 11 09:03:05 localhost kernel: [ 114.502025] EAX: 00000000 EBX: f788e000 ECX: ebf73f7c EDX: f788b07f
Sep 11 09:03:05 localhost kernel: [ 114.502025] ESI: 00000000 EDI: 0083eff4 EBP: ebf73f84 ESP: ebf73f78
Sep 11 09:03:05 localhost kernel: [ 114.502025] DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068
Sep 11 09:03:05 localhost kernel: [ 114.502025] Process insmod (pid: 2810, ti=ebf72000 task=ebc571a0 task.ti=ebf72000)
Sep 11 09:03:05 localhost kernel: [ 114.502025] Stack:
Sep 11 09:03:05 localhost kernel: [ 114.502025] f788b07f f788b067 f788b050 ebf73f9c c0401246 ebf73f9c c0451e51 f788b0c8
Sep 11 09:03:05 localhost kernel: [ 114.502025] <0> 00000000 ebf73fac c04621a4 098c7018 006cfc65 ebf72000 c07a73ac 098c7018
Sep 11 09:03:05 localhost kernel: [ 114.502025] <0> 00016259 098c7008 006cfc65 0083eff4 bf9f3978 ffffffda 0000007b 0000007b
Sep 11 09:03:05 localhost kernel: [ 114.502025] Call Trace:
Sep 11 09:03:05 localhost kernel: [ 114.502025] [<c0401246>] ? do_one_initcall+0x4f/0x139
Sep 11 09:03:05 localhost kernel: [ 114.502025] [<c0451e51>] ? blocking_notifier_call_chain+0x11/0x13
Sep 11 09:03:05 localhost kernel: [ 114.502025] [<c04621a4>] ? sys_init_module+0x7f/0x19b
Sep 11 09:03:05 localhost kernel: [ 114.502025] [<c07a73ac>] ? syscall_call+0x7/0xb
Sep 11 09:03:05 localhost kernel: [ 114.502025] Code: <c6> 05 00 00 00 00 03 c9 c3 00 00 00 00 00 00 00 00 00 00 00 00 55
Sep 11 09:03:05 localhost kernel: [ 114.502025] EIP: [<f788e023>] mini2440_hello_module_init+0x23/0x2c [test] SS:ESP 0068:ebf73f78
Sep 11 09:03:05 localhost kernel: [ 114.502025] CR2: 0000000000000000
Sep 11 09:03:05 localhost kernel: [ 114.506701] ---[ end trace 46dc376b81027f5f ]---
[root@localhost test]#
line 15表明cpu执行到
mini2440_hello_module_init+0x23/0x2c挂掉
看一下反汇编
[root@localhost test]# objdump -S test.ko
test.ko: file format elf32-i386
Disassembly of section .text:
00000000 <test_func>:
#include <linux/kernel.h>
#include <linux/module.h>
void test_func()
{
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: e8 fc ff ff ff call 4 <test_func+0x4>
char *p=NULL;
*p=3;
8: c6 05 00 00 00 00 03 movb $0x3,0x0
}
f: 5d pop %ebp
10: c3 ret
11: 90 nop
12: 90 nop
13: 90 nop
Disassembly of section .init.text:
00000000 <init_module>:
static int __init mini2440_hello_module_init(void)
{
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
printk(KERN_INFO"Hello, KERN_INFO !\n");
3: 68 00 00 00 00 push $0x0
8: e8 fc ff ff ff call 9 <init_module+0x9>
printk(KERN_ALERT"Hello, KERN_ALERT !\n");
d: 68 17 00 00 00 push $0x17
12: e8 fc ff ff ff call 13 <init_module+0x13>
printk("<1>Hello, KERN_ALERT 1 !\n");
17: 68 2f 00 00 00 push $0x2f
1c: e8 fc ff ff ff call 1d <init_module+0x1d>
test_func();
return 0;
}
21: 31 c0 xor %eax,%eax
#include <linux/kernel.h>
#include <linux/module.h>
void test_func()
{
char *p=NULL;
*p=3;
23: c6 05 00 00 00 00 03 movb $0x3,0x0
printk(KERN_INFO"Hello, KERN_INFO !\n");
printk(KERN_ALERT"Hello, KERN_ALERT !\n");
printk("<1>Hello, KERN_ALERT 1 !\n");
test_func();
return 0;
}
2a: c9 leave
2b: c3 ret
Disassembly of section .exit.text:
00000000 <cleanup_module>:
static void __exit mini2440_hello_module_cleanup(void)
{
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: e8 fc ff ff ff call 4 <cleanup_module+0x4>
printk("<4>Good-bye, Mini2440 module was removed!\n");
8: 68 49 00 00 00 push $0x49
d: e8 fc ff ff ff call e <cleanup_module+0xe>
12: 58 pop %eax
}
13: c9 leave
14: c3 ret
[root@localhost test]#
line 51-line 52
*p=3;
23: c6 05 00 00 00 00 03 movb $0x3,0x0
可以另开一个终端 去监视内核信息cat /proc/kmesg
dmesg 即cat /var/log/messages
**********************************************************************************************************************************************************
micro2440下测试,
改一下Makefile,KDIR := /opt/FriendlyARM/mini2440/linux-2.6.32.2/
[root@localhost test]# make CONFIG_DEBUG_INFO=y
为了得到容易看懂的反汇编信息,需要定义一下这个宏CONFIG_DEBUG_INFO,因为在源码根目录下的Makefile中有
ifdef CONFIG_DEBUG_INFO
KBUILD_CFLAGS += -g
KBUILD_AFLAGS += -gdwarf-2
endif
板子默认printk优先级是4,当前控制台优先级是7,
[root@FriendlyARM /]# cat /proc/sys/kernel/printk
7 4 1 7
所以3个printk都打印了,但/var/log/messages里没有记录东东
[root@FriendlyARM plg]# insmod test.ko
Hello, KERN_INFO !
Hello, KERN_ALERT !
Hello, KERN_ALERT 1 !
Unable to handle kernel NULL pointer dereference at virtual address 00000000
pgd = c3a9c000
[00000000] *pgd=33b27031, *pte=00000000, *ppte=00000000
Internal error: Oops: 817 [#1]
last sysfs file: /sys/devices/virtual/vc/vcs2/dev
Modules linked in: test(+)
CPU: 0 Not tainted (2.6.32.2-FriendlyARM #1)
PC is at mini2440_hello_module_init+0x2c/0x40 [test]
LR is at release_console_sem+0x188/0x1e4
pc : [<bf00302c>] lr : [<c004a6d8>] psr: 60000013
sp : c3b25f28 ip : c3b25e40 fp : c3b25f34
r10: bf003000 r9 : 00000000 r8 : c0503660
r7 : c3b24000 r6 : bf0000d4 r5 : 000a18b7 r4 : 000067b1
r3 : 00000003 r2 : c04c9638 r1 : 00009baa r0 : 00000000
Flags: nZCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment user
Control: c000717f Table: 33a9c000 DAC: 00000015
Process insmod (pid: 737, stack limit = 0xc3b24270)
Stack: (0xc3b25f28 to 0xc3b26000)
5f20: c3b25f7c c3b25f38 c003032c bf003010 00000000 00000000
5f40: 00000000 000067b1 000a18b7 bf0000d4 00000000 000067b1 000a18b7 bf0000d4
5f60: 00000000 c00310c8 c3b24000 00000000 c3b25fa4 c3b25f80 c0070344 c0030300
5f80: c009b6b0 c009b5a0 4001d838 00000000 befafe88 00000080 00000000 c3b25fa8
5fa0: c0030f20 c0070284 4001d838 00000000 000b5038 000067b1 000a18b7 00000000
5fc0: 4001d838 00000000 befafe88 00000080 befafe84 befafe88 00000001 00000000
5fe0: 00000069 befafb54 0001778c 401d3c24 60000010 000b5038 00000000 00000000
Backtrace:
[<bf003000>] (mini2440_hello_module_init+0x0/0x40 [test]) from [<c003032c>] (do_one_initcall+0x3c/0x1c8)
[<c00302f0>] (do_one_initcall+0x0/0x1c8) from [<c0070344>] (sys_init_module+0xd0/0x204)
[<c0070274>] (sys_init_module+0x0/0x204) from [<c0030f20>] (ret_fast_syscall+0x0/0x28)
r7:00000080 r6:befafe88 r5:00000000 r4:4001d838
Code: e59f0018 eb4e4a80 e3a00000 e3a03003 (e5c03000)
---[ end trace d35f442a6f62fcd9 ]---
Segmentation fault
line 12,
PC is at mini2440_hello_module_init+0x2c/0x40 [test]
[root@localhost test]# arm-linux-objdump -S test.ko
test.ko: file format elf32-littlearm
Disassembly of section .text:
00000000 <test_func>:
#include <linux/kernel.h>
#include <linux/module.h>
void test_func()
{
0: e1a0c00d mov ip, sp
4: e92dd800 push {fp, ip, lr, pc}
8: e24cb004 sub fp, ip, #4 ; 0x4
char *p=NULL;
*p=3;
c: e3a02003 mov r2, #3 ; 0x3
10: e3a03000 mov r3, #0 ; 0x0
14: e5c32000 strb r2, [r3]
}
18: e89da800 ldm sp, {fp, sp, pc}
Disassembly of section .exit.text:
00000000 <cleanup_module>:
test_func();
return 0;
}
static void __exit mini2440_hello_module_cleanup(void)
{
0: e1a0c00d mov ip, sp
4: e92dd800 push {fp, ip, lr, pc}
8: e24cb004 sub fp, ip, #4 ; 0x4
printk("<4>Good-bye, Mini2440 module was removed!\n");
c: e59f0004 ldr r0, [pc, #4] ; 18 <cleanup_module+0x18>
10: ebfffffe bl 0 <printk>
}
14: e89da800 ldm sp, {fp, sp, pc}
18: 00000000 .word 0x00000000
Disassembly of section .init.text:
00000000 <init_module>:
{
char *p=NULL;
*p=3;
}
static int __init mini2440_hello_module_init(void)
{
0: e1a0c00d mov ip, sp
4: e92dd800 push {fp, ip, lr, pc}
8: e24cb004 sub fp, ip, #4 ; 0x4
printk(KERN_INFO"Hello, KERN_INFO !\n");
c: e59f0020 ldr r0, [pc, #32] ; 34 <init_module+0x34>
10: ebfffffe bl 0 <printk>
printk(KERN_ALERT"Hello, KERN_ALERT !\n");
14: e59f001c ldr r0, [pc, #28] ; 38 <init_module+0x38>
18: ebfffffe bl 0 <printk>
printk("<1>Hello, KERN_ALERT 1 !\n");
1c: e59f0018 ldr r0, [pc, #24] ; 3c <init_module+0x3c>
20: ebfffffe bl 0 <printk>
#include <linux/kernel.h>
#include <linux/module.h>
void test_func()
{
char *p=NULL;
*p=3;
24: e3a00000 mov r0, #0 ; 0x0
28: e3a03003 mov r3, #3 ; 0x3
2c: e5c03000 strb r3, [r0]
printk(KERN_INFO"Hello, KERN_INFO !\n");
printk(KERN_ALERT"Hello, KERN_ALERT !\n");
printk("<1>Hello, KERN_ALERT 1 !\n");
test_func();
return 0;
}
30: e89da800 ldm sp, {fp, sp, pc}
34: 0000002c .word 0x0000002c
38: 00000044 .word 0x00000044
3c: 0000005c .word 0x0000005c
[root@localhost test]#
line 65 ----line 69
char *p=NULL;
*p=3;
24: e3a00000 mov r0, #0 ; 0x0
28: e3a03003 mov r3, #3 ; 0x3
2c: e5c03000 strb r3, [r0]