1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Linux WiMAX
4*4882a593Smuzhiyun * Collection of tools to manage debug operations.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright (C) 2005-2007 Intel Corporation
7*4882a593Smuzhiyun * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Don't #include this file directly, read on!
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * EXECUTING DEBUGGING ACTIONS OR NOT
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * The main thing this framework provides is decission power to take a
14*4882a593Smuzhiyun * debug action (like printing a message) if the current debug level
15*4882a593Smuzhiyun * allows it.
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * The decission power is at two levels: at compile-time (what does
18*4882a593Smuzhiyun * not make it is compiled out) and at run-time. The run-time
19*4882a593Smuzhiyun * selection is done per-submodule (as they are declared by the user
20*4882a593Smuzhiyun * of the framework).
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * A call to d_test(L) (L being the target debug level) returns true
23*4882a593Smuzhiyun * if the action should be taken because the current debug levels
24*4882a593Smuzhiyun * allow it (both compile and run time).
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * It follows that a call to d_test() that can be determined to be
27*4882a593Smuzhiyun * always false at compile time will get the code depending on it
28*4882a593Smuzhiyun * compiled out by optimization.
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * DEBUG LEVELS
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * It is up to the caller to define how much a debugging level is.
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun * Convention sets 0 as "no debug" (so an action marked as debug level 0
35*4882a593Smuzhiyun * will always be taken). The increasing debug levels are used for
36*4882a593Smuzhiyun * increased verbosity.
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * USAGE
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun * Group the code in modules and submodules inside each module [which
41*4882a593Smuzhiyun * in most cases maps to Linux modules and .c files that compose
42*4882a593Smuzhiyun * those].
43*4882a593Smuzhiyun *
44*4882a593Smuzhiyun * For each module, there is:
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun * - a MODULENAME (single word, legal C identifier)
47*4882a593Smuzhiyun *
48*4882a593Smuzhiyun * - a debug-levels.h header file that declares the list of
49*4882a593Smuzhiyun * submodules and that is included by all .c files that use
50*4882a593Smuzhiyun * the debugging tools. The file name can be anything.
51*4882a593Smuzhiyun *
52*4882a593Smuzhiyun * - some (optional) .c code to manipulate the runtime debug levels
53*4882a593Smuzhiyun * through debugfs.
54*4882a593Smuzhiyun *
55*4882a593Smuzhiyun * The debug-levels.h file would look like:
56*4882a593Smuzhiyun *
57*4882a593Smuzhiyun * #ifndef __debug_levels__h__
58*4882a593Smuzhiyun * #define __debug_levels__h__
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * #define D_MODULENAME modulename
61*4882a593Smuzhiyun * #define D_MASTER 10
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * #include <linux/wimax/debug.h>
64*4882a593Smuzhiyun *
65*4882a593Smuzhiyun * enum d_module {
66*4882a593Smuzhiyun * D_SUBMODULE_DECLARE(submodule_1),
67*4882a593Smuzhiyun * D_SUBMODULE_DECLARE(submodule_2),
68*4882a593Smuzhiyun * ...
69*4882a593Smuzhiyun * D_SUBMODULE_DECLARE(submodule_N)
70*4882a593Smuzhiyun * };
71*4882a593Smuzhiyun *
72*4882a593Smuzhiyun * #endif
73*4882a593Smuzhiyun *
74*4882a593Smuzhiyun * D_MASTER is the maximum compile-time debug level; any debug actions
75*4882a593Smuzhiyun * above this will be out. D_MODULENAME is the module name (legal C
76*4882a593Smuzhiyun * identifier), which has to be unique for each module (to avoid
77*4882a593Smuzhiyun * namespace collisions during linkage). Note those #defines need to
78*4882a593Smuzhiyun * be done before #including debug.h
79*4882a593Smuzhiyun *
80*4882a593Smuzhiyun * We declare N different submodules whose debug level can be
81*4882a593Smuzhiyun * independently controlled during runtime.
82*4882a593Smuzhiyun *
83*4882a593Smuzhiyun * In a .c file of the module (and only in one of them), define the
84*4882a593Smuzhiyun * following code:
85*4882a593Smuzhiyun *
86*4882a593Smuzhiyun * struct d_level D_LEVEL[] = {
87*4882a593Smuzhiyun * D_SUBMODULE_DEFINE(submodule_1),
88*4882a593Smuzhiyun * D_SUBMODULE_DEFINE(submodule_2),
89*4882a593Smuzhiyun * ...
90*4882a593Smuzhiyun * D_SUBMODULE_DEFINE(submodule_N),
91*4882a593Smuzhiyun * };
92*4882a593Smuzhiyun * size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
93*4882a593Smuzhiyun *
94*4882a593Smuzhiyun * Externs for d_level_MODULENAME and d_level_size_MODULENAME are used
95*4882a593Smuzhiyun * and declared in this file using the D_LEVEL and D_LEVEL_SIZE macros
96*4882a593Smuzhiyun * #defined also in this file.
97*4882a593Smuzhiyun *
98*4882a593Smuzhiyun * To manipulate from user space the levels, create a debugfs dentry
99*4882a593Smuzhiyun * and then register each submodule with:
100*4882a593Smuzhiyun *
101*4882a593Smuzhiyun * d_level_register_debugfs("PREFIX_", submodule_X, parent);
102*4882a593Smuzhiyun *
103*4882a593Smuzhiyun * Where PREFIX_ is a name of your chosing. This will create debugfs
104*4882a593Smuzhiyun * file with a single numeric value that can be use to tweak it. To
105*4882a593Smuzhiyun * remove the entires, just use debugfs_remove_recursive() on 'parent'.
106*4882a593Smuzhiyun *
107*4882a593Smuzhiyun * NOTE: remember that even if this will show attached to some
108*4882a593Smuzhiyun * particular instance of a device, the settings are *global*.
109*4882a593Smuzhiyun *
110*4882a593Smuzhiyun * On each submodule (for example, .c files), the debug infrastructure
111*4882a593Smuzhiyun * should be included like this:
112*4882a593Smuzhiyun *
113*4882a593Smuzhiyun * #define D_SUBMODULE submodule_x // matches one in debug-levels.h
114*4882a593Smuzhiyun * #include "debug-levels.h"
115*4882a593Smuzhiyun *
116*4882a593Smuzhiyun * after #including all your include files.
117*4882a593Smuzhiyun *
118*4882a593Smuzhiyun * Now you can use the d_*() macros below [d_test(), d_fnstart(),
119*4882a593Smuzhiyun * d_fnend(), d_printf(), d_dump()].
120*4882a593Smuzhiyun *
121*4882a593Smuzhiyun * If their debug level is greater than D_MASTER, they will be
122*4882a593Smuzhiyun * compiled out.
123*4882a593Smuzhiyun *
124*4882a593Smuzhiyun * If their debug level is lower or equal than D_MASTER but greater
125*4882a593Smuzhiyun * than the current debug level of their submodule, they'll be
126*4882a593Smuzhiyun * ignored.
127*4882a593Smuzhiyun *
128*4882a593Smuzhiyun * Otherwise, the action will be performed.
129*4882a593Smuzhiyun */
130*4882a593Smuzhiyun #ifndef __debug__h__
131*4882a593Smuzhiyun #define __debug__h__
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun #include <linux/types.h>
134*4882a593Smuzhiyun #include <linux/slab.h>
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun struct device;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun /* Backend stuff */
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /*
141*4882a593Smuzhiyun * Debug backend: generate a message header from a 'struct device'
142*4882a593Smuzhiyun *
143*4882a593Smuzhiyun * @head: buffer where to place the header
144*4882a593Smuzhiyun * @head_size: length of @head
145*4882a593Smuzhiyun * @dev: pointer to device used to generate a header from. If NULL,
146*4882a593Smuzhiyun * an empty ("") header is generated.
147*4882a593Smuzhiyun */
148*4882a593Smuzhiyun static inline
__d_head(char * head,size_t head_size,struct device * dev)149*4882a593Smuzhiyun void __d_head(char *head, size_t head_size,
150*4882a593Smuzhiyun struct device *dev)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun if (dev == NULL)
153*4882a593Smuzhiyun head[0] = 0;
154*4882a593Smuzhiyun else if ((unsigned long)dev < 4096) {
155*4882a593Smuzhiyun printk(KERN_ERR "E: Corrupt dev %p\n", dev);
156*4882a593Smuzhiyun WARN_ON(1);
157*4882a593Smuzhiyun } else
158*4882a593Smuzhiyun snprintf(head, head_size, "%s %s: ",
159*4882a593Smuzhiyun dev_driver_string(dev), dev_name(dev));
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun /*
164*4882a593Smuzhiyun * Debug backend: log some message if debugging is enabled
165*4882a593Smuzhiyun *
166*4882a593Smuzhiyun * @l: intended debug level
167*4882a593Smuzhiyun * @tag: tag to prefix the message with
168*4882a593Smuzhiyun * @dev: 'struct device' associated to this message
169*4882a593Smuzhiyun * @f: printf-like format and arguments
170*4882a593Smuzhiyun *
171*4882a593Smuzhiyun * Note this is optimized out if it doesn't pass the compile-time
172*4882a593Smuzhiyun * check; however, it is *always* compiled. This is useful to make
173*4882a593Smuzhiyun * sure the printf-like formats and variables are always checked and
174*4882a593Smuzhiyun * they don't get bit rot if you have all the debugging disabled.
175*4882a593Smuzhiyun */
176*4882a593Smuzhiyun #define _d_printf(l, tag, dev, f, a...) \
177*4882a593Smuzhiyun do { \
178*4882a593Smuzhiyun char head[64]; \
179*4882a593Smuzhiyun if (!d_test(l)) \
180*4882a593Smuzhiyun break; \
181*4882a593Smuzhiyun __d_head(head, sizeof(head), dev); \
182*4882a593Smuzhiyun printk(KERN_ERR "%s%s%s: " f, head, __func__, tag, ##a); \
183*4882a593Smuzhiyun } while (0)
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /*
187*4882a593Smuzhiyun * CPP syntactic sugar to generate A_B like symbol names when one of
188*4882a593Smuzhiyun * the arguments is a preprocessor #define.
189*4882a593Smuzhiyun */
190*4882a593Smuzhiyun #define __D_PASTE__(varname, modulename) varname##_##modulename
191*4882a593Smuzhiyun #define __D_PASTE(varname, modulename) (__D_PASTE__(varname, modulename))
192*4882a593Smuzhiyun #define _D_SUBMODULE_INDEX(_name) (D_SUBMODULE_DECLARE(_name))
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /*
196*4882a593Smuzhiyun * Store a submodule's runtime debug level and name
197*4882a593Smuzhiyun */
198*4882a593Smuzhiyun struct d_level {
199*4882a593Smuzhiyun u8 level;
200*4882a593Smuzhiyun const char *name;
201*4882a593Smuzhiyun };
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun /*
205*4882a593Smuzhiyun * List of available submodules and their debug levels
206*4882a593Smuzhiyun *
207*4882a593Smuzhiyun * We call them d_level_MODULENAME and d_level_size_MODULENAME; the
208*4882a593Smuzhiyun * macros D_LEVEL and D_LEVEL_SIZE contain the name already for
209*4882a593Smuzhiyun * convenience.
210*4882a593Smuzhiyun *
211*4882a593Smuzhiyun * This array and the size are defined on some .c file that is part of
212*4882a593Smuzhiyun * the current module.
213*4882a593Smuzhiyun */
214*4882a593Smuzhiyun #define D_LEVEL __D_PASTE(d_level, D_MODULENAME)
215*4882a593Smuzhiyun #define D_LEVEL_SIZE __D_PASTE(d_level_size, D_MODULENAME)
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun extern struct d_level D_LEVEL[];
218*4882a593Smuzhiyun extern size_t D_LEVEL_SIZE;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /*
222*4882a593Smuzhiyun * Frontend stuff
223*4882a593Smuzhiyun *
224*4882a593Smuzhiyun *
225*4882a593Smuzhiyun * Stuff you need to declare prior to using the actual "debug" actions
226*4882a593Smuzhiyun * (defined below).
227*4882a593Smuzhiyun */
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun #ifndef D_MODULENAME
230*4882a593Smuzhiyun #error D_MODULENAME is not defined in your debug-levels.h file
231*4882a593Smuzhiyun /**
232*4882a593Smuzhiyun * D_MODULE - Name of the current module
233*4882a593Smuzhiyun *
234*4882a593Smuzhiyun * #define in your module's debug-levels.h, making sure it is
235*4882a593Smuzhiyun * unique. This has to be a legal C identifier.
236*4882a593Smuzhiyun */
237*4882a593Smuzhiyun #define D_MODULENAME undefined_modulename
238*4882a593Smuzhiyun #endif
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun #ifndef D_MASTER
242*4882a593Smuzhiyun #warning D_MASTER not defined, but debug.h included! [see docs]
243*4882a593Smuzhiyun /**
244*4882a593Smuzhiyun * D_MASTER - Compile time maximum debug level
245*4882a593Smuzhiyun *
246*4882a593Smuzhiyun * #define in your debug-levels.h file to the maximum debug level the
247*4882a593Smuzhiyun * runtime code will be allowed to have. This allows you to provide a
248*4882a593Smuzhiyun * main knob.
249*4882a593Smuzhiyun *
250*4882a593Smuzhiyun * Anything above that level will be optimized out of the compile.
251*4882a593Smuzhiyun *
252*4882a593Smuzhiyun * Defaults to zero (no debug code compiled in).
253*4882a593Smuzhiyun *
254*4882a593Smuzhiyun * Maximum one definition per module (at the debug-levels.h file).
255*4882a593Smuzhiyun */
256*4882a593Smuzhiyun #define D_MASTER 0
257*4882a593Smuzhiyun #endif
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun #ifndef D_SUBMODULE
260*4882a593Smuzhiyun #error D_SUBMODULE not defined, but debug.h included! [see docs]
261*4882a593Smuzhiyun /**
262*4882a593Smuzhiyun * D_SUBMODULE - Name of the current submodule
263*4882a593Smuzhiyun *
264*4882a593Smuzhiyun * #define in your submodule .c file before #including debug-levels.h
265*4882a593Smuzhiyun * to the name of the current submodule as previously declared and
266*4882a593Smuzhiyun * defined with D_SUBMODULE_DECLARE() (in your module's
267*4882a593Smuzhiyun * debug-levels.h) and D_SUBMODULE_DEFINE().
268*4882a593Smuzhiyun *
269*4882a593Smuzhiyun * This is used to provide runtime-control over the debug levels.
270*4882a593Smuzhiyun *
271*4882a593Smuzhiyun * Maximum one per .c file! Can be shared among different .c files
272*4882a593Smuzhiyun * (meaning they belong to the same submodule categorization).
273*4882a593Smuzhiyun */
274*4882a593Smuzhiyun #define D_SUBMODULE undefined_module
275*4882a593Smuzhiyun #endif
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun /**
279*4882a593Smuzhiyun * D_SUBMODULE_DECLARE - Declare a submodule for runtime debug level control
280*4882a593Smuzhiyun *
281*4882a593Smuzhiyun * @_name: name of the submodule, restricted to the chars that make up a
282*4882a593Smuzhiyun * valid C identifier ([a-zA-Z0-9_]).
283*4882a593Smuzhiyun *
284*4882a593Smuzhiyun * Declare in the module's debug-levels.h header file as:
285*4882a593Smuzhiyun *
286*4882a593Smuzhiyun * enum d_module {
287*4882a593Smuzhiyun * D_SUBMODULE_DECLARE(submodule_1),
288*4882a593Smuzhiyun * D_SUBMODULE_DECLARE(submodule_2),
289*4882a593Smuzhiyun * D_SUBMODULE_DECLARE(submodule_3),
290*4882a593Smuzhiyun * };
291*4882a593Smuzhiyun *
292*4882a593Smuzhiyun * Some corresponding .c file needs to have a matching
293*4882a593Smuzhiyun * D_SUBMODULE_DEFINE().
294*4882a593Smuzhiyun */
295*4882a593Smuzhiyun #define D_SUBMODULE_DECLARE(_name) __D_SUBMODULE_##_name
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun /**
299*4882a593Smuzhiyun * D_SUBMODULE_DEFINE - Define a submodule for runtime debug level control
300*4882a593Smuzhiyun *
301*4882a593Smuzhiyun * @_name: name of the submodule, restricted to the chars that make up a
302*4882a593Smuzhiyun * valid C identifier ([a-zA-Z0-9_]).
303*4882a593Smuzhiyun *
304*4882a593Smuzhiyun * Use once per module (in some .c file) as:
305*4882a593Smuzhiyun *
306*4882a593Smuzhiyun * static
307*4882a593Smuzhiyun * struct d_level d_level_SUBMODULENAME[] = {
308*4882a593Smuzhiyun * D_SUBMODULE_DEFINE(submodule_1),
309*4882a593Smuzhiyun * D_SUBMODULE_DEFINE(submodule_2),
310*4882a593Smuzhiyun * D_SUBMODULE_DEFINE(submodule_3),
311*4882a593Smuzhiyun * };
312*4882a593Smuzhiyun * size_t d_level_size_SUBDMODULENAME = ARRAY_SIZE(d_level_SUBDMODULENAME);
313*4882a593Smuzhiyun *
314*4882a593Smuzhiyun * Matching D_SUBMODULE_DECLARE()s have to be present in a
315*4882a593Smuzhiyun * debug-levels.h header file.
316*4882a593Smuzhiyun */
317*4882a593Smuzhiyun #define D_SUBMODULE_DEFINE(_name) \
318*4882a593Smuzhiyun [__D_SUBMODULE_##_name] = { \
319*4882a593Smuzhiyun .level = 0, \
320*4882a593Smuzhiyun .name = #_name \
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun /* The actual "debug" operations */
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun /**
329*4882a593Smuzhiyun * d_test - Returns true if debugging should be enabled
330*4882a593Smuzhiyun *
331*4882a593Smuzhiyun * @l: intended debug level (unsigned)
332*4882a593Smuzhiyun *
333*4882a593Smuzhiyun * If the master debug switch is enabled and the current settings are
334*4882a593Smuzhiyun * higher or equal to the requested level, then debugging
335*4882a593Smuzhiyun * output/actions should be enabled.
336*4882a593Smuzhiyun *
337*4882a593Smuzhiyun * NOTE:
338*4882a593Smuzhiyun *
339*4882a593Smuzhiyun * This needs to be coded so that it can be evaluated in compile
340*4882a593Smuzhiyun * time; this is why the ugly BUG_ON() is placed in there, so the
341*4882a593Smuzhiyun * D_MASTER evaluation compiles all out if it is compile-time false.
342*4882a593Smuzhiyun */
343*4882a593Smuzhiyun #define d_test(l) \
344*4882a593Smuzhiyun ({ \
345*4882a593Smuzhiyun unsigned __l = l; /* type enforcer */ \
346*4882a593Smuzhiyun (D_MASTER) >= __l \
347*4882a593Smuzhiyun && ({ \
348*4882a593Smuzhiyun BUG_ON(_D_SUBMODULE_INDEX(D_SUBMODULE) >= D_LEVEL_SIZE);\
349*4882a593Smuzhiyun D_LEVEL[_D_SUBMODULE_INDEX(D_SUBMODULE)].level >= __l; \
350*4882a593Smuzhiyun }); \
351*4882a593Smuzhiyun })
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun /**
355*4882a593Smuzhiyun * d_fnstart - log message at function start if debugging enabled
356*4882a593Smuzhiyun *
357*4882a593Smuzhiyun * @l: intended debug level
358*4882a593Smuzhiyun * @_dev: 'struct device' pointer, NULL if none (for context)
359*4882a593Smuzhiyun * @f: printf-like format and arguments
360*4882a593Smuzhiyun */
361*4882a593Smuzhiyun #define d_fnstart(l, _dev, f, a...) _d_printf(l, " FNSTART", _dev, f, ## a)
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun /**
365*4882a593Smuzhiyun * d_fnend - log message at function end if debugging enabled
366*4882a593Smuzhiyun *
367*4882a593Smuzhiyun * @l: intended debug level
368*4882a593Smuzhiyun * @_dev: 'struct device' pointer, NULL if none (for context)
369*4882a593Smuzhiyun * @f: printf-like format and arguments
370*4882a593Smuzhiyun */
371*4882a593Smuzhiyun #define d_fnend(l, _dev, f, a...) _d_printf(l, " FNEND", _dev, f, ## a)
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun /**
375*4882a593Smuzhiyun * d_printf - log message if debugging enabled
376*4882a593Smuzhiyun *
377*4882a593Smuzhiyun * @l: intended debug level
378*4882a593Smuzhiyun * @_dev: 'struct device' pointer, NULL if none (for context)
379*4882a593Smuzhiyun * @f: printf-like format and arguments
380*4882a593Smuzhiyun */
381*4882a593Smuzhiyun #define d_printf(l, _dev, f, a...) _d_printf(l, "", _dev, f, ## a)
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun /**
385*4882a593Smuzhiyun * d_dump - log buffer hex dump if debugging enabled
386*4882a593Smuzhiyun *
387*4882a593Smuzhiyun * @l: intended debug level
388*4882a593Smuzhiyun * @_dev: 'struct device' pointer, NULL if none (for context)
389*4882a593Smuzhiyun * @f: printf-like format and arguments
390*4882a593Smuzhiyun */
391*4882a593Smuzhiyun #define d_dump(l, dev, ptr, size) \
392*4882a593Smuzhiyun do { \
393*4882a593Smuzhiyun char head[64]; \
394*4882a593Smuzhiyun if (!d_test(l)) \
395*4882a593Smuzhiyun break; \
396*4882a593Smuzhiyun __d_head(head, sizeof(head), dev); \
397*4882a593Smuzhiyun print_hex_dump(KERN_ERR, head, 0, 16, 1, \
398*4882a593Smuzhiyun ((void *) ptr), (size), 0); \
399*4882a593Smuzhiyun } while (0)
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun /**
403*4882a593Smuzhiyun * Export a submodule's debug level over debugfs as PREFIXSUBMODULE
404*4882a593Smuzhiyun *
405*4882a593Smuzhiyun * @prefix: string to prefix the name with
406*4882a593Smuzhiyun * @submodule: name of submodule (not a string, just the name)
407*4882a593Smuzhiyun * @dentry: debugfs parent dentry
408*4882a593Smuzhiyun *
409*4882a593Smuzhiyun * For removing, just use debugfs_remove_recursive() on the parent.
410*4882a593Smuzhiyun */
411*4882a593Smuzhiyun #define d_level_register_debugfs(prefix, name, parent) \
412*4882a593Smuzhiyun ({ \
413*4882a593Smuzhiyun debugfs_create_u8( \
414*4882a593Smuzhiyun prefix #name, 0600, parent, \
415*4882a593Smuzhiyun &(D_LEVEL[__D_SUBMODULE_ ## name].level)); \
416*4882a593Smuzhiyun })
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun static inline
d_submodule_set(struct d_level * d_level,size_t d_level_size,const char * submodule,u8 level,const char * tag)420*4882a593Smuzhiyun void d_submodule_set(struct d_level *d_level, size_t d_level_size,
421*4882a593Smuzhiyun const char *submodule, u8 level, const char *tag)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun struct d_level *itr, *top;
424*4882a593Smuzhiyun int index = -1;
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun for (itr = d_level, top = itr + d_level_size; itr < top; itr++) {
427*4882a593Smuzhiyun index++;
428*4882a593Smuzhiyun if (itr->name == NULL) {
429*4882a593Smuzhiyun printk(KERN_ERR "%s: itr->name NULL?? (%p, #%d)\n",
430*4882a593Smuzhiyun tag, itr, index);
431*4882a593Smuzhiyun continue;
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun if (!strcmp(itr->name, submodule)) {
434*4882a593Smuzhiyun itr->level = level;
435*4882a593Smuzhiyun return;
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun printk(KERN_ERR "%s: unknown submodule %s\n", tag, submodule);
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun /**
443*4882a593Smuzhiyun * d_parse_params - Parse a string with debug parameters from the
444*4882a593Smuzhiyun * command line
445*4882a593Smuzhiyun *
446*4882a593Smuzhiyun * @d_level: level structure (D_LEVEL)
447*4882a593Smuzhiyun * @d_level_size: number of items in the level structure
448*4882a593Smuzhiyun * (D_LEVEL_SIZE).
449*4882a593Smuzhiyun * @_params: string with the parameters; this is a space (not tab!)
450*4882a593Smuzhiyun * separated list of NAME:VALUE, where value is the debug level
451*4882a593Smuzhiyun * and NAME is the name of the submodule.
452*4882a593Smuzhiyun * @tag: string for error messages (example: MODULE.ARGNAME).
453*4882a593Smuzhiyun */
454*4882a593Smuzhiyun static inline
d_parse_params(struct d_level * d_level,size_t d_level_size,const char * _params,const char * tag)455*4882a593Smuzhiyun void d_parse_params(struct d_level *d_level, size_t d_level_size,
456*4882a593Smuzhiyun const char *_params, const char *tag)
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun char submodule[130], *params, *params_orig, *token, *colon;
459*4882a593Smuzhiyun unsigned level, tokens;
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun if (_params == NULL)
462*4882a593Smuzhiyun return;
463*4882a593Smuzhiyun params_orig = kstrdup(_params, GFP_KERNEL);
464*4882a593Smuzhiyun params = params_orig;
465*4882a593Smuzhiyun while (1) {
466*4882a593Smuzhiyun token = strsep(¶ms, " ");
467*4882a593Smuzhiyun if (token == NULL)
468*4882a593Smuzhiyun break;
469*4882a593Smuzhiyun if (*token == '\0') /* eat joint spaces */
470*4882a593Smuzhiyun continue;
471*4882a593Smuzhiyun /* kernel's sscanf %s eats until whitespace, so we
472*4882a593Smuzhiyun * replace : by \n so it doesn't get eaten later by
473*4882a593Smuzhiyun * strsep */
474*4882a593Smuzhiyun colon = strchr(token, ':');
475*4882a593Smuzhiyun if (colon != NULL)
476*4882a593Smuzhiyun *colon = '\n';
477*4882a593Smuzhiyun tokens = sscanf(token, "%s\n%u", submodule, &level);
478*4882a593Smuzhiyun if (colon != NULL)
479*4882a593Smuzhiyun *colon = ':'; /* set back, for error messages */
480*4882a593Smuzhiyun if (tokens == 2)
481*4882a593Smuzhiyun d_submodule_set(d_level, d_level_size,
482*4882a593Smuzhiyun submodule, level, tag);
483*4882a593Smuzhiyun else
484*4882a593Smuzhiyun printk(KERN_ERR "%s: can't parse '%s' as a "
485*4882a593Smuzhiyun "SUBMODULE:LEVEL (%d tokens)\n",
486*4882a593Smuzhiyun tag, token, tokens);
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun kfree(params_orig);
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun #endif /* #ifndef __debug__h__ */
492