dynamic_debug.c 25.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * lib/dynamic_debug.c
 *
 * make pr_debug()/dev_dbg() calls runtime configurable based upon their
 * source module.
 *
 * Copyright (C) 2008 Jason Baron <jbaron@redhat.com>
 * By Greg Banks <gnb@melbourne.sgi.com>
 * Copyright (c) 2008 Silicon Graphics Inc.  All Rights Reserved.
10
 * Copyright (C) 2011 Bart Van Assche.  All Rights Reserved.
11
 * Copyright (C) 2013 Du, Changbin <changbin.du@gmail.com>
12 13
 */

14 15
#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__

16 17
#include <linux/kernel.h>
#include <linux/module.h>
18 19 20
#include <linux/moduleparam.h>
#include <linux/kallsyms.h>
#include <linux/types.h>
21
#include <linux/mutex.h>
22
#include <linux/proc_fs.h>
23
#include <linux/seq_file.h>
24 25
#include <linux/list.h>
#include <linux/sysctl.h>
26
#include <linux/ctype.h>
27
#include <linux/string.h>
28
#include <linux/parser.h>
29
#include <linux/string_helpers.h>
30
#include <linux/uaccess.h>
31 32
#include <linux/dynamic_debug.h>
#include <linux/debugfs.h>
33
#include <linux/slab.h>
34
#include <linux/jump_label.h>
35
#include <linux/hardirq.h>
36
#include <linux/sched.h>
37
#include <linux/device.h>
38
#include <linux/netdevice.h>
39 40 41 42 43 44

extern struct _ddebug __start___verbose[];
extern struct _ddebug __stop___verbose[];

struct ddebug_table {
	struct list_head link;
45
	const char *mod_name;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	unsigned int num_ddebugs;
	struct _ddebug *ddebugs;
};

struct ddebug_query {
	const char *filename;
	const char *module;
	const char *function;
	const char *format;
	unsigned int first_lineno, last_lineno;
};

struct ddebug_iter {
	struct ddebug_table *table;
	unsigned int idx;
};

static DEFINE_MUTEX(ddebug_lock);
static LIST_HEAD(ddebug_tables);
65
static int verbose;
66
module_param(verbose, int, 0644);
67

68 69 70 71 72 73 74 75 76 77 78
/* Return the path relative to source root */
static inline const char *trim_prefix(const char *path)
{
	int skip = strlen(__FILE__) - strlen("lib/dynamic_debug.c");

	if (strncmp(path, __FILE__, skip))
		skip = 0; /* prefix mismatch, don't skip */

	return path + skip;
}

79 80 81 82 83 84
static struct { unsigned flag:8; char opt_char; } opt_array[] = {
	{ _DPRINTK_FLAGS_PRINT, 'p' },
	{ _DPRINTK_FLAGS_INCL_MODNAME, 'm' },
	{ _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' },
	{ _DPRINTK_FLAGS_INCL_LINENO, 'l' },
	{ _DPRINTK_FLAGS_INCL_TID, 't' },
85
	{ _DPRINTK_FLAGS_NONE, '_' },
86 87
};

88 89 90 91 92
/* format a string into buf[] which describes the _ddebug's flags */
static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,
				    size_t maxlen)
{
	char *p = buf;
93
	int i;
94

95
	BUG_ON(maxlen < 6);
96 97 98
	for (i = 0; i < ARRAY_SIZE(opt_array); ++i)
		if (dp->flags & opt_array[i].flag)
			*p++ = opt_array[i].opt_char;
99
	if (p == buf)
100
		*p++ = '_';
101 102 103 104 105
	*p = '\0';

	return buf;
}

106
#define vpr_info(fmt, ...)					\
107
do {								\
108 109
	if (verbose)						\
		pr_info(fmt, ##__VA_ARGS__);			\
110 111
} while (0)

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
static void vpr_info_dq(const struct ddebug_query *query, const char *msg)
{
	/* trim any trailing newlines */
	int fmtlen = 0;

	if (query->format) {
		fmtlen = strlen(query->format);
		while (fmtlen && query->format[fmtlen - 1] == '\n')
			fmtlen--;
	}

	vpr_info("%s: func=\"%s\" file=\"%s\" module=\"%s\" format=\"%.*s\" lineno=%u-%u\n",
		 msg,
		 query->function ? query->function : "",
		 query->filename ? query->filename : "",
		 query->module ? query->module : "",
		 fmtlen, query->format ? query->format : "",
		 query->first_lineno, query->last_lineno);
}

132
/*
133 134 135 136
 * Search the tables for _ddebug's which match the given `query' and
 * apply the `flags' and `mask' to them.  Returns number of matching
 * callsites, normally the same as number of changes.  If verbose,
 * logs the changes.  Takes ddebug_lock.
137
 */
138 139
static int ddebug_change(const struct ddebug_query *query,
			unsigned int flags, unsigned int mask)
140 141 142 143 144
{
	int i;
	struct ddebug_table *dt;
	unsigned int newflags;
	unsigned int nfound = 0;
145
	char flagbuf[10];
146 147 148 149 150 151

	/* search for matching ddebugs */
	mutex_lock(&ddebug_lock);
	list_for_each_entry(dt, &ddebug_tables, link) {

		/* match against the module name */
152 153
		if (query->module &&
		    !match_wildcard(query->module, dt->mod_name))
154 155
			continue;

156
		for (i = 0; i < dt->num_ddebugs; i++) {
157 158 159
			struct _ddebug *dp = &dt->ddebugs[i];

			/* match against the source filename */
160
			if (query->filename &&
161 162 163 164 165
			    !match_wildcard(query->filename, dp->filename) &&
			    !match_wildcard(query->filename,
					   kbasename(dp->filename)) &&
			    !match_wildcard(query->filename,
					   trim_prefix(dp->filename)))
166 167 168
				continue;

			/* match against the function */
169
			if (query->function &&
170
			    !match_wildcard(query->function, dp->function))
171 172 173
				continue;

			/* match against the format */
174 175
			if (query->format &&
			    !strstr(dp->format, query->format))
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
				continue;

			/* match against the line number range */
			if (query->first_lineno &&
			    dp->lineno < query->first_lineno)
				continue;
			if (query->last_lineno &&
			    dp->lineno > query->last_lineno)
				continue;

			nfound++;

			newflags = (dp->flags & mask) | flags;
			if (newflags == dp->flags)
				continue;
191 192 193 194 195 196 197
#ifdef HAVE_JUMP_LABEL
			if (dp->flags & _DPRINTK_FLAGS_PRINT) {
				if (!(flags & _DPRINTK_FLAGS_PRINT))
					static_branch_disable(&dp->key.dd_key_true);
			} else if (flags & _DPRINTK_FLAGS_PRINT)
				static_branch_enable(&dp->key.dd_key_true);
#endif
198
			dp->flags = newflags;
199
			vpr_info("changed %s:%d [%s]%s =%s\n",
200 201 202 203
				 trim_prefix(dp->filename), dp->lineno,
				 dt->mod_name, dp->function,
				 ddebug_describe_flags(dp, flagbuf,
						       sizeof(flagbuf)));
204 205 206 207 208
		}
	}
	mutex_unlock(&ddebug_lock);

	if (!nfound && verbose)
209
		pr_info("no matches for query\n");
210 211

	return nfound;
212 213 214 215
}

/*
 * Split the buffer `buf' into space-separated words.
216 217 218
 * Handles simple " and ' quoting, i.e. without nested,
 * embedded or escaped \".  Return the number of words
 * or <0 on error.
219 220 221 222 223
 */
static int ddebug_tokenize(char *buf, char *words[], int maxwords)
{
	int nwords = 0;

224 225 226 227
	while (*buf) {
		char *end;

		/* Skip leading whitespace */
228
		buf = skip_spaces(buf);
229 230
		if (!*buf)
			break;	/* oh, it was trailing whitespace */
231 232
		if (*buf == '#')
			break;	/* token starts comment, skip rest of line */
233

234
		/* find `end' of word, whitespace separated or quoted */
235 236
		if (*buf == '"' || *buf == '\'') {
			int quote = *buf++;
237
			for (end = buf; *end && *end != quote; end++)
238
				;
239 240
			if (!*end) {
				pr_err("unclosed quote: %s\n", buf);
241
				return -EINVAL;	/* unclosed quote */
242
			}
243
		} else {
244
			for (end = buf; *end && !isspace(*end); end++)
245 246 247 248
				;
			BUG_ON(end == buf);
		}

249
		/* `buf' is start of word, `end' is one past its end */
250 251
		if (nwords == maxwords) {
			pr_err("too many words, legal max <=%d\n", maxwords);
252
			return -EINVAL;	/* ran out of words[] before bytes */
253
		}
254 255 256 257 258
		if (*end)
			*end++ = '\0';	/* terminate the word */
		words[nwords++] = buf;
		buf = end;
	}
259 260 261

	if (verbose) {
		int i;
262
		pr_info("split into words:");
263
		for (i = 0; i < nwords; i++)
264 265
			pr_cont(" \"%s\"", words[i]);
		pr_cont("\n");
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
	}

	return nwords;
}

/*
 * Parse a single line number.  Note that the empty string ""
 * is treated as a special case and converted to zero, which
 * is later treated as a "don't care" value.
 */
static inline int parse_lineno(const char *str, unsigned int *val)
{
	BUG_ON(str == NULL);
	if (*str == '\0') {
		*val = 0;
		return 0;
	}
283
	if (kstrtouint(str, 10, val) < 0) {
284 285 286 287
		pr_err("bad line-number: %s\n", str);
		return -EINVAL;
	}
	return 0;
288 289
}

290 291 292 293 294 295
static int check_set(const char **dest, char *src, char *name)
{
	int rc = 0;

	if (*dest) {
		rc = -EINVAL;
296 297
		pr_err("match-spec:%s val:%s overridden by %s\n",
		       name, *dest, src);
298 299 300 301 302
	}
	*dest = src;
	return rc;
}

303 304 305 306 307 308 309 310 311 312 313
/*
 * Parse words[] as a ddebug query specification, which is a series
 * of (keyword, value) pairs chosen from these possibilities:
 *
 * func <function-name>
 * file <full-pathname>
 * file <base-filename>
 * module <module-name>
 * format <escaped-string-to-find-in-format>
 * line <lineno>
 * line <first-lineno>-<last-lineno> // where either may be empty
314 315 316
 *
 * Only 1 of each type is allowed.
 * Returns 0 on success, <0 on error.
317 318
 */
static int ddebug_parse_query(char *words[], int nwords,
319
			struct ddebug_query *query, const char *modname)
320 321
{
	unsigned int i;
322
	int rc = 0;
323 324

	/* check we have an even number of words */
325 326
	if (nwords % 2 != 0) {
		pr_err("expecting pairs of match-spec <value>\n");
327
		return -EINVAL;
328
	}
329 330
	memset(query, 0, sizeof(*query));

331 332 333 334
	if (modname)
		/* support $modname.dyndbg=<multiple queries> */
		query->module = modname;

335 336
	for (i = 0; i < nwords; i += 2) {
		if (!strcmp(words[i], "func")) {
337
			rc = check_set(&query->function, words[i+1], "func");
338
		} else if (!strcmp(words[i], "file")) {
339
			rc = check_set(&query->filename, words[i+1], "file");
340
		} else if (!strcmp(words[i], "module")) {
341
			rc = check_set(&query->module, words[i+1], "module");
342
		} else if (!strcmp(words[i], "format")) {
343 344 345 346
			string_unescape_inplace(words[i+1], UNESCAPE_SPACE |
							    UNESCAPE_OCTAL |
							    UNESCAPE_SPECIAL);
			rc = check_set(&query->format, words[i+1], "format");
347
		} else if (!strcmp(words[i], "line")) {
348 349
			char *first = words[i+1];
			char *last = strchr(first, '-');
350
			if (query->first_lineno || query->last_lineno) {
351
				pr_err("match-spec: line used 2x\n");
352 353
				return -EINVAL;
			}
354 355
			if (last)
				*last++ = '\0';
356
			if (parse_lineno(first, &query->first_lineno) < 0)
357
				return -EINVAL;
358
			if (last) {
359
				/* range <first>-<last> */
360 361 362
				if (parse_lineno(last, &query->last_lineno) < 0)
					return -EINVAL;

363 364 365 366
				/* special case for last lineno not specified */
				if (query->last_lineno == 0)
					query->last_lineno = UINT_MAX;

367
				if (query->last_lineno < query->first_lineno) {
368 369 370
					pr_err("last-line:%d < 1st-line:%d\n",
						query->last_lineno,
						query->first_lineno);
371
					return -EINVAL;
372
				}
373 374 375 376
			} else {
				query->last_lineno = query->first_lineno;
			}
		} else {
377
			pr_err("unknown keyword \"%s\"\n", words[i]);
378 379
			return -EINVAL;
		}
380 381
		if (rc)
			return rc;
382
	}
383
	vpr_info_dq(query, "parsed");
384 385 386 387 388 389 390 391 392 393 394 395 396
	return 0;
}

/*
 * Parse `str' as a flags specification, format [-+=][p]+.
 * Sets up *maskp and *flagsp to be used when changing the
 * flags fields of matched _ddebug's.  Returns 0 on success
 * or <0 on error.
 */
static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
			       unsigned int *maskp)
{
	unsigned flags = 0;
397
	int op = '=', i;
398 399 400 401 402 403 404 405

	switch (*str) {
	case '+':
	case '-':
	case '=':
		op = *str++;
		break;
	default:
406
		pr_err("bad flag-op %c, at start of %s\n", *str, str);
407 408
		return -EINVAL;
	}
409
	vpr_info("op='%c'\n", op);
410

411
	for (; *str ; ++str) {
412 413 414 415 416
		for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) {
			if (*str == opt_array[i].opt_char) {
				flags |= opt_array[i].flag;
				break;
			}
417
		}
418 419
		if (i < 0) {
			pr_err("unknown flag '%c' in \"%s\"\n", *str, str);
420
			return -EINVAL;
421
		}
422
	}
423
	vpr_info("flags=0x%x\n", flags);
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

	/* calculate final *flagsp, *maskp according to mask and op */
	switch (op) {
	case '=':
		*maskp = 0;
		*flagsp = flags;
		break;
	case '+':
		*maskp = ~0U;
		*flagsp = flags;
		break;
	case '-':
		*maskp = ~flags;
		*flagsp = 0;
		break;
	}
440
	vpr_info("*flagsp=0x%x *maskp=0x%x\n", *flagsp, *maskp);
441 442 443
	return 0;
}

444
static int ddebug_exec_query(char *query_string, const char *modname)
445 446 447 448
{
	unsigned int flags = 0, mask = 0;
	struct ddebug_query query;
#define MAXWORDS 9
449
	int nwords, nfound;
450 451 452
	char *words[MAXWORDS];

	nwords = ddebug_tokenize(query_string, words, MAXWORDS);
453 454
	if (nwords <= 0) {
		pr_err("tokenize failed\n");
455
		return -EINVAL;
456 457 458 459
	}
	/* check flags 1st (last arg) so query is pairs of spec,val */
	if (ddebug_parse_flags(words[nwords-1], &flags, &mask)) {
		pr_err("flags parse failed\n");
460
		return -EINVAL;
461 462 463
	}
	if (ddebug_parse_query(words, nwords-1, &query, modname)) {
		pr_err("query parse failed\n");
464
		return -EINVAL;
465
	}
466
	/* actually go and implement the change */
467
	nfound = ddebug_change(&query, flags, mask);
468
	vpr_info_dq(&query, nfound ? "applied" : "no-match");
469 470 471 472 473 474 475 476

	return nfound;
}

/* handle multiple queries in query string, continue on error, return
   last error or number of matching callsites.  Module name is either
   in param (for boot arg) or perhaps in query string.
*/
477
static int ddebug_exec_queries(char *query, const char *modname)
478 479 480 481 482 483 484 485 486 487 488 489 490
{
	char *split;
	int i, errs = 0, exitcode = 0, rc, nfound = 0;

	for (i = 0; query; query = split) {
		split = strpbrk(query, ";\n");
		if (split)
			*split++ = '\0';

		query = skip_spaces(query);
		if (!query || !*query || *query == '#')
			continue;

491
		vpr_info("query %d: \"%s\"\n", i, query);
492

493
		rc = ddebug_exec_query(query, modname);
494 495 496
		if (rc < 0) {
			errs++;
			exitcode = rc;
497
		} else {
498
			nfound += rc;
499
		}
500 501
		i++;
	}
502
	vpr_info("processed %d queries, with %d matches, %d errs\n",
503 504 505 506 507
		 i, nfound, errs);

	if (exitcode)
		return exitcode;
	return nfound;
508 509
}

510 511 512 513 514 515 516 517 518 519
#define PREFIX_SIZE 64

static int remaining(int wrote)
{
	if (PREFIX_SIZE - wrote > 0)
		return PREFIX_SIZE - wrote;
	return 0;
}

static char *dynamic_emit_prefix(const struct _ddebug *desc, char *buf)
520
{
521 522
	int pos_after_tid;
	int pos = 0;
523

524 525
	*buf = '\0';

526
	if (desc->flags & _DPRINTK_FLAGS_INCL_TID) {
527
		if (in_interrupt())
528
			pos += snprintf(buf + pos, remaining(pos), "<intr> ");
529
		else
530
			pos += snprintf(buf + pos, remaining(pos), "[%d] ",
531
					task_pid_vnr(current));
532
	}
533 534 535
	pos_after_tid = pos;
	if (desc->flags & _DPRINTK_FLAGS_INCL_MODNAME)
		pos += snprintf(buf + pos, remaining(pos), "%s:",
536
				desc->modname);
537 538
	if (desc->flags & _DPRINTK_FLAGS_INCL_FUNCNAME)
		pos += snprintf(buf + pos, remaining(pos), "%s:",
539
				desc->function);
540
	if (desc->flags & _DPRINTK_FLAGS_INCL_LINENO)
541
		pos += snprintf(buf + pos, remaining(pos), "%d:",
542
				desc->lineno);
543 544 545 546
	if (pos - pos_after_tid)
		pos += snprintf(buf + pos, remaining(pos), " ");
	if (pos >= PREFIX_SIZE)
		buf[PREFIX_SIZE - 1] = '\0';
547

548
	return buf;
549 550
}

551
void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
552 553
{
	va_list args;
554 555
	struct va_format vaf;
	char buf[PREFIX_SIZE];
556 557 558 559 560

	BUG_ON(!descriptor);
	BUG_ON(!fmt);

	va_start(args, fmt);
561

562 563
	vaf.fmt = fmt;
	vaf.va = &args;
564

565
	printk(KERN_DEBUG "%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf);
566

567 568 569 570
	va_end(args);
}
EXPORT_SYMBOL(__dynamic_pr_debug);

571
void __dynamic_dev_dbg(struct _ddebug *descriptor,
572 573 574 575 576 577 578 579 580
		      const struct device *dev, const char *fmt, ...)
{
	struct va_format vaf;
	va_list args;

	BUG_ON(!descriptor);
	BUG_ON(!fmt);

	va_start(args, fmt);
581

582 583
	vaf.fmt = fmt;
	vaf.va = &args;
584 585

	if (!dev) {
586
		printk(KERN_DEBUG "(NULL device *): %pV", &vaf);
587 588 589
	} else {
		char buf[PREFIX_SIZE];

590
		dev_printk_emit(LOGLEVEL_DEBUG, dev, "%s%s %s: %pV",
591 592 593
				dynamic_emit_prefix(descriptor, buf),
				dev_driver_string(dev), dev_name(dev),
				&vaf);
594 595
	}

596 597 598 599
	va_end(args);
}
EXPORT_SYMBOL(__dynamic_dev_dbg);

600 601
#ifdef CONFIG_NET

602 603
void __dynamic_netdev_dbg(struct _ddebug *descriptor,
			  const struct net_device *dev, const char *fmt, ...)
604 605 606 607 608 609 610 611
{
	struct va_format vaf;
	va_list args;

	BUG_ON(!descriptor);
	BUG_ON(!fmt);

	va_start(args, fmt);
612

613 614
	vaf.fmt = fmt;
	vaf.va = &args;
615 616 617

	if (dev && dev->dev.parent) {
		char buf[PREFIX_SIZE];
618

619
		dev_printk_emit(LOGLEVEL_DEBUG, dev->dev.parent,
620 621 622 623 624 625
				"%s%s %s %s%s: %pV",
				dynamic_emit_prefix(descriptor, buf),
				dev_driver_string(dev->dev.parent),
				dev_name(dev->dev.parent),
				netdev_name(dev), netdev_reg_state(dev),
				&vaf);
626
	} else if (dev) {
627 628
		printk(KERN_DEBUG "%s%s: %pV", netdev_name(dev),
		       netdev_reg_state(dev), &vaf);
629
	} else {
630
		printk(KERN_DEBUG "(NULL net_device): %pV", &vaf);
631 632
	}

633 634 635 636
	va_end(args);
}
EXPORT_SYMBOL(__dynamic_netdev_dbg);

637 638
#endif

639 640 641
#define DDEBUG_STRING_SIZE 1024
static __initdata char ddebug_setup_string[DDEBUG_STRING_SIZE];

642 643
static __init int ddebug_setup_query(char *str)
{
644
	if (strlen(str) >= DDEBUG_STRING_SIZE) {
645
		pr_warn("ddebug boot param string too large\n");
646 647
		return 0;
	}
648
	strlcpy(ddebug_setup_string, str, DDEBUG_STRING_SIZE);
649 650 651 652 653
	return 1;
}

__setup("ddebug_query=", ddebug_setup_query);

654
/*
655
 * File_ops->write method for <debugfs>/dynamic_debug/control.  Gathers the
656 657
 * command text from userspace, parses and executes it.
 */
658
#define USER_BUF_PAGE 4096
659 660 661
static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
				  size_t len, loff_t *offp)
{
662
	char *tmpbuf;
663
	int ret;
664 665 666

	if (len == 0)
		return 0;
667 668
	if (len > USER_BUF_PAGE - 1) {
		pr_warn("expected <%d bytes into control\n", USER_BUF_PAGE);
669
		return -E2BIG;
670
	}
671 672 673
	tmpbuf = memdup_user_nul(ubuf, len);
	if (IS_ERR(tmpbuf))
		return PTR_ERR(tmpbuf);
674
	vpr_info("read %d bytes from userspace\n", (int)len);
675

676
	ret = ddebug_exec_queries(tmpbuf, NULL);
677
	kfree(tmpbuf);
678
	if (ret < 0)
679
		return ret;
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736

	*offp += len;
	return len;
}

/*
 * Set the iterator to point to the first _ddebug object
 * and return a pointer to that first object.  Returns
 * NULL if there are no _ddebugs at all.
 */
static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
{
	if (list_empty(&ddebug_tables)) {
		iter->table = NULL;
		iter->idx = 0;
		return NULL;
	}
	iter->table = list_entry(ddebug_tables.next,
				 struct ddebug_table, link);
	iter->idx = 0;
	return &iter->table->ddebugs[iter->idx];
}

/*
 * Advance the iterator to point to the next _ddebug
 * object from the one the iterator currently points at,
 * and returns a pointer to the new _ddebug.  Returns
 * NULL if the iterator has seen all the _ddebugs.
 */
static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
{
	if (iter->table == NULL)
		return NULL;
	if (++iter->idx == iter->table->num_ddebugs) {
		/* iterate to next table */
		iter->idx = 0;
		if (list_is_last(&iter->table->link, &ddebug_tables)) {
			iter->table = NULL;
			return NULL;
		}
		iter->table = list_entry(iter->table->link.next,
					 struct ddebug_table, link);
	}
	return &iter->table->ddebugs[iter->idx];
}

/*
 * Seq_ops start method.  Called at the start of every
 * read() call from userspace.  Takes the ddebug_lock and
 * seeks the seq_file's iterator to the given position.
 */
static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
{
	struct ddebug_iter *iter = m->private;
	struct _ddebug *dp;
	int n = *pos;

737
	vpr_info("called m=%p *pos=%lld\n", m, (unsigned long long)*pos);
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760

	mutex_lock(&ddebug_lock);

	if (!n)
		return SEQ_START_TOKEN;
	if (n < 0)
		return NULL;
	dp = ddebug_iter_first(iter);
	while (dp != NULL && --n > 0)
		dp = ddebug_iter_next(iter);
	return dp;
}

/*
 * Seq_ops next method.  Called several times within a read()
 * call from userspace, with ddebug_lock held.  Walks to the
 * next _ddebug object with a special case for the header line.
 */
static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
{
	struct ddebug_iter *iter = m->private;
	struct _ddebug *dp;

761
	vpr_info("called m=%p p=%p *pos=%lld\n",
762
		 m, p, (unsigned long long)*pos);
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781

	if (p == SEQ_START_TOKEN)
		dp = ddebug_iter_first(iter);
	else
		dp = ddebug_iter_next(iter);
	++*pos;
	return dp;
}

/*
 * Seq_ops show method.  Called several times within a read()
 * call from userspace, with ddebug_lock held.  Formats the
 * current _ddebug as a single human-readable line, with a
 * special case for the header line.
 */
static int ddebug_proc_show(struct seq_file *m, void *p)
{
	struct ddebug_iter *iter = m->private;
	struct _ddebug *dp = p;
782
	char flagsbuf[10];
783

784
	vpr_info("called m=%p p=%p\n", m, p);
785 786 787

	if (p == SEQ_START_TOKEN) {
		seq_puts(m,
788
			 "# filename:lineno [module]function flags format\n");
789 790 791
		return 0;
	}

792
	seq_printf(m, "%s:%u [%s]%s =%s \"",
793 794 795
		   trim_prefix(dp->filename), dp->lineno,
		   iter->table->mod_name, dp->function,
		   ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
796 797 798 799 800 801 802 803 804 805 806 807
	seq_escape(m, dp->format, "\t\r\n\"");
	seq_puts(m, "\"\n");

	return 0;
}

/*
 * Seq_ops stop method.  Called at the end of each read()
 * call from userspace.  Drops ddebug_lock.
 */
static void ddebug_proc_stop(struct seq_file *m, void *p)
{
808
	vpr_info("called m=%p p=%p\n", m, p);
809 810 811 812 813 814 815 816 817 818 819
	mutex_unlock(&ddebug_lock);
}

static const struct seq_operations ddebug_proc_seqops = {
	.start = ddebug_proc_start,
	.next = ddebug_proc_next,
	.show = ddebug_proc_show,
	.stop = ddebug_proc_stop
};

/*
820 821 822 823 824
 * File_ops->open method for <debugfs>/dynamic_debug/control.  Does
 * the seq_file setup dance, and also creates an iterator to walk the
 * _ddebugs.  Note that we create a seq_file always, even for O_WRONLY
 * files where it's not needed, as doing so simplifies the ->release
 * method.
825 826 827
 */
static int ddebug_proc_open(struct inode *inode, struct file *file)
{
828
	vpr_info("called\n");
829 830
	return seq_open_private(file, &ddebug_proc_seqops,
				sizeof(struct ddebug_iter));
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
}

static const struct file_operations ddebug_proc_fops = {
	.owner = THIS_MODULE,
	.open = ddebug_proc_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = seq_release_private,
	.write = ddebug_proc_write
};

/*
 * Allocate a new ddebug_table for the given module
 * and add it to the global list.
 */
int ddebug_add_module(struct _ddebug *tab, unsigned int n,
			     const char *name)
{
	struct ddebug_table *dt;
850
	const char *new_name;
851 852 853 854

	dt = kzalloc(sizeof(*dt), GFP_KERNEL);
	if (dt == NULL)
		return -ENOMEM;
855
	new_name = kstrdup_const(name, GFP_KERNEL);
856 857 858 859 860 861 862 863 864 865 866 867
	if (new_name == NULL) {
		kfree(dt);
		return -ENOMEM;
	}
	dt->mod_name = new_name;
	dt->num_ddebugs = n;
	dt->ddebugs = tab;

	mutex_lock(&ddebug_lock);
	list_add_tail(&dt->link, &ddebug_tables);
	mutex_unlock(&ddebug_lock);

868
	vpr_info("%u debug prints in module %s\n", n, dt->mod_name);
869 870 871 872
	return 0;
}
EXPORT_SYMBOL_GPL(ddebug_add_module);

873 874 875
/* helper for ddebug_dyndbg_(boot|module)_param_cb */
static int ddebug_dyndbg_param_cb(char *param, char *val,
				const char *modname, int on_err)
876 877 878 879 880
{
	char *sep;

	sep = strchr(param, '.');
	if (sep) {
881
		/* needed only for ddebug_dyndbg_boot_param_cb */
882 883 884 885 886
		*sep = '\0';
		modname = param;
		param = sep + 1;
	}
	if (strcmp(param, "dyndbg"))
887
		return on_err; /* determined by caller */
888

889 890
	ddebug_exec_queries((val ? val : "+p"), modname);

891 892 893
	return 0; /* query failure shouldnt stop module load */
}

894 895
/* handle both dyndbg and $module.dyndbg params at boot */
static int ddebug_dyndbg_boot_param_cb(char *param, char *val,
896
				const char *unused, void *arg)
897
{
898 899 900
	vpr_info("%s=\"%s\"\n", param, val);
	return ddebug_dyndbg_param_cb(param, val, NULL, 0);
}
901

902 903 904 905 906 907 908 909 910
/*
 * modprobe foo finds foo.params in boot-args, strips "foo.", and
 * passes them to load_module().  This callback gets unknown params,
 * processes dyndbg params, rejects others.
 */
int ddebug_dyndbg_module_param_cb(char *param, char *val, const char *module)
{
	vpr_info("module: %s %s=\"%s\"\n", module, param, val);
	return ddebug_dyndbg_param_cb(param, val, module, -ENOENT);
911 912
}

913 914 915
static void ddebug_table_free(struct ddebug_table *dt)
{
	list_del_init(&dt->link);
916
	kfree_const(dt->mod_name);
917 918 919 920 921 922 923
	kfree(dt);
}

/*
 * Called in response to a module being unloaded.  Removes
 * any ddebug_table's which point at the module.
 */
924
int ddebug_remove_module(const char *mod_name)
925 926 927 928
{
	struct ddebug_table *dt, *nextdt;
	int ret = -ENOENT;

929
	vpr_info("removing module \"%s\"\n", mod_name);
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954

	mutex_lock(&ddebug_lock);
	list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
		if (!strcmp(dt->mod_name, mod_name)) {
			ddebug_table_free(dt);
			ret = 0;
		}
	}
	mutex_unlock(&ddebug_lock);
	return ret;
}
EXPORT_SYMBOL_GPL(ddebug_remove_module);

static void ddebug_remove_all_tables(void)
{
	mutex_lock(&ddebug_lock);
	while (!list_empty(&ddebug_tables)) {
		struct ddebug_table *dt = list_entry(ddebug_tables.next,
						      struct ddebug_table,
						      link);
		ddebug_table_free(dt);
	}
	mutex_unlock(&ddebug_lock);
}

955 956 957
static __initdata int ddebug_init_success;

static int __init dynamic_debug_init_debugfs(void)
958 959
{
	struct dentry *dir, *file;
960 961 962

	if (!ddebug_init_success)
		return -ENODEV;
963 964 965 966 967 968 969 970 971 972

	dir = debugfs_create_dir("dynamic_debug", NULL);
	if (!dir)
		return -ENOMEM;
	file = debugfs_create_file("control", 0644, dir, NULL,
					&ddebug_proc_fops);
	if (!file) {
		debugfs_remove(dir);
		return -ENOMEM;
	}
973 974 975 976 977 978 979
	return 0;
}

static int __init dynamic_debug_init(void)
{
	struct _ddebug *iter, *iter_start;
	const char *modname = NULL;
980
	char *cmdline;
981
	int ret = 0;
982 983
	int n = 0, entries = 0, modct = 0;
	int verbose_bytes = 0;
984

985
	if (__start___verbose == __stop___verbose) {
986
		pr_warn("_ddebug table is empty in a CONFIG_DYNAMIC_DEBUG build\n");
987 988 989 990 991 992
		return 1;
	}
	iter = __start___verbose;
	modname = iter->modname;
	iter_start = iter;
	for (; iter < __stop___verbose; iter++) {
993 994 995 996
		entries++;
		verbose_bytes += strlen(iter->modname) + strlen(iter->function)
			+ strlen(iter->filename) + strlen(iter->format);

997
		if (strcmp(modname, iter->modname)) {
998
			modct++;
999 1000
			ret = ddebug_add_module(iter_start, n, modname);
			if (ret)
1001
				goto out_err;
1002 1003 1004
			n = 0;
			modname = iter->modname;
			iter_start = iter;
1005
		}
1006
		n++;
1007
	}
1008 1009
	ret = ddebug_add_module(iter_start, n, modname);
	if (ret)
1010
		goto out_err;
1011

1012
	ddebug_init_success = 1;
1013 1014 1015
	vpr_info("%d modules, %d entries and %d bytes in ddebug tables, %d bytes in (readonly) verbose section\n",
		 modct, entries, (int)(modct * sizeof(struct ddebug_table)),
		 verbose_bytes + (int)(__stop___verbose - __start___verbose));
1016 1017

	/* apply ddebug_query boot param, dont unload tables on err */
1018
	if (ddebug_setup_string[0] != '\0') {
1019
		pr_warn("ddebug_query param name is deprecated, change it to dyndbg\n");
1020
		ret = ddebug_exec_queries(ddebug_setup_string, NULL);
1021
		if (ret < 0)
1022
			pr_warn("Invalid ddebug boot param %s\n",
1023
				ddebug_setup_string);
1024
		else
1025
			pr_info("%d changes by ddebug_query\n", ret);
1026
	}
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
	/* now that ddebug tables are loaded, process all boot args
	 * again to find and activate queries given in dyndbg params.
	 * While this has already been done for known boot params, it
	 * ignored the unknown ones (dyndbg in particular).  Reusing
	 * parse_args avoids ad-hoc parsing.  This will also attempt
	 * to activate queries for not-yet-loaded modules, which is
	 * slightly noisy if verbose, but harmless.
	 */
	cmdline = kstrdup(saved_command_line, GFP_KERNEL);
	parse_args("dyndbg params", cmdline, NULL,
1037
		   0, 0, 0, NULL, &ddebug_dyndbg_boot_param_cb);
1038
	kfree(cmdline);
1039
	return 0;
1040

1041 1042
out_err:
	ddebug_remove_all_tables();
1043 1044
	return 0;
}
1045
/* Allow early initialization for boot messages via boot param */
1046
early_initcall(dynamic_debug_init);
1047

1048
/* Debugfs setup must be done later */
1049
fs_initcall(dynamic_debug_init_debugfs);