printk.h 14 KB

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