4 * make pr_debug()/dev_dbg() calls runtime configurable based upon their
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.
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>
30 extern struct _ddebug __start___verbose
[];
31 extern struct _ddebug __stop___verbose
[];
33 /* dynamic_debug_enabled, and dynamic_debug_enabled2 are bitmasks in which
34 * bit n is set to 1 if any modname hashes into the bucket n, 0 otherwise. They
35 * use independent hash functions, to reduce the chance of false positives.
37 long long dynamic_debug_enabled
;
38 EXPORT_SYMBOL_GPL(dynamic_debug_enabled
);
39 long long dynamic_debug_enabled2
;
40 EXPORT_SYMBOL_GPL(dynamic_debug_enabled2
);
43 struct list_head link
;
45 unsigned int num_ddebugs
;
46 unsigned int num_enabled
;
47 struct _ddebug
*ddebugs
;
55 unsigned int first_lineno
, last_lineno
;
59 struct ddebug_table
*table
;
63 static DEFINE_MUTEX(ddebug_lock
);
64 static LIST_HEAD(ddebug_tables
);
65 static int verbose
= 0;
67 /* Return the last part of a pathname */
68 static inline const char *basename(const char *path
)
70 const char *tail
= strrchr(path
, '/');
71 return tail
? tail
+1 : path
;
74 /* format a string into buf[] which describes the _ddebug's flags */
75 static char *ddebug_describe_flags(struct _ddebug
*dp
, char *buf
,
81 if (dp
->flags
& _DPRINTK_FLAGS_PRINT
)
91 * must be called with ddebug_lock held
94 static int disabled_hash(char hash
, bool first_table
)
96 struct ddebug_table
*dt
;
97 char table_hash_value
;
99 list_for_each_entry(dt
, &ddebug_tables
, link
) {
101 table_hash_value
= dt
->ddebugs
->primary_hash
;
103 table_hash_value
= dt
->ddebugs
->secondary_hash
;
104 if (dt
->num_enabled
&& (hash
== table_hash_value
))
111 * Search the tables for _ddebug's which match the given
112 * `query' and apply the `flags' and `mask' to them. Tells
113 * the user which ddebug's were changed, or whether none
116 static void ddebug_change(const struct ddebug_query
*query
,
117 unsigned int flags
, unsigned int mask
)
120 struct ddebug_table
*dt
;
121 unsigned int newflags
;
122 unsigned int nfound
= 0;
125 /* search for matching ddebugs */
126 mutex_lock(&ddebug_lock
);
127 list_for_each_entry(dt
, &ddebug_tables
, link
) {
129 /* match against the module name */
130 if (query
->module
!= NULL
&&
131 strcmp(query
->module
, dt
->mod_name
))
134 for (i
= 0 ; i
< dt
->num_ddebugs
; i
++) {
135 struct _ddebug
*dp
= &dt
->ddebugs
[i
];
137 /* match against the source filename */
138 if (query
->filename
!= NULL
&&
139 strcmp(query
->filename
, dp
->filename
) &&
140 strcmp(query
->filename
, basename(dp
->filename
)))
143 /* match against the function */
144 if (query
->function
!= NULL
&&
145 strcmp(query
->function
, dp
->function
))
148 /* match against the format */
149 if (query
->format
!= NULL
&&
150 strstr(dp
->format
, query
->format
) == NULL
)
153 /* match against the line number range */
154 if (query
->first_lineno
&&
155 dp
->lineno
< query
->first_lineno
)
157 if (query
->last_lineno
&&
158 dp
->lineno
> query
->last_lineno
)
163 newflags
= (dp
->flags
& mask
) | flags
;
164 if (newflags
== dp
->flags
)
171 dp
->flags
= newflags
;
173 dynamic_debug_enabled
|=
174 (1LL << dp
->primary_hash
);
175 dynamic_debug_enabled2
|=
176 (1LL << dp
->secondary_hash
);
178 if (disabled_hash(dp
->primary_hash
, true))
179 dynamic_debug_enabled
&=
180 ~(1LL << dp
->primary_hash
);
181 if (disabled_hash(dp
->secondary_hash
, false))
182 dynamic_debug_enabled2
&=
183 ~(1LL << dp
->secondary_hash
);
187 "ddebug: changed %s:%d [%s]%s %s\n",
188 dp
->filename
, dp
->lineno
,
189 dt
->mod_name
, dp
->function
,
190 ddebug_describe_flags(dp
, flagbuf
,
194 mutex_unlock(&ddebug_lock
);
196 if (!nfound
&& verbose
)
197 printk(KERN_INFO
"ddebug: no matches for query\n");
201 * Split the buffer `buf' into space-separated words.
202 * Handles simple " and ' quoting, i.e. without nested,
203 * embedded or escaped \". Return the number of words
206 static int ddebug_tokenize(char *buf
, char *words
[], int maxwords
)
213 /* Skip leading whitespace */
214 buf
= skip_spaces(buf
);
216 break; /* oh, it was trailing whitespace */
218 /* Run `end' over a word, either whitespace separated or quoted */
219 if (*buf
== '"' || *buf
== '\'') {
221 for (end
= buf
; *end
&& *end
!= quote
; end
++)
224 return -EINVAL
; /* unclosed quote */
226 for (end
= buf
; *end
&& !isspace(*end
) ; end
++)
230 /* Here `buf' is the start of the word, `end' is one past the end */
232 if (nwords
== maxwords
)
233 return -EINVAL
; /* ran out of words[] before bytes */
235 *end
++ = '\0'; /* terminate the word */
236 words
[nwords
++] = buf
;
242 printk(KERN_INFO
"%s: split into words:", __func__
);
243 for (i
= 0 ; i
< nwords
; i
++)
244 printk(" \"%s\"", words
[i
]);
252 * Parse a single line number. Note that the empty string ""
253 * is treated as a special case and converted to zero, which
254 * is later treated as a "don't care" value.
256 static inline int parse_lineno(const char *str
, unsigned int *val
)
264 *val
= simple_strtoul(str
, &end
, 10);
265 return end
== NULL
|| end
== str
|| *end
!= '\0' ? -EINVAL
: 0;
269 * Undo octal escaping in a string, inplace. This is useful to
270 * allow the user to express a query which matches a format
271 * containing embedded spaces.
273 #define isodigit(c) ((c) >= '0' && (c) <= '7')
274 static char *unescape(char *str
)
285 } else if (in
[1] == 't') {
289 } else if (in
[1] == 'n') {
293 } else if (isodigit(in
[1]) &&
296 *out
++ = ((in
[1] - '0')<<6) |
311 * Parse words[] as a ddebug query specification, which is a series
312 * of (keyword, value) pairs chosen from these possibilities:
314 * func <function-name>
315 * file <full-pathname>
316 * file <base-filename>
317 * module <module-name>
318 * format <escaped-string-to-find-in-format>
320 * line <first-lineno>-<last-lineno> // where either may be empty
322 static int ddebug_parse_query(char *words
[], int nwords
,
323 struct ddebug_query
*query
)
327 /* check we have an even number of words */
330 memset(query
, 0, sizeof(*query
));
332 for (i
= 0 ; i
< nwords
; i
+= 2) {
333 if (!strcmp(words
[i
], "func"))
334 query
->function
= words
[i
+1];
335 else if (!strcmp(words
[i
], "file"))
336 query
->filename
= words
[i
+1];
337 else if (!strcmp(words
[i
], "module"))
338 query
->module
= words
[i
+1];
339 else if (!strcmp(words
[i
], "format"))
340 query
->format
= unescape(words
[i
+1]);
341 else if (!strcmp(words
[i
], "line")) {
342 char *first
= words
[i
+1];
343 char *last
= strchr(first
, '-');
346 if (parse_lineno(first
, &query
->first_lineno
) < 0)
349 /* range <first>-<last> */
350 if (parse_lineno(last
, &query
->last_lineno
) < 0)
353 query
->last_lineno
= query
->first_lineno
;
357 printk(KERN_ERR
"%s: unknown keyword \"%s\"\n",
364 printk(KERN_INFO
"%s: q->function=\"%s\" q->filename=\"%s\" "
365 "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n",
366 __func__
, query
->function
, query
->filename
,
367 query
->module
, query
->format
, query
->first_lineno
,
374 * Parse `str' as a flags specification, format [-+=][p]+.
375 * Sets up *maskp and *flagsp to be used when changing the
376 * flags fields of matched _ddebug's. Returns 0 on success
379 static int ddebug_parse_flags(const char *str
, unsigned int *flagsp
,
395 printk(KERN_INFO
"%s: op='%c'\n", __func__
, op
);
397 for ( ; *str
; ++str
) {
400 flags
|= _DPRINTK_FLAGS_PRINT
;
409 printk(KERN_INFO
"%s: flags=0x%x\n", __func__
, flags
);
411 /* calculate final *flagsp, *maskp according to mask and op */
427 printk(KERN_INFO
"%s: *flagsp=0x%x *maskp=0x%x\n",
428 __func__
, *flagsp
, *maskp
);
433 * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the
434 * command text from userspace, parses and executes it.
436 static ssize_t
ddebug_proc_write(struct file
*file
, const char __user
*ubuf
,
437 size_t len
, loff_t
*offp
)
439 unsigned int flags
= 0, mask
= 0;
440 struct ddebug_query query
;
443 char *words
[MAXWORDS
];
448 /* we don't check *offp -- multiple writes() are allowed */
449 if (len
> sizeof(tmpbuf
)-1)
451 if (copy_from_user(tmpbuf
, ubuf
, len
))
455 printk(KERN_INFO
"%s: read %d bytes from userspace\n",
458 nwords
= ddebug_tokenize(tmpbuf
, words
, MAXWORDS
);
461 if (ddebug_parse_query(words
, nwords
-1, &query
))
463 if (ddebug_parse_flags(words
[nwords
-1], &flags
, &mask
))
466 /* actually go and implement the change */
467 ddebug_change(&query
, flags
, mask
);
474 * Set the iterator to point to the first _ddebug object
475 * and return a pointer to that first object. Returns
476 * NULL if there are no _ddebugs at all.
478 static struct _ddebug
*ddebug_iter_first(struct ddebug_iter
*iter
)
480 if (list_empty(&ddebug_tables
)) {
485 iter
->table
= list_entry(ddebug_tables
.next
,
486 struct ddebug_table
, link
);
488 return &iter
->table
->ddebugs
[iter
->idx
];
492 * Advance the iterator to point to the next _ddebug
493 * object from the one the iterator currently points at,
494 * and returns a pointer to the new _ddebug. Returns
495 * NULL if the iterator has seen all the _ddebugs.
497 static struct _ddebug
*ddebug_iter_next(struct ddebug_iter
*iter
)
499 if (iter
->table
== NULL
)
501 if (++iter
->idx
== iter
->table
->num_ddebugs
) {
502 /* iterate to next table */
504 if (list_is_last(&iter
->table
->link
, &ddebug_tables
)) {
508 iter
->table
= list_entry(iter
->table
->link
.next
,
509 struct ddebug_table
, link
);
511 return &iter
->table
->ddebugs
[iter
->idx
];
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.
519 static void *ddebug_proc_start(struct seq_file
*m
, loff_t
*pos
)
521 struct ddebug_iter
*iter
= m
->private;
526 printk(KERN_INFO
"%s: called m=%p *pos=%lld\n",
527 __func__
, m
, (unsigned long long)*pos
);
529 mutex_lock(&ddebug_lock
);
532 return SEQ_START_TOKEN
;
535 dp
= ddebug_iter_first(iter
);
536 while (dp
!= NULL
&& --n
> 0)
537 dp
= ddebug_iter_next(iter
);
542 * Seq_ops next method. Called several times within a read()
543 * call from userspace, with ddebug_lock held. Walks to the
544 * next _ddebug object with a special case for the header line.
546 static void *ddebug_proc_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
548 struct ddebug_iter
*iter
= m
->private;
552 printk(KERN_INFO
"%s: called m=%p p=%p *pos=%lld\n",
553 __func__
, m
, p
, (unsigned long long)*pos
);
555 if (p
== SEQ_START_TOKEN
)
556 dp
= ddebug_iter_first(iter
);
558 dp
= ddebug_iter_next(iter
);
564 * Seq_ops show method. Called several times within a read()
565 * call from userspace, with ddebug_lock held. Formats the
566 * current _ddebug as a single human-readable line, with a
567 * special case for the header line.
569 static int ddebug_proc_show(struct seq_file
*m
, void *p
)
571 struct ddebug_iter
*iter
= m
->private;
572 struct _ddebug
*dp
= p
;
576 printk(KERN_INFO
"%s: called m=%p p=%p\n",
579 if (p
== SEQ_START_TOKEN
) {
581 "# filename:lineno [module]function flags format\n");
585 seq_printf(m
, "%s:%u [%s]%s %s \"",
586 dp
->filename
, dp
->lineno
,
587 iter
->table
->mod_name
, dp
->function
,
588 ddebug_describe_flags(dp
, flagsbuf
, sizeof(flagsbuf
)));
589 seq_escape(m
, dp
->format
, "\t\r\n\"");
596 * Seq_ops stop method. Called at the end of each read()
597 * call from userspace. Drops ddebug_lock.
599 static void ddebug_proc_stop(struct seq_file
*m
, void *p
)
602 printk(KERN_INFO
"%s: called m=%p p=%p\n",
604 mutex_unlock(&ddebug_lock
);
607 static const struct seq_operations ddebug_proc_seqops
= {
608 .start
= ddebug_proc_start
,
609 .next
= ddebug_proc_next
,
610 .show
= ddebug_proc_show
,
611 .stop
= ddebug_proc_stop
615 * File_ops->open method for <debugfs>/dynamic_debug/control. Does the seq_file
616 * setup dance, and also creates an iterator to walk the _ddebugs.
617 * Note that we create a seq_file always, even for O_WRONLY files
618 * where it's not needed, as doing so simplifies the ->release method.
620 static int ddebug_proc_open(struct inode
*inode
, struct file
*file
)
622 struct ddebug_iter
*iter
;
626 printk(KERN_INFO
"%s: called\n", __func__
);
628 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
632 err
= seq_open(file
, &ddebug_proc_seqops
);
637 ((struct seq_file
*) file
->private_data
)->private = iter
;
641 static const struct file_operations ddebug_proc_fops
= {
642 .owner
= THIS_MODULE
,
643 .open
= ddebug_proc_open
,
646 .release
= seq_release_private
,
647 .write
= ddebug_proc_write
651 * Allocate a new ddebug_table for the given module
652 * and add it to the global list.
654 int ddebug_add_module(struct _ddebug
*tab
, unsigned int n
,
657 struct ddebug_table
*dt
;
660 dt
= kzalloc(sizeof(*dt
), GFP_KERNEL
);
663 new_name
= kstrdup(name
, GFP_KERNEL
);
664 if (new_name
== NULL
) {
668 dt
->mod_name
= new_name
;
673 mutex_lock(&ddebug_lock
);
674 list_add_tail(&dt
->link
, &ddebug_tables
);
675 mutex_unlock(&ddebug_lock
);
678 printk(KERN_INFO
"%u debug prints in module %s\n",
682 EXPORT_SYMBOL_GPL(ddebug_add_module
);
684 static void ddebug_table_free(struct ddebug_table
*dt
)
686 list_del_init(&dt
->link
);
692 * Called in response to a module being unloaded. Removes
693 * any ddebug_table's which point at the module.
695 int ddebug_remove_module(const char *mod_name
)
697 struct ddebug_table
*dt
, *nextdt
;
701 printk(KERN_INFO
"%s: removing module \"%s\"\n",
704 mutex_lock(&ddebug_lock
);
705 list_for_each_entry_safe(dt
, nextdt
, &ddebug_tables
, link
) {
706 if (!strcmp(dt
->mod_name
, mod_name
)) {
707 ddebug_table_free(dt
);
711 mutex_unlock(&ddebug_lock
);
714 EXPORT_SYMBOL_GPL(ddebug_remove_module
);
716 static void ddebug_remove_all_tables(void)
718 mutex_lock(&ddebug_lock
);
719 while (!list_empty(&ddebug_tables
)) {
720 struct ddebug_table
*dt
= list_entry(ddebug_tables
.next
,
723 ddebug_table_free(dt
);
725 mutex_unlock(&ddebug_lock
);
728 static int __init
dynamic_debug_init(void)
730 struct dentry
*dir
, *file
;
731 struct _ddebug
*iter
, *iter_start
;
732 const char *modname
= NULL
;
736 dir
= debugfs_create_dir("dynamic_debug", NULL
);
739 file
= debugfs_create_file("control", 0644, dir
, NULL
,
745 if (__start___verbose
!= __stop___verbose
) {
746 iter
= __start___verbose
;
747 modname
= iter
->modname
;
749 for (; iter
< __stop___verbose
; iter
++) {
750 if (strcmp(modname
, iter
->modname
)) {
751 ret
= ddebug_add_module(iter_start
, n
, modname
);
755 modname
= iter
->modname
;
760 ret
= ddebug_add_module(iter_start
, n
, modname
);
764 ddebug_remove_all_tables();
766 debugfs_remove(file
);
770 module_init(dynamic_debug_init
);