Blame view

lib/dynamic_debug.c 20.3 KB
e9d376f0f   Jason Baron   dynamic debug: co...
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.
8ba6ebf58   Bart Van Assche   Dynamic debug: Ad...
10
   * Copyright (C) 2011 Bart Van Assche.  All Rights Reserved.
e9d376f0f   Jason Baron   dynamic debug: co...
11
   */
4ad275e5c   Joe Perches   dynamic_debug: Co...
12
  #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
e9d376f0f   Jason Baron   dynamic debug: co...
13
14
15
16
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/moduleparam.h>
  #include <linux/kallsyms.h>
e9d376f0f   Jason Baron   dynamic debug: co...
17
18
19
20
21
22
23
  #include <linux/types.h>
  #include <linux/mutex.h>
  #include <linux/proc_fs.h>
  #include <linux/seq_file.h>
  #include <linux/list.h>
  #include <linux/sysctl.h>
  #include <linux/ctype.h>
e7d2860b6   AndrĂ© Goddard Rosa   tree-wide: conver...
24
  #include <linux/string.h>
e9d376f0f   Jason Baron   dynamic debug: co...
25
26
27
  #include <linux/uaccess.h>
  #include <linux/dynamic_debug.h>
  #include <linux/debugfs.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
28
  #include <linux/slab.h>
52159d98b   Jason Baron   jump label: Conve...
29
  #include <linux/jump_label.h>
8ba6ebf58   Bart Van Assche   Dynamic debug: Ad...
30
  #include <linux/hardirq.h>
e8d9792aa   Greg Kroah-Hartman   dynamic_debug: ad...
31
  #include <linux/sched.h>
cbc466355   Joe Perches   dynamic_debug: Ad...
32
  #include <linux/device.h>
ffa10cb47   Jason Baron   dynamic_debug: ma...
33
  #include <linux/netdevice.h>
e9d376f0f   Jason Baron   dynamic debug: co...
34
35
36
  
  extern struct _ddebug __start___verbose[];
  extern struct _ddebug __stop___verbose[];
e9d376f0f   Jason Baron   dynamic debug: co...
37
38
39
40
  struct ddebug_table {
  	struct list_head link;
  	char *mod_name;
  	unsigned int num_ddebugs;
e9d376f0f   Jason Baron   dynamic debug: co...
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  	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);
  static int verbose = 0;
  
  /* Return the last part of a pathname */
  static inline const char *basename(const char *path)
  {
  	const char *tail = strrchr(path, '/');
  	return tail ? tail+1 : path;
  }
8ba6ebf58   Bart Van Assche   Dynamic debug: Ad...
67
68
69
70
71
72
73
  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' },
  };
e9d376f0f   Jason Baron   dynamic debug: co...
74
75
76
77
78
  /* 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;
8ba6ebf58   Bart Van Assche   Dynamic debug: Ad...
79
  	int i;
e9d376f0f   Jason Baron   dynamic debug: co...
80
81
  
  	BUG_ON(maxlen < 4);
8ba6ebf58   Bart Van Assche   Dynamic debug: Ad...
82
83
84
  	for (i = 0; i < ARRAY_SIZE(opt_array); ++i)
  		if (dp->flags & opt_array[i].flag)
  			*p++ = opt_array[i].opt_char;
e9d376f0f   Jason Baron   dynamic debug: co...
85
86
87
88
89
90
91
92
  	if (p == buf)
  		*p++ = '-';
  	*p = '\0';
  
  	return buf;
  }
  
  /*
e9d376f0f   Jason Baron   dynamic debug: co...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
   * Search the tables for _ddebug's which match the given
   * `query' and apply the `flags' and `mask' to them.  Tells
   * the user which ddebug's were changed, or whether none
   * were matched.
   */
  static void ddebug_change(const struct ddebug_query *query,
  			   unsigned int flags, unsigned int mask)
  {
  	int i;
  	struct ddebug_table *dt;
  	unsigned int newflags;
  	unsigned int nfound = 0;
  	char flagbuf[8];
  
  	/* search for matching ddebugs */
  	mutex_lock(&ddebug_lock);
  	list_for_each_entry(dt, &ddebug_tables, link) {
  
  		/* match against the module name */
  		if (query->module != NULL &&
  		    strcmp(query->module, dt->mod_name))
  			continue;
  
  		for (i = 0 ; i < dt->num_ddebugs ; i++) {
  			struct _ddebug *dp = &dt->ddebugs[i];
  
  			/* match against the source filename */
  			if (query->filename != NULL &&
  			    strcmp(query->filename, dp->filename) &&
  			    strcmp(query->filename, basename(dp->filename)))
  				continue;
  
  			/* match against the function */
  			if (query->function != NULL &&
  			    strcmp(query->function, dp->function))
  				continue;
  
  			/* match against the format */
  			if (query->format != NULL &&
  			    strstr(dp->format, query->format) == NULL)
  				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;
e9d376f0f   Jason Baron   dynamic debug: co...
148
  			dp->flags = newflags;
2d75af2f2   Jason Baron   dynamic debug: Fi...
149
150
151
152
  			if (newflags)
  				dp->enabled = 1;
  			else
  				dp->enabled = 0;
e9d376f0f   Jason Baron   dynamic debug: co...
153
  			if (verbose)
4ad275e5c   Joe Perches   dynamic_debug: Co...
154
155
  				pr_info("changed %s:%d [%s]%s %s
  ",
e9d376f0f   Jason Baron   dynamic debug: co...
156
157
158
159
160
161
162
163
164
  					dp->filename, dp->lineno,
  					dt->mod_name, dp->function,
  					ddebug_describe_flags(dp, flagbuf,
  							sizeof(flagbuf)));
  		}
  	}
  	mutex_unlock(&ddebug_lock);
  
  	if (!nfound && verbose)
4ad275e5c   Joe Perches   dynamic_debug: Co...
165
166
  		pr_info("no matches for query
  ");
e9d376f0f   Jason Baron   dynamic debug: co...
167
168
169
  }
  
  /*
e9d376f0f   Jason Baron   dynamic debug: co...
170
   * Split the buffer `buf' into space-separated words.
9898abb3d   Greg Banks   Dynamic debug: al...
171
172
173
   * Handles simple " and ' quoting, i.e. without nested,
   * embedded or escaped \".  Return the number of words
   * or <0 on error.
e9d376f0f   Jason Baron   dynamic debug: co...
174
175
176
177
   */
  static int ddebug_tokenize(char *buf, char *words[], int maxwords)
  {
  	int nwords = 0;
9898abb3d   Greg Banks   Dynamic debug: al...
178
179
180
181
  	while (*buf) {
  		char *end;
  
  		/* Skip leading whitespace */
e7d2860b6   AndrĂ© Goddard Rosa   tree-wide: conver...
182
  		buf = skip_spaces(buf);
9898abb3d   Greg Banks   Dynamic debug: al...
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  		if (!*buf)
  			break;	/* oh, it was trailing whitespace */
  
  		/* Run `end' over a word, either whitespace separated or quoted */
  		if (*buf == '"' || *buf == '\'') {
  			int quote = *buf++;
  			for (end = buf ; *end && *end != quote ; end++)
  				;
  			if (!*end)
  				return -EINVAL;	/* unclosed quote */
  		} else {
  			for (end = buf ; *end && !isspace(*end) ; end++)
  				;
  			BUG_ON(end == buf);
  		}
  		/* Here `buf' is the start of the word, `end' is one past the end */
  
  		if (nwords == maxwords)
  			return -EINVAL;	/* ran out of words[] before bytes */
  		if (*end)
  			*end++ = '\0';	/* terminate the word */
  		words[nwords++] = buf;
  		buf = end;
  	}
e9d376f0f   Jason Baron   dynamic debug: co...
207
208
209
  
  	if (verbose) {
  		int i;
4ad275e5c   Joe Perches   dynamic_debug: Co...
210
  		pr_info("split into words:");
e9d376f0f   Jason Baron   dynamic debug: co...
211
  		for (i = 0 ; i < nwords ; i++)
4ad275e5c   Joe Perches   dynamic_debug: Co...
212
213
214
  			pr_cont(" \"%s\"", words[i]);
  		pr_cont("
  ");
e9d376f0f   Jason Baron   dynamic debug: co...
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
  	}
  
  	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)
  {
  	char *end = NULL;
  	BUG_ON(str == NULL);
  	if (*str == '\0') {
  		*val = 0;
  		return 0;
  	}
  	*val = simple_strtoul(str, &end, 10);
  	return end == NULL || end == str || *end != '\0' ? -EINVAL : 0;
  }
  
  /*
   * Undo octal escaping in a string, inplace.  This is useful to
   * allow the user to express a query which matches a format
   * containing embedded spaces.
   */
  #define isodigit(c)		((c) >= '0' && (c) <= '7')
  static char *unescape(char *str)
  {
  	char *in = str;
  	char *out = str;
  
  	while (*in) {
  		if (*in == '\\') {
  			if (in[1] == '\\') {
  				*out++ = '\\';
  				in += 2;
  				continue;
  			} else if (in[1] == 't') {
  				*out++ = '\t';
  				in += 2;
  				continue;
  			} else if (in[1] == 'n') {
  				*out++ = '
  ';
  				in += 2;
  				continue;
  			} else if (isodigit(in[1]) &&
  			         isodigit(in[2]) &&
  			         isodigit(in[3])) {
  				*out++ = ((in[1] - '0')<<6) |
  				          ((in[2] - '0')<<3) |
  				          (in[3] - '0');
  				in += 4;
  				continue;
  			}
  		}
  		*out++ = *in++;
  	}
  	*out = '\0';
  
  	return str;
  }
  
  /*
   * 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
   */
  static int ddebug_parse_query(char *words[], int nwords,
  			       struct ddebug_query *query)
  {
  	unsigned int i;
  
  	/* check we have an even number of words */
  	if (nwords % 2 != 0)
  		return -EINVAL;
  	memset(query, 0, sizeof(*query));
  
  	for (i = 0 ; i < nwords ; i += 2) {
  		if (!strcmp(words[i], "func"))
  			query->function = words[i+1];
  		else if (!strcmp(words[i], "file"))
  			query->filename = words[i+1];
  		else if (!strcmp(words[i], "module"))
  			query->module = words[i+1];
  		else if (!strcmp(words[i], "format"))
  			query->format = unescape(words[i+1]);
  		else if (!strcmp(words[i], "line")) {
  			char *first = words[i+1];
  			char *last = strchr(first, '-');
  			if (last)
  				*last++ = '\0';
  			if (parse_lineno(first, &query->first_lineno) < 0)
  				return -EINVAL;
  			if (last != NULL) {
  				/* range <first>-<last> */
  				if (parse_lineno(last, &query->last_lineno) < 0)
  					return -EINVAL;
  			} else {
  				query->last_lineno = query->first_lineno;
  			}
  		} else {
  			if (verbose)
4ad275e5c   Joe Perches   dynamic_debug: Co...
327
328
  				pr_err("unknown keyword \"%s\"
  ", words[i]);
e9d376f0f   Jason Baron   dynamic debug: co...
329
330
331
332
333
  			return -EINVAL;
  		}
  	}
  
  	if (verbose)
4ad275e5c   Joe Perches   dynamic_debug: Co...
334
335
336
337
  		pr_info("q->function=\"%s\" q->filename=\"%s\" "
  			"q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u
  ",
  			query->function, query->filename,
e9d376f0f   Jason Baron   dynamic debug: co...
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
  			query->module, query->format, query->first_lineno,
  			query->last_lineno);
  
  	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;
8ba6ebf58   Bart Van Assche   Dynamic debug: Ad...
354
  	int op = '=', i;
e9d376f0f   Jason Baron   dynamic debug: co...
355
356
357
358
359
360
361
362
363
364
365
  
  	switch (*str) {
  	case '+':
  	case '-':
  	case '=':
  		op = *str++;
  		break;
  	default:
  		return -EINVAL;
  	}
  	if (verbose)
4ad275e5c   Joe Perches   dynamic_debug: Co...
366
367
  		pr_info("op='%c'
  ", op);
e9d376f0f   Jason Baron   dynamic debug: co...
368
369
  
  	for ( ; *str ; ++str) {
8ba6ebf58   Bart Van Assche   Dynamic debug: Ad...
370
371
372
373
374
  		for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) {
  			if (*str == opt_array[i].opt_char) {
  				flags |= opt_array[i].flag;
  				break;
  			}
e9d376f0f   Jason Baron   dynamic debug: co...
375
  		}
8ba6ebf58   Bart Van Assche   Dynamic debug: Ad...
376
377
  		if (i < 0)
  			return -EINVAL;
e9d376f0f   Jason Baron   dynamic debug: co...
378
379
380
381
  	}
  	if (flags == 0)
  		return -EINVAL;
  	if (verbose)
4ad275e5c   Joe Perches   dynamic_debug: Co...
382
383
  		pr_info("flags=0x%x
  ", flags);
e9d376f0f   Jason Baron   dynamic debug: co...
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
  
  	/* 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;
  	}
  	if (verbose)
4ad275e5c   Joe Perches   dynamic_debug: Co...
401
402
  		pr_info("*flagsp=0x%x *maskp=0x%x
  ", *flagsp, *maskp);
e9d376f0f   Jason Baron   dynamic debug: co...
403
404
  	return 0;
  }
fd89cfb87   Thomas Renninger   Dynamic Debug: Sp...
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
  static int ddebug_exec_query(char *query_string)
  {
  	unsigned int flags = 0, mask = 0;
  	struct ddebug_query query;
  #define MAXWORDS 9
  	int nwords;
  	char *words[MAXWORDS];
  
  	nwords = ddebug_tokenize(query_string, words, MAXWORDS);
  	if (nwords <= 0)
  		return -EINVAL;
  	if (ddebug_parse_query(words, nwords-1, &query))
  		return -EINVAL;
  	if (ddebug_parse_flags(words[nwords-1], &flags, &mask))
  		return -EINVAL;
  
  	/* actually go and implement the change */
  	ddebug_change(&query, flags, mask);
  	return 0;
  }
431625dac   Jason Baron   dynamic_debug: us...
425
426
427
428
429
430
431
432
433
434
  #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)
8ba6ebf58   Bart Van Assche   Dynamic debug: Ad...
435
  {
431625dac   Jason Baron   dynamic_debug: us...
436
437
  	int pos_after_tid;
  	int pos = 0;
8ba6ebf58   Bart Van Assche   Dynamic debug: Ad...
438

431625dac   Jason Baron   dynamic_debug: us...
439
440
  	pos += snprintf(buf + pos, remaining(pos), "%s", KERN_DEBUG);
  	if (desc->flags & _DPRINTK_FLAGS_INCL_TID) {
8ba6ebf58   Bart Van Assche   Dynamic debug: Ad...
441
  		if (in_interrupt())
431625dac   Jason Baron   dynamic_debug: us...
442
443
  			pos += snprintf(buf + pos, remaining(pos), "%s ",
  						"<intr>");
8ba6ebf58   Bart Van Assche   Dynamic debug: Ad...
444
  		else
431625dac   Jason Baron   dynamic_debug: us...
445
446
  			pos += snprintf(buf + pos, remaining(pos), "[%d] ",
  						task_pid_vnr(current));
8ba6ebf58   Bart Van Assche   Dynamic debug: Ad...
447
  	}
431625dac   Jason Baron   dynamic_debug: us...
448
449
450
451
452
453
454
455
456
457
458
459
460
  	pos_after_tid = pos;
  	if (desc->flags & _DPRINTK_FLAGS_INCL_MODNAME)
  		pos += snprintf(buf + pos, remaining(pos), "%s:",
  					desc->modname);
  	if (desc->flags & _DPRINTK_FLAGS_INCL_FUNCNAME)
  		pos += snprintf(buf + pos, remaining(pos), "%s:",
  					desc->function);
  	if (desc->flags & _DPRINTK_FLAGS_INCL_LINENO)
  		pos += snprintf(buf + pos, remaining(pos), "%d:", desc->lineno);
  	if (pos - pos_after_tid)
  		pos += snprintf(buf + pos, remaining(pos), " ");
  	if (pos >= PREFIX_SIZE)
  		buf[PREFIX_SIZE - 1] = '\0';
6c2140ee0   Joe Perches   dynamic_debug: Co...
461

431625dac   Jason Baron   dynamic_debug: us...
462
  	return buf;
6c2140ee0   Joe Perches   dynamic_debug: Co...
463
  }
8ba6ebf58   Bart Van Assche   Dynamic debug: Ad...
464
465
466
467
  int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
  {
  	va_list args;
  	int res;
431625dac   Jason Baron   dynamic_debug: us...
468
469
  	struct va_format vaf;
  	char buf[PREFIX_SIZE];
8ba6ebf58   Bart Van Assche   Dynamic debug: Ad...
470
471
472
473
474
  
  	BUG_ON(!descriptor);
  	BUG_ON(!fmt);
  
  	va_start(args, fmt);
431625dac   Jason Baron   dynamic_debug: us...
475
476
477
  	vaf.fmt = fmt;
  	vaf.va = &args;
  	res = printk("%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf);
8ba6ebf58   Bart Van Assche   Dynamic debug: Ad...
478
479
480
481
482
  	va_end(args);
  
  	return res;
  }
  EXPORT_SYMBOL(__dynamic_pr_debug);
cbc466355   Joe Perches   dynamic_debug: Ad...
483
484
485
486
487
488
  int __dynamic_dev_dbg(struct _ddebug *descriptor,
  		      const struct device *dev, const char *fmt, ...)
  {
  	struct va_format vaf;
  	va_list args;
  	int res;
431625dac   Jason Baron   dynamic_debug: us...
489
  	char buf[PREFIX_SIZE];
cbc466355   Joe Perches   dynamic_debug: Ad...
490
491
492
493
494
  
  	BUG_ON(!descriptor);
  	BUG_ON(!fmt);
  
  	va_start(args, fmt);
cbc466355   Joe Perches   dynamic_debug: Ad...
495
496
  	vaf.fmt = fmt;
  	vaf.va = &args;
431625dac   Jason Baron   dynamic_debug: us...
497
  	res = __dev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf);
cbc466355   Joe Perches   dynamic_debug: Ad...
498
499
500
501
502
  	va_end(args);
  
  	return res;
  }
  EXPORT_SYMBOL(__dynamic_dev_dbg);
0feefd978   Jason Baron   dynamic_debug: fi...
503
  #ifdef CONFIG_NET
ffa10cb47   Jason Baron   dynamic_debug: ma...
504
505
506
507
508
509
  int __dynamic_netdev_dbg(struct _ddebug *descriptor,
  		      const struct net_device *dev, const char *fmt, ...)
  {
  	struct va_format vaf;
  	va_list args;
  	int res;
431625dac   Jason Baron   dynamic_debug: us...
510
  	char buf[PREFIX_SIZE];
ffa10cb47   Jason Baron   dynamic_debug: ma...
511
512
513
514
515
  
  	BUG_ON(!descriptor);
  	BUG_ON(!fmt);
  
  	va_start(args, fmt);
ffa10cb47   Jason Baron   dynamic_debug: ma...
516
517
  	vaf.fmt = fmt;
  	vaf.va = &args;
431625dac   Jason Baron   dynamic_debug: us...
518
  	res = __netdev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf);
ffa10cb47   Jason Baron   dynamic_debug: ma...
519
520
521
522
523
  	va_end(args);
  
  	return res;
  }
  EXPORT_SYMBOL(__dynamic_netdev_dbg);
0feefd978   Jason Baron   dynamic_debug: fi...
524
  #endif
a648ec05b   Thomas Renninger   Dynamic Debug: In...
525
526
527
528
  static __initdata char ddebug_setup_string[1024];
  static __init int ddebug_setup_query(char *str)
  {
  	if (strlen(str) >= 1024) {
4ad275e5c   Joe Perches   dynamic_debug: Co...
529
530
  		pr_warn("ddebug boot param string too large
  ");
a648ec05b   Thomas Renninger   Dynamic Debug: In...
531
532
533
534
535
536
537
  		return 0;
  	}
  	strcpy(ddebug_setup_string, str);
  	return 1;
  }
  
  __setup("ddebug_query=", ddebug_setup_query);
e9d376f0f   Jason Baron   dynamic debug: co...
538
539
540
541
542
543
544
  /*
   * File_ops->write method for <debugfs>/dynamic_debug/conrol.  Gathers the
   * command text from userspace, parses and executes it.
   */
  static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
  				  size_t len, loff_t *offp)
  {
e9d376f0f   Jason Baron   dynamic debug: co...
545
  	char tmpbuf[256];
fd89cfb87   Thomas Renninger   Dynamic Debug: Sp...
546
  	int ret;
e9d376f0f   Jason Baron   dynamic debug: co...
547
548
549
550
551
552
553
554
555
556
  
  	if (len == 0)
  		return 0;
  	/* we don't check *offp -- multiple writes() are allowed */
  	if (len > sizeof(tmpbuf)-1)
  		return -E2BIG;
  	if (copy_from_user(tmpbuf, ubuf, len))
  		return -EFAULT;
  	tmpbuf[len] = '\0';
  	if (verbose)
4ad275e5c   Joe Perches   dynamic_debug: Co...
557
558
  		pr_info("read %d bytes from userspace
  ", (int)len);
e9d376f0f   Jason Baron   dynamic debug: co...
559

fd89cfb87   Thomas Renninger   Dynamic Debug: Sp...
560
561
562
  	ret = ddebug_exec_query(tmpbuf);
  	if (ret)
  		return ret;
e9d376f0f   Jason Baron   dynamic debug: co...
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
  
  	*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;
  
  	if (verbose)
4ad275e5c   Joe Perches   dynamic_debug: Co...
621
622
  		pr_info("called m=%p *pos=%lld
  ", m, (unsigned long long)*pos);
e9d376f0f   Jason Baron   dynamic debug: co...
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
  
  	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;
  
  	if (verbose)
4ad275e5c   Joe Perches   dynamic_debug: Co...
647
648
649
  		pr_info("called m=%p p=%p *pos=%lld
  ",
  			m, p, (unsigned long long)*pos);
e9d376f0f   Jason Baron   dynamic debug: co...
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
  
  	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;
  	char flagsbuf[8];
  
  	if (verbose)
4ad275e5c   Joe Perches   dynamic_debug: Co...
672
673
  		pr_info("called m=%p p=%p
  ", m, p);
e9d376f0f   Jason Baron   dynamic debug: co...
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
  
  	if (p == SEQ_START_TOKEN) {
  		seq_puts(m,
  			"# filename:lineno [module]function flags format
  ");
  		return 0;
  	}
  
  	seq_printf(m, "%s:%u [%s]%s %s \"",
  		   dp->filename, dp->lineno,
  		   iter->table->mod_name, dp->function,
  		   ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
  	seq_escape(m, dp->format, "\t\r
  \"");
  	seq_puts(m, "\"
  ");
  
  	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)
  {
  	if (verbose)
4ad275e5c   Joe Perches   dynamic_debug: Co...
701
702
  		pr_info("called m=%p p=%p
  ", m, p);
e9d376f0f   Jason Baron   dynamic debug: co...
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
  	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
  };
  
  /*
   * 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.
   */
  static int ddebug_proc_open(struct inode *inode, struct file *file)
  {
  	struct ddebug_iter *iter;
  	int err;
  
  	if (verbose)
4ad275e5c   Joe Perches   dynamic_debug: Co...
725
726
  		pr_info("called
  ");
e9d376f0f   Jason Baron   dynamic debug: co...
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
  
  	iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  	if (iter == NULL)
  		return -ENOMEM;
  
  	err = seq_open(file, &ddebug_proc_seqops);
  	if (err) {
  		kfree(iter);
  		return err;
  	}
  	((struct seq_file *) file->private_data)->private = iter;
  	return 0;
  }
  
  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;
  	char *new_name;
  
  	dt = kzalloc(sizeof(*dt), GFP_KERNEL);
  	if (dt == NULL)
  		return -ENOMEM;
  	new_name = kstrdup(name, GFP_KERNEL);
  	if (new_name == NULL) {
  		kfree(dt);
  		return -ENOMEM;
  	}
  	dt->mod_name = new_name;
  	dt->num_ddebugs = n;
e9d376f0f   Jason Baron   dynamic debug: co...
770
771
772
773
774
775
776
  	dt->ddebugs = tab;
  
  	mutex_lock(&ddebug_lock);
  	list_add_tail(&dt->link, &ddebug_tables);
  	mutex_unlock(&ddebug_lock);
  
  	if (verbose)
4ad275e5c   Joe Perches   dynamic_debug: Co...
777
778
  		pr_info("%u debug prints in module %s
  ", n, dt->mod_name);
e9d376f0f   Jason Baron   dynamic debug: co...
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
  	return 0;
  }
  EXPORT_SYMBOL_GPL(ddebug_add_module);
  
  static void ddebug_table_free(struct ddebug_table *dt)
  {
  	list_del_init(&dt->link);
  	kfree(dt->mod_name);
  	kfree(dt);
  }
  
  /*
   * Called in response to a module being unloaded.  Removes
   * any ddebug_table's which point at the module.
   */
ff49d74ad   Yehuda Sadeh   module: initializ...
794
  int ddebug_remove_module(const char *mod_name)
e9d376f0f   Jason Baron   dynamic debug: co...
795
796
797
798
799
  {
  	struct ddebug_table *dt, *nextdt;
  	int ret = -ENOENT;
  
  	if (verbose)
4ad275e5c   Joe Perches   dynamic_debug: Co...
800
801
  		pr_info("removing module \"%s\"
  ", mod_name);
e9d376f0f   Jason Baron   dynamic debug: co...
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
  
  	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);
  }
6a5c083de   Thomas Renninger   Dynamic Debug: In...
826
827
828
  static __initdata int ddebug_init_success;
  
  static int __init dynamic_debug_init_debugfs(void)
e9d376f0f   Jason Baron   dynamic debug: co...
829
830
  {
  	struct dentry *dir, *file;
6a5c083de   Thomas Renninger   Dynamic Debug: In...
831
832
833
  
  	if (!ddebug_init_success)
  		return -ENODEV;
e9d376f0f   Jason Baron   dynamic debug: co...
834
835
836
837
838
839
840
841
842
843
  
  	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;
  	}
6a5c083de   Thomas Renninger   Dynamic Debug: In...
844
845
846
847
848
849
850
851
852
  	return 0;
  }
  
  static int __init dynamic_debug_init(void)
  {
  	struct _ddebug *iter, *iter_start;
  	const char *modname = NULL;
  	int ret = 0;
  	int n = 0;
e9d376f0f   Jason Baron   dynamic debug: co...
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
  	if (__start___verbose != __stop___verbose) {
  		iter = __start___verbose;
  		modname = iter->modname;
  		iter_start = iter;
  		for (; iter < __stop___verbose; iter++) {
  			if (strcmp(modname, iter->modname)) {
  				ret = ddebug_add_module(iter_start, n, modname);
  				if (ret)
  					goto out_free;
  				n = 0;
  				modname = iter->modname;
  				iter_start = iter;
  			}
  			n++;
  		}
  		ret = ddebug_add_module(iter_start, n, modname);
  	}
a648ec05b   Thomas Renninger   Dynamic Debug: In...
870
871
872
873
874
  
  	/* ddebug_query boot param got passed -> set it up */
  	if (ddebug_setup_string[0] != '\0') {
  		ret = ddebug_exec_query(ddebug_setup_string);
  		if (ret)
4ad275e5c   Joe Perches   dynamic_debug: Co...
875
876
  			pr_warn("Invalid ddebug boot param %s",
  				ddebug_setup_string);
a648ec05b   Thomas Renninger   Dynamic Debug: In...
877
878
879
880
  		else
  			pr_info("ddebug initialized with string %s",
  				ddebug_setup_string);
  	}
e9d376f0f   Jason Baron   dynamic debug: co...
881
  out_free:
6a5c083de   Thomas Renninger   Dynamic Debug: In...
882
  	if (ret)
e9d376f0f   Jason Baron   dynamic debug: co...
883
  		ddebug_remove_all_tables();
6a5c083de   Thomas Renninger   Dynamic Debug: In...
884
885
  	else
  		ddebug_init_success = 1;
e9d376f0f   Jason Baron   dynamic debug: co...
886
887
  	return 0;
  }
6a5c083de   Thomas Renninger   Dynamic Debug: In...
888
889
890
891
  /* Allow early initialization for boot messages via boot param */
  arch_initcall(dynamic_debug_init);
  /* Debugfs setup must be done later */
  module_init(dynamic_debug_init_debugfs);