summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/cachefs/mdbug/dbug.c
blob: 175a7247da95af5d4bd9746462335c9989025f31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
/*
 *
 *			dbug.c
 *
 * Purpose:
 *    Implements the dbug_routine class.
 *    This code is derived from the public domain DBUG
 *    package written by Fred Fish.
 *
 */
#pragma ident	"%Z%%M%	%I%	%E% SMI"

#ifndef DBUG_OFF

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <thread.h>
#include <sys/types.h>
#include <signal.h>
#include "flist.h"
#include "mdbug.h"
#include "priv.h"

/* forward references */
static int listparse(register char *ctlp, flist_object_t *head);
static boolean inlist(flist_object_t *flist_object_p, const char *cp);
static boolean dotrace(dbug_state_object_t *dbug_state_object_p,
    const char *func, const char *process);
static void indent(register dbug_state_object_t *dbug_state_object_p,
    int indent);
static void doprefix(dbug_state_object_t *dbug_state_object_p, int line,
    long lineno, const char *file, const char *process);
static FILE *openfile(char *name);
static boolean writable(char *pathname);
static void changeowner(char *pathname);
static int delayarg(int value);
static void delay(uint_t xx);
static ulong_t getclock();
static char *mystrtok(char *s1, char *s2);
void doabort();

/* initialize static members of class */
int	sd_on = 0;
char	sd_process[128];
long	sd_lineno = 0;
dbug_state_object_t *sd_push = NULL;

/* this structure defines thread specific data */
typedef struct thread_data {
#ifdef STACKINIT
	unsigned long	 td_stackinit;		/* Begining of stack. */
#endif
	int		 td_line;		/* Current line number. */
	char		 td_keyword[64];	/* Current keyword. */
	dbug_object_t	*td_first;		/* Current routine. */
} thread_data_t;
#ifdef _REENTRANT
mutex_t		mdt_lock;
int		mdt_once = 0;
thread_key_t	mdt_key;
#else
thread_data_t	mdt_data;
#endif
/*
 * format of control string
 *   command[:command:...]
 *
 *   commands
 *   debugging on	'd'  d[,<keyword>[,...]]
 *   delay value	'D'  D[,<delay value>]
 *   function list	'f'  f[,<function name>[,...]]
 *   print filename	'F'  F
 *   print pid		'i'  i
 *   print line number	'L'  L
 *   print call depth	'n'  n
 *   number each line	'N'  N
 *   output file	'o'  o[,<filename>
 *   process name list	'p'  p[,<process name>[,...]]
 *   print proc name	'P'  P
 *   reset indentation	'r'  r
 *   print runtime	'R'  R
 *   print thread info	'T'  T
 *   print trace	't'  t
 *   print stack depth	's'  s
 */

/*
 *
 *		dbug_object_create
 *
 * Description:
 *	Constructor for the dbug_routine class.
 * Arguments:
 *	line	- line number where object was created.
 *	file	- file name object was created in.
 *	function- routine name object was created in.
 * Returns:
 * Errors:
 * Preconditions:
 */
void
dbug_object_create(int line, const char *file, const char *function)
{
	dbug_object_t  *dbug_object_p;
	dbug_state_object_t *dbug_state_object_p;
	ulong_t stacksize;
	int created = 0;
	char *cptr;

	thread_data_t *tdp = NULL;
#ifdef _REENTRANT
	LOCK_THREAD_DATA();
	if (!mdt_once) {
		if (thr_keycreate(&mdt_key, dbug_thread_exit) != 0)
			doabort();
		mdt_once++;
	}
	GET_THREAD_DATA_PTR(&tdp);
	if (tdp == NULL) {
		tdp = (thread_data_t *)calloc(sizeof (*tdp), 1);
		if (tdp == NULL)
			doabort();
		thr_setspecific(mdt_key, tdp);
		created = 1;
		tdp->td_keyword[0] = '\0';
		tdp->td_first = NULL;
	}
#else
	GET_THREAD_DATA_PTR(&tdp);
#endif

	dbug_object_p = (dbug_object_t *)calloc(sizeof (dbug_object_t), 1);

	if (dbug_object_p == NULL)
		doabort();

	/* save the function name */
	if (function)
		strcpy(dbug_object_p->d_func, function);
	else
		strcpy(dbug_object_p->d_func, "unknown");

	/* save the base of the file name */
	if (file) {
		cptr = strrchr(file, '/');
		if (cptr == NULL)
			strcpy(dbug_object_p->d_file, file);
		else
			strcpy(dbug_object_p->d_file, cptr++);
	} else
		strcpy(dbug_object_p->d_file, "unknown");

	/* Chain this onto our list of them */
	dbug_object_p->d_prev = tdp->td_first;
	tdp->td_first = dbug_object_p;

	/* set the default routine exit point line number to zero */
	dbug_object_p->d_leaveline = 0;

	/* If debugging is off, then all done */
	if (NOT db_debugon())
		goto out;

	/* if the active state is null initialize it */
	if (sd_push == NULL)
		db_push("d,:f,:F:i:L:n:N:o,cfsd_debug.out:p,:P:r:R:T:t:s");

	/* get a pointer to the active state */
	dbug_state_object_p = sd_push;

#ifdef STACKINIT
	/*
	 * Get the new stack depth.
	 * There a two problems associated with this.
	 * One is because c++ allows declarations anywhere inside of
	 * a routine.  So it is difficult to position the dbug_enter()
	 * macro after all declarations and still be useful.
	 * Two is that the dbug_enter() macro should be before all
	 * other automatic objects so that its destructor gets called
	 * last as the routine is returning.
	 * The solution is to advise placing the dbug_enter() macro at
	 * the start of the routine and specifying that that stack
	 * values apply upto but not including the current routine.
	 */
	stacksize = (ulong_t)this;
	if (GROWDOWN)
		stacksize = tdp->td_stackinit - stacksize;
	else
		stacksize = stacksize - tdp->td_stackinit;
#endif

	/* record the new nesting level */
	dbug_state_object_p->s_level++;

	/* if producing a trace of function calls */
	if (dotrace(dbug_state_object_p, dbug_object_p->d_func, sd_process)) {
		doprefix(dbug_state_object_p, line, sd_lineno++,
		    dbug_object_p->d_file, sd_process);
		indent(dbug_state_object_p, dbug_state_object_p->s_level);
		if (dbug_state_object_p->sf_stack)
			fprintf(dbug_state_object_p->s_out_file, ">%s   %ld\n",
			    dbug_object_p->d_func, stacksize);
		else
			fprintf(dbug_state_object_p->s_out_file, ">%s\n",
			    dbug_object_p->d_func);
		fflush(dbug_state_object_p->s_out_file);
		delay(dbug_state_object_p->s_delay);
	}

	/* if a new thread */
	if (created && dbug_state_object_p->sf_thread) {
		doprefix(dbug_state_object_p, line, sd_lineno++,
		    dbug_object_p->d_file, sd_process);
		indent(dbug_state_object_p, dbug_state_object_p->s_level);
		fprintf(dbug_state_object_p->s_out_file, "thread created\n");
		fflush(dbug_state_object_p->s_out_file);
		delay(dbug_state_object_p->s_delay);
	}

out:;
	UNLOCK_THREAD_DATA();
}

/*
 *
 *		dbug_object_destroy
 *
 * Description:
 *	Destructor for the dbug_routine class.
 *	Unchains this object from the list.
 * Arguments:
 * Returns:
 * Errors:
 * Preconditions:
 */
void
dbug_object_destroy(char *function_name, int line)
{
	dbug_object_t *dbug_object_p;
	dbug_state_object_t *dbug_state_object_p;
	thread_data_t *tdp;

	LOCK_THREAD_DATA();
	GET_THREAD_DATA_PTR(&tdp);

	/* unchain from the list of objects */
	dbug_object_p = tdp->td_first;
	tdp->td_first = dbug_object_p->d_prev;

	/* If debugging is off, then nothing else to do */
	if (NOT db_debugon())
		goto out;

	dbug_object_p->d_leaveline = line;

	/* get a pointer to the active state */
	dbug_state_object_p = sd_push;

	/*
	 * Make sure the last one created is being deleted.
	 * This will not be the case if there are multiple dbug_routine
	 * objects per routine or if one is created outside of a routine.
	 */
	if (strcmp(function_name, dbug_object_p->d_func)) {
		doprefix(dbug_state_object_p, dbug_object_p->d_leaveline,
		    sd_lineno++, dbug_object_p->d_file, sd_process);
		indent(dbug_state_object_p, dbug_state_object_p->s_level);
		fprintf(dbug_state_object_p->s_out_file,
		    "<expected %s, actual %s, ERROR: "
		    "dbug_enter/dbug_leave out of sequence.\n",
		    dbug_object_p->d_func, function_name);
		fflush(dbug_state_object_p->s_out_file);
		/* delay(dbug_state_object_p->s_delay); */
	}

	/* if producing a trace of function calls */
	if (dotrace(dbug_state_object_p, dbug_object_p->d_func, sd_process)) {
		doprefix(dbug_state_object_p, dbug_object_p->d_leaveline,
		    sd_lineno++, dbug_object_p->d_file, sd_process);
		indent(dbug_state_object_p, dbug_state_object_p->s_level);
		fprintf(dbug_state_object_p->s_out_file, "<%s\n",
		    dbug_object_p->d_func);
		fflush(dbug_state_object_p->s_out_file);
#if 0
		delay(dbug_state_object_p->s_delay);
#endif
	}


	/* record the new nesting level */
	dbug_state_object_p->s_level--;

out:;
	free(dbug_object_p);
	UNLOCK_THREAD_DATA();
}

/*
 *
 *		db_keyword
 *
 * Description:
 *	Test a keyword to determine if it is in the currently active
 *	keyword list.  As with the function list, a keyword is accepted
 *	if the list is null, otherwise it must match one of the list
 *	members.  When debugging is not on, no keywords are accepted.
 *	After the maximum trace level is exceeded, no keywords are
 *	accepted (this behavior subject to change).  Additionally,
 *	the current function and process must be accepted based on
 *	their respective lists.
 * Arguments:
 *	keyword - the keyword to test
 * Returns:
 *	Returns 1 if keyword accepted, 0 otherwise.
 * Errors:
 * Preconditions:
 *	precond(keyword)
 */
int
db_keyword(dbug_object_t *dbug_object_p, const char *keyword)
{
	dbug_state_object_t *dbug_state_object_p;
	int ret = 0;

	/* return FALSE if not debugging */
	if (NOT db_debugon())
		return (0);

	LOCK_THREAD_DATA();

	/* return FALSE if not debugging */
	if (NOT db_debugon())
		goto out;

	/* get a pointer to the active state */
	dbug_state_object_p = sd_push;

	if (dbug_state_object_p->sf_debug) {  /* is this test necessary ? */
		if (inlist(dbug_state_object_p->s_functions,
		    dbug_object_p->d_func)) {
			if (inlist(dbug_state_object_p->s_processes,
			    sd_process)) {
				if (inlist(dbug_state_object_p->s_keywords,
				    keyword)) {
					ret = 1;
					goto out;
				}
			}
		}
	}

out:
	UNLOCK_THREAD_DATA();
	return (ret);
}

/*
 *
 *		db_pargs
 *
 * Description:
 *	Saves arguments for subsequent usage by db_printf.
 * Arguments:
 *	line    - the line number the db_print occurs on
 *	keyword - determines whether or not to really print anything
 * Returns:
 * Errors:
 * Preconditions:
 *	precond(keyword)
 */
void
db_pargs(dbug_object_t *dbug_object_p, int line, char *keyword)
{
	thread_data_t *tdp;

	/* return if no debugging yet */
	if (NOT db_debugon())
		return;

	GET_THREAD_DATA_PTR(&tdp);

	tdp->td_line = line;
	if (keyword)
		strcpy(tdp->td_keyword, keyword);
	else
		tdp->td_keyword[0] = '\0';
}

int
db_getfd()
{
	return (fileno(sd_push->s_out_file));
}

/*
 *
 *		db_printf
 *
 * Description:
 *	Outputs the specified message if the keyword specified
 *	by db_pargs() has been selected.  The line number specified
 *	by db_pargs() is also used as the line number the db_printf()
 *	occurs on.  The format string should NOT include a terminating
 *	newline as one is supplied automatically.
 * Arguments:
 *	format - printf style printing control string
 *	...    - additional arguments required by the control string
 * Returns:
 * Errors:
 * Preconditions:
 *	precond(format)
 */
void
db_printf(char *keyword, char *format, ...)
{
	dbug_object_t *dbug_object_p;
	thread_data_t *tdp;
	dbug_state_object_t *dbug_state_object_p = sd_push;
	va_list args;

	dbug_object_p = db_get_dbug_object_p();
	/* return if no debugging yet */
	if (NOT db_debugon())
		return;

	GET_THREAD_DATA_PTR(&tdp);

	/* return if keyword not selected */
	if (NOT db_keyword(dbug_object_p, tdp->td_keyword))
		return;

	LOCK_THREAD_DATA();

	/* get a pointer to the active state */

	va_start(args, format);

	doprefix(dbug_state_object_p, tdp->td_line, sd_lineno++,
		dbug_object_p->d_file, sd_process);
	if (dbug_state_object_p->sf_trace)
		indent(dbug_state_object_p, dbug_state_object_p->s_level +1);
	else
		fprintf(dbug_state_object_p->s_out_file, "%s: ",
		    dbug_object_p->d_func);
	if (tdp->td_keyword[0])
		fprintf(dbug_state_object_p->s_out_file, "%s: ",
		    tdp->td_keyword);
	vfprintf(dbug_state_object_p->s_out_file, format, args);
	fprintf(dbug_state_object_p->s_out_file, "\n");
	fflush(dbug_state_object_p->s_out_file);
	delay(dbug_state_object_p->s_delay);

	va_end(args);

	UNLOCK_THREAD_DATA();
}

/*
 *
 *		db_traceprint
 *
 * Description:
 *	Prints out a trace of the call stack.
 * Arguments:
 *	line    - the line number where this call was made
 *	keyword - keyword to test against
 * Returns:
 * Errors:
 * Preconditions:
 */
void
db_traceprint(int line, const char *keyword)
{
	dbug_object_t *dbug_object_p;
	dbug_object_t *pdr;
	/* return if no debugging yet */
	if (NOT db_debugon())
		return;

	if ((dbug_object_p = db_get_dbug_object_p()) == NULL)
		doabort();

	/* If the specified keyword is enabled */
	if (db_keyword(dbug_object_p, keyword)) {
		/* perform setup for using db_printf */
		db_pargs(dbug_object_p, line, NULL);

		/* Output a header message */
		db_printf(NULL, "Stack Trace");

		/* walk the stack of dbug_routine objects */
		for (pdr = dbug_object_p; pdr != NULL; pdr = pdr->d_prev) {
			/* output the routine name */
			db_printf(NULL, "  %s() (%s)", pdr->d_func,
			    pdr->d_file);
		}
	}
}

/*
 *
 *			db_assert
 *
 * Description:
 *	Called when an assert fails.
 *	Prints out a stack trace and aborts.
 * Arguments:
 *	line	line number assert occurred at
 *	msgp	string form of assert code that failed
 * Returns:
 * Preconditions:
 *	precond(msgp)
 */
void
db_assert(dbug_object_t *dbug_object_p, int line, const char *msgp)
{
	if (NOT db_debugon())
		db_push("-#:d");
	db_pargs(dbug_object_p, line, NULL);
	db_printf(NULL, "Assertion Failed %s:%s():%d \"%s\"",
	    dbug_object_p->d_file, dbug_object_p->d_func, line, msgp);
	db_traceprint(line, NULL);
	doabort();
}

/*
 *
 *			db_precond
 *
 * Description:
 *	Called when an precond fails.
 *	Prints out a stack trace and aborts.
 * Arguments:
 *	line	line number precond occurred at
 *	msgp	string form of precond code that failed
 * Returns:
 * Preconditions:
 *	precond(msgp)
 */
void
db_precond(dbug_object_t *dbug_object_p, int line, const char *msgp)
{
	if (NOT db_debugon())
		db_push("-#:d");
	db_pargs(dbug_object_p, line, NULL);
	db_printf(NULL, "Precondition Failed %s:%s():%d \"%s\"",
	    dbug_object_p->d_file, dbug_object_p->d_func, line, msgp);
	db_traceprint(line, NULL);
	doabort();
}

/*
 *
 *		db_push
 *
 * Description:
 *	Push current debugger state and set up a new one.
 *	Returns NULL if no errors, an error string if there
 *	is an error.
 *
 * format of control string
 *   command[:command:...]
 *
 *   commands
 *   debugging on	'd'  d[,<keyword>[,...]]
 *   delay value	'D'  D[,<delay value>]
 *   function list	'f'  f[,<function name>[,...]]
 *   print filename	'F'  F
 *   print pid		'i'  i
 *   print line number	'L'  L
 *   print call depth	'n'  n
 *   number each line	'N'  N
 *   output file	'o'  o[,<filename>
 *   process name list	'p'  p[,<process name>[,...]]
 *   print proc name	'P'  P
 *   reset indentation	'r'  r
 *   print runtime	'R'  R
 *   print thread info	'T'  T
 *   print trace	't'  t
 *   print stack depth	's'  s
 */
char *
db_push(const char *control)
{
	char *dupcontrol = NULL;
	dbug_state_object_t *dbug_state_object_p;
	flist_object_t *flist_object_p;
	register char *scan;
	int retval;
	char res[100];
	int level;

	LOCK_THREAD_DATA();

	/* error if the control string is NULL */
	if (control == NULL) {
		strcpy(res, "mdbug: control string is NULL");
		goto out;
	}

	/* turn debugging flag off */
	sd_on = FALSE;

	/* get the level from the old state if it exists */
	if (sd_push == NULL)
		level = 0;
	else
		level = sd_push->s_level;

	/* Create a new state */
	dbug_state_object_p = dbug_state_create(level);
	if (dbug_state_object_p == NULL) {
		strcpy(res, "mdbug: out of memory, dbug_state_create");
		goto out;
	}

	/* add it to our list of states and make it the current one */
	dbug_state_object_p->s_next = sd_push;
	sd_push = dbug_state_object_p;

	/* Strip off -# if in the control string */
	if ((*control == '-') && (*(control+1) == '#'))
		control += 2;

	/* make a copy of the control string so we can modify it with strtok */
	dupcontrol = strdup(control);
	if (dupcontrol == NULL) {
		strcpy(res, "mdbug: out of memory, strdup");
		goto out;
	}

	/* parse the control string */
	for (scan = mystrtok(dupcontrol, ":");
	    scan != NULL;
	    scan = mystrtok(NULL, ":")) {
		switch (*scan++) {
		case 'd':			/* debugging on */
			sd_on = TRUE;
			dbug_state_object_p->sf_debug = TRUE;
			if (*scan++ == ',') {
				retval = listparse(scan,
				    dbug_state_object_p->s_keywords);
				if (retval < 0) {
					strcpy(res,
					    "mdbug: -d too many keywords");
					goto out;
				}
			}
			break;

		case 'D': 			/* specify delay value */
			dbug_state_object_p->s_delay = 0;
			if (*scan++ == ',') {
				flist_object_p = flist_create();
				retval = listparse(scan, flist_object_p);
				if (retval < 0) {
					strcpy(res,
					    "mdbug: -D too many delays");
					goto out;
				}
				if (flist_object_p->f_count > 0) {
					dbug_state_object_p->s_delay =
					    delayarg(atoi(
					    (char *)fl_top(flist_object_p)));
				}
				flist_destroy(flist_object_p);
			}
			break;

		case 'f': 			/* list of functions to watch */
			if (*scan++ == ',') {
				retval = listparse(scan,
				    dbug_state_object_p->s_functions);
				if (retval < 0) {
					strcpy(res,
					    "mdbug: -f too many functions");
					goto out;
				}
			}
			break;

		case 'F': 		/* print file name with dbug output */
			dbug_state_object_p->sf_file = TRUE;
			break;

		case 'i': 		/* print pid with dbug output */
			dbug_state_object_p->sf_pid = TRUE;
			break;

		case 'L':		/* print line nums with dbug output */
			dbug_state_object_p->sf_line = TRUE;
			break;

		case 'n': 		/* print function call depth */
			dbug_state_object_p->sf_depth = TRUE;
			break;

		case 'N': 		/* number each line of dbug output */
			dbug_state_object_p->sf_number = TRUE;
			break;

		case 'o': 		/* specifies output file for dbug */
			if (*scan++ == ',') {
				flist_object_p = flist_create();
				retval = listparse(scan, flist_object_p);
				if (retval < 0) {
					strcpy(res,
					    "mdbug: -o too many output files");
					goto out;
				}

				if (flist_object_p->f_count > 0) {
					dbug_state_object_p->s_out_file =
					    openfile((char *)
					    fl_top(flist_object_p));
					if (dbug_state_object_p->s_out_file !=
					    NULL)
						dbug_state_object_p->sf_didopen
						    = 1;
				} else
					dbug_state_object_p->s_out_file =
					    openfile(NULL);
				flist_destroy(flist_object_p);
			} else
				dbug_state_object_p->s_out_file =
				    openfile(NULL);
			if (dbug_state_object_p->s_out_file == NULL) {
				strcpy(res,
				    "mdbug: -o cannot open output file");
				goto out;
			}
			break;

		case 'p':			/* debug specified processes */
			if (*scan++ == ',') {
				retval = listparse(scan,
				    dbug_state_object_p->s_processes);
				if (retval < 0) {
					strcpy(res,
					    "mdbug: -p too many processes");
					goto out;
				}
			}
			break;

		case 'P': 		/* print process name on dbug output */
			dbug_state_object_p->sf_process = TRUE;
			break;

		case 'r': 			/* reset indentation to zero */
			dbug_state_object_p->s_level = 0;
			break;

		case 's': 			/* print stack depth on enter */
			dbug_state_object_p->sf_stack = TRUE;
			break;

		case 'R':		/* print time prog has been running */
			dbug_state_object_p->sf_time = TRUE;
			time(&dbug_state_object_p->s_starttime);
			break;

		case 'T':		/* print thread information */
			dbug_state_object_p->sf_thread = TRUE;
			break;

		case 't': 		/* print trace of functions called */
			dbug_state_object_p->sf_trace = TRUE;
			dbug_state_object_p->s_maxdepth = MAXDEPTH;
			if (*scan++ == ',') {
				flist_object_p = flist_create();
				retval = listparse(scan, flist_object_p);
				if (retval < 0) {
					strcpy(res,
					    "mdbug: -t too many traces");
					goto out;
				}
				if (flist_object_p->f_count > 0) {
					dbug_state_object_p->s_maxdepth =
					    atoi((char *)
					    fl_top(flist_object_p));
				}
				flist_destroy(flist_object_p);
			}
			break;
		}
	}

out:
	/* free up the dupped control string */
	free(dupcontrol);

	UNLOCK_THREAD_DATA();

	/* return result */
	return (NULL);
}

/*
 *
 *		db_pop
 *
 * Description:
 *	Pop the debug stack.
 */
void
db_pop()
{
	dbug_state_object_t *dbug_state_object_p;

	LOCK_THREAD_DATA();

	/* return if no debugging yet */
	if (sd_push == NULL)
		goto out;

	/* get and remove the top item from the list */
	dbug_state_object_p = sd_push;
	sd_push = dbug_state_object_p->s_next;

	/* Delete the item. */
	dbug_state_destroy(dbug_state_object_p);

	/* get the current top of the stack */
	dbug_state_object_p = sd_push;
	if (dbug_state_object_p) {
		/* See if debugging is turned on */
		if (dbug_state_object_p->sf_debug)
			sd_on = TRUE;
		else
			sd_on = FALSE;
	}

out:;
	UNLOCK_THREAD_DATA();
}

/*
 *
 *			db_process
 *
 * Description:
 *	Specifies the name of the process.
 *	Only the pointer is saved, the string is not copied.
 * Arguments:
 *	namep
 * Returns:
 * Preconditions:
 */
void
db_process(const char *namep)
{
	thread_data_t *tdp;

	strcpy(sd_process, namep);

#ifdef STACKINIT
	GET_THREAD_DATA_PTR(&tdp);
	tdp->td_stackinit = (ulong_t)this;
#endif
}

/*
 *
 *			listparse
 *
 * Description:
 *	parse list of modifiers in debug control string
 *
 *	Given pointer to a comma separated list of strings in "cltp",
 *	parses the list, building a list and returning a pointer to it.
 *	The original comma separated list is destroyed in the process of
 *	building the linked list, thus it had better be a duplicate
 *	if it is important.
 *
 *	This routine is only called from db_push.
 *	Returns 0 for success, -1 for failure.
 */
static int
listparse(register char *ctlp, flist_object_t *head)
{
	char *start;
	char *item;

	/* scan the string until end */
	while (*ctlp != '\0') {
		/* See if no more room on the list */
		if (fl_space(head) == 0)
			return (-1);

		/* save the begining of this section */
		start = ctlp;

		/* loop until the end of the token is found */
		while ((*ctlp != '\0') && (*ctlp != ','))
			ctlp++;

		/* add a string terminator if necessary, for strdup */
		if (*ctlp == ',')
			*ctlp++ = '\0';

		/* make a copy of the string */
		item = strdup(start);
		if (item == NULL)
			return (-1);

		/* add it to the list */
		fl_push(head, item);
	}

	return (0);
}

/*
 *
 *			inlist
 *
 * Description:
 *	Tests the string pointed to by "cp" to determine if it is in
 *	the list pointed to by "flist_object_p".  Linkp points to the first
 *	link in the list.  If flist_object_p is empty then the string is treated
 *	as if it is in the list (I.E all strings are in the null list).
 *	This may seem rather strange at first but leads to the desired
 *	operation if no list is given.  The net effect is that all
 *	strings will be accepted when there is no list, and when there
 *	is a list, only those strings in the list will be accepted.
 */
static boolean
inlist(flist_object_t *flist_object_p, const char *cp)
{
	register boolean accept;
	register char *item;

	if ((flist_object_p == NULL) || (flist_object_p->f_count == 0) ||
		(cp == NULL))
		accept = TRUE;
	else {
		accept = FALSE;

		/* walk the list of items */
		for (item = (char *)fl_top(flist_object_p);
		    item != NULL;
		    item = (char *)fl_next(flist_object_p)) {
			/* see if a match */
			if (strcmp(item, cp) == 0) {
				accept = TRUE;
				break;
			}
		}
	}

	return (accept);
}

/*
 *
 *			dotrace
 *
 * Description:
 *	Checks to see if tracing is enabled based on whether the
 *	user has specified tracing, the maximum trace depth has
 *	not yet been reached, the current function is selected,
 *	and the current process is selected.  Returns TRUE if
 *	tracing is enabled, FALSE otherwise.
 */
static boolean
dotrace(dbug_state_object_t *dbug_state_object_p, const char *func,
    const char *process)
{
	boolean trace;

	trace = FALSE;
	if (dbug_state_object_p->sf_trace) {
		if (dbug_state_object_p->s_level <=
		    dbug_state_object_p->s_maxdepth) {
			if (inlist(dbug_state_object_p->s_functions, func)) {
				if (inlist(dbug_state_object_p->s_processes,
				    process)) {
					trace = TRUE;
				}
			}
		}
	}

	return (trace);
}

/*
 *
 *			indent
 *
 * Description:
 *	Indent a line to the given level.  Note that this is
 *	a simple minded but portable implementation.
 *	There are better ways.
 *
 *	Also, the indent must be scaled by the compile time option
 *	of character positions per nesting level.
 */
static void
indent(register dbug_state_object_t *dbug_state_object_p, int indent)
{
	register int count;
	char buffer[PRINTBUF];

	indent *= INDENT;
	for (count = 0;
	    (count < (indent - INDENT)) && (count < (PRINTBUF - 1));
	    count++) {
		if ((count % INDENT) == 0)
			buffer[count] = '|';
		else
			buffer[count] = ' ';
	}

	buffer[count] = '\0';
	fprintf(dbug_state_object_p->s_out_file, buffer);
	fflush(dbug_state_object_p->s_out_file);
}

/*
 *
 *			doprefix
 *
 * Description:
 *	Print prefix common to all debugger output lines, prior to
 *	doing indentation if necessary.  Print such information as
 *	current process name, current source file name and line number,
 *	and current function nesting depth.
 */
static void
doprefix(dbug_state_object_t *dbug_state_object_p, int line, long lineno,
	const char *file, const char *process)
{
#if DBUG_UNIX
	if (dbug_state_object_p->sf_pid)
		fprintf(dbug_state_object_p->s_out_file, "%5d: ",
		    (int)getpid());
#endif

	if (dbug_state_object_p->sf_thread)
		fprintf(dbug_state_object_p->s_out_file, "%5ld: ",
		    (long)thr_self());

	if (dbug_state_object_p->sf_number)
		fprintf(dbug_state_object_p->s_out_file, "%5ld: ", lineno);

	if (dbug_state_object_p->sf_process && process)
		fprintf(dbug_state_object_p->s_out_file, "%s: ", process);

	if (dbug_state_object_p->sf_file)
		fprintf(dbug_state_object_p->s_out_file, "%14s: ", file);

	if (dbug_state_object_p->sf_line)
		fprintf(dbug_state_object_p->s_out_file, "%5d: ", line);

	if (dbug_state_object_p->sf_depth)
		fprintf(dbug_state_object_p->s_out_file, "%4d: ",
		dbug_state_object_p->s_level);

	fflush(dbug_state_object_p->s_out_file);
}

/*
 *
 *			openfile
 *
 * Description:
 *	Given name of a new file (or NULL for stdout) opens the file
 *	and sets the output stream to the new file.
 */
static FILE *
openfile(char *name)
{
	FILE *fp;
	boolean newfile;

	if (name == NULL)
		return (stdout);

	if (NOT writable(name))
		return (NULL);

	/* see if the file already exists */
	if (file_exists(name))
		newfile = FALSE;
	else
		newfile = TRUE;

	/* open the file */
	fp = fopen(name, "a+");
	if (fp == NULL)
		return (NULL);

	/*
	 * If the file is newly created, give it away to the user
	 * that started the program.
	 */
	if (newfile) {
		changeowner(name);
	}
	return (fp);
}

/*
 *
 *			writable
 *
 * Description:
 *	Because the debugger might be linked in with a program that
 *	runs with the set-uid-bit (suid) set, we have to be careful
 *	about opening a user named file for debug output.  This consists
 *	of checking the file for write access with the real user id,
 *	or checking the directory where the file will be created.
 *
 *	Returns TRUE if the user would normally be allowed write or
 *	create access to the named file.  Returns FALSE otherwise.
 */
static boolean
writable(char *pathname)
{
#if DBUG_UNIX

	char *lastslash;

	boolean granted = FALSE;
	if (file_exists(pathname)) {
		if (file_writable(pathname)) {
			granted = TRUE;
		}
	} else {
		lastslash = strrchr(pathname, '/');
		if (lastslash != NULL) {
			*lastslash = '\0';
		} else {
			pathname = ".";
		}
		if (file_writable(pathname)) {
			granted = TRUE;
		}
		if (lastslash != NULL) {
			*lastslash = '/';
		}
	}
	return (granted);
#else
	return (TRUE);
#endif
}

/*
 *
 *			changeowner
 *
 * Description:
 *	For unix systems, change the owner of the newly created debug
 *	file to the real owner.  This is strictly for the benefit of
 *	programs that are running with the set-user-id bit set.
 *
 *	Note that at this point, the fact that pathname represents
 *	a newly created file has already been established.  If the
 *	program that the debugger is linked to is not running with
 *	the suid bit set, then this operation is redundant (but
 *	harmless).
 */
static void
changeowner(char *pathname)
{
#if DBUG_UNIX
	chown(pathname, getuid(), getgid());
#endif
}

/*
 *
 *			delayarg
 *
 * Description:
 *	Converts delay argument, given in tenths of a second, to the
 *	appropriate numerical argument used by the system to delay
 *	that that many tenths of a second.  For example, on the
 *	amiga, there is a system call "Delay()" which takes an
 *	argument in ticks (50 per second).  On unix, the sleep
 *	command takes seconds.  Thus a value of "10", for one
 *	second of delay, gets converted to 50 on the amiga, and 1
 *	on unix.  Other systems will need to use a timing loop.
 */
static int
delayarg(int value)
{
	unsigned int delayarg = 0;

#if (unix || xenix)
	delayarg = value / 10;		/* Delay is in seconds for sleep () */
#endif
	return (delayarg);
}

/*
 *
 *			delay
 *
 * Description:
 *	Implements the delay function.
 *
 *	A dummy delay stub for systems that do not support delays.
 *	With a little work, this can be turned into a timing loop.
 */

static void
delay(uint_t xx)
{
#if (unix || xenix)
	sleep(xx);
#endif
#if amiga
	Delay(xx);
#endif
#ifdef __ZTC__
	msleep((ulong_t)xx);
#endif
}

/*
 *
 *			getclock
 *
 * Description:
 *	Returns the time in milliseconds used by this process
 *	so far.
 */
#if (unix || xenix)

#include <sys/param.h>
#if BSD4_3 || sun

#include <sys/time.h>
#include <sys/resource.h>

static ulong_t
getclock()
{
#if 0
	struct rusage ru;

	getrusage(RUSAGE_SELF, &ru);
	return ((ru.ru_utime.tv_sec * 1000) + (ru.ru_utime.tv_usec / 1000));
#else
	return (0);
#endif
}

#else

static ulong_t
getclock()
{
	return (0);
}

#endif
#endif	/* unix */

#ifdef MSDOS
static ulong_t
getclock()
{
	return (clock() * 10);
}
#endif

/*
 *
 *			mystrtok
 *
 * Description:
 *	A version of strtok for those systems without it
 */
static char *
mystrtok(char *s1, char *s2)
{
	static char *end = NULL;
	register char *rtnval;

	rtnval = NULL;
	if (s2 != NULL) {
		if (s1 != NULL) {
			end = s1;
			rtnval = mystrtok((char *)NULL, s2);
		} else if (end != NULL) {
			if (*end != '\0') {
				rtnval = end;
				while ((*end != *s2) && (*end != '\0')) {
					end++;
				}
				if (*end != '\0') {
					*end++ = '\0';
				}
			}
		}
	}

	return (rtnval);
}

/*
 *
 *			dbug_thread_exit
 *
 * Description:
 *	Called when a thread exits.
 * Arguments:
 *	data	pointer to thread specific data
 * Returns:
 * Preconditions:
 */
void
dbug_thread_exit(void *data)
{
	dbug_state_object_t *dbug_state_object_p;

	LOCK_THREAD_DATA();

	/* If debugging is off, then nothing else to do */
	if (NOT db_debugon())
		goto out;

	/* get a pointer to the active state */
	dbug_state_object_p = sd_push;

	if (dbug_state_object_p->sf_thread) {
		doprefix(dbug_state_object_p, 0, sd_lineno++, "unknown",
		    sd_process);
		indent(dbug_state_object_p, dbug_state_object_p->s_level);
		fprintf(dbug_state_object_p->s_out_file, "thread destroyed\n");
		fflush(dbug_state_object_p->s_out_file);
		delay(dbug_state_object_p->s_delay);
	}

out:;
	FREE_THREAD_DATA(data);
	UNLOCK_THREAD_DATA();
}

/*
 *
 *			doabort
 *
 * Description:
 *	Causes the process to exit immediatly with a core dump.
 * Arguments:
 * Returns:
 * Preconditions:
 */
void
doabort()
{
	dbug_state_object_t *dbug_state_object_p = sd_push;
	fflush(dbug_state_object_p->s_out_file);
	for (;;) {
		kill(getpid(), SIGABRT);
		(void) signal(SIGABRT, SIG_DFL);
		(void) sigrelse(SIGABRT);
	}
}

/*
 *
 *			dbug_state_create
 *
 * Description:
 *	Constructor for the dbug_state class.
 * Arguments:
 *	The current level in the call stack.
 * Returns:
 * Preconditions:
 */
dbug_state_object_t *
dbug_state_create(int level)
{
	dbug_state_object_t *dbug_state_object_p;

	dbug_state_object_p =
	    (dbug_state_object_t *)calloc(sizeof (dbug_state_object_t), 1);

	if (dbug_state_object_p == NULL)
		doabort();

	dbug_state_object_p->sf_trace = 0;
	dbug_state_object_p->sf_debug = 0;
	dbug_state_object_p->sf_file = 0;
	dbug_state_object_p->sf_line = 0;
	dbug_state_object_p->sf_depth = 0;
	dbug_state_object_p->sf_process = 0;
	dbug_state_object_p->sf_number = 0;
	dbug_state_object_p->sf_pid = 0;
	dbug_state_object_p->sf_stack = 0;
	dbug_state_object_p->sf_time = 0;
	dbug_state_object_p->sf_didopen = 0;
	dbug_state_object_p->sf_thread = 0;
	dbug_state_object_p->s_maxdepth = MAXDEPTH;
	dbug_state_object_p->s_delay = 0;
	dbug_state_object_p->s_level = level;
	dbug_state_object_p->s_starttime = 0;
	dbug_state_object_p->s_out_file = stderr;
	dbug_state_object_p->s_next = NULL;
	return (dbug_state_object_p);
}

/*
 *
 *			dbug_state_destroy
 *
 * Description:
 *	Destructor for the dbug_state class.
 * Arguments:
 * Returns:
 * Preconditions:
 */
void
dbug_state_destroy(dbug_state_object_t *dbug_state_object_p)
{
	if (dbug_state_object_p->sf_didopen)
		fclose(dbug_state_object_p->s_out_file);
	free(dbug_state_object_p);
}

/*
 *
 *		db_debugon
 *
 * Description:
 *   Returns 1 if debugging is currently enabled, 0 otherwise.
 * Arguments:
 * Returns:
 * Errors:
 * Preconditions:
 */

int
db_debugon(dbug_object_p)
dbug_object_t *dbug_object_p;
{
	return (sd_on);
}
boolean
file_exists(const char *pathname)
{
	return (access(pathname, F_OK) == 0);
}
boolean
file_writable(const char *pathname)
{
	return (access(pathname, W_OK) == 0);
}
dbug_object_t *
db_get_dbug_object_p()
{
	thread_data_t *tdp;

	GET_THREAD_DATA_PTR(&tdp);
	return (tdp->td_first);
}
#endif /* DBUG_OFF */