xref: /linux-6.15/include/linux/printk.h (revision 82d00a93)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KERNEL_PRINTK__
3 #define __KERNEL_PRINTK__
4 
5 #include <stdarg.h>
6 #include <linux/init.h>
7 #include <linux/kern_levels.h>
8 #include <linux/linkage.h>
9 #include <linux/cache.h>
10 
11 extern const char linux_banner[];
12 extern const char linux_proc_banner[];
13 
14 #define PRINTK_MAX_SINGLE_HEADER_LEN 2
15 
16 static inline int printk_get_level(const char *buffer)
17 {
18 	if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
19 		switch (buffer[1]) {
20 		case '0' ... '7':
21 		case 'c':	/* KERN_CONT */
22 			return buffer[1];
23 		}
24 	}
25 	return 0;
26 }
27 
28 static inline const char *printk_skip_level(const char *buffer)
29 {
30 	if (printk_get_level(buffer))
31 		return buffer + 2;
32 
33 	return buffer;
34 }
35 
36 static inline const char *printk_skip_headers(const char *buffer)
37 {
38 	while (printk_get_level(buffer))
39 		buffer = printk_skip_level(buffer);
40 
41 	return buffer;
42 }
43 
44 #define CONSOLE_EXT_LOG_MAX	8192
45 
46 /* printk's without a loglevel use this.. */
47 #define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT
48 
49 /* We show everything that is MORE important than this.. */
50 #define CONSOLE_LOGLEVEL_SILENT  0 /* Mum's the word */
51 #define CONSOLE_LOGLEVEL_MIN	 1 /* Minimum loglevel we let people use */
52 #define CONSOLE_LOGLEVEL_DEBUG	10 /* issue debug messages */
53 #define CONSOLE_LOGLEVEL_MOTORMOUTH 15	/* You can't shut this one up */
54 
55 /*
56  * Default used to be hard-coded at 7, quiet used to be hardcoded at 4,
57  * we're now allowing both to be set from kernel config.
58  */
59 #define CONSOLE_LOGLEVEL_DEFAULT CONFIG_CONSOLE_LOGLEVEL_DEFAULT
60 #define CONSOLE_LOGLEVEL_QUIET	 CONFIG_CONSOLE_LOGLEVEL_QUIET
61 
62 extern int console_printk[];
63 
64 #define console_loglevel (console_printk[0])
65 #define default_message_loglevel (console_printk[1])
66 #define minimum_console_loglevel (console_printk[2])
67 #define default_console_loglevel (console_printk[3])
68 
69 static inline void console_silent(void)
70 {
71 	console_loglevel = CONSOLE_LOGLEVEL_SILENT;
72 }
73 
74 static inline void console_verbose(void)
75 {
76 	if (console_loglevel)
77 		console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
78 }
79 
80 /* strlen("ratelimit") + 1 */
81 #define DEVKMSG_STR_MAX_SIZE 10
82 extern char devkmsg_log_str[];
83 struct ctl_table;
84 
85 extern int suppress_printk;
86 
87 struct va_format {
88 	const char *fmt;
89 	va_list *va;
90 };
91 
92 /*
93  * FW_BUG
94  * Add this to a message where you are sure the firmware is buggy or behaves
95  * really stupid or out of spec. Be aware that the responsible BIOS developer
96  * should be able to fix this issue or at least get a concrete idea of the
97  * problem by reading your message without the need of looking at the kernel
98  * code.
99  *
100  * Use it for definite and high priority BIOS bugs.
101  *
102  * FW_WARN
103  * Use it for not that clear (e.g. could the kernel messed up things already?)
104  * and medium priority BIOS bugs.
105  *
106  * FW_INFO
107  * Use this one if you want to tell the user or vendor about something
108  * suspicious, but generally harmless related to the firmware.
109  *
110  * Use it for information or very low priority BIOS bugs.
111  */
112 #define FW_BUG		"[Firmware Bug]: "
113 #define FW_WARN		"[Firmware Warn]: "
114 #define FW_INFO		"[Firmware Info]: "
115 
116 /*
117  * HW_ERR
118  * Add this to a message for hardware errors, so that user can report
119  * it to hardware vendor instead of LKML or software vendor.
120  */
121 #define HW_ERR		"[Hardware Error]: "
122 
123 /*
124  * DEPRECATED
125  * Add this to a message whenever you want to warn user space about the use
126  * of a deprecated aspect of an API so they can stop using it
127  */
128 #define DEPRECATED	"[Deprecated]: "
129 
130 /*
131  * Dummy printk for disabled debugging statements to use whilst maintaining
132  * gcc's format checking.
133  */
134 #define no_printk(fmt, ...)				\
135 ({							\
136 	if (0)						\
137 		printk(fmt, ##__VA_ARGS__);		\
138 	0;						\
139 })
140 
141 #ifdef CONFIG_EARLY_PRINTK
142 extern asmlinkage __printf(1, 2)
143 void early_printk(const char *fmt, ...);
144 #else
145 static inline __printf(1, 2) __cold
146 void early_printk(const char *s, ...) { }
147 #endif
148 
149 #ifdef CONFIG_PRINTK_NMI
150 extern void printk_nmi_enter(void);
151 extern void printk_nmi_exit(void);
152 extern void printk_nmi_direct_enter(void);
153 extern void printk_nmi_direct_exit(void);
154 #else
155 static inline void printk_nmi_enter(void) { }
156 static inline void printk_nmi_exit(void) { }
157 static inline void printk_nmi_direct_enter(void) { }
158 static inline void printk_nmi_direct_exit(void) { }
159 #endif /* PRINTK_NMI */
160 
161 #ifdef CONFIG_PRINTK
162 asmlinkage __printf(5, 0)
163 int vprintk_emit(int facility, int level,
164 		 const char *dict, size_t dictlen,
165 		 const char *fmt, va_list args);
166 
167 asmlinkage __printf(1, 0)
168 int vprintk(const char *fmt, va_list args);
169 
170 asmlinkage __printf(1, 2) __cold
171 int printk(const char *fmt, ...);
172 
173 /*
174  * Special printk facility for scheduler/timekeeping use only, _DO_NOT_USE_ !
175  */
176 __printf(1, 2) __cold int printk_deferred(const char *fmt, ...);
177 
178 /*
179  * Please don't use printk_ratelimit(), because it shares ratelimiting state
180  * with all other unrelated printk_ratelimit() callsites.  Instead use
181  * printk_ratelimited() or plain old __ratelimit().
182  */
183 extern int __printk_ratelimit(const char *func);
184 #define printk_ratelimit() __printk_ratelimit(__func__)
185 extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
186 				   unsigned int interval_msec);
187 
188 extern int printk_delay_msec;
189 extern int dmesg_restrict;
190 
191 extern int
192 devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, void __user *buf,
193 			  size_t *lenp, loff_t *ppos);
194 
195 extern void wake_up_klogd(void);
196 
197 char *log_buf_addr_get(void);
198 u32 log_buf_len_get(void);
199 void log_buf_vmcoreinfo_setup(void);
200 void __init setup_log_buf(int early);
201 __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...);
202 void dump_stack_print_info(const char *log_lvl);
203 void show_regs_print_info(const char *log_lvl);
204 extern asmlinkage void dump_stack(void) __cold;
205 extern void printk_safe_flush(void);
206 extern void printk_safe_flush_on_panic(void);
207 #else
208 static inline __printf(1, 0)
209 int vprintk(const char *s, va_list args)
210 {
211 	return 0;
212 }
213 static inline __printf(1, 2) __cold
214 int printk(const char *s, ...)
215 {
216 	return 0;
217 }
218 static inline __printf(1, 2) __cold
219 int printk_deferred(const char *s, ...)
220 {
221 	return 0;
222 }
223 static inline int printk_ratelimit(void)
224 {
225 	return 0;
226 }
227 static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
228 					  unsigned int interval_msec)
229 {
230 	return false;
231 }
232 
233 static inline void wake_up_klogd(void)
234 {
235 }
236 
237 static inline char *log_buf_addr_get(void)
238 {
239 	return NULL;
240 }
241 
242 static inline u32 log_buf_len_get(void)
243 {
244 	return 0;
245 }
246 
247 static inline void log_buf_vmcoreinfo_setup(void)
248 {
249 }
250 
251 static inline void setup_log_buf(int early)
252 {
253 }
254 
255 static inline __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...)
256 {
257 }
258 
259 static inline void dump_stack_print_info(const char *log_lvl)
260 {
261 }
262 
263 static inline void show_regs_print_info(const char *log_lvl)
264 {
265 }
266 
267 static inline void dump_stack(void)
268 {
269 }
270 
271 static inline void printk_safe_flush(void)
272 {
273 }
274 
275 static inline void printk_safe_flush_on_panic(void)
276 {
277 }
278 #endif
279 
280 extern int kptr_restrict;
281 
282 #ifndef pr_fmt
283 #define pr_fmt(fmt) fmt
284 #endif
285 
286 /*
287  * These can be used to print at the various log levels.
288  * All of these will print unconditionally, although note that pr_debug()
289  * and other debug macros are compiled out unless either DEBUG is defined
290  * or CONFIG_DYNAMIC_DEBUG is set.
291  */
292 #define pr_emerg(fmt, ...) \
293 	printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
294 #define pr_alert(fmt, ...) \
295 	printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
296 #define pr_crit(fmt, ...) \
297 	printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
298 #define pr_err(fmt, ...) \
299 	printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
300 #define pr_warn(fmt, ...) \
301 	printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
302 #define pr_notice(fmt, ...) \
303 	printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
304 #define pr_info(fmt, ...) \
305 	printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
306 /*
307  * Like KERN_CONT, pr_cont() should only be used when continuing
308  * a line with no newline ('\n') enclosed. Otherwise it defaults
309  * back to KERN_DEFAULT.
310  */
311 #define pr_cont(fmt, ...) \
312 	printk(KERN_CONT fmt, ##__VA_ARGS__)
313 
314 /* pr_devel() should produce zero code unless DEBUG is defined */
315 #ifdef DEBUG
316 #define pr_devel(fmt, ...) \
317 	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
318 #else
319 #define pr_devel(fmt, ...) \
320 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
321 #endif
322 
323 
324 /* If you are writing a driver, please use dev_dbg instead */
325 #if defined(CONFIG_DYNAMIC_DEBUG)
326 #include <linux/dynamic_debug.h>
327 
328 /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
329 #define pr_debug(fmt, ...) \
330 	dynamic_pr_debug(fmt, ##__VA_ARGS__)
331 #elif defined(DEBUG)
332 #define pr_debug(fmt, ...) \
333 	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
334 #else
335 #define pr_debug(fmt, ...) \
336 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
337 #endif
338 
339 /*
340  * Print a one-time message (analogous to WARN_ONCE() et al):
341  */
342 
343 #ifdef CONFIG_PRINTK
344 #define printk_once(fmt, ...)					\
345 ({								\
346 	static bool __section(.data.once) __print_once;		\
347 	bool __ret_print_once = !__print_once;			\
348 								\
349 	if (!__print_once) {					\
350 		__print_once = true;				\
351 		printk(fmt, ##__VA_ARGS__);			\
352 	}							\
353 	unlikely(__ret_print_once);				\
354 })
355 #define printk_deferred_once(fmt, ...)				\
356 ({								\
357 	static bool __section(.data.once) __print_once;		\
358 	bool __ret_print_once = !__print_once;			\
359 								\
360 	if (!__print_once) {					\
361 		__print_once = true;				\
362 		printk_deferred(fmt, ##__VA_ARGS__);		\
363 	}							\
364 	unlikely(__ret_print_once);				\
365 })
366 #else
367 #define printk_once(fmt, ...)					\
368 	no_printk(fmt, ##__VA_ARGS__)
369 #define printk_deferred_once(fmt, ...)				\
370 	no_printk(fmt, ##__VA_ARGS__)
371 #endif
372 
373 #define pr_emerg_once(fmt, ...)					\
374 	printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
375 #define pr_alert_once(fmt, ...)					\
376 	printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
377 #define pr_crit_once(fmt, ...)					\
378 	printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
379 #define pr_err_once(fmt, ...)					\
380 	printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
381 #define pr_warn_once(fmt, ...)					\
382 	printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
383 #define pr_notice_once(fmt, ...)				\
384 	printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
385 #define pr_info_once(fmt, ...)					\
386 	printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
387 #define pr_cont_once(fmt, ...)					\
388 	printk_once(KERN_CONT pr_fmt(fmt), ##__VA_ARGS__)
389 
390 #if defined(DEBUG)
391 #define pr_devel_once(fmt, ...)					\
392 	printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
393 #else
394 #define pr_devel_once(fmt, ...)					\
395 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
396 #endif
397 
398 /* If you are writing a driver, please use dev_dbg instead */
399 #if defined(DEBUG)
400 #define pr_debug_once(fmt, ...)					\
401 	printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
402 #else
403 #define pr_debug_once(fmt, ...)					\
404 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
405 #endif
406 
407 /*
408  * ratelimited messages with local ratelimit_state,
409  * no local ratelimit_state used in the !PRINTK case
410  */
411 #ifdef CONFIG_PRINTK
412 #define printk_ratelimited(fmt, ...)					\
413 ({									\
414 	static DEFINE_RATELIMIT_STATE(_rs,				\
415 				      DEFAULT_RATELIMIT_INTERVAL,	\
416 				      DEFAULT_RATELIMIT_BURST);		\
417 									\
418 	if (__ratelimit(&_rs))						\
419 		printk(fmt, ##__VA_ARGS__);				\
420 })
421 #else
422 #define printk_ratelimited(fmt, ...)					\
423 	no_printk(fmt, ##__VA_ARGS__)
424 #endif
425 
426 #define pr_emerg_ratelimited(fmt, ...)					\
427 	printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
428 #define pr_alert_ratelimited(fmt, ...)					\
429 	printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
430 #define pr_crit_ratelimited(fmt, ...)					\
431 	printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
432 #define pr_err_ratelimited(fmt, ...)					\
433 	printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
434 #define pr_warn_ratelimited(fmt, ...)					\
435 	printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
436 #define pr_notice_ratelimited(fmt, ...)					\
437 	printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
438 #define pr_info_ratelimited(fmt, ...)					\
439 	printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
440 /* no pr_cont_ratelimited, don't do that... */
441 
442 #if defined(DEBUG)
443 #define pr_devel_ratelimited(fmt, ...)					\
444 	printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
445 #else
446 #define pr_devel_ratelimited(fmt, ...)					\
447 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
448 #endif
449 
450 /* If you are writing a driver, please use dev_dbg instead */
451 #if defined(CONFIG_DYNAMIC_DEBUG)
452 /* descriptor check is first to prevent flooding with "callbacks suppressed" */
453 #define pr_debug_ratelimited(fmt, ...)					\
454 do {									\
455 	static DEFINE_RATELIMIT_STATE(_rs,				\
456 				      DEFAULT_RATELIMIT_INTERVAL,	\
457 				      DEFAULT_RATELIMIT_BURST);		\
458 	DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt));		\
459 	if (DYNAMIC_DEBUG_BRANCH(descriptor) &&				\
460 	    __ratelimit(&_rs))						\
461 		__dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__);	\
462 } while (0)
463 #elif defined(DEBUG)
464 #define pr_debug_ratelimited(fmt, ...)					\
465 	printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
466 #else
467 #define pr_debug_ratelimited(fmt, ...) \
468 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
469 #endif
470 
471 extern const struct file_operations kmsg_fops;
472 
473 enum {
474 	DUMP_PREFIX_NONE,
475 	DUMP_PREFIX_ADDRESS,
476 	DUMP_PREFIX_OFFSET
477 };
478 extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
479 			      int groupsize, char *linebuf, size_t linebuflen,
480 			      bool ascii);
481 #ifdef CONFIG_PRINTK
482 extern void print_hex_dump(const char *level, const char *prefix_str,
483 			   int prefix_type, int rowsize, int groupsize,
484 			   const void *buf, size_t len, bool ascii);
485 #else
486 static inline void print_hex_dump(const char *level, const char *prefix_str,
487 				  int prefix_type, int rowsize, int groupsize,
488 				  const void *buf, size_t len, bool ascii)
489 {
490 }
491 static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
492 					const void *buf, size_t len)
493 {
494 }
495 
496 #endif
497 
498 #if defined(CONFIG_DYNAMIC_DEBUG)
499 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize,	\
500 			     groupsize, buf, len, ascii)	\
501 	dynamic_hex_dump(prefix_str, prefix_type, rowsize,	\
502 			 groupsize, buf, len, ascii)
503 #elif defined(DEBUG)
504 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize,		\
505 			     groupsize, buf, len, ascii)		\
506 	print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize,	\
507 		       groupsize, buf, len, ascii)
508 #else
509 static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type,
510 					int rowsize, int groupsize,
511 					const void *buf, size_t len, bool ascii)
512 {
513 }
514 #endif
515 
516 /**
517  * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params
518  * @prefix_str: string to prefix each line with;
519  *  caller supplies trailing spaces for alignment if desired
520  * @prefix_type: controls whether prefix of an offset, address, or none
521  *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
522  * @buf: data blob to dump
523  * @len: number of bytes in the @buf
524  *
525  * Calls print_hex_dump(), with log level of KERN_DEBUG,
526  * rowsize of 16, groupsize of 1, and ASCII output included.
527  */
528 #define print_hex_dump_bytes(prefix_str, prefix_type, buf, len)	\
529 	print_hex_dump_debug(prefix_str, prefix_type, 16, 1, buf, len, true)
530 
531 #endif
532