1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Linux WiMAX 4 * Collection of tools to manage debug operations. 5 * 6 * Copyright (C) 2005-2007 Intel Corporation 7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> 8 * 9 * Don't #include this file directly, read on! 10 * 11 * EXECUTING DEBUGGING ACTIONS OR NOT 12 * 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. 16 * 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). 21 * 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). 25 * 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. 29 * 30 * DEBUG LEVELS 31 * 32 * It is up to the caller to define how much a debugging level is. 33 * 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. 37 * 38 * USAGE 39 * 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]. 43 * 44 * For each module, there is: 45 * 46 * - a MODULENAME (single word, legal C identifier) 47 * 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. 51 * 52 * - some (optional) .c code to manipulate the runtime debug levels 53 * through debugfs. 54 * 55 * The debug-levels.h file would look like: 56 * 57 * #ifndef __debug_levels__h__ 58 * #define __debug_levels__h__ 59 * 60 * #define D_MODULENAME modulename 61 * #define D_MASTER 10 62 * 63 * #include <linux/wimax/debug.h> 64 * 65 * enum d_module { 66 * D_SUBMODULE_DECLARE(submodule_1), 67 * D_SUBMODULE_DECLARE(submodule_2), 68 * ... 69 * D_SUBMODULE_DECLARE(submodule_N) 70 * }; 71 * 72 * #endif 73 * 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 79 * 80 * We declare N different submodules whose debug level can be 81 * independently controlled during runtime. 82 * 83 * In a .c file of the module (and only in one of them), define the 84 * following code: 85 * 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); 93 * 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. 97 * 98 * To manipulate from user space the levels, create a debugfs dentry 99 * and then register each submodule with: 100 * 101 * d_level_register_debugfs("PREFIX_", submodule_X, parent); 102 * 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'. 106 * 107 * NOTE: remember that even if this will show attached to some 108 * particular instance of a device, the settings are *global*. 109 * 110 * On each submodule (for example, .c files), the debug infrastructure 111 * should be included like this: 112 * 113 * #define D_SUBMODULE submodule_x // matches one in debug-levels.h 114 * #include "debug-levels.h" 115 * 116 * after #including all your include files. 117 * 118 * Now you can use the d_*() macros below [d_test(), d_fnstart(), 119 * d_fnend(), d_printf(), d_dump()]. 120 * 121 * If their debug level is greater than D_MASTER, they will be 122 * compiled out. 123 * 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. 127 * 128 * Otherwise, the action will be performed. 129 */ 130 #ifndef __debug__h__ 131 #define __debug__h__ 132 133 #include <linux/types.h> 134 #include <linux/slab.h> 135 136 struct device; 137 138 /* Backend stuff */ 139 140 /* 141 * Debug backend: generate a message header from a 'struct device' 142 * 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. 147 */ 148 static inline 149 void __d_head(char *head, size_t head_size, 150 struct device *dev) 151 { 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)); 160 } 161 162 163 /* 164 * Debug backend: log some message if debugging is enabled 165 * 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 170 * 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. 175 */ 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) 184 185 186 /* 187 * CPP sintatic sugar to generate A_B like symbol names when one of 188 * the arguments is a a preprocessor #define. 189 */ 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)) 193 194 195 /* 196 * Store a submodule's runtime debug level and name 197 */ 198 struct d_level { 199 u8 level; 200 const char *name; 201 }; 202 203 204 /* 205 * List of available submodules and their debug levels 206 * 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. 210 * 211 * This array and the size are defined on some .c file that is part of 212 * the current module. 213 */ 214 #define D_LEVEL __D_PASTE(d_level, D_MODULENAME) 215 #define D_LEVEL_SIZE __D_PASTE(d_level_size, D_MODULENAME) 216 217 extern struct d_level D_LEVEL[]; 218 extern size_t D_LEVEL_SIZE; 219 220 221 /* 222 * Frontend stuff 223 * 224 * 225 * Stuff you need to declare prior to using the actual "debug" actions 226 * (defined below). 227 */ 228 229 #ifndef D_MODULENAME 230 #error D_MODULENAME is not defined in your debug-levels.h file 231 /** 232 * D_MODULE - Name of the current module 233 * 234 * #define in your module's debug-levels.h, making sure it is 235 * unique. This has to be a legal C identifier. 236 */ 237 #define D_MODULENAME undefined_modulename 238 #endif 239 240 241 #ifndef D_MASTER 242 #warning D_MASTER not defined, but debug.h included! [see docs] 243 /** 244 * D_MASTER - Compile time maximum debug level 245 * 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. 249 * 250 * Anything above that level will be optimized out of the compile. 251 * 252 * Defaults to zero (no debug code compiled in). 253 * 254 * Maximum one definition per module (at the debug-levels.h file). 255 */ 256 #define D_MASTER 0 257 #endif 258 259 #ifndef D_SUBMODULE 260 #error D_SUBMODULE not defined, but debug.h included! [see docs] 261 /** 262 * D_SUBMODULE - Name of the current submodule 263 * 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(). 268 * 269 * This is used to provide runtime-control over the debug levels. 270 * 271 * Maximum one per .c file! Can be shared among different .c files 272 * (meaning they belong to the same submodule categorization). 273 */ 274 #define D_SUBMODULE undefined_module 275 #endif 276 277 278 /** 279 * D_SUBMODULE_DECLARE - Declare a submodule for runtime debug level control 280 * 281 * @_name: name of the submodule, restricted to the chars that make up a 282 * valid C identifier ([a-zA-Z0-9_]). 283 * 284 * Declare in the module's debug-levels.h header file as: 285 * 286 * enum d_module { 287 * D_SUBMODULE_DECLARE(submodule_1), 288 * D_SUBMODULE_DECLARE(submodule_2), 289 * D_SUBMODULE_DECLARE(submodule_3), 290 * }; 291 * 292 * Some corresponding .c file needs to have a matching 293 * D_SUBMODULE_DEFINE(). 294 */ 295 #define D_SUBMODULE_DECLARE(_name) __D_SUBMODULE_##_name 296 297 298 /** 299 * D_SUBMODULE_DEFINE - Define a submodule for runtime debug level control 300 * 301 * @_name: name of the submodule, restricted to the chars that make up a 302 * valid C identifier ([a-zA-Z0-9_]). 303 * 304 * Use once per module (in some .c file) as: 305 * 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); 313 * 314 * Matching D_SUBMODULE_DECLARE()s have to be present in a 315 * debug-levels.h header file. 316 */ 317 #define D_SUBMODULE_DEFINE(_name) \ 318 [__D_SUBMODULE_##_name] = { \ 319 .level = 0, \ 320 .name = #_name \ 321 } 322 323 324 325 /* The actual "debug" operations */ 326 327 328 /** 329 * d_test - Returns true if debugging should be enabled 330 * 331 * @l: intended debug level (unsigned) 332 * 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. 336 * 337 * NOTE: 338 * 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. 342 */ 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 }); \ 351 }) 352 353 354 /** 355 * d_fnstart - log message at function start if debugging enabled 356 * 357 * @l: intended debug level 358 * @_dev: 'struct device' pointer, NULL if none (for context) 359 * @f: printf-like format and arguments 360 */ 361 #define d_fnstart(l, _dev, f, a...) _d_printf(l, " FNSTART", _dev, f, ## a) 362 363 364 /** 365 * d_fnend - log message at function end if debugging enabled 366 * 367 * @l: intended debug level 368 * @_dev: 'struct device' pointer, NULL if none (for context) 369 * @f: printf-like format and arguments 370 */ 371 #define d_fnend(l, _dev, f, a...) _d_printf(l, " FNEND", _dev, f, ## a) 372 373 374 /** 375 * d_printf - log message if debugging enabled 376 * 377 * @l: intended debug level 378 * @_dev: 'struct device' pointer, NULL if none (for context) 379 * @f: printf-like format and arguments 380 */ 381 #define d_printf(l, _dev, f, a...) _d_printf(l, "", _dev, f, ## a) 382 383 384 /** 385 * d_dump - log buffer hex dump if debugging enabled 386 * 387 * @l: intended debug level 388 * @_dev: 'struct device' pointer, NULL if none (for context) 389 * @f: printf-like format and arguments 390 */ 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) 400 401 402 /** 403 * Export a submodule's debug level over debugfs as PREFIXSUBMODULE 404 * 405 * @prefix: string to prefix the name with 406 * @submodule: name of submodule (not a string, just the name) 407 * @dentry: debugfs parent dentry 408 * 409 * For removing, just use debugfs_remove_recursive() on the parent. 410 */ 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)); \ 416 }) 417 418 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) 422 { 423 struct d_level *itr, *top; 424 int index = -1; 425 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; 432 } 433 if (!strcmp(itr->name, submodule)) { 434 itr->level = level; 435 return; 436 } 437 } 438 printk(KERN_ERR "%s: unknown submodule %s\n", tag, submodule); 439 } 440 441 442 /** 443 * d_parse_params - Parse a string with debug parameters from the 444 * command line 445 * 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). 453 */ 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) 457 { 458 char submodule[130], *params, *params_orig, *token, *colon; 459 unsigned level, tokens; 460 461 if (_params == NULL) 462 return; 463 params_orig = kstrdup(_params, GFP_KERNEL); 464 params = params_orig; 465 while (1) { 466 token = strsep(¶ms, " "); 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); 487 } 488 kfree(params_orig); 489 } 490 491 #endif /* #ifndef __debug__h__ */