简体   繁体   English

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

[英]Using printk to print data on Linux terminal

I currently started learning the Linux Device driver programming in Linux. 我目前开始学习Linux中的Linux设备驱动程序编程。 where I found this small piece of code printing hello world using printk() function. 在这里,我发现使用printk()函数的这一小段代码打印了世界。

#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);

After compiling code using make command and load driver using insmod command. 使用make命令编译代码并使用insmod命令加载驱动程序之后。 I'm not getting the "Hello world" printed on screen instead its printing only on the log file /var/log/kern.log . 我没有在屏幕打印“ Hello world” ,而是仅在日志文件/var/log/kern.log上打印。 But I want printk to print on my ubuntu terminal. 但是我想在我的ubuntu终端上打印printk I'm using ubuntu(14.04). 我正在使用ubuntu(14.04)。 Is it possible? 可能吗?

It isn't possible to redirect kernel logs and massages to gnome-terminal and there you have to use dmesg . 不可能将内核日志和消息重定向到gnome-terminal,您必须在其中使用dmesg But in a virtual terminal(open one with ctrl+F1-F6 ) you can redirect them to standard output. 但是在虚拟终端(使用ctrl+F1-F6打开一个终端)中,您可以将它们重定向到标准输出。
First determine tty number by entering tty command in virtual terminal.The output may be /dev/tty(1-6) . 首先在虚拟终端中输入tty命令来确定tty号,输出可能是/ dev / tty(1-6)
Compile and run this code with the argument you specified. 使用您指定的参数编译并运行此代码。

/*
* 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);
}

For example if your output for tty command was /dev/tty1 then type this two command: 例如,如果您的tty命令输出为/ dev / tty1,则键入以下两个命令:

gcc setconsole.c -o setconsole
sudo ./setconsole 1

This will set your tty to receive kernel messages. 这会将您的tty设置为接收内核消息。
Then compile and run this code. 然后编译并运行此代码。

/*
 * 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);
}

There are 8 level of kernel messages as you specify in your code KERN_ALERT is one of them.To make console receive all of them you should run above code with 8 as arguement. 您在代码中指定的内核消息共有8级, KERN_ALERT是其中之一。要使控制台接收所有内核消息,您应在代码上面以8为理由运行。
gcc setlevel.c -o setlevel
sudo ./setlevel 8

Now you can insert your module to kernel and see kernel logs in console. 现在,您可以将模块插入内核,并在控制台中查看内核日志。
By the way these codes are from ldd3 examples. 顺便说一下,这些代码来自ldd3示例。

printk prints to the kernel log. printk打印到内核日志。 There is no "screen" as far as the kernel is concerned. 就内核而言,没有“屏幕”。 If you want to see the output of printk in real time, you can open a terminal and type the following dmesg -w . 如果要实时查看printk的输出,可以打开一个终端并键入以下dmesg -w Note that the -w flag is only supported by recent versions of dmesg (which is provided by the util-linux package). 请注意,仅-w标志仅受dmesg的最新版本(由util-linux软件包提供)支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM