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