gpt4 book ai didi

c - linux终端使用printk打印数据

转载 作者:太空宇宙 更新时间:2023-11-04 10:32:12 25 4
gpt4 key购买 nike

我目前开始在 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)。可能吗?

最佳答案

无法将内核日志和消息重定向到 gnome 终端,您必须在那里使用 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 示例。

关于c - linux终端使用printk打印数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39330184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com