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