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