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.
10 * Copyright (C) 2011 Bart Van Assche. All Rights Reserved.
13 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
15 #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>
27 #include <linux/string.h>
28 #include <linux/uaccess.h>
29 #include <linux/dynamic_debug.h>
30 #include <linux/debugfs.h>
31 #include <linux/slab.h>
32 #include <linux/jump_label.h>
33 #include <linux/hardirq.h>
34 #include <linux/sched.h>
35 #include <linux/device.h>
36 #include <linux/netdevice.h>
38 extern struct _ddebug __start___verbose
[];
39 extern struct _ddebug __stop___verbose
[];
42 struct list_head link
;
44 unsigned int num_ddebugs
;
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 static 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' },
80 /* format a string into buf[] which describes the _ddebug's flags */
81 static char *ddebug_describe_flags(struct _ddebug
*dp
, char *buf
,
88 for (i
= 0; i
< ARRAY_SIZE(opt_array
); ++i
)
89 if (dp
->flags
& opt_array
[i
].flag
)
90 *p
++ = opt_array
[i
].opt_char
;
99 * 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
104 static void ddebug_change(const struct ddebug_query
*query
,
105 unsigned int flags
, unsigned int mask
)
108 struct ddebug_table
*dt
;
109 unsigned int newflags
;
110 unsigned int nfound
= 0;
113 /* search for matching ddebugs */
114 mutex_lock(&ddebug_lock
);
115 list_for_each_entry(dt
, &ddebug_tables
, link
) {
117 /* match against the module name */
118 if (query
->module
!= NULL
&&
119 strcmp(query
->module
, dt
->mod_name
))
122 for (i
= 0 ; i
< dt
->num_ddebugs
; i
++) {
123 struct _ddebug
*dp
= &dt
->ddebugs
[i
];
125 /* match against the source filename */
126 if (query
->filename
!= NULL
&&
127 strcmp(query
->filename
, dp
->filename
) &&
128 strcmp(query
->filename
, basename(dp
->filename
)))
131 /* match against the function */
132 if (query
->function
!= NULL
&&
133 strcmp(query
->function
, dp
->function
))
136 /* match against the format */
137 if (query
->format
!= NULL
&&
138 strstr(dp
->format
, query
->format
) == NULL
)
141 /* match against the line number range */
142 if (query
->first_lineno
&&
143 dp
->lineno
< query
->first_lineno
)
145 if (query
->last_lineno
&&
146 dp
->lineno
> query
->last_lineno
)
151 newflags
= (dp
->flags
& mask
) | flags
;
152 if (newflags
== dp
->flags
)
154 dp
->flags
= newflags
;
160 pr_info("changed %s:%d [%s]%s %s\n",
161 dp
->filename
, dp
->lineno
,
162 dt
->mod_name
, dp
->function
,
163 ddebug_describe_flags(dp
, flagbuf
,
167 mutex_unlock(&ddebug_lock
);
169 if (!nfound
&& verbose
)
170 pr_info("no matches for query\n");
174 * Split the buffer `buf' into space-separated words.
175 * Handles simple " and ' quoting, i.e. without nested,
176 * embedded or escaped \". Return the number of words
179 static int ddebug_tokenize(char *buf
, char *words
[], int maxwords
)
186 /* Skip leading whitespace */
187 buf
= skip_spaces(buf
);
189 break; /* oh, it was trailing whitespace */
191 /* Run `end' over a word, either whitespace separated or quoted */
192 if (*buf
== '"' || *buf
== '\'') {
194 for (end
= buf
; *end
&& *end
!= quote
; end
++)
197 return -EINVAL
; /* unclosed quote */
199 for (end
= buf
; *end
&& !isspace(*end
) ; end
++)
203 /* Here `buf' is the start of the word, `end' is one past the end */
205 if (nwords
== maxwords
)
206 return -EINVAL
; /* ran out of words[] before bytes */
208 *end
++ = '\0'; /* terminate the word */
209 words
[nwords
++] = buf
;
215 pr_info("split into words:");
216 for (i
= 0 ; i
< nwords
; i
++)
217 pr_cont(" \"%s\"", words
[i
]);
225 * Parse a single line number. Note that the empty string ""
226 * is treated as a special case and converted to zero, which
227 * is later treated as a "don't care" value.
229 static inline int parse_lineno(const char *str
, unsigned int *val
)
237 *val
= simple_strtoul(str
, &end
, 10);
238 return end
== NULL
|| end
== str
|| *end
!= '\0' ? -EINVAL
: 0;
242 * Undo octal escaping in a string, inplace. This is useful to
243 * allow the user to express a query which matches a format
244 * containing embedded spaces.
246 #define isodigit(c) ((c) >= '0' && (c) <= '7')
247 static char *unescape(char *str
)
258 } else if (in
[1] == 't') {
262 } else if (in
[1] == 'n') {
266 } else if (isodigit(in
[1]) &&
269 *out
++ = ((in
[1] - '0')<<6) |
284 * Parse words[] as a ddebug query specification, which is a series
285 * of (keyword, value) pairs chosen from these possibilities:
287 * func <function-name>
288 * file <full-pathname>
289 * file <base-filename>
290 * module <module-name>
291 * format <escaped-string-to-find-in-format>
293 * line <first-lineno>-<last-lineno> // where either may be empty
295 static int ddebug_parse_query(char *words
[], int nwords
,
296 struct ddebug_query
*query
)
300 /* check we have an even number of words */
303 memset(query
, 0, sizeof(*query
));
305 for (i
= 0 ; i
< nwords
; i
+= 2) {
306 if (!strcmp(words
[i
], "func"))
307 query
->function
= words
[i
+1];
308 else if (!strcmp(words
[i
], "file"))
309 query
->filename
= words
[i
+1];
310 else if (!strcmp(words
[i
], "module"))
311 query
->module
= words
[i
+1];
312 else if (!strcmp(words
[i
], "format"))
313 query
->format
= unescape(words
[i
+1]);
314 else if (!strcmp(words
[i
], "line")) {
315 char *first
= words
[i
+1];
316 char *last
= strchr(first
, '-');
319 if (parse_lineno(first
, &query
->first_lineno
) < 0)
322 /* range <first>-<last> */
323 if (parse_lineno(last
, &query
->last_lineno
) < 0)
326 query
->last_lineno
= query
->first_lineno
;
330 pr_err("unknown keyword \"%s\"\n", words
[i
]);
336 pr_info("q->function=\"%s\" q->filename=\"%s\" "
337 "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n",
338 query
->function
, query
->filename
,
339 query
->module
, query
->format
, query
->first_lineno
,
346 * Parse `str' as a flags specification, format [-+=][p]+.
347 * Sets up *maskp and *flagsp to be used when changing the
348 * flags fields of matched _ddebug's. Returns 0 on success
351 static int ddebug_parse_flags(const char *str
, unsigned int *flagsp
,
367 pr_info("op='%c'\n", op
);
369 for ( ; *str
; ++str
) {
370 for (i
= ARRAY_SIZE(opt_array
) - 1; i
>= 0; i
--) {
371 if (*str
== opt_array
[i
].opt_char
) {
372 flags
|= opt_array
[i
].flag
;
382 pr_info("flags=0x%x\n", flags
);
384 /* calculate final *flagsp, *maskp according to mask and op */
400 pr_info("*flagsp=0x%x *maskp=0x%x\n", *flagsp
, *maskp
);
404 static int ddebug_exec_query(char *query_string
)
406 unsigned int flags
= 0, mask
= 0;
407 struct ddebug_query query
;
410 char *words
[MAXWORDS
];
412 nwords
= ddebug_tokenize(query_string
, words
, MAXWORDS
);
415 if (ddebug_parse_query(words
, nwords
-1, &query
))
417 if (ddebug_parse_flags(words
[nwords
-1], &flags
, &mask
))
420 /* actually go and implement the change */
421 ddebug_change(&query
, flags
, mask
);
425 static int dynamic_emit_prefix(const struct _ddebug
*descriptor
)
427 char tid
[sizeof(int) + sizeof(int)/2 + 4];
428 char lineno
[sizeof(int) + sizeof(int)/2];
430 if (descriptor
->flags
& _DPRINTK_FLAGS_INCL_TID
) {
432 snprintf(tid
, sizeof(tid
), "%s", "<intr> ");
434 snprintf(tid
, sizeof(tid
), "[%d] ",
435 task_pid_vnr(current
));
440 if (descriptor
->flags
& _DPRINTK_FLAGS_INCL_LINENO
)
441 snprintf(lineno
, sizeof(lineno
), "%d", descriptor
->lineno
);
445 return printk(KERN_DEBUG
"%s%s%s%s%s%s",
447 (descriptor
->flags
& _DPRINTK_FLAGS_INCL_MODNAME
) ?
448 descriptor
->modname
: "",
449 (descriptor
->flags
& _DPRINTK_FLAGS_INCL_MODNAME
) ?
451 (descriptor
->flags
& _DPRINTK_FLAGS_INCL_FUNCNAME
) ?
452 descriptor
->function
: "",
453 (descriptor
->flags
& _DPRINTK_FLAGS_INCL_FUNCNAME
) ?
458 int __dynamic_pr_debug(struct _ddebug
*descriptor
, const char *fmt
, ...)
468 res
= dynamic_emit_prefix(descriptor
);
469 res
+= vprintk(fmt
, args
);
475 EXPORT_SYMBOL(__dynamic_pr_debug
);
477 int __dynamic_dev_dbg(struct _ddebug
*descriptor
,
478 const struct device
*dev
, const char *fmt
, ...)
480 struct va_format vaf
;
492 res
= dynamic_emit_prefix(descriptor
);
493 res
+= __dev_printk(KERN_CONT
, dev
, &vaf
);
499 EXPORT_SYMBOL(__dynamic_dev_dbg
);
501 int __dynamic_netdev_dbg(struct _ddebug
*descriptor
,
502 const struct net_device
*dev
, const char *fmt
, ...)
504 struct va_format vaf
;
516 res
= dynamic_emit_prefix(descriptor
);
517 res
+= __netdev_printk(KERN_CONT
, dev
, &vaf
);
523 EXPORT_SYMBOL(__dynamic_netdev_dbg
);
525 static __initdata
char ddebug_setup_string
[1024];
526 static __init
int ddebug_setup_query(char *str
)
528 if (strlen(str
) >= 1024) {
529 pr_warn("ddebug boot param string too large\n");
532 strcpy(ddebug_setup_string
, str
);
536 __setup("ddebug_query=", ddebug_setup_query
);
539 * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the
540 * command text from userspace, parses and executes it.
542 static ssize_t
ddebug_proc_write(struct file
*file
, const char __user
*ubuf
,
543 size_t len
, loff_t
*offp
)
550 /* we don't check *offp -- multiple writes() are allowed */
551 if (len
> sizeof(tmpbuf
)-1)
553 if (copy_from_user(tmpbuf
, ubuf
, len
))
557 pr_info("read %d bytes from userspace\n", (int)len
);
559 ret
= ddebug_exec_query(tmpbuf
);
568 * Set the iterator to point to the first _ddebug object
569 * and return a pointer to that first object. Returns
570 * NULL if there are no _ddebugs at all.
572 static struct _ddebug
*ddebug_iter_first(struct ddebug_iter
*iter
)
574 if (list_empty(&ddebug_tables
)) {
579 iter
->table
= list_entry(ddebug_tables
.next
,
580 struct ddebug_table
, link
);
582 return &iter
->table
->ddebugs
[iter
->idx
];
586 * Advance the iterator to point to the next _ddebug
587 * object from the one the iterator currently points at,
588 * and returns a pointer to the new _ddebug. Returns
589 * NULL if the iterator has seen all the _ddebugs.
591 static struct _ddebug
*ddebug_iter_next(struct ddebug_iter
*iter
)
593 if (iter
->table
== NULL
)
595 if (++iter
->idx
== iter
->table
->num_ddebugs
) {
596 /* iterate to next table */
598 if (list_is_last(&iter
->table
->link
, &ddebug_tables
)) {
602 iter
->table
= list_entry(iter
->table
->link
.next
,
603 struct ddebug_table
, link
);
605 return &iter
->table
->ddebugs
[iter
->idx
];
609 * Seq_ops start method. Called at the start of every
610 * read() call from userspace. Takes the ddebug_lock and
611 * seeks the seq_file's iterator to the given position.
613 static void *ddebug_proc_start(struct seq_file
*m
, loff_t
*pos
)
615 struct ddebug_iter
*iter
= m
->private;
620 pr_info("called m=%p *pos=%lld\n", m
, (unsigned long long)*pos
);
622 mutex_lock(&ddebug_lock
);
625 return SEQ_START_TOKEN
;
628 dp
= ddebug_iter_first(iter
);
629 while (dp
!= NULL
&& --n
> 0)
630 dp
= ddebug_iter_next(iter
);
635 * Seq_ops next method. Called several times within a read()
636 * call from userspace, with ddebug_lock held. Walks to the
637 * next _ddebug object with a special case for the header line.
639 static void *ddebug_proc_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
641 struct ddebug_iter
*iter
= m
->private;
645 pr_info("called m=%p p=%p *pos=%lld\n",
646 m
, p
, (unsigned long long)*pos
);
648 if (p
== SEQ_START_TOKEN
)
649 dp
= ddebug_iter_first(iter
);
651 dp
= ddebug_iter_next(iter
);
657 * Seq_ops show method. Called several times within a read()
658 * call from userspace, with ddebug_lock held. Formats the
659 * current _ddebug as a single human-readable line, with a
660 * special case for the header line.
662 static int ddebug_proc_show(struct seq_file
*m
, void *p
)
664 struct ddebug_iter
*iter
= m
->private;
665 struct _ddebug
*dp
= p
;
669 pr_info("called m=%p p=%p\n", m
, p
);
671 if (p
== SEQ_START_TOKEN
) {
673 "# filename:lineno [module]function flags format\n");
677 seq_printf(m
, "%s:%u [%s]%s %s \"",
678 dp
->filename
, dp
->lineno
,
679 iter
->table
->mod_name
, dp
->function
,
680 ddebug_describe_flags(dp
, flagsbuf
, sizeof(flagsbuf
)));
681 seq_escape(m
, dp
->format
, "\t\r\n\"");
688 * Seq_ops stop method. Called at the end of each read()
689 * call from userspace. Drops ddebug_lock.
691 static void ddebug_proc_stop(struct seq_file
*m
, void *p
)
694 pr_info("called m=%p p=%p\n", m
, p
);
695 mutex_unlock(&ddebug_lock
);
698 static const struct seq_operations ddebug_proc_seqops
= {
699 .start
= ddebug_proc_start
,
700 .next
= ddebug_proc_next
,
701 .show
= ddebug_proc_show
,
702 .stop
= ddebug_proc_stop
706 * File_ops->open method for <debugfs>/dynamic_debug/control. Does the seq_file
707 * setup dance, and also creates an iterator to walk the _ddebugs.
708 * Note that we create a seq_file always, even for O_WRONLY files
709 * where it's not needed, as doing so simplifies the ->release method.
711 static int ddebug_proc_open(struct inode
*inode
, struct file
*file
)
713 struct ddebug_iter
*iter
;
719 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
723 err
= seq_open(file
, &ddebug_proc_seqops
);
728 ((struct seq_file
*) file
->private_data
)->private = iter
;
732 static const struct file_operations ddebug_proc_fops
= {
733 .owner
= THIS_MODULE
,
734 .open
= ddebug_proc_open
,
737 .release
= seq_release_private
,
738 .write
= ddebug_proc_write
742 * Allocate a new ddebug_table for the given module
743 * and add it to the global list.
745 int ddebug_add_module(struct _ddebug
*tab
, unsigned int n
,
748 struct ddebug_table
*dt
;
751 dt
= kzalloc(sizeof(*dt
), GFP_KERNEL
);
754 new_name
= kstrdup(name
, GFP_KERNEL
);
755 if (new_name
== NULL
) {
759 dt
->mod_name
= new_name
;
763 mutex_lock(&ddebug_lock
);
764 list_add_tail(&dt
->link
, &ddebug_tables
);
765 mutex_unlock(&ddebug_lock
);
768 pr_info("%u debug prints in module %s\n", n
, dt
->mod_name
);
771 EXPORT_SYMBOL_GPL(ddebug_add_module
);
773 static void ddebug_table_free(struct ddebug_table
*dt
)
775 list_del_init(&dt
->link
);
781 * Called in response to a module being unloaded. Removes
782 * any ddebug_table's which point at the module.
784 int ddebug_remove_module(const char *mod_name
)
786 struct ddebug_table
*dt
, *nextdt
;
790 pr_info("removing module \"%s\"\n", mod_name
);
792 mutex_lock(&ddebug_lock
);
793 list_for_each_entry_safe(dt
, nextdt
, &ddebug_tables
, link
) {
794 if (!strcmp(dt
->mod_name
, mod_name
)) {
795 ddebug_table_free(dt
);
799 mutex_unlock(&ddebug_lock
);
802 EXPORT_SYMBOL_GPL(ddebug_remove_module
);
804 static void ddebug_remove_all_tables(void)
806 mutex_lock(&ddebug_lock
);
807 while (!list_empty(&ddebug_tables
)) {
808 struct ddebug_table
*dt
= list_entry(ddebug_tables
.next
,
811 ddebug_table_free(dt
);
813 mutex_unlock(&ddebug_lock
);
816 static __initdata
int ddebug_init_success
;
818 static int __init
dynamic_debug_init_debugfs(void)
820 struct dentry
*dir
, *file
;
822 if (!ddebug_init_success
)
825 dir
= debugfs_create_dir("dynamic_debug", NULL
);
828 file
= debugfs_create_file("control", 0644, dir
, NULL
,
837 static int __init
dynamic_debug_init(void)
839 struct _ddebug
*iter
, *iter_start
;
840 const char *modname
= NULL
;
844 if (__start___verbose
!= __stop___verbose
) {
845 iter
= __start___verbose
;
846 modname
= iter
->modname
;
848 for (; iter
< __stop___verbose
; iter
++) {
849 if (strcmp(modname
, iter
->modname
)) {
850 ret
= ddebug_add_module(iter_start
, n
, modname
);
854 modname
= iter
->modname
;
859 ret
= ddebug_add_module(iter_start
, n
, modname
);
862 /* ddebug_query boot param got passed -> set it up */
863 if (ddebug_setup_string
[0] != '\0') {
864 ret
= ddebug_exec_query(ddebug_setup_string
);
866 pr_warn("Invalid ddebug boot param %s",
867 ddebug_setup_string
);
869 pr_info("ddebug initialized with string %s",
870 ddebug_setup_string
);
875 ddebug_remove_all_tables();
877 ddebug_init_success
= 1;
880 /* Allow early initialization for boot messages via boot param */
881 arch_initcall(dynamic_debug_init
);
882 /* Debugfs setup must be done later */
883 module_init(dynamic_debug_init_debugfs
);