dynamic_debug: enlarge command/query write buffer
[linux-2.6-block.git] / lib / dynamic_debug.c
CommitLineData
e9d376f0
JB
1/*
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.
8ba6ebf5 10 * Copyright (C) 2011 Bart Van Assche. All Rights Reserved.
e9d376f0
JB
11 */
12
4ad275e5
JP
13#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
14
e9d376f0
JB
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/kallsyms.h>
e9d376f0
JB
19#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>
e7d2860b 26#include <linux/string.h>
e9d376f0
JB
27#include <linux/uaccess.h>
28#include <linux/dynamic_debug.h>
29#include <linux/debugfs.h>
5a0e3ad6 30#include <linux/slab.h>
52159d98 31#include <linux/jump_label.h>
8ba6ebf5 32#include <linux/hardirq.h>
e8d9792a 33#include <linux/sched.h>
cbc46635 34#include <linux/device.h>
ffa10cb4 35#include <linux/netdevice.h>
e9d376f0
JB
36
37extern struct _ddebug __start___verbose[];
38extern struct _ddebug __stop___verbose[];
39
e9d376f0
JB
40struct ddebug_table {
41 struct list_head link;
42 char *mod_name;
43 unsigned int num_ddebugs;
e9d376f0
JB
44 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;
74df138d 63module_param(verbose, int, 0644);
e9d376f0
JB
64
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
8ba6ebf5
BVA
72static 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' },
5ca7d2a6 78 { _DPRINTK_FLAGS_NONE, '_' },
8ba6ebf5
BVA
79};
80
e9d376f0
JB
81/* 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;
8ba6ebf5 86 int i;
e9d376f0 87
5ca7d2a6 88 BUG_ON(maxlen < 6);
8ba6ebf5
BVA
89 for (i = 0; i < ARRAY_SIZE(opt_array); ++i)
90 if (dp->flags & opt_array[i].flag)
91 *p++ = opt_array[i].opt_char;
e9d376f0 92 if (p == buf)
5ca7d2a6 93 *p++ = '_';
e9d376f0
JB
94 *p = '\0';
95
96 return buf;
97}
98
e9d376f0
JB
99/*
100 * 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;
5ca7d2a6 112 char flagbuf[10];
e9d376f0
JB
113
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 */
d6a238d2 119 if (query->module && strcmp(query->module, dt->mod_name))
e9d376f0
JB
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 */
d6a238d2 126 if (query->filename &&
e9d376f0
JB
127 strcmp(query->filename, dp->filename) &&
128 strcmp(query->filename, basename(dp->filename)))
129 continue;
130
131 /* match against the function */
d6a238d2 132 if (query->function &&
e9d376f0
JB
133 strcmp(query->function, dp->function))
134 continue;
135
136 /* match against the format */
d6a238d2
JC
137 if (query->format &&
138 !strstr(dp->format, query->format))
e9d376f0
JB
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;
e9d376f0 154 dp->flags = newflags;
e9d376f0 155 if (verbose)
5ca7d2a6 156 pr_info("changed %s:%d [%s]%s =%s\n",
e9d376f0
JB
157 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)
4ad275e5 166 pr_info("no matches for query\n");
e9d376f0
JB
167}
168
e9d376f0
JB
169/*
170 * Split the buffer `buf' into space-separated words.
9898abb3
GB
171 * Handles simple " and ' quoting, i.e. without nested,
172 * embedded or escaped \". Return the number of words
173 * or <0 on error.
e9d376f0
JB
174 */
175static int ddebug_tokenize(char *buf, char *words[], int maxwords)
176{
177 int nwords = 0;
178
9898abb3
GB
179 while (*buf) {
180 char *end;
181
182 /* Skip leading whitespace */
e7d2860b 183 buf = skip_spaces(buf);
9898abb3
GB
184 if (!*buf)
185 break; /* oh, it was trailing whitespace */
8bd6026e
JC
186 if (*buf == '#')
187 break; /* token starts comment, skip rest of line */
9898abb3 188
07100be7 189 /* find `end' of word, whitespace separated or quoted */
9898abb3
GB
190 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 }
9898abb3 201
07100be7 202 /* `buf' is start of word, `end' is one past its end */
9898abb3
GB
203 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 }
e9d376f0
JB
210
211 if (verbose) {
212 int i;
4ad275e5 213 pr_info("split into words:");
e9d376f0 214 for (i = 0 ; i < nwords ; i++)
4ad275e5
JP
215 pr_cont(" \"%s\"", words[i]);
216 pr_cont("\n");
e9d376f0
JB
217 }
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
820874c7
JC
281static 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
e9d376f0
JB
294/*
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
820874c7
JC
305 *
306 * Only 1 of each type is allowed.
307 * Returns 0 on success, <0 on error.
e9d376f0
JB
308 */
309static int ddebug_parse_query(char *words[], int nwords,
310 struct ddebug_query *query)
311{
312 unsigned int i;
820874c7 313 int rc;
e9d376f0
JB
314
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"))
820874c7 322 rc = check_set(&query->function, words[i+1], "func");
e9d376f0 323 else if (!strcmp(words[i], "file"))
820874c7 324 rc = check_set(&query->filename, words[i+1], "file");
e9d376f0 325 else if (!strcmp(words[i], "module"))
820874c7 326 rc = check_set(&query->module, words[i+1], "module");
e9d376f0 327 else if (!strcmp(words[i], "format"))
820874c7
JC
328 rc = check_set(&query->format, unescape(words[i+1]),
329 "format");
e9d376f0
JB
330 else if (!strcmp(words[i], "line")) {
331 char *first = words[i+1];
332 char *last = strchr(first, '-');
820874c7
JC
333 if (query->first_lineno || query->last_lineno) {
334 pr_err("match-spec:line given 2 times\n");
335 return -EINVAL;
336 }
e9d376f0
JB
337 if (last)
338 *last++ = '\0';
339 if (parse_lineno(first, &query->first_lineno) < 0)
340 return -EINVAL;
820874c7 341 if (last) {
e9d376f0 342 /* range <first>-<last> */
820874c7
JC
343 if (parse_lineno(last, &query->last_lineno)
344 < query->first_lineno) {
345 pr_err("last-line < 1st-line\n");
e9d376f0 346 return -EINVAL;
820874c7 347 }
e9d376f0
JB
348 } else {
349 query->last_lineno = query->first_lineno;
350 }
351 } else {
ae27f86a 352 pr_err("unknown keyword \"%s\"\n", words[i]);
e9d376f0
JB
353 return -EINVAL;
354 }
820874c7
JC
355 if (rc)
356 return rc;
e9d376f0
JB
357 }
358
359 if (verbose)
4ad275e5
JP
360 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,
e9d376f0
JB
363 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;
8ba6ebf5 379 int op = '=', i;
e9d376f0
JB
380
381 switch (*str) {
382 case '+':
383 case '-':
384 case '=':
385 op = *str++;
386 break;
387 default:
388 return -EINVAL;
389 }
390 if (verbose)
4ad275e5 391 pr_info("op='%c'\n", op);
e9d376f0
JB
392
393 for ( ; *str ; ++str) {
8ba6ebf5
BVA
394 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 }
e9d376f0 399 }
8ba6ebf5
BVA
400 if (i < 0)
401 return -EINVAL;
e9d376f0 402 }
e9d376f0 403 if (verbose)
4ad275e5 404 pr_info("flags=0x%x\n", flags);
e9d376f0
JB
405
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)
4ad275e5 422 pr_info("*flagsp=0x%x *maskp=0x%x\n", *flagsp, *maskp);
e9d376f0
JB
423 return 0;
424}
425
fd89cfb8
TR
426static 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
431625da
JB
447#define PREFIX_SIZE 64
448
449static int remaining(int wrote)
450{
451 if (PREFIX_SIZE - wrote > 0)
452 return PREFIX_SIZE - wrote;
453 return 0;
454}
455
456static char *dynamic_emit_prefix(const struct _ddebug *desc, char *buf)
8ba6ebf5 457{
431625da
JB
458 int pos_after_tid;
459 int pos = 0;
8ba6ebf5 460
431625da
JB
461 pos += snprintf(buf + pos, remaining(pos), "%s", KERN_DEBUG);
462 if (desc->flags & _DPRINTK_FLAGS_INCL_TID) {
8ba6ebf5 463 if (in_interrupt())
431625da
JB
464 pos += snprintf(buf + pos, remaining(pos), "%s ",
465 "<intr>");
8ba6ebf5 466 else
431625da
JB
467 pos += snprintf(buf + pos, remaining(pos), "[%d] ",
468 task_pid_vnr(current));
8ba6ebf5 469 }
431625da
JB
470 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)
07100be7
JC
478 pos += snprintf(buf + pos, remaining(pos), "%d:",
479 desc->lineno);
431625da
JB
480 if (pos - pos_after_tid)
481 pos += snprintf(buf + pos, remaining(pos), " ");
482 if (pos >= PREFIX_SIZE)
483 buf[PREFIX_SIZE - 1] = '\0';
6c2140ee 484
431625da 485 return buf;
6c2140ee
JP
486}
487
8ba6ebf5
BVA
488int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
489{
490 va_list args;
491 int res;
431625da
JB
492 struct va_format vaf;
493 char buf[PREFIX_SIZE];
8ba6ebf5
BVA
494
495 BUG_ON(!descriptor);
496 BUG_ON(!fmt);
497
498 va_start(args, fmt);
431625da
JB
499 vaf.fmt = fmt;
500 vaf.va = &args;
501 res = printk("%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf);
8ba6ebf5
BVA
502 va_end(args);
503
504 return res;
505}
506EXPORT_SYMBOL(__dynamic_pr_debug);
507
cbc46635
JP
508int __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;
431625da 514 char buf[PREFIX_SIZE];
cbc46635
JP
515
516 BUG_ON(!descriptor);
517 BUG_ON(!fmt);
518
519 va_start(args, fmt);
cbc46635
JP
520 vaf.fmt = fmt;
521 vaf.va = &args;
431625da 522 res = __dev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf);
cbc46635
JP
523 va_end(args);
524
525 return res;
526}
527EXPORT_SYMBOL(__dynamic_dev_dbg);
528
0feefd97
JB
529#ifdef CONFIG_NET
530
ffa10cb4
JB
531int __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;
431625da 537 char buf[PREFIX_SIZE];
ffa10cb4
JB
538
539 BUG_ON(!descriptor);
540 BUG_ON(!fmt);
541
542 va_start(args, fmt);
ffa10cb4
JB
543 vaf.fmt = fmt;
544 vaf.va = &args;
431625da 545 res = __netdev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf);
ffa10cb4
JB
546 va_end(args);
547
548 return res;
549}
550EXPORT_SYMBOL(__dynamic_netdev_dbg);
551
0feefd97
JB
552#endif
553
bc757f6f
JC
554#define DDEBUG_STRING_SIZE 1024
555static __initdata char ddebug_setup_string[DDEBUG_STRING_SIZE];
556
a648ec05
TR
557static __init int ddebug_setup_query(char *str)
558{
bc757f6f 559 if (strlen(str) >= DDEBUG_STRING_SIZE) {
4ad275e5 560 pr_warn("ddebug boot param string too large\n");
a648ec05
TR
561 return 0;
562 }
bc757f6f 563 strlcpy(ddebug_setup_string, str, DDEBUG_STRING_SIZE);
a648ec05
TR
564 return 1;
565}
566
567__setup("ddebug_query=", ddebug_setup_query);
568
e9d376f0
JB
569/*
570 * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the
571 * command text from userspace, parses and executes it.
572 */
7281491c 573#define USER_BUF_PAGE 4096
e9d376f0
JB
574static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
575 size_t len, loff_t *offp)
576{
7281491c 577 char *tmpbuf;
fd89cfb8 578 int ret;
e9d376f0
JB
579
580 if (len == 0)
581 return 0;
7281491c
JC
582 if (len > USER_BUF_PAGE - 1) {
583 pr_warn("expected <%d bytes into control\n", USER_BUF_PAGE);
e9d376f0 584 return -E2BIG;
7281491c
JC
585 }
586 tmpbuf = kmalloc(len + 1, GFP_KERNEL);
587 if (!tmpbuf)
588 return -ENOMEM;
589 if (copy_from_user(tmpbuf, ubuf, len)) {
590 kfree(tmpbuf);
e9d376f0 591 return -EFAULT;
7281491c 592 }
e9d376f0
JB
593 tmpbuf[len] = '\0';
594 if (verbose)
4ad275e5 595 pr_info("read %d bytes from userspace\n", (int)len);
e9d376f0 596
fd89cfb8 597 ret = ddebug_exec_query(tmpbuf);
7281491c 598 kfree(tmpbuf);
fd89cfb8
TR
599 if (ret)
600 return ret;
e9d376f0
JB
601
602 *offp += len;
603 return len;
604}
605
606/*
607 * Set the iterator to point to the first _ddebug object
608 * and return a pointer to that first object. Returns
609 * NULL if there are no _ddebugs at all.
610 */
611static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
612{
613 if (list_empty(&ddebug_tables)) {
614 iter->table = NULL;
615 iter->idx = 0;
616 return NULL;
617 }
618 iter->table = list_entry(ddebug_tables.next,
619 struct ddebug_table, link);
620 iter->idx = 0;
621 return &iter->table->ddebugs[iter->idx];
622}
623
624/*
625 * Advance the iterator to point to the next _ddebug
626 * object from the one the iterator currently points at,
627 * and returns a pointer to the new _ddebug. Returns
628 * NULL if the iterator has seen all the _ddebugs.
629 */
630static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
631{
632 if (iter->table == NULL)
633 return NULL;
634 if (++iter->idx == iter->table->num_ddebugs) {
635 /* iterate to next table */
636 iter->idx = 0;
637 if (list_is_last(&iter->table->link, &ddebug_tables)) {
638 iter->table = NULL;
639 return NULL;
640 }
641 iter->table = list_entry(iter->table->link.next,
642 struct ddebug_table, link);
643 }
644 return &iter->table->ddebugs[iter->idx];
645}
646
647/*
648 * Seq_ops start method. Called at the start of every
649 * read() call from userspace. Takes the ddebug_lock and
650 * seeks the seq_file's iterator to the given position.
651 */
652static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
653{
654 struct ddebug_iter *iter = m->private;
655 struct _ddebug *dp;
656 int n = *pos;
657
658 if (verbose)
4ad275e5 659 pr_info("called m=%p *pos=%lld\n", m, (unsigned long long)*pos);
e9d376f0
JB
660
661 mutex_lock(&ddebug_lock);
662
663 if (!n)
664 return SEQ_START_TOKEN;
665 if (n < 0)
666 return NULL;
667 dp = ddebug_iter_first(iter);
668 while (dp != NULL && --n > 0)
669 dp = ddebug_iter_next(iter);
670 return dp;
671}
672
673/*
674 * Seq_ops next method. Called several times within a read()
675 * call from userspace, with ddebug_lock held. Walks to the
676 * next _ddebug object with a special case for the header line.
677 */
678static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
679{
680 struct ddebug_iter *iter = m->private;
681 struct _ddebug *dp;
682
683 if (verbose)
4ad275e5
JP
684 pr_info("called m=%p p=%p *pos=%lld\n",
685 m, p, (unsigned long long)*pos);
e9d376f0
JB
686
687 if (p == SEQ_START_TOKEN)
688 dp = ddebug_iter_first(iter);
689 else
690 dp = ddebug_iter_next(iter);
691 ++*pos;
692 return dp;
693}
694
695/*
696 * Seq_ops show method. Called several times within a read()
697 * call from userspace, with ddebug_lock held. Formats the
698 * current _ddebug as a single human-readable line, with a
699 * special case for the header line.
700 */
701static int ddebug_proc_show(struct seq_file *m, void *p)
702{
703 struct ddebug_iter *iter = m->private;
704 struct _ddebug *dp = p;
5ca7d2a6 705 char flagsbuf[10];
e9d376f0
JB
706
707 if (verbose)
4ad275e5 708 pr_info("called m=%p p=%p\n", m, p);
e9d376f0
JB
709
710 if (p == SEQ_START_TOKEN) {
711 seq_puts(m,
712 "# filename:lineno [module]function flags format\n");
713 return 0;
714 }
715
5ca7d2a6
JC
716 seq_printf(m, "%s:%u [%s]%s =%s \"",
717 dp->filename, dp->lineno,
718 iter->table->mod_name, dp->function,
719 ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
e9d376f0
JB
720 seq_escape(m, dp->format, "\t\r\n\"");
721 seq_puts(m, "\"\n");
722
723 return 0;
724}
725
726/*
727 * Seq_ops stop method. Called at the end of each read()
728 * call from userspace. Drops ddebug_lock.
729 */
730static void ddebug_proc_stop(struct seq_file *m, void *p)
731{
732 if (verbose)
4ad275e5 733 pr_info("called m=%p p=%p\n", m, p);
e9d376f0
JB
734 mutex_unlock(&ddebug_lock);
735}
736
737static const struct seq_operations ddebug_proc_seqops = {
738 .start = ddebug_proc_start,
739 .next = ddebug_proc_next,
740 .show = ddebug_proc_show,
741 .stop = ddebug_proc_stop
742};
743
744/*
07100be7
JC
745 * File_ops->open method for <debugfs>/dynamic_debug/control. Does
746 * the seq_file setup dance, and also creates an iterator to walk the
747 * _ddebugs. Note that we create a seq_file always, even for O_WRONLY
748 * files where it's not needed, as doing so simplifies the ->release
749 * method.
e9d376f0
JB
750 */
751static int ddebug_proc_open(struct inode *inode, struct file *file)
752{
753 struct ddebug_iter *iter;
754 int err;
755
756 if (verbose)
4ad275e5 757 pr_info("called\n");
e9d376f0
JB
758
759 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
760 if (iter == NULL)
761 return -ENOMEM;
762
763 err = seq_open(file, &ddebug_proc_seqops);
764 if (err) {
765 kfree(iter);
766 return err;
767 }
768 ((struct seq_file *) file->private_data)->private = iter;
769 return 0;
770}
771
772static const struct file_operations ddebug_proc_fops = {
773 .owner = THIS_MODULE,
774 .open = ddebug_proc_open,
775 .read = seq_read,
776 .llseek = seq_lseek,
777 .release = seq_release_private,
778 .write = ddebug_proc_write
779};
780
781/*
782 * Allocate a new ddebug_table for the given module
783 * and add it to the global list.
784 */
785int ddebug_add_module(struct _ddebug *tab, unsigned int n,
786 const char *name)
787{
788 struct ddebug_table *dt;
789 char *new_name;
790
791 dt = kzalloc(sizeof(*dt), GFP_KERNEL);
792 if (dt == NULL)
793 return -ENOMEM;
794 new_name = kstrdup(name, GFP_KERNEL);
795 if (new_name == NULL) {
796 kfree(dt);
797 return -ENOMEM;
798 }
799 dt->mod_name = new_name;
800 dt->num_ddebugs = n;
e9d376f0
JB
801 dt->ddebugs = tab;
802
803 mutex_lock(&ddebug_lock);
804 list_add_tail(&dt->link, &ddebug_tables);
805 mutex_unlock(&ddebug_lock);
806
807 if (verbose)
4ad275e5 808 pr_info("%u debug prints in module %s\n", n, dt->mod_name);
e9d376f0
JB
809 return 0;
810}
811EXPORT_SYMBOL_GPL(ddebug_add_module);
812
813static void ddebug_table_free(struct ddebug_table *dt)
814{
815 list_del_init(&dt->link);
816 kfree(dt->mod_name);
817 kfree(dt);
818}
819
820/*
821 * Called in response to a module being unloaded. Removes
822 * any ddebug_table's which point at the module.
823 */
ff49d74a 824int ddebug_remove_module(const char *mod_name)
e9d376f0
JB
825{
826 struct ddebug_table *dt, *nextdt;
827 int ret = -ENOENT;
828
829 if (verbose)
4ad275e5 830 pr_info("removing module \"%s\"\n", mod_name);
e9d376f0
JB
831
832 mutex_lock(&ddebug_lock);
833 list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
834 if (!strcmp(dt->mod_name, mod_name)) {
835 ddebug_table_free(dt);
836 ret = 0;
837 }
838 }
839 mutex_unlock(&ddebug_lock);
840 return ret;
841}
842EXPORT_SYMBOL_GPL(ddebug_remove_module);
843
844static void ddebug_remove_all_tables(void)
845{
846 mutex_lock(&ddebug_lock);
847 while (!list_empty(&ddebug_tables)) {
848 struct ddebug_table *dt = list_entry(ddebug_tables.next,
849 struct ddebug_table,
850 link);
851 ddebug_table_free(dt);
852 }
853 mutex_unlock(&ddebug_lock);
854}
855
6a5c083d
TR
856static __initdata int ddebug_init_success;
857
858static int __init dynamic_debug_init_debugfs(void)
e9d376f0
JB
859{
860 struct dentry *dir, *file;
6a5c083d
TR
861
862 if (!ddebug_init_success)
863 return -ENODEV;
e9d376f0
JB
864
865 dir = debugfs_create_dir("dynamic_debug", NULL);
866 if (!dir)
867 return -ENOMEM;
868 file = debugfs_create_file("control", 0644, dir, NULL,
869 &ddebug_proc_fops);
870 if (!file) {
871 debugfs_remove(dir);
872 return -ENOMEM;
873 }
6a5c083d
TR
874 return 0;
875}
876
877static int __init dynamic_debug_init(void)
878{
879 struct _ddebug *iter, *iter_start;
880 const char *modname = NULL;
881 int ret = 0;
882 int n = 0;
883
b5b78f83
JC
884 if (__start___verbose == __stop___verbose) {
885 pr_warn("_ddebug table is empty in a "
886 "CONFIG_DYNAMIC_DEBUG build");
887 return 1;
888 }
889 iter = __start___verbose;
890 modname = iter->modname;
891 iter_start = iter;
892 for (; iter < __stop___verbose; iter++) {
893 if (strcmp(modname, iter->modname)) {
894 ret = ddebug_add_module(iter_start, n, modname);
895 if (ret)
896 goto out_free;
897 n = 0;
898 modname = iter->modname;
899 iter_start = iter;
e9d376f0 900 }
b5b78f83 901 n++;
e9d376f0 902 }
b5b78f83
JC
903 ret = ddebug_add_module(iter_start, n, modname);
904 if (ret)
905 goto out_free;
a648ec05
TR
906
907 /* ddebug_query boot param got passed -> set it up */
908 if (ddebug_setup_string[0] != '\0') {
909 ret = ddebug_exec_query(ddebug_setup_string);
910 if (ret)
4ad275e5
JP
911 pr_warn("Invalid ddebug boot param %s",
912 ddebug_setup_string);
a648ec05
TR
913 else
914 pr_info("ddebug initialized with string %s",
915 ddebug_setup_string);
916 }
917
e9d376f0 918out_free:
6a5c083d 919 if (ret)
e9d376f0 920 ddebug_remove_all_tables();
6a5c083d
TR
921 else
922 ddebug_init_success = 1;
e9d376f0
JB
923 return 0;
924}
6a5c083d
TR
925/* Allow early initialization for boot messages via boot param */
926arch_initcall(dynamic_debug_init);
927/* Debugfs setup must be done later */
928module_init(dynamic_debug_init_debugfs);