dynamic_debug.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. int ddebug_add_module(struct _ddebug *tab, unsigned int n,
  47. const char *modname);
  48. #if defined(CONFIG_DYNAMIC_DEBUG)
  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. #define DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, key, init) \
  64. static struct _ddebug __aligned(8) \
  65. __attribute__((section("__verbose"))) name = { \
  66. .modname = KBUILD_MODNAME, \
  67. .function = __func__, \
  68. .filename = __FILE__, \
  69. .format = (fmt), \
  70. .lineno = __LINE__, \
  71. .flags = _DPRINTK_FLAGS_DEFAULT, \
  72. dd_key_init(key, init) \
  73. }
  74. #ifdef CONFIG_JUMP_LABEL
  75. #define dd_key_init(key, init) key = (init)
  76. #ifdef DEBUG
  77. #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
  78. DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, .key.dd_key_true, \
  79. (STATIC_KEY_TRUE_INIT))
  80. #define DYNAMIC_DEBUG_BRANCH(descriptor) \
  81. static_branch_likely(&descriptor.key.dd_key_true)
  82. #else
  83. #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
  84. DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, .key.dd_key_false, \
  85. (STATIC_KEY_FALSE_INIT))
  86. #define DYNAMIC_DEBUG_BRANCH(descriptor) \
  87. static_branch_unlikely(&descriptor.key.dd_key_false)
  88. #endif
  89. #else
  90. #define dd_key_init(key, init)
  91. #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
  92. DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, 0, 0)
  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
  101. #define dynamic_pr_debug(fmt, ...) \
  102. do { \
  103. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
  104. if (DYNAMIC_DEBUG_BRANCH(descriptor)) \
  105. __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \
  106. ##__VA_ARGS__); \
  107. } while (0)
  108. #define dynamic_dev_dbg(dev, fmt, ...) \
  109. do { \
  110. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
  111. if (DYNAMIC_DEBUG_BRANCH(descriptor)) \
  112. __dynamic_dev_dbg(&descriptor, dev, fmt, \
  113. ##__VA_ARGS__); \
  114. } while (0)
  115. #define dynamic_netdev_dbg(dev, fmt, ...) \
  116. do { \
  117. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
  118. if (DYNAMIC_DEBUG_BRANCH(descriptor)) \
  119. __dynamic_netdev_dbg(&descriptor, dev, fmt, \
  120. ##__VA_ARGS__); \
  121. } while (0)
  122. #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
  123. groupsize, buf, len, ascii) \
  124. do { \
  125. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, \
  126. __builtin_constant_p(prefix_str) ? prefix_str : "hexdump");\
  127. if (DYNAMIC_DEBUG_BRANCH(descriptor)) \
  128. print_hex_dump(KERN_DEBUG, prefix_str, \
  129. prefix_type, rowsize, groupsize, \
  130. buf, len, ascii); \
  131. } while (0)
  132. #else
  133. #include <linux/string.h>
  134. #include <linux/errno.h>
  135. static inline int ddebug_remove_module(const char *mod)
  136. {
  137. return 0;
  138. }
  139. static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
  140. const char *modname)
  141. {
  142. if (strstr(param, "dyndbg")) {
  143. /* avoid pr_warn(), which wants pr_fmt() fully defined */
  144. printk(KERN_WARNING "dyndbg param is supported only in "
  145. "CONFIG_DYNAMIC_DEBUG builds\n");
  146. return 0; /* allow and ignore */
  147. }
  148. return -EINVAL;
  149. }
  150. #define dynamic_pr_debug(fmt, ...) \
  151. do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
  152. #define dynamic_dev_dbg(dev, fmt, ...) \
  153. do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)
  154. #endif
  155. #endif