xref: /freebsd/sys/gnu/gcov/gcov_fs.c (revision da5432eda807c4b7232d030d5157d5b417ea4f52)
1 // SPDX-License-Identifier: GPL-2.0
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; version 2.
5 //
6 // This program is distributed in the hope that it will be useful,
7 // but WITHOUT ANY WARRANTY; without even the implied warranty of
8 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9 // GNU General Public License for more details.
10 //
11 // You should have received a copy of the GNU General Public License
12 // along with this program; if not, write to the Free Software
13 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
14 // 02110-1301, USA.
15 /*
16  *  This code exports profiling data as debugfs files to userspace.
17  *
18  *    Copyright IBM Corp. 2009
19  *    Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
20  *
21  *    Uses gcc-internal data definitions.
22  *    Based on the gcov-kernel patch by:
23  *		 Hubertus Franke <frankeh@us.ibm.com>
24  *		 Nigel Hinds <nhinds@us.ibm.com>
25  *		 Rajan Ravindran <rajancr@us.ibm.com>
26  *		 Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
27  *		 Paul Larson
28  *		 Yi CDL Yang
29  */
30 
31 
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/param.h>
36 #include <sys/sbuf.h>
37 
38 #include <sys/queue.h>
39 #include <sys/linker.h>
40 #include <sys/module.h>
41 #include <sys/eventhandler.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/syslog.h>
45 #include <sys/proc.h>
46 #include <sys/sched.h>
47 #include <sys/syslog.h>
48 #include <sys/sysctl.h>
49 #include <linux/debugfs.h>
50 
51 #include <gnu/gcov/gcov.h>
52 #include <sys/queue.h>
53 
54 extern int gcov_events_enabled;
55 static int gcov_persist;
56 static struct mtx gcov_mtx;
57 MTX_SYSINIT(gcov_init, &gcov_mtx, "gcov_mtx", MTX_DEF);
58 MALLOC_DEFINE(M_GCOV, "gcov", "gcov");
59 
60 void __gcov_init(struct gcov_info *info);
61 void __gcov_flush(void);
62 void __gcov_merge_add(gcov_type *counters, unsigned int n_counters);
63 void __gcov_merge_single(gcov_type *counters, unsigned int n_counters);
64 void __gcov_merge_delta(gcov_type *counters, unsigned int n_counters);
65 void __gcov_merge_ior(gcov_type *counters, unsigned int n_counters);
66 void __gcov_merge_time_profile(gcov_type *counters, unsigned int n_counters);
67 void __gcov_merge_icall_topn(gcov_type *counters, unsigned int n_counters);
68 void __gcov_exit(void);
69 
70 static void gcov_event(enum gcov_action action, struct gcov_info *info);
71 
72 
73 /*
74  * Private copy taken from libc
75  */
76 static char *
77 (basename)(char *path)
78 {
79 	char *ptr;
80 
81 	/*
82 	 * If path is a null pointer or points to an empty string,
83 	 * basename() shall return a pointer to the string ".".
84 	 */
85 	if (path == NULL || *path == '\0')
86 		return (__DECONST(char *, "."));
87 
88 	/* Find end of last pathname component and null terminate it. */
89 	ptr = path + strlen(path);
90 	while (ptr > path + 1 && *(ptr - 1) == '/')
91 		--ptr;
92 	*ptr-- = '\0';
93 
94 	/* Find beginning of last pathname component. */
95 	while (ptr > path && *(ptr - 1) != '/')
96 		--ptr;
97 	return (ptr);
98 }
99 
100 /*
101  * __gcov_init is called by gcc-generated constructor code for each object
102  * file compiled with -fprofile-arcs.
103  */
104 void
105 __gcov_init(struct gcov_info *info)
106 {
107 	static unsigned int gcov_version;
108 
109 	mtx_lock(&gcov_mtx);
110 	if (gcov_version == 0) {
111 		gcov_version = gcov_info_version(info);
112 		/*
113 		 * Printing gcc's version magic may prove useful for debugging
114 		 * incompatibility reports.
115 		 */
116 		log(LOG_INFO, "version magic: 0x%x\n", gcov_version);
117 	}
118 	/*
119 	 * Add new profiling data structure to list and inform event
120 	 * listener.
121 	 */
122 	gcov_info_link(info);
123 	if (gcov_events_enabled)
124 		gcov_event(GCOV_ADD, info);
125 	mtx_unlock(&gcov_mtx);
126 }
127 
128 /*
129  * These functions may be referenced by gcc-generated profiling code but serve
130  * no function for kernel profiling.
131  */
132 void
133 __gcov_flush(void)
134 {
135 	/* Unused. */
136 }
137 
138 void
139 __gcov_merge_add(gcov_type *counters, unsigned int n_counters)
140 {
141 	/* Unused. */
142 }
143 
144 void
145 __gcov_merge_single(gcov_type *counters, unsigned int n_counters)
146 {
147 	/* Unused. */
148 }
149 
150 void
151 __gcov_merge_delta(gcov_type *counters, unsigned int n_counters)
152 {
153 	/* Unused. */
154 }
155 
156 void
157 __gcov_merge_ior(gcov_type *counters, unsigned int n_counters)
158 {
159 	/* Unused. */
160 }
161 
162 void
163 __gcov_merge_time_profile(gcov_type *counters, unsigned int n_counters)
164 {
165 	/* Unused. */
166 }
167 
168 void
169 __gcov_merge_icall_topn(gcov_type *counters, unsigned int n_counters)
170 {
171 	/* Unused. */
172 }
173 
174 void
175 __gcov_exit(void)
176 {
177 	/* Unused. */
178 }
179 
180 
181 /**
182  * struct gcov_node - represents a debugfs entry
183  * @entry: list entry for parent's child node list
184  * @children: child nodes
185  * @all_entry: list entry for list of all nodes
186  * @parent: parent node
187  * @loaded_info: array of pointers to profiling data sets for loaded object
188  *   files.
189  * @num_loaded: number of profiling data sets for loaded object files.
190  * @unloaded_info: accumulated copy of profiling data sets for unloaded
191  *   object files. Used only when gcov_persist=1.
192  * @dentry: main debugfs entry, either a directory or data file
193  * @links: associated symbolic links
194  * @name: data file basename
195  *
196  * struct gcov_node represents an entity within the gcov/ subdirectory
197  * of debugfs. There are directory and data file nodes. The latter represent
198  * the actual synthesized data file plus any associated symbolic links which
199  * are needed by the gcov tool to work correctly.
200  */
201 struct gcov_node {
202 	LIST_ENTRY(gcov_node) children_entry;
203 	LIST_ENTRY(gcov_node) all_entry;
204 	struct {
205 		struct gcov_node *lh_first;
206 	} children;
207 	struct gcov_node *parent;
208 	struct gcov_info **loaded_info;
209 	struct gcov_info *unloaded_info;
210 	struct dentry *dentry;
211 	struct dentry **links;
212 	int num_loaded;
213 	char name[0];
214 };
215 
216 #ifdef notyet
217 static const char objtree[] = OBJTREE;
218 static const char srctree[] = SRCTREE;
219 #else
220 static const char objtree[] = "";
221 static const char srctree[] = "";
222 #endif
223 static struct gcov_node root_node;
224 static struct {
225 	struct gcov_node *lh_first;
226 } all_head;
227 static struct mtx node_lock;
228 MTX_SYSINIT(node_init, &node_lock, "node_lock", MTX_DEF);
229 static void remove_node(struct gcov_node *node);
230 
231 /*
232  * seq_file.start() implementation for gcov data files. Note that the
233  * gcov_iterator interface is designed to be more restrictive than seq_file
234  * (no start from arbitrary position, etc.), to simplify the iterator
235  * implementation.
236  */
237 static void *
238 gcov_seq_start(struct seq_file *seq, off_t *pos)
239 {
240 	off_t i;
241 
242 	gcov_iter_start(seq->private);
243 	for (i = 0; i < *pos; i++) {
244 		if (gcov_iter_next(seq->private))
245 			return NULL;
246 	}
247 	return seq->private;
248 }
249 
250 /* seq_file.next() implementation for gcov data files. */
251 static void *
252 gcov_seq_next(struct seq_file *seq, void *data, off_t *pos)
253 {
254 	struct gcov_iterator *iter = data;
255 
256 	if (gcov_iter_next(iter))
257 		return NULL;
258 	(*pos)++;
259 
260 	return iter;
261 }
262 
263 /* seq_file.show() implementation for gcov data files. */
264 static int
265 gcov_seq_show(struct seq_file *seq, void *data)
266 {
267 	struct gcov_iterator *iter = data;
268 
269 	if (gcov_iter_write(iter, seq->buf))
270 		return (-EINVAL);
271 	return (0);
272 }
273 
274 static void
275 gcov_seq_stop(struct seq_file *seq, void *data)
276 {
277 	/* Unused. */
278 }
279 
280 static const struct seq_operations gcov_seq_ops = {
281 	.start	= gcov_seq_start,
282 	.next	= gcov_seq_next,
283 	.show	= gcov_seq_show,
284 	.stop	= gcov_seq_stop,
285 };
286 
287 /*
288  * Return a profiling data set associated with the given node. This is
289  * either a data set for a loaded object file or a data set copy in case
290  * all associated object files have been unloaded.
291  */
292 static struct gcov_info *
293 get_node_info(struct gcov_node *node)
294 {
295 	if (node->num_loaded > 0)
296 		return (node->loaded_info[0]);
297 
298 	return (node->unloaded_info);
299 }
300 
301 /*
302  * Return a newly allocated profiling data set which contains the sum of
303  * all profiling data associated with the given node.
304  */
305 static struct gcov_info *
306 get_accumulated_info(struct gcov_node *node)
307 {
308 	struct gcov_info *info;
309 	int i = 0;
310 
311 	if (node->unloaded_info)
312 		info = gcov_info_dup(node->unloaded_info);
313 	else
314 		info = gcov_info_dup(node->loaded_info[i++]);
315 	if (info == NULL)
316 		return (NULL);
317 	for (; i < node->num_loaded; i++)
318 		gcov_info_add(info, node->loaded_info[i]);
319 
320 	return (info);
321 }
322 
323 /*
324  * open() implementation for gcov data files. Create a copy of the profiling
325  * data set and initialize the iterator and seq_file interface.
326  */
327 static int
328 gcov_seq_open(struct inode *inode, struct file *file)
329 {
330 	struct gcov_node *node = inode->i_private;
331 	struct gcov_iterator *iter;
332 	struct seq_file *seq;
333 	struct gcov_info *info;
334 	int rc = -ENOMEM;
335 
336 	mtx_lock(&node_lock);
337 	/*
338 	 * Read from a profiling data copy to minimize reference tracking
339 	 * complexity and concurrent access and to keep accumulating multiple
340 	 * profiling data sets associated with one node simple.
341 	 */
342 	info = get_accumulated_info(node);
343 	if (info == NULL)
344 		goto out_unlock;
345 	iter = gcov_iter_new(info);
346 	if (iter == NULL)
347 		goto err_free_info;
348 	rc = seq_open(file, &gcov_seq_ops);
349 	if (rc)
350 		goto err_free_iter_info;
351 	seq = file->private_data;
352 	seq->private = iter;
353 out_unlock:
354 	mtx_unlock(&node_lock);
355 	return (rc);
356 
357 err_free_iter_info:
358 	gcov_iter_free(iter);
359 err_free_info:
360 	gcov_info_free(info);
361 	goto out_unlock;
362 }
363 
364 /*
365  * release() implementation for gcov data files. Release resources allocated
366  * by open().
367  */
368 static int
369 gcov_seq_release(struct inode *inode, struct file *file)
370 {
371 	struct gcov_iterator *iter;
372 	struct gcov_info *info;
373 	struct seq_file *seq;
374 
375 	seq = file->private_data;
376 	iter = seq->private;
377 	info = gcov_iter_get_info(iter);
378 	gcov_iter_free(iter);
379 	gcov_info_free(info);
380 	seq_release(inode, file);
381 
382 	return (0);
383 }
384 
385 /*
386  * Find a node by the associated data file name. Needs to be called with
387  * node_lock held.
388  */
389 static struct gcov_node *
390 get_node_by_name(const char *name)
391 {
392 	struct gcov_node *node;
393 	struct gcov_info *info;
394 
395 	LIST_FOREACH(node, &all_head, all_entry) {
396 		info = get_node_info(node);
397 		if (info && (strcmp(gcov_info_filename(info), name) == 0))
398 			return (node);
399 	}
400 
401 	return (NULL);
402 }
403 
404 /*
405  * Reset all profiling data associated with the specified node.
406  */
407 static void
408 reset_node(struct gcov_node *node)
409 {
410 	int i;
411 
412 	if (node->unloaded_info)
413 		gcov_info_reset(node->unloaded_info);
414 	for (i = 0; i < node->num_loaded; i++)
415 		gcov_info_reset(node->loaded_info[i]);
416 }
417 
418 void
419 gcov_stats_reset(void)
420 {
421 	struct gcov_node *node;
422 
423 	mtx_lock(&node_lock);
424  restart:
425 	LIST_FOREACH(node, &all_head, all_entry) {
426 		if (node->num_loaded > 0)
427 			reset_node(node);
428 		else if (LIST_EMPTY(&node->children)) {
429 			remove_node(node);
430 			goto restart;
431 		}
432 	}
433 	mtx_unlock(&node_lock);
434 }
435 
436 /*
437  * write() implementation for gcov data files. Reset profiling data for the
438  * corresponding file. If all associated object files have been unloaded,
439  * remove the debug fs node as well.
440  */
441 static ssize_t
442 gcov_seq_write(struct file *file, const char *addr, size_t len, off_t *pos)
443 {
444 	struct seq_file *seq;
445 	struct gcov_info *info;
446 	struct gcov_node *node;
447 
448 	seq = file->private_data;
449 	info = gcov_iter_get_info(seq->private);
450 	mtx_lock(&node_lock);
451 	node = get_node_by_name(gcov_info_filename(info));
452 	if (node) {
453 		/* Reset counts or remove node for unloaded modules. */
454 		if (node->num_loaded == 0)
455 			remove_node(node);
456 		else
457 			reset_node(node);
458 	}
459 	/* Reset counts for open file. */
460 	gcov_info_reset(info);
461 	mtx_unlock(&node_lock);
462 
463 	return (len);
464 }
465 
466 /*
467  * Given a string <path> representing a file path of format:
468  *   path/to/file.gcda
469  * construct and return a new string:
470  *   <dir/>path/to/file.<ext>
471  */
472 static char *
473 link_target(const char *dir, const char *path, const char *ext)
474 {
475 	char *target;
476 	char *old_ext;
477 	char *copy;
478 
479 	copy = strdup_flags(path, M_GCOV, M_NOWAIT);
480 	if (!copy)
481 		return (NULL);
482 	old_ext = strrchr(copy, '.');
483 	if (old_ext)
484 		*old_ext = '\0';
485 	target = NULL;
486 	if (dir)
487 		asprintf(&target, M_GCOV, "%s/%s.%s", dir, copy, ext);
488 	else
489 		asprintf(&target, M_GCOV, "%s.%s", copy, ext);
490 	free(copy, M_GCOV);
491 
492 	return (target);
493 }
494 
495 /*
496  * Construct a string representing the symbolic link target for the given
497  * gcov data file name and link type. Depending on the link type and the
498  * location of the data file, the link target can either point to a
499  * subdirectory of srctree, objtree or in an external location.
500  */
501 static char *
502 get_link_target(const char *filename, const struct gcov_link *ext)
503 {
504 	const char *rel;
505 	char *result;
506 
507 	if (strncmp(filename, objtree, strlen(objtree)) == 0) {
508 		rel = filename + strlen(objtree) + 1;
509 		if (ext->dir == SRC_TREE)
510 			result = link_target(srctree, rel, ext->ext);
511 		else
512 			result = link_target(objtree, rel, ext->ext);
513 	} else {
514 		/* External compilation. */
515 		result = link_target(NULL, filename, ext->ext);
516 	}
517 
518 	return (result);
519 }
520 
521 #define SKEW_PREFIX	".tmp_"
522 
523 /*
524  * For a filename .tmp_filename.ext return filename.ext. Needed to compensate
525  * for filename skewing caused by the mod-versioning mechanism.
526  */
527 static const char *
528 deskew(const char *basename)
529 {
530 	if (strncmp(basename, SKEW_PREFIX, sizeof(SKEW_PREFIX) - 1) == 0)
531 		return (basename + sizeof(SKEW_PREFIX) - 1);
532 	return (basename);
533 }
534 
535 /*
536  * Create links to additional files (usually .c and .gcno files) which the
537  * gcov tool expects to find in the same directory as the gcov data file.
538  */
539 static void
540 add_links(struct gcov_node *node, struct dentry *parent)
541 {
542 	const char *path_basename;
543 	char *target;
544 	int num;
545 	int i;
546 
547 	for (num = 0; gcov_link[num].ext; num++)
548 		/* Nothing. */;
549 	node->links = malloc((num*sizeof(struct dentry *)), M_GCOV, M_NOWAIT|M_ZERO);
550 	if (node->links == NULL)
551 		return;
552 	for (i = 0; i < num; i++) {
553 		target = get_link_target(
554 				gcov_info_filename(get_node_info(node)),
555 				&gcov_link[i]);
556 		if (target == NULL)
557 			goto out_err;
558 		path_basename = basename(target);
559 		if (path_basename == target)
560 			goto out_err;
561 		node->links[i] = debugfs_create_symlink(deskew(path_basename),
562 							parent,	target);
563 		if (!node->links[i])
564 			goto out_err;
565 		free(target, M_GCOV);
566 	}
567 
568 	return;
569 out_err:
570 	free(target, M_GCOV);
571 	while (i-- > 0)
572 		debugfs_remove(node->links[i]);
573 	free(node->links, M_GCOV);
574 	node->links = NULL;
575 }
576 
577 static const struct file_operations gcov_data_fops = {
578 	.open		= gcov_seq_open,
579 	.release	= gcov_seq_release,
580 	.read		= seq_read,
581 	.llseek		= seq_lseek,
582 	.write		= gcov_seq_write,
583 };
584 
585 /* Basic initialization of a new node. */
586 static void
587 init_node(struct gcov_node *node, struct gcov_info *info,
588    const char *name, struct gcov_node *parent)
589 {
590 	LIST_INIT(&node->children);
591 	if (node->loaded_info) {
592 		node->loaded_info[0] = info;
593 		node->num_loaded = 1;
594 	}
595 	node->parent = parent;
596 	if (name)
597 		strcpy(node->name, name);
598 }
599 
600 /*
601  * Create a new node and associated debugfs entry. Needs to be called with
602  * node_lock held.
603  */
604 static struct gcov_node *
605 new_node(struct gcov_node *parent, struct gcov_info *info, const char *name)
606 {
607 	struct gcov_node *node;
608 
609 	node = malloc(sizeof(struct gcov_node) + strlen(name) + 1, M_GCOV, M_NOWAIT|M_ZERO);
610 	if (!node)
611 		goto err_nomem;
612 	if (info) {
613 		node->loaded_info = malloc(sizeof(struct gcov_info *), M_GCOV, M_NOWAIT|M_ZERO);
614 		if (!node->loaded_info)
615 			goto err_nomem;
616 	}
617 	init_node(node, info, name, parent);
618 	/* Differentiate between gcov data file nodes and directory nodes. */
619 	if (info) {
620 		node->dentry = debugfs_create_file(deskew(node->name), 0600,
621 					parent->dentry, node, &gcov_data_fops);
622 	} else
623 		node->dentry = debugfs_create_dir(node->name, parent->dentry);
624 	if (!node->dentry) {
625 		log(LOG_WARNING, "could not create file\n");
626 		free(node, M_GCOV);
627 		return NULL;
628 	}
629 	if (info)
630 		add_links(node, parent->dentry);
631 	LIST_INSERT_HEAD(&parent->children, node, children_entry);
632 	LIST_INSERT_HEAD(&all_head, node, all_entry);
633 
634 	return (node);
635 
636 err_nomem:
637 	free(node, M_GCOV);
638 	log(LOG_WARNING, "out of memory\n");
639 	return NULL;
640 }
641 
642 /* Remove symbolic links associated with node. */
643 static void
644 remove_links(struct gcov_node *node)
645 {
646 
647 	if (node->links == NULL)
648 		return;
649 	for (int i = 0; gcov_link[i].ext; i++)
650 		debugfs_remove(node->links[i]);
651 	free(node->links, M_GCOV);
652 	node->links = NULL;
653 }
654 
655 /*
656  * Remove node from all lists and debugfs and release associated resources.
657  * Needs to be called with node_lock held.
658  */
659 static void
660 release_node(struct gcov_node *node)
661 {
662 	LIST_REMOVE(node, children_entry);
663 	LIST_REMOVE(node, all_entry);
664 	debugfs_remove(node->dentry);
665 	remove_links(node);
666 	free(node->loaded_info, M_GCOV);
667 	if (node->unloaded_info)
668 		gcov_info_free(node->unloaded_info);
669 	free(node, M_GCOV);
670 }
671 
672 /* Release node and empty parents. Needs to be called with node_lock held. */
673 static void
674 remove_node(struct gcov_node *node)
675 {
676 	struct gcov_node *parent;
677 
678 	while ((node != &root_node) && LIST_EMPTY(&node->children)) {
679 		parent = node->parent;
680 		release_node(node);
681 		node = parent;
682 	}
683 }
684 
685 /*
686  * Find child node with given basename. Needs to be called with node_lock
687  * held.
688  */
689 static struct gcov_node *
690 get_child_by_name(struct gcov_node *parent, const char *name)
691 {
692 	struct gcov_node *node;
693 
694 	LIST_FOREACH(node, &parent->children, children_entry) {
695 		if (strcmp(node->name, name) == 0)
696 			return (node);
697 	}
698 
699 	return (NULL);
700 }
701 
702 /*
703  * Create a node for a given profiling data set and add it to all lists and
704  * debugfs. Needs to be called with node_lock held.
705  */
706 static void
707 add_node(struct gcov_info *info)
708 {
709 	char *filename;
710 	char *curr;
711 	char *next;
712 	struct gcov_node *parent;
713 	struct gcov_node *node;
714 
715 	filename = strdup_flags(gcov_info_filename(info), M_GCOV, M_NOWAIT);
716 	if (filename == NULL)
717 		return;
718 	parent = &root_node;
719 	/* Create directory nodes along the path. */
720 	for (curr = filename; (next = strchr(curr, '/')); curr = next + 1) {
721 		if (curr == next)
722 			continue;
723 		*next = 0;
724 		if (strcmp(curr, ".") == 0)
725 			continue;
726 		if (strcmp(curr, "..") == 0) {
727 			if (!parent->parent)
728 				goto err_remove;
729 			parent = parent->parent;
730 			continue;
731 		}
732 		node = get_child_by_name(parent, curr);
733 		if (!node) {
734 			node = new_node(parent, NULL, curr);
735 			if (!node)
736 				goto err_remove;
737 		}
738 		parent = node;
739 	}
740 	/* Create file node. */
741 	node = new_node(parent, info, curr);
742 	if (!node)
743 		goto err_remove;
744 out:
745 	free(filename, M_GCOV);
746 	return;
747 
748 err_remove:
749 	remove_node(parent);
750 	goto out;
751 }
752 
753 /*
754  * Associate a profiling data set with an existing node. Needs to be called
755  * with node_lock held.
756  */
757 static void
758 add_info(struct gcov_node *node, struct gcov_info *info)
759 {
760 	struct gcov_info **loaded_info;
761 	int num = node->num_loaded;
762 
763 	/*
764 	 * Prepare new array. This is done first to simplify cleanup in
765 	 * case the new data set is incompatible, the node only contains
766 	 * unloaded data sets and there's not enough memory for the array.
767 	 */
768 	loaded_info = malloc((num + 1)* sizeof(struct gcov_info *), M_GCOV, M_NOWAIT|M_ZERO);
769 	if (!loaded_info) {
770 		log(LOG_WARNING, "could not add '%s' (out of memory)\n",
771 			gcov_info_filename(info));
772 		return;
773 	}
774 	memcpy(loaded_info, node->loaded_info,
775 	       num * sizeof(struct gcov_info *));
776 	loaded_info[num] = info;
777 	/* Check if the new data set is compatible. */
778 	if (num == 0) {
779 		/*
780 		 * A module was unloaded, modified and reloaded. The new
781 		 * data set replaces the copy of the last one.
782 		 */
783 		if (!gcov_info_is_compatible(node->unloaded_info, info)) {
784 			log(LOG_WARNING, "discarding saved data for %s "
785 				"(incompatible version)\n",
786 				gcov_info_filename(info));
787 			gcov_info_free(node->unloaded_info);
788 			node->unloaded_info = NULL;
789 		}
790 	} else {
791 		/*
792 		 * Two different versions of the same object file are loaded.
793 		 * The initial one takes precedence.
794 		 */
795 		if (!gcov_info_is_compatible(node->loaded_info[0], info)) {
796 			log(LOG_WARNING, "could not add '%s' (incompatible "
797 				"version)\n", gcov_info_filename(info));
798 			free(loaded_info, M_GCOV);
799 			return;
800 		}
801 	}
802 	/* Overwrite previous array. */
803 	free(node->loaded_info, M_GCOV);
804 	node->loaded_info = loaded_info;
805 	node->num_loaded = num + 1;
806 }
807 
808 /*
809  * Return the index of a profiling data set associated with a node.
810  */
811 static int
812 get_info_index(struct gcov_node *node, struct gcov_info *info)
813 {
814 	int i;
815 
816 	for (i = 0; i < node->num_loaded; i++) {
817 		if (node->loaded_info[i] == info)
818 			return (i);
819 	}
820 	return (ENOENT);
821 }
822 
823 /*
824  * Save the data of a profiling data set which is being unloaded.
825  */
826 static void
827 save_info(struct gcov_node *node, struct gcov_info *info)
828 {
829 	if (node->unloaded_info)
830 		gcov_info_add(node->unloaded_info, info);
831 	else {
832 		node->unloaded_info = gcov_info_dup(info);
833 		if (!node->unloaded_info) {
834 			log(LOG_WARNING, "could not save data for '%s' "
835 				"(out of memory)\n",
836 				gcov_info_filename(info));
837 		}
838 	}
839 }
840 
841 /*
842  * Disassociate a profiling data set from a node. Needs to be called with
843  * node_lock held.
844  */
845 static void
846 remove_info(struct gcov_node *node, struct gcov_info *info)
847 {
848 	int i;
849 
850 	i = get_info_index(node, info);
851 	if (i < 0) {
852 		log(LOG_WARNING, "could not remove '%s' (not found)\n",
853 			gcov_info_filename(info));
854 		return;
855 	}
856 	if (gcov_persist)
857 		save_info(node, info);
858 	/* Shrink array. */
859 	node->loaded_info[i] = node->loaded_info[node->num_loaded - 1];
860 	node->num_loaded--;
861 	if (node->num_loaded > 0)
862 		return;
863 	/* Last loaded data set was removed. */
864 	free(node->loaded_info, M_GCOV);
865 	node->loaded_info = NULL;
866 	node->num_loaded = 0;
867 	if (!node->unloaded_info)
868 		remove_node(node);
869 }
870 
871 /*
872  * Callback to create/remove profiling files when code compiled with
873  * -fprofile-arcs is loaded/unloaded.
874  */
875 static void
876 gcov_event(enum gcov_action action, struct gcov_info *info)
877 {
878 	struct gcov_node *node;
879 
880 	mtx_lock(&node_lock);
881 	node = get_node_by_name(gcov_info_filename(info));
882 	switch (action) {
883 	case GCOV_ADD:
884 		if (node)
885 			add_info(node, info);
886 		else
887 			add_node(info);
888 		break;
889 	case GCOV_REMOVE:
890 		if (node)
891 			remove_info(node, info);
892 		else {
893 			log(LOG_WARNING, "could not remove '%s' (not found)\n",
894 				gcov_info_filename(info));
895 		}
896 		break;
897 	}
898 	mtx_unlock(&node_lock);
899 }
900 
901 /**
902  * gcov_enable_events - enable event reporting through gcov_event()
903  *
904  * Turn on reporting of profiling data load/unload-events through the
905  * gcov_event() callback. Also replay all previous events once. This function
906  * is needed because some events are potentially generated too early for the
907  * callback implementation to handle them initially.
908  */
909 void
910 gcov_enable_events(void)
911 {
912 	struct gcov_info *info = NULL;
913 	int count;
914 
915 	mtx_lock(&gcov_mtx);
916 	count = 0;
917 
918 	/* Perform event callback for previously registered entries. */
919 	while ((info = gcov_info_next(info))) {
920 		gcov_event(GCOV_ADD, info);
921 		sched_relinquish(curthread);
922 		count++;
923 	}
924 
925 	mtx_unlock(&gcov_mtx);
926 	printf("%s found %d events\n", __func__, count);
927 }
928 
929 /* Update list and generate events when modules are unloaded. */
930 void
931 gcov_module_unload(void *arg __unused, module_t mod)
932 {
933 	struct gcov_info *info = NULL;
934 	struct gcov_info *prev = NULL;
935 
936 	mtx_lock(&gcov_mtx );
937 
938 	/* Remove entries located in module from linked list. */
939 	while ((info = gcov_info_next(info))) {
940 		if (within_module((vm_offset_t)info, mod)) {
941 			gcov_info_unlink(prev, info);
942 			if (gcov_events_enabled)
943 				gcov_event(GCOV_REMOVE, info);
944 		} else
945 			prev = info;
946 	}
947 
948 	mtx_unlock(&gcov_mtx);
949 }
950 
951 void
952 gcov_fs_init(void)
953 {
954 	init_node(&root_node, NULL, NULL, NULL);
955 	root_node.dentry = debugfs_create_dir("gcov", NULL);
956 }
957