1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 __FBSDID("$FreeBSD$"); 31 32 #include <sys/types.h> 33 #include <sys/systm.h> 34 #include <sys/param.h> 35 #include <sys/sbuf.h> 36 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> 44 #include <sys/proc.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" 51 52 53 static void gcov_invoke_ctors(void); 54 static int gcov_ctors_done; 55 int gcov_events_enabled; 56 57 static int 58 gcov_stats_reset_sysctl(SYSCTL_HANDLER_ARGS) 59 { 60 int error, v; 61 62 v = 0; 63 error = sysctl_handle_int(oidp, &v, 0, req); 64 if (error) 65 return (error); 66 if (req->newptr == NULL) 67 return (error); 68 if (v == 0) 69 return (0); 70 gcov_stats_reset(); 71 72 return (0); 73 } 74 75 static int 76 gcov_stats_enable_sysctl(SYSCTL_HANDLER_ARGS) 77 { 78 int error, v; 79 80 v = gcov_events_enabled; 81 error = sysctl_handle_int(oidp, &v, v, req); 82 if (error) 83 return (error); 84 if (req->newptr == NULL) 85 return (error); 86 if (v == gcov_events_enabled) 87 return (0); 88 //gcov_events_reset(); 89 gcov_events_enabled = !!v; 90 if (!gcov_ctors_done) 91 gcov_invoke_ctors(); 92 if (gcov_events_enabled) 93 gcov_enable_events(); 94 95 return (0); 96 } 97 98 int 99 within_module(vm_offset_t addr, module_t mod) 100 { 101 linker_file_t link_info; 102 vm_offset_t mod_addr; 103 size_t mod_size; 104 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) 109 return (1); 110 return (0); 111 } 112 113 114 115 #define GCOV_PREFIX "_GLOBAL__sub_I_65535_0_" 116 117 static int 118 gcov_invoke_ctor(const char *name, void *arg) 119 { 120 void (*ctor)(void); 121 c_linker_sym_t sym; 122 linker_symval_t symval; 123 linker_file_t lf; 124 125 if (strstr(name, GCOV_PREFIX) == NULL) 126 return (0); 127 lf = arg; 128 LINKER_LOOKUP_SYMBOL(lf, name, &sym); 129 LINKER_SYMBOL_VALUES(lf, sym, &symval); 130 ctor = (void *)symval.value; 131 ctor(); 132 return (0); 133 } 134 135 static int 136 gcov_invoke_lf_ctors(linker_file_t lf, void *arg __unused) 137 { 138 139 printf("%s processing file: %s\n", __func__, lf->filename); 140 LINKER_EACH_FUNCTION_NAME(lf, gcov_invoke_ctor, lf); 141 return (0); 142 } 143 144 static void 145 gcov_invoke_ctors(void) 146 { 147 148 gcov_fs_init(); 149 150 linker_file_foreach(gcov_invoke_lf_ctors, NULL); 151 gcov_ctors_done = 1; 152 } 153 154 static int 155 gcov_init(void *arg __unused) 156 { 157 EVENTHANDLER_REGISTER(module_unload, gcov_module_unload, NULL, 0); 158 gcov_enable_events(); 159 return (0); 160 } 161 162 SYSINIT(gcov_init, SI_SUB_EVENTHANDLER, SI_ORDER_ANY, gcov_init, NULL); 163 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"); 174