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 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/kallsyms.h>
17 #include <linux/version.h>
18 #include <linux/types.h>
19 #include <linux/mutex.h>
20 #include <linux/proc_fs.h>
21 #include <linux/seq_file.h>
22 #include <linux/list.h>
23 #include <linux/sysctl.h>
24 #include <linux/ctype.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
27 #include <linux/dynamic_debug.h>
28 #include <linux/debugfs.h>
29 #include <linux/slab.h>
30 #include <linux/jump_label.h>
31 #include <linux/hardirq.h>
32 #include <linux/sched.h>
33 #include <linux/device.h>
35 extern struct _ddebug __start___verbose[];
36 extern struct _ddebug __stop___verbose[];
39 struct list_head link;
41 unsigned int num_ddebugs;
42 unsigned int num_enabled;
43 struct _ddebug *ddebugs;
51 unsigned int first_lineno, last_lineno;
55 struct ddebug_table *table;
59 static DEFINE_MUTEX(ddebug_lock);
60 static LIST_HEAD(ddebug_tables);
61 static int verbose = 0;
63 /* Return the last part of a pathname */
64 static inline const char *basename(const char *path)
66 const char *tail = strrchr(path, '/');
67 return tail ? tail+1 : path;
70 static struct { unsigned flag:8; char opt_char; } opt_array[] = {
71 { _DPRINTK_FLAGS_PRINT, 'p' },
72 { _DPRINTK_FLAGS_INCL_MODNAME, 'm' },
73 { _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' },
74 { _DPRINTK_FLAGS_INCL_LINENO, 'l' },
75 { _DPRINTK_FLAGS_INCL_TID, 't' },
78 /* format a string into buf[] which describes the _ddebug's flags */
79 static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,
86 for (i = 0; i < ARRAY_SIZE(opt_array); ++i)
87 if (dp->flags & opt_array[i].flag)
88 *p++ = opt_array[i].opt_char;
97 * Search the tables for _ddebug's which match the given
98 * `query' and apply the `flags' and `mask' to them. Tells
99 * the user which ddebug's were changed, or whether none
102 static void ddebug_change(const struct ddebug_query *query,
103 unsigned int flags, unsigned int mask)
106 struct ddebug_table *dt;
107 unsigned int newflags;
108 unsigned int nfound = 0;
111 /* search for matching ddebugs */
112 mutex_lock(&ddebug_lock);
113 list_for_each_entry(dt, &ddebug_tables, link) {
115 /* match against the module name */
116 if (query->module != NULL &&
117 strcmp(query->module, dt->mod_name))
120 for (i = 0 ; i < dt->num_ddebugs ; i++) {
121 struct _ddebug *dp = &dt->ddebugs[i];
123 /* match against the source filename */
124 if (query->filename != NULL &&
125 strcmp(query->filename, dp->filename) &&
126 strcmp(query->filename, basename(dp->filename)))
129 /* match against the function */
130 if (query->function != NULL &&
131 strcmp(query->function, dp->function))
134 /* match against the format */
135 if (query->format != NULL &&
136 strstr(dp->format, query->format) == NULL)
139 /* match against the line number range */
140 if (query->first_lineno &&
141 dp->lineno < query->first_lineno)
143 if (query->last_lineno &&
144 dp->lineno > query->last_lineno)
149 newflags = (dp->flags & mask) | flags;
150 if (newflags == dp->flags)
157 dp->flags = newflags;
164 "ddebug: changed %s:%d [%s]%s %s\n",
165 dp->filename, dp->lineno,
166 dt->mod_name, dp->function,
167 ddebug_describe_flags(dp, flagbuf,
171 mutex_unlock(&ddebug_lock);
173 if (!nfound && verbose)
174 printk(KERN_INFO "ddebug: no matches for query\n");
178 * Split the buffer `buf' into space-separated words.
179 * Handles simple " and ' quoting, i.e. without nested,
180 * embedded or escaped \". Return the number of words
183 static int ddebug_tokenize(char *buf, char *words[], int maxwords)
190 /* Skip leading whitespace */
191 buf = skip_spaces(buf);
193 break; /* oh, it was trailing whitespace */
195 /* Run `end' over a word, either whitespace separated or quoted */
196 if (*buf == '"' || *buf == '\'') {
198 for (end = buf ; *end && *end != quote ; end++)
201 return -EINVAL; /* unclosed quote */
203 for (end = buf ; *end && !isspace(*end) ; end++)
207 /* Here `buf' is the start of the word, `end' is one past the end */
209 if (nwords == maxwords)
210 return -EINVAL; /* ran out of words[] before bytes */
212 *end++ = '\0'; /* terminate the word */
213 words[nwords++] = buf;
219 printk(KERN_INFO "%s: split into words:", __func__);
220 for (i = 0 ; i < nwords ; i++)
221 printk(" \"%s\"", words[i]);
229 * Parse a single line number. Note that the empty string ""
230 * is treated as a special case and converted to zero, which
231 * is later treated as a "don't care" value.
233 static inline int parse_lineno(const char *str, unsigned int *val)
241 *val = simple_strtoul(str, &end, 10);
242 return end == NULL || end == str || *end != '\0' ? -EINVAL : 0;
246 * Undo octal escaping in a string, inplace. This is useful to
247 * allow the user to express a query which matches a format
248 * containing embedded spaces.
250 #define isodigit(c) ((c) >= '0' && (c) <= '7')
251 static char *unescape(char *str)
262 } else if (in[1] == 't') {
266 } else if (in[1] == 'n') {
270 } else if (isodigit(in[1]) &&
273 *out++ = ((in[1] - '0')<<6) |
288 * Parse words[] as a ddebug query specification, which is a series
289 * of (keyword, value) pairs chosen from these possibilities:
291 * func <function-name>
292 * file <full-pathname>
293 * file <base-filename>
294 * module <module-name>
295 * format <escaped-string-to-find-in-format>
297 * line <first-lineno>-<last-lineno> // where either may be empty
299 static int ddebug_parse_query(char *words[], int nwords,
300 struct ddebug_query *query)
304 /* check we have an even number of words */
307 memset(query, 0, sizeof(*query));
309 for (i = 0 ; i < nwords ; i += 2) {
310 if (!strcmp(words[i], "func"))
311 query->function = words[i+1];
312 else if (!strcmp(words[i], "file"))
313 query->filename = words[i+1];
314 else if (!strcmp(words[i], "module"))
315 query->module = words[i+1];
316 else if (!strcmp(words[i], "format"))
317 query->format = unescape(words[i+1]);
318 else if (!strcmp(words[i], "line")) {
319 char *first = words[i+1];
320 char *last = strchr(first, '-');
323 if (parse_lineno(first, &query->first_lineno) < 0)
326 /* range <first>-<last> */
327 if (parse_lineno(last, &query->last_lineno) < 0)
330 query->last_lineno = query->first_lineno;
334 printk(KERN_ERR "%s: unknown keyword \"%s\"\n",
341 printk(KERN_INFO "%s: q->function=\"%s\" q->filename=\"%s\" "
342 "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n",
343 __func__, query->function, query->filename,
344 query->module, query->format, query->first_lineno,
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
356 static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
372 printk(KERN_INFO "%s: op='%c'\n", __func__, op);
374 for ( ; *str ; ++str) {
375 for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) {
376 if (*str == opt_array[i].opt_char) {
377 flags |= opt_array[i].flag;
387 printk(KERN_INFO "%s: flags=0x%x\n", __func__, flags);
389 /* calculate final *flagsp, *maskp according to mask and op */
405 printk(KERN_INFO "%s: *flagsp=0x%x *maskp=0x%x\n",
406 __func__, *flagsp, *maskp);
410 static int ddebug_exec_query(char *query_string)
412 unsigned int flags = 0, mask = 0;
413 struct ddebug_query query;
416 char *words[MAXWORDS];
418 nwords = ddebug_tokenize(query_string, words, MAXWORDS);
421 if (ddebug_parse_query(words, nwords-1, &query))
423 if (ddebug_parse_flags(words[nwords-1], &flags, &mask))
426 /* actually go and implement the change */
427 ddebug_change(&query, flags, mask);
431 static int dynamic_emit_prefix(const struct _ddebug *descriptor)
435 res = printk(KERN_DEBUG);
436 if (descriptor->flags & _DPRINTK_FLAGS_INCL_TID) {
438 res += printk(KERN_CONT "<intr> ");
440 res += printk(KERN_CONT "[%d] ", task_pid_vnr(current));
442 if (descriptor->flags & _DPRINTK_FLAGS_INCL_MODNAME)
443 res += printk(KERN_CONT "%s:", descriptor->modname);
444 if (descriptor->flags & _DPRINTK_FLAGS_INCL_FUNCNAME)
445 res += printk(KERN_CONT "%s:", descriptor->function);
446 if (descriptor->flags & _DPRINTK_FLAGS_INCL_LINENO)
447 res += printk(KERN_CONT "%d ", descriptor->lineno);
452 int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
462 res = dynamic_emit_prefix(descriptor);
463 res += vprintk(fmt, args);
469 EXPORT_SYMBOL(__dynamic_pr_debug);
471 int __dynamic_dev_dbg(struct _ddebug *descriptor,
472 const struct device *dev, const char *fmt, ...)
474 struct va_format vaf;
486 res = dynamic_emit_prefix(descriptor);
487 res += __dev_printk(KERN_CONT, dev, &vaf);
493 EXPORT_SYMBOL(__dynamic_dev_dbg);
495 static __initdata char ddebug_setup_string[1024];
496 static __init int ddebug_setup_query(char *str)
498 if (strlen(str) >= 1024) {
499 pr_warning("ddebug boot param string too large\n");
502 strcpy(ddebug_setup_string, str);
506 __setup("ddebug_query=", ddebug_setup_query);
509 * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the
510 * command text from userspace, parses and executes it.
512 static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
513 size_t len, loff_t *offp)
520 /* we don't check *offp -- multiple writes() are allowed */
521 if (len > sizeof(tmpbuf)-1)
523 if (copy_from_user(tmpbuf, ubuf, len))
527 printk(KERN_INFO "%s: read %d bytes from userspace\n",
530 ret = ddebug_exec_query(tmpbuf);
539 * Set the iterator to point to the first _ddebug object
540 * and return a pointer to that first object. Returns
541 * NULL if there are no _ddebugs at all.
543 static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
545 if (list_empty(&ddebug_tables)) {
550 iter->table = list_entry(ddebug_tables.next,
551 struct ddebug_table, link);
553 return &iter->table->ddebugs[iter->idx];
557 * Advance the iterator to point to the next _ddebug
558 * object from the one the iterator currently points at,
559 * and returns a pointer to the new _ddebug. Returns
560 * NULL if the iterator has seen all the _ddebugs.
562 static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
564 if (iter->table == NULL)
566 if (++iter->idx == iter->table->num_ddebugs) {
567 /* iterate to next table */
569 if (list_is_last(&iter->table->link, &ddebug_tables)) {
573 iter->table = list_entry(iter->table->link.next,
574 struct ddebug_table, link);
576 return &iter->table->ddebugs[iter->idx];
580 * Seq_ops start method. Called at the start of every
581 * read() call from userspace. Takes the ddebug_lock and
582 * seeks the seq_file's iterator to the given position.
584 static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
586 struct ddebug_iter *iter = m->private;
591 printk(KERN_INFO "%s: called m=%p *pos=%lld\n",
592 __func__, m, (unsigned long long)*pos);
594 mutex_lock(&ddebug_lock);
597 return SEQ_START_TOKEN;
600 dp = ddebug_iter_first(iter);
601 while (dp != NULL && --n > 0)
602 dp = ddebug_iter_next(iter);
607 * Seq_ops next method. Called several times within a read()
608 * call from userspace, with ddebug_lock held. Walks to the
609 * next _ddebug object with a special case for the header line.
611 static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
613 struct ddebug_iter *iter = m->private;
617 printk(KERN_INFO "%s: called m=%p p=%p *pos=%lld\n",
618 __func__, m, p, (unsigned long long)*pos);
620 if (p == SEQ_START_TOKEN)
621 dp = ddebug_iter_first(iter);
623 dp = ddebug_iter_next(iter);
629 * Seq_ops show method. Called several times within a read()
630 * call from userspace, with ddebug_lock held. Formats the
631 * current _ddebug as a single human-readable line, with a
632 * special case for the header line.
634 static int ddebug_proc_show(struct seq_file *m, void *p)
636 struct ddebug_iter *iter = m->private;
637 struct _ddebug *dp = p;
641 printk(KERN_INFO "%s: called m=%p p=%p\n",
644 if (p == SEQ_START_TOKEN) {
646 "# filename:lineno [module]function flags format\n");
650 seq_printf(m, "%s:%u [%s]%s %s \"",
651 dp->filename, dp->lineno,
652 iter->table->mod_name, dp->function,
653 ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
654 seq_escape(m, dp->format, "\t\r\n\"");
661 * Seq_ops stop method. Called at the end of each read()
662 * call from userspace. Drops ddebug_lock.
664 static void ddebug_proc_stop(struct seq_file *m, void *p)
667 printk(KERN_INFO "%s: called m=%p p=%p\n",
669 mutex_unlock(&ddebug_lock);
672 static const struct seq_operations ddebug_proc_seqops = {
673 .start = ddebug_proc_start,
674 .next = ddebug_proc_next,
675 .show = ddebug_proc_show,
676 .stop = ddebug_proc_stop
680 * File_ops->open method for <debugfs>/dynamic_debug/control. Does the seq_file
681 * setup dance, and also creates an iterator to walk the _ddebugs.
682 * Note that we create a seq_file always, even for O_WRONLY files
683 * where it's not needed, as doing so simplifies the ->release method.
685 static int ddebug_proc_open(struct inode *inode, struct file *file)
687 struct ddebug_iter *iter;
691 printk(KERN_INFO "%s: called\n", __func__);
693 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
697 err = seq_open(file, &ddebug_proc_seqops);
702 ((struct seq_file *) file->private_data)->private = iter;
706 static const struct file_operations ddebug_proc_fops = {
707 .owner = THIS_MODULE,
708 .open = ddebug_proc_open,
711 .release = seq_release_private,
712 .write = ddebug_proc_write
716 * Allocate a new ddebug_table for the given module
717 * and add it to the global list.
719 int ddebug_add_module(struct _ddebug *tab, unsigned int n,
722 struct ddebug_table *dt;
725 dt = kzalloc(sizeof(*dt), GFP_KERNEL);
728 new_name = kstrdup(name, GFP_KERNEL);
729 if (new_name == NULL) {
733 dt->mod_name = new_name;
738 mutex_lock(&ddebug_lock);
739 list_add_tail(&dt->link, &ddebug_tables);
740 mutex_unlock(&ddebug_lock);
743 printk(KERN_INFO "%u debug prints in module %s\n",
747 EXPORT_SYMBOL_GPL(ddebug_add_module);
749 static void ddebug_table_free(struct ddebug_table *dt)
751 list_del_init(&dt->link);
757 * Called in response to a module being unloaded. Removes
758 * any ddebug_table's which point at the module.
760 int ddebug_remove_module(const char *mod_name)
762 struct ddebug_table *dt, *nextdt;
766 printk(KERN_INFO "%s: removing module \"%s\"\n",
769 mutex_lock(&ddebug_lock);
770 list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
771 if (!strcmp(dt->mod_name, mod_name)) {
772 ddebug_table_free(dt);
776 mutex_unlock(&ddebug_lock);
779 EXPORT_SYMBOL_GPL(ddebug_remove_module);
781 static void ddebug_remove_all_tables(void)
783 mutex_lock(&ddebug_lock);
784 while (!list_empty(&ddebug_tables)) {
785 struct ddebug_table *dt = list_entry(ddebug_tables.next,
788 ddebug_table_free(dt);
790 mutex_unlock(&ddebug_lock);
793 static __initdata int ddebug_init_success;
795 static int __init dynamic_debug_init_debugfs(void)
797 struct dentry *dir, *file;
799 if (!ddebug_init_success)
802 dir = debugfs_create_dir("dynamic_debug", NULL);
805 file = debugfs_create_file("control", 0644, dir, NULL,
814 static int __init dynamic_debug_init(void)
816 struct _ddebug *iter, *iter_start;
817 const char *modname = NULL;
821 if (__start___verbose != __stop___verbose) {
822 iter = __start___verbose;
823 modname = iter->modname;
825 for (; iter < __stop___verbose; iter++) {
826 if (strcmp(modname, iter->modname)) {
827 ret = ddebug_add_module(iter_start, n, modname);
831 modname = iter->modname;
836 ret = ddebug_add_module(iter_start, n, modname);
839 /* ddebug_query boot param got passed -> set it up */
840 if (ddebug_setup_string[0] != '\0') {
841 ret = ddebug_exec_query(ddebug_setup_string);
843 pr_warning("Invalid ddebug boot param %s",
844 ddebug_setup_string);
846 pr_info("ddebug initialized with string %s",
847 ddebug_setup_string);
852 ddebug_remove_all_tables();
854 ddebug_init_success = 1;
857 /* Allow early initialization for boot messages via boot param */
858 arch_initcall(dynamic_debug_init);
859 /* Debugfs setup must be done later */
860 module_init(dynamic_debug_init_debugfs);