* valprint.c (val_print_string): Don't print leading space.
[external/binutils.git] / gdb / f-valprint.c
1 /* Support for printing Fortran values for GDB, the GNU debugger.
2
3    Copyright (C) 1993-1996, 1998-2000, 2003, 2005-2012 Free Software
4    Foundation, Inc.
5
6    Contributed by Motorola.  Adapted from the C definitions by Farooq Butt
7    (fmbutt@engage.sps.mot.com), additionally worked over by Stan Shebs.
8
9    This file is part of GDB.
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
23
24 #include "defs.h"
25 #include "gdb_string.h"
26 #include "symtab.h"
27 #include "gdbtypes.h"
28 #include "expression.h"
29 #include "value.h"
30 #include "valprint.h"
31 #include "language.h"
32 #include "f-lang.h"
33 #include "frame.h"
34 #include "gdbcore.h"
35 #include "command.h"
36 #include "block.h"
37
38 #if 0
39 static int there_is_a_visible_common_named (char *);
40 #endif
41
42 extern void _initialize_f_valprint (void);
43 static void info_common_command (char *, int);
44 static void list_all_visible_commons (const char *);
45 static void f77_create_arrayprint_offset_tbl (struct type *,
46                                               struct ui_file *);
47 static void f77_get_dynamic_length_of_aggregate (struct type *);
48
49 int f77_array_offset_tbl[MAX_FORTRAN_DIMS + 1][2];
50
51 /* Array which holds offsets to be applied to get a row's elements
52    for a given array.  Array also holds the size of each subarray.  */
53
54 /* The following macro gives us the size of the nth dimension, Where 
55    n is 1 based.  */
56
57 #define F77_DIM_SIZE(n) (f77_array_offset_tbl[n][1])
58
59 /* The following gives us the offset for row n where n is 1-based.  */
60
61 #define F77_DIM_OFFSET(n) (f77_array_offset_tbl[n][0])
62
63 int
64 f77_get_lowerbound (struct type *type)
65 {
66   if (TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED (type))
67     error (_("Lower bound may not be '*' in F77"));
68
69   return TYPE_ARRAY_LOWER_BOUND_VALUE (type);
70 }
71
72 int
73 f77_get_upperbound (struct type *type)
74 {
75   if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
76     {
77       /* We have an assumed size array on our hands.  Assume that
78          upper_bound == lower_bound so that we show at least 1 element.
79          If the user wants to see more elements, let him manually ask for 'em
80          and we'll subscript the array and show him.  */
81
82       return f77_get_lowerbound (type);
83     }
84
85   return TYPE_ARRAY_UPPER_BOUND_VALUE (type);
86 }
87
88 /* Obtain F77 adjustable array dimensions.  */
89
90 static void
91 f77_get_dynamic_length_of_aggregate (struct type *type)
92 {
93   int upper_bound = -1;
94   int lower_bound = 1;
95
96   /* Recursively go all the way down into a possibly multi-dimensional
97      F77 array and get the bounds.  For simple arrays, this is pretty
98      easy but when the bounds are dynamic, we must be very careful 
99      to add up all the lengths correctly.  Not doing this right 
100      will lead to horrendous-looking arrays in parameter lists.
101
102      This function also works for strings which behave very 
103      similarly to arrays.  */
104
105   if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
106       || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRING)
107     f77_get_dynamic_length_of_aggregate (TYPE_TARGET_TYPE (type));
108
109   /* Recursion ends here, start setting up lengths.  */
110   lower_bound = f77_get_lowerbound (type);
111   upper_bound = f77_get_upperbound (type);
112
113   /* Patch in a valid length value.  */
114
115   TYPE_LENGTH (type) =
116     (upper_bound - lower_bound + 1)
117     * TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type)));
118 }
119
120 /* Function that sets up the array offset,size table for the array 
121    type "type".  */
122
123 static void
124 f77_create_arrayprint_offset_tbl (struct type *type, struct ui_file *stream)
125 {
126   struct type *tmp_type;
127   int eltlen;
128   int ndimen = 1;
129   int upper, lower;
130
131   tmp_type = type;
132
133   while ((TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY))
134     {
135       upper = f77_get_upperbound (tmp_type);
136       lower = f77_get_lowerbound (tmp_type);
137
138       F77_DIM_SIZE (ndimen) = upper - lower + 1;
139
140       tmp_type = TYPE_TARGET_TYPE (tmp_type);
141       ndimen++;
142     }
143
144   /* Now we multiply eltlen by all the offsets, so that later we 
145      can print out array elements correctly.  Up till now we 
146      know an offset to apply to get the item but we also 
147      have to know how much to add to get to the next item.  */
148
149   ndimen--;
150   eltlen = TYPE_LENGTH (tmp_type);
151   F77_DIM_OFFSET (ndimen) = eltlen;
152   while (--ndimen > 0)
153     {
154       eltlen *= F77_DIM_SIZE (ndimen + 1);
155       F77_DIM_OFFSET (ndimen) = eltlen;
156     }
157 }
158
159
160
161 /* Actual function which prints out F77 arrays, Valaddr == address in 
162    the superior.  Address == the address in the inferior.  */
163
164 static void
165 f77_print_array_1 (int nss, int ndimensions, struct type *type,
166                    const gdb_byte *valaddr,
167                    int embedded_offset, CORE_ADDR address,
168                    struct ui_file *stream, int recurse,
169                    const struct value *val,
170                    const struct value_print_options *options,
171                    int *elts)
172 {
173   int i;
174
175   if (nss != ndimensions)
176     {
177       for (i = 0;
178            (i < F77_DIM_SIZE (nss) && (*elts) < options->print_max);
179            i++)
180         {
181           fprintf_filtered (stream, "( ");
182           f77_print_array_1 (nss + 1, ndimensions, TYPE_TARGET_TYPE (type),
183                              valaddr,
184                              embedded_offset + i * F77_DIM_OFFSET (nss),
185                              address,
186                              stream, recurse, val, options, elts);
187           fprintf_filtered (stream, ") ");
188         }
189       if (*elts >= options->print_max && i < F77_DIM_SIZE (nss)) 
190         fprintf_filtered (stream, "...");
191     }
192   else
193     {
194       for (i = 0; i < F77_DIM_SIZE (nss) && (*elts) < options->print_max;
195            i++, (*elts)++)
196         {
197           val_print (TYPE_TARGET_TYPE (type),
198                      valaddr,
199                      embedded_offset + i * F77_DIM_OFFSET (ndimensions),
200                      address, stream, recurse,
201                      val, options, current_language);
202
203           if (i != (F77_DIM_SIZE (nss) - 1))
204             fprintf_filtered (stream, ", ");
205
206           if ((*elts == options->print_max - 1)
207               && (i != (F77_DIM_SIZE (nss) - 1)))
208             fprintf_filtered (stream, "...");
209         }
210     }
211 }
212
213 /* This function gets called to print an F77 array, we set up some 
214    stuff and then immediately call f77_print_array_1().  */
215
216 static void
217 f77_print_array (struct type *type, const gdb_byte *valaddr,
218                  int embedded_offset,
219                  CORE_ADDR address, struct ui_file *stream,
220                  int recurse,
221                  const struct value *val,
222                  const struct value_print_options *options)
223 {
224   int ndimensions;
225   int elts = 0;
226
227   ndimensions = calc_f77_array_dims (type);
228
229   if (ndimensions > MAX_FORTRAN_DIMS || ndimensions < 0)
230     error (_("\
231 Type node corrupt! F77 arrays cannot have %d subscripts (%d Max)"),
232            ndimensions, MAX_FORTRAN_DIMS);
233
234   /* Since F77 arrays are stored column-major, we set up an 
235      offset table to get at the various row's elements.  The 
236      offset table contains entries for both offset and subarray size.  */
237
238   f77_create_arrayprint_offset_tbl (type, stream);
239
240   f77_print_array_1 (1, ndimensions, type, valaddr, embedded_offset,
241                      address, stream, recurse, val, options, &elts);
242 }
243 \f
244
245 /* Decorations for Fortran.  */
246
247 static const struct generic_val_print_decorations f_decorations =
248 {
249   "(",
250   ",",
251   ")",
252   ".TRUE.",
253   ".FALSE.",
254   "VOID",
255 };
256
257 /* See val_print for a description of the various parameters of this
258    function; they are identical.  */
259
260 void
261 f_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
262              CORE_ADDR address, struct ui_file *stream, int recurse,
263              const struct value *original_value,
264              const struct value_print_options *options)
265 {
266   struct gdbarch *gdbarch = get_type_arch (type);
267   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
268   unsigned int i = 0;   /* Number of characters printed.  */
269   struct type *elttype;
270   LONGEST val;
271   CORE_ADDR addr;
272   int index;
273
274   CHECK_TYPEDEF (type);
275   switch (TYPE_CODE (type))
276     {
277     case TYPE_CODE_STRING:
278       f77_get_dynamic_length_of_aggregate (type);
279       LA_PRINT_STRING (stream, builtin_type (gdbarch)->builtin_char,
280                        valaddr + embedded_offset,
281                        TYPE_LENGTH (type), NULL, 0, options);
282       break;
283
284     case TYPE_CODE_ARRAY:
285       if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_CHAR)
286         {
287           fprintf_filtered (stream, "(");
288           f77_print_array (type, valaddr, embedded_offset,
289                            address, stream, recurse, original_value, options);
290           fprintf_filtered (stream, ")");
291         }
292       else
293         {
294           struct type *ch_type = TYPE_TARGET_TYPE (type);
295
296           f77_get_dynamic_length_of_aggregate (type);
297           LA_PRINT_STRING (stream, ch_type,
298                            valaddr + embedded_offset,
299                            TYPE_LENGTH (type) / TYPE_LENGTH (ch_type),
300                            NULL, 0, options);
301         }
302       break;
303
304     case TYPE_CODE_PTR:
305       if (options->format && options->format != 's')
306         {
307           val_print_scalar_formatted (type, valaddr, embedded_offset,
308                                       original_value, options, 0, stream);
309           break;
310         }
311       else
312         {
313           int want_space = 0;
314
315           addr = unpack_pointer (type, valaddr + embedded_offset);
316           elttype = check_typedef (TYPE_TARGET_TYPE (type));
317
318           if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
319             {
320               /* Try to print what function it points to.  */
321               print_function_pointer_address (options, gdbarch, addr, stream);
322               return;
323             }
324
325           if (options->addressprint && options->format != 's')
326             {
327               fputs_filtered (paddress (gdbarch, addr), stream);
328               want_space = 1;
329             }
330
331           /* For a pointer to char or unsigned char, also print the string
332              pointed to, unless pointer is null.  */
333           if (TYPE_LENGTH (elttype) == 1
334               && TYPE_CODE (elttype) == TYPE_CODE_INT
335               && (options->format == 0 || options->format == 's')
336               && addr != 0)
337             {
338               if (want_space)
339                 fputs_filtered (" ", stream);
340               i = val_print_string (TYPE_TARGET_TYPE (type), NULL, addr, -1,
341                                     stream, options);
342             }
343           return;
344         }
345       break;
346
347     case TYPE_CODE_INT:
348       if (options->format || options->output_format)
349         {
350           struct value_print_options opts = *options;
351
352           opts.format = (options->format ? options->format
353                          : options->output_format);
354           val_print_scalar_formatted (type, valaddr, embedded_offset,
355                                       original_value, options, 0, stream);
356         }
357       else
358         {
359           val_print_type_code_int (type, valaddr + embedded_offset, stream);
360           /* C and C++ has no single byte int type, char is used instead.
361              Since we don't know whether the value is really intended to
362              be used as an integer or a character, print the character
363              equivalent as well.  */
364           if (TYPE_LENGTH (type) == 1)
365             {
366               LONGEST c;
367
368               fputs_filtered (" ", stream);
369               c = unpack_long (type, valaddr + embedded_offset);
370               LA_PRINT_CHAR ((unsigned char) c, type, stream);
371             }
372         }
373       break;
374
375     case TYPE_CODE_STRUCT:
376     case TYPE_CODE_UNION:
377       /* Starting from the Fortran 90 standard, Fortran supports derived
378          types.  */
379       fprintf_filtered (stream, "( ");
380       for (index = 0; index < TYPE_NFIELDS (type); index++)
381         {
382           int offset = TYPE_FIELD_BITPOS (type, index) / 8;
383
384           val_print (TYPE_FIELD_TYPE (type, index), valaddr,
385                      embedded_offset + offset,
386                      address, stream, recurse + 1,
387                      original_value, options, current_language);
388           if (index != TYPE_NFIELDS (type) - 1)
389             fputs_filtered (", ", stream);
390         }
391       fprintf_filtered (stream, " )");
392       break;     
393
394     case TYPE_CODE_REF:
395     case TYPE_CODE_FUNC:
396     case TYPE_CODE_FLAGS:
397     case TYPE_CODE_FLT:
398     case TYPE_CODE_VOID:
399     case TYPE_CODE_ERROR:
400     case TYPE_CODE_RANGE:
401     case TYPE_CODE_UNDEF:
402     case TYPE_CODE_COMPLEX:
403     case TYPE_CODE_BOOL:
404     case TYPE_CODE_CHAR:
405     default:
406       generic_val_print (type, valaddr, embedded_offset, address,
407                          stream, recurse, original_value, options,
408                          &f_decorations);
409       break;
410     }
411   gdb_flush (stream);
412 }
413
414 static void
415 list_all_visible_commons (const char *funname)
416 {
417   SAVED_F77_COMMON_PTR tmp;
418
419   tmp = head_common_list;
420
421   printf_filtered (_("All COMMON blocks visible at this level:\n\n"));
422
423   while (tmp != NULL)
424     {
425       if (strcmp (tmp->owning_function, funname) == 0)
426         printf_filtered ("%s\n", tmp->name);
427
428       tmp = tmp->next;
429     }
430 }
431
432 /* This function is used to print out the values in a given COMMON 
433    block.  It will always use the most local common block of the 
434    given name.  */
435
436 static void
437 info_common_command (char *comname, int from_tty)
438 {
439   SAVED_F77_COMMON_PTR the_common;
440   COMMON_ENTRY_PTR entry;
441   struct frame_info *fi;
442   const char *funname = 0;
443   struct symbol *func;
444
445   /* We have been told to display the contents of F77 COMMON 
446      block supposedly visible in this function.  Let us 
447      first make sure that it is visible and if so, let 
448      us display its contents.  */
449
450   fi = get_selected_frame (_("No frame selected"));
451
452   /* The following is generally ripped off from stack.c's routine 
453      print_frame_info().  */
454
455   func = find_pc_function (get_frame_pc (fi));
456   if (func)
457     {
458       /* In certain pathological cases, the symtabs give the wrong
459          function (when we are in the first function in a file which
460          is compiled without debugging symbols, the previous function
461          is compiled with debugging symbols, and the "foo.o" symbol
462          that is supposed to tell us where the file with debugging symbols
463          ends has been truncated by ar because it is longer than 15
464          characters).
465
466          So look in the minimal symbol tables as well, and if it comes
467          up with a larger address for the function use that instead.
468          I don't think this can ever cause any problems; there shouldn't
469          be any minimal symbols in the middle of a function.
470          FIXME:  (Not necessarily true.  What about text labels?)  */
471
472       struct minimal_symbol *msymbol = 
473         lookup_minimal_symbol_by_pc (get_frame_pc (fi));
474
475       if (msymbol != NULL
476           && (SYMBOL_VALUE_ADDRESS (msymbol)
477               > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
478         funname = SYMBOL_LINKAGE_NAME (msymbol);
479       else
480         funname = SYMBOL_LINKAGE_NAME (func);
481     }
482   else
483     {
484       struct minimal_symbol *msymbol =
485         lookup_minimal_symbol_by_pc (get_frame_pc (fi));
486
487       if (msymbol != NULL)
488         funname = SYMBOL_LINKAGE_NAME (msymbol);
489       else /* Got no 'funname', code below will fail.  */
490         error (_("No function found for frame."));
491     }
492
493   /* If comname is NULL, we assume the user wishes to see the 
494      which COMMON blocks are visible here and then return.  */
495
496   if (comname == 0)
497     {
498       list_all_visible_commons (funname);
499       return;
500     }
501
502   the_common = find_common_for_function (comname, funname);
503
504   if (the_common)
505     {
506       if (strcmp (comname, BLANK_COMMON_NAME_LOCAL) == 0)
507         printf_filtered (_("Contents of blank COMMON block:\n"));
508       else
509         printf_filtered (_("Contents of F77 COMMON block '%s':\n"), comname);
510
511       printf_filtered ("\n");
512       entry = the_common->entries;
513
514       while (entry != NULL)
515         {
516           print_variable_and_value (NULL, entry->symbol, fi, gdb_stdout, 0);
517           entry = entry->next;
518         }
519     }
520   else
521     printf_filtered (_("Cannot locate the common block %s in function '%s'\n"),
522                      comname, funname);
523 }
524
525 /* This function is used to determine whether there is a
526    F77 common block visible at the current scope called 'comname'.  */
527
528 #if 0
529 static int
530 there_is_a_visible_common_named (char *comname)
531 {
532   SAVED_F77_COMMON_PTR the_common;
533   struct frame_info *fi;
534   char *funname = 0;
535   struct symbol *func;
536
537   if (comname == NULL)
538     error (_("Cannot deal with NULL common name!"));
539
540   fi = get_selected_frame (_("No frame selected"));
541
542   /* The following is generally ripped off from stack.c's routine 
543      print_frame_info().  */
544
545   func = find_pc_function (fi->pc);
546   if (func)
547     {
548       /* In certain pathological cases, the symtabs give the wrong
549          function (when we are in the first function in a file which
550          is compiled without debugging symbols, the previous function
551          is compiled with debugging symbols, and the "foo.o" symbol
552          that is supposed to tell us where the file with debugging symbols
553          ends has been truncated by ar because it is longer than 15
554          characters).
555
556          So look in the minimal symbol tables as well, and if it comes
557          up with a larger address for the function use that instead.
558          I don't think this can ever cause any problems; there shouldn't
559          be any minimal symbols in the middle of a function.
560          FIXME:  (Not necessarily true.  What about text labels?)  */
561
562       struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
563
564       if (msymbol != NULL
565           && (SYMBOL_VALUE_ADDRESS (msymbol)
566               > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
567         funname = SYMBOL_LINKAGE_NAME (msymbol);
568       else
569         funname = SYMBOL_LINKAGE_NAME (func);
570     }
571   else
572     {
573       struct minimal_symbol *msymbol =
574         lookup_minimal_symbol_by_pc (fi->pc);
575
576       if (msymbol != NULL)
577         funname = SYMBOL_LINKAGE_NAME (msymbol);
578     }
579
580   the_common = find_common_for_function (comname, funname);
581
582   return (the_common ? 1 : 0);
583 }
584 #endif
585
586 void
587 _initialize_f_valprint (void)
588 {
589   add_info ("common", info_common_command,
590             _("Print out the values contained in a Fortran COMMON block."));
591   if (xdb_commands)
592     add_com ("lc", class_info, info_common_command,
593              _("Print out the values contained in a Fortran COMMON block."));
594 }