printk.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __KERNEL_PRINTK__
  3. #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. extern const char linux_banner[];
  10. extern const char linux_proc_banner[];
  11. #define PRINTK_MAX_SINGLE_HEADER_LEN 2
  12. static inline int printk_get_level(const char *buffer)
  13. {
  14. if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
  15. switch (buffer[1]) {
  16. case '0' ... '7':
  17. case 'd': /* KERN_DEFAULT */
  18. case 'c': /* KERN_CONT */
  19. return buffer[1];
  20. }
  21. }
  22. return 0;
  23. }
  24. static inline const char *printk_skip_level(const char *buffer)
  25. {
  26. if (printk_get_level(buffer))
  27. return buffer + 2;
  28. return buffer;
  29. }
  30. static inline const char *printk_skip_headers(const char *buffer)
  31. {
  32. while (printk_get_level(buffer))
  33. buffer = printk_skip_level(buffer);
  34. return buffer;
  35. }
  36. #define CONSOLE_EXT_LOG_MAX 8192
  37. /* printk's without a loglevel use this.. */
  38. #define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT
  39. /* We show everything that is MORE important than this.. */
  40. #define CONSOLE_LOGLEVEL_SILENT 0 /* Mum's the word */
  41. #define CONSOLE_LOGLEVEL_MIN 1 /* Minimum loglevel we let people use */
  42. #define CONSOLE_LOGLEVEL_DEBUG 10 /* issue debug messages */
  43. #define CONSOLE_LOGLEVEL_MOTORMOUTH 15 /* You can't shut this one up */
  44. /*
  45. * Default used to be hard-coded at 7, quiet used to be hardcoded at 4,
  46. * we're now allowing both to be set from kernel config.
  47. */
  48. #define CONSOLE_LOGLEVEL_DEFAULT CONFIG_CONSOLE_LOGLEVEL_DEFAULT
  49. #define CONSOLE_LOGLEVEL_QUIET CONFIG_CONSOLE_LOGLEVEL_QUIET
  50. extern int console_printk[];
  51. #define console_loglevel (console_printk[0])
  52. #define default_message_loglevel (console_printk[1])
  53. #define minimum_console_loglevel (console_printk[2])
  54. #define default_console_loglevel (console_printk[3])
  55. static inline void console_silent(void)
  56. {
  57. console_loglevel = CONSOLE_LOGLEVEL_SILENT;
  58. }
  59. static inline void console_verbose(void)
  60. {
  61. if (console_loglevel)
  62. console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
  63. }
  64. /* strlen("ratelimit") + 1 */
  65. #define DEVKMSG_STR_MAX_SIZE 10
  66. extern char devkmsg_log_str[];
  67. struct ctl_table;
  68. struct va_format {
  69. const char *fmt;
  70. va_list *va;
  71. };
  72. /*
  73. * FW_BUG
  74. * Add this to a message where you are sure the firmware is buggy or behaves
  75. * really stupid or out of spec. Be aware that the responsible BIOS developer
  76. * should be able to fix this issue or at least get a concrete idea of the
  77. * problem by reading your message without the need of looking at the kernel
  78. * code.
  79. *
  80. * Use it for definite and high priority BIOS bugs.
  81. *
  82. * FW_WARN
  83. * Use it for not that clear (e.g. could the kernel messed up things already?)
  84. * and medium priority BIOS bugs.
  85. *
  86. * FW_INFO
  87. * Use this one if you want to tell the user or vendor about something
  88. * suspicious, but generally harmless related to the firmware.
  89. *
  90. * Use it for information or very low priority BIOS bugs.
  91. */
  92. #define FW_BUG "[Firmware Bug]: "
  93. #define FW_WARN "[Firmware Warn]: "
  94. #define FW_INFO "[Firmware Info]: "
  95. /*
  96. * HW_ERR
  97. * Add this to a message for hardware errors, so that user can report
  98. * it to hardware vendor instead of LKML or software vendor.
  99. */
  100. #define HW_ERR "[Hardware Error]: "
  101. /*
  102. * DEPRECATED
  103. * Add this to a message whenever you want to warn user space about the use
  104. * of a deprecated aspect of an API so they can stop using it
  105. */
  106. #define DEPRECATED "[Deprecated]: "
  107. /*
  108. * Dummy printk for disabled debugging statements to use whilst maintaining
  109. * gcc's format checking.
  110. */
  111. #define no_printk(fmt, ...) \
  112. ({ \
  113. if (0) \
  114. printk(fmt, ##__VA_ARGS__); \
  115. 0; \
  116. })
  117. #ifdef CONFIG_EARLY_PRINTK
  118. extern asmlinkage __printf(1, 2)
  119. void early_printk(const char *fmt, ...);
  120. #else
  121. static inline __printf(1, 2) __cold
  122. void early_printk(const char *s, ...) { }
  123. #endif
  124. #ifdef CONFIG_PRINTK_NMI
  125. extern void printk_nmi_enter(void);
  126. extern void printk_nmi_exit(void);
  127. extern void printk_nmi_direct_enter(void);
  128. extern void printk_nmi_direct_exit(void);
  129. #else
  130. static inline void printk_nmi_enter(void) { }
  131. static inline void printk_nmi_exit(void) { }
  132. static inline void printk_nmi_direct_enter(void) { }
  133. static inline void printk_nmi_direct_exit(void) { }
  134. #endif /* PRINTK_NMI */
  135. #ifdef CONFIG_PRINTK
  136. asmlinkage __printf(5, 0)
  137. int vprintk_emit(int facility, int level,
  138. const char *dict, size_t dictlen,
  139. const char *fmt, va_list args);
  140. asmlinkage __printf(1, 0)
  141. int vprintk(const char *fmt, va_list args);
  142. asmlinkage __printf(5, 6) __cold
  143. int printk_emit(int facility, int level,
  144. const char *dict, size_t dictlen,
  145. const char *fmt, ...);
  146. asmlinkage __printf(1, 2) __cold
  147. int printk(const char *fmt, ...);
  148. /*
  149. * Special printk facility for scheduler/timekeeping use only, _DO_NOT_USE_ !
  150. */
  151. __printf(1, 2) __cold int printk_deferred(const char *fmt, ...);
  152. /*
  153. * Please don't use printk_ratelimit(), because it shares ratelimiting state
  154. * with all other unrelated printk_ratelimit() callsites. Instead use
  155. * printk_ratelimited() or plain old __ratelimit().
  156. */
  157. extern int __printk_ratelimit(const char *func);
  158. #define printk_ratelimit() __printk_ratelimit(__func__)
  159. extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  160. unsigned int interval_msec);
  161. extern int printk_delay_msec;
  162. extern int dmesg_restrict;
  163. extern int
  164. devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, void __user *buf,
  165. size_t *lenp, loff_t *ppos);
  166. extern void wake_up_klogd(void);
  167. char *log_buf_addr_get(void);
  168. u32 log_buf_len_get(void);
  169. void log_buf_vmcoreinfo_setup(void);
  170. void __init setup_log_buf(int early);
  171. __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...);
  172. void dump_stack_print_info(const char *log_lvl);
  173. void show_regs_print_info(const char *log_lvl);
  174. extern asmlinkage void dump_stack(void) __cold;
  175. extern void printk_safe_init(void);
  176. extern void printk_safe_flush(void);
  177. extern void printk_safe_flush_on_panic(void);
  178. #else
  179. static inline __printf(1, 0)
  180. int vprintk(const char *s, va_list args)
  181. {
  182. return 0;
  183. }
  184. static inline __printf(1, 2) __cold
  185. int printk(const char *s, ...)
  186. {
  187. return 0;
  188. }
  189. static inline __printf(1, 2) __cold
  190. int printk_deferred(const char *s, ...)
  191. {
  192. return 0;
  193. }
  194. static inline int printk_ratelimit(void)
  195. {
  196. return 0;
  197. }
  198. static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  199. unsigned int interval_msec)
  200. {
  201. return false;
  202. }
  203. static inline void wake_up_klogd(void)
  204. {
  205. }
  206. static inline char *log_buf_addr_get(void)
  207. {
  208. return NULL;
  209. }
  210. static inline u32 log_buf_len_get(void)
  211. {
  212. return 0;
  213. }
  214. static inline void log_buf_vmcoreinfo_setup(void)
  215. {
  216. }
  217. static inline void setup_log_buf(int early)
  218. {
  219. }
  220. static inline __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...)
  221. {
  222. }
  223. static inline void dump_stack_print_info(const char *log_lvl)
  224. {
  225. }
  226. static inline void show_regs_print_info(const char *log_lvl)
  227. {
  228. }
  229. static inline asmlinkage void dump_stack(void)
  230. {
  231. }
  232. static inline void printk_safe_init(void)
  233. {
  234. }
  235. static inline void printk_safe_flush(void)
  236. {
  237. }
  238. static inline void printk_safe_flush_on_panic(void)
  239. {
  240. }
  241. #endif
  242. extern int kptr_restrict;
  243. #ifndef pr_fmt
  244. #define pr_fmt(fmt) fmt
  245. #endif
  246. /*
  247. * These can be used to print at the various log levels.
  248. * All of these will print unconditionally, although note that pr_debug()
  249. * and other debug macros are compiled out unless either DEBUG is defined
  250. * or CONFIG_DYNAMIC_DEBUG is set.
  251. */
  252. #define pr_emerg(fmt, ...) \
  253. printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  254. #define pr_alert(fmt, ...) \
  255. printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  256. #define pr_crit(fmt, ...) \
  257. printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  258. #define pr_err(fmt, ...) \
  259. printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  260. #define pr_warning(fmt, ...) \
  261. printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  262. #define pr_warn pr_warning
  263. #define pr_notice(fmt, ...) \
  264. printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  265. #define pr_info(fmt, ...) \
  266. printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  267. /*
  268. * Like KERN_CONT, pr_cont() should only be used when continuing
  269. * a line with no newline ('\n') enclosed. Otherwise it defaults
  270. * back to KERN_DEFAULT.
  271. */
  272. #define pr_cont(fmt, ...) \
  273. printk(KERN_CONT fmt, ##__VA_ARGS__)
  274. /* pr_devel() should produce zero code unless DEBUG is defined */
  275. #ifdef DEBUG
  276. #define pr_devel(fmt, ...) \
  277. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  278. #else
  279. #define pr_devel(fmt, ...) \
  280. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  281. #endif
  282. /* If you are writing a driver, please use dev_dbg instead */
  283. #if defined(CONFIG_DYNAMIC_DEBUG)
  284. #include <linux/dynamic_debug.h>
  285. /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
  286. #define pr_debug(fmt, ...) \
  287. dynamic_pr_debug(fmt, ##__VA_ARGS__)
  288. #elif defined(DEBUG)
  289. #define pr_debug(fmt, ...) \
  290. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  291. #else
  292. #define pr_debug(fmt, ...) \
  293. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  294. #endif
  295. /*
  296. * Print a one-time message (analogous to WARN_ONCE() et al):
  297. */
  298. #ifdef CONFIG_PRINTK
  299. #define printk_once(fmt, ...) \
  300. ({ \
  301. static bool __print_once __read_mostly; \
  302. bool __ret_print_once = !__print_once; \
  303. \
  304. if (!__print_once) { \
  305. __print_once = true; \
  306. printk(fmt, ##__VA_ARGS__); \
  307. } \
  308. unlikely(__ret_print_once); \
  309. })
  310. #define printk_deferred_once(fmt, ...) \
  311. ({ \
  312. static bool __print_once __read_mostly; \
  313. bool __ret_print_once = !__print_once; \
  314. \
  315. if (!__print_once) { \
  316. __print_once = true; \
  317. printk_deferred(fmt, ##__VA_ARGS__); \
  318. } \
  319. unlikely(__ret_print_once); \
  320. })
  321. #else
  322. #define printk_once(fmt, ...) \
  323. no_printk(fmt, ##__VA_ARGS__)
  324. #define printk_deferred_once(fmt, ...) \
  325. no_printk(fmt, ##__VA_ARGS__)
  326. #endif
  327. #define pr_emerg_once(fmt, ...) \
  328. printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  329. #define pr_alert_once(fmt, ...) \
  330. printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  331. #define pr_crit_once(fmt, ...) \
  332. printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  333. #define pr_err_once(fmt, ...) \
  334. printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  335. #define pr_warn_once(fmt, ...) \
  336. printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  337. #define pr_notice_once(fmt, ...) \
  338. printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  339. #define pr_info_once(fmt, ...) \
  340. printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  341. #define pr_cont_once(fmt, ...) \
  342. printk_once(KERN_CONT pr_fmt(fmt), ##__VA_ARGS__)
  343. #if defined(DEBUG)
  344. #define pr_devel_once(fmt, ...) \
  345. printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  346. #else
  347. #define pr_devel_once(fmt, ...) \
  348. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  349. #endif
  350. /* If you are writing a driver, please use dev_dbg instead */
  351. #if defined(DEBUG)
  352. #define pr_debug_once(fmt, ...) \
  353. printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  354. #else
  355. #define pr_debug_once(fmt, ...) \
  356. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  357. #endif
  358. /*
  359. * ratelimited messages with local ratelimit_state,
  360. * no local ratelimit_state used in the !PRINTK case
  361. */
  362. #ifdef CONFIG_PRINTK
  363. #define printk_ratelimited(fmt, ...) \
  364. ({ \
  365. static DEFINE_RATELIMIT_STATE(_rs, \
  366. DEFAULT_RATELIMIT_INTERVAL, \
  367. DEFAULT_RATELIMIT_BURST); \
  368. \
  369. if (__ratelimit(&_rs)) \
  370. printk(fmt, ##__VA_ARGS__); \
  371. })
  372. #else
  373. #define printk_ratelimited(fmt, ...) \
  374. no_printk(fmt, ##__VA_ARGS__)
  375. #endif
  376. #define pr_emerg_ratelimited(fmt, ...) \
  377. printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  378. #define pr_alert_ratelimited(fmt, ...) \
  379. printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  380. #define pr_crit_ratelimited(fmt, ...) \
  381. printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  382. #define pr_err_ratelimited(fmt, ...) \
  383. printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  384. #define pr_warn_ratelimited(fmt, ...) \
  385. printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  386. #define pr_notice_ratelimited(fmt, ...) \
  387. printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  388. #define pr_info_ratelimited(fmt, ...) \
  389. printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  390. /* no pr_cont_ratelimited, don't do that... */
  391. #if defined(DEBUG)
  392. #define pr_devel_ratelimited(fmt, ...) \
  393. printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  394. #else
  395. #define pr_devel_ratelimited(fmt, ...) \
  396. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  397. #endif
  398. /* If you are writing a driver, please use dev_dbg instead */
  399. #if defined(CONFIG_DYNAMIC_DEBUG)
  400. /* descriptor check is first to prevent flooding with "callbacks suppressed" */
  401. #define pr_debug_ratelimited(fmt, ...) \
  402. do { \
  403. static DEFINE_RATELIMIT_STATE(_rs, \
  404. DEFAULT_RATELIMIT_INTERVAL, \
  405. DEFAULT_RATELIMIT_BURST); \
  406. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt)); \
  407. if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) && \
  408. __ratelimit(&_rs)) \
  409. __dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__); \
  410. } while (0)
  411. #elif defined(DEBUG)
  412. #define pr_debug_ratelimited(fmt, ...) \
  413. printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  414. #else
  415. #define pr_debug_ratelimited(fmt, ...) \
  416. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  417. #endif
  418. extern const struct file_operations kmsg_fops;
  419. enum {
  420. DUMP_PREFIX_NONE,
  421. DUMP_PREFIX_ADDRESS,
  422. DUMP_PREFIX_OFFSET
  423. };
  424. extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
  425. int groupsize, char *linebuf, size_t linebuflen,
  426. bool ascii);
  427. #ifdef CONFIG_PRINTK
  428. extern void print_hex_dump(const char *level, const char *prefix_str,
  429. int prefix_type, int rowsize, int groupsize,
  430. const void *buf, size_t len, bool ascii);
  431. #if defined(CONFIG_DYNAMIC_DEBUG)
  432. #define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
  433. dynamic_hex_dump(prefix_str, prefix_type, 16, 1, buf, len, true)
  434. #else
  435. extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  436. const void *buf, size_t len);
  437. #endif /* defined(CONFIG_DYNAMIC_DEBUG) */
  438. #else
  439. static inline void print_hex_dump(const char *level, const char *prefix_str,
  440. int prefix_type, int rowsize, int groupsize,
  441. const void *buf, size_t len, bool ascii)
  442. {
  443. }
  444. static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  445. const void *buf, size_t len)
  446. {
  447. }
  448. #endif
  449. #if defined(CONFIG_DYNAMIC_DEBUG)
  450. #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
  451. groupsize, buf, len, ascii) \
  452. dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
  453. groupsize, buf, len, ascii)
  454. #elif defined(DEBUG)
  455. #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
  456. groupsize, buf, len, ascii) \
  457. print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize, \
  458. groupsize, buf, len, ascii)
  459. #else
  460. static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type,
  461. int rowsize, int groupsize,
  462. const void *buf, size_t len, bool ascii)
  463. {
  464. }
  465. #endif
  466. #endif