printk.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #include <linux/cache.h>
  8. extern const char linux_banner[];
  9. extern const char linux_proc_banner[];
  10. static inline int printk_get_level(const char *buffer)
  11. {
  12. if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
  13. switch (buffer[1]) {
  14. case '0' ... '7':
  15. case 'd': /* KERN_DEFAULT */
  16. return buffer[1];
  17. }
  18. }
  19. return 0;
  20. }
  21. static inline const char *printk_skip_level(const char *buffer)
  22. {
  23. if (printk_get_level(buffer))
  24. return buffer + 2;
  25. return buffer;
  26. }
  27. /* printk's without a loglevel use this.. */
  28. #define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT
  29. /* We show everything that is MORE important than this.. */
  30. #define CONSOLE_LOGLEVEL_SILENT 0 /* Mum's the word */
  31. #define CONSOLE_LOGLEVEL_MIN 1 /* Minimum loglevel we let people use */
  32. #define CONSOLE_LOGLEVEL_QUIET 4 /* Shhh ..., when booted with "quiet" */
  33. #define CONSOLE_LOGLEVEL_DEFAULT 7 /* anything MORE serious than KERN_DEBUG */
  34. #define CONSOLE_LOGLEVEL_DEBUG 10 /* issue debug messages */
  35. #define CONSOLE_LOGLEVEL_MOTORMOUTH 15 /* You can't shut this one up */
  36. extern int console_printk[];
  37. #define console_loglevel (console_printk[0])
  38. #define default_message_loglevel (console_printk[1])
  39. #define minimum_console_loglevel (console_printk[2])
  40. #define default_console_loglevel (console_printk[3])
  41. static inline void console_silent(void)
  42. {
  43. console_loglevel = CONSOLE_LOGLEVEL_SILENT;
  44. }
  45. static inline void console_verbose(void)
  46. {
  47. if (console_loglevel)
  48. console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
  49. }
  50. struct va_format {
  51. const char *fmt;
  52. va_list *va;
  53. };
  54. /*
  55. * FW_BUG
  56. * Add this to a message where you are sure the firmware is buggy or behaves
  57. * really stupid or out of spec. Be aware that the responsible BIOS developer
  58. * should be able to fix this issue or at least get a concrete idea of the
  59. * problem by reading your message without the need of looking at the kernel
  60. * code.
  61. *
  62. * Use it for definite and high priority BIOS bugs.
  63. *
  64. * FW_WARN
  65. * Use it for not that clear (e.g. could the kernel messed up things already?)
  66. * and medium priority BIOS bugs.
  67. *
  68. * FW_INFO
  69. * Use this one if you want to tell the user or vendor about something
  70. * suspicious, but generally harmless related to the firmware.
  71. *
  72. * Use it for information or very low priority BIOS bugs.
  73. */
  74. #define FW_BUG "[Firmware Bug]: "
  75. #define FW_WARN "[Firmware Warn]: "
  76. #define FW_INFO "[Firmware Info]: "
  77. /*
  78. * HW_ERR
  79. * Add this to a message for hardware errors, so that user can report
  80. * it to hardware vendor instead of LKML or software vendor.
  81. */
  82. #define HW_ERR "[Hardware Error]: "
  83. /*
  84. * DEPRECATED
  85. * Add this to a message whenever you want to warn user space about the use
  86. * of a deprecated aspect of an API so they can stop using it
  87. */
  88. #define DEPRECATED "[Deprecated]: "
  89. /*
  90. * Dummy printk for disabled debugging statements to use whilst maintaining
  91. * gcc's format and side-effect checking.
  92. */
  93. static inline __printf(1, 2)
  94. int no_printk(const char *fmt, ...)
  95. {
  96. return 0;
  97. }
  98. #ifdef CONFIG_EARLY_PRINTK
  99. extern asmlinkage __printf(1, 2)
  100. void early_printk(const char *fmt, ...);
  101. #else
  102. static inline __printf(1, 2) __cold
  103. void early_printk(const char *s, ...) { }
  104. #endif
  105. typedef int(*printk_func_t)(const char *fmt, va_list args);
  106. #ifdef CONFIG_PRINTK
  107. asmlinkage __printf(5, 0)
  108. int vprintk_emit(int facility, int level,
  109. const char *dict, size_t dictlen,
  110. const char *fmt, va_list args);
  111. asmlinkage __printf(1, 0)
  112. int vprintk(const char *fmt, va_list args);
  113. asmlinkage __printf(5, 6) __cold
  114. int printk_emit(int facility, int level,
  115. const char *dict, size_t dictlen,
  116. const char *fmt, ...);
  117. asmlinkage __printf(1, 2) __cold
  118. int printk(const char *fmt, ...);
  119. /*
  120. * Special printk facility for scheduler/timekeeping use only, _DO_NOT_USE_ !
  121. */
  122. __printf(1, 2) __cold int printk_deferred(const char *fmt, ...);
  123. /*
  124. * Please don't use printk_ratelimit(), because it shares ratelimiting state
  125. * with all other unrelated printk_ratelimit() callsites. Instead use
  126. * printk_ratelimited() or plain old __ratelimit().
  127. */
  128. extern int __printk_ratelimit(const char *func);
  129. #define printk_ratelimit() __printk_ratelimit(__func__)
  130. extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  131. unsigned int interval_msec);
  132. extern int printk_delay_msec;
  133. extern int dmesg_restrict;
  134. extern int kptr_restrict;
  135. extern void wake_up_klogd(void);
  136. char *log_buf_addr_get(void);
  137. u32 log_buf_len_get(void);
  138. void log_buf_kexec_setup(void);
  139. void __init setup_log_buf(int early);
  140. void dump_stack_set_arch_desc(const char *fmt, ...);
  141. void dump_stack_print_info(const char *log_lvl);
  142. void show_regs_print_info(const char *log_lvl);
  143. #else
  144. static inline __printf(1, 0)
  145. int vprintk(const char *s, va_list args)
  146. {
  147. return 0;
  148. }
  149. static inline __printf(1, 2) __cold
  150. int printk(const char *s, ...)
  151. {
  152. return 0;
  153. }
  154. static inline __printf(1, 2) __cold
  155. int printk_deferred(const char *s, ...)
  156. {
  157. return 0;
  158. }
  159. static inline int printk_ratelimit(void)
  160. {
  161. return 0;
  162. }
  163. static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  164. unsigned int interval_msec)
  165. {
  166. return false;
  167. }
  168. static inline void wake_up_klogd(void)
  169. {
  170. }
  171. static inline char *log_buf_addr_get(void)
  172. {
  173. return NULL;
  174. }
  175. static inline u32 log_buf_len_get(void)
  176. {
  177. return 0;
  178. }
  179. static inline void log_buf_kexec_setup(void)
  180. {
  181. }
  182. static inline void setup_log_buf(int early)
  183. {
  184. }
  185. static inline void dump_stack_set_arch_desc(const char *fmt, ...)
  186. {
  187. }
  188. static inline void dump_stack_print_info(const char *log_lvl)
  189. {
  190. }
  191. static inline void show_regs_print_info(const char *log_lvl)
  192. {
  193. }
  194. #endif
  195. extern asmlinkage void dump_stack(void) __cold;
  196. #ifndef pr_fmt
  197. #define pr_fmt(fmt) fmt
  198. #endif
  199. /*
  200. * These can be used to print at the various log levels.
  201. * All of these will print unconditionally, although note that pr_debug()
  202. * and other debug macros are compiled out unless either DEBUG is defined
  203. * or CONFIG_DYNAMIC_DEBUG is set.
  204. */
  205. #define pr_emerg(fmt, ...) \
  206. printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  207. #define pr_alert(fmt, ...) \
  208. printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  209. #define pr_crit(fmt, ...) \
  210. printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  211. #define pr_err(fmt, ...) \
  212. printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  213. #define pr_warning(fmt, ...) \
  214. printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  215. #define pr_warn pr_warning
  216. #define pr_notice(fmt, ...) \
  217. printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  218. #define pr_info(fmt, ...) \
  219. printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  220. #define pr_cont(fmt, ...) \
  221. printk(KERN_CONT fmt, ##__VA_ARGS__)
  222. /* pr_devel() should produce zero code unless DEBUG is defined */
  223. #ifdef DEBUG
  224. #define pr_devel(fmt, ...) \
  225. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  226. #else
  227. #define pr_devel(fmt, ...) \
  228. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  229. #endif
  230. #include <linux/dynamic_debug.h>
  231. /* If you are writing a driver, please use dev_dbg instead */
  232. #if defined(CONFIG_DYNAMIC_DEBUG)
  233. /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
  234. #define pr_debug(fmt, ...) \
  235. dynamic_pr_debug(fmt, ##__VA_ARGS__)
  236. #elif defined(DEBUG)
  237. #define pr_debug(fmt, ...) \
  238. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  239. #else
  240. #define pr_debug(fmt, ...) \
  241. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  242. #endif
  243. /*
  244. * Print a one-time message (analogous to WARN_ONCE() et al):
  245. */
  246. #ifdef CONFIG_PRINTK
  247. #define printk_once(fmt, ...) \
  248. ({ \
  249. static bool __print_once __read_mostly; \
  250. \
  251. if (!__print_once) { \
  252. __print_once = true; \
  253. printk(fmt, ##__VA_ARGS__); \
  254. } \
  255. })
  256. #define printk_deferred_once(fmt, ...) \
  257. ({ \
  258. static bool __print_once __read_mostly; \
  259. \
  260. if (!__print_once) { \
  261. __print_once = true; \
  262. printk_deferred(fmt, ##__VA_ARGS__); \
  263. } \
  264. })
  265. #else
  266. #define printk_once(fmt, ...) \
  267. no_printk(fmt, ##__VA_ARGS__)
  268. #define printk_deferred_once(fmt, ...) \
  269. no_printk(fmt, ##__VA_ARGS__)
  270. #endif
  271. #define pr_emerg_once(fmt, ...) \
  272. printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  273. #define pr_alert_once(fmt, ...) \
  274. printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  275. #define pr_crit_once(fmt, ...) \
  276. printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  277. #define pr_err_once(fmt, ...) \
  278. printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  279. #define pr_warn_once(fmt, ...) \
  280. printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  281. #define pr_notice_once(fmt, ...) \
  282. printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  283. #define pr_info_once(fmt, ...) \
  284. printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  285. #define pr_cont_once(fmt, ...) \
  286. printk_once(KERN_CONT pr_fmt(fmt), ##__VA_ARGS__)
  287. #if defined(DEBUG)
  288. #define pr_devel_once(fmt, ...) \
  289. printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  290. #else
  291. #define pr_devel_once(fmt, ...) \
  292. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  293. #endif
  294. /* If you are writing a driver, please use dev_dbg instead */
  295. #if defined(DEBUG)
  296. #define pr_debug_once(fmt, ...) \
  297. printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  298. #else
  299. #define pr_debug_once(fmt, ...) \
  300. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  301. #endif
  302. /*
  303. * ratelimited messages with local ratelimit_state,
  304. * no local ratelimit_state used in the !PRINTK case
  305. */
  306. #ifdef CONFIG_PRINTK
  307. #define printk_ratelimited(fmt, ...) \
  308. ({ \
  309. static DEFINE_RATELIMIT_STATE(_rs, \
  310. DEFAULT_RATELIMIT_INTERVAL, \
  311. DEFAULT_RATELIMIT_BURST); \
  312. \
  313. if (__ratelimit(&_rs)) \
  314. printk(fmt, ##__VA_ARGS__); \
  315. })
  316. #else
  317. #define printk_ratelimited(fmt, ...) \
  318. no_printk(fmt, ##__VA_ARGS__)
  319. #endif
  320. #define pr_emerg_ratelimited(fmt, ...) \
  321. printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  322. #define pr_alert_ratelimited(fmt, ...) \
  323. printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  324. #define pr_crit_ratelimited(fmt, ...) \
  325. printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  326. #define pr_err_ratelimited(fmt, ...) \
  327. printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  328. #define pr_warn_ratelimited(fmt, ...) \
  329. printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  330. #define pr_notice_ratelimited(fmt, ...) \
  331. printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  332. #define pr_info_ratelimited(fmt, ...) \
  333. printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  334. /* no pr_cont_ratelimited, don't do that... */
  335. #if defined(DEBUG)
  336. #define pr_devel_ratelimited(fmt, ...) \
  337. printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  338. #else
  339. #define pr_devel_ratelimited(fmt, ...) \
  340. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  341. #endif
  342. /* If you are writing a driver, please use dev_dbg instead */
  343. #if defined(CONFIG_DYNAMIC_DEBUG)
  344. /* descriptor check is first to prevent flooding with "callbacks suppressed" */
  345. #define pr_debug_ratelimited(fmt, ...) \
  346. do { \
  347. static DEFINE_RATELIMIT_STATE(_rs, \
  348. DEFAULT_RATELIMIT_INTERVAL, \
  349. DEFAULT_RATELIMIT_BURST); \
  350. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
  351. if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) && \
  352. __ratelimit(&_rs)) \
  353. __dynamic_pr_debug(&descriptor, fmt, ##__VA_ARGS__); \
  354. } while (0)
  355. #elif defined(DEBUG)
  356. #define pr_debug_ratelimited(fmt, ...) \
  357. printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  358. #else
  359. #define pr_debug_ratelimited(fmt, ...) \
  360. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  361. #endif
  362. extern const struct file_operations kmsg_fops;
  363. enum {
  364. DUMP_PREFIX_NONE,
  365. DUMP_PREFIX_ADDRESS,
  366. DUMP_PREFIX_OFFSET
  367. };
  368. extern void hex_dump_to_buffer(const void *buf, size_t len,
  369. int rowsize, int groupsize,
  370. char *linebuf, size_t linebuflen, bool ascii);
  371. #ifdef CONFIG_PRINTK
  372. extern void print_hex_dump(const char *level, const char *prefix_str,
  373. int prefix_type, int rowsize, int groupsize,
  374. const void *buf, size_t len, bool ascii);
  375. #if defined(CONFIG_DYNAMIC_DEBUG)
  376. #define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
  377. dynamic_hex_dump(prefix_str, prefix_type, 16, 1, buf, len, true)
  378. #else
  379. extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  380. const void *buf, size_t len);
  381. #endif /* defined(CONFIG_DYNAMIC_DEBUG) */
  382. #else
  383. static inline void print_hex_dump(const char *level, const char *prefix_str,
  384. int prefix_type, int rowsize, int groupsize,
  385. const void *buf, size_t len, bool ascii)
  386. {
  387. }
  388. static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  389. const void *buf, size_t len)
  390. {
  391. }
  392. #endif
  393. #if defined(CONFIG_DYNAMIC_DEBUG)
  394. #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
  395. groupsize, buf, len, ascii) \
  396. dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
  397. groupsize, buf, len, ascii)
  398. #else
  399. #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \
  400. groupsize, buf, len, ascii) \
  401. print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize, \
  402. groupsize, buf, len, ascii)
  403. #endif /* defined(CONFIG_DYNAMIC_DEBUG) */
  404. #endif