dynamic_debug.h 3.9 KB

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