翻ldd3書看得雲裡霧裡的,幸好有源碼,可以找到源碼,編譯一下,自己動手學習ldd3到底講了些什麼知識。找到一份源碼後,運行起來再說,不管是白貓還是黑貓,要抓老鼠,你得先得給我跑起來,哈哈!
上一篇給出了ldd3,下載examples.tar.gz解壓有以下子目錄
include Makefile pci scullc scullv simple ttylddbus misc-modules sbull sculld short skull usbLICENSE misc-progs scull scullp shortprint snull
cd 到simple目錄,有以下檔案
Makefile simple.c simple_load simple_unload
Makefile,simple_load,simple_unload書中的講解比較詳細,不懂的杜娘或者google,其實有些我也不懂。
1)CFLAGS錯誤
bory@chong:~/ldd3/test/simple$ makemake -C /lib/modules/2.6.32-37-generic/build M=/home/fang/ldd3/test/simple LDDINCDIR=/home/fang/ldd3/test/simple/../include modulesmake[1]: 正在進入目錄 `/usr/src/linux-headers-2.6.32-37-generic'scripts/Makefile.build:49: *** CFLAGS was changed in "/home/fang/ldd3/test/simple/Makefile". Fix it to use EXTRA_CFLAGS。 停止。make[1]: *** [_module_/home/fang/ldd3/test/simple] 錯誤 2make[1]:正在離開目錄 `/usr/src/linux-headers-2.6.32-37-generic'make: *** [default] 錯誤 2
因源碼是針對linux核心2.6.10以下版本的,bory和您用的版本都高於2.6.10版本。所以在編譯源碼時會出現很多版本相關的錯誤。上面的錯誤的
解決方案:
將Makefile檔案中的“CFLAGS”替換成“EXTRA_CFLAGS”
儲存繼續編譯。
2)linux/config.h: 沒有那個檔案或目錄
bory@chong:~/ldd3/test/simple$ makemake -C /lib/modules/2.6.32-37-generic/build M=/home/fang/ldd3/test/simple LDDINCDIR=/home/fang/ldd3/test/simple/../include modulesmake[1]: 正在進入目錄 `/usr/src/linux-headers-2.6.32-37-generic' CC [M] /home/fang/ldd3/test/simple/simple.o/home/fang/ldd3/test/simple/simple.c:18:26: error: linux/config.h: 沒有那個檔案或目錄/home/fang/ldd3/test/simple/simple.c: In function ‘simple_vma_nopage’:/home/fang/ldd3/test/simple/simple.c:115: error: ‘NOPAGE_SIGBUS’ undeclared (first use in this function)/home/fang/ldd3/test/simple/simple.c:115: error: (Each undeclared identifier is reported only once/home/fang/ldd3/test/simple/simple.c:115: error: for each function it appears in.)/home/fang/ldd3/test/simple/simple.c: At top level:/home/fang/ldd3/test/simple/simple.c:128: error: unknown field ‘nopage’ specified in initializer/home/fang/ldd3/test/simple/simple.c:128: warning: initialization from incompatible pointer typemake[2]: *** [/home/fang/ldd3/test/simple/simple.o] 錯誤 1make[1]: *** [_module_/home/fang/ldd3/test/simple] 錯誤 2make[1]:正在離開目錄 `/usr/src/linux-headers-2.6.32-37-generic'make: *** [default] 錯誤 2
linux/config.h這個檔案不存在。這是因為config.h這個檔案在新版本已經被移除。
解決方案:
將simple.c的標頭檔config.h登出掉
//#include <linux/config.h>
3)‘NOPAGE_SIGBUS’ undeclared (first use in this function) 和 unknown field ‘nopage’ specified in initializer
繼續編譯
bory@chong:~/ldd3/test/simple$ makemake -C /lib/modules/2.6.32-37-generic/build M=/home/fang/ldd3/test/simple LDDINCDIR=/home/fang/ldd3/test/simple/../include modulesmake[1]: 正在進入目錄 `/usr/src/linux-headers-2.6.32-37-generic' CC [M] /home/fang/ldd3/test/simple/simple.o/home/fang/ldd3/test/simple/simple.c: In function ‘simple_vma_nopage’:/home/fang/ldd3/test/simple/simple.c:115: error: ‘NOPAGE_SIGBUS’ undeclared (first use in this function)/home/fang/ldd3/test/simple/simple.c:115: error: (Each undeclared identifier is reported only once/home/fang/ldd3/test/simple/simple.c:115: error: for each function it appears in.)/home/fang/ldd3/test/simple/simple.c: At top level:/home/fang/ldd3/test/simple/simple.c:128: error: unknown field ‘nopage’ specified in initializer/home/fang/ldd3/test/simple/simple.c:128: warning: initialization from incompatible pointer typemake[2]: *** [/home/fang/ldd3/test/simple/simple.o] 錯誤 1make[1]: *** [_module_/home/fang/ldd3/test/simple] 錯誤 2make[1]:正在離開目錄 `/usr/src/linux-headers-2.6.32-37-generic'make: *** [default] 錯誤 2
又是錯誤。看來核心版本不匹配,帶來的麻煩真不少阿。未定義NOPAGE_SIGBUS和nopage。因新版本vm_operations_struct去掉了nopage成員。我們這裡也去掉她。
解決方案:
編輯simple.c,找到return NOPAGE_SIGBUS; 這一行,作如下改動。(因為這個方法我們不會用到,返回NULL沒關係的)
//return NOPAGE_SIGBUS; return NULL;
再找到.nopage = simple_vma_nopage,這一行,作如下改動
//.nopage = simple_vma_nopage,
因為新核心版本在vm_operations_struct去掉了nopage成員
4)裝載驅動
儲存,繼續編譯,這次大功告成。編譯好這個簡單的字元驅動後,如何裝載呢?
simple_load是裝載的shell指令碼,simple_unload是卸載指令碼。我們先改許可權
bory@chong:~/ldd3/test/simple$ chmod 755 simple_load
bory@chong:~/ldd3/test/simple$ chmod 755 simple_unload
繼續
bory@chong:~/ldd3/test/simple$ sudo ./simple_load
裝載後,如何查看是否裝載成功呢?
bory@chong:~/ldd3/test/simple$ lsmod
Module Size Used by
simple 3868 0
binfmt_misc 7960 1
ppdev 6375 0
joydev 11104 0
。。。
列表中有我們剛裝載的字元驅動simple,說明我們的操作是成功的。如果我們在驅動程式列印log了,如何查看這些log呢?
找到檔案/var/log/syslog,就可以查看到您列印的log了。
如有錯誤之處,請您指出,謝謝您的不吝賜教!