linux编译驱动模块必要条件,Linux驱动开发hello world

【1】有必要查询下Linux内核

# uname -r

2.6.28-11-generic

# ls /usr/src/

linux-headers-2.6.28-11 linux-headers-2.6.28-11-generic

由此可见内核版本和内核头文件版本是一致的,都是2.6.28-11。(如果不一致的话在insmod一步必定出错:

Error inserting './hello.ko': -1 Invalid module format

网上有纠正这个错误的方法,但是感觉是在投机——躲避内核的版本检查;笔者在安装Ubuntu 8.04的时候出现过header头文件和内核版本不匹配的问题,后来通过重装Ubuntu为9.04解决之)。

【2】编写hello.c

新建自己的工作目录,如:

# mkdir /home/wk/hello

编写hello.c

# cd /home/wk/hello

# gedit hello.c

加入以下内容:

//Begin---hello.c

#include

#include

#include

static int hello_init(void)

{

printk(KERN_INFO "hello world!\n");

return 0;

}

static void hello_exit(void)

{

printk(KERN_ALERT "goodbye! linux\n");

}

module_init(hello_init);

module_exit(hello_exit);

MODULE_LICENSE("Dual BSD/GPL");

//End---hello.c

保存退出(Ctrl+Q)。

【3】编写Makefile

# cd /home/wk/hello

# gedit Makefile

注意大小写,Makefile的“M”,不是“m”。

#Begin---Makefile

obj-m := hello.o

KERNEL_DIR := /lib/modules/$(shell uname -r)/build

PWD := $(shell pwd)

all:

make -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules

clean:

rm *.o *.ko

#End---Makefile

(注意:

all、 clean的下一行都是一个TAP

)

KERNELDIR=/lib/modules/2.6.28-11-generic/build  定义了内核路径,$(shell uname -r)就是内核的版本

PWD:=$(shell pwd)  也就是当前路径,也就是你自己写的hello.c模块和Makefile的路径。

保存退出。

make语法:

make  -C  内核路径  M=模块路径 modules  :执行内核模块的编译

疑问?为什么需要一个内核模块路径和自定义的模块路径?!

【4】开始make

# make

输出类似以下消息:

make -C /lib/modules/2.6.32-21-generic/build SUBDIRS=/home/wang modules

make[1]: Entering directory `/usr/src/linux-headers-2.6.32-21-generic'

Building modules, stage 2.

MODPOST 1 modules

make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-21-generic'

从上面可以看出,编译器首先进入了内核源代码所在的目录,进入该目录的目的为了生成hello.o文件。编译器的第二个阶段是运行MODPOST程序,生成hello.mod.c文件,最后连接hello.c和hello.mod.c文件生成最终的模块文件hello.ko

则编译成功,用

# ls

查看应该可以看到生成一堆文件,如:

hello.c   hello.mod.c install    Makefile1       Module.symvers

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

hello.ko hello.o      Makefile~ modules.order

【5】安装模块:

# insmod hello.ko

或者

# insmod ./hello.ko

一个意思。

这一步完了没反应。接着再试一次:

# insmod hello.ko

提示错误:

insmod: error inserting 'hello.ko': -1 File exists

说明hello模块是安装成功的。

# lsmod

也能查看到hello模块。

防止打印太多,用:

# lsmod | head -5

只显示前5行等。如:

Module                  Size Used by

hello                   9344 0

binfmt_misc            16776 1

bridge                 56340 0

stp                    10500 1 bridge

看到hello模块了吧~~~

可是为什么不打印呢?先不说这个。。。

【6】卸载模块

# rmmod hello.ko

再用

# lsmod | head -5

发现hello模块已经不存在了。

【7】关于printk

现在谈谈这个问题:insmod的时候,printk问什么没有在终端中显示Hello World呢?因为hello.c中明明是这么写的:

static int hello_init(void)

{

printk(KERN_ALERT "Hello World!\n");

return 0;

}

答案只有一句:printk不显示在超级终端上!靠!为什么?我也不知道。。。

printk打印输出的优先级

#define  KERN_EMERG     "<0>"/*紧急事件消息,系统崩溃之前提示,表示系统不可用*/

#define  KERN_ALERT      "<1>"/*报告消息,表示必须立即采取措施*/

#define  KERN_CRIT        "<2>"/*临界条件,通常涉及严重的硬件或软件操作失败*/

#define  KERN_ERR         " <3>"/*错误条件,驱动程序。常用KERN_ERR来报告硬件的错误*/

#define KERN_WARNING   "<4>"/*警告条件,对可能出现问题的情况进行警告*/

#define KERN_NOTICE   "<5>"/*正常但又重要的条件用于提醒常用于与安全相关的消息*/

#define KERN_INFO         "<6>"/*提示信息,如驱动程序启动时,打印硬件信息*/

#define KERN_DEBUG      "<7>"/*调试级别的消息*/

这些事内核打印出的基本消息,但是需要注意的不是那个宏定义都可以在终端上显示、但是可以肯定的一点是,你在调试的时候用到这些宏定义,一定在某些地方有显示,比喻在日志等一些文件下显示。

【8】printk调试信息打印的补救方法

用命令:

# dmesg | tail -8

这个命令格式类似于lsmod(只显示最后8行)。

[   95.586960] Good bye, ubuntu

[   96.483964] Hello World!

[   97.031787] Good bye, ubuntu

[   97.594151] Hello World!

[   98.109896] Good bye, ubuntu

[   98.615569] Hello World!

[   99.098943] Good bye, ubuntu

[ 1893.976170] Hello World!

以上的信息显示:笔者在95.586960做了rmmod hello.ko,在96.483964做了insmod hello.ko……

这里[]中的数字表示执行模块安装/卸载聚开机的时间,如96.483964 S(约开机一个半小时以后)。

【9】那么这些信息都存在哪儿呢?

都说默认是在/val/log/messages目录下;可是有时候你看了,没有;

那么请看看另一个目录:

# cd /etc

# ls

你会找到包含syslog的文件,如:

syslog.conf

那么就是它了。

# cat syslog.conf | head -20

同理,只显示前20行的东东;看到类似以下的东东:

# /etc/syslog.conf Configuration file for syslogd.

#

#    For more information see syslog.conf(5)

#    manpage.

#

# First some standard logfiles. Log by facility.

#

auth,authpriv.*    /var/log/auth.log

*.*;auth,authpriv.none   -/var/log/syslog

#cron.*     /var/log/cron.log

daemon.*    -/var/log/daemon.log

kern.*     -/var/log/kern.log

lpr.*     -/var/log/lpr.log

mail.*     -/var/log/mail.log

user.*     -/var/log/user.log

#

# Logging for the mail system. Split it up so that

...

看到kern.*了吧~看到/var/log/kern.log了吧!我猜消息就在这底下。

于是,

# cat /var/log/kern.log | tail -10 显示: Aug 4 23:40:53 ubuntu kernel: [   38.848634] eth0: no IPv6 routers present Aug 4 23:41:47 ubuntu kernel: [   92.893834] Hello World! Aug 4 23:41:50 ubuntu kernel: [   95.586960] Good bye, ubuntu Aug 4 23:41:51 ubuntu kernel: [   96.483964] Hello World! Aug 4 23:41:51 ubuntu kernel: [   97.031787] Good bye, ubuntu Aug 4 23:41:52 ubuntu kernel: [   97.594151] Hello World! Aug 4 23:41:52 ubuntu kernel: [   98.109896] Good bye, ubuntu Aug 4 23:41:53 ubuntu kernel: [   98.615569] Hello World! Aug 4 23:41:53 ubuntu kernel: [   99.098943] Good bye, ubuntu Aug 5 00:11:48 ubuntu kernel: [ 1893.976170] Hello World! 看到了吧!跟先前的dmesg显示的内容相似而更完整了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值