dynamic_debug.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_CORE)
  47. /* exported for module authors to exercise >control */
  48. int dynamic_debug_exec_queries(const char *query, const char *modname);
  49. int ddebug_add_module(struct _ddebug *tab, unsigned int n,
  50. const char *modname);
  51. extern int ddebug_remove_module(const char *mod_name);
  52. extern __printf(2, 3)
  53. void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...);
  54. extern int ddebug_dyndbg_module_param_cb(char *param, char *val,
  55. const char *modname);
  56. struct device;
  57. extern __printf(3, 4)
  58. void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev,
  59. const char *fmt, ...);
  60. struct net_device;
  61. extern __printf(3, 4)
  62. void __dynamic_netdev_dbg(struct _ddebug *descriptor,
  63. const struct net_device *dev,
  64. const char *fmt, ...);
  65. struct ib_device;
  66. extern __printf(3, 4)
  67. void __dynamic_ibdev_dbg(struct _ddebug *descriptor,
  68. const struct ib_device *ibdev,
  69. const char *fmt, ...);
  70. #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
  71. static struct _ddebug __aligned(8) \
  72. __section("__dyndbg") name = { \
  73. .modname = KBUILD_MODNAME, \
  74. .function = __func__, \
  75. .filename = __FILE__, \
  76. .format = (fmt), \
  77. .lineno = __LINE__, \
  78. .flags = _DPRINTK_FLAGS_DEFAULT, \
  79. _DPRINTK_KEY_INIT \
  80. }
  81. #ifdef CONFIG_JUMP_LABEL
  82. #ifdef DEBUG
  83. #define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT)
  84. #define DYNAMIC_DEBUG_BRANCH(descriptor) \
  85. static_branch_likely(&descriptor.key.dd_key_true)
  86. #else
  87. #define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT)
  88. #define DYNAMIC_DEBUG_BRANCH(descriptor) \
  89. static_branch_unlikely(&descriptor.key.dd_key_false)
  90. #endif
  91. #else /* !CONFIG_JUMP_LABEL */
  92. #define _DPRINTK_KEY_INIT
  93. #ifdef DEBUG
  94. #define DYNAMIC_DEBUG_BRANCH(descriptor) \
  95. likely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
  96. #else
  97. #define DYNAMIC_DEBUG_BRANCH(descriptor) \
  98. unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
  99. #endif
  100. #endif /* CONFIG_JUMP_LABEL */
  101. #define __dynamic_func_call(id, fmt, func, ...) do { \
  102. DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \
  103. if (DYNAMIC_DEBUG_BRANCH(id)) \
  104. func(&id, ##__VA_ARGS__); \
  105. } while (0)
  106. #define __dynamic_func_call_no_desc(id, fmt, func, ...) do { \
  107. DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \
  108. if (DYNAMIC_DEBUG_BRANCH(id)) \
  109. func(__VA_ARGS__); \
  110. } while (0)
  111. /*
  112. * "Factory macro" for generating a call to func, guarded by a
  113. * DYNAMIC_DEBUG_BRANCH. The dynamic debug descriptor will be
  114. * initialized using the fmt argument. The function will be called with
  115. * the address of the descriptor as first argument, followed by all
  116. * the varargs. Note that fmt is repeated in invocations of this
  117. * macro.
  118. */
  119. #define _dynamic_func_call(fmt, func, ...) \
  120. __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
  121. /*
  122. * A variant that does the same, except that the descriptor is not
  123. * passed as the first argument to the function; it is only called
  124. * with precisely the macro's varargs.
  125. */
  126. #define _dynamic_func_call_no_desc(fmt, func, ...) \
  127. __dynamic_func_call_no_desc(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
  128. #define dynamic_pr_debug(fmt, ...) \
  129. _dynamic_func_call(fmt, __dynamic_pr_debug, \
  130. pr_fmt(fmt), ##__VA_ARGS__)
  131. #define dynamic_dev_dbg(dev, fmt, ...) \
  132. _dynamic_func_call(fmt,__dynamic_dev_dbg, \
  133. dev, fmt, ##__VA_ARGS__)
  134. #define dynamic_netdev_dbg(dev, fmt, ...) \
  135. _dynamic_func_call(fmt, __dynamic_netdev_dbg, \
  136. dev, fmt, ##__VA_ARGS__)
  137. #define dynamic_ibdev_dbg(dev, fmt, ...) \
  138. _dynamic_func_call(fmt, __dynamic_ibdev_dbg, \
  139. dev, fmt, ##__VA_ARGS__)
  140. #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
  141. groupsize, buf, len, ascii) \
  142. _dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \
  143. print_hex_dump, \
  144. KERN_DEBUG, prefix_str, prefix_type, \
  145. rowsize, groupsize, buf, len, ascii)
  146. #else /* !CONFIG_DYNAMIC_DEBUG_CORE */
  147. #include <linux/string.h>
  148. #include <linux/errno.h>
  149. #include <linux/printk.h>
  150. static inline int ddebug_add_module(struct _ddebug *tab, unsigned int n,
  151. const char *modname)
  152. {
  153. return 0;
  154. }
  155. static inline int ddebug_remove_module(const char *mod)
  156. {
  157. return 0;
  158. }
  159. static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
  160. const char *modname)
  161. {
  162. if (strstr(param, "dyndbg")) {
  163. /* avoid pr_warn(), which wants pr_fmt() fully defined */
  164. printk(KERN_WARNING "dyndbg param is supported only in "
  165. "CONFIG_DYNAMIC_DEBUG builds\n");
  166. return 0; /* allow and ignore */
  167. }
  168. return -EINVAL;
  169. }
  170. #define dynamic_pr_debug(fmt, ...) \
  171. do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
  172. #define dynamic_dev_dbg(dev, fmt, ...) \
  173. do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)
  174. #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
  175. groupsize, buf, len, ascii) \
  176. do { if (0) \
  177. print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, \
  178. rowsize, groupsize, buf, len, ascii); \
  179. } while (0)
  180. static inline int dynamic_debug_exec_queries(const char *query, const char *modname)
  181. {
  182. pr_warn("kernel not built with CONFIG_DYNAMIC_DEBUG_CORE\n");
  183. return 0;
  184. }
  185. #endif /* !CONFIG_DYNAMIC_DEBUG_CORE */
  186. #endif