xref: /titanic_41/usr/src/lib/libbc/csu/common/mon.c (revision 5d54f3d8999eac1762fe0a8c7177d20f1f201fae)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1989 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  *	Environment variable PROFDIR added such that:
317c478bd9Sstevel@tonic-gate  *		If PROFDIR doesn't exist, "mon.out" is produced as before.
327c478bd9Sstevel@tonic-gate  *		If PROFDIR = NULL, no profiling output is produced.
337c478bd9Sstevel@tonic-gate  *		If PROFDIR = string, "string/pid.progname" is produced,
347c478bd9Sstevel@tonic-gate  *		  where name consists of argv[0] suitably massaged.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate #include <sys/param.h>
377c478bd9Sstevel@tonic-gate #include <sys/dir.h>
387c478bd9Sstevel@tonic-gate #include "mon.h"
39*5d54f3d8Smuffin #include <sys/fcntl.h>
40*5d54f3d8Smuffin #include <unistd.h>
41*5d54f3d8Smuffin #include <stdlib.h>
42*5d54f3d8Smuffin #include <string.h>
43*5d54f3d8Smuffin 
447c478bd9Sstevel@tonic-gate #define PROFDIR	"PROFDIR"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate extern void	profil(), perror();
477c478bd9Sstevel@tonic-gate 
48*5d54f3d8Smuffin void	monitor(char *, char *, char *, int, int);
49*5d54f3d8Smuffin void	moncontrol(int);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate char **___Argv = NULL; /* initialized to argv array by mcrt0 (if loaded) */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate struct cnt	*countbase;
547c478bd9Sstevel@tonic-gate int		numctrs;
557c478bd9Sstevel@tonic-gate int		profiling;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static struct mondata {
587c478bd9Sstevel@tonic-gate 	char	*s_sbuf;
597c478bd9Sstevel@tonic-gate 	int 	s_bufsiz;
607c478bd9Sstevel@tonic-gate 	int	s_scale;
617c478bd9Sstevel@tonic-gate 	int	s_lowpc;
627c478bd9Sstevel@tonic-gate 	char	mon_out[MAXPATHLEN];
637c478bd9Sstevel@tonic-gate 	char	progname[MAXNAMLEN];
647c478bd9Sstevel@tonic-gate } *mondata, *_mondata();
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #define	MSG "No space for monitor buffer(s)\n"
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static struct mondata *
_mondata(void)69*5d54f3d8Smuffin _mondata(void)
707c478bd9Sstevel@tonic-gate {
71*5d54f3d8Smuffin 	struct mondata *d = mondata;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	if (d == 0) {
747c478bd9Sstevel@tonic-gate 		if ((d = (struct mondata *)
757c478bd9Sstevel@tonic-gate 			calloc(1, sizeof(struct mondata))) == NULL) {
767c478bd9Sstevel@tonic-gate 				return (NULL);
777c478bd9Sstevel@tonic-gate 		}
787c478bd9Sstevel@tonic-gate 		mondata = d;
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 	return (d);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
83*5d54f3d8Smuffin void
monstartup(char * lowpc,char * highpc)84*5d54f3d8Smuffin monstartup(char *lowpc, char *highpc)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	int monsize;
877c478bd9Sstevel@tonic-gate 	char *buffer;
887c478bd9Sstevel@tonic-gate 	int cntsiz;
897c478bd9Sstevel@tonic-gate 	char *_alloc_profil_buf();
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	cntsiz = (highpc - lowpc) * ARCDENSITY / 100;
927c478bd9Sstevel@tonic-gate 	if (cntsiz < MINARCS)
937c478bd9Sstevel@tonic-gate 		cntsiz = MINARCS;
947c478bd9Sstevel@tonic-gate 	monsize = (highpc - lowpc + HISTFRACTION - 1) / HISTFRACTION
957c478bd9Sstevel@tonic-gate 		+ sizeof(struct phdr) + cntsiz * sizeof(struct cnt);
967c478bd9Sstevel@tonic-gate 	buffer = _alloc_profil_buf(monsize);
977c478bd9Sstevel@tonic-gate 	if (buffer == (char *)-1) {
987c478bd9Sstevel@tonic-gate 		write(2, MSG, sizeof(MSG));
997c478bd9Sstevel@tonic-gate 		return;
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 	monitor(lowpc, highpc, buffer, monsize, cntsiz);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
104*5d54f3d8Smuffin /*
105*5d54f3d8Smuffin  * Arguments
106*5d54f3d8Smuffin  *	lowpc, hightpc:	boundaries of text to be monitored
107*5d54f3d8Smuffin  *	buf:		ptr to space for monitor data (WORDs)
108*5d54f3d8Smuffin  *	bufsiz:		size of above space (in WORDs)
109*5d54f3d8Smuffin  *	cntsiz:		max no. of functions whose calls are counted
110*5d54f3d8Smuffin  */
1117c478bd9Sstevel@tonic-gate void
monitor(char * lowpc,char * highpc,char * buf,int bufsiz,int cntsiz)112*5d54f3d8Smuffin monitor(char *lowpc, char *highpc, char *buf, int bufsiz, int cntsiz)
1137c478bd9Sstevel@tonic-gate {
114*5d54f3d8Smuffin 	struct mondata *d = _mondata();
115*5d54f3d8Smuffin 	int o;
1167c478bd9Sstevel@tonic-gate 	struct phdr *php;
1177c478bd9Sstevel@tonic-gate 	static int ssiz;
1187c478bd9Sstevel@tonic-gate 	static char *sbuf;
119*5d54f3d8Smuffin 	char *s, *name;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	name = d->mon_out;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if (lowpc == NULL) {		/* true only at the end */
1247c478bd9Sstevel@tonic-gate 		moncontrol(0);
1257c478bd9Sstevel@tonic-gate 		if (sbuf != NULL) {
126*5d54f3d8Smuffin 			int pid, n;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 			if (d->progname[0] != '\0') { /* finish constructing
1297c478bd9Sstevel@tonic-gate 						    "PROFDIR/pid.progname" */
1307c478bd9Sstevel@tonic-gate 			    /* set name to end of PROFDIR */
1317c478bd9Sstevel@tonic-gate 			    name = strrchr(d->mon_out, '\0');
1327c478bd9Sstevel@tonic-gate 			    if ((pid = getpid()) <= 0) /* extra test just in case */
1337c478bd9Sstevel@tonic-gate 				pid = 1; /* getpid returns something inappropriate */
1347c478bd9Sstevel@tonic-gate 			    for (n = 10000; n > pid; n /= 10)
1357c478bd9Sstevel@tonic-gate 				; /* suppress leading zeros */
1367c478bd9Sstevel@tonic-gate 			    for ( ; ; n /= 10) {
1377c478bd9Sstevel@tonic-gate 				*name++ = pid/n + '0';
1387c478bd9Sstevel@tonic-gate 				if (n == 1)
1397c478bd9Sstevel@tonic-gate 				    break;
1407c478bd9Sstevel@tonic-gate 				pid %= n;
1417c478bd9Sstevel@tonic-gate 			    }
1427c478bd9Sstevel@tonic-gate 			    *name++ = '.';
1437c478bd9Sstevel@tonic-gate 			    (void)strcpy(name, d->progname);
1447c478bd9Sstevel@tonic-gate 			}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 			if ((o = creat(d->mon_out, 0666)) < 0 ||
1477c478bd9Sstevel@tonic-gate 			    write(o, sbuf, (unsigned)ssiz) == -1)
1487c478bd9Sstevel@tonic-gate 				perror(d->mon_out);
1497c478bd9Sstevel@tonic-gate 			if (o >= 0)
1507c478bd9Sstevel@tonic-gate 				close(o);
1517c478bd9Sstevel@tonic-gate 		}
1527c478bd9Sstevel@tonic-gate 		return;
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 	countbase = (struct cnt *)(buf + sizeof(struct phdr));
1557c478bd9Sstevel@tonic-gate 	sbuf = NULL;
1567c478bd9Sstevel@tonic-gate 	o = sizeof(struct phdr) + cntsiz * sizeof(struct cnt);
1577c478bd9Sstevel@tonic-gate 	if (ssiz >= bufsiz || lowpc >= highpc)
1587c478bd9Sstevel@tonic-gate 		return;		/* buffer too small or PC range bad */
1597c478bd9Sstevel@tonic-gate 	if ((s = getenv(PROFDIR)) == NULL) /* PROFDIR not in environment */
1607c478bd9Sstevel@tonic-gate 		(void)strcpy(name, MON_OUT); /* use default "mon.out" */
1617c478bd9Sstevel@tonic-gate 	else if (*s == '\0') /* value of PROFDIR is NULL */
1627c478bd9Sstevel@tonic-gate 		return; /* no profiling on this run */
1637c478bd9Sstevel@tonic-gate 	else { /* set up mon_out and progname to construct
1647c478bd9Sstevel@tonic-gate 		  "PROFDIR/pid.progname" when done profiling */
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		while (*s != '\0') /* copy PROFDIR value (path-prefix) */
1677c478bd9Sstevel@tonic-gate 			*name++ = *s++;
1687c478bd9Sstevel@tonic-gate 		*name++ = '/'; /* two slashes won't hurt */
1697c478bd9Sstevel@tonic-gate 		if (___Argv != NULL) /* mcrt0.s executed */
1707c478bd9Sstevel@tonic-gate 			if ((s = strrchr(___Argv[0], '/')) != NULL)
1717c478bd9Sstevel@tonic-gate 			    strcpy(d->progname, s + 1);
1727c478bd9Sstevel@tonic-gate 			else
1737c478bd9Sstevel@tonic-gate 			    strcpy(d->progname, ___Argv[0]);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 	sbuf = buf;		/* for writing buffer at the wrapup */
1767c478bd9Sstevel@tonic-gate 	ssiz = bufsiz;
1777c478bd9Sstevel@tonic-gate 	php = (struct phdr *)&buf[0];
1787c478bd9Sstevel@tonic-gate 	php->lpc = (char *)lowpc;	/* initialize the first */
1797c478bd9Sstevel@tonic-gate 	php->hpc = (char *)highpc;	/* region of the buffer */
1807c478bd9Sstevel@tonic-gate 	php->ncnt = cntsiz;
1817c478bd9Sstevel@tonic-gate 	numctrs = cntsiz;
1827c478bd9Sstevel@tonic-gate 	buf += o;
1837c478bd9Sstevel@tonic-gate 	bufsiz -= o;
1847c478bd9Sstevel@tonic-gate 	if (bufsiz <= 0)
1857c478bd9Sstevel@tonic-gate 		return;
1867c478bd9Sstevel@tonic-gate 	o = (highpc - lowpc);
1877c478bd9Sstevel@tonic-gate 	if(bufsiz < o)
1887c478bd9Sstevel@tonic-gate 		o = ((float) bufsiz / o) * 65536;
1897c478bd9Sstevel@tonic-gate 	else
1907c478bd9Sstevel@tonic-gate 		o = 65536;
1917c478bd9Sstevel@tonic-gate 	d->s_scale = o;
1927c478bd9Sstevel@tonic-gate 	d->s_sbuf = buf;
1937c478bd9Sstevel@tonic-gate 	d->s_bufsiz = bufsiz;
1947c478bd9Sstevel@tonic-gate 	d->s_lowpc = (int) lowpc;
1957c478bd9Sstevel@tonic-gate 	moncontrol(1);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate  * Control profiling
2007c478bd9Sstevel@tonic-gate  *	profiling is what mcount checks to see if
2017c478bd9Sstevel@tonic-gate  *	all the data structures are ready.
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate void
moncontrol(int mode)204*5d54f3d8Smuffin moncontrol(int mode)
2057c478bd9Sstevel@tonic-gate {
206*5d54f3d8Smuffin     struct mondata *d = _mondata();
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate     if (mode) {
2097c478bd9Sstevel@tonic-gate 	/* start */
2107c478bd9Sstevel@tonic-gate 	profil(d->s_sbuf, d->s_bufsiz, d->s_lowpc, d->s_scale);
2117c478bd9Sstevel@tonic-gate 	profiling = 0;
2127c478bd9Sstevel@tonic-gate     } else {
2137c478bd9Sstevel@tonic-gate 	/* stop */
2147c478bd9Sstevel@tonic-gate 	profil((char *)0, 0, 0, 0);
2157c478bd9Sstevel@tonic-gate 	profiling = 3;
2167c478bd9Sstevel@tonic-gate     }
2177c478bd9Sstevel@tonic-gate }
218