printk.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #ifndef __KERNEL_PRINTK__
  2. #define __KERNEL_PRINTK__
  3. #include <stdarg.h>
  4. #include <linux/init.h>
  5. #include <linux/kern_levels.h>
  6. extern const char linux_banner[];
  7. extern const char linux_proc_banner[];
  8. static inline int printk_get_level(const char *buffer)
  9. {
  10. if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
  11. switch (buffer[1]) {
  12. case '0' ... '7':
  13. case 'd': /* KERN_DEFAULT */
  14. return buffer[1];
  15. }
  16. }
  17. return 0;
  18. }
  19. static inline const char *printk_skip_level(const char *buffer)
  20. {
  21. if (printk_get_level(buffer)) {
  22. switch (buffer[1]) {
  23. case '0' ... '7':
  24. case 'd': /* KERN_DEFAULT */
  25. return buffer + 2;
  26. }
  27. }
  28. return buffer;
  29. }
  30. extern int console_printk[];
  31. #define console_loglevel (console_printk[0])
  32. #define default_message_loglevel (console_printk[1])
  33. #define minimum_console_loglevel (console_printk[2])
  34. #define default_console_loglevel (console_printk[3])
  35. static inline void console_silent(void)
  36. {
  37. console_loglevel = 0;
  38. }
  39. static inline void console_verbose(void)
  40. {
  41. if (console_loglevel)
  42. console_loglevel = 15;
  43. }
  44. struct va_format {
  45. const char *fmt;
  46. va_list *va;
  47. };
  48. /*
  49. * FW_BUG
  50. * Add this to a message where you are sure the firmware is buggy or behaves
  51. * really stupid or out of spec. Be aware that the responsible BIOS developer
  52. * should be able to fix this issue or at least get a concrete idea of the
  53. * problem by reading your message without the need of looking at the kernel
  54. * code.
  55. *
  56. * Use it for definite and high priority BIOS bugs.
  57. *
  58. * FW_WARN
  59. * Use it for not that clear (e.g. could the kernel messed up things already?)
  60. * and medium priority BIOS bugs.
  61. *
  62. * FW_INFO
  63. * Use this one if you want to tell the user or vendor about something
  64. * suspicious, but generally harmless related to the firmware.
  65. *
  66. * Use it for information or very low priority BIOS bugs.
  67. */
  68. #define FW_BUG "[Firmware Bug]: "
  69. #define FW_WARN "[Firmware Warn]: "
  70. #define FW_INFO "[Firmware Info]: "
  71. /*
  72. * HW_ERR
  73. * Add this to a message for hardware errors, so that user can report
  74. * it to hardware vendor instead of LKML or software vendor.
  75. */
  76. #define HW_ERR "[Hardware Error]: "
  77. /*
  78. * Dummy printk for disabled debugging statements to use whilst maintaining
  79. * gcc's format and side-effect checking.
  80. */
  81. static inline __printf(1, 2)
  82. int no_printk(const char *fmt, ...)
  83. {
  84. return 0;
  85. }
  86. #ifdef CONFIG_EARLY_PRINTK
  87. extern asmlinkage __printf(1, 2)
  88. void early_printk(const char *fmt, ...);
  89. void early_vprintk(const char *fmt, va_list ap);
  90. #else
  91. static inline __printf(1, 2) __cold
  92. void early_printk(const char *s, ...) { }
  93. #endif
  94. #ifdef CONFIG_PRINTK
  95. asmlinkage __printf(5, 0)
  96. int vprintk_emit(int facility, int level,
  97. const char *dict, size_t dictlen,
  98. const char *fmt, va_list args);
  99. asmlinkage __printf(1, 0)
  100. int vprintk(const char *fmt, va_list args);
  101. asmlinkage __printf(5, 6) __cold
  102. asmlinkage int printk_emit(int facility, int level,
  103. const char *dict, size_t dictlen,
  104. const char *fmt, ...);
  105. asmlinkage __printf(1, 2) __cold
  106. int printk(const char *fmt, ...);
  107. /*
  108. * Special printk facility for scheduler use only, _DO_NOT_USE_ !
  109. */
  110. __printf(1, 2) __cold int printk_sched(const char *fmt, ...);
  111. /*
  112. * Please don't use printk_ratelimit(), because it shares ratelimiting state
  113. * with all other unrelated printk_ratelimit() callsites. Instead use
  114. * printk_ratelimited() or plain old __ratelimit().
  115. */
  116. extern int __printk_ratelimit(const char *func);
  117. #define printk_ratelimit() __printk_ratelimit(__func__)
  118. extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  119. unsigned int interval_msec);
  120. extern int printk_delay_msec;
  121. extern int dmesg_restrict;
  122. extern int kptr_restrict;
  123. extern void wake_up_klogd(void);
  124. void log_buf_kexec_setup(void);
  125. void __init setup_log_buf(int early);
  126. void dump_stack_set_arch_desc(const char *fmt, ...);
  127. void dump_stack_print_info(const char *log_lvl);
  128. void show_regs_print_info(const char *log_lvl);
  129. #else
  130. static inline __printf(1, 0)
  131. int vprintk(const char *s, va_list args)
  132. {
  133. return 0;
  134. }
  135. static inline __printf(1, 2) __cold
  136. int printk(const char *s, ...)
  137. {
  138. return 0;
  139. }
  140. static inline __printf(1, 2) __cold
  141. int printk_sched(const char *s, ...)
  142. {
  143. return 0;
  144. }
  145. static inline int printk_ratelimit(void)
  146. {
  147. return 0;
  148. }
  149. static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  150. unsigned int interval_msec)
  151. {
  152. return false;
  153. }
  154. static inline void wake_up_klogd(void)
  155. {
  156. }
  157. static inline void log_buf_kexec_setup(void)
  158. {
  159. }
  160. static inline void setup_log_buf(int early)
  161. {
  162. }
  163. static inline void dump_stack_set_arch_desc(const char *fmt, ...)
  164. {
  165. }
  166. static inline void dump_stack_print_info(const char *log_lvl)
  167. {
  168. }
  169. static inline void show_regs_print_info(const char *log_lvl)
  170. {
  171. }
  172. #endif
  173. extern void dump_stack(void) __cold;
  174. #ifndef pr_fmt
  175. #define pr_fmt(fmt) fmt
  176. #endif
  177. #define pr_emerg(fmt, ...) \
  178. printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  179. #define pr_alert(fmt, ...) \
  180. printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  181. #define pr_crit(fmt, ...) \
  182. printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  183. #define pr_err(fmt, ...) \
  184. printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  185. #define pr_warning(fmt, ...) \
  186. printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  187. #define pr_warn pr_warning
  188. #define pr_notice(fmt, ...) \
  189. printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  190. #define pr_info(fmt, ...) \
  191. printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  192. #define pr_cont(fmt, ...) \
  193. printk(KERN_CONT fmt, ##__VA_ARGS__)
  194. /* pr_devel() should produce zero code unless DEBUG is defined */
  195. #ifdef DEBUG
  196. #define pr_devel(fmt, ...) \
  197. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  198. #else
  199. #define pr_devel(fmt, ...) \
  200. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  201. #endif
  202. /* If you are writing a driver, please use dev_dbg instead */
  203. #if defined(CONFIG_DYNAMIC_DEBUG)
  204. /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
  205. #define pr_debug(fmt, ...) \
  206. dynamic_pr_debug(fmt, ##__VA_ARGS__)
  207. #elif defined(DEBUG)
  208. #define pr_debug(fmt, ...) \
  209. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  210. #else
  211. #define pr_debug(fmt, ...) \
  212. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  213. #endif
  214. /*
  215. * Print a one-time message (analogous to WARN_ONCE() et al):
  216. */
  217. #ifdef CONFIG_PRINTK
  218. #define printk_once(fmt, ...) \
  219. ({ \
  220. static bool __print_once; \
  221. \
  222. if (!__print_once) { \
  223. __print_once = true; \
  224. printk(fmt, ##__VA_ARGS__); \
  225. } \
  226. })
  227. #else
  228. #define printk_once(fmt, ...) \
  229. no_printk(fmt, ##__VA_ARGS__)
  230. #endif
  231. #define pr_emerg_once(fmt, ...) \
  232. printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  233. #define pr_alert_once(fmt, ...) \
  234. printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  235. #define pr_crit_once(fmt, ...) \
  236. printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  237. #define pr_err_once(fmt, ...) \
  238. printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  239. #define pr_warn_once(fmt, ...) \
  240. printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  241. #define pr_notice_once(fmt, ...) \
  242. printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  243. #define pr_info_once(fmt, ...) \
  244. printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  245. #define pr_cont_once(fmt, ...) \
  246. printk_once(KERN_CONT pr_fmt(fmt), ##__VA_ARGS__)
  247. #if defined(DEBUG)
  248. #define pr_devel_once(fmt, ...) \
  249. printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  250. #else
  251. #define pr_devel_once(fmt, ...) \
  252. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  253. #endif
  254. /* If you are writing a driver, please use dev_dbg instead */
  255. #if defined(DEBUG)
  256. #define pr_debug_once(fmt, ...) \
  257. printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  258. #else
  259. #define pr_debug_once(fmt, ...) \
  260. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  261. #endif
  262. /*
  263. * ratelimited messages with local ratelimit_state,
  264. * no local ratelimit_state used in the !PRINTK case
  265. */
  266. #ifdef CONFIG_PRINTK
  267. #define printk_ratelimited(fmt, ...) \
  268. ({ \
  269. static DEFINE_RATELIMIT_STATE(_rs, \
  270. DEFAULT_RATELIMIT_INTERVAL, \
  271. DEFAULT_RATELIMIT_BURST); \
  272. \
  273. if (__ratelimit(&_rs)) \
  274. printk(fmt, ##__VA_ARGS__); \
  275. })
  276. #else
  277. #define printk_ratelimited(fmt, ...) \
  278. no_printk(fmt, ##__VA_ARGS__)
  279. #endif
  280. #define pr_emerg_ratelimited(fmt, ...) \
  281. printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  282. #define pr_alert_ratelimited(fmt, ...) \
  283. printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  284. #define pr_crit_ratelimited(fmt, ...) \
  285. printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  286. #define pr_err_ratelimited(fmt, ...) \
  287. printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  288. #define pr_warn_ratelimited(fmt, ...) \
  289. printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  290. #define pr_notice_ratelimited(fmt, ...) \
  291. printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  292. #define pr_info_ratelimited(fmt, ...) \
  293. printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  294. /* no pr_cont_ratelimited, don't do that... */
  295. #if defined(DEBUG)
  296. #define pr_devel_ratelimited(fmt, ...) \
  297. printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  298. #else
  299. #define pr_devel_ratelimited(fmt, ...) \
  300. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  301. #endif
  302. /* If you are writing a driver, please use dev_dbg instead */
  303. #if defined(DEBUG)
  304. #define pr_debug_ratelimited(fmt, ...) \
  305. printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  306. #else
  307. #define pr_debug_ratelimited(fmt, ...) \
  308. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  309. #endif
  310. extern const struct file_operations kmsg_fops;
  311. enum {
  312. DUMP_PREFIX_NONE,
  313. DUMP_PREFIX_ADDRESS,
  314. DUMP_PREFIX_OFFSET
  315. };
  316. extern void hex_dump_to_buffer(const void *buf, size_t len,
  317. int rowsize, int groupsize,
  318. char *linebuf, size_t linebuflen, bool ascii);
  319. #ifdef CONFIG_PRINTK
  320. extern void print_hex_dump(const char *level, const char *prefix_str,
  321. int prefix_type, int rowsize, int groupsize,
  322. const void *buf, size_t len, bool ascii);
  323. #if defined(CONFIG_DYNAMIC_DEBUG)
  324. #define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
  325. dynamic_hex_dump(prefix_str, prefix_type, 16, 1, buf, len, true)
  326. #else
  327. extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  328. const void *buf, size_t len);
  329. #endif /* defined(CONFIG_DYNAMIC_DEBUG) */
  330. #else
  331. static inline void print_hex_dump(const char *level, const char *prefix_str,
  332. int prefix_type, int rowsize, int groupsize,
  333. const void *buf, size_t len, bool ascii)
  334. {
  335. }
  336. static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  337. const void *buf, size_t len)
  338. {
  339. }
  340. #endif
  341. #if defined(CONFIG_DYNAMIC_DEBUG)
  342. #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
  343. groupsize, buf, len, ascii) \
  344. dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
  345. groupsize, buf, len, ascii)
  346. #else
  347. #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
  348. groupsize, buf, len, ascii) \
  349. print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize, \
  350. groupsize, buf, len, ascii)
  351. #endif /* defined(CONFIG_DYNAMIC_DEBUG) */
  352. #endif