at v5.12 18 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef __KERNEL_PRINTK__ 3#define __KERNEL_PRINTK__ 4 5#include <stdarg.h> 6#include <linux/init.h> 7#include <linux/kern_levels.h> 8#include <linux/linkage.h> 9#include <linux/cache.h> 10#include <linux/ratelimit_types.h> 11 12extern const char linux_banner[]; 13extern const char linux_proc_banner[]; 14 15extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */ 16 17#define PRINTK_MAX_SINGLE_HEADER_LEN 2 18 19static inline int printk_get_level(const char *buffer) 20{ 21 if (buffer[0] == KERN_SOH_ASCII && buffer[1]) { 22 switch (buffer[1]) { 23 case '0' ... '7': 24 case 'c': /* KERN_CONT */ 25 return buffer[1]; 26 } 27 } 28 return 0; 29} 30 31static inline const char *printk_skip_level(const char *buffer) 32{ 33 if (printk_get_level(buffer)) 34 return buffer + 2; 35 36 return buffer; 37} 38 39static inline const char *printk_skip_headers(const char *buffer) 40{ 41 while (printk_get_level(buffer)) 42 buffer = printk_skip_level(buffer); 43 44 return buffer; 45} 46 47#define CONSOLE_EXT_LOG_MAX 8192 48 49/* printk's without a loglevel use this.. */ 50#define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT 51 52/* We show everything that is MORE important than this.. */ 53#define CONSOLE_LOGLEVEL_SILENT 0 /* Mum's the word */ 54#define CONSOLE_LOGLEVEL_MIN 1 /* Minimum loglevel we let people use */ 55#define CONSOLE_LOGLEVEL_DEBUG 10 /* issue debug messages */ 56#define CONSOLE_LOGLEVEL_MOTORMOUTH 15 /* You can't shut this one up */ 57 58/* 59 * Default used to be hard-coded at 7, quiet used to be hardcoded at 4, 60 * we're now allowing both to be set from kernel config. 61 */ 62#define CONSOLE_LOGLEVEL_DEFAULT CONFIG_CONSOLE_LOGLEVEL_DEFAULT 63#define CONSOLE_LOGLEVEL_QUIET CONFIG_CONSOLE_LOGLEVEL_QUIET 64 65extern int console_printk[]; 66 67#define console_loglevel (console_printk[0]) 68#define default_message_loglevel (console_printk[1]) 69#define minimum_console_loglevel (console_printk[2]) 70#define default_console_loglevel (console_printk[3]) 71 72static inline void console_silent(void) 73{ 74 console_loglevel = CONSOLE_LOGLEVEL_SILENT; 75} 76 77static inline void console_verbose(void) 78{ 79 if (console_loglevel) 80 console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH; 81} 82 83/* strlen("ratelimit") + 1 */ 84#define DEVKMSG_STR_MAX_SIZE 10 85extern char devkmsg_log_str[]; 86struct ctl_table; 87 88extern int suppress_printk; 89 90struct va_format { 91 const char *fmt; 92 va_list *va; 93}; 94 95/* 96 * FW_BUG 97 * Add this to a message where you are sure the firmware is buggy or behaves 98 * really stupid or out of spec. Be aware that the responsible BIOS developer 99 * should be able to fix this issue or at least get a concrete idea of the 100 * problem by reading your message without the need of looking at the kernel 101 * code. 102 * 103 * Use it for definite and high priority BIOS bugs. 104 * 105 * FW_WARN 106 * Use it for not that clear (e.g. could the kernel messed up things already?) 107 * and medium priority BIOS bugs. 108 * 109 * FW_INFO 110 * Use this one if you want to tell the user or vendor about something 111 * suspicious, but generally harmless related to the firmware. 112 * 113 * Use it for information or very low priority BIOS bugs. 114 */ 115#define FW_BUG "[Firmware Bug]: " 116#define FW_WARN "[Firmware Warn]: " 117#define FW_INFO "[Firmware Info]: " 118 119/* 120 * HW_ERR 121 * Add this to a message for hardware errors, so that user can report 122 * it to hardware vendor instead of LKML or software vendor. 123 */ 124#define HW_ERR "[Hardware Error]: " 125 126/* 127 * DEPRECATED 128 * Add this to a message whenever you want to warn user space about the use 129 * of a deprecated aspect of an API so they can stop using it 130 */ 131#define DEPRECATED "[Deprecated]: " 132 133/* 134 * Dummy printk for disabled debugging statements to use whilst maintaining 135 * gcc's format checking. 136 */ 137#define no_printk(fmt, ...) \ 138({ \ 139 if (0) \ 140 printk(fmt, ##__VA_ARGS__); \ 141 0; \ 142}) 143 144#ifdef CONFIG_EARLY_PRINTK 145extern asmlinkage __printf(1, 2) 146void early_printk(const char *fmt, ...); 147#else 148static inline __printf(1, 2) __cold 149void early_printk(const char *s, ...) { } 150#endif 151 152#ifdef CONFIG_PRINTK_NMI 153extern void printk_nmi_enter(void); 154extern void printk_nmi_exit(void); 155extern void printk_nmi_direct_enter(void); 156extern void printk_nmi_direct_exit(void); 157#else 158static inline void printk_nmi_enter(void) { } 159static inline void printk_nmi_exit(void) { } 160static inline void printk_nmi_direct_enter(void) { } 161static inline void printk_nmi_direct_exit(void) { } 162#endif /* PRINTK_NMI */ 163 164struct dev_printk_info; 165 166#ifdef CONFIG_PRINTK 167asmlinkage __printf(4, 0) 168int vprintk_emit(int facility, int level, 169 const struct dev_printk_info *dev_info, 170 const char *fmt, va_list args); 171 172asmlinkage __printf(1, 0) 173int vprintk(const char *fmt, va_list args); 174 175asmlinkage __printf(1, 2) __cold 176int printk(const char *fmt, ...); 177 178/* 179 * Special printk facility for scheduler/timekeeping use only, _DO_NOT_USE_ ! 180 */ 181__printf(1, 2) __cold int printk_deferred(const char *fmt, ...); 182 183/* 184 * Please don't use printk_ratelimit(), because it shares ratelimiting state 185 * with all other unrelated printk_ratelimit() callsites. Instead use 186 * printk_ratelimited() or plain old __ratelimit(). 187 */ 188extern int __printk_ratelimit(const char *func); 189#define printk_ratelimit() __printk_ratelimit(__func__) 190extern bool printk_timed_ratelimit(unsigned long *caller_jiffies, 191 unsigned int interval_msec); 192 193extern int printk_delay_msec; 194extern int dmesg_restrict; 195 196extern int 197devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, void *buf, 198 size_t *lenp, loff_t *ppos); 199 200extern void wake_up_klogd(void); 201 202char *log_buf_addr_get(void); 203u32 log_buf_len_get(void); 204void log_buf_vmcoreinfo_setup(void); 205void __init setup_log_buf(int early); 206__printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...); 207void dump_stack_print_info(const char *log_lvl); 208void show_regs_print_info(const char *log_lvl); 209extern asmlinkage void dump_stack(void) __cold; 210extern void printk_safe_flush(void); 211extern void printk_safe_flush_on_panic(void); 212#else 213static inline __printf(1, 0) 214int vprintk(const char *s, va_list args) 215{ 216 return 0; 217} 218static inline __printf(1, 2) __cold 219int printk(const char *s, ...) 220{ 221 return 0; 222} 223static inline __printf(1, 2) __cold 224int printk_deferred(const char *s, ...) 225{ 226 return 0; 227} 228static inline int printk_ratelimit(void) 229{ 230 return 0; 231} 232static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, 233 unsigned int interval_msec) 234{ 235 return false; 236} 237 238static inline void wake_up_klogd(void) 239{ 240} 241 242static inline char *log_buf_addr_get(void) 243{ 244 return NULL; 245} 246 247static inline u32 log_buf_len_get(void) 248{ 249 return 0; 250} 251 252static inline void log_buf_vmcoreinfo_setup(void) 253{ 254} 255 256static inline void setup_log_buf(int early) 257{ 258} 259 260static inline __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...) 261{ 262} 263 264static inline void dump_stack_print_info(const char *log_lvl) 265{ 266} 267 268static inline void show_regs_print_info(const char *log_lvl) 269{ 270} 271 272static inline void dump_stack(void) 273{ 274} 275 276static inline void printk_safe_flush(void) 277{ 278} 279 280static inline void printk_safe_flush_on_panic(void) 281{ 282} 283#endif 284 285extern int kptr_restrict; 286 287/** 288 * pr_fmt - used by the pr_*() macros to generate the printk format string 289 * @fmt: format string passed from a pr_*() macro 290 * 291 * This macro can be used to generate a unified format string for pr_*() 292 * macros. A common use is to prefix all pr_*() messages in a file with a common 293 * string. For example, defining this at the top of a source file: 294 * 295 * #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 296 * 297 * would prefix all pr_info, pr_emerg... messages in the file with the module 298 * name. 299 */ 300#ifndef pr_fmt 301#define pr_fmt(fmt) fmt 302#endif 303 304/** 305 * pr_emerg - Print an emergency-level message 306 * @fmt: format string 307 * @...: arguments for the format string 308 * 309 * This macro expands to a printk with KERN_EMERG loglevel. It uses pr_fmt() to 310 * generate the format string. 311 */ 312#define pr_emerg(fmt, ...) \ 313 printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__) 314/** 315 * pr_alert - Print an alert-level message 316 * @fmt: format string 317 * @...: arguments for the format string 318 * 319 * This macro expands to a printk with KERN_ALERT loglevel. It uses pr_fmt() to 320 * generate the format string. 321 */ 322#define pr_alert(fmt, ...) \ 323 printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__) 324/** 325 * pr_crit - Print a critical-level message 326 * @fmt: format string 327 * @...: arguments for the format string 328 * 329 * This macro expands to a printk with KERN_CRIT loglevel. It uses pr_fmt() to 330 * generate the format string. 331 */ 332#define pr_crit(fmt, ...) \ 333 printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__) 334/** 335 * pr_err - Print an error-level message 336 * @fmt: format string 337 * @...: arguments for the format string 338 * 339 * This macro expands to a printk with KERN_ERR loglevel. It uses pr_fmt() to 340 * generate the format string. 341 */ 342#define pr_err(fmt, ...) \ 343 printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__) 344/** 345 * pr_warn - Print a warning-level message 346 * @fmt: format string 347 * @...: arguments for the format string 348 * 349 * This macro expands to a printk with KERN_WARNING loglevel. It uses pr_fmt() 350 * to generate the format string. 351 */ 352#define pr_warn(fmt, ...) \ 353 printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__) 354/** 355 * pr_notice - Print a notice-level message 356 * @fmt: format string 357 * @...: arguments for the format string 358 * 359 * This macro expands to a printk with KERN_NOTICE loglevel. It uses pr_fmt() to 360 * generate the format string. 361 */ 362#define pr_notice(fmt, ...) \ 363 printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__) 364/** 365 * pr_info - Print an info-level message 366 * @fmt: format string 367 * @...: arguments for the format string 368 * 369 * This macro expands to a printk with KERN_INFO loglevel. It uses pr_fmt() to 370 * generate the format string. 371 */ 372#define pr_info(fmt, ...) \ 373 printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__) 374 375/** 376 * pr_cont - Continues a previous log message in the same line. 377 * @fmt: format string 378 * @...: arguments for the format string 379 * 380 * This macro expands to a printk with KERN_CONT loglevel. It should only be 381 * used when continuing a log message with no newline ('\n') enclosed. Otherwise 382 * it defaults back to KERN_DEFAULT loglevel. 383 */ 384#define pr_cont(fmt, ...) \ 385 printk(KERN_CONT fmt, ##__VA_ARGS__) 386 387/** 388 * pr_devel - Print a debug-level message conditionally 389 * @fmt: format string 390 * @...: arguments for the format string 391 * 392 * This macro expands to a printk with KERN_DEBUG loglevel if DEBUG is 393 * defined. Otherwise it does nothing. 394 * 395 * It uses pr_fmt() to generate the format string. 396 */ 397#ifdef DEBUG 398#define pr_devel(fmt, ...) \ 399 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 400#else 401#define pr_devel(fmt, ...) \ 402 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 403#endif 404 405 406/* If you are writing a driver, please use dev_dbg instead */ 407#if defined(CONFIG_DYNAMIC_DEBUG) || \ 408 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE)) 409#include <linux/dynamic_debug.h> 410 411/** 412 * pr_debug - Print a debug-level message conditionally 413 * @fmt: format string 414 * @...: arguments for the format string 415 * 416 * This macro expands to dynamic_pr_debug() if CONFIG_DYNAMIC_DEBUG is 417 * set. Otherwise, if DEBUG is defined, it's equivalent to a printk with 418 * KERN_DEBUG loglevel. If DEBUG is not defined it does nothing. 419 * 420 * It uses pr_fmt() to generate the format string (dynamic_pr_debug() uses 421 * pr_fmt() internally). 422 */ 423#define pr_debug(fmt, ...) \ 424 dynamic_pr_debug(fmt, ##__VA_ARGS__) 425#elif defined(DEBUG) 426#define pr_debug(fmt, ...) \ 427 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 428#else 429#define pr_debug(fmt, ...) \ 430 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 431#endif 432 433/* 434 * Print a one-time message (analogous to WARN_ONCE() et al): 435 */ 436 437#ifdef CONFIG_PRINTK 438#define printk_once(fmt, ...) \ 439({ \ 440 static bool __section(".data.once") __print_once; \ 441 bool __ret_print_once = !__print_once; \ 442 \ 443 if (!__print_once) { \ 444 __print_once = true; \ 445 printk(fmt, ##__VA_ARGS__); \ 446 } \ 447 unlikely(__ret_print_once); \ 448}) 449#define printk_deferred_once(fmt, ...) \ 450({ \ 451 static bool __section(".data.once") __print_once; \ 452 bool __ret_print_once = !__print_once; \ 453 \ 454 if (!__print_once) { \ 455 __print_once = true; \ 456 printk_deferred(fmt, ##__VA_ARGS__); \ 457 } \ 458 unlikely(__ret_print_once); \ 459}) 460#else 461#define printk_once(fmt, ...) \ 462 no_printk(fmt, ##__VA_ARGS__) 463#define printk_deferred_once(fmt, ...) \ 464 no_printk(fmt, ##__VA_ARGS__) 465#endif 466 467#define pr_emerg_once(fmt, ...) \ 468 printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__) 469#define pr_alert_once(fmt, ...) \ 470 printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__) 471#define pr_crit_once(fmt, ...) \ 472 printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__) 473#define pr_err_once(fmt, ...) \ 474 printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__) 475#define pr_warn_once(fmt, ...) \ 476 printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__) 477#define pr_notice_once(fmt, ...) \ 478 printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__) 479#define pr_info_once(fmt, ...) \ 480 printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__) 481/* no pr_cont_once, don't do that... */ 482 483#if defined(DEBUG) 484#define pr_devel_once(fmt, ...) \ 485 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 486#else 487#define pr_devel_once(fmt, ...) \ 488 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 489#endif 490 491/* If you are writing a driver, please use dev_dbg instead */ 492#if defined(DEBUG) 493#define pr_debug_once(fmt, ...) \ 494 printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 495#else 496#define pr_debug_once(fmt, ...) \ 497 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 498#endif 499 500/* 501 * ratelimited messages with local ratelimit_state, 502 * no local ratelimit_state used in the !PRINTK case 503 */ 504#ifdef CONFIG_PRINTK 505#define printk_ratelimited(fmt, ...) \ 506({ \ 507 static DEFINE_RATELIMIT_STATE(_rs, \ 508 DEFAULT_RATELIMIT_INTERVAL, \ 509 DEFAULT_RATELIMIT_BURST); \ 510 \ 511 if (__ratelimit(&_rs)) \ 512 printk(fmt, ##__VA_ARGS__); \ 513}) 514#else 515#define printk_ratelimited(fmt, ...) \ 516 no_printk(fmt, ##__VA_ARGS__) 517#endif 518 519#define pr_emerg_ratelimited(fmt, ...) \ 520 printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__) 521#define pr_alert_ratelimited(fmt, ...) \ 522 printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__) 523#define pr_crit_ratelimited(fmt, ...) \ 524 printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__) 525#define pr_err_ratelimited(fmt, ...) \ 526 printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__) 527#define pr_warn_ratelimited(fmt, ...) \ 528 printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__) 529#define pr_notice_ratelimited(fmt, ...) \ 530 printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__) 531#define pr_info_ratelimited(fmt, ...) \ 532 printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__) 533/* no pr_cont_ratelimited, don't do that... */ 534 535#if defined(DEBUG) 536#define pr_devel_ratelimited(fmt, ...) \ 537 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 538#else 539#define pr_devel_ratelimited(fmt, ...) \ 540 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 541#endif 542 543/* If you are writing a driver, please use dev_dbg instead */ 544#if defined(CONFIG_DYNAMIC_DEBUG) || \ 545 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE)) 546/* descriptor check is first to prevent flooding with "callbacks suppressed" */ 547#define pr_debug_ratelimited(fmt, ...) \ 548do { \ 549 static DEFINE_RATELIMIT_STATE(_rs, \ 550 DEFAULT_RATELIMIT_INTERVAL, \ 551 DEFAULT_RATELIMIT_BURST); \ 552 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt)); \ 553 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \ 554 __ratelimit(&_rs)) \ 555 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__); \ 556} while (0) 557#elif defined(DEBUG) 558#define pr_debug_ratelimited(fmt, ...) \ 559 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 560#else 561#define pr_debug_ratelimited(fmt, ...) \ 562 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 563#endif 564 565extern const struct file_operations kmsg_fops; 566 567enum { 568 DUMP_PREFIX_NONE, 569 DUMP_PREFIX_ADDRESS, 570 DUMP_PREFIX_OFFSET 571}; 572extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, 573 int groupsize, char *linebuf, size_t linebuflen, 574 bool ascii); 575#ifdef CONFIG_PRINTK 576extern void print_hex_dump(const char *level, const char *prefix_str, 577 int prefix_type, int rowsize, int groupsize, 578 const void *buf, size_t len, bool ascii); 579#else 580static inline void print_hex_dump(const char *level, const char *prefix_str, 581 int prefix_type, int rowsize, int groupsize, 582 const void *buf, size_t len, bool ascii) 583{ 584} 585static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type, 586 const void *buf, size_t len) 587{ 588} 589 590#endif 591 592#if defined(CONFIG_DYNAMIC_DEBUG) || \ 593 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE)) 594#define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \ 595 groupsize, buf, len, ascii) \ 596 dynamic_hex_dump(prefix_str, prefix_type, rowsize, \ 597 groupsize, buf, len, ascii) 598#elif defined(DEBUG) 599#define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \ 600 groupsize, buf, len, ascii) \ 601 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize, \ 602 groupsize, buf, len, ascii) 603#else 604static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type, 605 int rowsize, int groupsize, 606 const void *buf, size_t len, bool ascii) 607{ 608} 609#endif 610 611/** 612 * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params 613 * @prefix_str: string to prefix each line with; 614 * caller supplies trailing spaces for alignment if desired 615 * @prefix_type: controls whether prefix of an offset, address, or none 616 * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE) 617 * @buf: data blob to dump 618 * @len: number of bytes in the @buf 619 * 620 * Calls print_hex_dump(), with log level of KERN_DEBUG, 621 * rowsize of 16, groupsize of 1, and ASCII output included. 622 */ 623#define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \ 624 print_hex_dump_debug(prefix_str, prefix_type, 16, 1, buf, len, true) 625 626#endif