dynamic_debug.c 21 KB

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