dynamic_debug.c 20 KB

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