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