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(void) __cold;
210 extern void printk_safe_flush(void);
211 extern void printk_safe_flush_on_panic(void);
213 static inline __printf(1, 0)
214 int vprintk(const char *s, va_list args)
218 static inline __printf(1, 2) __cold
219 int printk(const char *s, ...)
223 static inline __printf(1, 2) __cold
224 int printk_deferred(const char *s, ...)
228 static inline int printk_ratelimit(void)
232 static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
233 unsigned int interval_msec)
238 static inline void wake_up_klogd(void)
242 static inline char *log_buf_addr_get(void)
247 static inline u32 log_buf_len_get(void)
252 static inline void log_buf_vmcoreinfo_setup(void)
256 static inline void setup_log_buf(int early)
260 static inline __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...)
264 static inline void dump_stack_print_info(const char *log_lvl)
268 static inline void show_regs_print_info(const char *log_lvl)
272 static inline void dump_stack(void)
276 static inline void printk_safe_flush(void)
280 static inline void printk_safe_flush_on_panic(void)
285 extern int kptr_restrict;
288 * pr_fmt - used by the pr_*() macros to generate the printk format string
289 * @fmt: format string passed from a pr_*() macro
291 * This macro can be used to generate a unified format string for pr_*()
292 * macros. A common use is to prefix all pr_*() messages in a file with a common
293 * string. For example, defining this at the top of a source file:
295 * #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
297 * would prefix all pr_info, pr_emerg... messages in the file with the module
301 #define pr_fmt(fmt) fmt
305 * pr_emerg - Print an emergency-level message
306 * @fmt: format string
307 * @...: arguments for the format string
309 * This macro expands to a printk with KERN_EMERG loglevel. It uses pr_fmt() to
310 * generate the format string.
312 #define pr_emerg(fmt, ...) \
313 printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
315 * pr_alert - Print an alert-level message
316 * @fmt: format string
317 * @...: arguments for the format string
319 * This macro expands to a printk with KERN_ALERT loglevel. It uses pr_fmt() to
320 * generate the format string.
322 #define pr_alert(fmt, ...) \
323 printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
325 * pr_crit - Print a critical-level message
326 * @fmt: format string
327 * @...: arguments for the format string
329 * This macro expands to a printk with KERN_CRIT loglevel. It uses pr_fmt() to
330 * generate the format string.
332 #define pr_crit(fmt, ...) \
333 printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
335 * pr_err - Print an error-level message
336 * @fmt: format string
337 * @...: arguments for the format string
339 * This macro expands to a printk with KERN_ERR loglevel. It uses pr_fmt() to
340 * generate the format string.
342 #define pr_err(fmt, ...) \
343 printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
345 * pr_warn - Print a warning-level message
346 * @fmt: format string
347 * @...: arguments for the format string
349 * This macro expands to a printk with KERN_WARNING loglevel. It uses pr_fmt()
350 * to generate the format string.
352 #define pr_warn(fmt, ...) \
353 printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
355 * pr_notice - Print a notice-level message
356 * @fmt: format string
357 * @...: arguments for the format string
359 * This macro expands to a printk with KERN_NOTICE loglevel. It uses pr_fmt() to
360 * generate the format string.
362 #define pr_notice(fmt, ...) \
363 printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
365 * pr_info - Print an info-level message
366 * @fmt: format string
367 * @...: arguments for the format string
369 * This macro expands to a printk with KERN_INFO loglevel. It uses pr_fmt() to
370 * generate the format string.
372 #define pr_info(fmt, ...) \
373 printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
376 * pr_cont - Continues a previous log message in the same line.
377 * @fmt: format string
378 * @...: arguments for the format string
380 * This macro expands to a printk with KERN_CONT loglevel. It should only be
381 * used when continuing a log message with no newline ('\n') enclosed. Otherwise
382 * it defaults back to KERN_DEFAULT loglevel.
384 #define pr_cont(fmt, ...) \
385 printk(KERN_CONT fmt, ##__VA_ARGS__)
388 * pr_devel - Print a debug-level message conditionally
389 * @fmt: format string
390 * @...: arguments for the format string
392 * This macro expands to a printk with KERN_DEBUG loglevel if DEBUG is
393 * defined. Otherwise it does nothing.
395 * It uses pr_fmt() to generate the format string.
398 #define pr_devel(fmt, ...) \
399 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
401 #define pr_devel(fmt, ...) \
402 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
406 /* If you are writing a driver, please use dev_dbg instead */
407 #if defined(CONFIG_DYNAMIC_DEBUG) || \
408 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
409 #include <linux/dynamic_debug.h>
412 * pr_debug - Print a debug-level message conditionally
413 * @fmt: format string
414 * @...: arguments for the format string
416 * This macro expands to dynamic_pr_debug() if CONFIG_DYNAMIC_DEBUG is
417 * set. Otherwise, if DEBUG is defined, it's equivalent to a printk with
418 * KERN_DEBUG loglevel. If DEBUG is not defined it does nothing.
420 * It uses pr_fmt() to generate the format string (dynamic_pr_debug() uses
421 * pr_fmt() internally).
423 #define pr_debug(fmt, ...) \
424 dynamic_pr_debug(fmt, ##__VA_ARGS__)
426 #define pr_debug(fmt, ...) \
427 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
429 #define pr_debug(fmt, ...) \
430 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
434 * Print a one-time message (analogous to WARN_ONCE() et al):
438 #define printk_once(fmt, ...) \
440 static bool __section(".data.once") __print_once; \
441 bool __ret_print_once = !__print_once; \
443 if (!__print_once) { \
444 __print_once = true; \
445 printk(fmt, ##__VA_ARGS__); \
447 unlikely(__ret_print_once); \
449 #define printk_deferred_once(fmt, ...) \
451 static bool __section(".data.once") __print_once; \
452 bool __ret_print_once = !__print_once; \
454 if (!__print_once) { \
455 __print_once = true; \
456 printk_deferred(fmt, ##__VA_ARGS__); \
458 unlikely(__ret_print_once); \
461 #define printk_once(fmt, ...) \
462 no_printk(fmt, ##__VA_ARGS__)
463 #define printk_deferred_once(fmt, ...) \
464 no_printk(fmt, ##__VA_ARGS__)
467 #define pr_emerg_once(fmt, ...) \
468 printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
469 #define pr_alert_once(fmt, ...) \
470 printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
471 #define pr_crit_once(fmt, ...) \
472 printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
473 #define pr_err_once(fmt, ...) \
474 printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
475 #define pr_warn_once(fmt, ...) \
476 printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
477 #define pr_notice_once(fmt, ...) \
478 printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
479 #define pr_info_once(fmt, ...) \
480 printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
481 /* no pr_cont_once, don't do that... */
484 #define pr_devel_once(fmt, ...) \
485 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
487 #define pr_devel_once(fmt, ...) \
488 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
491 /* If you are writing a driver, please use dev_dbg instead */
493 #define pr_debug_once(fmt, ...) \
494 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
496 #define pr_debug_once(fmt, ...) \
497 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
501 * ratelimited messages with local ratelimit_state,
502 * no local ratelimit_state used in the !PRINTK case
505 #define printk_ratelimited(fmt, ...) \
507 static DEFINE_RATELIMIT_STATE(_rs, \
508 DEFAULT_RATELIMIT_INTERVAL, \
509 DEFAULT_RATELIMIT_BURST); \
511 if (__ratelimit(&_rs)) \
512 printk(fmt, ##__VA_ARGS__); \
515 #define printk_ratelimited(fmt, ...) \
516 no_printk(fmt, ##__VA_ARGS__)
519 #define pr_emerg_ratelimited(fmt, ...) \
520 printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
521 #define pr_alert_ratelimited(fmt, ...) \
522 printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
523 #define pr_crit_ratelimited(fmt, ...) \
524 printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
525 #define pr_err_ratelimited(fmt, ...) \
526 printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
527 #define pr_warn_ratelimited(fmt, ...) \
528 printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
529 #define pr_notice_ratelimited(fmt, ...) \
530 printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
531 #define pr_info_ratelimited(fmt, ...) \
532 printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
533 /* no pr_cont_ratelimited, don't do that... */
536 #define pr_devel_ratelimited(fmt, ...) \
537 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
539 #define pr_devel_ratelimited(fmt, ...) \
540 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
543 /* If you are writing a driver, please use dev_dbg instead */
544 #if defined(CONFIG_DYNAMIC_DEBUG) || \
545 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
546 /* descriptor check is first to prevent flooding with "callbacks suppressed" */
547 #define pr_debug_ratelimited(fmt, ...) \
549 static DEFINE_RATELIMIT_STATE(_rs, \
550 DEFAULT_RATELIMIT_INTERVAL, \
551 DEFAULT_RATELIMIT_BURST); \
552 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt)); \
553 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
555 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__); \
558 #define pr_debug_ratelimited(fmt, ...) \
559 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
561 #define pr_debug_ratelimited(fmt, ...) \
562 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
565 extern const struct file_operations kmsg_fops;
572 extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
573 int groupsize, char *linebuf, size_t linebuflen,
576 extern 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);
580 static inline void print_hex_dump(const char *level, const char *prefix_str,
581 int prefix_type, int rowsize, int groupsize,
582 const void *buf, size_t len, bool ascii)
585 static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
586 const void *buf, size_t len)
592 #if defined(CONFIG_DYNAMIC_DEBUG) || \
593 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
594 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
595 groupsize, buf, len, ascii) \
596 dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
597 groupsize, buf, len, ascii)
599 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
600 groupsize, buf, len, ascii) \
601 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize, \
602 groupsize, buf, len, ascii)
604 static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type,
605 int rowsize, int groupsize,
606 const void *buf, size_t len, bool ascii)
612 * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params
613 * @prefix_str: string to prefix each line with;
614 * caller supplies trailing spaces for alignment if desired
615 * @prefix_type: controls whether prefix of an offset, address, or none
616 * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
617 * @buf: data blob to dump
618 * @len: number of bytes in the @buf
620 * Calls print_hex_dump(), with log level of KERN_DEBUG,
621 * rowsize of 16, groupsize of 1, and ASCII output included.
623 #define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
624 print_hex_dump_debug(prefix_str, prefix_type, 16, 1, buf, len, true)