home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / IDEBUG.C < prev    next >
C/C++ Source or Header  |  1992-07-17  |  7KB  |  239 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* idebug.c */
  21. /* Debugging support for Ghostscript interpreter */
  22. #include "string_.h"
  23. #include "ghost.h"
  24. #include "iutil.h"
  25. #include "dict.h"
  26. #include "name.h"
  27. #include "ostack.h"            /* for opdef.h */
  28. #include "opdef.h"
  29. #include "packed.h"
  30. #include "store.h"        /* for make_oper for opdef.h */
  31.  
  32. /* Forward references */
  33. void debug_print_string(P2(const byte *, ushort));
  34. void debug_print_ref(P1(const ref *));
  35.  
  36. #define debug_print_name(pnref)\
  37.   debug_print_string((pnref)->value.pname->string_bytes,\
  38.              (pnref)->value.pname->string_size)
  39.  
  40. /* Table of type name strings */
  41. static const char *type_strings[] = { type_print_strings };
  42.  
  43. /* Print a ref */
  44. 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_boolean:
  54.       dprintf1("boolean %x", pref->value.index); break;
  55.     case t_condition:
  56.       dprintf1("condition 0x%lx", (ulong)pref->value.pcond); 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_gstate:
  67.       dprintf1("gstate 0x%lx", (ulong)pref->value.pgstate); break;
  68.     case t_integer: dprintf1("int %ld", pref->value.intval); break;
  69.     case t_lock:
  70.       dprintf1("lock 0x%lx", (ulong)pref->value.plock); break;
  71.     case t_mark: 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: dprintf("null"); break;
  81.     case t_oparray:
  82.       dprintf1("op_array(0x%x)", size);
  83.       name_index_ref(op_array_nx_table[size - op_def_count], &nref);
  84.       debug_print_name(&nref);
  85.       break;
  86.     case t_operator:
  87.       dprintf1("op(0x%x", size);
  88.       if ( size )
  89.         dprintf1(":%s", (const char *)(op_def_table[size]->oname + 1));
  90.       dprintf1(")0x%lx", (ulong)pref->value.opproc);
  91.       break;
  92.     case t_real: dprintf1("real %f", pref->value.realval); break;
  93.     case t_shortarray:
  94.       dprintf2("short packedarray(%u)0x%lx", size,
  95.            (ulong)pref->value.packed); break;
  96.     case t_string:
  97.       dprintf2("string(%u)0x%lx", size, (ulong)pref->value.bytes); break;
  98.     default: dprintf1("type 0x%x", r_type(pref));
  99.        }
  100. }
  101. void
  102. debug_print_packed_ref(const ref_packed *pref)
  103. {    ushort elt = *pref;
  104.     ref nref;
  105.     switch ( elt >> packed_type_shift )
  106.        {
  107.     case pt_executable_operator:
  108.       dprintf("<op_name>");
  109.       elt &= packed_int_mask;
  110.       op_index_ref(elt, &nref);
  111.       debug_print_ref(&nref);
  112.       break;
  113.     case pt_integer:
  114.       dprintf1("<int> %d", (int)(elt & packed_int_mask) + packed_min_intval);
  115.       break;
  116.     case pt_literal_name: case pt_literal_name+1:
  117.       dprintf("<lit_name>"); elt &= packed_max_name_index; goto ptn;
  118.     case pt_executable_name: case pt_executable_name+1:
  119.       dprintf("<exec_name>"); elt &= packed_max_name_index;
  120. ptn:      name_index_ref(elt, &nref);
  121.       dprintf2("(0x%lx#%x)", (ulong)nref.value.pname, elt);
  122.       debug_print_name(&nref);
  123.       break;
  124.        }
  125. }
  126. void
  127. debug_print_ref(const ref *pref)
  128. {    if ( r_is_packed(pref) )
  129.         debug_print_packed_ref((const ref_packed *)pref);
  130.     else
  131.         debug_print_full_ref(pref);
  132.     fflush(dstderr);
  133. }
  134.  
  135. /* Print a string */
  136. void
  137. debug_print_string(const byte *chrs, ushort len)
  138. {    ushort i;
  139.     for ( i = 0; i < len; i++ )
  140.         dputc(chrs[i]);
  141.     fflush(dstderr);
  142. }
  143.  
  144. /* Dump one ref */
  145. void
  146. debug_dump_one_ref(const ref *p)
  147. {    uint attrs = r_type_attrs(p);
  148.     uint btype = r_btype(p);
  149.     static const char *as = attr_print_string;
  150.     const char *ap = as;
  151. #define buf_size 30
  152.     char buf[buf_size + 1];
  153.     uint plen;
  154.     if ( btype >= t_next_index )
  155.         dprintf1("0x%02x?? ", btype);
  156.     else
  157.         dprintf1("%s ", type_strings[btype]);
  158.     for ( ; *ap; ap++, attrs >>= 1 )
  159.       if ( *ap != '.' )
  160.         dputc(((attrs & 1) ? *ap : '-'));
  161.     dprintf2(" 0x%04x 0x%08lx", r_size(p), *(const ulong *)&p->value);
  162.     if ( obj_cvs(p, (byte *)buf, countof(buf) - 1, &plen) >= 0 &&
  163.          ((buf[plen] = 0), strcmp(buf, "--nostringval--"))
  164.        )
  165.         dprintf1(" = %s", buf);
  166.     fflush(dstderr);
  167. }
  168.  
  169. /* Dump a region of memory containing refs */
  170. void
  171. debug_dump_refs(const ref *from, uint size, const char *msg)
  172. {    const ref *p = from;
  173.     uint count = size;
  174.     if ( size && msg )
  175.         dprintf2("%s at 0x%lx:\n", msg, (ulong)from);
  176.     while ( count-- )
  177.        {    dprintf2("..%04x: 0x%02x ", (uint)p & 0xffff, r_type(p));
  178.         debug_dump_one_ref(p);
  179.         dputc('\n');
  180.         p++;
  181.        }
  182. }
  183.  
  184. /* Dump a region of memory */
  185. void
  186. debug_dump_bytes(const byte *from, const byte *to, const char *msg)
  187. {    const byte *p = from;
  188.     if ( from < to && msg )
  189.         dprintf1("%s:\n", msg);
  190.     while ( p != to )
  191.        {    const byte *q = min(p + 16, to);
  192.         dprintf1("%lx:", (ulong)p);
  193.         while ( p != q ) dprintf1(" %02x", *p++);
  194.         dputc('\n');
  195.        }
  196. }
  197.  
  198. /* Dump an array. */
  199. void
  200. debug_dump_array(const ref *array)
  201. {    const ref_packed *pp;
  202.     unsigned int type = r_type(array);
  203.     uint len;
  204.  
  205.     switch (type)
  206.        {
  207.     default:
  208.         dprintf2 ("%s at 0x%lx isn't an array.\n",
  209.               (type < countof(type_strings) ?
  210.                type_strings[type] : "????"),
  211.               (ulong)array);
  212.         return;
  213.     case t_oparray:
  214.         /* This isn't really an array, but we'd like to see */
  215.         /* its contents anyway. */
  216.         debug_dump_array(op_array_table.value.refs + op_index(array) -
  217.                  op_def_count);
  218.         return;
  219.     case t_array:
  220.     case t_mixedarray:
  221.     case t_shortarray: 
  222.         ;
  223.        }
  224.  
  225.     /* This "packed" loop works for all array-types. */
  226.     for ( len = r_size (array), pp = array->value.packed;
  227.           len > 0;
  228.           len--, pp = packed_next(pp))
  229.        {    ref temp;
  230.         packed_get(pp, &temp);
  231.         dprintf3("..%04x%c 0x%02x ", 
  232.              (uint)pp & 0xffff,
  233.              ((r_is_packed(pp)) ? '*' : ':'),
  234.              r_type(&temp));
  235.         debug_dump_one_ref(&temp);
  236.         dputc ('\n');
  237.        }
  238. }
  239.