linux/include/linux/printk.h
<<
>>
Prefs
   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 __user *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
 285#ifdef CONFIG_SMP
 286extern int __printk_cpu_trylock(void);
 287extern void __printk_wait_on_cpu_lock(void);
 288extern void __printk_cpu_unlock(void);
 289
 290/**
 291 * printk_cpu_lock_irqsave() - Acquire the printk cpu-reentrant spinning
 292 *                             lock and disable interrupts.
 293 * @flags: Stack-allocated storage for saving local interrupt state,
 294 *         to be passed to printk_cpu_unlock_irqrestore().
 295 *
 296 * If the lock is owned by another CPU, spin until it becomes available.
 297 * Interrupts are restored while spinning.
 298 */
 299#define printk_cpu_lock_irqsave(flags)          \
 300        for (;;) {                              \
 301                local_irq_save(flags);          \
 302                if (__printk_cpu_trylock())     \
 303                        break;                  \
 304                local_irq_restore(flags);       \
 305                __printk_wait_on_cpu_lock();    \
 306        }
 307
 308/**
 309 * printk_cpu_unlock_irqrestore() - Release the printk cpu-reentrant spinning
 310 *                                  lock and restore interrupts.
 311 * @flags: Caller's saved interrupt state, from printk_cpu_lock_irqsave().
 312 */
 313#define printk_cpu_unlock_irqrestore(flags)     \
 314        do {                                    \
 315                __printk_cpu_unlock();          \
 316                local_irq_restore(flags);       \
 317        } while (0)                             \
 318
 319#else
 320
 321#define printk_cpu_lock_irqsave(flags) ((void)flags)
 322#define printk_cpu_unlock_irqrestore(flags) ((void)flags)
 323
 324#endif /* CONFIG_SMP */
 325
 326extern int kptr_restrict;
 327
 328#ifndef pr_fmt
 329#define pr_fmt(fmt) fmt
 330#endif
 331
 332/*
 333 * These can be used to print at the various log levels.
 334 * All of these will print unconditionally, although note that pr_debug()
 335 * and other debug macros are compiled out unless either DEBUG is defined
 336 * or CONFIG_DYNAMIC_DEBUG is set.
 337 */
 338#define pr_emerg(fmt, ...) \
 339        printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
 340#define pr_alert(fmt, ...) \
 341        printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
 342#define pr_crit(fmt, ...) \
 343        printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
 344#define pr_err(fmt, ...) \
 345        printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
 346#define pr_warning(fmt, ...) \
 347        printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
 348#define pr_warn pr_warning
 349#define pr_notice(fmt, ...) \
 350        printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
 351#define pr_info(fmt, ...) \
 352        printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
 353/*
 354 * Like KERN_CONT, pr_cont() should only be used when continuing
 355 * a line with no newline ('\n') enclosed. Otherwise it defaults
 356 * back to KERN_DEFAULT.
 357 */
 358#define pr_cont(fmt, ...) \
 359        printk(KERN_CONT fmt, ##__VA_ARGS__)
 360
 361/* pr_devel() should produce zero code unless DEBUG is defined */
 362#ifdef DEBUG
 363#define pr_devel(fmt, ...) \
 364        printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
 365#else
 366#define pr_devel(fmt, ...) \
 367        no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
 368#endif
 369
 370
 371/* If you are writing a driver, please use dev_dbg instead */
 372#if defined(CONFIG_DYNAMIC_DEBUG)
 373#include <linux/dynamic_debug.h>
 374
 375/* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
 376#define pr_debug(fmt, ...) \
 377        dynamic_pr_debug(fmt, ##__VA_ARGS__)
 378#elif defined(DEBUG)
 379#define pr_debug(fmt, ...) \
 380        printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
 381#else
 382#define pr_debug(fmt, ...) \
 383        no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
 384#endif
 385
 386/*
 387 * Print a one-time message (analogous to WARN_ONCE() et al):
 388 */
 389
 390#ifdef CONFIG_PRINTK
 391#define printk_once(fmt, ...)                                   \
 392({                                                              \
 393        static bool __section(.data.once) __print_once;         \
 394        bool __ret_print_once = !__print_once;                  \
 395                                                                \
 396        if (!__print_once) {                                    \
 397                __print_once = true;                            \
 398                printk(fmt, ##__VA_ARGS__);                     \
 399        }                                                       \
 400        unlikely(__ret_print_once);                             \
 401})
 402#define printk_deferred_once(fmt, ...)                          \
 403({                                                              \
 404        static bool __section(.data.once) __print_once;         \
 405        bool __ret_print_once = !__print_once;                  \
 406                                                                \
 407        if (!__print_once) {                                    \
 408                __print_once = true;                            \
 409                printk_deferred(fmt, ##__VA_ARGS__);            \
 410        }                                                       \
 411        unlikely(__ret_print_once);                             \
 412})
 413#else
 414#define printk_once(fmt, ...)                                   \
 415        no_printk(fmt, ##__VA_ARGS__)
 416#define printk_deferred_once(fmt, ...)                          \
 417        no_printk(fmt, ##__VA_ARGS__)
 418#endif
 419
 420#define pr_emerg_once(fmt, ...)                                 \
 421        printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
 422#define pr_alert_once(fmt, ...)                                 \
 423        printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
 424#define pr_crit_once(fmt, ...)                                  \
 425        printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
 426#define pr_err_once(fmt, ...)                                   \
 427        printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
 428#define pr_warn_once(fmt, ...)                                  \
 429        printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
 430#define pr_notice_once(fmt, ...)                                \
 431        printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
 432#define pr_info_once(fmt, ...)                                  \
 433        printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
 434/* no pr_cont_once, don't do that... */
 435
 436#if defined(DEBUG)
 437#define pr_devel_once(fmt, ...)                                 \
 438        printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
 439#else
 440#define pr_devel_once(fmt, ...)                                 \
 441        no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
 442#endif
 443
 444/* If you are writing a driver, please use dev_dbg instead */
 445#if defined(DEBUG)
 446#define pr_debug_once(fmt, ...)                                 \
 447        printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
 448#else
 449#define pr_debug_once(fmt, ...)                                 \
 450        no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
 451#endif
 452
 453/*
 454 * ratelimited messages with local ratelimit_state,
 455 * no local ratelimit_state used in the !PRINTK case
 456 */
 457#ifdef CONFIG_PRINTK
 458#define printk_ratelimited(fmt, ...)                                    \
 459({                                                                      \
 460        static DEFINE_RATELIMIT_STATE(_rs,                              \
 461                                      DEFAULT_RATELIMIT_INTERVAL,       \
 462                                      DEFAULT_RATELIMIT_BURST);         \
 463                                                                        \
 464        if (__ratelimit(&_rs))                                          \
 465                printk(fmt, ##__VA_ARGS__);                             \
 466})
 467#else
 468#define printk_ratelimited(fmt, ...)                                    \
 469        no_printk(fmt, ##__VA_ARGS__)
 470#endif
 471
 472#define pr_emerg_ratelimited(fmt, ...)                                  \
 473        printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
 474#define pr_alert_ratelimited(fmt, ...)                                  \
 475        printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
 476#define pr_crit_ratelimited(fmt, ...)                                   \
 477        printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
 478#define pr_err_ratelimited(fmt, ...)                                    \
 479        printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
 480#define pr_warn_ratelimited(fmt, ...)                                   \
 481        printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
 482#define pr_notice_ratelimited(fmt, ...)                                 \
 483        printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
 484#define pr_info_ratelimited(fmt, ...)                                   \
 485        printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
 486/* no pr_cont_ratelimited, don't do that... */
 487
 488#if defined(DEBUG)
 489#define pr_devel_ratelimited(fmt, ...)                                  \
 490        printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
 491#else
 492#define pr_devel_ratelimited(fmt, ...)                                  \
 493        no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
 494#endif
 495
 496/* If you are writing a driver, please use dev_dbg instead */
 497#if defined(CONFIG_DYNAMIC_DEBUG)
 498/* descriptor check is first to prevent flooding with "callbacks suppressed" */
 499#define pr_debug_ratelimited(fmt, ...)                                  \
 500do {                                                                    \
 501        static DEFINE_RATELIMIT_STATE(_rs,                              \
 502                                      DEFAULT_RATELIMIT_INTERVAL,       \
 503                                      DEFAULT_RATELIMIT_BURST);         \
 504        DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt));         \
 505        if (DYNAMIC_DEBUG_BRANCH(descriptor) &&                         \
 506            __ratelimit(&_rs))                                          \
 507                __dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__);    \
 508} while (0)
 509#elif defined(DEBUG)
 510#define pr_debug_ratelimited(fmt, ...)                                  \
 511        printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
 512#else
 513#define pr_debug_ratelimited(fmt, ...) \
 514        no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
 515#endif
 516
 517extern const struct file_operations kmsg_fops;
 518
 519enum {
 520        DUMP_PREFIX_NONE,
 521        DUMP_PREFIX_ADDRESS,
 522        DUMP_PREFIX_OFFSET
 523};
 524extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
 525                              int groupsize, char *linebuf, size_t linebuflen,
 526                              bool ascii);
 527#ifdef CONFIG_PRINTK
 528extern void print_hex_dump(const char *level, const char *prefix_str,
 529                           int prefix_type, int rowsize, int groupsize,
 530                           const void *buf, size_t len, bool ascii);
 531#if defined(CONFIG_DYNAMIC_DEBUG)
 532#define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
 533        dynamic_hex_dump(prefix_str, prefix_type, 16, 1, buf, len, true)
 534#else
 535extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
 536                                 const void *buf, size_t len);
 537#endif /* defined(CONFIG_DYNAMIC_DEBUG) */
 538#else
 539static inline void print_hex_dump(const char *level, const char *prefix_str,
 540                                  int prefix_type, int rowsize, int groupsize,
 541                                  const void *buf, size_t len, bool ascii)
 542{
 543}
 544static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
 545                                        const void *buf, size_t len)
 546{
 547}
 548
 549#endif
 550
 551#if defined(CONFIG_DYNAMIC_DEBUG)
 552#define print_hex_dump_debug(prefix_str, prefix_type, rowsize,  \
 553                             groupsize, buf, len, ascii)        \
 554        dynamic_hex_dump(prefix_str, prefix_type, rowsize,      \
 555                         groupsize, buf, len, ascii)
 556#elif defined(DEBUG)
 557#define print_hex_dump_debug(prefix_str, prefix_type, rowsize,          \
 558                             groupsize, buf, len, ascii)                \
 559        print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize,    \
 560                       groupsize, buf, len, ascii)
 561#else
 562static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type,
 563                                        int rowsize, int groupsize,
 564                                        const void *buf, size_t len, bool ascii)
 565{
 566}
 567#endif
 568
 569#endif
 570