home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / m0 / part04 / l_dump.c next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  5.8 KB  |  276 lines

  1. /*
  2.     l_dump.c
  3. */
  4. /*  Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
  5.  
  6.     Distributed under the terms of the GNU General Public License
  7.     version 2 of june 1991 as published by the Free Software
  8.     Foundation, Inc.
  9.  
  10.              This file is part of M0.
  11.  
  12. M0 is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY.  No author or distributor accepts responsibility to anyone for
  14. the consequences of using it or for whether it serves any particular
  15. purpose or works at all, unless he says so in writing.  Refer to the GNU
  16. General Public License for full details. 
  17.  
  18. Everyone is granted permission to copy, modify and redistribute M0, but
  19. only under the conditions described in the GNU General Public License. 
  20. A copy of this license is supposed to have been given to you along with
  21. M0 so you can know your rights and responsibilities.  It should be in a
  22. file named LICENSE.  Among other things, the copyright notice and this
  23. notice must be preserved on all copies.  */
  24.  
  25. #ifdef DEBUG
  26.  
  27. #include "l_proto.h"
  28.  
  29. int trace;
  30.  
  31.  
  32. static void
  33. dump_e(FILE *f, mproc p, eindex ei, int depth)
  34. {
  35.     eptr ep = eaddr(p,ei);
  36.     char fmt[10];
  37.     int i, j;
  38.     eindex *ea;
  39.     mproc p2;
  40.  
  41.     for (i=0; i<depth; i++)
  42.         fprintf(f, "    ");
  43.  
  44.     if (!ei) {
  45.         fprintf(f, "%4d --- (invalid element)\n", ei);
  46.         return;
  47.     }
  48.         fprintf(f, "%5d %4d %4d %c%c%c %c%c%c", ei,
  49.             (int) eplen(ep), (int) eprefcnt(ep), 
  50.             epattr(ep)&A_READ?'r':'_',
  51.             epattr(ep)&A_WRITE?'w':'_',
  52.             epattr(ep)&A_EXEC?'x':'_',
  53.             epattr(ep)&A_EXECUTABLE?'!':'_',
  54.             epattr(ep)&A_SUB?'s':'_',
  55.             epattr(ep)&A_FRAG?'f':'_'
  56.         );
  57.     if (eptype(ep)<LAST_TYPE)
  58.         fprintf(f, " %-7s: ", type_names[eptype(ep)]);
  59.     else
  60.         fprintf(f, " ?%-05d?: ", eptype(ep));
  61.     switch (eptype(ep)) {
  62.         case T_ARRAY:
  63.         if (epattr(ep)&A_SUB)
  64.             fprintf(f, "(%d, offs=%d)", ep->V.sub.e, ep->V.sub.offset);
  65.         if (eplen(ep) == 0)
  66.             fprintf(f, "[]");
  67.         else {
  68.             fprintf(f, "[ ");
  69.             for (i=0; i<eplen(ep); i++)
  70.                 fprintf(f, "%d ", array_get(p,ei,i));
  71.             fprintf(f, "]");
  72.         }
  73.         break;
  74.         case T_DICT:
  75.         if (epattr(ep)&A_SUB) {
  76.             fprintf(f, "(%d) ", ep->V.sub.e);
  77.             ei = desub(p, ei);
  78.             ep = eaddr(p,ei);
  79.         }
  80.         fprintf(f, "\n");
  81.         for (i=ep->V.dic.alen, ea = ep->V.dic.d; i>0; i--, ea+=2)
  82.             if (*ea && *ea != DICT_DELETED) {
  83.                 for (j=0; j<depth; j++)
  84.                     fprintf(f, "    ");
  85.                 fprintf(f, "      %3d --> %3d\n", *ea, *(ea+1));
  86.             }
  87.         return;
  88.         case T_INT:
  89. #ifdef __MSDOS__
  90.         fprintf(f, "%ld", ep->V.i); break;
  91. #else
  92.         fprintf(f, "%d", ep->V.i); break;
  93. #endif
  94.         case T_KEY:
  95.         if (epattr(ep)&A_SUB) {
  96.             fprintf(f, "(%d) ", ep->V.sub.e);
  97.             ei = desub(p, ei);
  98.             ep = eaddr(p,ei);
  99.         }
  100.         fprintf(f, "\\");
  101.         for (i=0; i<8; i++)
  102.             fprintf(f, "%02x", ep->V.nam.u.s[i]);
  103.         fprintf(f, "\\");
  104.         break;
  105.         case T_NAME:
  106.         if (epattr(ep)&A_SUB) {
  107.             fprintf(f, "(%d) ", ep->V.sub.e);
  108.             ei = desub(p, ei);
  109.             ep = eaddr(p,ei);
  110.         }
  111.         sprintf(fmt, "%%.%ds", (int) eplen(ep));
  112.         if (eplen(ep) <= SHORTNAMELEN)
  113.             fprintf(f, fmt, ep->V.nam.u.n);
  114.         else
  115.             fprintf(f, fmt, ep->V.nam.u.s);
  116.         break;
  117.         case T_QUEUE:
  118.         if (!ep->V.que.head)
  119.             fprintf(f, "pids: -- /");
  120.         else
  121.             fprintf(f, "pids: %d /", ep->V.que.head->pid);
  122.         if (!ep->V.que.tail)
  123.             fprintf(f, " --");
  124.         else for (p2 = ep->V.que.tail; p2; p2 = p2->qtail)
  125.                 fprintf(f, " %d", p2->pid);
  126.         break;
  127.         case T_STRING:
  128.         if (epattr(ep)&A_SUB)
  129.             fprintf(f, "(%d, offs=%d) ", ep->V.sub.e, ep->V.sub.offset);
  130.         if (epattr(ep)&A_FRAG)
  131.             fprintf(f, "(%d+%d) ", ep->V.fra.f[0], ep->V.fra.f[1]);
  132.         fprintf(f, "\"");
  133.         for (i=0;i<eplen(ep);i++) {
  134.             int c = str_get(p, ei, i);
  135.             fprintf(f,!isprint(c) ? "\\x%02x" : "%c", c);
  136.         }
  137.         fprintf(f, "\"");
  138.         break;
  139.         case T_TIME:
  140. #ifdef __MSDOS__
  141.         fprintf(f, "%ld + %ld", ep->V.tim.sec, ep->V.tim.usec);
  142. #else
  143.         fprintf(f, "%d + %d", ep->V.tim.sec, ep->V.tim.usec);
  144. #endif
  145.         break;
  146.         default: break;
  147.     }
  148.     fprintf(f, "\n");
  149. }
  150.  
  151.  
  152. void
  153. dump_element(FILE *f, mproc p, eindex ei)
  154. {
  155.     dump_e(f, p, ei, 0);
  156. }
  157.  
  158. void
  159. dump_elements(FILE *f, mproc p)
  160. {
  161.     eptr ep;
  162.     int lim, i;
  163.  
  164.     if (!p) {
  165.         lim = MAXGLOBALS;
  166.         ep = global;
  167.         fprintf(f, "Global elements:\n");
  168.     } else {
  169.         lim = MAXLOCALS;
  170.         ep = p->local;
  171.         fprintf(f, "Local elements:\n");
  172.     }
  173.     for (i=0; i<lim; i++, ep++) {
  174.         if (eptype(ep) != T_EMPTY)
  175.             dump_e(f, p, p ? i+1 : -i-1, 0);
  176.     }
  177. }
  178.  
  179. void
  180. dump_stack(FILE *f, eindex *s, ushort lim)
  181. {
  182.     ushort i;
  183.  
  184.     fprintf(f, "[");
  185.     for (i=lim; i>0; i--, s++)
  186.         if (*s)
  187.             fprintf(f, " %d", *s);
  188.     fprintf(f, " ]\n");
  189. }
  190.  
  191. void
  192. dump_elements_to_file(char *fn, mproc p)
  193. {
  194.     FILE *f = fopen(fn, "w");
  195.  
  196.     if (!f)
  197.         return;
  198.  
  199.     dump_elements(f, p);
  200.  
  201.     fclose(f);
  202. }
  203.  
  204.  
  205. void
  206. dump_process(FILE *f, mproc p)
  207. {
  208.     fprintf(f, "*** Dump of process %d\n", p->pid);
  209.     fprintf(f, "state: %s\n",
  210.         !p->state ? "run" :
  211.         p->state==S_BLOCKED ? "block" : "term");
  212.     if (p->last_error == OK)
  213.         fprintf(f, "interpretation ok!\n");
  214.     else {
  215.         fprintf(f, "last error of this process: %d (%s)\n",
  216.             p->last_error, error_names[p->last_error]);
  217.         fprintf(f, "error element is %d\n", p->err_element);
  218.     }
  219.     fprintf(f, "process queue: %d\n", p->qkey);
  220.     fprintf(f, "oper stack: ");
  221.     dump_stack(f, p->os, p->osp);
  222.     fprintf(f, "dict stack: ");
  223.     dump_stack(f, p->ds, p->dsp);
  224.     fprintf(f, "exec stack: ");
  225.     dump_stack(f, p->es, p->esp);
  226. }
  227.  
  228.  
  229. void
  230. dump_process_to_file(char *fn, mproc p)
  231. {
  232.     FILE *f = fopen(fn, "w");
  233.  
  234.     if (!f)
  235.         return;
  236.  
  237.     dump_process(f, p);
  238.     dump_elements(f, p);
  239.     dump_elements(f, 0);
  240.     fclose(f);
  241. }
  242.  
  243. #endif /* DEBUG */
  244.  
  245.  
  246. void terminate(int s)
  247. {
  248.     printf("\n");
  249.  
  250. #ifdef DEBUG
  251.     if (trace > 0) {
  252.         char *fn = unique_filename("exit");
  253.         FILE *f = fopen(fn, "w");
  254.  
  255.         if (f) {
  256.             mproc p = current;
  257.  
  258.             printf("## dumping memory to file %s\n", fn);
  259.             while (p) {
  260.                 dump_process(f, p);
  261.                 dump_elements(f, p);
  262.                 p = p->next;
  263.                 if (p == current)
  264.                     break;
  265.             }
  266.             fprintf(f, "\n## global memory:\n");
  267.             dump_elements(f, 0);
  268.             fclose(f);
  269.         }
  270.     }
  271. #endif
  272.  
  273.     printf("## M0 platform ends.\n");
  274.     exit(0);
  275. }
  276.