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