xref: /onnv-gate/usr/src/cmd/sgs/gprof/common/dfn.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 <stdio.h>
310Sstevel@tonic-gate #include "gprof.h"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #define	DFN_DEPTH	100
340Sstevel@tonic-gate 
350Sstevel@tonic-gate struct dfnstruct {
360Sstevel@tonic-gate 	nltype	*nlentryp;
370Sstevel@tonic-gate 	int	cycletop;
380Sstevel@tonic-gate };
390Sstevel@tonic-gate 
400Sstevel@tonic-gate typedef struct dfnstruct	dfntype;
410Sstevel@tonic-gate 
420Sstevel@tonic-gate dfntype	*dfn_stack = NULL;
430Sstevel@tonic-gate int	dfn_depth = 0;
440Sstevel@tonic-gate int	dfn_sz = 0;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate int	dfn_counter = DFN_NAN;
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  *	given this parent, depth first number its children.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate void
dfn(nltype * parentp)520Sstevel@tonic-gate dfn(nltype *parentp)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	arctype	*arcp;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #ifdef DEBUG
570Sstevel@tonic-gate 	if (debug & DFNDEBUG) {
58*211Smike_s 		(void) printf("[dfn] dfn(");
590Sstevel@tonic-gate 		printname(parentp);
60*211Smike_s 		(void) printf(")\n");
610Sstevel@tonic-gate 	}
620Sstevel@tonic-gate #endif /* DEBUG */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	if (!dfn_stack) {
650Sstevel@tonic-gate 		dfn_sz = DFN_DEPTH;
660Sstevel@tonic-gate 		dfn_stack = (dfntype *) malloc(dfn_sz * sizeof (dfntype));
670Sstevel@tonic-gate 		if (!dfn_stack) {
68*211Smike_s 			(void) fprintf(stderr,
690Sstevel@tonic-gate 			    "fatal: can't malloc %d objects\n", dfn_sz);
700Sstevel@tonic-gate 			exit(1);
710Sstevel@tonic-gate 		}
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	/*
750Sstevel@tonic-gate 	 *	if we're already numbered, no need to look any furthur.
760Sstevel@tonic-gate 	 */
770Sstevel@tonic-gate 	if (dfn_numbered(parentp))
780Sstevel@tonic-gate 		return;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	/*
810Sstevel@tonic-gate 	 *	if we're already busy, must be a cycle
820Sstevel@tonic-gate 	 */
830Sstevel@tonic-gate 	if (dfn_busy(parentp)) {
840Sstevel@tonic-gate 		dfn_findcycle(parentp);
850Sstevel@tonic-gate 		return;
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	/*
890Sstevel@tonic-gate 	 *	visit yourself before your children
900Sstevel@tonic-gate 	 */
910Sstevel@tonic-gate 	dfn_pre_visit(parentp);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	/*
940Sstevel@tonic-gate 	 *	visit children
950Sstevel@tonic-gate 	 */
960Sstevel@tonic-gate 	for (arcp = parentp->children; arcp; arcp = arcp->arc_childlist)
970Sstevel@tonic-gate 		dfn(arcp->arc_childp);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	/*
1000Sstevel@tonic-gate 	 *	visit yourself after your children
1010Sstevel@tonic-gate 	 */
1020Sstevel@tonic-gate 	dfn_post_visit(parentp);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  *	push a parent onto the stack and mark it busy
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate void
dfn_pre_visit(nltype * parentp)1090Sstevel@tonic-gate dfn_pre_visit(nltype *parentp)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (!dfn_stack) {
1130Sstevel@tonic-gate 		dfn_sz = DFN_DEPTH;
1140Sstevel@tonic-gate 		dfn_stack = (dfntype *) malloc(dfn_sz * sizeof (dfntype));
1150Sstevel@tonic-gate 		if (!dfn_stack) {
116*211Smike_s 			(void) printf("fatal: can't malloc %d objects\n",
117*211Smike_s 			    dfn_sz);
1180Sstevel@tonic-gate 			exit(1);
1190Sstevel@tonic-gate 		}
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	dfn_depth += 1;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if (dfn_depth >= dfn_sz) {
1250Sstevel@tonic-gate 		dfn_sz += DFN_DEPTH;
1260Sstevel@tonic-gate 		dfn_stack = (dfntype *) realloc(dfn_stack,
1270Sstevel@tonic-gate 		    dfn_sz * sizeof (dfntype));
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 		if (!dfn_stack) {
130*211Smike_s 			(void) fprintf(stderr,
1310Sstevel@tonic-gate 			    "fatal: can't realloc %d objects\n", dfn_sz);
1320Sstevel@tonic-gate 			exit(1);
1330Sstevel@tonic-gate 		}
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	dfn_stack[dfn_depth].nlentryp = parentp;
1370Sstevel@tonic-gate 	dfn_stack[dfn_depth].cycletop = dfn_depth;
1380Sstevel@tonic-gate 	parentp->toporder = DFN_BUSY;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate #ifdef DEBUG
1410Sstevel@tonic-gate 	if (debug & DFNDEBUG) {
142*211Smike_s 		(void) printf("[dfn_pre_visit]\t\t%d:", dfn_depth);
1430Sstevel@tonic-gate 		printname(parentp);
144*211Smike_s 		(void) printf("\n");
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate #endif /* DEBUG */
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate  *	are we already numbered?
1510Sstevel@tonic-gate  */
1520Sstevel@tonic-gate bool
dfn_numbered(nltype * childp)1530Sstevel@tonic-gate dfn_numbered(nltype *childp)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate 	return (childp->toporder != DFN_NAN && childp->toporder != DFN_BUSY);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate  *	are we already busy?
1600Sstevel@tonic-gate  */
1610Sstevel@tonic-gate bool
dfn_busy(nltype * childp)1620Sstevel@tonic-gate dfn_busy(nltype *childp)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	if (childp->toporder == DFN_NAN)
1650Sstevel@tonic-gate 		return (FALSE);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	return (TRUE);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate void
dfn_findcycle(nltype * childp)1710Sstevel@tonic-gate dfn_findcycle(nltype *childp)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	int		cycletop;
1740Sstevel@tonic-gate 	nltype	*cycleheadp;
1750Sstevel@tonic-gate 	nltype	*tailp;
1760Sstevel@tonic-gate 	int		index;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	for (cycletop = dfn_depth; cycletop > 0; cycletop -= 1) {
1790Sstevel@tonic-gate 		cycleheadp = dfn_stack[cycletop].nlentryp;
1800Sstevel@tonic-gate 		if (childp == cycleheadp)
1810Sstevel@tonic-gate 			break;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 		if (childp->cyclehead != childp &&
1840Sstevel@tonic-gate 		    childp->cyclehead == cycleheadp)
1850Sstevel@tonic-gate 			break;
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (cycletop <= 0) {
1890Sstevel@tonic-gate 		/*
1900Sstevel@tonic-gate 		 * don't report non existent functions
1910Sstevel@tonic-gate 		 */
1920Sstevel@tonic-gate 		if (childp->value) {
193*211Smike_s 			(void) fprintf(stderr,
194*211Smike_s 			    "[dfn_findcycle] couldn't find head "
1950Sstevel@tonic-gate 			    "of cycle for %s\n", childp->name);
1960Sstevel@tonic-gate 			return;
1970Sstevel@tonic-gate 		}
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate #ifdef DEBUG
2010Sstevel@tonic-gate 	if (debug & DFNDEBUG) {
202*211Smike_s 		(void) printf("[dfn_findcycle] dfn_depth %d cycletop %d ",
2030Sstevel@tonic-gate 		    dfn_depth, cycletop);
2040Sstevel@tonic-gate 		printname(cycleheadp);
205*211Smike_s 		(void) printf("\n");
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate #endif /* DEBUG */
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	if (cycletop == dfn_depth) {
2100Sstevel@tonic-gate 		/*
2110Sstevel@tonic-gate 		 *	this is previous function, e.g. this calls itself
2120Sstevel@tonic-gate 		 *	sort of boring
2130Sstevel@tonic-gate 		 */
2140Sstevel@tonic-gate 		dfn_self_cycle(childp);
2150Sstevel@tonic-gate 	} else {
2160Sstevel@tonic-gate 		/*
2170Sstevel@tonic-gate 		 *	glom intervening functions that aren't already
2180Sstevel@tonic-gate 		 *	glommed into this cycle.
2190Sstevel@tonic-gate 		 *	things have been glommed when their cyclehead field
2200Sstevel@tonic-gate 		 *	points to the head of the cycle they are glommed into.
2210Sstevel@tonic-gate 		 */
2220Sstevel@tonic-gate 		for (tailp = cycleheadp; tailp->cnext; tailp = tailp->cnext) {
2230Sstevel@tonic-gate 			/* void: chase down to tail of things already glommed */
2240Sstevel@tonic-gate #ifdef DEBUG
2250Sstevel@tonic-gate 			if (debug & DFNDEBUG) {
226*211Smike_s 				(void) printf("[dfn_findcycle] tail ");
2270Sstevel@tonic-gate 				printname(tailp);
228*211Smike_s 				(void) printf("\n");
2290Sstevel@tonic-gate 			}
2300Sstevel@tonic-gate #endif /* DEBUG */
2310Sstevel@tonic-gate 		}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 		/*
2340Sstevel@tonic-gate 		 *	if what we think is the top of the cycle
2350Sstevel@tonic-gate 		 *	has a cyclehead field, then it's not really the
2360Sstevel@tonic-gate 		 *	head of the cycle, which is really what we want
2370Sstevel@tonic-gate 		 */
2380Sstevel@tonic-gate 		if (cycleheadp->cyclehead != cycleheadp) {
2390Sstevel@tonic-gate 			cycleheadp = cycleheadp->cyclehead;
2400Sstevel@tonic-gate #ifdef DEBUG
2410Sstevel@tonic-gate 			if (debug & DFNDEBUG) {
242*211Smike_s 				(void) printf("[dfn_findcycle] new cyclehead ");
2430Sstevel@tonic-gate 				printname(cycleheadp);
244*211Smike_s 				(void) printf("\n");
2450Sstevel@tonic-gate 			}
2460Sstevel@tonic-gate #endif /* DEBUG */
2470Sstevel@tonic-gate 		}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 		for (index = cycletop + 1; index <= dfn_depth; index += 1) {
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 			childp = dfn_stack[index].nlentryp;
2520Sstevel@tonic-gate 			if (childp->cyclehead == childp) {
2530Sstevel@tonic-gate 				/*
2540Sstevel@tonic-gate 				 *	not yet glommed anywhere, glom it
2550Sstevel@tonic-gate 				 *	and fix any children it has glommed
2560Sstevel@tonic-gate 				 */
2570Sstevel@tonic-gate 				tailp->cnext = childp;
2580Sstevel@tonic-gate 				childp->cyclehead = cycleheadp;
2590Sstevel@tonic-gate #ifdef DEBUG
2600Sstevel@tonic-gate 				if (debug & DFNDEBUG) {
261*211Smike_s 					(void) printf(
262*211Smike_s 					    "[dfn_findcycle] glomming ");
2630Sstevel@tonic-gate 					printname(childp);
264*211Smike_s 					(void) printf(" onto ");
2650Sstevel@tonic-gate 					printname(cycleheadp);
266*211Smike_s 					(void) printf("\n");
2670Sstevel@tonic-gate 				}
2680Sstevel@tonic-gate #endif /* DEBUG */
2690Sstevel@tonic-gate 				for (tailp = childp; tailp->cnext;
2700Sstevel@tonic-gate 				    tailp = tailp->cnext) {
2710Sstevel@tonic-gate 					tailp->cnext->cyclehead = cycleheadp;
2720Sstevel@tonic-gate #ifdef DEBUG
2730Sstevel@tonic-gate 					if (debug & DFNDEBUG) {
274*211Smike_s 						(void) printf("[dfn_findcycle]"
2750Sstevel@tonic-gate 						    " and its tail ");
2760Sstevel@tonic-gate 						printname(tailp->cnext);
277*211Smike_s 						(void) printf(" onto ");
2780Sstevel@tonic-gate 						printname(cycleheadp);
279*211Smike_s 						(void) printf("\n");
2800Sstevel@tonic-gate 					}
2810Sstevel@tonic-gate #endif /* DEBUG */
2820Sstevel@tonic-gate 				}
2830Sstevel@tonic-gate 			} else if (childp->cyclehead != cycleheadp) {
284*211Smike_s 				(void) fprintf(stderr, "[dfn_busy] glommed,"
2850Sstevel@tonic-gate 				    " but not to cyclehead\n");
2860Sstevel@tonic-gate 			}
2870Sstevel@tonic-gate 		}
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate /*
2920Sstevel@tonic-gate  *	deal with self-cycles
2930Sstevel@tonic-gate  *	for lint: ARGSUSED
2940Sstevel@tonic-gate  */
2950Sstevel@tonic-gate /* ARGSUSED */
2960Sstevel@tonic-gate void
dfn_self_cycle(nltype * parentp)2970Sstevel@tonic-gate dfn_self_cycle(nltype *parentp)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate 	/*
3000Sstevel@tonic-gate 	 *	since we are taking out self-cycles elsewhere
3010Sstevel@tonic-gate 	 *	no need for the special case, here.
3020Sstevel@tonic-gate 	 */
3030Sstevel@tonic-gate #ifdef DEBUG
3040Sstevel@tonic-gate 	if (debug & DFNDEBUG) {
305*211Smike_s 		(void) printf("[dfn_self_cycle] ");
3060Sstevel@tonic-gate 		printname(parentp);
307*211Smike_s 		(void) printf("\n");
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate #endif /* DEBUG */
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate  *	visit a node after all its children
3140Sstevel@tonic-gate  *	[MISSING: an explanation]
3150Sstevel@tonic-gate  *	and pop it off the stack
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate void
dfn_post_visit(nltype * parentp)3180Sstevel@tonic-gate dfn_post_visit(nltype *parentp)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	nltype	*memberp;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate #ifdef DEBUG
3230Sstevel@tonic-gate 	if (debug & DFNDEBUG) {
324*211Smike_s 		(void) printf("[dfn_post_visit]\t%d: ", dfn_depth);
3250Sstevel@tonic-gate 		printname(parentp);
326*211Smike_s 		(void) printf("\n");
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate #endif /* DEBUG */
3290Sstevel@tonic-gate 	/*
3300Sstevel@tonic-gate 	 *	number functions and things in their cycles
3310Sstevel@tonic-gate 	 *	unless the function is itself part of a cycle
3320Sstevel@tonic-gate 	 */
3330Sstevel@tonic-gate 	if (parentp->cyclehead == parentp) {
3340Sstevel@tonic-gate 		dfn_counter += 1;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 		for (memberp = parentp; memberp; memberp = memberp->cnext) {
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 			memberp->toporder = dfn_counter;
3390Sstevel@tonic-gate #ifdef DEBUG
3400Sstevel@tonic-gate 			if (debug & DFNDEBUG) {
341*211Smike_s 				(void) printf("[dfn_post_visit]\t\tmember ");
3420Sstevel@tonic-gate 				printname(memberp);
343*211Smike_s 				(void) printf(" -> toporder = %d\n",
344*211Smike_s 				    dfn_counter);
3450Sstevel@tonic-gate 			}
3460Sstevel@tonic-gate #endif /* DEBUG */
3470Sstevel@tonic-gate 		}
3480Sstevel@tonic-gate #ifdef DEBUG
3490Sstevel@tonic-gate 	} else {
3500Sstevel@tonic-gate 		if (debug & DFNDEBUG)
351*211Smike_s 			(void) printf(
352*211Smike_s 			    "[dfn_post_visit]\t\tis part of a cycle\n");
3530Sstevel@tonic-gate #endif /* DEBUG */
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 	dfn_depth -= 1;
3560Sstevel@tonic-gate }
357