printk.h 12.7 KB
Newer Older
1 2 3
#ifndef __KERNEL_PRINTK__
#define __KERNEL_PRINTK__

4
#include <stdarg.h>
5
#include <linux/init.h>
6
#include <linux/kern_levels.h>
7
#include <linux/linkage.h>
8
#include <linux/cache.h>
9

10 11 12
extern const char linux_banner[];
extern const char linux_proc_banner[];

13 14 15
extern char *log_buf_addr_get(void);
extern u32 log_buf_len_get(void);

16 17
static inline int printk_get_level(const char *buffer)
{
18
	if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
19 20 21 22 23 24 25 26 27 28 29
		switch (buffer[1]) {
		case '0' ... '7':
		case 'd':	/* KERN_DEFAULT */
			return buffer[1];
		}
	}
	return 0;
}

static inline const char *printk_skip_level(const char *buffer)
{
30 31 32
	if (printk_get_level(buffer))
		return buffer + 2;

33 34 35
	return buffer;
}

36
/* printk's without a loglevel use this.. */
37
#define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT
38 39 40 41 42 43 44 45 46

/* We show everything that is MORE important than this.. */
#define CONSOLE_LOGLEVEL_SILENT  0 /* Mum's the word */
#define CONSOLE_LOGLEVEL_MIN	 1 /* Minimum loglevel we let people use */
#define CONSOLE_LOGLEVEL_QUIET	 4 /* Shhh ..., when booted with "quiet" */
#define CONSOLE_LOGLEVEL_DEFAULT 7 /* anything MORE serious than KERN_DEBUG */
#define CONSOLE_LOGLEVEL_DEBUG	10 /* issue debug messages */
#define CONSOLE_LOGLEVEL_MOTORMOUTH 15	/* You can't shut this one up */

47 48 49 50 51 52 53
extern int console_printk[];

#define console_loglevel (console_printk[0])
#define default_message_loglevel (console_printk[1])
#define minimum_console_loglevel (console_printk[2])
#define default_console_loglevel (console_printk[3])

54 55
static inline void console_silent(void)
{
56
	console_loglevel = CONSOLE_LOGLEVEL_SILENT;
57 58 59 60 61
}

static inline void console_verbose(void)
{
	if (console_loglevel)
62
		console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
63 64
}

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
struct va_format {
	const char *fmt;
	va_list *va;
};

/*
 * FW_BUG
 * Add this to a message where you are sure the firmware is buggy or behaves
 * really stupid or out of spec. Be aware that the responsible BIOS developer
 * should be able to fix this issue or at least get a concrete idea of the
 * problem by reading your message without the need of looking at the kernel
 * code.
 *
 * Use it for definite and high priority BIOS bugs.
 *
 * FW_WARN
 * Use it for not that clear (e.g. could the kernel messed up things already?)
 * and medium priority BIOS bugs.
 *
 * FW_INFO
 * Use this one if you want to tell the user or vendor about something
 * suspicious, but generally harmless related to the firmware.
 *
 * Use it for information or very low priority BIOS bugs.
 */
#define FW_BUG		"[Firmware Bug]: "
#define FW_WARN		"[Firmware Warn]: "
#define FW_INFO		"[Firmware Info]: "

/*
 * HW_ERR
 * Add this to a message for hardware errors, so that user can report
 * it to hardware vendor instead of LKML or software vendor.
 */
#define HW_ERR		"[Hardware Error]: "

N
Neil Horman 已提交
101 102 103 104 105 106 107
/*
 * DEPRECATED
 * Add this to a message whenever you want to warn user space about the use
 * of a deprecated aspect of an API so they can stop using it
 */
#define DEPRECATED	"[Deprecated]: "

108 109 110 111
/*
 * Dummy printk for disabled debugging statements to use whilst maintaining
 * gcc's format and side-effect checking.
 */
112
static inline __printf(1, 2)
113 114 115 116 117
int no_printk(const char *fmt, ...)
{
	return 0;
}

118
#ifdef CONFIG_EARLY_PRINTK
119
extern asmlinkage __printf(1, 2)
120
void early_printk(const char *fmt, ...);
121 122 123 124 125
void early_vprintk(const char *fmt, va_list ap);
#else
static inline __printf(1, 2) __cold
void early_printk(const char *s, ...) { }
#endif
126

127
#ifdef CONFIG_PRINTK
128 129 130 131 132
asmlinkage __printf(5, 0)
int vprintk_emit(int facility, int level,
		 const char *dict, size_t dictlen,
		 const char *fmt, va_list args);

133
asmlinkage __printf(1, 0)
134
int vprintk(const char *fmt, va_list args);
135 136

asmlinkage __printf(5, 6) __cold
137 138 139
int printk_emit(int facility, int level,
		const char *dict, size_t dictlen,
		const char *fmt, ...);
140

141
asmlinkage __printf(1, 2) __cold
142
int printk(const char *fmt, ...);
143

144
/*
145
 * Special printk facility for scheduler/timekeeping use only, _DO_NOT_USE_ !
146
 */
147
__printf(1, 2) __cold int printk_deferred(const char *fmt, ...);
148

149 150 151 152 153 154 155 156 157 158 159 160
/*
 * Please don't use printk_ratelimit(), because it shares ratelimiting state
 * with all other unrelated printk_ratelimit() callsites.  Instead use
 * printk_ratelimited() or plain old __ratelimit().
 */
extern int __printk_ratelimit(const char *func);
#define printk_ratelimit() __printk_ratelimit(__func__)
extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
				   unsigned int interval_msec);

extern int printk_delay_msec;
extern int dmesg_restrict;
161
extern int kptr_restrict;
162

163 164
extern void wake_up_klogd(void);

165
void log_buf_kexec_setup(void);
166
void __init setup_log_buf(int early);
167
void dump_stack_set_arch_desc(const char *fmt, ...);
168
void dump_stack_print_info(const char *log_lvl);
169
void show_regs_print_info(const char *log_lvl);
170
#else
171
static inline __printf(1, 0)
172 173 174 175
int vprintk(const char *s, va_list args)
{
	return 0;
}
176
static inline __printf(1, 2) __cold
177 178 179 180
int printk(const char *s, ...)
{
	return 0;
}
181
static inline __printf(1, 2) __cold
182
int printk_deferred(const char *s, ...)
183 184 185
{
	return 0;
}
186 187 188 189 190 191 192 193 194
static inline int printk_ratelimit(void)
{
	return 0;
}
static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
					  unsigned int interval_msec)
{
	return false;
}
195

196 197 198 199
static inline void wake_up_klogd(void)
{
}

200 201 202
static inline void log_buf_kexec_setup(void)
{
}
203 204 205 206

static inline void setup_log_buf(int early)
{
}
207

208 209 210 211
static inline void dump_stack_set_arch_desc(const char *fmt, ...)
{
}

212 213 214
static inline void dump_stack_print_info(const char *log_lvl)
{
}
215 216 217 218

static inline void show_regs_print_info(const char *log_lvl)
{
}
219 220
#endif

221
extern asmlinkage void dump_stack(void) __cold;
222 223 224 225 226

#ifndef pr_fmt
#define pr_fmt(fmt) fmt
#endif

227 228 229 230 231 232
/*
 * These can be used to print at the various log levels.
 * All of these will print unconditionally, although note that pr_debug()
 * and other debug macros are compiled out unless either DEBUG is defined
 * or CONFIG_DYNAMIC_DEBUG is set.
 */
233
#define pr_emerg(fmt, ...) \
234
	printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
235
#define pr_alert(fmt, ...) \
236
	printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
237
#define pr_crit(fmt, ...) \
238
	printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
239
#define pr_err(fmt, ...) \
240
	printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
241
#define pr_warning(fmt, ...) \
242
	printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
243 244
#define pr_warn pr_warning
#define pr_notice(fmt, ...) \
245
	printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
246
#define pr_info(fmt, ...) \
247
	printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
248 249 250 251 252 253 254 255 256
#define pr_cont(fmt, ...) \
	printk(KERN_CONT fmt, ##__VA_ARGS__)

/* pr_devel() should produce zero code unless DEBUG is defined */
#ifdef DEBUG
#define pr_devel(fmt, ...) \
	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#else
#define pr_devel(fmt, ...) \
257
	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
258 259
#endif

260 261
#include <linux/dynamic_debug.h>

262
/* If you are writing a driver, please use dev_dbg instead */
263
#if defined(CONFIG_DYNAMIC_DEBUG)
264 265 266
/* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
#define pr_debug(fmt, ...) \
	dynamic_pr_debug(fmt, ##__VA_ARGS__)
267 268 269
#elif defined(DEBUG)
#define pr_debug(fmt, ...) \
	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
270 271
#else
#define pr_debug(fmt, ...) \
272
	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
273 274
#endif

275 276 277 278 279
/*
 * Print a one-time message (analogous to WARN_ONCE() et al):
 */

#ifdef CONFIG_PRINTK
280 281 282 283 284 285 286 287
#define printk_once(fmt, ...)					\
({								\
	static bool __print_once __read_mostly;			\
								\
	if (!__print_once) {					\
		__print_once = true;				\
		printk(fmt, ##__VA_ARGS__);			\
	}							\
288
})
J
John Stultz 已提交
289 290 291 292 293 294 295 296 297
#define printk_deferred_once(fmt, ...)				\
({								\
	static bool __print_once __read_mostly;			\
								\
	if (!__print_once) {					\
		__print_once = true;				\
		printk_deferred(fmt, ##__VA_ARGS__);		\
	}							\
})
298
#else
299
#define printk_once(fmt, ...)					\
300
	no_printk(fmt, ##__VA_ARGS__)
J
John Stultz 已提交
301 302
#define printk_deferred_once(fmt, ...)				\
	no_printk(fmt, ##__VA_ARGS__)
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
#endif

#define pr_emerg_once(fmt, ...)					\
	printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
#define pr_alert_once(fmt, ...)					\
	printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
#define pr_crit_once(fmt, ...)					\
	printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
#define pr_err_once(fmt, ...)					\
	printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
#define pr_warn_once(fmt, ...)					\
	printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
#define pr_notice_once(fmt, ...)				\
	printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
#define pr_info_once(fmt, ...)					\
	printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
#define pr_cont_once(fmt, ...)					\
	printk_once(KERN_CONT pr_fmt(fmt), ##__VA_ARGS__)
321 322 323 324 325 326 327 328 329

#if defined(DEBUG)
#define pr_devel_once(fmt, ...)					\
	printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#else
#define pr_devel_once(fmt, ...)					\
	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#endif

330 331 332 333 334 335 336 337 338
/* If you are writing a driver, please use dev_dbg instead */
#if defined(DEBUG)
#define pr_debug_once(fmt, ...)					\
	printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#else
#define pr_debug_once(fmt, ...)					\
	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#endif

339 340 341 342 343
/*
 * ratelimited messages with local ratelimit_state,
 * no local ratelimit_state used in the !PRINTK case
 */
#ifdef CONFIG_PRINTK
344 345
#define printk_ratelimited(fmt, ...)					\
({									\
346 347 348 349 350 351 352 353
	static DEFINE_RATELIMIT_STATE(_rs,				\
				      DEFAULT_RATELIMIT_INTERVAL,	\
				      DEFAULT_RATELIMIT_BURST);		\
									\
	if (__ratelimit(&_rs))						\
		printk(fmt, ##__VA_ARGS__);				\
})
#else
354 355
#define printk_ratelimited(fmt, ...)					\
	no_printk(fmt, ##__VA_ARGS__)
356 357
#endif

358
#define pr_emerg_ratelimited(fmt, ...)					\
359
	printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
360
#define pr_alert_ratelimited(fmt, ...)					\
361
	printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
362
#define pr_crit_ratelimited(fmt, ...)					\
363
	printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
364
#define pr_err_ratelimited(fmt, ...)					\
365
	printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
366
#define pr_warn_ratelimited(fmt, ...)					\
367
	printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
368
#define pr_notice_ratelimited(fmt, ...)					\
369
	printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
370
#define pr_info_ratelimited(fmt, ...)					\
371 372
	printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
/* no pr_cont_ratelimited, don't do that... */
373 374 375 376 377 378 379 380 381

#if defined(DEBUG)
#define pr_devel_ratelimited(fmt, ...)					\
	printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#else
#define pr_devel_ratelimited(fmt, ...)					\
	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#endif

382
/* If you are writing a driver, please use dev_dbg instead */
383 384 385 386 387 388 389 390 391 392 393 394 395
#if defined(CONFIG_DYNAMIC_DEBUG)
/* descriptor check is first to prevent flooding with "callbacks suppressed" */
#define pr_debug_ratelimited(fmt, ...)					\
do {									\
	static DEFINE_RATELIMIT_STATE(_rs,				\
				      DEFAULT_RATELIMIT_INTERVAL,	\
				      DEFAULT_RATELIMIT_BURST);		\
	DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt);			\
	if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) &&	\
	    __ratelimit(&_rs))						\
		__dynamic_pr_debug(&descriptor, fmt, ##__VA_ARGS__);	\
} while (0)
#elif defined(DEBUG)
396
#define pr_debug_ratelimited(fmt, ...)					\
397 398 399
	printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#else
#define pr_debug_ratelimited(fmt, ...) \
400
	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
401 402
#endif

403 404
extern const struct file_operations kmsg_fops;

405 406 407 408 409 410 411 412 413 414 415 416
enum {
	DUMP_PREFIX_NONE,
	DUMP_PREFIX_ADDRESS,
	DUMP_PREFIX_OFFSET
};
extern void hex_dump_to_buffer(const void *buf, size_t len,
			       int rowsize, int groupsize,
			       char *linebuf, size_t linebuflen, bool ascii);
#ifdef CONFIG_PRINTK
extern void print_hex_dump(const char *level, const char *prefix_str,
			   int prefix_type, int rowsize, int groupsize,
			   const void *buf, size_t len, bool ascii);
417 418 419 420
#if defined(CONFIG_DYNAMIC_DEBUG)
#define print_hex_dump_bytes(prefix_str, prefix_type, buf, len)	\
	dynamic_hex_dump(prefix_str, prefix_type, 16, 1, buf, len, true)
#else
421 422
extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
				 const void *buf, size_t len);
423
#endif /* defined(CONFIG_DYNAMIC_DEBUG) */
424 425 426 427 428 429 430 431 432 433 434 435 436
#else
static inline void print_hex_dump(const char *level, const char *prefix_str,
				  int prefix_type, int rowsize, int groupsize,
				  const void *buf, size_t len, bool ascii)
{
}
static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
					const void *buf, size_t len)
{
}

#endif

437 438 439 440 441 442 443 444 445 446 447 448
#if defined(CONFIG_DYNAMIC_DEBUG)
#define print_hex_dump_debug(prefix_str, prefix_type, rowsize,	\
			     groupsize, buf, len, ascii)	\
	dynamic_hex_dump(prefix_str, prefix_type, rowsize,	\
			 groupsize, buf, len, ascii)
#else
#define print_hex_dump_debug(prefix_str, prefix_type, rowsize,		\
			     groupsize, buf, len, ascii)		\
	print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize,	\
		       groupsize, buf, len, ascii)
#endif /* defined(CONFIG_DYNAMIC_DEBUG) */

449
#endif