kernel.printk = 5 4 1 7
时间: 2024-05-18 17:16:17 浏览: 102
这是一个 Linux 内核的配置参数,用于设置内核消息的输出级别。具体来说,这里的数字表示四个级别:
- 5:打印所有的内核信息,包括调试信息、信息、警告和错误。
- 4:打印信息、警告和错误,但不包括调试信息。
- 1:只打印紧急情况下的错误信息。
- 7:打印所有内核信息,包括调试信息、信息、警告和错误,并将它们发送到控制台和系统日志。
通常情况下,我们可以将这个参数设置为默认值,即 7,以便获取尽可能多的内核信息,方便进行故障排查和调试。
相关问题
#include <linux/init.h> /* __init and __exit macroses */ #include <linux/kernel.h> /* KERN_INFO macros */ #include <linux/module.h> /* required for all kernel modules */ #include <linux/moduleparam.h> /* module_param() and MODULE_PARM_DESC() */ #include <linux/fs.h> /* struct file_operations, struct file */ #include <linux/miscdevice.h> /* struct miscdevice and misc_[de]register() */ #include <linux/slab.h> /* kzalloc() function */ #include <linux/uaccess.h> /* copy_{to,from}_user() */ #include <linux/init_task.h> //init_task再次定义 #include "proc_relate.h" MODULE_LICENSE("GPL"); MODULE_AUTHOR("Wu Yimin>"); MODULE_DESCRIPTION("proc_relate kernel modoule"); static int proc_relate_open(struct inode *inode, struct file *file) { struct proc_info *buf; int err = 0; buf=kmalloc(sizeof(struct proc_info)*30,GFP_KERNEL); file->private_data = buf; return err; } static ssize_t proc_relate_read(struct file *file, char __user * out,size_t size, loff_t * off) { struct proc_info *buf = file->private_data; /* 你需要补充的代码 */ } static int proc_relate_close(struct inode *inode, struct file *file) { struct buffer *buf = file->private_data; kfree(buf); return 0; } static struct file_operations proc_relate_fops = { .owner = THIS_MODULE, .open = proc_relate_open, .read = proc_relate_read, .release = proc_relate_close, .llseek = noop_llseek }; static struct miscdevice proc_relate_misc_device = { .minor = MISC_DYNAMIC_MINOR, .name = "proc_relate", .fops = &proc_relate_fops }; static int __init proc_relate_init(void) { misc_register(&proc_relate_misc_device); printk(KERN_INFO "proc_relate device has been registered.\n"); return 0; } static void __exit proc_relate_exit(void) { misc_deregister(&proc_relate_misc_device); printk(KERN_INFO "proc_relate device has been unregistered\n"); } module_init(proc_relate_init); module_exit(proc_relate_exit);补充这段代码需要补充的函数部分,使其能编译为内核模块,安装该内核模块后测试程序,运行结果类似如下:Here is parent process,pid = 7329 this is a child,pid is 7330 this is another child,pid is 7331 this is a child,pid is 7333 In thread,pid=7331 tid=7334 thread id=1254224352 this is a child,pid is 7332 this is a child,pid is 7335 ------------------------------------------------------- pid=2616 tgid=2616 comm=sshd sessionid=4 mm=ffff8000fae19000 activeMM=ffff8000fae19000 parent =1971 real_parent=1971 group_leader2616 ------------------------------------------------------- pid=2670 tgid=2670 comm=sshd sessionid=4 mm=ffff8000fa477500 activeMM=ffff8000fa477500 parent =2616 real_parent=2616 group_leader2670 -------------------------------------------------------
这段代码中缺少的部分是 proc_relate_read() 函数的实现。这个函数需要完成从内核空间读取信息并将其复制到用户空间的功能。
以下是一个可能的实现:
static ssize_t proc_relate_read(struct file *file, char __user * out,
size_t size, loff_t * off)
{
struct proc_info *buf = file->private_data;
struct task_struct *task;
int count = 0;
char *tmp_buf;
if (*off > 0) {
return 0; /* End of file */
}
tmp_buf = kmalloc(size, GFP_KERNEL);
if (!tmp_buf) {
return -ENOMEM;
}
/* Traverse the process tree and copy information to buffer */
for_each_process(task) {
snprintf(tmp_buf + count, size - count,
"this is a child,pid is %d\n", task->pid);
count += strlen(tmp_buf + count);
if (list_empty(&task->children)) {
continue;
}
/* Traverse the children of the current task */
list_for_each_entry(task, &task->children, sibling) {
snprintf(tmp_buf + count, size - count,
"this is a child,pid is %d\n", task->pid);
count += strlen(tmp_buf + count);
}
}
/* Copy buffer to user space */
if (copy_to_user(out, tmp_buf, count)) {
kfree(tmp_buf);
return -EFAULT;
}
*off += count;
kfree(tmp_buf);
return count;
}
这个函数使用了 for_each_process() 宏来遍历进程树,并将每个进程的 PID 写入缓冲区。然后,它使用 copy_to_user() 函数将缓冲区的内容复制到用户空间。
注意,该实现并不完美。例如,它没有处理缓冲区溢出的情况,并且只返回进程的 PID,而不是更有用的信息。但是,它可以作为一个起点,让你了解如何在内核模块中读取和复制信息。
#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/uaccess.h> #include <linux/slab.h> #define DEVICE_NAME "mydevice" #define BUF_SIZE 4096 static char *dev_buf; static int major; static int open(struct inode *inode, struct file *file) { printk(KERN_INFO "mydevice: device opened.\n"); return 0; } static int release(struct inode *inode, struct file *file) { printk(KERN_INFO "mydevice: device closed.\n"); return 0; } static ssize_t read(struct file *file, char __user *buf, size_t count, loff_t *pos) { int bytes_read = 0; if (*pos >= BUF_SIZE) { return 0; } if (count + *pos > BUF_SIZE) { count = BUF_SIZE - *pos; } if (copy_to_user(buf, dev_buf + *pos, count)) { return -EFAULT; } *pos += count; bytes_read = count; printk(KERN_INFO "mydevice: %d bytes read.\n", bytes_read); return bytes_read; } static ssize_t write(struct file *file, const char __user *buf, size_t count, loff_t *pos) { int bytes_written = 0; if (*pos >= BUF_SIZE) { return -ENOSPC; } if (count + *pos > BUF_SIZE) { count = BUF_SIZE - *pos; } if (copy_from_user(dev_buf + *pos, buf, count)) { return -EFAULT; } *pos += count; bytes_written = count; printk(KERN_INFO "mydevice: %d bytes written.\n", bytes_written); return bytes_written; } static long ioctl(struct file *file, unsigned int cmd, unsigned long arg) { switch (cmd) { case 0: // 控制命令0 // 执行相应的控制操作 break; case 1: // 控制命令1 // 执行相应的控制操作 break; default: return -ENOTTY; } return 0; } static loff_t lseek(struct file *file, loff_t offset, int whence) { loff_t newpos = 0; switch (whence) { case 0: // SEEK_SET newpos = offset; break; case 1: // SEEK_CUR newpos = file->f_pos + offset; break; case 2: // SEEK_END newpos = BUF_SIZE + offset; break; default: return -EINVAL; } if (newpos < 0 || newpos > BUF_SIZE) { return -EINVAL; } file->f_pos = newpos; return newpos; } static struct file_operations mydevice_fops = { .owner = THIS_MODULE, .open = open, .release = release, .read = read, .write = write, .unlocked_ioctl = ioctl, .llseek = lseek, }; static int __init mydevice_init(void) { dev_buf = kmalloc(BUF_SIZE, GFP_KERNEL); if (!dev_buf) { printk(KERN_ALERT "mydevice: kmalloc failed.\n"); return -ENOMEM; } memset(dev_buf, 0, BUF_SIZE); major = register_chrdev(0, DEVICE_NAME, &mydevice_fops); if (major < 0) { printk(KERN_ALERT "mydevice: register_chrdev failed.\n"); return major; } printk(KERN_INFO "mydevice: Device registered, major = %d.\n", major); return 0; } static void __exit mydevice_exit(void) { unregister_chrdev(major, DEVICE_NAME); kfree(dev_buf); printk(KERN_INFO "mydevice: Device unregistered.\n"); } module_init(mydevice_init); module_exit(mydevice_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("My Device Driver");解释这串代码
这段代码是一个Linux设备驱动程序的示例,其目的是创建一个名为“mydevice”的字符设备,并实现设备文件的读写操作、文件指针的移动、ioctl操作等。该驱动程序通过调用register_chrdev函数注册字符设备,并实现了open、read、write、ioctl、lseek、release等文件操作函数。其中,open和release函数用于打开和关闭设备文件;read和write函数用于读写设备文件;lseek函数用于移动文件指针;ioctl函数用于实现设备的控制操作。该驱动程序还使用了动态内存分配函数kmalloc和动态内存释放函数kfree来分配和释放设备缓冲区dev_buf。最后,该驱动程序使用module_init和module_exit宏定义来指定驱动程序的初始化和退出函数,以及使用MODULE_LICENSE、MODULE_AUTHOR和MODULE_DESCRIPTION宏定义来指定驱动程序的许可证、作者和描述信息。
阅读全文