xref: /openbsd/lib/libc/gmon/gmon.c (revision 39152b4f)
1 /*	$OpenBSD: gmon.c,v 1.33 2022/07/26 04:07:13 cheloha Exp $ */
2 /*-
3  * Copyright (c) 1983, 1992, 1993
4  *	The Regents of the University of California.  All rights reserved.
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  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/time.h>
32 #include <sys/gmon.h>
33 #include <sys/mman.h>
34 #include <sys/sysctl.h>
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <fcntl.h>
40 #include <limits.h>
41 #include <unistd.h>
42 
43 struct gmonparam _gmonparam = { GMON_PROF_OFF };
44 
45 static int	s_scale;
46 /* see profil(2) where this is describe (incorrectly) */
47 #define		SCALE_1_TO_1	0x10000L
48 
49 #define ERR(s) write(STDERR_FILENO, s, sizeof(s))
50 
51 PROTO_NORMAL(moncontrol);
52 PROTO_DEPRECATED(monstartup);
53 
54 void
monstartup(u_long lowpc,u_long highpc)55 monstartup(u_long lowpc, u_long highpc)
56 {
57 	int o;
58 	void *addr;
59 	struct gmonparam *p = &_gmonparam;
60 
61 	/*
62 	 * round lowpc and highpc to multiples of the density we're using
63 	 * so the rest of the scaling (here and in gprof) stays in ints.
64 	 */
65 	p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
66 	p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
67 	p->textsize = p->highpc - p->lowpc;
68 	p->kcountsize = p->textsize / HISTFRACTION;
69 	p->hashfraction = HASHFRACTION;
70 	p->fromssize = p->textsize / p->hashfraction;
71 	p->tolimit = p->textsize * ARCDENSITY / 100;
72 	if (p->tolimit < MINARCS)
73 		p->tolimit = MINARCS;
74 	else if (p->tolimit > MAXARCS)
75 		p->tolimit = MAXARCS;
76 	p->tossize = p->tolimit * sizeof(struct tostruct);
77 
78 	addr = mmap(NULL, p->kcountsize,  PROT_READ|PROT_WRITE,
79 	    MAP_ANON|MAP_PRIVATE, -1, 0);
80 	if (addr == MAP_FAILED)
81 		goto mapfailed;
82 	p->kcount = addr;
83 
84 	addr = mmap(NULL, p->fromssize,  PROT_READ|PROT_WRITE,
85 	    MAP_ANON|MAP_PRIVATE, -1, 0);
86 	if (addr == MAP_FAILED)
87 		goto mapfailed;
88 	p->froms = addr;
89 
90 	addr = mmap(NULL, p->tossize,  PROT_READ|PROT_WRITE,
91 	    MAP_ANON|MAP_PRIVATE, -1, 0);
92 	if (addr == MAP_FAILED)
93 		goto mapfailed;
94 	p->tos = addr;
95 	p->tos[0].link = 0;
96 
97 	o = p->highpc - p->lowpc;
98 	if (p->kcountsize < o) {
99 #ifndef notdef
100 		s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
101 #else /* avoid floating point */
102 		int quot = o / p->kcountsize;
103 
104 		if (quot >= 0x10000)
105 			s_scale = 1;
106 		else if (quot >= 0x100)
107 			s_scale = 0x10000 / quot;
108 		else if (o >= 0x800000)
109 			s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
110 		else
111 			s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
112 #endif
113 	} else
114 		s_scale = SCALE_1_TO_1;
115 
116 	moncontrol(1);
117 	return;
118 
119 mapfailed:
120 	if (p->kcount != NULL) {
121 		munmap(p->kcount, p->kcountsize);
122 		p->kcount = NULL;
123 	}
124 	if (p->froms != NULL) {
125 		munmap(p->froms, p->fromssize);
126 		p->froms = NULL;
127 	}
128 	if (p->tos != NULL) {
129 		munmap(p->tos, p->tossize);
130 		p->tos = NULL;
131 	}
132 	ERR("monstartup: out of memory\n");
133 }
134 __strong_alias(_monstartup,monstartup);
135 
136 void
_mcleanup(void)137 _mcleanup(void)
138 {
139 	int fd;
140 	int fromindex;
141 	int endfrom;
142 	u_long frompc;
143 	int toindex;
144 	struct rawarc rawarc;
145 	struct gmonparam *p = &_gmonparam;
146 	struct gmonhdr gmonhdr, *hdr;
147 	struct clockinfo clockinfo;
148 	const int mib[2] = { CTL_KERN, KERN_CLOCKRATE };
149 	size_t size;
150 	char *profdir;
151 	char *proffile;
152 	char  buf[PATH_MAX];
153 #ifdef DEBUG
154 	int log, len;
155 	char dbuf[200];
156 #endif
157 
158 	if (p->state == GMON_PROF_ERROR)
159 		ERR("_mcleanup: tos overflow\n");
160 
161 	/*
162 	 * There is nothing we can do if sysctl(2) fails or if
163 	 * clockinfo.hz is unset.
164 	 */
165 	size = sizeof(clockinfo);
166 	if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) == -1) {
167 		clockinfo.profhz = 0;
168 	} else if (clockinfo.profhz == 0) {
169 		clockinfo.profhz = clockinfo.hz;	/* best guess */
170 	}
171 
172 	moncontrol(0);
173 
174 	if (issetugid() == 0 && (profdir = getenv("PROFDIR")) != NULL) {
175 		char *s, *t, *limit;
176 		pid_t pid;
177 		long divisor;
178 
179 		/* If PROFDIR contains a null value, no profiling
180 		   output is produced */
181 		if (*profdir == '\0') {
182 			return;
183 		}
184 
185 		limit = buf + sizeof buf - 1 - 10 - 1 -
186 		    strlen(__progname) - 1;
187 		t = buf;
188 		s = profdir;
189 		while((*t = *s) != '\0' && t < limit) {
190 			t++;
191 			s++;
192 		}
193 		*t++ = '/';
194 
195 		/*
196 		 * Copy and convert pid from a pid_t to a string.  For
197 		 * best performance, divisor should be initialized to
198 		 * the largest power of 10 less than PID_MAX.
199 		 */
200 		pid = getpid();
201 		divisor=10000;
202 		while (divisor > pid) divisor /= 10;	/* skip leading zeros */
203 		do {
204 			*t++ = (pid/divisor) + '0';
205 			pid %= divisor;
206 		} while (divisor /= 10);
207 		*t++ = '.';
208 
209 		s = __progname;
210 		while ((*t++ = *s++) != '\0')
211 			;
212 
213 		proffile = buf;
214 	} else {
215 		proffile = "gmon.out";
216 	}
217 
218 	fd = open(proffile , O_CREAT|O_TRUNC|O_WRONLY, 0664);
219 	if (fd == -1) {
220 		perror( proffile );
221 		return;
222 	}
223 #ifdef DEBUG
224 	log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
225 	if (log == -1) {
226 		perror("mcount: gmon.log");
227 		close(fd);
228 		return;
229 	}
230 	snprintf(dbuf, sizeof dbuf, "[mcleanup1] kcount 0x%x ssiz %d\n",
231 	    p->kcount, p->kcountsize);
232 	write(log, dbuf, strlen(dbuf));
233 #endif
234 	hdr = (struct gmonhdr *)&gmonhdr;
235 	bzero(hdr, sizeof(*hdr));
236 	hdr->lpc = p->lowpc;
237 	hdr->hpc = p->highpc;
238 	hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
239 	hdr->version = GMONVERSION;
240 	hdr->profrate = clockinfo.profhz;
241 	write(fd, (char *)hdr, sizeof *hdr);
242 	write(fd, p->kcount, p->kcountsize);
243 	endfrom = p->fromssize / sizeof(*p->froms);
244 	for (fromindex = 0; fromindex < endfrom; fromindex++) {
245 		if (p->froms[fromindex] == 0)
246 			continue;
247 
248 		frompc = p->lowpc;
249 		frompc += fromindex * p->hashfraction * sizeof(*p->froms);
250 		for (toindex = p->froms[fromindex]; toindex != 0;
251 		     toindex = p->tos[toindex].link) {
252 #ifdef DEBUG
253 			(void) snprintf(dbuf, sizeof dbuf,
254 			"[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
255 				frompc, p->tos[toindex].selfpc,
256 				p->tos[toindex].count);
257 			write(log, dbuf, strlen(dbuf));
258 #endif
259 			rawarc.raw_frompc = frompc;
260 			rawarc.raw_selfpc = p->tos[toindex].selfpc;
261 			rawarc.raw_count = p->tos[toindex].count;
262 			write(fd, &rawarc, sizeof rawarc);
263 		}
264 	}
265 	close(fd);
266 #ifdef notyet
267 	if (p->kcount != NULL) {
268 		munmap(p->kcount, p->kcountsize);
269 		p->kcount = NULL;
270 	}
271 	if (p->froms != NULL) {
272 		munmap(p->froms, p->fromssize);
273 		p->froms = NULL;
274 	}
275 	if (p->tos != NULL) {
276 		munmap(p->tos, p->tossize);
277 		p->tos = NULL;
278 	}
279 #endif
280 }
281 
282 /*
283  * Control profiling
284  *	profiling is what mcount checks to see if
285  *	all the data structures are ready.
286  */
287 void
moncontrol(int mode)288 moncontrol(int mode)
289 {
290 	struct gmonparam *p = &_gmonparam;
291 
292 	if (mode) {
293 		/* start */
294 		profil((char *)p->kcount, p->kcountsize, p->lowpc,
295 		    s_scale);
296 		p->state = GMON_PROF_ON;
297 	} else {
298 		/* stop */
299 		profil(NULL, 0, 0, 0);
300 		p->state = GMON_PROF_OFF;
301 	}
302 }
303 DEF_WEAK(moncontrol);
304