2009년 10월 9일 금요일

Linux device 및 module probe될때 debugging

Kernel에 새로운 device나 module이 probe가 일어날때, debugging하기 위한 tool로 Kprobes 가있으며, 이는 debugging을 위해 재빌드 할 필요없이 debug processs를 수행할 수 있다.

Kprobes는 running kernel에서 break points를 insert하고 debug information을 얻는 방법을 제공한다.

kprobes는 www.kernel.org에서 제공하는2.6.9 이후으 kernel소스에는 모든 포함되어 있기 때문에, 2.6.9이상의 kernel을 사용한다면, 별도의 patch는 필요없다. kprobes를 활성화 하기 위해서는 make xconfig에서 "Kernel hacking"--> "Kprobes"를 활성화 시키면 된다.

Kprobe 사용방법
kprobe는 별도의 module로 만들어 build하여 insmod를 통해 kernel에 포함시켜 debugging하기 위한 부분을 debugging 할 수 있다.
위 코드는 /usr/src/linux/fs/open.c파일의 sys_open함수로서 kernel에서 file open할때 불리우는 함수 이다. 다음은 sys_open이 불릴때 마다 Kprobe를 통해 debugging이 가능하도록 하는 과정을 설명 하겠다.
1. sys_open의 심볼 정보를 얻는다. 다음의 3가지 방법이 있다.
- use the /proc/kallsyms
- use the nm command on the kernel
- use the kernel's System.map
# grep sys_open System.map
-> c01569c0 T sys_open :와 같은 정보를 얻을 수 있다.

2. sys_open Routine을 위한 Kprobe 모듈을 만든다.
위 모듈을 compil하기 위한 간단한 makefile을 만드는데, 아래와 같은 1 줄을 추가하면 된다.
obj-m += chp12-kprobe_sys_open.o
- 이후 make 수행한다.
make -C /usr/src/linux SUBDIRS=$PWD modules
* 보다 자세한 kernel module을 위한 makefile을 위한 정보는 linux/Documentation/kbuild/makefiles.txt파일을 통해 알 수 있다.
- insmode ./chip12-kprob_sys_open.ko 하게 되면 된다.

이후 cat /var/log/message를 통해 시스템에서 파일 open이 일어날때마다 정보를 알수 있게 된다.

출처 : Debugging and Performance Tuning chapter 12