xref: /onnv-gate/usr/src/cmd/sgs/gprof/common/lookup.c (revision 211:37c8180b1ace)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*211Smike_s 
230Sstevel@tonic-gate /*
24*211Smike_s  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*211Smike_s  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include "gprof.h"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * look up an address in a sorted-by-address namelist
340Sstevel@tonic-gate  * this deals with misses by mapping them to the next lower
350Sstevel@tonic-gate  * entry point.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate static   int	searchmsg = 0; /* Emit the diagnostic only once */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate nltype *
nllookup(mod_info_t * module,pctype address,pctype * nxtsym)400Sstevel@tonic-gate nllookup(mod_info_t *module, pctype address, pctype *nxtsym)
410Sstevel@tonic-gate {
420Sstevel@tonic-gate 	size_t	low = 0, middle, high = module->nname - 1;
430Sstevel@tonic-gate 	pctype	keyval;
440Sstevel@tonic-gate 	nltype	*mnl = module->nl;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	/*
470Sstevel@tonic-gate 	 * If this is the program executable in which we are looking up
480Sstevel@tonic-gate 	 * a symbol, then the actual load address will be the same as the
490Sstevel@tonic-gate 	 * address specified in the ELF file. For shared objects, the
500Sstevel@tonic-gate 	 * load address may differ from what is specified in the file. In
510Sstevel@tonic-gate 	 * this case, we may need to look for a different value altogether.
520Sstevel@tonic-gate 	 */
530Sstevel@tonic-gate 	keyval = module->txt_origin + (address - module->load_base);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	if (keyval < mnl[low].value) {
560Sstevel@tonic-gate 		if (nxtsym) {
570Sstevel@tonic-gate 			*nxtsym = module->load_base +
580Sstevel@tonic-gate 					(mnl[low].value - module->txt_origin);
590Sstevel@tonic-gate 		}
600Sstevel@tonic-gate 		return (NULL);
610Sstevel@tonic-gate 	}
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	if (keyval >= mnl[high].value) {
640Sstevel@tonic-gate 		if (nxtsym)
650Sstevel@tonic-gate 			*nxtsym = module->load_end;
660Sstevel@tonic-gate 		return (&mnl[high]);
670Sstevel@tonic-gate 	}
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	while (low != high) {
700Sstevel@tonic-gate 		middle = (high + low) >> 1;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 		if (mnl[middle].value <= keyval &&
730Sstevel@tonic-gate 					    mnl[middle + 1].value > keyval) {
740Sstevel@tonic-gate 			if (nxtsym) {
750Sstevel@tonic-gate 				*nxtsym = module->load_base +
760Sstevel@tonic-gate 						    (mnl[middle + 1].value -
770Sstevel@tonic-gate 						    module->txt_origin);
780Sstevel@tonic-gate 			}
790Sstevel@tonic-gate 			return (&mnl[middle]);
800Sstevel@tonic-gate 		}
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 		if (mnl[middle].value > keyval) {
830Sstevel@tonic-gate 			high = middle;
840Sstevel@tonic-gate 		} else {
850Sstevel@tonic-gate 			low = middle + 1;
860Sstevel@tonic-gate 		}
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if (searchmsg++ == 0)
90*211Smike_s 		(void) fprintf(stderr, "[nllookup] binary search fails???\n");
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	/* must never reach here! */
930Sstevel@tonic-gate 	return (0);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate arctype *
arclookup(nltype * parentp,nltype * childp)970Sstevel@tonic-gate arclookup(nltype *parentp, nltype *childp)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	arctype	*arcp;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if (parentp == 0 || childp == 0) {
102*211Smike_s 		(void) fprintf(stderr,
103*211Smike_s 		    "[arclookup] parentp == 0 || childp == 0\n");
1040Sstevel@tonic-gate 		return (0);
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate #ifdef DEBUG
1070Sstevel@tonic-gate 	if (debug & LOOKUPDEBUG) {
108*211Smike_s 		(void) printf("[arclookup] parent %s child %s\n",
1090Sstevel@tonic-gate 		    parentp->name, childp->name);
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate #endif /* DEBUG */
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	for (arcp = parentp->children; arcp; arcp = arcp->arc_childlist) {
1140Sstevel@tonic-gate #ifdef DEBUG
1150Sstevel@tonic-gate 		if (debug & LOOKUPDEBUG) {
116*211Smike_s 			(void) printf(
117*211Smike_s 			    "[arclookup]\t arc_parent %s arc_child %s\n",
1180Sstevel@tonic-gate 			    arcp->arc_parentp->name,
1190Sstevel@tonic-gate 			    arcp->arc_childp->name);
1200Sstevel@tonic-gate 		}
1210Sstevel@tonic-gate #endif /* DEBUG */
1220Sstevel@tonic-gate 		if (arcp->arc_childp == childp) {
1230Sstevel@tonic-gate 			return (arcp);
1240Sstevel@tonic-gate 		}
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 	return (0);
1270Sstevel@tonic-gate }
128