printk.h 13 KB

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