home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / IDEBUG.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  7KB  |  256 lines

  1. /* Copyright (C) 1989, 1992, 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* idebug.c */
  20. /* Debugging support for Ghostscript interpreter */
  21. /* This file must always be compiled with DEBUG set. */
  22. #undef DEBUG
  23. #define DEBUG
  24. #include "string_.h"
  25. #include "ghost.h"
  26. #include "ialloc.h"        /* for imemory for getting struct type */
  27. #include "idebug.h"        /* defines interface */
  28. #include "idict.h"
  29. #include "iname.h"
  30. #include "ipacked.h"
  31. #include "istack.h"
  32. #include "iutil.h"
  33. #include "ostack.h"            /* for opdef.h */
  34. #include "opdef.h"
  35. #include "store.h"        /* for make_oper for opdef.h */
  36.  
  37. /* Table of type name strings */
  38. static const char *type_strings[] = { type_print_strings };
  39.  
  40. /* First unassigned type index */
  41. extern const int tx_next_index;        /* in interp.c */
  42.  
  43. /* Print a ref. */
  44. private void
  45. debug_print_full_ref(const ref *pref)
  46. {    unsigned size = r_size(pref);
  47.     ref nref;
  48.     dprintf1("(%x)", r_type_attrs(pref));
  49.     switch ( r_type(pref) )
  50.        {
  51.     case t_array:
  52.       dprintf2("array(%u)0x%lx", size, (ulong)pref->value.refs); break;
  53.     case t_astruct:
  54.       goto strct;
  55.     case t_boolean:
  56.       dprintf1("boolean %x", pref->value.boolval); break;
  57.     case t_device:
  58.       dprintf1("device 0x%lx", (ulong)pref->value.pdevice); break;
  59.     case t_dictionary:
  60.       dprintf3("dict(%u/%u)0x%lx",
  61.            dict_length(pref), dict_maxlength(pref),
  62.            (ulong)pref->value.pdict);
  63.       break;
  64.     case t_file:
  65.       dprintf1("file 0x%lx", (ulong)pref->value.pfile); break;
  66.     case t_fontID:
  67.       goto strct;
  68.     case t_integer:
  69.       dprintf1("int %ld", pref->value.intval); break;
  70.     case t_mark:
  71.       dprintf("mark"); break;
  72.     case t_mixedarray:
  73.       dprintf2("mixed packedarray(%u)0x%lx", size,
  74.            (ulong)pref->value.packed); break;
  75.     case t_name:
  76.       dprintf2("name(0x%lx#%x)", (ulong)pref->value.pname,
  77.            name_index(pref));
  78.       debug_print_name(pref);
  79.       break;
  80.     case t_null:
  81.       dprintf("null"); break;
  82.     case t_oparray:
  83.       dprintf1("op_array(0x%x)", size);
  84.       name_index_ref(op_array_nx_table[size - op_def_count], &nref);
  85.       debug_print_name(&nref);
  86.       break;
  87.     case t_operator:
  88.       dprintf1("op(0x%x", size);
  89.       if ( size > 0 && size < op_def_count ) /* just in case */
  90.         dprintf1(":%s", (const char *)(op_def_table[size]->oname + 1));
  91.       dprintf1(")0x%lx", (ulong)pref->value.opproc);
  92.       break;
  93.     case t_real:
  94.       dprintf1("real %f", pref->value.realval); break;
  95.     case t_save:
  96.       dprintf1("save %lu", pref->value.saveid); break;
  97.     case t_shortarray:
  98.       dprintf2("short packedarray(%u)0x%lx", size,
  99.            (ulong)pref->value.packed); break;
  100.     case t_string:
  101.       dprintf2("string(%u)0x%lx", size, (ulong)pref->value.bytes); break;
  102.     case t_struct:
  103. strct:      { obj_header_t *obj = (obj_header_t *)pref->value.pstruct;
  104.         dprintf2("struct %s 0x%lx",
  105.              (r_has_attr(pref, a_foreign) ? "-foreign-" :
  106.               gs_struct_type_name_string(gs_object_type(imemory, obj))),
  107.              (ulong)obj);
  108.       }
  109.       break;
  110.     default: dprintf1("type 0x%x", r_type(pref));
  111.        }
  112. }
  113. private void
  114. debug_print_packed_ref(const ref_packed *pref)
  115. {    ushort elt = *pref & packed_value_mask;
  116.     ref nref;
  117.     switch ( *pref >> packed_type_shift )
  118.     {
  119.     case pt_executable_operator:
  120.       dprintf("<op_name>");
  121.       op_index_ref(elt, &nref);
  122.       debug_print_ref(&nref);
  123.       break;
  124.     case pt_integer:
  125.       dprintf1("<int> %d", (int)elt + packed_min_intval);
  126.       break;
  127.     case pt_literal_name:
  128.       dprintf("<lit_name>"); goto ptn;
  129.     case pt_executable_name:
  130.       dprintf("<exec_name>");
  131. ptn:      name_index_ref(elt, &nref);
  132.       dprintf2("(0x%lx#%x)", (ulong)nref.value.pname, elt);
  133.       debug_print_name(&nref);
  134.       break;
  135.     default:
  136.       dprintf2("<packed_%d?>0x%x", *pref >> packed_type_shift, elt);
  137.     }
  138. }
  139. void
  140. debug_print_ref(const ref *pref)
  141. {    if ( r_is_packed(pref) )
  142.         debug_print_packed_ref((const ref_packed *)pref);
  143.     else
  144.         debug_print_full_ref(pref);
  145.     fflush(dstderr);
  146. }
  147.  
  148. /* Print a string. */
  149. void
  150. debug_print_string(const byte *chrs, uint len)
  151. {    uint i;
  152.     for ( i = 0; i < len; i++ )
  153.         dputc(chrs[i]);
  154.     fflush(dstderr);
  155. }
  156.  
  157. /* Dump one ref. */
  158. void
  159. debug_dump_one_ref(const ref *p)
  160. {    uint attrs = r_type_attrs(p);
  161.     uint type = r_type(p);
  162.     static const char *as = attr_print_string;
  163.     const char *ap = as;
  164. #define buf_size 30
  165.     char buf[buf_size + 1];
  166.     uint plen;
  167.     if ( type >= tx_next_index )
  168.         dprintf1("0x%02x?? ", type);
  169.     else if ( type >= t_next_index )
  170.         dprintf("opr* ");
  171.     else
  172.         dprintf1("%s ", type_strings[type]);
  173.     for ( ; *ap; ap++, attrs >>= 1 )
  174.       if ( *ap != '.' )
  175.         dputc(((attrs & 1) ? *ap : '-'));
  176.     dprintf2(" 0x%04x 0x%08lx", r_size(p), *(const ulong *)&p->value);
  177.     if ( obj_cvs(p, (byte *)buf, countof(buf) - 1, &plen, NULL) >= 0 &&
  178.          ((buf[plen] = 0), strcmp(buf, "--nostringval--"))
  179.        )
  180.         dprintf1(" = %s", buf);
  181.     fflush(dstderr);
  182. }
  183.  
  184. /* Dump a region of memory containing refs. */
  185. void
  186. debug_dump_refs(const ref *from, uint size, const char *msg)
  187. {    const ref *p = from;
  188.     uint count = size;
  189.     if ( size && msg )
  190.         dprintf2("%s at 0x%lx:\n", msg, (ulong)from);
  191.     while ( count-- )
  192.        {    dprintf2("..%04x: 0x%02x ", (uint)p & 0xffff, r_type(p));
  193.         debug_dump_one_ref(p);
  194.         dputc('\n');
  195.         p++;
  196.        }
  197. }
  198.  
  199. /* Dump a stack. */
  200. void
  201. debug_dump_stack(const ref_stack *pstack, const char *msg)
  202. {    uint i;
  203.     const char *m = msg;
  204.     for ( i = ref_stack_count(pstack); i != 0; )
  205.       { const ref *p = ref_stack_index(pstack, --i);
  206.         if ( m )
  207.           dprintf2("%s at 0x%lx:\n", m, (ulong)pstack),
  208.           m = NULL;
  209.         dprintf2("0x%lx: 0x%02x ", (ulong)p, r_type(p));
  210.         debug_dump_one_ref(p);
  211.         dputc('\n');
  212.       }
  213. }
  214.  
  215. /* Dump an array. */
  216. void
  217. debug_dump_array(const ref *array)
  218. {    const ref_packed *pp;
  219.     unsigned int type = r_type(array);
  220.     uint len;
  221.  
  222.     switch (type)
  223.        {
  224.     default:
  225.         dprintf2 ("%s at 0x%lx isn't an array.\n",
  226.               (type < countof(type_strings) ?
  227.                type_strings[type] : "????"),
  228.               (ulong)array);
  229.         return;
  230.     case t_oparray:
  231.         /* This isn't really an array, but we'd like to see */
  232.         /* its contents anyway. */
  233.         debug_dump_array(op_array_table.value.refs + op_index(array) -
  234.                  op_def_count);
  235.         return;
  236.     case t_array:
  237.     case t_mixedarray:
  238.     case t_shortarray: 
  239.         ;
  240.        }
  241.  
  242.     /* This "packed" loop works for all array-types. */
  243.     for ( len = r_size (array), pp = array->value.packed;
  244.           len > 0;
  245.           len--, pp = packed_next(pp))
  246.        {    ref temp;
  247.         packed_get(pp, &temp);
  248.         dprintf3("..%04x%c 0x%02x ", 
  249.              (uint)pp & 0xffff,
  250.              ((r_is_packed(pp)) ? '*' : ':'),
  251.              r_type(&temp));
  252.         debug_dump_one_ref(&temp);
  253.         dputc ('\n');
  254.        }
  255. }
  256.