blob: c2be029b9b539f28b6b2c4fe63941701504543c2 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Jason Barone9d376f2009-02-05 11:51:38 -05002#ifndef _DYNAMIC_DEBUG_H
3#define _DYNAMIC_DEBUG_H
4
Masahiro Yamadae9666d12018-12-31 00:14:15 +09005#if defined(CONFIG_JUMP_LABEL)
Jason Baron9049fc72016-08-03 13:46:39 -07006#include <linux/jump_label.h>
7#endif
8
Jason Barone9d376f2009-02-05 11:51:38 -05009/*
10 * An instance of this structure is created in a special
11 * ELF section at every dynamic debug callsite. At runtime,
12 * the special section is treated as an array of these.
13 */
14struct _ddebug {
15 /*
16 * These fields are used to drive the user interface
17 * for selecting and displaying debug callsites.
18 */
19 const char *modname;
20 const char *function;
21 const char *filename;
22 const char *format;
Jim Cromiee703ddae2011-12-19 17:12:59 -050023 unsigned int lineno:18;
Jason Barone9d376f2009-02-05 11:51:38 -050024 /*
Jim Cromie3faa2862012-04-27 14:30:33 -060025 * The flags field controls the behaviour at the callsite.
26 * The bits here are changed dynamically when the user
Florian Ragwitz2b2f68b2010-05-24 14:33:21 -070027 * writes commands to <debugfs>/dynamic_debug/control
Jason Barone9d376f2009-02-05 11:51:38 -050028 */
Jim Cromie5ca7d2a2011-12-19 17:12:44 -050029#define _DPRINTK_FLAGS_NONE 0
30#define _DPRINTK_FLAGS_PRINT (1<<0) /* printk() a message using the format */
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +010031#define _DPRINTK_FLAGS_INCL_MODNAME (1<<1)
32#define _DPRINTK_FLAGS_INCL_FUNCNAME (1<<2)
33#define _DPRINTK_FLAGS_INCL_LINENO (1<<3)
34#define _DPRINTK_FLAGS_INCL_TID (1<<4)
Jim Cromieb558c962011-12-19 17:11:18 -050035#if defined DEBUG
36#define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
37#else
Jason Barone9d376f2009-02-05 11:51:38 -050038#define _DPRINTK_FLAGS_DEFAULT 0
Jim Cromieb558c962011-12-19 17:11:18 -050039#endif
Jason Barone9d376f2009-02-05 11:51:38 -050040 unsigned int flags:8;
Masahiro Yamadae9666d12018-12-31 00:14:15 +090041#ifdef CONFIG_JUMP_LABEL
Jason Baron9049fc72016-08-03 13:46:39 -070042 union {
43 struct static_key_true dd_key_true;
44 struct static_key_false dd_key_false;
45 } key;
46#endif
Jason Barone9d376f2009-02-05 11:51:38 -050047} __attribute__((aligned(8)));
48
49
Jason Barone9d376f2009-02-05 11:51:38 -050050
51#if defined(CONFIG_DYNAMIC_DEBUG)
Rasmus Villemoesa4507fe2019-03-07 16:27:52 -080052int ddebug_add_module(struct _ddebug *tab, unsigned int n,
53 const char *modname);
Yehuda Sadehff49d742010-07-03 13:07:35 +100054extern int ddebug_remove_module(const char *mod_name);
Joe Perchesb9075fa2011-10-31 17:11:33 -070055extern __printf(2, 3)
Joe Perches906d2012014-09-24 11:17:56 -070056void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...);
Jason Barone9d376f2009-02-05 11:51:38 -050057
Jim Cromieb48420c2012-04-27 14:30:35 -060058extern int ddebug_dyndbg_module_param_cb(char *param, char *val,
59 const char *modname);
60
Joe Perchescbc46632011-08-11 14:36:21 -040061struct device;
62
Joe Perchesb9075fa2011-10-31 17:11:33 -070063extern __printf(3, 4)
Joe Perches906d2012014-09-24 11:17:56 -070064void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev,
65 const char *fmt, ...);
Joe Perchescbc46632011-08-11 14:36:21 -040066
Jason Baronffa10cb2011-08-11 14:36:48 -040067struct net_device;
68
Joe Perchesb9075fa2011-10-31 17:11:33 -070069extern __printf(3, 4)
Joe Perches906d2012014-09-24 11:17:56 -070070void __dynamic_netdev_dbg(struct _ddebug *descriptor,
71 const struct net_device *dev,
72 const char *fmt, ...);
Jason Baronffa10cb2011-08-11 14:36:48 -040073
Rasmus Villemoes2bdde672019-03-07 16:27:33 -080074#define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
Joe Perchesc0d2af62012-10-18 12:07:03 -070075 static struct _ddebug __aligned(8) \
Jason Baron07613b02011-10-04 14:13:15 -070076 __attribute__((section("__verbose"))) name = { \
77 .modname = KBUILD_MODNAME, \
78 .function = __func__, \
79 .filename = __FILE__, \
80 .format = (fmt), \
81 .lineno = __LINE__, \
Jason Baron9049fc72016-08-03 13:46:39 -070082 .flags = _DPRINTK_FLAGS_DEFAULT, \
Rasmus Villemoes2bdde672019-03-07 16:27:33 -080083 _DPRINTK_KEY_INIT \
Jason Baron07613b02011-10-04 14:13:15 -070084 }
Jason Barone9d376f2009-02-05 11:51:38 -050085
Masahiro Yamadae9666d12018-12-31 00:14:15 +090086#ifdef CONFIG_JUMP_LABEL
Jason Baron9049fc72016-08-03 13:46:39 -070087
Jason Baron9049fc72016-08-03 13:46:39 -070088#ifdef DEBUG
Rasmus Villemoes2bdde672019-03-07 16:27:33 -080089
90#define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT)
Jason Baron9049fc72016-08-03 13:46:39 -070091
92#define DYNAMIC_DEBUG_BRANCH(descriptor) \
93 static_branch_likely(&descriptor.key.dd_key_true)
94#else
Rasmus Villemoes2bdde672019-03-07 16:27:33 -080095#define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT)
Jason Baron9049fc72016-08-03 13:46:39 -070096
97#define DYNAMIC_DEBUG_BRANCH(descriptor) \
98 static_branch_unlikely(&descriptor.key.dd_key_false)
99#endif
100
Rasmus Villemoes2bdde672019-03-07 16:27:33 -0800101#else /* !HAVE_JUMP_LABEL */
Jason Baron9049fc72016-08-03 13:46:39 -0700102
Rasmus Villemoes2bdde672019-03-07 16:27:33 -0800103#define _DPRINTK_KEY_INIT
Jason Baron9049fc72016-08-03 13:46:39 -0700104
105#ifdef DEBUG
106#define DYNAMIC_DEBUG_BRANCH(descriptor) \
107 likely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
108#else
109#define DYNAMIC_DEBUG_BRANCH(descriptor) \
110 unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
111#endif
112
113#endif
114
Rasmus Villemoes47cdd642019-03-07 16:27:56 -0800115#define __dynamic_func_call(id, fmt, func, ...) do { \
116 DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \
117 if (DYNAMIC_DEBUG_BRANCH(id)) \
118 func(&id, ##__VA_ARGS__); \
Jason Baron07613b02011-10-04 14:13:15 -0700119} while (0)
Jason Barone9d376f2009-02-05 11:51:38 -0500120
Rasmus Villemoes47cdd642019-03-07 16:27:56 -0800121#define __dynamic_func_call_no_desc(id, fmt, func, ...) do { \
122 DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \
123 if (DYNAMIC_DEBUG_BRANCH(id)) \
124 func(__VA_ARGS__); \
125} while (0)
126
127/*
128 * "Factory macro" for generating a call to func, guarded by a
129 * DYNAMIC_DEBUG_BRANCH. The dynamic debug decriptor will be
130 * initialized using the fmt argument. The function will be called with
131 * the address of the descriptor as first argument, followed by all
132 * the varargs. Note that fmt is repeated in invocations of this
133 * macro.
134 */
135#define _dynamic_func_call(fmt, func, ...) \
136 __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
137/*
138 * A variant that does the same, except that the descriptor is not
139 * passed as the first argument to the function; it is only called
140 * with precisely the macro's varargs.
141 */
142#define _dynamic_func_call_no_desc(fmt, func, ...) \
143 __dynamic_func_call_no_desc(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
144
145#define dynamic_pr_debug(fmt, ...) \
146 _dynamic_func_call(fmt, __dynamic_pr_debug, \
147 pr_fmt(fmt), ##__VA_ARGS__)
148
Jason Baron07613b02011-10-04 14:13:15 -0700149#define dynamic_dev_dbg(dev, fmt, ...) \
Rasmus Villemoes47cdd642019-03-07 16:27:56 -0800150 _dynamic_func_call(fmt,__dynamic_dev_dbg, \
151 dev, fmt, ##__VA_ARGS__)
Jason Baron07613b02011-10-04 14:13:15 -0700152
153#define dynamic_netdev_dbg(dev, fmt, ...) \
Rasmus Villemoes47cdd642019-03-07 16:27:56 -0800154 _dynamic_func_call(fmt, __dynamic_netdev_dbg, \
155 dev, fmt, ##__VA_ARGS__)
Jason Baronffa10cb2011-08-11 14:36:48 -0400156
Rasmus Villemoes47cdd642019-03-07 16:27:56 -0800157#define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
158 groupsize, buf, len, ascii) \
159 _dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \
160 print_hex_dump, \
161 KERN_DEBUG, prefix_str, prefix_type, \
162 rowsize, groupsize, buf, len, ascii)
Vladimir Kondratiev7a555612012-12-05 16:48:27 -0500163
Jason Barone9d376f2009-02-05 11:51:38 -0500164#else
165
Jim Cromieb48420c2012-04-27 14:30:35 -0600166#include <linux/string.h>
167#include <linux/errno.h>
168
Rasmus Villemoesa4507fe2019-03-07 16:27:52 -0800169static inline int ddebug_add_module(struct _ddebug *tab, unsigned int n,
170 const char *modname)
171{
172 return 0;
173}
174
Yehuda Sadehff49d742010-07-03 13:07:35 +1000175static inline int ddebug_remove_module(const char *mod)
Jason Barone9d376f2009-02-05 11:51:38 -0500176{
177 return 0;
178}
179
Jim Cromieb48420c2012-04-27 14:30:35 -0600180static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
181 const char *modname)
182{
183 if (strstr(param, "dyndbg")) {
Jim Cromie516cf1b2012-05-01 05:23:12 -0600184 /* avoid pr_warn(), which wants pr_fmt() fully defined */
185 printk(KERN_WARNING "dyndbg param is supported only in "
Jim Cromieb48420c2012-04-27 14:30:35 -0600186 "CONFIG_DYNAMIC_DEBUG builds\n");
187 return 0; /* allow and ignore */
188 }
189 return -EINVAL;
190}
191
Joe Perches00b558642009-12-14 18:00:14 -0800192#define dynamic_pr_debug(fmt, ...) \
193 do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
Philipp Reisnerbe70e262010-10-14 11:58:20 +0200194#define dynamic_dev_dbg(dev, fmt, ...) \
Joe Perches00b558642009-12-14 18:00:14 -0800195 do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)
Jason Barone9d376f2009-02-05 11:51:38 -0500196#endif
197
198#endif