File: debug.c

package info (click to toggle)
klic 3.003-1.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 7,068 kB
  • ctags: 6,333
  • sloc: ansic: 101,584; makefile: 3,395; sh: 1,321; perl: 312; exp: 131; tcl: 111; asm: 102; lisp: 4; sed: 1
file content (387 lines) | stat: -rw-r--r-- 7,635 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* ---------------------------------------------------------- 
%   (C)1993,1994,1995 Institute for New Generation Computer Technology 
%       (Read COPYRIGHT for detailed information.) 
%   (C)1996, 1997, 1998, 1999 Japan Information Processing Development Center
%       (Read COPYRIGHT-JIPDEC for detailed information.)
----------------------------------------------------------- */
#include <stdio.h>
#include <setjmp.h>
#include <ctype.h>
#include <errno.h>
#include <klic/basic.h>
#include <klic/struct.h>
#include <klic/primitives.h>
#include <klic/unify.h>
#include <klic/index.h>
#include <klic/atomstuffs.h>
#include <klic/functorstuffs.h>

#ifdef DIST
#include <klic/distio.h>
#include <klic/interpe.h>
#endif

#ifdef SHM
#include <klic/shm.h>
#endif

/*
  Function name convention:

  klic_fprintf, klic_printf, ...: for system tracer, user, i.e.
  redirect stdout, stderr to the IO process.
  debug_fprintf, debug_printf, ...: debugging aids, i.e.
  DO NOT redirect stdout, stderr.

  iosXXXX: equivalent to klic_fprintf(stderr, ...)
  ioXXXX: equivalent to klic_fprintf(stdout, ...)
  ioeXXXX: equivalent to klic_fprintf(stderr, ...)
  */

extern struct predicate topsucceed_pred;
extern jmp_buf klic_topmost;

void fatal(message)
     char *message;
{
  declare_globals;

  klic_fprintf(stderr, "Fatal Error: %s\n", message);

#ifdef DIST
  /* klic_fprintf(stderr, "Node %d Going to dump core\n", my_node); */

  /*  ERROR_STOP; */

  DetachIO();
  EmergencyStop();
#endif

  if (postmortem_pred !=0 && postmortem_args !=0) {
    longjmp(klic_topmost, 1);
  }
#ifdef DIST
  Close_net();
#endif
#ifdef SHM
  abend("");
#endif
  exit(-1);
}

klic_exit(N)
int N;
{
#ifdef DIST
  kill_child();
#endif
#ifdef SHM
  abend("");
#endif
  exit(N);
}

void fatalf(format, a0, a1, a2, a3, a4, a5, a6, a7)
     char *format;
     long a0, a1, a2, a3, a4, a5, a6, a7;
{
  char buf[4096];
  (void) sprintf(buf, format, a0, a1, a2, a3, a4, a5, a6, a7);
  fatal(buf);
}

void fatalp(where, format, a0, a1, a2, a3, a4, a5, a6, a7)
     char *where, *format;
     long a0, a1, a2, a3, a4, a5, a6, a7;
{
#ifdef DIST
  char buf[4096];
  char *bufp;
#ifdef DECL_SYS_ERRLIST
  extern char *sys_errlist[];
#endif
  (void) sprintf(buf, "%s: %s\n", where, sys_errlist[errno]);
  bufp = buf + strlen(buf);
  (void) sprintf(bufp, format, a0, a1, a2, a3, a4, a5, a6, a7);
  fatal(buf);
#else
  perror(where);
  fatalf(format, a0, a1, a2, a3, a4, a5, a6, a7);
#endif
}

#ifdef DIST
/* inner functions for DISTRIBUTED version */
void 
iofprintf_inner(out, format, args)
FILE *out;
char *format;
va_list args;
{
  char io_buf[2048];
  declare_globals;
  vsprintf(io_buf, format, args);
  
  if ( Node_type() ) {
    if(out == stdout)
      Print(io_buf);
    else if(out == stderr)
      EPrint(io_buf);
    else
      fputs(io_buf, out);
  } else {
    fputs(io_buf, out );
  }
  va_end(args);
}

void
#ifdef USE_STDARG
iofprintf(FILE *out, char *format, ...)
#else
iofprintf(out, format, va_alist)
FILE *out;
char *format;
va_dcl
#endif
{
    va_list args;
    char io_buf[2048];
    declare_globals;

    VA_START(args, format);
    iofprintf_inner(out, format, args);
    va_end(args);
}


void
#ifdef USE_STDARG
ioprintf(char *format, ...)
#else
ioprintf(format, va_alist)
char *format;
va_dcl
#endif
{
    va_list args;
    char io_buf[2048];
    declare_globals;

    VA_START(args, format);
    iofprintf_inner(stdout, format, args);
    va_end(args);
}

void
#ifdef USE_STDARG
ioeprintf(char *format, ...)
#else
ioeprintf(format, va_alist)
char *format;
va_dcl
#endif
{
    va_list args;
    char io_buf[2048];
    declare_globals;

    VA_START(args, format);
    iofprintf_inner(stderr, format, args);
    va_end(args);
}

void
#ifdef USE_STDARG
iosprintf(char *format, ...)
#else
iosprintf(format, va_alist)
char *format;
va_dcl
#endif
{
    va_list args;
    char io_buf[2048];
    declare_globals;

    VA_START(args, format);
    iofprintf_inner(stderr, format, args);
    va_end(args);
}

static char *putbuf_fordist(redirect, bufp, buf, out)
int redirect;
char *bufp;
char *buf;
FILE *out;
{
  if(bufp != buf) {
    *bufp = '\0';
    if(redirect) {
      if(Node_type()) {
	/* If not IO node */
	if(out == stdout)
	  Print(buf);
	else if (out == stderr)
	  SystemPrint(buf);
	else
	  fputs(buf, out);
      } else {
	/* if IO node */
	fputs(buf, out);
      }
    } else {
      fputs(buf, out);
    }
    bufp = buf;
  }
  return bufp;
}
#endif  /* DIST */

#ifdef DECL_FPRINTF
#ifdef __STDC__
extern int fprintf(FILE *out, char *format, ...);
#else
extern int fprintf();
#endif
#endif

static Inline void 
klic_inner_fprintf(redirect, out, format,
		   a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab)
     int redirect; /* if redirect != 0 -> redirect stdout/err */
     FILE *out;
     char *format;
     long a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab;
{
  char *f;
#ifdef DIST
  char buf[4096];
  char *bufp = buf;
  declare_globals;
#endif

#ifdef __STDC__ 
  void (*fprintf_func)(FILE *out, char *format, ...);
#else
  void (*fprintf_func)();
#endif

  long args[12], *argp;
  args[0] = a0; args[1] = a1; args[2] = a2; args[3] = a3;
  args[4] = a4; args[5] = a5; args[6] = a6; args[7] = a7;
  args[8] = a8; args[9] = a9; args[10] = aa; args[11] = ab;
  argp = &args[0];

#ifdef DIST
  if(redirect) {
    fprintf_func = iofprintf;
  } else {
#endif
#ifdef __STDC__
    fprintf_func = (void (*)(FILE *, char *, ...))(fprintf);
#else
    fprintf_func = (void (*)())(fprintf);
#endif
#ifdef DIST
  }
#endif

  for (f = format; *f != '\0'; f++) {
    if (*f == '%') {
#ifdef DIST
      bufp = putbuf_fordist(redirect, bufp, buf, out);
#endif
      f++;
      switch (*f) {
      case 'k':
#ifdef DEBUGLIB
	fprint_partially(out, (q)*argp, 3, 10);
#else
	fprintf_func(out, "???");
#endif
	argp++;
	break;
      case 'F':
#ifdef DEBUGLIB
	(*fprintf_func)(out, "%s/%d",
			atomname[functors[*argp-FUNCTORNUMBERBASE]-
			      ATOMNUMBERBASE],
			arities[*argp++ -FUNCTORNUMBERBASE]);
#else
	(*fprintf_func)(out, "???/%d",
			arities[*argp++ -FUNCTORNUMBERBASE]);
#endif
	break;
      default:
	{
	  int k;
	  char fmtbuf[1024];
	  fmtbuf[0] = '%';
	  k=1;
	  while ((!isalpha(*f) || *f == 'l') && *f != 0) {
	    if (k>=1022) {
	      fatal("Too complicated format string for debug_printf");
	    }
	    fmtbuf[k++] = *f++;
	  }
	  fmtbuf[k++] = *f;
	  fmtbuf[k] = 0;
	  (*fprintf_func)(out, fmtbuf, *argp++);
	}
      }
    } else {
#ifdef DIST
      *bufp++ = *f;
#else
      klic_putc(*f, out);
#endif
    }
  }
#ifdef DIST
  bufp = putbuf_fordist(redirect, bufp, buf, out);
#endif
}

void
klic_fprintf(out, format,
		  a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab)
     FILE *out;
     char *format;
     long a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab;
{
  klic_inner_fprintf(1, out, format,
		     a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab);
}

klic_printf(format,
	    a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab)
     char *format;
     long a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab;
{
  klic_inner_fprintf(1, stdout, format,
		     a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab);
}

void
debug_fprintf(out, format,
		  a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab)
     FILE *out;
     char *format;
     long a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab;
{
  klic_inner_fprintf(0, out, format,
		     a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab);
}


void debug_printf(format,
		  a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab)
     char *format;
     long a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab;
{
  debug_fprintf(stderr, format,
		a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab);
}