dynamic_debug.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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. int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
  385. {
  386. va_list args;
  387. int res;
  388. BUG_ON(!descriptor);
  389. BUG_ON(!fmt);
  390. va_start(args, fmt);
  391. res = printk(KERN_DEBUG);
  392. if (descriptor->flags & _DPRINTK_FLAGS_INCL_TID) {
  393. if (in_interrupt())
  394. res += printk(KERN_CONT "<intr> ");
  395. else
  396. res += printk(KERN_CONT "[%d] ", task_pid_vnr(current));
  397. }
  398. if (descriptor->flags & _DPRINTK_FLAGS_INCL_MODNAME)
  399. res += printk(KERN_CONT "%s:", descriptor->modname);
  400. if (descriptor->flags & _DPRINTK_FLAGS_INCL_FUNCNAME)
  401. res += printk(KERN_CONT "%s:", descriptor->function);
  402. if (descriptor->flags & _DPRINTK_FLAGS_INCL_LINENO)
  403. res += printk(KERN_CONT "%d ", descriptor->lineno);
  404. res += vprintk(fmt, args);
  405. va_end(args);
  406. return res;
  407. }
  408. EXPORT_SYMBOL(__dynamic_pr_debug);
  409. int __dynamic_dev_dbg(struct _ddebug *descriptor,
  410. const struct device *dev, const char *fmt, ...)
  411. {
  412. struct va_format vaf;
  413. va_list args;
  414. int res;
  415. BUG_ON(!descriptor);
  416. BUG_ON(!fmt);
  417. va_start(args, fmt);
  418. vaf.fmt = fmt;
  419. vaf.va = &args;
  420. res = printk(KERN_DEBUG);
  421. if (descriptor->flags & _DPRINTK_FLAGS_INCL_TID) {
  422. if (in_interrupt())
  423. res += printk(KERN_CONT "<intr> ");
  424. else
  425. res += printk(KERN_CONT "[%d] ", task_pid_vnr(current));
  426. }
  427. if (descriptor->flags & _DPRINTK_FLAGS_INCL_MODNAME)
  428. res += printk(KERN_CONT "%s:", descriptor->modname);
  429. if (descriptor->flags & _DPRINTK_FLAGS_INCL_FUNCNAME)
  430. res += printk(KERN_CONT "%s:", descriptor->function);
  431. if (descriptor->flags & _DPRINTK_FLAGS_INCL_LINENO)
  432. res += printk(KERN_CONT "%d ", descriptor->lineno);
  433. res += __dev_printk(KERN_CONT, dev, &vaf);
  434. va_end(args);
  435. return res;
  436. }
  437. EXPORT_SYMBOL(__dynamic_dev_dbg);
  438. static __initdata char ddebug_setup_string[1024];
  439. static __init int ddebug_setup_query(char *str)
  440. {
  441. if (strlen(str) >= 1024) {
  442. pr_warning("ddebug boot param string too large\n");
  443. return 0;
  444. }
  445. strcpy(ddebug_setup_string, str);
  446. return 1;
  447. }
  448. __setup("ddebug_query=", ddebug_setup_query);
  449. /*
  450. * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the
  451. * command text from userspace, parses and executes it.
  452. */
  453. static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
  454. size_t len, loff_t *offp)
  455. {
  456. char tmpbuf[256];
  457. int ret;
  458. if (len == 0)
  459. return 0;
  460. /* we don't check *offp -- multiple writes() are allowed */
  461. if (len > sizeof(tmpbuf)-1)
  462. return -E2BIG;
  463. if (copy_from_user(tmpbuf, ubuf, len))
  464. return -EFAULT;
  465. tmpbuf[len] = '\0';
  466. if (verbose)
  467. printk(KERN_INFO "%s: read %d bytes from userspace\n",
  468. __func__, (int)len);
  469. ret = ddebug_exec_query(tmpbuf);
  470. if (ret)
  471. return ret;
  472. *offp += len;
  473. return len;
  474. }
  475. /*
  476. * Set the iterator to point to the first _ddebug object
  477. * and return a pointer to that first object. Returns
  478. * NULL if there are no _ddebugs at all.
  479. */
  480. static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
  481. {
  482. if (list_empty(&ddebug_tables)) {
  483. iter->table = NULL;
  484. iter->idx = 0;
  485. return NULL;
  486. }
  487. iter->table = list_entry(ddebug_tables.next,
  488. struct ddebug_table, link);
  489. iter->idx = 0;
  490. return &iter->table->ddebugs[iter->idx];
  491. }
  492. /*
  493. * Advance the iterator to point to the next _ddebug
  494. * object from the one the iterator currently points at,
  495. * and returns a pointer to the new _ddebug. Returns
  496. * NULL if the iterator has seen all the _ddebugs.
  497. */
  498. static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
  499. {
  500. if (iter->table == NULL)
  501. return NULL;
  502. if (++iter->idx == iter->table->num_ddebugs) {
  503. /* iterate to next table */
  504. iter->idx = 0;
  505. if (list_is_last(&iter->table->link, &ddebug_tables)) {
  506. iter->table = NULL;
  507. return NULL;
  508. }
  509. iter->table = list_entry(iter->table->link.next,
  510. struct ddebug_table, link);
  511. }
  512. return &iter->table->ddebugs[iter->idx];
  513. }
  514. /*
  515. * Seq_ops start method. Called at the start of every
  516. * read() call from userspace. Takes the ddebug_lock and
  517. * seeks the seq_file's iterator to the given position.
  518. */
  519. static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
  520. {
  521. struct ddebug_iter *iter = m->private;
  522. struct _ddebug *dp;
  523. int n = *pos;
  524. if (verbose)
  525. printk(KERN_INFO "%s: called m=%p *pos=%lld\n",
  526. __func__, m, (unsigned long long)*pos);
  527. mutex_lock(&ddebug_lock);
  528. if (!n)
  529. return SEQ_START_TOKEN;
  530. if (n < 0)
  531. return NULL;
  532. dp = ddebug_iter_first(iter);
  533. while (dp != NULL && --n > 0)
  534. dp = ddebug_iter_next(iter);
  535. return dp;
  536. }
  537. /*
  538. * Seq_ops next method. Called several times within a read()
  539. * call from userspace, with ddebug_lock held. Walks to the
  540. * next _ddebug object with a special case for the header line.
  541. */
  542. static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
  543. {
  544. struct ddebug_iter *iter = m->private;
  545. struct _ddebug *dp;
  546. if (verbose)
  547. printk(KERN_INFO "%s: called m=%p p=%p *pos=%lld\n",
  548. __func__, m, p, (unsigned long long)*pos);
  549. if (p == SEQ_START_TOKEN)
  550. dp = ddebug_iter_first(iter);
  551. else
  552. dp = ddebug_iter_next(iter);
  553. ++*pos;
  554. return dp;
  555. }
  556. /*
  557. * Seq_ops show method. Called several times within a read()
  558. * call from userspace, with ddebug_lock held. Formats the
  559. * current _ddebug as a single human-readable line, with a
  560. * special case for the header line.
  561. */
  562. static int ddebug_proc_show(struct seq_file *m, void *p)
  563. {
  564. struct ddebug_iter *iter = m->private;
  565. struct _ddebug *dp = p;
  566. char flagsbuf[8];
  567. if (verbose)
  568. printk(KERN_INFO "%s: called m=%p p=%p\n",
  569. __func__, m, p);
  570. if (p == SEQ_START_TOKEN) {
  571. seq_puts(m,
  572. "# filename:lineno [module]function flags format\n");
  573. return 0;
  574. }
  575. seq_printf(m, "%s:%u [%s]%s %s \"",
  576. dp->filename, dp->lineno,
  577. iter->table->mod_name, dp->function,
  578. ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
  579. seq_escape(m, dp->format, "\t\r\n\"");
  580. seq_puts(m, "\"\n");
  581. return 0;
  582. }
  583. /*
  584. * Seq_ops stop method. Called at the end of each read()
  585. * call from userspace. Drops ddebug_lock.
  586. */
  587. static void ddebug_proc_stop(struct seq_file *m, void *p)
  588. {
  589. if (verbose)
  590. printk(KERN_INFO "%s: called m=%p p=%p\n",
  591. __func__, m, p);
  592. mutex_unlock(&ddebug_lock);
  593. }
  594. static const struct seq_operations ddebug_proc_seqops = {
  595. .start = ddebug_proc_start,
  596. .next = ddebug_proc_next,
  597. .show = ddebug_proc_show,
  598. .stop = ddebug_proc_stop
  599. };
  600. /*
  601. * File_ops->open method for <debugfs>/dynamic_debug/control. Does the seq_file
  602. * setup dance, and also creates an iterator to walk the _ddebugs.
  603. * Note that we create a seq_file always, even for O_WRONLY files
  604. * where it's not needed, as doing so simplifies the ->release method.
  605. */
  606. static int ddebug_proc_open(struct inode *inode, struct file *file)
  607. {
  608. struct ddebug_iter *iter;
  609. int err;
  610. if (verbose)
  611. printk(KERN_INFO "%s: called\n", __func__);
  612. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  613. if (iter == NULL)
  614. return -ENOMEM;
  615. err = seq_open(file, &ddebug_proc_seqops);
  616. if (err) {
  617. kfree(iter);
  618. return err;
  619. }
  620. ((struct seq_file *) file->private_data)->private = iter;
  621. return 0;
  622. }
  623. static const struct file_operations ddebug_proc_fops = {
  624. .owner = THIS_MODULE,
  625. .open = ddebug_proc_open,
  626. .read = seq_read,
  627. .llseek = seq_lseek,
  628. .release = seq_release_private,
  629. .write = ddebug_proc_write
  630. };
  631. /*
  632. * Allocate a new ddebug_table for the given module
  633. * and add it to the global list.
  634. */
  635. int ddebug_add_module(struct _ddebug *tab, unsigned int n,
  636. const char *name)
  637. {
  638. struct ddebug_table *dt;
  639. char *new_name;
  640. dt = kzalloc(sizeof(*dt), GFP_KERNEL);
  641. if (dt == NULL)
  642. return -ENOMEM;
  643. new_name = kstrdup(name, GFP_KERNEL);
  644. if (new_name == NULL) {
  645. kfree(dt);
  646. return -ENOMEM;
  647. }
  648. dt->mod_name = new_name;
  649. dt->num_ddebugs = n;
  650. dt->num_enabled = 0;
  651. dt->ddebugs = tab;
  652. mutex_lock(&ddebug_lock);
  653. list_add_tail(&dt->link, &ddebug_tables);
  654. mutex_unlock(&ddebug_lock);
  655. if (verbose)
  656. printk(KERN_INFO "%u debug prints in module %s\n",
  657. n, dt->mod_name);
  658. return 0;
  659. }
  660. EXPORT_SYMBOL_GPL(ddebug_add_module);
  661. static void ddebug_table_free(struct ddebug_table *dt)
  662. {
  663. list_del_init(&dt->link);
  664. kfree(dt->mod_name);
  665. kfree(dt);
  666. }
  667. /*
  668. * Called in response to a module being unloaded. Removes
  669. * any ddebug_table's which point at the module.
  670. */
  671. int ddebug_remove_module(const char *mod_name)
  672. {
  673. struct ddebug_table *dt, *nextdt;
  674. int ret = -ENOENT;
  675. if (verbose)
  676. printk(KERN_INFO "%s: removing module \"%s\"\n",
  677. __func__, mod_name);
  678. mutex_lock(&ddebug_lock);
  679. list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
  680. if (!strcmp(dt->mod_name, mod_name)) {
  681. ddebug_table_free(dt);
  682. ret = 0;
  683. }
  684. }
  685. mutex_unlock(&ddebug_lock);
  686. return ret;
  687. }
  688. EXPORT_SYMBOL_GPL(ddebug_remove_module);
  689. static void ddebug_remove_all_tables(void)
  690. {
  691. mutex_lock(&ddebug_lock);
  692. while (!list_empty(&ddebug_tables)) {
  693. struct ddebug_table *dt = list_entry(ddebug_tables.next,
  694. struct ddebug_table,
  695. link);
  696. ddebug_table_free(dt);
  697. }
  698. mutex_unlock(&ddebug_lock);
  699. }
  700. static __initdata int ddebug_init_success;
  701. static int __init dynamic_debug_init_debugfs(void)
  702. {
  703. struct dentry *dir, *file;
  704. if (!ddebug_init_success)
  705. return -ENODEV;
  706. dir = debugfs_create_dir("dynamic_debug", NULL);
  707. if (!dir)
  708. return -ENOMEM;
  709. file = debugfs_create_file("control", 0644, dir, NULL,
  710. &ddebug_proc_fops);
  711. if (!file) {
  712. debugfs_remove(dir);
  713. return -ENOMEM;
  714. }
  715. return 0;
  716. }
  717. static int __init dynamic_debug_init(void)
  718. {
  719. struct _ddebug *iter, *iter_start;
  720. const char *modname = NULL;
  721. int ret = 0;
  722. int n = 0;
  723. if (__start___verbose != __stop___verbose) {
  724. iter = __start___verbose;
  725. modname = iter->modname;
  726. iter_start = iter;
  727. for (; iter < __stop___verbose; iter++) {
  728. if (strcmp(modname, iter->modname)) {
  729. ret = ddebug_add_module(iter_start, n, modname);
  730. if (ret)
  731. goto out_free;
  732. n = 0;
  733. modname = iter->modname;
  734. iter_start = iter;
  735. }
  736. n++;
  737. }
  738. ret = ddebug_add_module(iter_start, n, modname);
  739. }
  740. /* ddebug_query boot param got passed -> set it up */
  741. if (ddebug_setup_string[0] != '\0') {
  742. ret = ddebug_exec_query(ddebug_setup_string);
  743. if (ret)
  744. pr_warning("Invalid ddebug boot param %s",
  745. ddebug_setup_string);
  746. else
  747. pr_info("ddebug initialized with string %s",
  748. ddebug_setup_string);
  749. }
  750. out_free:
  751. if (ret)
  752. ddebug_remove_all_tables();
  753. else
  754. ddebug_init_success = 1;
  755. return 0;
  756. }
  757. /* Allow early initialization for boot messages via boot param */
  758. arch_initcall(dynamic_debug_init);
  759. /* Debugfs setup must be done later */
  760. module_init(dynamic_debug_init_debugfs);