dynamic_debug.h 6.1 KB

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