dynamic_debug.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /*
  2. * lib/dynamic_debug.c
  3. *
  4. * make pr_debug()/dev_dbg() calls runtime configurable based upon their
  5. * source module.
  6. *
  7. * Copyright (C) 2008 Jason Baron <jbaron@redhat.com>
  8. * By Greg Banks <gnb@melbourne.sgi.com>
  9. * Copyright (c) 2008 Silicon Graphics Inc. All Rights Reserved.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/kallsyms.h>
  15. #include <linux/version.h>
  16. #include <linux/types.h>
  17. #include <linux/mutex.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/list.h>
  21. #include <linux/sysctl.h>
  22. #include <linux/ctype.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/dynamic_debug.h>
  25. #include <linux/debugfs.h>
  26. extern struct _ddebug __start___verbose[];
  27. extern struct _ddebug __stop___verbose[];
  28. /* dynamic_debug_enabled, and dynamic_debug_enabled2 are bitmasks in which
  29. * bit n is set to 1 if any modname hashes into the bucket n, 0 otherwise. They
  30. * use independent hash functions, to reduce the chance of false positives.
  31. */
  32. long long dynamic_debug_enabled;
  33. EXPORT_SYMBOL_GPL(dynamic_debug_enabled);
  34. long long dynamic_debug_enabled2;
  35. EXPORT_SYMBOL_GPL(dynamic_debug_enabled2);
  36. struct ddebug_table {
  37. struct list_head link;
  38. char *mod_name;
  39. unsigned int num_ddebugs;
  40. unsigned int num_enabled;
  41. struct _ddebug *ddebugs;
  42. };
  43. struct ddebug_query {
  44. const char *filename;
  45. const char *module;
  46. const char *function;
  47. const char *format;
  48. unsigned int first_lineno, last_lineno;
  49. };
  50. struct ddebug_iter {
  51. struct ddebug_table *table;
  52. unsigned int idx;
  53. };
  54. static DEFINE_MUTEX(ddebug_lock);
  55. static LIST_HEAD(ddebug_tables);
  56. static int verbose = 0;
  57. /* Return the last part of a pathname */
  58. static inline const char *basename(const char *path)
  59. {
  60. const char *tail = strrchr(path, '/');
  61. return tail ? tail+1 : path;
  62. }
  63. /* format a string into buf[] which describes the _ddebug's flags */
  64. static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,
  65. size_t maxlen)
  66. {
  67. char *p = buf;
  68. BUG_ON(maxlen < 4);
  69. if (dp->flags & _DPRINTK_FLAGS_PRINT)
  70. *p++ = 'p';
  71. if (p == buf)
  72. *p++ = '-';
  73. *p = '\0';
  74. return buf;
  75. }
  76. /*
  77. * must be called with ddebug_lock held
  78. */
  79. static int disabled_hash(char hash, bool first_table)
  80. {
  81. struct ddebug_table *dt;
  82. char table_hash_value;
  83. list_for_each_entry(dt, &ddebug_tables, link) {
  84. if (first_table)
  85. table_hash_value = dt->ddebugs->primary_hash;
  86. else
  87. table_hash_value = dt->ddebugs->secondary_hash;
  88. if (dt->num_enabled && (hash == table_hash_value))
  89. return 0;
  90. }
  91. return 1;
  92. }
  93. /*
  94. * Search the tables for _ddebug's which match the given
  95. * `query' and apply the `flags' and `mask' to them. Tells
  96. * the user which ddebug's were changed, or whether none
  97. * were matched.
  98. */
  99. static void ddebug_change(const struct ddebug_query *query,
  100. unsigned int flags, unsigned int mask)
  101. {
  102. int i;
  103. struct ddebug_table *dt;
  104. unsigned int newflags;
  105. unsigned int nfound = 0;
  106. char flagbuf[8];
  107. /* search for matching ddebugs */
  108. mutex_lock(&ddebug_lock);
  109. list_for_each_entry(dt, &ddebug_tables, link) {
  110. /* match against the module name */
  111. if (query->module != NULL &&
  112. strcmp(query->module, dt->mod_name))
  113. continue;
  114. for (i = 0 ; i < dt->num_ddebugs ; i++) {
  115. struct _ddebug *dp = &dt->ddebugs[i];
  116. /* match against the source filename */
  117. if (query->filename != NULL &&
  118. strcmp(query->filename, dp->filename) &&
  119. strcmp(query->filename, basename(dp->filename)))
  120. continue;
  121. /* match against the function */
  122. if (query->function != NULL &&
  123. strcmp(query->function, dp->function))
  124. continue;
  125. /* match against the format */
  126. if (query->format != NULL &&
  127. strstr(dp->format, query->format) == NULL)
  128. continue;
  129. /* match against the line number range */
  130. if (query->first_lineno &&
  131. dp->lineno < query->first_lineno)
  132. continue;
  133. if (query->last_lineno &&
  134. dp->lineno > query->last_lineno)
  135. continue;
  136. nfound++;
  137. newflags = (dp->flags & mask) | flags;
  138. if (newflags == dp->flags)
  139. continue;
  140. if (!newflags)
  141. dt->num_enabled--;
  142. else if (!dp-flags)
  143. dt->num_enabled++;
  144. dp->flags = newflags;
  145. if (newflags) {
  146. dynamic_debug_enabled |=
  147. (1LL << dp->primary_hash);
  148. dynamic_debug_enabled2 |=
  149. (1LL << dp->secondary_hash);
  150. } else {
  151. if (disabled_hash(dp->primary_hash, true))
  152. dynamic_debug_enabled &=
  153. ~(1LL << dp->primary_hash);
  154. if (disabled_hash(dp->secondary_hash, false))
  155. dynamic_debug_enabled2 &=
  156. ~(1LL << dp->secondary_hash);
  157. }
  158. if (verbose)
  159. printk(KERN_INFO
  160. "ddebug: changed %s:%d [%s]%s %s\n",
  161. dp->filename, dp->lineno,
  162. dt->mod_name, dp->function,
  163. ddebug_describe_flags(dp, flagbuf,
  164. sizeof(flagbuf)));
  165. }
  166. }
  167. mutex_unlock(&ddebug_lock);
  168. if (!nfound && verbose)
  169. printk(KERN_INFO "ddebug: no matches for query\n");
  170. }
  171. /*
  172. * Wrapper around strsep() to collapse the multiple empty tokens
  173. * that it returns when fed sequences of separator characters.
  174. * Now, if we had strtok_r()...
  175. */
  176. static inline char *nearly_strtok_r(char **p, const char *sep)
  177. {
  178. char *r;
  179. while ((r = strsep(p, sep)) != NULL && *r == '\0')
  180. ;
  181. return r;
  182. }
  183. /*
  184. * Split the buffer `buf' into space-separated words.
  185. * Return the number of such words or <0 on error.
  186. */
  187. static int ddebug_tokenize(char *buf, char *words[], int maxwords)
  188. {
  189. int nwords = 0;
  190. while (nwords < maxwords &&
  191. (words[nwords] = nearly_strtok_r(&buf, " \t\r\n")) != NULL)
  192. nwords++;
  193. if (buf)
  194. return -EINVAL; /* ran out of words[] before bytes */
  195. if (verbose) {
  196. int i;
  197. printk(KERN_INFO "%s: split into words:", __func__);
  198. for (i = 0 ; i < nwords ; i++)
  199. printk(" \"%s\"", words[i]);
  200. printk("\n");
  201. }
  202. return nwords;
  203. }
  204. /*
  205. * Parse a single line number. Note that the empty string ""
  206. * is treated as a special case and converted to zero, which
  207. * is later treated as a "don't care" value.
  208. */
  209. static inline int parse_lineno(const char *str, unsigned int *val)
  210. {
  211. char *end = NULL;
  212. BUG_ON(str == NULL);
  213. if (*str == '\0') {
  214. *val = 0;
  215. return 0;
  216. }
  217. *val = simple_strtoul(str, &end, 10);
  218. return end == NULL || end == str || *end != '\0' ? -EINVAL : 0;
  219. }
  220. /*
  221. * Undo octal escaping in a string, inplace. This is useful to
  222. * allow the user to express a query which matches a format
  223. * containing embedded spaces.
  224. */
  225. #define isodigit(c) ((c) >= '0' && (c) <= '7')
  226. static char *unescape(char *str)
  227. {
  228. char *in = str;
  229. char *out = str;
  230. while (*in) {
  231. if (*in == '\\') {
  232. if (in[1] == '\\') {
  233. *out++ = '\\';
  234. in += 2;
  235. continue;
  236. } else if (in[1] == 't') {
  237. *out++ = '\t';
  238. in += 2;
  239. continue;
  240. } else if (in[1] == 'n') {
  241. *out++ = '\n';
  242. in += 2;
  243. continue;
  244. } else if (isodigit(in[1]) &&
  245. isodigit(in[2]) &&
  246. isodigit(in[3])) {
  247. *out++ = ((in[1] - '0')<<6) |
  248. ((in[2] - '0')<<3) |
  249. (in[3] - '0');
  250. in += 4;
  251. continue;
  252. }
  253. }
  254. *out++ = *in++;
  255. }
  256. *out = '\0';
  257. return str;
  258. }
  259. /*
  260. * Parse words[] as a ddebug query specification, which is a series
  261. * of (keyword, value) pairs chosen from these possibilities:
  262. *
  263. * func <function-name>
  264. * file <full-pathname>
  265. * file <base-filename>
  266. * module <module-name>
  267. * format <escaped-string-to-find-in-format>
  268. * line <lineno>
  269. * line <first-lineno>-<last-lineno> // where either may be empty
  270. */
  271. static int ddebug_parse_query(char *words[], int nwords,
  272. struct ddebug_query *query)
  273. {
  274. unsigned int i;
  275. /* check we have an even number of words */
  276. if (nwords % 2 != 0)
  277. return -EINVAL;
  278. memset(query, 0, sizeof(*query));
  279. for (i = 0 ; i < nwords ; i += 2) {
  280. if (!strcmp(words[i], "func"))
  281. query->function = words[i+1];
  282. else if (!strcmp(words[i], "file"))
  283. query->filename = words[i+1];
  284. else if (!strcmp(words[i], "module"))
  285. query->module = words[i+1];
  286. else if (!strcmp(words[i], "format"))
  287. query->format = unescape(words[i+1]);
  288. else if (!strcmp(words[i], "line")) {
  289. char *first = words[i+1];
  290. char *last = strchr(first, '-');
  291. if (last)
  292. *last++ = '\0';
  293. if (parse_lineno(first, &query->first_lineno) < 0)
  294. return -EINVAL;
  295. if (last != NULL) {
  296. /* range <first>-<last> */
  297. if (parse_lineno(last, &query->last_lineno) < 0)
  298. return -EINVAL;
  299. } else {
  300. query->last_lineno = query->first_lineno;
  301. }
  302. } else {
  303. if (verbose)
  304. printk(KERN_ERR "%s: unknown keyword \"%s\"\n",
  305. __func__, words[i]);
  306. return -EINVAL;
  307. }
  308. }
  309. if (verbose)
  310. printk(KERN_INFO "%s: q->function=\"%s\" q->filename=\"%s\" "
  311. "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n",
  312. __func__, query->function, query->filename,
  313. query->module, query->format, query->first_lineno,
  314. query->last_lineno);
  315. return 0;
  316. }
  317. /*
  318. * Parse `str' as a flags specification, format [-+=][p]+.
  319. * Sets up *maskp and *flagsp to be used when changing the
  320. * flags fields of matched _ddebug's. Returns 0 on success
  321. * or <0 on error.
  322. */
  323. static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
  324. unsigned int *maskp)
  325. {
  326. unsigned flags = 0;
  327. int op = '=';
  328. switch (*str) {
  329. case '+':
  330. case '-':
  331. case '=':
  332. op = *str++;
  333. break;
  334. default:
  335. return -EINVAL;
  336. }
  337. if (verbose)
  338. printk(KERN_INFO "%s: op='%c'\n", __func__, op);
  339. for ( ; *str ; ++str) {
  340. switch (*str) {
  341. case 'p':
  342. flags |= _DPRINTK_FLAGS_PRINT;
  343. break;
  344. default:
  345. return -EINVAL;
  346. }
  347. }
  348. if (flags == 0)
  349. return -EINVAL;
  350. if (verbose)
  351. printk(KERN_INFO "%s: flags=0x%x\n", __func__, flags);
  352. /* calculate final *flagsp, *maskp according to mask and op */
  353. switch (op) {
  354. case '=':
  355. *maskp = 0;
  356. *flagsp = flags;
  357. break;
  358. case '+':
  359. *maskp = ~0U;
  360. *flagsp = flags;
  361. break;
  362. case '-':
  363. *maskp = ~flags;
  364. *flagsp = 0;
  365. break;
  366. }
  367. if (verbose)
  368. printk(KERN_INFO "%s: *flagsp=0x%x *maskp=0x%x\n",
  369. __func__, *flagsp, *maskp);
  370. return 0;
  371. }
  372. /*
  373. * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the
  374. * command text from userspace, parses and executes it.
  375. */
  376. static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
  377. size_t len, loff_t *offp)
  378. {
  379. unsigned int flags = 0, mask = 0;
  380. struct ddebug_query query;
  381. #define MAXWORDS 9
  382. int nwords;
  383. char *words[MAXWORDS];
  384. char tmpbuf[256];
  385. if (len == 0)
  386. return 0;
  387. /* we don't check *offp -- multiple writes() are allowed */
  388. if (len > sizeof(tmpbuf)-1)
  389. return -E2BIG;
  390. if (copy_from_user(tmpbuf, ubuf, len))
  391. return -EFAULT;
  392. tmpbuf[len] = '\0';
  393. if (verbose)
  394. printk(KERN_INFO "%s: read %d bytes from userspace\n",
  395. __func__, (int)len);
  396. nwords = ddebug_tokenize(tmpbuf, words, MAXWORDS);
  397. if (nwords < 0)
  398. return -EINVAL;
  399. if (ddebug_parse_query(words, nwords-1, &query))
  400. return -EINVAL;
  401. if (ddebug_parse_flags(words[nwords-1], &flags, &mask))
  402. return -EINVAL;
  403. /* actually go and implement the change */
  404. ddebug_change(&query, flags, mask);
  405. *offp += len;
  406. return len;
  407. }
  408. /*
  409. * Set the iterator to point to the first _ddebug object
  410. * and return a pointer to that first object. Returns
  411. * NULL if there are no _ddebugs at all.
  412. */
  413. static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
  414. {
  415. if (list_empty(&ddebug_tables)) {
  416. iter->table = NULL;
  417. iter->idx = 0;
  418. return NULL;
  419. }
  420. iter->table = list_entry(ddebug_tables.next,
  421. struct ddebug_table, link);
  422. iter->idx = 0;
  423. return &iter->table->ddebugs[iter->idx];
  424. }
  425. /*
  426. * Advance the iterator to point to the next _ddebug
  427. * object from the one the iterator currently points at,
  428. * and returns a pointer to the new _ddebug. Returns
  429. * NULL if the iterator has seen all the _ddebugs.
  430. */
  431. static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
  432. {
  433. if (iter->table == NULL)
  434. return NULL;
  435. if (++iter->idx == iter->table->num_ddebugs) {
  436. /* iterate to next table */
  437. iter->idx = 0;
  438. if (list_is_last(&iter->table->link, &ddebug_tables)) {
  439. iter->table = NULL;
  440. return NULL;
  441. }
  442. iter->table = list_entry(iter->table->link.next,
  443. struct ddebug_table, link);
  444. }
  445. return &iter->table->ddebugs[iter->idx];
  446. }
  447. /*
  448. * Seq_ops start method. Called at the start of every
  449. * read() call from userspace. Takes the ddebug_lock and
  450. * seeks the seq_file's iterator to the given position.
  451. */
  452. static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
  453. {
  454. struct ddebug_iter *iter = m->private;
  455. struct _ddebug *dp;
  456. int n = *pos;
  457. if (verbose)
  458. printk(KERN_INFO "%s: called m=%p *pos=%lld\n",
  459. __func__, m, (unsigned long long)*pos);
  460. mutex_lock(&ddebug_lock);
  461. if (!n)
  462. return SEQ_START_TOKEN;
  463. if (n < 0)
  464. return NULL;
  465. dp = ddebug_iter_first(iter);
  466. while (dp != NULL && --n > 0)
  467. dp = ddebug_iter_next(iter);
  468. return dp;
  469. }
  470. /*
  471. * Seq_ops next method. Called several times within a read()
  472. * call from userspace, with ddebug_lock held. Walks to the
  473. * next _ddebug object with a special case for the header line.
  474. */
  475. static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
  476. {
  477. struct ddebug_iter *iter = m->private;
  478. struct _ddebug *dp;
  479. if (verbose)
  480. printk(KERN_INFO "%s: called m=%p p=%p *pos=%lld\n",
  481. __func__, m, p, (unsigned long long)*pos);
  482. if (p == SEQ_START_TOKEN)
  483. dp = ddebug_iter_first(iter);
  484. else
  485. dp = ddebug_iter_next(iter);
  486. ++*pos;
  487. return dp;
  488. }
  489. /*
  490. * Seq_ops show method. Called several times within a read()
  491. * call from userspace, with ddebug_lock held. Formats the
  492. * current _ddebug as a single human-readable line, with a
  493. * special case for the header line.
  494. */
  495. static int ddebug_proc_show(struct seq_file *m, void *p)
  496. {
  497. struct ddebug_iter *iter = m->private;
  498. struct _ddebug *dp = p;
  499. char flagsbuf[8];
  500. if (verbose)
  501. printk(KERN_INFO "%s: called m=%p p=%p\n",
  502. __func__, m, p);
  503. if (p == SEQ_START_TOKEN) {
  504. seq_puts(m,
  505. "# filename:lineno [module]function flags format\n");
  506. return 0;
  507. }
  508. seq_printf(m, "%s:%u [%s]%s %s \"",
  509. dp->filename, dp->lineno,
  510. iter->table->mod_name, dp->function,
  511. ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
  512. seq_escape(m, dp->format, "\t\r\n\"");
  513. seq_puts(m, "\"\n");
  514. return 0;
  515. }
  516. /*
  517. * Seq_ops stop method. Called at the end of each read()
  518. * call from userspace. Drops ddebug_lock.
  519. */
  520. static void ddebug_proc_stop(struct seq_file *m, void *p)
  521. {
  522. if (verbose)
  523. printk(KERN_INFO "%s: called m=%p p=%p\n",
  524. __func__, m, p);
  525. mutex_unlock(&ddebug_lock);
  526. }
  527. static const struct seq_operations ddebug_proc_seqops = {
  528. .start = ddebug_proc_start,
  529. .next = ddebug_proc_next,
  530. .show = ddebug_proc_show,
  531. .stop = ddebug_proc_stop
  532. };
  533. /*
  534. * File_ops->open method for <debugfs>/dynamic_debug/control. Does the seq_file
  535. * setup dance, and also creates an iterator to walk the _ddebugs.
  536. * Note that we create a seq_file always, even for O_WRONLY files
  537. * where it's not needed, as doing so simplifies the ->release method.
  538. */
  539. static int ddebug_proc_open(struct inode *inode, struct file *file)
  540. {
  541. struct ddebug_iter *iter;
  542. int err;
  543. if (verbose)
  544. printk(KERN_INFO "%s: called\n", __func__);
  545. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  546. if (iter == NULL)
  547. return -ENOMEM;
  548. err = seq_open(file, &ddebug_proc_seqops);
  549. if (err) {
  550. kfree(iter);
  551. return err;
  552. }
  553. ((struct seq_file *) file->private_data)->private = iter;
  554. return 0;
  555. }
  556. static const struct file_operations ddebug_proc_fops = {
  557. .owner = THIS_MODULE,
  558. .open = ddebug_proc_open,
  559. .read = seq_read,
  560. .llseek = seq_lseek,
  561. .release = seq_release_private,
  562. .write = ddebug_proc_write
  563. };
  564. /*
  565. * Allocate a new ddebug_table for the given module
  566. * and add it to the global list.
  567. */
  568. int ddebug_add_module(struct _ddebug *tab, unsigned int n,
  569. const char *name)
  570. {
  571. struct ddebug_table *dt;
  572. char *new_name;
  573. dt = kzalloc(sizeof(*dt), GFP_KERNEL);
  574. if (dt == NULL)
  575. return -ENOMEM;
  576. new_name = kstrdup(name, GFP_KERNEL);
  577. if (new_name == NULL) {
  578. kfree(dt);
  579. return -ENOMEM;
  580. }
  581. dt->mod_name = new_name;
  582. dt->num_ddebugs = n;
  583. dt->num_enabled = 0;
  584. dt->ddebugs = tab;
  585. mutex_lock(&ddebug_lock);
  586. list_add_tail(&dt->link, &ddebug_tables);
  587. mutex_unlock(&ddebug_lock);
  588. if (verbose)
  589. printk(KERN_INFO "%u debug prints in module %s\n",
  590. n, dt->mod_name);
  591. return 0;
  592. }
  593. EXPORT_SYMBOL_GPL(ddebug_add_module);
  594. static void ddebug_table_free(struct ddebug_table *dt)
  595. {
  596. list_del_init(&dt->link);
  597. kfree(dt->mod_name);
  598. kfree(dt);
  599. }
  600. /*
  601. * Called in response to a module being unloaded. Removes
  602. * any ddebug_table's which point at the module.
  603. */
  604. int ddebug_remove_module(char *mod_name)
  605. {
  606. struct ddebug_table *dt, *nextdt;
  607. int ret = -ENOENT;
  608. if (verbose)
  609. printk(KERN_INFO "%s: removing module \"%s\"\n",
  610. __func__, mod_name);
  611. mutex_lock(&ddebug_lock);
  612. list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
  613. if (!strcmp(dt->mod_name, mod_name)) {
  614. ddebug_table_free(dt);
  615. ret = 0;
  616. }
  617. }
  618. mutex_unlock(&ddebug_lock);
  619. return ret;
  620. }
  621. EXPORT_SYMBOL_GPL(ddebug_remove_module);
  622. static void ddebug_remove_all_tables(void)
  623. {
  624. mutex_lock(&ddebug_lock);
  625. while (!list_empty(&ddebug_tables)) {
  626. struct ddebug_table *dt = list_entry(ddebug_tables.next,
  627. struct ddebug_table,
  628. link);
  629. ddebug_table_free(dt);
  630. }
  631. mutex_unlock(&ddebug_lock);
  632. }
  633. static int __init dynamic_debug_init(void)
  634. {
  635. struct dentry *dir, *file;
  636. struct _ddebug *iter, *iter_start;
  637. const char *modname = NULL;
  638. int ret = 0;
  639. int n = 0;
  640. dir = debugfs_create_dir("dynamic_debug", NULL);
  641. if (!dir)
  642. return -ENOMEM;
  643. file = debugfs_create_file("control", 0644, dir, NULL,
  644. &ddebug_proc_fops);
  645. if (!file) {
  646. debugfs_remove(dir);
  647. return -ENOMEM;
  648. }
  649. if (__start___verbose != __stop___verbose) {
  650. iter = __start___verbose;
  651. modname = iter->modname;
  652. iter_start = iter;
  653. for (; iter < __stop___verbose; iter++) {
  654. if (strcmp(modname, iter->modname)) {
  655. ret = ddebug_add_module(iter_start, n, modname);
  656. if (ret)
  657. goto out_free;
  658. n = 0;
  659. modname = iter->modname;
  660. iter_start = iter;
  661. }
  662. n++;
  663. }
  664. ret = ddebug_add_module(iter_start, n, modname);
  665. }
  666. out_free:
  667. if (ret) {
  668. ddebug_remove_all_tables();
  669. debugfs_remove(dir);
  670. debugfs_remove(file);
  671. }
  672. return 0;
  673. }
  674. module_init(dynamic_debug_init);