printk.h 14 KB

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