xref: /linux/include/linux/printk.h (revision 3fd6c59042dbba50391e30862beac979491145fe)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KERNEL_PRINTK__
3 #define __KERNEL_PRINTK__
4 
5 #include <linux/stdarg.h>
6 #include <linux/init.h>
7 #include <linux/kern_levels.h>
8 #include <linux/linkage.h>
9 #include <linux/ratelimit_types.h>
10 #include <linux/once_lite.h>
11 
12 struct console;
13 
14 extern const char linux_banner[];
15 extern const char linux_proc_banner[];
16 
17 extern int oops_in_progress;	/* If set, an oops, panic(), BUG() or die() is in progress */
18 
19 #define PRINTK_MAX_SINGLE_HEADER_LEN 2
20 
printk_get_level(const char * buffer)21 static inline int printk_get_level(const char *buffer)
22 {
23 	if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
24 		switch (buffer[1]) {
25 		case '0' ... '7':
26 		case 'c':	/* KERN_CONT */
27 			return buffer[1];
28 		}
29 	}
30 	return 0;
31 }
32 
printk_skip_level(const char * buffer)33 static inline const char *printk_skip_level(const char *buffer)
34 {
35 	if (printk_get_level(buffer))
36 		return buffer + 2;
37 
38 	return buffer;
39 }
40 
printk_skip_headers(const char * buffer)41 static inline const char *printk_skip_headers(const char *buffer)
42 {
43 	while (printk_get_level(buffer))
44 		buffer = printk_skip_level(buffer);
45 
46 	return buffer;
47 }
48 
49 /* printk's without a loglevel use this.. */
50 #define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT
51 
52 /* We show everything that is MORE important than this.. */
53 #define CONSOLE_LOGLEVEL_SILENT  0 /* Mum's the word */
54 #define CONSOLE_LOGLEVEL_MIN	 1 /* Minimum loglevel we let people use */
55 #define CONSOLE_LOGLEVEL_DEBUG	10 /* issue debug messages */
56 #define CONSOLE_LOGLEVEL_MOTORMOUTH 15	/* You can't shut this one up */
57 
58 /*
59  * Default used to be hard-coded at 7, quiet used to be hardcoded at 4,
60  * we're now allowing both to be set from kernel config.
61  */
62 #define CONSOLE_LOGLEVEL_DEFAULT CONFIG_CONSOLE_LOGLEVEL_DEFAULT
63 #define CONSOLE_LOGLEVEL_QUIET	 CONFIG_CONSOLE_LOGLEVEL_QUIET
64 
65 int match_devname_and_update_preferred_console(const char *match,
66 					       const char *name,
67 					       const short idx);
68 
69 extern int console_printk[];
70 
71 #define console_loglevel (console_printk[0])
72 #define default_message_loglevel (console_printk[1])
73 #define minimum_console_loglevel (console_printk[2])
74 #define default_console_loglevel (console_printk[3])
75 
76 extern void console_verbose(void);
77 
78 /* strlen("ratelimit") + 1 */
79 #define DEVKMSG_STR_MAX_SIZE 10
80 extern char devkmsg_log_str[DEVKMSG_STR_MAX_SIZE];
81 struct ctl_table;
82 
83 extern int suppress_printk;
84 
85 struct va_format {
86 	const char *fmt;
87 	va_list *va;
88 };
89 
90 /*
91  * FW_BUG
92  * Add this to a message where you are sure the firmware is buggy or behaves
93  * really stupid or out of spec. Be aware that the responsible BIOS developer
94  * should be able to fix this issue or at least get a concrete idea of the
95  * problem by reading your message without the need of looking at the kernel
96  * code.
97  *
98  * Use it for definite and high priority BIOS bugs.
99  *
100  * FW_WARN
101  * Use it for not that clear (e.g. could the kernel messed up things already?)
102  * and medium priority BIOS bugs.
103  *
104  * FW_INFO
105  * Use this one if you want to tell the user or vendor about something
106  * suspicious, but generally harmless related to the firmware.
107  *
108  * Use it for information or very low priority BIOS bugs.
109  */
110 #define FW_BUG		"[Firmware Bug]: "
111 #define FW_WARN		"[Firmware Warn]: "
112 #define FW_INFO		"[Firmware Info]: "
113 
114 /*
115  * HW_ERR
116  * Add this to a message for hardware errors, so that user can report
117  * it to hardware vendor instead of LKML or software vendor.
118  */
119 #define HW_ERR		"[Hardware Error]: "
120 
121 /*
122  * DEPRECATED
123  * Add this to a message whenever you want to warn user space about the use
124  * of a deprecated aspect of an API so they can stop using it
125  */
126 #define DEPRECATED	"[Deprecated]: "
127 
128 /*
129  * Dummy printk for disabled debugging statements to use whilst maintaining
130  * gcc's format checking.
131  */
132 #define no_printk(fmt, ...)				\
133 ({							\
134 	if (0)						\
135 		_printk(fmt, ##__VA_ARGS__);		\
136 	0;						\
137 })
138 
139 #ifdef CONFIG_EARLY_PRINTK
140 extern asmlinkage __printf(1, 2)
141 void early_printk(const char *fmt, ...);
142 #else
143 static inline __printf(1, 2) __cold
early_printk(const char * s,...)144 void early_printk(const char *s, ...) { }
145 #endif
146 
147 struct dev_printk_info;
148 
149 #ifdef CONFIG_PRINTK
150 asmlinkage __printf(4, 0)
151 int vprintk_emit(int facility, int level,
152 		 const struct dev_printk_info *dev_info,
153 		 const char *fmt, va_list args);
154 
155 asmlinkage __printf(1, 0)
156 int vprintk(const char *fmt, va_list args);
157 
158 asmlinkage __printf(1, 2) __cold
159 int _printk(const char *fmt, ...);
160 
161 /*
162  * Special printk facility for scheduler/timekeeping use only, _DO_NOT_USE_ !
163  */
164 __printf(1, 2) __cold int _printk_deferred(const char *fmt, ...);
165 
166 extern void __printk_deferred_enter(void);
167 extern void __printk_deferred_exit(void);
168 
169 extern void printk_force_console_enter(void);
170 extern void printk_force_console_exit(void);
171 
172 /*
173  * The printk_deferred_enter/exit macros are available only as a hack for
174  * some code paths that need to defer all printk console printing. Interrupts
175  * must be disabled for the deferred duration.
176  */
177 #define printk_deferred_enter() __printk_deferred_enter()
178 #define printk_deferred_exit() __printk_deferred_exit()
179 
180 /*
181  * Please don't use printk_ratelimit(), because it shares ratelimiting state
182  * with all other unrelated printk_ratelimit() callsites.  Instead use
183  * printk_ratelimited() or plain old __ratelimit().
184  */
185 extern int __printk_ratelimit(const char *func);
186 #define printk_ratelimit() __printk_ratelimit(__func__)
187 extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
188 				   unsigned int interval_msec);
189 
190 extern int printk_delay_msec;
191 extern int dmesg_restrict;
192 
193 extern void wake_up_klogd(void);
194 
195 char *log_buf_addr_get(void);
196 u32 log_buf_len_get(void);
197 void log_buf_vmcoreinfo_setup(void);
198 void __init setup_log_buf(int early);
199 __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...);
200 void dump_stack_print_info(const char *log_lvl);
201 void show_regs_print_info(const char *log_lvl);
202 extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold;
203 extern asmlinkage void dump_stack(void) __cold;
204 void printk_trigger_flush(void);
205 void console_try_replay_all(void);
206 void printk_legacy_allow_panic_sync(void);
207 extern bool nbcon_device_try_acquire(struct console *con);
208 extern void nbcon_device_release(struct console *con);
209 void nbcon_atomic_flush_unsafe(void);
210 #else
211 static inline __printf(1, 0)
vprintk(const char * s,va_list args)212 int vprintk(const char *s, va_list args)
213 {
214 	return 0;
215 }
216 static inline __printf(1, 2) __cold
_printk(const char * s,...)217 int _printk(const char *s, ...)
218 {
219 	return 0;
220 }
221 static inline __printf(1, 2) __cold
_printk_deferred(const char * s,...)222 int _printk_deferred(const char *s, ...)
223 {
224 	return 0;
225 }
226 
printk_deferred_enter(void)227 static inline void printk_deferred_enter(void)
228 {
229 }
230 
printk_deferred_exit(void)231 static inline void printk_deferred_exit(void)
232 {
233 }
234 
printk_force_console_enter(void)235 static inline void printk_force_console_enter(void)
236 {
237 }
238 
printk_force_console_exit(void)239 static inline void printk_force_console_exit(void)
240 {
241 }
242 
printk_ratelimit(void)243 static inline int printk_ratelimit(void)
244 {
245 	return 0;
246 }
printk_timed_ratelimit(unsigned long * caller_jiffies,unsigned int interval_msec)247 static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
248 					  unsigned int interval_msec)
249 {
250 	return false;
251 }
252 
wake_up_klogd(void)253 static inline void wake_up_klogd(void)
254 {
255 }
256 
log_buf_addr_get(void)257 static inline char *log_buf_addr_get(void)
258 {
259 	return NULL;
260 }
261 
log_buf_len_get(void)262 static inline u32 log_buf_len_get(void)
263 {
264 	return 0;
265 }
266 
log_buf_vmcoreinfo_setup(void)267 static inline void log_buf_vmcoreinfo_setup(void)
268 {
269 }
270 
setup_log_buf(int early)271 static inline void setup_log_buf(int early)
272 {
273 }
274 
dump_stack_set_arch_desc(const char * fmt,...)275 static inline __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...)
276 {
277 }
278 
dump_stack_print_info(const char * log_lvl)279 static inline void dump_stack_print_info(const char *log_lvl)
280 {
281 }
282 
show_regs_print_info(const char * log_lvl)283 static inline void show_regs_print_info(const char *log_lvl)
284 {
285 }
286 
dump_stack_lvl(const char * log_lvl)287 static inline void dump_stack_lvl(const char *log_lvl)
288 {
289 }
290 
dump_stack(void)291 static inline void dump_stack(void)
292 {
293 }
printk_trigger_flush(void)294 static inline void printk_trigger_flush(void)
295 {
296 }
console_try_replay_all(void)297 static inline void console_try_replay_all(void)
298 {
299 }
300 
printk_legacy_allow_panic_sync(void)301 static inline void printk_legacy_allow_panic_sync(void)
302 {
303 }
304 
nbcon_device_try_acquire(struct console * con)305 static inline bool nbcon_device_try_acquire(struct console *con)
306 {
307 	return false;
308 }
309 
nbcon_device_release(struct console * con)310 static inline void nbcon_device_release(struct console *con)
311 {
312 }
313 
nbcon_atomic_flush_unsafe(void)314 static inline void nbcon_atomic_flush_unsafe(void)
315 {
316 }
317 
318 #endif
319 
320 bool this_cpu_in_panic(void);
321 
322 #ifdef CONFIG_SMP
323 extern int __printk_cpu_sync_try_get(void);
324 extern void __printk_cpu_sync_wait(void);
325 extern void __printk_cpu_sync_put(void);
326 
327 #else
328 
329 #define __printk_cpu_sync_try_get() true
330 #define __printk_cpu_sync_wait()
331 #define __printk_cpu_sync_put()
332 #endif /* CONFIG_SMP */
333 
334 /**
335  * printk_cpu_sync_get_irqsave() - Disable interrupts and acquire the printk
336  *                                 cpu-reentrant spinning lock.
337  * @flags: Stack-allocated storage for saving local interrupt state,
338  *         to be passed to printk_cpu_sync_put_irqrestore().
339  *
340  * If the lock is owned by another CPU, spin until it becomes available.
341  * Interrupts are restored while spinning.
342  *
343  * CAUTION: This function must be used carefully. It does not behave like a
344  * typical lock. Here are important things to watch out for...
345  *
346  *     * This function is reentrant on the same CPU. Therefore the calling
347  *       code must not assume exclusive access to data if code accessing the
348  *       data can run reentrant or within NMI context on the same CPU.
349  *
350  *     * If there exists usage of this function from NMI context, it becomes
351  *       unsafe to perform any type of locking or spinning to wait for other
352  *       CPUs after calling this function from any context. This includes
353  *       using spinlocks or any other busy-waiting synchronization methods.
354  */
355 #define printk_cpu_sync_get_irqsave(flags)		\
356 	for (;;) {					\
357 		local_irq_save(flags);			\
358 		if (__printk_cpu_sync_try_get())	\
359 			break;				\
360 		local_irq_restore(flags);		\
361 		__printk_cpu_sync_wait();		\
362 	}
363 
364 /**
365  * printk_cpu_sync_put_irqrestore() - Release the printk cpu-reentrant spinning
366  *                                    lock and restore interrupts.
367  * @flags: Caller's saved interrupt state, from printk_cpu_sync_get_irqsave().
368  */
369 #define printk_cpu_sync_put_irqrestore(flags)	\
370 	do {					\
371 		__printk_cpu_sync_put();	\
372 		local_irq_restore(flags);	\
373 	} while (0)
374 
375 extern int kptr_restrict;
376 
377 /**
378  * pr_fmt - used by the pr_*() macros to generate the printk format string
379  * @fmt: format string passed from a pr_*() macro
380  *
381  * This macro can be used to generate a unified format string for pr_*()
382  * macros. A common use is to prefix all pr_*() messages in a file with a common
383  * string. For example, defining this at the top of a source file:
384  *
385  *        #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
386  *
387  * would prefix all pr_info, pr_emerg... messages in the file with the module
388  * name.
389  */
390 #ifndef pr_fmt
391 #define pr_fmt(fmt) fmt
392 #endif
393 
394 struct module;
395 
396 #ifdef CONFIG_PRINTK_INDEX
397 struct pi_entry {
398 	const char *fmt;
399 	const char *func;
400 	const char *file;
401 	unsigned int line;
402 
403 	/*
404 	 * While printk and pr_* have the level stored in the string at compile
405 	 * time, some subsystems dynamically add it at runtime through the
406 	 * format string. For these dynamic cases, we allow the subsystem to
407 	 * tell us the level at compile time.
408 	 *
409 	 * NULL indicates that the level, if any, is stored in fmt.
410 	 */
411 	const char *level;
412 
413 	/*
414 	 * The format string used by various subsystem specific printk()
415 	 * wrappers to prefix the message.
416 	 *
417 	 * Note that the static prefix defined by the pr_fmt() macro is stored
418 	 * directly in the message format (@fmt), not here.
419 	 */
420 	const char *subsys_fmt_prefix;
421 } __packed;
422 
423 #define __printk_index_emit(_fmt, _level, _subsys_fmt_prefix)		\
424 	do {								\
425 		if (__builtin_constant_p(_fmt) && __builtin_constant_p(_level)) { \
426 			/*
427 			 * We check __builtin_constant_p multiple times here
428 			 * for the same input because GCC will produce an error
429 			 * if we try to assign a static variable to fmt if it
430 			 * is not a constant, even with the outer if statement.
431 			 */						\
432 			static const struct pi_entry _entry		\
433 			__used = {					\
434 				.fmt = __builtin_constant_p(_fmt) ? (_fmt) : NULL, \
435 				.func = __func__,			\
436 				.file = __FILE__,			\
437 				.line = __LINE__,			\
438 				.level = __builtin_constant_p(_level) ? (_level) : NULL, \
439 				.subsys_fmt_prefix = _subsys_fmt_prefix,\
440 			};						\
441 			static const struct pi_entry *_entry_ptr	\
442 			__used __section(".printk_index") = &_entry;	\
443 		}							\
444 	} while (0)
445 
446 #else /* !CONFIG_PRINTK_INDEX */
447 #define __printk_index_emit(...) do {} while (0)
448 #endif /* CONFIG_PRINTK_INDEX */
449 
450 /*
451  * Some subsystems have their own custom printk that applies a va_format to a
452  * generic format, for example, to include a device number or other metadata
453  * alongside the format supplied by the caller.
454  *
455  * In order to store these in the way they would be emitted by the printk
456  * infrastructure, the subsystem provides us with the start, fixed string, and
457  * any subsequent text in the format string.
458  *
459  * We take a variable argument list as pr_fmt/dev_fmt/etc are sometimes passed
460  * as multiple arguments (eg: `"%s: ", "blah"`), and we must only take the
461  * first one.
462  *
463  * subsys_fmt_prefix must be known at compile time, or compilation will fail
464  * (since this is a mistake). If fmt or level is not known at compile time, no
465  * index entry will be made (since this can legitimately happen).
466  */
467 #define printk_index_subsys_emit(subsys_fmt_prefix, level, fmt, ...) \
468 	__printk_index_emit(fmt, level, subsys_fmt_prefix)
469 
470 #define printk_index_wrap(_p_func, _fmt, ...)				\
471 	({								\
472 		__printk_index_emit(_fmt, NULL, NULL);			\
473 		_p_func(_fmt, ##__VA_ARGS__);				\
474 	})
475 
476 
477 /**
478  * printk - print a kernel message
479  * @fmt: format string
480  *
481  * This is printk(). It can be called from any context. We want it to work.
482  *
483  * If printk indexing is enabled, _printk() is called from printk_index_wrap.
484  * Otherwise, printk is simply #defined to _printk.
485  *
486  * We try to grab the console_lock. If we succeed, it's easy - we log the
487  * output and call the console drivers.  If we fail to get the semaphore, we
488  * place the output into the log buffer and return. The current holder of
489  * the console_sem will notice the new output in console_unlock(); and will
490  * send it to the consoles before releasing the lock.
491  *
492  * One effect of this deferred printing is that code which calls printk() and
493  * then changes console_loglevel may break. This is because console_loglevel
494  * is inspected when the actual printing occurs.
495  *
496  * See also:
497  * printf(3)
498  *
499  * See the vsnprintf() documentation for format string extensions over C99.
500  */
501 #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
502 #define printk_deferred(fmt, ...)					\
503 	printk_index_wrap(_printk_deferred, fmt, ##__VA_ARGS__)
504 
505 /**
506  * pr_emerg - Print an emergency-level message
507  * @fmt: format string
508  * @...: arguments for the format string
509  *
510  * This macro expands to a printk with KERN_EMERG loglevel. It uses pr_fmt() to
511  * generate the format string.
512  */
513 #define pr_emerg(fmt, ...) \
514 	printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
515 /**
516  * pr_alert - Print an alert-level message
517  * @fmt: format string
518  * @...: arguments for the format string
519  *
520  * This macro expands to a printk with KERN_ALERT loglevel. It uses pr_fmt() to
521  * generate the format string.
522  */
523 #define pr_alert(fmt, ...) \
524 	printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
525 /**
526  * pr_crit - Print a critical-level message
527  * @fmt: format string
528  * @...: arguments for the format string
529  *
530  * This macro expands to a printk with KERN_CRIT loglevel. It uses pr_fmt() to
531  * generate the format string.
532  */
533 #define pr_crit(fmt, ...) \
534 	printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
535 /**
536  * pr_err - Print an error-level message
537  * @fmt: format string
538  * @...: arguments for the format string
539  *
540  * This macro expands to a printk with KERN_ERR loglevel. It uses pr_fmt() to
541  * generate the format string.
542  */
543 #define pr_err(fmt, ...) \
544 	printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
545 /**
546  * pr_warn - Print a warning-level message
547  * @fmt: format string
548  * @...: arguments for the format string
549  *
550  * This macro expands to a printk with KERN_WARNING loglevel. It uses pr_fmt()
551  * to generate the format string.
552  */
553 #define pr_warn(fmt, ...) \
554 	printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
555 /**
556  * pr_notice - Print a notice-level message
557  * @fmt: format string
558  * @...: arguments for the format string
559  *
560  * This macro expands to a printk with KERN_NOTICE loglevel. It uses pr_fmt() to
561  * generate the format string.
562  */
563 #define pr_notice(fmt, ...) \
564 	printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
565 /**
566  * pr_info - Print an info-level message
567  * @fmt: format string
568  * @...: arguments for the format string
569  *
570  * This macro expands to a printk with KERN_INFO loglevel. It uses pr_fmt() to
571  * generate the format string.
572  */
573 #define pr_info(fmt, ...) \
574 	printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
575 
576 /**
577  * pr_cont - Continues a previous log message in the same line.
578  * @fmt: format string
579  * @...: arguments for the format string
580  *
581  * This macro expands to a printk with KERN_CONT loglevel. It should only be
582  * used when continuing a log message with no newline ('\n') enclosed. Otherwise
583  * it defaults back to KERN_DEFAULT loglevel.
584  */
585 #define pr_cont(fmt, ...) \
586 	printk(KERN_CONT fmt, ##__VA_ARGS__)
587 
588 /**
589  * pr_devel - Print a debug-level message conditionally
590  * @fmt: format string
591  * @...: arguments for the format string
592  *
593  * This macro expands to a printk with KERN_DEBUG loglevel if DEBUG is
594  * defined. Otherwise it does nothing.
595  *
596  * It uses pr_fmt() to generate the format string.
597  */
598 #ifdef DEBUG
599 #define pr_devel(fmt, ...) \
600 	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
601 #else
602 #define pr_devel(fmt, ...) \
603 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
604 #endif
605 
606 
607 /* If you are writing a driver, please use dev_dbg instead */
608 #if defined(CONFIG_DYNAMIC_DEBUG) || \
609 	(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
610 #include <linux/dynamic_debug.h>
611 
612 /**
613  * pr_debug - Print a debug-level message conditionally
614  * @fmt: format string
615  * @...: arguments for the format string
616  *
617  * This macro expands to dynamic_pr_debug() if CONFIG_DYNAMIC_DEBUG is
618  * set. Otherwise, if DEBUG is defined, it's equivalent to a printk with
619  * KERN_DEBUG loglevel. If DEBUG is not defined it does nothing.
620  *
621  * It uses pr_fmt() to generate the format string (dynamic_pr_debug() uses
622  * pr_fmt() internally).
623  */
624 #define pr_debug(fmt, ...)			\
625 	dynamic_pr_debug(fmt, ##__VA_ARGS__)
626 #elif defined(DEBUG)
627 #define pr_debug(fmt, ...) \
628 	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
629 #else
630 #define pr_debug(fmt, ...) \
631 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
632 #endif
633 
634 /*
635  * Print a one-time message (analogous to WARN_ONCE() et al):
636  */
637 
638 #ifdef CONFIG_PRINTK
639 #define printk_once(fmt, ...)					\
640 	DO_ONCE_LITE(printk, fmt, ##__VA_ARGS__)
641 #define printk_deferred_once(fmt, ...)				\
642 	DO_ONCE_LITE(printk_deferred, fmt, ##__VA_ARGS__)
643 #else
644 #define printk_once(fmt, ...)					\
645 	no_printk(fmt, ##__VA_ARGS__)
646 #define printk_deferred_once(fmt, ...)				\
647 	no_printk(fmt, ##__VA_ARGS__)
648 #endif
649 
650 #define pr_emerg_once(fmt, ...)					\
651 	printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
652 #define pr_alert_once(fmt, ...)					\
653 	printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
654 #define pr_crit_once(fmt, ...)					\
655 	printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
656 #define pr_err_once(fmt, ...)					\
657 	printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
658 #define pr_warn_once(fmt, ...)					\
659 	printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
660 #define pr_notice_once(fmt, ...)				\
661 	printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
662 #define pr_info_once(fmt, ...)					\
663 	printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
664 /* no pr_cont_once, don't do that... */
665 
666 #if defined(DEBUG)
667 #define pr_devel_once(fmt, ...)					\
668 	printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
669 #else
670 #define pr_devel_once(fmt, ...)					\
671 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
672 #endif
673 
674 /* If you are writing a driver, please use dev_dbg instead */
675 #if defined(DEBUG)
676 #define pr_debug_once(fmt, ...)					\
677 	printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
678 #else
679 #define pr_debug_once(fmt, ...)					\
680 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
681 #endif
682 
683 /*
684  * ratelimited messages with local ratelimit_state,
685  * no local ratelimit_state used in the !PRINTK case
686  */
687 #ifdef CONFIG_PRINTK
688 #define printk_ratelimited(fmt, ...)					\
689 ({									\
690 	static DEFINE_RATELIMIT_STATE(_rs,				\
691 				      DEFAULT_RATELIMIT_INTERVAL,	\
692 				      DEFAULT_RATELIMIT_BURST);		\
693 									\
694 	if (__ratelimit(&_rs))						\
695 		printk(fmt, ##__VA_ARGS__);				\
696 })
697 #else
698 #define printk_ratelimited(fmt, ...)					\
699 	no_printk(fmt, ##__VA_ARGS__)
700 #endif
701 
702 #define pr_emerg_ratelimited(fmt, ...)					\
703 	printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
704 #define pr_alert_ratelimited(fmt, ...)					\
705 	printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
706 #define pr_crit_ratelimited(fmt, ...)					\
707 	printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
708 #define pr_err_ratelimited(fmt, ...)					\
709 	printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
710 #define pr_warn_ratelimited(fmt, ...)					\
711 	printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
712 #define pr_notice_ratelimited(fmt, ...)					\
713 	printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
714 #define pr_info_ratelimited(fmt, ...)					\
715 	printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
716 /* no pr_cont_ratelimited, don't do that... */
717 
718 #if defined(DEBUG)
719 #define pr_devel_ratelimited(fmt, ...)					\
720 	printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
721 #else
722 #define pr_devel_ratelimited(fmt, ...)					\
723 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
724 #endif
725 
726 /* If you are writing a driver, please use dev_dbg instead */
727 #if defined(CONFIG_DYNAMIC_DEBUG) || \
728 	(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
729 /* descriptor check is first to prevent flooding with "callbacks suppressed" */
730 #define pr_debug_ratelimited(fmt, ...)					\
731 do {									\
732 	static DEFINE_RATELIMIT_STATE(_rs,				\
733 				      DEFAULT_RATELIMIT_INTERVAL,	\
734 				      DEFAULT_RATELIMIT_BURST);		\
735 	DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt));		\
736 	if (DYNAMIC_DEBUG_BRANCH(descriptor) &&				\
737 	    __ratelimit(&_rs))						\
738 		__dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__);	\
739 } while (0)
740 #elif defined(DEBUG)
741 #define pr_debug_ratelimited(fmt, ...)					\
742 	printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
743 #else
744 #define pr_debug_ratelimited(fmt, ...) \
745 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
746 #endif
747 
748 extern const struct file_operations kmsg_fops;
749 
750 enum {
751 	DUMP_PREFIX_NONE,
752 	DUMP_PREFIX_ADDRESS,
753 	DUMP_PREFIX_OFFSET
754 };
755 extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
756 			      int groupsize, char *linebuf, size_t linebuflen,
757 			      bool ascii);
758 #ifdef CONFIG_PRINTK
759 extern void print_hex_dump(const char *level, const char *prefix_str,
760 			   int prefix_type, int rowsize, int groupsize,
761 			   const void *buf, size_t len, bool ascii);
762 #else
print_hex_dump(const char * level,const char * prefix_str,int prefix_type,int rowsize,int groupsize,const void * buf,size_t len,bool ascii)763 static inline void print_hex_dump(const char *level, const char *prefix_str,
764 				  int prefix_type, int rowsize, int groupsize,
765 				  const void *buf, size_t len, bool ascii)
766 {
767 }
print_hex_dump_bytes(const char * prefix_str,int prefix_type,const void * buf,size_t len)768 static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
769 					const void *buf, size_t len)
770 {
771 }
772 
773 #endif
774 
775 #if defined(CONFIG_DYNAMIC_DEBUG) || \
776 	(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
777 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize,	\
778 			     groupsize, buf, len, ascii)	\
779 	dynamic_hex_dump(prefix_str, prefix_type, rowsize,	\
780 			 groupsize, buf, len, ascii)
781 #elif defined(DEBUG)
782 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize,		\
783 			     groupsize, buf, len, ascii)		\
784 	print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize,	\
785 		       groupsize, buf, len, ascii)
786 #else
print_hex_dump_debug(const char * prefix_str,int prefix_type,int rowsize,int groupsize,const void * buf,size_t len,bool ascii)787 static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type,
788 					int rowsize, int groupsize,
789 					const void *buf, size_t len, bool ascii)
790 {
791 }
792 #endif
793 
794 /**
795  * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params
796  * @prefix_str: string to prefix each line with;
797  *  caller supplies trailing spaces for alignment if desired
798  * @prefix_type: controls whether prefix of an offset, address, or none
799  *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
800  * @buf: data blob to dump
801  * @len: number of bytes in the @buf
802  *
803  * Calls print_hex_dump(), with log level of KERN_DEBUG,
804  * rowsize of 16, groupsize of 1, and ASCII output included.
805  */
806 #define print_hex_dump_bytes(prefix_str, prefix_type, buf, len)	\
807 	print_hex_dump_debug(prefix_str, prefix_type, 16, 1, buf, len, true)
808 
809 #endif
810