dynamic_debug.c 25 KB

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