【问题标题】:how to use printk()? [closed]如何使用 printk()? [关闭]
【发布时间】:2020-05-11 16:09:58
【问题描述】:

这是第一次在 Linux 环境中工作。 我需要你的很多帮助。 我想在 shced_setattr 添加 prink() https://elixir.bootlin.com/linux/v4.18/source/kernel/sched/core.c#L4578

SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
                   unsigned int, flags)
{
    printk();
    struct sched_attr attr;
    struct task_struct *p;
    int retval;

    if (!uattr || pid < 0 || flags)
        return -EINVAL;

    retval = sched_copy_attr(uattr, &attr);
    if (retval)
        return retval;

    if ((int)attr.sched_policy < 0)
        return -EINVAL;

    rcu_read_lock();
    retval = -ESRCH;
    p = find_process_by_pid(pid);
    if (p != NULL)
        retval = sched_setattr(p, &attr);
    rcu_read_unlock();

    return retval;
}

但我不知道如何编辑程序。 如果有人可以解释我为 printk() 函数编辑 linux 函数,那就太好了!

【问题讨论】:

  • 它(大部分)像printf一样工作 - 你有什么具体问题吗?
  • 您要打印什么?

标签: linux printk


【解决方案1】:

这完全取决于您尝试使用printk 打印的内容,这类似于printf。 printk 文档位于:

https://www.kernel.org/doc/html/latest/core-api/printk-formats.html

也是源码的一部分:

Documentation/printk-formats.txt

最简单的调用只涉及没有占位符的格式字符串:

printk("sched_setattr called\n");

使用pid 打印会导致:

printk("sched_setattr called: pid: %d\n", pid);

可以使用dmesg读取输出。

printk 也支持多个日志级别,但这与入门无关。

【讨论】:

  • 感谢您的回答!!但我也想知道如何在 shced_setattr() 中添加 printk() 函数
  • 您能详细说明一下吗?您遇到的技术问题是什么?
猜你喜欢
  • 1970-01-01
  • 2017-08-15
  • 1970-01-01
  • 1970-01-01
  • 2014-11-18
  • 2019-01-16
  • 2012-03-08
  • 2018-09-15
相关资源
最近更新 更多