home *** CD-ROM | disk | FTP | other *** search
- /*
- l_dump.c
- */
- /* Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
-
- Distributed under the terms of the GNU General Public License
- version 2 of june 1991 as published by the Free Software
- Foundation, Inc.
-
- This file is part of M0.
-
- M0 is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY. No author or distributor accepts responsibility to anyone for
- the consequences of using it or for whether it serves any particular
- purpose or works at all, unless he says so in writing. Refer to the GNU
- General Public License for full details.
-
- Everyone is granted permission to copy, modify and redistribute M0, but
- only under the conditions described in the GNU General Public License.
- A copy of this license is supposed to have been given to you along with
- M0 so you can know your rights and responsibilities. It should be in a
- file named LICENSE. Among other things, the copyright notice and this
- notice must be preserved on all copies. */
-
- #ifdef DEBUG
-
- #include "l_proto.h"
-
- int trace;
-
-
- static void
- dump_e(FILE *f, mproc p, eindex ei, int depth)
- {
- eptr ep = eaddr(p,ei);
- char fmt[10];
- int i, j;
- eindex *ea;
- mproc p2;
-
- for (i=0; i<depth; i++)
- fprintf(f, " ");
-
- if (!ei) {
- fprintf(f, "%4d --- (invalid element)\n", ei);
- return;
- }
- fprintf(f, "%5d %4d %4d %c%c%c %c%c%c", ei,
- (int) eplen(ep), (int) eprefcnt(ep),
- epattr(ep)&A_READ?'r':'_',
- epattr(ep)&A_WRITE?'w':'_',
- epattr(ep)&A_EXEC?'x':'_',
- epattr(ep)&A_EXECUTABLE?'!':'_',
- epattr(ep)&A_SUB?'s':'_',
- epattr(ep)&A_FRAG?'f':'_'
- );
- if (eptype(ep)<LAST_TYPE)
- fprintf(f, " %-7s: ", type_names[eptype(ep)]);
- else
- fprintf(f, " ?%-05d?: ", eptype(ep));
- switch (eptype(ep)) {
- case T_ARRAY:
- if (epattr(ep)&A_SUB)
- fprintf(f, "(%d, offs=%d)", ep->V.sub.e, ep->V.sub.offset);
- if (eplen(ep) == 0)
- fprintf(f, "[]");
- else {
- fprintf(f, "[ ");
- for (i=0; i<eplen(ep); i++)
- fprintf(f, "%d ", array_get(p,ei,i));
- fprintf(f, "]");
- }
- break;
- case T_DICT:
- if (epattr(ep)&A_SUB) {
- fprintf(f, "(%d) ", ep->V.sub.e);
- ei = desub(p, ei);
- ep = eaddr(p,ei);
- }
- fprintf(f, "\n");
- for (i=ep->V.dic.alen, ea = ep->V.dic.d; i>0; i--, ea+=2)
- if (*ea && *ea != DICT_DELETED) {
- for (j=0; j<depth; j++)
- fprintf(f, " ");
- fprintf(f, " %3d --> %3d\n", *ea, *(ea+1));
- }
- return;
- case T_INT:
- #ifdef __MSDOS__
- fprintf(f, "%ld", ep->V.i); break;
- #else
- fprintf(f, "%d", ep->V.i); break;
- #endif
- case T_KEY:
- if (epattr(ep)&A_SUB) {
- fprintf(f, "(%d) ", ep->V.sub.e);
- ei = desub(p, ei);
- ep = eaddr(p,ei);
- }
- fprintf(f, "\\");
- for (i=0; i<8; i++)
- fprintf(f, "%02x", ep->V.nam.u.s[i]);
- fprintf(f, "\\");
- break;
- case T_NAME:
- if (epattr(ep)&A_SUB) {
- fprintf(f, "(%d) ", ep->V.sub.e);
- ei = desub(p, ei);
- ep = eaddr(p,ei);
- }
- sprintf(fmt, "%%.%ds", (int) eplen(ep));
- if (eplen(ep) <= SHORTNAMELEN)
- fprintf(f, fmt, ep->V.nam.u.n);
- else
- fprintf(f, fmt, ep->V.nam.u.s);
- break;
- case T_QUEUE:
- if (!ep->V.que.head)
- fprintf(f, "pids: -- /");
- else
- fprintf(f, "pids: %d /", ep->V.que.head->pid);
- if (!ep->V.que.tail)
- fprintf(f, " --");
- else for (p2 = ep->V.que.tail; p2; p2 = p2->qtail)
- fprintf(f, " %d", p2->pid);
- break;
- case T_STRING:
- if (epattr(ep)&A_SUB)
- fprintf(f, "(%d, offs=%d) ", ep->V.sub.e, ep->V.sub.offset);
- if (epattr(ep)&A_FRAG)
- fprintf(f, "(%d+%d) ", ep->V.fra.f[0], ep->V.fra.f[1]);
- fprintf(f, "\"");
- for (i=0;i<eplen(ep);i++) {
- int c = str_get(p, ei, i);
- fprintf(f,!isprint(c) ? "\\x%02x" : "%c", c);
- }
- fprintf(f, "\"");
- break;
- case T_TIME:
- #ifdef __MSDOS__
- fprintf(f, "%ld + %ld", ep->V.tim.sec, ep->V.tim.usec);
- #else
- fprintf(f, "%d + %d", ep->V.tim.sec, ep->V.tim.usec);
- #endif
- break;
- default: break;
- }
- fprintf(f, "\n");
- }
-
-
- void
- dump_element(FILE *f, mproc p, eindex ei)
- {
- dump_e(f, p, ei, 0);
- }
-
- void
- dump_elements(FILE *f, mproc p)
- {
- eptr ep;
- int lim, i;
-
- if (!p) {
- lim = MAXGLOBALS;
- ep = global;
- fprintf(f, "Global elements:\n");
- } else {
- lim = MAXLOCALS;
- ep = p->local;
- fprintf(f, "Local elements:\n");
- }
- for (i=0; i<lim; i++, ep++) {
- if (eptype(ep) != T_EMPTY)
- dump_e(f, p, p ? i+1 : -i-1, 0);
- }
- }
-
- void
- dump_stack(FILE *f, eindex *s, ushort lim)
- {
- ushort i;
-
- fprintf(f, "[");
- for (i=lim; i>0; i--, s++)
- if (*s)
- fprintf(f, " %d", *s);
- fprintf(f, " ]\n");
- }
-
- void
- dump_elements_to_file(char *fn, mproc p)
- {
- FILE *f = fopen(fn, "w");
-
- if (!f)
- return;
-
- dump_elements(f, p);
-
- fclose(f);
- }
-
-
- void
- dump_process(FILE *f, mproc p)
- {
- fprintf(f, "*** Dump of process %d\n", p->pid);
- fprintf(f, "state: %s\n",
- !p->state ? "run" :
- p->state==S_BLOCKED ? "block" : "term");
- if (p->last_error == OK)
- fprintf(f, "interpretation ok!\n");
- else {
- fprintf(f, "last error of this process: %d (%s)\n",
- p->last_error, error_names[p->last_error]);
- fprintf(f, "error element is %d\n", p->err_element);
- }
- fprintf(f, "process queue: %d\n", p->qkey);
- fprintf(f, "oper stack: ");
- dump_stack(f, p->os, p->osp);
- fprintf(f, "dict stack: ");
- dump_stack(f, p->ds, p->dsp);
- fprintf(f, "exec stack: ");
- dump_stack(f, p->es, p->esp);
- }
-
-
- void
- dump_process_to_file(char *fn, mproc p)
- {
- FILE *f = fopen(fn, "w");
-
- if (!f)
- return;
-
- dump_process(f, p);
- dump_elements(f, p);
- dump_elements(f, 0);
- fclose(f);
- }
-
- #endif /* DEBUG */
-
-
- void terminate(int s)
- {
- printf("\n");
-
- #ifdef DEBUG
- if (trace > 0) {
- char *fn = unique_filename("exit");
- FILE *f = fopen(fn, "w");
-
- if (f) {
- mproc p = current;
-
- printf("## dumping memory to file %s\n", fn);
- while (p) {
- dump_process(f, p);
- dump_elements(f, p);
- p = p->next;
- if (p == current)
- break;
- }
- fprintf(f, "\n## global memory:\n");
- dump_elements(f, 0);
- fclose(f);
- }
- }
- #endif
-
- printf("## M0 platform ends.\n");
- exit(0);
- }
-