blob: ee3b9ba625c5401ecc537eee81827ed8312aced3 [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 Assche8ba6ebf52011-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 Assche8ba6ebf52011-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 Baronffa10cb2011-08-11 14:36:48 -040036#include <linux/netdevice.h>
Jason Barone9d376f2009-02-05 11:51:38 -050037
38extern struct _ddebug __start___verbose[];
39extern struct _ddebug __stop___verbose[];
40
Jason Barone9d376f2009-02-05 11:51:38 -050041struct ddebug_table {
42 struct list_head link;
43 char *mod_name;
44 unsigned int num_ddebugs;
45 unsigned int num_enabled;
46 struct _ddebug *ddebugs;
47};
48
49struct ddebug_query {
50 const char *filename;
51 const char *module;
52 const char *function;
53 const char *format;
54 unsigned int first_lineno, last_lineno;
55};
56
57struct ddebug_iter {
58 struct ddebug_table *table;
59 unsigned int idx;
60};
61
62static DEFINE_MUTEX(ddebug_lock);
63static LIST_HEAD(ddebug_tables);
64static int verbose = 0;
65
66/* Return the last part of a pathname */
67static inline const char *basename(const char *path)
68{
69 const char *tail = strrchr(path, '/');
70 return tail ? tail+1 : path;
71}
72
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +010073static struct { unsigned flag:8; char opt_char; } opt_array[] = {
74 { _DPRINTK_FLAGS_PRINT, 'p' },
75 { _DPRINTK_FLAGS_INCL_MODNAME, 'm' },
76 { _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' },
77 { _DPRINTK_FLAGS_INCL_LINENO, 'l' },
78 { _DPRINTK_FLAGS_INCL_TID, 't' },
79};
80
Jason Barone9d376f2009-02-05 11:51:38 -050081/* format a string into buf[] which describes the _ddebug's flags */
82static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,
83 size_t maxlen)
84{
85 char *p = buf;
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +010086 int i;
Jason Barone9d376f2009-02-05 11:51:38 -050087
88 BUG_ON(maxlen < 4);
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +010089 for (i = 0; i < ARRAY_SIZE(opt_array); ++i)
90 if (dp->flags & opt_array[i].flag)
91 *p++ = opt_array[i].opt_char;
Jason Barone9d376f2009-02-05 11:51:38 -050092 if (p == buf)
93 *p++ = '-';
94 *p = '\0';
95
96 return buf;
97}
98
99/*
Jason Barone9d376f2009-02-05 11:51:38 -0500100 * Search the tables for _ddebug's which match the given
101 * `query' and apply the `flags' and `mask' to them. Tells
102 * the user which ddebug's were changed, or whether none
103 * were matched.
104 */
105static void ddebug_change(const struct ddebug_query *query,
106 unsigned int flags, unsigned int mask)
107{
108 int i;
109 struct ddebug_table *dt;
110 unsigned int newflags;
111 unsigned int nfound = 0;
112 char flagbuf[8];
113
114 /* search for matching ddebugs */
115 mutex_lock(&ddebug_lock);
116 list_for_each_entry(dt, &ddebug_tables, link) {
117
118 /* match against the module name */
119 if (query->module != NULL &&
120 strcmp(query->module, dt->mod_name))
121 continue;
122
123 for (i = 0 ; i < dt->num_ddebugs ; i++) {
124 struct _ddebug *dp = &dt->ddebugs[i];
125
126 /* match against the source filename */
127 if (query->filename != NULL &&
128 strcmp(query->filename, dp->filename) &&
129 strcmp(query->filename, basename(dp->filename)))
130 continue;
131
132 /* match against the function */
133 if (query->function != NULL &&
134 strcmp(query->function, dp->function))
135 continue;
136
137 /* match against the format */
138 if (query->format != NULL &&
139 strstr(dp->format, query->format) == NULL)
140 continue;
141
142 /* match against the line number range */
143 if (query->first_lineno &&
144 dp->lineno < query->first_lineno)
145 continue;
146 if (query->last_lineno &&
147 dp->lineno > query->last_lineno)
148 continue;
149
150 nfound++;
151
152 newflags = (dp->flags & mask) | flags;
153 if (newflags == dp->flags)
154 continue;
155
156 if (!newflags)
157 dt->num_enabled--;
Roel Kluin4df7b3e2009-07-15 20:29:07 +0200158 else if (!dp->flags)
Jason Barone9d376f2009-02-05 11:51:38 -0500159 dt->num_enabled++;
160 dp->flags = newflags;
Jason Baron2d75af22011-01-07 13:36:58 -0500161 if (newflags)
162 dp->enabled = 1;
163 else
164 dp->enabled = 0;
Jason Barone9d376f2009-02-05 11:51:38 -0500165 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400166 pr_info("changed %s:%d [%s]%s %s\n",
Jason Barone9d376f2009-02-05 11:51:38 -0500167 dp->filename, dp->lineno,
168 dt->mod_name, dp->function,
169 ddebug_describe_flags(dp, flagbuf,
170 sizeof(flagbuf)));
171 }
172 }
173 mutex_unlock(&ddebug_lock);
174
175 if (!nfound && verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400176 pr_info("no matches for query\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500177}
178
179/*
Jason Barone9d376f2009-02-05 11:51:38 -0500180 * Split the buffer `buf' into space-separated words.
Greg Banks9898abb2009-02-06 12:54:26 +1100181 * Handles simple " and ' quoting, i.e. without nested,
182 * embedded or escaped \". Return the number of words
183 * or <0 on error.
Jason Barone9d376f2009-02-05 11:51:38 -0500184 */
185static int ddebug_tokenize(char *buf, char *words[], int maxwords)
186{
187 int nwords = 0;
188
Greg Banks9898abb2009-02-06 12:54:26 +1100189 while (*buf) {
190 char *end;
191
192 /* Skip leading whitespace */
André Goddard Rosae7d28602009-12-14 18:01:06 -0800193 buf = skip_spaces(buf);
Greg Banks9898abb2009-02-06 12:54:26 +1100194 if (!*buf)
195 break; /* oh, it was trailing whitespace */
196
197 /* Run `end' over a word, either whitespace separated or quoted */
198 if (*buf == '"' || *buf == '\'') {
199 int quote = *buf++;
200 for (end = buf ; *end && *end != quote ; end++)
201 ;
202 if (!*end)
203 return -EINVAL; /* unclosed quote */
204 } else {
205 for (end = buf ; *end && !isspace(*end) ; end++)
206 ;
207 BUG_ON(end == buf);
208 }
209 /* Here `buf' is the start of the word, `end' is one past the end */
210
211 if (nwords == maxwords)
212 return -EINVAL; /* ran out of words[] before bytes */
213 if (*end)
214 *end++ = '\0'; /* terminate the word */
215 words[nwords++] = buf;
216 buf = end;
217 }
Jason Barone9d376f2009-02-05 11:51:38 -0500218
219 if (verbose) {
220 int i;
Joe Perches4ad275e2011-08-11 14:36:33 -0400221 pr_info("split into words:");
Jason Barone9d376f2009-02-05 11:51:38 -0500222 for (i = 0 ; i < nwords ; i++)
Joe Perches4ad275e2011-08-11 14:36:33 -0400223 pr_cont(" \"%s\"", words[i]);
224 pr_cont("\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500225 }
226
227 return nwords;
228}
229
230/*
231 * Parse a single line number. Note that the empty string ""
232 * is treated as a special case and converted to zero, which
233 * is later treated as a "don't care" value.
234 */
235static inline int parse_lineno(const char *str, unsigned int *val)
236{
237 char *end = NULL;
238 BUG_ON(str == NULL);
239 if (*str == '\0') {
240 *val = 0;
241 return 0;
242 }
243 *val = simple_strtoul(str, &end, 10);
244 return end == NULL || end == str || *end != '\0' ? -EINVAL : 0;
245}
246
247/*
248 * Undo octal escaping in a string, inplace. This is useful to
249 * allow the user to express a query which matches a format
250 * containing embedded spaces.
251 */
252#define isodigit(c) ((c) >= '0' && (c) <= '7')
253static char *unescape(char *str)
254{
255 char *in = str;
256 char *out = str;
257
258 while (*in) {
259 if (*in == '\\') {
260 if (in[1] == '\\') {
261 *out++ = '\\';
262 in += 2;
263 continue;
264 } else if (in[1] == 't') {
265 *out++ = '\t';
266 in += 2;
267 continue;
268 } else if (in[1] == 'n') {
269 *out++ = '\n';
270 in += 2;
271 continue;
272 } else if (isodigit(in[1]) &&
273 isodigit(in[2]) &&
274 isodigit(in[3])) {
275 *out++ = ((in[1] - '0')<<6) |
276 ((in[2] - '0')<<3) |
277 (in[3] - '0');
278 in += 4;
279 continue;
280 }
281 }
282 *out++ = *in++;
283 }
284 *out = '\0';
285
286 return str;
287}
288
289/*
290 * Parse words[] as a ddebug query specification, which is a series
291 * of (keyword, value) pairs chosen from these possibilities:
292 *
293 * func <function-name>
294 * file <full-pathname>
295 * file <base-filename>
296 * module <module-name>
297 * format <escaped-string-to-find-in-format>
298 * line <lineno>
299 * line <first-lineno>-<last-lineno> // where either may be empty
300 */
301static int ddebug_parse_query(char *words[], int nwords,
302 struct ddebug_query *query)
303{
304 unsigned int i;
305
306 /* check we have an even number of words */
307 if (nwords % 2 != 0)
308 return -EINVAL;
309 memset(query, 0, sizeof(*query));
310
311 for (i = 0 ; i < nwords ; i += 2) {
312 if (!strcmp(words[i], "func"))
313 query->function = words[i+1];
314 else if (!strcmp(words[i], "file"))
315 query->filename = words[i+1];
316 else if (!strcmp(words[i], "module"))
317 query->module = words[i+1];
318 else if (!strcmp(words[i], "format"))
319 query->format = unescape(words[i+1]);
320 else if (!strcmp(words[i], "line")) {
321 char *first = words[i+1];
322 char *last = strchr(first, '-');
323 if (last)
324 *last++ = '\0';
325 if (parse_lineno(first, &query->first_lineno) < 0)
326 return -EINVAL;
327 if (last != NULL) {
328 /* range <first>-<last> */
329 if (parse_lineno(last, &query->last_lineno) < 0)
330 return -EINVAL;
331 } else {
332 query->last_lineno = query->first_lineno;
333 }
334 } else {
335 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400336 pr_err("unknown keyword \"%s\"\n", words[i]);
Jason Barone9d376f2009-02-05 11:51:38 -0500337 return -EINVAL;
338 }
339 }
340
341 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400342 pr_info("q->function=\"%s\" q->filename=\"%s\" "
343 "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n",
344 query->function, query->filename,
Jason Barone9d376f2009-02-05 11:51:38 -0500345 query->module, query->format, query->first_lineno,
346 query->last_lineno);
347
348 return 0;
349}
350
351/*
352 * Parse `str' as a flags specification, format [-+=][p]+.
353 * Sets up *maskp and *flagsp to be used when changing the
354 * flags fields of matched _ddebug's. Returns 0 on success
355 * or <0 on error.
356 */
357static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
358 unsigned int *maskp)
359{
360 unsigned flags = 0;
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100361 int op = '=', i;
Jason Barone9d376f2009-02-05 11:51:38 -0500362
363 switch (*str) {
364 case '+':
365 case '-':
366 case '=':
367 op = *str++;
368 break;
369 default:
370 return -EINVAL;
371 }
372 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400373 pr_info("op='%c'\n", op);
Jason Barone9d376f2009-02-05 11:51:38 -0500374
375 for ( ; *str ; ++str) {
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100376 for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) {
377 if (*str == opt_array[i].opt_char) {
378 flags |= opt_array[i].flag;
379 break;
380 }
Jason Barone9d376f2009-02-05 11:51:38 -0500381 }
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100382 if (i < 0)
383 return -EINVAL;
Jason Barone9d376f2009-02-05 11:51:38 -0500384 }
385 if (flags == 0)
386 return -EINVAL;
387 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400388 pr_info("flags=0x%x\n", flags);
Jason Barone9d376f2009-02-05 11:51:38 -0500389
390 /* calculate final *flagsp, *maskp according to mask and op */
391 switch (op) {
392 case '=':
393 *maskp = 0;
394 *flagsp = flags;
395 break;
396 case '+':
397 *maskp = ~0U;
398 *flagsp = flags;
399 break;
400 case '-':
401 *maskp = ~flags;
402 *flagsp = 0;
403 break;
404 }
405 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400406 pr_info("*flagsp=0x%x *maskp=0x%x\n", *flagsp, *maskp);
Jason Barone9d376f2009-02-05 11:51:38 -0500407 return 0;
408}
409
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200410static int ddebug_exec_query(char *query_string)
411{
412 unsigned int flags = 0, mask = 0;
413 struct ddebug_query query;
414#define MAXWORDS 9
415 int nwords;
416 char *words[MAXWORDS];
417
418 nwords = ddebug_tokenize(query_string, words, MAXWORDS);
419 if (nwords <= 0)
420 return -EINVAL;
421 if (ddebug_parse_query(words, nwords-1, &query))
422 return -EINVAL;
423 if (ddebug_parse_flags(words[nwords-1], &flags, &mask))
424 return -EINVAL;
425
426 /* actually go and implement the change */
427 ddebug_change(&query, flags, mask);
428 return 0;
429}
430
Joe Perches6c2140e2011-08-11 14:36:25 -0400431static int dynamic_emit_prefix(const struct _ddebug *descriptor)
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100432{
Joe Perches5b2ebce2011-08-11 14:36:29 -0400433 char tid[sizeof(int) + sizeof(int)/2 + 4];
434 char lineno[sizeof(int) + sizeof(int)/2];
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100435
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100436 if (descriptor->flags & _DPRINTK_FLAGS_INCL_TID) {
437 if (in_interrupt())
Joe Perches5b2ebce2011-08-11 14:36:29 -0400438 snprintf(tid, sizeof(tid), "%s", "<intr> ");
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100439 else
Joe Perches5b2ebce2011-08-11 14:36:29 -0400440 snprintf(tid, sizeof(tid), "[%d] ",
441 task_pid_vnr(current));
442 } else {
443 tid[0] = 0;
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100444 }
Joe Perches6c2140e2011-08-11 14:36:25 -0400445
Joe Perches5b2ebce2011-08-11 14:36:29 -0400446 if (descriptor->flags & _DPRINTK_FLAGS_INCL_LINENO)
447 snprintf(lineno, sizeof(lineno), "%d", descriptor->lineno);
448 else
449 lineno[0] = 0;
450
451 return printk(KERN_DEBUG "%s%s%s%s%s%s",
452 tid,
453 (descriptor->flags & _DPRINTK_FLAGS_INCL_MODNAME) ?
454 descriptor->modname : "",
455 (descriptor->flags & _DPRINTK_FLAGS_INCL_MODNAME) ?
456 ":" : "",
457 (descriptor->flags & _DPRINTK_FLAGS_INCL_FUNCNAME) ?
458 descriptor->function : "",
459 (descriptor->flags & _DPRINTK_FLAGS_INCL_FUNCNAME) ?
460 ":" : "",
461 lineno);
Joe Perches6c2140e2011-08-11 14:36:25 -0400462}
463
464int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
465{
466 va_list args;
467 int res;
468
469 BUG_ON(!descriptor);
470 BUG_ON(!fmt);
471
472 va_start(args, fmt);
473
474 res = dynamic_emit_prefix(descriptor);
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100475 res += vprintk(fmt, args);
Joe Perches6c2140e2011-08-11 14:36:25 -0400476
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100477 va_end(args);
478
479 return res;
480}
481EXPORT_SYMBOL(__dynamic_pr_debug);
482
Joe Perchescbc46632011-08-11 14:36:21 -0400483int __dynamic_dev_dbg(struct _ddebug *descriptor,
484 const struct device *dev, const char *fmt, ...)
485{
486 struct va_format vaf;
487 va_list args;
488 int res;
489
490 BUG_ON(!descriptor);
491 BUG_ON(!fmt);
492
493 va_start(args, fmt);
494
495 vaf.fmt = fmt;
496 vaf.va = &args;
497
Joe Perches6c2140e2011-08-11 14:36:25 -0400498 res = dynamic_emit_prefix(descriptor);
Joe Perchescbc46632011-08-11 14:36:21 -0400499 res += __dev_printk(KERN_CONT, dev, &vaf);
500
501 va_end(args);
502
503 return res;
504}
505EXPORT_SYMBOL(__dynamic_dev_dbg);
506
Jason Baronffa10cb2011-08-11 14:36:48 -0400507int __dynamic_netdev_dbg(struct _ddebug *descriptor,
508 const struct net_device *dev, const char *fmt, ...)
509{
510 struct va_format vaf;
511 va_list args;
512 int res;
513
514 BUG_ON(!descriptor);
515 BUG_ON(!fmt);
516
517 va_start(args, fmt);
518
519 vaf.fmt = fmt;
520 vaf.va = &args;
521
522 res = dynamic_emit_prefix(descriptor);
523 res += __netdev_printk(KERN_CONT, dev, &vaf);
524
525 va_end(args);
526
527 return res;
528}
529EXPORT_SYMBOL(__dynamic_netdev_dbg);
530
Thomas Renningera648ec02010-08-06 16:11:02 +0200531static __initdata char ddebug_setup_string[1024];
532static __init int ddebug_setup_query(char *str)
533{
534 if (strlen(str) >= 1024) {
Joe Perches4ad275e2011-08-11 14:36:33 -0400535 pr_warn("ddebug boot param string too large\n");
Thomas Renningera648ec02010-08-06 16:11:02 +0200536 return 0;
537 }
538 strcpy(ddebug_setup_string, str);
539 return 1;
540}
541
542__setup("ddebug_query=", ddebug_setup_query);
543
Jason Barone9d376f2009-02-05 11:51:38 -0500544/*
545 * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the
546 * command text from userspace, parses and executes it.
547 */
548static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
549 size_t len, loff_t *offp)
550{
Jason Barone9d376f2009-02-05 11:51:38 -0500551 char tmpbuf[256];
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200552 int ret;
Jason Barone9d376f2009-02-05 11:51:38 -0500553
554 if (len == 0)
555 return 0;
556 /* we don't check *offp -- multiple writes() are allowed */
557 if (len > sizeof(tmpbuf)-1)
558 return -E2BIG;
559 if (copy_from_user(tmpbuf, ubuf, len))
560 return -EFAULT;
561 tmpbuf[len] = '\0';
562 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400563 pr_info("read %d bytes from userspace\n", (int)len);
Jason Barone9d376f2009-02-05 11:51:38 -0500564
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200565 ret = ddebug_exec_query(tmpbuf);
566 if (ret)
567 return ret;
Jason Barone9d376f2009-02-05 11:51:38 -0500568
569 *offp += len;
570 return len;
571}
572
573/*
574 * Set the iterator to point to the first _ddebug object
575 * and return a pointer to that first object. Returns
576 * NULL if there are no _ddebugs at all.
577 */
578static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
579{
580 if (list_empty(&ddebug_tables)) {
581 iter->table = NULL;
582 iter->idx = 0;
583 return NULL;
584 }
585 iter->table = list_entry(ddebug_tables.next,
586 struct ddebug_table, link);
587 iter->idx = 0;
588 return &iter->table->ddebugs[iter->idx];
589}
590
591/*
592 * Advance the iterator to point to the next _ddebug
593 * object from the one the iterator currently points at,
594 * and returns a pointer to the new _ddebug. Returns
595 * NULL if the iterator has seen all the _ddebugs.
596 */
597static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
598{
599 if (iter->table == NULL)
600 return NULL;
601 if (++iter->idx == iter->table->num_ddebugs) {
602 /* iterate to next table */
603 iter->idx = 0;
604 if (list_is_last(&iter->table->link, &ddebug_tables)) {
605 iter->table = NULL;
606 return NULL;
607 }
608 iter->table = list_entry(iter->table->link.next,
609 struct ddebug_table, link);
610 }
611 return &iter->table->ddebugs[iter->idx];
612}
613
614/*
615 * Seq_ops start method. Called at the start of every
616 * read() call from userspace. Takes the ddebug_lock and
617 * seeks the seq_file's iterator to the given position.
618 */
619static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
620{
621 struct ddebug_iter *iter = m->private;
622 struct _ddebug *dp;
623 int n = *pos;
624
625 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400626 pr_info("called m=%p *pos=%lld\n", m, (unsigned long long)*pos);
Jason Barone9d376f2009-02-05 11:51:38 -0500627
628 mutex_lock(&ddebug_lock);
629
630 if (!n)
631 return SEQ_START_TOKEN;
632 if (n < 0)
633 return NULL;
634 dp = ddebug_iter_first(iter);
635 while (dp != NULL && --n > 0)
636 dp = ddebug_iter_next(iter);
637 return dp;
638}
639
640/*
641 * Seq_ops next method. Called several times within a read()
642 * call from userspace, with ddebug_lock held. Walks to the
643 * next _ddebug object with a special case for the header line.
644 */
645static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
646{
647 struct ddebug_iter *iter = m->private;
648 struct _ddebug *dp;
649
650 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400651 pr_info("called m=%p p=%p *pos=%lld\n",
652 m, p, (unsigned long long)*pos);
Jason Barone9d376f2009-02-05 11:51:38 -0500653
654 if (p == SEQ_START_TOKEN)
655 dp = ddebug_iter_first(iter);
656 else
657 dp = ddebug_iter_next(iter);
658 ++*pos;
659 return dp;
660}
661
662/*
663 * Seq_ops show method. Called several times within a read()
664 * call from userspace, with ddebug_lock held. Formats the
665 * current _ddebug as a single human-readable line, with a
666 * special case for the header line.
667 */
668static int ddebug_proc_show(struct seq_file *m, void *p)
669{
670 struct ddebug_iter *iter = m->private;
671 struct _ddebug *dp = p;
672 char flagsbuf[8];
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
677 if (p == SEQ_START_TOKEN) {
678 seq_puts(m,
679 "# filename:lineno [module]function flags format\n");
680 return 0;
681 }
682
683 seq_printf(m, "%s:%u [%s]%s %s \"",
684 dp->filename, dp->lineno,
685 iter->table->mod_name, dp->function,
686 ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
687 seq_escape(m, dp->format, "\t\r\n\"");
688 seq_puts(m, "\"\n");
689
690 return 0;
691}
692
693/*
694 * Seq_ops stop method. Called at the end of each read()
695 * call from userspace. Drops ddebug_lock.
696 */
697static void ddebug_proc_stop(struct seq_file *m, void *p)
698{
699 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400700 pr_info("called m=%p p=%p\n", m, p);
Jason Barone9d376f2009-02-05 11:51:38 -0500701 mutex_unlock(&ddebug_lock);
702}
703
704static const struct seq_operations ddebug_proc_seqops = {
705 .start = ddebug_proc_start,
706 .next = ddebug_proc_next,
707 .show = ddebug_proc_show,
708 .stop = ddebug_proc_stop
709};
710
711/*
712 * File_ops->open method for <debugfs>/dynamic_debug/control. Does the seq_file
713 * setup dance, and also creates an iterator to walk the _ddebugs.
714 * Note that we create a seq_file always, even for O_WRONLY files
715 * where it's not needed, as doing so simplifies the ->release method.
716 */
717static int ddebug_proc_open(struct inode *inode, struct file *file)
718{
719 struct ddebug_iter *iter;
720 int err;
721
722 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400723 pr_info("called\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500724
725 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
726 if (iter == NULL)
727 return -ENOMEM;
728
729 err = seq_open(file, &ddebug_proc_seqops);
730 if (err) {
731 kfree(iter);
732 return err;
733 }
734 ((struct seq_file *) file->private_data)->private = iter;
735 return 0;
736}
737
738static const struct file_operations ddebug_proc_fops = {
739 .owner = THIS_MODULE,
740 .open = ddebug_proc_open,
741 .read = seq_read,
742 .llseek = seq_lseek,
743 .release = seq_release_private,
744 .write = ddebug_proc_write
745};
746
747/*
748 * Allocate a new ddebug_table for the given module
749 * and add it to the global list.
750 */
751int ddebug_add_module(struct _ddebug *tab, unsigned int n,
752 const char *name)
753{
754 struct ddebug_table *dt;
755 char *new_name;
756
757 dt = kzalloc(sizeof(*dt), GFP_KERNEL);
758 if (dt == NULL)
759 return -ENOMEM;
760 new_name = kstrdup(name, GFP_KERNEL);
761 if (new_name == NULL) {
762 kfree(dt);
763 return -ENOMEM;
764 }
765 dt->mod_name = new_name;
766 dt->num_ddebugs = n;
767 dt->num_enabled = 0;
768 dt->ddebugs = tab;
769
770 mutex_lock(&ddebug_lock);
771 list_add_tail(&dt->link, &ddebug_tables);
772 mutex_unlock(&ddebug_lock);
773
774 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400775 pr_info("%u debug prints in module %s\n", n, dt->mod_name);
Jason Barone9d376f2009-02-05 11:51:38 -0500776 return 0;
777}
778EXPORT_SYMBOL_GPL(ddebug_add_module);
779
780static void ddebug_table_free(struct ddebug_table *dt)
781{
782 list_del_init(&dt->link);
783 kfree(dt->mod_name);
784 kfree(dt);
785}
786
787/*
788 * Called in response to a module being unloaded. Removes
789 * any ddebug_table's which point at the module.
790 */
Yehuda Sadehff49d742010-07-03 13:07:35 +1000791int ddebug_remove_module(const char *mod_name)
Jason Barone9d376f2009-02-05 11:51:38 -0500792{
793 struct ddebug_table *dt, *nextdt;
794 int ret = -ENOENT;
795
796 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400797 pr_info("removing module \"%s\"\n", mod_name);
Jason Barone9d376f2009-02-05 11:51:38 -0500798
799 mutex_lock(&ddebug_lock);
800 list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
801 if (!strcmp(dt->mod_name, mod_name)) {
802 ddebug_table_free(dt);
803 ret = 0;
804 }
805 }
806 mutex_unlock(&ddebug_lock);
807 return ret;
808}
809EXPORT_SYMBOL_GPL(ddebug_remove_module);
810
811static void ddebug_remove_all_tables(void)
812{
813 mutex_lock(&ddebug_lock);
814 while (!list_empty(&ddebug_tables)) {
815 struct ddebug_table *dt = list_entry(ddebug_tables.next,
816 struct ddebug_table,
817 link);
818 ddebug_table_free(dt);
819 }
820 mutex_unlock(&ddebug_lock);
821}
822
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200823static __initdata int ddebug_init_success;
824
825static int __init dynamic_debug_init_debugfs(void)
Jason Barone9d376f2009-02-05 11:51:38 -0500826{
827 struct dentry *dir, *file;
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200828
829 if (!ddebug_init_success)
830 return -ENODEV;
Jason Barone9d376f2009-02-05 11:51:38 -0500831
832 dir = debugfs_create_dir("dynamic_debug", NULL);
833 if (!dir)
834 return -ENOMEM;
835 file = debugfs_create_file("control", 0644, dir, NULL,
836 &ddebug_proc_fops);
837 if (!file) {
838 debugfs_remove(dir);
839 return -ENOMEM;
840 }
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200841 return 0;
842}
843
844static int __init dynamic_debug_init(void)
845{
846 struct _ddebug *iter, *iter_start;
847 const char *modname = NULL;
848 int ret = 0;
849 int n = 0;
850
Jason Barone9d376f2009-02-05 11:51:38 -0500851 if (__start___verbose != __stop___verbose) {
852 iter = __start___verbose;
853 modname = iter->modname;
854 iter_start = iter;
855 for (; iter < __stop___verbose; iter++) {
856 if (strcmp(modname, iter->modname)) {
857 ret = ddebug_add_module(iter_start, n, modname);
858 if (ret)
859 goto out_free;
860 n = 0;
861 modname = iter->modname;
862 iter_start = iter;
863 }
864 n++;
865 }
866 ret = ddebug_add_module(iter_start, n, modname);
867 }
Thomas Renningera648ec02010-08-06 16:11:02 +0200868
869 /* ddebug_query boot param got passed -> set it up */
870 if (ddebug_setup_string[0] != '\0') {
871 ret = ddebug_exec_query(ddebug_setup_string);
872 if (ret)
Joe Perches4ad275e2011-08-11 14:36:33 -0400873 pr_warn("Invalid ddebug boot param %s",
874 ddebug_setup_string);
Thomas Renningera648ec02010-08-06 16:11:02 +0200875 else
876 pr_info("ddebug initialized with string %s",
877 ddebug_setup_string);
Jason Barone9d376f2009-02-05 11:51:38 -0500878 }
Thomas Renningera648ec02010-08-06 16:11:02 +0200879
Jason Barone9d376f2009-02-05 11:51:38 -0500880out_free:
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200881 if (ret)
Jason Barone9d376f2009-02-05 11:51:38 -0500882 ddebug_remove_all_tables();
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200883 else
884 ddebug_init_success = 1;
Jason Barone9d376f2009-02-05 11:51:38 -0500885 return 0;
886}
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200887/* Allow early initialization for boot messages via boot param */
888arch_initcall(dynamic_debug_init);
889/* Debugfs setup must be done later */
890module_init(dynamic_debug_init_debugfs);