【问题标题】:How to debug in kernel mode using printk如何使用 printk 在内核模式下调试
【发布时间】:2015-12-13 21:13:59
【问题描述】:

我正在尝试向 Linux task_struct 添加一些东西。

在这个区域,我从用户那里复制了一个字符串并尝试将它存储在我的结构中。

我尝试通过添加将打印复制的字符串的printk 来调试我的代码。

这是代码的调试部分:

newTODO->TODO_description=(char*)(kmalloc(in_description_size+1,0));
    if( newTODO->TODO_description){
        kfree(newTODO);
        return -1;
    }

    res=copy_from_user(newTODO->TODO_description, in_TODO_description, in_description_size);
        if (res)                                //  error copying from user space, 1 or more char werent copied.
        {
            printk(KERN_ALERT "function: create element failed to copy from user\n");
            return -EFAULT;
        }
    newTODO->TODO_description[in_description_size]='\o';
    printk(KERN_ALERT "the copied string is: %s \n",newTODO->TODO_description);

对我来说必须重要的印刷品是

printk(KERN_ALERT "the copied string is: %s \n",newTODO->TODO_description);

会有用吗?

了解 printk:

什么时候我会在调用 printk 时从终端运行我的测试文件,它会将输出打印到工作终端?

【问题讨论】:

  • 使用dmesg查看内核日志。
  • ='\o'; 看起来很可疑。你是说='\0';(或者只是= 0;

标签: c linux linux-kernel kernel printk


【解决方案1】:

printk 函数会将消息附加到内核消息缓冲区中,但除非您给出命令,否则此缓冲区的包含不会显示在终端上。

正如 Ilya Matveychikov 所说,您可以使用“dmesg”命令转储内核消息缓冲区。

或使用以下命令获取实时内核消息观察。

echo 8 > /proc/sys/kernel/printk
tail -f /var/log/kern.log &



cat /proc/kmsg & (Android 环境)

【讨论】:

    猜你喜欢
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 2017-08-04
    • 2017-01-19
    • 1970-01-01
    • 2011-02-27
    • 2015-09-26
    相关资源
    最近更新 更多