printk.h 10.4 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

8 9 10
extern const char linux_banner[];
extern const char linux_proc_banner[];

11 12
static inline int printk_get_level(const char *buffer)
{
13
	if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
		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)
{
	if (printk_get_level(buffer)) {
		switch (buffer[1]) {
		case '0' ... '7':
		case 'd':	/* KERN_DEFAULT */
29
			return buffer + 2;
30 31 32 33 34
		}
	}
	return buffer;
}

35 36 37 38 39 40 41
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])

42 43 44 45 46 47 48 49 50 51 52
static inline void console_silent(void)
{
	console_loglevel = 0;
}

static inline void console_verbose(void)
{
	if (console_loglevel)
		console_loglevel = 15;
}

53 54 55 56 57 58 59 60 61 62 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
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]: "

89 90 91 92
/*
 * Dummy printk for disabled debugging statements to use whilst maintaining
 * gcc's format and side-effect checking.
 */
93
static inline __printf(1, 2)
94 95 96 97 98
int no_printk(const char *fmt, ...)
{
	return 0;
}

99
#ifdef CONFIG_EARLY_PRINTK
100
extern asmlinkage __printf(1, 2)
101
void early_printk(const char *fmt, ...);
102 103 104 105 106
void early_vprintk(const char *fmt, va_list ap);
#else
static inline __printf(1, 2) __cold
void early_printk(const char *s, ...) { }
#endif
107

108
#ifdef CONFIG_PRINTK
109 110 111 112 113
asmlinkage __printf(5, 0)
int vprintk_emit(int facility, int level,
		 const char *dict, size_t dictlen,
		 const char *fmt, va_list args);

114
asmlinkage __printf(1, 0)
115
int vprintk(const char *fmt, va_list args);
116 117 118 119 120 121

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

122
asmlinkage __printf(1, 2) __cold
123
int printk(const char *fmt, ...);
124

125 126 127 128 129
/*
 * Special printk facility for scheduler use only, _DO_NOT_USE_ !
 */
__printf(1, 2) __cold int printk_sched(const char *fmt, ...);

130 131 132 133 134 135 136 137 138 139 140 141
/*
 * 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;
142
extern int kptr_restrict;
143

144 145
extern void wake_up_klogd(void);

146
void log_buf_kexec_setup(void);
147
void __init setup_log_buf(int early);
148
#else
149
static inline __printf(1, 0)
150 151 152 153
int vprintk(const char *s, va_list args)
{
	return 0;
}
154
static inline __printf(1, 2) __cold
155 156 157 158
int printk(const char *s, ...)
{
	return 0;
}
159 160 161 162 163
static inline __printf(1, 2) __cold
int printk_sched(const char *s, ...)
{
	return 0;
}
164 165 166 167 168 169 170 171 172
static inline int printk_ratelimit(void)
{
	return 0;
}
static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
					  unsigned int interval_msec)
{
	return false;
}
173

174 175 176 177
static inline void wake_up_klogd(void)
{
}

178 179 180
static inline void log_buf_kexec_setup(void)
{
}
181 182 183 184

static inline void setup_log_buf(int early)
{
}
185 186 187 188 189 190 191 192 193
#endif

extern void dump_stack(void) __cold;

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

#define pr_emerg(fmt, ...) \
194
	printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
195
#define pr_alert(fmt, ...) \
196
	printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
197
#define pr_crit(fmt, ...) \
198
	printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
199
#define pr_err(fmt, ...) \
200
	printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
201
#define pr_warning(fmt, ...) \
202
	printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
203 204
#define pr_warn pr_warning
#define pr_notice(fmt, ...) \
205
	printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
206
#define pr_info(fmt, ...) \
207
	printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
208 209 210 211 212 213 214 215 216
#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, ...) \
217
	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
218 219 220
#endif

/* If you are writing a driver, please use dev_dbg instead */
221
#if defined(CONFIG_DYNAMIC_DEBUG)
222 223 224
/* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
#define pr_debug(fmt, ...) \
	dynamic_pr_debug(fmt, ##__VA_ARGS__)
225 226 227
#elif defined(DEBUG)
#define pr_debug(fmt, ...) \
	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
228 229
#else
#define pr_debug(fmt, ...) \
230
	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
231 232
#endif

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
/*
 * Print a one-time message (analogous to WARN_ONCE() et al):
 */

#ifdef CONFIG_PRINTK
#define printk_once(fmt, ...)			\
({						\
	static bool __print_once;		\
						\
	if (!__print_once) {			\
		__print_once = true;		\
		printk(fmt, ##__VA_ARGS__);	\
	}					\
})
#else
#define printk_once(fmt, ...)			\
	no_printk(fmt, ##__VA_ARGS__)
#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__)
268 269 270 271 272 273 274 275 276

#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

277 278 279 280 281 282 283 284 285
/* 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

286 287 288 289 290
/*
 * ratelimited messages with local ratelimit_state,
 * no local ratelimit_state used in the !PRINTK case
 */
#ifdef CONFIG_PRINTK
291 292
#define printk_ratelimited(fmt, ...)					\
({									\
293 294 295 296 297 298 299 300
	static DEFINE_RATELIMIT_STATE(_rs,				\
				      DEFAULT_RATELIMIT_INTERVAL,	\
				      DEFAULT_RATELIMIT_BURST);		\
									\
	if (__ratelimit(&_rs))						\
		printk(fmt, ##__VA_ARGS__);				\
})
#else
301 302
#define printk_ratelimited(fmt, ...)					\
	no_printk(fmt, ##__VA_ARGS__)
303 304
#endif

305
#define pr_emerg_ratelimited(fmt, ...)					\
306
	printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
307
#define pr_alert_ratelimited(fmt, ...)					\
308
	printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
309
#define pr_crit_ratelimited(fmt, ...)					\
310
	printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
311
#define pr_err_ratelimited(fmt, ...)					\
312
	printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
313
#define pr_warn_ratelimited(fmt, ...)					\
314
	printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
315
#define pr_notice_ratelimited(fmt, ...)					\
316
	printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
317
#define pr_info_ratelimited(fmt, ...)					\
318 319
	printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
/* no pr_cont_ratelimited, don't do that... */
320 321 322 323 324 325 326 327 328

#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

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

338 339
extern const struct file_operations kmsg_fops;

340 341 342 343 344 345 346 347 348 349 350 351
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);
352 353 354 355
#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
356 357
extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
				 const void *buf, size_t len);
358
#endif /* defined(CONFIG_DYNAMIC_DEBUG) */
359 360 361 362 363 364 365 366 367 368 369 370 371
#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

372 373 374 375 376 377 378 379 380 381 382 383
#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) */

384
#endif