將內核日誌和按摩重定向到gnome-terminal是不可能的,並且您必須使用dmesg
。 但是在一個虛擬終端(打開一個與ctrl+F1-F6
),你可以將它們重定向到標準輸出。
首先通過在虛擬終端中輸入tty
命令來確定tty編號。輸出可能是/dev/tty(1-6)。
使用您指定的參數編譯並運行此代碼。
/*
* setconsole.c -- choose a console to receive kernel messages
*
* Copyright (C) 1998,2000,2001 Alessandro Rubini
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
int main(int argc, char **argv)
{
char bytes[2] = {11,0}; /* 11 is the TIOCLINUX cmd number */
if (argc==2) bytes[1] = atoi(argv[1]); /* the chosen console */
else {
fprintf(stderr, "%s: need a single arg\n",argv[0]); exit(1);
}
if (ioctl(STDIN_FILENO, TIOCLINUX, bytes)<0) { /* use stdin */
fprintf(stderr,"%s: ioctl(stdin, TIOCLINUX): %s\n",
argv[0], strerror(errno));
exit(1);
}
exit(0);
}
例如,如果你的tty
命令的輸出爲的/ dev/tty1上然後鍵入這兩個命令:
gcc setconsole.c -o setconsole
sudo ./setconsole 1
這將設置您的TTY接收內核消息。
然後編譯並運行這段代碼。
/*
* setlevel.c -- choose a console_loglevel for the kernel
*
* Copyright (C) 1998,2000,2001 Alessandro Rubini
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/klog.h>
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 (klogctl(8,NULL,level) < 0) {
fprintf(stderr,"%s: syslog(setlevel): %s\n",
argv[0],strerror(errno));
exit(1);
}
exit(0);
}
有8級內核信息爲你設置的代碼KERN_ALERT是them.To之一使控制檯接收所有的人,你應該用8上面運行代碼arguement。
gcc setlevel.c -o setlevel
sudo ./setlevel 8
現在,您可以將您的模塊到內核,看看在控制檯內核日誌。
順便說一下這些代碼來自ldd3的例子。
有一些技巧是可能的,但你可能不需要它。 – 0andriy
輸出將顯示在*系統控制檯*上。對於嵌入式系統和SBC,控制檯通常是特定的串行端口。內核參數'console = ...'用於指定具有可選屬性的設備。 Ubuntu發行版通常不會在命令行中定義控制檯。 – sawdust
https://wiki.ubuntu.com/Kernel/KernelDebuggingTricks – sawdust