dynamic_debug.c 19 KB

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