home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / pascal / px / utilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-16  |  4.8 KB  |  184 lines

  1. /*-
  2.  * Copyright (c) 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)utilities.c    5.3 (Berkeley) 4/16/91";
  36. #endif /* not lint */
  37.  
  38. #include    <signal.h>
  39. #include    "whoami.h"
  40. #include    "vars.h"
  41. #include    "objfmt.h"
  42. #include    <sys/time.h>
  43. #include    <sys/resource.h>
  44.  
  45. stats()
  46. {
  47.     struct rusage ru;
  48.     register double l;
  49.     register long count;
  50. #    ifdef PROFILE
  51. #    define    proffile    "/vb/grad/mckusick/px/profile/pcnt.out"
  52.     struct cntrec {
  53.         double    counts[NUMOPS];    /* instruction counts */
  54.         long    runs;        /* number of interpreter runs */
  55.         long    startdate;    /* date profile started */
  56.         long    usrtime;    /* total user time consumed */
  57.         long    systime;    /* total system time consumed */
  58.         double    stmts;        /* number of pascal stmts executed */
  59.     } profdata;
  60.     FILE *datafile;
  61. #    endif PROFILE
  62.  
  63.     if (_nodump)
  64.         return(0);
  65.     getrusage(RUSAGE_SELF, &ru);
  66. #    ifdef PROFILE
  67.     datafile = fopen(proffile,"r");
  68.     if (datafile == NULL)
  69.         goto skipprof;
  70.     count = fread(&profdata,1,sizeof(profdata),datafile);
  71.     if (count != sizeof(profdata))
  72.         goto skipprof;
  73.     for (count = 0;  count < NUMOPS;  count++)
  74.         profdata.counts[count] += _profcnts[count];
  75.     profdata.runs += 1;
  76.     profdata.stmts += _stcnt;
  77.     profdata.usrtime += ru.ru_utime.tv_sec;
  78.     profdata.systime += ru.ru_stime.tv_sec;
  79.     datafile = freopen(proffile,"w",datafile);
  80.     if (datafile == NULL)
  81.         goto skipprof;
  82.     count = fwrite(&profdata,1,sizeof(profdata),datafile);
  83.     if (count != sizeof(profdata))
  84.         goto skipprof;
  85.     fclose(datafile);
  86. skipprof:
  87. #    endif PROFILE
  88.     fprintf(stderr,
  89.         "\n%1ld statements executed in %d.%02d seconds cpu time.\n",
  90.         _stcnt, ru.ru_utime.tv_sec, ru.ru_utime.tv_usec / 10000);
  91. }
  92.  
  93. backtrace(type)
  94.     char    *type;
  95. {
  96.     register struct dispsave *mydp;
  97.     register struct blockmark *ap;
  98.     register char *cp;
  99.     register long i, linum;
  100.     union display disp;
  101.  
  102.     if (_lino <= 0) {
  103.         fputs("Program was not executed.\n",stderr);
  104.         return;
  105.     }
  106.     disp = _display;
  107.     fprintf(stderr, "\n\t%s in \"", type);
  108.     mydp = _dp;
  109.     linum = _lino;
  110.     for (;;) {
  111.         ap = mydp->stp;
  112.         i = linum - (((ap)->entry)->offset & 0177777);
  113.         fprintf(stderr,"%s\"",(ap->entry)->name);
  114.         if (_nodump == FALSE)
  115.             fprintf(stderr,"+%D near line %D.",i,linum);
  116.         fputc('\n',stderr);
  117.         *mydp = (ap)->odisp;
  118.         if (mydp <= &_display.frame[1]){
  119.             _display = disp;
  120.             return;
  121.         }
  122.         mydp = (ap)->dp;
  123.         linum = (ap)->lino;
  124.         fputs("\tCalled by \"",stderr);
  125.     }
  126. }
  127.  
  128. psexit(code)
  129.  
  130.     int    code;
  131. {
  132.     if (_pcpcount != 0)
  133.         PMFLUSH(_cntrs, _rtns, _pcpcount);
  134.     if (_mode == PIX) {
  135.         fputs("Execution terminated",stderr);
  136.         if (code)
  137.             fputs(" abnormally",stderr);
  138.         fputc('.',stderr);
  139.         fputc('\n',stderr);
  140.     }
  141.     stats();
  142.     exit(code);
  143. }
  144.  
  145. /*
  146.  * Routines to field various types of signals
  147.  *
  148.  * catch a library error and generate a backtrace
  149.  */
  150. liberr()
  151. {
  152.     backtrace("Error");
  153.     psexit(2);
  154. }
  155.  
  156. /*
  157.  * catch an interrupt and generate a backtrace
  158.  */
  159. intr()
  160. {
  161.     signal(SIGINT, intr);
  162.     backtrace("Interrupted");
  163.     psexit(1);
  164. }
  165.  
  166. /*
  167.  * catch memory faults
  168.  */
  169. memsize()
  170. {
  171.     signal(SIGSEGV, memsize);
  172.     ERROR("Run time stack overflow\n");
  173. }
  174.  
  175. /*
  176.  * catch random system faults
  177.  */
  178. syserr(signum)
  179.     int signum;
  180. {
  181.     signal(signum, syserr);
  182.     ERROR("Panic: Computational error in interpreter\n");
  183. }
  184.