debug.h 15 KB

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