xref: /illumos-gate/usr/src/cmd/sgs/gprof/common/lookup.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*92ed1782Smike_s 
237c478bd9Sstevel@tonic-gate /*
24*92ed1782Smike_s  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*92ed1782Smike_s  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include "gprof.h"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * look up an address in a sorted-by-address namelist
327c478bd9Sstevel@tonic-gate  * this deals with misses by mapping them to the next lower
337c478bd9Sstevel@tonic-gate  * entry point.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate static   int	searchmsg = 0; /* Emit the diagnostic only once */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate nltype *
nllookup(mod_info_t * module,pctype address,pctype * nxtsym)387c478bd9Sstevel@tonic-gate nllookup(mod_info_t *module, pctype address, pctype *nxtsym)
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate 	size_t	low = 0, middle, high = module->nname - 1;
417c478bd9Sstevel@tonic-gate 	pctype	keyval;
427c478bd9Sstevel@tonic-gate 	nltype	*mnl = module->nl;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 	/*
457c478bd9Sstevel@tonic-gate 	 * If this is the program executable in which we are looking up
467c478bd9Sstevel@tonic-gate 	 * a symbol, then the actual load address will be the same as the
477c478bd9Sstevel@tonic-gate 	 * address specified in the ELF file. For shared objects, the
487c478bd9Sstevel@tonic-gate 	 * load address may differ from what is specified in the file. In
497c478bd9Sstevel@tonic-gate 	 * this case, we may need to look for a different value altogether.
507c478bd9Sstevel@tonic-gate 	 */
517c478bd9Sstevel@tonic-gate 	keyval = module->txt_origin + (address - module->load_base);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	if (keyval < mnl[low].value) {
547c478bd9Sstevel@tonic-gate 		if (nxtsym) {
557c478bd9Sstevel@tonic-gate 			*nxtsym = module->load_base +
567c478bd9Sstevel@tonic-gate 					(mnl[low].value - module->txt_origin);
577c478bd9Sstevel@tonic-gate 		}
587c478bd9Sstevel@tonic-gate 		return (NULL);
597c478bd9Sstevel@tonic-gate 	}
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	if (keyval >= mnl[high].value) {
627c478bd9Sstevel@tonic-gate 		if (nxtsym)
637c478bd9Sstevel@tonic-gate 			*nxtsym = module->load_end;
647c478bd9Sstevel@tonic-gate 		return (&mnl[high]);
657c478bd9Sstevel@tonic-gate 	}
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	while (low != high) {
687c478bd9Sstevel@tonic-gate 		middle = (high + low) >> 1;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 		if (mnl[middle].value <= keyval &&
717c478bd9Sstevel@tonic-gate 					    mnl[middle + 1].value > keyval) {
727c478bd9Sstevel@tonic-gate 			if (nxtsym) {
737c478bd9Sstevel@tonic-gate 				*nxtsym = module->load_base +
747c478bd9Sstevel@tonic-gate 						    (mnl[middle + 1].value -
757c478bd9Sstevel@tonic-gate 						    module->txt_origin);
767c478bd9Sstevel@tonic-gate 			}
777c478bd9Sstevel@tonic-gate 			return (&mnl[middle]);
787c478bd9Sstevel@tonic-gate 		}
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 		if (mnl[middle].value > keyval) {
817c478bd9Sstevel@tonic-gate 			high = middle;
827c478bd9Sstevel@tonic-gate 		} else {
837c478bd9Sstevel@tonic-gate 			low = middle + 1;
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if (searchmsg++ == 0)
88*92ed1782Smike_s 		(void) fprintf(stderr, "[nllookup] binary search fails???\n");
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/* must never reach here! */
917c478bd9Sstevel@tonic-gate 	return (0);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate arctype *
arclookup(nltype * parentp,nltype * childp)957c478bd9Sstevel@tonic-gate arclookup(nltype *parentp, nltype *childp)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	arctype	*arcp;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (parentp == 0 || childp == 0) {
100*92ed1782Smike_s 		(void) fprintf(stderr,
101*92ed1782Smike_s 		    "[arclookup] parentp == 0 || childp == 0\n");
1027c478bd9Sstevel@tonic-gate 		return (0);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate #ifdef DEBUG
1057c478bd9Sstevel@tonic-gate 	if (debug & LOOKUPDEBUG) {
106*92ed1782Smike_s 		(void) printf("[arclookup] parent %s child %s\n",
1077c478bd9Sstevel@tonic-gate 		    parentp->name, childp->name);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	for (arcp = parentp->children; arcp; arcp = arcp->arc_childlist) {
1127c478bd9Sstevel@tonic-gate #ifdef DEBUG
1137c478bd9Sstevel@tonic-gate 		if (debug & LOOKUPDEBUG) {
114*92ed1782Smike_s 			(void) printf(
115*92ed1782Smike_s 			    "[arclookup]\t arc_parent %s arc_child %s\n",
1167c478bd9Sstevel@tonic-gate 			    arcp->arc_parentp->name,
1177c478bd9Sstevel@tonic-gate 			    arcp->arc_childp->name);
1187c478bd9Sstevel@tonic-gate 		}
1197c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1207c478bd9Sstevel@tonic-gate 		if (arcp->arc_childp == childp) {
1217c478bd9Sstevel@tonic-gate 			return (arcp);
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 	return (0);
1257c478bd9Sstevel@tonic-gate }
126