- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前开始在 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/
据我了解系统控制变量,kernel.printk 中的第一个值是内核消息必须小于的数字才能写入控制台。因此,如果它是 4,那么由此生成的消息将只能通过 dmesg 可见: printk(KERN_IN
我正在尝试从 mm/filemap.c 中打印 __generic_file_write_iter() 中的一些信息. 我修改了函数如下: struct file *file = iocb->ki_f
我有一个关于在 3.10 内核中实现 printk() 的问题。我看到了calls local_irq_save一开始。然后我看到了 calls raw_spin_lock(&logbuf_lock)
我正在为 Linux 内核编写一个系统调用,但到目前为止它并没有像我预期的那样工作(这并不奇怪)。我正在尝试使用 printk 消息在一个函数中对其进行调试,但它们不起作用。然而,单独功能中的功能确实
有没有办法记录/打印内核中的 float 值。它是否取决于运行内核的 FPU? 编译 printk(KERN_DEBUG "error = %f ",floatvalue) 时,出现错误: 对 __a
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我使用 printk 在内核级别输出中断时间戳,这意味着每个中断一个 printk(在 irg_handler_ 方法中)。在使用那个新内核时,我注意到我的设备运行速度变慢了。 所以我的问题是每个 p
我正在尝试实现我自己的新 schedule()。我想调试我的代码。 我可以在 sched.c 中使用 printk 函数吗? 我使用了 printk 但它不起作用。我错过了什么? 最佳答案 你知道 s
在 Linux 应用程序级别,所有 printf 消息都转到 stdout,所有 err 消息都转到 stderr。在哪里 stderr 通常是无缓冲的, 标准输出是行缓冲的 所以在 kernel s
假设我想使用 printk()在 arch/x86/boot/string.c 内在编译内核之前。我必须包含哪个头文件,以便链接器知道在哪里可以找到 printk() ?我试过 #include 和
只是一个关于内核级别 printk() 函数的快速问题,如果我调用它,消息将打印到哪里?(我在 i386 架构上使用 Ubuntu 并下载最新的内核) 到处都找不到, 如果有人也可以为我指出正确的方向
我在内核代码中放了一些调试信息。 已经检查了/var/log/messages、dmesg 并且那里没有这样的转储。 syslogd 正在机器上运行 我还将/proc/sys/kernel/print
当我 printk(KERN_INFO, ...) 时,我得到这样的结果: [116584.000183] ... 方括号之间的数字到底是多少? 最佳答案 启动后的秒数。您可以控制是否使用内核配置中的
我正在做一些内核修改,并试图让 printk 将信息输出回控制台。我用它传递了任何内核日志级别,即使是最高日志级别,也没有在控制台上正确地返回任何响应。 我检查过,printk 的当前日志配置是 4
我在我的内核可加载模块中使用 printk 来记录系统调用的参数。对于每个系统调用,使用几个 printk 来打印变量,它们以“\t”作为分隔符出现。要将内核日志缓冲区中的数据重定向到系统/var/l
从原子上下文调用 printk 是否安全,例如IRQ? 最佳答案 是的,明确允许在原子上下文中使用它;该函数实际上非常强大,因为它也可能在紧急情况下被调用。 printk 只会尝试锁定控制台来打印消息
systemd 的 journald 支持内核风格的日志记录。因此,该服务可以在 stderr 上写入以“”开头的消息,它们将像信息一样被解析,“”- 警告。 但是在开发服务时,它是在 systemd
我试图让我的模块显示一个printk。我是新手,所以我可能会遇到一些编程错误。这是我的模块 C 文件: #include #include #include asmlinkage long sy
我是 Linux 内核开发的新手,当我尝试通过在 timekeep.c 文件中添加一个 printk 函数来修改系统调用函数时,比如 printk("BEGIN!$$$$$\n"); dmesg 实
printk()打印的信息只有在Alt+Ctrl+F1 ~ F7 控制台。这些控制台非常不方便调试,因为它们无法回滚。我正在使用 KDE 桌面环境和控制台终端,如何将 printk() 消息重定向到控
我是一名优秀的程序员,十分优秀!