2007-05-30 H.J. Lu <hongjiu.lu@intel.com>
[official-gcc.git] / gcc / config / i386 / gmon-sol2.c
blobd20762156a6aa4e6314252533c50377aae091f75
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. [rescinded 22 July 1999]
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
32 * This is a modified gmon.c by J.W.Hawtin <oolon@ankh.org>,
33 * 14/8/96 based on the original gmon.c in GCC and the hacked version
34 * solaris 2 sparc version (config/sparc/gmon-sol.c) by Mark Eichin. To do
35 * process profiling on solaris 2.X X86
37 * It must be used in conjunction with sol2-gc1.asm, which is used to start
38 * and stop process monitoring.
40 * Differences.
42 * On Solaris 2 _mcount is called by library functions not mcount, so support
43 * has been added for both.
45 * Also the prototype for profil() is different
47 * Solaris 2 does not seem to have char *minbrk whcih allows the setting of
48 * the minimum SBRK region so this code has been removed and lets pray malloc
49 * does not mess it up.
51 * Notes
53 * This code could easily be integrated with the original gmon.c and perhaps
54 * should be.
56 #include "tconfig.h"
57 #include "tsystem.h"
58 #include <fcntl.h> /* for creat() */
60 #ifdef DEBUG
61 #include <stdio.h>
62 #endif
64 static void moncontrol (int);
65 extern void monstartup (char *, char *);
66 extern void _mcleanup (void);
67 extern void internal_mcount (
68 #ifdef __x86_64__
69 char *, unsigned short *
70 #else
71 void
72 #endif
76 struct phdr {
77 char *lpc;
78 char *hpc;
79 int ncnt;
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 */
91 struct tostruct {
92 char *selfpc;
93 long count;
94 unsigned short link;
97 struct rawarc {
98 unsigned long raw_frompc;
99 unsigned long raw_selfpc;
100 long raw_count;
102 #define ROUNDDOWN(x,y) (((x)/(y))*(y))
103 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
105 /* char *minbrk; */
107 typedef __SIZE_TYPE__ size_t;
108 typedef __PTRDIFF_TYPE__ intptr_t;
111 * froms is actually a bunch of unsigned shorts indexing tos
113 static int profiling = 3;
114 static unsigned short *froms;
115 static struct tostruct *tos = 0;
116 static long tolimit = 0;
117 static char *s_lowpc = 0;
118 static char *s_highpc = 0;
119 static size_t s_textsize = 0;
121 static int ssiz;
122 static char *sbuf;
123 static int s_scale;
124 /* see profil(2) where this is describe (incorrectly) */
125 #define SCALE_1_TO_1 0x10000L
127 #define MSG "No space for profiling buffer(s)\n"
129 extern int errno;
131 extern void *sbrk (intptr_t);
133 void
134 monstartup(char *lowpc, char *highpc)
136 size_t monsize;
137 char *buffer;
138 register size_t o;
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.
144 lowpc = (char *)
145 ROUNDDOWN((size_t)lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
146 s_lowpc = lowpc;
147 highpc = (char *)
148 ROUNDUP((size_t)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;
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;
163 tolimit = s_textsize * ARCDENSITY / 100;
164 if ( tolimit < MINARCS ) {
165 tolimit = MINARCS;
166 } else if ( tolimit > 65534 ) {
167 tolimit = 65534;
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;
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 */
192 int quot = o / monsize;
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);
203 #endif
204 else
205 s_scale = SCALE_1_TO_1;
206 moncontrol(1);
209 void
210 _mcleanup (void)
212 int fd;
213 int fromindex;
214 int endfrom;
215 char *frompc;
216 int toindex;
217 struct rawarc rawarc;
219 moncontrol(0);
220 fd = creat( "gmon.out" , 0666 );
221 if ( fd < 0 ) {
222 perror( "mcount: gmon.out" );
223 return;
225 # ifdef DEBUG
226 fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf , ssiz );
227 # endif /* DEBUG */
229 write( fd , sbuf , ssiz );
230 endfrom = s_textsize / (HASHFRACTION * sizeof(*froms));
231 for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) {
232 if ( froms[fromindex] == 0 ) {
233 continue;
235 frompc = s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms));
236 for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) {
237 # ifdef DEBUG
238 fprintf( stderr ,
239 "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
240 frompc , tos[toindex].selfpc , tos[toindex].count );
241 # endif /* DEBUG */
242 rawarc.raw_frompc = (unsigned long) frompc;
243 rawarc.raw_selfpc = (unsigned long) tos[toindex].selfpc;
244 rawarc.raw_count = tos[toindex].count;
245 write( fd , &rawarc , sizeof rawarc );
248 close( fd );
251 #ifdef __x86_64__
252 /* See GLIBC for additional information about this technique. */
253 asm(".globl _mcount\n"
254 "\t.type\t_mcount, @function\n"
255 "_mcount:\n"
256 /* The compiler calls _mcount after the prologue, and does not
257 save any of the registers. Therefore we must preserve all
258 seven registers which may contain function arguments. */
259 "\tsubq\t$0x38,%rsp\n"
260 "\tmovq\t%rax,(%rsp)\n"
261 "\tmovq\t%rcx,0x08(%rsp)\n"
262 "\tmovq\t%rdx,0x10(%rsp)\n"
263 "\tmovq\t%rsi,0x18(%rsp)\n"
264 "\tmovq\t%rdi,0x20(%rsp)\n"
265 "\tmovq\t%r8,0x28(%rsp)\n"
266 "\tmovq\t%r9,0x30(%rsp)\n"
267 /* Get SELFPC (pushed by the call to this function) and
268 FROMPCINDEX (via the frame pointer. */
269 "\tmovq\t0x38(%rsp),%rdi\n"
270 "\tmovq\t0x8(%rbp),%rsi\n"
271 "\tcallq\tinternal_mcount\n"
272 /* Restore the saved registers. */
273 "\tmovq\t0x30(%rsp),%r9\n"
274 "\tmovq\t0x28(%rsp),%r8\n"
275 "\tmovq\t0x20(%rsp),%rdi\n"
276 "\tmovq\t0x18(%rsp),%rsi\n"
277 "\tmovq\t0x10(%rsp),%rdx\n"
278 "\tmovq\t0x08(%rsp),%rdx\n"
279 "\tmovq\t(%rsp),%rax\n"
280 "\taddq\t$0x38,%rsp\n"
281 "\tretq\n"
283 #else
284 /* Solaris 2 libraries use _mcount. */
285 asm(".globl _mcount; _mcount: jmp internal_mcount");
286 /* This is for compatibility with old versions of gcc which used mcount. */
287 asm(".globl mcount; mcount: jmp internal_mcount");
288 #endif
290 void
291 internal_mcount (
292 #ifdef __x86_64__
293 char *selfpc,
294 unsigned short *frompcindex
295 #else
296 void
297 #endif
300 #ifndef __x86_64__
301 register char *selfpc;
302 register unsigned short *frompcindex;
303 #endif
304 register struct tostruct *top;
305 register struct tostruct *prevtop;
306 register long toindex;
307 static char already_setup;
309 #ifndef __x86_64__
311 * find the return address for mcount,
312 * and the return address for mcount's caller.
315 /* selfpc = pc pushed by mcount call.
316 This identifies the function that was just entered. */
317 selfpc = (void *) __builtin_return_address (0);
318 /* frompcindex = pc in preceding frame.
319 This identifies the caller of the function just entered. */
320 frompcindex = (void *) __builtin_return_address (1);
321 #endif
323 if(!already_setup) {
324 extern char etext[];
325 already_setup = 1;
326 #ifdef __x86_64__
327 monstartup(0, etext);
328 #else
329 monstartup((char*)0x08040000, etext);
330 #endif
331 #ifdef USE_ONEXIT
332 on_exit(_mcleanup, 0);
333 #else
334 atexit(_mcleanup);
335 #endif
338 * check that we are profiling
339 * and that we aren't recursively invoked.
341 if (profiling) {
342 goto out;
344 profiling++;
346 * check that frompcindex is a reasonable pc value.
347 * for example: signal catchers get called from the stack,
348 * not from text space. too bad.
350 frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc);
351 if ((unsigned long)frompcindex > s_textsize) {
352 goto done;
354 frompcindex =
355 &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))];
356 toindex = *frompcindex;
357 if (toindex == 0) {
359 * first time traversing this arc
361 toindex = ++tos[0].link;
362 if (toindex >= tolimit) {
363 goto overflow;
365 *frompcindex = toindex;
366 top = &tos[toindex];
367 top->selfpc = selfpc;
368 top->count = 1;
369 top->link = 0;
370 goto done;
372 top = &tos[toindex];
373 if (top->selfpc == selfpc) {
375 * arc at front of chain; usual case.
377 top->count++;
378 goto done;
381 * have to go looking down chain for it.
382 * top points to what we are looking at,
383 * prevtop points to previous top.
384 * we know it is not at the head of the chain.
386 for (; /* goto done */; ) {
387 if (top->link == 0) {
389 * top is end of the chain and none of the chain
390 * had top->selfpc == selfpc.
391 * so we allocate a new tostruct
392 * and link it to the head of the chain.
394 toindex = ++tos[0].link;
395 if (toindex >= tolimit) {
396 goto overflow;
398 top = &tos[toindex];
399 top->selfpc = selfpc;
400 top->count = 1;
401 top->link = *frompcindex;
402 *frompcindex = toindex;
403 goto done;
406 * otherwise, check the next arc on the chain.
408 prevtop = top;
409 top = &tos[top->link];
410 if (top->selfpc == selfpc) {
412 * there it is.
413 * increment its count
414 * move it to the head of the chain.
416 top->count++;
417 toindex = prevtop->link;
418 prevtop->link = top->link;
419 top->link = *frompcindex;
420 *frompcindex = toindex;
421 goto done;
425 done:
426 profiling--;
427 /* and fall through */
428 out:
429 return; /* normal return restores saved registers */
431 overflow:
432 profiling++; /* halt further profiling */
433 # define TOLIMIT "mcount: tos overflow\n"
434 write(2, TOLIMIT, sizeof(TOLIMIT));
435 goto out;
439 * Control profiling
440 * profiling is what mcount checks to see if
441 * all the data structures are ready.
443 static void
444 moncontrol(int mode)
446 if (mode)
448 /* start */
449 profil((unsigned short *)(sbuf + sizeof(struct phdr)),
450 ssiz - sizeof(struct phdr),
451 (size_t)s_lowpc, s_scale);
453 profiling = 0;
454 } else {
455 /* stop */
456 profil((unsigned short *)0, 0, 0, 0);
457 profiling = 3;