dynamic_debug.c 18 KB

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