blob: 2651b82ed35299bab55c69c0cba5aa7362929090 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds968ab182010-11-15 13:37:37 -08002#ifndef __KERNEL_PRINTK__
3#define __KERNEL_PRINTK__
4
Andrew Morton1b2c2892013-04-29 16:17:19 -07005#include <stdarg.h>
Mike Travis162a7e72011-05-24 17:13:20 -07006#include <linux/init.h>
Joe Perches314ba352012-07-30 14:40:11 -07007#include <linux/kern_levels.h>
Ralf Baechle154c2672013-05-21 10:51:10 +02008#include <linux/linkage.h>
Joe Perchesc28aa1f02014-01-23 15:54:16 -08009#include <linux/cache.h>
Herbert Xub4a461e2020-07-21 16:22:48 +100010#include <linux/ratelimit_types.h>
Tanner Lovea358f402021-06-28 09:50:06 -040011#include <linux/once_lite.h>
Mike Travis162a7e72011-05-24 17:13:20 -070012
Linus Torvalds968ab182010-11-15 13:37:37 -080013extern const char linux_banner[];
14extern const char linux_proc_banner[];
15
Andy Shevchenko36d818f2020-09-11 20:02:02 +030016extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */
17
Petr Mladek262c5e82016-12-12 16:45:50 -080018#define PRINTK_MAX_SINGLE_HEADER_LEN 2
19
Joe Perchesacc8fa412012-07-30 14:40:09 -070020static inline int printk_get_level(const char *buffer)
21{
Joe Perches04d2c8c2012-07-30 14:40:17 -070022 if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
Joe Perchesacc8fa412012-07-30 14:40:09 -070023 switch (buffer[1]) {
24 case '0' ... '7':
Linus Torvalds4bcc5952016-10-08 20:32:40 -070025 case 'c': /* KERN_CONT */
Joe Perchesacc8fa412012-07-30 14:40:09 -070026 return buffer[1];
27 }
28 }
29 return 0;
30}
31
32static inline const char *printk_skip_level(const char *buffer)
33{
Petr Mladek01856982014-04-03 14:48:38 -070034 if (printk_get_level(buffer))
35 return buffer + 2;
36
Joe Perchesacc8fa412012-07-30 14:40:09 -070037 return buffer;
38}
39
Petr Mladek49795752016-12-12 16:45:47 -080040static inline const char *printk_skip_headers(const char *buffer)
41{
42 while (printk_get_level(buffer))
43 buffer = printk_skip_level(buffer);
44
45 return buffer;
46}
47
Tejun Heod43ff432015-06-25 15:01:24 -070048#define CONSOLE_EXT_LOG_MAX 8192
49
Borislav Petkova8fe19e2014-06-04 16:11:46 -070050/* printk's without a loglevel use this.. */
Alex Elder42a9dc02014-08-06 16:09:01 -070051#define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT
Borislav Petkova8fe19e2014-06-04 16:11:46 -070052
53/* We show everything that is MORE important than this.. */
54#define CONSOLE_LOGLEVEL_SILENT 0 /* Mum's the word */
55#define CONSOLE_LOGLEVEL_MIN 1 /* Minimum loglevel we let people use */
Borislav Petkova8fe19e2014-06-04 16:11:46 -070056#define CONSOLE_LOGLEVEL_DEBUG 10 /* issue debug messages */
57#define CONSOLE_LOGLEVEL_MOTORMOUTH 15 /* You can't shut this one up */
58
Olof Johanssona8cfdc62016-12-12 16:45:56 -080059/*
Hans de Goede22eceb82018-06-19 13:57:26 +020060 * Default used to be hard-coded at 7, quiet used to be hardcoded at 4,
61 * we're now allowing both to be set from kernel config.
Olof Johanssona8cfdc62016-12-12 16:45:56 -080062 */
63#define CONSOLE_LOGLEVEL_DEFAULT CONFIG_CONSOLE_LOGLEVEL_DEFAULT
Hans de Goede22eceb82018-06-19 13:57:26 +020064#define CONSOLE_LOGLEVEL_QUIET CONFIG_CONSOLE_LOGLEVEL_QUIET
Olof Johanssona8cfdc62016-12-12 16:45:56 -080065
Linus Torvalds968ab182010-11-15 13:37:37 -080066extern int console_printk[];
67
68#define console_loglevel (console_printk[0])
69#define default_message_loglevel (console_printk[1])
70#define minimum_console_loglevel (console_printk[2])
71#define default_console_loglevel (console_printk[3])
72
Joe Perchesa9747cc32011-01-12 16:59:43 -080073static inline void console_silent(void)
74{
Borislav Petkova8fe19e2014-06-04 16:11:46 -070075 console_loglevel = CONSOLE_LOGLEVEL_SILENT;
Joe Perchesa9747cc32011-01-12 16:59:43 -080076}
77
78static inline void console_verbose(void)
79{
80 if (console_loglevel)
Borislav Petkova8fe19e2014-06-04 16:11:46 -070081 console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
Joe Perchesa9747cc32011-01-12 16:59:43 -080082}
83
Borislav Petkov750afe72016-08-02 14:04:07 -070084/* strlen("ratelimit") + 1 */
85#define DEVKMSG_STR_MAX_SIZE 10
86extern char devkmsg_log_str[];
87struct ctl_table;
88
Feng Tangc39ea0b2019-05-14 15:45:34 -070089extern int suppress_printk;
90
Linus Torvalds968ab182010-11-15 13:37:37 -080091struct va_format {
92 const char *fmt;
93 va_list *va;
94};
95
96/*
97 * FW_BUG
98 * Add this to a message where you are sure the firmware is buggy or behaves
99 * really stupid or out of spec. Be aware that the responsible BIOS developer
100 * should be able to fix this issue or at least get a concrete idea of the
101 * problem by reading your message without the need of looking at the kernel
102 * code.
103 *
104 * Use it for definite and high priority BIOS bugs.
105 *
106 * FW_WARN
107 * Use it for not that clear (e.g. could the kernel messed up things already?)
108 * and medium priority BIOS bugs.
109 *
110 * FW_INFO
111 * Use this one if you want to tell the user or vendor about something
112 * suspicious, but generally harmless related to the firmware.
113 *
114 * Use it for information or very low priority BIOS bugs.
115 */
116#define FW_BUG "[Firmware Bug]: "
117#define FW_WARN "[Firmware Warn]: "
118#define FW_INFO "[Firmware Info]: "
119
120/*
121 * HW_ERR
122 * Add this to a message for hardware errors, so that user can report
123 * it to hardware vendor instead of LKML or software vendor.
124 */
125#define HW_ERR "[Hardware Error]: "
126
Joe Perches5264f2f2011-01-12 16:59:45 -0800127/*
Neil Horman0a9a8bf2013-12-23 08:29:42 -0500128 * DEPRECATED
129 * Add this to a message whenever you want to warn user space about the use
130 * of a deprecated aspect of an API so they can stop using it
131 */
132#define DEPRECATED "[Deprecated]: "
133
134/*
Joe Perches5264f2f2011-01-12 16:59:45 -0800135 * Dummy printk for disabled debugging statements to use whilst maintaining
Aaron Conolefe22cd9b2016-01-15 16:59:12 -0800136 * gcc's format checking.
Joe Perches5264f2f2011-01-12 16:59:45 -0800137 */
Borislav Petkov069f0cd2016-07-05 00:31:26 +0200138#define no_printk(fmt, ...) \
139({ \
Masahiro Yamada93b138d2017-09-18 00:01:44 +0900140 if (0) \
141 printk(fmt, ##__VA_ARGS__); \
Borislav Petkov069f0cd2016-07-05 00:31:26 +0200142 0; \
143})
Joe Perches5264f2f2011-01-12 16:59:45 -0800144
Thomas Gleixnerd0380e62013-04-29 16:17:18 -0700145#ifdef CONFIG_EARLY_PRINTK
Joe Perchesb9075fa2011-10-31 17:11:33 -0700146extern asmlinkage __printf(1, 2)
Joe Perches5264f2f2011-01-12 16:59:45 -0800147void early_printk(const char *fmt, ...);
Thomas Gleixnerd0380e62013-04-29 16:17:18 -0700148#else
149static inline __printf(1, 2) __cold
150void early_printk(const char *s, ...) { }
151#endif
Joe Perches5264f2f2011-01-12 16:59:45 -0800152
Petr Mladek42a0bb32016-05-20 17:00:33 -0700153#ifdef CONFIG_PRINTK_NMI
Petr Mladek42a0bb32016-05-20 17:00:33 -0700154extern void printk_nmi_enter(void);
155extern void printk_nmi_exit(void);
Petr Mladek03fc7f92018-06-27 16:20:28 +0200156extern void printk_nmi_direct_enter(void);
157extern void printk_nmi_direct_exit(void);
Petr Mladek42a0bb32016-05-20 17:00:33 -0700158#else
Petr Mladek42a0bb32016-05-20 17:00:33 -0700159static inline void printk_nmi_enter(void) { }
160static inline void printk_nmi_exit(void) { }
Petr Mladek03fc7f92018-06-27 16:20:28 +0200161static inline void printk_nmi_direct_enter(void) { }
162static inline void printk_nmi_direct_exit(void) { }
Petr Mladek42a0bb32016-05-20 17:00:33 -0700163#endif /* PRINTK_NMI */
Steven Rostedt (Red Hat)04b74b22014-11-21 09:16:58 -0500164
John Ogness74caba72020-09-21 13:24:45 +0206165struct dev_printk_info;
166
Linus Torvalds968ab182010-11-15 13:37:37 -0800167#ifdef CONFIG_PRINTK
John Ogness74caba72020-09-21 13:24:45 +0206168asmlinkage __printf(4, 0)
Kay Sievers7ff95542012-05-03 02:29:13 +0200169int vprintk_emit(int facility, int level,
John Ogness74caba72020-09-21 13:24:45 +0206170 const struct dev_printk_info *dev_info,
Kay Sievers7ff95542012-05-03 02:29:13 +0200171 const char *fmt, va_list args);
172
Joe Perchesb9075fa2011-10-31 17:11:33 -0700173asmlinkage __printf(1, 0)
Joe Perches5264f2f2011-01-12 16:59:45 -0800174int vprintk(const char *fmt, va_list args);
Kay Sievers7ff95542012-05-03 02:29:13 +0200175
Joe Perchesb9075fa2011-10-31 17:11:33 -0700176asmlinkage __printf(1, 2) __cold
Chris Down33701552021-06-15 17:52:53 +0100177int _printk(const char *fmt, ...);
Linus Torvalds968ab182010-11-15 13:37:37 -0800178
179/*
John Stultzaac74dc2014-06-04 16:11:40 -0700180 * Special printk facility for scheduler/timekeeping use only, _DO_NOT_USE_ !
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +0100181 */
Chris Down33701552021-06-15 17:52:53 +0100182__printf(1, 2) __cold int _printk_deferred(const char *fmt, ...);
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +0100183
184/*
Linus Torvalds968ab182010-11-15 13:37:37 -0800185 * Please don't use printk_ratelimit(), because it shares ratelimiting state
186 * with all other unrelated printk_ratelimit() callsites. Instead use
187 * printk_ratelimited() or plain old __ratelimit().
188 */
189extern int __printk_ratelimit(const char *func);
190#define printk_ratelimit() __printk_ratelimit(__func__)
191extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
192 unsigned int interval_msec);
193
194extern int printk_delay_msec;
195extern int dmesg_restrict;
Linus Torvalds968ab182010-11-15 13:37:37 -0800196
Borislav Petkov750afe72016-08-02 14:04:07 -0700197extern int
Christoph Hellwig32927392020-04-24 08:43:38 +0200198devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, void *buf,
Borislav Petkov750afe72016-08-02 14:04:07 -0700199 size_t *lenp, loff_t *ppos);
200
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -0700201extern void wake_up_klogd(void);
202
Pranith Kumar07261ed2015-01-26 12:58:43 -0800203char *log_buf_addr_get(void);
204u32 log_buf_len_get(void);
Hari Bathini692f66f2017-05-08 15:56:18 -0700205void log_buf_vmcoreinfo_setup(void);
Mike Travis162a7e72011-05-24 17:13:20 -0700206void __init setup_log_buf(int early);
Nicolas Iooss8db14862015-07-17 16:23:42 -0700207__printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...);
Tejun Heo196779b2013-04-30 15:27:12 -0700208void dump_stack_print_info(const char *log_lvl);
Tejun Heoa43cb952013-04-30 15:27:17 -0700209void show_regs_print_info(const char *log_lvl);
Alexander Potapenko4469c0f2021-06-28 19:40:30 -0700210extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold;
Dave Younge36df282018-02-13 15:28:34 +0800211extern asmlinkage void dump_stack(void) __cold;
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900212extern void printk_safe_flush(void);
213extern void printk_safe_flush_on_panic(void);
Linus Torvalds968ab182010-11-15 13:37:37 -0800214#else
Joe Perchesb9075fa2011-10-31 17:11:33 -0700215static inline __printf(1, 0)
Joe Perches5264f2f2011-01-12 16:59:45 -0800216int vprintk(const char *s, va_list args)
217{
218 return 0;
219}
Joe Perchesb9075fa2011-10-31 17:11:33 -0700220static inline __printf(1, 2) __cold
Chris Down33701552021-06-15 17:52:53 +0100221int _printk(const char *s, ...)
Joe Perches5264f2f2011-01-12 16:59:45 -0800222{
223 return 0;
224}
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +0100225static inline __printf(1, 2) __cold
Chris Down33701552021-06-15 17:52:53 +0100226int _printk_deferred(const char *s, ...)
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +0100227{
228 return 0;
229}
Joe Perches5264f2f2011-01-12 16:59:45 -0800230static inline int printk_ratelimit(void)
231{
232 return 0;
233}
234static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
235 unsigned int interval_msec)
236{
237 return false;
238}
Linus Torvalds968ab182010-11-15 13:37:37 -0800239
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -0700240static inline void wake_up_klogd(void)
241{
242}
243
Pranith Kumar07261ed2015-01-26 12:58:43 -0800244static inline char *log_buf_addr_get(void)
245{
246 return NULL;
247}
248
249static inline u32 log_buf_len_get(void)
250{
251 return 0;
252}
253
Hari Bathini692f66f2017-05-08 15:56:18 -0700254static inline void log_buf_vmcoreinfo_setup(void)
Linus Torvalds968ab182010-11-15 13:37:37 -0800255{
256}
Mike Travis162a7e72011-05-24 17:13:20 -0700257
258static inline void setup_log_buf(int early)
259{
260}
Tejun Heo196779b2013-04-30 15:27:12 -0700261
Nicolas Iooss8db14862015-07-17 16:23:42 -0700262static inline __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...)
Tejun Heo98e5e1b2013-04-30 15:27:15 -0700263{
264}
265
Tejun Heo196779b2013-04-30 15:27:12 -0700266static inline void dump_stack_print_info(const char *log_lvl)
267{
268}
Tejun Heoa43cb952013-04-30 15:27:17 -0700269
270static inline void show_regs_print_info(const char *log_lvl)
271{
272}
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900273
Alexander Potapenko4469c0f2021-06-28 19:40:30 -0700274static inline void dump_stack_lvl(const char *log_lvl)
275{
276}
277
Alexey Dobriyane6310f02019-01-03 15:26:37 -0800278static inline void dump_stack(void)
Dave Younge36df282018-02-13 15:28:34 +0800279{
280}
281
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900282static inline void printk_safe_flush(void)
283{
284}
285
286static inline void printk_safe_flush_on_panic(void)
287{
288}
Linus Torvalds968ab182010-11-15 13:37:37 -0800289#endif
290
John Ogness766c2682021-06-17 11:56:50 +0206291#ifdef CONFIG_SMP
292extern int __printk_cpu_trylock(void);
293extern void __printk_wait_on_cpu_lock(void);
294extern void __printk_cpu_unlock(void);
295
296/**
297 * printk_cpu_lock_irqsave() - Acquire the printk cpu-reentrant spinning
298 * lock and disable interrupts.
299 * @flags: Stack-allocated storage for saving local interrupt state,
300 * to be passed to printk_cpu_unlock_irqrestore().
301 *
302 * If the lock is owned by another CPU, spin until it becomes available.
303 * Interrupts are restored while spinning.
304 */
305#define printk_cpu_lock_irqsave(flags) \
306 for (;;) { \
307 local_irq_save(flags); \
308 if (__printk_cpu_trylock()) \
309 break; \
310 local_irq_restore(flags); \
311 __printk_wait_on_cpu_lock(); \
312 }
313
314/**
315 * printk_cpu_unlock_irqrestore() - Release the printk cpu-reentrant spinning
316 * lock and restore interrupts.
317 * @flags: Caller's saved interrupt state, from printk_cpu_lock_irqsave().
318 */
319#define printk_cpu_unlock_irqrestore(flags) \
320 do { \
321 __printk_cpu_unlock(); \
322 local_irq_restore(flags); \
323 } while (0) \
324
325#else
326
327#define printk_cpu_lock_irqsave(flags) ((void)flags)
328#define printk_cpu_unlock_irqrestore(flags) ((void)flags)
329
330#endif /* CONFIG_SMP */
331
Arnd Bergmann01c313d2017-11-13 17:50:59 +0100332extern int kptr_restrict;
333
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200334/**
335 * pr_fmt - used by the pr_*() macros to generate the printk format string
336 * @fmt: format string passed from a pr_*() macro
337 *
338 * This macro can be used to generate a unified format string for pr_*()
339 * macros. A common use is to prefix all pr_*() messages in a file with a common
340 * string. For example, defining this at the top of a source file:
341 *
342 * #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
343 *
344 * would prefix all pr_info, pr_emerg... messages in the file with the module
345 * name.
346 */
Linus Torvalds968ab182010-11-15 13:37:37 -0800347#ifndef pr_fmt
348#define pr_fmt(fmt) fmt
349#endif
350
Chris Down33701552021-06-15 17:52:53 +0100351struct module;
352
353#ifdef CONFIG_PRINTK_INDEX
354struct pi_entry {
355 const char *fmt;
356 const char *func;
357 const char *file;
358 unsigned int line;
359
360 /*
361 * While printk and pr_* have the level stored in the string at compile
362 * time, some subsystems dynamically add it at runtime through the
363 * format string. For these dynamic cases, we allow the subsystem to
364 * tell us the level at compile time.
365 *
366 * NULL indicates that the level, if any, is stored in fmt.
367 */
368 const char *level;
369
370 /*
371 * The format string used by various subsystem specific printk()
372 * wrappers to prefix the message.
373 *
374 * Note that the static prefix defined by the pr_fmt() macro is stored
375 * directly in the message format (@fmt), not here.
376 */
377 const char *subsys_fmt_prefix;
378} __packed;
379
380#define __printk_index_emit(_fmt, _level, _subsys_fmt_prefix) \
381 do { \
382 if (__builtin_constant_p(_fmt) && __builtin_constant_p(_level)) { \
383 /*
384 * We check __builtin_constant_p multiple times here
385 * for the same input because GCC will produce an error
386 * if we try to assign a static variable to fmt if it
387 * is not a constant, even with the outer if statement.
388 */ \
389 static const struct pi_entry _entry \
390 __used = { \
391 .fmt = __builtin_constant_p(_fmt) ? (_fmt) : NULL, \
392 .func = __func__, \
393 .file = __FILE__, \
394 .line = __LINE__, \
395 .level = __builtin_constant_p(_level) ? (_level) : NULL, \
396 .subsys_fmt_prefix = _subsys_fmt_prefix,\
397 }; \
398 static const struct pi_entry *_entry_ptr \
399 __used __section(".printk_index") = &_entry; \
400 } \
401 } while (0)
402
403#else /* !CONFIG_PRINTK_INDEX */
404#define __printk_index_emit(...) do {} while (0)
405#endif /* CONFIG_PRINTK_INDEX */
406
407/*
408 * Some subsystems have their own custom printk that applies a va_format to a
409 * generic format, for example, to include a device number or other metadata
410 * alongside the format supplied by the caller.
411 *
412 * In order to store these in the way they would be emitted by the printk
413 * infrastructure, the subsystem provides us with the start, fixed string, and
414 * any subsequent text in the format string.
415 *
416 * We take a variable argument list as pr_fmt/dev_fmt/etc are sometimes passed
417 * as multiple arguments (eg: `"%s: ", "blah"`), and we must only take the
418 * first one.
419 *
420 * subsys_fmt_prefix must be known at compile time, or compilation will fail
421 * (since this is a mistake). If fmt or level is not known at compile time, no
422 * index entry will be made (since this can legitimately happen).
423 */
424#define printk_index_subsys_emit(subsys_fmt_prefix, level, fmt, ...) \
425 __printk_index_emit(fmt, level, subsys_fmt_prefix)
426
427#define printk_index_wrap(_p_func, _fmt, ...) \
428 ({ \
429 __printk_index_emit(_fmt, NULL, NULL); \
430 _p_func(_fmt, ##__VA_ARGS__); \
431 })
432
433
434#define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
435#define printk_deferred(fmt, ...) \
436 printk_index_wrap(_printk_deferred, fmt, ##__VA_ARGS__)
437
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200438/**
439 * pr_emerg - Print an emergency-level message
440 * @fmt: format string
441 * @...: arguments for the format string
442 *
443 * This macro expands to a printk with KERN_EMERG loglevel. It uses pr_fmt() to
444 * generate the format string.
Dan Streetman6e099f52014-06-04 16:11:44 -0700445 */
Linus Torvaldsa0cba212016-08-09 10:48:18 -0700446#define pr_emerg(fmt, ...) \
447 printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200448/**
449 * pr_alert - Print an alert-level message
450 * @fmt: format string
451 * @...: arguments for the format string
452 *
453 * This macro expands to a printk with KERN_ALERT loglevel. It uses pr_fmt() to
454 * generate the format string.
455 */
Linus Torvaldsa0cba212016-08-09 10:48:18 -0700456#define pr_alert(fmt, ...) \
457 printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200458/**
459 * pr_crit - Print a critical-level message
460 * @fmt: format string
461 * @...: arguments for the format string
462 *
463 * This macro expands to a printk with KERN_CRIT loglevel. It uses pr_fmt() to
464 * generate the format string.
465 */
Linus Torvaldsa0cba212016-08-09 10:48:18 -0700466#define pr_crit(fmt, ...) \
467 printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200468/**
469 * pr_err - Print an error-level message
470 * @fmt: format string
471 * @...: arguments for the format string
472 *
473 * This macro expands to a printk with KERN_ERR loglevel. It uses pr_fmt() to
474 * generate the format string.
475 */
Linus Torvaldsa0cba212016-08-09 10:48:18 -0700476#define pr_err(fmt, ...) \
477 printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200478/**
479 * pr_warn - Print a warning-level message
480 * @fmt: format string
481 * @...: arguments for the format string
482 *
483 * This macro expands to a printk with KERN_WARNING loglevel. It uses pr_fmt()
484 * to generate the format string.
485 */
Kefeng Wang61ff72f2019-11-28 08:47:51 +0800486#define pr_warn(fmt, ...) \
Linus Torvaldsa0cba212016-08-09 10:48:18 -0700487 printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200488/**
489 * pr_notice - Print a notice-level message
490 * @fmt: format string
491 * @...: arguments for the format string
492 *
493 * This macro expands to a printk with KERN_NOTICE loglevel. It uses pr_fmt() to
494 * generate the format string.
495 */
Linus Torvaldsa0cba212016-08-09 10:48:18 -0700496#define pr_notice(fmt, ...) \
497 printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200498/**
499 * pr_info - Print an info-level message
500 * @fmt: format string
501 * @...: arguments for the format string
502 *
503 * This macro expands to a printk with KERN_INFO loglevel. It uses pr_fmt() to
504 * generate the format string.
505 */
Linus Torvaldsa0cba212016-08-09 10:48:18 -0700506#define pr_info(fmt, ...) \
507 printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200508
509/**
510 * pr_cont - Continues a previous log message in the same line.
511 * @fmt: format string
512 * @...: arguments for the format string
513 *
514 * This macro expands to a printk with KERN_CONT loglevel. It should only be
515 * used when continuing a log message with no newline ('\n') enclosed. Otherwise
516 * it defaults back to KERN_DEFAULT loglevel.
Steven Rostedt7b1460e2015-04-15 16:16:59 -0700517 */
Linus Torvalds968ab182010-11-15 13:37:37 -0800518#define pr_cont(fmt, ...) \
519 printk(KERN_CONT fmt, ##__VA_ARGS__)
520
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200521/**
522 * pr_devel - Print a debug-level message conditionally
523 * @fmt: format string
524 * @...: arguments for the format string
525 *
526 * This macro expands to a printk with KERN_DEBUG loglevel if DEBUG is
527 * defined. Otherwise it does nothing.
528 *
529 * It uses pr_fmt() to generate the format string.
530 */
Linus Torvalds968ab182010-11-15 13:37:37 -0800531#ifdef DEBUG
532#define pr_devel(fmt, ...) \
533 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
534#else
535#define pr_devel(fmt, ...) \
Joe Perches5264f2f2011-01-12 16:59:45 -0800536 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
Linus Torvalds968ab182010-11-15 13:37:37 -0800537#endif
538
Joe Perches29fc2bc2013-10-26 20:41:53 -0700539
Linus Torvalds968ab182010-11-15 13:37:37 -0800540/* If you are writing a driver, please use dev_dbg instead */
Orson Zhaiceabef72020-06-07 21:40:14 -0700541#if defined(CONFIG_DYNAMIC_DEBUG) || \
542 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
Luis de Bethencourt9d5059c2016-08-02 14:03:47 -0700543#include <linux/dynamic_debug.h>
544
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200545/**
546 * pr_debug - Print a debug-level message conditionally
547 * @fmt: format string
548 * @...: arguments for the format string
549 *
550 * This macro expands to dynamic_pr_debug() if CONFIG_DYNAMIC_DEBUG is
551 * set. Otherwise, if DEBUG is defined, it's equivalent to a printk with
552 * KERN_DEBUG loglevel. If DEBUG is not defined it does nothing.
553 *
554 * It uses pr_fmt() to generate the format string (dynamic_pr_debug() uses
555 * pr_fmt() internally).
556 */
557#define pr_debug(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800558 dynamic_pr_debug(fmt, ##__VA_ARGS__)
Jim Cromieb558c962011-12-19 17:11:18 -0500559#elif defined(DEBUG)
560#define pr_debug(fmt, ...) \
561 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
Linus Torvalds968ab182010-11-15 13:37:37 -0800562#else
563#define pr_debug(fmt, ...) \
Joe Perches5264f2f2011-01-12 16:59:45 -0800564 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
Linus Torvalds968ab182010-11-15 13:37:37 -0800565#endif
566
567/*
Joe Perches16cb8392011-01-12 16:59:46 -0800568 * Print a one-time message (analogous to WARN_ONCE() et al):
569 */
570
571#ifdef CONFIG_PRINTK
Joe Perchesc28aa1f02014-01-23 15:54:16 -0800572#define printk_once(fmt, ...) \
Tanner Lovea358f402021-06-28 09:50:06 -0400573 DO_ONCE_LITE(printk, fmt, ##__VA_ARGS__)
John Stultzc2248152014-06-04 16:11:41 -0700574#define printk_deferred_once(fmt, ...) \
Tanner Lovea358f402021-06-28 09:50:06 -0400575 DO_ONCE_LITE(printk_deferred, fmt, ##__VA_ARGS__)
Joe Perches16cb8392011-01-12 16:59:46 -0800576#else
Joe Perchesc28aa1f02014-01-23 15:54:16 -0800577#define printk_once(fmt, ...) \
Joe Perches16cb8392011-01-12 16:59:46 -0800578 no_printk(fmt, ##__VA_ARGS__)
John Stultzc2248152014-06-04 16:11:41 -0700579#define printk_deferred_once(fmt, ...) \
580 no_printk(fmt, ##__VA_ARGS__)
Joe Perches16cb8392011-01-12 16:59:46 -0800581#endif
582
583#define pr_emerg_once(fmt, ...) \
584 printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
585#define pr_alert_once(fmt, ...) \
586 printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
587#define pr_crit_once(fmt, ...) \
588 printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
589#define pr_err_once(fmt, ...) \
590 printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
591#define pr_warn_once(fmt, ...) \
592 printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
593#define pr_notice_once(fmt, ...) \
594 printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
595#define pr_info_once(fmt, ...) \
596 printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
Tetsuo Handaeb012d12020-05-25 00:32:43 +0900597/* no pr_cont_once, don't do that... */
Mikhail Gruzdev36d308d2013-02-21 16:43:10 -0800598
599#if defined(DEBUG)
600#define pr_devel_once(fmt, ...) \
601 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
602#else
603#define pr_devel_once(fmt, ...) \
604 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
605#endif
606
Joe Perches16cb8392011-01-12 16:59:46 -0800607/* If you are writing a driver, please use dev_dbg instead */
608#if defined(DEBUG)
609#define pr_debug_once(fmt, ...) \
610 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
611#else
612#define pr_debug_once(fmt, ...) \
613 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
614#endif
615
616/*
Linus Torvalds968ab182010-11-15 13:37:37 -0800617 * ratelimited messages with local ratelimit_state,
618 * no local ratelimit_state used in the !PRINTK case
619 */
620#ifdef CONFIG_PRINTK
Joe Perches6ec42a562011-01-12 16:59:47 -0800621#define printk_ratelimited(fmt, ...) \
622({ \
Linus Torvalds968ab182010-11-15 13:37:37 -0800623 static DEFINE_RATELIMIT_STATE(_rs, \
624 DEFAULT_RATELIMIT_INTERVAL, \
625 DEFAULT_RATELIMIT_BURST); \
626 \
627 if (__ratelimit(&_rs)) \
628 printk(fmt, ##__VA_ARGS__); \
629})
630#else
Joe Perches6ec42a562011-01-12 16:59:47 -0800631#define printk_ratelimited(fmt, ...) \
632 no_printk(fmt, ##__VA_ARGS__)
Linus Torvalds968ab182010-11-15 13:37:37 -0800633#endif
634
Joe Perches6ec42a562011-01-12 16:59:47 -0800635#define pr_emerg_ratelimited(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800636 printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
Joe Perches6ec42a562011-01-12 16:59:47 -0800637#define pr_alert_ratelimited(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800638 printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
Joe Perches6ec42a562011-01-12 16:59:47 -0800639#define pr_crit_ratelimited(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800640 printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
Joe Perches6ec42a562011-01-12 16:59:47 -0800641#define pr_err_ratelimited(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800642 printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
Joe Perches6ec42a562011-01-12 16:59:47 -0800643#define pr_warn_ratelimited(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800644 printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
Joe Perches6ec42a562011-01-12 16:59:47 -0800645#define pr_notice_ratelimited(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800646 printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
Joe Perches6ec42a562011-01-12 16:59:47 -0800647#define pr_info_ratelimited(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800648 printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
649/* no pr_cont_ratelimited, don't do that... */
Mikhail Gruzdev36d308d2013-02-21 16:43:10 -0800650
651#if defined(DEBUG)
652#define pr_devel_ratelimited(fmt, ...) \
653 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
654#else
655#define pr_devel_ratelimited(fmt, ...) \
656 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
657#endif
658
Linus Torvalds968ab182010-11-15 13:37:37 -0800659/* If you are writing a driver, please use dev_dbg instead */
Orson Zhaiceabef72020-06-07 21:40:14 -0700660#if defined(CONFIG_DYNAMIC_DEBUG) || \
661 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
Joe Perches29fc2bc2013-10-26 20:41:53 -0700662/* descriptor check is first to prevent flooding with "callbacks suppressed" */
663#define pr_debug_ratelimited(fmt, ...) \
664do { \
665 static DEFINE_RATELIMIT_STATE(_rs, \
666 DEFAULT_RATELIMIT_INTERVAL, \
667 DEFAULT_RATELIMIT_BURST); \
Jason A. Donenfeld515a9ad2015-09-09 15:36:12 -0700668 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt)); \
Rasmus Villemoesa9d4ab72019-03-07 16:27:29 -0800669 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
Joe Perches29fc2bc2013-10-26 20:41:53 -0700670 __ratelimit(&_rs)) \
Jason A. Donenfeld515a9ad2015-09-09 15:36:12 -0700671 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__); \
Joe Perches29fc2bc2013-10-26 20:41:53 -0700672} while (0)
673#elif defined(DEBUG)
Joe Perches6ec42a562011-01-12 16:59:47 -0800674#define pr_debug_ratelimited(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800675 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
676#else
677#define pr_debug_ratelimited(fmt, ...) \
Joe Perches5264f2f2011-01-12 16:59:45 -0800678 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
Linus Torvalds968ab182010-11-15 13:37:37 -0800679#endif
680
Kay Sieverse11fea922012-05-03 02:29:41 +0200681extern const struct file_operations kmsg_fops;
682
Joe Perchesac83ed62011-01-12 16:59:47 -0800683enum {
684 DUMP_PREFIX_NONE,
685 DUMP_PREFIX_ADDRESS,
686 DUMP_PREFIX_OFFSET
687};
Andy Shevchenko114fc1a2015-02-12 15:02:29 -0800688extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
689 int groupsize, char *linebuf, size_t linebuflen,
690 bool ascii);
Joe Perchesac83ed62011-01-12 16:59:47 -0800691#ifdef CONFIG_PRINTK
692extern void print_hex_dump(const char *level, const char *prefix_str,
693 int prefix_type, int rowsize, int groupsize,
694 const void *buf, size_t len, bool ascii);
Joe Perchesac83ed62011-01-12 16:59:47 -0800695#else
696static inline void print_hex_dump(const char *level, const char *prefix_str,
697 int prefix_type, int rowsize, int groupsize,
698 const void *buf, size_t len, bool ascii)
699{
700}
701static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
702 const void *buf, size_t len)
703{
704}
705
706#endif
707
Orson Zhaiceabef72020-06-07 21:40:14 -0700708#if defined(CONFIG_DYNAMIC_DEBUG) || \
709 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
Vladimir Kondratiev7a555612012-12-05 16:48:27 -0500710#define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
711 groupsize, buf, len, ascii) \
712 dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
713 groupsize, buf, len, ascii)
Linus Walleijcdf17442015-09-09 15:37:11 -0700714#elif defined(DEBUG)
Vladimir Kondratiev7a555612012-12-05 16:48:27 -0500715#define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
716 groupsize, buf, len, ascii) \
717 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize, \
718 groupsize, buf, len, ascii)
Linus Walleijcdf17442015-09-09 15:37:11 -0700719#else
720static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type,
721 int rowsize, int groupsize,
722 const void *buf, size_t len, bool ascii)
723{
724}
725#endif
Vladimir Kondratiev7a555612012-12-05 16:48:27 -0500726
Stephen Boyd091cb0992019-09-25 16:46:29 -0700727/**
728 * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params
729 * @prefix_str: string to prefix each line with;
730 * caller supplies trailing spaces for alignment if desired
731 * @prefix_type: controls whether prefix of an offset, address, or none
732 * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
733 * @buf: data blob to dump
734 * @len: number of bytes in the @buf
735 *
736 * Calls print_hex_dump(), with log level of KERN_DEBUG,
737 * rowsize of 16, groupsize of 1, and ASCII output included.
738 */
739#define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
740 print_hex_dump_debug(prefix_str, prefix_type, 16, 1, buf, len, true)
741
Linus Torvalds968ab182010-11-15 13:37:37 -0800742#endif