1
2
3
4
5
6
7
8
9
10
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/kallsyms.h>
16#include <linux/version.h>
17#include <linux/types.h>
18#include <linux/mutex.h>
19#include <linux/proc_fs.h>
20#include <linux/seq_file.h>
21#include <linux/list.h>
22#include <linux/sysctl.h>
23#include <linux/ctype.h>
24#include <linux/string.h>
25#include <linux/uaccess.h>
26#include <linux/dynamic_debug.h>
27#include <linux/debugfs.h>
28#include <linux/slab.h>
29#include <linux/jump_label.h>
30
31extern struct _ddebug __start___verbose[];
32extern struct _ddebug __stop___verbose[];
33
34struct ddebug_table {
35 struct list_head link;
36 char *mod_name;
37 unsigned int num_ddebugs;
38 unsigned int num_enabled;
39 struct _ddebug *ddebugs;
40};
41
42struct ddebug_query {
43 const char *filename;
44 const char *module;
45 const char *function;
46 const char *format;
47 unsigned int first_lineno, last_lineno;
48};
49
50struct ddebug_iter {
51 struct ddebug_table *table;
52 unsigned int idx;
53};
54
55static DEFINE_MUTEX(ddebug_lock);
56static LIST_HEAD(ddebug_tables);
57static int verbose = 0;
58
59
60static inline const char *basename(const char *path)
61{
62 const char *tail = strrchr(path, '/');
63 return tail ? tail+1 : path;
64}
65
66
67static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,
68 size_t maxlen)
69{
70 char *p = buf;
71
72 BUG_ON(maxlen < 4);
73 if (dp->flags & _DPRINTK_FLAGS_PRINT)
74 *p++ = 'p';
75 if (p == buf)
76 *p++ = '-';
77 *p = '\0';
78
79 return buf;
80}
81
82
83
84
85
86
87
88static void ddebug_change(const struct ddebug_query *query,
89 unsigned int flags, unsigned int mask)
90{
91 int i;
92 struct ddebug_table *dt;
93 unsigned int newflags;
94 unsigned int nfound = 0;
95 char flagbuf[8];
96
97
98 mutex_lock(&ddebug_lock);
99 list_for_each_entry(dt, &ddebug_tables, link) {
100
101
102 if (query->module != NULL &&
103 strcmp(query->module, dt->mod_name))
104 continue;
105
106 for (i = 0 ; i < dt->num_ddebugs ; i++) {
107 struct _ddebug *dp = &dt->ddebugs[i];
108
109
110 if (query->filename != NULL &&
111 strcmp(query->filename, dp->filename) &&
112 strcmp(query->filename, basename(dp->filename)))
113 continue;
114
115
116 if (query->function != NULL &&
117 strcmp(query->function, dp->function))
118 continue;
119
120
121 if (query->format != NULL &&
122 strstr(dp->format, query->format) == NULL)
123 continue;
124
125
126 if (query->first_lineno &&
127 dp->lineno < query->first_lineno)
128 continue;
129 if (query->last_lineno &&
130 dp->lineno > query->last_lineno)
131 continue;
132
133 nfound++;
134
135 newflags = (dp->flags & mask) | flags;
136 if (newflags == dp->flags)
137 continue;
138
139 if (!newflags)
140 dt->num_enabled--;
141 else if (!dp->flags)
142 dt->num_enabled++;
143 dp->flags = newflags;
144 if (newflags)
145 dp->enabled = 1;
146 else
147 dp->enabled = 0;
148 if (verbose)
149 printk(KERN_INFO
150 "ddebug: changed %s:%d [%s]%s %s\n",
151 dp->filename, dp->lineno,
152 dt->mod_name, dp->function,
153 ddebug_describe_flags(dp, flagbuf,
154 sizeof(flagbuf)));
155 }
156 }
157 mutex_unlock(&ddebug_lock);
158
159 if (!nfound && verbose)
160 printk(KERN_INFO "ddebug: no matches for query\n");
161}
162
163
164
165
166
167
168
169static int ddebug_tokenize(char *buf, char *words[], int maxwords)
170{
171 int nwords = 0;
172
173 while (*buf) {
174 char *end;
175
176
177 buf = skip_spaces(buf);
178 if (!*buf)
179 break;
180
181
182 if (*buf == '"' || *buf == '\'') {
183 int quote = *buf++;
184 for (end = buf ; *end && *end != quote ; end++)
185 ;
186 if (!*end)
187 return -EINVAL;
188 } else {
189 for (end = buf ; *end && !isspace(*end) ; end++)
190 ;
191 BUG_ON(end == buf);
192 }
193
194
195 if (nwords == maxwords)
196 return -EINVAL;
197 if (*end)
198 *end++ = '\0';
199 words[nwords++] = buf;
200 buf = end;
201 }
202
203 if (verbose) {
204 int i;
205 printk(KERN_INFO "%s: split into words:", __func__);
206 for (i = 0 ; i < nwords ; i++)
207 printk(" \"%s\"", words[i]);
208 printk("\n");
209 }
210
211 return nwords;
212}
213
214
215
216
217
218
219static inline int parse_lineno(const char *str, unsigned int *val)
220{
221 char *end = NULL;
222 BUG_ON(str == NULL);
223 if (*str == '\0') {
224 *val = 0;
225 return 0;
226 }
227 *val = simple_strtoul(str, &end, 10);
228 return end == NULL || end == str || *end != '\0' ? -EINVAL : 0;
229}
230
231
232
233
234
235
236#define isodigit(c) ((c) >= '0' && (c) <= '7')
237static char *unescape(char *str)
238{
239 char *in = str;
240 char *out = str;
241
242 while (*in) {
243 if (*in == '\\') {
244 if (in[1] == '\\') {
245 *out++ = '\\';
246 in += 2;
247 continue;
248 } else if (in[1] == 't') {
249 *out++ = '\t';
250 in += 2;
251 continue;
252 } else if (in[1] == 'n') {
253 *out++ = '\n';
254 in += 2;
255 continue;
256 } else if (isodigit(in[1]) &&
257 isodigit(in[2]) &&
258 isodigit(in[3])) {
259 *out++ = ((in[1] - '0')<<6) |
260 ((in[2] - '0')<<3) |
261 (in[3] - '0');
262 in += 4;
263 continue;
264 }
265 }
266 *out++ = *in++;
267 }
268 *out = '\0';
269
270 return str;
271}
272
273
274
275
276
277
278
279
280
281
282
283
284
285static int ddebug_parse_query(char *words[], int nwords,
286 struct ddebug_query *query)
287{
288 unsigned int i;
289
290
291 if (nwords % 2 != 0)
292 return -EINVAL;
293 memset(query, 0, sizeof(*query));
294
295 for (i = 0 ; i < nwords ; i += 2) {
296 if (!strcmp(words[i], "func"))
297 query->function = words[i+1];
298 else if (!strcmp(words[i], "file"))
299 query->filename = words[i+1];
300 else if (!strcmp(words[i], "module"))
301 query->module = words[i+1];
302 else if (!strcmp(words[i], "format"))
303 query->format = unescape(words[i+1]);
304 else if (!strcmp(words[i], "line")) {
305 char *first = words[i+1];
306 char *last = strchr(first, '-');
307 if (last)
308 *last++ = '\0';
309 if (parse_lineno(first, &query->first_lineno) < 0)
310 return -EINVAL;
311 if (last != NULL) {
312
313 if (parse_lineno(last, &query->last_lineno) < 0)
314 return -EINVAL;
315 } else {
316 query->last_lineno = query->first_lineno;
317 }
318 } else {
319 if (verbose)
320 printk(KERN_ERR "%s: unknown keyword \"%s\"\n",
321 __func__, words[i]);
322 return -EINVAL;
323 }
324 }
325
326 if (verbose)
327 printk(KERN_INFO "%s: q->function=\"%s\" q->filename=\"%s\" "
328 "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n",
329 __func__, query->function, query->filename,
330 query->module, query->format, query->first_lineno,
331 query->last_lineno);
332
333 return 0;
334}
335
336
337
338
339
340
341
342static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
343 unsigned int *maskp)
344{
345 unsigned flags = 0;
346 int op = '=';
347
348 switch (*str) {
349 case '+':
350 case '-':
351 case '=':
352 op = *str++;
353 break;
354 default:
355 return -EINVAL;
356 }
357 if (verbose)
358 printk(KERN_INFO "%s: op='%c'\n", __func__, op);
359
360 for ( ; *str ; ++str) {
361 switch (*str) {
362 case 'p':
363 flags |= _DPRINTK_FLAGS_PRINT;
364 break;
365 default:
366 return -EINVAL;
367 }
368 }
369 if (flags == 0)
370 return -EINVAL;
371 if (verbose)
372 printk(KERN_INFO "%s: flags=0x%x\n", __func__, flags);
373
374
375 switch (op) {
376 case '=':
377 *maskp = 0;
378 *flagsp = flags;
379 break;
380 case '+':
381 *maskp = ~0U;
382 *flagsp = flags;
383 break;
384 case '-':
385 *maskp = ~flags;
386 *flagsp = 0;
387 break;
388 }
389 if (verbose)
390 printk(KERN_INFO "%s: *flagsp=0x%x *maskp=0x%x\n",
391 __func__, *flagsp, *maskp);
392 return 0;
393}
394
395static int ddebug_exec_query(char *query_string)
396{
397 unsigned int flags = 0, mask = 0;
398 struct ddebug_query query;
399#define MAXWORDS 9
400 int nwords;
401 char *words[MAXWORDS];
402
403 nwords = ddebug_tokenize(query_string, words, MAXWORDS);
404 if (nwords <= 0)
405 return -EINVAL;
406 if (ddebug_parse_query(words, nwords-1, &query))
407 return -EINVAL;
408 if (ddebug_parse_flags(words[nwords-1], &flags, &mask))
409 return -EINVAL;
410
411
412 ddebug_change(&query, flags, mask);
413 return 0;
414}
415
416static __initdata char ddebug_setup_string[1024];
417static __init int ddebug_setup_query(char *str)
418{
419 if (strlen(str) >= 1024) {
420 pr_warning("ddebug boot param string too large\n");
421 return 0;
422 }
423 strcpy(ddebug_setup_string, str);
424 return 1;
425}
426
427__setup("ddebug_query=", ddebug_setup_query);
428
429
430
431
432
433static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
434 size_t len, loff_t *offp)
435{
436 char tmpbuf[256];
437 int ret;
438
439 if (len == 0)
440 return 0;
441
442 if (len > sizeof(tmpbuf)-1)
443 return -E2BIG;
444 if (copy_from_user(tmpbuf, ubuf, len))
445 return -EFAULT;
446 tmpbuf[len] = '\0';
447 if (verbose)
448 printk(KERN_INFO "%s: read %d bytes from userspace\n",
449 __func__, (int)len);
450
451 ret = ddebug_exec_query(tmpbuf);
452 if (ret)
453 return ret;
454
455 *offp += len;
456 return len;
457}
458
459
460
461
462
463
464static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
465{
466 if (list_empty(&ddebug_tables)) {
467 iter->table = NULL;
468 iter->idx = 0;
469 return NULL;
470 }
471 iter->table = list_entry(ddebug_tables.next,
472 struct ddebug_table, link);
473 iter->idx = 0;
474 return &iter->table->ddebugs[iter->idx];
475}
476
477
478
479
480
481
482
483static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
484{
485 if (iter->table == NULL)
486 return NULL;
487 if (++iter->idx == iter->table->num_ddebugs) {
488
489 iter->idx = 0;
490 if (list_is_last(&iter->table->link, &ddebug_tables)) {
491 iter->table = NULL;
492 return NULL;
493 }
494 iter->table = list_entry(iter->table->link.next,
495 struct ddebug_table, link);
496 }
497 return &iter->table->ddebugs[iter->idx];
498}
499
500
501
502
503
504
505static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
506{
507 struct ddebug_iter *iter = m->private;
508 struct _ddebug *dp;
509 int n = *pos;
510
511 if (verbose)
512 printk(KERN_INFO "%s: called m=%p *pos=%lld\n",
513 __func__, m, (unsigned long long)*pos);
514
515 mutex_lock(&ddebug_lock);
516
517 if (!n)
518 return SEQ_START_TOKEN;
519 if (n < 0)
520 return NULL;
521 dp = ddebug_iter_first(iter);
522 while (dp != NULL && --n > 0)
523 dp = ddebug_iter_next(iter);
524 return dp;
525}
526
527
528
529
530
531
532static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
533{
534 struct ddebug_iter *iter = m->private;
535 struct _ddebug *dp;
536
537 if (verbose)
538 printk(KERN_INFO "%s: called m=%p p=%p *pos=%lld\n",
539 __func__, m, p, (unsigned long long)*pos);
540
541 if (p == SEQ_START_TOKEN)
542 dp = ddebug_iter_first(iter);
543 else
544 dp = ddebug_iter_next(iter);
545 ++*pos;
546 return dp;
547}
548
549
550
551
552
553
554
555static int ddebug_proc_show(struct seq_file *m, void *p)
556{
557 struct ddebug_iter *iter = m->private;
558 struct _ddebug *dp = p;
559 char flagsbuf[8];
560
561 if (verbose)
562 printk(KERN_INFO "%s: called m=%p p=%p\n",
563 __func__, m, p);
564
565 if (p == SEQ_START_TOKEN) {
566 seq_puts(m,
567 "# filename:lineno [module]function flags format\n");
568 return 0;
569 }
570
571 seq_printf(m, "%s:%u [%s]%s %s \"",
572 dp->filename, dp->lineno,
573 iter->table->mod_name, dp->function,
574 ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
575 seq_escape(m, dp->format, "\t\r\n\"");
576 seq_puts(m, "\"\n");
577
578 return 0;
579}
580
581
582
583
584
585static void ddebug_proc_stop(struct seq_file *m, void *p)
586{
587 if (verbose)
588 printk(KERN_INFO "%s: called m=%p p=%p\n",
589 __func__, m, p);
590 mutex_unlock(&ddebug_lock);
591}
592
593static const struct seq_operations ddebug_proc_seqops = {
594 .start = ddebug_proc_start,
595 .next = ddebug_proc_next,
596 .show = ddebug_proc_show,
597 .stop = ddebug_proc_stop
598};
599
600
601
602
603
604
605
606static int ddebug_proc_open(struct inode *inode, struct file *file)
607{
608 struct ddebug_iter *iter;
609 int err;
610
611 if (verbose)
612 printk(KERN_INFO "%s: called\n", __func__);
613
614 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
615 if (iter == NULL)
616 return -ENOMEM;
617
618 err = seq_open(file, &ddebug_proc_seqops);
619 if (err) {
620 kfree(iter);
621 return err;
622 }
623 ((struct seq_file *) file->private_data)->private = iter;
624 return 0;
625}
626
627static const struct file_operations ddebug_proc_fops = {
628 .owner = THIS_MODULE,
629 .open = ddebug_proc_open,
630 .read = seq_read,
631 .llseek = seq_lseek,
632 .release = seq_release_private,
633 .write = ddebug_proc_write
634};
635
636
637
638
639
640int ddebug_add_module(struct _ddebug *tab, unsigned int n,
641 const char *name)
642{
643 struct ddebug_table *dt;
644 char *new_name;
645
646 dt = kzalloc(sizeof(*dt), GFP_KERNEL);
647 if (dt == NULL)
648 return -ENOMEM;
649 new_name = kstrdup(name, GFP_KERNEL);
650 if (new_name == NULL) {
651 kfree(dt);
652 return -ENOMEM;
653 }
654 dt->mod_name = new_name;
655 dt->num_ddebugs = n;
656 dt->num_enabled = 0;
657 dt->ddebugs = tab;
658
659 mutex_lock(&ddebug_lock);
660 list_add_tail(&dt->link, &ddebug_tables);
661 mutex_unlock(&ddebug_lock);
662
663 if (verbose)
664 printk(KERN_INFO "%u debug prints in module %s\n",
665 n, dt->mod_name);
666 return 0;
667}
668EXPORT_SYMBOL_GPL(ddebug_add_module);
669
670static void ddebug_table_free(struct ddebug_table *dt)
671{
672 list_del_init(&dt->link);
673 kfree(dt->mod_name);
674 kfree(dt);
675}
676
677
678
679
680
681int ddebug_remove_module(const char *mod_name)
682{
683 struct ddebug_table *dt, *nextdt;
684 int ret = -ENOENT;
685
686 if (verbose)
687 printk(KERN_INFO "%s: removing module \"%s\"\n",
688 __func__, mod_name);
689
690 mutex_lock(&ddebug_lock);
691 list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
692 if (!strcmp(dt->mod_name, mod_name)) {
693 ddebug_table_free(dt);
694 ret = 0;
695 }
696 }
697 mutex_unlock(&ddebug_lock);
698 return ret;
699}
700EXPORT_SYMBOL_GPL(ddebug_remove_module);
701
702static void ddebug_remove_all_tables(void)
703{
704 mutex_lock(&ddebug_lock);
705 while (!list_empty(&ddebug_tables)) {
706 struct ddebug_table *dt = list_entry(ddebug_tables.next,
707 struct ddebug_table,
708 link);
709 ddebug_table_free(dt);
710 }
711 mutex_unlock(&ddebug_lock);
712}
713
714static __initdata int ddebug_init_success;
715
716static int __init dynamic_debug_init_debugfs(void)
717{
718 struct dentry *dir, *file;
719
720 if (!ddebug_init_success)
721 return -ENODEV;
722
723 dir = debugfs_create_dir("dynamic_debug", NULL);
724 if (!dir)
725 return -ENOMEM;
726 file = debugfs_create_file("control", 0644, dir, NULL,
727 &ddebug_proc_fops);
728 if (!file) {
729 debugfs_remove(dir);
730 return -ENOMEM;
731 }
732 return 0;
733}
734
735static int __init dynamic_debug_init(void)
736{
737 struct _ddebug *iter, *iter_start;
738 const char *modname = NULL;
739 int ret = 0;
740 int n = 0;
741
742 if (__start___verbose != __stop___verbose) {
743 iter = __start___verbose;
744 modname = iter->modname;
745 iter_start = iter;
746 for (; iter < __stop___verbose; iter++) {
747 if (strcmp(modname, iter->modname)) {
748 ret = ddebug_add_module(iter_start, n, modname);
749 if (ret)
750 goto out_free;
751 n = 0;
752 modname = iter->modname;
753 iter_start = iter;
754 }
755 n++;
756 }
757 ret = ddebug_add_module(iter_start, n, modname);
758 }
759
760
761 if (ddebug_setup_string[0] != '\0') {
762 ret = ddebug_exec_query(ddebug_setup_string);
763 if (ret)
764 pr_warning("Invalid ddebug boot param %s",
765 ddebug_setup_string);
766 else
767 pr_info("ddebug initialized with string %s",
768 ddebug_setup_string);
769 }
770
771out_free:
772 if (ret)
773 ddebug_remove_all_tables();
774 else
775 ddebug_init_success = 1;
776 return 0;
777}
778
779arch_initcall(dynamic_debug_init);
780
781module_init(dynamic_debug_init_debugfs);
782