dynamic_debug.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. static int ddebug_exec_query(char *query_string)
  386. {
  387. unsigned int flags = 0, mask = 0;
  388. struct ddebug_query query;
  389. #define MAXWORDS 9
  390. int nwords;
  391. char *words[MAXWORDS];
  392. nwords = ddebug_tokenize(query_string, words, MAXWORDS);
  393. if (nwords <= 0)
  394. return -EINVAL;
  395. if (ddebug_parse_query(words, nwords-1, &query))
  396. return -EINVAL;
  397. if (ddebug_parse_flags(words[nwords-1], &flags, &mask))
  398. return -EINVAL;
  399. /* actually go and implement the change */
  400. ddebug_change(&query, flags, mask);
  401. return 0;
  402. }
  403. /*
  404. * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the
  405. * command text from userspace, parses and executes it.
  406. */
  407. static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
  408. size_t len, loff_t *offp)
  409. {
  410. char tmpbuf[256];
  411. int ret;
  412. if (len == 0)
  413. return 0;
  414. /* we don't check *offp -- multiple writes() are allowed */
  415. if (len > sizeof(tmpbuf)-1)
  416. return -E2BIG;
  417. if (copy_from_user(tmpbuf, ubuf, len))
  418. return -EFAULT;
  419. tmpbuf[len] = '\0';
  420. if (verbose)
  421. printk(KERN_INFO "%s: read %d bytes from userspace\n",
  422. __func__, (int)len);
  423. ret = ddebug_exec_query(tmpbuf);
  424. if (ret)
  425. return ret;
  426. *offp += len;
  427. return len;
  428. }
  429. /*
  430. * Set the iterator to point to the first _ddebug object
  431. * and return a pointer to that first object. Returns
  432. * NULL if there are no _ddebugs at all.
  433. */
  434. static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
  435. {
  436. if (list_empty(&ddebug_tables)) {
  437. iter->table = NULL;
  438. iter->idx = 0;
  439. return NULL;
  440. }
  441. iter->table = list_entry(ddebug_tables.next,
  442. struct ddebug_table, link);
  443. iter->idx = 0;
  444. return &iter->table->ddebugs[iter->idx];
  445. }
  446. /*
  447. * Advance the iterator to point to the next _ddebug
  448. * object from the one the iterator currently points at,
  449. * and returns a pointer to the new _ddebug. Returns
  450. * NULL if the iterator has seen all the _ddebugs.
  451. */
  452. static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
  453. {
  454. if (iter->table == NULL)
  455. return NULL;
  456. if (++iter->idx == iter->table->num_ddebugs) {
  457. /* iterate to next table */
  458. iter->idx = 0;
  459. if (list_is_last(&iter->table->link, &ddebug_tables)) {
  460. iter->table = NULL;
  461. return NULL;
  462. }
  463. iter->table = list_entry(iter->table->link.next,
  464. struct ddebug_table, link);
  465. }
  466. return &iter->table->ddebugs[iter->idx];
  467. }
  468. /*
  469. * Seq_ops start method. Called at the start of every
  470. * read() call from userspace. Takes the ddebug_lock and
  471. * seeks the seq_file's iterator to the given position.
  472. */
  473. static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
  474. {
  475. struct ddebug_iter *iter = m->private;
  476. struct _ddebug *dp;
  477. int n = *pos;
  478. if (verbose)
  479. printk(KERN_INFO "%s: called m=%p *pos=%lld\n",
  480. __func__, m, (unsigned long long)*pos);
  481. mutex_lock(&ddebug_lock);
  482. if (!n)
  483. return SEQ_START_TOKEN;
  484. if (n < 0)
  485. return NULL;
  486. dp = ddebug_iter_first(iter);
  487. while (dp != NULL && --n > 0)
  488. dp = ddebug_iter_next(iter);
  489. return dp;
  490. }
  491. /*
  492. * Seq_ops next method. Called several times within a read()
  493. * call from userspace, with ddebug_lock held. Walks to the
  494. * next _ddebug object with a special case for the header line.
  495. */
  496. static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
  497. {
  498. struct ddebug_iter *iter = m->private;
  499. struct _ddebug *dp;
  500. if (verbose)
  501. printk(KERN_INFO "%s: called m=%p p=%p *pos=%lld\n",
  502. __func__, m, p, (unsigned long long)*pos);
  503. if (p == SEQ_START_TOKEN)
  504. dp = ddebug_iter_first(iter);
  505. else
  506. dp = ddebug_iter_next(iter);
  507. ++*pos;
  508. return dp;
  509. }
  510. /*
  511. * Seq_ops show method. Called several times within a read()
  512. * call from userspace, with ddebug_lock held. Formats the
  513. * current _ddebug as a single human-readable line, with a
  514. * special case for the header line.
  515. */
  516. static int ddebug_proc_show(struct seq_file *m, void *p)
  517. {
  518. struct ddebug_iter *iter = m->private;
  519. struct _ddebug *dp = p;
  520. char flagsbuf[8];
  521. if (verbose)
  522. printk(KERN_INFO "%s: called m=%p p=%p\n",
  523. __func__, m, p);
  524. if (p == SEQ_START_TOKEN) {
  525. seq_puts(m,
  526. "# filename:lineno [module]function flags format\n");
  527. return 0;
  528. }
  529. seq_printf(m, "%s:%u [%s]%s %s \"",
  530. dp->filename, dp->lineno,
  531. iter->table->mod_name, dp->function,
  532. ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
  533. seq_escape(m, dp->format, "\t\r\n\"");
  534. seq_puts(m, "\"\n");
  535. return 0;
  536. }
  537. /*
  538. * Seq_ops stop method. Called at the end of each read()
  539. * call from userspace. Drops ddebug_lock.
  540. */
  541. static void ddebug_proc_stop(struct seq_file *m, void *p)
  542. {
  543. if (verbose)
  544. printk(KERN_INFO "%s: called m=%p p=%p\n",
  545. __func__, m, p);
  546. mutex_unlock(&ddebug_lock);
  547. }
  548. static const struct seq_operations ddebug_proc_seqops = {
  549. .start = ddebug_proc_start,
  550. .next = ddebug_proc_next,
  551. .show = ddebug_proc_show,
  552. .stop = ddebug_proc_stop
  553. };
  554. /*
  555. * File_ops->open method for <debugfs>/dynamic_debug/control. Does the seq_file
  556. * setup dance, and also creates an iterator to walk the _ddebugs.
  557. * Note that we create a seq_file always, even for O_WRONLY files
  558. * where it's not needed, as doing so simplifies the ->release method.
  559. */
  560. static int ddebug_proc_open(struct inode *inode, struct file *file)
  561. {
  562. struct ddebug_iter *iter;
  563. int err;
  564. if (verbose)
  565. printk(KERN_INFO "%s: called\n", __func__);
  566. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  567. if (iter == NULL)
  568. return -ENOMEM;
  569. err = seq_open(file, &ddebug_proc_seqops);
  570. if (err) {
  571. kfree(iter);
  572. return err;
  573. }
  574. ((struct seq_file *) file->private_data)->private = iter;
  575. return 0;
  576. }
  577. static const struct file_operations ddebug_proc_fops = {
  578. .owner = THIS_MODULE,
  579. .open = ddebug_proc_open,
  580. .read = seq_read,
  581. .llseek = seq_lseek,
  582. .release = seq_release_private,
  583. .write = ddebug_proc_write
  584. };
  585. /*
  586. * Allocate a new ddebug_table for the given module
  587. * and add it to the global list.
  588. */
  589. int ddebug_add_module(struct _ddebug *tab, unsigned int n,
  590. const char *name)
  591. {
  592. struct ddebug_table *dt;
  593. char *new_name;
  594. dt = kzalloc(sizeof(*dt), GFP_KERNEL);
  595. if (dt == NULL)
  596. return -ENOMEM;
  597. new_name = kstrdup(name, GFP_KERNEL);
  598. if (new_name == NULL) {
  599. kfree(dt);
  600. return -ENOMEM;
  601. }
  602. dt->mod_name = new_name;
  603. dt->num_ddebugs = n;
  604. dt->num_enabled = 0;
  605. dt->ddebugs = tab;
  606. mutex_lock(&ddebug_lock);
  607. list_add_tail(&dt->link, &ddebug_tables);
  608. mutex_unlock(&ddebug_lock);
  609. if (verbose)
  610. printk(KERN_INFO "%u debug prints in module %s\n",
  611. n, dt->mod_name);
  612. return 0;
  613. }
  614. EXPORT_SYMBOL_GPL(ddebug_add_module);
  615. static void ddebug_table_free(struct ddebug_table *dt)
  616. {
  617. list_del_init(&dt->link);
  618. kfree(dt->mod_name);
  619. kfree(dt);
  620. }
  621. /*
  622. * Called in response to a module being unloaded. Removes
  623. * any ddebug_table's which point at the module.
  624. */
  625. int ddebug_remove_module(const char *mod_name)
  626. {
  627. struct ddebug_table *dt, *nextdt;
  628. int ret = -ENOENT;
  629. if (verbose)
  630. printk(KERN_INFO "%s: removing module \"%s\"\n",
  631. __func__, mod_name);
  632. mutex_lock(&ddebug_lock);
  633. list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
  634. if (!strcmp(dt->mod_name, mod_name)) {
  635. ddebug_table_free(dt);
  636. ret = 0;
  637. }
  638. }
  639. mutex_unlock(&ddebug_lock);
  640. return ret;
  641. }
  642. EXPORT_SYMBOL_GPL(ddebug_remove_module);
  643. static void ddebug_remove_all_tables(void)
  644. {
  645. mutex_lock(&ddebug_lock);
  646. while (!list_empty(&ddebug_tables)) {
  647. struct ddebug_table *dt = list_entry(ddebug_tables.next,
  648. struct ddebug_table,
  649. link);
  650. ddebug_table_free(dt);
  651. }
  652. mutex_unlock(&ddebug_lock);
  653. }
  654. static int __init dynamic_debug_init(void)
  655. {
  656. struct dentry *dir, *file;
  657. struct _ddebug *iter, *iter_start;
  658. const char *modname = NULL;
  659. int ret = 0;
  660. int n = 0;
  661. dir = debugfs_create_dir("dynamic_debug", NULL);
  662. if (!dir)
  663. return -ENOMEM;
  664. file = debugfs_create_file("control", 0644, dir, NULL,
  665. &ddebug_proc_fops);
  666. if (!file) {
  667. debugfs_remove(dir);
  668. return -ENOMEM;
  669. }
  670. if (__start___verbose != __stop___verbose) {
  671. iter = __start___verbose;
  672. modname = iter->modname;
  673. iter_start = iter;
  674. for (; iter < __stop___verbose; iter++) {
  675. if (strcmp(modname, iter->modname)) {
  676. ret = ddebug_add_module(iter_start, n, modname);
  677. if (ret)
  678. goto out_free;
  679. n = 0;
  680. modname = iter->modname;
  681. iter_start = iter;
  682. }
  683. n++;
  684. }
  685. ret = ddebug_add_module(iter_start, n, modname);
  686. }
  687. out_free:
  688. if (ret) {
  689. ddebug_remove_all_tables();
  690. debugfs_remove(dir);
  691. debugfs_remove(file);
  692. }
  693. return 0;
  694. }
  695. module_init(dynamic_debug_init);