dynamic_debug.c 18 KB

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