dynamic_debug.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #ifndef _DYNAMIC_DEBUG_H
  2. #define _DYNAMIC_DEBUG_H
  3. #if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_JUMP_LABEL)
  4. #include <linux/jump_label.h>
  5. #endif
  6. /*
  7. * An instance of this structure is created in a special
  8. * ELF section at every dynamic debug callsite. At runtime,
  9. * the special section is treated as an array of these.
  10. */
  11. struct _ddebug {
  12. /*
  13. * These fields are used to drive the user interface
  14. * for selecting and displaying debug callsites.
  15. */
  16. const char *modname;
  17. const char *function;
  18. const char *filename;
  19. const char *format;
  20. unsigned int lineno:18;
  21. /*
  22. * The flags field controls the behaviour at the callsite.
  23. * The bits here are changed dynamically when the user
  24. * writes commands to <debugfs>/dynamic_debug/control
  25. */
  26. #define _DPRINTK_FLAGS_NONE 0
  27. #define _DPRINTK_FLAGS_PRINT (1<<0) /* printk() a message using the format */
  28. #define _DPRINTK_FLAGS_INCL_MODNAME (1<<1)
  29. #define _DPRINTK_FLAGS_INCL_FUNCNAME (1<<2)
  30. #define _DPRINTK_FLAGS_INCL_LINENO (1<<3)
  31. #define _DPRINTK_FLAGS_INCL_TID (1<<4)
  32. #if defined DEBUG
  33. #define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
  34. #else
  35. #define _DPRINTK_FLAGS_DEFAULT 0
  36. #endif
  37. unsigned int flags:8;
  38. #ifdef HAVE_JUMP_LABEL
  39. union {
  40. struct static_key_true dd_key_true;
  41. struct static_key_false dd_key_false;
  42. } key;
  43. #endif
  44. } __attribute__((aligned(8)));
  45. int ddebug_add_module(struct _ddebug *tab, unsigned int n,
  46. const char *modname);
  47. #if defined(CONFIG_DYNAMIC_DEBUG)
  48. extern int ddebug_remove_module(const char *mod_name);
  49. extern __printf(2, 3)
  50. void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...);
  51. extern int ddebug_dyndbg_module_param_cb(char *param, char *val,
  52. const char *modname);
  53. struct device;
  54. extern __printf(3, 4)
  55. void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev,
  56. const char *fmt, ...);
  57. struct net_device;
  58. extern __printf(3, 4)
  59. void __dynamic_netdev_dbg(struct _ddebug *descriptor,
  60. const struct net_device *dev,
  61. const char *fmt, ...);
  62. #define DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, key, init) \
  63. static struct _ddebug __aligned(8) \
  64. __attribute__((section("__verbose"))) name = { \
  65. .modname = KBUILD_MODNAME, \
  66. .function = __func__, \
  67. .filename = __FILE__, \
  68. .format = (fmt), \
  69. .lineno = __LINE__, \
  70. .flags = _DPRINTK_FLAGS_DEFAULT, \
  71. dd_key_init(key, init) \
  72. }
  73. #ifdef HAVE_JUMP_LABEL
  74. #define dd_key_init(key, init) key = (init)
  75. #ifdef DEBUG
  76. #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
  77. DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, .key.dd_key_true, \
  78. (STATIC_KEY_TRUE_INIT))
  79. #define DYNAMIC_DEBUG_BRANCH(descriptor) \
  80. static_branch_likely(&descriptor.key.dd_key_true)
  81. #else
  82. #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
  83. DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, .key.dd_key_false, \
  84. (STATIC_KEY_FALSE_INIT))
  85. #define DYNAMIC_DEBUG_BRANCH(descriptor) \
  86. static_branch_unlikely(&descriptor.key.dd_key_false)
  87. #endif
  88. #else
  89. #define dd_key_init(key, init)
  90. #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
  91. DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, 0, 0)
  92. #ifdef DEBUG
  93. #define DYNAMIC_DEBUG_BRANCH(descriptor) \
  94. likely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
  95. #else
  96. #define DYNAMIC_DEBUG_BRANCH(descriptor) \
  97. unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
  98. #endif
  99. #endif
  100. #define dynamic_pr_debug(fmt, ...) \
  101. do { \
  102. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
  103. if (DYNAMIC_DEBUG_BRANCH(descriptor)) \
  104. __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \
  105. ##__VA_ARGS__); \
  106. } while (0)
  107. #define dynamic_dev_dbg(dev, fmt, ...) \
  108. do { \
  109. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
  110. if (DYNAMIC_DEBUG_BRANCH(descriptor)) \
  111. __dynamic_dev_dbg(&descriptor, dev, fmt, \
  112. ##__VA_ARGS__); \
  113. } while (0)
  114. #define dynamic_netdev_dbg(dev, fmt, ...) \
  115. do { \
  116. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
  117. if (DYNAMIC_DEBUG_BRANCH(descriptor)) \
  118. __dynamic_netdev_dbg(&descriptor, dev, fmt, \
  119. ##__VA_ARGS__); \
  120. } while (0)
  121. #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
  122. groupsize, buf, len, ascii) \
  123. do { \
  124. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, \
  125. __builtin_constant_p(prefix_str) ? prefix_str : "hexdump");\
  126. if (DYNAMIC_DEBUG_BRANCH(descriptor)) \
  127. print_hex_dump(KERN_DEBUG, prefix_str, \
  128. prefix_type, rowsize, groupsize, \
  129. buf, len, ascii); \
  130. } while (0)
  131. #else
  132. #include <linux/string.h>
  133. #include <linux/errno.h>
  134. static inline int ddebug_remove_module(const char *mod)
  135. {
  136. return 0;
  137. }
  138. static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
  139. const char *modname)
  140. {
  141. if (strstr(param, "dyndbg")) {
  142. /* avoid pr_warn(), which wants pr_fmt() fully defined */
  143. printk(KERN_WARNING "dyndbg param is supported only in "
  144. "CONFIG_DYNAMIC_DEBUG builds\n");
  145. return 0; /* allow and ignore */
  146. }
  147. return -EINVAL;
  148. }
  149. #define dynamic_pr_debug(fmt, ...) \
  150. do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
  151. #define dynamic_dev_dbg(dev, fmt, ...) \
  152. do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)
  153. #endif
  154. #endif