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/cdefs.h>
30 #include <sys/types.h>
31 #include <sys/systm.h>
32 #include <sys/param.h>
33 #include <sys/sbuf.h>
34
35 #include <sys/queue.h>
36 #include <sys/linker.h>
37 #include <sys/module.h>
38 #include <sys/eventhandler.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/syslog.h>
42 #include <sys/proc.h>
43 #include <sys/sched.h>
44 #include <sys/syslog.h>
45 #include <sys/sysctl.h>
46 #include <gnu/gcov/gcov.h>
47 #include <sys/queue.h>
48 #include "linker_if.h"
49
50
51 static void gcov_invoke_ctors(void);
52 static int gcov_ctors_done;
53 int gcov_events_enabled;
54
55 static int
gcov_stats_reset_sysctl(SYSCTL_HANDLER_ARGS)56 gcov_stats_reset_sysctl(SYSCTL_HANDLER_ARGS)
57 {
58 int error, v;
59
60 v = 0;
61 error = sysctl_handle_int(oidp, &v, 0, req);
62 if (error)
63 return (error);
64 if (req->newptr == NULL)
65 return (error);
66 if (v == 0)
67 return (0);
68 gcov_stats_reset();
69
70 return (0);
71 }
72
73 static int
gcov_stats_enable_sysctl(SYSCTL_HANDLER_ARGS)74 gcov_stats_enable_sysctl(SYSCTL_HANDLER_ARGS)
75 {
76 int error, v;
77
78 v = gcov_events_enabled;
79 error = sysctl_handle_int(oidp, &v, v, req);
80 if (error)
81 return (error);
82 if (req->newptr == NULL)
83 return (error);
84 if (v == gcov_events_enabled)
85 return (0);
86 //gcov_events_reset();
87 gcov_events_enabled = !!v;
88 if (!gcov_ctors_done)
89 gcov_invoke_ctors();
90 if (gcov_events_enabled)
91 gcov_enable_events();
92
93 return (0);
94 }
95
96 int
within_module(vm_offset_t addr,module_t mod)97 within_module(vm_offset_t addr, module_t mod)
98 {
99 linker_file_t link_info;
100 vm_offset_t mod_addr;
101 size_t mod_size;
102
103 link_info = module_file(mod);
104 mod_addr = (vm_offset_t)link_info->address;
105 mod_size = link_info->size;
106 if (addr >= mod_addr && addr < mod_addr + mod_size)
107 return (1);
108 return (0);
109 }
110
111
112
113 #define GCOV_PREFIX "_GLOBAL__sub_I_65535_0_"
114
115 static int
gcov_invoke_ctor(const char * name,void * arg)116 gcov_invoke_ctor(const char *name, void *arg)
117 {
118 void (*ctor)(void);
119 c_linker_sym_t sym;
120 linker_symval_t symval;
121 linker_file_t lf;
122
123 if (strstr(name, GCOV_PREFIX) == NULL)
124 return (0);
125 lf = arg;
126 LINKER_LOOKUP_SYMBOL(lf, name, &sym);
127 LINKER_SYMBOL_VALUES(lf, sym, &symval);
128 ctor = (void *)symval.value;
129 ctor();
130 return (0);
131 }
132
133 static int
gcov_invoke_lf_ctors(linker_file_t lf,void * arg __unused)134 gcov_invoke_lf_ctors(linker_file_t lf, void *arg __unused)
135 {
136
137 printf("%s processing file: %s\n", __func__, lf->filename);
138 LINKER_EACH_FUNCTION_NAME(lf, gcov_invoke_ctor, lf);
139 return (0);
140 }
141
142 static void
gcov_invoke_ctors(void)143 gcov_invoke_ctors(void)
144 {
145
146 gcov_fs_init();
147
148 linker_file_foreach(gcov_invoke_lf_ctors, NULL);
149 gcov_ctors_done = 1;
150 }
151
152 static int
gcov_init(void * arg __unused)153 gcov_init(void *arg __unused)
154 {
155 EVENTHANDLER_REGISTER(module_unload, gcov_module_unload, NULL, 0);
156 gcov_enable_events();
157 return (0);
158 }
159
160 SYSINIT(gcov_init, SI_SUB_EVENTHANDLER, SI_ORDER_ANY, gcov_init, NULL);
161
162 static SYSCTL_NODE(_debug, OID_AUTO, gcov, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
163 "gcov code coverage");
164 SYSCTL_PROC(_debug_gcov, OID_AUTO, reset,
165 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
166 gcov_stats_reset_sysctl, "I",
167 "Reset all profiling counts");
168 SYSCTL_PROC(_debug_gcov, OID_AUTO, enable,
169 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
170 gcov_stats_enable_sysctl, "I",
171 "Enable code coverage");
172