printk.h 15.0 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
static inline int printk_get_level(const char *buffer)
{
15
	if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
16 17 18 19 20 21 22 23 24 25 26
		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)
{
27 28 29
	if (printk_get_level(buffer))
		return buffer + 2;

30 31 32
	return buffer;
}

33 34
#define CONSOLE_EXT_LOG_MAX	8192

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

/* 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 */

46 47 48 49 50 51 52
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])

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

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

64 65 66 67 68
/* strlen("ratelimit") + 1 */
#define DEVKMSG_STR_MAX_SIZE 10
extern char devkmsg_log_str[];
struct ctl_table;

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 101 102 103 104
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 已提交
105 106 107 108 109 110 111
/*
 * 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]: "

112 113
/*
 * Dummy printk for disabled debugging statements to use whilst maintaining
114
 * gcc's format checking.
115
 */
116 117 118 119 120 121 122 123
#define no_printk(fmt, ...)				\
({							\
	do {						\
		if (0)					\
			printk(fmt, ##__VA_ARGS__);	\
	} while (0);					\
	0;						\
})
124

125
#ifdef CONFIG_EARLY_PRINTK
126
extern asmlinkage __printf(1, 2)
127
void early_printk(const char *fmt, ...);
128 129 130 131
#else
static inline __printf(1, 2) __cold
void early_printk(const char *s, ...) { }
#endif
132

133 134 135 136 137
#ifdef CONFIG_PRINTK_NMI
extern void printk_nmi_init(void);
extern void printk_nmi_enter(void);
extern void printk_nmi_exit(void);
extern void printk_nmi_flush(void);
138
extern void printk_nmi_flush_on_panic(void);
139 140 141 142 143
#else
static inline void printk_nmi_init(void) { }
static inline void printk_nmi_enter(void) { }
static inline void printk_nmi_exit(void) { }
static inline void printk_nmi_flush(void) { }
144
static inline void printk_nmi_flush_on_panic(void) { }
145
#endif /* PRINTK_NMI */
146

147
#ifdef CONFIG_PRINTK
148 149 150 151 152
asmlinkage __printf(5, 0)
int vprintk_emit(int facility, int level,
		 const char *dict, size_t dictlen,
		 const char *fmt, va_list args);

153
asmlinkage __printf(1, 0)
154
int vprintk(const char *fmt, va_list args);
155 156

asmlinkage __printf(5, 6) __cold
157 158 159
int printk_emit(int facility, int level,
		const char *dict, size_t dictlen,
		const char *fmt, ...);
160

161
asmlinkage __printf(1, 2) __cold
162
int printk(const char *fmt, ...);
163

164
/*
165
 * Special printk facility for scheduler/timekeeping use only, _DO_NOT_USE_ !
166
 */
167
__printf(1, 2) __cold int printk_deferred(const char *fmt, ...);
168

169 170 171 172 173 174 175 176 177 178 179 180
/*
 * 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;
181
extern int kptr_restrict;
182

183 184 185 186
extern int
devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, void __user *buf,
			  size_t *lenp, loff_t *ppos);

187 188
extern void wake_up_klogd(void);

189 190
char *log_buf_addr_get(void);
u32 log_buf_len_get(void);
191
void log_buf_kexec_setup(void);
192
void __init setup_log_buf(int early);
193
__printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...);
194
void dump_stack_print_info(const char *log_lvl);
195
void show_regs_print_info(const char *log_lvl);
196
#else
197
static inline __printf(1, 0)
198 199 200 201
int vprintk(const char *s, va_list args)
{
	return 0;
}
202
static inline __printf(1, 2) __cold
203 204 205 206
int printk(const char *s, ...)
{
	return 0;
}
207
static inline __printf(1, 2) __cold
208
int printk_deferred(const char *s, ...)
209 210 211
{
	return 0;
}
212 213 214 215 216 217 218 219 220
static inline int printk_ratelimit(void)
{
	return 0;
}
static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
					  unsigned int interval_msec)
{
	return false;
}
221

222 223 224 225
static inline void wake_up_klogd(void)
{
}

226 227 228 229 230 231 232 233 234 235
static inline char *log_buf_addr_get(void)
{
	return NULL;
}

static inline u32 log_buf_len_get(void)
{
	return 0;
}

236 237 238
static inline void log_buf_kexec_setup(void)
{
}
239 240 241 242

static inline void setup_log_buf(int early)
{
}
243

244
static inline __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...)
245 246 247
{
}

248 249 250
static inline void dump_stack_print_info(const char *log_lvl)
{
}
251 252 253 254

static inline void show_regs_print_info(const char *log_lvl)
{
}
255 256
#endif

257
extern asmlinkage void dump_stack(void) __cold;
258 259 260 261 262

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

263 264 265 266 267 268
/*
 * 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.
 */
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301

#ifdef CONFIG_PRINTK

asmlinkage __printf(1, 2) __cold void __pr_emerg(const char *fmt, ...);
asmlinkage __printf(1, 2) __cold void __pr_alert(const char *fmt, ...);
asmlinkage __printf(1, 2) __cold void __pr_crit(const char *fmt, ...);
asmlinkage __printf(1, 2) __cold void __pr_err(const char *fmt, ...);
asmlinkage __printf(1, 2) __cold void __pr_warn(const char *fmt, ...);
asmlinkage __printf(1, 2) __cold void __pr_notice(const char *fmt, ...);
asmlinkage __printf(1, 2) __cold void __pr_info(const char *fmt, ...);

#define pr_emerg(fmt, ...)	__pr_emerg(pr_fmt(fmt), ##__VA_ARGS__)
#define pr_alert(fmt, ...)	__pr_alert(pr_fmt(fmt), ##__VA_ARGS__)
#define pr_crit(fmt, ...)	__pr_crit(pr_fmt(fmt), ##__VA_ARGS__)
#define pr_err(fmt, ...)	__pr_err(pr_fmt(fmt), ##__VA_ARGS__)
#define pr_warn(fmt, ...)	__pr_warn(pr_fmt(fmt), ##__VA_ARGS__)
#define pr_notice(fmt, ...)	__pr_notice(pr_fmt(fmt), ##__VA_ARGS__)
#define pr_info(fmt, ...)	__pr_info(pr_fmt(fmt), ##__VA_ARGS__)

#else

#define pr_emerg(fmt, ...)	printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
#define pr_alert(fmt, ...)	printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
#define pr_crit(fmt, ...)	printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
#define pr_err(fmt, ...)	printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
#define pr_warn(fmt, ...)	printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
#define pr_notice(fmt, ...)	printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
#define pr_info(fmt, ...)	printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)

#endif

#define pr_warning pr_warn

302 303 304 305 306
/*
 * Like KERN_CONT, pr_cont() should only be used when continuing
 * a line with no newline ('\n') enclosed. Otherwise it defaults
 * back to KERN_DEFAULT.
 */
307 308 309 310 311 312 313 314 315
#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, ...) \
316
	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
317 318
#endif

319

320
/* If you are writing a driver, please use dev_dbg instead */
321
#if defined(CONFIG_DYNAMIC_DEBUG)
322 323
#include <linux/dynamic_debug.h>

324 325 326
/* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
#define pr_debug(fmt, ...) \
	dynamic_pr_debug(fmt, ##__VA_ARGS__)
327 328 329
#elif defined(DEBUG)
#define pr_debug(fmt, ...) \
	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
330 331
#else
#define pr_debug(fmt, ...) \
332
	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
333 334
#endif

335 336 337 338 339
/*
 * Print a one-time message (analogous to WARN_ONCE() et al):
 */

#ifdef CONFIG_PRINTK
340 341 342
#define printk_once(fmt, ...)					\
({								\
	static bool __print_once __read_mostly;			\
343
	bool __ret_print_once = !__print_once;			\
344 345 346 347 348
								\
	if (!__print_once) {					\
		__print_once = true;				\
		printk(fmt, ##__VA_ARGS__);			\
	}							\
349
	unlikely(__ret_print_once);				\
350
})
J
John Stultz 已提交
351 352 353
#define printk_deferred_once(fmt, ...)				\
({								\
	static bool __print_once __read_mostly;			\
354
	bool __ret_print_once = !__print_once;			\
J
John Stultz 已提交
355 356 357 358 359
								\
	if (!__print_once) {					\
		__print_once = true;				\
		printk_deferred(fmt, ##__VA_ARGS__);		\
	}							\
360
	unlikely(__ret_print_once);				\
J
John Stultz 已提交
361
})
362
#else
363
#define printk_once(fmt, ...)					\
364
	no_printk(fmt, ##__VA_ARGS__)
J
John Stultz 已提交
365 366
#define printk_deferred_once(fmt, ...)				\
	no_printk(fmt, ##__VA_ARGS__)
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
#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__)
385 386 387 388 389 390 391 392 393

#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

394 395 396 397 398 399 400 401 402
/* 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

403 404 405 406 407
/*
 * ratelimited messages with local ratelimit_state,
 * no local ratelimit_state used in the !PRINTK case
 */
#ifdef CONFIG_PRINTK
408 409
#define printk_ratelimited(fmt, ...)					\
({									\
410 411 412 413 414 415 416 417
	static DEFINE_RATELIMIT_STATE(_rs,				\
				      DEFAULT_RATELIMIT_INTERVAL,	\
				      DEFAULT_RATELIMIT_BURST);		\
									\
	if (__ratelimit(&_rs))						\
		printk(fmt, ##__VA_ARGS__);				\
})
#else
418 419
#define printk_ratelimited(fmt, ...)					\
	no_printk(fmt, ##__VA_ARGS__)
420 421
#endif

422
#define pr_emerg_ratelimited(fmt, ...)					\
423
	printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
424
#define pr_alert_ratelimited(fmt, ...)					\
425
	printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
426
#define pr_crit_ratelimited(fmt, ...)					\
427
	printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
428
#define pr_err_ratelimited(fmt, ...)					\
429
	printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
430
#define pr_warn_ratelimited(fmt, ...)					\
431
	printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
432
#define pr_notice_ratelimited(fmt, ...)					\
433
	printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
434
#define pr_info_ratelimited(fmt, ...)					\
435 436
	printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
/* no pr_cont_ratelimited, don't do that... */
437 438 439 440 441 442 443 444 445

#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

446
/* If you are writing a driver, please use dev_dbg instead */
447 448 449 450 451 452 453
#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);		\
454
	DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt));		\
455 456
	if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) &&	\
	    __ratelimit(&_rs))						\
457
		__dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__);	\
458 459
} while (0)
#elif defined(DEBUG)
460
#define pr_debug_ratelimited(fmt, ...)					\
461 462 463
	printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#else
#define pr_debug_ratelimited(fmt, ...) \
464
	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
465 466
#endif

467 468
extern const struct file_operations kmsg_fops;

469 470 471 472 473
enum {
	DUMP_PREFIX_NONE,
	DUMP_PREFIX_ADDRESS,
	DUMP_PREFIX_OFFSET
};
474 475 476
extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
			      int groupsize, char *linebuf, size_t linebuflen,
			      bool ascii);
477 478 479 480
#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);
481 482 483 484
#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
485 486
extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
				 const void *buf, size_t len);
487
#endif /* defined(CONFIG_DYNAMIC_DEBUG) */
488 489 490 491 492 493 494 495 496 497 498 499 500
#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

501 502 503 504 505
#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)
506
#elif defined(DEBUG)
507 508 509 510
#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)
511 512 513 514 515 516 517
#else
static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type,
					int rowsize, int groupsize,
					const void *buf, size_t len, bool ascii)
{
}
#endif
518

519
#endif