Linux内核打印printk


本文将展开介绍内核打印函数printk
printk函数是Linux内核中常用的记录消息的函数之一。能在任何上下文中使用,并提供灵活的格式化输出功能。通过使用 printk函数,开发者可以在内核代码中记录关键消息,进行调试以及了解系统的运行状况。

函数原型

/**
 * printk - print a kernel message
 * @fmt: format string
 *
 * This is printk(). It can be called from any context. We want it to work.
 *
 * We try to grab the console_lock. If we succeed, it's easy - we log the
 * output and call the console drivers.  If we fail to get the semaphore, we
 * place the output into the log buffer and return. The current holder of
 * the console_sem will notice the new output in console_unlock(); and will
 * send it to the consoles before releasing the lock.
 *
 * One effect of this deferred printing is that code which calls printk() and
 * then changes console_loglevel may break. This is because console_loglevel
 * is inspected when the actual printing occurs.
 *
 * See also:
 * printf(3)
 *
 * See the vsnprintf() documentation for format string extensions over C99.
 */
asmlinkage __visible int printk(const char *fmt, ...)
{
	printk_func_t vprintk_func;
	va_list args;
	int r;

	va_start(args, fmt);

	/*
	 * If a caller overrides the per_cpu printk_func, then it needs
	 * to disable preemption when calling printk(). Otherwise
	 * the printk_func should be set to the default. No need to
	 * disable preemption here.
	 */
	vprintk_func = this_cpu_read(printk_func);
	r = vprintk_func(fmt, args);

	va_end(args);

	return r;
}
EXPORT_SYMBOL(printk);

printk函数使用asmlinkage__visible修饰符,表示这是一个可见的内联函数,并可以在任意上下文中调用。
EXPORT_SYMBOL(printk)表示将printk函数导出为符号,使得其他模块能够使用该函数。

打印级别

共有如下8个级别,这些宏通常与printk函数一起使用,用于在内核中输出不同级别的消息到系统日志。

#define KERN_EMERG	KERN_SOH "0"	/* system is unusable */
#define KERN_ALERT	KERN_SOH "1"	/* action must be taken immediately */
#define KERN_CRIT	KERN_SOH "2"	/* critical conditions */
#define KERN_ERR	KERN_SOH "3"	/* error conditions */
#define KERN_WARNING	KERN_SOH "4"	/* warning conditions */
#define KERN_NOTICE	KERN_SOH "5"	/* normal but significant condition */
#define KERN_INFO	KERN_SOH "6"	/* informational */
#define KERN_DEBUG	KERN_SOH "7"	/* debug-level messages */

#define KERN_DEFAULT	KERN_SOH "d"	/* the default kernel loglevel */ 

默认打印级别

console_printk数组

Linux内核源代码中,console_printk是一个名为console_printk的整型数组。该数组的定义如下所示:

int console_printk[4] = {
    CONSOLE_LOGLEVEL_DEFAULT,     /* console_loglevel */
    MESSAGE_LOGLEVEL_DEFAULT,     /* default_message_loglevel */
    CONSOLE_LOGLEVEL_MIN,         /* minimum_console_loglevel */
    CONSOLE_LOGLEVEL_DEFAULT,     /* default_console_loglevel */
};

其中,数组console_printk的四个元素依次表示以下意义的值:

  1. console_loglevel:控制台(console)输出日志的级别。
  2. default_message_loglevel:默认消息日志级别。
  3. minimum_console_loglevel:最小控制台日志级别。
  4. default_console_loglevel:默认控制台日志级别。

这些值在内核的初始化阶段被用于设定控制台输出日志的级别。这些级别控制了内核消息何时会被输出到系统的控制台上。

在这个数组中,CONSOLE_LOGLEVEL_DEFAULTMESSAGE_LOGLEVEL_DEFAULT分别代表控制台和消息的默认日志级别,而CONSOLE_LOGLEVEL_MIN代表最小的控制台日志级别。

这些值是Linux内核中用于控制内核消息输出的重要参数,在调试系统问题和进行日志记录时起着关键作用。

查看当前控制台的打印级别

cat /proc/sys/kernel/printk

输出的内容通常是一个由空格分隔的四个整数,分别表示以下意义的值:

  1. 控制台(console)输出日志的最小级别
  2. 默认消息日志级别
  3. 控制台输出日志的最小级别
  4. 默认控制台日志级别

输出可能是类似这样的信息:

4       4       1       7

与上面的console_printk数组对应

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值