xref: /titanic_41/usr/src/lib/libbc/csu/common/mon.c (revision 4610e4a00999c6d2291b3fc263926b890ec500a5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1989 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  *	Environment variable PROFDIR added such that:
31  *		If PROFDIR doesn't exist, "mon.out" is produced as before.
32  *		If PROFDIR = NULL, no profiling output is produced.
33  *		If PROFDIR = string, "string/pid.progname" is produced,
34  *		  where name consists of argv[0] suitably massaged.
35  */
36 #include <sys/param.h>
37 #include <sys/dir.h>
38 #include "mon.h"
39 #include <sys/fcntl.h>
40 #include <unistd.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #define PROFDIR	"PROFDIR"
45 
46 extern void	profil(), perror();
47 
48 void	monitor(char *, char *, char *, int, int);
49 void	moncontrol(int);
50 
51 char **___Argv = NULL; /* initialized to argv array by mcrt0 (if loaded) */
52 
53 struct cnt	*countbase;
54 int		numctrs;
55 int		profiling;
56 
57 static struct mondata {
58 	char	*s_sbuf;
59 	int 	s_bufsiz;
60 	int	s_scale;
61 	int	s_lowpc;
62 	char	mon_out[MAXPATHLEN];
63 	char	progname[MAXNAMLEN];
64 } *mondata, *_mondata();
65 
66 #define	MSG "No space for monitor buffer(s)\n"
67 
68 static struct mondata *
69 _mondata(void)
70 {
71 	struct mondata *d = mondata;
72 
73 	if (d == 0) {
74 		if ((d = (struct mondata *)
75 			calloc(1, sizeof(struct mondata))) == NULL) {
76 				return (NULL);
77 		}
78 		mondata = d;
79 	}
80 	return (d);
81 }
82 
83 void
84 monstartup(char *lowpc, char *highpc)
85 {
86 	int monsize;
87 	char *buffer;
88 	int cntsiz;
89 	char *_alloc_profil_buf();
90 
91 	cntsiz = (highpc - lowpc) * ARCDENSITY / 100;
92 	if (cntsiz < MINARCS)
93 		cntsiz = MINARCS;
94 	monsize = (highpc - lowpc + HISTFRACTION - 1) / HISTFRACTION
95 		+ sizeof(struct phdr) + cntsiz * sizeof(struct cnt);
96 	buffer = _alloc_profil_buf(monsize);
97 	if (buffer == (char *)-1) {
98 		write(2, MSG, sizeof(MSG));
99 		return;
100 	}
101 	monitor(lowpc, highpc, buffer, monsize, cntsiz);
102 }
103 
104 /*
105  * Arguments
106  *	lowpc, hightpc:	boundaries of text to be monitored
107  *	buf:		ptr to space for monitor data (WORDs)
108  *	bufsiz:		size of above space (in WORDs)
109  *	cntsiz:		max no. of functions whose calls are counted
110  */
111 void
112 monitor(char *lowpc, char *highpc, char *buf, int bufsiz, int cntsiz)
113 {
114 	struct mondata *d = _mondata();
115 	int o;
116 	struct phdr *php;
117 	static int ssiz;
118 	static char *sbuf;
119 	char *s, *name;
120 
121 	name = d->mon_out;
122 
123 	if (lowpc == NULL) {		/* true only at the end */
124 		moncontrol(0);
125 		if (sbuf != NULL) {
126 			int pid, n;
127 
128 			if (d->progname[0] != '\0') { /* finish constructing
129 						    "PROFDIR/pid.progname" */
130 			    /* set name to end of PROFDIR */
131 			    name = strrchr(d->mon_out, '\0');
132 			    if ((pid = getpid()) <= 0) /* extra test just in case */
133 				pid = 1; /* getpid returns something inappropriate */
134 			    for (n = 10000; n > pid; n /= 10)
135 				; /* suppress leading zeros */
136 			    for ( ; ; n /= 10) {
137 				*name++ = pid/n + '0';
138 				if (n == 1)
139 				    break;
140 				pid %= n;
141 			    }
142 			    *name++ = '.';
143 			    (void)strcpy(name, d->progname);
144 			}
145 
146 			if ((o = creat(d->mon_out, 0666)) < 0 ||
147 			    write(o, sbuf, (unsigned)ssiz) == -1)
148 				perror(d->mon_out);
149 			if (o >= 0)
150 				close(o);
151 		}
152 		return;
153 	}
154 	countbase = (struct cnt *)(buf + sizeof(struct phdr));
155 	sbuf = NULL;
156 	o = sizeof(struct phdr) + cntsiz * sizeof(struct cnt);
157 	if (ssiz >= bufsiz || lowpc >= highpc)
158 		return;		/* buffer too small or PC range bad */
159 	if ((s = getenv(PROFDIR)) == NULL) /* PROFDIR not in environment */
160 		(void)strcpy(name, MON_OUT); /* use default "mon.out" */
161 	else if (*s == '\0') /* value of PROFDIR is NULL */
162 		return; /* no profiling on this run */
163 	else { /* set up mon_out and progname to construct
164 		  "PROFDIR/pid.progname" when done profiling */
165 
166 		while (*s != '\0') /* copy PROFDIR value (path-prefix) */
167 			*name++ = *s++;
168 		*name++ = '/'; /* two slashes won't hurt */
169 		if (___Argv != NULL) /* mcrt0.s executed */
170 			if ((s = strrchr(___Argv[0], '/')) != NULL)
171 			    strcpy(d->progname, s + 1);
172 			else
173 			    strcpy(d->progname, ___Argv[0]);
174 	}
175 	sbuf = buf;		/* for writing buffer at the wrapup */
176 	ssiz = bufsiz;
177 	php = (struct phdr *)&buf[0];
178 	php->lpc = (char *)lowpc;	/* initialize the first */
179 	php->hpc = (char *)highpc;	/* region of the buffer */
180 	php->ncnt = cntsiz;
181 	numctrs = cntsiz;
182 	buf += o;
183 	bufsiz -= o;
184 	if (bufsiz <= 0)
185 		return;
186 	o = (highpc - lowpc);
187 	if(bufsiz < o)
188 		o = ((float) bufsiz / o) * 65536;
189 	else
190 		o = 65536;
191 	d->s_scale = o;
192 	d->s_sbuf = buf;
193 	d->s_bufsiz = bufsiz;
194 	d->s_lowpc = (int) lowpc;
195 	moncontrol(1);
196 }
197 
198 /*
199  * Control profiling
200  *	profiling is what mcount checks to see if
201  *	all the data structures are ready.
202  */
203 void
204 moncontrol(int mode)
205 {
206     struct mondata *d = _mondata();
207 
208     if (mode) {
209 	/* start */
210 	profil(d->s_sbuf, d->s_bufsiz, d->s_lowpc, d->s_scale);
211 	profiling = 0;
212     } else {
213 	/* stop */
214 	profil((char *)0, 0, 0, 0);
215 	profiling = 3;
216     }
217 }
218