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