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(LIBC_SCCS) && !defined(lint)
31static char sccsid[] = "@(#)gmon.c	8.1 (Berkeley) 6/4/93";
32#endif
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD$");
35
36#include "namespace.h"
37#include <sys/param.h>
38#include <sys/time.h>
39#include <sys/gmon.h>
40#include <sys/mman.h>
41#include <sys/sysctl.h>
42
43#include <err.h>
44#include <fcntl.h>
45#include <inttypes.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <unistd.h>
50#include "un-namespace.h"
51
52#include "libc_private.h"
53
54struct gmonparam _gmonparam = { GMON_PROF_OFF };
55
56static int	s_scale;
57/* See profil(2) where this is described (incorrectly). */
58#define	SCALE_SHIFT	16
59
60#define ERR(s) _write(2, s, sizeof(s))
61
62void	moncontrol(int);
63static int hertz(void);
64void	_mcleanup(void);
65
66void
67monstartup(u_long lowpc, u_long highpc)
68{
69	int o;
70	char *cp;
71	struct gmonparam *p = &_gmonparam;
72
73	/*
74	 * round lowpc and highpc to multiples of the density we're using
75	 * so the rest of the scaling (here and in gprof) stays in ints.
76	 */
77	p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
78	p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
79	p->textsize = p->highpc - p->lowpc;
80	p->kcountsize = p->textsize / HISTFRACTION;
81	p->hashfraction = HASHFRACTION;
82	p->fromssize = p->textsize / HASHFRACTION;
83	p->tolimit = p->textsize * ARCDENSITY / 100;
84	if (p->tolimit < MINARCS)
85		p->tolimit = MINARCS;
86	else if (p->tolimit > MAXARCS)
87		p->tolimit = MAXARCS;
88	p->tossize = p->tolimit * sizeof(struct tostruct);
89
90	cp = mmap(NULL, p->kcountsize + p->fromssize + p->tossize,
91	    PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
92	if (cp == MAP_FAILED) {
93		ERR("monstartup: out of memory\n");
94		return;
95	}
96#ifdef notdef
97	bzero(cp, p->kcountsize + p->fromssize + p->tossize);
98#endif
99	p->tos = (struct tostruct *)cp;
100	cp += p->tossize;
101	p->kcount = (u_short *)cp;
102	cp += p->kcountsize;
103	p->froms = (u_short *)cp;
104
105	p->tos[0].link = 0;
106
107	o = p->highpc - p->lowpc;
108	s_scale = (p->kcountsize < o) ?
109	    ((uintmax_t)p->kcountsize << SCALE_SHIFT) / o : (1 << SCALE_SHIFT);
110	moncontrol(1);
111}
112
113void
114_mcleanup(void)
115{
116	int fd;
117	int fromindex;
118	int endfrom;
119	u_long frompc;
120	int toindex;
121	struct rawarc rawarc;
122	struct gmonparam *p = &_gmonparam;
123	struct gmonhdr gmonhdr, *hdr;
124	struct clockinfo clockinfo;
125	char outname[128];
126	int mib[2];
127	size_t size;
128#ifdef DEBUG
129	int log, len;
130	char buf[200];
131#endif
132
133	if (p->state == GMON_PROF_ERROR)
134		ERR("_mcleanup: tos overflow\n");
135
136	size = sizeof(clockinfo);
137	mib[0] = CTL_KERN;
138	mib[1] = KERN_CLOCKRATE;
139	if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
140		/*
141		 * Best guess
142		 */
143		clockinfo.profhz = hertz();
144	} else if (clockinfo.profhz == 0) {
145		if (clockinfo.hz != 0)
146			clockinfo.profhz = clockinfo.hz;
147		else
148			clockinfo.profhz = hertz();
149	}
150
151	moncontrol(0);
152	if (getenv("PROFIL_USE_PID"))
153		snprintf(outname, sizeof(outname), "%s.%d.gmon",
154		    _getprogname(), getpid());
155	else
156		snprintf(outname, sizeof(outname), "%s.gmon", _getprogname());
157
158	fd = _open(outname, O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0666);
159	if (fd < 0) {
160		_warn("_mcleanup: %s", outname);
161		return;
162	}
163#ifdef DEBUG
164	log = _open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0664);
165	if (log < 0) {
166		_warn("_mcleanup: gmon.log");
167		return;
168	}
169	len = sprintf(buf, "[mcleanup1] kcount 0x%p ssiz %lu\n",
170	    p->kcount, p->kcountsize);
171	_write(log, buf, len);
172#endif
173	hdr = (struct gmonhdr *)&gmonhdr;
174	bzero(hdr, sizeof(*hdr));
175	hdr->lpc = p->lowpc;
176	hdr->hpc = p->highpc;
177	hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
178	hdr->version = GMONVERSION;
179	hdr->profrate = clockinfo.profhz;
180	_write(fd, (char *)hdr, sizeof *hdr);
181	_write(fd, p->kcount, p->kcountsize);
182	endfrom = p->fromssize / sizeof(*p->froms);
183	for (fromindex = 0; fromindex < endfrom; fromindex++) {
184		if (p->froms[fromindex] == 0)
185			continue;
186
187		frompc = p->lowpc;
188		frompc += fromindex * p->hashfraction * sizeof(*p->froms);
189		for (toindex = p->froms[fromindex]; toindex != 0;
190		     toindex = p->tos[toindex].link) {
191#ifdef DEBUG
192			len = sprintf(buf,
193			"[mcleanup2] frompc 0x%lx selfpc 0x%lx count %lu\n" ,
194				frompc, p->tos[toindex].selfpc,
195				p->tos[toindex].count);
196			_write(log, buf, len);
197#endif
198			rawarc.raw_frompc = frompc;
199			rawarc.raw_selfpc = p->tos[toindex].selfpc;
200			rawarc.raw_count = p->tos[toindex].count;
201			_write(fd, &rawarc, sizeof rawarc);
202		}
203	}
204	_close(fd);
205}
206
207/*
208 * Control profiling
209 *	profiling is what mcount checks to see if
210 *	all the data structures are ready.
211 */
212void
213moncontrol(int mode)
214{
215	struct gmonparam *p = &_gmonparam;
216
217	if (mode) {
218		/* start */
219		profil((char *)p->kcount, p->kcountsize, p->lowpc, s_scale);
220		p->state = GMON_PROF_ON;
221	} else {
222		/* stop */
223		profil((char *)0, 0, 0, 0);
224		p->state = GMON_PROF_OFF;
225	}
226}
227
228/*
229 * discover the tick frequency of the machine
230 * if something goes wrong, we return 0, an impossible hertz.
231 */
232static int
233hertz(void)
234{
235	struct itimerval tim;
236
237	tim.it_interval.tv_sec = 0;
238	tim.it_interval.tv_usec = 1;
239	tim.it_value.tv_sec = 0;
240	tim.it_value.tv_usec = 0;
241	setitimer(ITIMER_REAL, &tim, 0);
242	setitimer(ITIMER_REAL, 0, &tim);
243	if (tim.it_interval.tv_usec < 2)
244		return(0);
245	return (1000000 / tim.it_interval.tv_usec);
246}
247