]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/sparc/gmon-sol2.c
Merge basic-improvements-branch to trunk
[thirdparty/gcc.git] / gcc / config / sparc / 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. [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.
17 *
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.
29 */
30
31 /* Mangled into a form that works on SPARC Solaris 2 by Mark Eichin
32 * for Cygnus Support, July 1992.
33 */
34
35 #include "tconfig.h"
36 #include "tsystem.h"
37 #include "coretypes.h"
38 #include "tm.h"
39
40 #if 0
41 #include "sparc/gmon.h"
42 #else
43 struct phdr {
44 char *lpc;
45 char *hpc;
46 int ncnt;
47 };
48 #define HISTFRACTION 2
49 #define HISTCOUNTER unsigned short
50 #define HASHFRACTION 1
51 #define ARCDENSITY 2
52 #define MINARCS 50
53 struct tostruct {
54 char *selfpc;
55 long count;
56 unsigned short link;
57 };
58 struct rawarc {
59 unsigned long raw_frompc;
60 unsigned long raw_selfpc;
61 long raw_count;
62 };
63 #define ROUNDDOWN(x,y) (((x)/(y))*(y))
64 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
65
66 #endif
67
68 /* extern mcount() asm ("mcount"); */
69 /*extern*/ char *minbrk /* asm ("minbrk") */;
70
71 /*
72 * froms is actually a bunch of unsigned shorts indexing tos
73 */
74 static int profiling = 3;
75 static unsigned short *froms;
76 static struct tostruct *tos = 0;
77 static long tolimit = 0;
78 static char *s_lowpc = 0;
79 static char *s_highpc = 0;
80 static unsigned long s_textsize = 0;
81
82 static int ssiz;
83 static char *sbuf;
84 static int s_scale;
85 /* see profil(2) where this is describe (incorrectly) */
86 #define SCALE_1_TO_1 0x10000L
87
88 #define MSG "No space for profiling buffer(s)\n"
89
90 static void moncontrol PARAMS ((int));
91 extern void monstartup PARAMS ((char *, char *));
92 extern void _mcleanup PARAMS ((void));
93
94 void monstartup(lowpc, highpc)
95 char *lowpc;
96 char *highpc;
97 {
98 int monsize;
99 char *buffer;
100 register int o;
101
102 /*
103 * round lowpc and highpc to multiples of the density we're using
104 * so the rest of the scaling (here and in gprof) stays in ints.
105 */
106 lowpc = (char *)
107 ROUNDDOWN((unsigned long)lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
108 s_lowpc = lowpc;
109 highpc = (char *)
110 ROUNDUP((unsigned long)highpc, HISTFRACTION*sizeof(HISTCOUNTER));
111 s_highpc = highpc;
112 s_textsize = highpc - lowpc;
113 monsize = (s_textsize / HISTFRACTION) + sizeof(struct phdr);
114 buffer = sbrk( monsize );
115 if ( buffer == (char *) -1 ) {
116 write( 2 , MSG , sizeof(MSG) );
117 return;
118 }
119 froms = (unsigned short *) sbrk( s_textsize / HASHFRACTION );
120 if ( froms == (unsigned short *) -1 ) {
121 write( 2 , MSG , sizeof(MSG) );
122 froms = 0;
123 return;
124 }
125 tolimit = s_textsize * ARCDENSITY / 100;
126 if ( tolimit < MINARCS ) {
127 tolimit = MINARCS;
128 } else if ( tolimit > 65534 ) {
129 tolimit = 65534;
130 }
131 tos = (struct tostruct *) sbrk( tolimit * sizeof( struct tostruct ) );
132 if ( tos == (struct tostruct *) -1 ) {
133 write( 2 , MSG , sizeof(MSG) );
134 froms = 0;
135 tos = 0;
136 return;
137 }
138 minbrk = sbrk(0);
139 tos[0].link = 0;
140 sbuf = buffer;
141 ssiz = monsize;
142 ( (struct phdr *) buffer ) -> lpc = lowpc;
143 ( (struct phdr *) buffer ) -> hpc = highpc;
144 ( (struct phdr *) buffer ) -> ncnt = ssiz;
145 monsize -= sizeof(struct phdr);
146 if ( monsize <= 0 )
147 return;
148 o = highpc - lowpc;
149 if( monsize < o )
150 #ifndef hp300
151 s_scale = ( (float) monsize / o ) * SCALE_1_TO_1;
152 #else /* avoid floating point */
153 {
154 int quot = o / monsize;
155
156 if (quot >= 0x10000)
157 s_scale = 1;
158 else if (quot >= 0x100)
159 s_scale = 0x10000 / quot;
160 else if (o >= 0x800000)
161 s_scale = 0x1000000 / (o / (monsize >> 8));
162 else
163 s_scale = 0x1000000 / ((o << 8) / monsize);
164 }
165 #endif
166 else
167 s_scale = SCALE_1_TO_1;
168 moncontrol(1);
169 }
170
171 void
172 _mcleanup()
173 {
174 int fd;
175 int fromindex;
176 int endfrom;
177 char *frompc;
178 int toindex;
179 struct rawarc rawarc;
180 char *profdir;
181 const char *proffile;
182 char *progname;
183 char buf[PATH_MAX];
184 extern char **___Argv;
185
186 moncontrol(0);
187
188 if ((profdir = getenv("PROFDIR")) != NULL) {
189 /* If PROFDIR contains a null value, no profiling output is produced */
190 if (*profdir == '\0') {
191 return;
192 }
193
194 progname=strrchr(___Argv[0], '/');
195 if (progname == NULL)
196 progname=___Argv[0];
197 else
198 progname++;
199
200 sprintf(buf, "%s/%ld.%s", profdir, (long) getpid(), progname);
201 proffile = buf;
202 } else {
203 proffile = "gmon.out";
204 }
205
206 fd = creat( proffile, 0666 );
207 if ( fd < 0 ) {
208 perror( proffile );
209 return;
210 }
211 # ifdef DEBUG
212 fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf , ssiz );
213 # endif /* DEBUG */
214 write( fd , sbuf , ssiz );
215 endfrom = s_textsize / (HASHFRACTION * sizeof(*froms));
216 for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) {
217 if ( froms[fromindex] == 0 ) {
218 continue;
219 }
220 frompc = s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms));
221 for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) {
222 # ifdef DEBUG
223 fprintf( stderr ,
224 "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
225 frompc , tos[toindex].selfpc , tos[toindex].count );
226 # endif /* DEBUG */
227 rawarc.raw_frompc = (unsigned long) frompc;
228 rawarc.raw_selfpc = (unsigned long) tos[toindex].selfpc;
229 rawarc.raw_count = tos[toindex].count;
230 write( fd , &rawarc , sizeof rawarc );
231 }
232 }
233 close( fd );
234 }
235
236 /*
237 * The SPARC stack frame is only held together by the frame pointers
238 * in the register windows. According to the SVR4 SPARC ABI
239 * Supplement, Low Level System Information/Operating System
240 * Interface/Software Trap Types, a type 3 trap will flush all of the
241 * register windows to the stack, which will make it possible to walk
242 * the frames and find the return addresses.
243 * However, it seems awfully expensive to incur a trap (system
244 * call) for every function call. It turns out that "call" simply puts
245 * the return address in %o7 expecting the "save" in the procedure to
246 * shift it into %i7; this means that before the "save" occurs, %o7
247 * contains the address of the call to mcount, and %i7 still contains
248 * the caller above that. The asm mcount here simply saves those
249 * registers in argument registers and branches to internal_mcount,
250 * simulating a call with arguments.
251 * Kludges:
252 * 1) the branch to internal_mcount is hard coded; it should be
253 * possible to tell asm to use the assembler-name of a symbol.
254 * 2) in theory, the function calling mcount could have saved %i7
255 * somewhere and reused the register; in practice, I *think* this will
256 * break longjmp (and maybe the debugger) but I'm not certain. (I take
257 * some comfort in the knowledge that it will break the native mcount
258 * as well.)
259 * 3) if builtin_return_address worked, this could be portable.
260 * However, it would really have to be optimized for arguments of 0
261 * and 1 and do something like what we have here in order to avoid the
262 * trap per function call performance hit.
263 * 4) the atexit and monsetup calls prevent this from simply
264 * being a leaf routine that doesn't do a "save" (and would thus have
265 * access to %o7 and %i7 directly) but the call to write() at the end
266 * would have also prevented this.
267 *
268 * -- [eichin:19920702.1107EST]
269 */
270
271 static void internal_mcount PARAMS ((char *, unsigned short *)) ATTRIBUTE_UNUSED;
272
273 /* i7 == last ret, -> frompcindex */
274 /* o7 == current ret, -> selfpc */
275 /* Solaris 2 libraries use _mcount. */
276 asm(".global _mcount; _mcount: mov %i7,%o1; mov %o7,%o0;b,a internal_mcount");
277 /* This is for compatibility with old versions of gcc which used mcount. */
278 asm(".global mcount; mcount: mov %i7,%o1; mov %o7,%o0;b,a internal_mcount");
279
280 static void internal_mcount(selfpc, frompcindex)
281 register char *selfpc;
282 register unsigned short *frompcindex;
283 {
284 register struct tostruct *top;
285 register struct tostruct *prevtop;
286 register long toindex;
287 static char already_setup;
288
289 /*
290 * find the return address for mcount,
291 * and the return address for mcount's caller.
292 */
293
294 if(!already_setup) {
295 extern char etext[];
296 extern char _start[];
297 extern char _init[];
298 already_setup = 1;
299 monstartup(_start < _init ? _start : _init, etext);
300 #ifdef USE_ONEXIT
301 on_exit(_mcleanup, 0);
302 #else
303 atexit(_mcleanup);
304 #endif
305 }
306 /*
307 * check that we are profiling
308 * and that we aren't recursively invoked.
309 */
310 if (profiling) {
311 goto out;
312 }
313 profiling++;
314 /*
315 * check that frompcindex is a reasonable pc value.
316 * for example: signal catchers get called from the stack,
317 * not from text space. too bad.
318 */
319 frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc);
320 if ((unsigned long)frompcindex > s_textsize) {
321 goto done;
322 }
323 frompcindex =
324 &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))];
325 toindex = *frompcindex;
326 if (toindex == 0) {
327 /*
328 * first time traversing this arc
329 */
330 toindex = ++tos[0].link;
331 if (toindex >= tolimit) {
332 goto overflow;
333 }
334 *frompcindex = toindex;
335 top = &tos[toindex];
336 top->selfpc = selfpc;
337 top->count = 1;
338 top->link = 0;
339 goto done;
340 }
341 top = &tos[toindex];
342 if (top->selfpc == selfpc) {
343 /*
344 * arc at front of chain; usual case.
345 */
346 top->count++;
347 goto done;
348 }
349 /*
350 * have to go looking down chain for it.
351 * top points to what we are looking at,
352 * prevtop points to previous top.
353 * we know it is not at the head of the chain.
354 */
355 for (; /* goto done */; ) {
356 if (top->link == 0) {
357 /*
358 * top is end of the chain and none of the chain
359 * had top->selfpc == selfpc.
360 * so we allocate a new tostruct
361 * and link it to the head of the chain.
362 */
363 toindex = ++tos[0].link;
364 if (toindex >= tolimit) {
365 goto overflow;
366 }
367 top = &tos[toindex];
368 top->selfpc = selfpc;
369 top->count = 1;
370 top->link = *frompcindex;
371 *frompcindex = toindex;
372 goto done;
373 }
374 /*
375 * otherwise, check the next arc on the chain.
376 */
377 prevtop = top;
378 top = &tos[top->link];
379 if (top->selfpc == selfpc) {
380 /*
381 * there it is.
382 * increment its count
383 * move it to the head of the chain.
384 */
385 top->count++;
386 toindex = prevtop->link;
387 prevtop->link = top->link;
388 top->link = *frompcindex;
389 *frompcindex = toindex;
390 goto done;
391 }
392
393 }
394 done:
395 profiling--;
396 /* and fall through */
397 out:
398 return; /* normal return restores saved registers */
399
400 overflow:
401 profiling++; /* halt further profiling */
402 # define TOLIMIT "mcount: tos overflow\n"
403 write(2, TOLIMIT, sizeof(TOLIMIT));
404 goto out;
405 }
406
407 /*
408 * Control profiling
409 * profiling is what mcount checks to see if
410 * all the data structures are ready.
411 */
412 static void moncontrol(mode)
413 int mode;
414 {
415 if (mode) {
416 /* start */
417 profil((unsigned short *)(sbuf + sizeof(struct phdr)),
418 ssiz - sizeof(struct phdr),
419 (long)s_lowpc, s_scale);
420 profiling = 0;
421 } else {
422 /* stop */
423 profil((unsigned short *)0, 0, 0, 0);
424 profiling = 3;
425 }
426 }