blob: 2fd8006153c35081444a9e7bd43e73ce8cf888d8 [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
Jason Baron9049fc72016-08-03 13:46:39 -07005#if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_JUMP_LABEL)
6#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 Assche8ba6ebf2011-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;
Jason Baron9049fc72016-08-03 13:46:39 -070041#ifdef HAVE_JUMP_LABEL
42 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
50int ddebug_add_module(struct _ddebug *tab, unsigned int n,
51 const char *modname);
52
53#if defined(CONFIG_DYNAMIC_DEBUG)
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
Jason Baron9049fc72016-08-03 13:46:39 -070074#define DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, key, init) \
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, \
83 dd_key_init(key, init) \
Jason Baron07613b02011-10-04 14:13:15 -070084 }
Jason Barone9d376f2009-02-05 11:51:38 -050085
Jason Baron9049fc72016-08-03 13:46:39 -070086#ifdef HAVE_JUMP_LABEL
87
88#define dd_key_init(key, init) key = (init)
89
90#ifdef DEBUG
91#define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
92 DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, .key.dd_key_true, \
93 (STATIC_KEY_TRUE_INIT))
94
95#define DYNAMIC_DEBUG_BRANCH(descriptor) \
96 static_branch_likely(&descriptor.key.dd_key_true)
97#else
98#define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
99 DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, .key.dd_key_false, \
100 (STATIC_KEY_FALSE_INIT))
101
102#define DYNAMIC_DEBUG_BRANCH(descriptor) \
103 static_branch_unlikely(&descriptor.key.dd_key_false)
104#endif
105
106#else
107
108#define dd_key_init(key, init)
109
110#define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
111 DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, 0, 0)
112
113#ifdef DEBUG
114#define DYNAMIC_DEBUG_BRANCH(descriptor) \
115 likely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
116#else
117#define DYNAMIC_DEBUG_BRANCH(descriptor) \
118 unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
119#endif
120
121#endif
122
Jason Baron07613b02011-10-04 14:13:15 -0700123#define dynamic_pr_debug(fmt, ...) \
124do { \
125 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
Jason Baron9049fc72016-08-03 13:46:39 -0700126 if (DYNAMIC_DEBUG_BRANCH(descriptor)) \
Jason Baron07613b02011-10-04 14:13:15 -0700127 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \
128 ##__VA_ARGS__); \
129} while (0)
Jason Barone9d376f2009-02-05 11:51:38 -0500130
Jason Baron07613b02011-10-04 14:13:15 -0700131#define dynamic_dev_dbg(dev, fmt, ...) \
132do { \
Jim Cromie87e6f962011-12-19 17:11:13 -0500133 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
Jason Baron9049fc72016-08-03 13:46:39 -0700134 if (DYNAMIC_DEBUG_BRANCH(descriptor)) \
Jason Baron07613b02011-10-04 14:13:15 -0700135 __dynamic_dev_dbg(&descriptor, dev, fmt, \
136 ##__VA_ARGS__); \
137} while (0)
138
139#define dynamic_netdev_dbg(dev, fmt, ...) \
140do { \
141 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
Jason Baron9049fc72016-08-03 13:46:39 -0700142 if (DYNAMIC_DEBUG_BRANCH(descriptor)) \
Jason Baron07613b02011-10-04 14:13:15 -0700143 __dynamic_netdev_dbg(&descriptor, dev, fmt, \
144 ##__VA_ARGS__); \
145} while (0)
Jason Baronffa10cb2011-08-11 14:36:48 -0400146
Vladimir Kondratiev7a555612012-12-05 16:48:27 -0500147#define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
148 groupsize, buf, len, ascii) \
149do { \
150 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, \
151 __builtin_constant_p(prefix_str) ? prefix_str : "hexdump");\
Jason Baron9049fc72016-08-03 13:46:39 -0700152 if (DYNAMIC_DEBUG_BRANCH(descriptor)) \
Vladimir Kondratiev7a555612012-12-05 16:48:27 -0500153 print_hex_dump(KERN_DEBUG, prefix_str, \
154 prefix_type, rowsize, groupsize, \
155 buf, len, ascii); \
156} while (0)
157
Jason Barone9d376f2009-02-05 11:51:38 -0500158#else
159
Jim Cromieb48420c2012-04-27 14:30:35 -0600160#include <linux/string.h>
161#include <linux/errno.h>
162
Yehuda Sadehff49d742010-07-03 13:07:35 +1000163static inline int ddebug_remove_module(const char *mod)
Jason Barone9d376f2009-02-05 11:51:38 -0500164{
165 return 0;
166}
167
Jim Cromieb48420c2012-04-27 14:30:35 -0600168static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
169 const char *modname)
170{
171 if (strstr(param, "dyndbg")) {
Jim Cromie516cf1b2012-05-01 05:23:12 -0600172 /* avoid pr_warn(), which wants pr_fmt() fully defined */
173 printk(KERN_WARNING "dyndbg param is supported only in "
Jim Cromieb48420c2012-04-27 14:30:35 -0600174 "CONFIG_DYNAMIC_DEBUG builds\n");
175 return 0; /* allow and ignore */
176 }
177 return -EINVAL;
178}
179
Joe Perches00b55862009-12-14 18:00:14 -0800180#define dynamic_pr_debug(fmt, ...) \
181 do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
Philipp Reisnerbe70e262010-10-14 11:58:20 +0200182#define dynamic_dev_dbg(dev, fmt, ...) \
Joe Perches00b55862009-12-14 18:00:14 -0800183 do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)
Jason Barone9d376f2009-02-05 11:51:38 -0500184#endif
185
186#endif