linux内核解析xml,Linux内核模块编译、加载&卸载及查看运行结果

Linux内核模块编译、加载&卸载及查看运行结果

[环境]VirtualBox +

Fedora8(Kernel:2.6.23.1-42.fc8-i686)

===================================================

[源代码hello.c]

这里我仅使用了最简单的代码HelloWorld

#include

#include

MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)

{

printk(KERN_EMERG "Hello World enter!\n");

return 0;

}

static void hello_exit(void)

{

printk(KERN_EMERG "Hello world exit!\n");

}

module_init(hello_init);

module_exit(hello_exit);

//MODULE_AUTHOR("stone.shi");

//MODULE_DESCRIPTION("This is an example of programming

driver!");

//MODULE_ALIAS("a simplest module”);

==============================================

[Makefile文件内容]obj-m := hello.o

KERNELDIR ?= /lib/modules/$(shell uname -r)/build

PWD ?= $(shell pwd)

defalult:

make -C ${KERNELDIR} M=${PWD} modules

install:

insmod hello.ko

uninstall:

rmmod hello.ko

clear:

make -C ${KERNELDIR} M=${PWD} clean

****说明:

-C选项指定内核头文件的路径,所以如果这里不正确的话,就得仔细检查你的目录是不是正确。build只是一个符号链接,一般它指定的正是你的源码所在的目录,如果有的话。如果它不能正确链接,那么可能是你的源码并未安装或者安装的版本与当前正在工作的内核不对应。这时你就需要安装正确的与当前内核匹配的源码。

注意,Makefile的命令行要以TABLE开头,依赖条件顶格

==============================================

[编译]

好了,这时已经有两个文件了,hello.c和Makefile,随便找个目录放下,shell到这目录,输入#make:MODPOST

1 modules看到这个的话说明编译通过了,生成正确的内核模块文件hello.ko

===============================================

[加载]

执行#insmod hello.ko

[卸载]

#rmmod hellomod

[查看内核输出]

#tail /var/log/messages

这时就可以看到内核模块在加载和卸载时输出的信息了。

================================================

相关调试信息:

[root@localhost drivertest]# make

make -C /lib/modules/2.6.23.1-42.fc8/build M=/home/stone/drivertest modules

make[1]: Entering directory

`/usr/src/kernels/2.6.23.1-42.fc8-i686'

CC [M] /home/stone/drivertest/hello.o

Building modules, stage 2.

MODPOST 1 modules

WARNING: "struct_module" [/home/stone/drivertest/hello.ko]

undefined!

CC /home/stone/drivertest/hello.mod.o

LD [M] /home/stone/drivertest/hello.ko

make[1]: Leaving directory

`/usr/src/kernels/2.6.23.1-42.fc8-i686'

[root@localhost drivertest]# ls

hello.c hello.ko hello.mod.o helloworld Makefile~ myproject1-0.9.10

hello.c~ hello.mod.c hello.o Makefile Module.symvers

[root@localhost drivertest]# insmod hello.ko

[root@localhost drivertest]#

Message from syslogd@localhost at Feb 17 19:50:45

...

kernel: Hello World enter!

[root@localhost drivertest]# rmmod hello

[root@localhost drivertest]#

Message from syslogd@localhost at Feb 17 19:51:43

...

kernel: Hello world exit!

[root@localhost drivertest]# vi hello.c

[root@localhost drivertest]# tail /var/log/messages

Feb 17 19:33:21 localhost gconfd (root-1949):

地址“xml:readonly:/etc/gconf/gconf.xml.mandatory”解析为位于 0 的只读配置源

Feb 17 19:33:21 localhost gconfd (root-1949):

地址“xml:readwrite:/root/.gconf”解析为位于 1 的可写入配置源

Feb 17 19:33:21 localhost gconfd (root-1949):

地址“xml:readonly:/etc/gconf/gconf.xml.defaults”解析为位于 2 的只读配置源

Feb 17 19:33:26 localhost pulseaudio[1965]: main.c: This program is

not intended to be run as root (unless --system is

specified).

Feb 17 19:33:26 localhost pulseaudio[1965]: pid.c: Daemon already

running.

Feb 17 19:33:26 localhost pulseaudio[1965]: main.c:

pa_pid_file_create() failed.

Feb 17 19:33:33 localhost gconfd (root-1949):

地址“xml:readwrite:/root/.gconf”解析为位于 0 的可写入配置源

Feb 17 19:34:11 localhost yum-updatesd-helper: error getting update

info: Cannot retrieve repository metadata (repomd.xml) for

repository: fedora. Please verify its path and try again

Feb 17 19:50:45 localhost kernel: Hello World enter!

Feb 17 19:51:43 localhost kernel: Hello world exit!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值