Linux驅動模組初始教程:一步一步,從helloworld到insmod->printk!!!

來源:互聯網
上載者:User

【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</usr/src/linux-headers-2.6.28-11/include/linux/init.h>
#include</usr/src/linux-headers-2.6.28-11/include/linux/module.h>

MODULE_LICENSE("GPL");
//printk(KERN_ALERT "Begin\n");
static int hello_init(void)
{
printk(KERN_ALERT "Hello World!\n");
return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT "Good bye, ubuntu\n");
// return 0;
}

module_init(hello_init);
module_exit(hello_exit);
//End---hello.c
注意第一行
#include</usr/src/linux-headers-2.6.28-11/include/linux/init.h>
位置要正確;或者你只要寫成
#include<linux/init.h>
儲存退出(Ctrl+Q)。

【3】編寫Makefile
# cd /home/wk/hello
# gedit Makefile
注意大小寫。
#Begin---Makefile
KERNELDIR=/lib/modules/2.6.28-11-generic/build
PWD:=$(shell pwd)
INSTALLDIR=/home/wk/hello/install
obj-m:= hello.o
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
cp hello.ko $(INSTALLDIR)
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
#End---Makefile
網上有Makefile的詳解,就不冗述了。
儲存退出。

【4】開始make
# make
輸出類似以下訊息:
make -C /lib/modules/2.6.28-11-generic/build M=/home/wk/hello modules
make[1]: 正在進入目錄 `/usr/src/linux-headers-2.6.28-11-generic'
Building modules, stage 2.
MODPOST 1 modules
make[1]:正在離開目錄 `/usr/src/linux-headers-2.6.28-11-generic'
則編譯成功,用
# 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不顯示在超級終端上!靠!為什嗎?我也不知道。。。

【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顯示的內容相似而更完整了!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.