1
2#ifndef __KERNEL_PRINTK__
3#define __KERNEL_PRINTK__
4
5#include <linux/stdarg.h>
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>
11#include <linux/once_lite.h>
12
13extern const char linux_banner[];
14extern const char linux_proc_banner[];
15
16extern int oops_in_progress;
17
18#define PRINTK_MAX_SINGLE_HEADER_LEN 2
19
20static inline int printk_get_level(const char *buffer)
21{
22 if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
23 switch (buffer[1]) {
24 case '0' ... '7':
25 case 'c':
26 return buffer[1];
27 }
28 }
29 return 0;
30}
31
32static inline const char *printk_skip_level(const char *buffer)
33{
34 if (printk_get_level(buffer))
35 return buffer + 2;
36
37 return buffer;
38}
39
40static 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
48#define CONSOLE_EXT_LOG_MAX 8192
49
50
51#define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT
52
53
54#define CONSOLE_LOGLEVEL_SILENT 0
55#define CONSOLE_LOGLEVEL_MIN 1
56#define CONSOLE_LOGLEVEL_DEBUG 10
57#define CONSOLE_LOGLEVEL_MOTORMOUTH 15
58
59
60
61
62
63#define CONSOLE_LOGLEVEL_DEFAULT CONFIG_CONSOLE_LOGLEVEL_DEFAULT
64#define CONSOLE_LOGLEVEL_QUIET CONFIG_CONSOLE_LOGLEVEL_QUIET
65
66extern 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
73extern void console_verbose(void);
74
75
76#define DEVKMSG_STR_MAX_SIZE 10
77extern char devkmsg_log_str[];
78struct ctl_table;
79
80extern int suppress_printk;
81
82struct va_format {
83 const char *fmt;
84 va_list *va;
85};
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107#define FW_BUG "[Firmware Bug]: "
108#define FW_WARN "[Firmware Warn]: "
109#define FW_INFO "[Firmware Info]: "
110
111
112
113
114
115
116#define HW_ERR "[Hardware Error]: "
117
118
119
120
121
122
123#define DEPRECATED "[Deprecated]: "
124
125
126
127
128
129#define no_printk(fmt, ...) \
130({ \
131 if (0) \
132 printk(fmt, ##__VA_ARGS__); \
133 0; \
134})
135
136#ifdef CONFIG_EARLY_PRINTK
137extern asmlinkage __printf(1, 2)
138void early_printk(const char *fmt, ...);
139#else
140static inline __printf(1, 2) __cold
141void early_printk(const char *s, ...) { }
142#endif
143
144struct dev_printk_info;
145
146#ifdef CONFIG_PRINTK
147asmlinkage __printf(4, 0)
148int vprintk_emit(int facility, int level,
149 const struct dev_printk_info *dev_info,
150 const char *fmt, va_list args);
151
152asmlinkage __printf(1, 0)
153int vprintk(const char *fmt, va_list args);
154
155asmlinkage __printf(1, 2) __cold
156int _printk(const char *fmt, ...);
157
158
159
160
161__printf(1, 2) __cold int _printk_deferred(const char *fmt, ...);
162
163extern void __printk_safe_enter(void);
164extern void __printk_safe_exit(void);
165
166
167
168
169
170#define printk_deferred_enter __printk_safe_enter
171#define printk_deferred_exit __printk_safe_exit
172
173
174
175
176
177
178extern int __printk_ratelimit(const char *func);
179#define printk_ratelimit() __printk_ratelimit(__func__)
180extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
181 unsigned int interval_msec);
182
183extern int printk_delay_msec;
184extern int dmesg_restrict;
185
186extern int
187devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, void *buf,
188 size_t *lenp, loff_t *ppos);
189
190extern void wake_up_klogd(void);
191
192char *log_buf_addr_get(void);
193u32 log_buf_len_get(void);
194void log_buf_vmcoreinfo_setup(void);
195void __init setup_log_buf(int early);
196__printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...);
197void dump_stack_print_info(const char *log_lvl);
198void show_regs_print_info(const char *log_lvl);
199extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold;
200extern asmlinkage void dump_stack(void) __cold;
201#else
202static inline __printf(1, 0)
203int vprintk(const char *s, va_list args)
204{
205 return 0;
206}
207static inline __printf(1, 2) __cold
208int _printk(const char *s, ...)
209{
210 return 0;
211}
212static inline __printf(1, 2) __cold
213int _printk_deferred(const char *s, ...)
214{
215 return 0;
216}
217
218static inline void printk_deferred_enter(void)
219{
220}
221
222static inline void printk_deferred_exit(void)
223{
224}
225
226static inline int printk_ratelimit(void)
227{
228 return 0;
229}
230static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
231 unsigned int interval_msec)
232{
233 return false;
234}
235
236static inline void wake_up_klogd(void)
237{
238}
239
240static inline char *log_buf_addr_get(void)
241{
242 return NULL;
243}
244
245static inline u32 log_buf_len_get(void)
246{
247 return 0;
248}
249
250static inline void log_buf_vmcoreinfo_setup(void)
251{
252}
253
254static inline void setup_log_buf(int early)
255{
256}
257
258static inline __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...)
259{
260}
261
262static inline void dump_stack_print_info(const char *log_lvl)
263{
264}
265
266static inline void show_regs_print_info(const char *log_lvl)
267{
268}
269
270static inline void dump_stack_lvl(const char *log_lvl)
271{
272}
273
274static inline void dump_stack(void)
275{
276}
277#endif
278
279#ifdef CONFIG_SMP
280extern int __printk_cpu_trylock(void);
281extern void __printk_wait_on_cpu_lock(void);
282extern void __printk_cpu_unlock(void);
283
284
285
286
287
288
289
290
291
292
293#define printk_cpu_lock_irqsave(flags) \
294 for (;;) { \
295 local_irq_save(flags); \
296 if (__printk_cpu_trylock()) \
297 break; \
298 local_irq_restore(flags); \
299 __printk_wait_on_cpu_lock(); \
300 }
301
302
303
304
305
306
307#define printk_cpu_unlock_irqrestore(flags) \
308 do { \
309 __printk_cpu_unlock(); \
310 local_irq_restore(flags); \
311 } while (0) \
312
313#else
314
315#define printk_cpu_lock_irqsave(flags) ((void)flags)
316#define printk_cpu_unlock_irqrestore(flags) ((void)flags)
317
318#endif
319
320extern int kptr_restrict;
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335#ifndef pr_fmt
336#define pr_fmt(fmt) fmt
337#endif
338
339struct module;
340
341#ifdef CONFIG_PRINTK_INDEX
342struct pi_entry {
343 const char *fmt;
344 const char *func;
345 const char *file;
346 unsigned int line;
347
348
349
350
351
352
353
354
355
356 const char *level;
357
358
359
360
361
362
363
364
365 const char *subsys_fmt_prefix;
366} __packed;
367
368#define __printk_index_emit(_fmt, _level, _subsys_fmt_prefix) \
369 do { \
370 if (__builtin_constant_p(_fmt) && __builtin_constant_p(_level)) { \
371
372
373
374
375
376 \
377 static const struct pi_entry _entry \
378 __used = { \
379 .fmt = __builtin_constant_p(_fmt) ? (_fmt) : NULL, \
380 .func = __func__, \
381 .file = __FILE__, \
382 .line = __LINE__, \
383 .level = __builtin_constant_p(_level) ? (_level) : NULL, \
384 .subsys_fmt_prefix = _subsys_fmt_prefix,\
385 }; \
386 static const struct pi_entry *_entry_ptr \
387 __used __section(".printk_index") = &_entry; \
388 } \
389 } while (0)
390
391#else
392#define __printk_index_emit(...) do {} while (0)
393#endif
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412#define printk_index_subsys_emit(subsys_fmt_prefix, level, fmt, ...) \
413 __printk_index_emit(fmt, level, subsys_fmt_prefix)
414
415#define printk_index_wrap(_p_func, _fmt, ...) \
416 ({ \
417 __printk_index_emit(_fmt, NULL, NULL); \
418 _p_func(_fmt, ##__VA_ARGS__); \
419 })
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446#define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
447#define printk_deferred(fmt, ...) \
448 printk_index_wrap(_printk_deferred, fmt, ##__VA_ARGS__)
449
450
451
452
453
454
455
456
457
458#define pr_emerg(fmt, ...) \
459 printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
460
461
462
463
464
465
466
467
468#define pr_alert(fmt, ...) \
469 printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
470
471
472
473
474
475
476
477
478#define pr_crit(fmt, ...) \
479 printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
480
481
482
483
484
485
486
487
488#define pr_err(fmt, ...) \
489 printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
490
491
492
493
494
495
496
497
498#define pr_warn(fmt, ...) \
499 printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
500
501
502
503
504
505
506
507
508#define pr_notice(fmt, ...) \
509 printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
510
511
512
513
514
515
516
517
518#define pr_info(fmt, ...) \
519 printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
520
521
522
523
524
525
526
527
528
529
530#define pr_cont(fmt, ...) \
531 printk(KERN_CONT fmt, ##__VA_ARGS__)
532
533
534
535
536
537
538
539
540
541
542
543#ifdef DEBUG
544#define pr_devel(fmt, ...) \
545 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
546#else
547#define pr_devel(fmt, ...) \
548 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
549#endif
550
551
552
553#if defined(CONFIG_DYNAMIC_DEBUG) || \
554 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
555#include <linux/dynamic_debug.h>
556
557
558
559
560
561
562
563
564
565
566
567
568
569#define pr_debug(fmt, ...) \
570 dynamic_pr_debug(fmt, ##__VA_ARGS__)
571#elif defined(DEBUG)
572#define pr_debug(fmt, ...) \
573 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
574#else
575#define pr_debug(fmt, ...) \
576 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
577#endif
578
579
580
581
582
583#ifdef CONFIG_PRINTK
584#define printk_once(fmt, ...) \
585 DO_ONCE_LITE(printk, fmt, ##__VA_ARGS__)
586#define printk_deferred_once(fmt, ...) \
587 DO_ONCE_LITE(printk_deferred, fmt, ##__VA_ARGS__)
588#else
589#define printk_once(fmt, ...) \
590 no_printk(fmt, ##__VA_ARGS__)
591#define printk_deferred_once(fmt, ...) \
592 no_printk(fmt, ##__VA_ARGS__)
593#endif
594
595#define pr_emerg_once(fmt, ...) \
596 printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
597#define pr_alert_once(fmt, ...) \
598 printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
599#define pr_crit_once(fmt, ...) \
600 printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
601#define pr_err_once(fmt, ...) \
602 printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
603#define pr_warn_once(fmt, ...) \
604 printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
605#define pr_notice_once(fmt, ...) \
606 printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
607#define pr_info_once(fmt, ...) \
608 printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
609
610
611#if defined(DEBUG)
612#define pr_devel_once(fmt, ...) \
613 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
614#else
615#define pr_devel_once(fmt, ...) \
616 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
617#endif
618
619
620#if defined(DEBUG)
621#define pr_debug_once(fmt, ...) \
622 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
623#else
624#define pr_debug_once(fmt, ...) \
625 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
626#endif
627
628
629
630
631
632#ifdef CONFIG_PRINTK
633#define printk_ratelimited(fmt, ...) \
634({ \
635 static DEFINE_RATELIMIT_STATE(_rs, \
636 DEFAULT_RATELIMIT_INTERVAL, \
637 DEFAULT_RATELIMIT_BURST); \
638 \
639 if (__ratelimit(&_rs)) \
640 printk(fmt, ##__VA_ARGS__); \
641})
642#else
643#define printk_ratelimited(fmt, ...) \
644 no_printk(fmt, ##__VA_ARGS__)
645#endif
646
647#define pr_emerg_ratelimited(fmt, ...) \
648 printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
649#define pr_alert_ratelimited(fmt, ...) \
650 printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
651#define pr_crit_ratelimited(fmt, ...) \
652 printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
653#define pr_err_ratelimited(fmt, ...) \
654 printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
655#define pr_warn_ratelimited(fmt, ...) \
656 printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
657#define pr_notice_ratelimited(fmt, ...) \
658 printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
659#define pr_info_ratelimited(fmt, ...) \
660 printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
661
662
663#if defined(DEBUG)
664#define pr_devel_ratelimited(fmt, ...) \
665 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
666#else
667#define pr_devel_ratelimited(fmt, ...) \
668 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
669#endif
670
671
672#if defined(CONFIG_DYNAMIC_DEBUG) || \
673 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
674
675#define pr_debug_ratelimited(fmt, ...) \
676do { \
677 static DEFINE_RATELIMIT_STATE(_rs, \
678 DEFAULT_RATELIMIT_INTERVAL, \
679 DEFAULT_RATELIMIT_BURST); \
680 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt)); \
681 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
682 __ratelimit(&_rs)) \
683 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__); \
684} while (0)
685#elif defined(DEBUG)
686#define pr_debug_ratelimited(fmt, ...) \
687 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
688#else
689#define pr_debug_ratelimited(fmt, ...) \
690 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
691#endif
692
693extern const struct file_operations kmsg_fops;
694
695enum {
696 DUMP_PREFIX_NONE,
697 DUMP_PREFIX_ADDRESS,
698 DUMP_PREFIX_OFFSET
699};
700extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
701 int groupsize, char *linebuf, size_t linebuflen,
702 bool ascii);
703#ifdef CONFIG_PRINTK
704extern void print_hex_dump(const char *level, const char *prefix_str,
705 int prefix_type, int rowsize, int groupsize,
706 const void *buf, size_t len, bool ascii);
707#else
708static inline void print_hex_dump(const char *level, const char *prefix_str,
709 int prefix_type, int rowsize, int groupsize,
710 const void *buf, size_t len, bool ascii)
711{
712}
713static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
714 const void *buf, size_t len)
715{
716}
717
718#endif
719
720#if defined(CONFIG_DYNAMIC_DEBUG) || \
721 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
722#define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
723 groupsize, buf, len, ascii) \
724 dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
725 groupsize, buf, len, ascii)
726#elif defined(DEBUG)
727#define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
728 groupsize, buf, len, ascii) \
729 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize, \
730 groupsize, buf, len, ascii)
731#else
732static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type,
733 int rowsize, int groupsize,
734 const void *buf, size_t len, bool ascii)
735{
736}
737#endif
738
739
740
741
742
743
744
745
746
747
748
749
750
751#define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
752 print_hex_dump_debug(prefix_str, prefix_type, 16, 1, buf, len, true)
753
754#endif
755