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