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 */
2292ed1782Smike_s
237c478bd9Sstevel@tonic-gate /*
2492ed1782Smike_s * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
2592ed1782Smike_s * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include "gprof.h"
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #define DFN_DEPTH 100
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate struct dfnstruct {
347c478bd9Sstevel@tonic-gate nltype *nlentryp;
357c478bd9Sstevel@tonic-gate int cycletop;
367c478bd9Sstevel@tonic-gate };
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate typedef struct dfnstruct dfntype;
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate dfntype *dfn_stack = NULL;
417c478bd9Sstevel@tonic-gate int dfn_depth = 0;
427c478bd9Sstevel@tonic-gate int dfn_sz = 0;
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate int dfn_counter = DFN_NAN;
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate * given this parent, depth first number its children.
487c478bd9Sstevel@tonic-gate */
497c478bd9Sstevel@tonic-gate void
dfn(nltype * parentp)507c478bd9Sstevel@tonic-gate dfn(nltype *parentp)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate arctype *arcp;
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate #ifdef DEBUG
557c478bd9Sstevel@tonic-gate if (debug & DFNDEBUG) {
5692ed1782Smike_s (void) printf("[dfn] dfn(");
577c478bd9Sstevel@tonic-gate printname(parentp);
5892ed1782Smike_s (void) printf(")\n");
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate #endif /* DEBUG */
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate if (!dfn_stack) {
637c478bd9Sstevel@tonic-gate dfn_sz = DFN_DEPTH;
647c478bd9Sstevel@tonic-gate dfn_stack = (dfntype *) malloc(dfn_sz * sizeof (dfntype));
657c478bd9Sstevel@tonic-gate if (!dfn_stack) {
6692ed1782Smike_s (void) fprintf(stderr,
677c478bd9Sstevel@tonic-gate "fatal: can't malloc %d objects\n", dfn_sz);
687c478bd9Sstevel@tonic-gate exit(1);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate * if we're already numbered, no need to look any furthur.
747c478bd9Sstevel@tonic-gate */
757c478bd9Sstevel@tonic-gate if (dfn_numbered(parentp))
767c478bd9Sstevel@tonic-gate return;
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate * if we're already busy, must be a cycle
807c478bd9Sstevel@tonic-gate */
817c478bd9Sstevel@tonic-gate if (dfn_busy(parentp)) {
827c478bd9Sstevel@tonic-gate dfn_findcycle(parentp);
837c478bd9Sstevel@tonic-gate return;
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate * visit yourself before your children
887c478bd9Sstevel@tonic-gate */
897c478bd9Sstevel@tonic-gate dfn_pre_visit(parentp);
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate * visit children
937c478bd9Sstevel@tonic-gate */
947c478bd9Sstevel@tonic-gate for (arcp = parentp->children; arcp; arcp = arcp->arc_childlist)
957c478bd9Sstevel@tonic-gate dfn(arcp->arc_childp);
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate * visit yourself after your children
997c478bd9Sstevel@tonic-gate */
1007c478bd9Sstevel@tonic-gate dfn_post_visit(parentp);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate * push a parent onto the stack and mark it busy
1057c478bd9Sstevel@tonic-gate */
1067c478bd9Sstevel@tonic-gate void
dfn_pre_visit(nltype * parentp)1077c478bd9Sstevel@tonic-gate dfn_pre_visit(nltype *parentp)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate if (!dfn_stack) {
1117c478bd9Sstevel@tonic-gate dfn_sz = DFN_DEPTH;
1127c478bd9Sstevel@tonic-gate dfn_stack = (dfntype *) malloc(dfn_sz * sizeof (dfntype));
1137c478bd9Sstevel@tonic-gate if (!dfn_stack) {
11492ed1782Smike_s (void) printf("fatal: can't malloc %d objects\n",
11592ed1782Smike_s dfn_sz);
1167c478bd9Sstevel@tonic-gate exit(1);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate dfn_depth += 1;
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate if (dfn_depth >= dfn_sz) {
1237c478bd9Sstevel@tonic-gate dfn_sz += DFN_DEPTH;
1247c478bd9Sstevel@tonic-gate dfn_stack = (dfntype *) realloc(dfn_stack,
1257c478bd9Sstevel@tonic-gate dfn_sz * sizeof (dfntype));
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate if (!dfn_stack) {
12892ed1782Smike_s (void) fprintf(stderr,
1297c478bd9Sstevel@tonic-gate "fatal: can't realloc %d objects\n", dfn_sz);
1307c478bd9Sstevel@tonic-gate exit(1);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate dfn_stack[dfn_depth].nlentryp = parentp;
1357c478bd9Sstevel@tonic-gate dfn_stack[dfn_depth].cycletop = dfn_depth;
1367c478bd9Sstevel@tonic-gate parentp->toporder = DFN_BUSY;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate #ifdef DEBUG
1397c478bd9Sstevel@tonic-gate if (debug & DFNDEBUG) {
14092ed1782Smike_s (void) printf("[dfn_pre_visit]\t\t%d:", dfn_depth);
1417c478bd9Sstevel@tonic-gate printname(parentp);
14292ed1782Smike_s (void) printf("\n");
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate * are we already numbered?
1497c478bd9Sstevel@tonic-gate */
1507c478bd9Sstevel@tonic-gate bool
dfn_numbered(nltype * childp)1517c478bd9Sstevel@tonic-gate dfn_numbered(nltype *childp)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate return (childp->toporder != DFN_NAN && childp->toporder != DFN_BUSY);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate * are we already busy?
1587c478bd9Sstevel@tonic-gate */
1597c478bd9Sstevel@tonic-gate bool
dfn_busy(nltype * childp)1607c478bd9Sstevel@tonic-gate dfn_busy(nltype *childp)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate if (childp->toporder == DFN_NAN)
1637c478bd9Sstevel@tonic-gate return (FALSE);
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate return (TRUE);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate void
dfn_findcycle(nltype * childp)1697c478bd9Sstevel@tonic-gate dfn_findcycle(nltype *childp)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate int cycletop;
172*7879e8a6SToomas Soome nltype *cycleheadp = NULL;
1737c478bd9Sstevel@tonic-gate nltype *tailp;
1747c478bd9Sstevel@tonic-gate int index;
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate for (cycletop = dfn_depth; cycletop > 0; cycletop -= 1) {
1777c478bd9Sstevel@tonic-gate cycleheadp = dfn_stack[cycletop].nlentryp;
1787c478bd9Sstevel@tonic-gate if (childp == cycleheadp)
1797c478bd9Sstevel@tonic-gate break;
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate if (childp->cyclehead != childp &&
1827c478bd9Sstevel@tonic-gate childp->cyclehead == cycleheadp)
1837c478bd9Sstevel@tonic-gate break;
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate if (cycletop <= 0) {
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate * don't report non existent functions
1897c478bd9Sstevel@tonic-gate */
1907c478bd9Sstevel@tonic-gate if (childp->value) {
19192ed1782Smike_s (void) fprintf(stderr,
19292ed1782Smike_s "[dfn_findcycle] couldn't find head "
1937c478bd9Sstevel@tonic-gate "of cycle for %s\n", childp->name);
1947c478bd9Sstevel@tonic-gate return;
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate #ifdef DEBUG
1997c478bd9Sstevel@tonic-gate if (debug & DFNDEBUG) {
20092ed1782Smike_s (void) printf("[dfn_findcycle] dfn_depth %d cycletop %d ",
2017c478bd9Sstevel@tonic-gate dfn_depth, cycletop);
2027c478bd9Sstevel@tonic-gate printname(cycleheadp);
20392ed1782Smike_s (void) printf("\n");
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate if (cycletop == dfn_depth) {
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate * this is previous function, e.g. this calls itself
2107c478bd9Sstevel@tonic-gate * sort of boring
2117c478bd9Sstevel@tonic-gate */
2127c478bd9Sstevel@tonic-gate dfn_self_cycle(childp);
2137c478bd9Sstevel@tonic-gate } else {
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate * glom intervening functions that aren't already
2167c478bd9Sstevel@tonic-gate * glommed into this cycle.
2177c478bd9Sstevel@tonic-gate * things have been glommed when their cyclehead field
2187c478bd9Sstevel@tonic-gate * points to the head of the cycle they are glommed into.
2197c478bd9Sstevel@tonic-gate */
2207c478bd9Sstevel@tonic-gate for (tailp = cycleheadp; tailp->cnext; tailp = tailp->cnext) {
2217c478bd9Sstevel@tonic-gate /* void: chase down to tail of things already glommed */
2227c478bd9Sstevel@tonic-gate #ifdef DEBUG
2237c478bd9Sstevel@tonic-gate if (debug & DFNDEBUG) {
22492ed1782Smike_s (void) printf("[dfn_findcycle] tail ");
2257c478bd9Sstevel@tonic-gate printname(tailp);
22692ed1782Smike_s (void) printf("\n");
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate * if what we think is the top of the cycle
2337c478bd9Sstevel@tonic-gate * has a cyclehead field, then it's not really the
2347c478bd9Sstevel@tonic-gate * head of the cycle, which is really what we want
2357c478bd9Sstevel@tonic-gate */
2367c478bd9Sstevel@tonic-gate if (cycleheadp->cyclehead != cycleheadp) {
2377c478bd9Sstevel@tonic-gate cycleheadp = cycleheadp->cyclehead;
2387c478bd9Sstevel@tonic-gate #ifdef DEBUG
2397c478bd9Sstevel@tonic-gate if (debug & DFNDEBUG) {
24092ed1782Smike_s (void) printf("[dfn_findcycle] new cyclehead ");
2417c478bd9Sstevel@tonic-gate printname(cycleheadp);
24292ed1782Smike_s (void) printf("\n");
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate for (index = cycletop + 1; index <= dfn_depth; index += 1) {
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate childp = dfn_stack[index].nlentryp;
2507c478bd9Sstevel@tonic-gate if (childp->cyclehead == childp) {
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate * not yet glommed anywhere, glom it
2537c478bd9Sstevel@tonic-gate * and fix any children it has glommed
2547c478bd9Sstevel@tonic-gate */
2557c478bd9Sstevel@tonic-gate tailp->cnext = childp;
2567c478bd9Sstevel@tonic-gate childp->cyclehead = cycleheadp;
2577c478bd9Sstevel@tonic-gate #ifdef DEBUG
2587c478bd9Sstevel@tonic-gate if (debug & DFNDEBUG) {
25992ed1782Smike_s (void) printf(
26092ed1782Smike_s "[dfn_findcycle] glomming ");
2617c478bd9Sstevel@tonic-gate printname(childp);
26292ed1782Smike_s (void) printf(" onto ");
2637c478bd9Sstevel@tonic-gate printname(cycleheadp);
26492ed1782Smike_s (void) printf("\n");
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2677c478bd9Sstevel@tonic-gate for (tailp = childp; tailp->cnext;
2687c478bd9Sstevel@tonic-gate tailp = tailp->cnext) {
2697c478bd9Sstevel@tonic-gate tailp->cnext->cyclehead = cycleheadp;
2707c478bd9Sstevel@tonic-gate #ifdef DEBUG
2717c478bd9Sstevel@tonic-gate if (debug & DFNDEBUG) {
27292ed1782Smike_s (void) printf("[dfn_findcycle]"
2737c478bd9Sstevel@tonic-gate " and its tail ");
2747c478bd9Sstevel@tonic-gate printname(tailp->cnext);
27592ed1782Smike_s (void) printf(" onto ");
2767c478bd9Sstevel@tonic-gate printname(cycleheadp);
27792ed1782Smike_s (void) printf("\n");
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate } else if (childp->cyclehead != cycleheadp) {
28292ed1782Smike_s (void) fprintf(stderr, "[dfn_busy] glommed,"
2837c478bd9Sstevel@tonic-gate " but not to cyclehead\n");
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate * deal with self-cycles
2917c478bd9Sstevel@tonic-gate * for lint: ARGSUSED
2927c478bd9Sstevel@tonic-gate */
2937c478bd9Sstevel@tonic-gate /* ARGSUSED */
2947c478bd9Sstevel@tonic-gate void
dfn_self_cycle(nltype * parentp)2957c478bd9Sstevel@tonic-gate dfn_self_cycle(nltype *parentp)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate * since we are taking out self-cycles elsewhere
2997c478bd9Sstevel@tonic-gate * no need for the special case, here.
3007c478bd9Sstevel@tonic-gate */
3017c478bd9Sstevel@tonic-gate #ifdef DEBUG
3027c478bd9Sstevel@tonic-gate if (debug & DFNDEBUG) {
30392ed1782Smike_s (void) printf("[dfn_self_cycle] ");
3047c478bd9Sstevel@tonic-gate printname(parentp);
30592ed1782Smike_s (void) printf("\n");
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate #endif /* DEBUG */
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate /*
3117c478bd9Sstevel@tonic-gate * visit a node after all its children
3127c478bd9Sstevel@tonic-gate * [MISSING: an explanation]
3137c478bd9Sstevel@tonic-gate * and pop it off the stack
3147c478bd9Sstevel@tonic-gate */
3157c478bd9Sstevel@tonic-gate void
dfn_post_visit(nltype * parentp)3167c478bd9Sstevel@tonic-gate dfn_post_visit(nltype *parentp)
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate nltype *memberp;
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate #ifdef DEBUG
3217c478bd9Sstevel@tonic-gate if (debug & DFNDEBUG) {
32292ed1782Smike_s (void) printf("[dfn_post_visit]\t%d: ", dfn_depth);
3237c478bd9Sstevel@tonic-gate printname(parentp);
32492ed1782Smike_s (void) printf("\n");
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate #endif /* DEBUG */
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate * number functions and things in their cycles
3297c478bd9Sstevel@tonic-gate * unless the function is itself part of a cycle
3307c478bd9Sstevel@tonic-gate */
3317c478bd9Sstevel@tonic-gate if (parentp->cyclehead == parentp) {
3327c478bd9Sstevel@tonic-gate dfn_counter += 1;
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate for (memberp = parentp; memberp; memberp = memberp->cnext) {
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate memberp->toporder = dfn_counter;
3377c478bd9Sstevel@tonic-gate #ifdef DEBUG
3387c478bd9Sstevel@tonic-gate if (debug & DFNDEBUG) {
33992ed1782Smike_s (void) printf("[dfn_post_visit]\t\tmember ");
3407c478bd9Sstevel@tonic-gate printname(memberp);
34192ed1782Smike_s (void) printf(" -> toporder = %d\n",
34292ed1782Smike_s dfn_counter);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate #endif /* DEBUG */
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate #ifdef DEBUG
3477c478bd9Sstevel@tonic-gate } else {
3487c478bd9Sstevel@tonic-gate if (debug & DFNDEBUG)
34992ed1782Smike_s (void) printf(
35092ed1782Smike_s "[dfn_post_visit]\t\tis part of a cycle\n");
3517c478bd9Sstevel@tonic-gate #endif /* DEBUG */
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate dfn_depth -= 1;
3547c478bd9Sstevel@tonic-gate }
355