printk.h 9.1 KB

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