10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate * Copyright 1989 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
27*722Smuffin #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Environment variable PROFDIR added such that:
310Sstevel@tonic-gate * If PROFDIR doesn't exist, "mon.out" is produced as before.
320Sstevel@tonic-gate * If PROFDIR = NULL, no profiling output is produced.
330Sstevel@tonic-gate * If PROFDIR = string, "string/pid.progname" is produced,
340Sstevel@tonic-gate * where name consists of argv[0] suitably massaged.
350Sstevel@tonic-gate */
360Sstevel@tonic-gate #include <sys/param.h>
370Sstevel@tonic-gate #include <sys/dir.h>
380Sstevel@tonic-gate #include "mon.h"
39*722Smuffin #include <sys/fcntl.h>
40*722Smuffin #include <unistd.h>
41*722Smuffin #include <stdlib.h>
42*722Smuffin #include <string.h>
43*722Smuffin
440Sstevel@tonic-gate #define PROFDIR "PROFDIR"
450Sstevel@tonic-gate
46*722Smuffin extern void profil(), perror();
470Sstevel@tonic-gate
48*722Smuffin void monitor(char *, char *, char *, int, int);
49*722Smuffin void moncontrol(int);
500Sstevel@tonic-gate
510Sstevel@tonic-gate char **___Argv = NULL; /* initialized to argv array by mcrt0 (if loaded) */
520Sstevel@tonic-gate
530Sstevel@tonic-gate struct cnt *countbase;
540Sstevel@tonic-gate int numctrs;
550Sstevel@tonic-gate int profiling;
560Sstevel@tonic-gate
570Sstevel@tonic-gate static struct mondata {
580Sstevel@tonic-gate char *s_sbuf;
590Sstevel@tonic-gate int s_bufsiz;
600Sstevel@tonic-gate int s_scale;
610Sstevel@tonic-gate int s_lowpc;
620Sstevel@tonic-gate char mon_out[MAXPATHLEN];
630Sstevel@tonic-gate char progname[MAXNAMLEN];
640Sstevel@tonic-gate } *mondata, *_mondata();
650Sstevel@tonic-gate
660Sstevel@tonic-gate #define MSG "No space for monitor buffer(s)\n"
670Sstevel@tonic-gate
680Sstevel@tonic-gate static struct mondata *
_mondata(void)69*722Smuffin _mondata(void)
700Sstevel@tonic-gate {
71*722Smuffin struct mondata *d = mondata;
720Sstevel@tonic-gate
730Sstevel@tonic-gate if (d == 0) {
740Sstevel@tonic-gate if ((d = (struct mondata *)
750Sstevel@tonic-gate calloc(1, sizeof(struct mondata))) == NULL) {
760Sstevel@tonic-gate return (NULL);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate mondata = d;
790Sstevel@tonic-gate }
800Sstevel@tonic-gate return (d);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate
83*722Smuffin void
monstartup(char * lowpc,char * highpc)84*722Smuffin monstartup(char *lowpc, char *highpc)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate int monsize;
870Sstevel@tonic-gate char *buffer;
880Sstevel@tonic-gate int cntsiz;
890Sstevel@tonic-gate char *_alloc_profil_buf();
900Sstevel@tonic-gate
910Sstevel@tonic-gate cntsiz = (highpc - lowpc) * ARCDENSITY / 100;
920Sstevel@tonic-gate if (cntsiz < MINARCS)
930Sstevel@tonic-gate cntsiz = MINARCS;
940Sstevel@tonic-gate monsize = (highpc - lowpc + HISTFRACTION - 1) / HISTFRACTION
950Sstevel@tonic-gate + sizeof(struct phdr) + cntsiz * sizeof(struct cnt);
960Sstevel@tonic-gate buffer = _alloc_profil_buf(monsize);
970Sstevel@tonic-gate if (buffer == (char *)-1) {
980Sstevel@tonic-gate write(2, MSG, sizeof(MSG));
990Sstevel@tonic-gate return;
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate monitor(lowpc, highpc, buffer, monsize, cntsiz);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
104*722Smuffin /*
105*722Smuffin * Arguments
106*722Smuffin * lowpc, hightpc: boundaries of text to be monitored
107*722Smuffin * buf: ptr to space for monitor data (WORDs)
108*722Smuffin * bufsiz: size of above space (in WORDs)
109*722Smuffin * cntsiz: max no. of functions whose calls are counted
110*722Smuffin */
1110Sstevel@tonic-gate void
monitor(char * lowpc,char * highpc,char * buf,int bufsiz,int cntsiz)112*722Smuffin monitor(char *lowpc, char *highpc, char *buf, int bufsiz, int cntsiz)
1130Sstevel@tonic-gate {
114*722Smuffin struct mondata *d = _mondata();
115*722Smuffin int o;
1160Sstevel@tonic-gate struct phdr *php;
1170Sstevel@tonic-gate static int ssiz;
1180Sstevel@tonic-gate static char *sbuf;
119*722Smuffin char *s, *name;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate name = d->mon_out;
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate if (lowpc == NULL) { /* true only at the end */
1240Sstevel@tonic-gate moncontrol(0);
1250Sstevel@tonic-gate if (sbuf != NULL) {
126*722Smuffin int pid, n;
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate if (d->progname[0] != '\0') { /* finish constructing
1290Sstevel@tonic-gate "PROFDIR/pid.progname" */
1300Sstevel@tonic-gate /* set name to end of PROFDIR */
1310Sstevel@tonic-gate name = strrchr(d->mon_out, '\0');
1320Sstevel@tonic-gate if ((pid = getpid()) <= 0) /* extra test just in case */
1330Sstevel@tonic-gate pid = 1; /* getpid returns something inappropriate */
1340Sstevel@tonic-gate for (n = 10000; n > pid; n /= 10)
1350Sstevel@tonic-gate ; /* suppress leading zeros */
1360Sstevel@tonic-gate for ( ; ; n /= 10) {
1370Sstevel@tonic-gate *name++ = pid/n + '0';
1380Sstevel@tonic-gate if (n == 1)
1390Sstevel@tonic-gate break;
1400Sstevel@tonic-gate pid %= n;
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate *name++ = '.';
1430Sstevel@tonic-gate (void)strcpy(name, d->progname);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate if ((o = creat(d->mon_out, 0666)) < 0 ||
1470Sstevel@tonic-gate write(o, sbuf, (unsigned)ssiz) == -1)
1480Sstevel@tonic-gate perror(d->mon_out);
1490Sstevel@tonic-gate if (o >= 0)
1500Sstevel@tonic-gate close(o);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate return;
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate countbase = (struct cnt *)(buf + sizeof(struct phdr));
1550Sstevel@tonic-gate sbuf = NULL;
1560Sstevel@tonic-gate o = sizeof(struct phdr) + cntsiz * sizeof(struct cnt);
1570Sstevel@tonic-gate if (ssiz >= bufsiz || lowpc >= highpc)
1580Sstevel@tonic-gate return; /* buffer too small or PC range bad */
1590Sstevel@tonic-gate if ((s = getenv(PROFDIR)) == NULL) /* PROFDIR not in environment */
1600Sstevel@tonic-gate (void)strcpy(name, MON_OUT); /* use default "mon.out" */
1610Sstevel@tonic-gate else if (*s == '\0') /* value of PROFDIR is NULL */
1620Sstevel@tonic-gate return; /* no profiling on this run */
1630Sstevel@tonic-gate else { /* set up mon_out and progname to construct
1640Sstevel@tonic-gate "PROFDIR/pid.progname" when done profiling */
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate while (*s != '\0') /* copy PROFDIR value (path-prefix) */
1670Sstevel@tonic-gate *name++ = *s++;
1680Sstevel@tonic-gate *name++ = '/'; /* two slashes won't hurt */
1690Sstevel@tonic-gate if (___Argv != NULL) /* mcrt0.s executed */
1700Sstevel@tonic-gate if ((s = strrchr(___Argv[0], '/')) != NULL)
1710Sstevel@tonic-gate strcpy(d->progname, s + 1);
1720Sstevel@tonic-gate else
1730Sstevel@tonic-gate strcpy(d->progname, ___Argv[0]);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate sbuf = buf; /* for writing buffer at the wrapup */
1760Sstevel@tonic-gate ssiz = bufsiz;
1770Sstevel@tonic-gate php = (struct phdr *)&buf[0];
1780Sstevel@tonic-gate php->lpc = (char *)lowpc; /* initialize the first */
1790Sstevel@tonic-gate php->hpc = (char *)highpc; /* region of the buffer */
1800Sstevel@tonic-gate php->ncnt = cntsiz;
1810Sstevel@tonic-gate numctrs = cntsiz;
1820Sstevel@tonic-gate buf += o;
1830Sstevel@tonic-gate bufsiz -= o;
1840Sstevel@tonic-gate if (bufsiz <= 0)
1850Sstevel@tonic-gate return;
1860Sstevel@tonic-gate o = (highpc - lowpc);
1870Sstevel@tonic-gate if(bufsiz < o)
1880Sstevel@tonic-gate o = ((float) bufsiz / o) * 65536;
1890Sstevel@tonic-gate else
1900Sstevel@tonic-gate o = 65536;
1910Sstevel@tonic-gate d->s_scale = o;
1920Sstevel@tonic-gate d->s_sbuf = buf;
1930Sstevel@tonic-gate d->s_bufsiz = bufsiz;
1940Sstevel@tonic-gate d->s_lowpc = (int) lowpc;
1950Sstevel@tonic-gate moncontrol(1);
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate * Control profiling
2000Sstevel@tonic-gate * profiling is what mcount checks to see if
2010Sstevel@tonic-gate * all the data structures are ready.
2020Sstevel@tonic-gate */
2030Sstevel@tonic-gate void
moncontrol(int mode)204*722Smuffin moncontrol(int mode)
2050Sstevel@tonic-gate {
206*722Smuffin struct mondata *d = _mondata();
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate if (mode) {
2090Sstevel@tonic-gate /* start */
2100Sstevel@tonic-gate profil(d->s_sbuf, d->s_bufsiz, d->s_lowpc, d->s_scale);
2110Sstevel@tonic-gate profiling = 0;
2120Sstevel@tonic-gate } else {
2130Sstevel@tonic-gate /* stop */
2140Sstevel@tonic-gate profil((char *)0, 0, 0, 0);
2150Sstevel@tonic-gate profiling = 3;
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate }
218