1#ifndef __KERNEL_PRINTK__
2#define __KERNEL_PRINTK__
3
4#include <stdarg.h>
5#include <linux/init.h>
6#include <linux/kern_levels.h>
7#include <linux/linkage.h>
8
9extern const char linux_banner[];
10extern const char linux_proc_banner[];
11
12extern char *log_buf_addr_get(void);
13extern u32 log_buf_len_get(void);
14
15static inline int printk_get_level(const char *buffer)
16{
17 if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
18 switch (buffer[1]) {
19 case '0' ... '7':
20 case 'd':
21 return buffer[1];
22 }
23 }
24 return 0;
25}
26
27static inline const char *printk_skip_level(const char *buffer)
28{
29 if (printk_get_level(buffer)) {
30 switch (buffer[1]) {
31 case '0' ... '7':
32 case 'd':
33 return buffer + 2;
34 }
35 }
36 return buffer;
37}
38
39extern int console_printk[];
40
41#define console_loglevel (console_printk[0])
42#define default_message_loglevel (console_printk[1])
43#define minimum_console_loglevel (console_printk[2])
44#define default_console_loglevel (console_printk[3])
45
46static inline void console_silent(void)
47{
48 console_loglevel = 0;
49}
50
51static inline void console_verbose(void)
52{
53 if (console_loglevel)
54 console_loglevel = 15;
55}
56
57struct va_format {
58 const char *fmt;
59 va_list *va;
60};
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82#define FW_BUG "[Firmware Bug]: "
83#define FW_WARN "[Firmware Warn]: "
84#define FW_INFO "[Firmware Info]: "
85
86
87
88
89
90
91#define HW_ERR "[Hardware Error]: "
92
93
94
95
96
97static inline __printf(1, 2)
98int no_printk(const char *fmt, ...)
99{
100 return 0;
101}
102
103#ifdef CONFIG_EARLY_PRINTK
104extern asmlinkage __printf(1, 2)
105void early_printk(const char *fmt, ...);
106void early_vprintk(const char *fmt, va_list ap);
107#else
108static inline __printf(1, 2) __cold
109void early_printk(const char *s, ...) { }
110#endif
111
112#ifdef CONFIG_PRINTK
113asmlinkage __printf(5, 0)
114int vprintk_emit(int facility, int level,
115 const char *dict, size_t dictlen,
116 const char *fmt, va_list args);
117
118asmlinkage __printf(1, 0)
119int vprintk(const char *fmt, va_list args);
120
121asmlinkage __printf(5, 6) __cold
122asmlinkage int printk_emit(int facility, int level,
123 const char *dict, size_t dictlen,
124 const char *fmt, ...);
125
126asmlinkage __printf(1, 2) __cold
127int printk(const char *fmt, ...);
128
129
130
131
132__printf(1, 2) __cold int printk_deferred(const char *fmt, ...);
133
134
135
136
137
138
139extern int __printk_ratelimit(const char *func);
140#define printk_ratelimit() __printk_ratelimit(__func__)
141extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
142 unsigned int interval_msec);
143
144extern int printk_delay_msec;
145extern int dmesg_restrict;
146extern int kptr_restrict;
147
148extern void wake_up_klogd(void);
149
150typedef int(*printk_func_t)(const char *fmt, va_list args);
151
152void log_buf_vmcoreinfo_setup(void);
153void __init setup_log_buf(int early);
154void dump_stack_set_arch_desc(const char *fmt, ...);
155void dump_stack_print_info(const char *log_lvl);
156void show_regs_print_info(const char *log_lvl);
157#else
158static inline __printf(1, 0)
159int vprintk(const char *s, va_list args)
160{
161 return 0;
162}
163static inline __printf(1, 2) __cold
164int printk(const char *s, ...)
165{
166 return 0;
167}
168static inline __printf(1, 2) __cold
169int printk_deferred(const char *s, ...)
170{
171 return 0;
172}
173static inline int printk_ratelimit(void)
174{
175 return 0;
176}
177static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
178 unsigned int interval_msec)
179{
180 return false;
181}
182
183static inline void wake_up_klogd(void)
184{
185}
186
187static inline void log_buf_vmcoreinfo_setup(void)
188{
189}
190
191static inline void setup_log_buf(int early)
192{
193}
194
195static inline void dump_stack_set_arch_desc(const char *fmt, ...)
196{
197}
198
199static inline void dump_stack_print_info(const char *log_lvl)
200{
201}
202
203static inline void show_regs_print_info(const char *log_lvl)
204{
205}
206#endif
207
208extern void dump_stack(void) __cold;
209
210#ifndef pr_fmt
211#define pr_fmt(fmt) fmt
212#endif
213
214#define pr_emerg(fmt, ...) \
215 printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
216#define pr_alert(fmt, ...) \
217 printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
218#define pr_crit(fmt, ...) \
219 printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
220#define pr_err(fmt, ...) \
221 printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
222#define pr_warning(fmt, ...) \
223 printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
224#define pr_warn pr_warning
225#define pr_notice(fmt, ...) \
226 printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
227#define pr_info(fmt, ...) \
228 printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
229#define pr_cont(fmt, ...) \
230 printk(KERN_CONT fmt, ##__VA_ARGS__)
231
232
233#ifdef DEBUG
234#define pr_devel(fmt, ...) \
235 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
236#else
237#define pr_devel(fmt, ...) \
238 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
239#endif
240
241#include <linux/dynamic_debug.h>
242
243
244#if defined(CONFIG_DYNAMIC_DEBUG)
245
246#define pr_debug(fmt, ...) \
247 dynamic_pr_debug(fmt, ##__VA_ARGS__)
248#elif defined(DEBUG)
249#define pr_debug(fmt, ...) \
250 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
251#else
252#define pr_debug(fmt, ...) \
253 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
254#endif
255
256
257
258
259
260#ifdef CONFIG_PRINTK
261#define printk_once(fmt, ...) \
262({ \
263 static bool __print_once; \
264 \
265 if (!__print_once) { \
266 __print_once = true; \
267 printk(fmt, ##__VA_ARGS__); \
268 } \
269})
270#define printk_deferred_once(fmt, ...) \
271({ \
272 static bool __print_once __read_mostly; \
273 \
274 if (!__print_once) { \
275 __print_once = true; \
276 printk_deferred(fmt, ##__VA_ARGS__); \
277 } \
278})
279#else
280#define printk_once(fmt, ...) \
281 no_printk(fmt, ##__VA_ARGS__)
282#define printk_deferred_once(fmt, ...) \
283 no_printk(fmt, ##__VA_ARGS__)
284#endif
285
286#define pr_emerg_once(fmt, ...) \
287 printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
288#define pr_alert_once(fmt, ...) \
289 printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
290#define pr_crit_once(fmt, ...) \
291 printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
292#define pr_err_once(fmt, ...) \
293 printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
294#define pr_warn_once(fmt, ...) \
295 printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
296#define pr_notice_once(fmt, ...) \
297 printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
298#define pr_info_once(fmt, ...) \
299 printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
300#define pr_cont_once(fmt, ...) \
301 printk_once(KERN_CONT pr_fmt(fmt), ##__VA_ARGS__)
302
303#if defined(DEBUG)
304#define pr_devel_once(fmt, ...) \
305 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
306#else
307#define pr_devel_once(fmt, ...) \
308 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
309#endif
310
311
312#if defined(DEBUG)
313#define pr_debug_once(fmt, ...) \
314 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
315#else
316#define pr_debug_once(fmt, ...) \
317 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
318#endif
319
320
321
322
323
324#ifdef CONFIG_PRINTK
325#define printk_ratelimited(fmt, ...) \
326({ \
327 static DEFINE_RATELIMIT_STATE(_rs, \
328 DEFAULT_RATELIMIT_INTERVAL, \
329 DEFAULT_RATELIMIT_BURST); \
330 \
331 if (__ratelimit(&_rs)) \
332 printk(fmt, ##__VA_ARGS__); \
333})
334#else
335#define printk_ratelimited(fmt, ...) \
336 no_printk(fmt, ##__VA_ARGS__)
337#endif
338
339#define pr_emerg_ratelimited(fmt, ...) \
340 printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
341#define pr_alert_ratelimited(fmt, ...) \
342 printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
343#define pr_crit_ratelimited(fmt, ...) \
344 printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
345#define pr_err_ratelimited(fmt, ...) \
346 printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
347#define pr_warn_ratelimited(fmt, ...) \
348 printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
349#define pr_notice_ratelimited(fmt, ...) \
350 printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
351#define pr_info_ratelimited(fmt, ...) \
352 printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
353
354
355#if defined(DEBUG)
356#define pr_devel_ratelimited(fmt, ...) \
357 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
358#else
359#define pr_devel_ratelimited(fmt, ...) \
360 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
361#endif
362
363
364#if defined(CONFIG_DYNAMIC_DEBUG)
365
366#define pr_debug_ratelimited(fmt, ...) \
367do { \
368 static DEFINE_RATELIMIT_STATE(_rs, \
369 DEFAULT_RATELIMIT_INTERVAL, \
370 DEFAULT_RATELIMIT_BURST); \
371 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt)); \
372 if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) && \
373 __ratelimit(&_rs)) \
374 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__); \
375} while (0)
376#elif defined(DEBUG)
377#define pr_debug_ratelimited(fmt, ...) \
378 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
379#else
380#define pr_debug_ratelimited(fmt, ...) \
381 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
382#endif
383
384extern const struct file_operations kmsg_fops;
385
386enum {
387 DUMP_PREFIX_NONE,
388 DUMP_PREFIX_ADDRESS,
389 DUMP_PREFIX_OFFSET
390};
391extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
392 int groupsize, char *linebuf, size_t linebuflen,
393 bool ascii);
394#ifdef CONFIG_PRINTK
395extern void print_hex_dump(const char *level, const char *prefix_str,
396 int prefix_type, int rowsize, int groupsize,
397 const void *buf, size_t len, bool ascii);
398#if defined(CONFIG_DYNAMIC_DEBUG)
399#define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
400 dynamic_hex_dump(prefix_str, prefix_type, 16, 1, buf, len, true)
401#else
402extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
403 const void *buf, size_t len);
404#endif
405#else
406static inline void print_hex_dump(const char *level, const char *prefix_str,
407 int prefix_type, int rowsize, int groupsize,
408 const void *buf, size_t len, bool ascii)
409{
410}
411static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
412 const void *buf, size_t len)
413{
414}
415
416#endif
417
418#if defined(CONFIG_DYNAMIC_DEBUG)
419#define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
420 groupsize, buf, len, ascii) \
421 dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
422 groupsize, buf, len, ascii)
423#else
424#define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
425 groupsize, buf, len, ascii) \
426 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize, \
427 groupsize, buf, len, ascii)
428#endif
429
430#endif
431