1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1983, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #if defined(LIBC_SCCS) && !defined(lint) 33 static char sccsid[] = "@(#)gmon.c 8.1 (Berkeley) 6/4/93"; 34 #endif 35 #include "namespace.h" 36 #include <sys/param.h> 37 #include <sys/time.h> 38 #include <sys/gmon.h> 39 #include <sys/mman.h> 40 #include <sys/sysctl.h> 41 42 #include <err.h> 43 #include <fcntl.h> 44 #include <inttypes.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 #include "un-namespace.h" 50 51 #include "libc_private.h" 52 53 struct gmonparam _gmonparam = { GMON_PROF_OFF }; 54 55 static int s_scale; 56 /* See profil(2) where this is described (incorrectly). */ 57 #define SCALE_SHIFT 16 58 59 #define ERR(s) _write(2, s, sizeof(s)) 60 61 void moncontrol(int); 62 static int hertz(void); 63 void _mcleanup(void); 64 65 void 66 monstartup(u_long lowpc, u_long highpc) 67 { 68 int o; 69 char *cp; 70 struct gmonparam *p = &_gmonparam; 71 72 /* 73 * round lowpc and highpc to multiples of the density we're using 74 * so the rest of the scaling (here and in gprof) stays in ints. 75 */ 76 p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER)); 77 p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER)); 78 p->textsize = p->highpc - p->lowpc; 79 p->kcountsize = p->textsize / HISTFRACTION; 80 p->hashfraction = HASHFRACTION; 81 p->fromssize = p->textsize / HASHFRACTION; 82 p->tolimit = p->textsize * ARCDENSITY / 100; 83 if (p->tolimit < MINARCS) 84 p->tolimit = MINARCS; 85 else if (p->tolimit > MAXARCS) 86 p->tolimit = MAXARCS; 87 p->tossize = p->tolimit * sizeof(struct tostruct); 88 89 cp = mmap(NULL, p->kcountsize + p->fromssize + p->tossize, 90 PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); 91 if (cp == MAP_FAILED) { 92 ERR("monstartup: out of memory\n"); 93 return; 94 } 95 #ifdef notdef 96 bzero(cp, p->kcountsize + p->fromssize + p->tossize); 97 #endif 98 p->tos = (struct tostruct *)cp; 99 cp += p->tossize; 100 p->kcount = (u_short *)cp; 101 cp += p->kcountsize; 102 p->froms = (u_short *)cp; 103 104 p->tos[0].link = 0; 105 106 o = p->highpc - p->lowpc; 107 s_scale = (p->kcountsize < o) ? 108 ((uintmax_t)p->kcountsize << SCALE_SHIFT) / o : (1 << SCALE_SHIFT); 109 moncontrol(1); 110 } 111 112 void 113 _mcleanup(void) 114 { 115 int fd; 116 int fromindex; 117 int endfrom; 118 u_long frompc; 119 int toindex; 120 struct rawarc rawarc; 121 struct gmonparam *p = &_gmonparam; 122 struct gmonhdr gmonhdr, *hdr; 123 struct clockinfo clockinfo; 124 char outname[128]; 125 int mib[2]; 126 size_t size; 127 #ifdef DEBUG 128 int log, len; 129 char buf[200]; 130 #endif 131 132 if (p->state == GMON_PROF_ERROR) 133 ERR("_mcleanup: tos overflow\n"); 134 135 size = sizeof(clockinfo); 136 mib[0] = CTL_KERN; 137 mib[1] = KERN_CLOCKRATE; 138 if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) { 139 /* 140 * Best guess 141 */ 142 clockinfo.profhz = hertz(); 143 } else if (clockinfo.profhz == 0) { 144 if (clockinfo.hz != 0) 145 clockinfo.profhz = clockinfo.hz; 146 else 147 clockinfo.profhz = hertz(); 148 } 149 150 moncontrol(0); 151 if (getenv("PROFIL_USE_PID")) 152 snprintf(outname, sizeof(outname), "%s.%d.gmon", 153 _getprogname(), getpid()); 154 else 155 snprintf(outname, sizeof(outname), "%s.gmon", _getprogname()); 156 157 fd = _open(outname, O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0666); 158 if (fd < 0) { 159 _warn("_mcleanup: %s", outname); 160 return; 161 } 162 #ifdef DEBUG 163 log = _open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0664); 164 if (log < 0) { 165 _warn("_mcleanup: gmon.log"); 166 return; 167 } 168 len = sprintf(buf, "[mcleanup1] kcount 0x%p ssiz %lu\n", 169 p->kcount, p->kcountsize); 170 _write(log, buf, len); 171 #endif 172 hdr = (struct gmonhdr *)&gmonhdr; 173 bzero(hdr, sizeof(*hdr)); 174 hdr->lpc = p->lowpc; 175 hdr->hpc = p->highpc; 176 hdr->ncnt = p->kcountsize + sizeof(gmonhdr); 177 hdr->version = GMONVERSION; 178 hdr->profrate = clockinfo.profhz; 179 _write(fd, (char *)hdr, sizeof *hdr); 180 _write(fd, p->kcount, p->kcountsize); 181 endfrom = p->fromssize / sizeof(*p->froms); 182 for (fromindex = 0; fromindex < endfrom; fromindex++) { 183 if (p->froms[fromindex] == 0) 184 continue; 185 186 frompc = p->lowpc; 187 frompc += fromindex * p->hashfraction * sizeof(*p->froms); 188 for (toindex = p->froms[fromindex]; toindex != 0; 189 toindex = p->tos[toindex].link) { 190 #ifdef DEBUG 191 len = sprintf(buf, 192 "[mcleanup2] frompc 0x%lx selfpc 0x%lx count %lu\n" , 193 frompc, p->tos[toindex].selfpc, 194 p->tos[toindex].count); 195 _write(log, buf, len); 196 #endif 197 rawarc.raw_frompc = frompc; 198 rawarc.raw_selfpc = p->tos[toindex].selfpc; 199 rawarc.raw_count = p->tos[toindex].count; 200 _write(fd, &rawarc, sizeof rawarc); 201 } 202 } 203 _close(fd); 204 } 205 206 /* 207 * Control profiling 208 * profiling is what mcount checks to see if 209 * all the data structures are ready. 210 */ 211 void 212 moncontrol(int mode) 213 { 214 struct gmonparam *p = &_gmonparam; 215 216 if (mode) { 217 /* start */ 218 profil((char *)p->kcount, p->kcountsize, p->lowpc, s_scale); 219 p->state = GMON_PROF_ON; 220 } else { 221 /* stop */ 222 profil((char *)0, 0, 0, 0); 223 p->state = GMON_PROF_OFF; 224 } 225 } 226 227 /* 228 * discover the tick frequency of the machine 229 * if something goes wrong, we return 0, an impossible hertz. 230 */ 231 static int 232 hertz(void) 233 { 234 struct itimerval tim; 235 236 tim.it_interval.tv_sec = 0; 237 tim.it_interval.tv_usec = 1; 238 tim.it_value.tv_sec = 0; 239 tim.it_value.tv_usec = 0; 240 setitimer(ITIMER_REAL, &tim, 0); 241 setitimer(ITIMER_REAL, 0, &tim); 242 if (tim.it_interval.tv_usec < 2) 243 return(0); 244 return (1000000 / tim.it_interval.tv_usec); 245 } 246