xref: /linux/lib/dynamic_debug.c (revision 44e668c6faa9a6c477a32788e7e88f0754c54a4e)
1 /*
2  * lib/dynamic_debug.c
3  *
4  * make pr_debug()/dev_dbg() calls runtime configurable based upon their
5  * source module.
6  *
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  */
11 
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>
30 
31 extern struct _ddebug __start___verbose[];
32 extern struct _ddebug __stop___verbose[];
33 
34 struct ddebug_table {
35 	struct list_head link;
36 	char *mod_name;
37 	unsigned int num_ddebugs;
38 	unsigned int num_enabled;
39 	struct _ddebug *ddebugs;
40 };
41 
42 struct ddebug_query {
43 	const char *filename;
44 	const char *module;
45 	const char *function;
46 	const char *format;
47 	unsigned int first_lineno, last_lineno;
48 };
49 
50 struct ddebug_iter {
51 	struct ddebug_table *table;
52 	unsigned int idx;
53 };
54 
55 static DEFINE_MUTEX(ddebug_lock);
56 static LIST_HEAD(ddebug_tables);
57 static int verbose = 0;
58 
59 /* Return the last part of a pathname */
60 static inline const char *basename(const char *path)
61 {
62 	const char *tail = strrchr(path, '/');
63 	return tail ? tail+1 : path;
64 }
65 
66 /* format a string into buf[] which describes the _ddebug's flags */
67 static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,
68 				    size_t maxlen)
69 {
70 	char *p = buf;
71 
72 	BUG_ON(maxlen < 4);
73 	if (dp->flags & _DPRINTK_FLAGS_PRINT)
74 		*p++ = 'p';
75 	if (p == buf)
76 		*p++ = '-';
77 	*p = '\0';
78 
79 	return buf;
80 }
81 
82 /*
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
86  * were matched.
87  */
88 static void ddebug_change(const struct ddebug_query *query,
89 			   unsigned int flags, unsigned int mask)
90 {
91 	int i;
92 	struct ddebug_table *dt;
93 	unsigned int newflags;
94 	unsigned int nfound = 0;
95 	char flagbuf[8];
96 
97 	/* search for matching ddebugs */
98 	mutex_lock(&ddebug_lock);
99 	list_for_each_entry(dt, &ddebug_tables, link) {
100 
101 		/* match against the module name */
102 		if (query->module != NULL &&
103 		    strcmp(query->module, dt->mod_name))
104 			continue;
105 
106 		for (i = 0 ; i < dt->num_ddebugs ; i++) {
107 			struct _ddebug *dp = &dt->ddebugs[i];
108 
109 			/* match against the source filename */
110 			if (query->filename != NULL &&
111 			    strcmp(query->filename, dp->filename) &&
112 			    strcmp(query->filename, basename(dp->filename)))
113 				continue;
114 
115 			/* match against the function */
116 			if (query->function != NULL &&
117 			    strcmp(query->function, dp->function))
118 				continue;
119 
120 			/* match against the format */
121 			if (query->format != NULL &&
122 			    strstr(dp->format, query->format) == NULL)
123 				continue;
124 
125 			/* match against the line number range */
126 			if (query->first_lineno &&
127 			    dp->lineno < query->first_lineno)
128 				continue;
129 			if (query->last_lineno &&
130 			    dp->lineno > query->last_lineno)
131 				continue;
132 
133 			nfound++;
134 
135 			newflags = (dp->flags & mask) | flags;
136 			if (newflags == dp->flags)
137 				continue;
138 
139 			if (!newflags)
140 				dt->num_enabled--;
141 			else if (!dp->flags)
142 				dt->num_enabled++;
143 			dp->flags = newflags;
144 			if (newflags) {
145 				jump_label_enable(&dp->enabled);
146 			} else {
147 				jump_label_disable(&dp->enabled);
148 			}
149 			if (verbose)
150 				printk(KERN_INFO
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,
155 							sizeof(flagbuf)));
156 		}
157 	}
158 	mutex_unlock(&ddebug_lock);
159 
160 	if (!nfound && verbose)
161 		printk(KERN_INFO "ddebug: no matches for query\n");
162 }
163 
164 /*
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
168  * or <0 on error.
169  */
170 static int ddebug_tokenize(char *buf, char *words[], int maxwords)
171 {
172 	int nwords = 0;
173 
174 	while (*buf) {
175 		char *end;
176 
177 		/* Skip leading whitespace */
178 		buf = skip_spaces(buf);
179 		if (!*buf)
180 			break;	/* oh, it was trailing whitespace */
181 
182 		/* Run `end' over a word, either whitespace separated or quoted */
183 		if (*buf == '"' || *buf == '\'') {
184 			int quote = *buf++;
185 			for (end = buf ; *end && *end != quote ; end++)
186 				;
187 			if (!*end)
188 				return -EINVAL;	/* unclosed quote */
189 		} else {
190 			for (end = buf ; *end && !isspace(*end) ; end++)
191 				;
192 			BUG_ON(end == buf);
193 		}
194 		/* Here `buf' is the start of the word, `end' is one past the end */
195 
196 		if (nwords == maxwords)
197 			return -EINVAL;	/* ran out of words[] before bytes */
198 		if (*end)
199 			*end++ = '\0';	/* terminate the word */
200 		words[nwords++] = buf;
201 		buf = end;
202 	}
203 
204 	if (verbose) {
205 		int i;
206 		printk(KERN_INFO "%s: split into words:", __func__);
207 		for (i = 0 ; i < nwords ; i++)
208 			printk(" \"%s\"", words[i]);
209 		printk("\n");
210 	}
211 
212 	return nwords;
213 }
214 
215 /*
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.
219  */
220 static inline int parse_lineno(const char *str, unsigned int *val)
221 {
222 	char *end = NULL;
223 	BUG_ON(str == NULL);
224 	if (*str == '\0') {
225 		*val = 0;
226 		return 0;
227 	}
228 	*val = simple_strtoul(str, &end, 10);
229 	return end == NULL || end == str || *end != '\0' ? -EINVAL : 0;
230 }
231 
232 /*
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.
236  */
237 #define isodigit(c)		((c) >= '0' && (c) <= '7')
238 static char *unescape(char *str)
239 {
240 	char *in = str;
241 	char *out = str;
242 
243 	while (*in) {
244 		if (*in == '\\') {
245 			if (in[1] == '\\') {
246 				*out++ = '\\';
247 				in += 2;
248 				continue;
249 			} else if (in[1] == 't') {
250 				*out++ = '\t';
251 				in += 2;
252 				continue;
253 			} else if (in[1] == 'n') {
254 				*out++ = '\n';
255 				in += 2;
256 				continue;
257 			} else if (isodigit(in[1]) &&
258 			         isodigit(in[2]) &&
259 			         isodigit(in[3])) {
260 				*out++ = ((in[1] - '0')<<6) |
261 				          ((in[2] - '0')<<3) |
262 				          (in[3] - '0');
263 				in += 4;
264 				continue;
265 			}
266 		}
267 		*out++ = *in++;
268 	}
269 	*out = '\0';
270 
271 	return str;
272 }
273 
274 /*
275  * Parse words[] as a ddebug query specification, which is a series
276  * of (keyword, value) pairs chosen from these possibilities:
277  *
278  * func <function-name>
279  * file <full-pathname>
280  * file <base-filename>
281  * module <module-name>
282  * format <escaped-string-to-find-in-format>
283  * line <lineno>
284  * line <first-lineno>-<last-lineno> // where either may be empty
285  */
286 static int ddebug_parse_query(char *words[], int nwords,
287 			       struct ddebug_query *query)
288 {
289 	unsigned int i;
290 
291 	/* check we have an even number of words */
292 	if (nwords % 2 != 0)
293 		return -EINVAL;
294 	memset(query, 0, sizeof(*query));
295 
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, '-');
308 			if (last)
309 				*last++ = '\0';
310 			if (parse_lineno(first, &query->first_lineno) < 0)
311 				return -EINVAL;
312 			if (last != NULL) {
313 				/* range <first>-<last> */
314 				if (parse_lineno(last, &query->last_lineno) < 0)
315 					return -EINVAL;
316 			} else {
317 				query->last_lineno = query->first_lineno;
318 			}
319 		} else {
320 			if (verbose)
321 				printk(KERN_ERR "%s: unknown keyword \"%s\"\n",
322 					__func__, words[i]);
323 			return -EINVAL;
324 		}
325 	}
326 
327 	if (verbose)
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,
332 			query->last_lineno);
333 
334 	return 0;
335 }
336 
337 /*
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
341  * or <0 on error.
342  */
343 static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
344 			       unsigned int *maskp)
345 {
346 	unsigned flags = 0;
347 	int op = '=';
348 
349 	switch (*str) {
350 	case '+':
351 	case '-':
352 	case '=':
353 		op = *str++;
354 		break;
355 	default:
356 		return -EINVAL;
357 	}
358 	if (verbose)
359 		printk(KERN_INFO "%s: op='%c'\n", __func__, op);
360 
361 	for ( ; *str ; ++str) {
362 		switch (*str) {
363 		case 'p':
364 			flags |= _DPRINTK_FLAGS_PRINT;
365 			break;
366 		default:
367 			return -EINVAL;
368 		}
369 	}
370 	if (flags == 0)
371 		return -EINVAL;
372 	if (verbose)
373 		printk(KERN_INFO "%s: flags=0x%x\n", __func__, flags);
374 
375 	/* calculate final *flagsp, *maskp according to mask and op */
376 	switch (op) {
377 	case '=':
378 		*maskp = 0;
379 		*flagsp = flags;
380 		break;
381 	case '+':
382 		*maskp = ~0U;
383 		*flagsp = flags;
384 		break;
385 	case '-':
386 		*maskp = ~flags;
387 		*flagsp = 0;
388 		break;
389 	}
390 	if (verbose)
391 		printk(KERN_INFO "%s: *flagsp=0x%x *maskp=0x%x\n",
392 			__func__, *flagsp, *maskp);
393 	return 0;
394 }
395 
396 /*
397  * File_ops->write method for <debugfs>/dynamic_debug/conrol.  Gathers the
398  * command text from userspace, parses and executes it.
399  */
400 static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
401 				  size_t len, loff_t *offp)
402 {
403 	unsigned int flags = 0, mask = 0;
404 	struct ddebug_query query;
405 #define MAXWORDS 9
406 	int nwords;
407 	char *words[MAXWORDS];
408 	char tmpbuf[256];
409 
410 	if (len == 0)
411 		return 0;
412 	/* we don't check *offp -- multiple writes() are allowed */
413 	if (len > sizeof(tmpbuf)-1)
414 		return -E2BIG;
415 	if (copy_from_user(tmpbuf, ubuf, len))
416 		return -EFAULT;
417 	tmpbuf[len] = '\0';
418 	if (verbose)
419 		printk(KERN_INFO "%s: read %d bytes from userspace\n",
420 			__func__, (int)len);
421 
422 	nwords = ddebug_tokenize(tmpbuf, words, MAXWORDS);
423 	if (nwords <= 0)
424 		return -EINVAL;
425 	if (ddebug_parse_query(words, nwords-1, &query))
426 		return -EINVAL;
427 	if (ddebug_parse_flags(words[nwords-1], &flags, &mask))
428 		return -EINVAL;
429 
430 	/* actually go and implement the change */
431 	ddebug_change(&query, flags, mask);
432 
433 	*offp += len;
434 	return len;
435 }
436 
437 /*
438  * Set the iterator to point to the first _ddebug object
439  * and return a pointer to that first object.  Returns
440  * NULL if there are no _ddebugs at all.
441  */
442 static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
443 {
444 	if (list_empty(&ddebug_tables)) {
445 		iter->table = NULL;
446 		iter->idx = 0;
447 		return NULL;
448 	}
449 	iter->table = list_entry(ddebug_tables.next,
450 				 struct ddebug_table, link);
451 	iter->idx = 0;
452 	return &iter->table->ddebugs[iter->idx];
453 }
454 
455 /*
456  * Advance the iterator to point to the next _ddebug
457  * object from the one the iterator currently points at,
458  * and returns a pointer to the new _ddebug.  Returns
459  * NULL if the iterator has seen all the _ddebugs.
460  */
461 static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
462 {
463 	if (iter->table == NULL)
464 		return NULL;
465 	if (++iter->idx == iter->table->num_ddebugs) {
466 		/* iterate to next table */
467 		iter->idx = 0;
468 		if (list_is_last(&iter->table->link, &ddebug_tables)) {
469 			iter->table = NULL;
470 			return NULL;
471 		}
472 		iter->table = list_entry(iter->table->link.next,
473 					 struct ddebug_table, link);
474 	}
475 	return &iter->table->ddebugs[iter->idx];
476 }
477 
478 /*
479  * Seq_ops start method.  Called at the start of every
480  * read() call from userspace.  Takes the ddebug_lock and
481  * seeks the seq_file's iterator to the given position.
482  */
483 static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
484 {
485 	struct ddebug_iter *iter = m->private;
486 	struct _ddebug *dp;
487 	int n = *pos;
488 
489 	if (verbose)
490 		printk(KERN_INFO "%s: called m=%p *pos=%lld\n",
491 			__func__, m, (unsigned long long)*pos);
492 
493 	mutex_lock(&ddebug_lock);
494 
495 	if (!n)
496 		return SEQ_START_TOKEN;
497 	if (n < 0)
498 		return NULL;
499 	dp = ddebug_iter_first(iter);
500 	while (dp != NULL && --n > 0)
501 		dp = ddebug_iter_next(iter);
502 	return dp;
503 }
504 
505 /*
506  * Seq_ops next method.  Called several times within a read()
507  * call from userspace, with ddebug_lock held.  Walks to the
508  * next _ddebug object with a special case for the header line.
509  */
510 static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
511 {
512 	struct ddebug_iter *iter = m->private;
513 	struct _ddebug *dp;
514 
515 	if (verbose)
516 		printk(KERN_INFO "%s: called m=%p p=%p *pos=%lld\n",
517 			__func__, m, p, (unsigned long long)*pos);
518 
519 	if (p == SEQ_START_TOKEN)
520 		dp = ddebug_iter_first(iter);
521 	else
522 		dp = ddebug_iter_next(iter);
523 	++*pos;
524 	return dp;
525 }
526 
527 /*
528  * Seq_ops show method.  Called several times within a read()
529  * call from userspace, with ddebug_lock held.  Formats the
530  * current _ddebug as a single human-readable line, with a
531  * special case for the header line.
532  */
533 static int ddebug_proc_show(struct seq_file *m, void *p)
534 {
535 	struct ddebug_iter *iter = m->private;
536 	struct _ddebug *dp = p;
537 	char flagsbuf[8];
538 
539 	if (verbose)
540 		printk(KERN_INFO "%s: called m=%p p=%p\n",
541 			__func__, m, p);
542 
543 	if (p == SEQ_START_TOKEN) {
544 		seq_puts(m,
545 			"# filename:lineno [module]function flags format\n");
546 		return 0;
547 	}
548 
549 	seq_printf(m, "%s:%u [%s]%s %s \"",
550 		   dp->filename, dp->lineno,
551 		   iter->table->mod_name, dp->function,
552 		   ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
553 	seq_escape(m, dp->format, "\t\r\n\"");
554 	seq_puts(m, "\"\n");
555 
556 	return 0;
557 }
558 
559 /*
560  * Seq_ops stop method.  Called at the end of each read()
561  * call from userspace.  Drops ddebug_lock.
562  */
563 static void ddebug_proc_stop(struct seq_file *m, void *p)
564 {
565 	if (verbose)
566 		printk(KERN_INFO "%s: called m=%p p=%p\n",
567 			__func__, m, p);
568 	mutex_unlock(&ddebug_lock);
569 }
570 
571 static const struct seq_operations ddebug_proc_seqops = {
572 	.start = ddebug_proc_start,
573 	.next = ddebug_proc_next,
574 	.show = ddebug_proc_show,
575 	.stop = ddebug_proc_stop
576 };
577 
578 /*
579  * File_ops->open method for <debugfs>/dynamic_debug/control.  Does the seq_file
580  * setup dance, and also creates an iterator to walk the _ddebugs.
581  * Note that we create a seq_file always, even for O_WRONLY files
582  * where it's not needed, as doing so simplifies the ->release method.
583  */
584 static int ddebug_proc_open(struct inode *inode, struct file *file)
585 {
586 	struct ddebug_iter *iter;
587 	int err;
588 
589 	if (verbose)
590 		printk(KERN_INFO "%s: called\n", __func__);
591 
592 	iter = kzalloc(sizeof(*iter), GFP_KERNEL);
593 	if (iter == NULL)
594 		return -ENOMEM;
595 
596 	err = seq_open(file, &ddebug_proc_seqops);
597 	if (err) {
598 		kfree(iter);
599 		return err;
600 	}
601 	((struct seq_file *) file->private_data)->private = iter;
602 	return 0;
603 }
604 
605 static const struct file_operations ddebug_proc_fops = {
606 	.owner = THIS_MODULE,
607 	.open = ddebug_proc_open,
608 	.read = seq_read,
609 	.llseek = seq_lseek,
610 	.release = seq_release_private,
611 	.write = ddebug_proc_write
612 };
613 
614 /*
615  * Allocate a new ddebug_table for the given module
616  * and add it to the global list.
617  */
618 int ddebug_add_module(struct _ddebug *tab, unsigned int n,
619 			     const char *name)
620 {
621 	struct ddebug_table *dt;
622 	char *new_name;
623 
624 	dt = kzalloc(sizeof(*dt), GFP_KERNEL);
625 	if (dt == NULL)
626 		return -ENOMEM;
627 	new_name = kstrdup(name, GFP_KERNEL);
628 	if (new_name == NULL) {
629 		kfree(dt);
630 		return -ENOMEM;
631 	}
632 	dt->mod_name = new_name;
633 	dt->num_ddebugs = n;
634 	dt->num_enabled = 0;
635 	dt->ddebugs = tab;
636 
637 	mutex_lock(&ddebug_lock);
638 	list_add_tail(&dt->link, &ddebug_tables);
639 	mutex_unlock(&ddebug_lock);
640 
641 	if (verbose)
642 		printk(KERN_INFO "%u debug prints in module %s\n",
643 				 n, dt->mod_name);
644 	return 0;
645 }
646 EXPORT_SYMBOL_GPL(ddebug_add_module);
647 
648 static void ddebug_table_free(struct ddebug_table *dt)
649 {
650 	list_del_init(&dt->link);
651 	kfree(dt->mod_name);
652 	kfree(dt);
653 }
654 
655 /*
656  * Called in response to a module being unloaded.  Removes
657  * any ddebug_table's which point at the module.
658  */
659 int ddebug_remove_module(const char *mod_name)
660 {
661 	struct ddebug_table *dt, *nextdt;
662 	int ret = -ENOENT;
663 
664 	if (verbose)
665 		printk(KERN_INFO "%s: removing module \"%s\"\n",
666 				__func__, mod_name);
667 
668 	mutex_lock(&ddebug_lock);
669 	list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
670 		if (!strcmp(dt->mod_name, mod_name)) {
671 			ddebug_table_free(dt);
672 			ret = 0;
673 		}
674 	}
675 	mutex_unlock(&ddebug_lock);
676 	return ret;
677 }
678 EXPORT_SYMBOL_GPL(ddebug_remove_module);
679 
680 static void ddebug_remove_all_tables(void)
681 {
682 	mutex_lock(&ddebug_lock);
683 	while (!list_empty(&ddebug_tables)) {
684 		struct ddebug_table *dt = list_entry(ddebug_tables.next,
685 						      struct ddebug_table,
686 						      link);
687 		ddebug_table_free(dt);
688 	}
689 	mutex_unlock(&ddebug_lock);
690 }
691 
692 static int __init dynamic_debug_init(void)
693 {
694 	struct dentry *dir, *file;
695 	struct _ddebug *iter, *iter_start;
696 	const char *modname = NULL;
697 	int ret = 0;
698 	int n = 0;
699 
700 	dir = debugfs_create_dir("dynamic_debug", NULL);
701 	if (!dir)
702 		return -ENOMEM;
703 	file = debugfs_create_file("control", 0644, dir, NULL,
704 					&ddebug_proc_fops);
705 	if (!file) {
706 		debugfs_remove(dir);
707 		return -ENOMEM;
708 	}
709 	if (__start___verbose != __stop___verbose) {
710 		iter = __start___verbose;
711 		modname = iter->modname;
712 		iter_start = iter;
713 		for (; iter < __stop___verbose; iter++) {
714 			if (strcmp(modname, iter->modname)) {
715 				ret = ddebug_add_module(iter_start, n, modname);
716 				if (ret)
717 					goto out_free;
718 				n = 0;
719 				modname = iter->modname;
720 				iter_start = iter;
721 			}
722 			n++;
723 		}
724 		ret = ddebug_add_module(iter_start, n, modname);
725 	}
726 out_free:
727 	if (ret) {
728 		ddebug_remove_all_tables();
729 		debugfs_remove(dir);
730 		debugfs_remove(file);
731 	}
732 	return 0;
733 }
734 module_init(dynamic_debug_init);
735