xref: /csrg-svn/lib/libc/gmon/gmon.c (revision 4564)
1 static	char *sccsid = "@(#)gmon.c	1.3 (Berkeley) 10/20/81";
2 
3 #include <stdio.h>
4 
5 #include "gmcrt0.h"
6 
7     /*
8      *	C start up routines, for monitoring
9      *	Robert Henry, UCB, 20 Oct 81
10      *
11      *	We make the following (true) assumptions:
12      *	1) when the kernel calls start, it does a jump to location 2,
13      *	and thus avoids the register save mask.  We are NOT called
14      *	with a calls!  see sys1.c:setregs().
15      *	2) The only register variable that we can trust is sp,
16      *	which points to the base of the kernel calling frame.
17      *	Do NOT believe the documentation in exec(2) regarding the
18      *	values of fp and ap.
19      *	3) We can allocate as many register variables as we want,
20      *	and don't have to save them for anybody.
21      *	4) Because of the ways that asm's work, we can't have
22      *	any automatic variables allocated on the stack, because
23      *	we must catch the value of sp before any automatics are
24      *	allocated.
25      */
26 
27 char **environ;
28 extern	unsigned char	etext;
29 asm( "#define _eprol eprol" );
30 extern	unsigned char	eprol;
31 
32 asm( "#define _start start" );
33 start()
34 {
35     struct kframe {
36 	int	kargc;
37 	char	*kargv[1];		/* size depends on kargc */
38 	char	kargstr[1];		/* size varies */
39 	char	kenvstr[1];		/* size varies */
40     };
41 	/*
42 	 *	ALL REGISTER VARIABLES!!!
43 	 */
44     register struct kframe	*kfp;		/* r11 */
45     register char		**targv;
46     register char		**argv;
47 
48 #ifdef lint
49     kfp = 0;
50 #else not lint
51     asm( "	movl	sp,r11" );		/* catch it quick */
52 #endif not lint
53     for ( argv = targv = &kfp -> kargv[0] ; *targv++ ; /* void */ )
54 	/* VOID */ ;
55     if ( targv >= (char **) ( *argv ) )
56 	--targv;
57     environ = targv;
58 asm("eprol:");
59     _mstartup( &eprol , &etext );
60     exit( main( kfp -> kargc , argv , environ ) );
61 }
62 asm( "#undef _start" );
63 asm( "#undef _eprol" );
64 
65 exit( code )
66 	/* ARGSUSED */
67     register int	code;	/* r11 */
68 {
69 
70     fflush( stdout );
71     _mcleanup();
72     _cleanup();
73     asm( "	movl r11, r0" );
74     asm( "	chmk $1" );
75 }
76 
77     /*
78      *	froms is actually a bunch of unsigned shorts indexing tos
79      */
80 static unsigned short	*froms;
81 static struct tostruct	*tos = 0;
82 static unsigned short	tolimit = 0;
83 static char		*s_lowpc = 0;
84 static char		*s_highpc = 0;
85 
86 static int	ssiz;
87 static int	*sbuf;
88 
89 #define	MSG "No space for monitor buffer(s)\n"
90 
91 _mstartup(lowpc, highpc)
92     char	*lowpc;
93     char	*highpc;
94 {
95     int		monsize;
96     char	*buffer;
97     int		textsize;
98     char	*sbrk();
99 
100     s_lowpc = lowpc;
101     s_highpc = highpc;
102     textsize = ( (char *) highpc - (char *) lowpc );
103     monsize = textsize + sizeof(struct phdr);
104     buffer = sbrk( monsize );
105     if ( buffer == (char *) -1 ) {
106 	write( 2 , MSG , sizeof(MSG) );
107 	return;
108     }
109     froms = (unsigned short *) sbrk( textsize );
110     if ( froms == (unsigned short *) -1 ) {
111 	write( 2 , MSG , sizeof(MSG) );
112 	froms = 0;
113 	return;
114     }
115     tos = (struct tostruct *) sbrk(textsize);
116     if ( tos == (struct tostruct *) -1 ) {
117 	write( 2 , MSG , sizeof(MSG) );
118 	froms = 0;
119 	tos = 0;
120 	return;
121     }
122     tolimit = textsize / sizeof(struct tostruct);
123     tos[0].link = 0;
124     monitor( lowpc , highpc , buffer , monsize );
125 }
126 
127 _mcleanup()
128 {
129     FILE	*fd;
130     int		fromindex;
131     char	*frompc;
132     int		toindex;
133     int		textsize;
134 
135     monitor( (int (*)()) 0 );
136     fd = fopen( "gmon.out" , "w" );
137     if ( fd == NULL ) {
138 	perror( "mcount: gmon.out" );
139 	return;
140     }
141 #   ifdef DEBUG
142 	fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf , ssiz );
143 #   endif DEBUG
144     fwrite( sbuf , 1 , ssiz , fd );
145     textsize = s_highpc - s_lowpc;
146     for ( fromindex = 0 ; fromindex < textsize>>1 ; fromindex++ ) {
147 	if ( froms[fromindex] == 0 ) {
148 	    continue;
149 	}
150 	frompc = s_lowpc + (fromindex<<1);
151 	for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) {
152 #	    ifdef DEBUG
153 		fprintf( stderr , "[mcleanup] frompc %d selfpc %d count %d\n" ,
154 			frompc , tos[toindex].selfpc , tos[toindex].count );
155 #	    endif DEBUG
156 	    fwrite( &frompc, 1, sizeof frompc, fd );
157 	    fwrite( &tos[toindex].selfpc, 1, sizeof tos[toindex].selfpc, fd );
158 	    fwrite( &tos[toindex].count, 1, sizeof tos[toindex].count, fd );
159 	}
160     }
161     fclose( fd );
162 }
163 
164     /*
165      *	This routine is massaged so that it may be jsb'ed to
166      */
167 asm("#define _mcount mcount");
168 mcount()
169 {
170     register char		*selfpc;	/* r11 */
171     register unsigned short	*frompcindex;	/* r10 */
172     register struct tostruct	*top;		/* r9 */
173     static int			profiling = 0;
174 
175     asm( "	forgot to run ex script on gmcrt0.s" );
176     asm( "#define r11 r5" );
177     asm( "#define r10 r4" );
178     asm( "#define r9 r3" );
179 #ifdef lint
180     selfpc = (char *) 0;
181     frompcindex = 0;
182 #else not lint
183 	/*
184 	 *	find the return address for mcount,
185 	 *	and the return address for mcount's caller.
186 	 */
187     asm("	movl (sp), r11");	/* selfpc = ... (jsb frame) */
188     asm("	movl 16(fp), r10");	/* frompcindex =     (calls frame) */
189 #endif not lint
190 	/*
191 	 *	check that we are profiling
192 	 *	and that we aren't recursively invoked.
193 	 */
194     if ( tos == 0 ) {
195 	goto out;
196     }
197     if ( profiling ) {
198 	goto out;
199     }
200     profiling = 1;
201 	/*
202 	 *	check that frompcindex is a reasonable pc value.
203 	 *	for example:	signal catchers get called from the stack,
204 	 *			not from text space.  too bad.
205 	 */
206     if ( (char *) frompcindex < s_lowpc || (char *) frompcindex > s_highpc ) {
207 	goto done;
208     }
209     frompcindex = &froms[ ( (long) frompcindex - (long) s_lowpc ) >> 1 ];
210     if ( *frompcindex == 0 ) {
211 	*frompcindex = ++tos[0].link;
212 	if ( *frompcindex >= tolimit ) {
213 	    goto overflow;
214 	}
215 	top = &tos[ *frompcindex ];
216 	top->selfpc = selfpc;
217 	top->count = 0;
218 	top->link = 0;
219     } else {
220 	top = &tos[ *frompcindex ];
221     }
222     for ( ; /* goto done */ ; top = &tos[ top -> link ] ) {
223 	if ( top -> selfpc == selfpc ) {
224 	    top -> count++;
225 	    goto done;
226 	}
227 	if ( top -> link == 0 ) {
228 	    top -> link = ++tos[0].link;
229 	    if ( top -> link >= tolimit )
230 		goto overflow;
231 	    top = &tos[ top -> link ];
232 	    top -> selfpc = selfpc;
233 	    top -> count = 1;
234 	    top -> link = 0;
235 	    goto done;
236 	}
237     }
238 done:
239     profiling = 0;
240     /* and fall through */
241 out:
242     asm( "	rsb" );
243     asm( "#undef r11" );
244     asm( "#undef r10" );
245     asm( "#undef r9" );
246     asm( "#undef _mcount");
247 
248 overflow:
249 #   define	TOLIMIT	"mcount: tos overflow\n"
250     write( 2 , TOLIMIT , sizeof( TOLIMIT ) );
251     tos = 0;
252     froms = 0;
253     goto out;
254 }
255 
256 monitor( lowpc , highpc , buf , bufsiz )
257     char	*lowpc;
258     /* VARARGS1 */
259     char	*highpc;
260     int		*buf, bufsiz;
261 {
262     register o;
263 
264     if ( lowpc == 0 ) {
265 	profil( (char *) 0 , 0 , 0 , 0 );
266 	return;
267     }
268     sbuf = buf;
269     ssiz = bufsiz;
270     ( (struct phdr *) buf ) -> lpc = lowpc;
271     ( (struct phdr *) buf ) -> hpc = highpc;
272     ( (struct phdr *) buf ) -> ncnt = ssiz;
273     o = sizeof(struct phdr);
274     buf = (int *) ( ( (int) buf ) + o );
275     bufsiz -= o;
276     if ( bufsiz <= 0 )
277 	return;
278     o = ( ( (char *) highpc - (char *) lowpc) >> 1 );
279     if( bufsiz < o )
280 	o = ( (float) bufsiz / o ) * 32768;
281     else
282 	o = 0177777;
283     profil( buf , bufsiz , lowpc , o );
284 }
285