Branch: Tag:

2001-07-10

2001-07-10 11:57:26 by Martin Stjernholm <mast@lysator.liu.se>

Improved debug. ENCODE_DEBUG is now selectively turned on by a nonzero
third argument to encode_value, encode_value_canonic and decode_value.

Rev: src/encode.c:1.108

25:   #include "version.h"   #include "bignum.h"    - RCSID("$Id: encode.c,v 1.107 2001/07/03 15:04:51 grubba Exp $"); + RCSID("$Id: encode.c,v 1.108 2001/07/10 11:57:26 mast Exp $");      /* #define ENCODE_DEBUG */      #ifdef ENCODE_DEBUG - #define EDB(X) X + /* Pass a nonzero integer as the third arg to encode_value, +  * encode_value_canonic and decode_value to activate this debug. */ + #define EDB(X) do if (data->debug) {X;} while (0)   #else - #define EDB(X) + #define EDB(X) do {} while (0)   #endif      /* The sp macro conflicts with Solaris 2.5.1's <sys/conf.h>. */
127:    struct svalue counter;    struct mapping *encoded;    dynamic_buffer buf; + #ifdef ENCODE_DEBUG +  int debug, depth; + #endif   };      static void encode_value2(struct svalue *val, struct encode_data *data);
197:   {    int t;    EDB( -  fprintf(stderr,"encode: code_entry(tag=%d (%s), num=%ld)\n", -  tag, +  fprintf(stderr,"%*sencode: code_entry(tag=%d (%s), num=%ld)\n", +  data->depth, "", tag,    get_name_of_type(tag_to_type(tag)),    (long)num) );    if(num<0)
532:    INT32 i;    struct svalue *tmp;    + #ifdef ENCODE_DEBUG +  data->depth += 2; + #endif +     if((val->type == T_OBJECT ||    (val->type==T_FUNCTION && val->subtype!=FUNCTION_BUILTIN)) &&    !val->u.object->prog)
539:       if((tmp=low_mapping_lookup(data->encoded, val)))    { +  EDB(fprintf(stderr, "%*sEncoding TAG_AGAIN to <%d>\n", +  data->depth, "", tmp->u.integer));    code_entry(TAG_AGAIN, tmp->u.integer, data); -  + #ifdef ENCODE_DEBUG +  data->depth -= 2; + #endif    return;    }else if (val->type != T_TYPE) { -  +  EDB(fprintf(stderr, "%*sEncoding to <%d>: ", +  data->depth, "", data->counter.u.integer); +  print_svalue(stderr, val); +  fputc('\n', stderr););    mapping_insert(data->encoded, val, &data->counter);    data->counter.u.integer++;    }
570:    Pike_error("Canonical encoding of the type type not supported.\n");    code_entry(TAG_TYPE, 0, data); /* Type encoding #0 */    encode_type(val->u.type, data); +  EDB(fprintf(stderr, "%*sEncoded type to <%d>: ", +  data->depth, "", data->counter.u.integer); +  print_svalue(stderr, val); +  fputc('\n', stderr););    mapping_insert(data->encoded, val, &data->counter);    data->counter.u.integer++;    break;
766:       /* Put value back in cache */    mapping_insert(data->encoded, val, &tmp); + #ifdef ENCODE_DEBUG +  data->depth -= 2; + #endif    return;    }    }
822:       /* Put value back in cache */    mapping_insert(data->encoded, val, &tmp); + #ifdef ENCODE_DEBUG +  data->depth -= 2; + #endif    return;    }    if( p->event_handler )
871:    p->id,d,    p->identifier_references[d].inherit_offset,    p->identifier_references[d].identifier_offset, -  p->identifier_references[d].id_flags);) +  p->identifier_references[d].id_flags););    }       for(d=0;d<p->num_strings;d++) adddata(p->strings[d]);
890:    Pike_sp[-1].subtype=p->inherits[d].parent_identifier;    Pike_sp[-1].type=T_FUNCTION;    EDB(fprintf(stderr,"INHERIT%x coded as func { %p, %d }\n", -  p->id, p->inherits[d].parent, p->inherits[d].parent_identifier);) +  p->id, p->inherits[d].parent, p->inherits[d].parent_identifier););    }else if(p->inherits[d].prog){    ref_push_program(p->inherits[d].prog);    }else{
905:    EDB(fprintf(stderr,"INHERIT%x > %d: %d id=%d\n",    p->id,d,    p->inherits[d].prog->num_identifiers, -  p->inherits[d].prog->id);) +  p->inherits[d].prog->id););    }       for(d=0;d<p->num_identifiers;d++)
940:    break;    }    } +  + #ifdef ENCODE_DEBUG +  data->depth -= 2; + #endif   }      static void free_encode_data(struct encode_data *data)
984:    struct encode_data d, *data;    data=&d;    + #ifdef ENCODE_DEBUG +  check_all_args("encode_value", args, BIT_MIXED, BIT_VOID | BIT_OBJECT, +  BIT_VOID | BIT_INT, 0); + #else    check_all_args("encode_value", args, BIT_MIXED, BIT_VOID | BIT_OBJECT, 0); -  + #endif       initialize_buf(&data->buf);    data->canonic = 0;
997:    }else{    data->codec=get_master();    } + #ifdef ENCODE_DEBUG +  data->debug = args > 2 ? Pike_sp[2-args].u.integer : 0; +  data->depth = -2; + #endif       SET_ONERROR(tmp, free_encode_data, data);    addstr("\266ke0", 4);
1033:    struct encode_data d, *data;    data=&d;    + #ifdef ENCODE_DEBUG +  check_all_args("encode_value_canonic", args, BIT_MIXED, BIT_VOID | BIT_OBJECT, +  BIT_VOID | BIT_INT, 0); + #else    check_all_args("encode_value_canonic", args, BIT_MIXED, BIT_VOID | BIT_OBJECT, 0); -  + #endif       initialize_buf(&data->buf);    data->canonic = 1;
1046:    }else{    data->codec=get_master();    } + #ifdef ENCODE_DEBUG +  data->debug = args > 2 ? Pike_sp[2-args].u.integer : 0; +  data->depth = -2; + #endif       SET_ONERROR(tmp, free_encode_data, data);    addstr("\266ke0", 4);
1077:    int pickyness;    struct pike_string *raw;    struct decode_data *next; + #ifdef ENCODE_DEBUG +  int debug, depth; + #endif   };      static void decode_value2(struct decode_data *data);
1109:      #define DECODE(Z) do { \    EDB( \ -  fprintf(stderr,"decode(%s) at %d: ",(Z),__LINE__)); \ +  fprintf(stderr,"%*sdecode(%s) at %d: ", \ +  data->depth,"",(Z),__LINE__)); \    what=GETC(); \    e=what>>SIZE_SHIFT; \    numh=0; \
1469:    INT32 what, e, num, numh;    struct svalue tmp, *tmp2;    + #ifdef ENCODE_DEBUG +  data->depth += 2; + #endif +     DECODE("decode_value2");       check_stack(1);
1483:    {    push_svalue(tmp2);    }else{ -  Pike_error("Failed to decode string. (invalid T_AGAIN)\n"); +  Pike_error("Failed to decode TAG_AGAIN entry <%d>.\n", num);    } -  + #ifdef ENCODE_DEBUG +  data->depth -= 2; + #endif    return;       case TAG_INT:
1553:       tmp.type=T_ARRAY;    tmp.u.array=a=allocate_array(num); +  EDB(fprintf(stderr, "%*sDecoding array of size %d to <%d>\n", +  data->depth, "", num, data->counter.u.integer));    mapping_insert(data->decoded, & data->counter, &tmp);    data->counter.u.integer++;   
1570:    dmalloc_touch_svalue(Pike_sp);    }    ref_push_array(a); + #ifdef ENCODE_DEBUG +  data->depth -= 2; + #endif    return;    }   
1586:    m=allocate_mapping(num);    tmp.type=T_MAPPING;    tmp.u.mapping=m; +  EDB(fprintf(stderr, "%*sDecoding mapping of size %d to <%d>\n", +  data->depth, "", num, data->counter.u.integer));    mapping_insert(data->decoded, & data->counter, &tmp);    data->counter.u.integer++;    m->refs--;
1598:    pop_n_elems(2);    }    ref_push_mapping(m); + #ifdef ENCODE_DEBUG +  data->depth -= 2; + #endif    return;    }   
1617:    m = allocate_multiset(a);    tmp.type = T_MULTISET;    tmp.u.multiset = m; +  EDB(fprintf(stderr, "%*sDecoding multiset of size %d to <%d>\n", +  data->depth, "", num, data->counter.u.integer));    mapping_insert(data->decoded, & data->counter, &tmp);    data->counter.u.integer++;    debug_malloc_touch(m);
1631:    array_fix_type_field(a);    order_multiset(m);    push_multiset(m); + #ifdef ENCODE_DEBUG +  data->depth -= 2; + #endif    return;    }   
1654:    case 1:    if(IS_ZERO(Pike_sp-1))    { +  EDB(fprintf(stderr, "%*sDecoded a failed object to <%d>: ", +  data->depth, "", tmp.u.integer); +  print_svalue(stderr, Pike_sp-1); +  fputc('\n', stderr););    mapping_insert(data->decoded, &tmp, Pike_sp-1);    decode_value2(data);    pop_stack();    }else{    f_call_function(1); -  +  EDB(fprintf(stderr, "%*sDecoded an object to <%d>: ", +  data->depth, "", tmp.u.integer); +  print_svalue(stderr, Pike_sp-1); +  fputc('\n', stderr););    mapping_insert(data->decoded, &tmp, Pike_sp-1);    push_svalue(Pike_sp-1);    decode_value2(data);
1669:    }    if(data->pickyness && Pike_sp[-1].type != T_OBJECT)    Pike_error("Failed to decode object.\n"); + #ifdef ENCODE_DEBUG +  data->depth -= 2; + #endif    return;      #ifdef AUTO_BIGNUM
1801:    debug_malloc_touch(p);    tmp.type=T_PROGRAM;    tmp.u.program=p; +  EDB(fprintf(stderr, "%*sDecoding a program to <%d>: ", +  data->depth, "", data->counter.u.integer); +  print_svalue(stderr, &tmp); +  fputc('\n', stderr););    mapping_insert(data->decoded, & data->counter, &tmp);    data->counter.u.integer++;    p->refs--;
1931:    p->id,d,    p->identifier_references[d].inherit_offset,    p->identifier_references[d].identifier_offset, -  p->identifier_references[d].id_flags); ) +  p->identifier_references[d].id_flags); );    }       debug_malloc_touch(p);
1972:    if(Pike_sp[-1].subtype == FUNCTION_BUILTIN)    Pike_error("Failed to decode parent.\n");    -  EDB( fprintf(stderr,"INHERIT%x = func { %x, %d} \n",p->id,Pike_sp[-1].u.object, Pike_sp[-1].subtype); ) +  EDB( fprintf(stderr,"INHERIT%x = func { %p, %d} \n",p->id,Pike_sp[-1].u.object, Pike_sp[-1].subtype); );       p->inherits[d].parent_identifier=Pike_sp[-1].subtype;    p->inherits[d].prog=program_from_svalue(Pike_sp-1);
1985:    break;       case T_PROGRAM: -  EDB( fprintf(stderr,"INHERIT%x = prog\n",p->id); ) +  EDB( fprintf(stderr,"INHERIT%x = prog\n",p->id); );    p->inherits[d].prog=Pike_sp[-1].u.program;    Pike_sp--;    dmalloc_touch_svalue(Pike_sp);
1999:    EDB( fprintf(stderr,"INHERIT%x < %d: %d id=%d\n",    p->id,d,    p->inherits[d].prog->num_identifiers, -  p->inherits[d].prog->id); ) +  p->inherits[d].prog->id); );    }       debug_malloc_touch(dat);
2120: Inside #if defined(_REENTRANT)
   UNSET_ONERROR(err);    exit_threads_disable(NULL);   #endif + #ifdef ENCODE_DEBUG +  data->depth -= 2; + #endif    return;    }   
2166:    Pike_error("Failed to restore string. (Illegal type)\n");    }    +  EDB(fprintf(stderr, "%*sDecoded to <%d>: ", data->depth, "", tmp.u.integer); +  print_svalue(stderr, Pike_sp-1); +  fputc('\n', stderr););    mapping_insert(data->decoded, & tmp, Pike_sp-1); -  + #ifdef ENCODE_DEBUG +  data->depth -= 2; + #endif   }      
2200:   }      static INT32 my_decode(struct pike_string *tmp, -  struct object *codec) +  struct object *codec + #ifdef ENCODE_DEBUG +  , int debug + #endif +  )   {    ONERROR err;    struct decode_data d, *data;
2236:    data->unfinished_programs=0;    data->raw = tmp;    data->next = current_decode; + #ifdef ENCODE_DEBUG +  data->debug = debug; +  data->depth = -2; + #endif       if (tmp->size_shift) return 0;    if(data->len < 5) return 0;
2392:    struct pike_string *s;    struct object *codec;    + #ifdef ENCODE_DEBUG +  int debug;    check_all_args("decode_value", args, -  +  BIT_STRING, BIT_VOID | BIT_OBJECT | BIT_INT, BIT_VOID | BIT_INT, 0); +  debug = args > 2 ? Pike_sp[2-args].u.integer : 0; + #else +  check_all_args("decode_value", args,    BIT_STRING, BIT_VOID | BIT_OBJECT | BIT_INT, 0); -  + #endif       s = Pike_sp[-args].u.string;    if(args<2)
2409:    codec=0;    }    -  if(!my_decode(s, codec)) +  if(!my_decode(s, codec + #ifdef ENCODE_DEBUG +  , debug + #endif +  ))    {    char *v=s->str;    ptrdiff_t l=s->len;