2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019, Matthew Macy <mmacy@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/linker.h>
39 #include <sys/module.h>
40 #include <sys/eventhandler.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/syslog.h>
45 #include <sys/sched.h>
46 #include <sys/syslog.h>
47 #include <sys/sysctl.h>
48 #include <gnu/gcov/gcov.h>
49 #include <sys/queue.h>
50 #include "linker_if.h"
53 static void gcov_invoke_ctors(void);
54 static int gcov_ctors_done;
55 int gcov_events_enabled;
58 gcov_stats_reset_sysctl(SYSCTL_HANDLER_ARGS)
63 error = sysctl_handle_int(oidp, &v, 0, req);
66 if (req->newptr == NULL)
76 gcov_stats_enable_sysctl(SYSCTL_HANDLER_ARGS)
80 v = gcov_events_enabled;
81 error = sysctl_handle_int(oidp, &v, v, req);
84 if (req->newptr == NULL)
86 if (v == gcov_events_enabled)
88 //gcov_events_reset();
89 gcov_events_enabled = !!v;
92 if (gcov_events_enabled)
99 within_module(vm_offset_t addr, module_t mod)
101 linker_file_t link_info;
102 vm_offset_t mod_addr;
105 link_info = module_file(mod);
106 mod_addr = (vm_offset_t)link_info->address;
107 mod_size = link_info->size;
108 if (addr >= mod_addr && addr < mod_addr + mod_size)
115 #define GCOV_PREFIX "_GLOBAL__sub_I_65535_0_"
118 gcov_invoke_ctor(const char *name, void *arg)
122 linker_symval_t symval;
125 if (strstr(name, GCOV_PREFIX) == NULL)
128 LINKER_LOOKUP_SYMBOL(lf, name, &sym);
129 LINKER_SYMBOL_VALUES(lf, sym, &symval);
130 ctor = (void *)symval.value;
136 gcov_invoke_lf_ctors(linker_file_t lf, void *arg __unused)
139 printf("%s processing file: %s\n", __func__, lf->filename);
140 LINKER_EACH_FUNCTION_NAME(lf, gcov_invoke_ctor, lf);
145 gcov_invoke_ctors(void)
150 linker_file_foreach(gcov_invoke_lf_ctors, NULL);
155 gcov_init(void *arg __unused)
157 EVENTHANDLER_REGISTER(module_unload, gcov_module_unload, NULL, 0);
158 gcov_enable_events();
162 SYSINIT(gcov_init, SI_SUB_EVENTHANDLER, SI_ORDER_ANY, gcov_init, NULL);
164 static SYSCTL_NODE(_debug, OID_AUTO, gcov, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
165 "gcov code coverage");
166 SYSCTL_PROC(_debug_gcov, OID_AUTO, reset,
167 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
168 gcov_stats_reset_sysctl, "I",
169 "Reset all profiling counts");
170 SYSCTL_PROC(_debug_gcov, OID_AUTO, enable,
171 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
172 gcov_stats_enable_sysctl, "I",
173 "Enable code coverage");