blob: 546d68057e3b7716c55f3c9d097a854ac8646aee [file] [log] [blame]
Jason Barone9d376f2009-02-05 11:51:38 -05001#ifndef _DYNAMIC_DEBUG_H
2#define _DYNAMIC_DEBUG_H
3
Jason Baron9049fc72016-08-03 13:46:39 -07004#if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_JUMP_LABEL)
5#include <linux/jump_label.h>
6#endif
7
Jason Barone9d376f2009-02-05 11:51:38 -05008/*
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 */
13struct _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;
Jim Cromiee703ddae2011-12-19 17:12:59 -050022 unsigned int lineno:18;
Jason Barone9d376f2009-02-05 11:51:38 -050023 /*
Jim Cromie3faa2862012-04-27 14:30:33 -060024 * The flags field controls the behaviour at the callsite.
25 * The bits here are changed dynamically when the user
Florian Ragwitz2b2f68b2010-05-24 14:33:21 -070026 * writes commands to <debugfs>/dynamic_debug/control
Jason Barone9d376f2009-02-05 11:51:38 -050027 */
Jim Cromie5ca7d2a2011-12-19 17:12:44 -050028#define _DPRINTK_FLAGS_NONE 0
29#define _DPRINTK_FLAGS_PRINT (1<<0) /* printk() a message using the format */
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +010030#define _DPRINTK_FLAGS_INCL_MODNAME (1<<1)
31#define _DPRINTK_FLAGS_INCL_FUNCNAME (1<<2)
32#define _DPRINTK_FLAGS_INCL_LINENO (1<<3)
33#define _DPRINTK_FLAGS_INCL_TID (1<<4)
Jim Cromieb558c962011-12-19 17:11:18 -050034#if defined DEBUG
35#define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
36#else
Jason Barone9d376f2009-02-05 11:51:38 -050037#define _DPRINTK_FLAGS_DEFAULT 0
Jim Cromieb558c962011-12-19 17:11:18 -050038#endif
Jason Barone9d376f2009-02-05 11:51:38 -050039 unsigned int flags:8;
Jason Baron9049fc72016-08-03 13:46:39 -070040#ifdef HAVE_JUMP_LABEL
41 union {
42 struct static_key_true dd_key_true;
43 struct static_key_false dd_key_false;
44 } key;
45#endif
Jason Barone9d376f2009-02-05 11:51:38 -050046} __attribute__((aligned(8)));
47
48
49int ddebug_add_module(struct _ddebug *tab, unsigned int n,
50 const char *modname);
51
52#if defined(CONFIG_DYNAMIC_DEBUG)
Yehuda Sadehff49d742010-07-03 13:07:35 +100053extern int ddebug_remove_module(const char *mod_name);
Joe Perchesb9075fa2011-10-31 17:11:33 -070054extern __printf(2, 3)
Joe Perches906d2012014-09-24 11:17:56 -070055void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...);
Jason Barone9d376f2009-02-05 11:51:38 -050056
Jim Cromieb48420c2012-04-27 14:30:35 -060057extern int ddebug_dyndbg_module_param_cb(char *param, char *val,
58 const char *modname);
59
Joe Perchescbc46632011-08-11 14:36:21 -040060struct device;
61
Joe Perchesb9075fa2011-10-31 17:11:33 -070062extern __printf(3, 4)
Joe Perches906d2012014-09-24 11:17:56 -070063void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev,
64 const char *fmt, ...);
Joe Perchescbc46632011-08-11 14:36:21 -040065
Jason Baronffa10cb2011-08-11 14:36:48 -040066struct net_device;
67
Joe Perchesb9075fa2011-10-31 17:11:33 -070068extern __printf(3, 4)
Joe Perches906d2012014-09-24 11:17:56 -070069void __dynamic_netdev_dbg(struct _ddebug *descriptor,
70 const struct net_device *dev,
71 const char *fmt, ...);
Jason Baronffa10cb2011-08-11 14:36:48 -040072
Jason Baron9049fc72016-08-03 13:46:39 -070073#define DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, key, init) \
Joe Perchesc0d2af62012-10-18 12:07:03 -070074 static struct _ddebug __aligned(8) \
Jason Baron07613b02011-10-04 14:13:15 -070075 __attribute__((section("__verbose"))) name = { \
76 .modname = KBUILD_MODNAME, \
77 .function = __func__, \
78 .filename = __FILE__, \
79 .format = (fmt), \
80 .lineno = __LINE__, \
Jason Baron9049fc72016-08-03 13:46:39 -070081 .flags = _DPRINTK_FLAGS_DEFAULT, \
82 dd_key_init(key, init) \
Jason Baron07613b02011-10-04 14:13:15 -070083 }
Jason Barone9d376f2009-02-05 11:51:38 -050084
Jason Baron9049fc72016-08-03 13:46:39 -070085#ifdef HAVE_JUMP_LABEL
86
87#define dd_key_init(key, init) key = (init)
88
89#ifdef DEBUG
90#define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
91 DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, .key.dd_key_true, \
92 (STATIC_KEY_TRUE_INIT))
93
94#define DYNAMIC_DEBUG_BRANCH(descriptor) \
95 static_branch_likely(&descriptor.key.dd_key_true)
96#else
97#define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
98 DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, .key.dd_key_false, \
99 (STATIC_KEY_FALSE_INIT))
100
101#define DYNAMIC_DEBUG_BRANCH(descriptor) \
102 static_branch_unlikely(&descriptor.key.dd_key_false)
103#endif
104
105#else
106
107#define dd_key_init(key, init)
108
109#define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
110 DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, 0, 0)
111
112#ifdef DEBUG
113#define DYNAMIC_DEBUG_BRANCH(descriptor) \
114 likely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
115#else
116#define DYNAMIC_DEBUG_BRANCH(descriptor) \
117 unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
118#endif
119
120#endif
121
Jason Baron07613b02011-10-04 14:13:15 -0700122#define dynamic_pr_debug(fmt, ...) \
123do { \
124 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
Jason Baron9049fc72016-08-03 13:46:39 -0700125 if (DYNAMIC_DEBUG_BRANCH(descriptor)) \
Jason Baron07613b02011-10-04 14:13:15 -0700126 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \
127 ##__VA_ARGS__); \
128} while (0)
Jason Barone9d376f2009-02-05 11:51:38 -0500129
Jason Baron07613b02011-10-04 14:13:15 -0700130#define dynamic_dev_dbg(dev, fmt, ...) \
131do { \
Jim Cromie87e6f962011-12-19 17:11:13 -0500132 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
Jason Baron9049fc72016-08-03 13:46:39 -0700133 if (DYNAMIC_DEBUG_BRANCH(descriptor)) \
Jason Baron07613b02011-10-04 14:13:15 -0700134 __dynamic_dev_dbg(&descriptor, dev, fmt, \
135 ##__VA_ARGS__); \
136} while (0)
137
138#define dynamic_netdev_dbg(dev, fmt, ...) \
139do { \
140 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
Jason Baron9049fc72016-08-03 13:46:39 -0700141 if (DYNAMIC_DEBUG_BRANCH(descriptor)) \
Jason Baron07613b02011-10-04 14:13:15 -0700142 __dynamic_netdev_dbg(&descriptor, dev, fmt, \
143 ##__VA_ARGS__); \
144} while (0)
Jason Baronffa10cb2011-08-11 14:36:48 -0400145
Vladimir Kondratiev7a555612012-12-05 16:48:27 -0500146#define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
147 groupsize, buf, len, ascii) \
148do { \
149 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, \
150 __builtin_constant_p(prefix_str) ? prefix_str : "hexdump");\
Jason Baron9049fc72016-08-03 13:46:39 -0700151 if (DYNAMIC_DEBUG_BRANCH(descriptor)) \
Vladimir Kondratiev7a555612012-12-05 16:48:27 -0500152 print_hex_dump(KERN_DEBUG, prefix_str, \
153 prefix_type, rowsize, groupsize, \
154 buf, len, ascii); \
155} while (0)
156
Jason Barone9d376f2009-02-05 11:51:38 -0500157#else
158
Jim Cromieb48420c2012-04-27 14:30:35 -0600159#include <linux/string.h>
160#include <linux/errno.h>
161
Yehuda Sadehff49d742010-07-03 13:07:35 +1000162static inline int ddebug_remove_module(const char *mod)
Jason Barone9d376f2009-02-05 11:51:38 -0500163{
164 return 0;
165}
166
Jim Cromieb48420c2012-04-27 14:30:35 -0600167static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
168 const char *modname)
169{
170 if (strstr(param, "dyndbg")) {
Jim Cromie516cf1b2012-05-01 05:23:12 -0600171 /* avoid pr_warn(), which wants pr_fmt() fully defined */
172 printk(KERN_WARNING "dyndbg param is supported only in "
Jim Cromieb48420c2012-04-27 14:30:35 -0600173 "CONFIG_DYNAMIC_DEBUG builds\n");
174 return 0; /* allow and ignore */
175 }
176 return -EINVAL;
177}
178
Joe Perches00b55862009-12-14 18:00:14 -0800179#define dynamic_pr_debug(fmt, ...) \
180 do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
Philipp Reisnerbe70e262010-10-14 11:58:20 +0200181#define dynamic_dev_dbg(dev, fmt, ...) \
Joe Perches00b55862009-12-14 18:00:14 -0800182 do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)
Jason Barone9d376f2009-02-05 11:51:38 -0500183#endif
184
185#endif