printk.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. extern int printk_needs_cpu(int cpu);
  88. extern void printk_tick(void);
  89. #ifdef CONFIG_PRINTK
  90. asmlinkage __printf(5, 0)
  91. int vprintk_emit(int facility, int level,
  92. const char *dict, size_t dictlen,
  93. const char *fmt, va_list args);
  94. asmlinkage __printf(1, 0)
  95. int vprintk(const char *fmt, va_list args);
  96. asmlinkage __printf(5, 6) __cold
  97. asmlinkage int printk_emit(int facility, int level,
  98. const char *dict, size_t dictlen,
  99. const char *fmt, ...);
  100. asmlinkage __printf(1, 2) __cold
  101. int printk(const char *fmt, ...);
  102. /*
  103. * Special printk facility for scheduler use only, _DO_NOT_USE_ !
  104. */
  105. __printf(1, 2) __cold int printk_sched(const char *fmt, ...);
  106. /*
  107. * Please don't use printk_ratelimit(), because it shares ratelimiting state
  108. * with all other unrelated printk_ratelimit() callsites. Instead use
  109. * printk_ratelimited() or plain old __ratelimit().
  110. */
  111. extern int __printk_ratelimit(const char *func);
  112. #define printk_ratelimit() __printk_ratelimit(__func__)
  113. extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  114. unsigned int interval_msec);
  115. extern int printk_delay_msec;
  116. extern int dmesg_restrict;
  117. extern int kptr_restrict;
  118. void log_buf_kexec_setup(void);
  119. void __init setup_log_buf(int early);
  120. #else
  121. static inline __printf(1, 0)
  122. int vprintk(const char *s, va_list args)
  123. {
  124. return 0;
  125. }
  126. static inline __printf(1, 2) __cold
  127. int printk(const char *s, ...)
  128. {
  129. return 0;
  130. }
  131. static inline __printf(1, 2) __cold
  132. int printk_sched(const char *s, ...)
  133. {
  134. return 0;
  135. }
  136. static inline int printk_ratelimit(void)
  137. {
  138. return 0;
  139. }
  140. static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  141. unsigned int interval_msec)
  142. {
  143. return false;
  144. }
  145. static inline void log_buf_kexec_setup(void)
  146. {
  147. }
  148. static inline void setup_log_buf(int early)
  149. {
  150. }
  151. #endif
  152. extern void dump_stack(void) __cold;
  153. #ifndef pr_fmt
  154. #define pr_fmt(fmt) fmt
  155. #endif
  156. #define pr_emerg(fmt, ...) \
  157. printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  158. #define pr_alert(fmt, ...) \
  159. printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  160. #define pr_crit(fmt, ...) \
  161. printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  162. #define pr_err(fmt, ...) \
  163. printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  164. #define pr_warning(fmt, ...) \
  165. printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  166. #define pr_warn pr_warning
  167. #define pr_notice(fmt, ...) \
  168. printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  169. #define pr_info(fmt, ...) \
  170. printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  171. #define pr_cont(fmt, ...) \
  172. printk(KERN_CONT fmt, ##__VA_ARGS__)
  173. /* pr_devel() should produce zero code unless DEBUG is defined */
  174. #ifdef DEBUG
  175. #define pr_devel(fmt, ...) \
  176. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  177. #else
  178. #define pr_devel(fmt, ...) \
  179. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  180. #endif
  181. /* If you are writing a driver, please use dev_dbg instead */
  182. #if defined(CONFIG_DYNAMIC_DEBUG)
  183. /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
  184. #define pr_debug(fmt, ...) \
  185. dynamic_pr_debug(fmt, ##__VA_ARGS__)
  186. #elif defined(DEBUG)
  187. #define pr_debug(fmt, ...) \
  188. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  189. #else
  190. #define pr_debug(fmt, ...) \
  191. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  192. #endif
  193. /*
  194. * Print a one-time message (analogous to WARN_ONCE() et al):
  195. */
  196. #ifdef CONFIG_PRINTK
  197. #define printk_once(fmt, ...) \
  198. ({ \
  199. static bool __print_once; \
  200. \
  201. if (!__print_once) { \
  202. __print_once = true; \
  203. printk(fmt, ##__VA_ARGS__); \
  204. } \
  205. })
  206. #else
  207. #define printk_once(fmt, ...) \
  208. no_printk(fmt, ##__VA_ARGS__)
  209. #endif
  210. #define pr_emerg_once(fmt, ...) \
  211. printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  212. #define pr_alert_once(fmt, ...) \
  213. printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  214. #define pr_crit_once(fmt, ...) \
  215. printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  216. #define pr_err_once(fmt, ...) \
  217. printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  218. #define pr_warn_once(fmt, ...) \
  219. printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  220. #define pr_notice_once(fmt, ...) \
  221. printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  222. #define pr_info_once(fmt, ...) \
  223. printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  224. #define pr_cont_once(fmt, ...) \
  225. printk_once(KERN_CONT pr_fmt(fmt), ##__VA_ARGS__)
  226. /* If you are writing a driver, please use dev_dbg instead */
  227. #if defined(DEBUG)
  228. #define pr_debug_once(fmt, ...) \
  229. printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  230. #else
  231. #define pr_debug_once(fmt, ...) \
  232. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  233. #endif
  234. /*
  235. * ratelimited messages with local ratelimit_state,
  236. * no local ratelimit_state used in the !PRINTK case
  237. */
  238. #ifdef CONFIG_PRINTK
  239. #define printk_ratelimited(fmt, ...) \
  240. ({ \
  241. static DEFINE_RATELIMIT_STATE(_rs, \
  242. DEFAULT_RATELIMIT_INTERVAL, \
  243. DEFAULT_RATELIMIT_BURST); \
  244. \
  245. if (__ratelimit(&_rs)) \
  246. printk(fmt, ##__VA_ARGS__); \
  247. })
  248. #else
  249. #define printk_ratelimited(fmt, ...) \
  250. no_printk(fmt, ##__VA_ARGS__)
  251. #endif
  252. #define pr_emerg_ratelimited(fmt, ...) \
  253. printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  254. #define pr_alert_ratelimited(fmt, ...) \
  255. printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  256. #define pr_crit_ratelimited(fmt, ...) \
  257. printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  258. #define pr_err_ratelimited(fmt, ...) \
  259. printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  260. #define pr_warn_ratelimited(fmt, ...) \
  261. printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  262. #define pr_notice_ratelimited(fmt, ...) \
  263. printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  264. #define pr_info_ratelimited(fmt, ...) \
  265. printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  266. /* no pr_cont_ratelimited, don't do that... */
  267. /* If you are writing a driver, please use dev_dbg instead */
  268. #if defined(DEBUG)
  269. #define pr_debug_ratelimited(fmt, ...) \
  270. printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  271. #else
  272. #define pr_debug_ratelimited(fmt, ...) \
  273. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  274. #endif
  275. extern const struct file_operations kmsg_fops;
  276. enum {
  277. DUMP_PREFIX_NONE,
  278. DUMP_PREFIX_ADDRESS,
  279. DUMP_PREFIX_OFFSET
  280. };
  281. extern void hex_dump_to_buffer(const void *buf, size_t len,
  282. int rowsize, int groupsize,
  283. char *linebuf, size_t linebuflen, bool ascii);
  284. #ifdef CONFIG_PRINTK
  285. extern void print_hex_dump(const char *level, const char *prefix_str,
  286. int prefix_type, int rowsize, int groupsize,
  287. const void *buf, size_t len, bool ascii);
  288. extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  289. const void *buf, size_t len);
  290. #else
  291. static inline void print_hex_dump(const char *level, const char *prefix_str,
  292. int prefix_type, int rowsize, int groupsize,
  293. const void *buf, size_t len, bool ascii)
  294. {
  295. }
  296. static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  297. const void *buf, size_t len)
  298. {
  299. }
  300. #endif
  301. #endif