File: _debug.c

package info (click to toggle)
rscheme 0.7.2-1.1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 10,672 kB
  • ctags: 12,430
  • sloc: lisp: 37,104; ansic: 29,763; cpp: 2,630; sh: 1,677; makefile: 568; yacc: 202; lex: 175; perl: 33
file content (422 lines) | stat: -rw-r--r-- 8,850 bytes parent folder | download | duplicates (4)
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*-----------------------------------------------------------------*-C-*---
 * File:    handc/runtime/_debug.c
 *
 *          Copyright (C)1997 Donovan Kolbly <d.kolbly@rscheme.org>
 *          as part of the RScheme project, licensed for free use.
 *          See <http://www.rscheme.org/> for the latest information.
 *
 * File version:     1.10
 * File mod date:    1997.11.29 23:10:48
 * System build:     v0.7.2, 97.12.21
 *
 * Purpose:          Low-level (C) debugging hooks
 *------------------------------------------------------------------------*/

#include <rscheme/runtime.h>
#include <rscheme/hashmain.h>
#include <setjmp.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include <rscheme/scheme.h>
#include <rscheme/smemory.h>

/*
    a BoundedStringPort is a device used for writing structures to
    a string that only has so much space.  The jmp_buf "escape"
    is the C equivalent to an exit continuation that would be used
    in implementing "with-output-to-truncated-string", because we
    have to actually ABORT any write operations that try to make
    it to big (you can't just ignore them, because you may be trying
    to a write a circular structure, or something like that)
    
    Eventually, [almost] everything in this file should be brought up
    to the Scheme level.
*/

typedef struct _BoundedStringPort {
    char	*ptr;
    char	*limit;
    char	*base;
    jmp_buf	escape;
} BSP;

static void pdisplay( BSP *dest, obj item );

static void BSPWriteChar( BSP *port, char ch )
{
    if (port->ptr >= port->limit)
	longjmp( port->escape, 1 );
    *(port->ptr)++ = ch;
}

static void BSPWriteStr( BSP *port, const char *str )
{
char *dest = port->ptr;
char *limit = port->limit;

    while (*(UINT_8 *)str)
    {
	if (dest >= limit)
	    longjmp( port->escape, 2 );
	*dest++ = *str++;
    }
    port->ptr = dest;
}

static void BSPWriteInt( BSP *port, int value )
{
char temp[20];

    sprintf( temp, "%d", value );
    BSPWriteStr( port, temp );
}

static void BSPWriteHex( BSP *port, unsigned value )
{
char temp[20];

    sprintf( temp, "%x", value );
    BSPWriteStr( port, temp );
}

static void BSPWritev( BSP *port, const char *fmt, va_list a )
{
    while (*fmt)
    {
	if (*fmt == '%')
	{
	    switch (*++fmt)
	    {
		case 's':	BSPWriteStr( port, va_arg( a, char * ) );
				break;
		case 'c':	BSPWriteChar( port, va_arg( a, int ) );
				break;
		case 'd':	BSPWriteInt( port, va_arg( a, int ) );
				break;
		case 'x':	BSPWriteHex( port, va_arg( a, unsigned ) );
				break;
		case 'p':	BSPWriteStr( port, "0x" );
				BSPWriteHex( port, va_arg( a, unsigned ) );
				break;
		case 'o':	pdisplay( port, va_arg( a, obj ) );
				break;
		default:
				BSPWriteChar( port, *fmt );
				break;
	    }
	    fmt++;
	}
	else
	    BSPWriteChar( port, *fmt++ );
    }
}

static void BSPWrite( BSP *port, const char *fmt, ... )
{
va_list a;

    va_start( a, fmt );
    BSPWritev( port, fmt, a );
    va_end( a );
}

static void BSPInit( BSP *port, char *buffer, int size )
{
    port->base = port->ptr = buffer;
    port->limit = buffer + size - 1;
}

static char *BSPClose( BSP *port )
{
    *port->ptr = 0;
    return port->base;
}

static void fixnum_display( BSP *dest, obj item )
{
    BSPWriteInt( dest, FIXNUM_TO_RAWINT(item) );
}

static void vector_display( BSP *dest, obj item )
{
obj i, n;
rs_bool flag;

    BSPWriteStr( dest, "#(" );
    n = OBJ(SIZEOF_PTR(item));
    i = ZERO;
    flag = NO;
    while (VAL(i)<VAL(n))
    {
	if (flag)
	    BSPWriteChar( dest, ' ' );
	pdisplay( dest, gvec_read( item, VAL(i) ) );
	i = ADD1( i );
	flag = YES;
    }
    BSPWriteStr( dest, ")" );
}

static void ascii_char_display( BSP *dest, obj item )
{
unsigned char ch = GET_IMMEDIATE_VALUE(item);

    if (isgraph(ch))
	BSPWrite( dest, "#\\%c", ch );
    else if (ch == '\n')
	BSPWrite( dest, "#\\newline" );
    else if (ch == '\t')
	BSPWrite( dest, "#\\tab" );
    else if (ch == ' ')
	BSPWrite( dest, "#\\space" );
    else
	BSPWrite( dest, "#/%d", ch );
}


static void class_display( BSP *dest, obj the_class )
{
    BSPWrite( dest, "#[<<Class>> %s", 
	symbol_text(class_name(the_class)) );

    if (!class_is_gvec(the_class))
	BSPWriteStr( dest, " (bvec)" );
    BSPWriteStr( dest, "]" );
}

static enum secondary_tag get_secondary_tag( obj item )
{
    return (enum secondary_tag)
    		((VAL(item) & SECONDARY_TAG_MASK) >> PRIMARY_TAG_SIZE);
}

static void pdisplay( BSP *dest, obj item )
{

    if (OBJ_ISA_FIXNUM(item))
	fixnum_display( dest, item );
    else if (OBJ_ISA_IMMOB(item))
    {
	switch (get_secondary_tag(item))
	{
	    case BOOLEAN_TAG:
				BSPWriteStr( dest, 
					     EQ(item,FALSE_OBJ) 
						    ? "#f" 
						    : "#t" );
				break;
	    case NIL_TAG:
				BSPWriteStr( dest, "()" );
				break;
	    case ASCII_CHAR_TAG:
				ascii_char_display( dest, item );
				break;
	    case UNICODE_CHAR_TAG:
				BSPWrite( dest, 
					  "#\\U%d", 
					  GET_IMMEDIATE_VALUE(item) );
				break;
	    case UNIQUE_OBJ_TAG:
	     switch (GET_IMMEDIATE_VALUE(item))
	       {
	       case UNDEFINED_TAG:
		 BSPWriteStr( dest, "#undef" );
		 break;
	       case UNINITIALIZED_TAG:
		 BSPWriteStr( dest, "#uninit" );
		 break;
	       case NOVALUE_TAG:
		 BSPWriteStr( dest, "#none" );
		 break;
	       case UNBOUND_TAG:
		 BSPWriteStr( dest, "#unbound" );
		 break;
	       default:
		 BSPWrite( dest, "#[huh? %d.%d.%d]", 
			  GET_IMMEDIATE_VALUE(item),
			  get_secondary_tag(item),
			  VAL(item) & PRIMARY_TAG_MASK );	 
	       }
		break;
	    default:
				BSPWrite( dest, "#[huh? %d.%d.%d]", 
					    GET_IMMEDIATE_VALUE(item),
					    get_secondary_tag(item),
					    VAL(item) & PRIMARY_TAG_MASK );
	}
    }
    else if (OBJ_ISA_PTR(item))
    {
	assert( CLASS_P(CLASSOF_PTR(item)) );
	if (PAIR_P(item))
	{
	    BSPWriteStr( dest, "(" );
	    pdisplay( dest, pair_car(item) );
	    while (PAIR_P(pair_cdr(item)))
	    {
		BSPWriteChar( dest, ' ' );
		item = pair_cdr(item);
		pdisplay( dest, pair_car(item) );
	    }
	    if (!NULL_P(pair_cdr(item)))
	    {
		BSPWriteStr( dest, " . " );
		pdisplay( dest, pair_cdr(item) );
	    }
	    BSPWriteStr( dest, ")" );
	}
	else if (SYMBOL_P(item))
	{
	    BSPWriteStr( dest, symbol_text(item) );
	}
	else if (STRING_P(item))
	{
	const char *s = string_text(item);
	
	    BSPWriteChar( dest, '\"' );
	    while (*s)
	    {
		if (*s == '\n')
		    BSPWriteStr( dest, "\\n" );
		else if (*s == '\t')
		    BSPWriteStr( dest, "\\t" );
		else
		    BSPWriteChar( dest, *s );
		s++;
	    }
	    BSPWriteChar( dest, '\"' );
	}
	else if (FUNCTION_P(item))
	{
	    BSPWrite( dest, "#[<Closure>]" );
	}
	else if (TEMPLATE_P(item))
	{
	
	    BSPWrite( dest, "#[<Template>]" );
	}
	else if (LONGFLOAT_P(item))
	{
	char temp[50];
	
	    sprintf( temp, "%g", extract_float(item) );
	    BSPWriteStr( dest, temp );
	}
	else if (VECTOR_P(item))
	{
	    vector_display( dest, item );
	}
#if 0
	else if (HASHTABLE_P(item))
	{
	    hash_table_display( dest, item );
	}
#endif
	else if (CLASS_P(item))
	{
	    class_display( dest, item );
	}
	else
	{
	int i, n;
	obj c = object_class(item);
	
	    n = SIZEOF_PTR(item) / sizeof(obj);
	    BSPWrite( dest, "#[%s", symbol_text(class_name(c)) );
	    if (EQ(gvec_read(c,SLOT(1)),FALSE_OBJ))
	    {
		for (i=0; i<n; i++)
		{
		    BSPWriteChar( dest, ' ' );
		    pdisplay( dest, gvec_read(item,SLOT(i)) );
		}
	    }
	    else
		BSPWrite( dest, " %d bytes", SIZEOF_PTR(item) );
	    BSPWriteStr( dest, "]" );
	}
    }
    else
	BSPWrite( dest, "#[unknown:%x.%x]", 
			VAL(item) >> PRIMARY_TAG_SIZE,
			VAL(item) & PRIMARY_TAG_MASK );
}

void fnprinto( FILE *dest, obj item, unsigned n )
{
BSP port;
char *volatile buffer;
char buff[1000];

    if (n > 1000)
      n = 1000;

    buffer = buff;

    BSPInit( &port, buffer, n-4 );
    if (!setjmp(port.escape))
    {
	pdisplay( &port, item );
	fputs( BSPClose( &port ), dest );
    }
    else
    {
        fputs( BSPClose(&port), dest );
	fputs( "...", dest );
    }
}

void fprinto( FILE *dest, obj item )
{
  fnprinto( dest, item, 1000 );
}

void debug( obj item )
{
    printf( "debug ::= " );
    fnprinto( stdout, item, 200 );
    putchar( '\n' );
}

void fdebug_slots( FILE *f, obj item )
{
  fprintf( f, "-- <%#x>. --\n ==> ", VAL(item) );
  fprinto( f, item );
  fprintf( f, "\n" );
  if (GVEC_P(item))
    {
      int k, len = SIZEOF_PTR(item);
      
      for (k=-SLOT(1); k<len; k+=SLOT(1))
	{
	  obj t;

	  if (k < 0)
	    {
	      fprintf( f, " class = " );
	      t = CLASSOF_PTR(item);
	    }
	  else
	    {
	      fprintf( f, "   [%u] = ", (unsigned)(k/SLOT(1)) );
	      t = gvec_ref( item, k );
	    }

	  if (OBJ_ISA_PTR(t))
	    {
	      fprintf( f, "<%#x> = ", VAL(t) );
	    }
	  fprinto( f, t );
	  fputc( '\n', f );
	}
      fputc( '\n', f );
    }
}

void debug_slots( obj item )
{
  fdebug_slots( stdout, item );
}