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 #define PRINTK_MAX_SINGLE_HEADER_LEN 2
17 static inline int printk_get_level(const char *buffer)
19 if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
22 case 'c': /* KERN_CONT */
29 static inline const char *printk_skip_level(const char *buffer)
31 if (printk_get_level(buffer))
37 static inline const char *printk_skip_headers(const char *buffer)
39 while (printk_get_level(buffer))
40 buffer = printk_skip_level(buffer);
45 #define CONSOLE_EXT_LOG_MAX 8192
47 /* printk's without a loglevel use this.. */
48 #define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT
50 /* We show everything that is MORE important than this.. */
51 #define CONSOLE_LOGLEVEL_SILENT 0 /* Mum's the word */
52 #define CONSOLE_LOGLEVEL_MIN 1 /* Minimum loglevel we let people use */
53 #define CONSOLE_LOGLEVEL_DEBUG 10 /* issue debug messages */
54 #define CONSOLE_LOGLEVEL_MOTORMOUTH 15 /* You can't shut this one up */
57 * Default used to be hard-coded at 7, quiet used to be hardcoded at 4,
58 * we're now allowing both to be set from kernel config.
60 #define CONSOLE_LOGLEVEL_DEFAULT CONFIG_CONSOLE_LOGLEVEL_DEFAULT
61 #define CONSOLE_LOGLEVEL_QUIET CONFIG_CONSOLE_LOGLEVEL_QUIET
63 extern int console_printk[];
65 #define console_loglevel (console_printk[0])
66 #define default_message_loglevel (console_printk[1])
67 #define minimum_console_loglevel (console_printk[2])
68 #define default_console_loglevel (console_printk[3])
70 static inline void console_silent(void)
72 console_loglevel = CONSOLE_LOGLEVEL_SILENT;
75 static inline void console_verbose(void)
78 console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
81 /* strlen("ratelimit") + 1 */
82 #define DEVKMSG_STR_MAX_SIZE 10
83 extern char devkmsg_log_str[];
86 extern int suppress_printk;
95 * Add this to a message where you are sure the firmware is buggy or behaves
96 * really stupid or out of spec. Be aware that the responsible BIOS developer
97 * should be able to fix this issue or at least get a concrete idea of the
98 * problem by reading your message without the need of looking at the kernel
101 * Use it for definite and high priority BIOS bugs.
104 * Use it for not that clear (e.g. could the kernel messed up things already?)
105 * and medium priority BIOS bugs.
108 * Use this one if you want to tell the user or vendor about something
109 * suspicious, but generally harmless related to the firmware.
111 * Use it for information or very low priority BIOS bugs.
113 #define FW_BUG "[Firmware Bug]: "
114 #define FW_WARN "[Firmware Warn]: "
115 #define FW_INFO "[Firmware Info]: "
119 * Add this to a message for hardware errors, so that user can report
120 * it to hardware vendor instead of LKML or software vendor.
122 #define HW_ERR "[Hardware Error]: "
126 * Add this to a message whenever you want to warn user space about the use
127 * of a deprecated aspect of an API so they can stop using it
129 #define DEPRECATED "[Deprecated]: "
132 * Dummy printk for disabled debugging statements to use whilst maintaining
133 * gcc's format checking.
135 #define no_printk(fmt, ...) \
138 printk(fmt, ##__VA_ARGS__); \
142 #ifdef CONFIG_EARLY_PRINTK
143 extern asmlinkage __printf(1, 2)
144 void early_printk(const char *fmt, ...);
146 static inline __printf(1, 2) __cold
147 void early_printk(const char *s, ...) { }
150 #ifdef CONFIG_PRINTK_NMI
151 extern void printk_nmi_enter(void);
152 extern void printk_nmi_exit(void);
153 extern void printk_nmi_direct_enter(void);
154 extern void printk_nmi_direct_exit(void);
156 static inline void printk_nmi_enter(void) { }
157 static inline void printk_nmi_exit(void) { }
158 static inline void printk_nmi_direct_enter(void) { }
159 static inline void printk_nmi_direct_exit(void) { }
160 #endif /* PRINTK_NMI */
163 asmlinkage __printf(5, 0)
164 int vprintk_emit(int facility, int level,
165 const char *dict, size_t dictlen,
166 const char *fmt, va_list args);
168 asmlinkage __printf(1, 0)
169 int vprintk(const char *fmt, va_list args);
171 asmlinkage __printf(1, 2) __cold
172 int printk(const char *fmt, ...);
175 * Special printk facility for scheduler/timekeeping use only, _DO_NOT_USE_ !
177 __printf(1, 2) __cold int printk_deferred(const char *fmt, ...);
180 * Please don't use printk_ratelimit(), because it shares ratelimiting state
181 * with all other unrelated printk_ratelimit() callsites. Instead use
182 * printk_ratelimited() or plain old __ratelimit().
184 extern int __printk_ratelimit(const char *func);
185 #define printk_ratelimit() __printk_ratelimit(__func__)
186 extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
187 unsigned int interval_msec);
189 extern int printk_delay_msec;
190 extern int dmesg_restrict;
193 devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, void *buf,
194 size_t *lenp, loff_t *ppos);
196 extern void wake_up_klogd(void);
198 char *log_buf_addr_get(void);
199 u32 log_buf_len_get(void);
200 void log_buf_vmcoreinfo_setup(void);
201 void __init setup_log_buf(int early);
202 __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...);
203 void dump_stack_print_info(const char *log_lvl);
204 void show_regs_print_info(const char *log_lvl);
205 extern asmlinkage void dump_stack(void) __cold;
206 extern void printk_safe_flush(void);
207 extern void printk_safe_flush_on_panic(void);
209 static inline __printf(1, 0)
210 int vprintk(const char *s, va_list args)
214 static inline __printf(1, 2) __cold
215 int printk(const char *s, ...)
219 static inline __printf(1, 2) __cold
220 int printk_deferred(const char *s, ...)
224 static inline int printk_ratelimit(void)
228 static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
229 unsigned int interval_msec)
234 static inline void wake_up_klogd(void)
238 static inline char *log_buf_addr_get(void)
243 static inline u32 log_buf_len_get(void)
248 static inline void log_buf_vmcoreinfo_setup(void)
252 static inline void setup_log_buf(int early)
256 static inline __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...)
260 static inline void dump_stack_print_info(const char *log_lvl)
264 static inline void show_regs_print_info(const char *log_lvl)
268 static inline void dump_stack(void)
272 static inline void printk_safe_flush(void)
276 static inline void printk_safe_flush_on_panic(void)
281 extern int kptr_restrict;
284 * pr_fmt - used by the pr_*() macros to generate the printk format string
285 * @fmt: format string passed from a pr_*() macro
287 * This macro can be used to generate a unified format string for pr_*()
288 * macros. A common use is to prefix all pr_*() messages in a file with a common
289 * string. For example, defining this at the top of a source file:
291 * #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
293 * would prefix all pr_info, pr_emerg... messages in the file with the module
297 #define pr_fmt(fmt) fmt
301 * pr_emerg - Print an emergency-level message
302 * @fmt: format string
303 * @...: arguments for the format string
305 * This macro expands to a printk with KERN_EMERG loglevel. It uses pr_fmt() to
306 * generate the format string.
308 #define pr_emerg(fmt, ...) \
309 printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
311 * pr_alert - Print an alert-level message
312 * @fmt: format string
313 * @...: arguments for the format string
315 * This macro expands to a printk with KERN_ALERT loglevel. It uses pr_fmt() to
316 * generate the format string.
318 #define pr_alert(fmt, ...) \
319 printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
321 * pr_crit - Print a critical-level message
322 * @fmt: format string
323 * @...: arguments for the format string
325 * This macro expands to a printk with KERN_CRIT loglevel. It uses pr_fmt() to
326 * generate the format string.
328 #define pr_crit(fmt, ...) \
329 printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
331 * pr_err - Print an error-level message
332 * @fmt: format string
333 * @...: arguments for the format string
335 * This macro expands to a printk with KERN_ERR loglevel. It uses pr_fmt() to
336 * generate the format string.
338 #define pr_err(fmt, ...) \
339 printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
341 * pr_warn - Print a warning-level message
342 * @fmt: format string
343 * @...: arguments for the format string
345 * This macro expands to a printk with KERN_WARNING loglevel. It uses pr_fmt()
346 * to generate the format string.
348 #define pr_warn(fmt, ...) \
349 printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
351 * pr_notice - Print a notice-level message
352 * @fmt: format string
353 * @...: arguments for the format string
355 * This macro expands to a printk with KERN_NOTICE loglevel. It uses pr_fmt() to
356 * generate the format string.
358 #define pr_notice(fmt, ...) \
359 printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
361 * pr_info - Print an info-level message
362 * @fmt: format string
363 * @...: arguments for the format string
365 * This macro expands to a printk with KERN_INFO loglevel. It uses pr_fmt() to
366 * generate the format string.
368 #define pr_info(fmt, ...) \
369 printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
372 * pr_cont - Continues a previous log message in the same line.
373 * @fmt: format string
374 * @...: arguments for the format string
376 * This macro expands to a printk with KERN_CONT loglevel. It should only be
377 * used when continuing a log message with no newline ('\n') enclosed. Otherwise
378 * it defaults back to KERN_DEFAULT loglevel.
380 #define pr_cont(fmt, ...) \
381 printk(KERN_CONT fmt, ##__VA_ARGS__)
384 * pr_devel - Print a debug-level message conditionally
385 * @fmt: format string
386 * @...: arguments for the format string
388 * This macro expands to a printk with KERN_DEBUG loglevel if DEBUG is
389 * defined. Otherwise it does nothing.
391 * It uses pr_fmt() to generate the format string.
394 #define pr_devel(fmt, ...) \
395 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
397 #define pr_devel(fmt, ...) \
398 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
402 /* If you are writing a driver, please use dev_dbg instead */
403 #if defined(CONFIG_DYNAMIC_DEBUG) || \
404 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
405 #include <linux/dynamic_debug.h>
408 * pr_debug - Print a debug-level message conditionally
409 * @fmt: format string
410 * @...: arguments for the format string
412 * This macro expands to dynamic_pr_debug() if CONFIG_DYNAMIC_DEBUG is
413 * set. Otherwise, if DEBUG is defined, it's equivalent to a printk with
414 * KERN_DEBUG loglevel. If DEBUG is not defined it does nothing.
416 * It uses pr_fmt() to generate the format string (dynamic_pr_debug() uses
417 * pr_fmt() internally).
419 #define pr_debug(fmt, ...) \
420 dynamic_pr_debug(fmt, ##__VA_ARGS__)
422 #define pr_debug(fmt, ...) \
423 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
425 #define pr_debug(fmt, ...) \
426 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
430 * Print a one-time message (analogous to WARN_ONCE() et al):
434 #define printk_once(fmt, ...) \
436 static bool __section(.data.once) __print_once; \
437 bool __ret_print_once = !__print_once; \
439 if (!__print_once) { \
440 __print_once = true; \
441 printk(fmt, ##__VA_ARGS__); \
443 unlikely(__ret_print_once); \
445 #define printk_deferred_once(fmt, ...) \
447 static bool __section(.data.once) __print_once; \
448 bool __ret_print_once = !__print_once; \
450 if (!__print_once) { \
451 __print_once = true; \
452 printk_deferred(fmt, ##__VA_ARGS__); \
454 unlikely(__ret_print_once); \
457 #define printk_once(fmt, ...) \
458 no_printk(fmt, ##__VA_ARGS__)
459 #define printk_deferred_once(fmt, ...) \
460 no_printk(fmt, ##__VA_ARGS__)
463 #define pr_emerg_once(fmt, ...) \
464 printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
465 #define pr_alert_once(fmt, ...) \
466 printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
467 #define pr_crit_once(fmt, ...) \
468 printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
469 #define pr_err_once(fmt, ...) \
470 printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
471 #define pr_warn_once(fmt, ...) \
472 printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
473 #define pr_notice_once(fmt, ...) \
474 printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
475 #define pr_info_once(fmt, ...) \
476 printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
477 /* no pr_cont_once, don't do that... */
480 #define pr_devel_once(fmt, ...) \
481 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
483 #define pr_devel_once(fmt, ...) \
484 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
487 /* If you are writing a driver, please use dev_dbg instead */
489 #define pr_debug_once(fmt, ...) \
490 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
492 #define pr_debug_once(fmt, ...) \
493 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
497 * ratelimited messages with local ratelimit_state,
498 * no local ratelimit_state used in the !PRINTK case
501 #define printk_ratelimited(fmt, ...) \
503 static DEFINE_RATELIMIT_STATE(_rs, \
504 DEFAULT_RATELIMIT_INTERVAL, \
505 DEFAULT_RATELIMIT_BURST); \
507 if (__ratelimit(&_rs)) \
508 printk(fmt, ##__VA_ARGS__); \
511 #define printk_ratelimited(fmt, ...) \
512 no_printk(fmt, ##__VA_ARGS__)
515 #define pr_emerg_ratelimited(fmt, ...) \
516 printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
517 #define pr_alert_ratelimited(fmt, ...) \
518 printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
519 #define pr_crit_ratelimited(fmt, ...) \
520 printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
521 #define pr_err_ratelimited(fmt, ...) \
522 printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
523 #define pr_warn_ratelimited(fmt, ...) \
524 printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
525 #define pr_notice_ratelimited(fmt, ...) \
526 printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
527 #define pr_info_ratelimited(fmt, ...) \
528 printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
529 /* no pr_cont_ratelimited, don't do that... */
532 #define pr_devel_ratelimited(fmt, ...) \
533 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
535 #define pr_devel_ratelimited(fmt, ...) \
536 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
539 /* If you are writing a driver, please use dev_dbg instead */
540 #if defined(CONFIG_DYNAMIC_DEBUG) || \
541 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
542 /* descriptor check is first to prevent flooding with "callbacks suppressed" */
543 #define pr_debug_ratelimited(fmt, ...) \
545 static DEFINE_RATELIMIT_STATE(_rs, \
546 DEFAULT_RATELIMIT_INTERVAL, \
547 DEFAULT_RATELIMIT_BURST); \
548 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt)); \
549 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
551 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__); \
554 #define pr_debug_ratelimited(fmt, ...) \
555 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
557 #define pr_debug_ratelimited(fmt, ...) \
558 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
561 extern const struct file_operations kmsg_fops;
568 extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
569 int groupsize, char *linebuf, size_t linebuflen,
572 extern void print_hex_dump(const char *level, const char *prefix_str,
573 int prefix_type, int rowsize, int groupsize,
574 const void *buf, size_t len, bool ascii);
576 static inline void print_hex_dump(const char *level, const char *prefix_str,
577 int prefix_type, int rowsize, int groupsize,
578 const void *buf, size_t len, bool ascii)
581 static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
582 const void *buf, size_t len)
588 #if defined(CONFIG_DYNAMIC_DEBUG) || \
589 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
590 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
591 groupsize, buf, len, ascii) \
592 dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
593 groupsize, buf, len, ascii)
595 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
596 groupsize, buf, len, ascii) \
597 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize, \
598 groupsize, buf, len, ascii)
600 static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type,
601 int rowsize, int groupsize,
602 const void *buf, size_t len, bool ascii)
608 * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params
609 * @prefix_str: string to prefix each line with;
610 * caller supplies trailing spaces for alignment if desired
611 * @prefix_type: controls whether prefix of an offset, address, or none
612 * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
613 * @buf: data blob to dump
614 * @len: number of bytes in the @buf
616 * Calls print_hex_dump(), with log level of KERN_DEBUG,
617 * rowsize of 16, groupsize of 1, and ASCII output included.
619 #define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
620 print_hex_dump_debug(prefix_str, prefix_type, 16, 1, buf, len, true)