kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

内核编译模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*filename: test.c*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>

staticintdummy_init(void)
{
    printk("hello,world.\n");
    return0;
}
staticvoiddummy_exit(void)
{
    return;
}

module_init(dummy_init);
module_exit(dummy_exit);

MODULE_LICENSE("GPL")

执行如下命令:

1
2
$ gcc -c -O2 -DMODULE -D__KERNEL__ -I/usr/src/linux test.c
$ insmod test.o

No module found in object
insmod: error inserting ‘test.o’: -1 Invalid module format

正确的做法是写一个Makefile,由内核的Kbuild来帮你编译。

1
2
3
4
5
6
$ cat Makefile
obj-m :=test.o
KDIR :=/lib/modules/$(shell uname -r)/build
PWD :=$(shell pwd)
default:
    $(MAKE)-C $(KDIR)SUBDIRS=$(PWD)modules

执行如下命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$make
make -C /lib/modules/2.6.5-1.358/build SUBDIRS=/test modules
make[1]:Entering directory `/lib/modules/2.6.5-1.358/build'
  CC [M]  /test/modinject/test.o
  Building modules, stage 2.
  MODPOST
  CC      /test/modinject/test.mod.o
  LD [M]  /test/modinject/test.ko
make[1]: Leaving directory `/lib/modules/2.6.5-1.358/build'
$ls -l
-rw-r--r--1 root root   268 Jan  7 08:31 test.c
-rw-r--r--1 root root  2483 Jan  8 09:19 test.ko
-rw-r--r--1 root root   691 Jan  8 09:19 test.mod.c
-rw-r--r--1 root root  1964 Jan  8 09:19 test.mod.o
-rw-r--r--1 root root  1064 Jan  8 09:19 test.o

其实上边的test.o就是用gcc生成的test.o,而test.ko是使用下列命令来生成的。

1
$ld -m elf_i386  -r -o test.ko test.o  test.mod.o

再来看看test.mod.c,它是由/usr/src/linux/scripts/modpost.c来生成的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ cat test.mod.c
#include <linux/module.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>

MODULE_INFO(vermagic,VERMAGIC_STRING);
#undef unix

struct module __this_module
__attribute__((section(".gnu.linkonce.this_module")))={
.name =__stringify(KBUILD_MODNAME),
.init =init_module,
#ifdef CONFIG_MODULE_UNLOAD

.exit=cleanup_module,
#endif

};
static const struct modversion_info ____versions[]
__attribute_used__
__attribute__((section("__versions")))={
	{0,"cleanup_module"},
	{0,"init_module"},
	{0,"struct_module"},
	{0,"printk"},
};
static const char __module_depends[]
__attribute_used__
__attribute__((section(".modinfo")))=
"depends=";

可见,test.mod.o只是产生了几个ELF的节,分别是modinfo, .gun.linkonce.this_module(用于重定位,引进了rel.gnu.linkonce.this_module), __versions。而test.ko是test.o和test.mod.o合并的结果。

通常我们安装一个新的模块,先是编译出相应的ko文件,然后移动

1
/lib/modules/`uname -r`/

目录或者某个子目录下,locate xxx.ko确定该模块确实在上面提到的目录下面,执行depmod -a,depmod将会检查

1
/lib/modules/`uname -r`/

目录及其子目录中的所有模块文件,并根据相依性生成新的modules.dep文件,这时我们执行modprobe xxx.ko,该模块就会被正常加载了。

查看注册的kprobe列表

1
2
3
4
5
6
sudo mount -t debugfs none mount_dir/

#cat mount_dir/kprobes/list
c015d71a  k  vfs_read+0x0
c011a316  j  do_fork+0x0
c03dedc5  r  tcp_v4_rcv+0x0

第一列表示探测点插入的内核地址,第二列表示内核探测的类型,k表示kprobe,r表示kretprobe,j表示jprobe,第三列指定探测点的"符号+偏移"。如果被探测的函数属于一个模块,模块名也被指定。

打开和关闭kprobe的方法列出如下:

1
2
#echo ‘1’ mount_dir/kprobes/enabled
#echo ‘0’ mount_dir/kprobes/enabled

Makefile预定义变量、自动变量

Makefile中常见自动变量

1
2
3
4
5
6
7
8
命令格式     含     义
$*        不包含扩展名的目标文件名称
$+        所有的依赖文件,以空格分开,并以出现的先后为序,可能包含重复的依赖文件
$<     第一个依赖文件的名称
$?        所有时间戳比目标文件晚的依赖文件,并以空格分开 
$@        目标文件的完整名称
$^        所有不重复的依赖文件,以空格分开
$%        如果目标是归档成员,则该变量表示目标的归档成员名称

Makefile中常见预定义变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
命 令 格 式  含     义
AR                库文件维护程序的名称,默认值为ar
AS                汇编程序的名称,默认值为as
CC                C编译器的名称,默认值为cc
CPP               C预编译器的名称,默认值为$(CC) –E
CXX               C++编译器的名称,默认值为g++
FC                FORTRAN编译器的名称,默认值为f77
RM                文件删除程序的名称,默认值为rm –f
ARFLAGS           库文件维护程序的选项,无默认值
ASFLAGS           汇编程序的选项,无默认值
CFLAGS            C编译器的选项,无默认值
CPPFLAGS      C预编译的选项,无默认值
CXXFLAGS      C++编译器的选项,无默认值
FFLAGS            FORTRAN编译器的选项,无默认值
在Makefile中我们可以通过宏定义来控制源程序的编译。

只要在Makefile中的CFLAGS中通过选项-D来指定你于定义的宏即可。
如:
CFLAGS += -D KK

CFLAGS += -D KK=XX