2017-11-11 146 views
-1

我是新的內核模塊開發,並決定今天編寫我的第一個內核模塊。內核模塊負載導致錯誤

我的模塊應該接收2個數字作爲輸入,由空格分隔並將這些數字加在一起。

內核模塊通過一個名爲calc(/ proc/calc)的proc入口進行通信,因此從文件中讀取將返回輸出並且寫入該文件將給出新輸入(2個數字由空白分隔)

當加載模塊(insmod)時,我的外殼卡住了,在中斷它並查看dmesg時,我看到一個內核BUG行, here is the trace

我不確定這是否是我的代碼中的錯誤,或者它是在Linux內核的實際錯誤,並希望瞭解我做錯了什麼,我該如何開始調試我的模塊與給定dmesg日誌。

here is the source code of my module

我的makefile是很標準的一個,

obj-m += calc.o 
KDIR := /lib/modules/$(shell uname -r)/build 

all: 
    $(MAKE) -C $(KDIR) M=$(PWD) modules 

clean: 
    $(MAKE) -C $(KDIR) M=$(PWD) clean 

許多許多在此先感謝!

編輯:我使用Ubuntu 16.04.02,版本4.10.0-28,非常標準。

+0

根據堆棧溢出的規則,你的驅動器和跟蹤的代碼應該是在問題本身**的**的文字,沒有鏈接。 – Tsyvarev

+0

好的,沒有意識到,謝謝。 生病下一次更好 –

回答

0

您是否創建了設備文件?

[email protected]:~/s_flow/dd# ls -l /dev/my_device 

如果沒有的話在我的系統中使用以下命令

[email protected]:~/dd/char1# mknod /dev/my_device c 300 0 

首先創建設備文件(Ubuntu的14.04),這一切都很好,如下。

[email protected]:~/s_flow/dd# dmesg 
[ 519.751941] calc: module verification failed: signature and/or required key missing - tainting kernel 
[ 519.752368] Calculator initializing 
[ 519.752372] Initializing proc entry at /proc/calc 
[ 519.752380] initialized calc proc entry 
[ 519.752384] mallocing last_message for 128 bytes 
[ 519.752386] malloc finished, resetting calc 
[ 519.752389] calc reset, all good : 

你能給出使用這個模塊的應用程序的詳細信息嗎?

+0

我不太清楚爲什麼是點頭要求,謹慎解釋? p.s它沒有幫助,但不是我沒有得到任何日誌在我的dmesg –

0

問題是聲明last_message =「」;是錯誤的..你正在爲指針last_message分配無效的內存地址。這就是OOPS即將到來的原因。 last_message應該這樣

static void reset_calc(void) { 
    last_message_size = 0; 
    last_message = ""; 
    last_message_type = WAS_LAST_READ; // we expect the first action to  be write 
    } 

改變應改爲

memset(last_message,0,MAX_MESSAGE_SIZE);