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>
29 #include <linux/jump_label.h>
31 extern struct _ddebug __start___verbose[];
32 extern struct _ddebug __stop___verbose[];
35 struct list_head link;
37 unsigned int num_ddebugs;
38 unsigned int num_enabled;
39 struct _ddebug *ddebugs;
47 unsigned int first_lineno, last_lineno;
51 struct ddebug_table *table;
55 static DEFINE_MUTEX(ddebug_lock);
56 static LIST_HEAD(ddebug_tables);
57 static int verbose = 0;
59 /* Return the last part of a pathname */
60 static inline const char *basename(const char *path)
62 const char *tail = strrchr(path, '/');
63 return tail ? tail+1 : path;
66 /* format a string into buf[] which describes the _ddebug's flags */
67 static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,
73 if (dp->flags & _DPRINTK_FLAGS_PRINT)
83 * Search the tables for _ddebug's which match the given
84 * `query' and apply the `flags' and `mask' to them. Tells
85 * the user which ddebug's were changed, or whether none
88 static void ddebug_change(const struct ddebug_query *query,
89 unsigned int flags, unsigned int mask)
92 struct ddebug_table *dt;
93 unsigned int newflags;
94 unsigned int nfound = 0;
97 /* search for matching ddebugs */
98 mutex_lock(&ddebug_lock);
99 list_for_each_entry(dt, &ddebug_tables, link) {
101 /* match against the module name */
102 if (query->module != NULL &&
103 strcmp(query->module, dt->mod_name))
106 for (i = 0 ; i < dt->num_ddebugs ; i++) {
107 struct _ddebug *dp = &dt->ddebugs[i];
109 /* match against the source filename */
110 if (query->filename != NULL &&
111 strcmp(query->filename, dp->filename) &&
112 strcmp(query->filename, basename(dp->filename)))
115 /* match against the function */
116 if (query->function != NULL &&
117 strcmp(query->function, dp->function))
120 /* match against the format */
121 if (query->format != NULL &&
122 strstr(dp->format, query->format) == NULL)
125 /* match against the line number range */
126 if (query->first_lineno &&
127 dp->lineno < query->first_lineno)
129 if (query->last_lineno &&
130 dp->lineno > query->last_lineno)
135 newflags = (dp->flags & mask) | flags;
136 if (newflags == dp->flags)
143 dp->flags = newflags;
145 jump_label_enable(&dp->enabled);
147 jump_label_disable(&dp->enabled);
151 "ddebug: changed %s:%d [%s]%s %s\n",
152 dp->filename, dp->lineno,
153 dt->mod_name, dp->function,
154 ddebug_describe_flags(dp, flagbuf,
158 mutex_unlock(&ddebug_lock);
160 if (!nfound && verbose)
161 printk(KERN_INFO "ddebug: no matches for query\n");
165 * Split the buffer `buf' into space-separated words.
166 * Handles simple " and ' quoting, i.e. without nested,
167 * embedded or escaped \". Return the number of words
170 static int ddebug_tokenize(char *buf, char *words[], int maxwords)
177 /* Skip leading whitespace */
178 buf = skip_spaces(buf);
180 break; /* oh, it was trailing whitespace */
182 /* Run `end' over a word, either whitespace separated or quoted */
183 if (*buf == '"' || *buf == '\'') {
185 for (end = buf ; *end && *end != quote ; end++)
188 return -EINVAL; /* unclosed quote */
190 for (end = buf ; *end && !isspace(*end) ; end++)
194 /* Here `buf' is the start of the word, `end' is one past the end */
196 if (nwords == maxwords)
197 return -EINVAL; /* ran out of words[] before bytes */
199 *end++ = '\0'; /* terminate the word */
200 words[nwords++] = buf;
206 printk(KERN_INFO "%s: split into words:", __func__);
207 for (i = 0 ; i < nwords ; i++)
208 printk(" \"%s\"", words[i]);
216 * Parse a single line number. Note that the empty string ""
217 * is treated as a special case and converted to zero, which
218 * is later treated as a "don't care" value.
220 static inline int parse_lineno(const char *str, unsigned int *val)
228 *val = simple_strtoul(str, &end, 10);
229 return end == NULL || end == str || *end != '\0' ? -EINVAL : 0;
233 * Undo octal escaping in a string, inplace. This is useful to
234 * allow the user to express a query which matches a format
235 * containing embedded spaces.
237 #define isodigit(c) ((c) >= '0' && (c) <= '7')
238 static char *unescape(char *str)
249 } else if (in[1] == 't') {
253 } else if (in[1] == 'n') {
257 } else if (isodigit(in[1]) &&
260 *out++ = ((in[1] - '0')<<6) |
275 * Parse words[] as a ddebug query specification, which is a series
276 * of (keyword, value) pairs chosen from these possibilities:
278 * func <function-name>
279 * file <full-pathname>
280 * file <base-filename>
281 * module <module-name>
282 * format <escaped-string-to-find-in-format>
284 * line <first-lineno>-<last-lineno> // where either may be empty
286 static int ddebug_parse_query(char *words[], int nwords,
287 struct ddebug_query *query)
291 /* check we have an even number of words */
294 memset(query, 0, sizeof(*query));
296 for (i = 0 ; i < nwords ; i += 2) {
297 if (!strcmp(words[i], "func"))
298 query->function = words[i+1];
299 else if (!strcmp(words[i], "file"))
300 query->filename = words[i+1];
301 else if (!strcmp(words[i], "module"))
302 query->module = words[i+1];
303 else if (!strcmp(words[i], "format"))
304 query->format = unescape(words[i+1]);
305 else if (!strcmp(words[i], "line")) {
306 char *first = words[i+1];
307 char *last = strchr(first, '-');
310 if (parse_lineno(first, &query->first_lineno) < 0)
313 /* range <first>-<last> */
314 if (parse_lineno(last, &query->last_lineno) < 0)
317 query->last_lineno = query->first_lineno;
321 printk(KERN_ERR "%s: unknown keyword \"%s\"\n",
328 printk(KERN_INFO "%s: q->function=\"%s\" q->filename=\"%s\" "
329 "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n",
330 __func__, query->function, query->filename,
331 query->module, query->format, query->first_lineno,
338 * Parse `str' as a flags specification, format [-+=][p]+.
339 * Sets up *maskp and *flagsp to be used when changing the
340 * flags fields of matched _ddebug's. Returns 0 on success
343 static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
359 printk(KERN_INFO "%s: op='%c'\n", __func__, op);
361 for ( ; *str ; ++str) {
364 flags |= _DPRINTK_FLAGS_PRINT;
373 printk(KERN_INFO "%s: flags=0x%x\n", __func__, flags);
375 /* calculate final *flagsp, *maskp according to mask and op */
391 printk(KERN_INFO "%s: *flagsp=0x%x *maskp=0x%x\n",
392 __func__, *flagsp, *maskp);
396 static int ddebug_exec_query(char *query_string)
398 unsigned int flags = 0, mask = 0;
399 struct ddebug_query query;
402 char *words[MAXWORDS];
404 nwords = ddebug_tokenize(query_string, words, MAXWORDS);
407 if (ddebug_parse_query(words, nwords-1, &query))
409 if (ddebug_parse_flags(words[nwords-1], &flags, &mask))
412 /* actually go and implement the change */
413 ddebug_change(&query, flags, mask);
417 static __initdata char ddebug_setup_string[1024];
418 static __init int ddebug_setup_query(char *str)
420 if (strlen(str) >= 1024) {
421 pr_warning("ddebug boot param string too large\n");
424 strcpy(ddebug_setup_string, str);
428 __setup("ddebug_query=", ddebug_setup_query);
431 * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the
432 * command text from userspace, parses and executes it.
434 static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
435 size_t len, loff_t *offp)
442 /* we don't check *offp -- multiple writes() are allowed */
443 if (len > sizeof(tmpbuf)-1)
445 if (copy_from_user(tmpbuf, ubuf, len))
449 printk(KERN_INFO "%s: read %d bytes from userspace\n",
452 ret = ddebug_exec_query(tmpbuf);
461 * Set the iterator to point to the first _ddebug object
462 * and return a pointer to that first object. Returns
463 * NULL if there are no _ddebugs at all.
465 static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
467 if (list_empty(&ddebug_tables)) {
472 iter->table = list_entry(ddebug_tables.next,
473 struct ddebug_table, link);
475 return &iter->table->ddebugs[iter->idx];
479 * Advance the iterator to point to the next _ddebug
480 * object from the one the iterator currently points at,
481 * and returns a pointer to the new _ddebug. Returns
482 * NULL if the iterator has seen all the _ddebugs.
484 static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
486 if (iter->table == NULL)
488 if (++iter->idx == iter->table->num_ddebugs) {
489 /* iterate to next table */
491 if (list_is_last(&iter->table->link, &ddebug_tables)) {
495 iter->table = list_entry(iter->table->link.next,
496 struct ddebug_table, link);
498 return &iter->table->ddebugs[iter->idx];
502 * Seq_ops start method. Called at the start of every
503 * read() call from userspace. Takes the ddebug_lock and
504 * seeks the seq_file's iterator to the given position.
506 static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
508 struct ddebug_iter *iter = m->private;
513 printk(KERN_INFO "%s: called m=%p *pos=%lld\n",
514 __func__, m, (unsigned long long)*pos);
516 mutex_lock(&ddebug_lock);
519 return SEQ_START_TOKEN;
522 dp = ddebug_iter_first(iter);
523 while (dp != NULL && --n > 0)
524 dp = ddebug_iter_next(iter);
529 * Seq_ops next method. Called several times within a read()
530 * call from userspace, with ddebug_lock held. Walks to the
531 * next _ddebug object with a special case for the header line.
533 static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
535 struct ddebug_iter *iter = m->private;
539 printk(KERN_INFO "%s: called m=%p p=%p *pos=%lld\n",
540 __func__, m, p, (unsigned long long)*pos);
542 if (p == SEQ_START_TOKEN)
543 dp = ddebug_iter_first(iter);
545 dp = ddebug_iter_next(iter);
551 * Seq_ops show method. Called several times within a read()
552 * call from userspace, with ddebug_lock held. Formats the
553 * current _ddebug as a single human-readable line, with a
554 * special case for the header line.
556 static int ddebug_proc_show(struct seq_file *m, void *p)
558 struct ddebug_iter *iter = m->private;
559 struct _ddebug *dp = p;
563 printk(KERN_INFO "%s: called m=%p p=%p\n",
566 if (p == SEQ_START_TOKEN) {
568 "# filename:lineno [module]function flags format\n");
572 seq_printf(m, "%s:%u [%s]%s %s \"",
573 dp->filename, dp->lineno,
574 iter->table->mod_name, dp->function,
575 ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
576 seq_escape(m, dp->format, "\t\r\n\"");
583 * Seq_ops stop method. Called at the end of each read()
584 * call from userspace. Drops ddebug_lock.
586 static void ddebug_proc_stop(struct seq_file *m, void *p)
589 printk(KERN_INFO "%s: called m=%p p=%p\n",
591 mutex_unlock(&ddebug_lock);
594 static const struct seq_operations ddebug_proc_seqops = {
595 .start = ddebug_proc_start,
596 .next = ddebug_proc_next,
597 .show = ddebug_proc_show,
598 .stop = ddebug_proc_stop
602 * File_ops->open method for <debugfs>/dynamic_debug/control. Does the seq_file
603 * setup dance, and also creates an iterator to walk the _ddebugs.
604 * Note that we create a seq_file always, even for O_WRONLY files
605 * where it's not needed, as doing so simplifies the ->release method.
607 static int ddebug_proc_open(struct inode *inode, struct file *file)
609 struct ddebug_iter *iter;
613 printk(KERN_INFO "%s: called\n", __func__);
615 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
619 err = seq_open(file, &ddebug_proc_seqops);
624 ((struct seq_file *) file->private_data)->private = iter;
628 static const struct file_operations ddebug_proc_fops = {
629 .owner = THIS_MODULE,
630 .open = ddebug_proc_open,
633 .release = seq_release_private,
634 .write = ddebug_proc_write
638 * Allocate a new ddebug_table for the given module
639 * and add it to the global list.
641 int ddebug_add_module(struct _ddebug *tab, unsigned int n,
644 struct ddebug_table *dt;
647 dt = kzalloc(sizeof(*dt), GFP_KERNEL);
650 new_name = kstrdup(name, GFP_KERNEL);
651 if (new_name == NULL) {
655 dt->mod_name = new_name;
660 mutex_lock(&ddebug_lock);
661 list_add_tail(&dt->link, &ddebug_tables);
662 mutex_unlock(&ddebug_lock);
665 printk(KERN_INFO "%u debug prints in module %s\n",
669 EXPORT_SYMBOL_GPL(ddebug_add_module);
671 static void ddebug_table_free(struct ddebug_table *dt)
673 list_del_init(&dt->link);
679 * Called in response to a module being unloaded. Removes
680 * any ddebug_table's which point at the module.
682 int ddebug_remove_module(const char *mod_name)
684 struct ddebug_table *dt, *nextdt;
688 printk(KERN_INFO "%s: removing module \"%s\"\n",
691 mutex_lock(&ddebug_lock);
692 list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
693 if (!strcmp(dt->mod_name, mod_name)) {
694 ddebug_table_free(dt);
698 mutex_unlock(&ddebug_lock);
701 EXPORT_SYMBOL_GPL(ddebug_remove_module);
703 static void ddebug_remove_all_tables(void)
705 mutex_lock(&ddebug_lock);
706 while (!list_empty(&ddebug_tables)) {
707 struct ddebug_table *dt = list_entry(ddebug_tables.next,
710 ddebug_table_free(dt);
712 mutex_unlock(&ddebug_lock);
715 static __initdata int ddebug_init_success;
717 static int __init dynamic_debug_init_debugfs(void)
719 struct dentry *dir, *file;
721 if (!ddebug_init_success)
724 dir = debugfs_create_dir("dynamic_debug", NULL);
727 file = debugfs_create_file("control", 0644, dir, NULL,
736 static int __init dynamic_debug_init(void)
738 struct _ddebug *iter, *iter_start;
739 const char *modname = NULL;
743 if (__start___verbose != __stop___verbose) {
744 iter = __start___verbose;
745 modname = iter->modname;
747 for (; iter < __stop___verbose; iter++) {
748 if (strcmp(modname, iter->modname)) {
749 ret = ddebug_add_module(iter_start, n, modname);
753 modname = iter->modname;
758 ret = ddebug_add_module(iter_start, n, modname);
761 /* ddebug_query boot param got passed -> set it up */
762 if (ddebug_setup_string[0] != '\0') {
763 ret = ddebug_exec_query(ddebug_setup_string);
765 pr_warning("Invalid ddebug boot param %s",
766 ddebug_setup_string);
768 pr_info("ddebug initialized with string %s",
769 ddebug_setup_string);
774 ddebug_remove_all_tables();
776 ddebug_init_success = 1;
779 /* Allow early initialization for boot messages via boot param */
780 arch_initcall(dynamic_debug_init);
781 /* Debugfs setup must be done later */
782 module_init(dynamic_debug_init_debugfs);