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