printk.h 13 KB

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