Initial revision
[official-gcc.git] / gcc / config / i386 / gmon-sol2.c
blob13d87ec0e90b29b9a1e9b7146c9fca842584b278
1 /*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * All rights reserved.
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 /*
35 * Re rework of the solaris 2 version of gmon by J.W.Hawtin 12/8/1996
36 * Does not work right yet.
40 * This is a modified gmon.c by J.W.Hawtin <J.W.Hawtin@lboro.ac.uk>,
41 * 14/8/96 based on the original gmon.c in GCC and the hacked version
42 * solaris 2 sparc version (config/sparc/gmon-sol.c) by Mark Eichin. To do
43 * process profiling on solaris 2.4 X86
45 * It must be used in conjunction with sol2-gc1.asm, which is used to start
46 * and stop process monitoring.
48 * Differences.
50 * On Solaris 2 _mcount is called my library functions not mcount, so support
51 * has been added for both.
53 * Also the prototype for profil() is different
55 * Solaris 2 does not seem to have char *minbrk whcih allows the setting of
56 * the minimum SBRK region so this code has been removed and lets pray malloc
57 * does not mess it up.
59 * Notes
61 * This code could easily be integrated with the orginal gmon.c and perhaps
62 * should be.
65 #ifndef lint
66 static char sccsid[] = "@(#)gmon.c 5.3 (Berkeley) 5/22/91";
67 #endif /* not lint */
69 #if 0
70 #include <unistd.h>
72 #endif
73 #ifdef DEBUG
74 #include <stdio.h>
75 #endif
77 #if 0
78 #include "i386/gmon.h"
79 #else
81 struct phdr {
82 char *lpc;
83 char *hpc;
84 int ncnt;
88 #define HISTFRACTION 2
89 #define HISTCOUNTER unsigned short
90 #define HASHFRACTION 1
91 #define ARCDENSITY 2
92 #define MINARCS 50
93 #define BASEADDRESS 0x8000000 /* On Solaris 2 X86 all excutables start here
94 and not at 0 */
96 struct tostruct {
97 char *selfpc;
98 long count;
99 unsigned short link;
101 struct rawarc {
102 unsigned long raw_frompc;
103 unsigned long raw_selfpc;
104 long raw_count;
106 #define ROUNDDOWN(x,y) (((x)/(y))*(y))
107 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
108 #endif
110 /* char *minbrk; */
112 #ifdef __alpha
113 extern char *sbrk ();
114 #endif
117 * froms is actually a bunch of unsigned shorts indexing tos
119 static int profiling = 3;
120 static unsigned short *froms;
121 static struct tostruct *tos = 0;
122 static long tolimit = 0;
123 static char *s_lowpc = 0;
124 static char *s_highpc = 0;
125 static unsigned long s_textsize = 0;
127 static int ssiz;
128 static char *sbuf;
129 static int s_scale;
130 /* see profil(2) where this is describe (incorrectly) */
131 #define SCALE_1_TO_1 0x10000L
133 #define MSG "No space for profiling buffer(s)\n"
135 extern int errno;
137 monstartup(lowpc, highpc)
138 char *lowpc;
139 char *highpc;
141 int monsize;
142 char *buffer;
143 register int o;
146 * round lowpc and highpc to multiples of the density we're using
147 * so the rest of the scaling (here and in gprof) stays in ints.
149 lowpc = (char *)
150 ROUNDDOWN((unsigned)lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
151 s_lowpc = lowpc;
152 highpc = (char *)
153 ROUNDUP((unsigned)highpc, HISTFRACTION*sizeof(HISTCOUNTER));
154 s_highpc = highpc;
155 s_textsize = highpc - lowpc;
156 monsize = (s_textsize / HISTFRACTION) + sizeof(struct phdr);
157 buffer = (char *) sbrk( monsize );
158 if ( buffer == (char *) -1 ) {
159 write( 2 , MSG , sizeof(MSG) );
160 return;
162 froms = (unsigned short *) sbrk( s_textsize / HASHFRACTION );
163 if ( froms == (unsigned short *) -1 ) {
164 write( 2 , MSG , sizeof(MSG) );
165 froms = 0;
166 return;
168 tolimit = s_textsize * ARCDENSITY / 100;
169 if ( tolimit < MINARCS ) {
170 tolimit = MINARCS;
171 } else if ( tolimit > 65534 ) {
172 tolimit = 65534;
174 tos = (struct tostruct *) sbrk( tolimit * sizeof( struct tostruct ) );
175 if ( tos == (struct tostruct *) -1 ) {
176 write( 2 , MSG , sizeof(MSG) );
177 froms = 0;
178 tos = 0;
179 return;
181 /* minbrk = (char *) sbrk(0);*/
182 tos[0].link = 0;
183 sbuf = buffer;
184 ssiz = monsize;
185 ( (struct phdr *) buffer ) -> lpc = lowpc;
186 ( (struct phdr *) buffer ) -> hpc = highpc;
187 ( (struct phdr *) buffer ) -> ncnt = ssiz;
188 monsize -= sizeof(struct phdr);
189 if ( monsize <= 0 )
190 return;
191 o = highpc - lowpc;
192 if( monsize < o )
193 #ifndef hp300
194 s_scale = ( (float) monsize / o ) * SCALE_1_TO_1;
195 #else /* avoid floating point */
197 int quot = o / monsize;
199 if (quot >= 0x10000)
200 s_scale = 1;
201 else if (quot >= 0x100)
202 s_scale = 0x10000 / quot;
203 else if (o >= 0x800000)
204 s_scale = 0x1000000 / (o / (monsize >> 8));
205 else
206 s_scale = 0x1000000 / ((o << 8) / monsize);
208 #endif
209 else
210 s_scale = SCALE_1_TO_1;
211 moncontrol(1);
214 _mcleanup()
216 int fd;
217 int fromindex;
218 int endfrom;
219 char *frompc;
220 int toindex;
221 struct rawarc rawarc;
223 moncontrol(0);
224 fd = creat( "gmon.out" , 0666 );
225 if ( fd < 0 ) {
226 perror( "mcount: gmon.out" );
227 return;
229 # ifdef DEBUG
230 fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf , ssiz );
231 # endif DEBUG
233 write( fd , sbuf , ssiz );
234 endfrom = s_textsize / (HASHFRACTION * sizeof(*froms));
235 for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) {
236 if ( froms[fromindex] == 0 ) {
237 continue;
239 frompc = s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms));
240 for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) {
241 # ifdef DEBUG
242 fprintf( stderr ,
243 "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
244 frompc , tos[toindex].selfpc , tos[toindex].count );
245 # endif DEBUG
246 rawarc.raw_frompc = (unsigned long) frompc;
247 rawarc.raw_selfpc = (unsigned long) tos[toindex].selfpc;
248 rawarc.raw_count = tos[toindex].count;
249 write( fd , &rawarc , sizeof rawarc );
252 close( fd );
255 /* Solaris 2 libraries use _mcount. */
256 asm(".globl _mcount; _mcount: jmp internal_mcount");
257 /* This is for compatibility with old versions of gcc which used mcount. */
258 asm(".globl mcount; mcount: jmp internal_mcount");
260 internal_mcount()
262 register char *selfpc;
263 register unsigned short *frompcindex;
264 register struct tostruct *top;
265 register struct tostruct *prevtop;
266 register long toindex;
269 * find the return address for mcount,
270 * and the return address for mcount's caller.
273 /* selfpc = pc pushed by mcount call.
274 This identifies the function that was just entered. */
275 selfpc = (void *) __builtin_return_address (0);
276 /* frompcindex = pc in preceding frame.
277 This identifies the caller of the function just entered. */
278 frompcindex = (void *) __builtin_return_address (1);
281 * check that we are profiling
282 * and that we aren't recursively invoked.
284 if (profiling) {
285 goto out;
287 profiling++;
289 * check that frompcindex is a reasonable pc value.
290 * for example: signal catchers get called from the stack,
291 * not from text space. too bad.
293 frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc);
294 if ((unsigned long)frompcindex > s_textsize) {
295 goto done;
297 frompcindex =
298 &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))];
299 toindex = *frompcindex;
300 if (toindex == 0) {
302 * first time traversing this arc
304 toindex = ++tos[0].link;
305 if (toindex >= tolimit) {
306 goto overflow;
308 *frompcindex = toindex;
309 top = &tos[toindex];
310 top->selfpc = selfpc;
311 top->count = 1;
312 top->link = 0;
313 goto done;
315 top = &tos[toindex];
316 if (top->selfpc == selfpc) {
318 * arc at front of chain; usual case.
320 top->count++;
321 goto done;
324 * have to go looking down chain for it.
325 * top points to what we are looking at,
326 * prevtop points to previous top.
327 * we know it is not at the head of the chain.
329 for (; /* goto done */; ) {
330 if (top->link == 0) {
332 * top is end of the chain and none of the chain
333 * had top->selfpc == selfpc.
334 * so we allocate a new tostruct
335 * and link it to the head of the chain.
337 toindex = ++tos[0].link;
338 if (toindex >= tolimit) {
339 goto overflow;
341 top = &tos[toindex];
342 top->selfpc = selfpc;
343 top->count = 1;
344 top->link = *frompcindex;
345 *frompcindex = toindex;
346 goto done;
349 * otherwise, check the next arc on the chain.
351 prevtop = top;
352 top = &tos[top->link];
353 if (top->selfpc == selfpc) {
355 * there it is.
356 * increment its count
357 * move it to the head of the chain.
359 top->count++;
360 toindex = prevtop->link;
361 prevtop->link = top->link;
362 top->link = *frompcindex;
363 *frompcindex = toindex;
364 goto done;
368 done:
369 profiling--;
370 /* and fall through */
371 out:
372 return; /* normal return restores saved registers */
374 overflow:
375 profiling++; /* halt further profiling */
376 # define TOLIMIT "mcount: tos overflow\n"
377 write(2, TOLIMIT, sizeof(TOLIMIT));
378 goto out;
382 * Control profiling
383 * profiling is what mcount checks to see if
384 * all the data structures are ready.
386 moncontrol(mode)
387 int mode;
389 if (mode)
391 /* start */
392 profil((unsigned short *)(sbuf + sizeof(struct phdr)),
393 ssiz - sizeof(struct phdr),
394 (int)s_lowpc, s_scale);
396 profiling = 0;
397 } else {
398 /* stop */
399 profil((unsigned short *)0, 0, 0, 0);
400 profiling = 3;