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