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