xref: /linux-6.15/include/linux/printk.h (revision 8fdff1dc)
1 #ifndef __KERNEL_PRINTK__
2 #define __KERNEL_PRINTK__
3 
4 #include <linux/init.h>
5 #include <linux/kern_levels.h>
6 
7 extern const char linux_banner[];
8 extern const char linux_proc_banner[];
9 
10 static inline int printk_get_level(const char *buffer)
11 {
12 	if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
13 		switch (buffer[1]) {
14 		case '0' ... '7':
15 		case 'd':	/* KERN_DEFAULT */
16 			return buffer[1];
17 		}
18 	}
19 	return 0;
20 }
21 
22 static inline const char *printk_skip_level(const char *buffer)
23 {
24 	if (printk_get_level(buffer)) {
25 		switch (buffer[1]) {
26 		case '0' ... '7':
27 		case 'd':	/* KERN_DEFAULT */
28 			return buffer + 2;
29 		}
30 	}
31 	return buffer;
32 }
33 
34 extern int console_printk[];
35 
36 #define console_loglevel (console_printk[0])
37 #define default_message_loglevel (console_printk[1])
38 #define minimum_console_loglevel (console_printk[2])
39 #define default_console_loglevel (console_printk[3])
40 
41 static inline void console_silent(void)
42 {
43 	console_loglevel = 0;
44 }
45 
46 static inline void console_verbose(void)
47 {
48 	if (console_loglevel)
49 		console_loglevel = 15;
50 }
51 
52 struct va_format {
53 	const char *fmt;
54 	va_list *va;
55 };
56 
57 /*
58  * FW_BUG
59  * Add this to a message where you are sure the firmware is buggy or behaves
60  * really stupid or out of spec. Be aware that the responsible BIOS developer
61  * should be able to fix this issue or at least get a concrete idea of the
62  * problem by reading your message without the need of looking at the kernel
63  * code.
64  *
65  * Use it for definite and high priority BIOS bugs.
66  *
67  * FW_WARN
68  * Use it for not that clear (e.g. could the kernel messed up things already?)
69  * and medium priority BIOS bugs.
70  *
71  * FW_INFO
72  * Use this one if you want to tell the user or vendor about something
73  * suspicious, but generally harmless related to the firmware.
74  *
75  * Use it for information or very low priority BIOS bugs.
76  */
77 #define FW_BUG		"[Firmware Bug]: "
78 #define FW_WARN		"[Firmware Warn]: "
79 #define FW_INFO		"[Firmware Info]: "
80 
81 /*
82  * HW_ERR
83  * Add this to a message for hardware errors, so that user can report
84  * it to hardware vendor instead of LKML or software vendor.
85  */
86 #define HW_ERR		"[Hardware Error]: "
87 
88 /*
89  * Dummy printk for disabled debugging statements to use whilst maintaining
90  * gcc's format and side-effect checking.
91  */
92 static inline __printf(1, 2)
93 int no_printk(const char *fmt, ...)
94 {
95 	return 0;
96 }
97 
98 extern asmlinkage __printf(1, 2)
99 void early_printk(const char *fmt, ...);
100 
101 extern int printk_needs_cpu(int cpu);
102 extern void printk_tick(void);
103 
104 #ifdef CONFIG_PRINTK
105 asmlinkage __printf(5, 0)
106 int vprintk_emit(int facility, int level,
107 		 const char *dict, size_t dictlen,
108 		 const char *fmt, va_list args);
109 
110 asmlinkage __printf(1, 0)
111 int vprintk(const char *fmt, va_list args);
112 
113 asmlinkage __printf(5, 6) __cold
114 asmlinkage int printk_emit(int facility, int level,
115 			   const char *dict, size_t dictlen,
116 			   const char *fmt, ...);
117 
118 asmlinkage __printf(1, 2) __cold
119 int printk(const char *fmt, ...);
120 
121 /*
122  * Special printk facility for scheduler use only, _DO_NOT_USE_ !
123  */
124 __printf(1, 2) __cold int printk_sched(const char *fmt, ...);
125 
126 /*
127  * Please don't use printk_ratelimit(), because it shares ratelimiting state
128  * with all other unrelated printk_ratelimit() callsites.  Instead use
129  * printk_ratelimited() or plain old __ratelimit().
130  */
131 extern int __printk_ratelimit(const char *func);
132 #define printk_ratelimit() __printk_ratelimit(__func__)
133 extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
134 				   unsigned int interval_msec);
135 
136 extern int printk_delay_msec;
137 extern int dmesg_restrict;
138 extern int kptr_restrict;
139 
140 void log_buf_kexec_setup(void);
141 void __init setup_log_buf(int early);
142 #else
143 static inline __printf(1, 0)
144 int vprintk(const char *s, va_list args)
145 {
146 	return 0;
147 }
148 static inline __printf(1, 2) __cold
149 int printk(const char *s, ...)
150 {
151 	return 0;
152 }
153 static inline __printf(1, 2) __cold
154 int printk_sched(const char *s, ...)
155 {
156 	return 0;
157 }
158 static inline int printk_ratelimit(void)
159 {
160 	return 0;
161 }
162 static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
163 					  unsigned int interval_msec)
164 {
165 	return false;
166 }
167 
168 static inline void log_buf_kexec_setup(void)
169 {
170 }
171 
172 static inline void setup_log_buf(int early)
173 {
174 }
175 #endif
176 
177 extern void dump_stack(void) __cold;
178 
179 #ifndef pr_fmt
180 #define pr_fmt(fmt) fmt
181 #endif
182 
183 #define pr_emerg(fmt, ...) \
184 	printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
185 #define pr_alert(fmt, ...) \
186 	printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
187 #define pr_crit(fmt, ...) \
188 	printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
189 #define pr_err(fmt, ...) \
190 	printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
191 #define pr_warning(fmt, ...) \
192 	printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
193 #define pr_warn pr_warning
194 #define pr_notice(fmt, ...) \
195 	printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
196 #define pr_info(fmt, ...) \
197 	printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
198 #define pr_cont(fmt, ...) \
199 	printk(KERN_CONT fmt, ##__VA_ARGS__)
200 
201 /* pr_devel() should produce zero code unless DEBUG is defined */
202 #ifdef DEBUG
203 #define pr_devel(fmt, ...) \
204 	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
205 #else
206 #define pr_devel(fmt, ...) \
207 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
208 #endif
209 
210 /* If you are writing a driver, please use dev_dbg instead */
211 #if defined(CONFIG_DYNAMIC_DEBUG)
212 /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
213 #define pr_debug(fmt, ...) \
214 	dynamic_pr_debug(fmt, ##__VA_ARGS__)
215 #elif defined(DEBUG)
216 #define pr_debug(fmt, ...) \
217 	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
218 #else
219 #define pr_debug(fmt, ...) \
220 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
221 #endif
222 
223 /*
224  * Print a one-time message (analogous to WARN_ONCE() et al):
225  */
226 
227 #ifdef CONFIG_PRINTK
228 #define printk_once(fmt, ...)			\
229 ({						\
230 	static bool __print_once;		\
231 						\
232 	if (!__print_once) {			\
233 		__print_once = true;		\
234 		printk(fmt, ##__VA_ARGS__);	\
235 	}					\
236 })
237 #else
238 #define printk_once(fmt, ...)			\
239 	no_printk(fmt, ##__VA_ARGS__)
240 #endif
241 
242 #define pr_emerg_once(fmt, ...)					\
243 	printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
244 #define pr_alert_once(fmt, ...)					\
245 	printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
246 #define pr_crit_once(fmt, ...)					\
247 	printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
248 #define pr_err_once(fmt, ...)					\
249 	printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
250 #define pr_warn_once(fmt, ...)					\
251 	printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
252 #define pr_notice_once(fmt, ...)				\
253 	printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
254 #define pr_info_once(fmt, ...)					\
255 	printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
256 #define pr_cont_once(fmt, ...)					\
257 	printk_once(KERN_CONT pr_fmt(fmt), ##__VA_ARGS__)
258 /* If you are writing a driver, please use dev_dbg instead */
259 #if defined(DEBUG)
260 #define pr_debug_once(fmt, ...)					\
261 	printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
262 #else
263 #define pr_debug_once(fmt, ...)					\
264 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
265 #endif
266 
267 /*
268  * ratelimited messages with local ratelimit_state,
269  * no local ratelimit_state used in the !PRINTK case
270  */
271 #ifdef CONFIG_PRINTK
272 #define printk_ratelimited(fmt, ...)					\
273 ({									\
274 	static DEFINE_RATELIMIT_STATE(_rs,				\
275 				      DEFAULT_RATELIMIT_INTERVAL,	\
276 				      DEFAULT_RATELIMIT_BURST);		\
277 									\
278 	if (__ratelimit(&_rs))						\
279 		printk(fmt, ##__VA_ARGS__);				\
280 })
281 #else
282 #define printk_ratelimited(fmt, ...)					\
283 	no_printk(fmt, ##__VA_ARGS__)
284 #endif
285 
286 #define pr_emerg_ratelimited(fmt, ...)					\
287 	printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
288 #define pr_alert_ratelimited(fmt, ...)					\
289 	printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
290 #define pr_crit_ratelimited(fmt, ...)					\
291 	printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
292 #define pr_err_ratelimited(fmt, ...)					\
293 	printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
294 #define pr_warn_ratelimited(fmt, ...)					\
295 	printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
296 #define pr_notice_ratelimited(fmt, ...)					\
297 	printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
298 #define pr_info_ratelimited(fmt, ...)					\
299 	printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
300 /* no pr_cont_ratelimited, don't do that... */
301 /* If you are writing a driver, please use dev_dbg instead */
302 #if defined(DEBUG)
303 #define pr_debug_ratelimited(fmt, ...)					\
304 	printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
305 #else
306 #define pr_debug_ratelimited(fmt, ...) \
307 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
308 #endif
309 
310 extern const struct file_operations kmsg_fops;
311 
312 enum {
313 	DUMP_PREFIX_NONE,
314 	DUMP_PREFIX_ADDRESS,
315 	DUMP_PREFIX_OFFSET
316 };
317 extern void hex_dump_to_buffer(const void *buf, size_t len,
318 			       int rowsize, int groupsize,
319 			       char *linebuf, size_t linebuflen, bool ascii);
320 #ifdef CONFIG_PRINTK
321 extern void print_hex_dump(const char *level, const char *prefix_str,
322 			   int prefix_type, int rowsize, int groupsize,
323 			   const void *buf, size_t len, bool ascii);
324 extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
325 				 const void *buf, size_t len);
326 #else
327 static inline void print_hex_dump(const char *level, const char *prefix_str,
328 				  int prefix_type, int rowsize, int groupsize,
329 				  const void *buf, size_t len, bool ascii)
330 {
331 }
332 static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
333 					const void *buf, size_t len)
334 {
335 }
336 
337 #endif
338 
339 #endif
340