dynamic_debug.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. #include <linux/build_bug.h>
  8. /*
  9. * An instance of this structure is created in a special
  10. * ELF section at every dynamic debug callsite. At runtime,
  11. * the special section is treated as an array of these.
  12. */
  13. struct _ddebug {
  14. /*
  15. * These fields are used to drive the user interface
  16. * for selecting and displaying debug callsites.
  17. */
  18. const char *modname;
  19. const char *function;
  20. const char *filename;
  21. const char *format;
  22. unsigned int lineno:18;
  23. #define CLS_BITS 6
  24. unsigned int class_id:CLS_BITS;
  25. #define _DPRINTK_CLASS_DFLT ((1 << CLS_BITS) - 1)
  26. /*
  27. * The flags field controls the behaviour at the callsite.
  28. * The bits here are changed dynamically when the user
  29. * writes commands to <debugfs>/dynamic_debug/control
  30. */
  31. #define _DPRINTK_FLAGS_NONE 0
  32. #define _DPRINTK_FLAGS_PRINT (1<<0) /* printk() a message using the format */
  33. #define _DPRINTK_FLAGS_INCL_MODNAME (1<<1)
  34. #define _DPRINTK_FLAGS_INCL_FUNCNAME (1<<2)
  35. #define _DPRINTK_FLAGS_INCL_LINENO (1<<3)
  36. #define _DPRINTK_FLAGS_INCL_TID (1<<4)
  37. #define _DPRINTK_FLAGS_INCL_ANY \
  38. (_DPRINTK_FLAGS_INCL_MODNAME | _DPRINTK_FLAGS_INCL_FUNCNAME |\
  39. _DPRINTK_FLAGS_INCL_LINENO | _DPRINTK_FLAGS_INCL_TID)
  40. #if defined DEBUG
  41. #define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
  42. #else
  43. #define _DPRINTK_FLAGS_DEFAULT 0
  44. #endif
  45. unsigned int flags:8;
  46. #ifdef CONFIG_JUMP_LABEL
  47. union {
  48. struct static_key_true dd_key_true;
  49. struct static_key_false dd_key_false;
  50. } key;
  51. #endif
  52. } __attribute__((aligned(8)));
  53. enum class_map_type {
  54. DD_CLASS_TYPE_DISJOINT_BITS,
  55. /**
  56. * DD_CLASS_TYPE_DISJOINT_BITS: classes are independent, one per bit.
  57. * expecting hex input. Built for drm.debug, basis for other types.
  58. */
  59. DD_CLASS_TYPE_LEVEL_NUM,
  60. /**
  61. * DD_CLASS_TYPE_LEVEL_NUM: input is numeric level, 0-N.
  62. * N turns on just bits N-1 .. 0, so N=0 turns all bits off.
  63. */
  64. DD_CLASS_TYPE_DISJOINT_NAMES,
  65. /**
  66. * DD_CLASS_TYPE_DISJOINT_NAMES: input is a CSV of [+-]CLASS_NAMES,
  67. * classes are independent, like _DISJOINT_BITS.
  68. */
  69. DD_CLASS_TYPE_LEVEL_NAMES,
  70. /**
  71. * DD_CLASS_TYPE_LEVEL_NAMES: input is a CSV of [+-]CLASS_NAMES,
  72. * intended for names like: INFO,DEBUG,TRACE, with a module prefix
  73. * avoid EMERG,ALERT,CRIT,ERR,WARNING: they're not debug
  74. */
  75. };
  76. struct ddebug_class_map {
  77. struct list_head link;
  78. struct module *mod;
  79. const char *mod_name; /* needed for builtins */
  80. const char **class_names;
  81. const int length;
  82. const int base; /* index of 1st .class_id, allows split/shared space */
  83. enum class_map_type map_type;
  84. };
  85. /**
  86. * DECLARE_DYNDBG_CLASSMAP - declare classnames known by a module
  87. * @_var: a struct ddebug_class_map, passed to module_param_cb
  88. * @_type: enum class_map_type, chooses bits/verbose, numeric/symbolic
  89. * @_base: offset of 1st class-name. splits .class_id space
  90. * @classes: class-names used to control class'd prdbgs
  91. */
  92. #define DECLARE_DYNDBG_CLASSMAP(_var, _maptype, _base, ...) \
  93. static const char *_var##_classnames[] = { __VA_ARGS__ }; \
  94. static struct ddebug_class_map __aligned(8) __used \
  95. __section("__dyndbg_classes") _var = { \
  96. .mod = THIS_MODULE, \
  97. .mod_name = KBUILD_MODNAME, \
  98. .base = _base, \
  99. .map_type = _maptype, \
  100. .length = NUM_TYPE_ARGS(char*, __VA_ARGS__), \
  101. .class_names = _var##_classnames, \
  102. }
  103. #define NUM_TYPE_ARGS(eltype, ...) \
  104. (sizeof((eltype[]){__VA_ARGS__}) / sizeof(eltype))
  105. /* encapsulate linker provided built-in (or module) dyndbg data */
  106. struct _ddebug_info {
  107. struct _ddebug *descs;
  108. struct ddebug_class_map *classes;
  109. unsigned int num_descs;
  110. unsigned int num_classes;
  111. };
  112. struct ddebug_class_param {
  113. union {
  114. unsigned long *bits;
  115. unsigned int *lvl;
  116. };
  117. char flags[8];
  118. const struct ddebug_class_map *map;
  119. };
  120. #if defined(CONFIG_DYNAMIC_DEBUG_CORE)
  121. int ddebug_add_module(struct _ddebug_info *dyndbg, const char *modname);
  122. extern int ddebug_remove_module(const char *mod_name);
  123. extern __printf(2, 3)
  124. void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...);
  125. extern int ddebug_dyndbg_module_param_cb(char *param, char *val,
  126. const char *modname);
  127. struct device;
  128. extern __printf(3, 4)
  129. void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev,
  130. const char *fmt, ...);
  131. struct net_device;
  132. extern __printf(3, 4)
  133. void __dynamic_netdev_dbg(struct _ddebug *descriptor,
  134. const struct net_device *dev,
  135. const char *fmt, ...);
  136. struct ib_device;
  137. extern __printf(3, 4)
  138. void __dynamic_ibdev_dbg(struct _ddebug *descriptor,
  139. const struct ib_device *ibdev,
  140. const char *fmt, ...);
  141. #define DEFINE_DYNAMIC_DEBUG_METADATA_CLS(name, cls, fmt) \
  142. static struct _ddebug __aligned(8) \
  143. __section("__dyndbg") name = { \
  144. .modname = KBUILD_MODNAME, \
  145. .function = __func__, \
  146. .filename = __FILE__, \
  147. .format = (fmt), \
  148. .lineno = __LINE__, \
  149. .flags = _DPRINTK_FLAGS_DEFAULT, \
  150. .class_id = cls, \
  151. _DPRINTK_KEY_INIT \
  152. }; \
  153. BUILD_BUG_ON_MSG(cls > _DPRINTK_CLASS_DFLT, \
  154. "classid value overflow")
  155. #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
  156. DEFINE_DYNAMIC_DEBUG_METADATA_CLS(name, _DPRINTK_CLASS_DFLT, fmt)
  157. #ifdef CONFIG_JUMP_LABEL
  158. #ifdef DEBUG
  159. #define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT)
  160. #define DYNAMIC_DEBUG_BRANCH(descriptor) \
  161. static_branch_likely(&descriptor.key.dd_key_true)
  162. #else
  163. #define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT)
  164. #define DYNAMIC_DEBUG_BRANCH(descriptor) \
  165. static_branch_unlikely(&descriptor.key.dd_key_false)
  166. #endif
  167. #else /* !CONFIG_JUMP_LABEL */
  168. #define _DPRINTK_KEY_INIT
  169. #ifdef DEBUG
  170. #define DYNAMIC_DEBUG_BRANCH(descriptor) \
  171. likely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
  172. #else
  173. #define DYNAMIC_DEBUG_BRANCH(descriptor) \
  174. unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
  175. #endif
  176. #endif /* CONFIG_JUMP_LABEL */
  177. /*
  178. * Factory macros: ($prefix)dynamic_func_call($suffix)
  179. *
  180. * Lower layer (with __ prefix) gets the callsite metadata, and wraps
  181. * the func inside a debug-branch/static-key construct. Upper layer
  182. * (with _ prefix) does the UNIQUE_ID once, so that lower can ref the
  183. * name/label multiple times, and tie the elements together.
  184. * Multiple flavors:
  185. * (|_cls): adds in _DPRINT_CLASS_DFLT as needed
  186. * (|_no_desc): former gets callsite descriptor as 1st arg (for prdbgs)
  187. */
  188. #define __dynamic_func_call_cls(id, cls, fmt, func, ...) do { \
  189. DEFINE_DYNAMIC_DEBUG_METADATA_CLS(id, cls, fmt); \
  190. if (DYNAMIC_DEBUG_BRANCH(id)) \
  191. func(&id, ##__VA_ARGS__); \
  192. } while (0)
  193. #define __dynamic_func_call(id, fmt, func, ...) \
  194. __dynamic_func_call_cls(id, _DPRINTK_CLASS_DFLT, fmt, \
  195. func, ##__VA_ARGS__)
  196. #define __dynamic_func_call_cls_no_desc(id, cls, fmt, func, ...) do { \
  197. DEFINE_DYNAMIC_DEBUG_METADATA_CLS(id, cls, fmt); \
  198. if (DYNAMIC_DEBUG_BRANCH(id)) \
  199. func(__VA_ARGS__); \
  200. } while (0)
  201. #define __dynamic_func_call_no_desc(id, fmt, func, ...) \
  202. __dynamic_func_call_cls_no_desc(id, _DPRINTK_CLASS_DFLT, \
  203. fmt, func, ##__VA_ARGS__)
  204. /*
  205. * "Factory macro" for generating a call to func, guarded by a
  206. * DYNAMIC_DEBUG_BRANCH. The dynamic debug descriptor will be
  207. * initialized using the fmt argument. The function will be called with
  208. * the address of the descriptor as first argument, followed by all
  209. * the varargs. Note that fmt is repeated in invocations of this
  210. * macro.
  211. */
  212. #define _dynamic_func_call_cls(cls, fmt, func, ...) \
  213. __dynamic_func_call_cls(__UNIQUE_ID(ddebug), cls, fmt, func, ##__VA_ARGS__)
  214. #define _dynamic_func_call(fmt, func, ...) \
  215. _dynamic_func_call_cls(_DPRINTK_CLASS_DFLT, fmt, func, ##__VA_ARGS__)
  216. /*
  217. * A variant that does the same, except that the descriptor is not
  218. * passed as the first argument to the function; it is only called
  219. * with precisely the macro's varargs.
  220. */
  221. #define _dynamic_func_call_cls_no_desc(cls, fmt, func, ...) \
  222. __dynamic_func_call_cls_no_desc(__UNIQUE_ID(ddebug), cls, fmt, \
  223. func, ##__VA_ARGS__)
  224. #define _dynamic_func_call_no_desc(fmt, func, ...) \
  225. _dynamic_func_call_cls_no_desc(_DPRINTK_CLASS_DFLT, fmt, \
  226. func, ##__VA_ARGS__)
  227. #define dynamic_pr_debug_cls(cls, fmt, ...) \
  228. _dynamic_func_call_cls(cls, fmt, __dynamic_pr_debug, \
  229. pr_fmt(fmt), ##__VA_ARGS__)
  230. #define dynamic_pr_debug(fmt, ...) \
  231. _dynamic_func_call(fmt, __dynamic_pr_debug, \
  232. pr_fmt(fmt), ##__VA_ARGS__)
  233. #define dynamic_dev_dbg(dev, fmt, ...) \
  234. _dynamic_func_call(fmt, __dynamic_dev_dbg, \
  235. dev, fmt, ##__VA_ARGS__)
  236. #define dynamic_netdev_dbg(dev, fmt, ...) \
  237. _dynamic_func_call(fmt, __dynamic_netdev_dbg, \
  238. dev, fmt, ##__VA_ARGS__)
  239. #define dynamic_ibdev_dbg(dev, fmt, ...) \
  240. _dynamic_func_call(fmt, __dynamic_ibdev_dbg, \
  241. dev, fmt, ##__VA_ARGS__)
  242. #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
  243. groupsize, buf, len, ascii) \
  244. _dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \
  245. print_hex_dump, \
  246. KERN_DEBUG, prefix_str, prefix_type, \
  247. rowsize, groupsize, buf, len, ascii)
  248. struct kernel_param;
  249. int param_set_dyndbg_classes(const char *instr, const struct kernel_param *kp);
  250. int param_get_dyndbg_classes(char *buffer, const struct kernel_param *kp);
  251. /* for test only, generally expect drm.debug style macro wrappers */
  252. #define __pr_debug_cls(cls, fmt, ...) do { \
  253. BUILD_BUG_ON_MSG(!__builtin_constant_p(cls), \
  254. "expecting constant class int/enum"); \
  255. dynamic_pr_debug_cls(cls, fmt, ##__VA_ARGS__); \
  256. } while (0)
  257. #else /* !CONFIG_DYNAMIC_DEBUG_CORE */
  258. #include <linux/string.h>
  259. #include <linux/errno.h>
  260. #include <linux/printk.h>
  261. static inline int ddebug_add_module(struct _ddebug_info *dinfo, const char *modname)
  262. {
  263. return 0;
  264. }
  265. static inline int ddebug_remove_module(const char *mod)
  266. {
  267. return 0;
  268. }
  269. static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
  270. const char *modname)
  271. {
  272. if (!strcmp(param, "dyndbg")) {
  273. /* avoid pr_warn(), which wants pr_fmt() fully defined */
  274. printk(KERN_WARNING "dyndbg param is supported only in "
  275. "CONFIG_DYNAMIC_DEBUG builds\n");
  276. return 0; /* allow and ignore */
  277. }
  278. return -EINVAL;
  279. }
  280. #define dynamic_pr_debug(fmt, ...) \
  281. do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
  282. #define dynamic_dev_dbg(dev, fmt, ...) \
  283. do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)
  284. #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
  285. groupsize, buf, len, ascii) \
  286. do { if (0) \
  287. print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, \
  288. rowsize, groupsize, buf, len, ascii); \
  289. } while (0)
  290. struct kernel_param;
  291. static inline int param_set_dyndbg_classes(const char *instr, const struct kernel_param *kp)
  292. { return 0; }
  293. static inline int param_get_dyndbg_classes(char *buffer, const struct kernel_param *kp)
  294. { return 0; }
  295. #endif /* !CONFIG_DYNAMIC_DEBUG_CORE */
  296. extern const struct kernel_param_ops param_ops_dyndbg_classes;
  297. #endif