Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.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
ca90fca7
JC
9#include <linux/build_bug.h>
10
e9d376f0
JB
11/*
12 * An instance of this structure is created in a special
13 * ELF section at every dynamic debug callsite. At runtime,
14 * the special section is treated as an array of these.
15 */
16struct _ddebug {
17 /*
18 * These fields are used to drive the user interface
19 * for selecting and displaying debug callsites.
20 */
21 const char *modname;
22 const char *function;
23 const char *filename;
24 const char *format;
e703ddae 25 unsigned int lineno:18;
ca90fca7
JC
26#define CLS_BITS 6
27 unsigned int class_id:CLS_BITS;
28#define _DPRINTK_CLASS_DFLT ((1 << CLS_BITS) - 1)
e9d376f0 29 /*
3faa2860
JC
30 * The flags field controls the behaviour at the callsite.
31 * The bits here are changed dynamically when the user
2b2f68b5 32 * writes commands to <debugfs>/dynamic_debug/control
e9d376f0 33 */
5ca7d2a6
JC
34#define _DPRINTK_FLAGS_NONE 0
35#define _DPRINTK_FLAGS_PRINT (1<<0) /* printk() a message using the format */
8ba6ebf5
BVA
36#define _DPRINTK_FLAGS_INCL_MODNAME (1<<1)
37#define _DPRINTK_FLAGS_INCL_FUNCNAME (1<<2)
38#define _DPRINTK_FLAGS_INCL_LINENO (1<<3)
39#define _DPRINTK_FLAGS_INCL_TID (1<<4)
31ed379b 40#define _DPRINTK_FLAGS_INCL_SOURCENAME (1<<5)
640d1eaf
JC
41
42#define _DPRINTK_FLAGS_INCL_ANY \
43 (_DPRINTK_FLAGS_INCL_MODNAME | _DPRINTK_FLAGS_INCL_FUNCNAME |\
31ed379b
TW
44 _DPRINTK_FLAGS_INCL_LINENO | _DPRINTK_FLAGS_INCL_TID |\
45 _DPRINTK_FLAGS_INCL_SOURCENAME)
640d1eaf 46
b558c96f
JC
47#if defined DEBUG
48#define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
49#else
e9d376f0 50#define _DPRINTK_FLAGS_DEFAULT 0
b558c96f 51#endif
e9d376f0 52 unsigned int flags:8;
e9666d10 53#ifdef CONFIG_JUMP_LABEL
9049fc74
JB
54 union {
55 struct static_key_true dd_key_true;
56 struct static_key_false dd_key_false;
57 } key;
58#endif
e9d376f0
JB
59} __attribute__((aligned(8)));
60
aad0214f
JC
61enum class_map_type {
62 DD_CLASS_TYPE_DISJOINT_BITS,
63 /**
64 * DD_CLASS_TYPE_DISJOINT_BITS: classes are independent, one per bit.
65 * expecting hex input. Built for drm.debug, basis for other types.
66 */
67 DD_CLASS_TYPE_LEVEL_NUM,
68 /**
69 * DD_CLASS_TYPE_LEVEL_NUM: input is numeric level, 0-N.
70 * N turns on just bits N-1 .. 0, so N=0 turns all bits off.
71 */
72 DD_CLASS_TYPE_DISJOINT_NAMES,
73 /**
74 * DD_CLASS_TYPE_DISJOINT_NAMES: input is a CSV of [+-]CLASS_NAMES,
75 * classes are independent, like _DISJOINT_BITS.
76 */
77 DD_CLASS_TYPE_LEVEL_NAMES,
78 /**
79 * DD_CLASS_TYPE_LEVEL_NAMES: input is a CSV of [+-]CLASS_NAMES,
80 * intended for names like: INFO,DEBUG,TRACE, with a module prefix
81 * avoid EMERG,ALERT,CRIT,ERR,WARNING: they're not debug
82 */
83};
84
85struct ddebug_class_map {
86 struct list_head link;
87 struct module *mod;
88 const char *mod_name; /* needed for builtins */
89 const char **class_names;
90 const int length;
91 const int base; /* index of 1st .class_id, allows split/shared space */
92 enum class_map_type map_type;
93};
94
95/**
96 * DECLARE_DYNDBG_CLASSMAP - declare classnames known by a module
97 * @_var: a struct ddebug_class_map, passed to module_param_cb
98 * @_type: enum class_map_type, chooses bits/verbose, numeric/symbolic
99 * @_base: offset of 1st class-name. splits .class_id space
100 * @classes: class-names used to control class'd prdbgs
101 */
102#define DECLARE_DYNDBG_CLASSMAP(_var, _maptype, _base, ...) \
103 static const char *_var##_classnames[] = { __VA_ARGS__ }; \
104 static struct ddebug_class_map __aligned(8) __used \
105 __section("__dyndbg_classes") _var = { \
106 .mod = THIS_MODULE, \
107 .mod_name = KBUILD_MODNAME, \
108 .base = _base, \
109 .map_type = _maptype, \
110 .length = NUM_TYPE_ARGS(char*, __VA_ARGS__), \
111 .class_names = _var##_classnames, \
112 }
113#define NUM_TYPE_ARGS(eltype, ...) \
114 (sizeof((eltype[]){__VA_ARGS__}) / sizeof(eltype))
115
b7b4eebd
JC
116/* encapsulate linker provided built-in (or module) dyndbg data */
117struct _ddebug_info {
118 struct _ddebug *descs;
66f4006b 119 struct ddebug_class_map *classes;
b7b4eebd 120 unsigned int num_descs;
66f4006b 121 unsigned int num_classes;
b7b4eebd 122};
e9d376f0 123
b9400852
JC
124struct ddebug_class_param {
125 union {
126 unsigned long *bits;
127 unsigned int *lvl;
128 };
129 char flags[8];
130 const struct ddebug_class_map *map;
131};
132
7ce93729
JB
133/*
134 * pr_debug() and friends are globally enabled or modules have selectively
135 * enabled them.
136 */
137#if defined(CONFIG_DYNAMIC_DEBUG) || \
138 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
a2d375ed 139
b9075fa9 140extern __printf(2, 3)
906d2015 141void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...);
e9d376f0 142
cbc46635
JP
143struct device;
144
b9075fa9 145extern __printf(3, 4)
906d2015
JP
146void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev,
147 const char *fmt, ...);
cbc46635 148
ffa10cb4
JB
149struct net_device;
150
b9075fa9 151extern __printf(3, 4)
906d2015
JP
152void __dynamic_netdev_dbg(struct _ddebug *descriptor,
153 const struct net_device *dev,
154 const char *fmt, ...);
ffa10cb4 155
923abb9d
GP
156struct ib_device;
157
158extern __printf(3, 4)
159void __dynamic_ibdev_dbg(struct _ddebug *descriptor,
160 const struct ib_device *ibdev,
161 const char *fmt, ...);
162
ca90fca7 163#define DEFINE_DYNAMIC_DEBUG_METADATA_CLS(name, cls, fmt) \
c0d2af63 164 static struct _ddebug __aligned(8) \
33def849 165 __section("__dyndbg") name = { \
07613b0b
JB
166 .modname = KBUILD_MODNAME, \
167 .function = __func__, \
168 .filename = __FILE__, \
169 .format = (fmt), \
170 .lineno = __LINE__, \
9049fc74 171 .flags = _DPRINTK_FLAGS_DEFAULT, \
ca90fca7 172 .class_id = cls, \
2bdde670 173 _DPRINTK_KEY_INIT \
ca90fca7
JC
174 }; \
175 BUILD_BUG_ON_MSG(cls > _DPRINTK_CLASS_DFLT, \
176 "classid value overflow")
177
178#define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
179 DEFINE_DYNAMIC_DEBUG_METADATA_CLS(name, _DPRINTK_CLASS_DFLT, fmt)
07613b0b 180
e9666d10 181#ifdef CONFIG_JUMP_LABEL
9049fc74 182
9049fc74 183#ifdef DEBUG
2bdde670
RV
184
185#define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT)
9049fc74
JB
186
187#define DYNAMIC_DEBUG_BRANCH(descriptor) \
188 static_branch_likely(&descriptor.key.dd_key_true)
189#else
2bdde670 190#define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT)
9049fc74
JB
191
192#define DYNAMIC_DEBUG_BRANCH(descriptor) \
193 static_branch_unlikely(&descriptor.key.dd_key_false)
194#endif
195
a2d375ed 196#else /* !CONFIG_JUMP_LABEL */
9049fc74 197
2bdde670 198#define _DPRINTK_KEY_INIT
9049fc74
JB
199
200#ifdef DEBUG
201#define DYNAMIC_DEBUG_BRANCH(descriptor) \
202 likely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
203#else
204#define DYNAMIC_DEBUG_BRANCH(descriptor) \
205 unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
206#endif
207
a2d375ed 208#endif /* CONFIG_JUMP_LABEL */
9049fc74 209
ca90fca7
JC
210/*
211 * Factory macros: ($prefix)dynamic_func_call($suffix)
212 *
213 * Lower layer (with __ prefix) gets the callsite metadata, and wraps
214 * the func inside a debug-branch/static-key construct. Upper layer
215 * (with _ prefix) does the UNIQUE_ID once, so that lower can ref the
216 * name/label multiple times, and tie the elements together.
217 * Multiple flavors:
218 * (|_cls): adds in _DPRINT_CLASS_DFLT as needed
219 * (|_no_desc): former gets callsite descriptor as 1st arg (for prdbgs)
220 */
221#define __dynamic_func_call_cls(id, cls, fmt, func, ...) do { \
222 DEFINE_DYNAMIC_DEBUG_METADATA_CLS(id, cls, fmt); \
47cdd64b 223 if (DYNAMIC_DEBUG_BRANCH(id)) \
ca90fca7
JC
224 func(&id, ##__VA_ARGS__); \
225} while (0)
226#define __dynamic_func_call(id, fmt, func, ...) \
227 __dynamic_func_call_cls(id, _DPRINTK_CLASS_DFLT, fmt, \
228 func, ##__VA_ARGS__)
229
230#define __dynamic_func_call_cls_no_desc(id, cls, fmt, func, ...) do { \
231 DEFINE_DYNAMIC_DEBUG_METADATA_CLS(id, cls, fmt); \
232 if (DYNAMIC_DEBUG_BRANCH(id)) \
233 func(__VA_ARGS__); \
07613b0b 234} while (0)
ca90fca7
JC
235#define __dynamic_func_call_no_desc(id, fmt, func, ...) \
236 __dynamic_func_call_cls_no_desc(id, _DPRINTK_CLASS_DFLT, \
237 fmt, func, ##__VA_ARGS__)
07613b0b 238
47cdd64b
RV
239/*
240 * "Factory macro" for generating a call to func, guarded by a
e5ebffe1 241 * DYNAMIC_DEBUG_BRANCH. The dynamic debug descriptor will be
47cdd64b
RV
242 * initialized using the fmt argument. The function will be called with
243 * the address of the descriptor as first argument, followed by all
244 * the varargs. Note that fmt is repeated in invocations of this
245 * macro.
246 */
ca90fca7
JC
247#define _dynamic_func_call_cls(cls, fmt, func, ...) \
248 __dynamic_func_call_cls(__UNIQUE_ID(ddebug), cls, fmt, func, ##__VA_ARGS__)
47cdd64b 249#define _dynamic_func_call(fmt, func, ...) \
ca90fca7
JC
250 _dynamic_func_call_cls(_DPRINTK_CLASS_DFLT, fmt, func, ##__VA_ARGS__)
251
47cdd64b
RV
252/*
253 * A variant that does the same, except that the descriptor is not
254 * passed as the first argument to the function; it is only called
255 * with precisely the macro's varargs.
256 */
ca90fca7
JC
257#define _dynamic_func_call_cls_no_desc(cls, fmt, func, ...) \
258 __dynamic_func_call_cls_no_desc(__UNIQUE_ID(ddebug), cls, fmt, \
259 func, ##__VA_ARGS__)
260#define _dynamic_func_call_no_desc(fmt, func, ...) \
261 _dynamic_func_call_cls_no_desc(_DPRINTK_CLASS_DFLT, fmt, \
262 func, ##__VA_ARGS__)
263
264#define dynamic_pr_debug_cls(cls, fmt, ...) \
265 _dynamic_func_call_cls(cls, fmt, __dynamic_pr_debug, \
266 pr_fmt(fmt), ##__VA_ARGS__)
ffa10cb4 267
47cdd64b 268#define dynamic_pr_debug(fmt, ...) \
ca90fca7 269 _dynamic_func_call(fmt, __dynamic_pr_debug, \
47cdd64b
RV
270 pr_fmt(fmt), ##__VA_ARGS__)
271
272#define dynamic_dev_dbg(dev, fmt, ...) \
ca90fca7 273 _dynamic_func_call(fmt, __dynamic_dev_dbg, \
47cdd64b
RV
274 dev, fmt, ##__VA_ARGS__)
275
276#define dynamic_netdev_dbg(dev, fmt, ...) \
277 _dynamic_func_call(fmt, __dynamic_netdev_dbg, \
278 dev, fmt, ##__VA_ARGS__)
279
923abb9d
GP
280#define dynamic_ibdev_dbg(dev, fmt, ...) \
281 _dynamic_func_call(fmt, __dynamic_ibdev_dbg, \
282 dev, fmt, ##__VA_ARGS__)
283
47cdd64b
RV
284#define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
285 groupsize, buf, len, ascii) \
286 _dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \
287 print_hex_dump, \
288 KERN_DEBUG, prefix_str, prefix_type, \
289 rowsize, groupsize, buf, len, ascii)
7a555613 290
3fc95d80
JC
291/* for test only, generally expect drm.debug style macro wrappers */
292#define __pr_debug_cls(cls, fmt, ...) do { \
293 BUILD_BUG_ON_MSG(!__builtin_constant_p(cls), \
294 "expecting constant class int/enum"); \
295 dynamic_pr_debug_cls(cls, fmt, ##__VA_ARGS__); \
296 } while (0)
297
7ce93729 298#else /* !(CONFIG_DYNAMIC_DEBUG || (CONFIG_DYNAMIC_DEBUG_CORE && DYNAMIC_DEBUG_MODULE)) */
e9d376f0 299
b48420c1
JC
300#include <linux/string.h>
301#include <linux/errno.h>
a2d375ed 302#include <linux/printk.h>
b48420c1 303
7ce93729
JB
304#define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt)
305#define DYNAMIC_DEBUG_BRANCH(descriptor) false
b48420c1 306
00b55864 307#define dynamic_pr_debug(fmt, ...) \
0381e588 308 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
be70e267 309#define dynamic_dev_dbg(dev, fmt, ...) \
0381e588 310 dev_no_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__)
011c7289
AB
311#define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
312 groupsize, buf, len, ascii) \
313 do { if (0) \
314 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, \
315 rowsize, groupsize, buf, len, ascii); \
316 } while (0)
a2d375ed 317
7ce93729
JB
318#endif /* CONFIG_DYNAMIC_DEBUG || (CONFIG_DYNAMIC_DEBUG_CORE && DYNAMIC_DEBUG_MODULE) */
319
320
321#ifdef CONFIG_DYNAMIC_DEBUG_CORE
322
323extern int ddebug_dyndbg_module_param_cb(char *param, char *val,
324 const char *modname);
325struct kernel_param;
326int param_set_dyndbg_classes(const char *instr, const struct kernel_param *kp);
327int param_get_dyndbg_classes(char *buffer, const struct kernel_param *kp);
328
329#else
330
331static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
332 const char *modname)
333{
334 if (!strcmp(param, "dyndbg")) {
335 /* avoid pr_warn(), which wants pr_fmt() fully defined */
336 printk(KERN_WARNING "dyndbg param is supported only in "
337 "CONFIG_DYNAMIC_DEBUG builds\n");
338 return 0; /* allow and ignore */
339 }
340 return -EINVAL;
341}
342
b9400852
JC
343struct kernel_param;
344static inline int param_set_dyndbg_classes(const char *instr, const struct kernel_param *kp)
345{ return 0; }
346static inline int param_get_dyndbg_classes(char *buffer, const struct kernel_param *kp)
347{ return 0; }
348
7ce93729
JB
349#endif
350
e9d376f0 351
b9400852
JC
352extern const struct kernel_param_ops param_ops_dyndbg_classes;
353
7ce93729 354#endif /* _DYNAMIC_DEBUG_H */