0001
0002 #ifndef __KERNEL_PRINTK__
0003 #define __KERNEL_PRINTK__
0004
0005 #include <linux/stdarg.h>
0006 #include <linux/init.h>
0007 #include <linux/kern_levels.h>
0008 #include <linux/linkage.h>
0009 #include <linux/ratelimit_types.h>
0010 #include <linux/once_lite.h>
0011
0012 extern const char linux_banner[];
0013 extern const char linux_proc_banner[];
0014
0015 extern int oops_in_progress;
0016
0017 #define PRINTK_MAX_SINGLE_HEADER_LEN 2
0018
0019 static inline int printk_get_level(const char *buffer)
0020 {
0021 if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
0022 switch (buffer[1]) {
0023 case '0' ... '7':
0024 case 'c':
0025 return buffer[1];
0026 }
0027 }
0028 return 0;
0029 }
0030
0031 static inline const char *printk_skip_level(const char *buffer)
0032 {
0033 if (printk_get_level(buffer))
0034 return buffer + 2;
0035
0036 return buffer;
0037 }
0038
0039 static inline const char *printk_skip_headers(const char *buffer)
0040 {
0041 while (printk_get_level(buffer))
0042 buffer = printk_skip_level(buffer);
0043
0044 return buffer;
0045 }
0046
0047 #define CONSOLE_EXT_LOG_MAX 8192
0048
0049
0050 #define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT
0051
0052
0053 #define CONSOLE_LOGLEVEL_SILENT 0
0054 #define CONSOLE_LOGLEVEL_MIN 1
0055 #define CONSOLE_LOGLEVEL_DEBUG 10
0056 #define CONSOLE_LOGLEVEL_MOTORMOUTH 15
0057
0058
0059
0060
0061
0062 #define CONSOLE_LOGLEVEL_DEFAULT CONFIG_CONSOLE_LOGLEVEL_DEFAULT
0063 #define CONSOLE_LOGLEVEL_QUIET CONFIG_CONSOLE_LOGLEVEL_QUIET
0064
0065 extern int console_printk[];
0066
0067 #define console_loglevel (console_printk[0])
0068 #define default_message_loglevel (console_printk[1])
0069 #define minimum_console_loglevel (console_printk[2])
0070 #define default_console_loglevel (console_printk[3])
0071
0072 extern void console_verbose(void);
0073
0074
0075 #define DEVKMSG_STR_MAX_SIZE 10
0076 extern char devkmsg_log_str[];
0077 struct ctl_table;
0078
0079 extern int suppress_printk;
0080
0081 struct va_format {
0082 const char *fmt;
0083 va_list *va;
0084 };
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106 #define FW_BUG "[Firmware Bug]: "
0107 #define FW_WARN "[Firmware Warn]: "
0108 #define FW_INFO "[Firmware Info]: "
0109
0110
0111
0112
0113
0114
0115 #define HW_ERR "[Hardware Error]: "
0116
0117
0118
0119
0120
0121
0122 #define DEPRECATED "[Deprecated]: "
0123
0124
0125
0126
0127
0128 #define no_printk(fmt, ...) \
0129 ({ \
0130 if (0) \
0131 printk(fmt, ##__VA_ARGS__); \
0132 0; \
0133 })
0134
0135 #ifdef CONFIG_EARLY_PRINTK
0136 extern asmlinkage __printf(1, 2)
0137 void early_printk(const char *fmt, ...);
0138 #else
0139 static inline __printf(1, 2) __cold
0140 void early_printk(const char *s, ...) { }
0141 #endif
0142
0143 struct dev_printk_info;
0144
0145 #ifdef CONFIG_PRINTK
0146 asmlinkage __printf(4, 0)
0147 int vprintk_emit(int facility, int level,
0148 const struct dev_printk_info *dev_info,
0149 const char *fmt, va_list args);
0150
0151 asmlinkage __printf(1, 0)
0152 int vprintk(const char *fmt, va_list args);
0153
0154 asmlinkage __printf(1, 2) __cold
0155 int _printk(const char *fmt, ...);
0156
0157
0158
0159
0160 __printf(1, 2) __cold int _printk_deferred(const char *fmt, ...);
0161
0162 extern void __printk_safe_enter(void);
0163 extern void __printk_safe_exit(void);
0164
0165
0166
0167
0168
0169 #define printk_deferred_enter __printk_safe_enter
0170 #define printk_deferred_exit __printk_safe_exit
0171
0172 extern bool pr_flush(int timeout_ms, bool reset_on_progress);
0173
0174
0175
0176
0177
0178
0179 extern int __printk_ratelimit(const char *func);
0180 #define printk_ratelimit() __printk_ratelimit(__func__)
0181 extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
0182 unsigned int interval_msec);
0183
0184 extern int printk_delay_msec;
0185 extern int dmesg_restrict;
0186
0187 extern void wake_up_klogd(void);
0188
0189 char *log_buf_addr_get(void);
0190 u32 log_buf_len_get(void);
0191 void log_buf_vmcoreinfo_setup(void);
0192 void __init setup_log_buf(int early);
0193 __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...);
0194 void dump_stack_print_info(const char *log_lvl);
0195 void show_regs_print_info(const char *log_lvl);
0196 extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold;
0197 extern asmlinkage void dump_stack(void) __cold;
0198 void printk_trigger_flush(void);
0199 #else
0200 static inline __printf(1, 0)
0201 int vprintk(const char *s, va_list args)
0202 {
0203 return 0;
0204 }
0205 static inline __printf(1, 2) __cold
0206 int _printk(const char *s, ...)
0207 {
0208 return 0;
0209 }
0210 static inline __printf(1, 2) __cold
0211 int _printk_deferred(const char *s, ...)
0212 {
0213 return 0;
0214 }
0215
0216 static inline void printk_deferred_enter(void)
0217 {
0218 }
0219
0220 static inline void printk_deferred_exit(void)
0221 {
0222 }
0223
0224 static inline bool pr_flush(int timeout_ms, bool reset_on_progress)
0225 {
0226 return true;
0227 }
0228
0229 static inline int printk_ratelimit(void)
0230 {
0231 return 0;
0232 }
0233 static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
0234 unsigned int interval_msec)
0235 {
0236 return false;
0237 }
0238
0239 static inline void wake_up_klogd(void)
0240 {
0241 }
0242
0243 static inline char *log_buf_addr_get(void)
0244 {
0245 return NULL;
0246 }
0247
0248 static inline u32 log_buf_len_get(void)
0249 {
0250 return 0;
0251 }
0252
0253 static inline void log_buf_vmcoreinfo_setup(void)
0254 {
0255 }
0256
0257 static inline void setup_log_buf(int early)
0258 {
0259 }
0260
0261 static inline __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...)
0262 {
0263 }
0264
0265 static inline void dump_stack_print_info(const char *log_lvl)
0266 {
0267 }
0268
0269 static inline void show_regs_print_info(const char *log_lvl)
0270 {
0271 }
0272
0273 static inline void dump_stack_lvl(const char *log_lvl)
0274 {
0275 }
0276
0277 static inline void dump_stack(void)
0278 {
0279 }
0280 static inline void printk_trigger_flush(void)
0281 {
0282 }
0283 #endif
0284
0285 #ifdef CONFIG_SMP
0286 extern int __printk_cpu_sync_try_get(void);
0287 extern void __printk_cpu_sync_wait(void);
0288 extern void __printk_cpu_sync_put(void);
0289
0290 #else
0291
0292 #define __printk_cpu_sync_try_get() true
0293 #define __printk_cpu_sync_wait()
0294 #define __printk_cpu_sync_put()
0295 #endif
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318 #define printk_cpu_sync_get_irqsave(flags) \
0319 for (;;) { \
0320 local_irq_save(flags); \
0321 if (__printk_cpu_sync_try_get()) \
0322 break; \
0323 local_irq_restore(flags); \
0324 __printk_cpu_sync_wait(); \
0325 }
0326
0327
0328
0329
0330
0331
0332 #define printk_cpu_sync_put_irqrestore(flags) \
0333 do { \
0334 __printk_cpu_sync_put(); \
0335 local_irq_restore(flags); \
0336 } while (0)
0337
0338 extern int kptr_restrict;
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353 #ifndef pr_fmt
0354 #define pr_fmt(fmt) fmt
0355 #endif
0356
0357 struct module;
0358
0359 #ifdef CONFIG_PRINTK_INDEX
0360 struct pi_entry {
0361 const char *fmt;
0362 const char *func;
0363 const char *file;
0364 unsigned int line;
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374 const char *level;
0375
0376
0377
0378
0379
0380
0381
0382
0383 const char *subsys_fmt_prefix;
0384 } __packed;
0385
0386 #define __printk_index_emit(_fmt, _level, _subsys_fmt_prefix) \
0387 do { \
0388 if (__builtin_constant_p(_fmt) && __builtin_constant_p(_level)) { \
0389
0390
0391
0392
0393
0394 \
0395 static const struct pi_entry _entry \
0396 __used = { \
0397 .fmt = __builtin_constant_p(_fmt) ? (_fmt) : NULL, \
0398 .func = __func__, \
0399 .file = __FILE__, \
0400 .line = __LINE__, \
0401 .level = __builtin_constant_p(_level) ? (_level) : NULL, \
0402 .subsys_fmt_prefix = _subsys_fmt_prefix,\
0403 }; \
0404 static const struct pi_entry *_entry_ptr \
0405 __used __section(".printk_index") = &_entry; \
0406 } \
0407 } while (0)
0408
0409 #else
0410 #define __printk_index_emit(...) do {} while (0)
0411 #endif
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430 #define printk_index_subsys_emit(subsys_fmt_prefix, level, fmt, ...) \
0431 __printk_index_emit(fmt, level, subsys_fmt_prefix)
0432
0433 #define printk_index_wrap(_p_func, _fmt, ...) \
0434 ({ \
0435 __printk_index_emit(_fmt, NULL, NULL); \
0436 _p_func(_fmt, ##__VA_ARGS__); \
0437 })
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464 #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
0465 #define printk_deferred(fmt, ...) \
0466 printk_index_wrap(_printk_deferred, fmt, ##__VA_ARGS__)
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476 #define pr_emerg(fmt, ...) \
0477 printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
0478
0479
0480
0481
0482
0483
0484
0485
0486 #define pr_alert(fmt, ...) \
0487 printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
0488
0489
0490
0491
0492
0493
0494
0495
0496 #define pr_crit(fmt, ...) \
0497 printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
0498
0499
0500
0501
0502
0503
0504
0505
0506 #define pr_err(fmt, ...) \
0507 printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
0508
0509
0510
0511
0512
0513
0514
0515
0516 #define pr_warn(fmt, ...) \
0517 printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
0518
0519
0520
0521
0522
0523
0524
0525
0526 #define pr_notice(fmt, ...) \
0527 printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
0528
0529
0530
0531
0532
0533
0534
0535
0536 #define pr_info(fmt, ...) \
0537 printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548 #define pr_cont(fmt, ...) \
0549 printk(KERN_CONT fmt, ##__VA_ARGS__)
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561 #ifdef DEBUG
0562 #define pr_devel(fmt, ...) \
0563 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
0564 #else
0565 #define pr_devel(fmt, ...) \
0566 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
0567 #endif
0568
0569
0570
0571 #if defined(CONFIG_DYNAMIC_DEBUG) || \
0572 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
0573 #include <linux/dynamic_debug.h>
0574
0575
0576
0577
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587 #define pr_debug(fmt, ...) \
0588 dynamic_pr_debug(fmt, ##__VA_ARGS__)
0589 #elif defined(DEBUG)
0590 #define pr_debug(fmt, ...) \
0591 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
0592 #else
0593 #define pr_debug(fmt, ...) \
0594 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
0595 #endif
0596
0597
0598
0599
0600
0601 #ifdef CONFIG_PRINTK
0602 #define printk_once(fmt, ...) \
0603 DO_ONCE_LITE(printk, fmt, ##__VA_ARGS__)
0604 #define printk_deferred_once(fmt, ...) \
0605 DO_ONCE_LITE(printk_deferred, fmt, ##__VA_ARGS__)
0606 #else
0607 #define printk_once(fmt, ...) \
0608 no_printk(fmt, ##__VA_ARGS__)
0609 #define printk_deferred_once(fmt, ...) \
0610 no_printk(fmt, ##__VA_ARGS__)
0611 #endif
0612
0613 #define pr_emerg_once(fmt, ...) \
0614 printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
0615 #define pr_alert_once(fmt, ...) \
0616 printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
0617 #define pr_crit_once(fmt, ...) \
0618 printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
0619 #define pr_err_once(fmt, ...) \
0620 printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
0621 #define pr_warn_once(fmt, ...) \
0622 printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
0623 #define pr_notice_once(fmt, ...) \
0624 printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
0625 #define pr_info_once(fmt, ...) \
0626 printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
0627
0628
0629 #if defined(DEBUG)
0630 #define pr_devel_once(fmt, ...) \
0631 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
0632 #else
0633 #define pr_devel_once(fmt, ...) \
0634 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
0635 #endif
0636
0637
0638 #if defined(DEBUG)
0639 #define pr_debug_once(fmt, ...) \
0640 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
0641 #else
0642 #define pr_debug_once(fmt, ...) \
0643 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
0644 #endif
0645
0646
0647
0648
0649
0650 #ifdef CONFIG_PRINTK
0651 #define printk_ratelimited(fmt, ...) \
0652 ({ \
0653 static DEFINE_RATELIMIT_STATE(_rs, \
0654 DEFAULT_RATELIMIT_INTERVAL, \
0655 DEFAULT_RATELIMIT_BURST); \
0656 \
0657 if (__ratelimit(&_rs)) \
0658 printk(fmt, ##__VA_ARGS__); \
0659 })
0660 #else
0661 #define printk_ratelimited(fmt, ...) \
0662 no_printk(fmt, ##__VA_ARGS__)
0663 #endif
0664
0665 #define pr_emerg_ratelimited(fmt, ...) \
0666 printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
0667 #define pr_alert_ratelimited(fmt, ...) \
0668 printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
0669 #define pr_crit_ratelimited(fmt, ...) \
0670 printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
0671 #define pr_err_ratelimited(fmt, ...) \
0672 printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
0673 #define pr_warn_ratelimited(fmt, ...) \
0674 printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
0675 #define pr_notice_ratelimited(fmt, ...) \
0676 printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
0677 #define pr_info_ratelimited(fmt, ...) \
0678 printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
0679
0680
0681 #if defined(DEBUG)
0682 #define pr_devel_ratelimited(fmt, ...) \
0683 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
0684 #else
0685 #define pr_devel_ratelimited(fmt, ...) \
0686 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
0687 #endif
0688
0689
0690 #if defined(CONFIG_DYNAMIC_DEBUG) || \
0691 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
0692
0693 #define pr_debug_ratelimited(fmt, ...) \
0694 do { \
0695 static DEFINE_RATELIMIT_STATE(_rs, \
0696 DEFAULT_RATELIMIT_INTERVAL, \
0697 DEFAULT_RATELIMIT_BURST); \
0698 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt)); \
0699 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
0700 __ratelimit(&_rs)) \
0701 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__); \
0702 } while (0)
0703 #elif defined(DEBUG)
0704 #define pr_debug_ratelimited(fmt, ...) \
0705 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
0706 #else
0707 #define pr_debug_ratelimited(fmt, ...) \
0708 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
0709 #endif
0710
0711 extern const struct file_operations kmsg_fops;
0712
0713 enum {
0714 DUMP_PREFIX_NONE,
0715 DUMP_PREFIX_ADDRESS,
0716 DUMP_PREFIX_OFFSET
0717 };
0718 extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
0719 int groupsize, char *linebuf, size_t linebuflen,
0720 bool ascii);
0721 #ifdef CONFIG_PRINTK
0722 extern void print_hex_dump(const char *level, const char *prefix_str,
0723 int prefix_type, int rowsize, int groupsize,
0724 const void *buf, size_t len, bool ascii);
0725 #else
0726 static inline void print_hex_dump(const char *level, const char *prefix_str,
0727 int prefix_type, int rowsize, int groupsize,
0728 const void *buf, size_t len, bool ascii)
0729 {
0730 }
0731 static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
0732 const void *buf, size_t len)
0733 {
0734 }
0735
0736 #endif
0737
0738 #if defined(CONFIG_DYNAMIC_DEBUG) || \
0739 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
0740 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
0741 groupsize, buf, len, ascii) \
0742 dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
0743 groupsize, buf, len, ascii)
0744 #elif defined(DEBUG)
0745 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
0746 groupsize, buf, len, ascii) \
0747 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize, \
0748 groupsize, buf, len, ascii)
0749 #else
0750 static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type,
0751 int rowsize, int groupsize,
0752 const void *buf, size_t len, bool ascii)
0753 {
0754 }
0755 #endif
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768
0769 #define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
0770 print_hex_dump_debug(prefix_str, prefix_type, 16, 1, buf, len, true)
0771
0772 #endif