dynamic_debug.c 18 KB

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