目录[-]html
为了实现内核调试,在内核配置上增长了几项:linux
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 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
启用选项例如: shell
slab layer debugging(slab层调试选项) high-memory debugging(高端内存调试选项) I/O mapping debugging(I/O映射调试选项) spin-lock debugging(自旋锁调试选项) stack-overflow checking(栈溢出检查选项) sleep-inside-spinlock checking(自旋锁内睡眠选项)
2 调试原子操做编程
从内核2.5开发,为了检查各种由原子操做引起的问题,内核提供了极佳的工具。CONFIG_PREEMPT = y CONFIG_DEBUG_KERNEL = y CONFIG_KLLSYMS = y CONFIG_SPINLOCK_SLEEP = y
一些内核调用能够用来方便标记bug,提供断言并输出信息。最经常使用的两个是BUG()和BUG_ON()。ubuntu
定义在<include/asm-generic>中:windows
#ifndef HAVE_ARCH_BUG #define BUG() do { printk("BUG: failure at %s:%d/%s()! ", __FILE__, __LINE__, __FUNCTION__); panic("BUG!"); /* 引起更严重的错误,不但打印错误消息,并且整个系统业会挂起 */ } while (0) #endif #ifndef HAVE_ARCH_BUG_ON #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while(0) #endif
#ifndef __WARN_TAINT #ifndef __ASSEMBLY__ extern void warn_slowpath_fmt(const char *file, const int line, const char *fmt, ...) __attribute__((format(printf, 3, 4))); extern void warn_slowpath_fmt_taint(const char *file, const int line, unsigned taint, const char *fmt, ...) __attribute__((format(printf, 4, 5))); extern void warn_slowpath_null(const char *file, const int line); #define WANT_WARN_ON_SLOWPATH #endif #define __WARN() warn_slowpath_null(__FILE__, __LINE__) #define __WARN_printf(arg...) warn_slowpath_fmt(__FILE__, __LINE__, arg) #define __WARN_printf_taint(taint, arg...) \ warn_slowpath_fmt_taint(__FILE__, __LINE__, taint, arg) #else #define __WARN() __WARN_TAINT(TAINT_WARN) #define __WARN_printf(arg...) do { printk(arg); __WARN(); } while (0) #define __WARN_printf_taint(taint, arg...) \ do { printk(arg); __WARN_TAINT(taint); } while (0) #endif #ifndef WARN_ON #define WARN_ON(condition) ({ \ int __ret_warn_on = !!(condition); \ if (unlikely(__ret_warn_on)) \ __WARN(); \ unlikely(__ret_warn_on); \ }) #endif #ifndef WARN #define WARN(condition, format...) ({ \ int __ret_warn_on = !!(condition); \ if (unlikely(__ret_warn_on)) \ __WARN_printf(format); \ unlikely(__ret_warn_on); \ }) #endif
if (!debug_check) { printk(KERN_DEBUG “provide some information…/n”); dump_stack(); }
#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 */ #define KERN_DEFAULT "<d>" /* Use the default kernel loglevel */
mtj@ubuntu :~$ cat /proc/sys/kernel/printk 4 4 1 7 mtj@ubuntu :~$ cat /proc/sys/kernel/printk_delay 0 mtj@ubuntu :~$ cat /proc/sys/kernel/printk_ratelimit 5 mtj@ubuntu :~$ cat /proc/sys/kernel/printk_ratelimit_burst 10
#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
CONFIG_LOG_BUF_SHIFT=18
※ 这个纪录缓冲区之因此称为环形,是由于它的读写都是按照环形队列的方式进行操做的。api
在标准的Linux系统上,用户空间的守护进程klogd从纪录缓冲区中获取内核消息,再经过syslogd守护进程把这些消息保存在系统日志文件中。klogd进程既能够从/proc/kmsg文件中,也能够经过syslog()系统调用读取这些消息。默认状况下,它选择读取/proc方式实现。klogd守护进程在消息缓冲区有新的消息以前,一直处于阻塞状态。一旦有新的内核消息,klogd被唤醒,读出内核消息并进行处理。默认状况下,处理例程就是把内核消息传给syslogd守护进程。syslogd守护进程通常把接收到的消息写入/var/log/messages文件中。不过,仍是能够经过/etc/syslog.conf文件来进行配置,能够选择其余的输出文件。数组
-源文件名缓存
-函数名sass
-行号(包括指定范围的行号)
-模块名
-格式化字符串
将要打印信息的格式写入<debugfs>/dynamic_debug/control中。nullarbor:~ # echo 'file svcsock.c line 1603 +p' > <debugfs>/dynamic_debug/control
#include <stdlib.h> #include <stdio.h> #include "memwatch.h" int main(void) { char *ptr1; char *ptr2; ptr1 = malloc(512); ptr2 = malloc(512); ptr2 = ptr1; free(ptr2); free(ptr1); }
gcc -DMEMWATCH -DMW_STDIO test1.c memwatchc -o test1
清单 2. test1 memwatch.log 文件
MEMWATCH 2.67 Copyright (C) 1992-1999 Johan Lindh ... double-free: <4> test1.c(15), 0x80517b4 was freed from test1.c(14) ... unfreed: <2> test1.c(11), 512 bytes at 0x80519e4 {FE FE FE FE FE FE FE FE FE FE FE FE ..............} Memory usage statistics (global): N)umber of allocations made: 2 L)argest memory usage : 1024 T)otal of all alloc() calls: 1024 U)nfreed bytes totals : 512
gcc -g test1.c -o test1
YAMD version 0.32 Executable: /usr/src/test/yamd-0.32/test1 ... INFO: Normal allocation of this block Address 0x40025e00, size 512 ... INFO: Normal allocation of this block Address 0x40028e00, size 512 ... INFO: Normal deallocation of this block Address 0x40025e00, size 512 ... ERROR: Multiple freeing At free of pointer already freed Address 0x40025e00, size 512 ... WARNING: Memory leak Address 0x40028e00, size 512 WARNING: Total memory leaks: 1 unfreed allocations totaling 512 bytes *** Finished at Tue ... 10:07:15 2002 Allocated a grand total of 1024 bytes 2 allocations Average of 512 bytes per allocation Max bytes allocated at one time: 1024 24 K alloced internally / 12 K mapped now / 8 K max Virtual program size is 1416 K End.
#include <stdlib.h> #include <stdio.h> int main(void) { char *ptr1; char *ptr2; char *chptr; int i = 1; ptr1 = malloc(512); ptr2 = malloc(512); chptr = (char *)malloc(512); for (i; i <= 512; i++) { chptr[i] = 'S'; } ptr2 = ptr1; free(ptr2); free(ptr1); free(chptr); }
./run-yamd /usr/src/test/test2/test2
Running /usr/src/test/test2/test2 Temp output to /tmp/yamd-out.1243 ********* ./run-yamd: line 101: 1248 Segmentation fault (core dumped) YAMD version 0.32 Starting run: /usr/src/test/test2/test2 Executable: /usr/src/test/test2/test2 Virtual program size is 1380 K ... INFO: Normal allocation of this block Address 0x40025e00, size 512 ... INFO: Normal allocation of this block Address 0x40028e00, size 512 ... INFO: Normal allocation of this block Address 0x4002be00, size 512 ERROR: Crash ... Tried to write address 0x4002c000 Seems to be part of this block: Address 0x4002be00, size 512 ... Address in question is at offset 512 (out of bounds) Will dump core after checking heap. Done.
execve("/sbin/mkfs.jfs", ["mkfs.jfs", "-f", "/dev/test1"], & ... open("/dev/test1", O_RDWR|O_LARGEFILE) = 4 stat64("/dev/test1", {st_mode=&, st_rdev=makedev(63, 255), ...}) = 0 ioctl(4, 0x40041271, 0xbfffe128) = -1 EINVAL (Invalid argument) write(2, "mkfs.jfs: warning - cannot setb" ..., 98mkfs.jfs: warning - cannot set blocksize on block device /dev/test1: Invalid argument ) = 98 stat64("/dev/test1", {st_mode=&, st_rdev=makedev(63, 255), ...}) = 0 open("/dev/test1", O_RDONLY|O_LARGEFILE) = 5 ioctl(5, 0x80041272, 0xbfffe124) = -1 EINVAL (Invalid argument) write(2, "mkfs.jfs: can\'t determine device"..., ..._exit(1) = ?
※ 做为内核的开发者,一定将会常常处理OOPS。
※ OOPS中包含的重要信息,对全部体系结构的机器都是彻底相同的:寄存器上下文和回溯线索(回溯线索显示了致使错误发生的函数调用链)。
※ 如:回溯线索中的地址,会经过ksymoops转化成名称可见的函数名。
ksymoops须要几项内容:Oops 消息输出、来自正在运行的内核的 System.map 文件,还有 /proc/ksyms、vmlinux和/proc/modules。ksymoops 2.4.0 on i686 2.4.17. Options used ... 15:59:37 sfb1 kernel: Unable to handle kernel NULL pointer dereference at virtual address 0000000 ... 15:59:37 sfb1 kernel: c01588fc ... 15:59:37 sfb1 kernel: *pde = 0000000 ... 15:59:37 sfb1 kernel: Oops: 0000 ... 15:59:37 sfb1 kernel: CPU: 0 ... 15:59:37 sfb1 kernel: EIP: 0010:[jfs_mount+60/704] ... 15:59:37 sfb1 kernel: Call Trace: [jfs_read_super+287/688] [get_sb_bdev+563/736] [do_kern_mount+189/336] [do_add_mount+35/208] [do_page_fault+0/1264] ... 15:59:37 sfb1 kernel: Call Trace: [<c0155d4f>]... ... 15:59:37 sfb1 kernel: [<c0106e04 ... ... 15:59:37 sfb1 kernel: Code: 8b 2d 00 00 00 00 55 ... >>EIP; c01588fc <jfs_mount+3c/2c0> <===== ... Trace; c0106cf3 <system_call+33/40> Code; c01588fc <jfs_mount+3c/2c0> 00000000 <_EIP>: Code; c01588fc <jfs_mount+3c/2c0> <===== 0: 8b 2d 00 00 00 00 mov 0x0,%ebp <===== Code; c0158902 <jfs_mount+42/2c0> 6: 55 push %ebp
109 printk("%d\n",*ptr); objdump jfs_mount.o jfs_mount.o: file format elf32-i386 Disassembly of section .text: 00000000 <jfs_mount>: 0:55 push %ebp ... 2c: e8 cf 03 00 00 call 400 <chkSuper> 31: 89 c3 mov %eax,%ebx 33: 58 pop %eax 34: 85 db test %ebx,%ebx 36: 0f 85 55 02 00 00 jne 291 <jfs_mount+0x291> 3c: 8b 2d 00 00 00 00 mov 0x0,%ebp << problem line above 42: 55 push %ebp
#cat /proc/kallsyms c0100240 T _stext c0100240 t run_init_process c0100240 T stext c0100269 t init …
3.1 Kdump 的基本概念
3.1.1 什么是 kexec ?
Kexec 是实现 kdump 机制的关键,它包括 2 个组成部分:一是内核空间的系统调用 kexec_load,负责在生产内核(production kernel 或 first kernel)启动时将捕获内核(capture kernel 或 sencond kernel)加载到指定地址。二是用户空间的工具 kexec-tools,他将捕获内核的地址传递给生产内核,从而在系统崩溃的时候可以找到捕获内核的地址并运行。没有 kexec 就没有 kdump。先有 kexec 实现了在一个内核中能够启动另外一个内核,才让 kdump 有了用武之地。kexec 原来的目的是为了节省 kernel 开发人员重启系统的时间,谁能想到这个“偷懒”的技术却孕育了最成功的内存转存机制呢?
3.1.2 什么是 kdump ?
Kdump 的概念出如今 2005 左右,是迄今为止最可靠的内核转存机制,已经被主要的 linux™ 厂商选用。kdump是一种先进的基于 kexec 的内核崩溃转储机制。当系统崩溃时,kdump 使用 kexec 启动到第二个内核。第二个内核一般叫作捕获内核,以很小内存启动以捕获转储镜像。第一个内核保留了内存的一部分给第二内核启动用。因为 kdump 利用 kexec 启动捕获内核,绕过了 BIOS,因此第一个内核的内存得以保留。这是内核崩溃转储的本质。
kdump 须要两个不一样目的的内核,生产内核和捕获内核。生产内核是捕获内核服务的对像。捕获内核会在生产内核崩溃时启动起来,与相应的 ramdisk 一块儿组建一个微环境,用以对生产内核下的内存进行收集和转存。
3.1.3 如何使用 kdump
构建系统和 dump-capture 内核,此操做有 2 种方式可选:
1)构建一个单独的自定义转储捕获内核以捕获内核转储;
2) 或者将系统内核自己做为转储捕获内核,这就不须要构建一个单独的转储捕获内核。
方法(2)只能用于可支持可重定位内核的体系结构上;目前 i386,x86_64,ppc64 和 ia64 体系结构支持可重定位内核。构建一个可重定位内核使得不须要构建第二个内核就能够捕获转储。可是可能有时想构建一个自定义转储捕获内核以知足特定要求。
3.1.4 如何访问捕获内存
在内核崩溃以前全部关于核心映像的必要信息都用 ELF 格式编码并存储在保留的内存区域中。ELF 头所在的物理地址被做为命令行参数(fcorehdr=)传递给新启动的转储内核。
在 i386 体系结构上,启动的时候须要使用物理内存开始的 640K,而无论操做系统内核转载在何处。所以,这个640K 的区域在从新启动第二个内核的时候由 kexec 备份。
在第二个内核中,“前一个系统的内存”能够经过两种方式访问:
1) 经过 /dev/oldmem 这个设备接口。
一个“捕捉”设备可使用“raw”(裸的)方式 “读”这个设备文件并写出到文件。这是关于内存的 “裸”的数据转储,同时这些分析 / 捕捉工具应该足够“智能”从而能够知道从哪里能够获得正确的信息。ELF 文件头(经过命令行参数传递过来的 elfcorehdr)可能会有帮助。
2) 经过 /proc/vmcore。
这个方式是将转储输出为一个 ELF 格式的文件,而且可使用一些文件拷贝命令(好比 cp,scp 等)将信息读出来。同时,gdb 能够在获得的转储文件上作一些调试(有限的)。这种方式保证了内存中的页面都以正确的途径被保存 ( 注意内存开始的 640K 被从新映射了 )。
3.1.5 kdump 的优点
1) 高可靠性
崩溃转储数据可从一个新启动内核的上下文中获取,而不是从已经崩溃内核的上下文。
2) 多版本支持
LKCD(Linux Kernel Crash Dump),netdump,diskdump 已被归入 LDPs(Linux Documen-tation Project) 内核。SUSE 和 RedHat 都对 kdump 有技术支持。
3.2 Kdump 实现流程
图 1. RHEL6.2 执行流程
图 2. sles11 执行流程
3.3 配置 kdump
3.3.1 安装软件包和实用程序
Kdump 用到的各类工具都在 kexec-tools 中。kernel-debuginfo 则是用来分析 vmcore 文件。从 rhel5 开始,kexec-tools 已被默认安装在发行版。而 novell 也在 sles10 发行版中把 kdump 集成进来。因此若是使用的是rhel5 和 sles10 以后的发行版,那就省去了安装 kexec-tools 的步骤。而若是须要调试 kdump 生成的 vmcore文件,则须要手动安装 kernel-debuginfo 包。检查安装包操做:
3.3.2 参数相关设置 uli13lp1:/ # rpm -qa|grep kexec kexec-tools-2.0.0-53.43.10 uli13lp1:/ # rpm -qa 'kernel*debuginfo*' kernel-default-debuginfo-3.0.13-0.27.1 kernel-ppc64-debuginfo-3.0.13-0.27.1
1) 修改内核引导参数,为启动捕获内核预留内存
经过下面的方法来配置 kdump 使用的内存大小。添加启动参数"crashkernel=Y@X",这里,Y 是为 kdump 捕捉内核保留的内存,X 是保留部份内存的开始位置。
在 ia64, 编辑 /etc/elilo.conf,添加"crashkernel=256M"到内核行。
2) kdump 配置文件
kdump 的配置文件是 /etc/kdump.conf(RHEL6.2);/etc/sysconfig/kdump(SLES11 sp2)。每一个文件头部都有选项说明,能够根据使用需求设置相应的选项。
3.3.3 启动 kdump 服务
在设置了预留内存后,须要重启机器,不然 kdump 是不可以使用的。启动 kdump 服务:
Rhel6.2:
# chkconfig kdump on
# service kdump status
Kdump is operational
# service kdump start
SLES11SP2:
# chkconfig boot.kdump on
# service boot.kdump start
3.3.4 测试配置是否有效
能够经过 kexec 加载内核镜像,让系统准备好去捕获一个崩溃时产生的 vmcore。能够经过 sysrq 强制系统崩溃。
# echo c > /proc/sysrq-trigger
这形成内核崩溃,如配置有效,系统将重启进入 kdump 内核,当系统进程进入到启动 kdump 服务的点时,vmcore 将会拷贝到你在 kdump 配置文件中设置的位置。RHEL 的缺省目录是 : /var/crash;SLES 的缺省目录是 : /var/log/dump。而后系统重启进入到正常的内核。一旦回复到正常的内核,就能够在上述的目录下发现 vmcore 文件,即内存转储文件。可使用以前安装的 kernel-debuginfo 中的 crash 工具来进行分析(crash 的更多详细用法将在本系列后面的文章中有介绍)。
# crash /usr/lib/debug/lib/modules/2.6.17-1.2621.el5/vmlinux /var/crash/2006-08-23-15:34/vmcore crash> bt
3.4 载入“转储捕获”内核
须要引导系统内核时,可以使用以下步骤和命令载入“转储捕获”内核:
kexec -p <dump-capture-kernel> \ --initrd=<initrd-for-dump-capture-kernel> --args-linux \ --append="root=<root-dev> init 1 irqpoll"
装载转储捕捉内核的注意事项:
3.5 后记
Kdump 是一个强大的、灵活的内核转储机制,可以在生产内核上下文中执行捕获内核是很是有价值的。本文仅介绍在 RHEL6.2 和 SLES11 中如何配置 kdump。望抛砖引玉,对阅读本文的读者有益。
参考:
1 kallsyms的分析2.1 软硬件准备
如下软硬件配置取自笔者进行试验的系统配置状况:stty ispeed 115200 ospeed 115200 -F /dev/ttyS0
stty ispeed 115200 ospeed 115200 -F /dev/ttyS0
echo hello > /dev/ttyS0
cat /dev/ttyS0
2.2 安装与配置
下面咱们须要应用kgdb补丁到Linux内核,设置内核选项并编译内核。这方面的资料相对较少,笔者这里给出详细的介绍。下面的工做在开发机(developement)上进行,以上面介绍的试验环境为例,某些具体步骤在实际的环境中可能要作适当的改动:I、内核的配置与编译
[root@lisl tmp]# tar -jxvf linux-2.6.7.tar.bz2 [root@lisl tmp]#tar -jxvf linux-2.6.7-kgdb-2.2.tar.tar [root@lisl tmp]#cd inux-2.6.7
[root@lisl tmp]#patch -p1 <../linux-2.6.7-kgdb-2.2/core-lite.patch
[root@lisl tmp]#make menuconfig
[*] KGDB: kernel debugging with remote gdb Method for KGDB communication (KGDB: On generic serial port (8250)) ---> [*] KGDB: Thread analysis [*] KGDB: Console messages through gdb [root@lisl tmp]#make
[root@lisl tmp]#scp arch/i386/boot/bzImage root@192.168.6.13:/boot/vmlinuz-2.6.7-kgdb [root@lisl tmp]#scp System.map root@192.168.6.13:/boot/System.map-2.6.7-kgdb
[root@lisl tmp]#mkinitrd /boot/initrd-2.6.7-kgdb 2.6.7 [root@lisl tmp]#scp initrd-2.6.7-kgdb root@192.168.6.13:/boot/ initrd-2.6.7-kgdb
title 2.6.7 kgdb root (hd0,0) kernel /boot/vmlinuz-2.6.7-kgdb ro root=/dev/hda1 kgdbwait kgdb8250=1,115200
image=/boot/vmlinuz-2.6.7-kgdb label=kgdb read-only root=/dev/hda3 append="gdb gdbttyS=1 gdbbaud=115200"
gdb file vmlinux set remotebaud 115200 target remote /dev/ttyS0
[root@lisl boot]#gdbstart -s 115200 -t /dev/ttyS0
[*]KGDB: kernel debugging with remote gdb Method for KGDB communication (KGDB: On ethernet) ---> ( ) KGDB: On generic serial port (8250) (X) KGDB: On ethernet
title 2.6.7 kgdb root (hd0,0) kernel /boot/vmlinuz-2.6.7-kgdb ro root=/dev/hda1 kgdbwait kgdboe=@192.168.5.13/,@192.168. 6.13/
[root@lisl tmp]# insmod -m hello.ko >modaddr
.this 00000060 c88d8000 2**2 .text 00000035 c88d8060 2**2 .rodata 00000069 c88d80a0 2**5 …… .data 00000000 c88d833c 2**2 .bss 00000000 c88d833c 2**2 ……
(gdb) Add-symbol-file hello.o 0xc88d8060 -s .data 0xc88d80a0 -s .rodata 0xc88d80a0 -s .bss 0x c88d833c
…… …… if (mod->init != NULL) ret = mod->init(); …… ……
II、在Linux 2.6.x内核中的内核模块调试方法
Linux 2.6以后的内核中,因为module-init-tools工具的更改,insmod命令再也不支持-m参数,只有采起其余的方法来获取模块加载到内核的地址。经过分析ELF文件格式,咱们知道程序中各段的意义以下:…… int bss_var; static int hello_init(void) { printk(KERN_ALERT "Text location .text(Code Segment):%p\n",hello_init); static int data_var=0; printk(KERN_ALERT "Data Location .data(Data Segment):%p\n",&data_var); printk(KERN_ALERT "BSS Location: .bss(BSS Segment):%p\n",&bss_var); …… } Module_init(hello_init);
3.2 VMware的使用技巧
VMware虚拟机是比较占用资源的,尤为是象上面那样在Windows中使用两台虚拟机。所以,最好为系统配备512M以上的内存,每台虚拟机至少分配128M的内存。这样的硬件要求,对目前主流配置的PC而言并非太高的要求。出于系统性能的考虑,在VMware中尽可能使用字符界面进行调试工做。同时,Linux系统默认状况下开启了sshd服务,建议使用SecureCRT登录到Linux进行操做,这样能够有较好的用户使用界面。
[root@lisl tmp]#chmod +x arm-uclinux-tools-base-gcc3.4.0-20040713.sh [root@lisl tmp]#./arm-uclinux-tools-base-gcc3.4.0-20040713.sh
[root@lisl tmp]# tar -jxvf uClinux-dist-20041215.tar.bz2 [root@lisl uClinux-dist]# tar -jxvf linux-2.6.9.tar.bz2 [root@lisl uClinux-dist]# gzip -dc linux-2.6.9-hsc0.patch.gz | patch -p0
[root@lisl uClinux-dist]# gunzip linux-2.6.9-hsc0.patch.gz [root@lisl uClinux-dist]patch -p0 < linux-2.6.9-hsc0.patch
[root@lisl uClinux-dist]# rm -rf linux-2.6.x/ [root@lisl uClinux-dist]# mv linux-2.6.9 linux-2.6.x
[root@lisl uClinux-dist]# cd linux-2.6.x [root@lisl linux-2.6.x]#make ARCH=armnommu CROSS_COMPILE=arm-uclinux- atmel_deconfig
[root@lisl linux-2.6.x]#make ARCH=armnommu CROSS_COMPILE=arm-uclinux- oldconfig
[root@lisl linux-2.6.x]# make ARCH=armnommu CROSS_COMPILE=arm-uclinux- v=1
CFLAGS += -g
Export PATH=$PATH:/root/bin/arm-linux-tool/bin
#bzip2 -d kdb-v4.2-2.4.20-common-1.bz2 #bzip2 -d kdb-v4.2-2.4.20-i386-1.bz2
#patch -p1 <kdb-v4.2-2.4.20-common-1 #patch -p1 <kdb-v4.2-2.4.20-i386-1
#echo "1" >/proc/sys/kernel/kdb
#echo "0" >/proc/sys/kernel/kdb
[0]kdb> md 0xc000000 15
[0]kdb> mm 0xc000000 0x10
[0]kdb> rd [0]kdb> rm %ebx 0x25
[0]kdb> bp sys_write
[0]kdb> bl
[0]kdb> bc 1
[0]kdb> bt
[0]kdb> btp 575
[0]kdb> id schedule
[0]kdb> ssb 0xc0105355 default_idle+0x25: cli 0xc0105356 default_idle+0x26: mov 0x14(%edx),%eax 0xc0105359 default_idle+0x29: test %eax, %eax 0xc010535b default_idle+0x2b: jne 0xc0105361 default_idle+0x31
[0]kdb> 0xc013db4c 0xc013db4c = 0xc013db4c (sys_read)
[0]kdb> sys_write sys_write = 0xc013dcc8 (sys_write)
[0]kdb> md %ebp 0xc74c9f38 c74c9f60 c0136c40 000001f0 00000000 0xc74c9f48 08053328 c0425238 c04253a8 00000000 0xc74c9f58 000001f0 00000246 c74c9f6c c0136a25 0xc74c9f68 c74c8000 c74c9f74 c0136d6d c74c9fbc 0xc74c9f78 c014fe45 c74c8000 00000000 08053328 [0]kdb> 0xc0136c40 0xc0136c40 = 0xc0136c40 (__alloc_pages +0x44) [0]kdb> 0xc0136a25 0xc0136a25 = 0xc0136a25 (_alloc_pages +0x19) [0]kdb> 0xc0136d6d 0xc0136d6d = 0xc0136d6d (__get_free_pages +0xd)
[0]kdb> defcmd name "usage" "help" [0]kdb> [defcmd] type the commands here [0]kdb> [defcmd] endefcmd
[0]kdb> defcmd hari "" "no arguments needed" [0]kdb> [defcmd] md 0xc000000 1 [0]kdb> [defcmd] rd [0]kdb> [defcmd] md %ebp 1 [0]kdb> [defcmd] endefcmd
[0]kdb> hari [hari]kdb> md 0xc000000 1 0xc000000 00000001 f000e816 f000e2c3 f000e816 [hari]kdb> rd eax = 0x00000000 ebx = 0xc0105330 ecx = 0xc0466000 edx = 0xc0466000 .... ... [hari]kdb> md %ebp 1 0xc0467fbc c0467fd0 c01053d2 00000002 000a0200 [0]kdb>
[0]kdb> bph 0xc0204060 dataw 4
[0]kdb> bph 0xc000000 datar 2
$tar -xvzf kprobes-2.6.8-rc1.tar.gz $cd /usr/src/linux-2.6.8-rc1 $patch -p1 < ../kprobes-2.6.8-rc1-base.patch
$patch -p1 < ../kprobes-2.6.8-rc1-sysrq.patch
/* pre_handler: this is called just before the probed instruction is * executed. */ int handler_pre(struct kprobe *p, struct pt_regs *regs) { printk("pre_handler: p->addr=0x%p, eflags=0x%lx\n",p->addr, regs->eflags); return 0; } /* post_handler: this is called after the probed instruction is executed * (provided no exception is generated). */ void handler_post(struct kprobe *p, struct pt_regs *regs, unsigned long flags) { printk("post_handler: p->addr=0x%p, eflags=0x%lx \n", p->addr, regs->eflags); } /* fault_handler: this is called if an exception is generated for any * instruction within the fault-handler, or when Kprobes * single-steps the probed instruction. */ int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr) { printk("fault_handler:p->addr=0x%p, eflags=0x%lx\n", p->addr, regs->eflags); return 0; }
/* specify pre_handler address */ kp.pre_handler=handler_pre; /* specify post_handler address */ kp.post_handler=handler_post; /* specify fault_handler address */ kp.fault_handler=handler_fault; /* specify the address/offset where you want to insert probe. * You can get the address using one of the methods described above. */ kp.addr = (kprobe_opcode_t *) kallsyms_lookup_name("do_fork"); /* check if the kallsyms_lookup_name() returned the correct value. */ if (kp.add == NULL) { printk("kallsyms_lookup_name could not find address for the specified symbol name\n"); return 1; } /* or specify address directly. * $grep "do_fork" /usr/src/linux/System.map * or * $cat /proc/kallsyms |grep do_fork * or * $nm vmlinuz |grep do_fork */ kp.addr = (kprobe_opcode_t *) 0xc01441d0; /* All set to register with Kprobes */ register_kprobe(&kp);
$tail -5 /var/log/messages Jun 14 18:21:18 llm05 kernel: pre_handler: p->addr=0xc01441d0, eflags=0x202 Jun 14 18:21:18 llm05 kernel: post_handler: p->addr=0xc01441d0, eflags=0x196
$objdump -D /usr/src/linux/kernel/fork.o > fork.dis
000022b0 <do_fork>: 22b0: 55 push %ebp 22b1: 89 e5 mov %esp,%ebp 22b3: 57 push %edi 22b4: 89 c7 mov %eax,%edi 22b6: 56 push %esi 22b7: 89 d6 mov %edx,%esi 22b9: 53 push %ebx 22ba: 83 ec 38 sub $0x38,%esp 22bd: c7 45 d0 00 00 00 00 movl $0x0,0xffffffd0(%ebp) 22c4: 89 cb mov %ecx,%ebx 22c6: 89 44 24 04 mov %eax,0x4(%esp) 22ca: c7 04 24 0a 00 00 00 movl $0xa,(%esp) 22d1: e8 fc ff ff ff call 22d2 <do_fork+0x22> 22d6: b8 00 e0 ff ff mov $0xffffe000,%eax 22db: 21 e0 and %esp,%eax 22dd: 8b 00 mov (%eax),%eax
void handler_post(struct kprobe *p, struct pt_regs *regs, unsigned long flags) { struct task_struct *task; read_lock(&tasklist_lock); for_each_process(task) { printk("pid =%x task-info_ptr=%lx\n", task->pid, task->thread_info); printk("thread-info element status=%lx,flags=%lx,cpu=%lx\n", task->thread_info->status, task->thread_info->flags, task->thread_info->cpu); } read_unlock(&tasklist_lock); }
$tail -10 /var/log/messages Jun 22 18:14:25 llm05 kernel: thread-info element status=0,flags=0, cpu=1 Jun 22 18:14:25 llm05 kernel: pid =5e4 task-info_ptr=f5948000 Jun 22 18:14:25 llm05 kernel: thread-info element status=0,flags=8, cpu=0 Jun 22 18:14:25 llm05 kernel: pid =5e5 task-info_ptr=f5eca000
$echo 1 > /proc/sys/kernel/sysrq
Jun 23 10:24:48 linux-udp4749545uds kernel: SysRq : Show kprobes Jun 23 10:24:48 linux-udp4749545uds kernel: Jun 23 10:24:48 linux-udp4749545uds kernel: [<c011ea60>] do_fork+0x0/0x1de