dynamic_debug.c 20 KB

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