GDB (API)
/home/stan/gdb/src/gdb/m2-valprint.c
Go to the documentation of this file.
00001 /* Support for printing Modula 2 values for GDB, the GNU debugger.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "symtab.h"
00022 #include "gdbtypes.h"
00023 #include "expression.h"
00024 #include "value.h"
00025 #include "valprint.h"
00026 #include "language.h"
00027 #include "typeprint.h"
00028 #include "c-lang.h"
00029 #include "m2-lang.h"
00030 #include "target.h"
00031 
00032 static int print_unpacked_pointer (struct type *type,
00033                                    CORE_ADDR address, CORE_ADDR addr,
00034                                    const struct value_print_options *options,
00035                                    struct ui_file *stream);
00036 static void
00037 m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
00038                          int embedded_offset, CORE_ADDR address,
00039                          struct ui_file *stream, int recurse,
00040                          const struct value *val,
00041                          const struct value_print_options *options,
00042                          int len);
00043 
00044 
00045 /* get_long_set_bounds - assigns the bounds of the long set to low and
00046                          high.  */
00047 
00048 int
00049 get_long_set_bounds (struct type *type, LONGEST *low, LONGEST *high)
00050 {
00051   int len, i;
00052 
00053   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
00054     {
00055       len = TYPE_NFIELDS (type);
00056       i = TYPE_N_BASECLASSES (type);
00057       if (len == 0)
00058         return 0;
00059       *low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
00060       *high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type,
00061                                                                  len-1)));
00062       return 1;
00063     }
00064   error (_("expecting long_set"));
00065   return 0;
00066 }
00067 
00068 static void
00069 m2_print_long_set (struct type *type, const gdb_byte *valaddr,
00070                    int embedded_offset, CORE_ADDR address,
00071                    struct ui_file *stream)
00072 {
00073   int empty_set        = 1;
00074   int element_seen     = 0;
00075   LONGEST previous_low = 0;
00076   LONGEST previous_high= 0;
00077   LONGEST i, low_bound, high_bound;
00078   LONGEST field_low, field_high;
00079   struct type *range;
00080   int len, field;
00081   struct type *target;
00082   int bitval;
00083 
00084   CHECK_TYPEDEF (type);
00085 
00086   fprintf_filtered (stream, "{");
00087   len = TYPE_NFIELDS (type);
00088   if (get_long_set_bounds (type, &low_bound, &high_bound))
00089     {
00090       field = TYPE_N_BASECLASSES (type);
00091       range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
00092     }
00093   else
00094     {
00095       fprintf_filtered (stream, " %s }", _("<unknown bounds of set>"));
00096       return;
00097     }
00098 
00099   target = TYPE_TARGET_TYPE (range);
00100 
00101   if (get_discrete_bounds (range, &field_low, &field_high) >= 0)
00102     {
00103       for (i = low_bound; i <= high_bound; i++)
00104         {
00105           bitval = value_bit_index (TYPE_FIELD_TYPE (type, field),
00106                                     (TYPE_FIELD_BITPOS (type, field) / 8) +
00107                                     valaddr + embedded_offset, i);
00108           if (bitval < 0)
00109             error (_("bit test is out of range"));
00110           else if (bitval > 0)
00111             {
00112               previous_high = i;
00113               if (! element_seen)
00114                 {
00115                   if (! empty_set)
00116                     fprintf_filtered (stream, ", ");
00117                   print_type_scalar (target, i, stream);
00118                   empty_set    = 0;
00119                   element_seen = 1;
00120                   previous_low = i;
00121                 }
00122             }
00123           else
00124             {
00125               /* bit is not set */
00126               if (element_seen)
00127                 {
00128                   if (previous_low+1 < previous_high)
00129                     fprintf_filtered (stream, "..");
00130                   if (previous_low+1 < previous_high)
00131                     print_type_scalar (target, previous_high, stream);
00132                   element_seen = 0;
00133                 }
00134             }
00135           if (i == field_high)
00136             {
00137               field++;
00138               if (field == len)
00139                 break;
00140               range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
00141               if (get_discrete_bounds (range, &field_low, &field_high) < 0)
00142                 break;
00143               target = TYPE_TARGET_TYPE (range);
00144             }
00145         }
00146       if (element_seen)
00147         {
00148           if (previous_low+1 < previous_high)
00149             {
00150               fprintf_filtered (stream, "..");
00151               print_type_scalar (target, previous_high, stream);
00152             }
00153           element_seen = 0;
00154         }
00155       fprintf_filtered (stream, "}");
00156     }
00157 }
00158 
00159 static void
00160 m2_print_unbounded_array (struct type *type, const gdb_byte *valaddr,
00161                           int embedded_offset, CORE_ADDR address,
00162                           struct ui_file *stream, int recurse,
00163                           const struct value_print_options *options)
00164 {
00165   struct type *content_type;
00166   CORE_ADDR addr;
00167   LONGEST len;
00168   struct value *val;
00169 
00170   CHECK_TYPEDEF (type);
00171   content_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0));
00172 
00173   addr = unpack_pointer (TYPE_FIELD_TYPE (type, 0),
00174                          (TYPE_FIELD_BITPOS (type, 0) / 8) +
00175                          valaddr + embedded_offset);
00176 
00177   val = value_at_lazy (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
00178                        addr);
00179   len = unpack_field_as_long (type, valaddr + embedded_offset, 1);
00180 
00181   fprintf_filtered (stream, "{");  
00182   m2_print_array_contents (value_type (val),
00183                            value_contents_for_printing (val),
00184                            value_embedded_offset (val), addr, stream,
00185                            recurse, val, options, len);
00186   fprintf_filtered (stream, ", HIGH = %d}", (int) len);
00187 }
00188 
00189 static int
00190 print_unpacked_pointer (struct type *type,
00191                         CORE_ADDR address, CORE_ADDR addr,
00192                         const struct value_print_options *options,
00193                         struct ui_file *stream)
00194 {
00195   struct gdbarch *gdbarch = get_type_arch (type);
00196   struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
00197   int want_space = 0;
00198 
00199   if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
00200     {
00201       /* Try to print what function it points to.  */
00202       print_function_pointer_address (options, gdbarch, addr, stream);
00203       /* Return value is irrelevant except for string pointers.  */
00204       return 0;
00205     }
00206 
00207   if (options->addressprint && options->format != 's')
00208     {
00209       fputs_filtered (paddress (gdbarch, address), stream);
00210       want_space = 1;
00211     }
00212 
00213   /* For a pointer to char or unsigned char, also print the string
00214      pointed to, unless pointer is null.  */
00215 
00216   if (TYPE_LENGTH (elttype) == 1
00217       && TYPE_CODE (elttype) == TYPE_CODE_INT
00218       && (options->format == 0 || options->format == 's')
00219       && addr != 0)
00220     {
00221       if (want_space)
00222         fputs_filtered (" ", stream);
00223       return val_print_string (TYPE_TARGET_TYPE (type), NULL, addr, -1,
00224                                stream, options);
00225     }
00226   
00227   return 0;
00228 }
00229 
00230 static void
00231 print_variable_at_address (struct type *type,
00232                            const gdb_byte *valaddr,
00233                            struct ui_file *stream,
00234                            int recurse,
00235                            const struct value_print_options *options)
00236 {
00237   struct gdbarch *gdbarch = get_type_arch (type);
00238   CORE_ADDR addr = unpack_pointer (type, valaddr);
00239   struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
00240 
00241   fprintf_filtered (stream, "[");
00242   fputs_filtered (paddress (gdbarch, addr), stream);
00243   fprintf_filtered (stream, "] : ");
00244   
00245   if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
00246     {
00247       struct value *deref_val =
00248         value_at (TYPE_TARGET_TYPE (type), unpack_pointer (type, valaddr));
00249 
00250       common_val_print (deref_val, stream, recurse, options, current_language);
00251     }
00252   else
00253     fputs_filtered ("???", stream);
00254 }
00255 
00256 
00257 /* m2_print_array_contents - prints out the contents of an
00258                              array up to a max_print values.
00259                              It prints arrays of char as a string
00260                              and all other data types as comma
00261                              separated values.  */
00262 
00263 static void
00264 m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
00265                          int embedded_offset, CORE_ADDR address,
00266                          struct ui_file *stream, int recurse,
00267                          const struct value *val,
00268                          const struct value_print_options *options,
00269                          int len)
00270 {
00271   CHECK_TYPEDEF (type);
00272 
00273   if (TYPE_LENGTH (type) > 0)
00274     {
00275       if (options->prettyformat_arrays)
00276         print_spaces_filtered (2 + 2 * recurse, stream);
00277       /* For an array of chars, print with string syntax.  */
00278       if (TYPE_LENGTH (type) == 1 &&
00279           ((TYPE_CODE (type) == TYPE_CODE_INT)
00280            || ((current_language->la_language == language_m2)
00281                && (TYPE_CODE (type) == TYPE_CODE_CHAR)))
00282           && (options->format == 0 || options->format == 's'))
00283         val_print_string (type, NULL, address, len+1, stream, options);
00284       else
00285         {
00286           fprintf_filtered (stream, "{");
00287           val_print_array_elements (type, valaddr, embedded_offset,
00288                                     address, stream, recurse, val,
00289                                     options, 0);
00290           fprintf_filtered (stream, "}");
00291         }
00292     }
00293 }
00294 
00295 /* Decorations for Modula 2.  */
00296 
00297 static const struct generic_val_print_decorations m2_decorations =
00298 {
00299   "",
00300   " + ",
00301   " * I",
00302   "TRUE",
00303   "FALSE",
00304   "void"
00305 };
00306 
00307 /* See val_print for a description of the various parameters of this
00308    function; they are identical.  */
00309 
00310 void
00311 m2_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
00312               CORE_ADDR address, struct ui_file *stream, int recurse,
00313               const struct value *original_value,
00314               const struct value_print_options *options)
00315 {
00316   struct gdbarch *gdbarch = get_type_arch (type);
00317   unsigned int i = 0;   /* Number of characters printed.  */
00318   unsigned len;
00319   struct type *elttype;
00320   CORE_ADDR addr;
00321 
00322   CHECK_TYPEDEF (type);
00323   switch (TYPE_CODE (type))
00324     {
00325     case TYPE_CODE_ARRAY:
00326       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
00327         {
00328           elttype = check_typedef (TYPE_TARGET_TYPE (type));
00329           len = TYPE_LENGTH (type) / TYPE_LENGTH (elttype);
00330           if (options->prettyformat_arrays)
00331             print_spaces_filtered (2 + 2 * recurse, stream);
00332           /* For an array of chars, print with string syntax.  */
00333           if (TYPE_LENGTH (elttype) == 1 &&
00334               ((TYPE_CODE (elttype) == TYPE_CODE_INT)
00335                || ((current_language->la_language == language_m2)
00336                    && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
00337               && (options->format == 0 || options->format == 's'))
00338             {
00339               /* If requested, look for the first null char and only print
00340                  elements up to it.  */
00341               if (options->stop_print_at_null)
00342                 {
00343                   unsigned int temp_len;
00344 
00345                   /* Look for a NULL char.  */
00346                   for (temp_len = 0;
00347                        (valaddr + embedded_offset)[temp_len]
00348                          && temp_len < len && temp_len < options->print_max;
00349                        temp_len++);
00350                   len = temp_len;
00351                 }
00352 
00353               LA_PRINT_STRING (stream, TYPE_TARGET_TYPE (type),
00354                                valaddr + embedded_offset, len, NULL,
00355                                0, options);
00356               i = len;
00357             }
00358           else
00359             {
00360               fprintf_filtered (stream, "{");
00361               val_print_array_elements (type, valaddr, embedded_offset,
00362                                         address, stream,
00363                                         recurse, original_value,
00364                                         options, 0);
00365               fprintf_filtered (stream, "}");
00366             }
00367           break;
00368         }
00369       /* Array of unspecified length: treat like pointer to first elt.  */
00370       print_unpacked_pointer (type, address, address, options, stream);
00371       break;
00372 
00373     case TYPE_CODE_PTR:
00374       if (TYPE_CONST (type))
00375         print_variable_at_address (type, valaddr + embedded_offset,
00376                                    stream, recurse, options);
00377       else if (options->format && options->format != 's')
00378         val_print_scalar_formatted (type, valaddr, embedded_offset,
00379                                     original_value, options, 0, stream);
00380       else
00381         {
00382           addr = unpack_pointer (type, valaddr + embedded_offset);
00383           print_unpacked_pointer (type, addr, address, options, stream);
00384         }
00385       break;
00386 
00387     case TYPE_CODE_UNION:
00388       if (recurse && !options->unionprint)
00389         {
00390           fprintf_filtered (stream, "{...}");
00391           break;
00392         }
00393       /* Fall through.  */
00394     case TYPE_CODE_STRUCT:
00395       if (m2_is_long_set (type))
00396         m2_print_long_set (type, valaddr, embedded_offset, address,
00397                            stream);
00398       else if (m2_is_unbounded_array (type))
00399         m2_print_unbounded_array (type, valaddr, embedded_offset,
00400                                   address, stream, recurse, options);
00401       else
00402         cp_print_value_fields (type, type, valaddr, embedded_offset,
00403                                address, stream, recurse, original_value,
00404                                options, NULL, 0);
00405       break;
00406 
00407     case TYPE_CODE_SET:
00408       elttype = TYPE_INDEX_TYPE (type);
00409       CHECK_TYPEDEF (elttype);
00410       if (TYPE_STUB (elttype))
00411         {
00412           fprintf_filtered (stream, _("<incomplete type>"));
00413           gdb_flush (stream);
00414           break;
00415         }
00416       else
00417         {
00418           struct type *range = elttype;
00419           LONGEST low_bound, high_bound;
00420           int i;
00421           int need_comma = 0;
00422 
00423           fputs_filtered ("{", stream);
00424 
00425           i = get_discrete_bounds (range, &low_bound, &high_bound);
00426         maybe_bad_bstring:
00427           if (i < 0)
00428             {
00429               fputs_filtered (_("<error value>"), stream);
00430               goto done;
00431             }
00432 
00433           for (i = low_bound; i <= high_bound; i++)
00434             {
00435               int element = value_bit_index (type, valaddr + embedded_offset,
00436                                              i);
00437 
00438               if (element < 0)
00439                 {
00440                   i = element;
00441                   goto maybe_bad_bstring;
00442                 }
00443               if (element)
00444                 {
00445                   if (need_comma)
00446                     fputs_filtered (", ", stream);
00447                   print_type_scalar (range, i, stream);
00448                   need_comma = 1;
00449 
00450                   if (i + 1 <= high_bound
00451                       && value_bit_index (type, valaddr + embedded_offset,
00452                                           ++i))
00453                     {
00454                       int j = i;
00455 
00456                       fputs_filtered ("..", stream);
00457                       while (i + 1 <= high_bound
00458                              && value_bit_index (type,
00459                                                  valaddr + embedded_offset,
00460                                                  ++i))
00461                         j = i;
00462                       print_type_scalar (range, j, stream);
00463                     }
00464                 }
00465             }
00466         done:
00467           fputs_filtered ("}", stream);
00468         }
00469       break;
00470 
00471     case TYPE_CODE_RANGE:
00472       if (TYPE_LENGTH (type) == TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
00473         {
00474           m2_val_print (TYPE_TARGET_TYPE (type), valaddr, embedded_offset,
00475                         address, stream, recurse, original_value, options);
00476           break;
00477         }
00478       /* FIXME: create_range_type does not set the unsigned bit in a
00479          range type (I think it probably should copy it from the target
00480          type), so we won't print values which are too large to
00481          fit in a signed integer correctly.  */
00482       /* FIXME: Doesn't handle ranges of enums correctly.  (Can't just
00483          print with the target type, though, because the size of our type
00484          and the target type might differ).  */
00485       /* FALLTHROUGH */
00486 
00487     case TYPE_CODE_REF:
00488     case TYPE_CODE_ENUM:
00489     case TYPE_CODE_FUNC:
00490     case TYPE_CODE_INT:
00491     case TYPE_CODE_FLT:
00492     case TYPE_CODE_METHOD:
00493     case TYPE_CODE_VOID:
00494     case TYPE_CODE_ERROR:
00495     case TYPE_CODE_UNDEF:
00496     case TYPE_CODE_BOOL:
00497     case TYPE_CODE_CHAR:
00498     default:
00499       generic_val_print (type, valaddr, embedded_offset, address,
00500                          stream, recurse, original_value, options,
00501                          &m2_decorations);
00502       break;
00503     }
00504   gdb_flush (stream);
00505 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines