printk.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. * DEPRECATED
  80. * Add this to a message whenever you want to warn user space about the use
  81. * of a deprecated aspect of an API so they can stop using it
  82. */
  83. #define DEPRECATED "[Deprecated]: "
  84. /*
  85. * Dummy printk for disabled debugging statements to use whilst maintaining
  86. * gcc's format and side-effect checking.
  87. */
  88. static inline __printf(1, 2)
  89. int no_printk(const char *fmt, ...)
  90. {
  91. return 0;
  92. }
  93. #ifdef CONFIG_EARLY_PRINTK
  94. extern asmlinkage __printf(1, 2)
  95. void early_printk(const char *fmt, ...);
  96. void early_vprintk(const char *fmt, va_list ap);
  97. #else
  98. static inline __printf(1, 2) __cold
  99. void early_printk(const char *s, ...) { }
  100. #endif
  101. #ifdef CONFIG_PRINTK
  102. asmlinkage __printf(5, 0)
  103. int vprintk_emit(int facility, int level,
  104. const char *dict, size_t dictlen,
  105. const char *fmt, va_list args);
  106. asmlinkage __printf(1, 0)
  107. int vprintk(const char *fmt, va_list args);
  108. asmlinkage __printf(5, 6) __cold
  109. asmlinkage int printk_emit(int facility, int level,
  110. const char *dict, size_t dictlen,
  111. const char *fmt, ...);
  112. asmlinkage __printf(1, 2) __cold
  113. int printk(const char *fmt, ...);
  114. /*
  115. * Special printk facility for scheduler use only, _DO_NOT_USE_ !
  116. */
  117. __printf(1, 2) __cold int printk_sched(const char *fmt, ...);
  118. /*
  119. * Please don't use printk_ratelimit(), because it shares ratelimiting state
  120. * with all other unrelated printk_ratelimit() callsites. Instead use
  121. * printk_ratelimited() or plain old __ratelimit().
  122. */
  123. extern int __printk_ratelimit(const char *func);
  124. #define printk_ratelimit() __printk_ratelimit(__func__)
  125. extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  126. unsigned int interval_msec);
  127. extern int printk_delay_msec;
  128. extern int dmesg_restrict;
  129. extern int kptr_restrict;
  130. extern void wake_up_klogd(void);
  131. void log_buf_kexec_setup(void);
  132. void __init setup_log_buf(int early);
  133. void dump_stack_set_arch_desc(const char *fmt, ...);
  134. void dump_stack_print_info(const char *log_lvl);
  135. void show_regs_print_info(const char *log_lvl);
  136. #else
  137. static inline __printf(1, 0)
  138. int vprintk(const char *s, va_list args)
  139. {
  140. return 0;
  141. }
  142. static inline __printf(1, 2) __cold
  143. int printk(const char *s, ...)
  144. {
  145. return 0;
  146. }
  147. static inline __printf(1, 2) __cold
  148. int printk_sched(const char *s, ...)
  149. {
  150. return 0;
  151. }
  152. static inline int printk_ratelimit(void)
  153. {
  154. return 0;
  155. }
  156. static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  157. unsigned int interval_msec)
  158. {
  159. return false;
  160. }
  161. static inline void wake_up_klogd(void)
  162. {
  163. }
  164. static inline void log_buf_kexec_setup(void)
  165. {
  166. }
  167. static inline void setup_log_buf(int early)
  168. {
  169. }
  170. static inline void dump_stack_set_arch_desc(const char *fmt, ...)
  171. {
  172. }
  173. static inline void dump_stack_print_info(const char *log_lvl)
  174. {
  175. }
  176. static inline void show_regs_print_info(const char *log_lvl)
  177. {
  178. }
  179. #endif
  180. extern asmlinkage void dump_stack(void) __cold;
  181. #ifndef pr_fmt
  182. #define pr_fmt(fmt) fmt
  183. #endif
  184. #define pr_emerg(fmt, ...) \
  185. printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  186. #define pr_alert(fmt, ...) \
  187. printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  188. #define pr_crit(fmt, ...) \
  189. printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  190. #define pr_err(fmt, ...) \
  191. printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  192. #define pr_warning(fmt, ...) \
  193. printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  194. #define pr_warn pr_warning
  195. #define pr_notice(fmt, ...) \
  196. printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  197. #define pr_info(fmt, ...) \
  198. printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  199. #define pr_cont(fmt, ...) \
  200. printk(KERN_CONT fmt, ##__VA_ARGS__)
  201. /* pr_devel() should produce zero code unless DEBUG is defined */
  202. #ifdef DEBUG
  203. #define pr_devel(fmt, ...) \
  204. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  205. #else
  206. #define pr_devel(fmt, ...) \
  207. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  208. #endif
  209. #include <linux/dynamic_debug.h>
  210. /* If you are writing a driver, please use dev_dbg instead */
  211. #if defined(CONFIG_DYNAMIC_DEBUG)
  212. /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
  213. #define pr_debug(fmt, ...) \
  214. dynamic_pr_debug(fmt, ##__VA_ARGS__)
  215. #elif defined(DEBUG)
  216. #define pr_debug(fmt, ...) \
  217. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  218. #else
  219. #define pr_debug(fmt, ...) \
  220. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  221. #endif
  222. /*
  223. * Print a one-time message (analogous to WARN_ONCE() et al):
  224. */
  225. #ifdef CONFIG_PRINTK
  226. #define printk_once(fmt, ...) \
  227. ({ \
  228. static bool __print_once; \
  229. \
  230. if (!__print_once) { \
  231. __print_once = true; \
  232. printk(fmt, ##__VA_ARGS__); \
  233. } \
  234. })
  235. #else
  236. #define printk_once(fmt, ...) \
  237. no_printk(fmt, ##__VA_ARGS__)
  238. #endif
  239. #define pr_emerg_once(fmt, ...) \
  240. printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  241. #define pr_alert_once(fmt, ...) \
  242. printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  243. #define pr_crit_once(fmt, ...) \
  244. printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  245. #define pr_err_once(fmt, ...) \
  246. printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  247. #define pr_warn_once(fmt, ...) \
  248. printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  249. #define pr_notice_once(fmt, ...) \
  250. printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  251. #define pr_info_once(fmt, ...) \
  252. printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  253. #define pr_cont_once(fmt, ...) \
  254. printk_once(KERN_CONT pr_fmt(fmt), ##__VA_ARGS__)
  255. #if defined(DEBUG)
  256. #define pr_devel_once(fmt, ...) \
  257. printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  258. #else
  259. #define pr_devel_once(fmt, ...) \
  260. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  261. #endif
  262. /* If you are writing a driver, please use dev_dbg instead */
  263. #if defined(DEBUG)
  264. #define pr_debug_once(fmt, ...) \
  265. printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  266. #else
  267. #define pr_debug_once(fmt, ...) \
  268. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  269. #endif
  270. /*
  271. * ratelimited messages with local ratelimit_state,
  272. * no local ratelimit_state used in the !PRINTK case
  273. */
  274. #ifdef CONFIG_PRINTK
  275. #define printk_ratelimited(fmt, ...) \
  276. ({ \
  277. static DEFINE_RATELIMIT_STATE(_rs, \
  278. DEFAULT_RATELIMIT_INTERVAL, \
  279. DEFAULT_RATELIMIT_BURST); \
  280. \
  281. if (__ratelimit(&_rs)) \
  282. printk(fmt, ##__VA_ARGS__); \
  283. })
  284. #else
  285. #define printk_ratelimited(fmt, ...) \
  286. no_printk(fmt, ##__VA_ARGS__)
  287. #endif
  288. #define pr_emerg_ratelimited(fmt, ...) \
  289. printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  290. #define pr_alert_ratelimited(fmt, ...) \
  291. printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  292. #define pr_crit_ratelimited(fmt, ...) \
  293. printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  294. #define pr_err_ratelimited(fmt, ...) \
  295. printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  296. #define pr_warn_ratelimited(fmt, ...) \
  297. printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  298. #define pr_notice_ratelimited(fmt, ...) \
  299. printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  300. #define pr_info_ratelimited(fmt, ...) \
  301. printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  302. /* no pr_cont_ratelimited, don't do that... */
  303. #if defined(DEBUG)
  304. #define pr_devel_ratelimited(fmt, ...) \
  305. printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  306. #else
  307. #define pr_devel_ratelimited(fmt, ...) \
  308. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  309. #endif
  310. /* If you are writing a driver, please use dev_dbg instead */
  311. #if defined(CONFIG_DYNAMIC_DEBUG)
  312. /* descriptor check is first to prevent flooding with "callbacks suppressed" */
  313. #define pr_debug_ratelimited(fmt, ...) \
  314. do { \
  315. static DEFINE_RATELIMIT_STATE(_rs, \
  316. DEFAULT_RATELIMIT_INTERVAL, \
  317. DEFAULT_RATELIMIT_BURST); \
  318. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
  319. if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) && \
  320. __ratelimit(&_rs)) \
  321. __dynamic_pr_debug(&descriptor, fmt, ##__VA_ARGS__); \
  322. } while (0)
  323. #elif defined(DEBUG)
  324. #define pr_debug_ratelimited(fmt, ...) \
  325. printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  326. #else
  327. #define pr_debug_ratelimited(fmt, ...) \
  328. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  329. #endif
  330. extern const struct file_operations kmsg_fops;
  331. enum {
  332. DUMP_PREFIX_NONE,
  333. DUMP_PREFIX_ADDRESS,
  334. DUMP_PREFIX_OFFSET
  335. };
  336. extern void hex_dump_to_buffer(const void *buf, size_t len,
  337. int rowsize, int groupsize,
  338. char *linebuf, size_t linebuflen, bool ascii);
  339. #ifdef CONFIG_PRINTK
  340. extern void print_hex_dump(const char *level, const char *prefix_str,
  341. int prefix_type, int rowsize, int groupsize,
  342. const void *buf, size_t len, bool ascii);
  343. #if defined(CONFIG_DYNAMIC_DEBUG)
  344. #define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
  345. dynamic_hex_dump(prefix_str, prefix_type, 16, 1, buf, len, true)
  346. #else
  347. extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  348. const void *buf, size_t len);
  349. #endif /* defined(CONFIG_DYNAMIC_DEBUG) */
  350. #else
  351. static inline void print_hex_dump(const char *level, const char *prefix_str,
  352. int prefix_type, int rowsize, int groupsize,
  353. const void *buf, size_t len, bool ascii)
  354. {
  355. }
  356. static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  357. const void *buf, size_t len)
  358. {
  359. }
  360. #endif
  361. #if defined(CONFIG_DYNAMIC_DEBUG)
  362. #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
  363. groupsize, buf, len, ascii) \
  364. dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
  365. groupsize, buf, len, ascii)
  366. #else
  367. #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
  368. groupsize, buf, len, ascii) \
  369. print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize, \
  370. groupsize, buf, len, ascii)
  371. #endif /* defined(CONFIG_DYNAMIC_DEBUG) */
  372. #endif