xref: /illumos-gate/usr/src/cmd/sgs/gprof/common/calls.c (revision 2a8bcb4e)
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 #include	"gprof.h"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  *	a namelist entry to be the child of indirect calls
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate nltype	indirectchild = {
33*7c478bd9Sstevel@tonic-gate 	"(*)",				/* the name */
34*7c478bd9Sstevel@tonic-gate 	&modules,			/* module [-c only for prog txtspace] */
35*7c478bd9Sstevel@tonic-gate 	(pctype)0,			/* the pc entry point */
36*7c478bd9Sstevel@tonic-gate 	(pctype)0,			/* aligned entry point */
37*7c478bd9Sstevel@tonic-gate 	(unsigned long)0,		/* function size */
38*7c478bd9Sstevel@tonic-gate 	(unsigned char)0,		/* symbol information */
39*7c478bd9Sstevel@tonic-gate 	(size_t)0,			/* ticks in this routine */
40*7c478bd9Sstevel@tonic-gate 	(double)0.0,			/* ticks in this routine (as double) */
41*7c478bd9Sstevel@tonic-gate 	(double)0.0,			/* cumulative ticks in children */
42*7c478bd9Sstevel@tonic-gate 	(long)0,			/* how many times called */
43*7c478bd9Sstevel@tonic-gate 	(long)0,			/* how many calls to self */
44*7c478bd9Sstevel@tonic-gate 	(double)1.0,			/* propagation fraction */
45*7c478bd9Sstevel@tonic-gate 	(double)0.0,			/* self propagation time */
46*7c478bd9Sstevel@tonic-gate 	(double)0.0,			/* child propagation time */
47*7c478bd9Sstevel@tonic-gate 	(bool)0,			/* print flag */
48*7c478bd9Sstevel@tonic-gate 	(int)0,				/* index in the graph list */
49*7c478bd9Sstevel@tonic-gate 	(int)0,				/* graph call chain top-sort order */
50*7c478bd9Sstevel@tonic-gate 	(int)0,				/* internal number of cycle on */
51*7c478bd9Sstevel@tonic-gate 	(struct nl *)&indirectchild,	/* pointer to head of cycle */
52*7c478bd9Sstevel@tonic-gate 	(struct nl *)0,			/* pointer to next member of cycle */
53*7c478bd9Sstevel@tonic-gate 	(arctype *)0,			/* list of caller arcs */
54*7c478bd9Sstevel@tonic-gate 	(arctype *)0, 			/* list of callee arcs */
55*7c478bd9Sstevel@tonic-gate 	(unsigned long)0		/* number of callers */
56*7c478bd9Sstevel@tonic-gate };
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate void
findcalls(nltype * parentp,pctype p_lowpc,pctype p_highpc)59*7c478bd9Sstevel@tonic-gate findcalls(nltype *parentp, pctype p_lowpc, pctype p_highpc)
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate 	unsigned long 	instructp;
62*7c478bd9Sstevel@tonic-gate 	sztype		length;
63*7c478bd9Sstevel@tonic-gate 	nltype		*childp;
64*7c478bd9Sstevel@tonic-gate 	pctype		destpc;
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	if (textspace == 0) {
67*7c478bd9Sstevel@tonic-gate 		return;
68*7c478bd9Sstevel@tonic-gate 	}
69*7c478bd9Sstevel@tonic-gate 	if (p_lowpc > s_highpc)
70*7c478bd9Sstevel@tonic-gate 		return;
71*7c478bd9Sstevel@tonic-gate 	if (p_highpc < s_lowpc)
72*7c478bd9Sstevel@tonic-gate 		return;
73*7c478bd9Sstevel@tonic-gate 	if (p_lowpc < s_lowpc)
74*7c478bd9Sstevel@tonic-gate 		p_lowpc = s_lowpc;
75*7c478bd9Sstevel@tonic-gate 	if (p_highpc > s_highpc)
76*7c478bd9Sstevel@tonic-gate 		p_highpc = s_highpc;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
79*7c478bd9Sstevel@tonic-gate 	if (debug & CALLSDEBUG) {
80*7c478bd9Sstevel@tonic-gate 	    printf("[findcalls] %s: 0x%llx to 0x%llx\n",
81*7c478bd9Sstevel@tonic-gate 		    parentp->name, p_lowpc, p_highpc);
82*7c478bd9Sstevel@tonic-gate 	}
83*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	length = 4;
86*7c478bd9Sstevel@tonic-gate 	for (instructp = (uintptr_t)textspace + p_lowpc -  TORIGIN;
87*7c478bd9Sstevel@tonic-gate 	    instructp < (uintptr_t)textspace + p_highpc - TORIGIN;
88*7c478bd9Sstevel@tonic-gate 	    instructp += length) {
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 		switch (OP(instructp)) {
91*7c478bd9Sstevel@tonic-gate 		case CALL:
92*7c478bd9Sstevel@tonic-gate 			/*
93*7c478bd9Sstevel@tonic-gate 			 *	May be a call, better check it out.
94*7c478bd9Sstevel@tonic-gate 			 */
95*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
96*7c478bd9Sstevel@tonic-gate 			if (debug & CALLSDEBUG) {
97*7c478bd9Sstevel@tonic-gate 				printf("[findcalls]\t0x%x:call\n",
98*7c478bd9Sstevel@tonic-gate 				    PC_VAL(instructp));
99*7c478bd9Sstevel@tonic-gate 			}
100*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
101*7c478bd9Sstevel@tonic-gate 			destpc = (DISP30(instructp) << 2) + PC_VAL(instructp);
102*7c478bd9Sstevel@tonic-gate 			break;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 		case FMT3_0x10:
105*7c478bd9Sstevel@tonic-gate 			if (OP3(instructp) != JMPL)
106*7c478bd9Sstevel@tonic-gate 				continue;
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
109*7c478bd9Sstevel@tonic-gate 			if (debug & CALLSDEBUG)
110*7c478bd9Sstevel@tonic-gate 				printf("[findcalls]\t0x%x:jmpl",
111*7c478bd9Sstevel@tonic-gate 				    PC_VAL(instructp));
112*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
113*7c478bd9Sstevel@tonic-gate 			if (RD(instructp) == R_G0) {
114*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
115*7c478bd9Sstevel@tonic-gate 				if (debug & CALLSDEBUG) {
116*7c478bd9Sstevel@tonic-gate 					switch (RS1(instructp)) {
117*7c478bd9Sstevel@tonic-gate 					case R_O7:
118*7c478bd9Sstevel@tonic-gate 						printf("\tprobably a RETL\n");
119*7c478bd9Sstevel@tonic-gate 						break;
120*7c478bd9Sstevel@tonic-gate 					case R_I7:
121*7c478bd9Sstevel@tonic-gate 						printf("\tprobably a RET\n");
122*7c478bd9Sstevel@tonic-gate 						break;
123*7c478bd9Sstevel@tonic-gate 					default:
124*7c478bd9Sstevel@tonic-gate 						printf(", but not a call: "
125*7c478bd9Sstevel@tonic-gate 						    "linked to g0\n");
126*7c478bd9Sstevel@tonic-gate 					}
127*7c478bd9Sstevel@tonic-gate 				}
128*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
129*7c478bd9Sstevel@tonic-gate 				continue;
130*7c478bd9Sstevel@tonic-gate 			}
131*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
132*7c478bd9Sstevel@tonic-gate 			if (debug & CALLSDEBUG) {
133*7c478bd9Sstevel@tonic-gate 				printf("\toperands are DST = R%d,\tSRC = R%d",
134*7c478bd9Sstevel@tonic-gate 				    RD(instructp), RS1(instructp));
135*7c478bd9Sstevel@tonic-gate 			}
136*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
137*7c478bd9Sstevel@tonic-gate 			if (IMMED(instructp)) {
138*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
139*7c478bd9Sstevel@tonic-gate 				if (debug & CALLSDEBUG) {
140*7c478bd9Sstevel@tonic-gate 					if (SIMM13(instructp) < 0) {
141*7c478bd9Sstevel@tonic-gate 						printf(" - 0x%x\n",
142*7c478bd9Sstevel@tonic-gate 						    -(SIMM13(instructp)));
143*7c478bd9Sstevel@tonic-gate 					} else {
144*7c478bd9Sstevel@tonic-gate 						printf(" + 0x%x\n",
145*7c478bd9Sstevel@tonic-gate 						    SIMM13(instructp));
146*7c478bd9Sstevel@tonic-gate 					}
147*7c478bd9Sstevel@tonic-gate 				}
148*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
149*7c478bd9Sstevel@tonic-gate 				switch (RS1(instructp)) {
150*7c478bd9Sstevel@tonic-gate 				case R_G0:
151*7c478bd9Sstevel@tonic-gate 					/*
152*7c478bd9Sstevel@tonic-gate 					 * absolute address, simm 13
153*7c478bd9Sstevel@tonic-gate 					 */
154*7c478bd9Sstevel@tonic-gate 					destpc = SIMM13(instructp);
155*7c478bd9Sstevel@tonic-gate 					break;
156*7c478bd9Sstevel@tonic-gate 				default:
157*7c478bd9Sstevel@tonic-gate 					/*
158*7c478bd9Sstevel@tonic-gate 					 * indirect call
159*7c478bd9Sstevel@tonic-gate 					 */
160*7c478bd9Sstevel@tonic-gate 					addarc(parentp, &indirectchild, 0);
161*7c478bd9Sstevel@tonic-gate 					continue;
162*7c478bd9Sstevel@tonic-gate 				}
163*7c478bd9Sstevel@tonic-gate 			} else {
164*7c478bd9Sstevel@tonic-gate 				/*
165*7c478bd9Sstevel@tonic-gate 				 * two register sources, all cases are indirect
166*7c478bd9Sstevel@tonic-gate 				 */
167*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
168*7c478bd9Sstevel@tonic-gate 				if (debug & CALLSDEBUG) {
169*7c478bd9Sstevel@tonic-gate 					printf(" + R%d\n", RS2(instructp));
170*7c478bd9Sstevel@tonic-gate 				}
171*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
172*7c478bd9Sstevel@tonic-gate 				addarc(parentp, &indirectchild, 0);
173*7c478bd9Sstevel@tonic-gate 				continue;
174*7c478bd9Sstevel@tonic-gate 			}
175*7c478bd9Sstevel@tonic-gate 			break;
176*7c478bd9Sstevel@tonic-gate 		default:
177*7c478bd9Sstevel@tonic-gate 			continue;
178*7c478bd9Sstevel@tonic-gate 		}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 		/*
181*7c478bd9Sstevel@tonic-gate 		 *	Check that the destination is the address of
182*7c478bd9Sstevel@tonic-gate 		 *	a function; this allows us to differentiate
183*7c478bd9Sstevel@tonic-gate 		 *	real calls from someone trying to get the PC,
184*7c478bd9Sstevel@tonic-gate 		 *	e.g. position independent switches.
185*7c478bd9Sstevel@tonic-gate 		 */
186*7c478bd9Sstevel@tonic-gate 		if (destpc >= s_lowpc && destpc <= s_highpc) {
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 			childp = nllookup(&modules, destpc, NULL);
189*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
190*7c478bd9Sstevel@tonic-gate 			if (debug & CALLSDEBUG) {
191*7c478bd9Sstevel@tonic-gate 				printf("[findcalls]\tdestpc 0x%llx", destpc);
192*7c478bd9Sstevel@tonic-gate 				printf(" childp->name %s", childp->name);
193*7c478bd9Sstevel@tonic-gate 				printf(" childp->value 0x%llx\n",
194*7c478bd9Sstevel@tonic-gate 				    childp->value);
195*7c478bd9Sstevel@tonic-gate 			}
196*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
197*7c478bd9Sstevel@tonic-gate 			if (childp->value == destpc) {
198*7c478bd9Sstevel@tonic-gate 				/*
199*7c478bd9Sstevel@tonic-gate 				 *	a hit
200*7c478bd9Sstevel@tonic-gate 				 */
201*7c478bd9Sstevel@tonic-gate 				addarc(parentp, childp, 0);
202*7c478bd9Sstevel@tonic-gate 				continue;
203*7c478bd9Sstevel@tonic-gate 			}
204*7c478bd9Sstevel@tonic-gate 		}
205*7c478bd9Sstevel@tonic-gate 		/*
206*7c478bd9Sstevel@tonic-gate 		 *	else:
207*7c478bd9Sstevel@tonic-gate 		 *	it looked like a call,
208*7c478bd9Sstevel@tonic-gate 		 *	but it wasn't to anywhere.
209*7c478bd9Sstevel@tonic-gate 		 */
210*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
211*7c478bd9Sstevel@tonic-gate 		if (debug & CALLSDEBUG) {
212*7c478bd9Sstevel@tonic-gate 			printf("[findcalls]\tbut it's a switch or a botch\n");
213*7c478bd9Sstevel@tonic-gate 		}
214*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
215*7c478bd9Sstevel@tonic-gate 		continue;
216*7c478bd9Sstevel@tonic-gate 	}
217*7c478bd9Sstevel@tonic-gate }
218