blob: 93fc5d500cd5e0aa90895d282dfd81150dcd1681 [file] [log] [blame]
Jason Barone9d376f2009-02-05 11:51:38 -05001/*
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.
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +010010 * Copyright (C) 2011 Bart Van Assche. All Rights Reserved.
Jason Barone9d376f2009-02-05 11:51:38 -050011 */
12
Joe Perches4ad275e2011-08-11 14:36:33 -040013#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
14
Jason Barone9d376f2009-02-05 11:51:38 -050015#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/kallsyms.h>
Jason Barone9d376f2009-02-05 11:51:38 -050019#include <linux/types.h>
20#include <linux/mutex.h>
21#include <linux/proc_fs.h>
22#include <linux/seq_file.h>
23#include <linux/list.h>
24#include <linux/sysctl.h>
25#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080026#include <linux/string.h>
Jason Barone9d376f2009-02-05 11:51:38 -050027#include <linux/uaccess.h>
28#include <linux/dynamic_debug.h>
29#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Jason Baron52159d92010-09-17 11:09:17 -040031#include <linux/jump_label.h>
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +010032#include <linux/hardirq.h>
Greg Kroah-Hartmane8d97922011-02-03 15:59:58 -080033#include <linux/sched.h>
Joe Perchescbc46632011-08-11 14:36:21 -040034#include <linux/device.h>
Jason Baronffa10cb2011-08-11 14:36:48 -040035#include <linux/netdevice.h>
Jason Barone9d376f2009-02-05 11:51:38 -050036
37extern struct _ddebug __start___verbose[];
38extern struct _ddebug __stop___verbose[];
39
Jason Barone9d376f2009-02-05 11:51:38 -050040struct ddebug_table {
41 struct list_head link;
42 char *mod_name;
43 unsigned int num_ddebugs;
Jason Barone9d376f2009-02-05 11:51:38 -050044 struct _ddebug *ddebugs;
45};
46
47struct ddebug_query {
48 const char *filename;
49 const char *module;
50 const char *function;
51 const char *format;
52 unsigned int first_lineno, last_lineno;
53};
54
55struct ddebug_iter {
56 struct ddebug_table *table;
57 unsigned int idx;
58};
59
60static DEFINE_MUTEX(ddebug_lock);
61static LIST_HEAD(ddebug_tables);
62static int verbose = 0;
Jim Cromie74df1382011-12-19 17:12:24 -050063module_param(verbose, int, 0644);
Jason Barone9d376f2009-02-05 11:51:38 -050064
65/* Return the last part of a pathname */
66static inline const char *basename(const char *path)
67{
68 const char *tail = strrchr(path, '/');
69 return tail ? tail+1 : path;
70}
71
Jim Cromie2b678312011-12-19 17:13:12 -050072/* Return the path relative to source root */
73static inline const char *trim_prefix(const char *path)
74{
75 int skip = strlen(__FILE__) - strlen("lib/dynamic_debug.c");
76
77 if (strncmp(path, __FILE__, skip))
78 skip = 0; /* prefix mismatch, don't skip */
79
80 return path + skip;
81}
82
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +010083static struct { unsigned flag:8; char opt_char; } opt_array[] = {
84 { _DPRINTK_FLAGS_PRINT, 'p' },
85 { _DPRINTK_FLAGS_INCL_MODNAME, 'm' },
86 { _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' },
87 { _DPRINTK_FLAGS_INCL_LINENO, 'l' },
88 { _DPRINTK_FLAGS_INCL_TID, 't' },
Jim Cromie5ca7d2a2011-12-19 17:12:44 -050089 { _DPRINTK_FLAGS_NONE, '_' },
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +010090};
91
Jason Barone9d376f2009-02-05 11:51:38 -050092/* format a string into buf[] which describes the _ddebug's flags */
93static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,
94 size_t maxlen)
95{
96 char *p = buf;
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +010097 int i;
Jason Barone9d376f2009-02-05 11:51:38 -050098
Jim Cromie5ca7d2a2011-12-19 17:12:44 -050099 BUG_ON(maxlen < 6);
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100100 for (i = 0; i < ARRAY_SIZE(opt_array); ++i)
101 if (dp->flags & opt_array[i].flag)
102 *p++ = opt_array[i].opt_char;
Jason Barone9d376f2009-02-05 11:51:38 -0500103 if (p == buf)
Jim Cromie5ca7d2a2011-12-19 17:12:44 -0500104 *p++ = '_';
Jason Barone9d376f2009-02-05 11:51:38 -0500105 *p = '\0';
106
107 return buf;
108}
109
Jim Cromie574b3722011-12-19 17:13:16 -0500110#define vpr_info_dq(q, msg) \
111do { \
112 if (verbose) \
113 /* trim last char off format print */ \
114 pr_info("%s: func=\"%s\" file=\"%s\" " \
115 "module=\"%s\" format=\"%.*s\" " \
116 "lineno=%u-%u", \
117 msg, \
118 q->function ? q->function : "", \
119 q->filename ? q->filename : "", \
120 q->module ? q->module : "", \
121 (int)(q->format ? strlen(q->format) - 1 : 0), \
122 q->format ? q->format : "", \
123 q->first_lineno, q->last_lineno); \
124} while (0)
125
Jason Barone9d376f2009-02-05 11:51:38 -0500126/*
Jason Barone9d376f2009-02-05 11:51:38 -0500127 * Search the tables for _ddebug's which match the given
128 * `query' and apply the `flags' and `mask' to them. Tells
129 * the user which ddebug's were changed, or whether none
130 * were matched.
131 */
132static void ddebug_change(const struct ddebug_query *query,
133 unsigned int flags, unsigned int mask)
134{
135 int i;
136 struct ddebug_table *dt;
137 unsigned int newflags;
138 unsigned int nfound = 0;
Jim Cromie5ca7d2a2011-12-19 17:12:44 -0500139 char flagbuf[10];
Jason Barone9d376f2009-02-05 11:51:38 -0500140
141 /* search for matching ddebugs */
142 mutex_lock(&ddebug_lock);
143 list_for_each_entry(dt, &ddebug_tables, link) {
144
145 /* match against the module name */
Jim Cromied6a238d2011-12-19 17:12:39 -0500146 if (query->module && strcmp(query->module, dt->mod_name))
Jason Barone9d376f2009-02-05 11:51:38 -0500147 continue;
148
149 for (i = 0 ; i < dt->num_ddebugs ; i++) {
150 struct _ddebug *dp = &dt->ddebugs[i];
151
152 /* match against the source filename */
Jim Cromied6a238d2011-12-19 17:12:39 -0500153 if (query->filename &&
Jason Barone9d376f2009-02-05 11:51:38 -0500154 strcmp(query->filename, dp->filename) &&
Jim Cromie2b678312011-12-19 17:13:12 -0500155 strcmp(query->filename, basename(dp->filename)) &&
156 strcmp(query->filename, trim_prefix(dp->filename)))
Jason Barone9d376f2009-02-05 11:51:38 -0500157 continue;
158
159 /* match against the function */
Jim Cromied6a238d2011-12-19 17:12:39 -0500160 if (query->function &&
Jason Barone9d376f2009-02-05 11:51:38 -0500161 strcmp(query->function, dp->function))
162 continue;
163
164 /* match against the format */
Jim Cromied6a238d2011-12-19 17:12:39 -0500165 if (query->format &&
166 !strstr(dp->format, query->format))
Jason Barone9d376f2009-02-05 11:51:38 -0500167 continue;
168
169 /* match against the line number range */
170 if (query->first_lineno &&
171 dp->lineno < query->first_lineno)
172 continue;
173 if (query->last_lineno &&
174 dp->lineno > query->last_lineno)
175 continue;
176
177 nfound++;
178
179 newflags = (dp->flags & mask) | flags;
180 if (newflags == dp->flags)
181 continue;
Jason Barone9d376f2009-02-05 11:51:38 -0500182 dp->flags = newflags;
Jason Barone9d376f2009-02-05 11:51:38 -0500183 if (verbose)
Jim Cromie5ca7d2a2011-12-19 17:12:44 -0500184 pr_info("changed %s:%d [%s]%s =%s\n",
Jim Cromie2b678312011-12-19 17:13:12 -0500185 trim_prefix(dp->filename), dp->lineno,
Jason Barone9d376f2009-02-05 11:51:38 -0500186 dt->mod_name, dp->function,
187 ddebug_describe_flags(dp, flagbuf,
188 sizeof(flagbuf)));
189 }
190 }
191 mutex_unlock(&ddebug_lock);
192
193 if (!nfound && verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400194 pr_info("no matches for query\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500195}
196
197/*
Jason Barone9d376f2009-02-05 11:51:38 -0500198 * Split the buffer `buf' into space-separated words.
Greg Banks9898abb2009-02-06 12:54:26 +1100199 * Handles simple " and ' quoting, i.e. without nested,
200 * embedded or escaped \". Return the number of words
201 * or <0 on error.
Jason Barone9d376f2009-02-05 11:51:38 -0500202 */
203static int ddebug_tokenize(char *buf, char *words[], int maxwords)
204{
205 int nwords = 0;
206
Greg Banks9898abb2009-02-06 12:54:26 +1100207 while (*buf) {
208 char *end;
209
210 /* Skip leading whitespace */
André Goddard Rosae7d28602009-12-14 18:01:06 -0800211 buf = skip_spaces(buf);
Greg Banks9898abb2009-02-06 12:54:26 +1100212 if (!*buf)
213 break; /* oh, it was trailing whitespace */
Jim Cromie8bd60262011-12-19 17:13:03 -0500214 if (*buf == '#')
215 break; /* token starts comment, skip rest of line */
Greg Banks9898abb2009-02-06 12:54:26 +1100216
Jim Cromie07100be2011-12-19 17:11:09 -0500217 /* find `end' of word, whitespace separated or quoted */
Greg Banks9898abb2009-02-06 12:54:26 +1100218 if (*buf == '"' || *buf == '\'') {
219 int quote = *buf++;
220 for (end = buf ; *end && *end != quote ; end++)
221 ;
222 if (!*end)
223 return -EINVAL; /* unclosed quote */
224 } else {
225 for (end = buf ; *end && !isspace(*end) ; end++)
226 ;
227 BUG_ON(end == buf);
228 }
Greg Banks9898abb2009-02-06 12:54:26 +1100229
Jim Cromie07100be2011-12-19 17:11:09 -0500230 /* `buf' is start of word, `end' is one past its end */
Greg Banks9898abb2009-02-06 12:54:26 +1100231 if (nwords == maxwords)
232 return -EINVAL; /* ran out of words[] before bytes */
233 if (*end)
234 *end++ = '\0'; /* terminate the word */
235 words[nwords++] = buf;
236 buf = end;
237 }
Jason Barone9d376f2009-02-05 11:51:38 -0500238
239 if (verbose) {
240 int i;
Joe Perches4ad275e2011-08-11 14:36:33 -0400241 pr_info("split into words:");
Jason Barone9d376f2009-02-05 11:51:38 -0500242 for (i = 0 ; i < nwords ; i++)
Joe Perches4ad275e2011-08-11 14:36:33 -0400243 pr_cont(" \"%s\"", words[i]);
244 pr_cont("\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500245 }
246
247 return nwords;
248}
249
250/*
251 * Parse a single line number. Note that the empty string ""
252 * is treated as a special case and converted to zero, which
253 * is later treated as a "don't care" value.
254 */
255static inline int parse_lineno(const char *str, unsigned int *val)
256{
257 char *end = NULL;
258 BUG_ON(str == NULL);
259 if (*str == '\0') {
260 *val = 0;
261 return 0;
262 }
263 *val = simple_strtoul(str, &end, 10);
264 return end == NULL || end == str || *end != '\0' ? -EINVAL : 0;
265}
266
267/*
268 * Undo octal escaping in a string, inplace. This is useful to
269 * allow the user to express a query which matches a format
270 * containing embedded spaces.
271 */
272#define isodigit(c) ((c) >= '0' && (c) <= '7')
273static char *unescape(char *str)
274{
275 char *in = str;
276 char *out = str;
277
278 while (*in) {
279 if (*in == '\\') {
280 if (in[1] == '\\') {
281 *out++ = '\\';
282 in += 2;
283 continue;
284 } else if (in[1] == 't') {
285 *out++ = '\t';
286 in += 2;
287 continue;
288 } else if (in[1] == 'n') {
289 *out++ = '\n';
290 in += 2;
291 continue;
292 } else if (isodigit(in[1]) &&
293 isodigit(in[2]) &&
294 isodigit(in[3])) {
295 *out++ = ((in[1] - '0')<<6) |
296 ((in[2] - '0')<<3) |
297 (in[3] - '0');
298 in += 4;
299 continue;
300 }
301 }
302 *out++ = *in++;
303 }
304 *out = '\0';
305
306 return str;
307}
308
Jim Cromie820874c2011-12-19 17:12:49 -0500309static int check_set(const char **dest, char *src, char *name)
310{
311 int rc = 0;
312
313 if (*dest) {
314 rc = -EINVAL;
315 pr_err("match-spec:%s val:%s overridden by %s",
316 name, *dest, src);
317 }
318 *dest = src;
319 return rc;
320}
321
Jason Barone9d376f2009-02-05 11:51:38 -0500322/*
323 * Parse words[] as a ddebug query specification, which is a series
324 * of (keyword, value) pairs chosen from these possibilities:
325 *
326 * func <function-name>
327 * file <full-pathname>
328 * file <base-filename>
329 * module <module-name>
330 * format <escaped-string-to-find-in-format>
331 * line <lineno>
332 * line <first-lineno>-<last-lineno> // where either may be empty
Jim Cromie820874c2011-12-19 17:12:49 -0500333 *
334 * Only 1 of each type is allowed.
335 * Returns 0 on success, <0 on error.
Jason Barone9d376f2009-02-05 11:51:38 -0500336 */
337static int ddebug_parse_query(char *words[], int nwords,
338 struct ddebug_query *query)
339{
340 unsigned int i;
Jim Cromie820874c2011-12-19 17:12:49 -0500341 int rc;
Jason Barone9d376f2009-02-05 11:51:38 -0500342
343 /* check we have an even number of words */
344 if (nwords % 2 != 0)
345 return -EINVAL;
346 memset(query, 0, sizeof(*query));
347
348 for (i = 0 ; i < nwords ; i += 2) {
349 if (!strcmp(words[i], "func"))
Jim Cromie820874c2011-12-19 17:12:49 -0500350 rc = check_set(&query->function, words[i+1], "func");
Jason Barone9d376f2009-02-05 11:51:38 -0500351 else if (!strcmp(words[i], "file"))
Jim Cromie820874c2011-12-19 17:12:49 -0500352 rc = check_set(&query->filename, words[i+1], "file");
Jason Barone9d376f2009-02-05 11:51:38 -0500353 else if (!strcmp(words[i], "module"))
Jim Cromie820874c2011-12-19 17:12:49 -0500354 rc = check_set(&query->module, words[i+1], "module");
Jason Barone9d376f2009-02-05 11:51:38 -0500355 else if (!strcmp(words[i], "format"))
Jim Cromie820874c2011-12-19 17:12:49 -0500356 rc = check_set(&query->format, unescape(words[i+1]),
357 "format");
Jason Barone9d376f2009-02-05 11:51:38 -0500358 else if (!strcmp(words[i], "line")) {
359 char *first = words[i+1];
360 char *last = strchr(first, '-');
Jim Cromie820874c2011-12-19 17:12:49 -0500361 if (query->first_lineno || query->last_lineno) {
362 pr_err("match-spec:line given 2 times\n");
363 return -EINVAL;
364 }
Jason Barone9d376f2009-02-05 11:51:38 -0500365 if (last)
366 *last++ = '\0';
367 if (parse_lineno(first, &query->first_lineno) < 0)
368 return -EINVAL;
Jim Cromie820874c2011-12-19 17:12:49 -0500369 if (last) {
Jason Barone9d376f2009-02-05 11:51:38 -0500370 /* range <first>-<last> */
Jim Cromie820874c2011-12-19 17:12:49 -0500371 if (parse_lineno(last, &query->last_lineno)
372 < query->first_lineno) {
373 pr_err("last-line < 1st-line\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500374 return -EINVAL;
Jim Cromie820874c2011-12-19 17:12:49 -0500375 }
Jason Barone9d376f2009-02-05 11:51:38 -0500376 } else {
377 query->last_lineno = query->first_lineno;
378 }
379 } else {
Jim Cromieae27f862011-12-19 17:12:34 -0500380 pr_err("unknown keyword \"%s\"\n", words[i]);
Jason Barone9d376f2009-02-05 11:51:38 -0500381 return -EINVAL;
382 }
Jim Cromie820874c2011-12-19 17:12:49 -0500383 if (rc)
384 return rc;
Jason Barone9d376f2009-02-05 11:51:38 -0500385 }
Jim Cromie574b3722011-12-19 17:13:16 -0500386 vpr_info_dq(query, "parsed");
Jason Barone9d376f2009-02-05 11:51:38 -0500387 return 0;
388}
389
390/*
391 * Parse `str' as a flags specification, format [-+=][p]+.
392 * Sets up *maskp and *flagsp to be used when changing the
393 * flags fields of matched _ddebug's. Returns 0 on success
394 * or <0 on error.
395 */
396static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
397 unsigned int *maskp)
398{
399 unsigned flags = 0;
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100400 int op = '=', i;
Jason Barone9d376f2009-02-05 11:51:38 -0500401
402 switch (*str) {
403 case '+':
404 case '-':
405 case '=':
406 op = *str++;
407 break;
408 default:
409 return -EINVAL;
410 }
411 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400412 pr_info("op='%c'\n", op);
Jason Barone9d376f2009-02-05 11:51:38 -0500413
414 for ( ; *str ; ++str) {
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100415 for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) {
416 if (*str == opt_array[i].opt_char) {
417 flags |= opt_array[i].flag;
418 break;
419 }
Jason Barone9d376f2009-02-05 11:51:38 -0500420 }
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100421 if (i < 0)
422 return -EINVAL;
Jason Barone9d376f2009-02-05 11:51:38 -0500423 }
Jason Barone9d376f2009-02-05 11:51:38 -0500424 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400425 pr_info("flags=0x%x\n", flags);
Jason Barone9d376f2009-02-05 11:51:38 -0500426
427 /* calculate final *flagsp, *maskp according to mask and op */
428 switch (op) {
429 case '=':
430 *maskp = 0;
431 *flagsp = flags;
432 break;
433 case '+':
434 *maskp = ~0U;
435 *flagsp = flags;
436 break;
437 case '-':
438 *maskp = ~flags;
439 *flagsp = 0;
440 break;
441 }
442 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400443 pr_info("*flagsp=0x%x *maskp=0x%x\n", *flagsp, *maskp);
Jason Barone9d376f2009-02-05 11:51:38 -0500444 return 0;
445}
446
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200447static int ddebug_exec_query(char *query_string)
448{
449 unsigned int flags = 0, mask = 0;
450 struct ddebug_query query;
451#define MAXWORDS 9
452 int nwords;
453 char *words[MAXWORDS];
454
455 nwords = ddebug_tokenize(query_string, words, MAXWORDS);
456 if (nwords <= 0)
457 return -EINVAL;
458 if (ddebug_parse_query(words, nwords-1, &query))
459 return -EINVAL;
460 if (ddebug_parse_flags(words[nwords-1], &flags, &mask))
461 return -EINVAL;
462
463 /* actually go and implement the change */
464 ddebug_change(&query, flags, mask);
465 return 0;
466}
467
Jason Baron431625d2011-10-04 14:13:19 -0700468#define PREFIX_SIZE 64
469
470static int remaining(int wrote)
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100471{
Jason Baron431625d2011-10-04 14:13:19 -0700472 if (PREFIX_SIZE - wrote > 0)
473 return PREFIX_SIZE - wrote;
474 return 0;
475}
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100476
Jason Baron431625d2011-10-04 14:13:19 -0700477static char *dynamic_emit_prefix(const struct _ddebug *desc, char *buf)
478{
479 int pos_after_tid;
480 int pos = 0;
481
482 pos += snprintf(buf + pos, remaining(pos), "%s", KERN_DEBUG);
483 if (desc->flags & _DPRINTK_FLAGS_INCL_TID) {
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100484 if (in_interrupt())
Jason Baron431625d2011-10-04 14:13:19 -0700485 pos += snprintf(buf + pos, remaining(pos), "%s ",
486 "<intr>");
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100487 else
Jason Baron431625d2011-10-04 14:13:19 -0700488 pos += snprintf(buf + pos, remaining(pos), "[%d] ",
489 task_pid_vnr(current));
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100490 }
Jason Baron431625d2011-10-04 14:13:19 -0700491 pos_after_tid = pos;
492 if (desc->flags & _DPRINTK_FLAGS_INCL_MODNAME)
493 pos += snprintf(buf + pos, remaining(pos), "%s:",
494 desc->modname);
495 if (desc->flags & _DPRINTK_FLAGS_INCL_FUNCNAME)
496 pos += snprintf(buf + pos, remaining(pos), "%s:",
497 desc->function);
498 if (desc->flags & _DPRINTK_FLAGS_INCL_LINENO)
Jim Cromie07100be2011-12-19 17:11:09 -0500499 pos += snprintf(buf + pos, remaining(pos), "%d:",
500 desc->lineno);
Jason Baron431625d2011-10-04 14:13:19 -0700501 if (pos - pos_after_tid)
502 pos += snprintf(buf + pos, remaining(pos), " ");
503 if (pos >= PREFIX_SIZE)
504 buf[PREFIX_SIZE - 1] = '\0';
Joe Perches6c2140e2011-08-11 14:36:25 -0400505
Jason Baron431625d2011-10-04 14:13:19 -0700506 return buf;
Joe Perches6c2140e2011-08-11 14:36:25 -0400507}
508
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100509int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
510{
511 va_list args;
512 int res;
Jason Baron431625d2011-10-04 14:13:19 -0700513 struct va_format vaf;
514 char buf[PREFIX_SIZE];
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100515
516 BUG_ON(!descriptor);
517 BUG_ON(!fmt);
518
519 va_start(args, fmt);
Jason Baron431625d2011-10-04 14:13:19 -0700520 vaf.fmt = fmt;
521 vaf.va = &args;
522 res = printk("%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf);
Bart Van Assche8ba6ebf2011-01-23 17:17:24 +0100523 va_end(args);
524
525 return res;
526}
527EXPORT_SYMBOL(__dynamic_pr_debug);
528
Joe Perchescbc46632011-08-11 14:36:21 -0400529int __dynamic_dev_dbg(struct _ddebug *descriptor,
530 const struct device *dev, const char *fmt, ...)
531{
532 struct va_format vaf;
533 va_list args;
534 int res;
Jason Baron431625d2011-10-04 14:13:19 -0700535 char buf[PREFIX_SIZE];
Joe Perchescbc46632011-08-11 14:36:21 -0400536
537 BUG_ON(!descriptor);
538 BUG_ON(!fmt);
539
540 va_start(args, fmt);
Joe Perchescbc46632011-08-11 14:36:21 -0400541 vaf.fmt = fmt;
542 vaf.va = &args;
Jason Baron431625d2011-10-04 14:13:19 -0700543 res = __dev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf);
Joe Perchescbc46632011-08-11 14:36:21 -0400544 va_end(args);
545
546 return res;
547}
548EXPORT_SYMBOL(__dynamic_dev_dbg);
549
Jason Baron0feefd92011-10-04 14:13:22 -0700550#ifdef CONFIG_NET
551
Jason Baronffa10cb2011-08-11 14:36:48 -0400552int __dynamic_netdev_dbg(struct _ddebug *descriptor,
553 const struct net_device *dev, const char *fmt, ...)
554{
555 struct va_format vaf;
556 va_list args;
557 int res;
Jason Baron431625d2011-10-04 14:13:19 -0700558 char buf[PREFIX_SIZE];
Jason Baronffa10cb2011-08-11 14:36:48 -0400559
560 BUG_ON(!descriptor);
561 BUG_ON(!fmt);
562
563 va_start(args, fmt);
Jason Baronffa10cb2011-08-11 14:36:48 -0400564 vaf.fmt = fmt;
565 vaf.va = &args;
Jason Baron431625d2011-10-04 14:13:19 -0700566 res = __netdev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf);
Jason Baronffa10cb2011-08-11 14:36:48 -0400567 va_end(args);
568
569 return res;
570}
571EXPORT_SYMBOL(__dynamic_netdev_dbg);
572
Jason Baron0feefd92011-10-04 14:13:22 -0700573#endif
574
Jim Cromiebc757f62011-12-19 17:12:29 -0500575#define DDEBUG_STRING_SIZE 1024
576static __initdata char ddebug_setup_string[DDEBUG_STRING_SIZE];
577
Thomas Renningera648ec02010-08-06 16:11:02 +0200578static __init int ddebug_setup_query(char *str)
579{
Jim Cromiebc757f62011-12-19 17:12:29 -0500580 if (strlen(str) >= DDEBUG_STRING_SIZE) {
Joe Perches4ad275e2011-08-11 14:36:33 -0400581 pr_warn("ddebug boot param string too large\n");
Thomas Renningera648ec02010-08-06 16:11:02 +0200582 return 0;
583 }
Jim Cromiebc757f62011-12-19 17:12:29 -0500584 strlcpy(ddebug_setup_string, str, DDEBUG_STRING_SIZE);
Thomas Renningera648ec02010-08-06 16:11:02 +0200585 return 1;
586}
587
588__setup("ddebug_query=", ddebug_setup_query);
589
Jason Barone9d376f2009-02-05 11:51:38 -0500590/*
591 * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the
592 * command text from userspace, parses and executes it.
593 */
Jim Cromie72814912011-12-19 17:13:07 -0500594#define USER_BUF_PAGE 4096
Jason Barone9d376f2009-02-05 11:51:38 -0500595static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
596 size_t len, loff_t *offp)
597{
Jim Cromie72814912011-12-19 17:13:07 -0500598 char *tmpbuf;
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200599 int ret;
Jason Barone9d376f2009-02-05 11:51:38 -0500600
601 if (len == 0)
602 return 0;
Jim Cromie72814912011-12-19 17:13:07 -0500603 if (len > USER_BUF_PAGE - 1) {
604 pr_warn("expected <%d bytes into control\n", USER_BUF_PAGE);
Jason Barone9d376f2009-02-05 11:51:38 -0500605 return -E2BIG;
Jim Cromie72814912011-12-19 17:13:07 -0500606 }
607 tmpbuf = kmalloc(len + 1, GFP_KERNEL);
608 if (!tmpbuf)
609 return -ENOMEM;
610 if (copy_from_user(tmpbuf, ubuf, len)) {
611 kfree(tmpbuf);
Jason Barone9d376f2009-02-05 11:51:38 -0500612 return -EFAULT;
Jim Cromie72814912011-12-19 17:13:07 -0500613 }
Jason Barone9d376f2009-02-05 11:51:38 -0500614 tmpbuf[len] = '\0';
615 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400616 pr_info("read %d bytes from userspace\n", (int)len);
Jason Barone9d376f2009-02-05 11:51:38 -0500617
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200618 ret = ddebug_exec_query(tmpbuf);
Jim Cromie72814912011-12-19 17:13:07 -0500619 kfree(tmpbuf);
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200620 if (ret)
621 return ret;
Jason Barone9d376f2009-02-05 11:51:38 -0500622
623 *offp += len;
624 return len;
625}
626
627/*
628 * Set the iterator to point to the first _ddebug object
629 * and return a pointer to that first object. Returns
630 * NULL if there are no _ddebugs at all.
631 */
632static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
633{
634 if (list_empty(&ddebug_tables)) {
635 iter->table = NULL;
636 iter->idx = 0;
637 return NULL;
638 }
639 iter->table = list_entry(ddebug_tables.next,
640 struct ddebug_table, link);
641 iter->idx = 0;
642 return &iter->table->ddebugs[iter->idx];
643}
644
645/*
646 * Advance the iterator to point to the next _ddebug
647 * object from the one the iterator currently points at,
648 * and returns a pointer to the new _ddebug. Returns
649 * NULL if the iterator has seen all the _ddebugs.
650 */
651static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
652{
653 if (iter->table == NULL)
654 return NULL;
655 if (++iter->idx == iter->table->num_ddebugs) {
656 /* iterate to next table */
657 iter->idx = 0;
658 if (list_is_last(&iter->table->link, &ddebug_tables)) {
659 iter->table = NULL;
660 return NULL;
661 }
662 iter->table = list_entry(iter->table->link.next,
663 struct ddebug_table, link);
664 }
665 return &iter->table->ddebugs[iter->idx];
666}
667
668/*
669 * Seq_ops start method. Called at the start of every
670 * read() call from userspace. Takes the ddebug_lock and
671 * seeks the seq_file's iterator to the given position.
672 */
673static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
674{
675 struct ddebug_iter *iter = m->private;
676 struct _ddebug *dp;
677 int n = *pos;
678
679 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400680 pr_info("called m=%p *pos=%lld\n", m, (unsigned long long)*pos);
Jason Barone9d376f2009-02-05 11:51:38 -0500681
682 mutex_lock(&ddebug_lock);
683
684 if (!n)
685 return SEQ_START_TOKEN;
686 if (n < 0)
687 return NULL;
688 dp = ddebug_iter_first(iter);
689 while (dp != NULL && --n > 0)
690 dp = ddebug_iter_next(iter);
691 return dp;
692}
693
694/*
695 * Seq_ops next method. Called several times within a read()
696 * call from userspace, with ddebug_lock held. Walks to the
697 * next _ddebug object with a special case for the header line.
698 */
699static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
700{
701 struct ddebug_iter *iter = m->private;
702 struct _ddebug *dp;
703
704 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400705 pr_info("called m=%p p=%p *pos=%lld\n",
706 m, p, (unsigned long long)*pos);
Jason Barone9d376f2009-02-05 11:51:38 -0500707
708 if (p == SEQ_START_TOKEN)
709 dp = ddebug_iter_first(iter);
710 else
711 dp = ddebug_iter_next(iter);
712 ++*pos;
713 return dp;
714}
715
716/*
717 * Seq_ops show method. Called several times within a read()
718 * call from userspace, with ddebug_lock held. Formats the
719 * current _ddebug as a single human-readable line, with a
720 * special case for the header line.
721 */
722static int ddebug_proc_show(struct seq_file *m, void *p)
723{
724 struct ddebug_iter *iter = m->private;
725 struct _ddebug *dp = p;
Jim Cromie5ca7d2a2011-12-19 17:12:44 -0500726 char flagsbuf[10];
Jason Barone9d376f2009-02-05 11:51:38 -0500727
728 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400729 pr_info("called m=%p p=%p\n", m, p);
Jason Barone9d376f2009-02-05 11:51:38 -0500730
731 if (p == SEQ_START_TOKEN) {
732 seq_puts(m,
733 "# filename:lineno [module]function flags format\n");
734 return 0;
735 }
736
Jim Cromie5ca7d2a2011-12-19 17:12:44 -0500737 seq_printf(m, "%s:%u [%s]%s =%s \"",
Jim Cromie2b678312011-12-19 17:13:12 -0500738 trim_prefix(dp->filename), dp->lineno,
Jim Cromie5ca7d2a2011-12-19 17:12:44 -0500739 iter->table->mod_name, dp->function,
740 ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
Jason Barone9d376f2009-02-05 11:51:38 -0500741 seq_escape(m, dp->format, "\t\r\n\"");
742 seq_puts(m, "\"\n");
743
744 return 0;
745}
746
747/*
748 * Seq_ops stop method. Called at the end of each read()
749 * call from userspace. Drops ddebug_lock.
750 */
751static void ddebug_proc_stop(struct seq_file *m, void *p)
752{
753 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400754 pr_info("called m=%p p=%p\n", m, p);
Jason Barone9d376f2009-02-05 11:51:38 -0500755 mutex_unlock(&ddebug_lock);
756}
757
758static const struct seq_operations ddebug_proc_seqops = {
759 .start = ddebug_proc_start,
760 .next = ddebug_proc_next,
761 .show = ddebug_proc_show,
762 .stop = ddebug_proc_stop
763};
764
765/*
Jim Cromie07100be2011-12-19 17:11:09 -0500766 * File_ops->open method for <debugfs>/dynamic_debug/control. Does
767 * the seq_file setup dance, and also creates an iterator to walk the
768 * _ddebugs. Note that we create a seq_file always, even for O_WRONLY
769 * files where it's not needed, as doing so simplifies the ->release
770 * method.
Jason Barone9d376f2009-02-05 11:51:38 -0500771 */
772static int ddebug_proc_open(struct inode *inode, struct file *file)
773{
774 struct ddebug_iter *iter;
775 int err;
776
777 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400778 pr_info("called\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500779
780 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
781 if (iter == NULL)
782 return -ENOMEM;
783
784 err = seq_open(file, &ddebug_proc_seqops);
785 if (err) {
786 kfree(iter);
787 return err;
788 }
789 ((struct seq_file *) file->private_data)->private = iter;
790 return 0;
791}
792
793static const struct file_operations ddebug_proc_fops = {
794 .owner = THIS_MODULE,
795 .open = ddebug_proc_open,
796 .read = seq_read,
797 .llseek = seq_lseek,
798 .release = seq_release_private,
799 .write = ddebug_proc_write
800};
801
802/*
803 * Allocate a new ddebug_table for the given module
804 * and add it to the global list.
805 */
806int ddebug_add_module(struct _ddebug *tab, unsigned int n,
807 const char *name)
808{
809 struct ddebug_table *dt;
810 char *new_name;
811
812 dt = kzalloc(sizeof(*dt), GFP_KERNEL);
813 if (dt == NULL)
814 return -ENOMEM;
815 new_name = kstrdup(name, GFP_KERNEL);
816 if (new_name == NULL) {
817 kfree(dt);
818 return -ENOMEM;
819 }
820 dt->mod_name = new_name;
821 dt->num_ddebugs = n;
Jason Barone9d376f2009-02-05 11:51:38 -0500822 dt->ddebugs = tab;
823
824 mutex_lock(&ddebug_lock);
825 list_add_tail(&dt->link, &ddebug_tables);
826 mutex_unlock(&ddebug_lock);
827
828 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400829 pr_info("%u debug prints in module %s\n", n, dt->mod_name);
Jason Barone9d376f2009-02-05 11:51:38 -0500830 return 0;
831}
832EXPORT_SYMBOL_GPL(ddebug_add_module);
833
834static void ddebug_table_free(struct ddebug_table *dt)
835{
836 list_del_init(&dt->link);
837 kfree(dt->mod_name);
838 kfree(dt);
839}
840
841/*
842 * Called in response to a module being unloaded. Removes
843 * any ddebug_table's which point at the module.
844 */
Yehuda Sadehff49d742010-07-03 13:07:35 +1000845int ddebug_remove_module(const char *mod_name)
Jason Barone9d376f2009-02-05 11:51:38 -0500846{
847 struct ddebug_table *dt, *nextdt;
848 int ret = -ENOENT;
849
850 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400851 pr_info("removing module \"%s\"\n", mod_name);
Jason Barone9d376f2009-02-05 11:51:38 -0500852
853 mutex_lock(&ddebug_lock);
854 list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
855 if (!strcmp(dt->mod_name, mod_name)) {
856 ddebug_table_free(dt);
857 ret = 0;
858 }
859 }
860 mutex_unlock(&ddebug_lock);
861 return ret;
862}
863EXPORT_SYMBOL_GPL(ddebug_remove_module);
864
865static void ddebug_remove_all_tables(void)
866{
867 mutex_lock(&ddebug_lock);
868 while (!list_empty(&ddebug_tables)) {
869 struct ddebug_table *dt = list_entry(ddebug_tables.next,
870 struct ddebug_table,
871 link);
872 ddebug_table_free(dt);
873 }
874 mutex_unlock(&ddebug_lock);
875}
876
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200877static __initdata int ddebug_init_success;
878
879static int __init dynamic_debug_init_debugfs(void)
Jason Barone9d376f2009-02-05 11:51:38 -0500880{
881 struct dentry *dir, *file;
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200882
883 if (!ddebug_init_success)
884 return -ENODEV;
Jason Barone9d376f2009-02-05 11:51:38 -0500885
886 dir = debugfs_create_dir("dynamic_debug", NULL);
887 if (!dir)
888 return -ENOMEM;
889 file = debugfs_create_file("control", 0644, dir, NULL,
890 &ddebug_proc_fops);
891 if (!file) {
892 debugfs_remove(dir);
893 return -ENOMEM;
894 }
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200895 return 0;
896}
897
898static int __init dynamic_debug_init(void)
899{
900 struct _ddebug *iter, *iter_start;
901 const char *modname = NULL;
902 int ret = 0;
903 int n = 0;
904
Jim Cromieb5b78f82011-12-19 17:12:54 -0500905 if (__start___verbose == __stop___verbose) {
906 pr_warn("_ddebug table is empty in a "
907 "CONFIG_DYNAMIC_DEBUG build");
908 return 1;
Jason Barone9d376f2009-02-05 11:51:38 -0500909 }
Jim Cromieb5b78f82011-12-19 17:12:54 -0500910 iter = __start___verbose;
911 modname = iter->modname;
912 iter_start = iter;
913 for (; iter < __stop___verbose; iter++) {
914 if (strcmp(modname, iter->modname)) {
915 ret = ddebug_add_module(iter_start, n, modname);
916 if (ret)
917 goto out_free;
918 n = 0;
919 modname = iter->modname;
920 iter_start = iter;
921 }
922 n++;
923 }
924 ret = ddebug_add_module(iter_start, n, modname);
925 if (ret)
926 goto out_free;
Thomas Renningera648ec02010-08-06 16:11:02 +0200927
928 /* ddebug_query boot param got passed -> set it up */
929 if (ddebug_setup_string[0] != '\0') {
930 ret = ddebug_exec_query(ddebug_setup_string);
931 if (ret)
Joe Perches4ad275e2011-08-11 14:36:33 -0400932 pr_warn("Invalid ddebug boot param %s",
933 ddebug_setup_string);
Thomas Renningera648ec02010-08-06 16:11:02 +0200934 else
935 pr_info("ddebug initialized with string %s",
936 ddebug_setup_string);
Jason Barone9d376f2009-02-05 11:51:38 -0500937 }
Thomas Renningera648ec02010-08-06 16:11:02 +0200938
Jason Barone9d376f2009-02-05 11:51:38 -0500939out_free:
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200940 if (ret)
Jason Barone9d376f2009-02-05 11:51:38 -0500941 ddebug_remove_all_tables();
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200942 else
943 ddebug_init_success = 1;
Jason Barone9d376f2009-02-05 11:51:38 -0500944 return 0;
945}
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200946/* Allow early initialization for boot messages via boot param */
947arch_initcall(dynamic_debug_init);
948/* Debugfs setup must be done later */
949module_init(dynamic_debug_init_debugfs);