1 /* Copyright (c) 1979 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)utilities.c 1.2 01/10/81";
4 
5 #include	"vars.h"
6 #include	"panics.h"
7 #include	"h02opcs.h"
8 
9 stats()
10 {
11 	struct	{
12 		long	usr_time;
13 		long	sys_time;
14 		long	child_usr_time;
15 		long	child_sys_time;
16 	} tbuf;
17 	register double l;
18 	register long count;
19 #	ifdef PROFILE
20 #	define	proffile	"/vb/grad/mckusick/px/profile/pcnt.out"
21 	struct cntrec {
22 		double	counts[NUMOPS];	/* instruction counts */
23 		long	runs;		/* number of interpreter runs */
24 		long	startdate;	/* date profile started */
25 		long	usrtime;	/* total user time consumed */
26 		long	systime;	/* total system time consumed */
27 		double	stmts;		/* number of pascal stmts executed */
28 	} profdata;
29 	FILE *datafile;
30 #	endif PROFILE
31 
32 	if (_nodump)
33 		return(0);
34 	times(&tbuf);
35 #	ifdef PROFILE
36 	datafile = fopen(proffile,"r");
37 	if (datafile == NULL)
38 		goto skipprof;
39 	count = fread(&profdata,1,sizeof(profdata),datafile);
40 	if (count != sizeof(profdata))
41 		goto skipprof;
42 	for (count = 0;  count < NUMOPS;  count++)
43 		profdata.counts[count] += _profcnts[count];
44 	profdata.runs += 1;
45 	profdata.stmts += _stcnt;
46 	profdata.usrtime += tbuf.usr_time;
47 	profdata.systime += tbuf.sys_time;
48 	datafile = freopen(proffile,"w",datafile);
49 	if (datafile == NULL)
50 		goto skipprof;
51 	count = fwrite(&profdata,1,sizeof(profdata),datafile);
52 	if (count != sizeof(profdata))
53 		goto skipprof;
54 	fclose(datafile);
55 skipprof:
56 #	endif PROFILE
57 	l = tbuf.usr_time;
58 	l = l / HZ;
59 	fprintf(stderr,
60 		"\n%1ld statements executed in %04.2f seconds cpu time.\n",
61 		_stcnt,l);
62 }
63 
64 backtrace(errnum)
65 	long	errnum;
66 {
67 	register struct disp *mydp;
68 	register struct stack *ap;
69 	register char *cp;
70 	register long i, linum;
71 	struct disply disp;
72 
73 	if (_lino <= 0) {
74 		fputs("Program was not executed.\n",stderr);
75 		return;
76 	}
77 	disp = _display;
78 	if (errnum == PINTR)
79 		fputs("\n\tInterrupted in \"",stderr);
80 	else if (errnum == PHALT)
81 		fputs("\n\tHalted in \"",stderr);
82 	else
83 		fputs("\n\tError in \"",stderr);
84 	mydp = _dp;
85 	linum = _lino;
86 	for (;;) {
87 		ap = mydp->stp;
88 		i = linum - (((ap)->entry)->offset & 0177777);
89 		fprintf(stderr,"%s\"",(ap->entry)->name);
90 		if (_nodump == 0)
91 			fprintf(stderr,"+%1d near line %1d.",i,linum);
92 		fputc('\n',stderr);
93 		*mydp = (ap)->odisp;
94 		if (mydp <= &_display.frame[1]){
95 			_display = disp;
96 			psexit(errnum);
97 		}
98 		mydp = (ap)->dp;
99 		linum = (ap)->lino;
100 		fputs("\tCalled by \"",stderr);
101 	}
102 }
103 
104 psexit(code)
105 
106 	long	code;
107 {
108 	if (_pcpcount != 0)
109 		PMFLUSH(_cntrs, _rtns, _pcpcount);
110 	if (_mode == PIX) {
111 		fputs("Execution terminated",stderr);
112 		if (code)
113 			fputs(" abnormally",stderr);
114 		fputc('.',stderr);
115 		fputc('\n',stderr);
116 	}
117 	stats();
118 	exit(code);
119 }
120