dynamic_debug.c 20 KB

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