xref: /titanic_41/usr/src/cmd/fs.d/cachefs/mdbug/dbug.c (revision 47644099886aa8d3f43120b9eede3044342be473)
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  */
227c478bd9Sstevel@tonic-gate /*
23*47644099Sgt29601  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*47644099Sgt29601  * Use is subject to license terms.
25*47644099Sgt29601  */
26*47644099Sgt29601 /*
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  *			dbug.c
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * Purpose:
317c478bd9Sstevel@tonic-gate  *    Implements the dbug_routine class.
327c478bd9Sstevel@tonic-gate  *    This code is derived from the public domain DBUG
337c478bd9Sstevel@tonic-gate  *    package written by Fred Fish.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #ifndef DBUG_OFF
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <string.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <stdarg.h>
457c478bd9Sstevel@tonic-gate #include <string.h>
467c478bd9Sstevel@tonic-gate #include <time.h>
477c478bd9Sstevel@tonic-gate #include <thread.h>
487c478bd9Sstevel@tonic-gate #include <sys/types.h>
497c478bd9Sstevel@tonic-gate #include <signal.h>
507c478bd9Sstevel@tonic-gate #include "flist.h"
517c478bd9Sstevel@tonic-gate #include "mdbug.h"
527c478bd9Sstevel@tonic-gate #include "priv.h"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /* forward references */
557c478bd9Sstevel@tonic-gate static int listparse(register char *ctlp, flist_object_t *head);
567c478bd9Sstevel@tonic-gate static boolean inlist(flist_object_t *flist_object_p, const char *cp);
577c478bd9Sstevel@tonic-gate static boolean dotrace(dbug_state_object_t *dbug_state_object_p,
587c478bd9Sstevel@tonic-gate     const char *func, const char *process);
597c478bd9Sstevel@tonic-gate static void indent(register dbug_state_object_t *dbug_state_object_p,
607c478bd9Sstevel@tonic-gate     int indent);
617c478bd9Sstevel@tonic-gate static void doprefix(dbug_state_object_t *dbug_state_object_p, int line,
627c478bd9Sstevel@tonic-gate     long lineno, const char *file, const char *process);
637c478bd9Sstevel@tonic-gate static FILE *openfile(char *name);
647c478bd9Sstevel@tonic-gate static boolean writable(char *pathname);
657c478bd9Sstevel@tonic-gate static void changeowner(char *pathname);
667c478bd9Sstevel@tonic-gate static int delayarg(int value);
67*47644099Sgt29601 static void delay(uint_t xx);
68*47644099Sgt29601 static ulong_t getclock();
697c478bd9Sstevel@tonic-gate static char *mystrtok(char *s1, char *s2);
707c478bd9Sstevel@tonic-gate void doabort();
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /* initialize static members of class */
737c478bd9Sstevel@tonic-gate int	sd_on = 0;
747c478bd9Sstevel@tonic-gate char	sd_process[128];
757c478bd9Sstevel@tonic-gate long	sd_lineno = 0;
767c478bd9Sstevel@tonic-gate dbug_state_object_t *sd_push = NULL;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /* this structure defines thread specific data */
797c478bd9Sstevel@tonic-gate typedef struct thread_data {
807c478bd9Sstevel@tonic-gate #ifdef STACKINIT
817c478bd9Sstevel@tonic-gate 	unsigned long	 td_stackinit;		/* Begining of stack. */
827c478bd9Sstevel@tonic-gate #endif
837c478bd9Sstevel@tonic-gate 	int		 td_line;		/* Current line number. */
847c478bd9Sstevel@tonic-gate 	char		 td_keyword[64];	/* Current keyword. */
857c478bd9Sstevel@tonic-gate 	dbug_object_t	*td_first;		/* Current routine. */
867c478bd9Sstevel@tonic-gate } thread_data_t;
877c478bd9Sstevel@tonic-gate #ifdef _REENTRANT
887c478bd9Sstevel@tonic-gate mutex_t		mdt_lock;
897c478bd9Sstevel@tonic-gate int		mdt_once = 0;
907c478bd9Sstevel@tonic-gate thread_key_t	mdt_key;
917c478bd9Sstevel@tonic-gate #else
927c478bd9Sstevel@tonic-gate thread_data_t	mdt_data;
937c478bd9Sstevel@tonic-gate #endif
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * format of control string
967c478bd9Sstevel@tonic-gate  *   command[:command:...]
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  *   commands
997c478bd9Sstevel@tonic-gate  *   debugging on	'd'  d[,<keyword>[,...]]
1007c478bd9Sstevel@tonic-gate  *   delay value	'D'  D[,<delay value>]
1017c478bd9Sstevel@tonic-gate  *   function list	'f'  f[,<function name>[,...]]
1027c478bd9Sstevel@tonic-gate  *   print filename	'F'  F
1037c478bd9Sstevel@tonic-gate  *   print pid		'i'  i
1047c478bd9Sstevel@tonic-gate  *   print line number	'L'  L
1057c478bd9Sstevel@tonic-gate  *   print call depth	'n'  n
1067c478bd9Sstevel@tonic-gate  *   number each line	'N'  N
1077c478bd9Sstevel@tonic-gate  *   output file	'o'  o[,<filename>
1087c478bd9Sstevel@tonic-gate  *   process name list	'p'  p[,<process name>[,...]]
1097c478bd9Sstevel@tonic-gate  *   print proc name	'P'  P
1107c478bd9Sstevel@tonic-gate  *   reset indentation	'r'  r
1117c478bd9Sstevel@tonic-gate  *   print runtime	'R'  R
1127c478bd9Sstevel@tonic-gate  *   print thread info	'T'  T
1137c478bd9Sstevel@tonic-gate  *   print trace	't'  t
1147c478bd9Sstevel@tonic-gate  *   print stack depth	's'  s
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  *		dbug_object_create
1207c478bd9Sstevel@tonic-gate  *
1217c478bd9Sstevel@tonic-gate  * Description:
1227c478bd9Sstevel@tonic-gate  *	Constructor for the dbug_routine class.
1237c478bd9Sstevel@tonic-gate  * Arguments:
1247c478bd9Sstevel@tonic-gate  *	line	- line number where object was created.
1257c478bd9Sstevel@tonic-gate  *	file	- file name object was created in.
1267c478bd9Sstevel@tonic-gate  *	function- routine name object was created in.
1277c478bd9Sstevel@tonic-gate  * Returns:
1287c478bd9Sstevel@tonic-gate  * Errors:
1297c478bd9Sstevel@tonic-gate  * Preconditions:
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate void
dbug_object_create(int line,const char * file,const char * function)1327c478bd9Sstevel@tonic-gate dbug_object_create(int line, const char *file, const char *function)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	dbug_object_t  *dbug_object_p;
1357c478bd9Sstevel@tonic-gate 	dbug_state_object_t *dbug_state_object_p;
136*47644099Sgt29601 	ulong_t stacksize;
1377c478bd9Sstevel@tonic-gate 	int created = 0;
1387c478bd9Sstevel@tonic-gate 	char *cptr;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	thread_data_t *tdp = NULL;
1417c478bd9Sstevel@tonic-gate #ifdef _REENTRANT
1427c478bd9Sstevel@tonic-gate 	LOCK_THREAD_DATA();
1437c478bd9Sstevel@tonic-gate 	if (!mdt_once) {
1447c478bd9Sstevel@tonic-gate 		if (thr_keycreate(&mdt_key, dbug_thread_exit) != 0)
1457c478bd9Sstevel@tonic-gate 			doabort();
1467c478bd9Sstevel@tonic-gate 		mdt_once++;
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 	GET_THREAD_DATA_PTR(&tdp);
1497c478bd9Sstevel@tonic-gate 	if (tdp == NULL) {
1507c478bd9Sstevel@tonic-gate 		tdp = (thread_data_t *)calloc(sizeof (*tdp), 1);
1517c478bd9Sstevel@tonic-gate 		if (tdp == NULL)
1527c478bd9Sstevel@tonic-gate 			doabort();
1537c478bd9Sstevel@tonic-gate 		thr_setspecific(mdt_key, tdp);
1547c478bd9Sstevel@tonic-gate 		created = 1;
1557c478bd9Sstevel@tonic-gate 		tdp->td_keyword[0] = '\0';
1567c478bd9Sstevel@tonic-gate 		tdp->td_first = NULL;
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate #else
1597c478bd9Sstevel@tonic-gate 	GET_THREAD_DATA_PTR(&tdp);
1607c478bd9Sstevel@tonic-gate #endif
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	dbug_object_p = (dbug_object_t *)calloc(sizeof (dbug_object_t), 1);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (dbug_object_p == NULL)
1657c478bd9Sstevel@tonic-gate 		doabort();
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/* save the function name */
1687c478bd9Sstevel@tonic-gate 	if (function)
1697c478bd9Sstevel@tonic-gate 		strcpy(dbug_object_p->d_func, function);
1707c478bd9Sstevel@tonic-gate 	else
1717c478bd9Sstevel@tonic-gate 		strcpy(dbug_object_p->d_func, "unknown");
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/* save the base of the file name */
1747c478bd9Sstevel@tonic-gate 	if (file) {
1757c478bd9Sstevel@tonic-gate 		cptr = strrchr(file, '/');
1767c478bd9Sstevel@tonic-gate 		if (cptr == NULL)
1777c478bd9Sstevel@tonic-gate 			strcpy(dbug_object_p->d_file, file);
1787c478bd9Sstevel@tonic-gate 		else
1797c478bd9Sstevel@tonic-gate 			strcpy(dbug_object_p->d_file, cptr++);
1807c478bd9Sstevel@tonic-gate 	} else
1817c478bd9Sstevel@tonic-gate 		strcpy(dbug_object_p->d_file, "unknown");
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/* Chain this onto our list of them */
1847c478bd9Sstevel@tonic-gate 	dbug_object_p->d_prev = tdp->td_first;
1857c478bd9Sstevel@tonic-gate 	tdp->td_first = dbug_object_p;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/* set the default routine exit point line number to zero */
1887c478bd9Sstevel@tonic-gate 	dbug_object_p->d_leaveline = 0;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	/* If debugging is off, then all done */
1917c478bd9Sstevel@tonic-gate 	if (NOT db_debugon())
1927c478bd9Sstevel@tonic-gate 		goto out;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/* if the active state is null initialize it */
1957c478bd9Sstevel@tonic-gate 	if (sd_push == NULL)
1967c478bd9Sstevel@tonic-gate 		db_push("d,:f,:F:i:L:n:N:o,cfsd_debug.out:p,:P:r:R:T:t:s");
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/* get a pointer to the active state */
1997c478bd9Sstevel@tonic-gate 	dbug_state_object_p = sd_push;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate #ifdef STACKINIT
2027c478bd9Sstevel@tonic-gate 	/*
2037c478bd9Sstevel@tonic-gate 	 * Get the new stack depth.
2047c478bd9Sstevel@tonic-gate 	 * There a two problems associated with this.
2057c478bd9Sstevel@tonic-gate 	 * One is because c++ allows declarations anywhere inside of
2067c478bd9Sstevel@tonic-gate 	 * a routine.  So it is difficult to position the dbug_enter()
2077c478bd9Sstevel@tonic-gate 	 * macro after all declarations and still be useful.
2087c478bd9Sstevel@tonic-gate 	 * Two is that the dbug_enter() macro should be before all
2097c478bd9Sstevel@tonic-gate 	 * other automatic objects so that its destructor gets called
2107c478bd9Sstevel@tonic-gate 	 * last as the routine is returning.
2117c478bd9Sstevel@tonic-gate 	 * The solution is to advise placing the dbug_enter() macro at
2127c478bd9Sstevel@tonic-gate 	 * the start of the routine and specifying that that stack
2137c478bd9Sstevel@tonic-gate 	 * values apply upto but not including the current routine.
2147c478bd9Sstevel@tonic-gate 	 */
215*47644099Sgt29601 	stacksize = (ulong_t)this;
2167c478bd9Sstevel@tonic-gate 	if (GROWDOWN)
2177c478bd9Sstevel@tonic-gate 		stacksize = tdp->td_stackinit - stacksize;
2187c478bd9Sstevel@tonic-gate 	else
2197c478bd9Sstevel@tonic-gate 		stacksize = stacksize - tdp->td_stackinit;
2207c478bd9Sstevel@tonic-gate #endif
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/* record the new nesting level */
2237c478bd9Sstevel@tonic-gate 	dbug_state_object_p->s_level++;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/* if producing a trace of function calls */
2267c478bd9Sstevel@tonic-gate 	if (dotrace(dbug_state_object_p, dbug_object_p->d_func, sd_process)) {
2277c478bd9Sstevel@tonic-gate 		doprefix(dbug_state_object_p, line, sd_lineno++,
2287c478bd9Sstevel@tonic-gate 		    dbug_object_p->d_file, sd_process);
2297c478bd9Sstevel@tonic-gate 		indent(dbug_state_object_p, dbug_state_object_p->s_level);
2307c478bd9Sstevel@tonic-gate 		if (dbug_state_object_p->sf_stack)
2317c478bd9Sstevel@tonic-gate 			fprintf(dbug_state_object_p->s_out_file, ">%s   %ld\n",
2327c478bd9Sstevel@tonic-gate 			    dbug_object_p->d_func, stacksize);
2337c478bd9Sstevel@tonic-gate 		else
2347c478bd9Sstevel@tonic-gate 			fprintf(dbug_state_object_p->s_out_file, ">%s\n",
2357c478bd9Sstevel@tonic-gate 			    dbug_object_p->d_func);
2367c478bd9Sstevel@tonic-gate 		fflush(dbug_state_object_p->s_out_file);
2377c478bd9Sstevel@tonic-gate 		delay(dbug_state_object_p->s_delay);
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	/* if a new thread */
2417c478bd9Sstevel@tonic-gate 	if (created && dbug_state_object_p->sf_thread) {
2427c478bd9Sstevel@tonic-gate 		doprefix(dbug_state_object_p, line, sd_lineno++,
2437c478bd9Sstevel@tonic-gate 		    dbug_object_p->d_file, sd_process);
2447c478bd9Sstevel@tonic-gate 		indent(dbug_state_object_p, dbug_state_object_p->s_level);
2457c478bd9Sstevel@tonic-gate 		fprintf(dbug_state_object_p->s_out_file, "thread created\n");
2467c478bd9Sstevel@tonic-gate 		fflush(dbug_state_object_p->s_out_file);
2477c478bd9Sstevel@tonic-gate 		delay(dbug_state_object_p->s_delay);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate out:;
2517c478bd9Sstevel@tonic-gate 	UNLOCK_THREAD_DATA();
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate  *
2567c478bd9Sstevel@tonic-gate  *		dbug_object_destroy
2577c478bd9Sstevel@tonic-gate  *
2587c478bd9Sstevel@tonic-gate  * Description:
2597c478bd9Sstevel@tonic-gate  *	Destructor for the dbug_routine class.
2607c478bd9Sstevel@tonic-gate  *	Unchains this object from the list.
2617c478bd9Sstevel@tonic-gate  * Arguments:
2627c478bd9Sstevel@tonic-gate  * Returns:
2637c478bd9Sstevel@tonic-gate  * Errors:
2647c478bd9Sstevel@tonic-gate  * Preconditions:
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate void
dbug_object_destroy(char * function_name,int line)2677c478bd9Sstevel@tonic-gate dbug_object_destroy(char *function_name, int line)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	dbug_object_t *dbug_object_p;
2707c478bd9Sstevel@tonic-gate 	dbug_state_object_t *dbug_state_object_p;
2717c478bd9Sstevel@tonic-gate 	thread_data_t *tdp;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	LOCK_THREAD_DATA();
2747c478bd9Sstevel@tonic-gate 	GET_THREAD_DATA_PTR(&tdp);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/* unchain from the list of objects */
2777c478bd9Sstevel@tonic-gate 	dbug_object_p = tdp->td_first;
2787c478bd9Sstevel@tonic-gate 	tdp->td_first = dbug_object_p->d_prev;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	/* If debugging is off, then nothing else to do */
2817c478bd9Sstevel@tonic-gate 	if (NOT db_debugon())
2827c478bd9Sstevel@tonic-gate 		goto out;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	dbug_object_p->d_leaveline = line;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	/* get a pointer to the active state */
2877c478bd9Sstevel@tonic-gate 	dbug_state_object_p = sd_push;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/*
2907c478bd9Sstevel@tonic-gate 	 * Make sure the last one created is being deleted.
2917c478bd9Sstevel@tonic-gate 	 * This will not be the case if there are multiple dbug_routine
2927c478bd9Sstevel@tonic-gate 	 * objects per routine or if one is created outside of a routine.
2937c478bd9Sstevel@tonic-gate 	 */
2947c478bd9Sstevel@tonic-gate 	if (strcmp(function_name, dbug_object_p->d_func)) {
2957c478bd9Sstevel@tonic-gate 		doprefix(dbug_state_object_p, dbug_object_p->d_leaveline,
2967c478bd9Sstevel@tonic-gate 		    sd_lineno++, dbug_object_p->d_file, sd_process);
2977c478bd9Sstevel@tonic-gate 		indent(dbug_state_object_p, dbug_state_object_p->s_level);
2987c478bd9Sstevel@tonic-gate 		fprintf(dbug_state_object_p->s_out_file,
2997c478bd9Sstevel@tonic-gate 		    "<expected %s, actual %s, ERROR: "
3007c478bd9Sstevel@tonic-gate 		    "dbug_enter/dbug_leave out of sequence.\n",
3017c478bd9Sstevel@tonic-gate 		    dbug_object_p->d_func, function_name);
3027c478bd9Sstevel@tonic-gate 		fflush(dbug_state_object_p->s_out_file);
3037c478bd9Sstevel@tonic-gate 		/* delay(dbug_state_object_p->s_delay); */
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/* if producing a trace of function calls */
3077c478bd9Sstevel@tonic-gate 	if (dotrace(dbug_state_object_p, dbug_object_p->d_func, sd_process)) {
3087c478bd9Sstevel@tonic-gate 		doprefix(dbug_state_object_p, dbug_object_p->d_leaveline,
3097c478bd9Sstevel@tonic-gate 		    sd_lineno++, dbug_object_p->d_file, sd_process);
3107c478bd9Sstevel@tonic-gate 		indent(dbug_state_object_p, dbug_state_object_p->s_level);
3117c478bd9Sstevel@tonic-gate 		fprintf(dbug_state_object_p->s_out_file, "<%s\n",
3127c478bd9Sstevel@tonic-gate 		    dbug_object_p->d_func);
3137c478bd9Sstevel@tonic-gate 		fflush(dbug_state_object_p->s_out_file);
3147c478bd9Sstevel@tonic-gate #if 0
3157c478bd9Sstevel@tonic-gate 		delay(dbug_state_object_p->s_delay);
3167c478bd9Sstevel@tonic-gate #endif
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/* record the new nesting level */
3217c478bd9Sstevel@tonic-gate 	dbug_state_object_p->s_level--;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate out:;
3247c478bd9Sstevel@tonic-gate 	free(dbug_object_p);
3257c478bd9Sstevel@tonic-gate 	UNLOCK_THREAD_DATA();
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate  *
3307c478bd9Sstevel@tonic-gate  *		db_keyword
3317c478bd9Sstevel@tonic-gate  *
3327c478bd9Sstevel@tonic-gate  * Description:
3337c478bd9Sstevel@tonic-gate  *	Test a keyword to determine if it is in the currently active
3347c478bd9Sstevel@tonic-gate  *	keyword list.  As with the function list, a keyword is accepted
3357c478bd9Sstevel@tonic-gate  *	if the list is null, otherwise it must match one of the list
3367c478bd9Sstevel@tonic-gate  *	members.  When debugging is not on, no keywords are accepted.
3377c478bd9Sstevel@tonic-gate  *	After the maximum trace level is exceeded, no keywords are
3387c478bd9Sstevel@tonic-gate  *	accepted (this behavior subject to change).  Additionally,
3397c478bd9Sstevel@tonic-gate  *	the current function and process must be accepted based on
3407c478bd9Sstevel@tonic-gate  *	their respective lists.
3417c478bd9Sstevel@tonic-gate  * Arguments:
3427c478bd9Sstevel@tonic-gate  *	keyword - the keyword to test
3437c478bd9Sstevel@tonic-gate  * Returns:
3447c478bd9Sstevel@tonic-gate  *	Returns 1 if keyword accepted, 0 otherwise.
3457c478bd9Sstevel@tonic-gate  * Errors:
3467c478bd9Sstevel@tonic-gate  * Preconditions:
3477c478bd9Sstevel@tonic-gate  *	precond(keyword)
3487c478bd9Sstevel@tonic-gate  */
3497c478bd9Sstevel@tonic-gate int
db_keyword(dbug_object_t * dbug_object_p,const char * keyword)3507c478bd9Sstevel@tonic-gate db_keyword(dbug_object_t *dbug_object_p, const char *keyword)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	dbug_state_object_t *dbug_state_object_p;
3537c478bd9Sstevel@tonic-gate 	int ret = 0;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	/* return FALSE if not debugging */
3567c478bd9Sstevel@tonic-gate 	if (NOT db_debugon())
3577c478bd9Sstevel@tonic-gate 		return (0);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	LOCK_THREAD_DATA();
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/* return FALSE if not debugging */
3627c478bd9Sstevel@tonic-gate 	if (NOT db_debugon())
3637c478bd9Sstevel@tonic-gate 		goto out;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/* get a pointer to the active state */
3667c478bd9Sstevel@tonic-gate 	dbug_state_object_p = sd_push;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (dbug_state_object_p->sf_debug) {  /* is this test necessary ? */
3697c478bd9Sstevel@tonic-gate 		if (inlist(dbug_state_object_p->s_functions,
3707c478bd9Sstevel@tonic-gate 		    dbug_object_p->d_func)) {
3717c478bd9Sstevel@tonic-gate 			if (inlist(dbug_state_object_p->s_processes,
3727c478bd9Sstevel@tonic-gate 			    sd_process)) {
3737c478bd9Sstevel@tonic-gate 				if (inlist(dbug_state_object_p->s_keywords,
3747c478bd9Sstevel@tonic-gate 				    keyword)) {
3757c478bd9Sstevel@tonic-gate 					ret = 1;
3767c478bd9Sstevel@tonic-gate 					goto out;
3777c478bd9Sstevel@tonic-gate 				}
3787c478bd9Sstevel@tonic-gate 			}
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate out:
3837c478bd9Sstevel@tonic-gate 	UNLOCK_THREAD_DATA();
3847c478bd9Sstevel@tonic-gate 	return (ret);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate /*
3887c478bd9Sstevel@tonic-gate  *
3897c478bd9Sstevel@tonic-gate  *		db_pargs
3907c478bd9Sstevel@tonic-gate  *
3917c478bd9Sstevel@tonic-gate  * Description:
3927c478bd9Sstevel@tonic-gate  *	Saves arguments for subsequent usage by db_printf.
3937c478bd9Sstevel@tonic-gate  * Arguments:
3947c478bd9Sstevel@tonic-gate  *	line    - the line number the db_print occurs on
3957c478bd9Sstevel@tonic-gate  *	keyword - determines whether or not to really print anything
3967c478bd9Sstevel@tonic-gate  * Returns:
3977c478bd9Sstevel@tonic-gate  * Errors:
3987c478bd9Sstevel@tonic-gate  * Preconditions:
3997c478bd9Sstevel@tonic-gate  *	precond(keyword)
4007c478bd9Sstevel@tonic-gate  */
4017c478bd9Sstevel@tonic-gate void
db_pargs(dbug_object_t * dbug_object_p,int line,char * keyword)4027c478bd9Sstevel@tonic-gate db_pargs(dbug_object_t *dbug_object_p, int line, char *keyword)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	thread_data_t *tdp;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	/* return if no debugging yet */
4077c478bd9Sstevel@tonic-gate 	if (NOT db_debugon())
4087c478bd9Sstevel@tonic-gate 		return;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	GET_THREAD_DATA_PTR(&tdp);
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	tdp->td_line = line;
4137c478bd9Sstevel@tonic-gate 	if (keyword)
4147c478bd9Sstevel@tonic-gate 		strcpy(tdp->td_keyword, keyword);
4157c478bd9Sstevel@tonic-gate 	else
4167c478bd9Sstevel@tonic-gate 		tdp->td_keyword[0] = '\0';
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate int
db_getfd()4207c478bd9Sstevel@tonic-gate db_getfd()
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	return (fileno(sd_push->s_out_file));
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate /*
4267c478bd9Sstevel@tonic-gate  *
4277c478bd9Sstevel@tonic-gate  *		db_printf
4287c478bd9Sstevel@tonic-gate  *
4297c478bd9Sstevel@tonic-gate  * Description:
4307c478bd9Sstevel@tonic-gate  *	Outputs the specified message if the keyword specified
4317c478bd9Sstevel@tonic-gate  *	by db_pargs() has been selected.  The line number specified
4327c478bd9Sstevel@tonic-gate  *	by db_pargs() is also used as the line number the db_printf()
4337c478bd9Sstevel@tonic-gate  *	occurs on.  The format string should NOT include a terminating
4347c478bd9Sstevel@tonic-gate  *	newline as one is supplied automatically.
4357c478bd9Sstevel@tonic-gate  * Arguments:
4367c478bd9Sstevel@tonic-gate  *	format - printf style printing control string
4377c478bd9Sstevel@tonic-gate  *	...    - additional arguments required by the control string
4387c478bd9Sstevel@tonic-gate  * Returns:
4397c478bd9Sstevel@tonic-gate  * Errors:
4407c478bd9Sstevel@tonic-gate  * Preconditions:
4417c478bd9Sstevel@tonic-gate  *	precond(format)
4427c478bd9Sstevel@tonic-gate  */
4437c478bd9Sstevel@tonic-gate void
db_printf(char * keyword,char * format,...)4447c478bd9Sstevel@tonic-gate db_printf(char *keyword, char *format, ...)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	dbug_object_t *dbug_object_p;
4477c478bd9Sstevel@tonic-gate 	thread_data_t *tdp;
4487c478bd9Sstevel@tonic-gate 	dbug_state_object_t *dbug_state_object_p = sd_push;
4497c478bd9Sstevel@tonic-gate 	va_list args;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	dbug_object_p = db_get_dbug_object_p();
4527c478bd9Sstevel@tonic-gate 	/* return if no debugging yet */
4537c478bd9Sstevel@tonic-gate 	if (NOT db_debugon())
4547c478bd9Sstevel@tonic-gate 		return;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	GET_THREAD_DATA_PTR(&tdp);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	/* return if keyword not selected */
4597c478bd9Sstevel@tonic-gate 	if (NOT db_keyword(dbug_object_p, tdp->td_keyword))
4607c478bd9Sstevel@tonic-gate 		return;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	LOCK_THREAD_DATA();
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	/* get a pointer to the active state */
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	va_start(args, format);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	doprefix(dbug_state_object_p, tdp->td_line, sd_lineno++,
4697c478bd9Sstevel@tonic-gate 		dbug_object_p->d_file, sd_process);
4707c478bd9Sstevel@tonic-gate 	if (dbug_state_object_p->sf_trace)
4717c478bd9Sstevel@tonic-gate 		indent(dbug_state_object_p, dbug_state_object_p->s_level +1);
4727c478bd9Sstevel@tonic-gate 	else
4737c478bd9Sstevel@tonic-gate 		fprintf(dbug_state_object_p->s_out_file, "%s: ",
4747c478bd9Sstevel@tonic-gate 		    dbug_object_p->d_func);
4757c478bd9Sstevel@tonic-gate 	if (tdp->td_keyword[0])
4767c478bd9Sstevel@tonic-gate 		fprintf(dbug_state_object_p->s_out_file, "%s: ",
4777c478bd9Sstevel@tonic-gate 		    tdp->td_keyword);
4787c478bd9Sstevel@tonic-gate 	vfprintf(dbug_state_object_p->s_out_file, format, args);
4797c478bd9Sstevel@tonic-gate 	fprintf(dbug_state_object_p->s_out_file, "\n");
4807c478bd9Sstevel@tonic-gate 	fflush(dbug_state_object_p->s_out_file);
4817c478bd9Sstevel@tonic-gate 	delay(dbug_state_object_p->s_delay);
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	va_end(args);
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	UNLOCK_THREAD_DATA();
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate /*
4897c478bd9Sstevel@tonic-gate  *
4907c478bd9Sstevel@tonic-gate  *		db_traceprint
4917c478bd9Sstevel@tonic-gate  *
4927c478bd9Sstevel@tonic-gate  * Description:
4937c478bd9Sstevel@tonic-gate  *	Prints out a trace of the call stack.
4947c478bd9Sstevel@tonic-gate  * Arguments:
4957c478bd9Sstevel@tonic-gate  *	line    - the line number where this call was made
4967c478bd9Sstevel@tonic-gate  *	keyword - keyword to test against
4977c478bd9Sstevel@tonic-gate  * Returns:
4987c478bd9Sstevel@tonic-gate  * Errors:
4997c478bd9Sstevel@tonic-gate  * Preconditions:
5007c478bd9Sstevel@tonic-gate  */
5017c478bd9Sstevel@tonic-gate void
db_traceprint(int line,const char * keyword)5027c478bd9Sstevel@tonic-gate db_traceprint(int line, const char *keyword)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	dbug_object_t *dbug_object_p;
5057c478bd9Sstevel@tonic-gate 	dbug_object_t *pdr;
5067c478bd9Sstevel@tonic-gate 	/* return if no debugging yet */
5077c478bd9Sstevel@tonic-gate 	if (NOT db_debugon())
5087c478bd9Sstevel@tonic-gate 		return;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	if ((dbug_object_p = db_get_dbug_object_p()) == NULL)
5117c478bd9Sstevel@tonic-gate 		doabort();
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	/* If the specified keyword is enabled */
5147c478bd9Sstevel@tonic-gate 	if (db_keyword(dbug_object_p, keyword)) {
5157c478bd9Sstevel@tonic-gate 		/* perform setup for using db_printf */
5167c478bd9Sstevel@tonic-gate 		db_pargs(dbug_object_p, line, NULL);
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 		/* Output a header message */
5197c478bd9Sstevel@tonic-gate 		db_printf(NULL, "Stack Trace");
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		/* walk the stack of dbug_routine objects */
5227c478bd9Sstevel@tonic-gate 		for (pdr = dbug_object_p; pdr != NULL; pdr = pdr->d_prev) {
5237c478bd9Sstevel@tonic-gate 			/* output the routine name */
5247c478bd9Sstevel@tonic-gate 			db_printf(NULL, "  %s() (%s)", pdr->d_func,
5257c478bd9Sstevel@tonic-gate 			    pdr->d_file);
5267c478bd9Sstevel@tonic-gate 		}
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate /*
5317c478bd9Sstevel@tonic-gate  *
5327c478bd9Sstevel@tonic-gate  *			db_assert
5337c478bd9Sstevel@tonic-gate  *
5347c478bd9Sstevel@tonic-gate  * Description:
5357c478bd9Sstevel@tonic-gate  *	Called when an assert fails.
5367c478bd9Sstevel@tonic-gate  *	Prints out a stack trace and aborts.
5377c478bd9Sstevel@tonic-gate  * Arguments:
5387c478bd9Sstevel@tonic-gate  *	line	line number assert occurred at
5397c478bd9Sstevel@tonic-gate  *	msgp	string form of assert code that failed
5407c478bd9Sstevel@tonic-gate  * Returns:
5417c478bd9Sstevel@tonic-gate  * Preconditions:
5427c478bd9Sstevel@tonic-gate  *	precond(msgp)
5437c478bd9Sstevel@tonic-gate  */
5447c478bd9Sstevel@tonic-gate void
db_assert(dbug_object_t * dbug_object_p,int line,const char * msgp)5457c478bd9Sstevel@tonic-gate db_assert(dbug_object_t *dbug_object_p, int line, const char *msgp)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	if (NOT db_debugon())
5487c478bd9Sstevel@tonic-gate 		db_push("-#:d");
5497c478bd9Sstevel@tonic-gate 	db_pargs(dbug_object_p, line, NULL);
5507c478bd9Sstevel@tonic-gate 	db_printf(NULL, "Assertion Failed %s:%s():%d \"%s\"",
5517c478bd9Sstevel@tonic-gate 	    dbug_object_p->d_file, dbug_object_p->d_func, line, msgp);
5527c478bd9Sstevel@tonic-gate 	db_traceprint(line, NULL);
5537c478bd9Sstevel@tonic-gate 	doabort();
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate  *
5587c478bd9Sstevel@tonic-gate  *			db_precond
5597c478bd9Sstevel@tonic-gate  *
5607c478bd9Sstevel@tonic-gate  * Description:
5617c478bd9Sstevel@tonic-gate  *	Called when an precond fails.
5627c478bd9Sstevel@tonic-gate  *	Prints out a stack trace and aborts.
5637c478bd9Sstevel@tonic-gate  * Arguments:
5647c478bd9Sstevel@tonic-gate  *	line	line number precond occurred at
5657c478bd9Sstevel@tonic-gate  *	msgp	string form of precond code that failed
5667c478bd9Sstevel@tonic-gate  * Returns:
5677c478bd9Sstevel@tonic-gate  * Preconditions:
5687c478bd9Sstevel@tonic-gate  *	precond(msgp)
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate void
db_precond(dbug_object_t * dbug_object_p,int line,const char * msgp)5717c478bd9Sstevel@tonic-gate db_precond(dbug_object_t *dbug_object_p, int line, const char *msgp)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	if (NOT db_debugon())
5747c478bd9Sstevel@tonic-gate 		db_push("-#:d");
5757c478bd9Sstevel@tonic-gate 	db_pargs(dbug_object_p, line, NULL);
5767c478bd9Sstevel@tonic-gate 	db_printf(NULL, "Precondition Failed %s:%s():%d \"%s\"",
5777c478bd9Sstevel@tonic-gate 	    dbug_object_p->d_file, dbug_object_p->d_func, line, msgp);
5787c478bd9Sstevel@tonic-gate 	db_traceprint(line, NULL);
5797c478bd9Sstevel@tonic-gate 	doabort();
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate /*
5837c478bd9Sstevel@tonic-gate  *
5847c478bd9Sstevel@tonic-gate  *		db_push
5857c478bd9Sstevel@tonic-gate  *
5867c478bd9Sstevel@tonic-gate  * Description:
5877c478bd9Sstevel@tonic-gate  *	Push current debugger state and set up a new one.
5887c478bd9Sstevel@tonic-gate  *	Returns NULL if no errors, an error string if there
5897c478bd9Sstevel@tonic-gate  *	is an error.
5907c478bd9Sstevel@tonic-gate  *
5917c478bd9Sstevel@tonic-gate  * format of control string
5927c478bd9Sstevel@tonic-gate  *   command[:command:...]
5937c478bd9Sstevel@tonic-gate  *
5947c478bd9Sstevel@tonic-gate  *   commands
5957c478bd9Sstevel@tonic-gate  *   debugging on	'd'  d[,<keyword>[,...]]
5967c478bd9Sstevel@tonic-gate  *   delay value	'D'  D[,<delay value>]
5977c478bd9Sstevel@tonic-gate  *   function list	'f'  f[,<function name>[,...]]
5987c478bd9Sstevel@tonic-gate  *   print filename	'F'  F
5997c478bd9Sstevel@tonic-gate  *   print pid		'i'  i
6007c478bd9Sstevel@tonic-gate  *   print line number	'L'  L
6017c478bd9Sstevel@tonic-gate  *   print call depth	'n'  n
6027c478bd9Sstevel@tonic-gate  *   number each line	'N'  N
6037c478bd9Sstevel@tonic-gate  *   output file	'o'  o[,<filename>
6047c478bd9Sstevel@tonic-gate  *   process name list	'p'  p[,<process name>[,...]]
6057c478bd9Sstevel@tonic-gate  *   print proc name	'P'  P
6067c478bd9Sstevel@tonic-gate  *   reset indentation	'r'  r
6077c478bd9Sstevel@tonic-gate  *   print runtime	'R'  R
6087c478bd9Sstevel@tonic-gate  *   print thread info	'T'  T
6097c478bd9Sstevel@tonic-gate  *   print trace	't'  t
6107c478bd9Sstevel@tonic-gate  *   print stack depth	's'  s
6117c478bd9Sstevel@tonic-gate  */
6127c478bd9Sstevel@tonic-gate char *
db_push(const char * control)6137c478bd9Sstevel@tonic-gate db_push(const char *control)
6147c478bd9Sstevel@tonic-gate {
6157c478bd9Sstevel@tonic-gate 	char *dupcontrol = NULL;
6167c478bd9Sstevel@tonic-gate 	dbug_state_object_t *dbug_state_object_p;
6177c478bd9Sstevel@tonic-gate 	flist_object_t *flist_object_p;
6187c478bd9Sstevel@tonic-gate 	register char *scan;
6197c478bd9Sstevel@tonic-gate 	int retval;
6207c478bd9Sstevel@tonic-gate 	char res[100];
6217c478bd9Sstevel@tonic-gate 	int level;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	LOCK_THREAD_DATA();
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/* error if the control string is NULL */
6267c478bd9Sstevel@tonic-gate 	if (control == NULL) {
6277c478bd9Sstevel@tonic-gate 		strcpy(res, "mdbug: control string is NULL");
6287c478bd9Sstevel@tonic-gate 		goto out;
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	/* turn debugging flag off */
6327c478bd9Sstevel@tonic-gate 	sd_on = FALSE;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	/* get the level from the old state if it exists */
6357c478bd9Sstevel@tonic-gate 	if (sd_push == NULL)
6367c478bd9Sstevel@tonic-gate 		level = 0;
6377c478bd9Sstevel@tonic-gate 	else
6387c478bd9Sstevel@tonic-gate 		level = sd_push->s_level;
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	/* Create a new state */
6417c478bd9Sstevel@tonic-gate 	dbug_state_object_p = dbug_state_create(level);
6427c478bd9Sstevel@tonic-gate 	if (dbug_state_object_p == NULL) {
6437c478bd9Sstevel@tonic-gate 		strcpy(res, "mdbug: out of memory, dbug_state_create");
6447c478bd9Sstevel@tonic-gate 		goto out;
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	/* add it to our list of states and make it the current one */
6487c478bd9Sstevel@tonic-gate 	dbug_state_object_p->s_next = sd_push;
6497c478bd9Sstevel@tonic-gate 	sd_push = dbug_state_object_p;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	/* Strip off -# if in the control string */
6527c478bd9Sstevel@tonic-gate 	if ((*control == '-') && (*(control+1) == '#'))
6537c478bd9Sstevel@tonic-gate 		control += 2;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	/* make a copy of the control string so we can modify it with strtok */
6567c478bd9Sstevel@tonic-gate 	dupcontrol = strdup(control);
6577c478bd9Sstevel@tonic-gate 	if (dupcontrol == NULL) {
6587c478bd9Sstevel@tonic-gate 		strcpy(res, "mdbug: out of memory, strdup");
6597c478bd9Sstevel@tonic-gate 		goto out;
6607c478bd9Sstevel@tonic-gate 	}
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	/* parse the control string */
6637c478bd9Sstevel@tonic-gate 	for (scan = mystrtok(dupcontrol, ":");
6647c478bd9Sstevel@tonic-gate 	    scan != NULL;
6657c478bd9Sstevel@tonic-gate 	    scan = mystrtok(NULL, ":")) {
6667c478bd9Sstevel@tonic-gate 		switch (*scan++) {
6677c478bd9Sstevel@tonic-gate 		case 'd':			/* debugging on */
6687c478bd9Sstevel@tonic-gate 			sd_on = TRUE;
6697c478bd9Sstevel@tonic-gate 			dbug_state_object_p->sf_debug = TRUE;
6707c478bd9Sstevel@tonic-gate 			if (*scan++ == ',') {
6717c478bd9Sstevel@tonic-gate 				retval = listparse(scan,
6727c478bd9Sstevel@tonic-gate 				    dbug_state_object_p->s_keywords);
6737c478bd9Sstevel@tonic-gate 				if (retval < 0) {
6747c478bd9Sstevel@tonic-gate 					strcpy(res,
6757c478bd9Sstevel@tonic-gate 					    "mdbug: -d too many keywords");
6767c478bd9Sstevel@tonic-gate 					goto out;
6777c478bd9Sstevel@tonic-gate 				}
6787c478bd9Sstevel@tonic-gate 			}
6797c478bd9Sstevel@tonic-gate 			break;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 		case 'D': 			/* specify delay value */
6827c478bd9Sstevel@tonic-gate 			dbug_state_object_p->s_delay = 0;
6837c478bd9Sstevel@tonic-gate 			if (*scan++ == ',') {
6847c478bd9Sstevel@tonic-gate 				flist_object_p = flist_create();
6857c478bd9Sstevel@tonic-gate 				retval = listparse(scan, flist_object_p);
6867c478bd9Sstevel@tonic-gate 				if (retval < 0) {
6877c478bd9Sstevel@tonic-gate 					strcpy(res,
6887c478bd9Sstevel@tonic-gate 					    "mdbug: -D too many delays");
6897c478bd9Sstevel@tonic-gate 					goto out;
6907c478bd9Sstevel@tonic-gate 				}
6917c478bd9Sstevel@tonic-gate 				if (flist_object_p->f_count > 0) {
6927c478bd9Sstevel@tonic-gate 					dbug_state_object_p->s_delay =
6937c478bd9Sstevel@tonic-gate 					    delayarg(atoi(
6947c478bd9Sstevel@tonic-gate 					    (char *)fl_top(flist_object_p)));
6957c478bd9Sstevel@tonic-gate 				}
6967c478bd9Sstevel@tonic-gate 				flist_destroy(flist_object_p);
6977c478bd9Sstevel@tonic-gate 			}
6987c478bd9Sstevel@tonic-gate 			break;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		case 'f': 			/* list of functions to watch */
7017c478bd9Sstevel@tonic-gate 			if (*scan++ == ',') {
7027c478bd9Sstevel@tonic-gate 				retval = listparse(scan,
7037c478bd9Sstevel@tonic-gate 				    dbug_state_object_p->s_functions);
7047c478bd9Sstevel@tonic-gate 				if (retval < 0) {
7057c478bd9Sstevel@tonic-gate 					strcpy(res,
7067c478bd9Sstevel@tonic-gate 					    "mdbug: -f too many functions");
7077c478bd9Sstevel@tonic-gate 					goto out;
7087c478bd9Sstevel@tonic-gate 				}
7097c478bd9Sstevel@tonic-gate 			}
7107c478bd9Sstevel@tonic-gate 			break;
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 		case 'F': 		/* print file name with dbug output */
7137c478bd9Sstevel@tonic-gate 			dbug_state_object_p->sf_file = TRUE;
7147c478bd9Sstevel@tonic-gate 			break;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 		case 'i': 		/* print pid with dbug output */
7177c478bd9Sstevel@tonic-gate 			dbug_state_object_p->sf_pid = TRUE;
7187c478bd9Sstevel@tonic-gate 			break;
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 		case 'L':		/* print line nums with dbug output */
7217c478bd9Sstevel@tonic-gate 			dbug_state_object_p->sf_line = TRUE;
7227c478bd9Sstevel@tonic-gate 			break;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 		case 'n': 		/* print function call depth */
7257c478bd9Sstevel@tonic-gate 			dbug_state_object_p->sf_depth = TRUE;
7267c478bd9Sstevel@tonic-gate 			break;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 		case 'N': 		/* number each line of dbug output */
7297c478bd9Sstevel@tonic-gate 			dbug_state_object_p->sf_number = TRUE;
7307c478bd9Sstevel@tonic-gate 			break;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 		case 'o': 		/* specifies output file for dbug */
7337c478bd9Sstevel@tonic-gate 			if (*scan++ == ',') {
7347c478bd9Sstevel@tonic-gate 				flist_object_p = flist_create();
7357c478bd9Sstevel@tonic-gate 				retval = listparse(scan, flist_object_p);
7367c478bd9Sstevel@tonic-gate 				if (retval < 0) {
7377c478bd9Sstevel@tonic-gate 					strcpy(res,
7387c478bd9Sstevel@tonic-gate 					    "mdbug: -o too many output files");
7397c478bd9Sstevel@tonic-gate 					goto out;
7407c478bd9Sstevel@tonic-gate 				}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 				if (flist_object_p->f_count > 0) {
7437c478bd9Sstevel@tonic-gate 					dbug_state_object_p->s_out_file =
7447c478bd9Sstevel@tonic-gate 					    openfile((char *)
7457c478bd9Sstevel@tonic-gate 					    fl_top(flist_object_p));
7467c478bd9Sstevel@tonic-gate 					if (dbug_state_object_p->s_out_file !=
7477c478bd9Sstevel@tonic-gate 					    NULL)
7487c478bd9Sstevel@tonic-gate 						dbug_state_object_p->sf_didopen
7497c478bd9Sstevel@tonic-gate 						    = 1;
7507c478bd9Sstevel@tonic-gate 				} else
7517c478bd9Sstevel@tonic-gate 					dbug_state_object_p->s_out_file =
7527c478bd9Sstevel@tonic-gate 					    openfile(NULL);
7537c478bd9Sstevel@tonic-gate 				flist_destroy(flist_object_p);
7547c478bd9Sstevel@tonic-gate 			} else
7557c478bd9Sstevel@tonic-gate 				dbug_state_object_p->s_out_file =
7567c478bd9Sstevel@tonic-gate 				    openfile(NULL);
7577c478bd9Sstevel@tonic-gate 			if (dbug_state_object_p->s_out_file == NULL) {
7587c478bd9Sstevel@tonic-gate 				strcpy(res,
7597c478bd9Sstevel@tonic-gate 				    "mdbug: -o cannot open output file");
7607c478bd9Sstevel@tonic-gate 				goto out;
7617c478bd9Sstevel@tonic-gate 			}
7627c478bd9Sstevel@tonic-gate 			break;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		case 'p':			/* debug specified processes */
7657c478bd9Sstevel@tonic-gate 			if (*scan++ == ',') {
7667c478bd9Sstevel@tonic-gate 				retval = listparse(scan,
7677c478bd9Sstevel@tonic-gate 				    dbug_state_object_p->s_processes);
7687c478bd9Sstevel@tonic-gate 				if (retval < 0) {
7697c478bd9Sstevel@tonic-gate 					strcpy(res,
7707c478bd9Sstevel@tonic-gate 					    "mdbug: -p too many processes");
7717c478bd9Sstevel@tonic-gate 					goto out;
7727c478bd9Sstevel@tonic-gate 				}
7737c478bd9Sstevel@tonic-gate 			}
7747c478bd9Sstevel@tonic-gate 			break;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		case 'P': 		/* print process name on dbug output */
7777c478bd9Sstevel@tonic-gate 			dbug_state_object_p->sf_process = TRUE;
7787c478bd9Sstevel@tonic-gate 			break;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 		case 'r': 			/* reset indentation to zero */
7817c478bd9Sstevel@tonic-gate 			dbug_state_object_p->s_level = 0;
7827c478bd9Sstevel@tonic-gate 			break;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		case 's': 			/* print stack depth on enter */
7857c478bd9Sstevel@tonic-gate 			dbug_state_object_p->sf_stack = TRUE;
7867c478bd9Sstevel@tonic-gate 			break;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 		case 'R':		/* print time prog has been running */
7897c478bd9Sstevel@tonic-gate 			dbug_state_object_p->sf_time = TRUE;
7907c478bd9Sstevel@tonic-gate 			time(&dbug_state_object_p->s_starttime);
7917c478bd9Sstevel@tonic-gate 			break;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 		case 'T':		/* print thread information */
7947c478bd9Sstevel@tonic-gate 			dbug_state_object_p->sf_thread = TRUE;
7957c478bd9Sstevel@tonic-gate 			break;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 		case 't': 		/* print trace of functions called */
7987c478bd9Sstevel@tonic-gate 			dbug_state_object_p->sf_trace = TRUE;
7997c478bd9Sstevel@tonic-gate 			dbug_state_object_p->s_maxdepth = MAXDEPTH;
8007c478bd9Sstevel@tonic-gate 			if (*scan++ == ',') {
8017c478bd9Sstevel@tonic-gate 				flist_object_p = flist_create();
8027c478bd9Sstevel@tonic-gate 				retval = listparse(scan, flist_object_p);
8037c478bd9Sstevel@tonic-gate 				if (retval < 0) {
8047c478bd9Sstevel@tonic-gate 					strcpy(res,
8057c478bd9Sstevel@tonic-gate 					    "mdbug: -t too many traces");
8067c478bd9Sstevel@tonic-gate 					goto out;
8077c478bd9Sstevel@tonic-gate 				}
8087c478bd9Sstevel@tonic-gate 				if (flist_object_p->f_count > 0) {
8097c478bd9Sstevel@tonic-gate 					dbug_state_object_p->s_maxdepth =
8107c478bd9Sstevel@tonic-gate 					    atoi((char *)
8117c478bd9Sstevel@tonic-gate 					    fl_top(flist_object_p));
8127c478bd9Sstevel@tonic-gate 				}
8137c478bd9Sstevel@tonic-gate 				flist_destroy(flist_object_p);
8147c478bd9Sstevel@tonic-gate 			}
8157c478bd9Sstevel@tonic-gate 			break;
8167c478bd9Sstevel@tonic-gate 		}
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate out:
8207c478bd9Sstevel@tonic-gate 	/* free up the dupped control string */
8217c478bd9Sstevel@tonic-gate 	free(dupcontrol);
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	UNLOCK_THREAD_DATA();
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	/* return result */
8267c478bd9Sstevel@tonic-gate 	return (NULL);
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate /*
8307c478bd9Sstevel@tonic-gate  *
8317c478bd9Sstevel@tonic-gate  *		db_pop
8327c478bd9Sstevel@tonic-gate  *
8337c478bd9Sstevel@tonic-gate  * Description:
8347c478bd9Sstevel@tonic-gate  *	Pop the debug stack.
8357c478bd9Sstevel@tonic-gate  */
8367c478bd9Sstevel@tonic-gate void
db_pop()8377c478bd9Sstevel@tonic-gate db_pop()
8387c478bd9Sstevel@tonic-gate {
8397c478bd9Sstevel@tonic-gate 	dbug_state_object_t *dbug_state_object_p;
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	LOCK_THREAD_DATA();
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	/* return if no debugging yet */
8447c478bd9Sstevel@tonic-gate 	if (sd_push == NULL)
8457c478bd9Sstevel@tonic-gate 		goto out;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	/* get and remove the top item from the list */
8487c478bd9Sstevel@tonic-gate 	dbug_state_object_p = sd_push;
8497c478bd9Sstevel@tonic-gate 	sd_push = dbug_state_object_p->s_next;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	/* Delete the item. */
8527c478bd9Sstevel@tonic-gate 	dbug_state_destroy(dbug_state_object_p);
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	/* get the current top of the stack */
8557c478bd9Sstevel@tonic-gate 	dbug_state_object_p = sd_push;
8567c478bd9Sstevel@tonic-gate 	if (dbug_state_object_p) {
8577c478bd9Sstevel@tonic-gate 		/* See if debugging is turned on */
8587c478bd9Sstevel@tonic-gate 		if (dbug_state_object_p->sf_debug)
8597c478bd9Sstevel@tonic-gate 			sd_on = TRUE;
8607c478bd9Sstevel@tonic-gate 		else
8617c478bd9Sstevel@tonic-gate 			sd_on = FALSE;
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate out:;
8657c478bd9Sstevel@tonic-gate 	UNLOCK_THREAD_DATA();
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate /*
8697c478bd9Sstevel@tonic-gate  *
8707c478bd9Sstevel@tonic-gate  *			db_process
8717c478bd9Sstevel@tonic-gate  *
8727c478bd9Sstevel@tonic-gate  * Description:
8737c478bd9Sstevel@tonic-gate  *	Specifies the name of the process.
8747c478bd9Sstevel@tonic-gate  *	Only the pointer is saved, the string is not copied.
8757c478bd9Sstevel@tonic-gate  * Arguments:
8767c478bd9Sstevel@tonic-gate  *	namep
8777c478bd9Sstevel@tonic-gate  * Returns:
8787c478bd9Sstevel@tonic-gate  * Preconditions:
8797c478bd9Sstevel@tonic-gate  */
8807c478bd9Sstevel@tonic-gate void
db_process(const char * namep)8817c478bd9Sstevel@tonic-gate db_process(const char *namep)
8827c478bd9Sstevel@tonic-gate {
8837c478bd9Sstevel@tonic-gate 	thread_data_t *tdp;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	strcpy(sd_process, namep);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate #ifdef STACKINIT
8887c478bd9Sstevel@tonic-gate 	GET_THREAD_DATA_PTR(&tdp);
889*47644099Sgt29601 	tdp->td_stackinit = (ulong_t)this;
8907c478bd9Sstevel@tonic-gate #endif
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate /*
8947c478bd9Sstevel@tonic-gate  *
8957c478bd9Sstevel@tonic-gate  *			listparse
8967c478bd9Sstevel@tonic-gate  *
8977c478bd9Sstevel@tonic-gate  * Description:
8987c478bd9Sstevel@tonic-gate  *	parse list of modifiers in debug control string
8997c478bd9Sstevel@tonic-gate  *
9007c478bd9Sstevel@tonic-gate  *	Given pointer to a comma separated list of strings in "cltp",
9017c478bd9Sstevel@tonic-gate  *	parses the list, building a list and returning a pointer to it.
9027c478bd9Sstevel@tonic-gate  *	The original comma separated list is destroyed in the process of
9037c478bd9Sstevel@tonic-gate  *	building the linked list, thus it had better be a duplicate
9047c478bd9Sstevel@tonic-gate  *	if it is important.
9057c478bd9Sstevel@tonic-gate  *
9067c478bd9Sstevel@tonic-gate  *	This routine is only called from db_push.
9077c478bd9Sstevel@tonic-gate  *	Returns 0 for success, -1 for failure.
9087c478bd9Sstevel@tonic-gate  */
9097c478bd9Sstevel@tonic-gate static int
listparse(register char * ctlp,flist_object_t * head)9107c478bd9Sstevel@tonic-gate listparse(register char *ctlp, flist_object_t *head)
9117c478bd9Sstevel@tonic-gate {
9127c478bd9Sstevel@tonic-gate 	char *start;
9137c478bd9Sstevel@tonic-gate 	char *item;
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	/* scan the string until end */
9167c478bd9Sstevel@tonic-gate 	while (*ctlp != '\0') {
9177c478bd9Sstevel@tonic-gate 		/* See if no more room on the list */
9187c478bd9Sstevel@tonic-gate 		if (fl_space(head) == 0)
9197c478bd9Sstevel@tonic-gate 			return (-1);
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 		/* save the begining of this section */
9227c478bd9Sstevel@tonic-gate 		start = ctlp;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 		/* loop until the end of the token is found */
9257c478bd9Sstevel@tonic-gate 		while ((*ctlp != '\0') && (*ctlp != ','))
9267c478bd9Sstevel@tonic-gate 			ctlp++;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 		/* add a string terminator if necessary, for strdup */
9297c478bd9Sstevel@tonic-gate 		if (*ctlp == ',')
9307c478bd9Sstevel@tonic-gate 			*ctlp++ = '\0';
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 		/* make a copy of the string */
9337c478bd9Sstevel@tonic-gate 		item = strdup(start);
9347c478bd9Sstevel@tonic-gate 		if (item == NULL)
9357c478bd9Sstevel@tonic-gate 			return (-1);
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 		/* add it to the list */
9387c478bd9Sstevel@tonic-gate 		fl_push(head, item);
9397c478bd9Sstevel@tonic-gate 	}
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	return (0);
9427c478bd9Sstevel@tonic-gate }
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate /*
9457c478bd9Sstevel@tonic-gate  *
9467c478bd9Sstevel@tonic-gate  *			inlist
9477c478bd9Sstevel@tonic-gate  *
9487c478bd9Sstevel@tonic-gate  * Description:
9497c478bd9Sstevel@tonic-gate  *	Tests the string pointed to by "cp" to determine if it is in
9507c478bd9Sstevel@tonic-gate  *	the list pointed to by "flist_object_p".  Linkp points to the first
9517c478bd9Sstevel@tonic-gate  *	link in the list.  If flist_object_p is empty then the string is treated
9527c478bd9Sstevel@tonic-gate  *	as if it is in the list (I.E all strings are in the null list).
9537c478bd9Sstevel@tonic-gate  *	This may seem rather strange at first but leads to the desired
9547c478bd9Sstevel@tonic-gate  *	operation if no list is given.  The net effect is that all
9557c478bd9Sstevel@tonic-gate  *	strings will be accepted when there is no list, and when there
9567c478bd9Sstevel@tonic-gate  *	is a list, only those strings in the list will be accepted.
9577c478bd9Sstevel@tonic-gate  */
9587c478bd9Sstevel@tonic-gate static boolean
inlist(flist_object_t * flist_object_p,const char * cp)9597c478bd9Sstevel@tonic-gate inlist(flist_object_t *flist_object_p, const char *cp)
9607c478bd9Sstevel@tonic-gate {
9617c478bd9Sstevel@tonic-gate 	register boolean accept;
9627c478bd9Sstevel@tonic-gate 	register char *item;
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	if ((flist_object_p == NULL) || (flist_object_p->f_count == 0) ||
9657c478bd9Sstevel@tonic-gate 		(cp == NULL))
9667c478bd9Sstevel@tonic-gate 		accept = TRUE;
9677c478bd9Sstevel@tonic-gate 	else {
9687c478bd9Sstevel@tonic-gate 		accept = FALSE;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 		/* walk the list of items */
9717c478bd9Sstevel@tonic-gate 		for (item = (char *)fl_top(flist_object_p);
9727c478bd9Sstevel@tonic-gate 		    item != NULL;
9737c478bd9Sstevel@tonic-gate 		    item = (char *)fl_next(flist_object_p)) {
9747c478bd9Sstevel@tonic-gate 			/* see if a match */
9757c478bd9Sstevel@tonic-gate 			if (strcmp(item, cp) == 0) {
9767c478bd9Sstevel@tonic-gate 				accept = TRUE;
9777c478bd9Sstevel@tonic-gate 				break;
9787c478bd9Sstevel@tonic-gate 			}
9797c478bd9Sstevel@tonic-gate 		}
9807c478bd9Sstevel@tonic-gate 	}
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	return (accept);
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate /*
9867c478bd9Sstevel@tonic-gate  *
9877c478bd9Sstevel@tonic-gate  *			dotrace
9887c478bd9Sstevel@tonic-gate  *
9897c478bd9Sstevel@tonic-gate  * Description:
9907c478bd9Sstevel@tonic-gate  *	Checks to see if tracing is enabled based on whether the
9917c478bd9Sstevel@tonic-gate  *	user has specified tracing, the maximum trace depth has
9927c478bd9Sstevel@tonic-gate  *	not yet been reached, the current function is selected,
9937c478bd9Sstevel@tonic-gate  *	and the current process is selected.  Returns TRUE if
9947c478bd9Sstevel@tonic-gate  *	tracing is enabled, FALSE otherwise.
9957c478bd9Sstevel@tonic-gate  */
9967c478bd9Sstevel@tonic-gate static boolean
dotrace(dbug_state_object_t * dbug_state_object_p,const char * func,const char * process)9977c478bd9Sstevel@tonic-gate dotrace(dbug_state_object_t *dbug_state_object_p, const char *func,
9987c478bd9Sstevel@tonic-gate     const char *process)
9997c478bd9Sstevel@tonic-gate {
10007c478bd9Sstevel@tonic-gate 	boolean trace;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	trace = FALSE;
10037c478bd9Sstevel@tonic-gate 	if (dbug_state_object_p->sf_trace) {
10047c478bd9Sstevel@tonic-gate 		if (dbug_state_object_p->s_level <=
10057c478bd9Sstevel@tonic-gate 		    dbug_state_object_p->s_maxdepth) {
10067c478bd9Sstevel@tonic-gate 			if (inlist(dbug_state_object_p->s_functions, func)) {
10077c478bd9Sstevel@tonic-gate 				if (inlist(dbug_state_object_p->s_processes,
10087c478bd9Sstevel@tonic-gate 				    process)) {
10097c478bd9Sstevel@tonic-gate 					trace = TRUE;
10107c478bd9Sstevel@tonic-gate 				}
10117c478bd9Sstevel@tonic-gate 			}
10127c478bd9Sstevel@tonic-gate 		}
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	return (trace);
10167c478bd9Sstevel@tonic-gate }
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate /*
10197c478bd9Sstevel@tonic-gate  *
10207c478bd9Sstevel@tonic-gate  *			indent
10217c478bd9Sstevel@tonic-gate  *
10227c478bd9Sstevel@tonic-gate  * Description:
10237c478bd9Sstevel@tonic-gate  *	Indent a line to the given level.  Note that this is
10247c478bd9Sstevel@tonic-gate  *	a simple minded but portable implementation.
10257c478bd9Sstevel@tonic-gate  *	There are better ways.
10267c478bd9Sstevel@tonic-gate  *
10277c478bd9Sstevel@tonic-gate  *	Also, the indent must be scaled by the compile time option
10287c478bd9Sstevel@tonic-gate  *	of character positions per nesting level.
10297c478bd9Sstevel@tonic-gate  */
10307c478bd9Sstevel@tonic-gate static void
indent(register dbug_state_object_t * dbug_state_object_p,int indent)10317c478bd9Sstevel@tonic-gate indent(register dbug_state_object_t *dbug_state_object_p, int indent)
10327c478bd9Sstevel@tonic-gate {
10337c478bd9Sstevel@tonic-gate 	register int count;
10347c478bd9Sstevel@tonic-gate 	char buffer[PRINTBUF];
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	indent *= INDENT;
10377c478bd9Sstevel@tonic-gate 	for (count = 0;
10387c478bd9Sstevel@tonic-gate 	    (count < (indent - INDENT)) && (count < (PRINTBUF - 1));
10397c478bd9Sstevel@tonic-gate 	    count++) {
10407c478bd9Sstevel@tonic-gate 		if ((count % INDENT) == 0)
10417c478bd9Sstevel@tonic-gate 			buffer[count] = '|';
10427c478bd9Sstevel@tonic-gate 		else
10437c478bd9Sstevel@tonic-gate 			buffer[count] = ' ';
10447c478bd9Sstevel@tonic-gate 	}
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	buffer[count] = '\0';
10477c478bd9Sstevel@tonic-gate 	fprintf(dbug_state_object_p->s_out_file, buffer);
10487c478bd9Sstevel@tonic-gate 	fflush(dbug_state_object_p->s_out_file);
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate /*
10527c478bd9Sstevel@tonic-gate  *
10537c478bd9Sstevel@tonic-gate  *			doprefix
10547c478bd9Sstevel@tonic-gate  *
10557c478bd9Sstevel@tonic-gate  * Description:
10567c478bd9Sstevel@tonic-gate  *	Print prefix common to all debugger output lines, prior to
10577c478bd9Sstevel@tonic-gate  *	doing indentation if necessary.  Print such information as
10587c478bd9Sstevel@tonic-gate  *	current process name, current source file name and line number,
10597c478bd9Sstevel@tonic-gate  *	and current function nesting depth.
10607c478bd9Sstevel@tonic-gate  */
10617c478bd9Sstevel@tonic-gate static void
doprefix(dbug_state_object_t * dbug_state_object_p,int line,long lineno,const char * file,const char * process)10627c478bd9Sstevel@tonic-gate doprefix(dbug_state_object_t *dbug_state_object_p, int line, long lineno,
10637c478bd9Sstevel@tonic-gate 	const char *file, const char *process)
10647c478bd9Sstevel@tonic-gate {
10657c478bd9Sstevel@tonic-gate #if DBUG_UNIX
10667c478bd9Sstevel@tonic-gate 	if (dbug_state_object_p->sf_pid)
1067*47644099Sgt29601 		fprintf(dbug_state_object_p->s_out_file, "%5d: ",
1068*47644099Sgt29601 		    (int)getpid());
10697c478bd9Sstevel@tonic-gate #endif
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	if (dbug_state_object_p->sf_thread)
1072*47644099Sgt29601 		fprintf(dbug_state_object_p->s_out_file, "%5ld: ",
1073*47644099Sgt29601 		    (long)thr_self());
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	if (dbug_state_object_p->sf_number)
10767c478bd9Sstevel@tonic-gate 		fprintf(dbug_state_object_p->s_out_file, "%5ld: ", lineno);
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	if (dbug_state_object_p->sf_process && process)
10797c478bd9Sstevel@tonic-gate 		fprintf(dbug_state_object_p->s_out_file, "%s: ", process);
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	if (dbug_state_object_p->sf_file)
10827c478bd9Sstevel@tonic-gate 		fprintf(dbug_state_object_p->s_out_file, "%14s: ", file);
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	if (dbug_state_object_p->sf_line)
10857c478bd9Sstevel@tonic-gate 		fprintf(dbug_state_object_p->s_out_file, "%5d: ", line);
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	if (dbug_state_object_p->sf_depth)
10887c478bd9Sstevel@tonic-gate 		fprintf(dbug_state_object_p->s_out_file, "%4d: ",
10897c478bd9Sstevel@tonic-gate 		dbug_state_object_p->s_level);
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	fflush(dbug_state_object_p->s_out_file);
10927c478bd9Sstevel@tonic-gate }
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate /*
10957c478bd9Sstevel@tonic-gate  *
10967c478bd9Sstevel@tonic-gate  *			openfile
10977c478bd9Sstevel@tonic-gate  *
10987c478bd9Sstevel@tonic-gate  * Description:
10997c478bd9Sstevel@tonic-gate  *	Given name of a new file (or NULL for stdout) opens the file
11007c478bd9Sstevel@tonic-gate  *	and sets the output stream to the new file.
11017c478bd9Sstevel@tonic-gate  */
11027c478bd9Sstevel@tonic-gate static FILE *
openfile(char * name)11037c478bd9Sstevel@tonic-gate openfile(char *name)
11047c478bd9Sstevel@tonic-gate {
11057c478bd9Sstevel@tonic-gate 	FILE *fp;
11067c478bd9Sstevel@tonic-gate 	boolean newfile;
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	if (name == NULL)
11097c478bd9Sstevel@tonic-gate 		return (stdout);
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	if (NOT writable(name))
11127c478bd9Sstevel@tonic-gate 		return (NULL);
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	/* see if the file already exists */
11157c478bd9Sstevel@tonic-gate 	if (file_exists(name))
11167c478bd9Sstevel@tonic-gate 		newfile = FALSE;
11177c478bd9Sstevel@tonic-gate 	else
11187c478bd9Sstevel@tonic-gate 		newfile = TRUE;
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	/* open the file */
11217c478bd9Sstevel@tonic-gate 	fp = fopen(name, "a+");
11227c478bd9Sstevel@tonic-gate 	if (fp == NULL)
11237c478bd9Sstevel@tonic-gate 		return (NULL);
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	/*
11267c478bd9Sstevel@tonic-gate 	 * If the file is newly created, give it away to the user
11277c478bd9Sstevel@tonic-gate 	 * that started the program.
11287c478bd9Sstevel@tonic-gate 	 */
11297c478bd9Sstevel@tonic-gate 	if (newfile) {
11307c478bd9Sstevel@tonic-gate 		changeowner(name);
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 	return (fp);
11337c478bd9Sstevel@tonic-gate }
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate /*
11367c478bd9Sstevel@tonic-gate  *
11377c478bd9Sstevel@tonic-gate  *			writable
11387c478bd9Sstevel@tonic-gate  *
11397c478bd9Sstevel@tonic-gate  * Description:
11407c478bd9Sstevel@tonic-gate  *	Because the debugger might be linked in with a program that
11417c478bd9Sstevel@tonic-gate  *	runs with the set-uid-bit (suid) set, we have to be careful
11427c478bd9Sstevel@tonic-gate  *	about opening a user named file for debug output.  This consists
11437c478bd9Sstevel@tonic-gate  *	of checking the file for write access with the real user id,
11447c478bd9Sstevel@tonic-gate  *	or checking the directory where the file will be created.
11457c478bd9Sstevel@tonic-gate  *
11467c478bd9Sstevel@tonic-gate  *	Returns TRUE if the user would normally be allowed write or
11477c478bd9Sstevel@tonic-gate  *	create access to the named file.  Returns FALSE otherwise.
11487c478bd9Sstevel@tonic-gate  */
11497c478bd9Sstevel@tonic-gate static boolean
writable(char * pathname)11507c478bd9Sstevel@tonic-gate writable(char *pathname)
11517c478bd9Sstevel@tonic-gate {
11527c478bd9Sstevel@tonic-gate #if DBUG_UNIX
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	char *lastslash;
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	boolean granted = FALSE;
11577c478bd9Sstevel@tonic-gate 	if (file_exists(pathname)) {
11587c478bd9Sstevel@tonic-gate 		if (file_writable(pathname)) {
11597c478bd9Sstevel@tonic-gate 			granted = TRUE;
11607c478bd9Sstevel@tonic-gate 		}
11617c478bd9Sstevel@tonic-gate 	} else {
11627c478bd9Sstevel@tonic-gate 		lastslash = strrchr(pathname, '/');
11637c478bd9Sstevel@tonic-gate 		if (lastslash != NULL) {
11647c478bd9Sstevel@tonic-gate 			*lastslash = '\0';
11657c478bd9Sstevel@tonic-gate 		} else {
11667c478bd9Sstevel@tonic-gate 			pathname = ".";
11677c478bd9Sstevel@tonic-gate 		}
11687c478bd9Sstevel@tonic-gate 		if (file_writable(pathname)) {
11697c478bd9Sstevel@tonic-gate 			granted = TRUE;
11707c478bd9Sstevel@tonic-gate 		}
11717c478bd9Sstevel@tonic-gate 		if (lastslash != NULL) {
11727c478bd9Sstevel@tonic-gate 			*lastslash = '/';
11737c478bd9Sstevel@tonic-gate 		}
11747c478bd9Sstevel@tonic-gate 	}
11757c478bd9Sstevel@tonic-gate 	return (granted);
11767c478bd9Sstevel@tonic-gate #else
11777c478bd9Sstevel@tonic-gate 	return (TRUE);
11787c478bd9Sstevel@tonic-gate #endif
11797c478bd9Sstevel@tonic-gate }
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate /*
11827c478bd9Sstevel@tonic-gate  *
11837c478bd9Sstevel@tonic-gate  *			changeowner
11847c478bd9Sstevel@tonic-gate  *
11857c478bd9Sstevel@tonic-gate  * Description:
11867c478bd9Sstevel@tonic-gate  *	For unix systems, change the owner of the newly created debug
11877c478bd9Sstevel@tonic-gate  *	file to the real owner.  This is strictly for the benefit of
11887c478bd9Sstevel@tonic-gate  *	programs that are running with the set-user-id bit set.
11897c478bd9Sstevel@tonic-gate  *
11907c478bd9Sstevel@tonic-gate  *	Note that at this point, the fact that pathname represents
11917c478bd9Sstevel@tonic-gate  *	a newly created file has already been established.  If the
11927c478bd9Sstevel@tonic-gate  *	program that the debugger is linked to is not running with
11937c478bd9Sstevel@tonic-gate  *	the suid bit set, then this operation is redundant (but
11947c478bd9Sstevel@tonic-gate  *	harmless).
11957c478bd9Sstevel@tonic-gate  */
11967c478bd9Sstevel@tonic-gate static void
changeowner(char * pathname)11977c478bd9Sstevel@tonic-gate changeowner(char *pathname)
11987c478bd9Sstevel@tonic-gate {
11997c478bd9Sstevel@tonic-gate #if DBUG_UNIX
12007c478bd9Sstevel@tonic-gate 	chown(pathname, getuid(), getgid());
12017c478bd9Sstevel@tonic-gate #endif
12027c478bd9Sstevel@tonic-gate }
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate /*
12057c478bd9Sstevel@tonic-gate  *
12067c478bd9Sstevel@tonic-gate  *			delayarg
12077c478bd9Sstevel@tonic-gate  *
12087c478bd9Sstevel@tonic-gate  * Description:
12097c478bd9Sstevel@tonic-gate  *	Converts delay argument, given in tenths of a second, to the
12107c478bd9Sstevel@tonic-gate  *	appropriate numerical argument used by the system to delay
12117c478bd9Sstevel@tonic-gate  *	that that many tenths of a second.  For example, on the
12127c478bd9Sstevel@tonic-gate  *	amiga, there is a system call "Delay()" which takes an
12137c478bd9Sstevel@tonic-gate  *	argument in ticks (50 per second).  On unix, the sleep
12147c478bd9Sstevel@tonic-gate  *	command takes seconds.  Thus a value of "10", for one
12157c478bd9Sstevel@tonic-gate  *	second of delay, gets converted to 50 on the amiga, and 1
12167c478bd9Sstevel@tonic-gate  *	on unix.  Other systems will need to use a timing loop.
12177c478bd9Sstevel@tonic-gate  */
12187c478bd9Sstevel@tonic-gate static int
delayarg(int value)12197c478bd9Sstevel@tonic-gate delayarg(int value)
12207c478bd9Sstevel@tonic-gate {
12217c478bd9Sstevel@tonic-gate 	unsigned int delayarg = 0;
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate #if (unix || xenix)
12247c478bd9Sstevel@tonic-gate 	delayarg = value / 10;		/* Delay is in seconds for sleep () */
12257c478bd9Sstevel@tonic-gate #endif
12267c478bd9Sstevel@tonic-gate 	return (delayarg);
12277c478bd9Sstevel@tonic-gate }
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate /*
12307c478bd9Sstevel@tonic-gate  *
12317c478bd9Sstevel@tonic-gate  *			delay
12327c478bd9Sstevel@tonic-gate  *
12337c478bd9Sstevel@tonic-gate  * Description:
12347c478bd9Sstevel@tonic-gate  *	Implements the delay function.
12357c478bd9Sstevel@tonic-gate  *
12367c478bd9Sstevel@tonic-gate  *	A dummy delay stub for systems that do not support delays.
12377c478bd9Sstevel@tonic-gate  *	With a little work, this can be turned into a timing loop.
12387c478bd9Sstevel@tonic-gate  */
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate static void
delay(uint_t xx)1241*47644099Sgt29601 delay(uint_t xx)
12427c478bd9Sstevel@tonic-gate {
12437c478bd9Sstevel@tonic-gate #if (unix || xenix)
12447c478bd9Sstevel@tonic-gate 	sleep(xx);
12457c478bd9Sstevel@tonic-gate #endif
12467c478bd9Sstevel@tonic-gate #if amiga
12477c478bd9Sstevel@tonic-gate 	Delay(xx);
12487c478bd9Sstevel@tonic-gate #endif
12497c478bd9Sstevel@tonic-gate #ifdef __ZTC__
1250*47644099Sgt29601 	msleep((ulong_t)xx);
12517c478bd9Sstevel@tonic-gate #endif
12527c478bd9Sstevel@tonic-gate }
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate /*
12557c478bd9Sstevel@tonic-gate  *
12567c478bd9Sstevel@tonic-gate  *			getclock
12577c478bd9Sstevel@tonic-gate  *
12587c478bd9Sstevel@tonic-gate  * Description:
12597c478bd9Sstevel@tonic-gate  *	Returns the time in milliseconds used by this process
12607c478bd9Sstevel@tonic-gate  *	so far.
12617c478bd9Sstevel@tonic-gate  */
12627c478bd9Sstevel@tonic-gate #if (unix || xenix)
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate #include <sys/param.h>
12657c478bd9Sstevel@tonic-gate #if BSD4_3 || sun
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate #include <sys/time.h>
12687c478bd9Sstevel@tonic-gate #include <sys/resource.h>
12697c478bd9Sstevel@tonic-gate 
1270*47644099Sgt29601 static ulong_t
getclock()12717c478bd9Sstevel@tonic-gate getclock()
12727c478bd9Sstevel@tonic-gate {
12737c478bd9Sstevel@tonic-gate #if 0
12747c478bd9Sstevel@tonic-gate 	struct rusage ru;
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 	getrusage(RUSAGE_SELF, &ru);
12777c478bd9Sstevel@tonic-gate 	return ((ru.ru_utime.tv_sec * 1000) + (ru.ru_utime.tv_usec / 1000));
12787c478bd9Sstevel@tonic-gate #else
12797c478bd9Sstevel@tonic-gate 	return (0);
12807c478bd9Sstevel@tonic-gate #endif
12817c478bd9Sstevel@tonic-gate }
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate #else
12847c478bd9Sstevel@tonic-gate 
1285*47644099Sgt29601 static ulong_t
getclock()12867c478bd9Sstevel@tonic-gate getclock()
12877c478bd9Sstevel@tonic-gate {
12887c478bd9Sstevel@tonic-gate 	return (0);
12897c478bd9Sstevel@tonic-gate }
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate #endif
12927c478bd9Sstevel@tonic-gate #endif	/* unix */
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate #ifdef MSDOS
1295*47644099Sgt29601 static ulong_t
getclock()12967c478bd9Sstevel@tonic-gate getclock()
12977c478bd9Sstevel@tonic-gate {
12987c478bd9Sstevel@tonic-gate 	return (clock() * 10);
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate #endif
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate /*
13037c478bd9Sstevel@tonic-gate  *
13047c478bd9Sstevel@tonic-gate  *			mystrtok
13057c478bd9Sstevel@tonic-gate  *
13067c478bd9Sstevel@tonic-gate  * Description:
13077c478bd9Sstevel@tonic-gate  *	A version of strtok for those systems without it
13087c478bd9Sstevel@tonic-gate  */
13097c478bd9Sstevel@tonic-gate static char *
mystrtok(char * s1,char * s2)13107c478bd9Sstevel@tonic-gate mystrtok(char *s1, char *s2)
13117c478bd9Sstevel@tonic-gate {
13127c478bd9Sstevel@tonic-gate 	static char *end = NULL;
13137c478bd9Sstevel@tonic-gate 	register char *rtnval;
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	rtnval = NULL;
13167c478bd9Sstevel@tonic-gate 	if (s2 != NULL) {
13177c478bd9Sstevel@tonic-gate 		if (s1 != NULL) {
13187c478bd9Sstevel@tonic-gate 			end = s1;
13197c478bd9Sstevel@tonic-gate 			rtnval = mystrtok((char *)NULL, s2);
13207c478bd9Sstevel@tonic-gate 		} else if (end != NULL) {
13217c478bd9Sstevel@tonic-gate 			if (*end != '\0') {
13227c478bd9Sstevel@tonic-gate 				rtnval = end;
13237c478bd9Sstevel@tonic-gate 				while ((*end != *s2) && (*end != '\0')) {
13247c478bd9Sstevel@tonic-gate 					end++;
13257c478bd9Sstevel@tonic-gate 				}
13267c478bd9Sstevel@tonic-gate 				if (*end != '\0') {
13277c478bd9Sstevel@tonic-gate 					*end++ = '\0';
13287c478bd9Sstevel@tonic-gate 				}
13297c478bd9Sstevel@tonic-gate 			}
13307c478bd9Sstevel@tonic-gate 		}
13317c478bd9Sstevel@tonic-gate 	}
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	return (rtnval);
13347c478bd9Sstevel@tonic-gate }
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate /*
13377c478bd9Sstevel@tonic-gate  *
13387c478bd9Sstevel@tonic-gate  *			dbug_thread_exit
13397c478bd9Sstevel@tonic-gate  *
13407c478bd9Sstevel@tonic-gate  * Description:
13417c478bd9Sstevel@tonic-gate  *	Called when a thread exits.
13427c478bd9Sstevel@tonic-gate  * Arguments:
13437c478bd9Sstevel@tonic-gate  *	data	pointer to thread specific data
13447c478bd9Sstevel@tonic-gate  * Returns:
13457c478bd9Sstevel@tonic-gate  * Preconditions:
13467c478bd9Sstevel@tonic-gate  */
13477c478bd9Sstevel@tonic-gate void
dbug_thread_exit(void * data)13487c478bd9Sstevel@tonic-gate dbug_thread_exit(void *data)
13497c478bd9Sstevel@tonic-gate {
13507c478bd9Sstevel@tonic-gate 	dbug_state_object_t *dbug_state_object_p;
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	LOCK_THREAD_DATA();
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 	/* If debugging is off, then nothing else to do */
13557c478bd9Sstevel@tonic-gate 	if (NOT db_debugon())
13567c478bd9Sstevel@tonic-gate 		goto out;
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	/* get a pointer to the active state */
13597c478bd9Sstevel@tonic-gate 	dbug_state_object_p = sd_push;
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	if (dbug_state_object_p->sf_thread) {
13627c478bd9Sstevel@tonic-gate 		doprefix(dbug_state_object_p, 0, sd_lineno++, "unknown",
13637c478bd9Sstevel@tonic-gate 		    sd_process);
13647c478bd9Sstevel@tonic-gate 		indent(dbug_state_object_p, dbug_state_object_p->s_level);
13657c478bd9Sstevel@tonic-gate 		fprintf(dbug_state_object_p->s_out_file, "thread destroyed\n");
13667c478bd9Sstevel@tonic-gate 		fflush(dbug_state_object_p->s_out_file);
13677c478bd9Sstevel@tonic-gate 		delay(dbug_state_object_p->s_delay);
13687c478bd9Sstevel@tonic-gate 	}
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate out:;
13717c478bd9Sstevel@tonic-gate 	FREE_THREAD_DATA(data);
13727c478bd9Sstevel@tonic-gate 	UNLOCK_THREAD_DATA();
13737c478bd9Sstevel@tonic-gate }
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate /*
13767c478bd9Sstevel@tonic-gate  *
13777c478bd9Sstevel@tonic-gate  *			doabort
13787c478bd9Sstevel@tonic-gate  *
13797c478bd9Sstevel@tonic-gate  * Description:
13807c478bd9Sstevel@tonic-gate  *	Causes the process to exit immediatly with a core dump.
13817c478bd9Sstevel@tonic-gate  * Arguments:
13827c478bd9Sstevel@tonic-gate  * Returns:
13837c478bd9Sstevel@tonic-gate  * Preconditions:
13847c478bd9Sstevel@tonic-gate  */
13857c478bd9Sstevel@tonic-gate void
doabort()13867c478bd9Sstevel@tonic-gate doabort()
13877c478bd9Sstevel@tonic-gate {
13887c478bd9Sstevel@tonic-gate 	dbug_state_object_t *dbug_state_object_p = sd_push;
13897c478bd9Sstevel@tonic-gate 	fflush(dbug_state_object_p->s_out_file);
13907c478bd9Sstevel@tonic-gate 	for (;;) {
13917c478bd9Sstevel@tonic-gate 		kill(getpid(), SIGABRT);
13927c478bd9Sstevel@tonic-gate 		(void) signal(SIGABRT, SIG_DFL);
13937c478bd9Sstevel@tonic-gate 		(void) sigrelse(SIGABRT);
13947c478bd9Sstevel@tonic-gate 	}
13957c478bd9Sstevel@tonic-gate }
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate /*
13987c478bd9Sstevel@tonic-gate  *
13997c478bd9Sstevel@tonic-gate  *			dbug_state_create
14007c478bd9Sstevel@tonic-gate  *
14017c478bd9Sstevel@tonic-gate  * Description:
14027c478bd9Sstevel@tonic-gate  *	Constructor for the dbug_state class.
14037c478bd9Sstevel@tonic-gate  * Arguments:
14047c478bd9Sstevel@tonic-gate  *	The current level in the call stack.
14057c478bd9Sstevel@tonic-gate  * Returns:
14067c478bd9Sstevel@tonic-gate  * Preconditions:
14077c478bd9Sstevel@tonic-gate  */
14087c478bd9Sstevel@tonic-gate dbug_state_object_t *
dbug_state_create(int level)14097c478bd9Sstevel@tonic-gate dbug_state_create(int level)
14107c478bd9Sstevel@tonic-gate {
14117c478bd9Sstevel@tonic-gate 	dbug_state_object_t *dbug_state_object_p;
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	dbug_state_object_p =
14147c478bd9Sstevel@tonic-gate 	    (dbug_state_object_t *)calloc(sizeof (dbug_state_object_t), 1);
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	if (dbug_state_object_p == NULL)
14177c478bd9Sstevel@tonic-gate 		doabort();
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	dbug_state_object_p->sf_trace = 0;
14207c478bd9Sstevel@tonic-gate 	dbug_state_object_p->sf_debug = 0;
14217c478bd9Sstevel@tonic-gate 	dbug_state_object_p->sf_file = 0;
14227c478bd9Sstevel@tonic-gate 	dbug_state_object_p->sf_line = 0;
14237c478bd9Sstevel@tonic-gate 	dbug_state_object_p->sf_depth = 0;
14247c478bd9Sstevel@tonic-gate 	dbug_state_object_p->sf_process = 0;
14257c478bd9Sstevel@tonic-gate 	dbug_state_object_p->sf_number = 0;
14267c478bd9Sstevel@tonic-gate 	dbug_state_object_p->sf_pid = 0;
14277c478bd9Sstevel@tonic-gate 	dbug_state_object_p->sf_stack = 0;
14287c478bd9Sstevel@tonic-gate 	dbug_state_object_p->sf_time = 0;
14297c478bd9Sstevel@tonic-gate 	dbug_state_object_p->sf_didopen = 0;
14307c478bd9Sstevel@tonic-gate 	dbug_state_object_p->sf_thread = 0;
14317c478bd9Sstevel@tonic-gate 	dbug_state_object_p->s_maxdepth = MAXDEPTH;
14327c478bd9Sstevel@tonic-gate 	dbug_state_object_p->s_delay = 0;
14337c478bd9Sstevel@tonic-gate 	dbug_state_object_p->s_level = level;
14347c478bd9Sstevel@tonic-gate 	dbug_state_object_p->s_starttime = 0;
14357c478bd9Sstevel@tonic-gate 	dbug_state_object_p->s_out_file = stderr;
14367c478bd9Sstevel@tonic-gate 	dbug_state_object_p->s_next = NULL;
14377c478bd9Sstevel@tonic-gate 	return (dbug_state_object_p);
14387c478bd9Sstevel@tonic-gate }
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate /*
14417c478bd9Sstevel@tonic-gate  *
14427c478bd9Sstevel@tonic-gate  *			dbug_state_destroy
14437c478bd9Sstevel@tonic-gate  *
14447c478bd9Sstevel@tonic-gate  * Description:
14457c478bd9Sstevel@tonic-gate  *	Destructor for the dbug_state class.
14467c478bd9Sstevel@tonic-gate  * Arguments:
14477c478bd9Sstevel@tonic-gate  * Returns:
14487c478bd9Sstevel@tonic-gate  * Preconditions:
14497c478bd9Sstevel@tonic-gate  */
14507c478bd9Sstevel@tonic-gate void
dbug_state_destroy(dbug_state_object_t * dbug_state_object_p)14517c478bd9Sstevel@tonic-gate dbug_state_destroy(dbug_state_object_t *dbug_state_object_p)
14527c478bd9Sstevel@tonic-gate {
14537c478bd9Sstevel@tonic-gate 	if (dbug_state_object_p->sf_didopen)
14547c478bd9Sstevel@tonic-gate 		fclose(dbug_state_object_p->s_out_file);
14557c478bd9Sstevel@tonic-gate 	free(dbug_state_object_p);
14567c478bd9Sstevel@tonic-gate }
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate /*
14597c478bd9Sstevel@tonic-gate  *
14607c478bd9Sstevel@tonic-gate  *		db_debugon
14617c478bd9Sstevel@tonic-gate  *
14627c478bd9Sstevel@tonic-gate  * Description:
14637c478bd9Sstevel@tonic-gate  *   Returns 1 if debugging is currently enabled, 0 otherwise.
14647c478bd9Sstevel@tonic-gate  * Arguments:
14657c478bd9Sstevel@tonic-gate  * Returns:
14667c478bd9Sstevel@tonic-gate  * Errors:
14677c478bd9Sstevel@tonic-gate  * Preconditions:
14687c478bd9Sstevel@tonic-gate  */
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate int
db_debugon(dbug_object_p)14717c478bd9Sstevel@tonic-gate db_debugon(dbug_object_p)
14727c478bd9Sstevel@tonic-gate dbug_object_t *dbug_object_p;
14737c478bd9Sstevel@tonic-gate {
14747c478bd9Sstevel@tonic-gate 	return (sd_on);
14757c478bd9Sstevel@tonic-gate }
14767c478bd9Sstevel@tonic-gate boolean
file_exists(const char * pathname)14777c478bd9Sstevel@tonic-gate file_exists(const char *pathname)
14787c478bd9Sstevel@tonic-gate {
14797c478bd9Sstevel@tonic-gate 	return (access(pathname, F_OK) == 0);
14807c478bd9Sstevel@tonic-gate }
14817c478bd9Sstevel@tonic-gate boolean
file_writable(const char * pathname)14827c478bd9Sstevel@tonic-gate file_writable(const char *pathname)
14837c478bd9Sstevel@tonic-gate {
14847c478bd9Sstevel@tonic-gate 	return (access(pathname, W_OK) == 0);
14857c478bd9Sstevel@tonic-gate }
14867c478bd9Sstevel@tonic-gate dbug_object_t *
db_get_dbug_object_p()14877c478bd9Sstevel@tonic-gate db_get_dbug_object_p()
14887c478bd9Sstevel@tonic-gate {
14897c478bd9Sstevel@tonic-gate 	thread_data_t *tdp;
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	GET_THREAD_DATA_PTR(&tdp);
14927c478bd9Sstevel@tonic-gate 	return (tdp->td_first);
14937c478bd9Sstevel@tonic-gate }
14947c478bd9Sstevel@tonic-gate #endif /* DBUG_OFF */
1495