dynamic_debug.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. #include <linux/device.h>
  33. extern struct _ddebug __start___verbose[];
  34. extern struct _ddebug __stop___verbose[];
  35. struct ddebug_table {
  36. struct list_head link;
  37. char *mod_name;
  38. unsigned int num_ddebugs;
  39. unsigned int num_enabled;
  40. struct _ddebug *ddebugs;
  41. };
  42. struct ddebug_query {
  43. const char *filename;
  44. const char *module;
  45. const char *function;
  46. const char *format;
  47. unsigned int first_lineno, last_lineno;
  48. };
  49. struct ddebug_iter {
  50. struct ddebug_table *table;
  51. unsigned int idx;
  52. };
  53. static DEFINE_MUTEX(ddebug_lock);
  54. static LIST_HEAD(ddebug_tables);
  55. static int verbose = 0;
  56. /* Return the last part of a pathname */
  57. static inline const char *basename(const char *path)
  58. {
  59. const char *tail = strrchr(path, '/');
  60. return tail ? tail+1 : path;
  61. }
  62. static struct { unsigned flag:8; char opt_char; } opt_array[] = {
  63. { _DPRINTK_FLAGS_PRINT, 'p' },
  64. { _DPRINTK_FLAGS_INCL_MODNAME, 'm' },
  65. { _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' },
  66. { _DPRINTK_FLAGS_INCL_LINENO, 'l' },
  67. { _DPRINTK_FLAGS_INCL_TID, 't' },
  68. };
  69. /* format a string into buf[] which describes the _ddebug's flags */
  70. static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,
  71. size_t maxlen)
  72. {
  73. char *p = buf;
  74. int i;
  75. BUG_ON(maxlen < 4);
  76. for (i = 0; i < ARRAY_SIZE(opt_array); ++i)
  77. if (dp->flags & opt_array[i].flag)
  78. *p++ = opt_array[i].opt_char;
  79. if (p == buf)
  80. *p++ = '-';
  81. *p = '\0';
  82. return buf;
  83. }
  84. /*
  85. * Search the tables for _ddebug's which match the given
  86. * `query' and apply the `flags' and `mask' to them. Tells
  87. * the user which ddebug's were changed, or whether none
  88. * were matched.
  89. */
  90. static void ddebug_change(const struct ddebug_query *query,
  91. unsigned int flags, unsigned int mask)
  92. {
  93. int i;
  94. struct ddebug_table *dt;
  95. unsigned int newflags;
  96. unsigned int nfound = 0;
  97. char flagbuf[8];
  98. /* search for matching ddebugs */
  99. mutex_lock(&ddebug_lock);
  100. list_for_each_entry(dt, &ddebug_tables, link) {
  101. /* match against the module name */
  102. if (query->module != NULL &&
  103. strcmp(query->module, dt->mod_name))
  104. continue;
  105. for (i = 0 ; i < dt->num_ddebugs ; i++) {
  106. struct _ddebug *dp = &dt->ddebugs[i];
  107. /* match against the source filename */
  108. if (query->filename != NULL &&
  109. strcmp(query->filename, dp->filename) &&
  110. strcmp(query->filename, basename(dp->filename)))
  111. continue;
  112. /* match against the function */
  113. if (query->function != NULL &&
  114. strcmp(query->function, dp->function))
  115. continue;
  116. /* match against the format */
  117. if (query->format != NULL &&
  118. strstr(dp->format, query->format) == NULL)
  119. continue;
  120. /* match against the line number range */
  121. if (query->first_lineno &&
  122. dp->lineno < query->first_lineno)
  123. continue;
  124. if (query->last_lineno &&
  125. dp->lineno > query->last_lineno)
  126. continue;
  127. nfound++;
  128. newflags = (dp->flags & mask) | flags;
  129. if (newflags == dp->flags)
  130. continue;
  131. if (!newflags)
  132. dt->num_enabled--;
  133. else if (!dp->flags)
  134. dt->num_enabled++;
  135. dp->flags = newflags;
  136. if (newflags)
  137. dp->enabled = 1;
  138. else
  139. dp->enabled = 0;
  140. if (verbose)
  141. printk(KERN_INFO
  142. "ddebug: 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. printk(KERN_INFO "ddebug: 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. printk(KERN_INFO "%s: split into words:", __func__);
  191. for (i = 0 ; i < nwords ; i++)
  192. printk(" \"%s\"", words[i]);
  193. printk("\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. printk(KERN_ERR "%s: unknown keyword \"%s\"\n",
  298. __func__, words[i]);
  299. return -EINVAL;
  300. }
  301. }
  302. if (verbose)
  303. printk(KERN_INFO "%s: q->function=\"%s\" q->filename=\"%s\" "
  304. "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n",
  305. __func__, query->function, query->filename,
  306. query->module, query->format, query->first_lineno,
  307. query->last_lineno);
  308. return 0;
  309. }
  310. /*
  311. * Parse `str' as a flags specification, format [-+=][p]+.
  312. * Sets up *maskp and *flagsp to be used when changing the
  313. * flags fields of matched _ddebug's. Returns 0 on success
  314. * or <0 on error.
  315. */
  316. static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
  317. unsigned int *maskp)
  318. {
  319. unsigned flags = 0;
  320. int op = '=', i;
  321. switch (*str) {
  322. case '+':
  323. case '-':
  324. case '=':
  325. op = *str++;
  326. break;
  327. default:
  328. return -EINVAL;
  329. }
  330. if (verbose)
  331. printk(KERN_INFO "%s: op='%c'\n", __func__, op);
  332. for ( ; *str ; ++str) {
  333. for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) {
  334. if (*str == opt_array[i].opt_char) {
  335. flags |= opt_array[i].flag;
  336. break;
  337. }
  338. }
  339. if (i < 0)
  340. return -EINVAL;
  341. }
  342. if (flags == 0)
  343. return -EINVAL;
  344. if (verbose)
  345. printk(KERN_INFO "%s: flags=0x%x\n", __func__, flags);
  346. /* calculate final *flagsp, *maskp according to mask and op */
  347. switch (op) {
  348. case '=':
  349. *maskp = 0;
  350. *flagsp = flags;
  351. break;
  352. case '+':
  353. *maskp = ~0U;
  354. *flagsp = flags;
  355. break;
  356. case '-':
  357. *maskp = ~flags;
  358. *flagsp = 0;
  359. break;
  360. }
  361. if (verbose)
  362. printk(KERN_INFO "%s: *flagsp=0x%x *maskp=0x%x\n",
  363. __func__, *flagsp, *maskp);
  364. return 0;
  365. }
  366. static int ddebug_exec_query(char *query_string)
  367. {
  368. unsigned int flags = 0, mask = 0;
  369. struct ddebug_query query;
  370. #define MAXWORDS 9
  371. int nwords;
  372. char *words[MAXWORDS];
  373. nwords = ddebug_tokenize(query_string, words, MAXWORDS);
  374. if (nwords <= 0)
  375. return -EINVAL;
  376. if (ddebug_parse_query(words, nwords-1, &query))
  377. return -EINVAL;
  378. if (ddebug_parse_flags(words[nwords-1], &flags, &mask))
  379. return -EINVAL;
  380. /* actually go and implement the change */
  381. ddebug_change(&query, flags, mask);
  382. return 0;
  383. }
  384. static int dynamic_emit_prefix(const struct _ddebug *descriptor)
  385. {
  386. char tid[sizeof(int) + sizeof(int)/2 + 4];
  387. char lineno[sizeof(int) + sizeof(int)/2];
  388. if (descriptor->flags & _DPRINTK_FLAGS_INCL_TID) {
  389. if (in_interrupt())
  390. snprintf(tid, sizeof(tid), "%s", "<intr> ");
  391. else
  392. snprintf(tid, sizeof(tid), "[%d] ",
  393. task_pid_vnr(current));
  394. } else {
  395. tid[0] = 0;
  396. }
  397. if (descriptor->flags & _DPRINTK_FLAGS_INCL_LINENO)
  398. snprintf(lineno, sizeof(lineno), "%d", descriptor->lineno);
  399. else
  400. lineno[0] = 0;
  401. return printk(KERN_DEBUG "%s%s%s%s%s%s",
  402. tid,
  403. (descriptor->flags & _DPRINTK_FLAGS_INCL_MODNAME) ?
  404. descriptor->modname : "",
  405. (descriptor->flags & _DPRINTK_FLAGS_INCL_MODNAME) ?
  406. ":" : "",
  407. (descriptor->flags & _DPRINTK_FLAGS_INCL_FUNCNAME) ?
  408. descriptor->function : "",
  409. (descriptor->flags & _DPRINTK_FLAGS_INCL_FUNCNAME) ?
  410. ":" : "",
  411. lineno);
  412. }
  413. int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
  414. {
  415. va_list args;
  416. int res;
  417. BUG_ON(!descriptor);
  418. BUG_ON(!fmt);
  419. va_start(args, fmt);
  420. res = dynamic_emit_prefix(descriptor);
  421. res += vprintk(fmt, args);
  422. va_end(args);
  423. return res;
  424. }
  425. EXPORT_SYMBOL(__dynamic_pr_debug);
  426. int __dynamic_dev_dbg(struct _ddebug *descriptor,
  427. const struct device *dev, const char *fmt, ...)
  428. {
  429. struct va_format vaf;
  430. va_list args;
  431. int res;
  432. BUG_ON(!descriptor);
  433. BUG_ON(!fmt);
  434. va_start(args, fmt);
  435. vaf.fmt = fmt;
  436. vaf.va = &args;
  437. res = dynamic_emit_prefix(descriptor);
  438. res += __dev_printk(KERN_CONT, dev, &vaf);
  439. va_end(args);
  440. return res;
  441. }
  442. EXPORT_SYMBOL(__dynamic_dev_dbg);
  443. static __initdata char ddebug_setup_string[1024];
  444. static __init int ddebug_setup_query(char *str)
  445. {
  446. if (strlen(str) >= 1024) {
  447. pr_warning("ddebug boot param string too large\n");
  448. return 0;
  449. }
  450. strcpy(ddebug_setup_string, str);
  451. return 1;
  452. }
  453. __setup("ddebug_query=", ddebug_setup_query);
  454. /*
  455. * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the
  456. * command text from userspace, parses and executes it.
  457. */
  458. static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
  459. size_t len, loff_t *offp)
  460. {
  461. char tmpbuf[256];
  462. int ret;
  463. if (len == 0)
  464. return 0;
  465. /* we don't check *offp -- multiple writes() are allowed */
  466. if (len > sizeof(tmpbuf)-1)
  467. return -E2BIG;
  468. if (copy_from_user(tmpbuf, ubuf, len))
  469. return -EFAULT;
  470. tmpbuf[len] = '\0';
  471. if (verbose)
  472. printk(KERN_INFO "%s: read %d bytes from userspace\n",
  473. __func__, (int)len);
  474. ret = ddebug_exec_query(tmpbuf);
  475. if (ret)
  476. return ret;
  477. *offp += len;
  478. return len;
  479. }
  480. /*
  481. * Set the iterator to point to the first _ddebug object
  482. * and return a pointer to that first object. Returns
  483. * NULL if there are no _ddebugs at all.
  484. */
  485. static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
  486. {
  487. if (list_empty(&ddebug_tables)) {
  488. iter->table = NULL;
  489. iter->idx = 0;
  490. return NULL;
  491. }
  492. iter->table = list_entry(ddebug_tables.next,
  493. struct ddebug_table, link);
  494. iter->idx = 0;
  495. return &iter->table->ddebugs[iter->idx];
  496. }
  497. /*
  498. * Advance the iterator to point to the next _ddebug
  499. * object from the one the iterator currently points at,
  500. * and returns a pointer to the new _ddebug. Returns
  501. * NULL if the iterator has seen all the _ddebugs.
  502. */
  503. static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
  504. {
  505. if (iter->table == NULL)
  506. return NULL;
  507. if (++iter->idx == iter->table->num_ddebugs) {
  508. /* iterate to next table */
  509. iter->idx = 0;
  510. if (list_is_last(&iter->table->link, &ddebug_tables)) {
  511. iter->table = NULL;
  512. return NULL;
  513. }
  514. iter->table = list_entry(iter->table->link.next,
  515. struct ddebug_table, link);
  516. }
  517. return &iter->table->ddebugs[iter->idx];
  518. }
  519. /*
  520. * Seq_ops start method. Called at the start of every
  521. * read() call from userspace. Takes the ddebug_lock and
  522. * seeks the seq_file's iterator to the given position.
  523. */
  524. static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
  525. {
  526. struct ddebug_iter *iter = m->private;
  527. struct _ddebug *dp;
  528. int n = *pos;
  529. if (verbose)
  530. printk(KERN_INFO "%s: called m=%p *pos=%lld\n",
  531. __func__, m, (unsigned long long)*pos);
  532. mutex_lock(&ddebug_lock);
  533. if (!n)
  534. return SEQ_START_TOKEN;
  535. if (n < 0)
  536. return NULL;
  537. dp = ddebug_iter_first(iter);
  538. while (dp != NULL && --n > 0)
  539. dp = ddebug_iter_next(iter);
  540. return dp;
  541. }
  542. /*
  543. * Seq_ops next method. Called several times within a read()
  544. * call from userspace, with ddebug_lock held. Walks to the
  545. * next _ddebug object with a special case for the header line.
  546. */
  547. static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
  548. {
  549. struct ddebug_iter *iter = m->private;
  550. struct _ddebug *dp;
  551. if (verbose)
  552. printk(KERN_INFO "%s: called m=%p p=%p *pos=%lld\n",
  553. __func__, m, p, (unsigned long long)*pos);
  554. if (p == SEQ_START_TOKEN)
  555. dp = ddebug_iter_first(iter);
  556. else
  557. dp = ddebug_iter_next(iter);
  558. ++*pos;
  559. return dp;
  560. }
  561. /*
  562. * Seq_ops show method. Called several times within a read()
  563. * call from userspace, with ddebug_lock held. Formats the
  564. * current _ddebug as a single human-readable line, with a
  565. * special case for the header line.
  566. */
  567. static int ddebug_proc_show(struct seq_file *m, void *p)
  568. {
  569. struct ddebug_iter *iter = m->private;
  570. struct _ddebug *dp = p;
  571. char flagsbuf[8];
  572. if (verbose)
  573. printk(KERN_INFO "%s: called m=%p p=%p\n",
  574. __func__, m, p);
  575. if (p == SEQ_START_TOKEN) {
  576. seq_puts(m,
  577. "# filename:lineno [module]function flags format\n");
  578. return 0;
  579. }
  580. seq_printf(m, "%s:%u [%s]%s %s \"",
  581. dp->filename, dp->lineno,
  582. iter->table->mod_name, dp->function,
  583. ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
  584. seq_escape(m, dp->format, "\t\r\n\"");
  585. seq_puts(m, "\"\n");
  586. return 0;
  587. }
  588. /*
  589. * Seq_ops stop method. Called at the end of each read()
  590. * call from userspace. Drops ddebug_lock.
  591. */
  592. static void ddebug_proc_stop(struct seq_file *m, void *p)
  593. {
  594. if (verbose)
  595. printk(KERN_INFO "%s: called m=%p p=%p\n",
  596. __func__, m, p);
  597. mutex_unlock(&ddebug_lock);
  598. }
  599. static const struct seq_operations ddebug_proc_seqops = {
  600. .start = ddebug_proc_start,
  601. .next = ddebug_proc_next,
  602. .show = ddebug_proc_show,
  603. .stop = ddebug_proc_stop
  604. };
  605. /*
  606. * File_ops->open method for <debugfs>/dynamic_debug/control. Does the seq_file
  607. * setup dance, and also creates an iterator to walk the _ddebugs.
  608. * Note that we create a seq_file always, even for O_WRONLY files
  609. * where it's not needed, as doing so simplifies the ->release method.
  610. */
  611. static int ddebug_proc_open(struct inode *inode, struct file *file)
  612. {
  613. struct ddebug_iter *iter;
  614. int err;
  615. if (verbose)
  616. printk(KERN_INFO "%s: called\n", __func__);
  617. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  618. if (iter == NULL)
  619. return -ENOMEM;
  620. err = seq_open(file, &ddebug_proc_seqops);
  621. if (err) {
  622. kfree(iter);
  623. return err;
  624. }
  625. ((struct seq_file *) file->private_data)->private = iter;
  626. return 0;
  627. }
  628. static const struct file_operations ddebug_proc_fops = {
  629. .owner = THIS_MODULE,
  630. .open = ddebug_proc_open,
  631. .read = seq_read,
  632. .llseek = seq_lseek,
  633. .release = seq_release_private,
  634. .write = ddebug_proc_write
  635. };
  636. /*
  637. * Allocate a new ddebug_table for the given module
  638. * and add it to the global list.
  639. */
  640. int ddebug_add_module(struct _ddebug *tab, unsigned int n,
  641. const char *name)
  642. {
  643. struct ddebug_table *dt;
  644. char *new_name;
  645. dt = kzalloc(sizeof(*dt), GFP_KERNEL);
  646. if (dt == NULL)
  647. return -ENOMEM;
  648. new_name = kstrdup(name, GFP_KERNEL);
  649. if (new_name == NULL) {
  650. kfree(dt);
  651. return -ENOMEM;
  652. }
  653. dt->mod_name = new_name;
  654. dt->num_ddebugs = n;
  655. dt->num_enabled = 0;
  656. dt->ddebugs = tab;
  657. mutex_lock(&ddebug_lock);
  658. list_add_tail(&dt->link, &ddebug_tables);
  659. mutex_unlock(&ddebug_lock);
  660. if (verbose)
  661. printk(KERN_INFO "%u debug prints in module %s\n",
  662. n, dt->mod_name);
  663. return 0;
  664. }
  665. EXPORT_SYMBOL_GPL(ddebug_add_module);
  666. static void ddebug_table_free(struct ddebug_table *dt)
  667. {
  668. list_del_init(&dt->link);
  669. kfree(dt->mod_name);
  670. kfree(dt);
  671. }
  672. /*
  673. * Called in response to a module being unloaded. Removes
  674. * any ddebug_table's which point at the module.
  675. */
  676. int ddebug_remove_module(const char *mod_name)
  677. {
  678. struct ddebug_table *dt, *nextdt;
  679. int ret = -ENOENT;
  680. if (verbose)
  681. printk(KERN_INFO "%s: removing module \"%s\"\n",
  682. __func__, mod_name);
  683. mutex_lock(&ddebug_lock);
  684. list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
  685. if (!strcmp(dt->mod_name, mod_name)) {
  686. ddebug_table_free(dt);
  687. ret = 0;
  688. }
  689. }
  690. mutex_unlock(&ddebug_lock);
  691. return ret;
  692. }
  693. EXPORT_SYMBOL_GPL(ddebug_remove_module);
  694. static void ddebug_remove_all_tables(void)
  695. {
  696. mutex_lock(&ddebug_lock);
  697. while (!list_empty(&ddebug_tables)) {
  698. struct ddebug_table *dt = list_entry(ddebug_tables.next,
  699. struct ddebug_table,
  700. link);
  701. ddebug_table_free(dt);
  702. }
  703. mutex_unlock(&ddebug_lock);
  704. }
  705. static __initdata int ddebug_init_success;
  706. static int __init dynamic_debug_init_debugfs(void)
  707. {
  708. struct dentry *dir, *file;
  709. if (!ddebug_init_success)
  710. return -ENODEV;
  711. dir = debugfs_create_dir("dynamic_debug", NULL);
  712. if (!dir)
  713. return -ENOMEM;
  714. file = debugfs_create_file("control", 0644, dir, NULL,
  715. &ddebug_proc_fops);
  716. if (!file) {
  717. debugfs_remove(dir);
  718. return -ENOMEM;
  719. }
  720. return 0;
  721. }
  722. static int __init dynamic_debug_init(void)
  723. {
  724. struct _ddebug *iter, *iter_start;
  725. const char *modname = NULL;
  726. int ret = 0;
  727. int n = 0;
  728. if (__start___verbose != __stop___verbose) {
  729. iter = __start___verbose;
  730. modname = iter->modname;
  731. iter_start = iter;
  732. for (; iter < __stop___verbose; iter++) {
  733. if (strcmp(modname, iter->modname)) {
  734. ret = ddebug_add_module(iter_start, n, modname);
  735. if (ret)
  736. goto out_free;
  737. n = 0;
  738. modname = iter->modname;
  739. iter_start = iter;
  740. }
  741. n++;
  742. }
  743. ret = ddebug_add_module(iter_start, n, modname);
  744. }
  745. /* ddebug_query boot param got passed -> set it up */
  746. if (ddebug_setup_string[0] != '\0') {
  747. ret = ddebug_exec_query(ddebug_setup_string);
  748. if (ret)
  749. pr_warning("Invalid ddebug boot param %s",
  750. ddebug_setup_string);
  751. else
  752. pr_info("ddebug initialized with string %s",
  753. ddebug_setup_string);
  754. }
  755. out_free:
  756. if (ret)
  757. ddebug_remove_all_tables();
  758. else
  759. ddebug_init_success = 1;
  760. return 0;
  761. }
  762. /* Allow early initialization for boot messages via boot param */
  763. arch_initcall(dynamic_debug_init);
  764. /* Debugfs setup must be done later */
  765. module_init(dynamic_debug_init_debugfs);