1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#define pr_fmt(fmt) "gcov: " fmt
18
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/debugfs.h>
22#include <linux/fs.h>
23#include <linux/list.h>
24#include <linux/string.h>
25#include <linux/slab.h>
26#include <linux/mutex.h>
27#include <linux/seq_file.h>
28#include "gcov.h"
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50struct gcov_node {
51 struct list_head list;
52 struct list_head children;
53 struct list_head all;
54 struct gcov_node *parent;
55 struct gcov_info **loaded_info;
56 struct gcov_info *unloaded_info;
57 struct dentry *dentry;
58 struct dentry **links;
59 int num_loaded;
60 char name[0];
61};
62
63static const char objtree[] = OBJTREE;
64static const char srctree[] = SRCTREE;
65static struct gcov_node root_node;
66static struct dentry *reset_dentry;
67static LIST_HEAD(all_head);
68static DEFINE_MUTEX(node_lock);
69
70
71static int gcov_persist = 1;
72
73static int __init gcov_persist_setup(char *str)
74{
75 unsigned long val;
76
77 if (kstrtoul(str, 0, &val)) {
78 pr_warn("invalid gcov_persist parameter '%s'\n", str);
79 return 0;
80 }
81 gcov_persist = val;
82 pr_info("setting gcov_persist to %d\n", gcov_persist);
83
84 return 1;
85}
86__setup("gcov_persist=", gcov_persist_setup);
87
88
89
90
91
92
93
94static void *gcov_seq_start(struct seq_file *seq, loff_t *pos)
95{
96 loff_t i;
97
98 gcov_iter_start(seq->private);
99 for (i = 0; i < *pos; i++) {
100 if (gcov_iter_next(seq->private))
101 return NULL;
102 }
103 return seq->private;
104}
105
106
107static void *gcov_seq_next(struct seq_file *seq, void *data, loff_t *pos)
108{
109 struct gcov_iterator *iter = data;
110
111 if (gcov_iter_next(iter))
112 return NULL;
113 (*pos)++;
114
115 return iter;
116}
117
118
119static int gcov_seq_show(struct seq_file *seq, void *data)
120{
121 struct gcov_iterator *iter = data;
122
123 if (gcov_iter_write(iter, seq))
124 return -EINVAL;
125 return 0;
126}
127
128static void gcov_seq_stop(struct seq_file *seq, void *data)
129{
130
131}
132
133static const struct seq_operations gcov_seq_ops = {
134 .start = gcov_seq_start,
135 .next = gcov_seq_next,
136 .show = gcov_seq_show,
137 .stop = gcov_seq_stop,
138};
139
140
141
142
143
144
145static struct gcov_info *get_node_info(struct gcov_node *node)
146{
147 if (node->num_loaded > 0)
148 return node->loaded_info[0];
149
150 return node->unloaded_info;
151}
152
153
154
155
156
157static struct gcov_info *get_accumulated_info(struct gcov_node *node)
158{
159 struct gcov_info *info;
160 int i = 0;
161
162 if (node->unloaded_info)
163 info = gcov_info_dup(node->unloaded_info);
164 else
165 info = gcov_info_dup(node->loaded_info[i++]);
166 if (!info)
167 return NULL;
168 for (; i < node->num_loaded; i++)
169 gcov_info_add(info, node->loaded_info[i]);
170
171 return info;
172}
173
174
175
176
177
178static int gcov_seq_open(struct inode *inode, struct file *file)
179{
180 struct gcov_node *node = inode->i_private;
181 struct gcov_iterator *iter;
182 struct seq_file *seq;
183 struct gcov_info *info;
184 int rc = -ENOMEM;
185
186 mutex_lock(&node_lock);
187
188
189
190
191
192 info = get_accumulated_info(node);
193 if (!info)
194 goto out_unlock;
195 iter = gcov_iter_new(info);
196 if (!iter)
197 goto err_free_info;
198 rc = seq_open(file, &gcov_seq_ops);
199 if (rc)
200 goto err_free_iter_info;
201 seq = file->private_data;
202 seq->private = iter;
203out_unlock:
204 mutex_unlock(&node_lock);
205 return rc;
206
207err_free_iter_info:
208 gcov_iter_free(iter);
209err_free_info:
210 gcov_info_free(info);
211 goto out_unlock;
212}
213
214
215
216
217
218static int gcov_seq_release(struct inode *inode, struct file *file)
219{
220 struct gcov_iterator *iter;
221 struct gcov_info *info;
222 struct seq_file *seq;
223
224 seq = file->private_data;
225 iter = seq->private;
226 info = gcov_iter_get_info(iter);
227 gcov_iter_free(iter);
228 gcov_info_free(info);
229 seq_release(inode, file);
230
231 return 0;
232}
233
234
235
236
237
238static struct gcov_node *get_node_by_name(const char *name)
239{
240 struct gcov_node *node;
241 struct gcov_info *info;
242
243 list_for_each_entry(node, &all_head, all) {
244 info = get_node_info(node);
245 if (info && (strcmp(gcov_info_filename(info), name) == 0))
246 return node;
247 }
248
249 return NULL;
250}
251
252
253
254
255static void reset_node(struct gcov_node *node)
256{
257 int i;
258
259 if (node->unloaded_info)
260 gcov_info_reset(node->unloaded_info);
261 for (i = 0; i < node->num_loaded; i++)
262 gcov_info_reset(node->loaded_info[i]);
263}
264
265static void remove_node(struct gcov_node *node);
266
267
268
269
270
271
272static ssize_t gcov_seq_write(struct file *file, const char __user *addr,
273 size_t len, loff_t *pos)
274{
275 struct seq_file *seq;
276 struct gcov_info *info;
277 struct gcov_node *node;
278
279 seq = file->private_data;
280 info = gcov_iter_get_info(seq->private);
281 mutex_lock(&node_lock);
282 node = get_node_by_name(gcov_info_filename(info));
283 if (node) {
284
285 if (node->num_loaded == 0)
286 remove_node(node);
287 else
288 reset_node(node);
289 }
290
291 gcov_info_reset(info);
292 mutex_unlock(&node_lock);
293
294 return len;
295}
296
297
298
299
300
301
302
303static char *link_target(const char *dir, const char *path, const char *ext)
304{
305 char *target;
306 char *old_ext;
307 char *copy;
308
309 copy = kstrdup(path, GFP_KERNEL);
310 if (!copy)
311 return NULL;
312 old_ext = strrchr(copy, '.');
313 if (old_ext)
314 *old_ext = '\0';
315 if (dir)
316 target = kasprintf(GFP_KERNEL, "%s/%s.%s", dir, copy, ext);
317 else
318 target = kasprintf(GFP_KERNEL, "%s.%s", copy, ext);
319 kfree(copy);
320
321 return target;
322}
323
324
325
326
327
328
329
330static char *get_link_target(const char *filename, const struct gcov_link *ext)
331{
332 const char *rel;
333 char *result;
334
335 if (strncmp(filename, objtree, strlen(objtree)) == 0) {
336 rel = filename + strlen(objtree) + 1;
337 if (ext->dir == SRC_TREE)
338 result = link_target(srctree, rel, ext->ext);
339 else
340 result = link_target(objtree, rel, ext->ext);
341 } else {
342
343 result = link_target(NULL, filename, ext->ext);
344 }
345
346 return result;
347}
348
349#define SKEW_PREFIX ".tmp_"
350
351
352
353
354
355static const char *deskew(const char *basename)
356{
357 if (strncmp(basename, SKEW_PREFIX, sizeof(SKEW_PREFIX) - 1) == 0)
358 return basename + sizeof(SKEW_PREFIX) - 1;
359 return basename;
360}
361
362
363
364
365
366static void add_links(struct gcov_node *node, struct dentry *parent)
367{
368 const char *basename;
369 char *target;
370 int num;
371 int i;
372
373 for (num = 0; gcov_link[num].ext; num++)
374 ;
375 node->links = kcalloc(num, sizeof(struct dentry *), GFP_KERNEL);
376 if (!node->links)
377 return;
378 for (i = 0; i < num; i++) {
379 target = get_link_target(
380 gcov_info_filename(get_node_info(node)),
381 &gcov_link[i]);
382 if (!target)
383 goto out_err;
384 basename = kbasename(target);
385 if (basename == target)
386 goto out_err;
387 node->links[i] = debugfs_create_symlink(deskew(basename),
388 parent, target);
389 if (!node->links[i])
390 goto out_err;
391 kfree(target);
392 }
393
394 return;
395out_err:
396 kfree(target);
397 while (i-- > 0)
398 debugfs_remove(node->links[i]);
399 kfree(node->links);
400 node->links = NULL;
401}
402
403static const struct file_operations gcov_data_fops = {
404 .open = gcov_seq_open,
405 .release = gcov_seq_release,
406 .read = seq_read,
407 .llseek = seq_lseek,
408 .write = gcov_seq_write,
409};
410
411
412static void init_node(struct gcov_node *node, struct gcov_info *info,
413 const char *name, struct gcov_node *parent)
414{
415 INIT_LIST_HEAD(&node->list);
416 INIT_LIST_HEAD(&node->children);
417 INIT_LIST_HEAD(&node->all);
418 if (node->loaded_info) {
419 node->loaded_info[0] = info;
420 node->num_loaded = 1;
421 }
422 node->parent = parent;
423 if (name)
424 strcpy(node->name, name);
425}
426
427
428
429
430
431static struct gcov_node *new_node(struct gcov_node *parent,
432 struct gcov_info *info, const char *name)
433{
434 struct gcov_node *node;
435
436 node = kzalloc(sizeof(struct gcov_node) + strlen(name) + 1, GFP_KERNEL);
437 if (!node)
438 goto err_nomem;
439 if (info) {
440 node->loaded_info = kcalloc(1, sizeof(struct gcov_info *),
441 GFP_KERNEL);
442 if (!node->loaded_info)
443 goto err_nomem;
444 }
445 init_node(node, info, name, parent);
446
447 if (info) {
448 node->dentry = debugfs_create_file(deskew(node->name), 0600,
449 parent->dentry, node, &gcov_data_fops);
450 } else
451 node->dentry = debugfs_create_dir(node->name, parent->dentry);
452 if (!node->dentry) {
453 pr_warn("could not create file\n");
454 kfree(node);
455 return NULL;
456 }
457 if (info)
458 add_links(node, parent->dentry);
459 list_add(&node->list, &parent->children);
460 list_add(&node->all, &all_head);
461
462 return node;
463
464err_nomem:
465 kfree(node);
466 pr_warn("out of memory\n");
467 return NULL;
468}
469
470
471static void remove_links(struct gcov_node *node)
472{
473 int i;
474
475 if (!node->links)
476 return;
477 for (i = 0; gcov_link[i].ext; i++)
478 debugfs_remove(node->links[i]);
479 kfree(node->links);
480 node->links = NULL;
481}
482
483
484
485
486
487static void release_node(struct gcov_node *node)
488{
489 list_del(&node->list);
490 list_del(&node->all);
491 debugfs_remove(node->dentry);
492 remove_links(node);
493 kfree(node->loaded_info);
494 if (node->unloaded_info)
495 gcov_info_free(node->unloaded_info);
496 kfree(node);
497}
498
499
500static void remove_node(struct gcov_node *node)
501{
502 struct gcov_node *parent;
503
504 while ((node != &root_node) && list_empty(&node->children)) {
505 parent = node->parent;
506 release_node(node);
507 node = parent;
508 }
509}
510
511
512
513
514
515static struct gcov_node *get_child_by_name(struct gcov_node *parent,
516 const char *name)
517{
518 struct gcov_node *node;
519
520 list_for_each_entry(node, &parent->children, list) {
521 if (strcmp(node->name, name) == 0)
522 return node;
523 }
524
525 return NULL;
526}
527
528
529
530
531
532static ssize_t reset_write(struct file *file, const char __user *addr,
533 size_t len, loff_t *pos)
534{
535 struct gcov_node *node;
536
537 mutex_lock(&node_lock);
538restart:
539 list_for_each_entry(node, &all_head, all) {
540 if (node->num_loaded > 0)
541 reset_node(node);
542 else if (list_empty(&node->children)) {
543 remove_node(node);
544
545 goto restart;
546 }
547 }
548 mutex_unlock(&node_lock);
549
550 return len;
551}
552
553
554static ssize_t reset_read(struct file *file, char __user *addr, size_t len,
555 loff_t *pos)
556{
557
558 return 0;
559}
560
561static const struct file_operations gcov_reset_fops = {
562 .write = reset_write,
563 .read = reset_read,
564 .llseek = noop_llseek,
565};
566
567
568
569
570
571static void add_node(struct gcov_info *info)
572{
573 char *filename;
574 char *curr;
575 char *next;
576 struct gcov_node *parent;
577 struct gcov_node *node;
578
579 filename = kstrdup(gcov_info_filename(info), GFP_KERNEL);
580 if (!filename)
581 return;
582 parent = &root_node;
583
584 for (curr = filename; (next = strchr(curr, '/')); curr = next + 1) {
585 if (curr == next)
586 continue;
587 *next = 0;
588 if (strcmp(curr, ".") == 0)
589 continue;
590 if (strcmp(curr, "..") == 0) {
591 if (!parent->parent)
592 goto err_remove;
593 parent = parent->parent;
594 continue;
595 }
596 node = get_child_by_name(parent, curr);
597 if (!node) {
598 node = new_node(parent, NULL, curr);
599 if (!node)
600 goto err_remove;
601 }
602 parent = node;
603 }
604
605 node = new_node(parent, info, curr);
606 if (!node)
607 goto err_remove;
608out:
609 kfree(filename);
610 return;
611
612err_remove:
613 remove_node(parent);
614 goto out;
615}
616
617
618
619
620
621static void add_info(struct gcov_node *node, struct gcov_info *info)
622{
623 struct gcov_info **loaded_info;
624 int num = node->num_loaded;
625
626
627
628
629
630
631 loaded_info = kcalloc(num + 1, sizeof(struct gcov_info *), GFP_KERNEL);
632 if (!loaded_info) {
633 pr_warn("could not add '%s' (out of memory)\n",
634 gcov_info_filename(info));
635 return;
636 }
637 memcpy(loaded_info, node->loaded_info,
638 num * sizeof(struct gcov_info *));
639 loaded_info[num] = info;
640
641 if (num == 0) {
642
643
644
645
646 if (!gcov_info_is_compatible(node->unloaded_info, info)) {
647 pr_warn("discarding saved data for %s "
648 "(incompatible version)\n",
649 gcov_info_filename(info));
650 gcov_info_free(node->unloaded_info);
651 node->unloaded_info = NULL;
652 }
653 } else {
654
655
656
657
658 if (!gcov_info_is_compatible(node->loaded_info[0], info)) {
659 pr_warn("could not add '%s' (incompatible "
660 "version)\n", gcov_info_filename(info));
661 kfree(loaded_info);
662 return;
663 }
664 }
665
666 kfree(node->loaded_info);
667 node->loaded_info = loaded_info;
668 node->num_loaded = num + 1;
669}
670
671
672
673
674static int get_info_index(struct gcov_node *node, struct gcov_info *info)
675{
676 int i;
677
678 for (i = 0; i < node->num_loaded; i++) {
679 if (node->loaded_info[i] == info)
680 return i;
681 }
682 return -ENOENT;
683}
684
685
686
687
688static void save_info(struct gcov_node *node, struct gcov_info *info)
689{
690 if (node->unloaded_info)
691 gcov_info_add(node->unloaded_info, info);
692 else {
693 node->unloaded_info = gcov_info_dup(info);
694 if (!node->unloaded_info) {
695 pr_warn("could not save data for '%s' "
696 "(out of memory)\n",
697 gcov_info_filename(info));
698 }
699 }
700}
701
702
703
704
705
706static void remove_info(struct gcov_node *node, struct gcov_info *info)
707{
708 int i;
709
710 i = get_info_index(node, info);
711 if (i < 0) {
712 pr_warn("could not remove '%s' (not found)\n",
713 gcov_info_filename(info));
714 return;
715 }
716 if (gcov_persist)
717 save_info(node, info);
718
719 node->loaded_info[i] = node->loaded_info[node->num_loaded - 1];
720 node->num_loaded--;
721 if (node->num_loaded > 0)
722 return;
723
724 kfree(node->loaded_info);
725 node->loaded_info = NULL;
726 node->num_loaded = 0;
727 if (!node->unloaded_info)
728 remove_node(node);
729}
730
731
732
733
734
735void gcov_event(enum gcov_action action, struct gcov_info *info)
736{
737 struct gcov_node *node;
738
739 mutex_lock(&node_lock);
740 node = get_node_by_name(gcov_info_filename(info));
741 switch (action) {
742 case GCOV_ADD:
743 if (node)
744 add_info(node, info);
745 else
746 add_node(info);
747 break;
748 case GCOV_REMOVE:
749 if (node)
750 remove_info(node, info);
751 else {
752 pr_warn("could not remove '%s' (not found)\n",
753 gcov_info_filename(info));
754 }
755 break;
756 }
757 mutex_unlock(&node_lock);
758}
759
760
761static __init int gcov_fs_init(void)
762{
763 int rc = -EIO;
764
765 init_node(&root_node, NULL, NULL, NULL);
766
767
768
769
770 root_node.dentry = debugfs_create_dir("gcov", NULL);
771 if (!root_node.dentry)
772 goto err_remove;
773
774
775
776
777 reset_dentry = debugfs_create_file("reset", 0600, root_node.dentry,
778 NULL, &gcov_reset_fops);
779 if (!reset_dentry)
780 goto err_remove;
781
782 gcov_enable_events();
783 return 0;
784
785err_remove:
786 pr_err("init failed\n");
787 debugfs_remove(root_node.dentry);
788
789 return rc;
790}
791device_initcall(gcov_fs_init);
792