dynamic_debug.c 20 KB

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