1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KERNEL_PRINTK__
3 #define __KERNEL_PRINTK__
6 #include <linux/init.h>
7 #include <linux/kern_levels.h>
8 #include <linux/linkage.h>
9 #include <linux/cache.h>
10 #include <linux/ratelimit_types.h>
12 extern const char linux_banner[];
13 extern const char linux_proc_banner[];
15 extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */
17 #define PRINTK_MAX_SINGLE_HEADER_LEN 2
19 static inline int printk_get_level(const char *buffer)
21 if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
24 case 'c': /* KERN_CONT */
31 static inline const char *printk_skip_level(const char *buffer)
33 if (printk_get_level(buffer))
39 static inline const char *printk_skip_headers(const char *buffer)
41 while (printk_get_level(buffer))
42 buffer = printk_skip_level(buffer);
47 #define CONSOLE_EXT_LOG_MAX 8192
49 /* printk's without a loglevel use this.. */
50 #define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT
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 */
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.
62 #define CONSOLE_LOGLEVEL_DEFAULT CONFIG_CONSOLE_LOGLEVEL_DEFAULT
63 #define CONSOLE_LOGLEVEL_QUIET CONFIG_CONSOLE_LOGLEVEL_QUIET
65 extern int console_printk[];
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])
72 static inline void console_silent(void)
74 console_loglevel = CONSOLE_LOGLEVEL_SILENT;
77 static inline void console_verbose(void)
80 console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
83 /* strlen("ratelimit") + 1 */
84 #define DEVKMSG_STR_MAX_SIZE 10
85 extern char devkmsg_log_str[];
88 extern int suppress_printk;
97 * Add this to a message where you are sure the firmware is buggy or behaves
98 * really stupid or out of spec. Be aware that the responsible BIOS developer
99 * should be able to fix this issue or at least get a concrete idea of the
100 * problem by reading your message without the need of looking at the kernel
103 * Use it for definite and high priority BIOS bugs.
106 * Use it for not that clear (e.g. could the kernel messed up things already?)
107 * and medium priority BIOS bugs.
110 * Use this one if you want to tell the user or vendor about something
111 * suspicious, but generally harmless related to the firmware.
113 * Use it for information or very low priority BIOS bugs.
115 #define FW_BUG "[Firmware Bug]: "
116 #define FW_WARN "[Firmware Warn]: "
117 #define FW_INFO "[Firmware Info]: "
121 * Add this to a message for hardware errors, so that user can report
122 * it to hardware vendor instead of LKML or software vendor.
124 #define HW_ERR "[Hardware Error]: "
128 * Add this to a message whenever you want to warn user space about the use
129 * of a deprecated aspect of an API so they can stop using it
131 #define DEPRECATED "[Deprecated]: "
134 * Dummy printk for disabled debugging statements to use whilst maintaining
135 * gcc's format checking.
137 #define no_printk(fmt, ...) \
140 printk(fmt, ##__VA_ARGS__); \
144 #ifdef CONFIG_EARLY_PRINTK
145 extern asmlinkage __printf(1, 2)
146 void early_printk(const char *fmt, ...);
148 static inline __printf(1, 2) __cold
149 void early_printk(const char *s, ...) { }
152 #ifdef CONFIG_PRINTK_NMI
153 extern void printk_nmi_enter(void);
154 extern void printk_nmi_exit(void);
155 extern void printk_nmi_direct_enter(void);
156 extern void printk_nmi_direct_exit(void);
158 static inline void printk_nmi_enter(void) { }
159 static inline void printk_nmi_exit(void) { }
160 static inline void printk_nmi_direct_enter(void) { }
161 static inline void printk_nmi_direct_exit(void) { }
162 #endif /* PRINTK_NMI */
164 struct dev_printk_info;
167 asmlinkage __printf(4, 0)
168 int vprintk_emit(int facility, int level,
169 const struct dev_printk_info *dev_info,
170 const char *fmt, va_list args);
172 asmlinkage __printf(1, 0)
173 int vprintk(const char *fmt, va_list args);
175 asmlinkage __printf(1, 2) __cold
176 int printk(const char *fmt, ...);
179 * Special printk facility for scheduler/timekeeping use only, _DO_NOT_USE_ !
181 __printf(1, 2) __cold int printk_deferred(const char *fmt, ...);
184 * Please don't use printk_ratelimit(), because it shares ratelimiting state
185 * with all other unrelated printk_ratelimit() callsites. Instead use
186 * printk_ratelimited() or plain old __ratelimit().
188 extern int __printk_ratelimit(const char *func);
189 #define printk_ratelimit() __printk_ratelimit(__func__)
190 extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
191 unsigned int interval_msec);
193 extern int printk_delay_msec;
194 extern int dmesg_restrict;
197 devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, void *buf,
198 size_t *lenp, loff_t *ppos);
200 extern void wake_up_klogd(void);
202 char *log_buf_addr_get(void);
203 u32 log_buf_len_get(void);
204 void log_buf_vmcoreinfo_setup(void);
205 void __init setup_log_buf(int early);
206 __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...);
207 void dump_stack_print_info(const char *log_lvl);
208 void show_regs_print_info(const char *log_lvl);
209 extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold;
210 extern asmlinkage void dump_stack(void) __cold;
211 extern void printk_safe_flush(void);
212 extern void printk_safe_flush_on_panic(void);
214 static inline __printf(1, 0)
215 int vprintk(const char *s, va_list args)
219 static inline __printf(1, 2) __cold
220 int printk(const char *s, ...)
224 static inline __printf(1, 2) __cold
225 int printk_deferred(const char *s, ...)
229 static inline int printk_ratelimit(void)
233 static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
234 unsigned int interval_msec)
239 static inline void wake_up_klogd(void)
243 static inline char *log_buf_addr_get(void)
248 static inline u32 log_buf_len_get(void)
253 static inline void log_buf_vmcoreinfo_setup(void)
257 static inline void setup_log_buf(int early)
261 static inline __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...)
265 static inline void dump_stack_print_info(const char *log_lvl)
269 static inline void show_regs_print_info(const char *log_lvl)
273 static inline void dump_stack_lvl(const char *log_lvl)
277 static inline void dump_stack(void)
281 static inline void printk_safe_flush(void)
285 static inline void printk_safe_flush_on_panic(void)
291 extern int __printk_cpu_trylock(void);
292 extern void __printk_wait_on_cpu_lock(void);
293 extern void __printk_cpu_unlock(void);
296 * printk_cpu_lock_irqsave() - Acquire the printk cpu-reentrant spinning
297 * lock and disable interrupts.
298 * @flags: Stack-allocated storage for saving local interrupt state,
299 * to be passed to printk_cpu_unlock_irqrestore().
301 * If the lock is owned by another CPU, spin until it becomes available.
302 * Interrupts are restored while spinning.
304 #define printk_cpu_lock_irqsave(flags) \
306 local_irq_save(flags); \
307 if (__printk_cpu_trylock()) \
309 local_irq_restore(flags); \
310 __printk_wait_on_cpu_lock(); \
314 * printk_cpu_unlock_irqrestore() - Release the printk cpu-reentrant spinning
315 * lock and restore interrupts.
316 * @flags: Caller's saved interrupt state, from printk_cpu_lock_irqsave().
318 #define printk_cpu_unlock_irqrestore(flags) \
320 __printk_cpu_unlock(); \
321 local_irq_restore(flags); \
326 #define printk_cpu_lock_irqsave(flags) ((void)flags)
327 #define printk_cpu_unlock_irqrestore(flags) ((void)flags)
329 #endif /* CONFIG_SMP */
331 extern int kptr_restrict;
334 * pr_fmt - used by the pr_*() macros to generate the printk format string
335 * @fmt: format string passed from a pr_*() macro
337 * This macro can be used to generate a unified format string for pr_*()
338 * macros. A common use is to prefix all pr_*() messages in a file with a common
339 * string. For example, defining this at the top of a source file:
341 * #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
343 * would prefix all pr_info, pr_emerg... messages in the file with the module
347 #define pr_fmt(fmt) fmt
351 * pr_emerg - Print an emergency-level message
352 * @fmt: format string
353 * @...: arguments for the format string
355 * This macro expands to a printk with KERN_EMERG loglevel. It uses pr_fmt() to
356 * generate the format string.
358 #define pr_emerg(fmt, ...) \
359 printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
361 * pr_alert - Print an alert-level message
362 * @fmt: format string
363 * @...: arguments for the format string
365 * This macro expands to a printk with KERN_ALERT loglevel. It uses pr_fmt() to
366 * generate the format string.
368 #define pr_alert(fmt, ...) \
369 printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
371 * pr_crit - Print a critical-level message
372 * @fmt: format string
373 * @...: arguments for the format string
375 * This macro expands to a printk with KERN_CRIT loglevel. It uses pr_fmt() to
376 * generate the format string.
378 #define pr_crit(fmt, ...) \
379 printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
381 * pr_err - Print an error-level message
382 * @fmt: format string
383 * @...: arguments for the format string
385 * This macro expands to a printk with KERN_ERR loglevel. It uses pr_fmt() to
386 * generate the format string.
388 #define pr_err(fmt, ...) \
389 printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
391 * pr_warn - Print a warning-level message
392 * @fmt: format string
393 * @...: arguments for the format string
395 * This macro expands to a printk with KERN_WARNING loglevel. It uses pr_fmt()
396 * to generate the format string.
398 #define pr_warn(fmt, ...) \
399 printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
401 * pr_notice - Print a notice-level message
402 * @fmt: format string
403 * @...: arguments for the format string
405 * This macro expands to a printk with KERN_NOTICE loglevel. It uses pr_fmt() to
406 * generate the format string.
408 #define pr_notice(fmt, ...) \
409 printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
411 * pr_info - Print an info-level message
412 * @fmt: format string
413 * @...: arguments for the format string
415 * This macro expands to a printk with KERN_INFO loglevel. It uses pr_fmt() to
416 * generate the format string.
418 #define pr_info(fmt, ...) \
419 printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
422 * pr_cont - Continues a previous log message in the same line.
423 * @fmt: format string
424 * @...: arguments for the format string
426 * This macro expands to a printk with KERN_CONT loglevel. It should only be
427 * used when continuing a log message with no newline ('\n') enclosed. Otherwise
428 * it defaults back to KERN_DEFAULT loglevel.
430 #define pr_cont(fmt, ...) \
431 printk(KERN_CONT fmt, ##__VA_ARGS__)
434 * pr_devel - Print a debug-level message conditionally
435 * @fmt: format string
436 * @...: arguments for the format string
438 * This macro expands to a printk with KERN_DEBUG loglevel if DEBUG is
439 * defined. Otherwise it does nothing.
441 * It uses pr_fmt() to generate the format string.
444 #define pr_devel(fmt, ...) \
445 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
447 #define pr_devel(fmt, ...) \
448 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
452 /* If you are writing a driver, please use dev_dbg instead */
453 #if defined(CONFIG_DYNAMIC_DEBUG) || \
454 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
455 #include <linux/dynamic_debug.h>
458 * pr_debug - Print a debug-level message conditionally
459 * @fmt: format string
460 * @...: arguments for the format string
462 * This macro expands to dynamic_pr_debug() if CONFIG_DYNAMIC_DEBUG is
463 * set. Otherwise, if DEBUG is defined, it's equivalent to a printk with
464 * KERN_DEBUG loglevel. If DEBUG is not defined it does nothing.
466 * It uses pr_fmt() to generate the format string (dynamic_pr_debug() uses
467 * pr_fmt() internally).
469 #define pr_debug(fmt, ...) \
470 dynamic_pr_debug(fmt, ##__VA_ARGS__)
472 #define pr_debug(fmt, ...) \
473 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
475 #define pr_debug(fmt, ...) \
476 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
480 * Print a one-time message (analogous to WARN_ONCE() et al):
484 #define printk_once(fmt, ...) \
486 static bool __section(".data.once") __print_once; \
487 bool __ret_print_once = !__print_once; \
489 if (!__print_once) { \
490 __print_once = true; \
491 printk(fmt, ##__VA_ARGS__); \
493 unlikely(__ret_print_once); \
495 #define printk_deferred_once(fmt, ...) \
497 static bool __section(".data.once") __print_once; \
498 bool __ret_print_once = !__print_once; \
500 if (!__print_once) { \
501 __print_once = true; \
502 printk_deferred(fmt, ##__VA_ARGS__); \
504 unlikely(__ret_print_once); \
507 #define printk_once(fmt, ...) \
508 no_printk(fmt, ##__VA_ARGS__)
509 #define printk_deferred_once(fmt, ...) \
510 no_printk(fmt, ##__VA_ARGS__)
513 #define pr_emerg_once(fmt, ...) \
514 printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
515 #define pr_alert_once(fmt, ...) \
516 printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
517 #define pr_crit_once(fmt, ...) \
518 printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
519 #define pr_err_once(fmt, ...) \
520 printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
521 #define pr_warn_once(fmt, ...) \
522 printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
523 #define pr_notice_once(fmt, ...) \
524 printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
525 #define pr_info_once(fmt, ...) \
526 printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
527 /* no pr_cont_once, don't do that... */
530 #define pr_devel_once(fmt, ...) \
531 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
533 #define pr_devel_once(fmt, ...) \
534 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
537 /* If you are writing a driver, please use dev_dbg instead */
539 #define pr_debug_once(fmt, ...) \
540 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
542 #define pr_debug_once(fmt, ...) \
543 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
547 * ratelimited messages with local ratelimit_state,
548 * no local ratelimit_state used in the !PRINTK case
551 #define printk_ratelimited(fmt, ...) \
553 static DEFINE_RATELIMIT_STATE(_rs, \
554 DEFAULT_RATELIMIT_INTERVAL, \
555 DEFAULT_RATELIMIT_BURST); \
557 if (__ratelimit(&_rs)) \
558 printk(fmt, ##__VA_ARGS__); \
561 #define printk_ratelimited(fmt, ...) \
562 no_printk(fmt, ##__VA_ARGS__)
565 #define pr_emerg_ratelimited(fmt, ...) \
566 printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
567 #define pr_alert_ratelimited(fmt, ...) \
568 printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
569 #define pr_crit_ratelimited(fmt, ...) \
570 printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
571 #define pr_err_ratelimited(fmt, ...) \
572 printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
573 #define pr_warn_ratelimited(fmt, ...) \
574 printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
575 #define pr_notice_ratelimited(fmt, ...) \
576 printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
577 #define pr_info_ratelimited(fmt, ...) \
578 printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
579 /* no pr_cont_ratelimited, don't do that... */
582 #define pr_devel_ratelimited(fmt, ...) \
583 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
585 #define pr_devel_ratelimited(fmt, ...) \
586 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
589 /* If you are writing a driver, please use dev_dbg instead */
590 #if defined(CONFIG_DYNAMIC_DEBUG) || \
591 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
592 /* descriptor check is first to prevent flooding with "callbacks suppressed" */
593 #define pr_debug_ratelimited(fmt, ...) \
595 static DEFINE_RATELIMIT_STATE(_rs, \
596 DEFAULT_RATELIMIT_INTERVAL, \
597 DEFAULT_RATELIMIT_BURST); \
598 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt)); \
599 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
601 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__); \
604 #define pr_debug_ratelimited(fmt, ...) \
605 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
607 #define pr_debug_ratelimited(fmt, ...) \
608 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
611 extern const struct file_operations kmsg_fops;
618 extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
619 int groupsize, char *linebuf, size_t linebuflen,
622 extern void print_hex_dump(const char *level, const char *prefix_str,
623 int prefix_type, int rowsize, int groupsize,
624 const void *buf, size_t len, bool ascii);
626 static inline void print_hex_dump(const char *level, const char *prefix_str,
627 int prefix_type, int rowsize, int groupsize,
628 const void *buf, size_t len, bool ascii)
631 static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
632 const void *buf, size_t len)
638 #if defined(CONFIG_DYNAMIC_DEBUG) || \
639 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
640 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
641 groupsize, buf, len, ascii) \
642 dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
643 groupsize, buf, len, ascii)
645 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
646 groupsize, buf, len, ascii) \
647 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize, \
648 groupsize, buf, len, ascii)
650 static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type,
651 int rowsize, int groupsize,
652 const void *buf, size_t len, bool ascii)
658 * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params
659 * @prefix_str: string to prefix each line with;
660 * caller supplies trailing spaces for alignment if desired
661 * @prefix_type: controls whether prefix of an offset, address, or none
662 * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
663 * @buf: data blob to dump
664 * @len: number of bytes in the @buf
666 * Calls print_hex_dump(), with log level of KERN_DEBUG,
667 * rowsize of 16, groupsize of 1, and ASCII output included.
669 #define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
670 print_hex_dump_debug(prefix_str, prefix_type, 16, 1, buf, len, true)