- 論壇徽章:
- 0
|
Linux設(shè)備驅(qū)動程序之讀書筆記(二)
——第二章調(diào)試技術(shù)中的printk
在Linux中系統(tǒng)信息的顯示有以下3種情況:
1、如果系統(tǒng)中只運行klogd,那么可以通過klogd -c 重新啟動klogd并設(shè)置console_loglevel,然后小于console_loglevel的所有信息都會打印到控制臺;
2、如果系統(tǒng)中只運行syslogd,那么消息不會傳遞到用戶空間,只能通過讀/proc/kmsg來察看消息;
3、如果系統(tǒng)中同時允許klogd和syslogd,那么在根據(jù)console_loglevel將消息篩選打印到控制臺的前提下,任何消息都會被追加到文件/var/log/messages中。
在Linux下寫驅(qū)動程序的過程中肯定要用到printk語句輸出信息,printk與普通printf語句的差別之一就是,printk根據(jù)附加的不同的日志級別(loglevel),或者說是消息級別,來表示的嚴重程度對消息進行分類。
比如:printk(KERN_DEBUG “Hi, My printk!”);
上述語句所要輸出的信息就是調(diào)試級別的。需要注意的是KERN_DEBUG和后面引號中的內(nèi)容之間不能用逗號隔開,否則編譯時會報錯!
Linux在頭文件<linux/kernel.h>中定義了8種可用的日志級別字符串,根據(jù)嚴重程度排序如下:
KERN_EMERG KERN_ALERT KERN_CRIT KERN_ERR
KERN_WARNING KERN_NOTICE KERN_INFO KERN_DEBUG
未指定日志級別的printk語句采用默認級別,也就是在kernel/printk.c中被指定的宏DEFAULT_MESSAGE_LOGLEVEL,這個宏通常就是KERN_WARNING。
另外,控制臺(一般是指文本終端,也有可能是串口)也有一個表示級別的整數(shù)變量,叫做console_loglevel,它在系統(tǒng)啟動后的初始值是DEFAULT_CONSOLE_LOGLEVEL。
內(nèi)核會將程序中信息的日志級別與console_loglevel進行比較,只有當信息的日志級別小于console_loglevel這個整數(shù)變量的值時,信息才會在當前控制臺上顯示出來。
因此,我們需要事先將系統(tǒng)的console_loglevel調(diào)到一個適當?shù)臄?shù)值,以便控制臺正確顯示我們所需要的信息。比如上面語句中的“Hi, My first printk!”,若不修改console_loglevel的值,就無法在控制終端上顯示。
修改方法有兩種:一是通過sys_syslog系統(tǒng)調(diào)用進行修改;二是用dmesg進行修改或者修改/proc/sys/kernel/printk文件中的第一行,比如dmesg -n 1(命令中的1就是要設(shè)置的console_loglevel值,其實上面所說的KERN_EMERG到KERN_DEBUG也就是從0到7的整數(shù)值)。其中前者是臨時性的,每當系統(tǒng)重啟后console_logleve就會回到默認狀態(tài),而后者卻是永久性的,只要用dmesg修改了console_loglevel的值,那么以后它就一直是這個值了。
我選擇前者,因為不想任意改變系統(tǒng)的狀態(tài),而且Linux Device Drvier3的作者也提供了一個叫做setlevel的小程序,可以通過由這個程序編譯得到的可執(zhí)行文件來設(shè)置console_loglevel,非常方便。
但現(xiàn)在的問題是,Linux Device Driver3提供的setlevel.c在2.6.20內(nèi)核上編譯通不過,在編譯時提示:expected declaration specifiers or ‘…’ before syslog
setlevel.c中的代碼如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
/* #include <unistd.h> */ /* conflicting on the alpha */
#define __LIBRARY__ /* _syscall3 and friends are only available through this */
#include <linux/unistd.h>
/* define the system call, to override the library function */
_syscall3(int, syslog, int, type, char *, bufp, int, len);
int main(int argc, char **argv)
{
int level;
if (argc==2) {
level = atoi(argv[1]); /* the chosen console */
} else {
fprintf(stderr, "%s: need a single arg\n",argv[0]); exit(1);
}
if (syslog(8,NULL,level) < 0) {
fprintf(stderr,"%s: syslog(setlevel): %s\n",
argv[0],strerror(errno));
exit(1);
}
exit(0);
}
問題很顯然出現(xiàn)在_syscall3上。
程序中_syscall3(int, syslog, int, type, char *, bufp, int, len)這條語句的作用是告訴編譯器,需要的系統(tǒng)調(diào)用是syslog,返回值是int,而后面的字符串則是syslog需要的3個參數(shù)及其類型。
而setlevel.c之所以編譯通不過,是因為Linux Device Driver3的作者是在2.6.10的Linux內(nèi)核中寫的這個程序,但是2.6.20的Linux內(nèi)核中卻并沒有定義_syscall3這個函數(shù)。(我在Red Hat 9.0(2.4的內(nèi)核)中編譯過setlevel.c,可以正常通過。)
于是只要在setlevel.c中調(diào)用_syscall3之前定義它的原型,那么setlevel.c就可以正常編譯和使用了。增加代碼后的setlevel.c如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/syscall.h> /*需要加上這個頭文件,否則編譯不能通過*/
#define __syscall_return(type, res) \
do { \
if ((unsigned long)(res) >= (unsigned long)(-125)) { \
errno = -(res); \
res = -1; \
} \
return (type) (res); \
} while (0)
#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
type name(type1 arg1,type2 arg2,type3 arg3) \
{ \
long __res; \
__asm__ volatile ("int $0x80" \
: "=a" (__res) \
: "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
"d" ((long)(arg3))); \
__syscall_return(type,__res); \
}
_syscall3(int,syslog, int,type, char *,bufp, int,len);
int main(int argc, char **argv)
{
int level;
if (argc==2) {
level = atoi(argv[1]); /* the chosen console */
} else {
fprintf(stderr, "%s: need a single arg\n",argv[0]); exit(1);
}
if (syslog(8,NULL,level) < 0) {
fprintf(stderr,"%s: syslog(setlevel): %s\n",
argv[0],strerror(errno));
exit(1);
}
exit(0);
} |
評分
-
查看全部評分
|