]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/cp-support.c
Fix memory leak in cp-support.c
[thirdparty/binutils-gdb.git] / gdb / cp-support.c
1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
3
4 Contributed by MontaVista Software.
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 3 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, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "cp-support.h"
23 #include "demangle.h"
24 #include "gdbcmd.h"
25 #include "dictionary.h"
26 #include "objfiles.h"
27 #include "frame.h"
28 #include "symtab.h"
29 #include "block.h"
30 #include "complaints.h"
31 #include "gdbtypes.h"
32 #include "expression.h"
33 #include "value.h"
34 #include "cp-abi.h"
35 #include "namespace.h"
36 #include <signal.h>
37 #include "gdb_setjmp.h"
38 #include "safe-ctype.h"
39
40 #define d_left(dc) (dc)->u.s_binary.left
41 #define d_right(dc) (dc)->u.s_binary.right
42
43 /* Functions related to demangled name parsing. */
44
45 static unsigned int cp_find_first_component_aux (const char *name,
46 int permissive);
47
48 static void demangled_name_complaint (const char *name);
49
50 /* Functions/variables related to overload resolution. */
51
52 static int sym_return_val_size = -1;
53 static int sym_return_val_index;
54 static struct symbol **sym_return_val;
55
56 static void overload_list_add_symbol (struct symbol *sym,
57 const char *oload_name);
58
59 static void make_symbol_overload_list_using (const char *func_name,
60 const char *the_namespace);
61
62 static void make_symbol_overload_list_qualified (const char *func_name);
63
64 /* The list of "maint cplus" commands. */
65
66 struct cmd_list_element *maint_cplus_cmd_list = NULL;
67
68 /* The actual commands. */
69
70 static void maint_cplus_command (char *arg, int from_tty);
71 static void first_component_command (char *arg, int from_tty);
72
73 /* A list of typedefs which should not be substituted by replace_typedefs. */
74 static const char * const ignore_typedefs[] =
75 {
76 "std::istream", "std::iostream", "std::ostream", "std::string"
77 };
78
79 static void
80 replace_typedefs (struct demangle_parse_info *info,
81 struct demangle_component *ret_comp,
82 canonicalization_ftype *finder,
83 void *data);
84
85 /* A convenience function to copy STRING into OBSTACK, returning a pointer
86 to the newly allocated string and saving the number of bytes saved in LEN.
87
88 It does not copy the terminating '\0' byte! */
89
90 static char *
91 copy_string_to_obstack (struct obstack *obstack, const char *string,
92 long *len)
93 {
94 *len = strlen (string);
95 return (char *) obstack_copy (obstack, string, *len);
96 }
97
98 /* Return 1 if STRING is clearly already in canonical form. This
99 function is conservative; things which it does not recognize are
100 assumed to be non-canonical, and the parser will sort them out
101 afterwards. This speeds up the critical path for alphanumeric
102 identifiers. */
103
104 static int
105 cp_already_canonical (const char *string)
106 {
107 /* Identifier start character [a-zA-Z_]. */
108 if (!ISIDST (string[0]))
109 return 0;
110
111 /* These are the only two identifiers which canonicalize to other
112 than themselves or an error: unsigned -> unsigned int and
113 signed -> int. */
114 if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
115 return 0;
116 else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
117 return 0;
118
119 /* Identifier character [a-zA-Z0-9_]. */
120 while (ISIDNUM (string[1]))
121 string++;
122
123 if (string[1] == '\0')
124 return 1;
125 else
126 return 0;
127 }
128
129 /* Inspect the given RET_COMP for its type. If it is a typedef,
130 replace the node with the typedef's tree.
131
132 Returns 1 if any typedef substitutions were made, 0 otherwise. */
133
134 static int
135 inspect_type (struct demangle_parse_info *info,
136 struct demangle_component *ret_comp,
137 canonicalization_ftype *finder,
138 void *data)
139 {
140 int i;
141 char *name;
142 struct symbol *sym;
143
144 /* Copy the symbol's name from RET_COMP and look it up
145 in the symbol table. */
146 name = (char *) alloca (ret_comp->u.s_name.len + 1);
147 memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len);
148 name[ret_comp->u.s_name.len] = '\0';
149
150 /* Ignore any typedefs that should not be substituted. */
151 for (i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i)
152 {
153 if (strcmp (name, ignore_typedefs[i]) == 0)
154 return 0;
155 }
156
157 sym = NULL;
158
159 TRY
160 {
161 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0).symbol;
162 }
163 CATCH (except, RETURN_MASK_ALL)
164 {
165 return 0;
166 }
167 END_CATCH
168
169 if (sym != NULL)
170 {
171 struct type *otype = SYMBOL_TYPE (sym);
172
173 if (finder != NULL)
174 {
175 const char *new_name = (*finder) (otype, data);
176
177 if (new_name != NULL)
178 {
179 ret_comp->u.s_name.s = new_name;
180 ret_comp->u.s_name.len = strlen (new_name);
181 return 1;
182 }
183
184 return 0;
185 }
186
187 /* If the type is a typedef or namespace alias, replace it. */
188 if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF
189 || TYPE_CODE (otype) == TYPE_CODE_NAMESPACE)
190 {
191 long len;
192 int is_anon;
193 struct type *type;
194 std::unique_ptr<demangle_parse_info> i;
195
196 /* Get the real type of the typedef. */
197 type = check_typedef (otype);
198
199 /* If the symbol is a namespace and its type name is no different
200 than the name we looked up, this symbol is not a namespace
201 alias and does not need to be substituted. */
202 if (TYPE_CODE (otype) == TYPE_CODE_NAMESPACE
203 && strcmp (TYPE_NAME (type), name) == 0)
204 return 0;
205
206 is_anon = (TYPE_TAG_NAME (type) == NULL
207 && (TYPE_CODE (type) == TYPE_CODE_ENUM
208 || TYPE_CODE (type) == TYPE_CODE_STRUCT
209 || TYPE_CODE (type) == TYPE_CODE_UNION));
210 if (is_anon)
211 {
212 struct type *last = otype;
213
214 /* Find the last typedef for the type. */
215 while (TYPE_TARGET_TYPE (last) != NULL
216 && (TYPE_CODE (TYPE_TARGET_TYPE (last))
217 == TYPE_CODE_TYPEDEF))
218 last = TYPE_TARGET_TYPE (last);
219
220 /* If there is only one typedef for this anonymous type,
221 do not substitute it. */
222 if (type == otype)
223 return 0;
224 else
225 /* Use the last typedef seen as the type for this
226 anonymous type. */
227 type = last;
228 }
229
230 string_file buf;
231 TRY
232 {
233 type_print (type, "", &buf, -1);
234 }
235 /* If type_print threw an exception, there is little point
236 in continuing, so just bow out gracefully. */
237 CATCH (except, RETURN_MASK_ERROR)
238 {
239 return 0;
240 }
241 END_CATCH
242
243 len = buf.size ();
244 name = (char *) obstack_copy0 (&info->obstack, buf.c_str (), len);
245
246 /* Turn the result into a new tree. Note that this
247 tree will contain pointers into NAME, so NAME cannot
248 be free'd until all typedef conversion is done and
249 the final result is converted into a string. */
250 i = cp_demangled_name_to_comp (name, NULL);
251 if (i != NULL)
252 {
253 /* Merge the two trees. */
254 cp_merge_demangle_parse_infos (info, ret_comp, i.get ());
255
256 /* Replace any newly introduced typedefs -- but not
257 if the type is anonymous (that would lead to infinite
258 looping). */
259 if (!is_anon)
260 replace_typedefs (info, ret_comp, finder, data);
261 }
262 else
263 {
264 /* This shouldn't happen unless the type printer has
265 output something that the name parser cannot grok.
266 Nonetheless, an ounce of prevention...
267
268 Canonicalize the name again, and store it in the
269 current node (RET_COMP). */
270 std::string canon = cp_canonicalize_string_no_typedefs (name);
271
272 if (!canon.empty ())
273 {
274 /* Copy the canonicalization into the obstack. */
275 name = copy_string_to_obstack (&info->obstack, canon.c_str (), &len);
276 }
277
278 ret_comp->u.s_name.s = name;
279 ret_comp->u.s_name.len = len;
280 }
281
282 return 1;
283 }
284 }
285
286 return 0;
287 }
288
289 /* Replace any typedefs appearing in the qualified name
290 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
291 given in INFO. */
292
293 static void
294 replace_typedefs_qualified_name (struct demangle_parse_info *info,
295 struct demangle_component *ret_comp,
296 canonicalization_ftype *finder,
297 void *data)
298 {
299 long len;
300 char *name;
301 string_file buf;
302 struct demangle_component *comp = ret_comp;
303
304 /* Walk each node of the qualified name, reconstructing the name of
305 this element. With every node, check for any typedef substitutions.
306 If a substitution has occurred, replace the qualified name node
307 with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
308 substituted name. */
309 while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME)
310 {
311 if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME)
312 {
313 struct demangle_component newobj;
314
315 buf.write (d_left (comp)->u.s_name.s, d_left (comp)->u.s_name.len);
316 len = buf.size ();
317 name = (char *) obstack_copy0 (&info->obstack, buf.c_str (), len);
318 newobj.type = DEMANGLE_COMPONENT_NAME;
319 newobj.u.s_name.s = name;
320 newobj.u.s_name.len = len;
321 if (inspect_type (info, &newobj, finder, data))
322 {
323 char *n, *s;
324 long slen;
325
326 /* A typedef was substituted in NEW. Convert it to a
327 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
328 node. */
329
330 buf.clear ();
331 n = cp_comp_to_string (&newobj, 100);
332 if (n == NULL)
333 {
334 /* If something went astray, abort typedef substitutions. */
335 return;
336 }
337
338 s = copy_string_to_obstack (&info->obstack, n, &slen);
339 xfree (n);
340
341 d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME;
342 d_left (ret_comp)->u.s_name.s = s;
343 d_left (ret_comp)->u.s_name.len = slen;
344 d_right (ret_comp) = d_right (comp);
345 comp = ret_comp;
346 continue;
347 }
348 }
349 else
350 {
351 /* The current node is not a name, so simply replace any
352 typedefs in it. Then print it to the stream to continue
353 checking for more typedefs in the tree. */
354 replace_typedefs (info, d_left (comp), finder, data);
355 name = cp_comp_to_string (d_left (comp), 100);
356 if (name == NULL)
357 {
358 /* If something went astray, abort typedef substitutions. */
359 return;
360 }
361 buf.puts (name);
362 xfree (name);
363 }
364
365 buf.write ("::", 2);
366 comp = d_right (comp);
367 }
368
369 /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
370 name assembled above and append the name given by COMP. Then use this
371 reassembled name to check for a typedef. */
372
373 if (comp->type == DEMANGLE_COMPONENT_NAME)
374 {
375 buf.write (comp->u.s_name.s, comp->u.s_name.len);
376 len = buf.size ();
377 name = (char *) obstack_copy0 (&info->obstack, buf.c_str (), len);
378
379 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
380 with a DEMANGLE_COMPONENT_NAME node containing the whole
381 name. */
382 ret_comp->type = DEMANGLE_COMPONENT_NAME;
383 ret_comp->u.s_name.s = name;
384 ret_comp->u.s_name.len = len;
385 inspect_type (info, ret_comp, finder, data);
386 }
387 else
388 replace_typedefs (info, comp, finder, data);
389 }
390
391
392 /* A function to check const and volatile qualifiers for argument types.
393
394 "Parameter declarations that differ only in the presence
395 or absence of `const' and/or `volatile' are equivalent."
396 C++ Standard N3290, clause 13.1.3 #4. */
397
398 static void
399 check_cv_qualifiers (struct demangle_component *ret_comp)
400 {
401 while (d_left (ret_comp) != NULL
402 && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST
403 || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE))
404 {
405 d_left (ret_comp) = d_left (d_left (ret_comp));
406 }
407 }
408
409 /* Walk the parse tree given by RET_COMP, replacing any typedefs with
410 their basic types. */
411
412 static void
413 replace_typedefs (struct demangle_parse_info *info,
414 struct demangle_component *ret_comp,
415 canonicalization_ftype *finder,
416 void *data)
417 {
418 if (ret_comp)
419 {
420 if (finder != NULL
421 && (ret_comp->type == DEMANGLE_COMPONENT_NAME
422 || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME
423 || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE
424 || ret_comp->type == DEMANGLE_COMPONENT_BUILTIN_TYPE))
425 {
426 char *local_name = cp_comp_to_string (ret_comp, 10);
427
428 if (local_name != NULL)
429 {
430 struct symbol *sym = NULL;
431
432 sym = NULL;
433 TRY
434 {
435 sym = lookup_symbol (local_name, 0, VAR_DOMAIN, 0).symbol;
436 }
437 CATCH (except, RETURN_MASK_ALL)
438 {
439 }
440 END_CATCH
441
442 xfree (local_name);
443
444 if (sym != NULL)
445 {
446 struct type *otype = SYMBOL_TYPE (sym);
447 const char *new_name = (*finder) (otype, data);
448
449 if (new_name != NULL)
450 {
451 ret_comp->type = DEMANGLE_COMPONENT_NAME;
452 ret_comp->u.s_name.s = new_name;
453 ret_comp->u.s_name.len = strlen (new_name);
454 return;
455 }
456 }
457 }
458 }
459
460 switch (ret_comp->type)
461 {
462 case DEMANGLE_COMPONENT_ARGLIST:
463 check_cv_qualifiers (ret_comp);
464 /* Fall through */
465
466 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
467 case DEMANGLE_COMPONENT_TEMPLATE:
468 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
469 case DEMANGLE_COMPONENT_TYPED_NAME:
470 replace_typedefs (info, d_left (ret_comp), finder, data);
471 replace_typedefs (info, d_right (ret_comp), finder, data);
472 break;
473
474 case DEMANGLE_COMPONENT_NAME:
475 inspect_type (info, ret_comp, finder, data);
476 break;
477
478 case DEMANGLE_COMPONENT_QUAL_NAME:
479 replace_typedefs_qualified_name (info, ret_comp, finder, data);
480 break;
481
482 case DEMANGLE_COMPONENT_LOCAL_NAME:
483 case DEMANGLE_COMPONENT_CTOR:
484 case DEMANGLE_COMPONENT_ARRAY_TYPE:
485 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
486 replace_typedefs (info, d_right (ret_comp), finder, data);
487 break;
488
489 case DEMANGLE_COMPONENT_CONST:
490 case DEMANGLE_COMPONENT_RESTRICT:
491 case DEMANGLE_COMPONENT_VOLATILE:
492 case DEMANGLE_COMPONENT_VOLATILE_THIS:
493 case DEMANGLE_COMPONENT_CONST_THIS:
494 case DEMANGLE_COMPONENT_RESTRICT_THIS:
495 case DEMANGLE_COMPONENT_POINTER:
496 case DEMANGLE_COMPONENT_REFERENCE:
497 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
498 replace_typedefs (info, d_left (ret_comp), finder, data);
499 break;
500
501 default:
502 break;
503 }
504 }
505 }
506
507 /* Parse STRING and convert it to canonical form, resolving any
508 typedefs. If parsing fails, or if STRING is already canonical,
509 return the empty string. Otherwise return the canonical form. If
510 FINDER is not NULL, then type components are passed to FINDER to be
511 looked up. DATA is passed verbatim to FINDER. */
512
513 std::string
514 cp_canonicalize_string_full (const char *string,
515 canonicalization_ftype *finder,
516 void *data)
517 {
518 std::string ret;
519 unsigned int estimated_len;
520 std::unique_ptr<demangle_parse_info> info;
521
522 estimated_len = strlen (string) * 2;
523 info = cp_demangled_name_to_comp (string, NULL);
524 if (info != NULL)
525 {
526 /* Replace all the typedefs in the tree. */
527 replace_typedefs (info.get (), info->tree, finder, data);
528
529 /* Convert the tree back into a string. */
530 gdb::unique_xmalloc_ptr<char> us (cp_comp_to_string (info->tree,
531 estimated_len));
532 gdb_assert (us);
533
534 ret = us.get ();
535 /* Finally, compare the original string with the computed
536 name, returning NULL if they are the same. */
537 if (ret == string)
538 return std::string ();
539 }
540
541 return ret;
542 }
543
544 /* Like cp_canonicalize_string_full, but always passes NULL for
545 FINDER. */
546
547 std::string
548 cp_canonicalize_string_no_typedefs (const char *string)
549 {
550 return cp_canonicalize_string_full (string, NULL, NULL);
551 }
552
553 /* Parse STRING and convert it to canonical form. If parsing fails,
554 or if STRING is already canonical, return the empty string.
555 Otherwise return the canonical form. */
556
557 std::string
558 cp_canonicalize_string (const char *string)
559 {
560 std::unique_ptr<demangle_parse_info> info;
561 unsigned int estimated_len;
562
563 if (cp_already_canonical (string))
564 return std::string ();
565
566 info = cp_demangled_name_to_comp (string, NULL);
567 if (info == NULL)
568 return std::string ();
569
570 estimated_len = strlen (string) * 2;
571 gdb::unique_xmalloc_ptr<char> us (cp_comp_to_string (info->tree,
572 estimated_len));
573
574 if (!us)
575 {
576 warning (_("internal error: string \"%s\" failed to be canonicalized"),
577 string);
578 return std::string ();
579 }
580
581 std::string ret (us.get ());
582
583 if (ret == string)
584 return std::string ();
585
586 return ret;
587 }
588
589 /* Convert a mangled name to a demangle_component tree. *MEMORY is
590 set to the block of used memory that should be freed when finished
591 with the tree. DEMANGLED_P is set to the char * that should be
592 freed when finished with the tree, or NULL if none was needed.
593 OPTIONS will be passed to the demangler. */
594
595 static std::unique_ptr<demangle_parse_info>
596 mangled_name_to_comp (const char *mangled_name, int options,
597 void **memory, char **demangled_p)
598 {
599 char *demangled_name;
600
601 /* If it looks like a v3 mangled name, then try to go directly
602 to trees. */
603 if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
604 {
605 struct demangle_component *ret;
606
607 ret = cplus_demangle_v3_components (mangled_name,
608 options, memory);
609 if (ret)
610 {
611 std::unique_ptr<demangle_parse_info> info (new demangle_parse_info);
612 info->tree = ret;
613 *demangled_p = NULL;
614 return info;
615 }
616 }
617
618 /* If it doesn't, or if that failed, then try to demangle the
619 name. */
620 demangled_name = gdb_demangle (mangled_name, options);
621 if (demangled_name == NULL)
622 return NULL;
623
624 /* If we could demangle the name, parse it to build the component
625 tree. */
626 std::unique_ptr<demangle_parse_info> info
627 = cp_demangled_name_to_comp (demangled_name, NULL);
628
629 if (info == NULL)
630 {
631 xfree (demangled_name);
632 return NULL;
633 }
634
635 *demangled_p = demangled_name;
636 return info;
637 }
638
639 /* Return the name of the class containing method PHYSNAME. */
640
641 char *
642 cp_class_name_from_physname (const char *physname)
643 {
644 void *storage = NULL;
645 char *demangled_name = NULL, *ret;
646 struct demangle_component *ret_comp, *prev_comp, *cur_comp;
647 std::unique_ptr<demangle_parse_info> info;
648 int done;
649
650 info = mangled_name_to_comp (physname, DMGL_ANSI,
651 &storage, &demangled_name);
652 if (info == NULL)
653 return NULL;
654
655 done = 0;
656 ret_comp = info->tree;
657
658 /* First strip off any qualifiers, if we have a function or
659 method. */
660 while (!done)
661 switch (ret_comp->type)
662 {
663 case DEMANGLE_COMPONENT_CONST:
664 case DEMANGLE_COMPONENT_RESTRICT:
665 case DEMANGLE_COMPONENT_VOLATILE:
666 case DEMANGLE_COMPONENT_CONST_THIS:
667 case DEMANGLE_COMPONENT_RESTRICT_THIS:
668 case DEMANGLE_COMPONENT_VOLATILE_THIS:
669 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
670 ret_comp = d_left (ret_comp);
671 break;
672 default:
673 done = 1;
674 break;
675 }
676
677 /* If what we have now is a function, discard the argument list. */
678 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
679 ret_comp = d_left (ret_comp);
680
681 /* If what we have now is a template, strip off the template
682 arguments. The left subtree may be a qualified name. */
683 if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
684 ret_comp = d_left (ret_comp);
685
686 /* What we have now should be a name, possibly qualified.
687 Additional qualifiers could live in the left subtree or the right
688 subtree. Find the last piece. */
689 done = 0;
690 prev_comp = NULL;
691 cur_comp = ret_comp;
692 while (!done)
693 switch (cur_comp->type)
694 {
695 case DEMANGLE_COMPONENT_QUAL_NAME:
696 case DEMANGLE_COMPONENT_LOCAL_NAME:
697 prev_comp = cur_comp;
698 cur_comp = d_right (cur_comp);
699 break;
700 case DEMANGLE_COMPONENT_TEMPLATE:
701 case DEMANGLE_COMPONENT_NAME:
702 case DEMANGLE_COMPONENT_CTOR:
703 case DEMANGLE_COMPONENT_DTOR:
704 case DEMANGLE_COMPONENT_OPERATOR:
705 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
706 done = 1;
707 break;
708 default:
709 done = 1;
710 cur_comp = NULL;
711 break;
712 }
713
714 ret = NULL;
715 if (cur_comp != NULL && prev_comp != NULL)
716 {
717 /* We want to discard the rightmost child of PREV_COMP. */
718 *prev_comp = *d_left (prev_comp);
719 /* The ten is completely arbitrary; we don't have a good
720 estimate. */
721 ret = cp_comp_to_string (ret_comp, 10);
722 }
723
724 xfree (storage);
725 xfree (demangled_name);
726 return ret;
727 }
728
729 /* Return the child of COMP which is the basename of a method,
730 variable, et cetera. All scope qualifiers are discarded, but
731 template arguments will be included. The component tree may be
732 modified. */
733
734 static struct demangle_component *
735 unqualified_name_from_comp (struct demangle_component *comp)
736 {
737 struct demangle_component *ret_comp = comp, *last_template;
738 int done;
739
740 done = 0;
741 last_template = NULL;
742 while (!done)
743 switch (ret_comp->type)
744 {
745 case DEMANGLE_COMPONENT_QUAL_NAME:
746 case DEMANGLE_COMPONENT_LOCAL_NAME:
747 ret_comp = d_right (ret_comp);
748 break;
749 case DEMANGLE_COMPONENT_TYPED_NAME:
750 ret_comp = d_left (ret_comp);
751 break;
752 case DEMANGLE_COMPONENT_TEMPLATE:
753 gdb_assert (last_template == NULL);
754 last_template = ret_comp;
755 ret_comp = d_left (ret_comp);
756 break;
757 case DEMANGLE_COMPONENT_CONST:
758 case DEMANGLE_COMPONENT_RESTRICT:
759 case DEMANGLE_COMPONENT_VOLATILE:
760 case DEMANGLE_COMPONENT_CONST_THIS:
761 case DEMANGLE_COMPONENT_RESTRICT_THIS:
762 case DEMANGLE_COMPONENT_VOLATILE_THIS:
763 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
764 ret_comp = d_left (ret_comp);
765 break;
766 case DEMANGLE_COMPONENT_NAME:
767 case DEMANGLE_COMPONENT_CTOR:
768 case DEMANGLE_COMPONENT_DTOR:
769 case DEMANGLE_COMPONENT_OPERATOR:
770 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
771 done = 1;
772 break;
773 default:
774 return NULL;
775 break;
776 }
777
778 if (last_template)
779 {
780 d_left (last_template) = ret_comp;
781 return last_template;
782 }
783
784 return ret_comp;
785 }
786
787 /* Return the name of the method whose linkage name is PHYSNAME. */
788
789 char *
790 method_name_from_physname (const char *physname)
791 {
792 void *storage = NULL;
793 char *demangled_name = NULL, *ret;
794 struct demangle_component *ret_comp;
795 std::unique_ptr<demangle_parse_info> info;
796
797 info = mangled_name_to_comp (physname, DMGL_ANSI,
798 &storage, &demangled_name);
799 if (info == NULL)
800 return NULL;
801
802 ret_comp = unqualified_name_from_comp (info->tree);
803
804 ret = NULL;
805 if (ret_comp != NULL)
806 /* The ten is completely arbitrary; we don't have a good
807 estimate. */
808 ret = cp_comp_to_string (ret_comp, 10);
809
810 xfree (storage);
811 xfree (demangled_name);
812 return ret;
813 }
814
815 /* If FULL_NAME is the demangled name of a C++ function (including an
816 arg list, possibly including namespace/class qualifications),
817 return a new string containing only the function name (without the
818 arg list/class qualifications). Otherwise, return NULL. The
819 caller is responsible for freeing the memory in question. */
820
821 char *
822 cp_func_name (const char *full_name)
823 {
824 char *ret;
825 struct demangle_component *ret_comp;
826 std::unique_ptr<demangle_parse_info> info;
827
828 info = cp_demangled_name_to_comp (full_name, NULL);
829 if (!info)
830 return NULL;
831
832 ret_comp = unqualified_name_from_comp (info->tree);
833
834 ret = NULL;
835 if (ret_comp != NULL)
836 ret = cp_comp_to_string (ret_comp, 10);
837
838 return ret;
839 }
840
841 /* DEMANGLED_NAME is the name of a function, including parameters and
842 (optionally) a return type. Return the name of the function without
843 parameters or return type, or NULL if we can not parse the name. */
844
845 char *
846 cp_remove_params (const char *demangled_name)
847 {
848 int done = 0;
849 struct demangle_component *ret_comp;
850 std::unique_ptr<demangle_parse_info> info;
851 char *ret = NULL;
852
853 if (demangled_name == NULL)
854 return NULL;
855
856 info = cp_demangled_name_to_comp (demangled_name, NULL);
857 if (info == NULL)
858 return NULL;
859
860 /* First strip off any qualifiers, if we have a function or method. */
861 ret_comp = info->tree;
862 while (!done)
863 switch (ret_comp->type)
864 {
865 case DEMANGLE_COMPONENT_CONST:
866 case DEMANGLE_COMPONENT_RESTRICT:
867 case DEMANGLE_COMPONENT_VOLATILE:
868 case DEMANGLE_COMPONENT_CONST_THIS:
869 case DEMANGLE_COMPONENT_RESTRICT_THIS:
870 case DEMANGLE_COMPONENT_VOLATILE_THIS:
871 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
872 ret_comp = d_left (ret_comp);
873 break;
874 default:
875 done = 1;
876 break;
877 }
878
879 /* What we have now should be a function. Return its name. */
880 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
881 ret = cp_comp_to_string (d_left (ret_comp), 10);
882
883 return ret;
884 }
885
886 /* Here are some random pieces of trivia to keep in mind while trying
887 to take apart demangled names:
888
889 - Names can contain function arguments or templates, so the process
890 has to be, to some extent recursive: maybe keep track of your
891 depth based on encountering <> and ().
892
893 - Parentheses don't just have to happen at the end of a name: they
894 can occur even if the name in question isn't a function, because
895 a template argument might be a type that's a function.
896
897 - Conversely, even if you're trying to deal with a function, its
898 demangled name might not end with ')': it could be a const or
899 volatile class method, in which case it ends with "const" or
900 "volatile".
901
902 - Parentheses are also used in anonymous namespaces: a variable
903 'foo' in an anonymous namespace gets demangled as "(anonymous
904 namespace)::foo".
905
906 - And operator names can contain parentheses or angle brackets. */
907
908 /* FIXME: carlton/2003-03-13: We have several functions here with
909 overlapping functionality; can we combine them? Also, do they
910 handle all the above considerations correctly? */
911
912
913 /* This returns the length of first component of NAME, which should be
914 the demangled name of a C++ variable/function/method/etc.
915 Specifically, it returns the index of the first colon forming the
916 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
917 it returns the 1, and given 'foo', it returns 0. */
918
919 /* The character in NAME indexed by the return value is guaranteed to
920 always be either ':' or '\0'. */
921
922 /* NOTE: carlton/2003-03-13: This function is currently only intended
923 for internal use: it's probably not entirely safe when called on
924 user-generated input, because some of the 'index += 2' lines in
925 cp_find_first_component_aux might go past the end of malformed
926 input. */
927
928 unsigned int
929 cp_find_first_component (const char *name)
930 {
931 return cp_find_first_component_aux (name, 0);
932 }
933
934 /* Helper function for cp_find_first_component. Like that function,
935 it returns the length of the first component of NAME, but to make
936 the recursion easier, it also stops if it reaches an unexpected ')'
937 or '>' if the value of PERMISSIVE is nonzero. */
938
939 static unsigned int
940 cp_find_first_component_aux (const char *name, int permissive)
941 {
942 unsigned int index = 0;
943 /* Operator names can show up in unexpected places. Since these can
944 contain parentheses or angle brackets, they can screw up the
945 recursion. But not every string 'operator' is part of an
946 operater name: e.g. you could have a variable 'cooperator'. So
947 this variable tells us whether or not we should treat the string
948 'operator' as starting an operator. */
949 int operator_possible = 1;
950
951 for (;; ++index)
952 {
953 switch (name[index])
954 {
955 case '<':
956 /* Template; eat it up. The calls to cp_first_component
957 should only return (I hope!) when they reach the '>'
958 terminating the component or a '::' between two
959 components. (Hence the '+ 2'.) */
960 index += 1;
961 for (index += cp_find_first_component_aux (name + index, 1);
962 name[index] != '>';
963 index += cp_find_first_component_aux (name + index, 1))
964 {
965 if (name[index] != ':')
966 {
967 demangled_name_complaint (name);
968 return strlen (name);
969 }
970 index += 2;
971 }
972 operator_possible = 1;
973 break;
974 case '(':
975 /* Similar comment as to '<'. */
976 index += 1;
977 for (index += cp_find_first_component_aux (name + index, 1);
978 name[index] != ')';
979 index += cp_find_first_component_aux (name + index, 1))
980 {
981 if (name[index] != ':')
982 {
983 demangled_name_complaint (name);
984 return strlen (name);
985 }
986 index += 2;
987 }
988 operator_possible = 1;
989 break;
990 case '>':
991 case ')':
992 if (permissive)
993 return index;
994 else
995 {
996 demangled_name_complaint (name);
997 return strlen (name);
998 }
999 case '\0':
1000 return index;
1001 case ':':
1002 /* ':' marks a component iff the next character is also a ':'.
1003 Otherwise it is probably malformed input. */
1004 if (name[index + 1] == ':')
1005 return index;
1006 break;
1007 case 'o':
1008 /* Operator names can screw up the recursion. */
1009 if (operator_possible
1010 && startswith (name + index, CP_OPERATOR_STR))
1011 {
1012 index += CP_OPERATOR_LEN;
1013 while (ISSPACE(name[index]))
1014 ++index;
1015 switch (name[index])
1016 {
1017 case '\0':
1018 return index;
1019 /* Skip over one less than the appropriate number of
1020 characters: the for loop will skip over the last
1021 one. */
1022 case '<':
1023 if (name[index + 1] == '<')
1024 index += 1;
1025 else
1026 index += 0;
1027 break;
1028 case '>':
1029 case '-':
1030 if (name[index + 1] == '>')
1031 index += 1;
1032 else
1033 index += 0;
1034 break;
1035 case '(':
1036 index += 1;
1037 break;
1038 default:
1039 index += 0;
1040 break;
1041 }
1042 }
1043 operator_possible = 0;
1044 break;
1045 case ' ':
1046 case ',':
1047 case '.':
1048 case '&':
1049 case '*':
1050 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1051 set of relevant characters are here: it's necessary to
1052 include any character that can show up before 'operator'
1053 in a demangled name, and it's safe to include any
1054 character that can't be part of an identifier's name. */
1055 operator_possible = 1;
1056 break;
1057 default:
1058 operator_possible = 0;
1059 break;
1060 }
1061 }
1062 }
1063
1064 /* Complain about a demangled name that we don't know how to parse.
1065 NAME is the demangled name in question. */
1066
1067 static void
1068 demangled_name_complaint (const char *name)
1069 {
1070 complaint (&symfile_complaints,
1071 "unexpected demangled name '%s'", name);
1072 }
1073
1074 /* If NAME is the fully-qualified name of a C++
1075 function/variable/method/etc., this returns the length of its
1076 entire prefix: all of the namespaces and classes that make up its
1077 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1078 4, given 'foo', it returns 0. */
1079
1080 unsigned int
1081 cp_entire_prefix_len (const char *name)
1082 {
1083 unsigned int current_len = cp_find_first_component (name);
1084 unsigned int previous_len = 0;
1085
1086 while (name[current_len] != '\0')
1087 {
1088 gdb_assert (name[current_len] == ':');
1089 previous_len = current_len;
1090 /* Skip the '::'. */
1091 current_len += 2;
1092 current_len += cp_find_first_component (name + current_len);
1093 }
1094
1095 return previous_len;
1096 }
1097
1098 /* Overload resolution functions. */
1099
1100 /* Test to see if SYM is a symbol that we haven't seen corresponding
1101 to a function named OLOAD_NAME. If so, add it to the current
1102 completion list. */
1103
1104 static void
1105 overload_list_add_symbol (struct symbol *sym,
1106 const char *oload_name)
1107 {
1108 int newsize;
1109 int i;
1110 char *sym_name;
1111
1112 /* If there is no type information, we can't do anything, so
1113 skip. */
1114 if (SYMBOL_TYPE (sym) == NULL)
1115 return;
1116
1117 /* skip any symbols that we've already considered. */
1118 for (i = 0; i < sym_return_val_index; ++i)
1119 if (strcmp (SYMBOL_LINKAGE_NAME (sym),
1120 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
1121 return;
1122
1123 /* Get the demangled name without parameters */
1124 sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
1125 if (!sym_name)
1126 return;
1127
1128 /* skip symbols that cannot match */
1129 if (strcmp (sym_name, oload_name) != 0)
1130 {
1131 xfree (sym_name);
1132 return;
1133 }
1134
1135 xfree (sym_name);
1136
1137 /* We have a match for an overload instance, so add SYM to the
1138 current list of overload instances */
1139 if (sym_return_val_index + 3 > sym_return_val_size)
1140 {
1141 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
1142 sym_return_val = (struct symbol **)
1143 xrealloc ((char *) sym_return_val, newsize);
1144 }
1145 sym_return_val[sym_return_val_index++] = sym;
1146 sym_return_val[sym_return_val_index] = NULL;
1147 }
1148
1149 /* Return a null-terminated list of pointers to function symbols that
1150 are named FUNC_NAME and are visible within NAMESPACE. */
1151
1152 struct symbol **
1153 make_symbol_overload_list (const char *func_name,
1154 const char *the_namespace)
1155 {
1156 struct cleanup *old_cleanups;
1157 const char *name;
1158
1159 sym_return_val_size = 100;
1160 sym_return_val_index = 0;
1161 sym_return_val = XNEWVEC (struct symbol *, sym_return_val_size + 1);
1162 sym_return_val[0] = NULL;
1163
1164 old_cleanups = make_cleanup (xfree, sym_return_val);
1165
1166 make_symbol_overload_list_using (func_name, the_namespace);
1167
1168 if (the_namespace[0] == '\0')
1169 name = func_name;
1170 else
1171 {
1172 char *concatenated_name
1173 = (char *) alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1);
1174 strcpy (concatenated_name, the_namespace);
1175 strcat (concatenated_name, "::");
1176 strcat (concatenated_name, func_name);
1177 name = concatenated_name;
1178 }
1179
1180 make_symbol_overload_list_qualified (name);
1181
1182 discard_cleanups (old_cleanups);
1183
1184 return sym_return_val;
1185 }
1186
1187 /* Add all symbols with a name matching NAME in BLOCK to the overload
1188 list. */
1189
1190 static void
1191 make_symbol_overload_list_block (const char *name,
1192 const struct block *block)
1193 {
1194 struct block_iterator iter;
1195 struct symbol *sym;
1196
1197 ALL_BLOCK_SYMBOLS_WITH_NAME (block, name, iter, sym)
1198 overload_list_add_symbol (sym, name);
1199 }
1200
1201 /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1202
1203 static void
1204 make_symbol_overload_list_namespace (const char *func_name,
1205 const char *the_namespace)
1206 {
1207 const char *name;
1208 const struct block *block = NULL;
1209
1210 if (the_namespace[0] == '\0')
1211 name = func_name;
1212 else
1213 {
1214 char *concatenated_name
1215 = (char *) alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1);
1216
1217 strcpy (concatenated_name, the_namespace);
1218 strcat (concatenated_name, "::");
1219 strcat (concatenated_name, func_name);
1220 name = concatenated_name;
1221 }
1222
1223 /* Look in the static block. */
1224 block = block_static_block (get_selected_block (0));
1225 if (block)
1226 make_symbol_overload_list_block (name, block);
1227
1228 /* Look in the global block. */
1229 block = block_global_block (block);
1230 if (block)
1231 make_symbol_overload_list_block (name, block);
1232
1233 }
1234
1235 /* Search the namespace of the given type and namespace of and public
1236 base types. */
1237
1238 static void
1239 make_symbol_overload_list_adl_namespace (struct type *type,
1240 const char *func_name)
1241 {
1242 char *the_namespace;
1243 const char *type_name;
1244 int i, prefix_len;
1245
1246 while (TYPE_CODE (type) == TYPE_CODE_PTR
1247 || TYPE_IS_REFERENCE (type)
1248 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1249 || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1250 {
1251 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1252 type = check_typedef(type);
1253 else
1254 type = TYPE_TARGET_TYPE (type);
1255 }
1256
1257 type_name = TYPE_NAME (type);
1258
1259 if (type_name == NULL)
1260 return;
1261
1262 prefix_len = cp_entire_prefix_len (type_name);
1263
1264 if (prefix_len != 0)
1265 {
1266 the_namespace = (char *) alloca (prefix_len + 1);
1267 strncpy (the_namespace, type_name, prefix_len);
1268 the_namespace[prefix_len] = '\0';
1269
1270 make_symbol_overload_list_namespace (func_name, the_namespace);
1271 }
1272
1273 /* Check public base type */
1274 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1275 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1276 {
1277 if (BASETYPE_VIA_PUBLIC (type, i))
1278 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
1279 i),
1280 func_name);
1281 }
1282 }
1283
1284 /* Adds the overload list overload candidates for FUNC_NAME found
1285 through argument dependent lookup. */
1286
1287 struct symbol **
1288 make_symbol_overload_list_adl (struct type **arg_types, int nargs,
1289 const char *func_name)
1290 {
1291 int i;
1292
1293 gdb_assert (sym_return_val_size != -1);
1294
1295 for (i = 1; i <= nargs; i++)
1296 make_symbol_overload_list_adl_namespace (arg_types[i - 1],
1297 func_name);
1298
1299 return sym_return_val;
1300 }
1301
1302 /* Used for cleanups to reset the "searched" flag in case of an
1303 error. */
1304
1305 static void
1306 reset_directive_searched (void *data)
1307 {
1308 struct using_direct *direct = (struct using_direct *) data;
1309 direct->searched = 0;
1310 }
1311
1312 /* This applies the using directives to add namespaces to search in,
1313 and then searches for overloads in all of those namespaces. It
1314 adds the symbols found to sym_return_val. Arguments are as in
1315 make_symbol_overload_list. */
1316
1317 static void
1318 make_symbol_overload_list_using (const char *func_name,
1319 const char *the_namespace)
1320 {
1321 struct using_direct *current;
1322 const struct block *block;
1323
1324 /* First, go through the using directives. If any of them apply,
1325 look in the appropriate namespaces for new functions to match
1326 on. */
1327
1328 for (block = get_selected_block (0);
1329 block != NULL;
1330 block = BLOCK_SUPERBLOCK (block))
1331 for (current = block_using (block);
1332 current != NULL;
1333 current = current->next)
1334 {
1335 /* Prevent recursive calls. */
1336 if (current->searched)
1337 continue;
1338
1339 /* If this is a namespace alias or imported declaration ignore
1340 it. */
1341 if (current->alias != NULL || current->declaration != NULL)
1342 continue;
1343
1344 if (strcmp (the_namespace, current->import_dest) == 0)
1345 {
1346 /* Mark this import as searched so that the recursive call
1347 does not search it again. */
1348 struct cleanup *old_chain;
1349 current->searched = 1;
1350 old_chain = make_cleanup (reset_directive_searched,
1351 current);
1352
1353 make_symbol_overload_list_using (func_name,
1354 current->import_src);
1355
1356 current->searched = 0;
1357 discard_cleanups (old_chain);
1358 }
1359 }
1360
1361 /* Now, add names for this namespace. */
1362 make_symbol_overload_list_namespace (func_name, the_namespace);
1363 }
1364
1365 /* This does the bulk of the work of finding overloaded symbols.
1366 FUNC_NAME is the name of the overloaded function we're looking for
1367 (possibly including namespace info). */
1368
1369 static void
1370 make_symbol_overload_list_qualified (const char *func_name)
1371 {
1372 struct compunit_symtab *cust;
1373 struct objfile *objfile;
1374 const struct block *b, *surrounding_static_block = 0;
1375
1376 /* Look through the partial symtabs for all symbols which begin by
1377 matching FUNC_NAME. Make sure we read that symbol table in. */
1378
1379 ALL_OBJFILES (objfile)
1380 {
1381 if (objfile->sf)
1382 objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
1383 }
1384
1385 /* Search upwards from currently selected frame (so that we can
1386 complete on local vars. */
1387
1388 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
1389 make_symbol_overload_list_block (func_name, b);
1390
1391 surrounding_static_block = block_static_block (get_selected_block (0));
1392
1393 /* Go through the symtabs and check the externs and statics for
1394 symbols which match. */
1395
1396 ALL_COMPUNITS (objfile, cust)
1397 {
1398 QUIT;
1399 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), GLOBAL_BLOCK);
1400 make_symbol_overload_list_block (func_name, b);
1401 }
1402
1403 ALL_COMPUNITS (objfile, cust)
1404 {
1405 QUIT;
1406 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), STATIC_BLOCK);
1407 /* Don't do this block twice. */
1408 if (b == surrounding_static_block)
1409 continue;
1410 make_symbol_overload_list_block (func_name, b);
1411 }
1412 }
1413
1414 /* Lookup the rtti type for a class name. */
1415
1416 struct type *
1417 cp_lookup_rtti_type (const char *name, struct block *block)
1418 {
1419 struct symbol * rtti_sym;
1420 struct type * rtti_type;
1421
1422 /* Use VAR_DOMAIN here as NAME may be a typedef. PR 18141, 18417.
1423 Classes "live" in both STRUCT_DOMAIN and VAR_DOMAIN. */
1424 rtti_sym = lookup_symbol (name, block, VAR_DOMAIN, NULL).symbol;
1425
1426 if (rtti_sym == NULL)
1427 {
1428 warning (_("RTTI symbol not found for class '%s'"), name);
1429 return NULL;
1430 }
1431
1432 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
1433 {
1434 warning (_("RTTI symbol for class '%s' is not a type"), name);
1435 return NULL;
1436 }
1437
1438 rtti_type = check_typedef (SYMBOL_TYPE (rtti_sym));
1439
1440 switch (TYPE_CODE (rtti_type))
1441 {
1442 case TYPE_CODE_STRUCT:
1443 break;
1444 case TYPE_CODE_NAMESPACE:
1445 /* chastain/2003-11-26: the symbol tables often contain fake
1446 symbols for namespaces with the same name as the struct.
1447 This warning is an indication of a bug in the lookup order
1448 or a bug in the way that the symbol tables are populated. */
1449 warning (_("RTTI symbol for class '%s' is a namespace"), name);
1450 return NULL;
1451 default:
1452 warning (_("RTTI symbol for class '%s' has bad type"), name);
1453 return NULL;
1454 }
1455
1456 return rtti_type;
1457 }
1458
1459 #ifdef HAVE_WORKING_FORK
1460
1461 /* If nonzero, attempt to catch crashes in the demangler and print
1462 useful debugging information. */
1463
1464 static int catch_demangler_crashes = 1;
1465
1466 /* Stack context and environment for demangler crash recovery. */
1467
1468 static SIGJMP_BUF gdb_demangle_jmp_buf;
1469
1470 /* If nonzero, attempt to dump core from the signal handler. */
1471
1472 static int gdb_demangle_attempt_core_dump = 1;
1473
1474 /* Signal handler for gdb_demangle. */
1475
1476 static void
1477 gdb_demangle_signal_handler (int signo)
1478 {
1479 if (gdb_demangle_attempt_core_dump)
1480 {
1481 if (fork () == 0)
1482 dump_core ();
1483
1484 gdb_demangle_attempt_core_dump = 0;
1485 }
1486
1487 SIGLONGJMP (gdb_demangle_jmp_buf, signo);
1488 }
1489
1490 #endif
1491
1492 /* A wrapper for bfd_demangle. */
1493
1494 char *
1495 gdb_demangle (const char *name, int options)
1496 {
1497 char *result = NULL;
1498 int crash_signal = 0;
1499
1500 #ifdef HAVE_WORKING_FORK
1501 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1502 struct sigaction sa, old_sa;
1503 #else
1504 sighandler_t ofunc;
1505 #endif
1506 static int core_dump_allowed = -1;
1507
1508 if (core_dump_allowed == -1)
1509 {
1510 core_dump_allowed = can_dump_core (LIMIT_CUR);
1511
1512 if (!core_dump_allowed)
1513 gdb_demangle_attempt_core_dump = 0;
1514 }
1515
1516 if (catch_demangler_crashes)
1517 {
1518 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1519 sa.sa_handler = gdb_demangle_signal_handler;
1520 sigemptyset (&sa.sa_mask);
1521 #ifdef HAVE_SIGALTSTACK
1522 sa.sa_flags = SA_ONSTACK;
1523 #else
1524 sa.sa_flags = 0;
1525 #endif
1526 sigaction (SIGSEGV, &sa, &old_sa);
1527 #else
1528 ofunc = signal (SIGSEGV, gdb_demangle_signal_handler);
1529 #endif
1530
1531 crash_signal = SIGSETJMP (gdb_demangle_jmp_buf);
1532 }
1533 #endif
1534
1535 if (crash_signal == 0)
1536 result = bfd_demangle (NULL, name, options);
1537
1538 #ifdef HAVE_WORKING_FORK
1539 if (catch_demangler_crashes)
1540 {
1541 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1542 sigaction (SIGSEGV, &old_sa, NULL);
1543 #else
1544 signal (SIGSEGV, ofunc);
1545 #endif
1546
1547 if (crash_signal != 0)
1548 {
1549 static int error_reported = 0;
1550
1551 if (!error_reported)
1552 {
1553 char *short_msg, *long_msg;
1554 struct cleanup *back_to;
1555
1556 short_msg = xstrprintf (_("unable to demangle '%s' "
1557 "(demangler failed with signal %d)"),
1558 name, crash_signal);
1559 back_to = make_cleanup (xfree, short_msg);
1560
1561 long_msg = xstrprintf ("%s:%d: %s: %s", __FILE__, __LINE__,
1562 "demangler-warning", short_msg);
1563 make_cleanup (xfree, long_msg);
1564
1565 make_cleanup_restore_target_terminal ();
1566 target_terminal_ours_for_output ();
1567
1568 begin_line ();
1569 if (core_dump_allowed)
1570 fprintf_unfiltered (gdb_stderr,
1571 _("%s\nAttempting to dump core.\n"),
1572 long_msg);
1573 else
1574 warn_cant_dump_core (long_msg);
1575
1576 demangler_warning (__FILE__, __LINE__, "%s", short_msg);
1577
1578 do_cleanups (back_to);
1579
1580 error_reported = 1;
1581 }
1582
1583 result = NULL;
1584 }
1585 }
1586 #endif
1587
1588 return result;
1589 }
1590
1591 /* See cp-support.h. */
1592
1593 int
1594 gdb_sniff_from_mangled_name (const char *mangled, char **demangled)
1595 {
1596 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
1597 return *demangled != NULL;
1598 }
1599
1600 /* Don't allow just "maintenance cplus". */
1601
1602 static void
1603 maint_cplus_command (char *arg, int from_tty)
1604 {
1605 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1606 "by the name of a command.\n"));
1607 help_list (maint_cplus_cmd_list,
1608 "maintenance cplus ",
1609 all_commands, gdb_stdout);
1610 }
1611
1612 /* This is a front end for cp_find_first_component, for unit testing.
1613 Be careful when using it: see the NOTE above
1614 cp_find_first_component. */
1615
1616 static void
1617 first_component_command (char *arg, int from_tty)
1618 {
1619 int len;
1620 char *prefix;
1621
1622 if (!arg)
1623 return;
1624
1625 len = cp_find_first_component (arg);
1626 prefix = (char *) alloca (len + 1);
1627
1628 memcpy (prefix, arg, len);
1629 prefix[len] = '\0';
1630
1631 printf_unfiltered ("%s\n", prefix);
1632 }
1633
1634 extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
1635
1636
1637 /* Implement "info vtbl". */
1638
1639 static void
1640 info_vtbl_command (char *arg, int from_tty)
1641 {
1642 struct value *value;
1643
1644 value = parse_and_eval (arg);
1645 cplus_print_vtable (value);
1646 }
1647
1648 void
1649 _initialize_cp_support (void)
1650 {
1651 add_prefix_cmd ("cplus", class_maintenance,
1652 maint_cplus_command,
1653 _("C++ maintenance commands."),
1654 &maint_cplus_cmd_list,
1655 "maintenance cplus ",
1656 0, &maintenancelist);
1657 add_alias_cmd ("cp", "cplus",
1658 class_maintenance, 1,
1659 &maintenancelist);
1660
1661 add_cmd ("first_component",
1662 class_maintenance,
1663 first_component_command,
1664 _("Print the first class/namespace component of NAME."),
1665 &maint_cplus_cmd_list);
1666
1667 add_info ("vtbl", info_vtbl_command,
1668 _("Show the virtual function table for a C++ object.\n\
1669 Usage: info vtbl EXPRESSION\n\
1670 Evaluate EXPRESSION and display the virtual function table for the\n\
1671 resulting object."));
1672
1673 #ifdef HAVE_WORKING_FORK
1674 add_setshow_boolean_cmd ("catch-demangler-crashes", class_maintenance,
1675 &catch_demangler_crashes, _("\
1676 Set whether to attempt to catch demangler crashes."), _("\
1677 Show whether to attempt to catch demangler crashes."), _("\
1678 If enabled GDB will attempt to catch demangler crashes and\n\
1679 display the offending symbol."),
1680 NULL,
1681 NULL,
1682 &maintenance_set_cmdlist,
1683 &maintenance_show_cmdlist);
1684 #endif
1685 }