dynamic_debug.c 20 KB

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