dynamic_debug.c 19 KB

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