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