【问题标题】:Using printk to print data on Linux terminalLinux终端使用printk打印数据
【发布时间】:2016-09-05 12:02:34
【问题描述】:

我目前开始在 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 命令加载驱动程序后。我没有在屏幕上打印“Hello world”,而是仅在日志文件/var/log/kern.log 上打印。但我希望 printk 在我的 ubuntu 终端上打印。我正在使用 ubuntu(14.04)。有可能吗?

【问题讨论】:

  • 有一些技巧是可能的,但你可能不需要它。
  • 输出将出现在系统控制台上。对于嵌入式系统和 SBC,控制台通常是一个特定的串行端口。内核参数console=... 用于指定具有可选属性的设备。 Ubuntu 发行版通常不在命令行中定义控制台。

标签: c linux linux-device-driver


【解决方案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 作为参数运行上述代码。
@ 987654328@
sudo ./setlevel 8

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

【讨论】:

  • 当 tty 命令显示 /dev/pts/2 时我们应该向 setconsole 传递什么
【解决方案2】:

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

【讨论】:

  • “就内核而言,没有“屏幕”” -- 嗯,(系统)控制台呢(可以通过参数指定内核命令行)?
  • 您可以使用dmesg -n9 要求内核将内核日志中的所有消息输出到主控制台,也可以使用较小的值仅选择部分消息。
  • "您可以要求内核将内核日志中的所有消息输出到主控制台..." -- 不,您不必"询问” 以获取系统控制台上的消息。请参阅上面有关内核调试的链接和this question regarding the console log-level。也许您只使用过带有 GUI 前端的 Linux?
  • 看起来我“只使用带有 GUI 前端的 Linux”吗?再次阅读OP的问题和我的答案。很明显,OP 期望如果他们运行modprobe/insmod,他们会在终端中看到printk 消息,他们从(“screen”)键入modprobe .我试图解释这不会以 OP 理解的方式起作用。
猜你喜欢
  • 2012-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-15
  • 2017-05-01
  • 2014-08-19
  • 1970-01-01
  • 2015-02-13
相关资源
最近更新 更多