blob: 6fc8622f0a8343aafa1c976c8b8819d586939ee2 [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' },
78};
79
Jason Barone9d376f2009-02-05 11:51:38 -050080/* format a string into buf[] which describes the _ddebug's flags */
81static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,
82 size_t maxlen)
83{
84 char *p = buf;
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +010085 int i;
Jason Barone9d376f2009-02-05 11:51:38 -050086
87 BUG_ON(maxlen < 4);
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +010088 for (i = 0; i < ARRAY_SIZE(opt_array); ++i)
89 if (dp->flags & opt_array[i].flag)
90 *p++ = opt_array[i].opt_char;
Jason Barone9d376f2009-02-05 11:51:38 -050091 if (p == buf)
92 *p++ = '-';
93 *p = '\0';
94
95 return buf;
96}
97
98/*
Jason Barone9d376f2009-02-05 11:51:38 -050099 * Search the tables for _ddebug's which match the given
100 * `query' and apply the `flags' and `mask' to them. Tells
101 * the user which ddebug's were changed, or whether none
102 * were matched.
103 */
104static void ddebug_change(const struct ddebug_query *query,
105 unsigned int flags, unsigned int mask)
106{
107 int i;
108 struct ddebug_table *dt;
109 unsigned int newflags;
110 unsigned int nfound = 0;
111 char flagbuf[8];
112
113 /* search for matching ddebugs */
114 mutex_lock(&ddebug_lock);
115 list_for_each_entry(dt, &ddebug_tables, link) {
116
117 /* match against the module name */
118 if (query->module != NULL &&
119 strcmp(query->module, dt->mod_name))
120 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 */
126 if (query->filename != NULL &&
127 strcmp(query->filename, dp->filename) &&
128 strcmp(query->filename, basename(dp->filename)))
129 continue;
130
131 /* match against the function */
132 if (query->function != NULL &&
133 strcmp(query->function, dp->function))
134 continue;
135
136 /* match against the format */
137 if (query->format != NULL &&
138 strstr(dp->format, query->format) == NULL)
139 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)
Joe Perches4ad275e2011-08-11 14:36:33 -0400156 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 */
186
Jim Cromie07100be2011-12-19 17:11:09 -0500187 /* find `end' of word, whitespace separated or quoted */
Greg Banks9898abb2009-02-06 12:54:26 +1100188 if (*buf == '"' || *buf == '\'') {
189 int quote = *buf++;
190 for (end = buf ; *end && *end != quote ; end++)
191 ;
192 if (!*end)
193 return -EINVAL; /* unclosed quote */
194 } else {
195 for (end = buf ; *end && !isspace(*end) ; end++)
196 ;
197 BUG_ON(end == buf);
198 }
Greg Banks9898abb2009-02-06 12:54:26 +1100199
Jim Cromie07100be2011-12-19 17:11:09 -0500200 /* `buf' is start of word, `end' is one past its end */
Greg Banks9898abb2009-02-06 12:54:26 +1100201 if (nwords == maxwords)
202 return -EINVAL; /* ran out of words[] before bytes */
203 if (*end)
204 *end++ = '\0'; /* terminate the word */
205 words[nwords++] = buf;
206 buf = end;
207 }
Jason Barone9d376f2009-02-05 11:51:38 -0500208
209 if (verbose) {
210 int i;
Joe Perches4ad275e2011-08-11 14:36:33 -0400211 pr_info("split into words:");
Jason Barone9d376f2009-02-05 11:51:38 -0500212 for (i = 0 ; i < nwords ; i++)
Joe Perches4ad275e2011-08-11 14:36:33 -0400213 pr_cont(" \"%s\"", words[i]);
214 pr_cont("\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500215 }
216
217 return nwords;
218}
219
220/*
221 * Parse a single line number. Note that the empty string ""
222 * is treated as a special case and converted to zero, which
223 * is later treated as a "don't care" value.
224 */
225static inline int parse_lineno(const char *str, unsigned int *val)
226{
227 char *end = NULL;
228 BUG_ON(str == NULL);
229 if (*str == '\0') {
230 *val = 0;
231 return 0;
232 }
233 *val = simple_strtoul(str, &end, 10);
234 return end == NULL || end == str || *end != '\0' ? -EINVAL : 0;
235}
236
237/*
238 * Undo octal escaping in a string, inplace. This is useful to
239 * allow the user to express a query which matches a format
240 * containing embedded spaces.
241 */
242#define isodigit(c) ((c) >= '0' && (c) <= '7')
243static char *unescape(char *str)
244{
245 char *in = str;
246 char *out = str;
247
248 while (*in) {
249 if (*in == '\\') {
250 if (in[1] == '\\') {
251 *out++ = '\\';
252 in += 2;
253 continue;
254 } else if (in[1] == 't') {
255 *out++ = '\t';
256 in += 2;
257 continue;
258 } else if (in[1] == 'n') {
259 *out++ = '\n';
260 in += 2;
261 continue;
262 } else if (isodigit(in[1]) &&
263 isodigit(in[2]) &&
264 isodigit(in[3])) {
265 *out++ = ((in[1] - '0')<<6) |
266 ((in[2] - '0')<<3) |
267 (in[3] - '0');
268 in += 4;
269 continue;
270 }
271 }
272 *out++ = *in++;
273 }
274 *out = '\0';
275
276 return str;
277}
278
279/*
280 * Parse words[] as a ddebug query specification, which is a series
281 * of (keyword, value) pairs chosen from these possibilities:
282 *
283 * func <function-name>
284 * file <full-pathname>
285 * file <base-filename>
286 * module <module-name>
287 * format <escaped-string-to-find-in-format>
288 * line <lineno>
289 * line <first-lineno>-<last-lineno> // where either may be empty
290 */
291static int ddebug_parse_query(char *words[], int nwords,
292 struct ddebug_query *query)
293{
294 unsigned int i;
295
296 /* check we have an even number of words */
297 if (nwords % 2 != 0)
298 return -EINVAL;
299 memset(query, 0, sizeof(*query));
300
301 for (i = 0 ; i < nwords ; i += 2) {
302 if (!strcmp(words[i], "func"))
303 query->function = words[i+1];
304 else if (!strcmp(words[i], "file"))
305 query->filename = words[i+1];
306 else if (!strcmp(words[i], "module"))
307 query->module = words[i+1];
308 else if (!strcmp(words[i], "format"))
309 query->format = unescape(words[i+1]);
310 else if (!strcmp(words[i], "line")) {
311 char *first = words[i+1];
312 char *last = strchr(first, '-');
313 if (last)
314 *last++ = '\0';
315 if (parse_lineno(first, &query->first_lineno) < 0)
316 return -EINVAL;
317 if (last != NULL) {
318 /* range <first>-<last> */
319 if (parse_lineno(last, &query->last_lineno) < 0)
320 return -EINVAL;
321 } else {
322 query->last_lineno = query->first_lineno;
323 }
324 } else {
325 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400326 pr_err("unknown keyword \"%s\"\n", words[i]);
Jason Barone9d376f2009-02-05 11:51:38 -0500327 return -EINVAL;
328 }
329 }
330
331 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400332 pr_info("q->function=\"%s\" q->filename=\"%s\" "
333 "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n",
334 query->function, query->filename,
Jason Barone9d376f2009-02-05 11:51:38 -0500335 query->module, query->format, query->first_lineno,
336 query->last_lineno);
337
338 return 0;
339}
340
341/*
342 * Parse `str' as a flags specification, format [-+=][p]+.
343 * Sets up *maskp and *flagsp to be used when changing the
344 * flags fields of matched _ddebug's. Returns 0 on success
345 * or <0 on error.
346 */
347static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
348 unsigned int *maskp)
349{
350 unsigned flags = 0;
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100351 int op = '=', i;
Jason Barone9d376f2009-02-05 11:51:38 -0500352
353 switch (*str) {
354 case '+':
355 case '-':
356 case '=':
357 op = *str++;
358 break;
359 default:
360 return -EINVAL;
361 }
362 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400363 pr_info("op='%c'\n", op);
Jason Barone9d376f2009-02-05 11:51:38 -0500364
365 for ( ; *str ; ++str) {
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100366 for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) {
367 if (*str == opt_array[i].opt_char) {
368 flags |= opt_array[i].flag;
369 break;
370 }
Jason Barone9d376f2009-02-05 11:51:38 -0500371 }
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100372 if (i < 0)
373 return -EINVAL;
Jason Barone9d376f2009-02-05 11:51:38 -0500374 }
375 if (flags == 0)
376 return -EINVAL;
377 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400378 pr_info("flags=0x%x\n", flags);
Jason Barone9d376f2009-02-05 11:51:38 -0500379
380 /* calculate final *flagsp, *maskp according to mask and op */
381 switch (op) {
382 case '=':
383 *maskp = 0;
384 *flagsp = flags;
385 break;
386 case '+':
387 *maskp = ~0U;
388 *flagsp = flags;
389 break;
390 case '-':
391 *maskp = ~flags;
392 *flagsp = 0;
393 break;
394 }
395 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400396 pr_info("*flagsp=0x%x *maskp=0x%x\n", *flagsp, *maskp);
Jason Barone9d376f2009-02-05 11:51:38 -0500397 return 0;
398}
399
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200400static int ddebug_exec_query(char *query_string)
401{
402 unsigned int flags = 0, mask = 0;
403 struct ddebug_query query;
404#define MAXWORDS 9
405 int nwords;
406 char *words[MAXWORDS];
407
408 nwords = ddebug_tokenize(query_string, words, MAXWORDS);
409 if (nwords <= 0)
410 return -EINVAL;
411 if (ddebug_parse_query(words, nwords-1, &query))
412 return -EINVAL;
413 if (ddebug_parse_flags(words[nwords-1], &flags, &mask))
414 return -EINVAL;
415
416 /* actually go and implement the change */
417 ddebug_change(&query, flags, mask);
418 return 0;
419}
420
Jason Baron431625d2011-10-04 14:13:19 -0700421#define PREFIX_SIZE 64
422
423static int remaining(int wrote)
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100424{
Jason Baron431625d2011-10-04 14:13:19 -0700425 if (PREFIX_SIZE - wrote > 0)
426 return PREFIX_SIZE - wrote;
427 return 0;
428}
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100429
Jason Baron431625d2011-10-04 14:13:19 -0700430static char *dynamic_emit_prefix(const struct _ddebug *desc, char *buf)
431{
432 int pos_after_tid;
433 int pos = 0;
434
435 pos += snprintf(buf + pos, remaining(pos), "%s", KERN_DEBUG);
436 if (desc->flags & _DPRINTK_FLAGS_INCL_TID) {
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100437 if (in_interrupt())
Jason Baron431625d2011-10-04 14:13:19 -0700438 pos += snprintf(buf + pos, remaining(pos), "%s ",
439 "<intr>");
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100440 else
Jason Baron431625d2011-10-04 14:13:19 -0700441 pos += snprintf(buf + pos, remaining(pos), "[%d] ",
442 task_pid_vnr(current));
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100443 }
Jason Baron431625d2011-10-04 14:13:19 -0700444 pos_after_tid = pos;
445 if (desc->flags & _DPRINTK_FLAGS_INCL_MODNAME)
446 pos += snprintf(buf + pos, remaining(pos), "%s:",
447 desc->modname);
448 if (desc->flags & _DPRINTK_FLAGS_INCL_FUNCNAME)
449 pos += snprintf(buf + pos, remaining(pos), "%s:",
450 desc->function);
451 if (desc->flags & _DPRINTK_FLAGS_INCL_LINENO)
Jim Cromie07100be2011-12-19 17:11:09 -0500452 pos += snprintf(buf + pos, remaining(pos), "%d:",
453 desc->lineno);
Jason Baron431625d2011-10-04 14:13:19 -0700454 if (pos - pos_after_tid)
455 pos += snprintf(buf + pos, remaining(pos), " ");
456 if (pos >= PREFIX_SIZE)
457 buf[PREFIX_SIZE - 1] = '\0';
Joe Perches6c2140e2011-08-11 14:36:25 -0400458
Jason Baron431625d2011-10-04 14:13:19 -0700459 return buf;
Joe Perches6c2140e2011-08-11 14:36:25 -0400460}
461
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100462int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
463{
464 va_list args;
465 int res;
Jason Baron431625d2011-10-04 14:13:19 -0700466 struct va_format vaf;
467 char buf[PREFIX_SIZE];
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100468
469 BUG_ON(!descriptor);
470 BUG_ON(!fmt);
471
472 va_start(args, fmt);
Jason Baron431625d2011-10-04 14:13:19 -0700473 vaf.fmt = fmt;
474 vaf.va = &args;
475 res = printk("%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf);
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100476 va_end(args);
477
478 return res;
479}
480EXPORT_SYMBOL(__dynamic_pr_debug);
481
Joe Perchescbc46632011-08-11 14:36:21 -0400482int __dynamic_dev_dbg(struct _ddebug *descriptor,
483 const struct device *dev, const char *fmt, ...)
484{
485 struct va_format vaf;
486 va_list args;
487 int res;
Jason Baron431625d2011-10-04 14:13:19 -0700488 char buf[PREFIX_SIZE];
Joe Perchescbc46632011-08-11 14:36:21 -0400489
490 BUG_ON(!descriptor);
491 BUG_ON(!fmt);
492
493 va_start(args, fmt);
Joe Perchescbc46632011-08-11 14:36:21 -0400494 vaf.fmt = fmt;
495 vaf.va = &args;
Jason Baron431625d2011-10-04 14:13:19 -0700496 res = __dev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf);
Joe Perchescbc46632011-08-11 14:36:21 -0400497 va_end(args);
498
499 return res;
500}
501EXPORT_SYMBOL(__dynamic_dev_dbg);
502
Jason Baron0feefd92011-10-04 14:13:22 -0700503#ifdef CONFIG_NET
504
Jason Baronffa10cb2011-08-11 14:36:48 -0400505int __dynamic_netdev_dbg(struct _ddebug *descriptor,
506 const struct net_device *dev, const char *fmt, ...)
507{
508 struct va_format vaf;
509 va_list args;
510 int res;
Jason Baron431625d2011-10-04 14:13:19 -0700511 char buf[PREFIX_SIZE];
Jason Baronffa10cb2011-08-11 14:36:48 -0400512
513 BUG_ON(!descriptor);
514 BUG_ON(!fmt);
515
516 va_start(args, fmt);
Jason Baronffa10cb2011-08-11 14:36:48 -0400517 vaf.fmt = fmt;
518 vaf.va = &args;
Jason Baron431625d2011-10-04 14:13:19 -0700519 res = __netdev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf);
Jason Baronffa10cb2011-08-11 14:36:48 -0400520 va_end(args);
521
522 return res;
523}
524EXPORT_SYMBOL(__dynamic_netdev_dbg);
525
Jason Baron0feefd92011-10-04 14:13:22 -0700526#endif
527
Jim Cromiebc757f62011-12-19 17:12:29 -0500528#define DDEBUG_STRING_SIZE 1024
529static __initdata char ddebug_setup_string[DDEBUG_STRING_SIZE];
530
Thomas Renningera648ec02010-08-06 16:11:02 +0200531static __init int ddebug_setup_query(char *str)
532{
Jim Cromiebc757f62011-12-19 17:12:29 -0500533 if (strlen(str) >= DDEBUG_STRING_SIZE) {
Joe Perches4ad275e2011-08-11 14:36:33 -0400534 pr_warn("ddebug boot param string too large\n");
Thomas Renningera648ec02010-08-06 16:11:02 +0200535 return 0;
536 }
Jim Cromiebc757f62011-12-19 17:12:29 -0500537 strlcpy(ddebug_setup_string, str, DDEBUG_STRING_SIZE);
Thomas Renningera648ec02010-08-06 16:11:02 +0200538 return 1;
539}
540
541__setup("ddebug_query=", ddebug_setup_query);
542
Jason Barone9d376f2009-02-05 11:51:38 -0500543/*
544 * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the
545 * command text from userspace, parses and executes it.
546 */
547static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
548 size_t len, loff_t *offp)
549{
Jason Barone9d376f2009-02-05 11:51:38 -0500550 char tmpbuf[256];
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200551 int ret;
Jason Barone9d376f2009-02-05 11:51:38 -0500552
553 if (len == 0)
554 return 0;
555 /* we don't check *offp -- multiple writes() are allowed */
556 if (len > sizeof(tmpbuf)-1)
557 return -E2BIG;
558 if (copy_from_user(tmpbuf, ubuf, len))
559 return -EFAULT;
560 tmpbuf[len] = '\0';
561 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400562 pr_info("read %d bytes from userspace\n", (int)len);
Jason Barone9d376f2009-02-05 11:51:38 -0500563
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200564 ret = ddebug_exec_query(tmpbuf);
565 if (ret)
566 return ret;
Jason Barone9d376f2009-02-05 11:51:38 -0500567
568 *offp += len;
569 return len;
570}
571
572/*
573 * Set the iterator to point to the first _ddebug object
574 * and return a pointer to that first object. Returns
575 * NULL if there are no _ddebugs at all.
576 */
577static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
578{
579 if (list_empty(&ddebug_tables)) {
580 iter->table = NULL;
581 iter->idx = 0;
582 return NULL;
583 }
584 iter->table = list_entry(ddebug_tables.next,
585 struct ddebug_table, link);
586 iter->idx = 0;
587 return &iter->table->ddebugs[iter->idx];
588}
589
590/*
591 * Advance the iterator to point to the next _ddebug
592 * object from the one the iterator currently points at,
593 * and returns a pointer to the new _ddebug. Returns
594 * NULL if the iterator has seen all the _ddebugs.
595 */
596static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
597{
598 if (iter->table == NULL)
599 return NULL;
600 if (++iter->idx == iter->table->num_ddebugs) {
601 /* iterate to next table */
602 iter->idx = 0;
603 if (list_is_last(&iter->table->link, &ddebug_tables)) {
604 iter->table = NULL;
605 return NULL;
606 }
607 iter->table = list_entry(iter->table->link.next,
608 struct ddebug_table, link);
609 }
610 return &iter->table->ddebugs[iter->idx];
611}
612
613/*
614 * Seq_ops start method. Called at the start of every
615 * read() call from userspace. Takes the ddebug_lock and
616 * seeks the seq_file's iterator to the given position.
617 */
618static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
619{
620 struct ddebug_iter *iter = m->private;
621 struct _ddebug *dp;
622 int n = *pos;
623
624 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400625 pr_info("called m=%p *pos=%lld\n", m, (unsigned long long)*pos);
Jason Barone9d376f2009-02-05 11:51:38 -0500626
627 mutex_lock(&ddebug_lock);
628
629 if (!n)
630 return SEQ_START_TOKEN;
631 if (n < 0)
632 return NULL;
633 dp = ddebug_iter_first(iter);
634 while (dp != NULL && --n > 0)
635 dp = ddebug_iter_next(iter);
636 return dp;
637}
638
639/*
640 * Seq_ops next method. Called several times within a read()
641 * call from userspace, with ddebug_lock held. Walks to the
642 * next _ddebug object with a special case for the header line.
643 */
644static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
645{
646 struct ddebug_iter *iter = m->private;
647 struct _ddebug *dp;
648
649 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400650 pr_info("called m=%p p=%p *pos=%lld\n",
651 m, p, (unsigned long long)*pos);
Jason Barone9d376f2009-02-05 11:51:38 -0500652
653 if (p == SEQ_START_TOKEN)
654 dp = ddebug_iter_first(iter);
655 else
656 dp = ddebug_iter_next(iter);
657 ++*pos;
658 return dp;
659}
660
661/*
662 * Seq_ops show method. Called several times within a read()
663 * call from userspace, with ddebug_lock held. Formats the
664 * current _ddebug as a single human-readable line, with a
665 * special case for the header line.
666 */
667static int ddebug_proc_show(struct seq_file *m, void *p)
668{
669 struct ddebug_iter *iter = m->private;
670 struct _ddebug *dp = p;
671 char flagsbuf[8];
672
673 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400674 pr_info("called m=%p p=%p\n", m, p);
Jason Barone9d376f2009-02-05 11:51:38 -0500675
676 if (p == SEQ_START_TOKEN) {
677 seq_puts(m,
678 "# filename:lineno [module]function flags format\n");
679 return 0;
680 }
681
682 seq_printf(m, "%s:%u [%s]%s %s \"",
683 dp->filename, dp->lineno,
684 iter->table->mod_name, dp->function,
685 ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
686 seq_escape(m, dp->format, "\t\r\n\"");
687 seq_puts(m, "\"\n");
688
689 return 0;
690}
691
692/*
693 * Seq_ops stop method. Called at the end of each read()
694 * call from userspace. Drops ddebug_lock.
695 */
696static void ddebug_proc_stop(struct seq_file *m, void *p)
697{
698 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400699 pr_info("called m=%p p=%p\n", m, p);
Jason Barone9d376f2009-02-05 11:51:38 -0500700 mutex_unlock(&ddebug_lock);
701}
702
703static const struct seq_operations ddebug_proc_seqops = {
704 .start = ddebug_proc_start,
705 .next = ddebug_proc_next,
706 .show = ddebug_proc_show,
707 .stop = ddebug_proc_stop
708};
709
710/*
Jim Cromie07100be2011-12-19 17:11:09 -0500711 * File_ops->open method for <debugfs>/dynamic_debug/control. Does
712 * the seq_file setup dance, and also creates an iterator to walk the
713 * _ddebugs. Note that we create a seq_file always, even for O_WRONLY
714 * files where it's not needed, as doing so simplifies the ->release
715 * method.
Jason Barone9d376f2009-02-05 11:51:38 -0500716 */
717static int ddebug_proc_open(struct inode *inode, struct file *file)
718{
719 struct ddebug_iter *iter;
720 int err;
721
722 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400723 pr_info("called\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500724
725 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
726 if (iter == NULL)
727 return -ENOMEM;
728
729 err = seq_open(file, &ddebug_proc_seqops);
730 if (err) {
731 kfree(iter);
732 return err;
733 }
734 ((struct seq_file *) file->private_data)->private = iter;
735 return 0;
736}
737
738static const struct file_operations ddebug_proc_fops = {
739 .owner = THIS_MODULE,
740 .open = ddebug_proc_open,
741 .read = seq_read,
742 .llseek = seq_lseek,
743 .release = seq_release_private,
744 .write = ddebug_proc_write
745};
746
747/*
748 * Allocate a new ddebug_table for the given module
749 * and add it to the global list.
750 */
751int ddebug_add_module(struct _ddebug *tab, unsigned int n,
752 const char *name)
753{
754 struct ddebug_table *dt;
755 char *new_name;
756
757 dt = kzalloc(sizeof(*dt), GFP_KERNEL);
758 if (dt == NULL)
759 return -ENOMEM;
760 new_name = kstrdup(name, GFP_KERNEL);
761 if (new_name == NULL) {
762 kfree(dt);
763 return -ENOMEM;
764 }
765 dt->mod_name = new_name;
766 dt->num_ddebugs = n;
Jason Barone9d376f2009-02-05 11:51:38 -0500767 dt->ddebugs = tab;
768
769 mutex_lock(&ddebug_lock);
770 list_add_tail(&dt->link, &ddebug_tables);
771 mutex_unlock(&ddebug_lock);
772
773 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400774 pr_info("%u debug prints in module %s\n", n, dt->mod_name);
Jason Barone9d376f2009-02-05 11:51:38 -0500775 return 0;
776}
777EXPORT_SYMBOL_GPL(ddebug_add_module);
778
779static void ddebug_table_free(struct ddebug_table *dt)
780{
781 list_del_init(&dt->link);
782 kfree(dt->mod_name);
783 kfree(dt);
784}
785
786/*
787 * Called in response to a module being unloaded. Removes
788 * any ddebug_table's which point at the module.
789 */
Yehuda Sadehff49d742010-07-03 13:07:35 +1000790int ddebug_remove_module(const char *mod_name)
Jason Barone9d376f2009-02-05 11:51:38 -0500791{
792 struct ddebug_table *dt, *nextdt;
793 int ret = -ENOENT;
794
795 if (verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400796 pr_info("removing module \"%s\"\n", mod_name);
Jason Barone9d376f2009-02-05 11:51:38 -0500797
798 mutex_lock(&ddebug_lock);
799 list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
800 if (!strcmp(dt->mod_name, mod_name)) {
801 ddebug_table_free(dt);
802 ret = 0;
803 }
804 }
805 mutex_unlock(&ddebug_lock);
806 return ret;
807}
808EXPORT_SYMBOL_GPL(ddebug_remove_module);
809
810static void ddebug_remove_all_tables(void)
811{
812 mutex_lock(&ddebug_lock);
813 while (!list_empty(&ddebug_tables)) {
814 struct ddebug_table *dt = list_entry(ddebug_tables.next,
815 struct ddebug_table,
816 link);
817 ddebug_table_free(dt);
818 }
819 mutex_unlock(&ddebug_lock);
820}
821
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200822static __initdata int ddebug_init_success;
823
824static int __init dynamic_debug_init_debugfs(void)
Jason Barone9d376f2009-02-05 11:51:38 -0500825{
826 struct dentry *dir, *file;
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200827
828 if (!ddebug_init_success)
829 return -ENODEV;
Jason Barone9d376f2009-02-05 11:51:38 -0500830
831 dir = debugfs_create_dir("dynamic_debug", NULL);
832 if (!dir)
833 return -ENOMEM;
834 file = debugfs_create_file("control", 0644, dir, NULL,
835 &ddebug_proc_fops);
836 if (!file) {
837 debugfs_remove(dir);
838 return -ENOMEM;
839 }
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200840 return 0;
841}
842
843static int __init dynamic_debug_init(void)
844{
845 struct _ddebug *iter, *iter_start;
846 const char *modname = NULL;
847 int ret = 0;
848 int n = 0;
849
Jason Barone9d376f2009-02-05 11:51:38 -0500850 if (__start___verbose != __stop___verbose) {
851 iter = __start___verbose;
852 modname = iter->modname;
853 iter_start = iter;
854 for (; iter < __stop___verbose; iter++) {
855 if (strcmp(modname, iter->modname)) {
856 ret = ddebug_add_module(iter_start, n, modname);
857 if (ret)
858 goto out_free;
859 n = 0;
860 modname = iter->modname;
861 iter_start = iter;
862 }
863 n++;
864 }
865 ret = ddebug_add_module(iter_start, n, modname);
866 }
Thomas Renningera648ec02010-08-06 16:11:02 +0200867
868 /* ddebug_query boot param got passed -> set it up */
869 if (ddebug_setup_string[0] != '\0') {
870 ret = ddebug_exec_query(ddebug_setup_string);
871 if (ret)
Joe Perches4ad275e2011-08-11 14:36:33 -0400872 pr_warn("Invalid ddebug boot param %s",
873 ddebug_setup_string);
Thomas Renningera648ec02010-08-06 16:11:02 +0200874 else
875 pr_info("ddebug initialized with string %s",
876 ddebug_setup_string);
Jason Barone9d376f2009-02-05 11:51:38 -0500877 }
Thomas Renningera648ec02010-08-06 16:11:02 +0200878
Jason Barone9d376f2009-02-05 11:51:38 -0500879out_free:
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200880 if (ret)
Jason Barone9d376f2009-02-05 11:51:38 -0500881 ddebug_remove_all_tables();
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200882 else
883 ddebug_init_success = 1;
Jason Barone9d376f2009-02-05 11:51:38 -0500884 return 0;
885}
Thomas Renninger6a5c0832010-08-06 16:11:03 +0200886/* Allow early initialization for boot messages via boot param */
887arch_initcall(dynamic_debug_init);
888/* Debugfs setup must be done later */
889module_init(dynamic_debug_init_debugfs);