printk.h 9.0 KB

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