dynamic_debug.c 20 KB

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