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