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>
33 extern struct _ddebug __start___verbose[];
34 extern struct _ddebug __stop___verbose[];
37 struct list_head link;
39 unsigned int num_ddebugs;
40 unsigned int num_enabled;
41 struct _ddebug *ddebugs;
49 unsigned int first_lineno, last_lineno;
53 struct ddebug_table *table;
57 static DEFINE_MUTEX(ddebug_lock);
58 static LIST_HEAD(ddebug_tables);
59 static int verbose = 0;
61 /* Return the last part of a pathname */
62 static inline const char *basename(const char *path)
64 const char *tail = strrchr(path, '/');
65 return tail ? tail+1 : path;
68 static struct { unsigned flag:8; char opt_char; } opt_array[] = {
69 { _DPRINTK_FLAGS_PRINT, 'p' },
70 { _DPRINTK_FLAGS_INCL_MODNAME, 'm' },
71 { _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' },
72 { _DPRINTK_FLAGS_INCL_LINENO, 'l' },
73 { _DPRINTK_FLAGS_INCL_TID, 't' },
76 /* format a string into buf[] which describes the _ddebug's flags */
77 static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,
84 for (i = 0; i < ARRAY_SIZE(opt_array); ++i)
85 if (dp->flags & opt_array[i].flag)
86 *p++ = opt_array[i].opt_char;
95 * Search the tables for _ddebug's which match the given
96 * `query' and apply the `flags' and `mask' to them. Tells
97 * the user which ddebug's were changed, or whether none
100 static void ddebug_change(const struct ddebug_query *query,
101 unsigned int flags, unsigned int mask)
104 struct ddebug_table *dt;
105 unsigned int newflags;
106 unsigned int nfound = 0;
109 /* search for matching ddebugs */
110 mutex_lock(&ddebug_lock);
111 list_for_each_entry(dt, &ddebug_tables, link) {
113 /* match against the module name */
114 if (query->module != NULL &&
115 strcmp(query->module, dt->mod_name))
118 for (i = 0 ; i < dt->num_ddebugs ; i++) {
119 struct _ddebug *dp = &dt->ddebugs[i];
121 /* match against the source filename */
122 if (query->filename != NULL &&
123 strcmp(query->filename, dp->filename) &&
124 strcmp(query->filename, basename(dp->filename)))
127 /* match against the function */
128 if (query->function != NULL &&
129 strcmp(query->function, dp->function))
132 /* match against the format */
133 if (query->format != NULL &&
134 strstr(dp->format, query->format) == NULL)
137 /* match against the line number range */
138 if (query->first_lineno &&
139 dp->lineno < query->first_lineno)
141 if (query->last_lineno &&
142 dp->lineno > query->last_lineno)
147 newflags = (dp->flags & mask) | flags;
148 if (newflags == dp->flags)
155 dp->flags = newflags;
162 "ddebug: changed %s:%d [%s]%s %s\n",
163 dp->filename, dp->lineno,
164 dt->mod_name, dp->function,
165 ddebug_describe_flags(dp, flagbuf,
169 mutex_unlock(&ddebug_lock);
171 if (!nfound && verbose)
172 printk(KERN_INFO "ddebug: no matches for query\n");
176 * Split the buffer `buf' into space-separated words.
177 * Handles simple " and ' quoting, i.e. without nested,
178 * embedded or escaped \". Return the number of words
181 static int ddebug_tokenize(char *buf, char *words[], int maxwords)
188 /* Skip leading whitespace */
189 buf = skip_spaces(buf);
191 break; /* oh, it was trailing whitespace */
193 /* Run `end' over a word, either whitespace separated or quoted */
194 if (*buf == '"' || *buf == '\'') {
196 for (end = buf ; *end && *end != quote ; end++)
199 return -EINVAL; /* unclosed quote */
201 for (end = buf ; *end && !isspace(*end) ; end++)
205 /* Here `buf' is the start of the word, `end' is one past the end */
207 if (nwords == maxwords)
208 return -EINVAL; /* ran out of words[] before bytes */
210 *end++ = '\0'; /* terminate the word */
211 words[nwords++] = buf;
217 printk(KERN_INFO "%s: split into words:", __func__);
218 for (i = 0 ; i < nwords ; i++)
219 printk(" \"%s\"", words[i]);
227 * Parse a single line number. Note that the empty string ""
228 * is treated as a special case and converted to zero, which
229 * is later treated as a "don't care" value.
231 static inline int parse_lineno(const char *str, unsigned int *val)
239 *val = simple_strtoul(str, &end, 10);
240 return end == NULL || end == str || *end != '\0' ? -EINVAL : 0;
244 * Undo octal escaping in a string, inplace. This is useful to
245 * allow the user to express a query which matches a format
246 * containing embedded spaces.
248 #define isodigit(c) ((c) >= '0' && (c) <= '7')
249 static char *unescape(char *str)
260 } else if (in[1] == 't') {
264 } else if (in[1] == 'n') {
268 } else if (isodigit(in[1]) &&
271 *out++ = ((in[1] - '0')<<6) |
286 * Parse words[] as a ddebug query specification, which is a series
287 * of (keyword, value) pairs chosen from these possibilities:
289 * func <function-name>
290 * file <full-pathname>
291 * file <base-filename>
292 * module <module-name>
293 * format <escaped-string-to-find-in-format>
295 * line <first-lineno>-<last-lineno> // where either may be empty
297 static int ddebug_parse_query(char *words[], int nwords,
298 struct ddebug_query *query)
302 /* check we have an even number of words */
305 memset(query, 0, sizeof(*query));
307 for (i = 0 ; i < nwords ; i += 2) {
308 if (!strcmp(words[i], "func"))
309 query->function = words[i+1];
310 else if (!strcmp(words[i], "file"))
311 query->filename = words[i+1];
312 else if (!strcmp(words[i], "module"))
313 query->module = words[i+1];
314 else if (!strcmp(words[i], "format"))
315 query->format = unescape(words[i+1]);
316 else if (!strcmp(words[i], "line")) {
317 char *first = words[i+1];
318 char *last = strchr(first, '-');
321 if (parse_lineno(first, &query->first_lineno) < 0)
324 /* range <first>-<last> */
325 if (parse_lineno(last, &query->last_lineno) < 0)
328 query->last_lineno = query->first_lineno;
332 printk(KERN_ERR "%s: unknown keyword \"%s\"\n",
339 printk(KERN_INFO "%s: q->function=\"%s\" q->filename=\"%s\" "
340 "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n",
341 __func__, query->function, query->filename,
342 query->module, query->format, query->first_lineno,
349 * Parse `str' as a flags specification, format [-+=][p]+.
350 * Sets up *maskp and *flagsp to be used when changing the
351 * flags fields of matched _ddebug's. Returns 0 on success
354 static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
370 printk(KERN_INFO "%s: op='%c'\n", __func__, op);
372 for ( ; *str ; ++str) {
373 for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) {
374 if (*str == opt_array[i].opt_char) {
375 flags |= opt_array[i].flag;
385 printk(KERN_INFO "%s: flags=0x%x\n", __func__, flags);
387 /* calculate final *flagsp, *maskp according to mask and op */
403 printk(KERN_INFO "%s: *flagsp=0x%x *maskp=0x%x\n",
404 __func__, *flagsp, *maskp);
408 static int ddebug_exec_query(char *query_string)
410 unsigned int flags = 0, mask = 0;
411 struct ddebug_query query;
414 char *words[MAXWORDS];
416 nwords = ddebug_tokenize(query_string, words, MAXWORDS);
419 if (ddebug_parse_query(words, nwords-1, &query))
421 if (ddebug_parse_flags(words[nwords-1], &flags, &mask))
424 /* actually go and implement the change */
425 ddebug_change(&query, flags, mask);
429 int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
438 res = printk(KERN_DEBUG);
439 if (descriptor->flags & _DPRINTK_FLAGS_INCL_TID) {
441 res += printk(KERN_CONT "<intr> ");
443 res += printk(KERN_CONT "[%d] ", task_pid_vnr(current));
445 if (descriptor->flags & _DPRINTK_FLAGS_INCL_MODNAME)
446 res += printk(KERN_CONT "%s:", descriptor->modname);
447 if (descriptor->flags & _DPRINTK_FLAGS_INCL_FUNCNAME)
448 res += printk(KERN_CONT "%s:", descriptor->function);
449 if (descriptor->flags & _DPRINTK_FLAGS_INCL_LINENO)
450 res += printk(KERN_CONT "%d ", descriptor->lineno);
451 res += vprintk(fmt, args);
456 EXPORT_SYMBOL(__dynamic_pr_debug);
458 static __initdata char ddebug_setup_string[1024];
459 static __init int ddebug_setup_query(char *str)
461 if (strlen(str) >= 1024) {
462 pr_warning("ddebug boot param string too large\n");
465 strcpy(ddebug_setup_string, str);
469 __setup("ddebug_query=", ddebug_setup_query);
472 * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the
473 * command text from userspace, parses and executes it.
475 static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
476 size_t len, loff_t *offp)
483 /* we don't check *offp -- multiple writes() are allowed */
484 if (len > sizeof(tmpbuf)-1)
486 if (copy_from_user(tmpbuf, ubuf, len))
490 printk(KERN_INFO "%s: read %d bytes from userspace\n",
493 ret = ddebug_exec_query(tmpbuf);
502 * Set the iterator to point to the first _ddebug object
503 * and return a pointer to that first object. Returns
504 * NULL if there are no _ddebugs at all.
506 static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
508 if (list_empty(&ddebug_tables)) {
513 iter->table = list_entry(ddebug_tables.next,
514 struct ddebug_table, link);
516 return &iter->table->ddebugs[iter->idx];
520 * Advance the iterator to point to the next _ddebug
521 * object from the one the iterator currently points at,
522 * and returns a pointer to the new _ddebug. Returns
523 * NULL if the iterator has seen all the _ddebugs.
525 static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
527 if (iter->table == NULL)
529 if (++iter->idx == iter->table->num_ddebugs) {
530 /* iterate to next table */
532 if (list_is_last(&iter->table->link, &ddebug_tables)) {
536 iter->table = list_entry(iter->table->link.next,
537 struct ddebug_table, link);
539 return &iter->table->ddebugs[iter->idx];
543 * Seq_ops start method. Called at the start of every
544 * read() call from userspace. Takes the ddebug_lock and
545 * seeks the seq_file's iterator to the given position.
547 static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
549 struct ddebug_iter *iter = m->private;
554 printk(KERN_INFO "%s: called m=%p *pos=%lld\n",
555 __func__, m, (unsigned long long)*pos);
557 mutex_lock(&ddebug_lock);
560 return SEQ_START_TOKEN;
563 dp = ddebug_iter_first(iter);
564 while (dp != NULL && --n > 0)
565 dp = ddebug_iter_next(iter);
570 * Seq_ops next method. Called several times within a read()
571 * call from userspace, with ddebug_lock held. Walks to the
572 * next _ddebug object with a special case for the header line.
574 static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
576 struct ddebug_iter *iter = m->private;
580 printk(KERN_INFO "%s: called m=%p p=%p *pos=%lld\n",
581 __func__, m, p, (unsigned long long)*pos);
583 if (p == SEQ_START_TOKEN)
584 dp = ddebug_iter_first(iter);
586 dp = ddebug_iter_next(iter);
592 * Seq_ops show method. Called several times within a read()
593 * call from userspace, with ddebug_lock held. Formats the
594 * current _ddebug as a single human-readable line, with a
595 * special case for the header line.
597 static int ddebug_proc_show(struct seq_file *m, void *p)
599 struct ddebug_iter *iter = m->private;
600 struct _ddebug *dp = p;
604 printk(KERN_INFO "%s: called m=%p p=%p\n",
607 if (p == SEQ_START_TOKEN) {
609 "# filename:lineno [module]function flags format\n");
613 seq_printf(m, "%s:%u [%s]%s %s \"",
614 dp->filename, dp->lineno,
615 iter->table->mod_name, dp->function,
616 ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
617 seq_escape(m, dp->format, "\t\r\n\"");
624 * Seq_ops stop method. Called at the end of each read()
625 * call from userspace. Drops ddebug_lock.
627 static void ddebug_proc_stop(struct seq_file *m, void *p)
630 printk(KERN_INFO "%s: called m=%p p=%p\n",
632 mutex_unlock(&ddebug_lock);
635 static const struct seq_operations ddebug_proc_seqops = {
636 .start = ddebug_proc_start,
637 .next = ddebug_proc_next,
638 .show = ddebug_proc_show,
639 .stop = ddebug_proc_stop
643 * File_ops->open method for <debugfs>/dynamic_debug/control. Does the seq_file
644 * setup dance, and also creates an iterator to walk the _ddebugs.
645 * Note that we create a seq_file always, even for O_WRONLY files
646 * where it's not needed, as doing so simplifies the ->release method.
648 static int ddebug_proc_open(struct inode *inode, struct file *file)
650 struct ddebug_iter *iter;
654 printk(KERN_INFO "%s: called\n", __func__);
656 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
660 err = seq_open(file, &ddebug_proc_seqops);
665 ((struct seq_file *) file->private_data)->private = iter;
669 static const struct file_operations ddebug_proc_fops = {
670 .owner = THIS_MODULE,
671 .open = ddebug_proc_open,
674 .release = seq_release_private,
675 .write = ddebug_proc_write
679 * Allocate a new ddebug_table for the given module
680 * and add it to the global list.
682 int ddebug_add_module(struct _ddebug *tab, unsigned int n,
685 struct ddebug_table *dt;
688 dt = kzalloc(sizeof(*dt), GFP_KERNEL);
691 new_name = kstrdup(name, GFP_KERNEL);
692 if (new_name == NULL) {
696 dt->mod_name = new_name;
701 mutex_lock(&ddebug_lock);
702 list_add_tail(&dt->link, &ddebug_tables);
703 mutex_unlock(&ddebug_lock);
706 printk(KERN_INFO "%u debug prints in module %s\n",
710 EXPORT_SYMBOL_GPL(ddebug_add_module);
712 static void ddebug_table_free(struct ddebug_table *dt)
714 list_del_init(&dt->link);
720 * Called in response to a module being unloaded. Removes
721 * any ddebug_table's which point at the module.
723 int ddebug_remove_module(const char *mod_name)
725 struct ddebug_table *dt, *nextdt;
729 printk(KERN_INFO "%s: removing module \"%s\"\n",
732 mutex_lock(&ddebug_lock);
733 list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
734 if (!strcmp(dt->mod_name, mod_name)) {
735 ddebug_table_free(dt);
739 mutex_unlock(&ddebug_lock);
742 EXPORT_SYMBOL_GPL(ddebug_remove_module);
744 static void ddebug_remove_all_tables(void)
746 mutex_lock(&ddebug_lock);
747 while (!list_empty(&ddebug_tables)) {
748 struct ddebug_table *dt = list_entry(ddebug_tables.next,
751 ddebug_table_free(dt);
753 mutex_unlock(&ddebug_lock);
756 static __initdata int ddebug_init_success;
758 static int __init dynamic_debug_init_debugfs(void)
760 struct dentry *dir, *file;
762 if (!ddebug_init_success)
765 dir = debugfs_create_dir("dynamic_debug", NULL);
768 file = debugfs_create_file("control", 0644, dir, NULL,
777 static int __init dynamic_debug_init(void)
779 struct _ddebug *iter, *iter_start;
780 const char *modname = NULL;
784 if (__start___verbose != __stop___verbose) {
785 iter = __start___verbose;
786 modname = iter->modname;
788 for (; iter < __stop___verbose; iter++) {
789 if (strcmp(modname, iter->modname)) {
790 ret = ddebug_add_module(iter_start, n, modname);
794 modname = iter->modname;
799 ret = ddebug_add_module(iter_start, n, modname);
802 /* ddebug_query boot param got passed -> set it up */
803 if (ddebug_setup_string[0] != '\0') {
804 ret = ddebug_exec_query(ddebug_setup_string);
806 pr_warning("Invalid ddebug boot param %s",
807 ddebug_setup_string);
809 pr_info("ddebug initialized with string %s",
810 ddebug_setup_string);
815 ddebug_remove_all_tables();
817 ddebug_init_success = 1;
820 /* Allow early initialization for boot messages via boot param */
821 arch_initcall(dynamic_debug_init);
822 /* Debugfs setup must be done later */
823 module_init(dynamic_debug_init_debugfs);