12083Smckusick /* Copyright (c) 1979 Regents of the University of California */
22083Smckusick 
3*2110Smckusic static char sccsid[] = "@(#)utilities.c 1.2 01/10/81";
42083Smckusick 
52083Smckusick #include	"vars.h"
62083Smckusick #include	"panics.h"
72083Smckusick #include	"h02opcs.h"
82083Smckusick 
92083Smckusick stats()
102083Smckusick {
112083Smckusick 	struct	{
122083Smckusick 		long	usr_time;
132083Smckusick 		long	sys_time;
142083Smckusick 		long	child_usr_time;
152083Smckusick 		long	child_sys_time;
16*2110Smckusic 	} tbuf;
172083Smckusick 	register double l;
182083Smckusick 	register long count;
19*2110Smckusic #	ifdef PROFILE
20*2110Smckusic #	define	proffile	"/vb/grad/mckusick/px/profile/pcnt.out"
21*2110Smckusic 	struct cntrec {
22*2110Smckusic 		double	counts[NUMOPS];	/* instruction counts */
23*2110Smckusic 		long	runs;		/* number of interpreter runs */
24*2110Smckusic 		long	startdate;	/* date profile started */
25*2110Smckusic 		long	usrtime;	/* total user time consumed */
26*2110Smckusic 		long	systime;	/* total system time consumed */
27*2110Smckusic 		double	stmts;		/* number of pascal stmts executed */
28*2110Smckusic 	} profdata;
29*2110Smckusic 	FILE *datafile;
30*2110Smckusic #	endif PROFILE
312083Smckusick 
322083Smckusick 	if (_nodump)
332083Smckusick 		return(0);
342083Smckusick 	times(&tbuf);
35*2110Smckusic #	ifdef PROFILE
362083Smckusick 	datafile = fopen(proffile,"r");
37*2110Smckusic 	if (datafile == NULL)
38*2110Smckusic 		goto skipprof;
39*2110Smckusic 	count = fread(&profdata,1,sizeof(profdata),datafile);
40*2110Smckusic 	if (count != sizeof(profdata))
41*2110Smckusic 		goto skipprof;
42*2110Smckusic 	for (count = 0;  count < NUMOPS;  count++)
43*2110Smckusic 		profdata.counts[count] += _profcnts[count];
44*2110Smckusic 	profdata.runs += 1;
45*2110Smckusic 	profdata.stmts += _stcnt;
46*2110Smckusic 	profdata.usrtime += tbuf.usr_time;
47*2110Smckusic 	profdata.systime += tbuf.sys_time;
48*2110Smckusic 	datafile = freopen(proffile,"w",datafile);
49*2110Smckusic 	if (datafile == NULL)
50*2110Smckusic 		goto skipprof;
51*2110Smckusic 	count = fwrite(&profdata,1,sizeof(profdata),datafile);
52*2110Smckusic 	if (count != sizeof(profdata))
53*2110Smckusic 		goto skipprof;
54*2110Smckusic 	fclose(datafile);
55*2110Smckusic skipprof:
56*2110Smckusic #	endif PROFILE
572083Smckusick 	l = tbuf.usr_time;
582083Smckusick 	l = l / HZ;
592083Smckusick 	fprintf(stderr,
602083Smckusick 		"\n%1ld statements executed in %04.2f seconds cpu time.\n",
612083Smckusick 		_stcnt,l);
622083Smckusick }
632083Smckusick 
642083Smckusick backtrace(errnum)
652083Smckusick 	long	errnum;
662083Smckusick {
672083Smckusick 	register struct disp *mydp;
682083Smckusick 	register struct stack *ap;
692083Smckusick 	register char *cp;
702083Smckusick 	register long i, linum;
71*2110Smckusic 	struct disply disp;
722083Smckusick 
732083Smckusick 	if (_lino <= 0) {
742083Smckusick 		fputs("Program was not executed.\n",stderr);
752083Smckusick 		return;
762083Smckusick 	}
77*2110Smckusic 	disp = _display;
782083Smckusick 	if (errnum == PINTR)
792083Smckusick 		fputs("\n\tInterrupted in \"",stderr);
802083Smckusick 	else if (errnum == PHALT)
812083Smckusick 		fputs("\n\tHalted in \"",stderr);
822083Smckusick 	else
832083Smckusick 		fputs("\n\tError in \"",stderr);
842083Smckusick 	mydp = _dp;
852083Smckusick 	linum = _lino;
862083Smckusick 	for (;;) {
872083Smckusick 		ap = mydp->stp;
882083Smckusick 		i = linum - (((ap)->entry)->offset & 0177777);
892083Smckusick 		fprintf(stderr,"%s\"",(ap->entry)->name);
902083Smckusick 		if (_nodump == 0)
912083Smckusick 			fprintf(stderr,"+%1d near line %1d.",i,linum);
922083Smckusick 		fputc('\n',stderr);
932083Smckusick 		*mydp = (ap)->odisp;
94*2110Smckusic 		if (mydp <= &_display.frame[1]){
95*2110Smckusic 			_display = disp;
962083Smckusick 			psexit(errnum);
972083Smckusick 		}
982083Smckusick 		mydp = (ap)->dp;
992083Smckusick 		linum = (ap)->lino;
1002083Smckusick 		fputs("\tCalled by \"",stderr);
1012083Smckusick 	}
1022083Smckusick }
1032083Smckusick 
1042083Smckusick psexit(code)
1052083Smckusick 
1062083Smckusick 	long	code;
1072083Smckusick {
1082083Smckusick 	if (_pcpcount != 0)
1092083Smckusick 		PMFLUSH(_cntrs, _rtns, _pcpcount);
1102083Smckusick 	if (_mode == PIX) {
1112083Smckusick 		fputs("Execution terminated",stderr);
1122083Smckusick 		if (code)
1132083Smckusick 			fputs(" abnormally",stderr);
1142083Smckusick 		fputc('.',stderr);
1152083Smckusick 		fputc('\n',stderr);
1162083Smckusick 	}
1172083Smckusick 	stats();
1182083Smckusick 	exit(code);
1192083Smckusick }
120