blob: 596ad6fa03366847302b75212754c85547f3df76 [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>
Mike Travis162a7e72011-05-24 17:13:20 -070011
Linus Torvalds968ab182010-11-15 13:37:37 -080012extern const char linux_banner[];
13extern const char linux_proc_banner[];
14
Andy Shevchenko36d818f2020-09-11 20:02:02 +030015extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */
16
Petr Mladek262c5e82016-12-12 16:45:50 -080017#define PRINTK_MAX_SINGLE_HEADER_LEN 2
18
Joe Perchesacc8fa412012-07-30 14:40:09 -070019static inline int printk_get_level(const char *buffer)
20{
Joe Perches04d2c8c2012-07-30 14:40:17 -070021 if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
Joe Perchesacc8fa412012-07-30 14:40:09 -070022 switch (buffer[1]) {
23 case '0' ... '7':
Linus Torvalds4bcc5952016-10-08 20:32:40 -070024 case 'c': /* KERN_CONT */
Joe Perchesacc8fa412012-07-30 14:40:09 -070025 return buffer[1];
26 }
27 }
28 return 0;
29}
30
31static inline const char *printk_skip_level(const char *buffer)
32{
Petr Mladek01856982014-04-03 14:48:38 -070033 if (printk_get_level(buffer))
34 return buffer + 2;
35
Joe Perchesacc8fa412012-07-30 14:40:09 -070036 return buffer;
37}
38
Petr Mladek49795752016-12-12 16:45:47 -080039static inline const char *printk_skip_headers(const char *buffer)
40{
41 while (printk_get_level(buffer))
42 buffer = printk_skip_level(buffer);
43
44 return buffer;
45}
46
Tejun Heod43ff432015-06-25 15:01:24 -070047#define CONSOLE_EXT_LOG_MAX 8192
48
Borislav Petkova8fe19e2014-06-04 16:11:46 -070049/* printk's without a loglevel use this.. */
Alex Elder42a9dc02014-08-06 16:09:01 -070050#define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT
Borislav Petkova8fe19e2014-06-04 16:11:46 -070051
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 */
Borislav Petkova8fe19e2014-06-04 16:11:46 -070055#define CONSOLE_LOGLEVEL_DEBUG 10 /* issue debug messages */
56#define CONSOLE_LOGLEVEL_MOTORMOUTH 15 /* You can't shut this one up */
57
Olof Johanssona8cfdc62016-12-12 16:45:56 -080058/*
Hans de Goede22eceb82018-06-19 13:57:26 +020059 * 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.
Olof Johanssona8cfdc62016-12-12 16:45:56 -080061 */
62#define CONSOLE_LOGLEVEL_DEFAULT CONFIG_CONSOLE_LOGLEVEL_DEFAULT
Hans de Goede22eceb82018-06-19 13:57:26 +020063#define CONSOLE_LOGLEVEL_QUIET CONFIG_CONSOLE_LOGLEVEL_QUIET
Olof Johanssona8cfdc62016-12-12 16:45:56 -080064
Linus Torvalds968ab182010-11-15 13:37:37 -080065extern int console_printk[];
66
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])
71
Joe Perchesa9747cc32011-01-12 16:59:43 -080072static inline void console_silent(void)
73{
Borislav Petkova8fe19e2014-06-04 16:11:46 -070074 console_loglevel = CONSOLE_LOGLEVEL_SILENT;
Joe Perchesa9747cc32011-01-12 16:59:43 -080075}
76
77static inline void console_verbose(void)
78{
79 if (console_loglevel)
Borislav Petkova8fe19e2014-06-04 16:11:46 -070080 console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
Joe Perchesa9747cc32011-01-12 16:59:43 -080081}
82
Borislav Petkov750afe72016-08-02 14:04:07 -070083/* strlen("ratelimit") + 1 */
84#define DEVKMSG_STR_MAX_SIZE 10
85extern char devkmsg_log_str[];
86struct ctl_table;
87
Feng Tangc39ea0b2019-05-14 15:45:34 -070088extern int suppress_printk;
89
Linus Torvalds968ab182010-11-15 13:37:37 -080090struct va_format {
91 const char *fmt;
92 va_list *va;
93};
94
95/*
96 * FW_BUG
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
101 * code.
102 *
103 * Use it for definite and high priority BIOS bugs.
104 *
105 * FW_WARN
106 * Use it for not that clear (e.g. could the kernel messed up things already?)
107 * and medium priority BIOS bugs.
108 *
109 * FW_INFO
110 * Use this one if you want to tell the user or vendor about something
111 * suspicious, but generally harmless related to the firmware.
112 *
113 * Use it for information or very low priority BIOS bugs.
114 */
115#define FW_BUG "[Firmware Bug]: "
116#define FW_WARN "[Firmware Warn]: "
117#define FW_INFO "[Firmware Info]: "
118
119/*
120 * HW_ERR
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.
123 */
124#define HW_ERR "[Hardware Error]: "
125
Joe Perches5264f2f2011-01-12 16:59:45 -0800126/*
Neil Horman0a9a8bf2013-12-23 08:29:42 -0500127 * DEPRECATED
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
130 */
131#define DEPRECATED "[Deprecated]: "
132
133/*
Joe Perches5264f2f2011-01-12 16:59:45 -0800134 * Dummy printk for disabled debugging statements to use whilst maintaining
Aaron Conolefe22cd9b2016-01-15 16:59:12 -0800135 * gcc's format checking.
Joe Perches5264f2f2011-01-12 16:59:45 -0800136 */
Borislav Petkov069f0cd2016-07-05 00:31:26 +0200137#define no_printk(fmt, ...) \
138({ \
Masahiro Yamada93b138d2017-09-18 00:01:44 +0900139 if (0) \
140 printk(fmt, ##__VA_ARGS__); \
Borislav Petkov069f0cd2016-07-05 00:31:26 +0200141 0; \
142})
Joe Perches5264f2f2011-01-12 16:59:45 -0800143
Thomas Gleixnerd0380e62013-04-29 16:17:18 -0700144#ifdef CONFIG_EARLY_PRINTK
Joe Perchesb9075fa2011-10-31 17:11:33 -0700145extern asmlinkage __printf(1, 2)
Joe Perches5264f2f2011-01-12 16:59:45 -0800146void early_printk(const char *fmt, ...);
Thomas Gleixnerd0380e62013-04-29 16:17:18 -0700147#else
148static inline __printf(1, 2) __cold
149void early_printk(const char *s, ...) { }
150#endif
Joe Perches5264f2f2011-01-12 16:59:45 -0800151
John Ogness74caba72020-09-21 13:24:45 +0206152struct dev_printk_info;
153
Linus Torvalds968ab182010-11-15 13:37:37 -0800154#ifdef CONFIG_PRINTK
John Ogness74caba72020-09-21 13:24:45 +0206155asmlinkage __printf(4, 0)
Kay Sievers7ff95542012-05-03 02:29:13 +0200156int vprintk_emit(int facility, int level,
John Ogness74caba72020-09-21 13:24:45 +0206157 const struct dev_printk_info *dev_info,
Kay Sievers7ff95542012-05-03 02:29:13 +0200158 const char *fmt, va_list args);
159
Joe Perchesb9075fa2011-10-31 17:11:33 -0700160asmlinkage __printf(1, 0)
Joe Perches5264f2f2011-01-12 16:59:45 -0800161int vprintk(const char *fmt, va_list args);
Kay Sievers7ff95542012-05-03 02:29:13 +0200162
Joe Perchesb9075fa2011-10-31 17:11:33 -0700163asmlinkage __printf(1, 2) __cold
Joe Perches5264f2f2011-01-12 16:59:45 -0800164int printk(const char *fmt, ...);
Linus Torvalds968ab182010-11-15 13:37:37 -0800165
166/*
John Stultzaac74dc2014-06-04 16:11:40 -0700167 * Special printk facility for scheduler/timekeeping use only, _DO_NOT_USE_ !
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +0100168 */
John Stultzaac74dc2014-06-04 16:11:40 -0700169__printf(1, 2) __cold int printk_deferred(const char *fmt, ...);
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +0100170
John Ogness85e3e7f2021-07-15 21:39:57 +0206171extern void __printk_safe_enter(void);
172extern void __printk_safe_exit(void);
173/*
174 * The printk_deferred_enter/exit macros are available only as a hack for
175 * some code paths that need to defer all printk console printing. Interrupts
176 * must be disabled for the deferred duration.
177 */
178#define printk_deferred_enter __printk_safe_enter
179#define printk_deferred_exit __printk_safe_exit
180
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +0100181/*
Linus Torvalds968ab182010-11-15 13:37:37 -0800182 * Please don't use printk_ratelimit(), because it shares ratelimiting state
183 * with all other unrelated printk_ratelimit() callsites. Instead use
184 * printk_ratelimited() or plain old __ratelimit().
185 */
186extern int __printk_ratelimit(const char *func);
187#define printk_ratelimit() __printk_ratelimit(__func__)
188extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
189 unsigned int interval_msec);
190
191extern int printk_delay_msec;
192extern int dmesg_restrict;
Linus Torvalds968ab182010-11-15 13:37:37 -0800193
Borislav Petkov750afe72016-08-02 14:04:07 -0700194extern int
Christoph Hellwig32927392020-04-24 08:43:38 +0200195devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, void *buf,
Borislav Petkov750afe72016-08-02 14:04:07 -0700196 size_t *lenp, loff_t *ppos);
197
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -0700198extern void wake_up_klogd(void);
199
Pranith Kumar07261ed2015-01-26 12:58:43 -0800200char *log_buf_addr_get(void);
201u32 log_buf_len_get(void);
Hari Bathini692f66f2017-05-08 15:56:18 -0700202void log_buf_vmcoreinfo_setup(void);
Mike Travis162a7e72011-05-24 17:13:20 -0700203void __init setup_log_buf(int early);
Nicolas Iooss8db14862015-07-17 16:23:42 -0700204__printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...);
Tejun Heo196779b2013-04-30 15:27:12 -0700205void dump_stack_print_info(const char *log_lvl);
Tejun Heoa43cb952013-04-30 15:27:17 -0700206void show_regs_print_info(const char *log_lvl);
Alexander Potapenko4469c0f2021-06-28 19:40:30 -0700207extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold;
Dave Younge36df282018-02-13 15:28:34 +0800208extern asmlinkage void dump_stack(void) __cold;
Nicholas Piggin5d5e4522021-11-07 14:51:16 +1000209void printk_trigger_flush(void);
Linus Torvalds968ab182010-11-15 13:37:37 -0800210#else
Joe Perchesb9075fa2011-10-31 17:11:33 -0700211static inline __printf(1, 0)
Joe Perches5264f2f2011-01-12 16:59:45 -0800212int vprintk(const char *s, va_list args)
213{
214 return 0;
215}
Joe Perchesb9075fa2011-10-31 17:11:33 -0700216static inline __printf(1, 2) __cold
Joe Perches5264f2f2011-01-12 16:59:45 -0800217int printk(const char *s, ...)
218{
219 return 0;
220}
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +0100221static inline __printf(1, 2) __cold
John Stultzaac74dc2014-06-04 16:11:40 -0700222int printk_deferred(const char *s, ...)
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +0100223{
224 return 0;
225}
John Ogness85e3e7f2021-07-15 21:39:57 +0206226
227static inline void printk_deferred_enter(void)
228{
229}
230
231static inline void printk_deferred_exit(void)
232{
233}
234
Joe Perches5264f2f2011-01-12 16:59:45 -0800235static inline int printk_ratelimit(void)
236{
237 return 0;
238}
239static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
240 unsigned int interval_msec)
241{
242 return false;
243}
Linus Torvalds968ab182010-11-15 13:37:37 -0800244
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -0700245static inline void wake_up_klogd(void)
246{
247}
248
Pranith Kumar07261ed2015-01-26 12:58:43 -0800249static inline char *log_buf_addr_get(void)
250{
251 return NULL;
252}
253
254static inline u32 log_buf_len_get(void)
255{
256 return 0;
257}
258
Hari Bathini692f66f2017-05-08 15:56:18 -0700259static inline void log_buf_vmcoreinfo_setup(void)
Linus Torvalds968ab182010-11-15 13:37:37 -0800260{
261}
Mike Travis162a7e72011-05-24 17:13:20 -0700262
263static inline void setup_log_buf(int early)
264{
265}
Tejun Heo196779b2013-04-30 15:27:12 -0700266
Nicolas Iooss8db14862015-07-17 16:23:42 -0700267static inline __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...)
Tejun Heo98e5e1b2013-04-30 15:27:15 -0700268{
269}
270
Tejun Heo196779b2013-04-30 15:27:12 -0700271static inline void dump_stack_print_info(const char *log_lvl)
272{
273}
Tejun Heoa43cb952013-04-30 15:27:17 -0700274
275static inline void show_regs_print_info(const char *log_lvl)
276{
277}
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900278
Alexander Potapenko4469c0f2021-06-28 19:40:30 -0700279static inline void dump_stack_lvl(const char *log_lvl)
280{
281}
282
Alexey Dobriyane6310f02019-01-03 15:26:37 -0800283static inline void dump_stack(void)
Dave Younge36df282018-02-13 15:28:34 +0800284{
285}
Nicholas Piggin5d5e4522021-11-07 14:51:16 +1000286static inline void printk_trigger_flush(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
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200351/**
352 * pr_emerg - Print an emergency-level message
353 * @fmt: format string
354 * @...: arguments for the format string
355 *
356 * This macro expands to a printk with KERN_EMERG loglevel. It uses pr_fmt() to
357 * generate the format string.
Dan Streetman6e099f52014-06-04 16:11:44 -0700358 */
Linus Torvaldsa0cba212016-08-09 10:48:18 -0700359#define pr_emerg(fmt, ...) \
360 printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200361/**
362 * pr_alert - Print an alert-level message
363 * @fmt: format string
364 * @...: arguments for the format string
365 *
366 * This macro expands to a printk with KERN_ALERT loglevel. It uses pr_fmt() to
367 * generate the format string.
368 */
Linus Torvaldsa0cba212016-08-09 10:48:18 -0700369#define pr_alert(fmt, ...) \
370 printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200371/**
372 * pr_crit - Print a critical-level message
373 * @fmt: format string
374 * @...: arguments for the format string
375 *
376 * This macro expands to a printk with KERN_CRIT loglevel. It uses pr_fmt() to
377 * generate the format string.
378 */
Linus Torvaldsa0cba212016-08-09 10:48:18 -0700379#define pr_crit(fmt, ...) \
380 printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200381/**
382 * pr_err - Print an error-level message
383 * @fmt: format string
384 * @...: arguments for the format string
385 *
386 * This macro expands to a printk with KERN_ERR loglevel. It uses pr_fmt() to
387 * generate the format string.
388 */
Linus Torvaldsa0cba212016-08-09 10:48:18 -0700389#define pr_err(fmt, ...) \
390 printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200391/**
392 * pr_warn - Print a warning-level message
393 * @fmt: format string
394 * @...: arguments for the format string
395 *
396 * This macro expands to a printk with KERN_WARNING loglevel. It uses pr_fmt()
397 * to generate the format string.
398 */
Kefeng Wang61ff72f2019-11-28 08:47:51 +0800399#define pr_warn(fmt, ...) \
Linus Torvaldsa0cba212016-08-09 10:48:18 -0700400 printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200401/**
402 * pr_notice - Print a notice-level message
403 * @fmt: format string
404 * @...: arguments for the format string
405 *
406 * This macro expands to a printk with KERN_NOTICE loglevel. It uses pr_fmt() to
407 * generate the format string.
408 */
Linus Torvaldsa0cba212016-08-09 10:48:18 -0700409#define pr_notice(fmt, ...) \
410 printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200411/**
412 * pr_info - Print an info-level message
413 * @fmt: format string
414 * @...: arguments for the format string
415 *
416 * This macro expands to a printk with KERN_INFO loglevel. It uses pr_fmt() to
417 * generate the format string.
418 */
Linus Torvaldsa0cba212016-08-09 10:48:18 -0700419#define pr_info(fmt, ...) \
420 printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200421
422/**
423 * pr_cont - Continues a previous log message in the same line.
424 * @fmt: format string
425 * @...: arguments for the format string
426 *
427 * This macro expands to a printk with KERN_CONT loglevel. It should only be
428 * used when continuing a log message with no newline ('\n') enclosed. Otherwise
429 * it defaults back to KERN_DEFAULT loglevel.
Steven Rostedt7b1460e2015-04-15 16:16:59 -0700430 */
Linus Torvalds968ab182010-11-15 13:37:37 -0800431#define pr_cont(fmt, ...) \
432 printk(KERN_CONT fmt, ##__VA_ARGS__)
433
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200434/**
435 * pr_devel - Print a debug-level message conditionally
436 * @fmt: format string
437 * @...: arguments for the format string
438 *
439 * This macro expands to a printk with KERN_DEBUG loglevel if DEBUG is
440 * defined. Otherwise it does nothing.
441 *
442 * It uses pr_fmt() to generate the format string.
443 */
Linus Torvalds968ab182010-11-15 13:37:37 -0800444#ifdef DEBUG
445#define pr_devel(fmt, ...) \
446 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
447#else
448#define pr_devel(fmt, ...) \
Joe Perches5264f2f2011-01-12 16:59:45 -0800449 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
Linus Torvalds968ab182010-11-15 13:37:37 -0800450#endif
451
Joe Perches29fc2bc2013-10-26 20:41:53 -0700452
Linus Torvalds968ab182010-11-15 13:37:37 -0800453/* If you are writing a driver, please use dev_dbg instead */
Orson Zhaiceabef72020-06-07 21:40:14 -0700454#if defined(CONFIG_DYNAMIC_DEBUG) || \
455 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
Luis de Bethencourt9d5059c2016-08-02 14:03:47 -0700456#include <linux/dynamic_debug.h>
457
Ricardo Cañuelo90c165f02020-04-03 11:36:17 +0200458/**
459 * pr_debug - Print a debug-level message conditionally
460 * @fmt: format string
461 * @...: arguments for the format string
462 *
463 * This macro expands to dynamic_pr_debug() if CONFIG_DYNAMIC_DEBUG is
464 * set. Otherwise, if DEBUG is defined, it's equivalent to a printk with
465 * KERN_DEBUG loglevel. If DEBUG is not defined it does nothing.
466 *
467 * It uses pr_fmt() to generate the format string (dynamic_pr_debug() uses
468 * pr_fmt() internally).
469 */
470#define pr_debug(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800471 dynamic_pr_debug(fmt, ##__VA_ARGS__)
Jim Cromieb558c962011-12-19 17:11:18 -0500472#elif defined(DEBUG)
473#define pr_debug(fmt, ...) \
474 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
Linus Torvalds968ab182010-11-15 13:37:37 -0800475#else
476#define pr_debug(fmt, ...) \
Joe Perches5264f2f2011-01-12 16:59:45 -0800477 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
Linus Torvalds968ab182010-11-15 13:37:37 -0800478#endif
479
480/*
Joe Perches16cb8392011-01-12 16:59:46 -0800481 * Print a one-time message (analogous to WARN_ONCE() et al):
482 */
483
484#ifdef CONFIG_PRINTK
Joe Perchesc28aa1f02014-01-23 15:54:16 -0800485#define printk_once(fmt, ...) \
486({ \
Joe Perches33def842020-10-21 19:36:07 -0700487 static bool __section(".data.once") __print_once; \
Borislav Petkov069f0cd2016-07-05 00:31:26 +0200488 bool __ret_print_once = !__print_once; \
Joe Perchesc28aa1f02014-01-23 15:54:16 -0800489 \
490 if (!__print_once) { \
491 __print_once = true; \
492 printk(fmt, ##__VA_ARGS__); \
493 } \
Borislav Petkov069f0cd2016-07-05 00:31:26 +0200494 unlikely(__ret_print_once); \
Joe Perches16cb8392011-01-12 16:59:46 -0800495})
John Stultzc2248152014-06-04 16:11:41 -0700496#define printk_deferred_once(fmt, ...) \
497({ \
Joe Perches33def842020-10-21 19:36:07 -0700498 static bool __section(".data.once") __print_once; \
Borislav Petkov069f0cd2016-07-05 00:31:26 +0200499 bool __ret_print_once = !__print_once; \
John Stultzc2248152014-06-04 16:11:41 -0700500 \
501 if (!__print_once) { \
502 __print_once = true; \
503 printk_deferred(fmt, ##__VA_ARGS__); \
504 } \
Borislav Petkov069f0cd2016-07-05 00:31:26 +0200505 unlikely(__ret_print_once); \
John Stultzc2248152014-06-04 16:11:41 -0700506})
Joe Perches16cb8392011-01-12 16:59:46 -0800507#else
Joe Perchesc28aa1f02014-01-23 15:54:16 -0800508#define printk_once(fmt, ...) \
Joe Perches16cb8392011-01-12 16:59:46 -0800509 no_printk(fmt, ##__VA_ARGS__)
John Stultzc2248152014-06-04 16:11:41 -0700510#define printk_deferred_once(fmt, ...) \
511 no_printk(fmt, ##__VA_ARGS__)
Joe Perches16cb8392011-01-12 16:59:46 -0800512#endif
513
514#define pr_emerg_once(fmt, ...) \
515 printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
516#define pr_alert_once(fmt, ...) \
517 printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
518#define pr_crit_once(fmt, ...) \
519 printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
520#define pr_err_once(fmt, ...) \
521 printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
522#define pr_warn_once(fmt, ...) \
523 printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
524#define pr_notice_once(fmt, ...) \
525 printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
526#define pr_info_once(fmt, ...) \
527 printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
Tetsuo Handaeb012d12020-05-25 00:32:43 +0900528/* no pr_cont_once, don't do that... */
Mikhail Gruzdev36d308d2013-02-21 16:43:10 -0800529
530#if defined(DEBUG)
531#define pr_devel_once(fmt, ...) \
532 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
533#else
534#define pr_devel_once(fmt, ...) \
535 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
536#endif
537
Joe Perches16cb8392011-01-12 16:59:46 -0800538/* If you are writing a driver, please use dev_dbg instead */
539#if defined(DEBUG)
540#define pr_debug_once(fmt, ...) \
541 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
542#else
543#define pr_debug_once(fmt, ...) \
544 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
545#endif
546
547/*
Linus Torvalds968ab182010-11-15 13:37:37 -0800548 * ratelimited messages with local ratelimit_state,
549 * no local ratelimit_state used in the !PRINTK case
550 */
551#ifdef CONFIG_PRINTK
Joe Perches6ec42a562011-01-12 16:59:47 -0800552#define printk_ratelimited(fmt, ...) \
553({ \
Linus Torvalds968ab182010-11-15 13:37:37 -0800554 static DEFINE_RATELIMIT_STATE(_rs, \
555 DEFAULT_RATELIMIT_INTERVAL, \
556 DEFAULT_RATELIMIT_BURST); \
557 \
558 if (__ratelimit(&_rs)) \
559 printk(fmt, ##__VA_ARGS__); \
560})
561#else
Joe Perches6ec42a562011-01-12 16:59:47 -0800562#define printk_ratelimited(fmt, ...) \
563 no_printk(fmt, ##__VA_ARGS__)
Linus Torvalds968ab182010-11-15 13:37:37 -0800564#endif
565
Joe Perches6ec42a562011-01-12 16:59:47 -0800566#define pr_emerg_ratelimited(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800567 printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
Joe Perches6ec42a562011-01-12 16:59:47 -0800568#define pr_alert_ratelimited(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800569 printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
Joe Perches6ec42a562011-01-12 16:59:47 -0800570#define pr_crit_ratelimited(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800571 printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
Joe Perches6ec42a562011-01-12 16:59:47 -0800572#define pr_err_ratelimited(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800573 printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
Joe Perches6ec42a562011-01-12 16:59:47 -0800574#define pr_warn_ratelimited(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800575 printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
Joe Perches6ec42a562011-01-12 16:59:47 -0800576#define pr_notice_ratelimited(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800577 printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
Joe Perches6ec42a562011-01-12 16:59:47 -0800578#define pr_info_ratelimited(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800579 printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
580/* no pr_cont_ratelimited, don't do that... */
Mikhail Gruzdev36d308d2013-02-21 16:43:10 -0800581
582#if defined(DEBUG)
583#define pr_devel_ratelimited(fmt, ...) \
584 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
585#else
586#define pr_devel_ratelimited(fmt, ...) \
587 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
588#endif
589
Linus Torvalds968ab182010-11-15 13:37:37 -0800590/* If you are writing a driver, please use dev_dbg instead */
Orson Zhaiceabef72020-06-07 21:40:14 -0700591#if defined(CONFIG_DYNAMIC_DEBUG) || \
592 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
Joe Perches29fc2bc2013-10-26 20:41:53 -0700593/* descriptor check is first to prevent flooding with "callbacks suppressed" */
594#define pr_debug_ratelimited(fmt, ...) \
595do { \
596 static DEFINE_RATELIMIT_STATE(_rs, \
597 DEFAULT_RATELIMIT_INTERVAL, \
598 DEFAULT_RATELIMIT_BURST); \
Jason A. Donenfeld515a9ad2015-09-09 15:36:12 -0700599 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt)); \
Rasmus Villemoesa9d4ab72019-03-07 16:27:29 -0800600 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
Joe Perches29fc2bc2013-10-26 20:41:53 -0700601 __ratelimit(&_rs)) \
Jason A. Donenfeld515a9ad2015-09-09 15:36:12 -0700602 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__); \
Joe Perches29fc2bc2013-10-26 20:41:53 -0700603} while (0)
604#elif defined(DEBUG)
Joe Perches6ec42a562011-01-12 16:59:47 -0800605#define pr_debug_ratelimited(fmt, ...) \
Linus Torvalds968ab182010-11-15 13:37:37 -0800606 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
607#else
608#define pr_debug_ratelimited(fmt, ...) \
Joe Perches5264f2f2011-01-12 16:59:45 -0800609 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
Linus Torvalds968ab182010-11-15 13:37:37 -0800610#endif
611
Kay Sieverse11fea922012-05-03 02:29:41 +0200612extern const struct file_operations kmsg_fops;
613
Joe Perchesac83ed62011-01-12 16:59:47 -0800614enum {
615 DUMP_PREFIX_NONE,
616 DUMP_PREFIX_ADDRESS,
617 DUMP_PREFIX_OFFSET
618};
Andy Shevchenko114fc1a2015-02-12 15:02:29 -0800619extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
620 int groupsize, char *linebuf, size_t linebuflen,
621 bool ascii);
Joe Perchesac83ed62011-01-12 16:59:47 -0800622#ifdef CONFIG_PRINTK
623extern void print_hex_dump(const char *level, const char *prefix_str,
624 int prefix_type, int rowsize, int groupsize,
625 const void *buf, size_t len, bool ascii);
Joe Perchesac83ed62011-01-12 16:59:47 -0800626#else
627static inline void print_hex_dump(const char *level, const char *prefix_str,
628 int prefix_type, int rowsize, int groupsize,
629 const void *buf, size_t len, bool ascii)
630{
631}
632static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
633 const void *buf, size_t len)
634{
635}
636
637#endif
638
Orson Zhaiceabef72020-06-07 21:40:14 -0700639#if defined(CONFIG_DYNAMIC_DEBUG) || \
640 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
Vladimir Kondratiev7a555612012-12-05 16:48:27 -0500641#define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
642 groupsize, buf, len, ascii) \
643 dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
644 groupsize, buf, len, ascii)
Linus Walleijcdf17442015-09-09 15:37:11 -0700645#elif defined(DEBUG)
Vladimir Kondratiev7a555612012-12-05 16:48:27 -0500646#define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
647 groupsize, buf, len, ascii) \
648 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize, \
649 groupsize, buf, len, ascii)
Linus Walleijcdf17442015-09-09 15:37:11 -0700650#else
651static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type,
652 int rowsize, int groupsize,
653 const void *buf, size_t len, bool ascii)
654{
655}
656#endif
Vladimir Kondratiev7a555612012-12-05 16:48:27 -0500657
Stephen Boyd091cb0992019-09-25 16:46:29 -0700658/**
659 * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params
660 * @prefix_str: string to prefix each line with;
661 * caller supplies trailing spaces for alignment if desired
662 * @prefix_type: controls whether prefix of an offset, address, or none
663 * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
664 * @buf: data blob to dump
665 * @len: number of bytes in the @buf
666 *
667 * Calls print_hex_dump(), with log level of KERN_DEBUG,
668 * rowsize of 16, groupsize of 1, and ASCII output included.
669 */
670#define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
671 print_hex_dump_debug(prefix_str, prefix_type, 16, 1, buf, len, true)
672
Linus Torvalds968ab182010-11-15 13:37:37 -0800673#endif