Annotation of src/usr.bin/gprof/tahoe.c, Revision 1.2
1.2 ! deraadt 1: /* $OpenBSD: tahoe.c,v 1.5 1995/04/19 07:16:27 cgd Exp $ */
1.1 deraadt 2: /* $NetBSD: tahoe.c,v 1.5 1995/04/19 07:16:27 cgd Exp $ */
3:
4: /*
5: * Copyright (c) 1983, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "@(#)tahoe.c 8.1 (Berkeley) 6/6/93";
40: #else
1.2 ! deraadt 41: static char rcsid[] = "$OpenBSD: tahoe.c,v 1.5 1995/04/19 07:16:27 cgd Exp $";
1.1 deraadt 42: #endif
43: #endif /* not lint */
44:
45: #include "gprof.h"
46:
47: /*
48: * a namelist entry to be the child of indirect callf
49: */
50: nltype indirectchild = {
51: "(*)" , /* the name */
52: (unsigned long) 0 , /* the pc entry point */
53: (unsigned long) 0 , /* entry point aligned to histogram */
54: (double) 0.0 , /* ticks in this routine */
55: (double) 0.0 , /* cumulative ticks in children */
56: (long) 0 , /* how many times called */
57: (long) 0 , /* how many calls to self */
58: (double) 1.0 , /* propagation fraction */
59: (double) 0.0 , /* self propagation time */
60: (double) 0.0 , /* child propagation time */
61: (bool) 0 , /* print flag */
62: (int) 0 , /* index in the graph list */
63: (int) 0 , /* graph call chain top-sort order */
64: (int) 0 , /* internal number of cycle on */
65: (struct nl *) &indirectchild , /* pointer to head of cycle */
66: (struct nl *) 0 , /* pointer to next member of cycle */
67: (arctype *) 0 , /* list of caller arcs */
68: (arctype *) 0 /* list of callee arcs */
69: };
70:
71: operandenum
72: operandmode( modep )
73: unsigned char *modep;
74: {
75: long usesreg = ((long)*modep) & 0xf;
76:
77: switch ( ((long)*modep) >> 4 ) {
78: case 0:
79: case 1:
80: case 2:
81: case 3:
82: return literal;
83: case 4:
84: return indexed;
85: case 5:
86: return reg;
87: case 6:
88: return regdef;
89: case 7:
90: return autodec;
91: case 8:
92: return ( usesreg != 0xe ? autoinc : immediate );
93: case 9:
94: return ( usesreg != PC ? autoincdef : absolute );
95: case 10:
96: return ( usesreg != PC ? bytedisp : byterel );
97: case 11:
98: return ( usesreg != PC ? bytedispdef : bytereldef );
99: case 12:
100: return ( usesreg != PC ? worddisp : wordrel );
101: case 13:
102: return ( usesreg != PC ? worddispdef : wordreldef );
103: case 14:
104: return ( usesreg != PC ? longdisp : longrel );
105: case 15:
106: return ( usesreg != PC ? longdispdef : longreldef );
107: }
108: /* NOTREACHED */
109: }
110:
111: char *
112: operandname( mode )
113: operandenum mode;
114: {
115:
116: switch ( mode ) {
117: case literal:
118: return "literal";
119: case indexed:
120: return "indexed";
121: case reg:
122: return "register";
123: case regdef:
124: return "register deferred";
125: case autodec:
126: return "autodecrement";
127: case autoinc:
128: return "autoincrement";
129: case autoincdef:
130: return "autoincrement deferred";
131: case bytedisp:
132: return "byte displacement";
133: case bytedispdef:
134: return "byte displacement deferred";
135: case byterel:
136: return "byte relative";
137: case bytereldef:
138: return "byte relative deferred";
139: case worddisp:
140: return "word displacement";
141: case worddispdef:
142: return "word displacement deferred";
143: case wordrel:
144: return "word relative";
145: case wordreldef:
146: return "word relative deferred";
147: case immediate:
148: return "immediate";
149: case absolute:
150: return "absolute";
151: case longdisp:
152: return "long displacement";
153: case longdispdef:
154: return "long displacement deferred";
155: case longrel:
156: return "long relative";
157: case longreldef:
158: return "long relative deferred";
159: }
160: /* NOTREACHED */
161: }
162:
163: long
164: operandlength( modep )
165: unsigned char *modep;
166: {
167:
168: switch ( operandmode( modep ) ) {
169: case literal:
170: case reg:
171: case regdef:
172: case autodec:
173: case autoinc:
174: case autoincdef:
175: return 1;
176: case bytedisp:
177: case bytedispdef:
178: case byterel:
179: case bytereldef:
180: return 2;
181: case worddisp:
182: case worddispdef:
183: case wordrel:
184: case wordreldef:
185: return 3;
186: case immediate:
187: case absolute:
188: case longdisp:
189: case longdispdef:
190: case longrel:
191: case longreldef:
192: return 5;
193: case indexed:
194: return 1+operandlength( modep + 1 );
195: }
196: /* NOTREACHED */
197: }
198:
199: unsigned long
200: reladdr( modep )
201: char *modep;
202: {
203: operandenum mode = operandmode( modep );
204: char *cp;
205: short *sp;
206: long *lp;
207: int i;
208: long value = 0;
209:
210: cp = modep;
211: cp += 1; /* skip over the mode */
212: switch ( mode ) {
213: default:
214: fprintf( stderr , "[reladdr] not relative address\n" );
215: return (unsigned long) modep;
216: case byterel:
217: return (unsigned long) ( cp + sizeof *cp + *cp );
218: case wordrel:
219: for (i = 0; i < sizeof *sp; i++)
220: value = (value << 8) + (cp[i] & 0xff);
221: return (unsigned long) ( cp + sizeof *sp + value );
222: case longrel:
223: for (i = 0; i < sizeof *lp; i++)
224: value = (value << 8) + (cp[i] & 0xff);
225: return (unsigned long) ( cp + sizeof *lp + value );
226: }
227: }
228:
229: findcall( parentp , p_lowpc , p_highpc )
230: nltype *parentp;
231: unsigned long p_lowpc;
232: unsigned long p_highpc;
233: {
234: unsigned char *instructp;
235: long length;
236: nltype *childp;
237: operandenum mode;
238: operandenum firstmode;
239: unsigned long destpc;
240:
241: if ( textspace == 0 ) {
242: return;
243: }
244: if ( p_lowpc < s_lowpc ) {
245: p_lowpc = s_lowpc;
246: }
247: if ( p_highpc > s_highpc ) {
248: p_highpc = s_highpc;
249: }
250: # ifdef DEBUG
251: if ( debug & CALLDEBUG ) {
252: printf( "[findcall] %s: 0x%x to 0x%x\n" ,
253: parentp -> name , p_lowpc , p_highpc );
254: }
255: # endif DEBUG
256: for ( instructp = textspace + p_lowpc ;
257: instructp < textspace + p_highpc ;
258: instructp += length ) {
259: length = 1;
260: if ( *instructp == CALLF ) {
261: /*
262: * maybe a callf, better check it out.
263: * skip the count of the number of arguments.
264: */
265: # ifdef DEBUG
266: if ( debug & CALLDEBUG ) {
267: printf( "[findcall]\t0x%x:callf" , instructp - textspace );
268: }
269: # endif DEBUG
270: firstmode = operandmode( instructp+length );
271: switch ( firstmode ) {
272: case literal:
273: case immediate:
274: break;
275: default:
276: goto botched;
277: }
278: length += operandlength( instructp+length );
279: mode = operandmode( instructp + length );
280: # ifdef DEBUG
281: if ( debug & CALLDEBUG ) {
282: printf( "\tfirst operand is %s", operandname( firstmode ) );
283: printf( "\tsecond operand is %s\n" , operandname( mode ) );
284: }
285: # endif DEBUG
286: switch ( mode ) {
287: case regdef:
288: case bytedispdef:
289: case worddispdef:
290: case longdispdef:
291: case bytereldef:
292: case wordreldef:
293: case longreldef:
294: /*
295: * indirect call: call through pointer
296: * either *d(r) as a parameter or local
297: * (r) as a return value
298: * *f as a global pointer
299: * [are there others that we miss?,
300: * e.g. arrays of pointers to functions???]
301: */
302: addarc( parentp , &indirectchild , (long) 0 );
303: length += operandlength( instructp + length );
304: continue;
305: case byterel:
306: case wordrel:
307: case longrel:
308: /*
309: * regular pc relative addressing
310: * check that this is the address of
311: * a function.
312: */
313: destpc = reladdr( instructp+length )
314: - (unsigned long) textspace;
315: if ( destpc >= s_lowpc && destpc <= s_highpc ) {
316: childp = nllookup( destpc );
317: # ifdef DEBUG
318: if ( debug & CALLDEBUG ) {
319: printf( "[findcall]\tdestpc 0x%x" , destpc );
320: printf( " childp->name %s" , childp -> name );
321: printf( " childp->value 0x%x\n" ,
322: childp -> value );
323: }
324: # endif DEBUG
325: if ( childp -> value == destpc ) {
326: /*
327: * a hit
328: */
329: addarc( parentp , childp , (long) 0 );
330: length += operandlength( instructp + length );
331: continue;
332: }
333: goto botched;
334: }
335: /*
336: * else:
337: * it looked like a callf,
338: * but it wasn't to anywhere.
339: */
340: goto botched;
341: default:
342: botched:
343: /*
344: * something funny going on.
345: */
346: # ifdef DEBUG
347: if ( debug & CALLDEBUG ) {
348: printf( "[findcall]\tbut it's a botch\n" );
349: }
350: # endif DEBUG
351: length = 1;
352: continue;
353: }
354: }
355: }
356: }