blob: 86a9abb9a1ce832e3872d7a4f5d553c4a147447e [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 Assche8ba6ebf52011-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 Assche8ba6ebf52011-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
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +010072static struct { unsigned flag:8; char opt_char; } opt_array[] = {
73 { _DPRINTK_FLAGS_PRINT, 'p' },
74 { _DPRINTK_FLAGS_INCL_MODNAME, 'm' },
75 { _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' },
76 { _DPRINTK_FLAGS_INCL_LINENO, 'l' },
77 { _DPRINTK_FLAGS_INCL_TID, 't' },
Jim Cromie5ca7d2a2011-12-19 17:12:44 -050078 { _DPRINTK_FLAGS_NONE, '_' },
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +010079};
80
Jason Barone9d376f2009-02-05 11:51:38 -050081/* format a string into buf[] which describes the _ddebug's flags */
82static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,
83 size_t maxlen)
84{
85 char *p = buf;
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +010086 int i;
Jason Barone9d376f2009-02-05 11:51:38 -050087
Jim Cromie5ca7d2a2011-12-19 17:12:44 -050088 BUG_ON(maxlen < 6);
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +010089 for (i = 0; i < ARRAY_SIZE(opt_array); ++i)
90 if (dp->flags & opt_array[i].flag)
91 *p++ = opt_array[i].opt_char;
Jason Barone9d376f2009-02-05 11:51:38 -050092 if (p == buf)
Jim Cromie5ca7d2a2011-12-19 17:12:44 -050093 *p++ = '_';
Jason Barone9d376f2009-02-05 11:51:38 -050094 *p = '\0';
95
96 return buf;
97}
98
99/*
Jason Barone9d376f2009-02-05 11:51:38 -0500100 * Search the tables for _ddebug's which match the given
101 * `query' and apply the `flags' and `mask' to them. Tells
102 * the user which ddebug's were changed, or whether none
103 * were matched.
104 */
105static void ddebug_change(const struct ddebug_query *query,
106 unsigned int flags, unsigned int mask)
107{
108 int i;
109 struct ddebug_table *dt;
110 unsigned int newflags;
111 unsigned int nfound = 0;
Jim Cromie5ca7d2a2011-12-19 17:12:44 -0500112 char flagbuf[10];
Jason Barone9d376f2009-02-05 11:51:38 -0500113
114 /* search for matching ddebugs */
115 mutex_lock(&ddebug_lock);
116 list_for_each_entry(dt, &ddebug_tables, link) {
117
118 /* match against the module name */
Jim Cromied6a238d2011-12-19 17:12:39 -0500119 if (query->module && strcmp(query->module, dt->mod_name))
Jason Barone9d376f2009-02-05 11:51:38 -0500120 continue;
121
122 for (i = 0 ; i < dt->num_ddebugs ; i++) {
123 struct _ddebug *dp = &dt->ddebugs[i];
124
125 /* match against the source filename */
Jim Cromied6a238d2011-12-19 17:12:39 -0500126 if (query->filename &&
Jason Barone9d376f2009-02-05 11:51:38 -0500127 strcmp(query->filename, dp->filename) &&
128 strcmp(query->filename, basename(dp->filename)))
129 continue;
130
131 /* match against the function */
Jim Cromied6a238d2011-12-19 17:12:39 -0500132 if (query->function &&
Jason Barone9d376f2009-02-05 11:51:38 -0500133 strcmp(query->function, dp->function))
134 continue;
135
136 /* match against the format */
Jim Cromied6a238d2011-12-19 17:12:39 -0500137 if (query->format &&
138 !strstr(dp->format, query->format))
Jason Barone9d376f2009-02-05 11:51:38 -0500139 continue;
140
141 /* match against the line number range */
142 if (query->first_lineno &&
143 dp->lineno < query->first_lineno)
144 continue;
145 if (query->last_lineno &&
146 dp->lineno > query->last_lineno)
147 continue;
148
149 nfound++;
150
151 newflags = (dp->flags & mask) | flags;
152 if (newflags == dp->flags)
153 continue;
Jason Barone9d376f2009-02-05 11:51:38 -0500154 dp->flags = newflags;
Jason Barone9d376f2009-02-05 11:51:38 -0500155 if (verbose)
Jim Cromie5ca7d2a2011-12-19 17:12:44 -0500156 pr_info("changed %s:%d [%s]%s =%s\n",
Jason Barone9d376f2009-02-05 11:51:38 -0500157 dp->filename, dp->lineno,
158 dt->mod_name, dp->function,
159 ddebug_describe_flags(dp, flagbuf,
160 sizeof(flagbuf)));
161 }
162 }
163 mutex_unlock(&ddebug_lock);
164
165 if (!nfound && verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400166 pr_info("no matches for query\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500167}
168
169/*
Jason Barone9d376f2009-02-05 11:51:38 -0500170 * Split the buffer `buf' into space-separated words.
Greg Banks9898abb2009-02-06 12:54:26 +1100171 * Handles simple " and ' quoting, i.e. without nested,
172 * embedded or escaped \". Return the number of words
173 * or <0 on error.
Jason Barone9d376f2009-02-05 11:51:38 -0500174 */
175static int ddebug_tokenize(char *buf, char *words[], int maxwords)
176{
177 int nwords = 0;
178
Greg Banks9898abb2009-02-06 12:54:26 +1100179 while (*buf) {
180 char *end;
181
182 /* Skip leading whitespace */
André Goddard Rosae7d28602009-12-14 18:01:06 -0800183 buf = skip_spaces(buf);
Greg Banks9898abb2009-02-06 12:54:26 +1100184 if (!*buf)
185 break; /* oh, it was trailing whitespace */
Jim Cromie8bd60262011-12-19 17:13:03 -0500186 if (*buf == '#')
187 break; /* token starts comment, skip rest of line */
Greg Banks9898abb2009-02-06 12:54:26 +1100188
Jim Cromie07100be2011-12-19 17:11:09 -0500189 /* find `end' of word, whitespace separated or quoted */
Greg Banks9898abb2009-02-06 12:54:26 +1100190 if (*buf == '"' || *buf == '\'') {
191 int quote = *buf++;
192 for (end = buf ; *end && *end != quote ; end++)
193 ;
194 if (!*end)
195 return -EINVAL; /* unclosed quote */
196 } else {
197 for (end = buf ; *end && !isspace(*end) ; end++)
198 ;
199 BUG_ON(end == buf);
200 }
Greg Banks9898abb2009-02-06 12:54:26 +1100201
Jim Cromie07100be2011-12-19 17:11:09 -0500202 /* `buf' is start of word, `end' is one past its end */
Greg Banks9898abb2009-02-06 12:54:26 +1100203 if (nwords == maxwords)
204 return -EINVAL; /* ran out of words[] before bytes */
205 if (*end)
206 *end++ = '\0'; /* terminate the word */
207 words[nwords++] = buf;
208 buf = end;
209 }
Jason Barone9d376f2009-02-05 11:51:38 -0500210
211 if (verbose) {
212 int i;
Joe Perches4ad275e2011-08-11 14:36:33 -0400213 pr_info("split into words:");
Jason Barone9d376f2009-02-05 11:51:38 -0500214 for (i = 0 ; i < nwords ; i++)
Joe Perches4ad275e2011-08-11 14:36:33 -0400215 pr_cont(" \"%s\"", words[i]);
216 pr_cont("\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500217 }
218
219 return nwords;
220}
221
222/*
223 * Parse a single line number. Note that the empty string ""
224 * is treated as a special case and converted to zero, which
225 * is later treated as a "don't care" value.
226 */
227static inline int parse_lineno(const char *str, unsigned int *val)
228{
229 char *end = NULL;
230 BUG_ON(str == NULL);
231 if (*str == '\0') {
232 *val = 0;
233 return 0;
234 }
235 *val = simple_strtoul(str, &end, 10);
236 return end == NULL || end == str || *end != '\0' ? -EINVAL : 0;
237}
238
239/*
240 * Undo octal escaping in a string, inplace. This is useful to
241 * allow the user to express a query which matches a format
242 * containing embedded spaces.
243 */
244#define isodigit(c) ((c) >= '0' && (c) <= '7')
245static char *unescape(char *str)
246{
247 char *in = str;
248 char *out = str;
249
250 while (*in) {
251 if (*in == '\\') {
252 if (in[1] == '\\') {
253 *out++ = '\\';
254 in += 2;
255 continue;
256 } else if (in[1] == 't') {
257 *out++ = '\t';
258 in += 2;
259 continue;
260 } else if (in[1] == 'n') {
261 *out++ = '\n';
262 in += 2;
263 continue;
264 } else if (isodigit(in[1]) &&
265 isodigit(in[2]) &&
266 isodigit(in[3])) {
267 *out++ = ((in[1] - '0')<<6) |
268 ((in[2] - '0')<<3) |
269 (in[3] - '0');
270 in += 4;
271 continue;
272 }
273 }
274 *out++ = *in++;
275 }
276 *out = '\0';
277
278 return str;
279}
280
Jim Cromie820874c2011-12-19 17:12:49 -0500281static int check_set(const char **dest, char *src, char *name)
282{
283 int rc = 0;
284
285 if (*dest) {
286 rc = -EINVAL;
287 pr_err("match-spec:%s val:%s overridden by %s",
288 name, *dest, src);
289 }
290 *dest = src;
291 return rc;
292}
293
Jason Barone9d376f2009-02-05 11:51:38 -0500294/*
295 * Parse words[] as a ddebug query specification, which is a series
296 * of (keyword, value) pairs chosen from these possibilities:
297 *
298 * func <function-name>
299 * file <full-pathname>
300 * file <base-filename>
301 * module <module-name>
302 * format <escaped-string-to-find-in-format>
303 * line <lineno>
304 * line <first-lineno>-<last-lineno> // where either may be empty
Jim Cromie820874c2011-12-19 17:12:49 -0500305 *
306 * Only 1 of each type is allowed.
307 * Returns 0 on success, <0 on error.
Jason Barone9d376f2009-02-05 11:51:38 -0500308 */
309static int ddebug_parse_query(char *words[], int nwords,
310 struct ddebug_query *query)
311{
312 unsigned int i;
Jim Cromie820874c2011-12-19 17:12:49 -0500313 int rc;
Jason Barone9d376f2009-02-05 11:51:38 -0500314
315 /* check we have an even number of words */
316 if (nwords % 2 != 0)
317 return -EINVAL;
318 memset(query, 0, sizeof(*query));
319
320 for (i = 0 ; i < nwords ; i += 2) {
321 if (!strcmp(words[i], "func"))
Jim Cromie820874c2011-12-19 17:12:49 -0500322 rc = check_set(&query->function, words[i+1], "func");
Jason Barone9d376f2009-02-05 11:51:38 -0500323 else if (!strcmp(words[i], "file"))
Jim Cromie820874c2011-12-19 17:12:49 -0500324 rc = check_set(&query->filename, words[i+1], "file");
Jason Barone9d376f2009-02-05 11:51:38 -0500325 else if (!strcmp(words[i], "module"))
Jim Cromie820874c2011-12-19 17:12:49 -0500326 rc = check_set(&query->module, words[i+1], "module");
Jason Barone9d376f2009-02-05 11:51:38 -0500327 else if (!strcmp(words[i], "format"))
Jim Cromie820874c2011-12-19 17:12:49 -0500328 rc = check_set(&query->format, unescape(words[i+1]),
329 "format");
Jason Barone9d376f2009-02-05 11:51:38 -0500330 else if (!strcmp(words[i], "line")) {
331 char *first = words[i+1];
332 char *last = strchr(first, '-');
Jim Cromie820874c2011-12-19 17:12:49 -0500333 if (query->first_lineno || query->last_lineno) {
334 pr_err("match-spec:line given 2 times\n");
335 return -EINVAL;
336 }
Jason Barone9d376f2009-02-05 11:51:38 -0500337 if (last)
338 *last++ = '\0';
339 if (parse_lineno(first, &query->first_lineno) < 0)
340 return -EINVAL;
Jim Cromie820874c2011-12-19 17:12:49 -0500341 if (last) {
Jason Barone9d376f2009-02-05 11:51:38 -0500342 /* range <first>-<last> */
Jim Cromie820874c2011-12-19 17:12:49 -0500343 if (parse_lineno(last, &query->last_lineno)
344 < query->first_lineno) {
345 pr_err("last-line < 1st-line\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500346 return -EINVAL;
Jim Cromie820874c2011-12-19 17:12:49 -0500347 }
Jason Barone9d376f2009-02-05 11:51:38 -0500348 } else {
349 query->last_lineno = query->first_lineno;
350 }
351 } else {
Jim Cromieae27f862011-12-19 17:12:34 -0500352 pr_err("unknown keyword \"%s\"\n", words[i]);
Jason Barone9d376f2009-02-05 11:51:38 -0500353 return -EINVAL;
354 }
Jim Cromie820874c2011-12-19 17:12:49 -0500355 if (rc)
356 return rc;
Jason Barone9d376f2009-02-05 11:51:38 -0500357 }
358
359 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400360 pr_info("q->function=\"%s\" q->filename=\"%s\" "
361 "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n",
362 query->function, query->filename,
Jason Barone9d376f2009-02-05 11:51:38 -0500363 query->module, query->format, query->first_lineno,
364 query->last_lineno);
365
366 return 0;
367}
368
369/*
370 * Parse `str' as a flags specification, format [-+=][p]+.
371 * Sets up *maskp and *flagsp to be used when changing the
372 * flags fields of matched _ddebug's. Returns 0 on success
373 * or <0 on error.
374 */
375static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
376 unsigned int *maskp)
377{
378 unsigned flags = 0;
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100379 int op = '=', i;
Jason Barone9d376f2009-02-05 11:51:38 -0500380
381 switch (*str) {
382 case '+':
383 case '-':
384 case '=':
385 op = *str++;
386 break;
387 default:
388 return -EINVAL;
389 }
390 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400391 pr_info("op='%c'\n", op);
Jason Barone9d376f2009-02-05 11:51:38 -0500392
393 for ( ; *str ; ++str) {
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100394 for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) {
395 if (*str == opt_array[i].opt_char) {
396 flags |= opt_array[i].flag;
397 break;
398 }
Jason Barone9d376f2009-02-05 11:51:38 -0500399 }
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100400 if (i < 0)
401 return -EINVAL;
Jason Barone9d376f2009-02-05 11:51:38 -0500402 }
Jason Barone9d376f2009-02-05 11:51:38 -0500403 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400404 pr_info("flags=0x%x\n", flags);
Jason Barone9d376f2009-02-05 11:51:38 -0500405
406 /* calculate final *flagsp, *maskp according to mask and op */
407 switch (op) {
408 case '=':
409 *maskp = 0;
410 *flagsp = flags;
411 break;
412 case '+':
413 *maskp = ~0U;
414 *flagsp = flags;
415 break;
416 case '-':
417 *maskp = ~flags;
418 *flagsp = 0;
419 break;
420 }
421 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400422 pr_info("*flagsp=0x%x *maskp=0x%x\n", *flagsp, *maskp);
Jason Barone9d376f2009-02-05 11:51:38 -0500423 return 0;
424}
425
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200426static int ddebug_exec_query(char *query_string)
427{
428 unsigned int flags = 0, mask = 0;
429 struct ddebug_query query;
430#define MAXWORDS 9
431 int nwords;
432 char *words[MAXWORDS];
433
434 nwords = ddebug_tokenize(query_string, words, MAXWORDS);
435 if (nwords <= 0)
436 return -EINVAL;
437 if (ddebug_parse_query(words, nwords-1, &query))
438 return -EINVAL;
439 if (ddebug_parse_flags(words[nwords-1], &flags, &mask))
440 return -EINVAL;
441
442 /* actually go and implement the change */
443 ddebug_change(&query, flags, mask);
444 return 0;
445}
446
Jason Baron431625d2011-10-04 14:13:19 -0700447#define PREFIX_SIZE 64
448
449static int remaining(int wrote)
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100450{
Jason Baron431625d2011-10-04 14:13:19 -0700451 if (PREFIX_SIZE - wrote > 0)
452 return PREFIX_SIZE - wrote;
453 return 0;
454}
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100455
Jason Baron431625d2011-10-04 14:13:19 -0700456static char *dynamic_emit_prefix(const struct _ddebug *desc, char *buf)
457{
458 int pos_after_tid;
459 int pos = 0;
460
461 pos += snprintf(buf + pos, remaining(pos), "%s", KERN_DEBUG);
462 if (desc->flags & _DPRINTK_FLAGS_INCL_TID) {
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100463 if (in_interrupt())
Jason Baron431625d2011-10-04 14:13:19 -0700464 pos += snprintf(buf + pos, remaining(pos), "%s ",
465 "<intr>");
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100466 else
Jason Baron431625d2011-10-04 14:13:19 -0700467 pos += snprintf(buf + pos, remaining(pos), "[%d] ",
468 task_pid_vnr(current));
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100469 }
Jason Baron431625d2011-10-04 14:13:19 -0700470 pos_after_tid = pos;
471 if (desc->flags & _DPRINTK_FLAGS_INCL_MODNAME)
472 pos += snprintf(buf + pos, remaining(pos), "%s:",
473 desc->modname);
474 if (desc->flags & _DPRINTK_FLAGS_INCL_FUNCNAME)
475 pos += snprintf(buf + pos, remaining(pos), "%s:",
476 desc->function);
477 if (desc->flags & _DPRINTK_FLAGS_INCL_LINENO)
Jim Cromie07100be2011-12-19 17:11:09 -0500478 pos += snprintf(buf + pos, remaining(pos), "%d:",
479 desc->lineno);
Jason Baron431625d2011-10-04 14:13:19 -0700480 if (pos - pos_after_tid)
481 pos += snprintf(buf + pos, remaining(pos), " ");
482 if (pos >= PREFIX_SIZE)
483 buf[PREFIX_SIZE - 1] = '\0';
Joe Perches6c2140e2011-08-11 14:36:25 -0400484
Jason Baron431625d2011-10-04 14:13:19 -0700485 return buf;
Joe Perches6c2140e2011-08-11 14:36:25 -0400486}
487
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100488int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
489{
490 va_list args;
491 int res;
Jason Baron431625d2011-10-04 14:13:19 -0700492 struct va_format vaf;
493 char buf[PREFIX_SIZE];
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100494
495 BUG_ON(!descriptor);
496 BUG_ON(!fmt);
497
498 va_start(args, fmt);
Jason Baron431625d2011-10-04 14:13:19 -0700499 vaf.fmt = fmt;
500 vaf.va = &args;
501 res = printk("%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf);
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100502 va_end(args);
503
504 return res;
505}
506EXPORT_SYMBOL(__dynamic_pr_debug);
507
Joe Perchescbc46632011-08-11 14:36:21 -0400508int __dynamic_dev_dbg(struct _ddebug *descriptor,
509 const struct device *dev, const char *fmt, ...)
510{
511 struct va_format vaf;
512 va_list args;
513 int res;
Jason Baron431625d2011-10-04 14:13:19 -0700514 char buf[PREFIX_SIZE];
Joe Perchescbc46632011-08-11 14:36:21 -0400515
516 BUG_ON(!descriptor);
517 BUG_ON(!fmt);
518
519 va_start(args, fmt);
Joe Perchescbc46632011-08-11 14:36:21 -0400520 vaf.fmt = fmt;
521 vaf.va = &args;
Jason Baron431625d2011-10-04 14:13:19 -0700522 res = __dev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf);
Joe Perchescbc46632011-08-11 14:36:21 -0400523 va_end(args);
524
525 return res;
526}
527EXPORT_SYMBOL(__dynamic_dev_dbg);
528
Jason Baron0feefd92011-10-04 14:13:22 -0700529#ifdef CONFIG_NET
530
Jason Baronffa10cb2011-08-11 14:36:48 -0400531int __dynamic_netdev_dbg(struct _ddebug *descriptor,
532 const struct net_device *dev, const char *fmt, ...)
533{
534 struct va_format vaf;
535 va_list args;
536 int res;
Jason Baron431625d2011-10-04 14:13:19 -0700537 char buf[PREFIX_SIZE];
Jason Baronffa10cb2011-08-11 14:36:48 -0400538
539 BUG_ON(!descriptor);
540 BUG_ON(!fmt);
541
542 va_start(args, fmt);
Jason Baronffa10cb2011-08-11 14:36:48 -0400543 vaf.fmt = fmt;
544 vaf.va = &args;
Jason Baron431625d2011-10-04 14:13:19 -0700545 res = __netdev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf);
Jason Baronffa10cb2011-08-11 14:36:48 -0400546 va_end(args);
547
548 return res;
549}
550EXPORT_SYMBOL(__dynamic_netdev_dbg);
551
Jason Baron0feefd92011-10-04 14:13:22 -0700552#endif
553
Jim Cromiebc757f62011-12-19 17:12:29 -0500554#define DDEBUG_STRING_SIZE 1024
555static __initdata char ddebug_setup_string[DDEBUG_STRING_SIZE];
556
Thomas Renningera648ec02010-08-06 16:11:02 +0200557static __init int ddebug_setup_query(char *str)
558{
Jim Cromiebc757f62011-12-19 17:12:29 -0500559 if (strlen(str) >= DDEBUG_STRING_SIZE) {
Joe Perches4ad275e2011-08-11 14:36:33 -0400560 pr_warn("ddebug boot param string too large\n");
Thomas Renningera648ec02010-08-06 16:11:02 +0200561 return 0;
562 }
Jim Cromiebc757f62011-12-19 17:12:29 -0500563 strlcpy(ddebug_setup_string, str, DDEBUG_STRING_SIZE);
Thomas Renningera648ec02010-08-06 16:11:02 +0200564 return 1;
565}
566
567__setup("ddebug_query=", ddebug_setup_query);
568
Jason Barone9d376f2009-02-05 11:51:38 -0500569/*
570 * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the
571 * command text from userspace, parses and executes it.
572 */
573static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
574 size_t len, loff_t *offp)
575{
Jason Barone9d376f2009-02-05 11:51:38 -0500576 char tmpbuf[256];
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200577 int ret;
Jason Barone9d376f2009-02-05 11:51:38 -0500578
579 if (len == 0)
580 return 0;
581 /* we don't check *offp -- multiple writes() are allowed */
582 if (len > sizeof(tmpbuf)-1)
583 return -E2BIG;
584 if (copy_from_user(tmpbuf, ubuf, len))
585 return -EFAULT;
586 tmpbuf[len] = '\0';
587 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400588 pr_info("read %d bytes from userspace\n", (int)len);
Jason Barone9d376f2009-02-05 11:51:38 -0500589
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200590 ret = ddebug_exec_query(tmpbuf);
591 if (ret)
592 return ret;
Jason Barone9d376f2009-02-05 11:51:38 -0500593
594 *offp += len;
595 return len;
596}
597
598/*
599 * Set the iterator to point to the first _ddebug object
600 * and return a pointer to that first object. Returns
601 * NULL if there are no _ddebugs at all.
602 */
603static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
604{
605 if (list_empty(&ddebug_tables)) {
606 iter->table = NULL;
607 iter->idx = 0;
608 return NULL;
609 }
610 iter->table = list_entry(ddebug_tables.next,
611 struct ddebug_table, link);
612 iter->idx = 0;
613 return &iter->table->ddebugs[iter->idx];
614}
615
616/*
617 * Advance the iterator to point to the next _ddebug
618 * object from the one the iterator currently points at,
619 * and returns a pointer to the new _ddebug. Returns
620 * NULL if the iterator has seen all the _ddebugs.
621 */
622static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
623{
624 if (iter->table == NULL)
625 return NULL;
626 if (++iter->idx == iter->table->num_ddebugs) {
627 /* iterate to next table */
628 iter->idx = 0;
629 if (list_is_last(&iter->table->link, &ddebug_tables)) {
630 iter->table = NULL;
631 return NULL;
632 }
633 iter->table = list_entry(iter->table->link.next,
634 struct ddebug_table, link);
635 }
636 return &iter->table->ddebugs[iter->idx];
637}
638
639/*
640 * Seq_ops start method. Called at the start of every
641 * read() call from userspace. Takes the ddebug_lock and
642 * seeks the seq_file's iterator to the given position.
643 */
644static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
645{
646 struct ddebug_iter *iter = m->private;
647 struct _ddebug *dp;
648 int n = *pos;
649
650 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400651 pr_info("called m=%p *pos=%lld\n", m, (unsigned long long)*pos);
Jason Barone9d376f2009-02-05 11:51:38 -0500652
653 mutex_lock(&ddebug_lock);
654
655 if (!n)
656 return SEQ_START_TOKEN;
657 if (n < 0)
658 return NULL;
659 dp = ddebug_iter_first(iter);
660 while (dp != NULL && --n > 0)
661 dp = ddebug_iter_next(iter);
662 return dp;
663}
664
665/*
666 * Seq_ops next method. Called several times within a read()
667 * call from userspace, with ddebug_lock held. Walks to the
668 * next _ddebug object with a special case for the header line.
669 */
670static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
671{
672 struct ddebug_iter *iter = m->private;
673 struct _ddebug *dp;
674
675 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400676 pr_info("called m=%p p=%p *pos=%lld\n",
677 m, p, (unsigned long long)*pos);
Jason Barone9d376f2009-02-05 11:51:38 -0500678
679 if (p == SEQ_START_TOKEN)
680 dp = ddebug_iter_first(iter);
681 else
682 dp = ddebug_iter_next(iter);
683 ++*pos;
684 return dp;
685}
686
687/*
688 * Seq_ops show method. Called several times within a read()
689 * call from userspace, with ddebug_lock held. Formats the
690 * current _ddebug as a single human-readable line, with a
691 * special case for the header line.
692 */
693static int ddebug_proc_show(struct seq_file *m, void *p)
694{
695 struct ddebug_iter *iter = m->private;
696 struct _ddebug *dp = p;
Jim Cromie5ca7d2a2011-12-19 17:12:44 -0500697 char flagsbuf[10];
Jason Barone9d376f2009-02-05 11:51:38 -0500698
699 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400700 pr_info("called m=%p p=%p\n", m, p);
Jason Barone9d376f2009-02-05 11:51:38 -0500701
702 if (p == SEQ_START_TOKEN) {
703 seq_puts(m,
704 "# filename:lineno [module]function flags format\n");
705 return 0;
706 }
707
Jim Cromie5ca7d2a2011-12-19 17:12:44 -0500708 seq_printf(m, "%s:%u [%s]%s =%s \"",
709 dp->filename, dp->lineno,
710 iter->table->mod_name, dp->function,
711 ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
Jason Barone9d376f2009-02-05 11:51:38 -0500712 seq_escape(m, dp->format, "\t\r\n\"");
713 seq_puts(m, "\"\n");
714
715 return 0;
716}
717
718/*
719 * Seq_ops stop method. Called at the end of each read()
720 * call from userspace. Drops ddebug_lock.
721 */
722static void ddebug_proc_stop(struct seq_file *m, void *p)
723{
724 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400725 pr_info("called m=%p p=%p\n", m, p);
Jason Barone9d376f2009-02-05 11:51:38 -0500726 mutex_unlock(&ddebug_lock);
727}
728
729static const struct seq_operations ddebug_proc_seqops = {
730 .start = ddebug_proc_start,
731 .next = ddebug_proc_next,
732 .show = ddebug_proc_show,
733 .stop = ddebug_proc_stop
734};
735
736/*
Jim Cromie07100be2011-12-19 17:11:09 -0500737 * File_ops->open method for <debugfs>/dynamic_debug/control. Does
738 * the seq_file setup dance, and also creates an iterator to walk the
739 * _ddebugs. Note that we create a seq_file always, even for O_WRONLY
740 * files where it's not needed, as doing so simplifies the ->release
741 * method.
Jason Barone9d376f2009-02-05 11:51:38 -0500742 */
743static int ddebug_proc_open(struct inode *inode, struct file *file)
744{
745 struct ddebug_iter *iter;
746 int err;
747
748 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400749 pr_info("called\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500750
751 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
752 if (iter == NULL)
753 return -ENOMEM;
754
755 err = seq_open(file, &ddebug_proc_seqops);
756 if (err) {
757 kfree(iter);
758 return err;
759 }
760 ((struct seq_file *) file->private_data)->private = iter;
761 return 0;
762}
763
764static const struct file_operations ddebug_proc_fops = {
765 .owner = THIS_MODULE,
766 .open = ddebug_proc_open,
767 .read = seq_read,
768 .llseek = seq_lseek,
769 .release = seq_release_private,
770 .write = ddebug_proc_write
771};
772
773/*
774 * Allocate a new ddebug_table for the given module
775 * and add it to the global list.
776 */
777int ddebug_add_module(struct _ddebug *tab, unsigned int n,
778 const char *name)
779{
780 struct ddebug_table *dt;
781 char *new_name;
782
783 dt = kzalloc(sizeof(*dt), GFP_KERNEL);
784 if (dt == NULL)
785 return -ENOMEM;
786 new_name = kstrdup(name, GFP_KERNEL);
787 if (new_name == NULL) {
788 kfree(dt);
789 return -ENOMEM;
790 }
791 dt->mod_name = new_name;
792 dt->num_ddebugs = n;
Jason Barone9d376f2009-02-05 11:51:38 -0500793 dt->ddebugs = tab;
794
795 mutex_lock(&ddebug_lock);
796 list_add_tail(&dt->link, &ddebug_tables);
797 mutex_unlock(&ddebug_lock);
798
799 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400800 pr_info("%u debug prints in module %s\n", n, dt->mod_name);
Jason Barone9d376f2009-02-05 11:51:38 -0500801 return 0;
802}
803EXPORT_SYMBOL_GPL(ddebug_add_module);
804
805static void ddebug_table_free(struct ddebug_table *dt)
806{
807 list_del_init(&dt->link);
808 kfree(dt->mod_name);
809 kfree(dt);
810}
811
812/*
813 * Called in response to a module being unloaded. Removes
814 * any ddebug_table's which point at the module.
815 */
Yehuda Sadehff49d742010-07-03 13:07:35 +1000816int ddebug_remove_module(const char *mod_name)
Jason Barone9d376f2009-02-05 11:51:38 -0500817{
818 struct ddebug_table *dt, *nextdt;
819 int ret = -ENOENT;
820
821 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400822 pr_info("removing module \"%s\"\n", mod_name);
Jason Barone9d376f2009-02-05 11:51:38 -0500823
824 mutex_lock(&ddebug_lock);
825 list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
826 if (!strcmp(dt->mod_name, mod_name)) {
827 ddebug_table_free(dt);
828 ret = 0;
829 }
830 }
831 mutex_unlock(&ddebug_lock);
832 return ret;
833}
834EXPORT_SYMBOL_GPL(ddebug_remove_module);
835
836static void ddebug_remove_all_tables(void)
837{
838 mutex_lock(&ddebug_lock);
839 while (!list_empty(&ddebug_tables)) {
840 struct ddebug_table *dt = list_entry(ddebug_tables.next,
841 struct ddebug_table,
842 link);
843 ddebug_table_free(dt);
844 }
845 mutex_unlock(&ddebug_lock);
846}
847
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200848static __initdata int ddebug_init_success;
849
850static int __init dynamic_debug_init_debugfs(void)
Jason Barone9d376f2009-02-05 11:51:38 -0500851{
852 struct dentry *dir, *file;
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200853
854 if (!ddebug_init_success)
855 return -ENODEV;
Jason Barone9d376f2009-02-05 11:51:38 -0500856
857 dir = debugfs_create_dir("dynamic_debug", NULL);
858 if (!dir)
859 return -ENOMEM;
860 file = debugfs_create_file("control", 0644, dir, NULL,
861 &ddebug_proc_fops);
862 if (!file) {
863 debugfs_remove(dir);
864 return -ENOMEM;
865 }
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200866 return 0;
867}
868
869static int __init dynamic_debug_init(void)
870{
871 struct _ddebug *iter, *iter_start;
872 const char *modname = NULL;
873 int ret = 0;
874 int n = 0;
875
Jim Cromieb5b78f82011-12-19 17:12:54 -0500876 if (__start___verbose == __stop___verbose) {
877 pr_warn("_ddebug table is empty in a "
878 "CONFIG_DYNAMIC_DEBUG build");
879 return 1;
Jason Barone9d376f2009-02-05 11:51:38 -0500880 }
Jim Cromieb5b78f82011-12-19 17:12:54 -0500881 iter = __start___verbose;
882 modname = iter->modname;
883 iter_start = iter;
884 for (; iter < __stop___verbose; iter++) {
885 if (strcmp(modname, iter->modname)) {
886 ret = ddebug_add_module(iter_start, n, modname);
887 if (ret)
888 goto out_free;
889 n = 0;
890 modname = iter->modname;
891 iter_start = iter;
892 }
893 n++;
894 }
895 ret = ddebug_add_module(iter_start, n, modname);
896 if (ret)
897 goto out_free;
Thomas Renningera648ec02010-08-06 16:11:02 +0200898
899 /* ddebug_query boot param got passed -> set it up */
900 if (ddebug_setup_string[0] != '\0') {
901 ret = ddebug_exec_query(ddebug_setup_string);
902 if (ret)
Joe Perches4ad275e2011-08-11 14:36:33 -0400903 pr_warn("Invalid ddebug boot param %s",
904 ddebug_setup_string);
Thomas Renningera648ec02010-08-06 16:11:02 +0200905 else
906 pr_info("ddebug initialized with string %s",
907 ddebug_setup_string);
Jason Barone9d376f2009-02-05 11:51:38 -0500908 }
Thomas Renningera648ec02010-08-06 16:11:02 +0200909
Jason Barone9d376f2009-02-05 11:51:38 -0500910out_free:
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200911 if (ret)
Jason Barone9d376f2009-02-05 11:51:38 -0500912 ddebug_remove_all_tables();
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200913 else
914 ddebug_init_success = 1;
Jason Barone9d376f2009-02-05 11:51:38 -0500915 return 0;
916}
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200917/* Allow early initialization for boot messages via boot param */
918arch_initcall(dynamic_debug_init);
919/* Debugfs setup must be done later */
920module_init(dynamic_debug_init_debugfs);