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/uaccess.h>
25 #include <linux/dynamic_debug.h>
26 #include <linux/debugfs.h>
28 extern struct _ddebug __start___verbose
[];
29 extern struct _ddebug __stop___verbose
[];
31 /* dynamic_debug_enabled, and dynamic_debug_enabled2 are bitmasks in which
32 * bit n is set to 1 if any modname hashes into the bucket n, 0 otherwise. They
33 * use independent hash functions, to reduce the chance of false positives.
35 long long dynamic_debug_enabled
;
36 EXPORT_SYMBOL_GPL(dynamic_debug_enabled
);
37 long long dynamic_debug_enabled2
;
38 EXPORT_SYMBOL_GPL(dynamic_debug_enabled2
);
41 struct list_head link
;
43 unsigned int num_ddebugs
;
44 unsigned int num_enabled
;
45 struct _ddebug
*ddebugs
;
53 unsigned int first_lineno
, last_lineno
;
57 struct ddebug_table
*table
;
61 static DEFINE_MUTEX(ddebug_lock
);
62 static LIST_HEAD(ddebug_tables
);
63 static int verbose
= 0;
65 /* Return the last part of a pathname */
66 static inline const char *basename(const char *path
)
68 const char *tail
= strrchr(path
, '/');
69 return tail
? tail
+1 : path
;
72 /* format a string into buf[] which describes the _ddebug's flags */
73 static char *ddebug_describe_flags(struct _ddebug
*dp
, char *buf
,
79 if (dp
->flags
& _DPRINTK_FLAGS_PRINT
)
89 * must be called with ddebug_lock held
92 static int disabled_hash(char hash
, bool first_table
)
94 struct ddebug_table
*dt
;
95 char table_hash_value
;
97 list_for_each_entry(dt
, &ddebug_tables
, link
) {
99 table_hash_value
= dt
->ddebugs
->primary_hash
;
101 table_hash_value
= dt
->ddebugs
->secondary_hash
;
102 if (dt
->num_enabled
&& (hash
== table_hash_value
))
109 * Search the tables for _ddebug's which match the given
110 * `query' and apply the `flags' and `mask' to them. Tells
111 * the user which ddebug's were changed, or whether none
114 static void ddebug_change(const struct ddebug_query
*query
,
115 unsigned int flags
, unsigned int mask
)
118 struct ddebug_table
*dt
;
119 unsigned int newflags
;
120 unsigned int nfound
= 0;
123 /* search for matching ddebugs */
124 mutex_lock(&ddebug_lock
);
125 list_for_each_entry(dt
, &ddebug_tables
, link
) {
127 /* match against the module name */
128 if (query
->module
!= NULL
&&
129 strcmp(query
->module
, dt
->mod_name
))
132 for (i
= 0 ; i
< dt
->num_ddebugs
; i
++) {
133 struct _ddebug
*dp
= &dt
->ddebugs
[i
];
135 /* match against the source filename */
136 if (query
->filename
!= NULL
&&
137 strcmp(query
->filename
, dp
->filename
) &&
138 strcmp(query
->filename
, basename(dp
->filename
)))
141 /* match against the function */
142 if (query
->function
!= NULL
&&
143 strcmp(query
->function
, dp
->function
))
146 /* match against the format */
147 if (query
->format
!= NULL
&&
148 strstr(dp
->format
, query
->format
) == NULL
)
151 /* match against the line number range */
152 if (query
->first_lineno
&&
153 dp
->lineno
< query
->first_lineno
)
155 if (query
->last_lineno
&&
156 dp
->lineno
> query
->last_lineno
)
161 newflags
= (dp
->flags
& mask
) | flags
;
162 if (newflags
== dp
->flags
)
169 dp
->flags
= newflags
;
171 dynamic_debug_enabled
|=
172 (1LL << dp
->primary_hash
);
173 dynamic_debug_enabled2
|=
174 (1LL << dp
->secondary_hash
);
176 if (disabled_hash(dp
->primary_hash
, true))
177 dynamic_debug_enabled
&=
178 ~(1LL << dp
->primary_hash
);
179 if (disabled_hash(dp
->secondary_hash
, false))
180 dynamic_debug_enabled2
&=
181 ~(1LL << dp
->secondary_hash
);
185 "ddebug: changed %s:%d [%s]%s %s\n",
186 dp
->filename
, dp
->lineno
,
187 dt
->mod_name
, dp
->function
,
188 ddebug_describe_flags(dp
, flagbuf
,
192 mutex_unlock(&ddebug_lock
);
194 if (!nfound
&& verbose
)
195 printk(KERN_INFO
"ddebug: no matches for query\n");
199 * Split the buffer `buf' into space-separated words.
200 * Handles simple " and ' quoting, i.e. without nested,
201 * embedded or escaped \". Return the number of words
204 static int ddebug_tokenize(char *buf
, char *words
[], int maxwords
)
211 /* Skip leading whitespace */
212 while (*buf
&& isspace(*buf
))
215 break; /* oh, it was trailing whitespace */
217 /* Run `end' over a word, either whitespace separated or quoted */
218 if (*buf
== '"' || *buf
== '\'') {
220 for (end
= buf
; *end
&& *end
!= quote
; end
++)
223 return -EINVAL
; /* unclosed quote */
225 for (end
= buf
; *end
&& !isspace(*end
) ; end
++)
229 /* Here `buf' is the start of the word, `end' is one past the end */
231 if (nwords
== maxwords
)
232 return -EINVAL
; /* ran out of words[] before bytes */
234 *end
++ = '\0'; /* terminate the word */
235 words
[nwords
++] = buf
;
241 printk(KERN_INFO
"%s: split into words:", __func__
);
242 for (i
= 0 ; i
< nwords
; i
++)
243 printk(" \"%s\"", words
[i
]);
251 * Parse a single line number. Note that the empty string ""
252 * is treated as a special case and converted to zero, which
253 * is later treated as a "don't care" value.
255 static inline int parse_lineno(const char *str
, unsigned int *val
)
263 *val
= simple_strtoul(str
, &end
, 10);
264 return end
== NULL
|| end
== str
|| *end
!= '\0' ? -EINVAL
: 0;
268 * Undo octal escaping in a string, inplace. This is useful to
269 * allow the user to express a query which matches a format
270 * containing embedded spaces.
272 #define isodigit(c) ((c) >= '0' && (c) <= '7')
273 static char *unescape(char *str
)
284 } else if (in
[1] == 't') {
288 } else if (in
[1] == 'n') {
292 } else if (isodigit(in
[1]) &&
295 *out
++ = ((in
[1] - '0')<<6) |
310 * Parse words[] as a ddebug query specification, which is a series
311 * of (keyword, value) pairs chosen from these possibilities:
313 * func <function-name>
314 * file <full-pathname>
315 * file <base-filename>
316 * module <module-name>
317 * format <escaped-string-to-find-in-format>
319 * line <first-lineno>-<last-lineno> // where either may be empty
321 static int ddebug_parse_query(char *words
[], int nwords
,
322 struct ddebug_query
*query
)
326 /* check we have an even number of words */
329 memset(query
, 0, sizeof(*query
));
331 for (i
= 0 ; i
< nwords
; i
+= 2) {
332 if (!strcmp(words
[i
], "func"))
333 query
->function
= words
[i
+1];
334 else if (!strcmp(words
[i
], "file"))
335 query
->filename
= words
[i
+1];
336 else if (!strcmp(words
[i
], "module"))
337 query
->module
= words
[i
+1];
338 else if (!strcmp(words
[i
], "format"))
339 query
->format
= unescape(words
[i
+1]);
340 else if (!strcmp(words
[i
], "line")) {
341 char *first
= words
[i
+1];
342 char *last
= strchr(first
, '-');
345 if (parse_lineno(first
, &query
->first_lineno
) < 0)
348 /* range <first>-<last> */
349 if (parse_lineno(last
, &query
->last_lineno
) < 0)
352 query
->last_lineno
= query
->first_lineno
;
356 printk(KERN_ERR
"%s: unknown keyword \"%s\"\n",
363 printk(KERN_INFO
"%s: q->function=\"%s\" q->filename=\"%s\" "
364 "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n",
365 __func__
, query
->function
, query
->filename
,
366 query
->module
, query
->format
, query
->first_lineno
,
373 * Parse `str' as a flags specification, format [-+=][p]+.
374 * Sets up *maskp and *flagsp to be used when changing the
375 * flags fields of matched _ddebug's. Returns 0 on success
378 static int ddebug_parse_flags(const char *str
, unsigned int *flagsp
,
394 printk(KERN_INFO
"%s: op='%c'\n", __func__
, op
);
396 for ( ; *str
; ++str
) {
399 flags
|= _DPRINTK_FLAGS_PRINT
;
408 printk(KERN_INFO
"%s: flags=0x%x\n", __func__
, flags
);
410 /* calculate final *flagsp, *maskp according to mask and op */
426 printk(KERN_INFO
"%s: *flagsp=0x%x *maskp=0x%x\n",
427 __func__
, *flagsp
, *maskp
);
432 * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the
433 * command text from userspace, parses and executes it.
435 static ssize_t
ddebug_proc_write(struct file
*file
, const char __user
*ubuf
,
436 size_t len
, loff_t
*offp
)
438 unsigned int flags
= 0, mask
= 0;
439 struct ddebug_query query
;
442 char *words
[MAXWORDS
];
447 /* we don't check *offp -- multiple writes() are allowed */
448 if (len
> sizeof(tmpbuf
)-1)
450 if (copy_from_user(tmpbuf
, ubuf
, len
))
454 printk(KERN_INFO
"%s: read %d bytes from userspace\n",
457 nwords
= ddebug_tokenize(tmpbuf
, words
, MAXWORDS
);
460 if (ddebug_parse_query(words
, nwords
-1, &query
))
462 if (ddebug_parse_flags(words
[nwords
-1], &flags
, &mask
))
465 /* actually go and implement the change */
466 ddebug_change(&query
, flags
, mask
);
473 * Set the iterator to point to the first _ddebug object
474 * and return a pointer to that first object. Returns
475 * NULL if there are no _ddebugs at all.
477 static struct _ddebug
*ddebug_iter_first(struct ddebug_iter
*iter
)
479 if (list_empty(&ddebug_tables
)) {
484 iter
->table
= list_entry(ddebug_tables
.next
,
485 struct ddebug_table
, link
);
487 return &iter
->table
->ddebugs
[iter
->idx
];
491 * Advance the iterator to point to the next _ddebug
492 * object from the one the iterator currently points at,
493 * and returns a pointer to the new _ddebug. Returns
494 * NULL if the iterator has seen all the _ddebugs.
496 static struct _ddebug
*ddebug_iter_next(struct ddebug_iter
*iter
)
498 if (iter
->table
== NULL
)
500 if (++iter
->idx
== iter
->table
->num_ddebugs
) {
501 /* iterate to next table */
503 if (list_is_last(&iter
->table
->link
, &ddebug_tables
)) {
507 iter
->table
= list_entry(iter
->table
->link
.next
,
508 struct ddebug_table
, link
);
510 return &iter
->table
->ddebugs
[iter
->idx
];
514 * Seq_ops start method. Called at the start of every
515 * read() call from userspace. Takes the ddebug_lock and
516 * seeks the seq_file's iterator to the given position.
518 static void *ddebug_proc_start(struct seq_file
*m
, loff_t
*pos
)
520 struct ddebug_iter
*iter
= m
->private;
525 printk(KERN_INFO
"%s: called m=%p *pos=%lld\n",
526 __func__
, m
, (unsigned long long)*pos
);
528 mutex_lock(&ddebug_lock
);
531 return SEQ_START_TOKEN
;
534 dp
= ddebug_iter_first(iter
);
535 while (dp
!= NULL
&& --n
> 0)
536 dp
= ddebug_iter_next(iter
);
541 * Seq_ops next method. Called several times within a read()
542 * call from userspace, with ddebug_lock held. Walks to the
543 * next _ddebug object with a special case for the header line.
545 static void *ddebug_proc_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
547 struct ddebug_iter
*iter
= m
->private;
551 printk(KERN_INFO
"%s: called m=%p p=%p *pos=%lld\n",
552 __func__
, m
, p
, (unsigned long long)*pos
);
554 if (p
== SEQ_START_TOKEN
)
555 dp
= ddebug_iter_first(iter
);
557 dp
= ddebug_iter_next(iter
);
563 * Seq_ops show method. Called several times within a read()
564 * call from userspace, with ddebug_lock held. Formats the
565 * current _ddebug as a single human-readable line, with a
566 * special case for the header line.
568 static int ddebug_proc_show(struct seq_file
*m
, void *p
)
570 struct ddebug_iter
*iter
= m
->private;
571 struct _ddebug
*dp
= p
;
575 printk(KERN_INFO
"%s: called m=%p p=%p\n",
578 if (p
== SEQ_START_TOKEN
) {
580 "# filename:lineno [module]function flags format\n");
584 seq_printf(m
, "%s:%u [%s]%s %s \"",
585 dp
->filename
, dp
->lineno
,
586 iter
->table
->mod_name
, dp
->function
,
587 ddebug_describe_flags(dp
, flagsbuf
, sizeof(flagsbuf
)));
588 seq_escape(m
, dp
->format
, "\t\r\n\"");
595 * Seq_ops stop method. Called at the end of each read()
596 * call from userspace. Drops ddebug_lock.
598 static void ddebug_proc_stop(struct seq_file
*m
, void *p
)
601 printk(KERN_INFO
"%s: called m=%p p=%p\n",
603 mutex_unlock(&ddebug_lock
);
606 static const struct seq_operations ddebug_proc_seqops
= {
607 .start
= ddebug_proc_start
,
608 .next
= ddebug_proc_next
,
609 .show
= ddebug_proc_show
,
610 .stop
= ddebug_proc_stop
614 * File_ops->open method for <debugfs>/dynamic_debug/control. Does the seq_file
615 * setup dance, and also creates an iterator to walk the _ddebugs.
616 * Note that we create a seq_file always, even for O_WRONLY files
617 * where it's not needed, as doing so simplifies the ->release method.
619 static int ddebug_proc_open(struct inode
*inode
, struct file
*file
)
621 struct ddebug_iter
*iter
;
625 printk(KERN_INFO
"%s: called\n", __func__
);
627 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
631 err
= seq_open(file
, &ddebug_proc_seqops
);
636 ((struct seq_file
*) file
->private_data
)->private = iter
;
640 static const struct file_operations ddebug_proc_fops
= {
641 .owner
= THIS_MODULE
,
642 .open
= ddebug_proc_open
,
645 .release
= seq_release_private
,
646 .write
= ddebug_proc_write
650 * Allocate a new ddebug_table for the given module
651 * and add it to the global list.
653 int ddebug_add_module(struct _ddebug
*tab
, unsigned int n
,
656 struct ddebug_table
*dt
;
659 dt
= kzalloc(sizeof(*dt
), GFP_KERNEL
);
662 new_name
= kstrdup(name
, GFP_KERNEL
);
663 if (new_name
== NULL
) {
667 dt
->mod_name
= new_name
;
672 mutex_lock(&ddebug_lock
);
673 list_add_tail(&dt
->link
, &ddebug_tables
);
674 mutex_unlock(&ddebug_lock
);
677 printk(KERN_INFO
"%u debug prints in module %s\n",
681 EXPORT_SYMBOL_GPL(ddebug_add_module
);
683 static void ddebug_table_free(struct ddebug_table
*dt
)
685 list_del_init(&dt
->link
);
691 * Called in response to a module being unloaded. Removes
692 * any ddebug_table's which point at the module.
694 int ddebug_remove_module(char *mod_name
)
696 struct ddebug_table
*dt
, *nextdt
;
700 printk(KERN_INFO
"%s: removing module \"%s\"\n",
703 mutex_lock(&ddebug_lock
);
704 list_for_each_entry_safe(dt
, nextdt
, &ddebug_tables
, link
) {
705 if (!strcmp(dt
->mod_name
, mod_name
)) {
706 ddebug_table_free(dt
);
710 mutex_unlock(&ddebug_lock
);
713 EXPORT_SYMBOL_GPL(ddebug_remove_module
);
715 static void ddebug_remove_all_tables(void)
717 mutex_lock(&ddebug_lock
);
718 while (!list_empty(&ddebug_tables
)) {
719 struct ddebug_table
*dt
= list_entry(ddebug_tables
.next
,
722 ddebug_table_free(dt
);
724 mutex_unlock(&ddebug_lock
);
727 static int __init
dynamic_debug_init(void)
729 struct dentry
*dir
, *file
;
730 struct _ddebug
*iter
, *iter_start
;
731 const char *modname
= NULL
;
735 dir
= debugfs_create_dir("dynamic_debug", NULL
);
738 file
= debugfs_create_file("control", 0644, dir
, NULL
,
744 if (__start___verbose
!= __stop___verbose
) {
745 iter
= __start___verbose
;
746 modname
= iter
->modname
;
748 for (; iter
< __stop___verbose
; iter
++) {
749 if (strcmp(modname
, iter
->modname
)) {
750 ret
= ddebug_add_module(iter_start
, n
, modname
);
754 modname
= iter
->modname
;
759 ret
= ddebug_add_module(iter_start
, n
, modname
);
763 ddebug_remove_all_tables();
765 debugfs_remove(file
);
769 module_init(dynamic_debug_init
);