1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gate #include "gprof.h"
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate * a namelist entry to be the child of indirect calls
33*7c478bd9Sstevel@tonic-gate */
34*7c478bd9Sstevel@tonic-gate nltype indirectchild = {
35*7c478bd9Sstevel@tonic-gate "(*)", /* the name */
36*7c478bd9Sstevel@tonic-gate &modules, /* module [-c only for prog txtspace] */
37*7c478bd9Sstevel@tonic-gate (pctype)0, /* the pc entry point */
38*7c478bd9Sstevel@tonic-gate (pctype)0, /* aligned entry point */
39*7c478bd9Sstevel@tonic-gate (unsigned long)0, /* function size */
40*7c478bd9Sstevel@tonic-gate (unsigned char)0, /* symbol information */
41*7c478bd9Sstevel@tonic-gate (size_t)0, /* ticks in this routine */
42*7c478bd9Sstevel@tonic-gate (double)0.0, /* ticks in this routine (as double) */
43*7c478bd9Sstevel@tonic-gate (double)0.0, /* cumulative ticks in children */
44*7c478bd9Sstevel@tonic-gate (long)0, /* how many times called */
45*7c478bd9Sstevel@tonic-gate (long)0, /* how many calls to self */
46*7c478bd9Sstevel@tonic-gate (double)1.0, /* propagation fraction */
47*7c478bd9Sstevel@tonic-gate (double)0.0, /* self propagation time */
48*7c478bd9Sstevel@tonic-gate (double)0.0, /* child propagation time */
49*7c478bd9Sstevel@tonic-gate (bool)0, /* print flag */
50*7c478bd9Sstevel@tonic-gate (int)0, /* index in the graph list */
51*7c478bd9Sstevel@tonic-gate (int)0, /* graph call chain top-sort order */
52*7c478bd9Sstevel@tonic-gate (int)0, /* internal number of cycle on */
53*7c478bd9Sstevel@tonic-gate (struct nl *)&indirectchild, /* pointer to head of cycle */
54*7c478bd9Sstevel@tonic-gate (struct nl *)0, /* pointer to next member of cycle */
55*7c478bd9Sstevel@tonic-gate (arctype *)0, /* list of caller arcs */
56*7c478bd9Sstevel@tonic-gate (arctype *)0, /* list of callee arcs */
57*7c478bd9Sstevel@tonic-gate (unsigned long)0 /* number of callers */
58*7c478bd9Sstevel@tonic-gate };
59*7c478bd9Sstevel@tonic-gate
60*7c478bd9Sstevel@tonic-gate void
findcalls(nltype * parentp,pctype p_lowpc,pctype p_highpc)61*7c478bd9Sstevel@tonic-gate findcalls(nltype *parentp, pctype p_lowpc, pctype p_highpc)
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate unsigned long instructp;
64*7c478bd9Sstevel@tonic-gate sztype length;
65*7c478bd9Sstevel@tonic-gate nltype *childp;
66*7c478bd9Sstevel@tonic-gate pctype destpc;
67*7c478bd9Sstevel@tonic-gate
68*7c478bd9Sstevel@tonic-gate if (textspace == 0) {
69*7c478bd9Sstevel@tonic-gate return;
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate if (p_lowpc > s_highpc)
72*7c478bd9Sstevel@tonic-gate return;
73*7c478bd9Sstevel@tonic-gate if (p_highpc < s_lowpc)
74*7c478bd9Sstevel@tonic-gate return;
75*7c478bd9Sstevel@tonic-gate if (p_lowpc < s_lowpc)
76*7c478bd9Sstevel@tonic-gate p_lowpc = s_lowpc;
77*7c478bd9Sstevel@tonic-gate if (p_highpc > s_highpc)
78*7c478bd9Sstevel@tonic-gate p_highpc = s_highpc;
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
81*7c478bd9Sstevel@tonic-gate if (debug & CALLSDEBUG) {
82*7c478bd9Sstevel@tonic-gate printf("[findcalls] %s: 0x%llx to 0x%llx\n",
83*7c478bd9Sstevel@tonic-gate parentp->name, p_lowpc, p_highpc);
84*7c478bd9Sstevel@tonic-gate }
85*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
86*7c478bd9Sstevel@tonic-gate
87*7c478bd9Sstevel@tonic-gate length = 4;
88*7c478bd9Sstevel@tonic-gate for (instructp = (uintptr_t)textspace + p_lowpc - TORIGIN;
89*7c478bd9Sstevel@tonic-gate instructp < (uintptr_t)textspace + p_highpc - TORIGIN;
90*7c478bd9Sstevel@tonic-gate instructp += length) {
91*7c478bd9Sstevel@tonic-gate
92*7c478bd9Sstevel@tonic-gate switch (OP(instructp)) {
93*7c478bd9Sstevel@tonic-gate case CALL:
94*7c478bd9Sstevel@tonic-gate /*
95*7c478bd9Sstevel@tonic-gate * May be a call, better check it out.
96*7c478bd9Sstevel@tonic-gate */
97*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
98*7c478bd9Sstevel@tonic-gate if (debug & CALLSDEBUG) {
99*7c478bd9Sstevel@tonic-gate printf("[findcalls]\t0x%x:call\n",
100*7c478bd9Sstevel@tonic-gate PC_VAL(instructp));
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
103*7c478bd9Sstevel@tonic-gate destpc = (DISP30(instructp) << 2) + PC_VAL(instructp);
104*7c478bd9Sstevel@tonic-gate break;
105*7c478bd9Sstevel@tonic-gate
106*7c478bd9Sstevel@tonic-gate case FMT3_0x10:
107*7c478bd9Sstevel@tonic-gate if (OP3(instructp) != JMPL)
108*7c478bd9Sstevel@tonic-gate continue;
109*7c478bd9Sstevel@tonic-gate
110*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
111*7c478bd9Sstevel@tonic-gate if (debug & CALLSDEBUG)
112*7c478bd9Sstevel@tonic-gate printf("[findcalls]\t0x%x:jmpl",
113*7c478bd9Sstevel@tonic-gate PC_VAL(instructp));
114*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
115*7c478bd9Sstevel@tonic-gate if (RD(instructp) == R_G0) {
116*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
117*7c478bd9Sstevel@tonic-gate if (debug & CALLSDEBUG) {
118*7c478bd9Sstevel@tonic-gate switch (RS1(instructp)) {
119*7c478bd9Sstevel@tonic-gate case R_O7:
120*7c478bd9Sstevel@tonic-gate printf("\tprobably a RETL\n");
121*7c478bd9Sstevel@tonic-gate break;
122*7c478bd9Sstevel@tonic-gate case R_I7:
123*7c478bd9Sstevel@tonic-gate printf("\tprobably a RET\n");
124*7c478bd9Sstevel@tonic-gate break;
125*7c478bd9Sstevel@tonic-gate default:
126*7c478bd9Sstevel@tonic-gate printf(", but not a call: "
127*7c478bd9Sstevel@tonic-gate "linked to g0\n");
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate }
130*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
131*7c478bd9Sstevel@tonic-gate continue;
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
134*7c478bd9Sstevel@tonic-gate if (debug & CALLSDEBUG) {
135*7c478bd9Sstevel@tonic-gate printf("\toperands are DST = R%d,\tSRC = R%d",
136*7c478bd9Sstevel@tonic-gate RD(instructp), RS1(instructp));
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
139*7c478bd9Sstevel@tonic-gate if (IMMED(instructp)) {
140*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
141*7c478bd9Sstevel@tonic-gate if (debug & CALLSDEBUG) {
142*7c478bd9Sstevel@tonic-gate if (SIMM13(instructp) < 0) {
143*7c478bd9Sstevel@tonic-gate printf(" - 0x%x\n",
144*7c478bd9Sstevel@tonic-gate -(SIMM13(instructp)));
145*7c478bd9Sstevel@tonic-gate } else {
146*7c478bd9Sstevel@tonic-gate printf(" + 0x%x\n",
147*7c478bd9Sstevel@tonic-gate SIMM13(instructp));
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate }
150*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
151*7c478bd9Sstevel@tonic-gate switch (RS1(instructp)) {
152*7c478bd9Sstevel@tonic-gate case R_G0:
153*7c478bd9Sstevel@tonic-gate /*
154*7c478bd9Sstevel@tonic-gate * absolute address, simm 13
155*7c478bd9Sstevel@tonic-gate */
156*7c478bd9Sstevel@tonic-gate destpc = SIMM13(instructp);
157*7c478bd9Sstevel@tonic-gate break;
158*7c478bd9Sstevel@tonic-gate default:
159*7c478bd9Sstevel@tonic-gate /*
160*7c478bd9Sstevel@tonic-gate * indirect call
161*7c478bd9Sstevel@tonic-gate */
162*7c478bd9Sstevel@tonic-gate addarc(parentp, &indirectchild, 0);
163*7c478bd9Sstevel@tonic-gate continue;
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate } else {
166*7c478bd9Sstevel@tonic-gate /*
167*7c478bd9Sstevel@tonic-gate * two register sources, all cases are indirect
168*7c478bd9Sstevel@tonic-gate */
169*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
170*7c478bd9Sstevel@tonic-gate if (debug & CALLSDEBUG) {
171*7c478bd9Sstevel@tonic-gate printf(" + R%d\n", RS2(instructp));
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
174*7c478bd9Sstevel@tonic-gate addarc(parentp, &indirectchild, 0);
175*7c478bd9Sstevel@tonic-gate continue;
176*7c478bd9Sstevel@tonic-gate }
177*7c478bd9Sstevel@tonic-gate break;
178*7c478bd9Sstevel@tonic-gate default:
179*7c478bd9Sstevel@tonic-gate continue;
180*7c478bd9Sstevel@tonic-gate }
181*7c478bd9Sstevel@tonic-gate
182*7c478bd9Sstevel@tonic-gate /*
183*7c478bd9Sstevel@tonic-gate * Check that the destination is the address of
184*7c478bd9Sstevel@tonic-gate * a function; this allows us to differentiate
185*7c478bd9Sstevel@tonic-gate * real calls from someone trying to get the PC,
186*7c478bd9Sstevel@tonic-gate * e.g. position independent switches.
187*7c478bd9Sstevel@tonic-gate */
188*7c478bd9Sstevel@tonic-gate if (destpc >= s_lowpc && destpc <= s_highpc) {
189*7c478bd9Sstevel@tonic-gate
190*7c478bd9Sstevel@tonic-gate childp = nllookup(&modules, destpc, NULL);
191*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
192*7c478bd9Sstevel@tonic-gate if (debug & CALLSDEBUG) {
193*7c478bd9Sstevel@tonic-gate printf("[findcalls]\tdestpc 0x%llx", destpc);
194*7c478bd9Sstevel@tonic-gate printf(" childp->name %s", childp->name);
195*7c478bd9Sstevel@tonic-gate printf(" childp->value 0x%llx\n",
196*7c478bd9Sstevel@tonic-gate childp->value);
197*7c478bd9Sstevel@tonic-gate }
198*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
199*7c478bd9Sstevel@tonic-gate if (childp->value == destpc) {
200*7c478bd9Sstevel@tonic-gate /*
201*7c478bd9Sstevel@tonic-gate * a hit
202*7c478bd9Sstevel@tonic-gate */
203*7c478bd9Sstevel@tonic-gate addarc(parentp, childp, 0);
204*7c478bd9Sstevel@tonic-gate continue;
205*7c478bd9Sstevel@tonic-gate }
206*7c478bd9Sstevel@tonic-gate }
207*7c478bd9Sstevel@tonic-gate /*
208*7c478bd9Sstevel@tonic-gate * else:
209*7c478bd9Sstevel@tonic-gate * it looked like a call,
210*7c478bd9Sstevel@tonic-gate * but it wasn't to anywhere.
211*7c478bd9Sstevel@tonic-gate */
212*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
213*7c478bd9Sstevel@tonic-gate if (debug & CALLSDEBUG) {
214*7c478bd9Sstevel@tonic-gate printf("[findcalls]\tbut it's a switch or a botch\n");
215*7c478bd9Sstevel@tonic-gate }
216*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
217*7c478bd9Sstevel@tonic-gate continue;
218*7c478bd9Sstevel@tonic-gate }
219*7c478bd9Sstevel@tonic-gate }
220