dynamic_debug.c 21 KB

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