printk.h 11 KB

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