使用printk在Linux终端上打印数据

时间:2016-09-05 12:02:34

标签: c linux linux-device-driver

我目前开始在Linux中学习 Linux设备驱动程序编程。我发现这个小代码使用printk()函数打印hello world。

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
        printk(KERN_ALERT "Hello World!!!\n");
        return 0;
}

static void hello_exit(void)
{
        printk(KERN_ALERT "Goodbye Hello World!!!\n");
}

module_init(hello_init);
module_exit(hello_exit);

使用make命令编译代码并使用insmod命令加载驱动程序。我没有在屏幕上打印&#34; Hello world&#34; ,而只是在日志文件/var/log/kern.log上打印。但是我想在我的ubuntu终端上打印printk。我使用的是ubuntu(14.04)。有可能吗?

2 个答案:

答案 0 :(得分:1)

无法将内核日志和按摩重定向到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 就是其中之一。要使控制台接收所有这些消息,您应该以8为代码运行代码。
gcc setlevel.c -o setlevel
sudo ./setlevel 8

现在您可以将模块插入内核并在控制台中查看内核日志 顺便说一句,这些代码来自ldd3示例。

答案 1 :(得分:0)

printk打印到内核日志。没有&#34;屏幕&#34;就内核而言。如果您想实时查看printk的输出,可以打开终端并输入以下dmesg -w。请注意-w标记仅受dmesg的最新版本支持(由util-linux包提供)。