printk.h 14 KB

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