printk.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 asmlinkage 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. #include <linux/dynamic_debug.h>
  204. /* If you are writing a driver, please use dev_dbg instead */
  205. #if defined(CONFIG_DYNAMIC_DEBUG)
  206. /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
  207. #define pr_debug(fmt, ...) \
  208. dynamic_pr_debug(fmt, ##__VA_ARGS__)
  209. #elif defined(DEBUG)
  210. #define pr_debug(fmt, ...) \
  211. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  212. #else
  213. #define pr_debug(fmt, ...) \
  214. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  215. #endif
  216. /*
  217. * Print a one-time message (analogous to WARN_ONCE() et al):
  218. */
  219. #ifdef CONFIG_PRINTK
  220. #define printk_once(fmt, ...) \
  221. ({ \
  222. static bool __print_once; \
  223. \
  224. if (!__print_once) { \
  225. __print_once = true; \
  226. printk(fmt, ##__VA_ARGS__); \
  227. } \
  228. })
  229. #else
  230. #define printk_once(fmt, ...) \
  231. no_printk(fmt, ##__VA_ARGS__)
  232. #endif
  233. #define pr_emerg_once(fmt, ...) \
  234. printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  235. #define pr_alert_once(fmt, ...) \
  236. printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  237. #define pr_crit_once(fmt, ...) \
  238. printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  239. #define pr_err_once(fmt, ...) \
  240. printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  241. #define pr_warn_once(fmt, ...) \
  242. printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  243. #define pr_notice_once(fmt, ...) \
  244. printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  245. #define pr_info_once(fmt, ...) \
  246. printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  247. #define pr_cont_once(fmt, ...) \
  248. printk_once(KERN_CONT pr_fmt(fmt), ##__VA_ARGS__)
  249. #if defined(DEBUG)
  250. #define pr_devel_once(fmt, ...) \
  251. printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  252. #else
  253. #define pr_devel_once(fmt, ...) \
  254. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  255. #endif
  256. /* If you are writing a driver, please use dev_dbg instead */
  257. #if defined(DEBUG)
  258. #define pr_debug_once(fmt, ...) \
  259. printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  260. #else
  261. #define pr_debug_once(fmt, ...) \
  262. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  263. #endif
  264. /*
  265. * ratelimited messages with local ratelimit_state,
  266. * no local ratelimit_state used in the !PRINTK case
  267. */
  268. #ifdef CONFIG_PRINTK
  269. #define printk_ratelimited(fmt, ...) \
  270. ({ \
  271. static DEFINE_RATELIMIT_STATE(_rs, \
  272. DEFAULT_RATELIMIT_INTERVAL, \
  273. DEFAULT_RATELIMIT_BURST); \
  274. \
  275. if (__ratelimit(&_rs)) \
  276. printk(fmt, ##__VA_ARGS__); \
  277. })
  278. #else
  279. #define printk_ratelimited(fmt, ...) \
  280. no_printk(fmt, ##__VA_ARGS__)
  281. #endif
  282. #define pr_emerg_ratelimited(fmt, ...) \
  283. printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  284. #define pr_alert_ratelimited(fmt, ...) \
  285. printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  286. #define pr_crit_ratelimited(fmt, ...) \
  287. printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  288. #define pr_err_ratelimited(fmt, ...) \
  289. printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  290. #define pr_warn_ratelimited(fmt, ...) \
  291. printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  292. #define pr_notice_ratelimited(fmt, ...) \
  293. printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  294. #define pr_info_ratelimited(fmt, ...) \
  295. printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  296. /* no pr_cont_ratelimited, don't do that... */
  297. #if defined(DEBUG)
  298. #define pr_devel_ratelimited(fmt, ...) \
  299. printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  300. #else
  301. #define pr_devel_ratelimited(fmt, ...) \
  302. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  303. #endif
  304. /* If you are writing a driver, please use dev_dbg instead */
  305. #if defined(CONFIG_DYNAMIC_DEBUG)
  306. /* descriptor check is first to prevent flooding with "callbacks suppressed" */
  307. #define pr_debug_ratelimited(fmt, ...) \
  308. do { \
  309. static DEFINE_RATELIMIT_STATE(_rs, \
  310. DEFAULT_RATELIMIT_INTERVAL, \
  311. DEFAULT_RATELIMIT_BURST); \
  312. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
  313. if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) && \
  314. __ratelimit(&_rs)) \
  315. __dynamic_pr_debug(&descriptor, fmt, ##__VA_ARGS__); \
  316. } while (0)
  317. #elif defined(DEBUG)
  318. #define pr_debug_ratelimited(fmt, ...) \
  319. printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  320. #else
  321. #define pr_debug_ratelimited(fmt, ...) \
  322. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  323. #endif
  324. extern const struct file_operations kmsg_fops;
  325. enum {
  326. DUMP_PREFIX_NONE,
  327. DUMP_PREFIX_ADDRESS,
  328. DUMP_PREFIX_OFFSET
  329. };
  330. extern void hex_dump_to_buffer(const void *buf, size_t len,
  331. int rowsize, int groupsize,
  332. char *linebuf, size_t linebuflen, bool ascii);
  333. #ifdef CONFIG_PRINTK
  334. extern void print_hex_dump(const char *level, const char *prefix_str,
  335. int prefix_type, int rowsize, int groupsize,
  336. const void *buf, size_t len, bool ascii);
  337. #if defined(CONFIG_DYNAMIC_DEBUG)
  338. #define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
  339. dynamic_hex_dump(prefix_str, prefix_type, 16, 1, buf, len, true)
  340. #else
  341. extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  342. const void *buf, size_t len);
  343. #endif /* defined(CONFIG_DYNAMIC_DEBUG) */
  344. #else
  345. static inline void print_hex_dump(const char *level, const char *prefix_str,
  346. int prefix_type, int rowsize, int groupsize,
  347. const void *buf, size_t len, bool ascii)
  348. {
  349. }
  350. static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  351. const void *buf, size_t len)
  352. {
  353. }
  354. #endif
  355. #if defined(CONFIG_DYNAMIC_DEBUG)
  356. #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
  357. groupsize, buf, len, ascii) \
  358. dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
  359. groupsize, buf, len, ascii)
  360. #else
  361. #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
  362. groupsize, buf, len, ascii) \
  363. print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize, \
  364. groupsize, buf, len, ascii)
  365. #endif /* defined(CONFIG_DYNAMIC_DEBUG) */
  366. #endif