dynamic_debug.c 19 KB

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