1
2#ifndef __KERNEL_PRINTK__
3#define __KERNEL_PRINTK__
4
5#include <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
12extern const char linux_banner[];
13extern const char linux_proc_banner[];
14
15#define PRINTK_MAX_SINGLE_HEADER_LEN 2
16
17static inline int printk_get_level(const char *buffer)
18{
19 if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
20 switch (buffer[1]) {
21 case '0' ... '7':
22 case 'c':
23 return buffer[1];
24 }
25 }
26 return 0;
27}
28
29static inline const char *printk_skip_level(const char *buffer)
30{
31 if (printk_get_level(buffer))
32 return buffer + 2;
33
34 return buffer;
35}
36
37static inline const char *printk_skip_headers(const char *buffer)
38{
39 while (printk_get_level(buffer))
40 buffer = printk_skip_level(buffer);
41
42 return buffer;
43}
44
45#define CONSOLE_EXT_LOG_MAX 8192
46
47
48#define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT
49
50
51#define CONSOLE_LOGLEVEL_SILENT 0
52#define CONSOLE_LOGLEVEL_MIN 1
53#define CONSOLE_LOGLEVEL_DEBUG 10
54#define CONSOLE_LOGLEVEL_MOTORMOUTH 15
55
56
57
58
59
60#define CONSOLE_LOGLEVEL_DEFAULT CONFIG_CONSOLE_LOGLEVEL_DEFAULT
61#define CONSOLE_LOGLEVEL_QUIET CONFIG_CONSOLE_LOGLEVEL_QUIET
62
63extern int console_printk[];
64
65#define console_loglevel (console_printk[0])
66#define default_message_loglevel (console_printk[1])
67#define minimum_console_loglevel (console_printk[2])
68#define default_console_loglevel (console_printk[3])
69
70static inline void console_silent(void)
71{
72 console_loglevel = CONSOLE_LOGLEVEL_SILENT;
73}
74
75static inline void console_verbose(void)
76{
77 if (console_loglevel)
78 console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
79}
80
81
82#define DEVKMSG_STR_MAX_SIZE 10
83extern char devkmsg_log_str[];
84struct ctl_table;
85
86extern int suppress_printk;
87
88struct va_format {
89 const char *fmt;
90 va_list *va;
91};
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113#define FW_BUG "[Firmware Bug]: "
114#define FW_WARN "[Firmware Warn]: "
115#define FW_INFO "[Firmware Info]: "
116
117
118
119
120
121
122#define HW_ERR "[Hardware Error]: "
123
124
125
126
127
128
129#define DEPRECATED "[Deprecated]: "
130
131
132
133
134
135#define no_printk(fmt, ...) \
136({ \
137 if (0) \
138 printk(fmt, ##__VA_ARGS__); \
139 0; \
140})
141
142#ifdef CONFIG_EARLY_PRINTK
143extern asmlinkage __printf(1, 2)
144void early_printk(const char *fmt, ...);
145#else
146static inline __printf(1, 2) __cold
147void early_printk(const char *s, ...) { }
148#endif
149
150#ifdef CONFIG_PRINTK_NMI
151extern void printk_nmi_enter(void);
152extern void printk_nmi_exit(void);
153extern void printk_nmi_direct_enter(void);
154extern void printk_nmi_direct_exit(void);
155#else
156static inline void printk_nmi_enter(void) { }
157static inline void printk_nmi_exit(void) { }
158static inline void printk_nmi_direct_enter(void) { }
159static inline void printk_nmi_direct_exit(void) { }
160#endif
161
162#ifdef CONFIG_PRINTK
163asmlinkage __printf(5, 0)
164int vprintk_emit(int facility, int level,
165 const char *dict, size_t dictlen,
166 const char *fmt, va_list args);
167
168asmlinkage __printf(1, 0)
169int vprintk(const char *fmt, va_list args);
170
171asmlinkage __printf(1, 2) __cold
172int printk(const char *fmt, ...);
173
174
175
176
177__printf(1, 2) __cold int printk_deferred(const char *fmt, ...);
178
179
180
181
182
183
184extern int __printk_ratelimit(const char *func);
185#define printk_ratelimit() __printk_ratelimit(__func__)
186extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
187 unsigned int interval_msec);
188
189extern int printk_delay_msec;
190extern int dmesg_restrict;
191
192extern int
193devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, void *buf,
194 size_t *lenp, loff_t *ppos);
195
196extern void wake_up_klogd(void);
197
198char *log_buf_addr_get(void);
199u32 log_buf_len_get(void);
200void log_buf_vmcoreinfo_setup(void);
201void __init setup_log_buf(int early);
202__printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...);
203void dump_stack_print_info(const char *log_lvl);
204void show_regs_print_info(const char *log_lvl);
205extern asmlinkage void dump_stack(void) __cold;
206extern void printk_safe_flush(void);
207extern void printk_safe_flush_on_panic(void);
208#else
209static inline __printf(1, 0)
210int vprintk(const char *s, va_list args)
211{
212 return 0;
213}
214static inline __printf(1, 2) __cold
215int printk(const char *s, ...)
216{
217 return 0;
218}
219static inline __printf(1, 2) __cold
220int printk_deferred(const char *s, ...)
221{
222 return 0;
223}
224static inline int printk_ratelimit(void)
225{
226 return 0;
227}
228static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
229 unsigned int interval_msec)
230{
231 return false;
232}
233
234static inline void wake_up_klogd(void)
235{
236}
237
238static inline char *log_buf_addr_get(void)
239{
240 return NULL;
241}
242
243static inline u32 log_buf_len_get(void)
244{
245 return 0;
246}
247
248static inline void log_buf_vmcoreinfo_setup(void)
249{
250}
251
252static inline void setup_log_buf(int early)
253{
254}
255
256static inline __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...)
257{
258}
259
260static inline void dump_stack_print_info(const char *log_lvl)
261{
262}
263
264static inline void show_regs_print_info(const char *log_lvl)
265{
266}
267
268static inline void dump_stack(void)
269{
270}
271
272static inline void printk_safe_flush(void)
273{
274}
275
276static inline void printk_safe_flush_on_panic(void)
277{
278}
279#endif
280
281extern int kptr_restrict;
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296#ifndef pr_fmt
297#define pr_fmt(fmt) fmt
298#endif
299
300
301
302
303
304
305
306
307
308#define pr_emerg(fmt, ...) \
309 printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
310
311
312
313
314
315
316
317
318#define pr_alert(fmt, ...) \
319 printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
320
321
322
323
324
325
326
327
328#define pr_crit(fmt, ...) \
329 printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
330
331
332
333
334
335
336
337
338#define pr_err(fmt, ...) \
339 printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
340
341
342
343
344
345
346
347
348#define pr_warn(fmt, ...) \
349 printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
350
351
352
353
354
355
356
357
358#define pr_notice(fmt, ...) \
359 printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
360
361
362
363
364
365
366
367
368#define pr_info(fmt, ...) \
369 printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
370
371
372
373
374
375
376
377
378
379
380#define pr_cont(fmt, ...) \
381 printk(KERN_CONT fmt, ##__VA_ARGS__)
382
383
384
385
386
387
388
389
390
391
392
393#ifdef DEBUG
394#define pr_devel(fmt, ...) \
395 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
396#else
397#define pr_devel(fmt, ...) \
398 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
399#endif
400
401
402
403#if defined(CONFIG_DYNAMIC_DEBUG) || \
404 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
405#include <linux/dynamic_debug.h>
406
407
408
409
410
411
412
413
414
415
416
417
418
419#define pr_debug(fmt, ...) \
420 dynamic_pr_debug(fmt, ##__VA_ARGS__)
421#elif defined(DEBUG)
422#define pr_debug(fmt, ...) \
423 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
424#else
425#define pr_debug(fmt, ...) \
426 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
427#endif
428
429
430
431
432
433#ifdef CONFIG_PRINTK
434#define printk_once(fmt, ...) \
435({ \
436 static bool __section(.data.once) __print_once; \
437 bool __ret_print_once = !__print_once; \
438 \
439 if (!__print_once) { \
440 __print_once = true; \
441 printk(fmt, ##__VA_ARGS__); \
442 } \
443 unlikely(__ret_print_once); \
444})
445#define printk_deferred_once(fmt, ...) \
446({ \
447 static bool __section(.data.once) __print_once; \
448 bool __ret_print_once = !__print_once; \
449 \
450 if (!__print_once) { \
451 __print_once = true; \
452 printk_deferred(fmt, ##__VA_ARGS__); \
453 } \
454 unlikely(__ret_print_once); \
455})
456#else
457#define printk_once(fmt, ...) \
458 no_printk(fmt, ##__VA_ARGS__)
459#define printk_deferred_once(fmt, ...) \
460 no_printk(fmt, ##__VA_ARGS__)
461#endif
462
463#define pr_emerg_once(fmt, ...) \
464 printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
465#define pr_alert_once(fmt, ...) \
466 printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
467#define pr_crit_once(fmt, ...) \
468 printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
469#define pr_err_once(fmt, ...) \
470 printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
471#define pr_warn_once(fmt, ...) \
472 printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
473#define pr_notice_once(fmt, ...) \
474 printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
475#define pr_info_once(fmt, ...) \
476 printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
477
478
479#if defined(DEBUG)
480#define pr_devel_once(fmt, ...) \
481 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
482#else
483#define pr_devel_once(fmt, ...) \
484 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
485#endif
486
487
488#if defined(DEBUG)
489#define pr_debug_once(fmt, ...) \
490 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
491#else
492#define pr_debug_once(fmt, ...) \
493 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
494#endif
495
496
497
498
499
500#ifdef CONFIG_PRINTK
501#define printk_ratelimited(fmt, ...) \
502({ \
503 static DEFINE_RATELIMIT_STATE(_rs, \
504 DEFAULT_RATELIMIT_INTERVAL, \
505 DEFAULT_RATELIMIT_BURST); \
506 \
507 if (__ratelimit(&_rs)) \
508 printk(fmt, ##__VA_ARGS__); \
509})
510#else
511#define printk_ratelimited(fmt, ...) \
512 no_printk(fmt, ##__VA_ARGS__)
513#endif
514
515#define pr_emerg_ratelimited(fmt, ...) \
516 printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
517#define pr_alert_ratelimited(fmt, ...) \
518 printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
519#define pr_crit_ratelimited(fmt, ...) \
520 printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
521#define pr_err_ratelimited(fmt, ...) \
522 printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
523#define pr_warn_ratelimited(fmt, ...) \
524 printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
525#define pr_notice_ratelimited(fmt, ...) \
526 printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
527#define pr_info_ratelimited(fmt, ...) \
528 printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
529
530
531#if defined(DEBUG)
532#define pr_devel_ratelimited(fmt, ...) \
533 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
534#else
535#define pr_devel_ratelimited(fmt, ...) \
536 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
537#endif
538
539
540#if defined(CONFIG_DYNAMIC_DEBUG) || \
541 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
542
543#define pr_debug_ratelimited(fmt, ...) \
544do { \
545 static DEFINE_RATELIMIT_STATE(_rs, \
546 DEFAULT_RATELIMIT_INTERVAL, \
547 DEFAULT_RATELIMIT_BURST); \
548 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt)); \
549 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
550 __ratelimit(&_rs)) \
551 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__); \
552} while (0)
553#elif defined(DEBUG)
554#define pr_debug_ratelimited(fmt, ...) \
555 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
556#else
557#define pr_debug_ratelimited(fmt, ...) \
558 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
559#endif
560
561extern const struct file_operations kmsg_fops;
562
563enum {
564 DUMP_PREFIX_NONE,
565 DUMP_PREFIX_ADDRESS,
566 DUMP_PREFIX_OFFSET
567};
568extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
569 int groupsize, char *linebuf, size_t linebuflen,
570 bool ascii);
571#ifdef CONFIG_PRINTK
572extern void print_hex_dump(const char *level, const char *prefix_str,
573 int prefix_type, int rowsize, int groupsize,
574 const void *buf, size_t len, bool ascii);
575#else
576static inline void print_hex_dump(const char *level, const char *prefix_str,
577 int prefix_type, int rowsize, int groupsize,
578 const void *buf, size_t len, bool ascii)
579{
580}
581static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
582 const void *buf, size_t len)
583{
584}
585
586#endif
587
588#if defined(CONFIG_DYNAMIC_DEBUG) || \
589 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
590#define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
591 groupsize, buf, len, ascii) \
592 dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
593 groupsize, buf, len, ascii)
594#elif defined(DEBUG)
595#define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
596 groupsize, buf, len, ascii) \
597 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize, \
598 groupsize, buf, len, ascii)
599#else
600static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type,
601 int rowsize, int groupsize,
602 const void *buf, size_t len, bool ascii)
603{
604}
605#endif
606
607
608
609
610
611
612
613
614
615
616
617
618
619#define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
620 print_hex_dump_debug(prefix_str, prefix_type, 16, 1, buf, len, true)
621
622#endif
623