]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/dynamic_debug.h
Merge tag 'fs.move_mount.move_mount_set_group.v5.15' of git://git.kernel.org/pub...
[mirror_ubuntu-jammy-kernel.git] / include / linux / dynamic_debug.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
e9d376f0
JB
2#ifndef _DYNAMIC_DEBUG_H
3#define _DYNAMIC_DEBUG_H
4
e9666d10 5#if defined(CONFIG_JUMP_LABEL)
9049fc74
JB
6#include <linux/jump_label.h>
7#endif
8
e9d376f0
JB
9/*
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;
e703ddae 23 unsigned int lineno:18;
e9d376f0 24 /*
3faa2860
JC
25 * The flags field controls the behaviour at the callsite.
26 * The bits here are changed dynamically when the user
2b2f68b5 27 * writes commands to <debugfs>/dynamic_debug/control
e9d376f0 28 */
5ca7d2a6
JC
29#define _DPRINTK_FLAGS_NONE 0
30#define _DPRINTK_FLAGS_PRINT (1<<0) /* printk() a message using the format */
8ba6ebf5
BVA
31#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)
640d1eaf
JC
35
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
b558c96f
JC
40#if defined DEBUG
41#define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
42#else
e9d376f0 43#define _DPRINTK_FLAGS_DEFAULT 0
b558c96f 44#endif
e9d376f0 45 unsigned int flags:8;
e9666d10 46#ifdef CONFIG_JUMP_LABEL
9049fc74
JB
47 union {
48 struct static_key_true dd_key_true;
49 struct static_key_false dd_key_false;
50 } key;
51#endif
e9d376f0
JB
52} __attribute__((aligned(8)));
53
54
e9d376f0 55
ceabef7d 56#if defined(CONFIG_DYNAMIC_DEBUG_CORE)
a2d375ed
JC
57
58/* exported for module authors to exercise >control */
59int dynamic_debug_exec_queries(const char *query, const char *modname);
60
a4507fed
RV
61int ddebug_add_module(struct _ddebug *tab, unsigned int n,
62 const char *modname);
ff49d74a 63extern int ddebug_remove_module(const char *mod_name);
b9075fa9 64extern __printf(2, 3)
906d2015 65void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...);
e9d376f0 66
b48420c1
JC
67extern int ddebug_dyndbg_module_param_cb(char *param, char *val,
68 const char *modname);
69
cbc46635
JP
70struct device;
71
b9075fa9 72extern __printf(3, 4)
906d2015
JP
73void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev,
74 const char *fmt, ...);
cbc46635 75
ffa10cb4
JB
76struct net_device;
77
b9075fa9 78extern __printf(3, 4)
906d2015
JP
79void __dynamic_netdev_dbg(struct _ddebug *descriptor,
80 const struct net_device *dev,
81 const char *fmt, ...);
ffa10cb4 82
923abb9d
GP
83struct ib_device;
84
85extern __printf(3, 4)
86void __dynamic_ibdev_dbg(struct _ddebug *descriptor,
87 const struct ib_device *ibdev,
88 const char *fmt, ...);
89
2bdde670 90#define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
c0d2af63 91 static struct _ddebug __aligned(8) \
33def849 92 __section("__dyndbg") name = { \
07613b0b
JB
93 .modname = KBUILD_MODNAME, \
94 .function = __func__, \
95 .filename = __FILE__, \
96 .format = (fmt), \
97 .lineno = __LINE__, \
9049fc74 98 .flags = _DPRINTK_FLAGS_DEFAULT, \
2bdde670 99 _DPRINTK_KEY_INIT \
07613b0b
JB
100 }
101
e9666d10 102#ifdef CONFIG_JUMP_LABEL
9049fc74 103
9049fc74 104#ifdef DEBUG
2bdde670
RV
105
106#define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT)
9049fc74
JB
107
108#define DYNAMIC_DEBUG_BRANCH(descriptor) \
109 static_branch_likely(&descriptor.key.dd_key_true)
110#else
2bdde670 111#define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT)
9049fc74
JB
112
113#define DYNAMIC_DEBUG_BRANCH(descriptor) \
114 static_branch_unlikely(&descriptor.key.dd_key_false)
115#endif
116
a2d375ed 117#else /* !CONFIG_JUMP_LABEL */
9049fc74 118
2bdde670 119#define _DPRINTK_KEY_INIT
9049fc74
JB
120
121#ifdef DEBUG
122#define DYNAMIC_DEBUG_BRANCH(descriptor) \
123 likely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
124#else
125#define DYNAMIC_DEBUG_BRANCH(descriptor) \
126 unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
127#endif
128
a2d375ed 129#endif /* CONFIG_JUMP_LABEL */
9049fc74 130
47cdd64b
RV
131#define __dynamic_func_call(id, fmt, func, ...) do { \
132 DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \
133 if (DYNAMIC_DEBUG_BRANCH(id)) \
134 func(&id, ##__VA_ARGS__); \
07613b0b
JB
135} while (0)
136
47cdd64b
RV
137#define __dynamic_func_call_no_desc(id, fmt, func, ...) do { \
138 DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \
139 if (DYNAMIC_DEBUG_BRANCH(id)) \
140 func(__VA_ARGS__); \
07613b0b
JB
141} while (0)
142
47cdd64b
RV
143/*
144 * "Factory macro" for generating a call to func, guarded by a
e5ebffe1 145 * DYNAMIC_DEBUG_BRANCH. The dynamic debug descriptor will be
47cdd64b
RV
146 * initialized using the fmt argument. The function will be called with
147 * the address of the descriptor as first argument, followed by all
148 * the varargs. Note that fmt is repeated in invocations of this
149 * macro.
150 */
151#define _dynamic_func_call(fmt, func, ...) \
152 __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
153/*
154 * A variant that does the same, except that the descriptor is not
155 * passed as the first argument to the function; it is only called
156 * with precisely the macro's varargs.
157 */
158#define _dynamic_func_call_no_desc(fmt, func, ...) \
159 __dynamic_func_call_no_desc(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
ffa10cb4 160
47cdd64b
RV
161#define dynamic_pr_debug(fmt, ...) \
162 _dynamic_func_call(fmt, __dynamic_pr_debug, \
163 pr_fmt(fmt), ##__VA_ARGS__)
164
165#define dynamic_dev_dbg(dev, fmt, ...) \
166 _dynamic_func_call(fmt,__dynamic_dev_dbg, \
167 dev, fmt, ##__VA_ARGS__)
168
169#define dynamic_netdev_dbg(dev, fmt, ...) \
170 _dynamic_func_call(fmt, __dynamic_netdev_dbg, \
171 dev, fmt, ##__VA_ARGS__)
172
923abb9d
GP
173#define dynamic_ibdev_dbg(dev, fmt, ...) \
174 _dynamic_func_call(fmt, __dynamic_ibdev_dbg, \
175 dev, fmt, ##__VA_ARGS__)
176
47cdd64b
RV
177#define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
178 groupsize, buf, len, ascii) \
179 _dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \
180 print_hex_dump, \
181 KERN_DEBUG, prefix_str, prefix_type, \
182 rowsize, groupsize, buf, len, ascii)
7a555613 183
a2d375ed 184#else /* !CONFIG_DYNAMIC_DEBUG_CORE */
e9d376f0 185
b48420c1
JC
186#include <linux/string.h>
187#include <linux/errno.h>
a2d375ed 188#include <linux/printk.h>
b48420c1 189
a4507fed
RV
190static inline int ddebug_add_module(struct _ddebug *tab, unsigned int n,
191 const char *modname)
192{
193 return 0;
194}
195
ff49d74a 196static inline int ddebug_remove_module(const char *mod)
e9d376f0
JB
197{
198 return 0;
199}
200
b48420c1
JC
201static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
202 const char *modname)
203{
204 if (strstr(param, "dyndbg")) {
516cf1be
JC
205 /* avoid pr_warn(), which wants pr_fmt() fully defined */
206 printk(KERN_WARNING "dyndbg param is supported only in "
b48420c1
JC
207 "CONFIG_DYNAMIC_DEBUG builds\n");
208 return 0; /* allow and ignore */
209 }
210 return -EINVAL;
211}
212
00b55864
JP
213#define dynamic_pr_debug(fmt, ...) \
214 do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
be70e267 215#define dynamic_dev_dbg(dev, fmt, ...) \
00b55864 216 do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)
011c7289
AB
217#define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
218 groupsize, buf, len, ascii) \
219 do { if (0) \
220 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, \
221 rowsize, groupsize, buf, len, ascii); \
222 } while (0)
a2d375ed
JC
223
224static inline int dynamic_debug_exec_queries(const char *query, const char *modname)
225{
226 pr_warn("kernel not built with CONFIG_DYNAMIC_DEBUG_CORE\n");
227 return 0;
228}
229
230#endif /* !CONFIG_DYNAMIC_DEBUG_CORE */
e9d376f0
JB
231
232#endif