Merge from vendor branch OPENSSH:
[dragonfly.git] / usr.bin / gprof / tahoe.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  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  * @(#)tahoe.c  8.1 (Berkeley) 6/6/93
34  *
35  * $DragonFly: src/usr.bin/gprof/Attic/tahoe.c,v 1.3 2003/10/04 20:36:45 hmp Exp $
36  */
37
38 #include        "gprof.h"
39
40     /*
41      *  a namelist entry to be the child of indirect callf
42      */
43 nltype  indirectchild = {
44         "(*)" ,                         /* the name */
45         (unsigned long) 0 ,             /* the pc entry point */
46         (unsigned long) 0 ,             /* entry point aligned to histogram */
47         (double) 0.0 ,                  /* ticks in this routine */
48         (double) 0.0 ,                  /* cumulative ticks in children */
49         (long) 0 ,                      /* how many times called */
50         (long) 0 ,                      /* how many calls to self */
51         (double) 1.0 ,                  /* propagation fraction */
52         (double) 0.0 ,                  /* self propagation time */
53         (double) 0.0 ,                  /* child propagation time */
54         (bool) 0 ,                      /* print flag */
55         (int) 0 ,                       /* index in the graph list */
56         (int) 0 ,                       /* graph call chain top-sort order */
57         (int) 0 ,                       /* internal number of cycle on */
58         (struct nl *) &indirectchild ,  /* pointer to head of cycle */
59         (struct nl *) 0 ,               /* pointer to next member of cycle */
60         (arctype *) 0 ,                 /* list of caller arcs */
61         (arctype *) 0                   /* list of callee arcs */
62     };
63
64 operandenum
65 operandmode(unsigned char *modep)
66 {
67     long        usesreg = ((long)*modep) & 0xf;
68
69     switch ( ((long)*modep) >> 4 ) {
70         case 0:
71         case 1:
72         case 2:
73         case 3:
74             return literal;
75         case 4:
76             return indexed;
77         case 5:
78             return reg;
79         case 6:
80             return regdef;
81         case 7:
82             return autodec;
83         case 8:
84             return ( usesreg != 0xe ? autoinc : immediate );
85         case 9:
86             return ( usesreg != PC ? autoincdef : absolute );
87         case 10:
88             return ( usesreg != PC ? bytedisp : byterel );
89         case 11:
90             return ( usesreg != PC ? bytedispdef : bytereldef );
91         case 12:
92             return ( usesreg != PC ? worddisp : wordrel );
93         case 13:
94             return ( usesreg != PC ? worddispdef : wordreldef );
95         case 14:
96             return ( usesreg != PC ? longdisp : longrel );
97         case 15:
98             return ( usesreg != PC ? longdispdef : longreldef );
99     }
100     /* NOTREACHED */
101 }
102
103 char *
104 operandname(operandenum mode)
105 {
106
107     switch ( mode ) {
108         case literal:
109             return "literal";
110         case indexed:
111             return "indexed";
112         case reg:
113             return "register";
114         case regdef:
115             return "register deferred";
116         case autodec:
117             return "autodecrement";
118         case autoinc:
119             return "autoincrement";
120         case autoincdef:
121             return "autoincrement deferred";
122         case bytedisp:
123             return "byte displacement";
124         case bytedispdef:
125             return "byte displacement deferred";
126         case byterel:
127             return "byte relative";
128         case bytereldef:
129             return "byte relative deferred";
130         case worddisp:
131             return "word displacement";
132         case worddispdef:
133             return "word displacement deferred";
134         case wordrel:
135             return "word relative";
136         case wordreldef:
137             return "word relative deferred";
138         case immediate:
139             return "immediate";
140         case absolute:
141             return "absolute";
142         case longdisp:
143             return "long displacement";
144         case longdispdef:
145             return "long displacement deferred";
146         case longrel:
147             return "long relative";
148         case longreldef:
149             return "long relative deferred";
150     }
151     /* NOTREACHED */
152 }
153
154 long
155 operandlength(unsigned char *modep)
156 {
157
158     switch ( operandmode( modep ) ) {
159         case literal:
160         case reg:
161         case regdef:
162         case autodec:
163         case autoinc:
164         case autoincdef:
165             return 1;
166         case bytedisp:
167         case bytedispdef:
168         case byterel:
169         case bytereldef:
170             return 2;
171         case worddisp:
172         case worddispdef:
173         case wordrel:
174         case wordreldef:
175             return 3;
176         case immediate:
177         case absolute:
178         case longdisp:
179         case longdispdef:
180         case longrel:
181         case longreldef:
182             return 5;
183         case indexed:
184             return 1+operandlength( modep + 1 );
185     }
186     /* NOTREACHED */
187 }
188
189 unsigned long
190 reladdr(char *modep)
191 {
192     operandenum mode = operandmode( modep );
193     char        *cp;
194     short       *sp;
195     long        *lp;
196     int         i;
197     long        value = 0;
198
199     cp = modep;
200     cp += 1;                    /* skip over the mode */
201     switch ( mode ) {
202         default:
203             fprintf( stderr , "[reladdr] not relative address\n" );
204             return (unsigned long) modep;
205         case byterel:
206             return (unsigned long) ( cp + sizeof *cp + *cp );
207         case wordrel:
208             for (i = 0; i < sizeof *sp; i++)
209                 value = (value << 8) + (cp[i] & 0xff);
210             return (unsigned long) ( cp + sizeof *sp + value );
211         case longrel:
212             for (i = 0; i < sizeof *lp; i++)
213                 value = (value << 8) + (cp[i] & 0xff);
214             return (unsigned long) ( cp + sizeof *lp + value );
215     }
216 }
217
218 findcall(nltype *parentp, unsigned long p_lowpc, unsigned long p_highpc)
219 {
220     unsigned char       *instructp;
221     long                length;
222     nltype              *childp;
223     operandenum         mode;
224     operandenum         firstmode;
225     unsigned long       destpc;
226
227     if ( textspace == 0 ) {
228         return;
229     }
230     if ( p_lowpc < s_lowpc ) {
231         p_lowpc = s_lowpc;
232     }
233     if ( p_highpc > s_highpc ) {
234         p_highpc = s_highpc;
235     }
236 #   ifdef DEBUG
237         if ( debug & CALLDEBUG ) {
238             printf( "[findcall] %s: 0x%x to 0x%x\n" ,
239                     parentp -> name , p_lowpc , p_highpc );
240         }
241 #   endif DEBUG
242     for (   instructp = textspace + p_lowpc ;
243             instructp < textspace + p_highpc ;
244             instructp += length ) {
245         length = 1;
246         if ( *instructp == CALLF ) {
247                 /*
248                  *      maybe a callf, better check it out.
249                  *      skip the count of the number of arguments.
250                  */
251 #           ifdef DEBUG
252                 if ( debug & CALLDEBUG ) {
253                     printf( "[findcall]\t0x%x:callf" , instructp - textspace );
254                 }
255 #           endif DEBUG
256             firstmode = operandmode( instructp+length );
257             switch ( firstmode ) {
258                 case literal:
259                 case immediate:
260                     break;
261                 default:
262                     goto botched;
263             }
264             length += operandlength( instructp+length );
265             mode = operandmode( instructp + length );
266 #           ifdef DEBUG
267                 if ( debug & CALLDEBUG ) {
268                     printf( "\tfirst operand is %s", operandname( firstmode ) );
269                     printf( "\tsecond operand is %s\n" , operandname( mode ) );
270                 }
271 #           endif DEBUG
272             switch ( mode ) {
273                 case regdef:
274                 case bytedispdef:
275                 case worddispdef:
276                 case longdispdef:
277                 case bytereldef:
278                 case wordreldef:
279                 case longreldef:
280                         /*
281                          *      indirect call: call through pointer
282                          *      either  *d(r)   as a parameter or local
283                          *              (r)     as a return value
284                          *              *f      as a global pointer
285                          *      [are there others that we miss?,
286                          *       e.g. arrays of pointers to functions???]
287                          */
288                     addarc( parentp , &indirectchild , (long) 0 );
289                     length += operandlength( instructp + length );
290                     continue;
291                 case byterel:
292                 case wordrel:
293                 case longrel:
294                         /*
295                          *      regular pc relative addressing
296                          *      check that this is the address of
297                          *      a function.
298                          */
299                     destpc = reladdr( instructp+length )
300                                 - (unsigned long) textspace;
301                     if ( destpc >= s_lowpc && destpc <= s_highpc ) {
302                         childp = nllookup( destpc );
303 #                       ifdef DEBUG
304                             if ( debug & CALLDEBUG ) {
305                                 printf( "[findcall]\tdestpc 0x%x" , destpc );
306                                 printf( " childp->name %s" , childp -> name );
307                                 printf( " childp->value 0x%x\n" ,
308                                         childp -> value );
309                             }
310 #                       endif DEBUG
311                         if ( childp -> value == destpc ) {
312                                 /*
313                                  *      a hit
314                                  */
315                             addarc( parentp , childp , (long) 0 );
316                             length += operandlength( instructp + length );
317                             continue;
318                         }
319                         goto botched;
320                     }
321                         /*
322                          *      else:
323                          *      it looked like a callf,
324                          *      but it wasn't to anywhere.
325                          */
326                     goto botched;
327                 default:
328                 botched:
329                         /*
330                          *      something funny going on.
331                          */
332 #                   ifdef DEBUG
333                         if ( debug & CALLDEBUG ) {
334                             printf( "[findcall]\tbut it's a botch\n" );
335                         }
336 #                   endif DEBUG
337                     length = 1;
338                     continue;
339             }
340         }
341     }
342 }