Updated Bulgarian and Russian translations for the gprof subdirectory
[binutils-gdb.git] / binutils / prdbg.c
blobcf39e9db39a7dad9d973420d1f159c365a92a2fc
1 /* prdbg.c -- Print out generic debugging information.
2 Copyright (C) 1995-2023 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>.
4 Tags style generation written by Salvador E. Tropea <set@computer.org>.
6 This file is part of GNU Binutils.
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 3 of the License, or
11 (at your option) any later version.
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.
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., 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* This file prints out the generic debugging information, by
24 supplying a set of routines to debug_write. */
26 #include "sysdep.h"
27 #include <assert.h>
28 #include "bfd.h"
29 #include "libiberty.h"
30 #include "demangle.h"
31 #include "debug.h"
32 #include "budbg.h"
34 /* This is the structure we use as a handle for these routines. */
36 struct pr_handle
38 /* File to print information to. */
39 FILE *f;
40 /* Current indentation level. */
41 unsigned int indent;
42 /* Type stack. */
43 struct pr_stack *stack;
44 /* Parameter number we are about to output. */
45 int parameter;
46 /* The following are used only by the tags code (tg_). */
47 /* Name of the file we are using. */
48 char *filename;
49 /* The BFD. */
50 bfd *abfd;
51 /* The symbols table for this BFD. */
52 asymbol **syms;
53 /* Pointer to a function to demangle symbols. */
54 char *(*demangler) (bfd *, const char *, int);
57 /* The type stack. */
59 struct pr_stack
61 /* Next element on the stack. */
62 struct pr_stack *next;
63 /* This element. */
64 char *type;
65 /* Current visibility of fields if this is a class. */
66 enum debug_visibility visibility;
67 /* Name of the current method we are handling. */
68 const char *method;
69 /* The following are used only by the tags code (tg_). */
70 /* Type for the container (struct, union, class, union class). */
71 const char *flavor;
72 /* A comma separated list of parent classes. */
73 char *parents;
74 /* How many parents contains parents. */
75 int num_parents;
78 static void indent (struct pr_handle *);
79 static bool push_type (struct pr_handle *, const char *);
80 static bool prepend_type (struct pr_handle *, const char *);
81 static bool append_type (struct pr_handle *, const char *);
82 static bool substitute_type (struct pr_handle *, const char *);
83 static bool indent_type (struct pr_handle *);
84 static char *pop_type (struct pr_handle *);
85 static void print_vma (bfd_vma, char *, bool, bool);
86 static bool pr_fix_visibility (struct pr_handle *, enum debug_visibility);
87 static bool pr_start_compilation_unit (void *, const char *);
88 static bool pr_start_source (void *, const char *);
89 static bool pr_empty_type (void *);
90 static bool pr_void_type (void *);
91 static bool pr_int_type (void *, unsigned int, bool);
92 static bool pr_float_type (void *, unsigned int);
93 static bool pr_complex_type (void *, unsigned int);
94 static bool pr_bool_type (void *, unsigned int);
95 static bool pr_enum_type
96 (void *, const char *, const char **, bfd_signed_vma *);
97 static bool pr_pointer_type (void *);
98 static bool pr_function_type (void *, int, bool);
99 static bool pr_reference_type (void *);
100 static bool pr_range_type (void *, bfd_signed_vma, bfd_signed_vma);
101 static bool pr_array_type (void *, bfd_signed_vma, bfd_signed_vma, bool);
102 static bool pr_set_type (void *, bool);
103 static bool pr_offset_type (void *);
104 static bool pr_method_type (void *, bool, int, bool);
105 static bool pr_const_type (void *);
106 static bool pr_volatile_type (void *);
107 static bool pr_start_struct_type
108 (void *, const char *, unsigned int, bool, unsigned int);
109 static bool pr_struct_field
110 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
111 static bool pr_end_struct_type (void *);
112 static bool pr_start_class_type
113 (void *, const char *, unsigned int, bool, unsigned int, bool, bool);
114 static bool pr_class_static_member
115 (void *, const char *, const char *, enum debug_visibility);
116 static bool pr_class_baseclass
117 (void *, bfd_vma, bool, enum debug_visibility);
118 static bool pr_class_start_method (void *, const char *);
119 static bool pr_class_method_variant
120 (void *, const char *, enum debug_visibility, bool, bool, bfd_vma, bool);
121 static bool pr_class_static_method_variant
122 (void *, const char *, enum debug_visibility, bool, bool);
123 static bool pr_class_end_method (void *);
124 static bool pr_end_class_type (void *);
125 static bool pr_typedef_type (void *, const char *);
126 static bool pr_tag_type
127 (void *, const char *, unsigned int, enum debug_type_kind);
128 static bool pr_typdef (void *, const char *);
129 static bool pr_tag (void *, const char *);
130 static bool pr_int_constant (void *, const char *, bfd_vma);
131 static bool pr_float_constant (void *, const char *, double);
132 static bool pr_typed_constant (void *, const char *, bfd_vma);
133 static bool pr_variable (void *, const char *, enum debug_var_kind, bfd_vma);
134 static bool pr_start_function (void *, const char *, bool);
135 static bool pr_function_parameter
136 (void *, const char *, enum debug_parm_kind, bfd_vma);
137 static bool pr_start_block (void *, bfd_vma);
138 static bool pr_end_block (void *, bfd_vma);
139 static bool pr_end_function (void *);
140 static bool pr_lineno (void *, const char *, unsigned long, bfd_vma);
141 static bool append_parent (struct pr_handle *, const char *);
142 /* Only used by tg_ code. */
143 static bool tg_fix_visibility
144 (struct pr_handle *, enum debug_visibility);
145 static void find_address_in_section (bfd *, asection *, void *);
146 static void translate_addresses (bfd *, char *, FILE *, asymbol **);
147 static const char *visibility_name (enum debug_visibility);
148 /* Tags style replacements. */
149 static bool tg_start_compilation_unit (void *, const char *);
150 static bool tg_start_source (void *, const char *);
151 static bool tg_enum_type
152 (void *, const char *, const char **, bfd_signed_vma *);
153 static bool tg_start_struct_type
154 (void *, const char *, unsigned int, bool, unsigned int);
155 static bool pr_struct_field
156 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
157 static bool tg_struct_field
158 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
159 static bool tg_struct_field
160 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
161 static bool tg_end_struct_type (void *);
162 static bool tg_start_class_type
163 (void *, const char *, unsigned int, bool, unsigned int, bool, bool);
164 static bool tg_class_static_member
165 (void *, const char *, const char *, enum debug_visibility);
166 static bool tg_class_baseclass (void *, bfd_vma, bool, enum debug_visibility);
167 static bool tg_class_method_variant
168 (void *, const char *, enum debug_visibility, bool, bool, bfd_vma, bool);
169 static bool tg_class_static_method_variant
170 (void *, const char *, enum debug_visibility, bool, bool);
171 static bool tg_end_class_type (void *);
172 static bool tg_tag_type
173 (void *, const char *, unsigned int, enum debug_type_kind);
174 static bool tg_typdef (void *, const char *);
175 static bool tg_tag (void *, const char *);
176 static bool tg_int_constant (void *, const char *, bfd_vma);
177 static bool tg_float_constant (void *, const char *, double);
178 static bool tg_typed_constant (void *, const char *, bfd_vma);
179 static bool tg_variable (void *, const char *, enum debug_var_kind, bfd_vma);
180 static bool tg_start_function (void *, const char *, bool);
181 static bool tg_function_parameter
182 (void *, const char *, enum debug_parm_kind, bfd_vma);
183 static bool tg_start_block (void *, bfd_vma);
184 static bool tg_end_block (void *, bfd_vma);
185 static bool tg_lineno (void *, const char *, unsigned long, bfd_vma);
187 static const struct debug_write_fns pr_fns =
189 pr_start_compilation_unit,
190 pr_start_source,
191 pr_empty_type,
192 pr_void_type,
193 pr_int_type,
194 pr_float_type,
195 pr_complex_type,
196 pr_bool_type,
197 pr_enum_type,
198 pr_pointer_type,
199 pr_function_type,
200 pr_reference_type,
201 pr_range_type,
202 pr_array_type,
203 pr_set_type,
204 pr_offset_type,
205 pr_method_type,
206 pr_const_type,
207 pr_volatile_type,
208 pr_start_struct_type,
209 pr_struct_field,
210 pr_end_struct_type,
211 pr_start_class_type,
212 pr_class_static_member,
213 pr_class_baseclass,
214 pr_class_start_method,
215 pr_class_method_variant,
216 pr_class_static_method_variant,
217 pr_class_end_method,
218 pr_end_class_type,
219 pr_typedef_type,
220 pr_tag_type,
221 pr_typdef,
222 pr_tag,
223 pr_int_constant,
224 pr_float_constant,
225 pr_typed_constant,
226 pr_variable,
227 pr_start_function,
228 pr_function_parameter,
229 pr_start_block,
230 pr_end_block,
231 pr_end_function,
232 pr_lineno
235 static const struct debug_write_fns tg_fns =
237 tg_start_compilation_unit,
238 tg_start_source,
239 pr_empty_type, /* Same, push_type. */
240 pr_void_type, /* Same, push_type. */
241 pr_int_type, /* Same, push_type. */
242 pr_float_type, /* Same, push_type. */
243 pr_complex_type, /* Same, push_type. */
244 pr_bool_type, /* Same, push_type. */
245 tg_enum_type,
246 pr_pointer_type, /* Same, changes to pointer. */
247 pr_function_type, /* Same, push_type. */
248 pr_reference_type, /* Same, changes to reference. */
249 pr_range_type, /* FIXME: What's that?. */
250 pr_array_type, /* Same, push_type. */
251 pr_set_type, /* FIXME: What's that?. */
252 pr_offset_type, /* FIXME: What's that?. */
253 pr_method_type, /* Same. */
254 pr_const_type, /* Same, changes to const. */
255 pr_volatile_type, /* Same, changes to volatile. */
256 tg_start_struct_type,
257 tg_struct_field,
258 tg_end_struct_type,
259 tg_start_class_type,
260 tg_class_static_member,
261 tg_class_baseclass,
262 pr_class_start_method, /* Same, remembers that's a method. */
263 tg_class_method_variant,
264 tg_class_static_method_variant,
265 pr_class_end_method, /* Same, forgets that's a method. */
266 tg_end_class_type,
267 pr_typedef_type, /* Same, just push type. */
268 tg_tag_type,
269 tg_typdef,
270 tg_tag,
271 tg_int_constant, /* Untested. */
272 tg_float_constant, /* Untested. */
273 tg_typed_constant, /* Untested. */
274 tg_variable,
275 tg_start_function,
276 tg_function_parameter,
277 tg_start_block,
278 tg_end_block,
279 pr_end_function, /* Same, does nothing. */
280 tg_lineno
283 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
285 /* Print out the generic debugging information recorded in dhandle. */
287 bool
288 print_debugging_info (FILE *f, void *dhandle, bfd *abfd, asymbol **syms,
289 char * (*demangler) (struct bfd *, const char *, int),
290 bool as_tags)
292 struct pr_handle info;
294 info.f = f;
295 info.indent = 0;
296 info.stack = NULL;
297 info.parameter = 0;
298 info.filename = NULL;
299 info.abfd = abfd;
300 info.syms = syms;
301 info.demangler = demangler;
303 if (as_tags)
305 fputs ("!_TAG_FILE_FORMAT\t2\t/extended format/\n", f);
306 fputs ("!_TAG_FILE_SORTED\t0\t/0=unsorted, 1=sorted/\n", f);
307 fputs ("!_TAG_PROGRAM_AUTHOR\tIan Lance Taylor, Salvador E. Tropea and others\t//\n", f);
308 fputs ("!_TAG_PROGRAM_NAME\tobjdump\t/From GNU binutils/\n", f);
311 return as_tags ? debug_write (dhandle, &tg_fns, (void *) & info)
312 : debug_write (dhandle, &pr_fns, (void *) & info);
315 /* Indent to the current indentation level. */
317 static void
318 indent (struct pr_handle *info)
320 unsigned int i;
322 for (i = 0; i < info->indent; i++)
323 putc (' ', info->f);
326 /* Push a type on the type stack. */
328 static bool
329 push_type (struct pr_handle *info, const char *type)
331 struct pr_stack *n;
333 if (type == NULL)
334 return false;
336 n = (struct pr_stack *) xmalloc (sizeof *n);
337 memset (n, 0, sizeof *n);
339 n->type = xstrdup (type);
340 n->visibility = DEBUG_VISIBILITY_IGNORE;
341 n->method = NULL;
342 n->next = info->stack;
343 info->stack = n;
345 return true;
348 /* Prepend a string onto the type on the top of the type stack. */
350 static bool
351 prepend_type (struct pr_handle *info, const char *s)
353 char *n;
355 assert (info->stack != NULL);
357 n = (char *) xmalloc (strlen (s) + strlen (info->stack->type) + 1);
358 sprintf (n, "%s%s", s, info->stack->type);
359 free (info->stack->type);
360 info->stack->type = n;
362 return true;
365 /* Append a string to the type on the top of the type stack. */
367 static bool
368 append_type (struct pr_handle *info, const char *s)
370 unsigned int len;
372 if (s == NULL)
373 return false;
375 assert (info->stack != NULL);
377 len = strlen (info->stack->type);
378 info->stack->type = (char *) xrealloc (info->stack->type,
379 len + strlen (s) + 1);
380 strcpy (info->stack->type + len, s);
382 return true;
385 /* Append a string to the parents on the top of the type stack. */
387 static bool
388 append_parent (struct pr_handle *info, const char *s)
390 unsigned int len;
392 if (s == NULL)
393 return false;
395 assert (info->stack != NULL);
397 len = info->stack->parents ? strlen (info->stack->parents) : 0;
398 info->stack->parents = (char *) xrealloc (info->stack->parents,
399 len + strlen (s) + 1);
400 strcpy (info->stack->parents + len, s);
402 return true;
405 /* We use an underscore to indicate where the name should go in a type
406 string. This function substitutes a string for the underscore. If
407 there is no underscore, the name follows the type. */
409 static bool
410 substitute_type (struct pr_handle *info, const char *s)
412 char *u;
414 assert (info->stack != NULL);
416 u = strchr (info->stack->type, '|');
417 if (u != NULL)
419 char *n;
421 n = (char *) xmalloc (strlen (info->stack->type) + strlen (s));
423 memcpy (n, info->stack->type, u - info->stack->type);
424 strcpy (n + (u - info->stack->type), s);
425 strcat (n, u + 1);
427 free (info->stack->type);
428 info->stack->type = n;
430 return true;
433 if (strchr (s, '|') != NULL
434 && (strchr (info->stack->type, '{') != NULL
435 || strchr (info->stack->type, '(') != NULL))
437 if (! prepend_type (info, "(")
438 || ! append_type (info, ")"))
439 return false;
442 if (*s == '\0')
443 return true;
445 return (append_type (info, " ")
446 && append_type (info, s));
449 /* Indent the type at the top of the stack by appending spaces. */
451 static bool
452 indent_type (struct pr_handle *info)
454 unsigned int i;
456 for (i = 0; i < info->indent; i++)
458 if (! append_type (info, " "))
459 return false;
462 return true;
465 /* Pop a type from the type stack. */
467 static char *
468 pop_type (struct pr_handle *info)
470 struct pr_stack *o;
471 char *ret;
473 assert (info->stack != NULL);
475 o = info->stack;
476 info->stack = o->next;
477 ret = o->type;
478 free (o);
480 return ret;
483 /* Print a VMA value into a string. */
485 static void
486 print_vma (bfd_vma vma, char *buf, bool unsignedp, bool hexp)
488 if (hexp)
489 sprintf (buf, "%#" PRIx64, (uint64_t) vma);
490 else if (unsignedp)
491 sprintf (buf, "%" PRIu64, (uint64_t) vma);
492 else
493 sprintf (buf, "%" PRId64, (int64_t) vma);
496 /* Start a new compilation unit. */
498 static bool
499 pr_start_compilation_unit (void *p, const char *filename)
501 struct pr_handle *info = (struct pr_handle *) p;
503 assert (info->indent == 0);
505 fprintf (info->f, "%s:\n", filename);
507 return true;
510 /* Start a source file within a compilation unit. */
512 static bool
513 pr_start_source (void *p, const char *filename)
515 struct pr_handle *info = (struct pr_handle *) p;
517 assert (info->indent == 0);
519 fprintf (info->f, " %s:\n", filename);
521 return true;
524 /* Push an empty type onto the type stack. */
526 static bool
527 pr_empty_type (void *p)
529 struct pr_handle *info = (struct pr_handle *) p;
531 return push_type (info, "<undefined>");
534 /* Push a void type onto the type stack. */
536 static bool
537 pr_void_type (void *p)
539 struct pr_handle *info = (struct pr_handle *) p;
541 return push_type (info, "void");
544 /* Push an integer type onto the type stack. */
546 static bool
547 pr_int_type (void *p, unsigned int size, bool unsignedp)
549 struct pr_handle *info = (struct pr_handle *) p;
550 char ab[40];
552 sprintf (ab, "%sint%d", unsignedp ? "u" : "", size * 8);
553 return push_type (info, ab);
556 /* Push a floating type onto the type stack. */
558 static bool
559 pr_float_type (void *p, unsigned int size)
561 struct pr_handle *info = (struct pr_handle *) p;
562 char ab[40];
564 if (size == 4)
565 return push_type (info, "float");
566 else if (size == 8)
567 return push_type (info, "double");
569 sprintf (ab, "float%d", size * 8);
570 return push_type (info, ab);
573 /* Push a complex type onto the type stack. */
575 static bool
576 pr_complex_type (void *p, unsigned int size)
578 struct pr_handle *info = (struct pr_handle *) p;
580 if (! pr_float_type (p, size))
581 return false;
583 return prepend_type (info, "complex ");
586 /* Push a bool type onto the type stack. */
588 static bool
589 pr_bool_type (void *p, unsigned int size)
591 struct pr_handle *info = (struct pr_handle *) p;
592 char ab[40];
594 sprintf (ab, "bool%d", size * 8);
596 return push_type (info, ab);
599 /* Push an enum type onto the type stack. */
601 static bool
602 pr_enum_type (void *p, const char *tag, const char **names,
603 bfd_signed_vma *values)
605 struct pr_handle *info = (struct pr_handle *) p;
606 unsigned int i;
607 bfd_signed_vma val;
609 if (! push_type (info, "enum "))
610 return false;
611 if (tag != NULL)
613 if (! append_type (info, tag)
614 || ! append_type (info, " "))
615 return false;
617 if (! append_type (info, "{ "))
618 return false;
620 if (names == NULL)
622 if (! append_type (info, "/* undefined */"))
623 return false;
625 else
627 val = 0;
628 for (i = 0; names[i] != NULL; i++)
630 if (i > 0)
632 if (! append_type (info, ", "))
633 return false;
636 if (! append_type (info, names[i]))
637 return false;
639 if (values[i] != val)
641 char ab[22];
643 print_vma (values[i], ab, false, false);
644 if (! append_type (info, " = ")
645 || ! append_type (info, ab))
646 return false;
647 val = values[i];
650 ++val;
654 return append_type (info, " }");
657 /* Turn the top type on the stack into a pointer. */
659 static bool
660 pr_pointer_type (void *p)
662 struct pr_handle *info = (struct pr_handle *) p;
663 char *s;
665 assert (info->stack != NULL);
667 s = strchr (info->stack->type, '|');
668 if (s != NULL && s[1] == '[')
669 return substitute_type (info, "(*|)");
670 return substitute_type (info, "*|");
673 /* Turn the top type on the stack into a function returning that type. */
675 static bool
676 pr_function_type (void *p, int argcount, bool varargs)
678 struct pr_handle *info = (struct pr_handle *) p;
679 char **arg_types;
680 unsigned int len;
681 char *s;
683 assert (info->stack != NULL);
685 len = 10;
687 if (argcount <= 0)
689 arg_types = NULL;
690 len += 15;
692 else
694 int i;
696 arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
697 for (i = argcount - 1; i >= 0; i--)
699 if (! substitute_type (info, ""))
701 free (arg_types);
702 return false;
704 arg_types[i] = pop_type (info);
705 if (arg_types[i] == NULL)
707 free (arg_types);
708 return false;
710 len += strlen (arg_types[i]) + 2;
712 if (varargs)
713 len += 5;
716 /* Now the return type is on the top of the stack. */
718 s = (char *) xmalloc (len);
719 strcpy (s, "(|) (");
721 if (argcount < 0)
722 strcat (s, "/* unknown */");
723 else
725 int i;
727 for (i = 0; i < argcount; i++)
729 if (i > 0)
730 strcat (s, ", ");
731 strcat (s, arg_types[i]);
733 if (varargs)
735 if (i > 0)
736 strcat (s, ", ");
737 strcat (s, "...");
739 if (argcount > 0)
740 free (arg_types);
743 strcat (s, ")");
745 bool ret = substitute_type (info, s);
746 free (s);
747 return ret;
750 /* Turn the top type on the stack into a reference to that type. */
752 static bool
753 pr_reference_type (void *p)
755 struct pr_handle *info = (struct pr_handle *) p;
757 assert (info->stack != NULL);
759 return substitute_type (info, "&|");
762 /* Make a range type. */
764 static bool
765 pr_range_type (void *p, bfd_signed_vma lower, bfd_signed_vma upper)
767 struct pr_handle *info = (struct pr_handle *) p;
768 char abl[22], abu[22];
770 assert (info->stack != NULL);
772 if (! substitute_type (info, ""))
773 return false;
775 print_vma (lower, abl, false, false);
776 print_vma (upper, abu, false, false);
778 return (prepend_type (info, "range (")
779 && append_type (info, "):")
780 && append_type (info, abl)
781 && append_type (info, ":")
782 && append_type (info, abu));
785 /* Make an array type. */
787 static bool
788 pr_array_type (void *p, bfd_signed_vma lower, bfd_signed_vma upper,
789 bool stringp)
791 struct pr_handle *info = (struct pr_handle *) p;
792 char *range_type;
793 char abl[22], abu[22], ab[50];
795 range_type = pop_type (info);
796 if (range_type == NULL)
797 return false;
799 if (lower == 0)
801 if (upper == -1)
802 sprintf (ab, "|[]");
803 else
805 print_vma (upper + 1, abu, false, false);
806 sprintf (ab, "|[%s]", abu);
809 else
811 print_vma (lower, abl, false, false);
812 print_vma (upper, abu, false, false);
813 sprintf (ab, "|[%s:%s]", abl, abu);
816 if (! substitute_type (info, ab))
817 return false;
819 if (strcmp (range_type, "int") != 0)
821 if (! append_type (info, ":")
822 || ! append_type (info, range_type))
823 return false;
826 if (stringp)
828 if (! append_type (info, " /* string */"))
829 return false;
832 return true;
835 /* Make a set type. */
837 static bool
838 pr_set_type (void *p, bool bitstringp)
840 struct pr_handle *info = (struct pr_handle *) p;
842 if (! substitute_type (info, ""))
843 return false;
845 if (! prepend_type (info, "set { ")
846 || ! append_type (info, " }"))
847 return false;
849 if (bitstringp)
851 if (! append_type (info, "/* bitstring */"))
852 return false;
855 return true;
858 /* Make an offset type. */
860 static bool
861 pr_offset_type (void *p)
863 struct pr_handle *info = (struct pr_handle *) p;
864 char *t;
866 if (! substitute_type (info, ""))
867 return false;
869 t = pop_type (info);
870 if (t == NULL)
871 return false;
873 return (substitute_type (info, "")
874 && prepend_type (info, " ")
875 && prepend_type (info, t)
876 && append_type (info, "::|"));
879 /* Make a method type. */
881 static bool
882 pr_method_type (void *p, bool domain, int argcount, bool varargs)
884 struct pr_handle *info = (struct pr_handle *) p;
885 unsigned int len;
886 char *domain_type;
887 char **arg_types;
888 char *s;
890 len = 10;
892 if (! domain)
893 domain_type = NULL;
894 else
896 if (! substitute_type (info, ""))
897 return false;
898 domain_type = pop_type (info);
899 if (domain_type == NULL)
900 return false;
901 if (startswith (domain_type, "class ")
902 && strchr (domain_type + sizeof "class " - 1, ' ') == NULL)
903 domain_type += sizeof "class " - 1;
904 else if (startswith (domain_type, "union class ")
905 && (strchr (domain_type + sizeof "union class " - 1, ' ')
906 == NULL))
907 domain_type += sizeof "union class " - 1;
908 len += strlen (domain_type);
911 if (argcount <= 0)
913 arg_types = NULL;
914 len += 15;
916 else
918 int i;
920 arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
921 for (i = argcount - 1; i >= 0; i--)
923 if (! substitute_type (info, ""))
925 free (arg_types);
926 return false;
928 arg_types[i] = pop_type (info);
929 if (arg_types[i] == NULL)
931 free (arg_types);
932 return false;
934 len += strlen (arg_types[i]) + 2;
936 if (varargs)
937 len += 5;
940 /* Now the return type is on the top of the stack. */
942 s = (char *) xmalloc (len);
943 if (! domain)
944 *s = '\0';
945 else
946 strcpy (s, domain_type);
947 strcat (s, "::| (");
949 if (argcount < 0)
950 strcat (s, "/* unknown */");
951 else
953 int i;
955 for (i = 0; i < argcount; i++)
957 if (i > 0)
958 strcat (s, ", ");
959 strcat (s, arg_types[i]);
961 if (varargs)
963 if (i > 0)
964 strcat (s, ", ");
965 strcat (s, "...");
967 if (argcount > 0)
968 free (arg_types);
971 strcat (s, ")");
973 if (! substitute_type (info, s))
974 return false;
976 free (s);
978 return true;
981 /* Make a const qualified type. */
983 static bool
984 pr_const_type (void *p)
986 struct pr_handle *info = (struct pr_handle *) p;
988 return substitute_type (info, "const |");
991 /* Make a volatile qualified type. */
993 static bool
994 pr_volatile_type (void *p)
996 struct pr_handle *info = (struct pr_handle *) p;
998 return substitute_type (info, "volatile |");
1001 /* Start accumulating a struct type. */
1003 static bool
1004 pr_start_struct_type (void *p, const char *tag, unsigned int id,
1005 bool structp, unsigned int size)
1007 struct pr_handle *info = (struct pr_handle *) p;
1009 info->indent += 2;
1011 if (! push_type (info, structp ? "struct " : "union "))
1012 return false;
1013 if (tag != NULL)
1015 if (! append_type (info, tag))
1016 return false;
1018 else
1020 char idbuf[20];
1022 sprintf (idbuf, "%%anon%u", id);
1023 if (! append_type (info, idbuf))
1024 return false;
1027 if (! append_type (info, " {"))
1028 return false;
1029 if (size != 0 || tag != NULL)
1031 char ab[30];
1033 if (! append_type (info, " /*"))
1034 return false;
1036 if (size != 0)
1038 sprintf (ab, " size %u", size);
1039 if (! append_type (info, ab))
1040 return false;
1042 if (tag != NULL)
1044 sprintf (ab, " id %u", id);
1045 if (! append_type (info, ab))
1046 return false;
1048 if (! append_type (info, " */"))
1049 return false;
1051 if (! append_type (info, "\n"))
1052 return false;
1054 info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
1056 return indent_type (info);
1059 /* Output the visibility of a field in a struct. */
1061 static bool
1062 pr_fix_visibility (struct pr_handle *info, enum debug_visibility visibility)
1064 const char *s = NULL;
1065 char *t;
1066 unsigned int len;
1068 assert (info->stack != NULL);
1070 if (info->stack->visibility == visibility)
1071 return true;
1073 switch (visibility)
1075 case DEBUG_VISIBILITY_PUBLIC:
1076 s = "public";
1077 break;
1078 case DEBUG_VISIBILITY_PRIVATE:
1079 s = "private";
1080 break;
1081 case DEBUG_VISIBILITY_PROTECTED:
1082 s = "protected";
1083 break;
1084 case DEBUG_VISIBILITY_IGNORE:
1085 s = "/* ignore */";
1086 break;
1087 default:
1088 abort ();
1089 return false;
1092 /* Trim off a trailing space in the struct string, to make the
1093 output look a bit better, then stick on the visibility string. */
1095 t = info->stack->type;
1096 len = strlen (t);
1097 assert (t[len - 1] == ' ');
1098 t[len - 1] = '\0';
1100 if (! append_type (info, s)
1101 || ! append_type (info, ":\n")
1102 || ! indent_type (info))
1103 return false;
1105 info->stack->visibility = visibility;
1107 return true;
1110 /* Add a field to a struct type. */
1112 static bool
1113 pr_struct_field (void *p, const char *name, bfd_vma bitpos, bfd_vma bitsize,
1114 enum debug_visibility visibility)
1116 struct pr_handle *info = (struct pr_handle *) p;
1117 char ab[22];
1118 char *t;
1120 if (! substitute_type (info, name))
1121 return false;
1123 if (! append_type (info, "; /* "))
1124 return false;
1126 if (bitsize != 0)
1128 print_vma (bitsize, ab, true, false);
1129 if (! append_type (info, "bitsize ")
1130 || ! append_type (info, ab)
1131 || ! append_type (info, ", "))
1132 return false;
1135 print_vma (bitpos, ab, true, false);
1136 if (! append_type (info, "bitpos ")
1137 || ! append_type (info, ab)
1138 || ! append_type (info, " */\n")
1139 || ! indent_type (info))
1140 return false;
1142 t = pop_type (info);
1143 if (t == NULL)
1144 return false;
1146 if (! pr_fix_visibility (info, visibility))
1147 return false;
1149 return append_type (info, t);
1152 /* Finish a struct type. */
1154 static bool
1155 pr_end_struct_type (void *p)
1157 struct pr_handle *info = (struct pr_handle *) p;
1158 char *s;
1160 assert (info->stack != NULL);
1161 assert (info->indent >= 2);
1163 info->indent -= 2;
1165 /* Change the trailing indentation to have a close brace. */
1166 s = info->stack->type + strlen (info->stack->type) - 2;
1167 assert (s[0] == ' ' && s[1] == ' ' && s[2] == '\0');
1169 *s++ = '}';
1170 *s = '\0';
1172 return true;
1175 /* Start a class type. */
1177 static bool
1178 pr_start_class_type (void *p, const char *tag, unsigned int id,
1179 bool structp, unsigned int size,
1180 bool vptr, bool ownvptr)
1182 struct pr_handle *info = (struct pr_handle *) p;
1183 char *tv = NULL;
1185 info->indent += 2;
1187 if (vptr && ! ownvptr)
1189 tv = pop_type (info);
1190 if (tv == NULL)
1191 return false;
1194 if (! push_type (info, structp ? "class " : "union class "))
1195 return false;
1196 if (tag != NULL)
1198 if (! append_type (info, tag))
1199 return false;
1201 else
1203 char idbuf[20];
1205 sprintf (idbuf, "%%anon%u", id);
1206 if (! append_type (info, idbuf))
1207 return false;
1210 if (! append_type (info, " {"))
1211 return false;
1212 if (size != 0 || vptr || ownvptr || tag != NULL)
1214 if (! append_type (info, " /*"))
1215 return false;
1217 if (size != 0)
1219 char ab[20];
1221 sprintf (ab, "%u", size);
1222 if (! append_type (info, " size ")
1223 || ! append_type (info, ab))
1224 return false;
1227 if (vptr)
1229 if (! append_type (info, " vtable "))
1230 return false;
1231 if (ownvptr)
1233 if (! append_type (info, "self "))
1234 return false;
1236 else
1238 if (! append_type (info, tv)
1239 || ! append_type (info, " "))
1240 return false;
1244 if (tag != NULL)
1246 char ab[30];
1248 sprintf (ab, " id %u", id);
1249 if (! append_type (info, ab))
1250 return false;
1253 if (! append_type (info, " */"))
1254 return false;
1257 info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
1259 return (append_type (info, "\n")
1260 && indent_type (info));
1263 /* Add a static member to a class. */
1265 static bool
1266 pr_class_static_member (void *p, const char *name, const char *physname,
1267 enum debug_visibility visibility)
1269 struct pr_handle *info = (struct pr_handle *) p;
1270 char *t;
1272 if (! substitute_type (info, name))
1273 return false;
1275 if (! prepend_type (info, "static ")
1276 || ! append_type (info, "; /* ")
1277 || ! append_type (info, physname)
1278 || ! append_type (info, " */\n")
1279 || ! indent_type (info))
1280 return false;
1282 t = pop_type (info);
1283 if (t == NULL)
1284 return false;
1286 if (! pr_fix_visibility (info, visibility))
1287 return false;
1289 return append_type (info, t);
1292 /* Add a base class to a class. */
1294 static bool
1295 pr_class_baseclass (void *p, bfd_vma bitpos, bool is_virtual,
1296 enum debug_visibility visibility)
1298 struct pr_handle *info = (struct pr_handle *) p;
1299 char *t;
1300 const char *prefix;
1301 char ab[22];
1302 char *s, *l, *n;
1304 assert (info->stack != NULL && info->stack->next != NULL);
1306 if (! substitute_type (info, ""))
1307 return false;
1309 t = pop_type (info);
1310 if (t == NULL)
1311 return false;
1313 if (startswith (t, "class "))
1314 t += sizeof "class " - 1;
1316 /* Push it back on to take advantage of the prepend_type and
1317 append_type routines. */
1318 if (! push_type (info, t))
1319 return false;
1321 if (is_virtual)
1323 if (! prepend_type (info, "virtual "))
1324 return false;
1327 switch (visibility)
1329 case DEBUG_VISIBILITY_PUBLIC:
1330 prefix = "public ";
1331 break;
1332 case DEBUG_VISIBILITY_PROTECTED:
1333 prefix = "protected ";
1334 break;
1335 case DEBUG_VISIBILITY_PRIVATE:
1336 prefix = "private ";
1337 break;
1338 default:
1339 prefix = "/* unknown visibility */ ";
1340 break;
1343 if (! prepend_type (info, prefix))
1344 return false;
1346 if (bitpos != 0)
1348 print_vma (bitpos, ab, true, false);
1349 if (! append_type (info, " /* bitpos ")
1350 || ! append_type (info, ab)
1351 || ! append_type (info, " */"))
1352 return false;
1355 /* Now the top of the stack is something like "public A / * bitpos
1356 10 * /". The next element on the stack is something like "class
1357 xx { / * size 8 * /\n...". We want to substitute the top of the
1358 stack in before the {. */
1359 s = strchr (info->stack->next->type, '{');
1360 assert (s != NULL);
1361 --s;
1363 /* If there is already a ':', then we already have a baseclass, and
1364 we must append this one after a comma. */
1365 for (l = info->stack->next->type; l != s; l++)
1366 if (*l == ':')
1367 break;
1368 if (! prepend_type (info, l == s ? " : " : ", "))
1369 return false;
1371 t = pop_type (info);
1372 if (t == NULL)
1373 return false;
1375 n = (char *) xmalloc (strlen (info->stack->type) + strlen (t) + 1);
1376 memcpy (n, info->stack->type, s - info->stack->type);
1377 strcpy (n + (s - info->stack->type), t);
1378 strcat (n, s);
1380 free (info->stack->type);
1381 info->stack->type = n;
1383 free (t);
1385 return true;
1388 /* Start adding a method to a class. */
1390 static bool
1391 pr_class_start_method (void *p, const char *name)
1393 struct pr_handle *info = (struct pr_handle *) p;
1395 assert (info->stack != NULL);
1396 info->stack->method = name;
1397 return true;
1400 /* Add a variant to a method. */
1402 static bool
1403 pr_class_method_variant (void *p, const char *physname,
1404 enum debug_visibility visibility,
1405 bool constp, bool volatilep,
1406 bfd_vma voffset, bool context)
1408 struct pr_handle *info = (struct pr_handle *) p;
1409 char *method_type;
1410 char *context_type;
1412 assert (info->stack != NULL);
1413 assert (info->stack->next != NULL);
1415 /* Put the const and volatile qualifiers on the type. */
1416 if (volatilep)
1418 if (! append_type (info, " volatile"))
1419 return false;
1421 if (constp)
1423 if (! append_type (info, " const"))
1424 return false;
1427 /* Stick the name of the method into its type. */
1428 if (! substitute_type (info,
1429 (context
1430 ? info->stack->next->next->method
1431 : info->stack->next->method)))
1432 return false;
1434 /* Get the type. */
1435 method_type = pop_type (info);
1436 if (method_type == NULL)
1437 return false;
1439 /* Pull off the context type if there is one. */
1440 if (! context)
1441 context_type = NULL;
1442 else
1444 context_type = pop_type (info);
1445 if (context_type == NULL)
1446 return false;
1449 /* Now the top of the stack is the class. */
1451 if (! pr_fix_visibility (info, visibility))
1452 return false;
1454 if (! append_type (info, method_type)
1455 || ! append_type (info, " /* ")
1456 || ! append_type (info, physname)
1457 || ! append_type (info, " "))
1458 return false;
1459 if (context || voffset != 0)
1461 char ab[22];
1463 if (context)
1465 if (! append_type (info, "context ")
1466 || ! append_type (info, context_type)
1467 || ! append_type (info, " "))
1468 return false;
1470 print_vma (voffset, ab, true, false);
1471 if (! append_type (info, "voffset ")
1472 || ! append_type (info, ab))
1473 return false;
1476 return (append_type (info, " */;\n")
1477 && indent_type (info));
1480 /* Add a static variant to a method. */
1482 static bool
1483 pr_class_static_method_variant (void *p, const char *physname,
1484 enum debug_visibility visibility,
1485 bool constp, bool volatilep)
1487 struct pr_handle *info = (struct pr_handle *) p;
1488 char *method_type;
1490 assert (info->stack != NULL);
1491 assert (info->stack->next != NULL);
1492 assert (info->stack->next->method != NULL);
1494 /* Put the const and volatile qualifiers on the type. */
1495 if (volatilep)
1497 if (! append_type (info, " volatile"))
1498 return false;
1500 if (constp)
1502 if (! append_type (info, " const"))
1503 return false;
1506 /* Mark it as static. */
1507 if (! prepend_type (info, "static "))
1508 return false;
1510 /* Stick the name of the method into its type. */
1511 if (! substitute_type (info, info->stack->next->method))
1512 return false;
1514 /* Get the type. */
1515 method_type = pop_type (info);
1516 if (method_type == NULL)
1517 return false;
1519 /* Now the top of the stack is the class. */
1521 if (! pr_fix_visibility (info, visibility))
1522 return false;
1524 return (append_type (info, method_type)
1525 && append_type (info, " /* ")
1526 && append_type (info, physname)
1527 && append_type (info, " */;\n")
1528 && indent_type (info));
1531 /* Finish up a method. */
1533 static bool
1534 pr_class_end_method (void *p)
1536 struct pr_handle *info = (struct pr_handle *) p;
1538 info->stack->method = NULL;
1539 return true;
1542 /* Finish up a class. */
1544 static bool
1545 pr_end_class_type (void *p)
1547 return pr_end_struct_type (p);
1550 /* Push a type on the stack using a typedef name. */
1552 static bool
1553 pr_typedef_type (void *p, const char *name)
1555 struct pr_handle *info = (struct pr_handle *) p;
1557 return push_type (info, name);
1560 /* Push a type on the stack using a tag name. */
1562 static bool
1563 pr_tag_type (void *p, const char *name, unsigned int id,
1564 enum debug_type_kind kind)
1566 struct pr_handle *info = (struct pr_handle *) p;
1567 const char *t, *tag;
1568 char idbuf[22];
1570 switch (kind)
1572 case DEBUG_KIND_STRUCT:
1573 t = "struct ";
1574 break;
1575 case DEBUG_KIND_UNION:
1576 t = "union ";
1577 break;
1578 case DEBUG_KIND_ENUM:
1579 t = "enum ";
1580 break;
1581 case DEBUG_KIND_CLASS:
1582 t = "class ";
1583 break;
1584 case DEBUG_KIND_UNION_CLASS:
1585 t = "union class ";
1586 break;
1587 default:
1588 /* PR 25625: Corrupt input can trigger this case. */
1589 return false;
1592 if (! push_type (info, t))
1593 return false;
1594 if (name != NULL)
1595 tag = name;
1596 else
1598 sprintf (idbuf, "%%anon%u", id);
1599 tag = idbuf;
1602 if (! append_type (info, tag))
1603 return false;
1604 if (name != NULL && kind != DEBUG_KIND_ENUM)
1606 sprintf (idbuf, " /* id %u */", id);
1607 if (! append_type (info, idbuf))
1608 return false;
1611 return true;
1614 /* Output a typedef. */
1616 static bool
1617 pr_typdef (void *p, const char *name)
1619 struct pr_handle *info = (struct pr_handle *) p;
1620 char *s;
1622 if (! substitute_type (info, name))
1623 return false;
1625 s = pop_type (info);
1626 if (s == NULL)
1627 return false;
1629 indent (info);
1630 fprintf (info->f, "typedef %s;\n", s);
1632 free (s);
1634 return true;
1637 /* Output a tag. The tag should already be in the string on the
1638 stack, so all we have to do here is print it out. */
1640 static bool
1641 pr_tag (void *p, const char *name ATTRIBUTE_UNUSED)
1643 struct pr_handle *info = (struct pr_handle *) p;
1644 char *t;
1646 t = pop_type (info);
1647 if (t == NULL)
1648 return false;
1650 indent (info);
1651 fprintf (info->f, "%s;\n", t);
1653 free (t);
1655 return true;
1658 /* Output an integer constant. */
1660 static bool
1661 pr_int_constant (void *p, const char *name, bfd_vma val)
1663 struct pr_handle *info = (struct pr_handle *) p;
1664 char ab[22];
1666 indent (info);
1667 print_vma (val, ab, false, false);
1668 fprintf (info->f, "const int %s = %s;\n", name, ab);
1669 return true;
1672 /* Output a floating point constant. */
1674 static bool
1675 pr_float_constant (void *p, const char *name, double val)
1677 struct pr_handle *info = (struct pr_handle *) p;
1679 indent (info);
1680 fprintf (info->f, "const double %s = %g;\n", name, val);
1681 return true;
1684 /* Output a typed constant. */
1686 static bool
1687 pr_typed_constant (void *p, const char *name, bfd_vma val)
1689 struct pr_handle *info = (struct pr_handle *) p;
1690 char *t;
1691 char ab[22];
1693 t = pop_type (info);
1694 if (t == NULL)
1695 return false;
1697 indent (info);
1698 print_vma (val, ab, false, false);
1699 fprintf (info->f, "const %s %s = %s;\n", t, name, ab);
1701 free (t);
1703 return true;
1706 /* Output a variable. */
1708 static bool
1709 pr_variable (void *p, const char *name, enum debug_var_kind kind,
1710 bfd_vma val)
1712 struct pr_handle *info = (struct pr_handle *) p;
1713 char *t;
1714 char ab[22];
1716 if (! substitute_type (info, name))
1717 return false;
1719 t = pop_type (info);
1720 if (t == NULL)
1721 return false;
1723 indent (info);
1724 switch (kind)
1726 case DEBUG_STATIC:
1727 case DEBUG_LOCAL_STATIC:
1728 fprintf (info->f, "static ");
1729 break;
1730 case DEBUG_REGISTER:
1731 fprintf (info->f, "register ");
1732 break;
1733 default:
1734 break;
1736 print_vma (val, ab, true, true);
1737 fprintf (info->f, "%s /* %s */;\n", t, ab);
1739 free (t);
1741 return true;
1744 /* Start outputting a function. */
1746 static bool
1747 pr_start_function (void *p, const char *name, bool global)
1749 struct pr_handle *info = (struct pr_handle *) p;
1750 char *t;
1752 if (! substitute_type (info, name))
1753 return false;
1755 t = pop_type (info);
1756 if (t == NULL)
1757 return false;
1759 indent (info);
1760 if (! global)
1761 fprintf (info->f, "static ");
1762 fprintf (info->f, "%s (", t);
1764 info->parameter = 1;
1766 return true;
1769 /* Output a function parameter. */
1771 static bool
1772 pr_function_parameter (void *p, const char *name,
1773 enum debug_parm_kind kind, bfd_vma val)
1775 struct pr_handle *info = (struct pr_handle *) p;
1776 char *t;
1777 char ab[22];
1779 if (kind == DEBUG_PARM_REFERENCE
1780 || kind == DEBUG_PARM_REF_REG)
1782 if (! pr_reference_type (p))
1783 return false;
1786 if (! substitute_type (info, name))
1787 return false;
1789 t = pop_type (info);
1790 if (t == NULL)
1791 return false;
1793 if (info->parameter != 1)
1794 fprintf (info->f, ", ");
1796 if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
1797 fprintf (info->f, "register ");
1799 print_vma (val, ab, true, true);
1800 fprintf (info->f, "%s /* %s */", t, ab);
1802 free (t);
1804 ++info->parameter;
1806 return true;
1809 /* Start writing out a block. */
1811 static bool
1812 pr_start_block (void *p, bfd_vma addr)
1814 struct pr_handle *info = (struct pr_handle *) p;
1815 char ab[22];
1817 if (info->parameter > 0)
1819 fprintf (info->f, ")\n");
1820 info->parameter = 0;
1823 indent (info);
1824 print_vma (addr, ab, true, true);
1825 fprintf (info->f, "{ /* %s */\n", ab);
1827 info->indent += 2;
1829 return true;
1832 /* Write out line number information. */
1834 static bool
1835 pr_lineno (void *p, const char *filename, unsigned long lineno, bfd_vma addr)
1837 struct pr_handle *info = (struct pr_handle *) p;
1838 char ab[22];
1840 indent (info);
1841 print_vma (addr, ab, true, true);
1842 fprintf (info->f, "/* file %s line %lu addr %s */\n", filename, lineno, ab);
1844 return true;
1847 /* Finish writing out a block. */
1849 static bool
1850 pr_end_block (void *p, bfd_vma addr)
1852 struct pr_handle *info = (struct pr_handle *) p;
1853 char ab[22];
1855 info->indent -= 2;
1857 indent (info);
1858 print_vma (addr, ab, true, true);
1859 fprintf (info->f, "} /* %s */\n", ab);
1861 return true;
1864 /* Finish writing out a function. */
1866 static bool
1867 pr_end_function (void *p ATTRIBUTE_UNUSED)
1869 return true;
1872 /* Tags style generation functions start here. */
1874 /* Variables for address to line translation. */
1875 static bfd_vma pc;
1876 static const char *filename;
1877 static const char *functionname;
1878 static unsigned int line;
1879 static bool found;
1881 /* Look for an address in a section. This is called via
1882 bfd_map_over_sections. */
1884 static void
1885 find_address_in_section (bfd *abfd, asection *section, void *data)
1887 bfd_vma vma;
1888 bfd_size_type size;
1889 asymbol **syms = (asymbol **) data;
1891 if (found)
1892 return;
1894 if ((bfd_section_flags (section) & SEC_ALLOC) == 0)
1895 return;
1897 vma = bfd_section_vma (section);
1898 if (pc < vma)
1899 return;
1901 size = bfd_section_size (section);
1902 if (pc >= vma + size)
1903 return;
1905 found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
1906 &filename, &functionname, &line);
1909 static void
1910 translate_addresses (bfd *abfd, char *addr_hex, FILE *f, asymbol **syms)
1912 pc = bfd_scan_vma (addr_hex, NULL, 16);
1913 found = false;
1914 bfd_map_over_sections (abfd, find_address_in_section, syms);
1916 if (! found)
1917 fprintf (f, "??");
1918 else
1919 fprintf (f, "%u", line);
1922 /* Start a new compilation unit. */
1924 static bool
1925 tg_start_compilation_unit (void * p, const char *fname ATTRIBUTE_UNUSED)
1927 struct pr_handle *info = (struct pr_handle *) p;
1929 free (info->filename);
1930 /* Should it be relative? best way to do it here?. */
1931 info->filename = strdup (fname);
1933 return true;
1936 /* Start a source file within a compilation unit. */
1938 static bool
1939 tg_start_source (void *p, const char *fname)
1941 struct pr_handle *info = (struct pr_handle *) p;
1943 free (info->filename);
1944 /* Should it be relative? best way to do it here?. */
1945 info->filename = strdup (fname);
1947 return true;
1950 /* Push an enum type onto the type stack. */
1952 static bool
1953 tg_enum_type (void *p, const char *tag, const char **names,
1954 bfd_signed_vma *values)
1956 struct pr_handle *info = (struct pr_handle *) p;
1957 unsigned int i;
1958 const char *name;
1959 char ab[22];
1961 if (! pr_enum_type (p, tag, names, values))
1962 return false;
1964 name = tag ? tag : "unknown";
1965 /* Generate an entry for the enum. */
1966 if (tag)
1967 fprintf (info->f, "%s\t%s\t0;\"\tkind:e\ttype:%s\n", tag,
1968 info->filename, info->stack->type);
1970 /* Generate entries for the values. */
1971 if (names != NULL)
1973 for (i = 0; names[i] != NULL; i++)
1975 print_vma (values[i], ab, false, false);
1976 fprintf (info->f, "%s\t%s\t0;\"\tkind:g\tenum:%s\tvalue:%s\n",
1977 names[i], info->filename, name, ab);
1981 return true;
1984 /* Start accumulating a struct type. */
1986 static bool
1987 tg_start_struct_type (void *p, const char *tag, unsigned int id,
1988 bool structp,
1989 unsigned int size ATTRIBUTE_UNUSED)
1991 struct pr_handle *info = (struct pr_handle *) p;
1992 const char *name;
1993 char idbuf[20];
1995 if (tag != NULL)
1996 name = tag;
1997 else
1999 name = idbuf;
2000 sprintf (idbuf, "%%anon%u", id);
2003 if (! push_type (info, name))
2004 return false;
2006 info->stack->flavor = structp ? "struct" : "union";
2008 fprintf (info->f, "%s\t%s\t0;\"\tkind:%c\n", name, info->filename,
2009 info->stack->flavor[0]);
2011 info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
2013 return indent_type (info);
2016 /* Output the visibility of a field in a struct. */
2018 static bool
2019 tg_fix_visibility (struct pr_handle *info, enum debug_visibility visibility)
2021 assert (info->stack != NULL);
2023 if (info->stack->visibility == visibility)
2024 return true;
2026 assert (info->stack->visibility != DEBUG_VISIBILITY_IGNORE);
2028 info->stack->visibility = visibility;
2030 return true;
2033 /* Add a field to a struct type. */
2035 static bool
2036 tg_struct_field (void *p, const char *name, bfd_vma bitpos ATTRIBUTE_UNUSED,
2037 bfd_vma bitsize ATTRIBUTE_UNUSED,
2038 enum debug_visibility visibility)
2040 struct pr_handle *info = (struct pr_handle *) p;
2041 char *t;
2043 t = pop_type (info);
2044 if (t == NULL)
2045 return false;
2047 if (! tg_fix_visibility (info, visibility))
2048 return false;
2050 /* It happens, a bug? */
2051 if (! name[0])
2052 return true;
2054 fprintf (info->f, "%s\t%s\t0;\"\tkind:m\ttype:%s\t%s:%s\taccess:%s\n",
2055 name, info->filename, t, info->stack->flavor, info->stack->type,
2056 visibility_name (visibility));
2058 return true;
2061 /* Finish a struct type. */
2063 static bool
2064 tg_end_struct_type (void *p ATTRIBUTE_UNUSED)
2066 assert (((struct pr_handle *) p)->stack != NULL);
2068 return true;
2071 /* Start a class type. */
2073 static bool
2074 tg_start_class_type (void *p, const char *tag, unsigned int id,
2075 bool structp, unsigned int size,
2076 bool vptr, bool ownvptr)
2078 struct pr_handle *info = (struct pr_handle *) p;
2079 char *tv = NULL;
2080 const char *name;
2081 char idbuf[20];
2083 info->indent += 2;
2085 if (vptr && ! ownvptr)
2087 tv = pop_type (info);
2088 if (tv == NULL)
2089 return false;
2092 if (tag != NULL)
2093 name = tag;
2094 else
2096 sprintf (idbuf, "%%anon%u", id);
2097 name = idbuf;
2100 if (! push_type (info, name))
2101 return false;
2103 info->stack->flavor = structp ? "class" : "union class";
2104 info->stack->parents = NULL;
2105 info->stack->num_parents = 0;
2107 if (size != 0 || vptr || ownvptr || tag != NULL)
2109 if (vptr)
2111 if (! append_type (info, " vtable "))
2112 return false;
2113 if (ownvptr)
2115 if (! append_type (info, "self "))
2116 return false;
2118 else
2120 if (! append_type (info, tv)
2121 || ! append_type (info, " "))
2122 return false;
2127 info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
2129 return true;
2132 /* Add a static member to a class. */
2134 static bool
2135 tg_class_static_member (void *p, const char *name,
2136 const char *physname ATTRIBUTE_UNUSED,
2137 enum debug_visibility visibility)
2139 struct pr_handle *info = (struct pr_handle *) p;
2140 char *t;
2141 int len_var, len_class;
2142 char *full_name;
2144 len_var = strlen (name);
2145 len_class = strlen (info->stack->next->type);
2146 full_name = (char *) xmalloc (len_var + len_class + 3);
2147 if (! full_name)
2148 return false;
2149 sprintf (full_name, "%s::%s", info->stack->next->type, name);
2151 if (! substitute_type (info, full_name))
2153 free (full_name);
2154 return false;
2157 if (! prepend_type (info, "static "))
2159 free (full_name);
2160 return false;
2163 t = pop_type (info);
2164 if (t == NULL)
2166 free (full_name);
2167 return false;
2170 if (! tg_fix_visibility (info, visibility))
2172 free (t);
2173 free (full_name);
2174 return false;
2177 fprintf (info->f, "%s\t%s\t0;\"\tkind:x\ttype:%s\tclass:%s\taccess:%s\n",
2178 name, info->filename, t, info->stack->type,
2179 visibility_name (visibility));
2180 free (t);
2181 free (full_name);
2183 return true;
2186 /* Add a base class to a class. */
2188 static bool
2189 tg_class_baseclass (void *p, bfd_vma bitpos ATTRIBUTE_UNUSED,
2190 bool is_virtual, enum debug_visibility visibility)
2192 struct pr_handle *info = (struct pr_handle *) p;
2193 char *t;
2194 const char *prefix;
2196 assert (info->stack != NULL && info->stack->next != NULL);
2198 t = pop_type (info);
2199 if (t == NULL)
2200 return false;
2202 if (startswith (t, "class "))
2203 t += sizeof "class " - 1;
2205 /* Push it back on to take advantage of the prepend_type and
2206 append_type routines. */
2207 if (! push_type (info, t))
2208 return false;
2210 if (is_virtual)
2212 if (! prepend_type (info, "virtual "))
2213 return false;
2216 switch (visibility)
2218 case DEBUG_VISIBILITY_PUBLIC:
2219 prefix = "public ";
2220 break;
2221 case DEBUG_VISIBILITY_PROTECTED:
2222 prefix = "protected ";
2223 break;
2224 case DEBUG_VISIBILITY_PRIVATE:
2225 prefix = "private ";
2226 break;
2227 default:
2228 prefix = "/* unknown visibility */ ";
2229 break;
2232 if (! prepend_type (info, prefix))
2233 return false;
2235 t = pop_type (info);
2236 if (t == NULL)
2237 return false;
2239 if (info->stack->num_parents && ! append_parent (info, ", "))
2240 return false;
2242 if (! append_parent (info, t))
2243 return false;
2244 info->stack->num_parents++;
2246 free (t);
2248 return true;
2251 /* Add a variant to a method. */
2253 static bool
2254 tg_class_method_variant (void *p, const char *physname ATTRIBUTE_UNUSED,
2255 enum debug_visibility visibility,
2256 bool constp, bool volatilep,
2257 bfd_vma voffset ATTRIBUTE_UNUSED,
2258 bool context)
2260 struct pr_handle *info = (struct pr_handle *) p;
2261 char *method_type;
2262 char *context_type;
2263 char *method_name;
2265 assert (info->stack != NULL);
2266 assert (info->stack->next != NULL);
2268 /* Put the const and volatile qualifiers on the type. */
2269 if (volatilep)
2271 if (! append_type (info, " volatile"))
2272 return false;
2274 if (constp)
2276 if (! append_type (info, " const"))
2277 return false;
2280 method_name = strdup (context ? info->stack->next->next->method
2281 : info->stack->next->method);
2283 /* Stick the name of the method into its type. */
2284 if (! substitute_type (info, method_name))
2286 free (method_name);
2287 return false;
2290 /* Get the type. */
2291 method_type = pop_type (info);
2292 if (method_type == NULL)
2294 free (method_name);
2295 return false;
2298 /* Pull off the context type if there is one. */
2299 if (! context)
2300 context_type = NULL;
2301 else
2303 context_type = pop_type (info);
2304 if (context_type == NULL)
2306 free (method_type);
2307 free (method_name);
2308 return false;
2312 /* Now the top of the stack is the class. */
2313 if (! tg_fix_visibility (info, visibility))
2315 free (method_type);
2316 free (method_name);
2317 free (context_type);
2318 return false;
2321 fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\n",
2322 method_name, info->filename, method_type, info->stack->type);
2323 free (method_type);
2324 free (method_name);
2325 free (context_type);
2327 return true;
2330 /* Add a static variant to a method. */
2332 static bool
2333 tg_class_static_method_variant (void *p,
2334 const char *physname ATTRIBUTE_UNUSED,
2335 enum debug_visibility visibility,
2336 bool constp, bool volatilep)
2338 struct pr_handle *info = (struct pr_handle *) p;
2339 char *method_type;
2340 char *method_name;
2342 assert (info->stack != NULL);
2343 assert (info->stack->next != NULL);
2344 assert (info->stack->next->method != NULL);
2346 /* Put the const and volatile qualifiers on the type. */
2347 if (volatilep)
2349 if (! append_type (info, " volatile"))
2350 return false;
2352 if (constp)
2354 if (! append_type (info, " const"))
2355 return false;
2358 /* Mark it as static. */
2359 if (! prepend_type (info, "static "))
2360 return false;
2362 method_name = strdup (info->stack->next->method);
2363 /* Stick the name of the method into its type. */
2364 if (! substitute_type (info, info->stack->next->method))
2366 free (method_name);
2367 return false;
2370 /* Get the type. */
2371 method_type = pop_type (info);
2372 if (method_type == NULL)
2374 free (method_name);
2375 return false;
2378 /* Now the top of the stack is the class. */
2379 if (! tg_fix_visibility (info, visibility))
2381 free (method_type);
2382 free (method_name);
2383 return false;
2386 fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\taccess:%s\n",
2387 method_name, info->filename, method_type, info->stack->type,
2388 visibility_name (visibility));
2389 free (method_type);
2390 free (method_name);
2392 return true;
2395 /* Finish up a class. */
2397 static bool
2398 tg_end_class_type (void *p)
2400 struct pr_handle *info = (struct pr_handle *) p;
2402 fprintf (info->f, "%s\t%s\t0;\"\tkind:c\ttype:%s", info->stack->type,
2403 info->filename, info->stack->flavor);
2404 if (info->stack->num_parents)
2406 fprintf (info->f, "\tinherits:%s", info->stack->parents);
2407 free (info->stack->parents);
2409 fputc ('\n', info->f);
2411 return tg_end_struct_type (p);
2414 /* Push a type on the stack using a tag name. */
2416 static bool
2417 tg_tag_type (void *p, const char *name, unsigned int id,
2418 enum debug_type_kind kind)
2420 struct pr_handle *info = (struct pr_handle *) p;
2421 const char *t, *tag;
2422 char idbuf[20];
2424 switch (kind)
2426 case DEBUG_KIND_STRUCT:
2427 t = "struct ";
2428 break;
2429 case DEBUG_KIND_UNION:
2430 t = "union ";
2431 break;
2432 case DEBUG_KIND_ENUM:
2433 t = "enum ";
2434 break;
2435 case DEBUG_KIND_CLASS:
2436 t = "class ";
2437 break;
2438 case DEBUG_KIND_UNION_CLASS:
2439 t = "union class ";
2440 break;
2441 default:
2442 abort ();
2443 return false;
2446 if (! push_type (info, t))
2447 return false;
2448 if (name != NULL)
2449 tag = name;
2450 else
2452 sprintf (idbuf, "%%anon%u", id);
2453 tag = idbuf;
2456 if (! append_type (info, tag))
2457 return false;
2459 return true;
2462 /* Output a typedef. */
2464 static bool
2465 tg_typdef (void *p, const char *name)
2467 struct pr_handle *info = (struct pr_handle *) p;
2468 char *s;
2470 s = pop_type (info);
2471 if (s == NULL)
2472 return false;
2474 fprintf (info->f, "%s\t%s\t0;\"\tkind:t\ttype:%s\n", name,
2475 info->filename, s);
2477 free (s);
2479 return true;
2482 /* Output a tag. The tag should already be in the string on the
2483 stack, so all we have to do here is print it out. */
2485 static bool
2486 tg_tag (void *p ATTRIBUTE_UNUSED, const char *name ATTRIBUTE_UNUSED)
2488 struct pr_handle *info = (struct pr_handle *) p;
2489 char *t;
2491 t = pop_type (info);
2492 if (t == NULL)
2493 return false;
2494 free (t);
2496 return true;
2499 /* Output an integer constant. */
2501 static bool
2502 tg_int_constant (void *p, const char *name, bfd_vma val)
2504 struct pr_handle *info = (struct pr_handle *) p;
2505 char ab[22];
2507 indent (info);
2508 print_vma (val, ab, false, false);
2509 fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const int\tvalue:%s\n",
2510 name, info->filename, ab);
2511 return true;
2514 /* Output a floating point constant. */
2516 static bool
2517 tg_float_constant (void *p, const char *name, double val)
2519 struct pr_handle *info = (struct pr_handle *) p;
2521 indent (info);
2522 fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const double\tvalue:%g\n",
2523 name, info->filename, val);
2524 return true;
2527 /* Output a typed constant. */
2529 static bool
2530 tg_typed_constant (void *p, const char *name, bfd_vma val)
2532 struct pr_handle *info = (struct pr_handle *) p;
2533 char *t;
2534 char ab[22];
2536 t = pop_type (info);
2537 if (t == NULL)
2538 return false;
2540 indent (info);
2541 print_vma (val, ab, false, false);
2542 fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const %s\tvalue:%s\n",
2543 name, info->filename, t, ab);
2545 free (t);
2547 return true;
2550 /* Output a variable. */
2552 static bool
2553 tg_variable (void *p, const char *name, enum debug_var_kind kind,
2554 bfd_vma val ATTRIBUTE_UNUSED)
2556 struct pr_handle *info = (struct pr_handle *) p;
2557 char *t, *dname, *from_class;
2559 t = pop_type (info);
2560 if (t == NULL)
2561 return false;
2563 dname = NULL;
2564 if (info->demangler)
2565 dname = info->demangler (info->abfd, name, demangle_flags);
2567 from_class = NULL;
2568 if (dname != NULL)
2570 char *sep;
2571 sep = strstr (dname, "::");
2572 if (sep)
2574 *sep = 0;
2575 name = sep + 2;
2576 from_class = dname;
2578 else
2579 /* Obscure types as vts and type_info nodes. */
2580 name = dname;
2583 fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:%s", name, info->filename, t);
2585 switch (kind)
2587 case DEBUG_STATIC:
2588 case DEBUG_LOCAL_STATIC:
2589 fprintf (info->f, "\tfile:");
2590 break;
2591 case DEBUG_REGISTER:
2592 fprintf (info->f, "\tregister:");
2593 break;
2594 default:
2595 break;
2598 if (from_class)
2599 fprintf (info->f, "\tclass:%s", from_class);
2601 if (dname)
2602 free (dname);
2604 fprintf (info->f, "\n");
2606 free (t);
2608 return true;
2611 /* Start outputting a function. */
2613 static bool
2614 tg_start_function (void *p, const char *name, bool global)
2616 struct pr_handle *info = (struct pr_handle *) p;
2617 char *dname;
2619 if (! global)
2620 info->stack->flavor = "static";
2621 else
2622 info->stack->flavor = NULL;
2624 dname = NULL;
2625 if (info->demangler)
2626 dname = info->demangler (info->abfd, name, demangle_flags);
2628 if (! substitute_type (info, dname ? dname : name))
2629 return false;
2631 info->stack->method = NULL;
2632 if (dname != NULL)
2634 char *sep;
2635 sep = strstr (dname, "::");
2636 if (sep)
2638 info->stack->method = dname;
2639 *sep = 0;
2640 name = sep + 2;
2642 else
2644 info->stack->method = "";
2645 name = dname;
2647 sep = strchr (name, '(');
2648 if (sep)
2649 *sep = 0;
2650 /* Obscure functions as type_info function. */
2653 info->stack->parents = strdup (name);
2655 if (! info->stack->method && ! append_type (info, "("))
2656 return false;
2658 info->parameter = 1;
2660 return true;
2663 /* Output a function parameter. */
2665 static bool
2666 tg_function_parameter (void *p, const char *name, enum debug_parm_kind kind,
2667 bfd_vma val ATTRIBUTE_UNUSED)
2669 struct pr_handle *info = (struct pr_handle *) p;
2670 char *t;
2672 if (kind == DEBUG_PARM_REFERENCE
2673 || kind == DEBUG_PARM_REF_REG)
2675 if (! pr_reference_type (p))
2676 return false;
2679 if (! substitute_type (info, name))
2680 return false;
2682 t = pop_type (info);
2683 if (t == NULL)
2684 return false;
2686 if (! info->stack->method)
2688 if (info->parameter != 1 && ! append_type (info, ", "))
2689 return false;
2691 if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
2692 if (! append_type (info, "register "))
2693 return false;
2695 if (! append_type (info, t))
2696 return false;
2699 free (t);
2701 ++info->parameter;
2703 return true;
2706 /* Start writing out a block. */
2708 static bool
2709 tg_start_block (void *p, bfd_vma addr)
2711 struct pr_handle *info = (struct pr_handle *) p;
2712 char ab[22], kind, *partof;
2713 char *t;
2714 bool local;
2716 if (info->parameter > 0)
2718 info->parameter = 0;
2720 /* Delayed name. */
2721 fprintf (info->f, "%s\t%s\t", info->stack->parents, info->filename);
2722 free (info->stack->parents);
2724 print_vma (addr, ab, true, true);
2725 translate_addresses (info->abfd, ab, info->f, info->syms);
2726 local = info->stack->flavor != NULL;
2727 if (info->stack->method && *info->stack->method)
2729 kind = 'm';
2730 partof = (char *) info->stack->method;
2732 else
2734 kind = 'f';
2735 partof = NULL;
2736 if (! info->stack->method && ! append_type (info, ")"))
2737 return false;
2739 t = pop_type (info);
2740 if (t == NULL)
2741 return false;
2742 fprintf (info->f, ";\"\tkind:%c\ttype:%s", kind, t);
2743 if (local)
2744 fputs ("\tfile:", info->f);
2745 if (partof)
2747 fprintf (info->f, "\tclass:%s", partof);
2748 free (partof);
2750 fputc ('\n', info->f);
2753 return true;
2756 /* Write out line number information. */
2758 static bool
2759 tg_lineno (void *p ATTRIBUTE_UNUSED, const char *fname ATTRIBUTE_UNUSED,
2760 unsigned long lineno ATTRIBUTE_UNUSED,
2761 bfd_vma addr ATTRIBUTE_UNUSED)
2763 return true;
2766 /* Finish writing out a block. */
2768 static bool
2769 tg_end_block (void *p ATTRIBUTE_UNUSED, bfd_vma addr ATTRIBUTE_UNUSED)
2771 return true;
2774 /* Convert the visibility value into a human readable name. */
2776 static const char *
2777 visibility_name (enum debug_visibility visibility)
2779 const char *s;
2781 switch (visibility)
2783 case DEBUG_VISIBILITY_PUBLIC:
2784 s = "public";
2785 break;
2786 case DEBUG_VISIBILITY_PRIVATE:
2787 s = "private";
2788 break;
2789 case DEBUG_VISIBILITY_PROTECTED:
2790 s = "protected";
2791 break;
2792 case DEBUG_VISIBILITY_IGNORE:
2793 s = "/* ignore */";
2794 break;
2795 default:
2796 abort ();
2797 return false;
2799 return s;