1 /* SPDX-License-Identifier: GPL-2.0-only */
4 * Collection of tools to manage debug operations.
6 * Copyright (C) 2005-2007 Intel Corporation
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
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
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.
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.
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
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
55 * The debug-levels.h file would look like:
57 * #ifndef __debug_levels__h__
58 * #define __debug_levels__h__
60 * #define D_MODULENAME modulename
63 * #include <linux/wimax/debug.h>
66 * D_SUBMODULE_DECLARE(submodule_1),
67 * D_SUBMODULE_DECLARE(submodule_2),
69 * D_SUBMODULE_DECLARE(submodule_N)
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
86 * struct d_level D_LEVEL[] = {
87 * D_SUBMODULE_DEFINE(submodule_1),
88 * D_SUBMODULE_DEFINE(submodule_2),
90 * D_SUBMODULE_DEFINE(submodule_N),
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 * result = d_level_register_debugfs("PREFIX_", submodule_X, parent);
105 * Where PREFIX_ is a name of your chosing. This will create debugfs
106 * file with a single numeric value that can be use to tweak it. To
107 * remove the entires, just use debugfs_remove_recursive() on 'parent'.
109 * NOTE: remember that even if this will show attached to some
110 * particular instance of a device, the settings are *global*.
112 * On each submodule (for example, .c files), the debug infrastructure
113 * should be included like this:
115 * #define D_SUBMODULE submodule_x // matches one in debug-levels.h
116 * #include "debug-levels.h"
118 * after #including all your include files.
120 * Now you can use the d_*() macros below [d_test(), d_fnstart(),
121 * d_fnend(), d_printf(), d_dump()].
123 * If their debug level is greater than D_MASTER, they will be
126 * If their debug level is lower or equal than D_MASTER but greater
127 * than the current debug level of their submodule, they'll be
130 * Otherwise, the action will be performed.
135 #include <linux/types.h>
136 #include <linux/slab.h>
143 * Debug backend: generate a message header from a 'struct device'
145 * @head: buffer where to place the header
146 * @head_size: length of @head
147 * @dev: pointer to device used to generate a header from. If NULL,
148 * an empty ("") header is generated.
151 void __d_head(char *head, size_t head_size,
156 else if ((unsigned long)dev < 4096) {
157 printk(KERN_ERR "E: Corrupt dev %p\n", dev);
160 snprintf(head, head_size, "%s %s: ",
161 dev_driver_string(dev), dev_name(dev));
166 * Debug backend: log some message if debugging is enabled
168 * @l: intended debug level
169 * @tag: tag to prefix the message with
170 * @dev: 'struct device' associated to this message
171 * @f: printf-like format and arguments
173 * Note this is optimized out if it doesn't pass the compile-time
174 * check; however, it is *always* compiled. This is useful to make
175 * sure the printf-like formats and variables are always checked and
176 * they don't get bit rot if you have all the debugging disabled.
178 #define _d_printf(l, tag, dev, f, a...) \
183 __d_head(head, sizeof(head), dev); \
184 printk(KERN_ERR "%s%s%s: " f, head, __func__, tag, ##a); \
189 * CPP sintatic sugar to generate A_B like symbol names when one of
190 * the arguments is a a preprocessor #define.
192 #define __D_PASTE__(varname, modulename) varname##_##modulename
193 #define __D_PASTE(varname, modulename) (__D_PASTE__(varname, modulename))
194 #define _D_SUBMODULE_INDEX(_name) (D_SUBMODULE_DECLARE(_name))
198 * Store a submodule's runtime debug level and name
207 * List of available submodules and their debug levels
209 * We call them d_level_MODULENAME and d_level_size_MODULENAME; the
210 * macros D_LEVEL and D_LEVEL_SIZE contain the name already for
213 * This array and the size are defined on some .c file that is part of
214 * the current module.
216 #define D_LEVEL __D_PASTE(d_level, D_MODULENAME)
217 #define D_LEVEL_SIZE __D_PASTE(d_level_size, D_MODULENAME)
219 extern struct d_level D_LEVEL[];
220 extern size_t D_LEVEL_SIZE;
227 * Stuff you need to declare prior to using the actual "debug" actions
232 #error D_MODULENAME is not defined in your debug-levels.h file
234 * D_MODULE - Name of the current module
236 * #define in your module's debug-levels.h, making sure it is
237 * unique. This has to be a legal C identifier.
239 #define D_MODULENAME undefined_modulename
244 #warning D_MASTER not defined, but debug.h included! [see docs]
246 * D_MASTER - Compile time maximum debug level
248 * #define in your debug-levels.h file to the maximum debug level the
249 * runtime code will be allowed to have. This allows you to provide a
252 * Anything above that level will be optimized out of the compile.
254 * Defaults to zero (no debug code compiled in).
256 * Maximum one definition per module (at the debug-levels.h file).
262 #error D_SUBMODULE not defined, but debug.h included! [see docs]
264 * D_SUBMODULE - Name of the current submodule
266 * #define in your submodule .c file before #including debug-levels.h
267 * to the name of the current submodule as previously declared and
268 * defined with D_SUBMODULE_DECLARE() (in your module's
269 * debug-levels.h) and D_SUBMODULE_DEFINE().
271 * This is used to provide runtime-control over the debug levels.
273 * Maximum one per .c file! Can be shared among different .c files
274 * (meaning they belong to the same submodule categorization).
276 #define D_SUBMODULE undefined_module
281 * D_SUBMODULE_DECLARE - Declare a submodule for runtime debug level control
283 * @_name: name of the submodule, restricted to the chars that make up a
284 * valid C identifier ([a-zA-Z0-9_]).
286 * Declare in the module's debug-levels.h header file as:
289 * D_SUBMODULE_DECLARE(submodule_1),
290 * D_SUBMODULE_DECLARE(submodule_2),
291 * D_SUBMODULE_DECLARE(submodule_3),
294 * Some corresponding .c file needs to have a matching
295 * D_SUBMODULE_DEFINE().
297 #define D_SUBMODULE_DECLARE(_name) __D_SUBMODULE_##_name
301 * D_SUBMODULE_DEFINE - Define a submodule for runtime debug level control
303 * @_name: name of the submodule, restricted to the chars that make up a
304 * valid C identifier ([a-zA-Z0-9_]).
306 * Use once per module (in some .c file) as:
309 * struct d_level d_level_SUBMODULENAME[] = {
310 * D_SUBMODULE_DEFINE(submodule_1),
311 * D_SUBMODULE_DEFINE(submodule_2),
312 * D_SUBMODULE_DEFINE(submodule_3),
314 * size_t d_level_size_SUBDMODULENAME = ARRAY_SIZE(d_level_SUBDMODULENAME);
316 * Matching D_SUBMODULE_DECLARE()s have to be present in a
317 * debug-levels.h header file.
319 #define D_SUBMODULE_DEFINE(_name) \
320 [__D_SUBMODULE_##_name] = { \
327 /* The actual "debug" operations */
331 * d_test - Returns true if debugging should be enabled
333 * @l: intended debug level (unsigned)
335 * If the master debug switch is enabled and the current settings are
336 * higher or equal to the requested level, then debugging
337 * output/actions should be enabled.
341 * This needs to be coded so that it can be evaluated in compile
342 * time; this is why the ugly BUG_ON() is placed in there, so the
343 * D_MASTER evaluation compiles all out if it is compile-time false.
347 unsigned __l = l; /* type enforcer */ \
350 BUG_ON(_D_SUBMODULE_INDEX(D_SUBMODULE) >= D_LEVEL_SIZE);\
351 D_LEVEL[_D_SUBMODULE_INDEX(D_SUBMODULE)].level >= __l; \
357 * d_fnstart - log message at function start if debugging enabled
359 * @l: intended debug level
360 * @_dev: 'struct device' pointer, NULL if none (for context)
361 * @f: printf-like format and arguments
363 #define d_fnstart(l, _dev, f, a...) _d_printf(l, " FNSTART", _dev, f, ## a)
367 * d_fnend - log message at function end if debugging enabled
369 * @l: intended debug level
370 * @_dev: 'struct device' pointer, NULL if none (for context)
371 * @f: printf-like format and arguments
373 #define d_fnend(l, _dev, f, a...) _d_printf(l, " FNEND", _dev, f, ## a)
377 * d_printf - log message if debugging enabled
379 * @l: intended debug level
380 * @_dev: 'struct device' pointer, NULL if none (for context)
381 * @f: printf-like format and arguments
383 #define d_printf(l, _dev, f, a...) _d_printf(l, "", _dev, f, ## a)
387 * d_dump - log buffer hex dump if debugging enabled
389 * @l: intended debug level
390 * @_dev: 'struct device' pointer, NULL if none (for context)
391 * @f: printf-like format and arguments
393 #define d_dump(l, dev, ptr, size) \
398 __d_head(head, sizeof(head), dev); \
399 print_hex_dump(KERN_ERR, head, 0, 16, 1, \
400 ((void *) ptr), (size), 0); \
405 * Export a submodule's debug level over debugfs as PREFIXSUBMODULE
407 * @prefix: string to prefix the name with
408 * @submodule: name of submodule (not a string, just the name)
409 * @dentry: debugfs parent dentry
411 * Returns: 0 if ok, < 0 errno on error.
413 * For removing, just use debugfs_remove_recursive() on the parent.
415 #define d_level_register_debugfs(prefix, name, parent) \
419 struct dentry *verify_parent_type = parent; \
420 fd = debugfs_create_u8( \
421 prefix #name, 0600, verify_parent_type, \
422 &(D_LEVEL[__D_SUBMODULE_ ## name].level)); \
424 if (IS_ERR(fd) && rc != -ENODEV) \
425 printk(KERN_ERR "%s: Can't create debugfs entry %s: " \
426 "%d\n", __func__, prefix #name, rc); \
434 void d_submodule_set(struct d_level *d_level, size_t d_level_size,
435 const char *submodule, u8 level, const char *tag)
437 struct d_level *itr, *top;
440 for (itr = d_level, top = itr + d_level_size; itr < top; itr++) {
442 if (itr->name == NULL) {
443 printk(KERN_ERR "%s: itr->name NULL?? (%p, #%d)\n",
447 if (!strcmp(itr->name, submodule)) {
452 printk(KERN_ERR "%s: unknown submodule %s\n", tag, submodule);
457 * d_parse_params - Parse a string with debug parameters from the
460 * @d_level: level structure (D_LEVEL)
461 * @d_level_size: number of items in the level structure
463 * @_params: string with the parameters; this is a space (not tab!)
464 * separated list of NAME:VALUE, where value is the debug level
465 * and NAME is the name of the submodule.
466 * @tag: string for error messages (example: MODULE.ARGNAME).
469 void d_parse_params(struct d_level *d_level, size_t d_level_size,
470 const char *_params, const char *tag)
472 char submodule[130], *params, *params_orig, *token, *colon;
473 unsigned level, tokens;
477 params_orig = kstrdup(_params, GFP_KERNEL);
478 params = params_orig;
480 token = strsep(¶ms, " ");
483 if (*token == '\0') /* eat joint spaces */
485 /* kernel's sscanf %s eats until whitespace, so we
486 * replace : by \n so it doesn't get eaten later by
488 colon = strchr(token, ':');
491 tokens = sscanf(token, "%s\n%u", submodule, &level);
493 *colon = ':'; /* set back, for error messages */
495 d_submodule_set(d_level, d_level_size,
496 submodule, level, tag);
498 printk(KERN_ERR "%s: can't parse '%s' as a "
499 "SUBMODULE:LEVEL (%d tokens)\n",
505 #endif /* #ifndef __debug__h__ */