blob: 4fc03ddb05f2132e051b211be928d3fbffd98050 [file] [log] [blame]
Jason Barone9d376f2009-02-05 11:51:38 -05001/*
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.
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +010010 * Copyright (C) 2011 Bart Van Assche. All Rights Reserved.
Jason Barone9d376f2009-02-05 11:51:38 -050011 */
12
Joe Perches4ad275e2011-08-11 14:36:33 -040013#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
14
Jason Barone9d376f2009-02-05 11:51:38 -050015#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/kallsyms.h>
19#include <linux/version.h>
20#include <linux/types.h>
21#include <linux/mutex.h>
22#include <linux/proc_fs.h>
23#include <linux/seq_file.h>
24#include <linux/list.h>
25#include <linux/sysctl.h>
26#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080027#include <linux/string.h>
Jason Barone9d376f2009-02-05 11:51:38 -050028#include <linux/uaccess.h>
29#include <linux/dynamic_debug.h>
30#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Jason Baron52159d92010-09-17 11:09:17 -040032#include <linux/jump_label.h>
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +010033#include <linux/hardirq.h>
Greg Kroah-Hartmane8d97922011-02-03 15:59:58 -080034#include <linux/sched.h>
Joe Perchescbc46632011-08-11 14:36:21 -040035#include <linux/device.h>
Jason Barone9d376f2009-02-05 11:51:38 -050036
37extern struct _ddebug __start___verbose[];
38extern struct _ddebug __stop___verbose[];
39
Jason Barone9d376f2009-02-05 11:51:38 -050040struct ddebug_table {
41 struct list_head link;
42 char *mod_name;
43 unsigned int num_ddebugs;
44 unsigned int num_enabled;
45 struct _ddebug *ddebugs;
46};
47
48struct ddebug_query {
49 const char *filename;
50 const char *module;
51 const char *function;
52 const char *format;
53 unsigned int first_lineno, last_lineno;
54};
55
56struct ddebug_iter {
57 struct ddebug_table *table;
58 unsigned int idx;
59};
60
61static DEFINE_MUTEX(ddebug_lock);
62static LIST_HEAD(ddebug_tables);
63static int verbose = 0;
64
65/* Return the last part of a pathname */
66static inline const char *basename(const char *path)
67{
68 const char *tail = strrchr(path, '/');
69 return tail ? tail+1 : path;
70}
71
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +010072static struct { unsigned flag:8; char opt_char; } opt_array[] = {
73 { _DPRINTK_FLAGS_PRINT, 'p' },
74 { _DPRINTK_FLAGS_INCL_MODNAME, 'm' },
75 { _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' },
76 { _DPRINTK_FLAGS_INCL_LINENO, 'l' },
77 { _DPRINTK_FLAGS_INCL_TID, 't' },
78};
79
Jason Barone9d376f2009-02-05 11:51:38 -050080/* format a string into buf[] which describes the _ddebug's flags */
81static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,
82 size_t maxlen)
83{
84 char *p = buf;
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +010085 int i;
Jason Barone9d376f2009-02-05 11:51:38 -050086
87 BUG_ON(maxlen < 4);
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +010088 for (i = 0; i < ARRAY_SIZE(opt_array); ++i)
89 if (dp->flags & opt_array[i].flag)
90 *p++ = opt_array[i].opt_char;
Jason Barone9d376f2009-02-05 11:51:38 -050091 if (p == buf)
92 *p++ = '-';
93 *p = '\0';
94
95 return buf;
96}
97
98/*
Jason Barone9d376f2009-02-05 11:51:38 -050099 * Search the tables for _ddebug's which match the given
100 * `query' and apply the `flags' and `mask' to them. Tells
101 * the user which ddebug's were changed, or whether none
102 * were matched.
103 */
104static void ddebug_change(const struct ddebug_query *query,
105 unsigned int flags, unsigned int mask)
106{
107 int i;
108 struct ddebug_table *dt;
109 unsigned int newflags;
110 unsigned int nfound = 0;
111 char flagbuf[8];
112
113 /* search for matching ddebugs */
114 mutex_lock(&ddebug_lock);
115 list_for_each_entry(dt, &ddebug_tables, link) {
116
117 /* match against the module name */
118 if (query->module != NULL &&
119 strcmp(query->module, dt->mod_name))
120 continue;
121
122 for (i = 0 ; i < dt->num_ddebugs ; i++) {
123 struct _ddebug *dp = &dt->ddebugs[i];
124
125 /* match against the source filename */
126 if (query->filename != NULL &&
127 strcmp(query->filename, dp->filename) &&
128 strcmp(query->filename, basename(dp->filename)))
129 continue;
130
131 /* match against the function */
132 if (query->function != NULL &&
133 strcmp(query->function, dp->function))
134 continue;
135
136 /* match against the format */
137 if (query->format != NULL &&
138 strstr(dp->format, query->format) == NULL)
139 continue;
140
141 /* match against the line number range */
142 if (query->first_lineno &&
143 dp->lineno < query->first_lineno)
144 continue;
145 if (query->last_lineno &&
146 dp->lineno > query->last_lineno)
147 continue;
148
149 nfound++;
150
151 newflags = (dp->flags & mask) | flags;
152 if (newflags == dp->flags)
153 continue;
154
155 if (!newflags)
156 dt->num_enabled--;
Roel Kluin4df7b3e2009-07-15 20:29:07 +0200157 else if (!dp->flags)
Jason Barone9d376f2009-02-05 11:51:38 -0500158 dt->num_enabled++;
159 dp->flags = newflags;
Jason Baron2d75af22011-01-07 13:36:58 -0500160 if (newflags)
161 dp->enabled = 1;
162 else
163 dp->enabled = 0;
Jason Barone9d376f2009-02-05 11:51:38 -0500164 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400165 pr_info("changed %s:%d [%s]%s %s\n",
Jason Barone9d376f2009-02-05 11:51:38 -0500166 dp->filename, dp->lineno,
167 dt->mod_name, dp->function,
168 ddebug_describe_flags(dp, flagbuf,
169 sizeof(flagbuf)));
170 }
171 }
172 mutex_unlock(&ddebug_lock);
173
174 if (!nfound && verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400175 pr_info("no matches for query\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500176}
177
178/*
Jason Barone9d376f2009-02-05 11:51:38 -0500179 * Split the buffer `buf' into space-separated words.
Greg Banks9898abb2009-02-06 12:54:26 +1100180 * Handles simple " and ' quoting, i.e. without nested,
181 * embedded or escaped \". Return the number of words
182 * or <0 on error.
Jason Barone9d376f2009-02-05 11:51:38 -0500183 */
184static int ddebug_tokenize(char *buf, char *words[], int maxwords)
185{
186 int nwords = 0;
187
Greg Banks9898abb2009-02-06 12:54:26 +1100188 while (*buf) {
189 char *end;
190
191 /* Skip leading whitespace */
André Goddard Rosae7d28602009-12-14 18:01:06 -0800192 buf = skip_spaces(buf);
Greg Banks9898abb2009-02-06 12:54:26 +1100193 if (!*buf)
194 break; /* oh, it was trailing whitespace */
195
196 /* Run `end' over a word, either whitespace separated or quoted */
197 if (*buf == '"' || *buf == '\'') {
198 int quote = *buf++;
199 for (end = buf ; *end && *end != quote ; end++)
200 ;
201 if (!*end)
202 return -EINVAL; /* unclosed quote */
203 } else {
204 for (end = buf ; *end && !isspace(*end) ; end++)
205 ;
206 BUG_ON(end == buf);
207 }
208 /* Here `buf' is the start of the word, `end' is one past the end */
209
210 if (nwords == maxwords)
211 return -EINVAL; /* ran out of words[] before bytes */
212 if (*end)
213 *end++ = '\0'; /* terminate the word */
214 words[nwords++] = buf;
215 buf = end;
216 }
Jason Barone9d376f2009-02-05 11:51:38 -0500217
218 if (verbose) {
219 int i;
Joe Perches4ad275e2011-08-11 14:36:33 -0400220 pr_info("split into words:");
Jason Barone9d376f2009-02-05 11:51:38 -0500221 for (i = 0 ; i < nwords ; i++)
Joe Perches4ad275e2011-08-11 14:36:33 -0400222 pr_cont(" \"%s\"", words[i]);
223 pr_cont("\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500224 }
225
226 return nwords;
227}
228
229/*
230 * Parse a single line number. Note that the empty string ""
231 * is treated as a special case and converted to zero, which
232 * is later treated as a "don't care" value.
233 */
234static inline int parse_lineno(const char *str, unsigned int *val)
235{
236 char *end = NULL;
237 BUG_ON(str == NULL);
238 if (*str == '\0') {
239 *val = 0;
240 return 0;
241 }
242 *val = simple_strtoul(str, &end, 10);
243 return end == NULL || end == str || *end != '\0' ? -EINVAL : 0;
244}
245
246/*
247 * Undo octal escaping in a string, inplace. This is useful to
248 * allow the user to express a query which matches a format
249 * containing embedded spaces.
250 */
251#define isodigit(c) ((c) >= '0' && (c) <= '7')
252static char *unescape(char *str)
253{
254 char *in = str;
255 char *out = str;
256
257 while (*in) {
258 if (*in == '\\') {
259 if (in[1] == '\\') {
260 *out++ = '\\';
261 in += 2;
262 continue;
263 } else if (in[1] == 't') {
264 *out++ = '\t';
265 in += 2;
266 continue;
267 } else if (in[1] == 'n') {
268 *out++ = '\n';
269 in += 2;
270 continue;
271 } else if (isodigit(in[1]) &&
272 isodigit(in[2]) &&
273 isodigit(in[3])) {
274 *out++ = ((in[1] - '0')<<6) |
275 ((in[2] - '0')<<3) |
276 (in[3] - '0');
277 in += 4;
278 continue;
279 }
280 }
281 *out++ = *in++;
282 }
283 *out = '\0';
284
285 return str;
286}
287
288/*
289 * Parse words[] as a ddebug query specification, which is a series
290 * of (keyword, value) pairs chosen from these possibilities:
291 *
292 * func <function-name>
293 * file <full-pathname>
294 * file <base-filename>
295 * module <module-name>
296 * format <escaped-string-to-find-in-format>
297 * line <lineno>
298 * line <first-lineno>-<last-lineno> // where either may be empty
299 */
300static int ddebug_parse_query(char *words[], int nwords,
301 struct ddebug_query *query)
302{
303 unsigned int i;
304
305 /* check we have an even number of words */
306 if (nwords % 2 != 0)
307 return -EINVAL;
308 memset(query, 0, sizeof(*query));
309
310 for (i = 0 ; i < nwords ; i += 2) {
311 if (!strcmp(words[i], "func"))
312 query->function = words[i+1];
313 else if (!strcmp(words[i], "file"))
314 query->filename = words[i+1];
315 else if (!strcmp(words[i], "module"))
316 query->module = words[i+1];
317 else if (!strcmp(words[i], "format"))
318 query->format = unescape(words[i+1]);
319 else if (!strcmp(words[i], "line")) {
320 char *first = words[i+1];
321 char *last = strchr(first, '-');
322 if (last)
323 *last++ = '\0';
324 if (parse_lineno(first, &query->first_lineno) < 0)
325 return -EINVAL;
326 if (last != NULL) {
327 /* range <first>-<last> */
328 if (parse_lineno(last, &query->last_lineno) < 0)
329 return -EINVAL;
330 } else {
331 query->last_lineno = query->first_lineno;
332 }
333 } else {
334 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400335 pr_err("unknown keyword \"%s\"\n", words[i]);
Jason Barone9d376f2009-02-05 11:51:38 -0500336 return -EINVAL;
337 }
338 }
339
340 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400341 pr_info("q->function=\"%s\" q->filename=\"%s\" "
342 "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n",
343 query->function, query->filename,
Jason Barone9d376f2009-02-05 11:51:38 -0500344 query->module, query->format, query->first_lineno,
345 query->last_lineno);
346
347 return 0;
348}
349
350/*
351 * Parse `str' as a flags specification, format [-+=][p]+.
352 * Sets up *maskp and *flagsp to be used when changing the
353 * flags fields of matched _ddebug's. Returns 0 on success
354 * or <0 on error.
355 */
356static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
357 unsigned int *maskp)
358{
359 unsigned flags = 0;
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100360 int op = '=', i;
Jason Barone9d376f2009-02-05 11:51:38 -0500361
362 switch (*str) {
363 case '+':
364 case '-':
365 case '=':
366 op = *str++;
367 break;
368 default:
369 return -EINVAL;
370 }
371 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400372 pr_info("op='%c'\n", op);
Jason Barone9d376f2009-02-05 11:51:38 -0500373
374 for ( ; *str ; ++str) {
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100375 for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) {
376 if (*str == opt_array[i].opt_char) {
377 flags |= opt_array[i].flag;
378 break;
379 }
Jason Barone9d376f2009-02-05 11:51:38 -0500380 }
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100381 if (i < 0)
382 return -EINVAL;
Jason Barone9d376f2009-02-05 11:51:38 -0500383 }
384 if (flags == 0)
385 return -EINVAL;
386 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400387 pr_info("flags=0x%x\n", flags);
Jason Barone9d376f2009-02-05 11:51:38 -0500388
389 /* calculate final *flagsp, *maskp according to mask and op */
390 switch (op) {
391 case '=':
392 *maskp = 0;
393 *flagsp = flags;
394 break;
395 case '+':
396 *maskp = ~0U;
397 *flagsp = flags;
398 break;
399 case '-':
400 *maskp = ~flags;
401 *flagsp = 0;
402 break;
403 }
404 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400405 pr_info("*flagsp=0x%x *maskp=0x%x\n", *flagsp, *maskp);
Jason Barone9d376f2009-02-05 11:51:38 -0500406 return 0;
407}
408
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200409static int ddebug_exec_query(char *query_string)
410{
411 unsigned int flags = 0, mask = 0;
412 struct ddebug_query query;
413#define MAXWORDS 9
414 int nwords;
415 char *words[MAXWORDS];
416
417 nwords = ddebug_tokenize(query_string, words, MAXWORDS);
418 if (nwords <= 0)
419 return -EINVAL;
420 if (ddebug_parse_query(words, nwords-1, &query))
421 return -EINVAL;
422 if (ddebug_parse_flags(words[nwords-1], &flags, &mask))
423 return -EINVAL;
424
425 /* actually go and implement the change */
426 ddebug_change(&query, flags, mask);
427 return 0;
428}
429
Joe Perches6c2140e2011-08-11 14:36:25 -0400430static int dynamic_emit_prefix(const struct _ddebug *descriptor)
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100431{
Joe Perches5b2ebce2011-08-11 14:36:29 -0400432 char tid[sizeof(int) + sizeof(int)/2 + 4];
433 char lineno[sizeof(int) + sizeof(int)/2];
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100434
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100435 if (descriptor->flags & _DPRINTK_FLAGS_INCL_TID) {
436 if (in_interrupt())
Joe Perches5b2ebce2011-08-11 14:36:29 -0400437 snprintf(tid, sizeof(tid), "%s", "<intr> ");
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100438 else
Joe Perches5b2ebce2011-08-11 14:36:29 -0400439 snprintf(tid, sizeof(tid), "[%d] ",
440 task_pid_vnr(current));
441 } else {
442 tid[0] = 0;
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100443 }
Joe Perches6c2140e2011-08-11 14:36:25 -0400444
Joe Perches5b2ebce2011-08-11 14:36:29 -0400445 if (descriptor->flags & _DPRINTK_FLAGS_INCL_LINENO)
446 snprintf(lineno, sizeof(lineno), "%d", descriptor->lineno);
447 else
448 lineno[0] = 0;
449
450 return printk(KERN_DEBUG "%s%s%s%s%s%s",
451 tid,
452 (descriptor->flags & _DPRINTK_FLAGS_INCL_MODNAME) ?
453 descriptor->modname : "",
454 (descriptor->flags & _DPRINTK_FLAGS_INCL_MODNAME) ?
455 ":" : "",
456 (descriptor->flags & _DPRINTK_FLAGS_INCL_FUNCNAME) ?
457 descriptor->function : "",
458 (descriptor->flags & _DPRINTK_FLAGS_INCL_FUNCNAME) ?
459 ":" : "",
460 lineno);
Joe Perches6c2140e2011-08-11 14:36:25 -0400461}
462
463int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
464{
465 va_list args;
466 int res;
467
468 BUG_ON(!descriptor);
469 BUG_ON(!fmt);
470
471 va_start(args, fmt);
472
473 res = dynamic_emit_prefix(descriptor);
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100474 res += vprintk(fmt, args);
Joe Perches6c2140e2011-08-11 14:36:25 -0400475
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100476 va_end(args);
477
478 return res;
479}
480EXPORT_SYMBOL(__dynamic_pr_debug);
481
Joe Perchescbc46632011-08-11 14:36:21 -0400482int __dynamic_dev_dbg(struct _ddebug *descriptor,
483 const struct device *dev, const char *fmt, ...)
484{
485 struct va_format vaf;
486 va_list args;
487 int res;
488
489 BUG_ON(!descriptor);
490 BUG_ON(!fmt);
491
492 va_start(args, fmt);
493
494 vaf.fmt = fmt;
495 vaf.va = &args;
496
Joe Perches6c2140e2011-08-11 14:36:25 -0400497 res = dynamic_emit_prefix(descriptor);
Joe Perchescbc46632011-08-11 14:36:21 -0400498 res += __dev_printk(KERN_CONT, dev, &vaf);
499
500 va_end(args);
501
502 return res;
503}
504EXPORT_SYMBOL(__dynamic_dev_dbg);
505
Thomas Renningera648ec02010-08-06 16:11:02 +0200506static __initdata char ddebug_setup_string[1024];
507static __init int ddebug_setup_query(char *str)
508{
509 if (strlen(str) >= 1024) {
Joe Perches4ad275e2011-08-11 14:36:33 -0400510 pr_warn("ddebug boot param string too large\n");
Thomas Renningera648ec02010-08-06 16:11:02 +0200511 return 0;
512 }
513 strcpy(ddebug_setup_string, str);
514 return 1;
515}
516
517__setup("ddebug_query=", ddebug_setup_query);
518
Jason Barone9d376f2009-02-05 11:51:38 -0500519/*
520 * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the
521 * command text from userspace, parses and executes it.
522 */
523static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
524 size_t len, loff_t *offp)
525{
Jason Barone9d376f2009-02-05 11:51:38 -0500526 char tmpbuf[256];
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200527 int ret;
Jason Barone9d376f2009-02-05 11:51:38 -0500528
529 if (len == 0)
530 return 0;
531 /* we don't check *offp -- multiple writes() are allowed */
532 if (len > sizeof(tmpbuf)-1)
533 return -E2BIG;
534 if (copy_from_user(tmpbuf, ubuf, len))
535 return -EFAULT;
536 tmpbuf[len] = '\0';
537 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400538 pr_info("read %d bytes from userspace\n", (int)len);
Jason Barone9d376f2009-02-05 11:51:38 -0500539
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200540 ret = ddebug_exec_query(tmpbuf);
541 if (ret)
542 return ret;
Jason Barone9d376f2009-02-05 11:51:38 -0500543
544 *offp += len;
545 return len;
546}
547
548/*
549 * Set the iterator to point to the first _ddebug object
550 * and return a pointer to that first object. Returns
551 * NULL if there are no _ddebugs at all.
552 */
553static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
554{
555 if (list_empty(&ddebug_tables)) {
556 iter->table = NULL;
557 iter->idx = 0;
558 return NULL;
559 }
560 iter->table = list_entry(ddebug_tables.next,
561 struct ddebug_table, link);
562 iter->idx = 0;
563 return &iter->table->ddebugs[iter->idx];
564}
565
566/*
567 * Advance the iterator to point to the next _ddebug
568 * object from the one the iterator currently points at,
569 * and returns a pointer to the new _ddebug. Returns
570 * NULL if the iterator has seen all the _ddebugs.
571 */
572static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
573{
574 if (iter->table == NULL)
575 return NULL;
576 if (++iter->idx == iter->table->num_ddebugs) {
577 /* iterate to next table */
578 iter->idx = 0;
579 if (list_is_last(&iter->table->link, &ddebug_tables)) {
580 iter->table = NULL;
581 return NULL;
582 }
583 iter->table = list_entry(iter->table->link.next,
584 struct ddebug_table, link);
585 }
586 return &iter->table->ddebugs[iter->idx];
587}
588
589/*
590 * Seq_ops start method. Called at the start of every
591 * read() call from userspace. Takes the ddebug_lock and
592 * seeks the seq_file's iterator to the given position.
593 */
594static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
595{
596 struct ddebug_iter *iter = m->private;
597 struct _ddebug *dp;
598 int n = *pos;
599
600 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400601 pr_info("called m=%p *pos=%lld\n", m, (unsigned long long)*pos);
Jason Barone9d376f2009-02-05 11:51:38 -0500602
603 mutex_lock(&ddebug_lock);
604
605 if (!n)
606 return SEQ_START_TOKEN;
607 if (n < 0)
608 return NULL;
609 dp = ddebug_iter_first(iter);
610 while (dp != NULL && --n > 0)
611 dp = ddebug_iter_next(iter);
612 return dp;
613}
614
615/*
616 * Seq_ops next method. Called several times within a read()
617 * call from userspace, with ddebug_lock held. Walks to the
618 * next _ddebug object with a special case for the header line.
619 */
620static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
621{
622 struct ddebug_iter *iter = m->private;
623 struct _ddebug *dp;
624
625 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400626 pr_info("called m=%p p=%p *pos=%lld\n",
627 m, p, (unsigned long long)*pos);
Jason Barone9d376f2009-02-05 11:51:38 -0500628
629 if (p == SEQ_START_TOKEN)
630 dp = ddebug_iter_first(iter);
631 else
632 dp = ddebug_iter_next(iter);
633 ++*pos;
634 return dp;
635}
636
637/*
638 * Seq_ops show method. Called several times within a read()
639 * call from userspace, with ddebug_lock held. Formats the
640 * current _ddebug as a single human-readable line, with a
641 * special case for the header line.
642 */
643static int ddebug_proc_show(struct seq_file *m, void *p)
644{
645 struct ddebug_iter *iter = m->private;
646 struct _ddebug *dp = p;
647 char flagsbuf[8];
648
649 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400650 pr_info("called m=%p p=%p\n", m, p);
Jason Barone9d376f2009-02-05 11:51:38 -0500651
652 if (p == SEQ_START_TOKEN) {
653 seq_puts(m,
654 "# filename:lineno [module]function flags format\n");
655 return 0;
656 }
657
658 seq_printf(m, "%s:%u [%s]%s %s \"",
659 dp->filename, dp->lineno,
660 iter->table->mod_name, dp->function,
661 ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
662 seq_escape(m, dp->format, "\t\r\n\"");
663 seq_puts(m, "\"\n");
664
665 return 0;
666}
667
668/*
669 * Seq_ops stop method. Called at the end of each read()
670 * call from userspace. Drops ddebug_lock.
671 */
672static void ddebug_proc_stop(struct seq_file *m, void *p)
673{
674 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400675 pr_info("called m=%p p=%p\n", m, p);
Jason Barone9d376f2009-02-05 11:51:38 -0500676 mutex_unlock(&ddebug_lock);
677}
678
679static const struct seq_operations ddebug_proc_seqops = {
680 .start = ddebug_proc_start,
681 .next = ddebug_proc_next,
682 .show = ddebug_proc_show,
683 .stop = ddebug_proc_stop
684};
685
686/*
687 * File_ops->open method for <debugfs>/dynamic_debug/control. Does the seq_file
688 * setup dance, and also creates an iterator to walk the _ddebugs.
689 * Note that we create a seq_file always, even for O_WRONLY files
690 * where it's not needed, as doing so simplifies the ->release method.
691 */
692static int ddebug_proc_open(struct inode *inode, struct file *file)
693{
694 struct ddebug_iter *iter;
695 int err;
696
697 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400698 pr_info("called\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500699
700 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
701 if (iter == NULL)
702 return -ENOMEM;
703
704 err = seq_open(file, &ddebug_proc_seqops);
705 if (err) {
706 kfree(iter);
707 return err;
708 }
709 ((struct seq_file *) file->private_data)->private = iter;
710 return 0;
711}
712
713static const struct file_operations ddebug_proc_fops = {
714 .owner = THIS_MODULE,
715 .open = ddebug_proc_open,
716 .read = seq_read,
717 .llseek = seq_lseek,
718 .release = seq_release_private,
719 .write = ddebug_proc_write
720};
721
722/*
723 * Allocate a new ddebug_table for the given module
724 * and add it to the global list.
725 */
726int ddebug_add_module(struct _ddebug *tab, unsigned int n,
727 const char *name)
728{
729 struct ddebug_table *dt;
730 char *new_name;
731
732 dt = kzalloc(sizeof(*dt), GFP_KERNEL);
733 if (dt == NULL)
734 return -ENOMEM;
735 new_name = kstrdup(name, GFP_KERNEL);
736 if (new_name == NULL) {
737 kfree(dt);
738 return -ENOMEM;
739 }
740 dt->mod_name = new_name;
741 dt->num_ddebugs = n;
742 dt->num_enabled = 0;
743 dt->ddebugs = tab;
744
745 mutex_lock(&ddebug_lock);
746 list_add_tail(&dt->link, &ddebug_tables);
747 mutex_unlock(&ddebug_lock);
748
749 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400750 pr_info("%u debug prints in module %s\n", n, dt->mod_name);
Jason Barone9d376f2009-02-05 11:51:38 -0500751 return 0;
752}
753EXPORT_SYMBOL_GPL(ddebug_add_module);
754
755static void ddebug_table_free(struct ddebug_table *dt)
756{
757 list_del_init(&dt->link);
758 kfree(dt->mod_name);
759 kfree(dt);
760}
761
762/*
763 * Called in response to a module being unloaded. Removes
764 * any ddebug_table's which point at the module.
765 */
Yehuda Sadehff49d742010-07-03 13:07:35 +1000766int ddebug_remove_module(const char *mod_name)
Jason Barone9d376f2009-02-05 11:51:38 -0500767{
768 struct ddebug_table *dt, *nextdt;
769 int ret = -ENOENT;
770
771 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400772 pr_info("removing module \"%s\"\n", mod_name);
Jason Barone9d376f2009-02-05 11:51:38 -0500773
774 mutex_lock(&ddebug_lock);
775 list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
776 if (!strcmp(dt->mod_name, mod_name)) {
777 ddebug_table_free(dt);
778 ret = 0;
779 }
780 }
781 mutex_unlock(&ddebug_lock);
782 return ret;
783}
784EXPORT_SYMBOL_GPL(ddebug_remove_module);
785
786static void ddebug_remove_all_tables(void)
787{
788 mutex_lock(&ddebug_lock);
789 while (!list_empty(&ddebug_tables)) {
790 struct ddebug_table *dt = list_entry(ddebug_tables.next,
791 struct ddebug_table,
792 link);
793 ddebug_table_free(dt);
794 }
795 mutex_unlock(&ddebug_lock);
796}
797
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200798static __initdata int ddebug_init_success;
799
800static int __init dynamic_debug_init_debugfs(void)
Jason Barone9d376f2009-02-05 11:51:38 -0500801{
802 struct dentry *dir, *file;
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200803
804 if (!ddebug_init_success)
805 return -ENODEV;
Jason Barone9d376f2009-02-05 11:51:38 -0500806
807 dir = debugfs_create_dir("dynamic_debug", NULL);
808 if (!dir)
809 return -ENOMEM;
810 file = debugfs_create_file("control", 0644, dir, NULL,
811 &ddebug_proc_fops);
812 if (!file) {
813 debugfs_remove(dir);
814 return -ENOMEM;
815 }
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200816 return 0;
817}
818
819static int __init dynamic_debug_init(void)
820{
821 struct _ddebug *iter, *iter_start;
822 const char *modname = NULL;
823 int ret = 0;
824 int n = 0;
825
Jason Barone9d376f2009-02-05 11:51:38 -0500826 if (__start___verbose != __stop___verbose) {
827 iter = __start___verbose;
828 modname = iter->modname;
829 iter_start = iter;
830 for (; iter < __stop___verbose; iter++) {
831 if (strcmp(modname, iter->modname)) {
832 ret = ddebug_add_module(iter_start, n, modname);
833 if (ret)
834 goto out_free;
835 n = 0;
836 modname = iter->modname;
837 iter_start = iter;
838 }
839 n++;
840 }
841 ret = ddebug_add_module(iter_start, n, modname);
842 }
Thomas Renningera648ec02010-08-06 16:11:02 +0200843
844 /* ddebug_query boot param got passed -> set it up */
845 if (ddebug_setup_string[0] != '\0') {
846 ret = ddebug_exec_query(ddebug_setup_string);
847 if (ret)
Joe Perches4ad275e2011-08-11 14:36:33 -0400848 pr_warn("Invalid ddebug boot param %s",
849 ddebug_setup_string);
Thomas Renningera648ec02010-08-06 16:11:02 +0200850 else
851 pr_info("ddebug initialized with string %s",
852 ddebug_setup_string);
Jason Barone9d376f2009-02-05 11:51:38 -0500853 }
Thomas Renningera648ec02010-08-06 16:11:02 +0200854
Jason Barone9d376f2009-02-05 11:51:38 -0500855out_free:
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200856 if (ret)
Jason Barone9d376f2009-02-05 11:51:38 -0500857 ddebug_remove_all_tables();
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200858 else
859 ddebug_init_success = 1;
Jason Barone9d376f2009-02-05 11:51:38 -0500860 return 0;
861}
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200862/* Allow early initialization for boot messages via boot param */
863arch_initcall(dynamic_debug_init);
864/* Debugfs setup must be done later */
865module_init(dynamic_debug_init_debugfs);