* c-typeprint.c (c_type_print_varspec_prefix,
[platform/upstream/binutils.git] / gdb / f-valprint.c
1 /* Support for printing Fortran values for GDB, the GNU debugger.
2    Copyright 1993, 1994 Free Software Foundation, Inc.
3    Contributed by Motorola.  Adapted from the C definitions by Farooq Butt
4    (fmbutt@engage.sps.mot.com), additionally worked over by Stan Shebs.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
21
22 #include "defs.h"
23 #include <string.h>
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "value.h"
28 #include "demangle.h"
29 #include "valprint.h"
30 #include "language.h"
31 #include "f-lang.h" 
32 #include "frame.h"
33 #include "gdbcore.h"
34 #include "command.h"
35
36 extern struct obstack dont_print_obstack;
37
38 extern unsigned int print_max; /* No of array elements to print */ 
39
40 extern int calc_f77_array_dims PARAMS ((struct type *));
41
42 int f77_array_offset_tbl[MAX_FORTRAN_DIMS+1][2];
43
44 /* Array which holds offsets to be applied to get a row's elements
45    for a given array. Array also holds the size of each subarray.  */
46
47 /* The following macro gives us the size of the nth dimension, Where 
48    n is 1 based. */ 
49
50 #define F77_DIM_SIZE(n) (f77_array_offset_tbl[n][1])
51
52 /* The following gives us the offset for row n where n is 1-based. */ 
53
54 #define F77_DIM_OFFSET(n) (f77_array_offset_tbl[n][0])
55
56 int 
57 f77_get_dynamic_lowerbound (type, lower_bound)
58      struct type *type;
59      int *lower_bound; 
60 {
61   CORE_ADDR current_frame_addr;   
62   CORE_ADDR ptr_to_lower_bound; 
63   
64   switch (TYPE_ARRAY_LOWER_BOUND_TYPE (type))
65     {
66     case BOUND_BY_VALUE_ON_STACK:
67       current_frame_addr = selected_frame->frame;
68       if (current_frame_addr > 0) 
69         {
70           *lower_bound = 
71             read_memory_integer (current_frame_addr + 
72                                  TYPE_ARRAY_LOWER_BOUND_VALUE (type),
73                                  4);
74         }
75       else
76         {
77           *lower_bound = DEFAULT_LOWER_BOUND; 
78           return BOUND_FETCH_ERROR; 
79         }
80       break; 
81       
82     case BOUND_SIMPLE:
83       *lower_bound = TYPE_ARRAY_LOWER_BOUND_VALUE (type);
84       break; 
85       
86     case BOUND_CANNOT_BE_DETERMINED: 
87       error ("Lower bound may not be '*' in F77"); 
88       break; 
89       
90     case BOUND_BY_REF_ON_STACK:
91       current_frame_addr = selected_frame->frame;
92       if (current_frame_addr > 0) 
93         {
94           ptr_to_lower_bound = 
95             read_memory_integer (current_frame_addr + 
96                                  TYPE_ARRAY_LOWER_BOUND_VALUE (type),
97                                  4);
98           *lower_bound = read_memory_integer (ptr_to_lower_bound, 4); 
99         }
100       else
101         {
102           *lower_bound = DEFAULT_LOWER_BOUND; 
103           return BOUND_FETCH_ERROR; 
104         }
105       break; 
106       
107     case BOUND_BY_REF_IN_REG: 
108     case BOUND_BY_VALUE_IN_REG: 
109     default: 
110       error ("??? unhandled dynamic array bound type ???");
111       break; 
112     }
113   return BOUND_FETCH_OK;
114 }
115
116 int 
117 f77_get_dynamic_upperbound (type, upper_bound)
118      struct type *type;
119      int *upper_bound;
120 {
121   CORE_ADDR current_frame_addr = 0;
122   CORE_ADDR ptr_to_upper_bound; 
123   
124   switch (TYPE_ARRAY_UPPER_BOUND_TYPE (type))
125     {
126     case BOUND_BY_VALUE_ON_STACK:
127       current_frame_addr = selected_frame->frame;
128       if (current_frame_addr > 0) 
129         {
130           *upper_bound = 
131             read_memory_integer (current_frame_addr + 
132                                  TYPE_ARRAY_UPPER_BOUND_VALUE (type),
133                                  4);
134         }
135       else
136         {
137           *upper_bound = DEFAULT_UPPER_BOUND; 
138           return BOUND_FETCH_ERROR; 
139         }
140       break; 
141       
142     case BOUND_SIMPLE:
143       *upper_bound = TYPE_ARRAY_UPPER_BOUND_VALUE (type);
144       break; 
145       
146     case BOUND_CANNOT_BE_DETERMINED: 
147       /* we have an assumed size array on our hands. Assume that 
148          upper_bound == lower_bound so that we show at least 
149          1 element.If the user wants to see more elements, let 
150          him manually ask for 'em and we'll subscript the 
151          array and show him */
152       f77_get_dynamic_lowerbound (type, upper_bound);
153       break; 
154       
155     case BOUND_BY_REF_ON_STACK:
156       current_frame_addr = selected_frame->frame;
157       if (current_frame_addr > 0) 
158         {
159           ptr_to_upper_bound = 
160             read_memory_integer (current_frame_addr + 
161                                  TYPE_ARRAY_UPPER_BOUND_VALUE (type),
162                                  4);
163           *upper_bound = read_memory_integer(ptr_to_upper_bound, 4); 
164         }
165       else
166         {
167           *upper_bound = DEFAULT_UPPER_BOUND; 
168           return BOUND_FETCH_ERROR;
169         }
170       break; 
171       
172     case BOUND_BY_REF_IN_REG: 
173     case BOUND_BY_VALUE_IN_REG: 
174     default: 
175       error ("??? unhandled dynamic array bound type ???");
176       break; 
177     }
178   return BOUND_FETCH_OK;
179 }
180
181 /* Obtain F77 adjustable array dimensions */ 
182
183 void
184 f77_get_dynamic_length_of_aggregate (type)
185      struct type *type;
186 {
187   int upper_bound = -1;
188   int lower_bound = 1; 
189   int retcode; 
190   
191   /* Recursively go all the way down into a possibly multi-dimensional
192      F77 array and get the bounds.  For simple arrays, this is pretty
193      easy but when the bounds are dynamic, we must be very careful 
194      to add up all the lengths correctly.  Not doing this right 
195      will lead to horrendous-looking arrays in parameter lists.
196      
197      This function also works for strings which behave very 
198      similarly to arrays.  */ 
199   
200   if (TYPE_CODE(TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
201       || TYPE_CODE(TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRING)
202     f77_get_dynamic_length_of_aggregate (TYPE_TARGET_TYPE (type));
203   
204   /* Recursion ends here, start setting up lengths.  */ 
205   retcode = f77_get_dynamic_lowerbound (type, &lower_bound); 
206   if (retcode == BOUND_FETCH_ERROR)
207     error ("Cannot obtain valid array lower bound"); 
208   
209   retcode = f77_get_dynamic_upperbound (type, &upper_bound); 
210   if (retcode == BOUND_FETCH_ERROR)
211     error ("Cannot obtain valid array upper bound"); 
212   
213   /* Patch in a valid length value. */ 
214   
215   TYPE_LENGTH (type) =
216     (upper_bound - lower_bound + 1) * TYPE_LENGTH (TYPE_TARGET_TYPE (type));
217 }       
218
219 /* Print a FORTRAN COMPLEX value of type TYPE, pointed to in GDB by VALADDR,
220    on STREAM.  which_complex indicates precision, which may be regular,
221    *16, or *32 */
222
223 void
224 f77_print_cmplx (valaddr, type, stream, which_complex)
225      char *valaddr;
226      struct type *type;
227      FILE *stream;
228      int which_complex;
229 {
230   float *f1,*f2;
231   double *d1, *d2;
232   
233   switch (which_complex)
234     {
235     case TARGET_COMPLEX_BIT:
236       f1 = (float *) valaddr;
237       f2 = (float *) (valaddr + sizeof(float));
238       fprintf_filtered (stream, "(%.7e,%.7e)", *f1, *f2);
239       break;
240       
241     case TARGET_DOUBLE_COMPLEX_BIT:
242       d1 = (double *) valaddr;
243       d2 = (double *) (valaddr + sizeof(double));
244       fprintf_filtered (stream, "(%.16e,%.16e)", *d1, *d2);
245       break;
246 #if 0
247     case TARGET_EXT_COMPLEX_BIT:
248       fprintf_filtered (stream, "<complex*32 format unavailable, "
249                        "printing raw data>\n");
250       
251       fprintf_filtered (stream, "( [ "); 
252       
253       for (i = 0;i<4;i++)
254         fprintf_filtered (stream, "0x%x ",
255                          * ( (unsigned int *) valaddr+i));
256       
257       fprintf_filtered (stream, "],\n  [ "); 
258       
259       for (i=4;i<8;i++)
260         fprintf_filtered (stream, "0x%x ",
261                          * ((unsigned int *) valaddr+i));
262       
263       fprintf_filtered (stream, "] )");
264       
265       break;
266 #endif
267     default:
268       fprintf_filtered (stream, "<cannot handle complex of this type>");
269       break;
270     }
271 }
272
273 /* Function that sets up the array offset,size table for the array 
274    type "type".  */ 
275
276 void 
277 f77_create_arrayprint_offset_tbl (type, stream)
278      struct type *type;
279      FILE *stream;
280 {
281   struct type *tmp_type;
282   int eltlen; 
283   int ndimen = 1;
284   int upper, lower, retcode; 
285   
286   tmp_type = type; 
287   
288   while ((TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY)) 
289     {
290       if (TYPE_ARRAY_UPPER_BOUND_TYPE (tmp_type) == BOUND_CANNOT_BE_DETERMINED)
291         fprintf_filtered (stream, "<assumed size array> "); 
292       
293       retcode = f77_get_dynamic_upperbound (tmp_type, &upper);
294       if (retcode == BOUND_FETCH_ERROR)
295         error ("Cannot obtain dynamic upper bound"); 
296       
297       retcode = f77_get_dynamic_lowerbound(tmp_type,&lower); 
298       if (retcode == BOUND_FETCH_ERROR)
299         error("Cannot obtain dynamic lower bound"); 
300       
301       F77_DIM_SIZE (ndimen) = upper - lower + 1;
302       
303       if (ndimen == 1)
304         F77_DIM_OFFSET (ndimen) = 1;
305       else
306         F77_DIM_OFFSET (ndimen) = 
307           F77_DIM_OFFSET (ndimen - 1) * F77_DIM_SIZE(ndimen - 1);
308       
309       tmp_type = TYPE_TARGET_TYPE (tmp_type);
310       ndimen++; 
311     }
312   
313   eltlen = TYPE_LENGTH (tmp_type); 
314
315   /* Now we multiply eltlen by all the offsets, so that later we 
316      can print out array elements correctly.  Up till now we 
317      know an offset to apply to get the item but we also 
318      have to know how much to add to get to the next item */
319   
320   tmp_type = type; 
321   ndimen = 1; 
322   
323   while ((TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY)) 
324     {
325       F77_DIM_OFFSET (ndimen) *= eltlen; 
326       ndimen++;
327       tmp_type = TYPE_TARGET_TYPE (tmp_type);
328     }
329 }
330
331 /* Actual function which prints out F77 arrays, Valaddr == address in 
332    the superior.  Address == the address in the inferior.  */
333
334 void 
335 f77_print_array_1 (nss, ndimensions, type, valaddr, address, 
336                    stream, format, deref_ref, recurse, pretty)
337      int nss;
338      int ndimensions; 
339      char *valaddr;
340      struct type *type;
341      CORE_ADDR address;
342      FILE *stream;
343      int format;
344      int deref_ref;
345      int recurse;
346      enum val_prettyprint pretty;
347 {
348   int i;
349   
350   if (nss != ndimensions)
351     {
352       for (i = 0; i< F77_DIM_SIZE(nss); i++)
353         {
354           fprintf_filtered (stream, "( ");
355           f77_print_array_1 (nss + 1, ndimensions, TYPE_TARGET_TYPE (type),
356                             valaddr + i * F77_DIM_OFFSET (nss),
357                             address + i * F77_DIM_OFFSET (nss), 
358                             stream, format, deref_ref, recurse, pretty, i);
359           fprintf_filtered (stream, ") ");
360         }
361     }
362   else
363     {
364       for (i = 0; (i < F77_DIM_SIZE (nss) && i < print_max); i++)
365         {
366           val_print (TYPE_TARGET_TYPE (type),
367                      valaddr + i * F77_DIM_OFFSET (ndimensions),
368                      address + i * F77_DIM_OFFSET (ndimensions),
369                      stream, format, deref_ref, recurse, pretty); 
370
371           if (i != (F77_DIM_SIZE (nss) - 1))
372             fprintf_filtered (stream, ", "); 
373           
374           if (i == print_max - 1)
375             fprintf_filtered (stream, "...");
376         }
377     }
378 }
379
380 /* This function gets called to print an F77 array, we set up some 
381    stuff and then immediately call f77_print_array_1() */
382
383 void 
384 f77_print_array (type, valaddr, address, stream, format, deref_ref, recurse, 
385                  pretty)
386      struct type *type;
387      char *valaddr;
388      CORE_ADDR address;
389      FILE *stream;
390      int format;
391      int deref_ref;
392      int recurse;
393      enum val_prettyprint pretty;
394 {
395   int ndimensions; 
396   
397   ndimensions = calc_f77_array_dims (type); 
398   
399   if (ndimensions > MAX_FORTRAN_DIMS || ndimensions < 0)
400     error ("Type node corrupt! F77 arrays cannot have %d subscripts (%d Max)",
401            ndimensions, MAX_FORTRAN_DIMS);
402   
403   /* Since F77 arrays are stored column-major, we set up an 
404      offset table to get at the various row's elements. The 
405      offset table contains entries for both offset and subarray size. */ 
406   
407   f77_create_arrayprint_offset_tbl (type, stream); 
408   
409   f77_print_array_1 (1, ndimensions, type, valaddr, address, stream, format, 
410                      deref_ref, recurse, pretty);
411 }
412
413 \f
414 /* Print data of type TYPE located at VALADDR (within GDB), which came from
415    the inferior at address ADDRESS, onto stdio stream STREAM according to
416    FORMAT (a letter or 0 for natural format).  The data at VALADDR is in
417    target byte order.
418    
419    If the data are a string pointer, returns the number of string characters
420    printed.
421    
422    If DEREF_REF is nonzero, then dereference references, otherwise just print
423    them like pointers.
424    
425    The PRETTY parameter controls prettyprinting.  */
426
427 int
428 f_val_print (type, valaddr, address, stream, format, deref_ref, recurse,
429              pretty)
430      struct type *type;
431      char *valaddr;
432      CORE_ADDR address;
433      FILE *stream;
434      int format;
435      int deref_ref;
436      int recurse;
437      enum val_prettyprint pretty;
438 {
439   register unsigned int i = 0;          /* Number of characters printed */
440   unsigned len;
441   struct type *elttype;
442   LONGEST val;
443   char *localstr;
444   char *straddr;
445   CORE_ADDR addr;
446   
447   switch (TYPE_CODE (type))
448     {
449     case TYPE_CODE_LITERAL_STRING: 
450       /* It is trivial to print out F77 strings allocated in the 
451          superior process. The address field is actually a 
452          pointer to the bytes of the literal. For an internalvar,
453          valaddr points to a ptr. which points to 
454          VALUE_LITERAL_DATA(value->internalvar->value)
455          and for straight literals (i.e. of the form 'hello world'), 
456          valaddr points a ptr to VALUE_LITERAL_DATA(value). */
457       
458       /* First dereference valaddr.  */ 
459       
460       straddr = * (CORE_ADDR *) valaddr; 
461       
462       if (straddr)
463         {
464           len = TYPE_LENGTH (type); 
465           localstr = alloca (len + 1);
466           strncpy (localstr, straddr, len);
467           localstr[len] = '\0'; 
468           fprintf_filtered (stream, "'%s'", localstr);
469         }
470       else
471         fprintf_filtered (stream, "Unable to print literal F77 string");
472       break; 
473       
474       /* Strings are a little bit funny. They can be viewed as 
475          monolithic arrays that are dealt with as atomic data 
476          items. As such they are the only atomic data items whose 
477          contents are not located in the superior process. Instead 
478          instead of having the actual data, they contain pointers 
479          to addresses in the inferior where data is located.  Thus 
480          instead of using valaddr, we use address. */ 
481       
482     case TYPE_CODE_STRING: 
483       f77_get_dynamic_length_of_aggregate (type);
484       val_print_string (address, TYPE_LENGTH (type), stream);
485       break;
486       
487     case TYPE_CODE_ARRAY:
488       fprintf_filtered (stream, "("); 
489       f77_print_array (type, valaddr, address, stream, format, 
490                        deref_ref, recurse, pretty); 
491       fprintf_filtered (stream, ")");
492       break;
493 #if 0
494       /* Array of unspecified length: treat like pointer to first elt.  */
495       valaddr = (char *) &address;
496       /* FALL THROUGH */
497 #endif 
498     case TYPE_CODE_PTR:
499       if (format && format != 's')
500         {
501           print_scalar_formatted (valaddr, type, format, 0, stream);
502           break;
503         }
504       else
505         {
506           addr = unpack_pointer (type, valaddr);
507           elttype = TYPE_TARGET_TYPE (type);
508           
509           if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
510             {
511               /* Try to print what function it points to.  */
512               print_address_demangle (addr, stream, demangle);
513               /* Return value is irrelevant except for string pointers.  */
514               return 0;
515             }
516           
517           if (addressprint && format != 's')
518             fprintf_filtered (stream, "0x%x", addr);
519           
520           /* For a pointer to char or unsigned char, also print the string
521              pointed to, unless pointer is null.  */
522           if (TYPE_LENGTH (elttype) == 1
523               && TYPE_CODE (elttype) == TYPE_CODE_INT
524               && (format == 0 || format == 's')
525               && addr != 0)
526             i = val_print_string (addr, 0, stream);
527           
528           /* Return number of characters printed, plus one for the
529              terminating null if we have "reached the end".  */
530           return (i + (print_max && i != print_max));
531         }
532       break;
533       
534     case TYPE_CODE_FUNC:
535       if (format)
536         {
537           print_scalar_formatted (valaddr, type, format, 0, stream);
538           break;
539         }
540       /* FIXME, we should consider, at least for ANSI C language, eliminating
541          the distinction made between FUNCs and POINTERs to FUNCs.  */
542       fprintf_filtered (stream, "{");
543       type_print (type, "", stream, -1);
544       fprintf_filtered (stream, "} ");
545       /* Try to print what function it points to, and its address.  */
546       print_address_demangle (address, stream, demangle);
547       break;
548       
549     case TYPE_CODE_INT:
550       format = format ? format : output_format;
551       if (format)
552         print_scalar_formatted (valaddr, type, format, 0, stream);
553       else
554         {
555           val_print_type_code_int (type, valaddr, stream);
556           /* C and C++ has no single byte int type, char is used instead.
557              Since we don't know whether the value is really intended to
558              be used as an integer or a character, print the character
559              equivalent as well. */
560           if (TYPE_LENGTH (type) == 1)
561             {
562               fputs_filtered (" ", stream);
563               LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
564                              stream);
565             }
566         }
567       break;
568       
569     case TYPE_CODE_FLT:
570       if (format)
571         print_scalar_formatted (valaddr, type, format, 0, stream);
572       else
573         print_floating (valaddr, type, stream);
574       break;
575       
576     case TYPE_CODE_VOID:
577       fprintf_filtered (stream, "VOID");
578       break;
579       
580     case TYPE_CODE_ERROR:
581       fprintf_filtered (stream, "<error type>");
582       break;
583       
584     case TYPE_CODE_RANGE:
585       /* FIXME, we should not ever have to print one of these yet.  */
586       fprintf_filtered (stream, "<range type>");
587       break;
588       
589     case TYPE_CODE_BOOL:
590       format = format ? format : output_format;
591       if (format)
592         print_scalar_formatted (valaddr, type, format, 0, stream);
593       else
594         {
595           val = 0; 
596           switch (TYPE_LENGTH(type))
597             {
598             case 1:
599               val = unpack_long (builtin_type_f_logical_s1, valaddr);
600               break ; 
601               
602             case 2: 
603               val = unpack_long (builtin_type_f_logical_s2, valaddr);
604               break ; 
605               
606             case 4: 
607               val = unpack_long (builtin_type_f_logical, valaddr);
608               break ; 
609               
610             default:
611               error ("Logicals of length %d bytes not supported",
612                      TYPE_LENGTH (type));
613               
614             }
615           
616           if (val == 0) 
617             fprintf_filtered (stream, ".FALSE.");
618           else 
619             if (val == 1) 
620               fprintf_filtered (stream, ".TRUE.");
621             else
622               /* Not a legitimate logical type, print as an integer.  */
623               {
624                 /* Bash the type code temporarily.  */
625                 TYPE_CODE (type) = TYPE_CODE_INT;
626                 f_val_print (type, valaddr, address, stream, format, 
627                              deref_ref, recurse, pretty); 
628                 /* Restore the type code so later uses work as intended. */
629                 TYPE_CODE (type) = TYPE_CODE_BOOL; 
630               }
631         }
632       break;
633       
634     case TYPE_CODE_LITERAL_COMPLEX:
635       /* We know that the literal complex is stored in the superior 
636          process not the inferior and that it is 16 bytes long. 
637          Just like the case above with a literal array, the
638          bytes for the the literal complex number are stored   
639          at the address pointed to by valaddr */ 
640       
641       if (TYPE_LENGTH (type) == 32)
642         error ("Cannot currently print out complex*32 literals");
643       
644       /* First dereference valaddr.  */ 
645       
646       addr = * (CORE_ADDR *) valaddr; 
647       
648       if (addr)
649         {
650           fprintf_filtered (stream, "("); 
651           
652           if (TYPE_LENGTH(type) == 16) 
653             { 
654               fprintf_filtered (stream, "%.16f", * (double *) addr); 
655               fprintf_filtered (stream, ", %.16f", * (double *) 
656                                 (addr + sizeof(double)));
657             }
658           else
659             {
660               fprintf_filtered (stream, "%.8f", * (float *) addr); 
661               fprintf_filtered (stream, ", %.8f", * (float *) 
662                                 (addr + sizeof(float)));
663             }
664           fprintf_filtered (stream, ") ");             
665         }
666       else
667         fprintf_filtered (stream, "Unable to print literal F77 array");
668       break; 
669       
670     case TYPE_CODE_COMPLEX:
671       switch (TYPE_LENGTH (type))
672         {
673         case 8:
674           f77_print_cmplx (valaddr, type, stream, TARGET_COMPLEX_BIT);
675           break;
676           
677         case 16: 
678           f77_print_cmplx(valaddr, type, stream, TARGET_DOUBLE_COMPLEX_BIT);
679           break; 
680 #if 0
681         case 32:
682           f77_print_cmplx(valaddr, type, stream, TARGET_EXT_COMPLEX_BIT);
683           break; 
684 #endif
685         default:
686           error ("Cannot print out complex*%d variables", TYPE_LENGTH(type)); 
687         }
688       break;
689       
690     case TYPE_CODE_UNDEF:
691       /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
692          dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
693          and no complete type for struct foo in that file.  */
694       fprintf_filtered (stream, "<incomplete type>");
695       break;
696       
697     default:
698       error ("Invalid F77 type code %d in symbol table.", TYPE_CODE (type));
699     }
700   fflush (stream);
701   return 0;
702 }
703
704 void
705 list_all_visible_commons (funname)
706      char *funname;
707 {
708   SAVED_F77_COMMON_PTR  tmp;
709   
710   tmp = head_common_list;
711   
712   printf_filtered ("All COMMON blocks visible at this level:\n\n");
713   
714   while (tmp != NULL)
715     {
716       if (STREQ(tmp->owning_function,funname))
717         printf_filtered ("%s\n", tmp->name); 
718       
719       tmp = tmp->next;
720     }
721 }
722
723 /* This function is used to print out the values in a given COMMON 
724    block. It will always use the most local common block of the 
725    given name */ 
726
727 static void 
728 info_common_command (comname, from_tty)
729      char *comname;
730      int from_tty;
731 {
732   SAVED_F77_COMMON_PTR  the_common; 
733   COMMON_ENTRY_PTR entry; 
734   struct frame_info *fi;
735   register char *funname = 0;
736   struct symbol *func;
737   
738   /* We have been told to display the contents of F77 COMMON 
739      block supposedly visible in this function.  Let us 
740      first make sure that it is visible and if so, let 
741      us display its contents */ 
742   
743   fi = selected_frame; 
744   
745   if (fi == NULL)
746     error ("No frame selected"); 
747   
748   /* The following is generally ripped off from stack.c's routine 
749      print_frame_info() */ 
750   
751   func = find_pc_function (fi->pc);
752   if (func)
753     {
754       /* In certain pathological cases, the symtabs give the wrong
755          function (when we are in the first function in a file which
756          is compiled without debugging symbols, the previous function
757          is compiled with debugging symbols, and the "foo.o" symbol
758          that is supposed to tell us where the file with debugging symbols
759          ends has been truncated by ar because it is longer than 15
760          characters).
761          
762          So look in the minimal symbol tables as well, and if it comes
763          up with a larger address for the function use that instead.
764          I don't think this can ever cause any problems; there shouldn't
765          be any minimal symbols in the middle of a function.
766          FIXME:  (Not necessarily true.  What about text labels) */
767       
768       struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
769       
770       if (msymbol != NULL
771           && (SYMBOL_VALUE_ADDRESS (msymbol) 
772               > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
773         funname = SYMBOL_NAME (msymbol);
774       else
775         funname = SYMBOL_NAME (func);
776     }
777   else
778     {
779       register struct minimal_symbol *msymbol =
780         lookup_minimal_symbol_by_pc (fi->pc);
781       
782       if (msymbol != NULL)
783         funname = SYMBOL_NAME (msymbol);
784     }
785   
786   /* If comnname is NULL, we assume the user wishes to see the 
787      which COMMON blocks are visible here and then return */ 
788   
789   if (strlen (comname) == 0) 
790     {
791       list_all_visible_commons (funname);
792       return; 
793     }
794   
795   the_common = find_common_for_function (comname,funname); 
796   
797   if (the_common)
798     {
799       if (STREQ(comname,BLANK_COMMON_NAME_LOCAL))
800         printf_filtered ("Contents of blank COMMON block:\n");
801       else 
802         printf_filtered ("Contents of F77 COMMON block '%s':\n",comname); 
803       
804       printf_filtered ("\n"); 
805       entry = the_common->entries; 
806       
807       while (entry != NULL)
808         {
809           printf_filtered ("%s = ",SYMBOL_NAME(entry->symbol)); 
810           print_variable_value (entry->symbol,fi,stdout); 
811           printf_filtered ("\n"); 
812           entry = entry->next; 
813         }
814     }
815   else 
816     printf_filtered ("Cannot locate the common block %s in function '%s'\n",
817                     comname, funname);
818 }
819
820 /* This function is used to determine whether there is a
821    F77 common block visible at the current scope called 'comname'. */ 
822
823 int
824 there_is_a_visible_common_named (comname)
825      char *comname;
826 {
827   SAVED_F77_COMMON_PTR  the_common; 
828   struct frame_info *fi;
829   register char *funname = 0;
830   struct symbol *func;
831   
832   if (comname == NULL)
833     error ("Cannot deal with NULL common name!"); 
834   
835   fi = selected_frame; 
836   
837   if (fi == NULL)
838     error ("No frame selected"); 
839   
840   /* The following is generally ripped off from stack.c's routine 
841      print_frame_info() */ 
842   
843   func = find_pc_function (fi->pc);
844   if (func)
845     {
846       /* In certain pathological cases, the symtabs give the wrong
847          function (when we are in the first function in a file which
848          is compiled without debugging symbols, the previous function
849          is compiled with debugging symbols, and the "foo.o" symbol
850          that is supposed to tell us where the file with debugging symbols
851          ends has been truncated by ar because it is longer than 15
852          characters).
853          
854          So look in the minimal symbol tables as well, and if it comes
855          up with a larger address for the function use that instead.
856          I don't think this can ever cause any problems; there shouldn't
857          be any minimal symbols in the middle of a function.
858          FIXME:  (Not necessarily true.  What about text labels) */
859       
860       struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
861       
862       if (msymbol != NULL
863           && (SYMBOL_VALUE_ADDRESS (msymbol) 
864               > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
865         funname = SYMBOL_NAME (msymbol);
866       else
867         funname = SYMBOL_NAME (func);
868     }
869   else
870     {
871       register struct minimal_symbol *msymbol = 
872         lookup_minimal_symbol_by_pc (fi->pc);
873       
874       if (msymbol != NULL)
875         funname = SYMBOL_NAME (msymbol);
876     }
877   
878   the_common = find_common_for_function (comname, funname); 
879   
880   return (the_common ? 1 : 0);
881 }
882
883 void
884 _initialize_f_valprint ()
885 {
886   add_info ("common", info_common_command,
887             "Print out the values contained in a Fortran COMMON block.");
888 }