blob: 8d9eec5f6d8bb38837ea592d470016f6efb10645 [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 Cromie640d1ea2021-05-04 16:22:34 -060035
36#define _DPRINTK_FLAGS_INCL_ANY \
37 (_DPRINTK_FLAGS_INCL_MODNAME | _DPRINTK_FLAGS_INCL_FUNCNAME |\
38 _DPRINTK_FLAGS_INCL_LINENO | _DPRINTK_FLAGS_INCL_TID)
39
Jim Cromieb558c962011-12-19 17:11:18 -050040#if defined DEBUG
41#define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
42#else
Jason Barone9d376f2009-02-05 11:51:38 -050043#define _DPRINTK_FLAGS_DEFAULT 0
Jim Cromieb558c962011-12-19 17:11:18 -050044#endif
Jason Barone9d376f2009-02-05 11:51:38 -050045 unsigned int flags:8;
Masahiro Yamadae9666d12018-12-31 00:14:15 +090046#ifdef CONFIG_JUMP_LABEL
Jason Baron9049fc72016-08-03 13:46:39 -070047 union {
48 struct static_key_true dd_key_true;
49 struct static_key_false dd_key_false;
50 } key;
51#endif
Jason Barone9d376f2009-02-05 11:51:38 -050052} __attribute__((aligned(8)));
53
54
Jason Barone9d376f2009-02-05 11:51:38 -050055
Orson Zhaiceabef72020-06-07 21:40:14 -070056#if defined(CONFIG_DYNAMIC_DEBUG_CORE)
Jim Cromiea2d375e2020-08-31 12:22:09 -060057
Rasmus Villemoesa4507fe2019-03-07 16:27:52 -080058int ddebug_add_module(struct _ddebug *tab, unsigned int n,
59 const char *modname);
Yehuda Sadehff49d742010-07-03 13:07:35 +100060extern int ddebug_remove_module(const char *mod_name);
Joe Perchesb9075fa2011-10-31 17:11:33 -070061extern __printf(2, 3)
Joe Perches906d2012014-09-24 11:17:56 -070062void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...);
Jason Barone9d376f2009-02-05 11:51:38 -050063
Jim Cromieb48420c2012-04-27 14:30:35 -060064extern int ddebug_dyndbg_module_param_cb(char *param, char *val,
65 const char *modname);
66
Joe Perchescbc46632011-08-11 14:36:21 -040067struct device;
68
Joe Perchesb9075fa2011-10-31 17:11:33 -070069extern __printf(3, 4)
Joe Perches906d2012014-09-24 11:17:56 -070070void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev,
71 const char *fmt, ...);
Joe Perchescbc46632011-08-11 14:36:21 -040072
Jason Baronffa10cb2011-08-11 14:36:48 -040073struct net_device;
74
Joe Perchesb9075fa2011-10-31 17:11:33 -070075extern __printf(3, 4)
Joe Perches906d2012014-09-24 11:17:56 -070076void __dynamic_netdev_dbg(struct _ddebug *descriptor,
77 const struct net_device *dev,
78 const char *fmt, ...);
Jason Baronffa10cb2011-08-11 14:36:48 -040079
Gal Pressman923abb92019-05-01 13:48:13 +030080struct ib_device;
81
82extern __printf(3, 4)
83void __dynamic_ibdev_dbg(struct _ddebug *descriptor,
84 const struct ib_device *ibdev,
85 const char *fmt, ...);
86
Rasmus Villemoes2bdde672019-03-07 16:27:33 -080087#define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
Joe Perchesc0d2af62012-10-18 12:07:03 -070088 static struct _ddebug __aligned(8) \
Joe Perches33def842020-10-21 19:36:07 -070089 __section("__dyndbg") name = { \
Jason Baron07613b02011-10-04 14:13:15 -070090 .modname = KBUILD_MODNAME, \
91 .function = __func__, \
92 .filename = __FILE__, \
93 .format = (fmt), \
94 .lineno = __LINE__, \
Jason Baron9049fc72016-08-03 13:46:39 -070095 .flags = _DPRINTK_FLAGS_DEFAULT, \
Rasmus Villemoes2bdde672019-03-07 16:27:33 -080096 _DPRINTK_KEY_INIT \
Jason Baron07613b02011-10-04 14:13:15 -070097 }
Jason Barone9d376f2009-02-05 11:51:38 -050098
Masahiro Yamadae9666d12018-12-31 00:14:15 +090099#ifdef CONFIG_JUMP_LABEL
Jason Baron9049fc72016-08-03 13:46:39 -0700100
Jason Baron9049fc72016-08-03 13:46:39 -0700101#ifdef DEBUG
Rasmus Villemoes2bdde672019-03-07 16:27:33 -0800102
103#define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT)
Jason Baron9049fc72016-08-03 13:46:39 -0700104
105#define DYNAMIC_DEBUG_BRANCH(descriptor) \
106 static_branch_likely(&descriptor.key.dd_key_true)
107#else
Rasmus Villemoes2bdde672019-03-07 16:27:33 -0800108#define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT)
Jason Baron9049fc72016-08-03 13:46:39 -0700109
110#define DYNAMIC_DEBUG_BRANCH(descriptor) \
111 static_branch_unlikely(&descriptor.key.dd_key_false)
112#endif
113
Jim Cromiea2d375e2020-08-31 12:22:09 -0600114#else /* !CONFIG_JUMP_LABEL */
Jason Baron9049fc72016-08-03 13:46:39 -0700115
Rasmus Villemoes2bdde672019-03-07 16:27:33 -0800116#define _DPRINTK_KEY_INIT
Jason Baron9049fc72016-08-03 13:46:39 -0700117
118#ifdef DEBUG
119#define DYNAMIC_DEBUG_BRANCH(descriptor) \
120 likely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
121#else
122#define DYNAMIC_DEBUG_BRANCH(descriptor) \
123 unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
124#endif
125
Jim Cromiea2d375e2020-08-31 12:22:09 -0600126#endif /* CONFIG_JUMP_LABEL */
Jason Baron9049fc72016-08-03 13:46:39 -0700127
Rasmus Villemoes47cdd642019-03-07 16:27:56 -0800128#define __dynamic_func_call(id, fmt, func, ...) do { \
129 DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \
130 if (DYNAMIC_DEBUG_BRANCH(id)) \
131 func(&id, ##__VA_ARGS__); \
Jason Baron07613b02011-10-04 14:13:15 -0700132} while (0)
Jason Barone9d376f2009-02-05 11:51:38 -0500133
Rasmus Villemoes47cdd642019-03-07 16:27:56 -0800134#define __dynamic_func_call_no_desc(id, fmt, func, ...) do { \
135 DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \
136 if (DYNAMIC_DEBUG_BRANCH(id)) \
137 func(__VA_ARGS__); \
138} while (0)
139
140/*
141 * "Factory macro" for generating a call to func, guarded by a
Jim Cromiee5ebffe2020-07-19 17:10:45 -0600142 * DYNAMIC_DEBUG_BRANCH. The dynamic debug descriptor will be
Rasmus Villemoes47cdd642019-03-07 16:27:56 -0800143 * initialized using the fmt argument. The function will be called with
144 * the address of the descriptor as first argument, followed by all
145 * the varargs. Note that fmt is repeated in invocations of this
146 * macro.
147 */
148#define _dynamic_func_call(fmt, func, ...) \
149 __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
150/*
151 * A variant that does the same, except that the descriptor is not
152 * passed as the first argument to the function; it is only called
153 * with precisely the macro's varargs.
154 */
155#define _dynamic_func_call_no_desc(fmt, func, ...) \
156 __dynamic_func_call_no_desc(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
157
158#define dynamic_pr_debug(fmt, ...) \
159 _dynamic_func_call(fmt, __dynamic_pr_debug, \
160 pr_fmt(fmt), ##__VA_ARGS__)
161
Jason Baron07613b02011-10-04 14:13:15 -0700162#define dynamic_dev_dbg(dev, fmt, ...) \
Rasmus Villemoes47cdd642019-03-07 16:27:56 -0800163 _dynamic_func_call(fmt,__dynamic_dev_dbg, \
164 dev, fmt, ##__VA_ARGS__)
Jason Baron07613b02011-10-04 14:13:15 -0700165
166#define dynamic_netdev_dbg(dev, fmt, ...) \
Rasmus Villemoes47cdd642019-03-07 16:27:56 -0800167 _dynamic_func_call(fmt, __dynamic_netdev_dbg, \
168 dev, fmt, ##__VA_ARGS__)
Jason Baronffa10cb2011-08-11 14:36:48 -0400169
Gal Pressman923abb92019-05-01 13:48:13 +0300170#define dynamic_ibdev_dbg(dev, fmt, ...) \
171 _dynamic_func_call(fmt, __dynamic_ibdev_dbg, \
172 dev, fmt, ##__VA_ARGS__)
173
Rasmus Villemoes47cdd642019-03-07 16:27:56 -0800174#define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
175 groupsize, buf, len, ascii) \
176 _dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \
177 print_hex_dump, \
178 KERN_DEBUG, prefix_str, prefix_type, \
179 rowsize, groupsize, buf, len, ascii)
Vladimir Kondratiev7a555612012-12-05 16:48:27 -0500180
Jim Cromiea2d375e2020-08-31 12:22:09 -0600181#else /* !CONFIG_DYNAMIC_DEBUG_CORE */
Jason Barone9d376f2009-02-05 11:51:38 -0500182
Jim Cromieb48420c2012-04-27 14:30:35 -0600183#include <linux/string.h>
184#include <linux/errno.h>
Jim Cromiea2d375e2020-08-31 12:22:09 -0600185#include <linux/printk.h>
Jim Cromieb48420c2012-04-27 14:30:35 -0600186
Rasmus Villemoesa4507fe2019-03-07 16:27:52 -0800187static inline int ddebug_add_module(struct _ddebug *tab, unsigned int n,
188 const char *modname)
189{
190 return 0;
191}
192
Yehuda Sadehff49d742010-07-03 13:07:35 +1000193static inline int ddebug_remove_module(const char *mod)
Jason Barone9d376f2009-02-05 11:51:38 -0500194{
195 return 0;
196}
197
Jim Cromieb48420c2012-04-27 14:30:35 -0600198static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
199 const char *modname)
200{
Jim Cromie0c0d9f32022-09-04 15:40:39 -0600201 if (!strcmp(param, "dyndbg")) {
Jim Cromie516cf1b2012-05-01 05:23:12 -0600202 /* avoid pr_warn(), which wants pr_fmt() fully defined */
203 printk(KERN_WARNING "dyndbg param is supported only in "
Jim Cromieb48420c2012-04-27 14:30:35 -0600204 "CONFIG_DYNAMIC_DEBUG builds\n");
205 return 0; /* allow and ignore */
206 }
207 return -EINVAL;
208}
209
Joe Perches00b558642009-12-14 18:00:14 -0800210#define dynamic_pr_debug(fmt, ...) \
211 do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
Philipp Reisnerbe70e262010-10-14 11:58:20 +0200212#define dynamic_dev_dbg(dev, fmt, ...) \
Joe Perches00b558642009-12-14 18:00:14 -0800213 do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)
Arnd Bergmann011c7282019-09-18 21:55:11 +0200214#define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
215 groupsize, buf, len, ascii) \
216 do { if (0) \
217 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, \
218 rowsize, groupsize, buf, len, ascii); \
219 } while (0)
Jim Cromiea2d375e2020-08-31 12:22:09 -0600220
Jim Cromiea2d375e2020-08-31 12:22:09 -0600221#endif /* !CONFIG_DYNAMIC_DEBUG_CORE */
Jason Barone9d376f2009-02-05 11:51:38 -0500222
223#endif