Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / gcc / config / i386 / gmon-sol2.c
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
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.
20  *
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.
32  */
33
34 /*
35  * This is a modified gmon.c by J.W.Hawtin <oolon@ankh.org>,
36  * 14/8/96 based on the original gmon.c in GCC and the hacked version
37  * solaris 2 sparc version (config/sparc/gmon-sol.c) by Mark Eichin. To do
38  * process profiling on solaris 2.X X86
39  *
40  * It must be used in conjunction with sol2-gc1.asm, which is used to start
41  * and stop process monitoring.
42  *
43  * Differences.
44  *
45  * On Solaris 2 _mcount is called by library functions not mcount, so support
46  * has been added for both.
47  *
48  * Also the prototype for profil() is different
49  *
50  * Solaris 2 does not seem to have char *minbrk whcih allows the setting of
51  * the minimum SBRK region so this code has been removed and lets pray malloc
52  * does not mess it up.
53  *
54  * Notes
55  *
56  * This code could easily be integrated with the original gmon.c and perhaps
57  * should be.
58  */
59
60 #ifndef lint
61 static char sccsid[] = "@(#)gmon.c      5.3 (Berkeley) 5/22/91";
62 #endif /* not lint */
63
64 #if 0
65 #include <unistd.h>
66
67 #endif
68 #ifdef DEBUG
69 #include <stdio.h>
70 #endif
71
72 #if 0
73 #include "i386/gmon.h"
74 #else
75
76 struct phdr {
77                 char    *lpc;
78                 char    *hpc;
79                 int     ncnt;
80 };
81
82
83 #define HISTFRACTION 2
84 #define HISTCOUNTER unsigned short
85 #define HASHFRACTION 1
86 #define ARCDENSITY 2
87 #define MINARCS 50
88 #define BASEADDRESS 0x8000000 /* On Solaris 2 X86 all executables start here
89                                  and not at 0 */ 
90
91 struct tostruct {
92   char *selfpc;
93   long count;
94   unsigned short link;
95 };
96 struct rawarc {
97     unsigned long       raw_frompc;
98     unsigned long       raw_selfpc;
99     long                raw_count;
100 };
101 #define ROUNDDOWN(x,y)  (((x)/(y))*(y))
102 #define ROUNDUP(x,y)    ((((x)+(y)-1)/(y))*(y))
103 #endif
104
105 /* char *minbrk; */
106
107 #ifdef __alpha
108 extern char *sbrk ();
109 #endif
110
111     /*
112      *  froms is actually a bunch of unsigned shorts indexing tos
113      */
114 static int              profiling = 3;
115 static unsigned short   *froms;
116 static struct tostruct  *tos = 0;
117 static long             tolimit = 0;
118 static char             *s_lowpc = 0;
119 static char             *s_highpc = 0;
120 static unsigned long    s_textsize = 0;
121
122 static int      ssiz;
123 static char     *sbuf;
124 static int      s_scale;
125     /* see profil(2) where this is describe (incorrectly) */
126 #define         SCALE_1_TO_1    0x10000L
127
128 #define MSG "No space for profiling buffer(s)\n"
129
130 extern int errno;
131
132 monstartup(lowpc, highpc)
133     char        *lowpc;
134     char        *highpc;
135 {
136     int                 monsize;
137     char                *buffer;
138     register int        o;
139
140         /*
141          *      round lowpc and highpc to multiples of the density we're using
142          *      so the rest of the scaling (here and in gprof) stays in ints.
143          */
144     lowpc = (char *)
145             ROUNDDOWN((unsigned)lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
146     s_lowpc = lowpc;
147     highpc = (char *)
148             ROUNDUP((unsigned)highpc, HISTFRACTION*sizeof(HISTCOUNTER));
149     s_highpc = highpc;
150     s_textsize = highpc - lowpc;
151     monsize = (s_textsize / HISTFRACTION) + sizeof(struct phdr);
152     buffer = (char *) sbrk( monsize );
153     if ( buffer == (char *) -1 ) {
154         write( 2 , MSG , sizeof(MSG) );
155         return;
156     }
157     froms = (unsigned short *) sbrk( s_textsize / HASHFRACTION );
158     if ( froms == (unsigned short *) -1 ) {
159         write( 2 , MSG , sizeof(MSG) );
160         froms = 0;
161         return;
162     }
163     tolimit = s_textsize * ARCDENSITY / 100;
164     if ( tolimit < MINARCS ) {
165         tolimit = MINARCS;
166     } else if ( tolimit > 65534 ) {
167         tolimit = 65534;
168     }
169     tos = (struct tostruct *) sbrk( tolimit * sizeof( struct tostruct ) );
170     if ( tos == (struct tostruct *) -1 ) {
171         write( 2 , MSG , sizeof(MSG) );
172         froms = 0;
173         tos = 0;
174         return;
175     }
176 /*    minbrk = (char *) sbrk(0);*/
177     tos[0].link = 0;
178     sbuf = buffer;
179     ssiz = monsize;
180     ( (struct phdr *) buffer ) -> lpc = lowpc;
181     ( (struct phdr *) buffer ) -> hpc = highpc;
182     ( (struct phdr *) buffer ) -> ncnt = ssiz;
183     monsize -= sizeof(struct phdr);
184     if ( monsize <= 0 )
185         return;
186     o = highpc - lowpc;
187     if( monsize < o )
188 #ifndef hp300
189         s_scale = ( (float) monsize / o ) * SCALE_1_TO_1;
190 #else /* avoid floating point */
191     {
192         int quot = o / monsize;
193
194         if (quot >= 0x10000)
195                 s_scale = 1;
196         else if (quot >= 0x100)
197                 s_scale = 0x10000 / quot;
198         else if (o >= 0x800000)
199                 s_scale = 0x1000000 / (o / (monsize >> 8));
200         else
201                 s_scale = 0x1000000 / ((o << 8) / monsize);
202     }
203 #endif
204     else
205         s_scale = SCALE_1_TO_1;
206     moncontrol(1);
207 }
208
209 _mcleanup()
210 {
211     int                 fd;
212     int                 fromindex;
213     int                 endfrom;
214     char                *frompc;
215     int                 toindex;
216     struct rawarc       rawarc;
217
218     moncontrol(0);
219     fd = creat( "gmon.out" , 0666 );
220     if ( fd < 0 ) {
221         perror( "mcount: gmon.out" );
222         return;
223     }
224 #   ifdef DEBUG
225         fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf , ssiz );
226 #   endif DEBUG
227
228     write( fd , sbuf , ssiz );
229     endfrom = s_textsize / (HASHFRACTION * sizeof(*froms));
230     for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) {
231         if ( froms[fromindex] == 0 ) {
232             continue;
233         }
234         frompc = s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms));
235         for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) {
236 #           ifdef DEBUG
237                 fprintf( stderr ,
238                         "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
239                         frompc , tos[toindex].selfpc , tos[toindex].count );
240 #           endif DEBUG
241             rawarc.raw_frompc = (unsigned long) frompc;
242             rawarc.raw_selfpc = (unsigned long) tos[toindex].selfpc;
243             rawarc.raw_count = tos[toindex].count;
244             write( fd , &rawarc , sizeof rawarc );
245         }
246     }
247     close( fd );
248 }
249
250 /* Solaris 2 libraries use _mcount.  */
251 asm(".globl _mcount; _mcount: jmp internal_mcount");
252 /* This is for compatibility with old versions of gcc which used mcount.  */
253 asm(".globl mcount; mcount: jmp internal_mcount");
254
255 internal_mcount()
256 {
257         register char                   *selfpc;
258         register unsigned short         *frompcindex;
259         register struct tostruct        *top;
260         register struct tostruct        *prevtop;
261         register long                   toindex;
262         static char already_setup;
263
264         /*
265          *      find the return address for mcount,
266          *      and the return address for mcount's caller.
267          */
268
269         /* selfpc = pc pushed by mcount call.
270            This identifies the function that was just entered.  */
271         selfpc = (void *) __builtin_return_address (0);
272         /* frompcindex = pc in preceding frame.
273            This identifies the caller of the function just entered.  */
274         frompcindex = (void *) __builtin_return_address (1);
275
276         if(!already_setup) {
277           extern etext();
278           already_setup = 1;
279 /*        monstartup(0, etext); */
280           monstartup(0x08040000, etext);
281 #ifdef USE_ONEXIT
282           on_exit(_mcleanup, 0);
283 #else
284           atexit(_mcleanup);
285 #endif
286         }
287         /*
288          *      check that we are profiling
289          *      and that we aren't recursively invoked.
290          */
291         if (profiling) {
292                 goto out;
293         }
294         profiling++;
295         /*
296          *      check that frompcindex is a reasonable pc value.
297          *      for example:    signal catchers get called from the stack,
298          *                      not from text space.  too bad.
299          */
300         frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc);
301         if ((unsigned long)frompcindex > s_textsize) {
302                 goto done;
303         }
304         frompcindex =
305             &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))];
306         toindex = *frompcindex;
307         if (toindex == 0) {
308                 /*
309                  *      first time traversing this arc
310                  */
311                 toindex = ++tos[0].link;
312                 if (toindex >= tolimit) {
313                         goto overflow;
314                 }
315                 *frompcindex = toindex;
316                 top = &tos[toindex];
317                 top->selfpc = selfpc;
318                 top->count = 1;
319                 top->link = 0;
320                 goto done;
321         }
322         top = &tos[toindex];
323         if (top->selfpc == selfpc) {
324                 /*
325                  *      arc at front of chain; usual case.
326                  */
327                 top->count++;
328                 goto done;
329         }
330         /*
331          *      have to go looking down chain for it.
332          *      top points to what we are looking at,
333          *      prevtop points to previous top.
334          *      we know it is not at the head of the chain.
335          */
336         for (; /* goto done */; ) {
337                 if (top->link == 0) {
338                         /*
339                          *      top is end of the chain and none of the chain
340                          *      had top->selfpc == selfpc.
341                          *      so we allocate a new tostruct
342                          *      and link it to the head of the chain.
343                          */
344                         toindex = ++tos[0].link;
345                         if (toindex >= tolimit) {
346                                 goto overflow;
347                         }
348                         top = &tos[toindex];
349                         top->selfpc = selfpc;
350                         top->count = 1;
351                         top->link = *frompcindex;
352                         *frompcindex = toindex;
353                         goto done;
354                 }
355                 /*
356                  *      otherwise, check the next arc on the chain.
357                  */
358                 prevtop = top;
359                 top = &tos[top->link];
360                 if (top->selfpc == selfpc) {
361                         /*
362                          *      there it is.
363                          *      increment its count
364                          *      move it to the head of the chain.
365                          */
366                         top->count++;
367                         toindex = prevtop->link;
368                         prevtop->link = top->link;
369                         top->link = *frompcindex;
370                         *frompcindex = toindex;
371                         goto done;
372                 }
373
374         }
375 done:
376         profiling--;
377         /* and fall through */
378 out:
379         return;         /* normal return restores saved registers */
380
381 overflow:
382         profiling++; /* halt further profiling */
383 #   define      TOLIMIT "mcount: tos overflow\n"
384         write(2, TOLIMIT, sizeof(TOLIMIT));
385         goto out;
386 }
387
388 /*
389  * Control profiling
390  *      profiling is what mcount checks to see if
391  *      all the data structures are ready.
392  */
393 moncontrol(mode)
394     int mode;
395 {
396     if (mode)
397     {
398       /* start */
399       profil((unsigned short *)(sbuf + sizeof(struct phdr)),
400              ssiz - sizeof(struct phdr),
401              (int)s_lowpc, s_scale);
402       
403       profiling = 0;
404     } else {
405       /* stop */
406       profil((unsigned short *)0, 0, 0, 0);
407       profiling = 3;
408     }
409 }