Debugger: Allow printing of pointer referents.
authorKeith Packard <keithp@keithp.com>
Wed, 29 Apr 2009 17:51:47 +0000 (10:51 -0700)
committerKeith Packard <keithp@keithp.com>
Wed, 29 Apr 2009 18:06:18 +0000 (11:06 -0700)
Allow array operator to be used on pointers
Add '*' operator for pointers
Add '->' operator for pointers to structures

debugger/mcs51/cmd.c

index 4bfc16fdf33c011416ca86b07b5e816a5b95e9b1..79dba8d30df0ccf2437d8fbbb99eef2af4f10e9f 100644 (file)
@@ -328,7 +328,7 @@ static char *warranty=
 #endif
 
 static void printTypeInfo(link *);
-static void printValAggregates (symbol *,link *,char,unsigned int,int);
+static void printValAggregates (symbol *,link *,char,unsigned int,int,int);
 static  int printOrSetSymValue (symbol *sym, context *cctxt,
                                 int flg, int dnum, int fmt,
                                 char *rs, char *val, char cmp);
@@ -749,6 +749,7 @@ static char *preparePrint(char *s, context *cctxt, int *fmt, symbol **sym)
 {
     char *bp;
     char save_ch ;
+    int ptr = 0;
 
     *fmt = FMT_NON;
     *sym = NULL;
@@ -779,6 +780,10 @@ static char *preparePrint(char *s, context *cctxt, int *fmt, symbol **sym)
         s++;
         s = trim_left(s);
     }
+    while (*s == '*') {
+       ptr++;
+       s++;
+    }
     for ( bp = s; *bp && ( isalnum( *bp ) || *bp == '_' || *bp == '$'); bp++ );
     save_ch = *bp;
     if ( *bp )
@@ -787,6 +792,8 @@ static char *preparePrint(char *s, context *cctxt, int *fmt, symbol **sym)
     if ( *s )
         *sym = symLookup(s,cctxt);
     *bp = save_ch;
+    while (ptr--)
+       strcat(bp,"*");
 
     if ( ! *sym )
         fprintf(stdout,"No symbol \"%s\" in current context.\n", s);
@@ -2480,7 +2487,7 @@ static void printValBasic(symbol *sym, link *type,
         fprintf(stdout,"%f",v.f);
     else
         if (IS_PTR(type))
-            fprintf(stdout,"0x%*lx",size<<1,v.val);
+            fprintf(stdout,"0x%0*lx",size<<1,v.val);
         else
         if (IS_INTEGRAL(type))
         {
@@ -2531,67 +2538,90 @@ static void printValFunc (symbol *sym, int fmt)
     fprintf(stdout,"print function not yet implemented");
 }
 
+static void
+do_indent(int indent) {
+    while (indent--)
+       fprintf(stdout, "  ");
+
+}
+
 /*-----------------------------------------------------------------*/
 /* printArrayValue - will print the values of array elements       */
 /*-----------------------------------------------------------------*/
 static void printArrayValue (symbol *sym,  link *type,
-                             char space, unsigned int addr, int fmt)
+                             char space, unsigned int addr, int fmt,
+                            int indent)
 {
-        link *elem_type = type->next;
-        int i;
+    link *elem_type = type->next;
+    int i;
+    int col;
 
-        fprintf(stdout,"{");
-        for (i = 0 ; i < DCL_ELEM(type) ; i++) {
-                if (IS_AGGREGATE(elem_type)) {
-                        printValAggregates(sym,elem_type,space,addr,fmt);
-                } else {
-                        printValBasic(sym,elem_type,space,addr,getSize(elem_type),fmt);
-                }
-                addr += getSize(elem_type);
-                if (i != DCL_ELEM(type) -1)
-                        fprintf(stdout,",");
-        }
+    fprintf(stdout,"{");
+    for (i = 0 ; i < DCL_ELEM(type) ; i++) {
+       if (IS_AGGREGATE(elem_type)) {
+           printValAggregates(sym,elem_type,space,addr,fmt,indent);
+           col = 0;
+       } else {
+           printValBasic(sym,elem_type,space,addr,getSize(elem_type),fmt);
+           col++;
+       }
+       addr += getSize(elem_type);
+       if (i != DCL_ELEM(type) -1) {
+           fprintf(stdout,",");
+           if (col == 16) {
+               fprintf(stdout,"\n");
+               do_indent(indent);
+               col = 0;
+           }
+       }
+    }
 
-        fprintf(stdout,"}");
+    fprintf(stdout,"}");
 }
 
 /*-----------------------------------------------------------------*/
 /* printStructValue - prints structures elements                   */
 /*-----------------------------------------------------------------*/
 static void printStructValue (symbol *sym, link *type,
-                              char space, unsigned int addr, int fmt)
+                              char space, unsigned int addr, int fmt,
+                             int indent)
 {
-        symbol *fields = SPEC_STRUCT(type)->fields;
+    symbol *fields = SPEC_STRUCT(type)->fields;
     int first = 1;
-        fprintf(stdout," { ");
-        while (fields) {
-                fprintf(stdout,"%s%s = ",(first ? "": ", "),fields->name);
-                first = 0;
-        if (IS_AGGREGATE(fields->type)) {
-                        printValAggregates(fields,fields->type,space, addr, fmt);
-                } else {
-                        printValBasic(fields,fields->type,space,addr,getSize(fields->type), fmt);
-                }
-                addr += getSize(fields->type);
-                fields = fields->next;
-        }
-        fprintf(stdout,"}");
+    do_indent (indent);
+    fprintf(stdout,"{\n");
+    while (fields) {
+       do_indent(indent + 1);
+       fprintf(stdout,"%s = ", fields->name);
+       first = 0;
+       if (IS_AGGREGATE(fields->type)) {
+           printValAggregates(fields,fields->type,space, addr, fmt, indent + 1);
+       } else {
+           printValBasic(fields,fields->type,space,addr,getSize(fields->type), fmt);
+       }
+        fprintf(stdout,",\n");
+       addr += getSize(fields->type);
+       fields = fields->next;
+    }
+    do_indent(indent);
+    fprintf(stdout,"}");
 }
 
 /*-----------------------------------------------------------------*/
 /* printValAggregates - print value of aggregates                  */
 /*-----------------------------------------------------------------*/
 static void printValAggregates (symbol *sym, link *type,
-                                char space,unsigned int addr, int fmt)
+                                char space,unsigned int addr, int fmt,
+                               int indent)
 {
 
         if (IS_ARRAY(type)) {
-                printArrayValue(sym, type, space, addr, fmt);
+                printArrayValue(sym, type, space, addr, fmt, indent);
                 return ;
         }
 
         if (IS_STRUCT(type)) {
-                printStructValue(sym, type, space, addr, fmt);
+                printStructValue(sym, type, space, addr, fmt, indent);
                 return;
         }
 }
@@ -2611,6 +2641,7 @@ static int printOrSetSymValue (symbol *sym, context *cctxt,
     int size, n;
     char *s, *s2;
     char save_ch, save_ch2;
+    int get_member = 0;
 
     /* if it is on stack then compute address & fall thru */
     if (sym->isonstack)
@@ -2650,7 +2681,7 @@ static int printOrSetSymValue (symbol *sym, context *cctxt,
 
     while ( *rs )
     {
-        if ( *rs == '[' && IS_ARRAY(type))
+        if ( *rs == '[' && (IS_ARRAY(type) || IS_PTR(type)))
         {
             s = rs+1;
             while ( *rs && *rs != ']' ) rs++ ;
@@ -2682,17 +2713,19 @@ static int printOrSetSymValue (symbol *sym, context *cctxt,
             {
                 n = strtol(s,0,0);
             }
-            if ( n < 0 || n >= DCL_ELEM(type))
+            if ( IS_ARRAY(type) && (n < 0 || n >= DCL_ELEM(type)))
             {
                 fprintf(stdout,"Wrong index %d.\n", n);
                 return 1;
             }
+           if (IS_PTR(type))
+               addr = simGetValue(addr, sym->addrspace, size);
             type = type->next;
             size = getSize(type);
-            addr += size * n;
+           addr += size * n;
             *rs++ = save_ch;
         }
-        else if ( *rs == '.' && IS_STRUCT(type))
+        else if ( (*rs == '.' || get_member) && IS_STRUCT(type))
         {
             s = rs+1;
             /* search structure element */
@@ -2715,8 +2748,26 @@ static int printOrSetSymValue (symbol *sym, context *cctxt,
             size = getSize(type);
             addr += fields->offset;
         }
+       else if ( *rs == '*' && IS_PTR(type))
+       {
+           addr = simGetValue(addr, sym->addrspace, size);
+           type = type->next;
+           size = getSize(type);
+           rs++;
+       }
+       else if ( rs[0] == '-' && rs[1] == '>' &&
+                IS_PTR(type) && IS_STRUCT(type->next))
+       {
+           addr = simGetValue(addr, sym->addrspace, size);
+           type = type->next;
+           size = getSize(type);
+           rs++;
+           get_member = 1;
+           continue;
+       }
         else
             break;
+       get_member = 0;
     }
 
     /* arrays & structures first */
@@ -2728,7 +2779,7 @@ static int printOrSetSymValue (symbol *sym, context *cctxt,
             return 1;
         }
         else
-            printValAggregates(sym,type,sym->addrspace,addr,fmt);
+            printValAggregates(sym,type,sym->addrspace,addr,fmt,0);
     }
     else
         /* functions */