歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Fedora 14 printk() 無法在終端顯示

Fedora 14 printk() 無法在終端顯示

日期:2017/2/28 16:09:47   编辑:Linux教程

最近遇到了內核模塊printk輸出的信息無法在終端顯示。

解決辦法:

原因1:printk()有一個控制日志級別的字段,如果該字段的日記級別高於console默認的日志級別那麼才會打印出來(數值越小日志級別越高,分為從0-7共計8個日志級別)。有一種簡單的改變當前終端的日志級別的方法,echo 8 > /proc/sys/kernel/printk。理論上這樣printk就能輸出到終端了。但是我的沒有。

cat /proc/sys/kernel/printk

8 4 1 7
四個數字含義

/proc/sys/kernel/printk
The four values in this file are console_loglevel, default_mes-
sage_loglevel, minimum_console_level and default_con-
sole_loglevel. These values influence printk() behavior when
printing or logging error messages. See syslog(2) for more info
on the different loglevels. Messages with a higher priority
than console_loglevel will be printed to the console. Messages
without an explicit priority will be printed with priority
default_message_level. minimum_console_loglevel is the minimum
(highest) value to which console_loglevel can be set.
default_console_loglevel is the default value for con-
sole_loglevel.

原因2:syslogd守護進程的規則有問題,/etc/syslog.conf中定義了一些列規則,其中就包含數內核消息的處理規則,Fedora中的syslogd守護進程叫做rsyslogd,相應它的規則配置文件叫rsyslog.conf,其中有一行”#kern.* /dev/console“它的意思是把所有日志級別的內核log都輸出到/dev/console即我們的終端。我們只需要把該行的'#'去掉,重啟,理論上那麼內核log (printk()輸出也是內核log)就會輸出到終端了。但是我的沒找到大哥所說的文件。

原因3:系統中同時有klogd和syslogd守護進程那麼不管日志級別是什麼都不能輸出到終端。

如果不能在終端上看到printk的輸出,那麼可以通過查看/var/log/messages文件,或運行dmesg命令查看,或查看/proc/kmsg文件獲得信息,或是通過ctrl+alt+f2~f6進入系統文本模式裝載模塊,這樣也可以看到prink()輸出的信息,當然這裡就准確對應原因1中所講的規則。感覺這方法還算靠譜。

附一位大仙說的原因:

printk () 打出來的東西不是打到 stdout 或者 stderr 上,所以你沒法直接看到。

你可以通過 dmesg 來查看。

附 printk() 作用的解釋, 取自 linux kernel primer:
printk()

One of the most basic kernel messaging systems is the printk() function. The kernel uses printk() as opposed to printf() because the standard C library is not linked to the kernel. printk() uses the same interface as printf() does and displays up to 1,024 characters to the console. The printk() function operates by trying to grab the console semaphore, place the output into the console's log buffer, and then call the console driver to flush the buffer. If printk() cannot grab the console semaphore, it places the output into the log buffer and relies on the process that has the console semaphore to flush the buffer. The log-buffer lock is taken before printk() places any data into the log buffer, so concurrent calls to printk() do not trample each other. If the console semaphore is being held, numerous calls to printk() can occur before the log buffer is flushed. So, do not rely on printk() statements to indicate any program timing.
printk() 將輸出打倒了內核提供的 console buffer 裡面,再由這個 console 的驅動將其打印出來。

Copyright © Linux教程網 All Rights Reserved