dynamic_debug.c 23 KB

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