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