使用printk調試kernel

來源:互聯網
上載者:User

http://hi.baidu.com/lei005/item/bf95c1161aaa4c0d8fbde41b

printk(KERN_ERR "\n\nfile: %s \t line = %d\t function: %s\n", __FILE__, __LINE__, __func__);

printk("KERNEL:File: %s\t line = %d\t function: %s\n", __FILE__, __LINE__, __func__);

調試kernel增加一些列印資訊,可能通過這個資訊從而看到函數被哪些函數調用了.

unsigned int * s_bp;

__asm__("movl %%ebp,%0":"=g"(s_bp));

printk( "\nKERNEL-DEBUG: file : [%s] \nFunction:[%s] \nLine = [%d] \nIs called Add = [%x]\n", __FILE__, __func__, __LINE__,* (unsigned int *) (s_bp+1));

查函數表:

$ cat /proc/kallsyms

C語言編譯器的預定義符號:
__LINE__           當前(原始碼檔案)行號 [整數]
__FILE__           當前正在編譯的檔案的檔案名稱   [字串]
__DATE__           當前日期,以“月月 日日 年年年年”的形式給出 [字串]
__TIME__           目前時間,以“HH:mm:ss”的格式給出   [字串]
__STDC__           如果編譯器符合ANSI C標準,該宏為1,否則為0
__STDC_HOSTED__    如果實現了所有C標準庫,該宏為1,否則為0
__STDC_VERSION__   被定義為199901L(不同編譯器可能不一樣,比如我用的gcc裡就沒有這個預定義符號)

註:這些預定義符號的首尾為兩個底線,如果是兩個 單詞,中間以一個底線串連。
如果在原始碼中使用了這些符號,它們會在預先處理時被轉換(使用gcc編譯器的 -E 選項可以看到替換後的值)

C 標準裡還在每個函數內預定義了一個標誌符: __func__
它被定義為 static const char __func__[]="function-name";
即不能在程式內對__func__賦值,也不能改變它所指向的字串(函數名),否則報 編譯錯誤

注:__func__是個標誌符,它在預先處理階段不被替換,所以使用gcc -E 是看不到任何效果的。
例:test.c
int main(){
printf("%s %s\n",__FILE__,__func__);
return 0;
}

輸 出: test.c main

在標頭檔<linux/kernel.h>中共定義了八個可用的記錄級;我們下面按其嚴重性倒序列出:
KERN_EMERG
Used for emergency messages, usually those that precede a crash.
用於突發性事件的訊息,通常在系統崩潰之前報告此類訊息。
KERN_ALERT
A situation requiring immediate action.
在需要立即操作的情況下使用此訊息。
KERN_CRIT
Critical conditions, often related to serious hardware or software failures.
用於臨界條件下,通常遇到嚴重的硬軟體錯誤時使用此訊息。
KERN_ERR
Used to report error conditions; device drivers often use KERN_ERR to report hardware difficulties.
用於報告錯誤條件;裝置驅動經常使用KERN_ERR報告硬體難題。
KERN_WARNING
Warnings about problematic situations that do not, in themselves, create serious problems with the system.
是關於問題狀況的警告,一般這些狀況不會引起系統的嚴重問題。
KERN_NOTICE
Situations that are normal, but still worthy of note. A number of security-related conditions are reported at this level.
該層級較為普通,但仍然值得注意。許多與安全性相關的情況會在這個層級被報告。
KERN_INFO
Informational messages. Many drivers print information about the hardware they find at startup time at this level.
資訊訊息。許多驅動程式在啟動時刻用它來輸出獲得的硬體資訊。
KERN_DEBUG
Used for debugging messages.
用於輸出調試資訊
printk(KERN_ERR, "file: %s \t line = %d\t function: %s\n", __FILE__, __LINE__, __func__);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.