f-valprint.c revision 1.10 1 1.1 christos /* Support for printing Fortran values for GDB, the GNU debugger.
2 1.1 christos
3 1.10 christos Copyright (C) 1993-2023 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos Contributed by Motorola. Adapted from the C definitions by Farooq Butt
6 1.1 christos (fmbutt (at) engage.sps.mot.com), additionally worked over by Stan Shebs.
7 1.1 christos
8 1.1 christos This file is part of GDB.
9 1.1 christos
10 1.1 christos This program is free software; you can redistribute it and/or modify
11 1.1 christos it under the terms of the GNU General Public License as published by
12 1.1 christos the Free Software Foundation; either version 3 of the License, or
13 1.1 christos (at your option) any later version.
14 1.1 christos
15 1.1 christos This program is distributed in the hope that it will be useful,
16 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
17 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 1.1 christos GNU General Public License for more details.
19 1.1 christos
20 1.1 christos You should have received a copy of the GNU General Public License
21 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 1.1 christos
23 1.1 christos #include "defs.h"
24 1.10 christos #include "annotate.h"
25 1.1 christos #include "symtab.h"
26 1.1 christos #include "gdbtypes.h"
27 1.1 christos #include "expression.h"
28 1.1 christos #include "value.h"
29 1.1 christos #include "valprint.h"
30 1.1 christos #include "language.h"
31 1.1 christos #include "f-lang.h"
32 1.1 christos #include "frame.h"
33 1.1 christos #include "gdbcore.h"
34 1.1 christos #include "command.h"
35 1.1 christos #include "block.h"
36 1.1 christos #include "dictionary.h"
37 1.9 christos #include "cli/cli-style.h"
38 1.9 christos #include "gdbarch.h"
39 1.10 christos #include "f-array-walker.h"
40 1.1 christos
41 1.1 christos static void f77_get_dynamic_length_of_aggregate (struct type *);
42 1.1 christos
43 1.9 christos LONGEST
44 1.1 christos f77_get_lowerbound (struct type *type)
45 1.1 christos {
46 1.10 christos if (type->bounds ()->low.kind () != PROP_CONST)
47 1.1 christos error (_("Lower bound may not be '*' in F77"));
48 1.1 christos
49 1.9 christos return type->bounds ()->low.const_val ();
50 1.1 christos }
51 1.1 christos
52 1.9 christos LONGEST
53 1.1 christos f77_get_upperbound (struct type *type)
54 1.1 christos {
55 1.10 christos if (type->bounds ()->high.kind () != PROP_CONST)
56 1.1 christos {
57 1.1 christos /* We have an assumed size array on our hands. Assume that
58 1.1 christos upper_bound == lower_bound so that we show at least 1 element.
59 1.1 christos If the user wants to see more elements, let him manually ask for 'em
60 1.1 christos and we'll subscript the array and show him. */
61 1.1 christos
62 1.1 christos return f77_get_lowerbound (type);
63 1.1 christos }
64 1.1 christos
65 1.9 christos return type->bounds ()->high.const_val ();
66 1.1 christos }
67 1.1 christos
68 1.1 christos /* Obtain F77 adjustable array dimensions. */
69 1.1 christos
70 1.1 christos static void
71 1.1 christos f77_get_dynamic_length_of_aggregate (struct type *type)
72 1.1 christos {
73 1.1 christos int upper_bound = -1;
74 1.1 christos int lower_bound = 1;
75 1.1 christos
76 1.1 christos /* Recursively go all the way down into a possibly multi-dimensional
77 1.1 christos F77 array and get the bounds. For simple arrays, this is pretty
78 1.1 christos easy but when the bounds are dynamic, we must be very careful
79 1.1 christos to add up all the lengths correctly. Not doing this right
80 1.1 christos will lead to horrendous-looking arrays in parameter lists.
81 1.1 christos
82 1.1 christos This function also works for strings which behave very
83 1.1 christos similarly to arrays. */
84 1.1 christos
85 1.10 christos if (type->target_type ()->code () == TYPE_CODE_ARRAY
86 1.10 christos || type->target_type ()->code () == TYPE_CODE_STRING)
87 1.10 christos f77_get_dynamic_length_of_aggregate (type->target_type ());
88 1.1 christos
89 1.1 christos /* Recursion ends here, start setting up lengths. */
90 1.1 christos lower_bound = f77_get_lowerbound (type);
91 1.1 christos upper_bound = f77_get_upperbound (type);
92 1.1 christos
93 1.1 christos /* Patch in a valid length value. */
94 1.10 christos type->set_length ((upper_bound - lower_bound + 1)
95 1.10 christos * check_typedef (type->target_type ())->length ());
96 1.10 christos }
97 1.10 christos
98 1.10 christos /* Per-dimension statistics. */
99 1.10 christos
100 1.10 christos struct dimension_stats
101 1.10 christos {
102 1.10 christos /* The type of the index used to address elements in the dimension. */
103 1.10 christos struct type *index_type;
104 1.10 christos
105 1.10 christos /* Total number of elements in the dimension, counted as we go. */
106 1.10 christos int nelts;
107 1.10 christos };
108 1.10 christos
109 1.10 christos /* A class used by FORTRAN_PRINT_ARRAY as a specialisation of the array
110 1.10 christos walking template. This specialisation prints Fortran arrays. */
111 1.10 christos
112 1.10 christos class fortran_array_printer_impl : public fortran_array_walker_base_impl
113 1.10 christos {
114 1.10 christos public:
115 1.10 christos /* Constructor. TYPE is the array type being printed, ADDRESS is the
116 1.10 christos address in target memory for the object of TYPE being printed. VAL is
117 1.10 christos the GDB value (of TYPE) being printed. STREAM is where to print to,
118 1.10 christos RECOURSE is passed through (and prevents infinite recursion), and
119 1.10 christos OPTIONS are the printing control options. */
120 1.10 christos explicit fortran_array_printer_impl (struct type *type,
121 1.10 christos CORE_ADDR address,
122 1.10 christos struct value *val,
123 1.10 christos struct ui_file *stream,
124 1.10 christos int recurse,
125 1.10 christos const struct value_print_options *options)
126 1.10 christos : m_elts (0),
127 1.10 christos m_val (val),
128 1.10 christos m_stream (stream),
129 1.10 christos m_recurse (recurse),
130 1.10 christos m_options (options),
131 1.10 christos m_dimension (0),
132 1.10 christos m_nrepeats (0),
133 1.10 christos m_stats (0)
134 1.10 christos { /* Nothing. */ }
135 1.10 christos
136 1.10 christos /* Called while iterating over the array bounds. When SHOULD_CONTINUE is
137 1.10 christos false then we must return false, as we have reached the end of the
138 1.10 christos array bounds for this dimension. However, we also return false if we
139 1.10 christos have printed too many elements (after printing '...'). In all other
140 1.10 christos cases, return true. */
141 1.10 christos bool continue_walking (bool should_continue)
142 1.10 christos {
143 1.10 christos bool cont = should_continue && (m_elts < m_options->print_max);
144 1.10 christos if (!cont && should_continue)
145 1.10 christos gdb_puts ("...", m_stream);
146 1.10 christos return cont;
147 1.10 christos }
148 1.10 christos
149 1.10 christos /* Called when we start iterating over a dimension. If it's not the
150 1.10 christos inner most dimension then print an opening '(' character. */
151 1.10 christos void start_dimension (struct type *index_type, LONGEST nelts, bool inner_p)
152 1.10 christos {
153 1.10 christos size_t dim_indx = m_dimension++;
154 1.10 christos
155 1.10 christos m_elt_type_prev = nullptr;
156 1.10 christos if (m_stats.size () < m_dimension)
157 1.10 christos {
158 1.10 christos m_stats.resize (m_dimension);
159 1.10 christos m_stats[dim_indx].index_type = index_type;
160 1.10 christos m_stats[dim_indx].nelts = nelts;
161 1.10 christos }
162 1.10 christos
163 1.10 christos gdb_puts ("(", m_stream);
164 1.10 christos }
165 1.10 christos
166 1.10 christos /* Called when we finish processing a batch of items within a dimension
167 1.10 christos of the array. Depending on whether this is the inner most dimension
168 1.10 christos or not we print different things, but this is all about adding
169 1.10 christos separators between elements, and dimensions of the array. */
170 1.10 christos void finish_dimension (bool inner_p, bool last_p)
171 1.10 christos {
172 1.10 christos gdb_puts (")", m_stream);
173 1.10 christos if (!last_p)
174 1.10 christos gdb_puts (" ", m_stream);
175 1.10 christos
176 1.10 christos m_dimension--;
177 1.10 christos }
178 1.10 christos
179 1.10 christos /* Called when processing dimensions of the array other than the
180 1.10 christos innermost one. WALK_1 is the walker to normally call, ELT_TYPE is
181 1.10 christos the type of the element being extracted, and ELT_OFF is the offset
182 1.10 christos of the element from the start of array being walked, INDEX_TYPE
183 1.10 christos and INDEX is the type and the value respectively of the element's
184 1.10 christos index in the dimension currently being walked and LAST_P is true
185 1.10 christos only when this is the last element that will be processed in this
186 1.10 christos dimension. */
187 1.10 christos void process_dimension (gdb::function_view<void (struct type *,
188 1.10 christos int, bool)> walk_1,
189 1.10 christos struct type *elt_type, LONGEST elt_off,
190 1.10 christos LONGEST index, bool last_p)
191 1.10 christos {
192 1.10 christos size_t dim_indx = m_dimension - 1;
193 1.10 christos struct type *elt_type_prev = m_elt_type_prev;
194 1.10 christos LONGEST elt_off_prev = m_elt_off_prev;
195 1.10 christos bool repeated = (m_options->repeat_count_threshold < UINT_MAX
196 1.10 christos && elt_type_prev != nullptr
197 1.10 christos && (m_elts + ((m_nrepeats + 1)
198 1.10 christos * m_stats[dim_indx + 1].nelts)
199 1.10 christos <= m_options->print_max)
200 1.10 christos && dimension_contents_eq (m_val, elt_type,
201 1.10 christos elt_off_prev, elt_off));
202 1.10 christos
203 1.10 christos if (repeated)
204 1.10 christos m_nrepeats++;
205 1.10 christos if (!repeated || last_p)
206 1.10 christos {
207 1.10 christos LONGEST nrepeats = m_nrepeats;
208 1.10 christos
209 1.10 christos m_nrepeats = 0;
210 1.10 christos if (nrepeats >= m_options->repeat_count_threshold)
211 1.10 christos {
212 1.10 christos annotate_elt_rep (nrepeats + 1);
213 1.10 christos gdb_printf (m_stream, "%p[<repeats %s times>%p]",
214 1.10 christos metadata_style.style ().ptr (),
215 1.10 christos plongest (nrepeats + 1),
216 1.10 christos nullptr);
217 1.10 christos annotate_elt_rep_end ();
218 1.10 christos if (!repeated)
219 1.10 christos gdb_puts (" ", m_stream);
220 1.10 christos m_elts += nrepeats * m_stats[dim_indx + 1].nelts;
221 1.10 christos }
222 1.10 christos else
223 1.10 christos for (LONGEST i = nrepeats; i > 0; i--)
224 1.10 christos {
225 1.10 christos maybe_print_array_index (m_stats[dim_indx].index_type,
226 1.10 christos index - nrepeats + repeated,
227 1.10 christos m_stream, m_options);
228 1.10 christos walk_1 (elt_type_prev, elt_off_prev, repeated && i == 1);
229 1.10 christos }
230 1.10 christos
231 1.10 christos if (!repeated)
232 1.10 christos {
233 1.10 christos /* We need to specially handle the case of hitting `print_max'
234 1.10 christos exactly as recursing would cause lone `(...)' to be printed.
235 1.10 christos And we need to print `...' by hand if the skipped element
236 1.10 christos would be the last one processed, because the subsequent call
237 1.10 christos to `continue_walking' from our caller won't do that. */
238 1.10 christos if (m_elts < m_options->print_max)
239 1.10 christos {
240 1.10 christos maybe_print_array_index (m_stats[dim_indx].index_type, index,
241 1.10 christos m_stream, m_options);
242 1.10 christos walk_1 (elt_type, elt_off, last_p);
243 1.10 christos nrepeats++;
244 1.10 christos }
245 1.10 christos else if (last_p)
246 1.10 christos gdb_puts ("...", m_stream);
247 1.10 christos }
248 1.10 christos }
249 1.10 christos
250 1.10 christos m_elt_type_prev = elt_type;
251 1.10 christos m_elt_off_prev = elt_off;
252 1.10 christos }
253 1.10 christos
254 1.10 christos /* Called to process an element of ELT_TYPE at offset ELT_OFF from the
255 1.10 christos start of the parent object, where INDEX is the value of the element's
256 1.10 christos index in the dimension currently being walked and LAST_P is true only
257 1.10 christos when this is the last element to be processed in this dimension. */
258 1.10 christos void process_element (struct type *elt_type, LONGEST elt_off,
259 1.10 christos LONGEST index, bool last_p)
260 1.10 christos {
261 1.10 christos size_t dim_indx = m_dimension - 1;
262 1.10 christos struct type *elt_type_prev = m_elt_type_prev;
263 1.10 christos LONGEST elt_off_prev = m_elt_off_prev;
264 1.10 christos bool repeated = (m_options->repeat_count_threshold < UINT_MAX
265 1.10 christos && elt_type_prev != nullptr
266 1.10 christos && value_contents_eq (m_val, elt_off_prev, m_val, elt_off,
267 1.10 christos elt_type->length ()));
268 1.10 christos
269 1.10 christos if (repeated)
270 1.10 christos m_nrepeats++;
271 1.10 christos if (!repeated || last_p || m_elts + 1 == m_options->print_max)
272 1.10 christos {
273 1.10 christos LONGEST nrepeats = m_nrepeats;
274 1.10 christos bool printed = false;
275 1.10 christos
276 1.10 christos if (nrepeats != 0)
277 1.10 christos {
278 1.10 christos m_nrepeats = 0;
279 1.10 christos if (nrepeats >= m_options->repeat_count_threshold)
280 1.10 christos {
281 1.10 christos annotate_elt_rep (nrepeats + 1);
282 1.10 christos gdb_printf (m_stream, "%p[<repeats %s times>%p]",
283 1.10 christos metadata_style.style ().ptr (),
284 1.10 christos plongest (nrepeats + 1),
285 1.10 christos nullptr);
286 1.10 christos annotate_elt_rep_end ();
287 1.10 christos }
288 1.10 christos else
289 1.10 christos {
290 1.10 christos /* Extract the element value from the parent value. */
291 1.10 christos struct value *e_val
292 1.10 christos = value_from_component (m_val, elt_type, elt_off_prev);
293 1.10 christos
294 1.10 christos for (LONGEST i = nrepeats; i > 0; i--)
295 1.10 christos {
296 1.10 christos maybe_print_array_index (m_stats[dim_indx].index_type,
297 1.10 christos index - i + 1,
298 1.10 christos m_stream, m_options);
299 1.10 christos common_val_print (e_val, m_stream, m_recurse, m_options,
300 1.10 christos current_language);
301 1.10 christos if (i > 1)
302 1.10 christos gdb_puts (", ", m_stream);
303 1.10 christos }
304 1.10 christos }
305 1.10 christos printed = true;
306 1.10 christos }
307 1.10 christos
308 1.10 christos if (!repeated)
309 1.10 christos {
310 1.10 christos /* Extract the element value from the parent value. */
311 1.10 christos struct value *e_val
312 1.10 christos = value_from_component (m_val, elt_type, elt_off);
313 1.10 christos
314 1.10 christos if (printed)
315 1.10 christos gdb_puts (", ", m_stream);
316 1.10 christos maybe_print_array_index (m_stats[dim_indx].index_type, index,
317 1.10 christos m_stream, m_options);
318 1.10 christos common_val_print (e_val, m_stream, m_recurse, m_options,
319 1.10 christos current_language);
320 1.10 christos }
321 1.10 christos if (!last_p)
322 1.10 christos gdb_puts (", ", m_stream);
323 1.10 christos }
324 1.10 christos
325 1.10 christos m_elt_type_prev = elt_type;
326 1.10 christos m_elt_off_prev = elt_off;
327 1.10 christos ++m_elts;
328 1.10 christos }
329 1.10 christos
330 1.10 christos private:
331 1.10 christos /* Called to compare two VAL elements of ELT_TYPE at offsets OFFSET1
332 1.10 christos and OFFSET2 each. Handle subarrays recursively, because they may
333 1.10 christos have been sliced and we do not want to compare any memory contents
334 1.10 christos present between the slices requested. */
335 1.10 christos bool
336 1.10 christos dimension_contents_eq (const struct value *val, struct type *type,
337 1.10 christos LONGEST offset1, LONGEST offset2)
338 1.10 christos {
339 1.10 christos if (type->code () == TYPE_CODE_ARRAY
340 1.10 christos && type->target_type ()->code () != TYPE_CODE_CHAR)
341 1.10 christos {
342 1.10 christos /* Extract the range, and get lower and upper bounds. */
343 1.10 christos struct type *range_type = check_typedef (type)->index_type ();
344 1.10 christos LONGEST lowerbound, upperbound;
345 1.10 christos if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
346 1.10 christos error ("failed to get range bounds");
347 1.10 christos
348 1.10 christos /* CALC is used to calculate the offsets for each element. */
349 1.10 christos fortran_array_offset_calculator calc (type);
350 1.10 christos
351 1.10 christos struct type *subarray_type = check_typedef (type->target_type ());
352 1.10 christos for (LONGEST i = lowerbound; i < upperbound + 1; i++)
353 1.10 christos {
354 1.10 christos /* Use the index and the stride to work out a new offset. */
355 1.10 christos LONGEST index_offset = calc.index_offset (i);
356 1.1 christos
357 1.10 christos if (!dimension_contents_eq (val, subarray_type,
358 1.10 christos offset1 + index_offset,
359 1.10 christos offset2 + index_offset))
360 1.10 christos return false;
361 1.10 christos }
362 1.10 christos return true;
363 1.10 christos }
364 1.10 christos else
365 1.10 christos return value_contents_eq (val, offset1, val, offset2,
366 1.10 christos type->length ());
367 1.10 christos }
368 1.10 christos
369 1.10 christos /* The number of elements printed so far. */
370 1.10 christos int m_elts;
371 1.10 christos
372 1.10 christos /* The value from which we are printing elements. */
373 1.10 christos struct value *m_val;
374 1.1 christos
375 1.10 christos /* The stream we should print too. */
376 1.10 christos struct ui_file *m_stream;
377 1.1 christos
378 1.10 christos /* The recursion counter, passed through when we print each element. */
379 1.10 christos int m_recurse;
380 1.1 christos
381 1.10 christos /* The print control options. Gives us the maximum number of elements to
382 1.10 christos print, and is passed through to each element that we print. */
383 1.10 christos const struct value_print_options *m_options = nullptr;
384 1.6 christos
385 1.10 christos /* The number of the current dimension being handled. */
386 1.10 christos LONGEST m_dimension;
387 1.1 christos
388 1.10 christos /* The number of element repetitions in the current series. */
389 1.10 christos LONGEST m_nrepeats;
390 1.6 christos
391 1.10 christos /* The type and offset from M_VAL of the element handled in the previous
392 1.10 christos iteration over the current dimension. */
393 1.10 christos struct type *m_elt_type_prev;
394 1.10 christos LONGEST m_elt_off_prev;
395 1.1 christos
396 1.10 christos /* Per-dimension stats. */
397 1.10 christos std::vector<struct dimension_stats> m_stats;
398 1.10 christos };
399 1.1 christos
400 1.10 christos /* This function gets called to print a Fortran array. */
401 1.1 christos
402 1.1 christos static void
403 1.10 christos fortran_print_array (struct type *type, CORE_ADDR address,
404 1.10 christos struct ui_file *stream, int recurse,
405 1.10 christos const struct value *val,
406 1.10 christos const struct value_print_options *options)
407 1.10 christos {
408 1.10 christos fortran_array_walker<fortran_array_printer_impl> p
409 1.10 christos (type, address, (struct value *) val, stream, recurse, options);
410 1.10 christos p.walk ();
411 1.1 christos }
412 1.1 christos
413 1.1 christos
415 1.1 christos /* Decorations for Fortran. */
416 1.1 christos
417 1.1 christos static const struct generic_val_print_decorations f_decorations =
418 1.1 christos {
419 1.1 christos "(",
420 1.1 christos ",",
421 1.1 christos ")",
422 1.1 christos ".TRUE.",
423 1.9 christos ".FALSE.",
424 1.6 christos "void",
425 1.6 christos "{",
426 1.1 christos "}"
427 1.1 christos };
428 1.9 christos
429 1.1 christos /* See f-lang.h. */
430 1.1 christos
431 1.10 christos void
432 1.10 christos f_language::value_print_inner (struct value *val, struct ui_file *stream,
433 1.10 christos int recurse,
434 1.1 christos const struct value_print_options *options) const
435 1.9 christos {
436 1.10 christos struct type *type = check_typedef (value_type (val));
437 1.6 christos struct gdbarch *gdbarch = type->arch ();
438 1.1 christos int printed_field = 0; /* Number of fields printed. */
439 1.1 christos struct type *elttype;
440 1.1 christos CORE_ADDR addr;
441 1.10 christos int index;
442 1.9 christos const gdb_byte *valaddr = value_contents_for_printing (val).data ();
443 1.1 christos const CORE_ADDR address = value_address (val);
444 1.9 christos
445 1.1 christos switch (type->code ())
446 1.1 christos {
447 1.1 christos case TYPE_CODE_STRING:
448 1.10 christos f77_get_dynamic_length_of_aggregate (type);
449 1.10 christos printstr (stream, builtin_type (gdbarch)->builtin_char, valaddr,
450 1.1 christos type->length (), NULL, 0, options);
451 1.1 christos break;
452 1.1 christos
453 1.10 christos case TYPE_CODE_ARRAY:
454 1.10 christos if (type->target_type ()->code () != TYPE_CODE_CHAR)
455 1.1 christos fortran_print_array (type, address, stream, recurse, val, options);
456 1.1 christos else
457 1.10 christos {
458 1.1 christos struct type *ch_type = type->target_type ();
459 1.1 christos
460 1.10 christos f77_get_dynamic_length_of_aggregate (type);
461 1.10 christos printstr (stream, ch_type, valaddr,
462 1.10 christos type->length () / ch_type->length (), NULL, 0,
463 1.1 christos options);
464 1.1 christos }
465 1.1 christos break;
466 1.1 christos
467 1.1 christos case TYPE_CODE_PTR:
468 1.1 christos if (options->format && options->format != 's')
469 1.9 christos {
470 1.1 christos value_print_scalar_formatted (val, options, 0, stream);
471 1.1 christos break;
472 1.1 christos }
473 1.1 christos else
474 1.1 christos {
475 1.1 christos int want_space = 0;
476 1.9 christos
477 1.10 christos addr = unpack_pointer (type, valaddr);
478 1.1 christos elttype = check_typedef (type->target_type ());
479 1.9 christos
480 1.1 christos if (elttype->code () == TYPE_CODE_FUNC)
481 1.1 christos {
482 1.1 christos /* Try to print what function it points to. */
483 1.1 christos print_function_pointer_address (options, gdbarch, addr, stream);
484 1.1 christos return;
485 1.1 christos }
486 1.1 christos
487 1.1 christos if (options->symbol_print)
488 1.1 christos want_space = print_address_demangle (options, gdbarch, addr,
489 1.1 christos stream, demangle);
490 1.1 christos else if (options->addressprint && options->format != 's')
491 1.10 christos {
492 1.1 christos gdb_puts (paddress (gdbarch, addr), stream);
493 1.1 christos want_space = 1;
494 1.1 christos }
495 1.1 christos
496 1.1 christos /* For a pointer to char or unsigned char, also print the string
497 1.10 christos pointed to, unless pointer is null. */
498 1.9 christos if (elttype->length () == 1
499 1.1 christos && elttype->code () == TYPE_CODE_INT
500 1.1 christos && (options->format == 0 || options->format == 's')
501 1.1 christos && addr != 0)
502 1.1 christos {
503 1.10 christos if (want_space)
504 1.10 christos gdb_puts (" ", stream);
505 1.6 christos val_print_string (type->target_type (), NULL, addr, -1,
506 1.1 christos stream, options);
507 1.1 christos }
508 1.1 christos return;
509 1.1 christos }
510 1.1 christos break;
511 1.1 christos
512 1.1 christos case TYPE_CODE_STRUCT:
513 1.10 christos case TYPE_CODE_UNION:
514 1.1 christos case TYPE_CODE_NAMELIST:
515 1.10 christos /* Starting from the Fortran 90 standard, Fortran supports derived
516 1.10 christos types. */
517 1.9 christos gdb_printf (stream, "( ");
518 1.10 christos for (index = 0; index < type->num_fields (); index++)
519 1.10 christos {
520 1.10 christos struct type *field_type
521 1.6 christos = check_typedef (type->field (index).type ());
522 1.9 christos
523 1.6 christos if (field_type->code () != TYPE_CODE_FUNC)
524 1.10 christos {
525 1.10 christos const char *field_name = type->field (index).name ();
526 1.10 christos struct value *field;
527 1.10 christos
528 1.10 christos if (type->code () == TYPE_CODE_NAMELIST)
529 1.10 christos {
530 1.10 christos /* While printing namelist items, fetch the appropriate
531 1.10 christos value field before printing its value. */
532 1.10 christos struct block_symbol sym
533 1.10 christos = lookup_symbol (field_name, get_selected_block (nullptr),
534 1.10 christos VAR_DOMAIN, nullptr);
535 1.10 christos if (sym.symbol == nullptr)
536 1.10 christos error (_("failed to find symbol for name list component %s"),
537 1.10 christos field_name);
538 1.10 christos field = value_of_variable (sym.symbol, sym.block);
539 1.10 christos }
540 1.10 christos else
541 1.6 christos field = value_field (val, index);
542 1.6 christos
543 1.10 christos if (printed_field > 0)
544 1.1 christos gdb_puts (", ", stream);
545 1.6 christos
546 1.6 christos if (field_name != NULL)
547 1.9 christos {
548 1.9 christos fputs_styled (field_name, variable_name_style.style (),
549 1.10 christos stream);
550 1.6 christos gdb_puts (" = ", stream);
551 1.6 christos }
552 1.9 christos
553 1.9 christos common_val_print (field, stream, recurse + 1,
554 1.6 christos options, current_language);
555 1.6 christos
556 1.6 christos ++printed_field;
557 1.6 christos }
558 1.10 christos }
559 1.1 christos gdb_printf (stream, " )");
560 1.1 christos break;
561 1.9 christos
562 1.9 christos case TYPE_CODE_BOOL:
563 1.9 christos if (options->format || options->output_format)
564 1.9 christos {
565 1.9 christos struct value_print_options opts = *options;
566 1.9 christos opts.format = (options->format ? options->format
567 1.9 christos : options->output_format);
568 1.9 christos value_print_scalar_formatted (val, &opts, 0, stream);
569 1.9 christos }
570 1.9 christos else
571 1.9 christos {
572 1.9 christos LONGEST longval = value_as_long (val);
573 1.9 christos /* The Fortran standard doesn't specify how logical types are
574 1.9 christos represented. Different compilers use different non zero
575 1.9 christos values to represent logical true. */
576 1.10 christos if (longval == 0)
577 1.9 christos gdb_puts (f_decorations.false_name, stream);
578 1.10 christos else
579 1.9 christos gdb_puts (f_decorations.true_name, stream);
580 1.9 christos }
581 1.9 christos break;
582 1.10 christos
583 1.1 christos case TYPE_CODE_INT:
584 1.1 christos case TYPE_CODE_REF:
585 1.1 christos case TYPE_CODE_FUNC:
586 1.1 christos case TYPE_CODE_FLAGS:
587 1.1 christos case TYPE_CODE_FLT:
588 1.1 christos case TYPE_CODE_VOID:
589 1.1 christos case TYPE_CODE_ERROR:
590 1.1 christos case TYPE_CODE_RANGE:
591 1.1 christos case TYPE_CODE_UNDEF:
592 1.1 christos case TYPE_CODE_COMPLEX:
593 1.1 christos case TYPE_CODE_CHAR:
594 1.9 christos default:
595 1.1 christos generic_value_print (val, stream, recurse, options, &f_decorations);
596 1.1 christos break;
597 1.1 christos }
598 1.1 christos }
599 1.1 christos
600 1.3 christos static void
601 1.1 christos info_common_command_for_block (const struct block *block, const char *comname,
602 1.1 christos int *any_printed)
603 1.1 christos {
604 1.1 christos struct block_iterator iter;
605 1.1 christos struct symbol *sym;
606 1.1 christos struct value_print_options opts;
607 1.1 christos
608 1.1 christos get_user_print_options (&opts);
609 1.1 christos
610 1.10 christos ALL_BLOCK_SYMBOLS (block, iter, sym)
611 1.1 christos if (sym->domain () == COMMON_BLOCK_DOMAIN)
612 1.10 christos {
613 1.1 christos const struct common_block *common = sym->value_common_block ();
614 1.1 christos size_t index;
615 1.10 christos
616 1.1 christos gdb_assert (sym->aclass () == LOC_COMMON_BLOCK);
617 1.9 christos
618 1.10 christos if (comname && (!sym->linkage_name ()
619 1.1 christos || strcmp (comname, sym->linkage_name ()) != 0))
620 1.1 christos continue;
621 1.1 christos
622 1.10 christos if (*any_printed)
623 1.1 christos gdb_putc ('\n');
624 1.1 christos else
625 1.9 christos *any_printed = 1;
626 1.10 christos if (sym->print_name ())
627 1.10 christos gdb_printf (_("Contents of F77 COMMON block '%s':\n"),
628 1.1 christos sym->print_name ());
629 1.10 christos else
630 1.1 christos gdb_printf (_("Contents of blank COMMON block:\n"));
631 1.1 christos
632 1.1 christos for (index = 0; index < common->n_entries; index++)
633 1.1 christos {
634 1.1 christos struct value *val = NULL;
635 1.10 christos
636 1.10 christos gdb_printf ("%s = ",
637 1.1 christos common->contents[index]->print_name ());
638 1.9 christos
639 1.1 christos try
640 1.1 christos {
641 1.1 christos val = value_of_variable (common->contents[index], block);
642 1.1 christos value_print (val, gdb_stdout, &opts);
643 1.1 christos }
644 1.9 christos
645 1.5 christos catch (const gdb_exception_error &except)
646 1.9 christos {
647 1.9 christos fprintf_styled (gdb_stdout, metadata_style.style (),
648 1.9 christos "<error reading variable: %s>",
649 1.5 christos except.what ());
650 1.5 christos }
651 1.10 christos
652 1.1 christos gdb_putc ('\n');
653 1.1 christos }
654 1.1 christos }
655 1.1 christos }
656 1.1 christos
657 1.1 christos /* This function is used to print out the values in a given COMMON
658 1.1 christos block. It will always use the most local common block of the
659 1.1 christos given name. */
660 1.1 christos
661 1.8 christos static void
662 1.1 christos info_common_command (const char *comname, int from_tty)
663 1.10 christos {
664 1.3 christos frame_info_ptr fi;
665 1.1 christos const struct block *block;
666 1.1 christos int values_printed = 0;
667 1.1 christos
668 1.1 christos /* We have been told to display the contents of F77 COMMON
669 1.1 christos block supposedly visible in this function. Let us
670 1.1 christos first make sure that it is visible and if so, let
671 1.1 christos us display its contents. */
672 1.1 christos
673 1.1 christos fi = get_selected_frame (_("No frame selected"));
674 1.1 christos
675 1.1 christos /* The following is generally ripped off from stack.c's routine
676 1.1 christos print_frame_info(). */
677 1.1 christos
678 1.1 christos block = get_frame_block (fi, 0);
679 1.1 christos if (block == NULL)
680 1.10 christos {
681 1.1 christos gdb_printf (_("No symbol table info available.\n"));
682 1.1 christos return;
683 1.1 christos }
684 1.1 christos
685 1.1 christos while (block)
686 1.1 christos {
687 1.1 christos info_common_command_for_block (block, comname, &values_printed);
688 1.10 christos /* After handling the function's top-level block, stop. Don't
689 1.10 christos continue to its superblock, the block of per-file symbols. */
690 1.1 christos if (block->function ())
691 1.10 christos break;
692 1.1 christos block = block->superblock ();
693 1.1 christos }
694 1.1 christos
695 1.1 christos if (!values_printed)
696 1.1 christos {
697 1.10 christos if (comname)
698 1.1 christos gdb_printf (_("No common block '%s'.\n"), comname);
699 1.10 christos else
700 1.1 christos gdb_printf (_("No common blocks.\n"));
701 1.1 christos }
702 1.1 christos }
703 1.9 christos
704 1.1 christos void _initialize_f_valprint ();
705 1.9 christos void
706 1.1 christos _initialize_f_valprint ()
707 1.1 christos {
708 1.1 christos add_info ("common", info_common_command,
709 1.1 christos _("Print out the values contained in a Fortran COMMON block."));
710 }
711