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 /*
170  * The printk_deferred_enter/exit macros are available only as a hack for
171  * some code paths that need to defer all printk console printing. Interrupts
172  * must be disabled for the deferred duration.
173  */
174 #define printk_deferred_enter() __printk_deferred_enter()
175 #define printk_deferred_exit() __printk_deferred_exit()
176 
177 /*
178  * Please don't use printk_ratelimit(), because it shares ratelimiting state
179  * with all other unrelated printk_ratelimit() callsites.  Instead use
180  * printk_ratelimited() or plain old __ratelimit().
181  */
182 extern int __printk_ratelimit(const char *func);
183 #define printk_ratelimit() __printk_ratelimit(__func__)
184 extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
185 				   unsigned int interval_msec);
186 
187 extern int printk_delay_msec;
188 extern int dmesg_restrict;
189 
190 extern void wake_up_klogd(void);
191 
192 char *log_buf_addr_get(void);
193 u32 log_buf_len_get(void);
194 void log_buf_vmcoreinfo_setup(void);
195 void __init setup_log_buf(int early);
196 __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...);
197 void dump_stack_print_info(const char *log_lvl);
198 void show_regs_print_info(const char *log_lvl);
199 extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold;
200 extern asmlinkage void dump_stack(void) __cold;
201 void printk_trigger_flush(void);
202 void console_try_replay_all(void);
203 void printk_legacy_allow_panic_sync(void);
204 extern bool nbcon_device_try_acquire(struct console *con);
205 extern void nbcon_device_release(struct console *con);
206 void nbcon_atomic_flush_unsafe(void);
207 #else
208 static inline __printf(1, 0)
vprintk(const char * s,va_list args)209 int vprintk(const char *s, va_list args)
210 {
211 	return 0;
212 }
213 static inline __printf(1, 2) __cold
_printk(const char * s,...)214 int _printk(const char *s, ...)
215 {
216 	return 0;
217 }
218 static inline __printf(1, 2) __cold
_printk_deferred(const char * s,...)219 int _printk_deferred(const char *s, ...)
220 {
221 	return 0;
222 }
223 
printk_deferred_enter(void)224 static inline void printk_deferred_enter(void)
225 {
226 }
227 
printk_deferred_exit(void)228 static inline void printk_deferred_exit(void)
229 {
230 }
231 
printk_ratelimit(void)232 static inline int printk_ratelimit(void)
233 {
234 	return 0;
235 }
printk_timed_ratelimit(unsigned long * caller_jiffies,unsigned int interval_msec)236 static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
237 					  unsigned int interval_msec)
238 {
239 	return false;
240 }
241 
wake_up_klogd(void)242 static inline void wake_up_klogd(void)
243 {
244 }
245 
log_buf_addr_get(void)246 static inline char *log_buf_addr_get(void)
247 {
248 	return NULL;
249 }
250 
log_buf_len_get(void)251 static inline u32 log_buf_len_get(void)
252 {
253 	return 0;
254 }
255 
log_buf_vmcoreinfo_setup(void)256 static inline void log_buf_vmcoreinfo_setup(void)
257 {
258 }
259 
setup_log_buf(int early)260 static inline void setup_log_buf(int early)
261 {
262 }
263 
dump_stack_set_arch_desc(const char * fmt,...)264 static inline __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...)
265 {
266 }
267 
dump_stack_print_info(const char * log_lvl)268 static inline void dump_stack_print_info(const char *log_lvl)
269 {
270 }
271 
show_regs_print_info(const char * log_lvl)272 static inline void show_regs_print_info(const char *log_lvl)
273 {
274 }
275 
dump_stack_lvl(const char * log_lvl)276 static inline void dump_stack_lvl(const char *log_lvl)
277 {
278 }
279 
dump_stack(void)280 static inline void dump_stack(void)
281 {
282 }
printk_trigger_flush(void)283 static inline void printk_trigger_flush(void)
284 {
285 }
console_try_replay_all(void)286 static inline void console_try_replay_all(void)
287 {
288 }
289 
printk_legacy_allow_panic_sync(void)290 static inline void printk_legacy_allow_panic_sync(void)
291 {
292 }
293 
nbcon_device_try_acquire(struct console * con)294 static inline bool nbcon_device_try_acquire(struct console *con)
295 {
296 	return false;
297 }
298 
nbcon_device_release(struct console * con)299 static inline void nbcon_device_release(struct console *con)
300 {
301 }
302 
nbcon_atomic_flush_unsafe(void)303 static inline void nbcon_atomic_flush_unsafe(void)
304 {
305 }
306 
307 #endif
308 
309 bool this_cpu_in_panic(void);
310 
311 #ifdef CONFIG_SMP
312 extern int __printk_cpu_sync_try_get(void);
313 extern void __printk_cpu_sync_wait(void);
314 extern void __printk_cpu_sync_put(void);
315 
316 #else
317 
318 #define __printk_cpu_sync_try_get() true
319 #define __printk_cpu_sync_wait()
320 #define __printk_cpu_sync_put()
321 #endif /* CONFIG_SMP */
322 
323 /**
324  * printk_cpu_sync_get_irqsave() - Disable interrupts and acquire the printk
325  *                                 cpu-reentrant spinning lock.
326  * @flags: Stack-allocated storage for saving local interrupt state,
327  *         to be passed to printk_cpu_sync_put_irqrestore().
328  *
329  * If the lock is owned by another CPU, spin until it becomes available.
330  * Interrupts are restored while spinning.
331  *
332  * CAUTION: This function must be used carefully. It does not behave like a
333  * typical lock. Here are important things to watch out for...
334  *
335  *     * This function is reentrant on the same CPU. Therefore the calling
336  *       code must not assume exclusive access to data if code accessing the
337  *       data can run reentrant or within NMI context on the same CPU.
338  *
339  *     * If there exists usage of this function from NMI context, it becomes
340  *       unsafe to perform any type of locking or spinning to wait for other
341  *       CPUs after calling this function from any context. This includes
342  *       using spinlocks or any other busy-waiting synchronization methods.
343  */
344 #define printk_cpu_sync_get_irqsave(flags)		\
345 	for (;;) {					\
346 		local_irq_save(flags);			\
347 		if (__printk_cpu_sync_try_get())	\
348 			break;				\
349 		local_irq_restore(flags);		\
350 		__printk_cpu_sync_wait();		\
351 	}
352 
353 /**
354  * printk_cpu_sync_put_irqrestore() - Release the printk cpu-reentrant spinning
355  *                                    lock and restore interrupts.
356  * @flags: Caller's saved interrupt state, from printk_cpu_sync_get_irqsave().
357  */
358 #define printk_cpu_sync_put_irqrestore(flags)	\
359 	do {					\
360 		__printk_cpu_sync_put();	\
361 		local_irq_restore(flags);	\
362 	} while (0)
363 
364 extern int kptr_restrict;
365 
366 /**
367  * pr_fmt - used by the pr_*() macros to generate the printk format string
368  * @fmt: format string passed from a pr_*() macro
369  *
370  * This macro can be used to generate a unified format string for pr_*()
371  * macros. A common use is to prefix all pr_*() messages in a file with a common
372  * string. For example, defining this at the top of a source file:
373  *
374  *        #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
375  *
376  * would prefix all pr_info, pr_emerg... messages in the file with the module
377  * name.
378  */
379 #ifndef pr_fmt
380 #define pr_fmt(fmt) fmt
381 #endif
382 
383 struct module;
384 
385 #ifdef CONFIG_PRINTK_INDEX
386 struct pi_entry {
387 	const char *fmt;
388 	const char *func;
389 	const char *file;
390 	unsigned int line;
391 
392 	/*
393 	 * While printk and pr_* have the level stored in the string at compile
394 	 * time, some subsystems dynamically add it at runtime through the
395 	 * format string. For these dynamic cases, we allow the subsystem to
396 	 * tell us the level at compile time.
397 	 *
398 	 * NULL indicates that the level, if any, is stored in fmt.
399 	 */
400 	const char *level;
401 
402 	/*
403 	 * The format string used by various subsystem specific printk()
404 	 * wrappers to prefix the message.
405 	 *
406 	 * Note that the static prefix defined by the pr_fmt() macro is stored
407 	 * directly in the message format (@fmt), not here.
408 	 */
409 	const char *subsys_fmt_prefix;
410 } __packed;
411 
412 #define __printk_index_emit(_fmt, _level, _subsys_fmt_prefix)		\
413 	do {								\
414 		if (__builtin_constant_p(_fmt) && __builtin_constant_p(_level)) { \
415 			/*
416 			 * We check __builtin_constant_p multiple times here
417 			 * for the same input because GCC will produce an error
418 			 * if we try to assign a static variable to fmt if it
419 			 * is not a constant, even with the outer if statement.
420 			 */						\
421 			static const struct pi_entry _entry		\
422 			__used = {					\
423 				.fmt = __builtin_constant_p(_fmt) ? (_fmt) : NULL, \
424 				.func = __func__,			\
425 				.file = __FILE__,			\
426 				.line = __LINE__,			\
427 				.level = __builtin_constant_p(_level) ? (_level) : NULL, \
428 				.subsys_fmt_prefix = _subsys_fmt_prefix,\
429 			};						\
430 			static const struct pi_entry *_entry_ptr	\
431 			__used __section(".printk_index") = &_entry;	\
432 		}							\
433 	} while (0)
434 
435 #else /* !CONFIG_PRINTK_INDEX */
436 #define __printk_index_emit(...) do {} while (0)
437 #endif /* CONFIG_PRINTK_INDEX */
438 
439 /*
440  * Some subsystems have their own custom printk that applies a va_format to a
441  * generic format, for example, to include a device number or other metadata
442  * alongside the format supplied by the caller.
443  *
444  * In order to store these in the way they would be emitted by the printk
445  * infrastructure, the subsystem provides us with the start, fixed string, and
446  * any subsequent text in the format string.
447  *
448  * We take a variable argument list as pr_fmt/dev_fmt/etc are sometimes passed
449  * as multiple arguments (eg: `"%s: ", "blah"`), and we must only take the
450  * first one.
451  *
452  * subsys_fmt_prefix must be known at compile time, or compilation will fail
453  * (since this is a mistake). If fmt or level is not known at compile time, no
454  * index entry will be made (since this can legitimately happen).
455  */
456 #define printk_index_subsys_emit(subsys_fmt_prefix, level, fmt, ...) \
457 	__printk_index_emit(fmt, level, subsys_fmt_prefix)
458 
459 #define printk_index_wrap(_p_func, _fmt, ...)				\
460 	({								\
461 		__printk_index_emit(_fmt, NULL, NULL);			\
462 		_p_func(_fmt, ##__VA_ARGS__);				\
463 	})
464 
465 
466 /**
467  * printk - print a kernel message
468  * @fmt: format string
469  *
470  * This is printk(). It can be called from any context. We want it to work.
471  *
472  * If printk indexing is enabled, _printk() is called from printk_index_wrap.
473  * Otherwise, printk is simply #defined to _printk.
474  *
475  * We try to grab the console_lock. If we succeed, it's easy - we log the
476  * output and call the console drivers.  If we fail to get the semaphore, we
477  * place the output into the log buffer and return. The current holder of
478  * the console_sem will notice the new output in console_unlock(); and will
479  * send it to the consoles before releasing the lock.
480  *
481  * One effect of this deferred printing is that code which calls printk() and
482  * then changes console_loglevel may break. This is because console_loglevel
483  * is inspected when the actual printing occurs.
484  *
485  * See also:
486  * printf(3)
487  *
488  * See the vsnprintf() documentation for format string extensions over C99.
489  */
490 #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
491 #define printk_deferred(fmt, ...)					\
492 	printk_index_wrap(_printk_deferred, fmt, ##__VA_ARGS__)
493 
494 /**
495  * pr_emerg - Print an emergency-level message
496  * @fmt: format string
497  * @...: arguments for the format string
498  *
499  * This macro expands to a printk with KERN_EMERG loglevel. It uses pr_fmt() to
500  * generate the format string.
501  */
502 #define pr_emerg(fmt, ...) \
503 	printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
504 /**
505  * pr_alert - Print an alert-level message
506  * @fmt: format string
507  * @...: arguments for the format string
508  *
509  * This macro expands to a printk with KERN_ALERT loglevel. It uses pr_fmt() to
510  * generate the format string.
511  */
512 #define pr_alert(fmt, ...) \
513 	printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
514 /**
515  * pr_crit - Print a critical-level message
516  * @fmt: format string
517  * @...: arguments for the format string
518  *
519  * This macro expands to a printk with KERN_CRIT loglevel. It uses pr_fmt() to
520  * generate the format string.
521  */
522 #define pr_crit(fmt, ...) \
523 	printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
524 /**
525  * pr_err - Print an error-level message
526  * @fmt: format string
527  * @...: arguments for the format string
528  *
529  * This macro expands to a printk with KERN_ERR loglevel. It uses pr_fmt() to
530  * generate the format string.
531  */
532 #define pr_err(fmt, ...) \
533 	printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
534 /**
535  * pr_warn - Print a warning-level message
536  * @fmt: format string
537  * @...: arguments for the format string
538  *
539  * This macro expands to a printk with KERN_WARNING loglevel. It uses pr_fmt()
540  * to generate the format string.
541  */
542 #define pr_warn(fmt, ...) \
543 	printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
544 /**
545  * pr_notice - Print a notice-level message
546  * @fmt: format string
547  * @...: arguments for the format string
548  *
549  * This macro expands to a printk with KERN_NOTICE loglevel. It uses pr_fmt() to
550  * generate the format string.
551  */
552 #define pr_notice(fmt, ...) \
553 	printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
554 /**
555  * pr_info - Print an info-level message
556  * @fmt: format string
557  * @...: arguments for the format string
558  *
559  * This macro expands to a printk with KERN_INFO loglevel. It uses pr_fmt() to
560  * generate the format string.
561  */
562 #define pr_info(fmt, ...) \
563 	printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
564 
565 /**
566  * pr_cont - Continues a previous log message in the same line.
567  * @fmt: format string
568  * @...: arguments for the format string
569  *
570  * This macro expands to a printk with KERN_CONT loglevel. It should only be
571  * used when continuing a log message with no newline ('\n') enclosed. Otherwise
572  * it defaults back to KERN_DEFAULT loglevel.
573  */
574 #define pr_cont(fmt, ...) \
575 	printk(KERN_CONT fmt, ##__VA_ARGS__)
576 
577 /**
578  * pr_devel - Print a debug-level message conditionally
579  * @fmt: format string
580  * @...: arguments for the format string
581  *
582  * This macro expands to a printk with KERN_DEBUG loglevel if DEBUG is
583  * defined. Otherwise it does nothing.
584  *
585  * It uses pr_fmt() to generate the format string.
586  */
587 #ifdef DEBUG
588 #define pr_devel(fmt, ...) \
589 	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
590 #else
591 #define pr_devel(fmt, ...) \
592 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
593 #endif
594 
595 
596 /* If you are writing a driver, please use dev_dbg instead */
597 #if defined(CONFIG_DYNAMIC_DEBUG) || \
598 	(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
599 #include <linux/dynamic_debug.h>
600 
601 /**
602  * pr_debug - Print a debug-level message conditionally
603  * @fmt: format string
604  * @...: arguments for the format string
605  *
606  * This macro expands to dynamic_pr_debug() if CONFIG_DYNAMIC_DEBUG is
607  * set. Otherwise, if DEBUG is defined, it's equivalent to a printk with
608  * KERN_DEBUG loglevel. If DEBUG is not defined it does nothing.
609  *
610  * It uses pr_fmt() to generate the format string (dynamic_pr_debug() uses
611  * pr_fmt() internally).
612  */
613 #define pr_debug(fmt, ...)			\
614 	dynamic_pr_debug(fmt, ##__VA_ARGS__)
615 #elif defined(DEBUG)
616 #define pr_debug(fmt, ...) \
617 	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
618 #else
619 #define pr_debug(fmt, ...) \
620 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
621 #endif
622 
623 /*
624  * Print a one-time message (analogous to WARN_ONCE() et al):
625  */
626 
627 #ifdef CONFIG_PRINTK
628 #define printk_once(fmt, ...)					\
629 	DO_ONCE_LITE(printk, fmt, ##__VA_ARGS__)
630 #define printk_deferred_once(fmt, ...)				\
631 	DO_ONCE_LITE(printk_deferred, fmt, ##__VA_ARGS__)
632 #else
633 #define printk_once(fmt, ...)					\
634 	no_printk(fmt, ##__VA_ARGS__)
635 #define printk_deferred_once(fmt, ...)				\
636 	no_printk(fmt, ##__VA_ARGS__)
637 #endif
638 
639 #define pr_emerg_once(fmt, ...)					\
640 	printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
641 #define pr_alert_once(fmt, ...)					\
642 	printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
643 #define pr_crit_once(fmt, ...)					\
644 	printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
645 #define pr_err_once(fmt, ...)					\
646 	printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
647 #define pr_warn_once(fmt, ...)					\
648 	printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
649 #define pr_notice_once(fmt, ...)				\
650 	printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
651 #define pr_info_once(fmt, ...)					\
652 	printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
653 /* no pr_cont_once, don't do that... */
654 
655 #if defined(DEBUG)
656 #define pr_devel_once(fmt, ...)					\
657 	printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
658 #else
659 #define pr_devel_once(fmt, ...)					\
660 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
661 #endif
662 
663 /* If you are writing a driver, please use dev_dbg instead */
664 #if defined(DEBUG)
665 #define pr_debug_once(fmt, ...)					\
666 	printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
667 #else
668 #define pr_debug_once(fmt, ...)					\
669 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
670 #endif
671 
672 /*
673  * ratelimited messages with local ratelimit_state,
674  * no local ratelimit_state used in the !PRINTK case
675  */
676 #ifdef CONFIG_PRINTK
677 #define printk_ratelimited(fmt, ...)					\
678 ({									\
679 	static DEFINE_RATELIMIT_STATE(_rs,				\
680 				      DEFAULT_RATELIMIT_INTERVAL,	\
681 				      DEFAULT_RATELIMIT_BURST);		\
682 									\
683 	if (__ratelimit(&_rs))						\
684 		printk(fmt, ##__VA_ARGS__);				\
685 })
686 #else
687 #define printk_ratelimited(fmt, ...)					\
688 	no_printk(fmt, ##__VA_ARGS__)
689 #endif
690 
691 #define pr_emerg_ratelimited(fmt, ...)					\
692 	printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
693 #define pr_alert_ratelimited(fmt, ...)					\
694 	printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
695 #define pr_crit_ratelimited(fmt, ...)					\
696 	printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
697 #define pr_err_ratelimited(fmt, ...)					\
698 	printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
699 #define pr_warn_ratelimited(fmt, ...)					\
700 	printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
701 #define pr_notice_ratelimited(fmt, ...)					\
702 	printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
703 #define pr_info_ratelimited(fmt, ...)					\
704 	printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
705 /* no pr_cont_ratelimited, don't do that... */
706 
707 #if defined(DEBUG)
708 #define pr_devel_ratelimited(fmt, ...)					\
709 	printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
710 #else
711 #define pr_devel_ratelimited(fmt, ...)					\
712 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
713 #endif
714 
715 /* If you are writing a driver, please use dev_dbg instead */
716 #if defined(CONFIG_DYNAMIC_DEBUG) || \
717 	(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
718 /* descriptor check is first to prevent flooding with "callbacks suppressed" */
719 #define pr_debug_ratelimited(fmt, ...)					\
720 do {									\
721 	static DEFINE_RATELIMIT_STATE(_rs,				\
722 				      DEFAULT_RATELIMIT_INTERVAL,	\
723 				      DEFAULT_RATELIMIT_BURST);		\
724 	DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt));		\
725 	if (DYNAMIC_DEBUG_BRANCH(descriptor) &&				\
726 	    __ratelimit(&_rs))						\
727 		__dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__);	\
728 } while (0)
729 #elif defined(DEBUG)
730 #define pr_debug_ratelimited(fmt, ...)					\
731 	printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
732 #else
733 #define pr_debug_ratelimited(fmt, ...) \
734 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
735 #endif
736 
737 extern const struct file_operations kmsg_fops;
738 
739 enum {
740 	DUMP_PREFIX_NONE,
741 	DUMP_PREFIX_ADDRESS,
742 	DUMP_PREFIX_OFFSET
743 };
744 extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
745 			      int groupsize, char *linebuf, size_t linebuflen,
746 			      bool ascii);
747 #ifdef CONFIG_PRINTK
748 extern void print_hex_dump(const char *level, const char *prefix_str,
749 			   int prefix_type, int rowsize, int groupsize,
750 			   const void *buf, size_t len, bool ascii);
751 #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)752 static inline void print_hex_dump(const char *level, const char *prefix_str,
753 				  int prefix_type, int rowsize, int groupsize,
754 				  const void *buf, size_t len, bool ascii)
755 {
756 }
print_hex_dump_bytes(const char * prefix_str,int prefix_type,const void * buf,size_t len)757 static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
758 					const void *buf, size_t len)
759 {
760 }
761 
762 #endif
763 
764 #if defined(CONFIG_DYNAMIC_DEBUG) || \
765 	(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
766 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize,	\
767 			     groupsize, buf, len, ascii)	\
768 	dynamic_hex_dump(prefix_str, prefix_type, rowsize,	\
769 			 groupsize, buf, len, ascii)
770 #elif defined(DEBUG)
771 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize,		\
772 			     groupsize, buf, len, ascii)		\
773 	print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize,	\
774 		       groupsize, buf, len, ascii)
775 #else
print_hex_dump_debug(const char * prefix_str,int prefix_type,int rowsize,int groupsize,const void * buf,size_t len,bool ascii)776 static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type,
777 					int rowsize, int groupsize,
778 					const void *buf, size_t len, bool ascii)
779 {
780 }
781 #endif
782 
783 /**
784  * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params
785  * @prefix_str: string to prefix each line with;
786  *  caller supplies trailing spaces for alignment if desired
787  * @prefix_type: controls whether prefix of an offset, address, or none
788  *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
789  * @buf: data blob to dump
790  * @len: number of bytes in the @buf
791  *
792  * Calls print_hex_dump(), with log level of KERN_DEBUG,
793  * rowsize of 16, groupsize of 1, and ASCII output included.
794  */
795 #define print_hex_dump_bytes(prefix_str, prefix_type, buf, len)	\
796 	print_hex_dump_debug(prefix_str, prefix_type, 16, 1, buf, len, true)
797 
798 #endif
799