dynamic_debug.c 20 KB

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