繁体   English   中英

如何使用printk在内核模式下调试

[英]How to debug in kernel mode using printk

我想在Linux task_struct添加一些东西。

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

我尝试通过添加printk来调试我的代码, 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时,我将从终端运行我的测试文件,它会将输出打印到工作终端吗?

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

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

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

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

要么

cat / proc / kmsg&(Android环境)

暂无
暂无

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

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