xref: /freebsd/sys/gnu/gcov/gcov_subr.c (revision 1719886f6d08408b834d270c59ffcfd821c8f63a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2019, Matthew Macy <mmacy@freebsd.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/types.h>
30 #include <sys/systm.h>
31 #include <sys/param.h>
32 #include <sys/sbuf.h>
33 
34 #include <sys/queue.h>
35 #include <sys/linker.h>
36 #include <sys/module.h>
37 #include <sys/eventhandler.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/syslog.h>
41 #include <sys/proc.h>
42 #include <sys/sched.h>
43 #include <sys/syslog.h>
44 #include <sys/sysctl.h>
45 #include <gnu/gcov/gcov.h>
46 #include <sys/queue.h>
47 #include "linker_if.h"
48 
49 
50 static void gcov_invoke_ctors(void);
51 static int gcov_ctors_done;
52 int gcov_events_enabled;
53 
54 static int
55 gcov_stats_reset_sysctl(SYSCTL_HANDLER_ARGS)
56 {
57 	int error, v;
58 
59 	v = 0;
60 	error = sysctl_handle_int(oidp, &v, 0, req);
61 	if (error)
62 		return (error);
63 	if (req->newptr == NULL)
64 		return (error);
65 	if (v == 0)
66 		return (0);
67 	gcov_stats_reset();
68 
69 	return (0);
70 }
71 
72 static int
73 gcov_stats_enable_sysctl(SYSCTL_HANDLER_ARGS)
74 {
75 	int error, v;
76 
77 	v = gcov_events_enabled;
78 	error = sysctl_handle_int(oidp, &v, v, req);
79 	if (error)
80 		return (error);
81 	if (req->newptr == NULL)
82 		return (error);
83 	if (v == gcov_events_enabled)
84 		return (0);
85 	//gcov_events_reset();
86 	gcov_events_enabled = !!v;
87 	if (!gcov_ctors_done)
88 		gcov_invoke_ctors();
89 	if (gcov_events_enabled)
90 		gcov_enable_events();
91 
92 	return (0);
93 }
94 
95 int
96 within_module(vm_offset_t addr, module_t mod)
97 {
98 	linker_file_t link_info;
99 	vm_offset_t mod_addr;
100 	size_t mod_size;
101 
102 	link_info = module_file(mod);
103 	mod_addr = (vm_offset_t)link_info->address;
104 	mod_size = link_info->size;
105 	if (addr >= mod_addr && addr < mod_addr + mod_size)
106 		return (1);
107 	return (0);
108 }
109 
110 
111 
112 #define GCOV_PREFIX "_GLOBAL__sub_I_65535_0_"
113 
114 static int
115 gcov_invoke_ctor(const char *name, void *arg)
116 {
117 	void (*ctor)(void);
118 	c_linker_sym_t sym;
119 	linker_symval_t symval;
120 	linker_file_t lf;
121 
122 	if (strstr(name, GCOV_PREFIX) == NULL)
123 		return (0);
124 	lf = arg;
125 	LINKER_LOOKUP_SYMBOL(lf, name, &sym);
126 	LINKER_SYMBOL_VALUES(lf, sym, &symval);
127 	ctor = (void *)symval.value;
128 	ctor();
129 	return (0);
130 }
131 
132 static int
133 gcov_invoke_lf_ctors(linker_file_t lf, void *arg __unused)
134 {
135 
136 	printf("%s processing file: %s\n", __func__, lf->filename);
137 	LINKER_EACH_FUNCTION_NAME(lf, gcov_invoke_ctor, lf);
138 	return (0);
139 }
140 
141 static void
142 gcov_invoke_ctors(void)
143 {
144 
145 	gcov_fs_init();
146 
147 	linker_file_foreach(gcov_invoke_lf_ctors, NULL);
148 	gcov_ctors_done = 1;
149 }
150 
151 static int
152 gcov_init(void *arg __unused)
153 {
154 	EVENTHANDLER_REGISTER(module_unload, gcov_module_unload, NULL, 0);
155 	gcov_enable_events();
156 	return (0);
157 }
158 
159 SYSINIT(gcov_init, SI_SUB_EVENTHANDLER, SI_ORDER_ANY, gcov_init, NULL);
160 
161 static SYSCTL_NODE(_debug, OID_AUTO, gcov, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
162     "gcov code coverage");
163 SYSCTL_PROC(_debug_gcov, OID_AUTO, reset,
164     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
165     gcov_stats_reset_sysctl, "I",
166     "Reset all profiling counts");
167 SYSCTL_PROC(_debug_gcov, OID_AUTO, enable,
168     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
169     gcov_stats_enable_sysctl, "I",
170     "Enable code coverage");
171