Add translations for various sub-directories
[binutils-gdb.git] / gdb / objc-lang.c
blob58aca298dbc8367230d0b8f684f0725912821a0e
1 /* Objective-C language support routines for GDB, the GNU debugger.
3 Copyright (C) 2002-2024 Free Software Foundation, Inc.
5 Contributed by Apple Computer, Inc.
6 Written by Michael Snyder.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "event-top.h"
24 #include "exceptions.h"
25 #include "symtab.h"
26 #include "gdbtypes.h"
27 #include "expression.h"
28 #include "parser-defs.h"
29 #include "language.h"
30 #include "varobj.h"
31 #include "c-lang.h"
32 #include "objc-lang.h"
33 #include "complaints.h"
34 #include "value.h"
35 #include "symfile.h"
36 #include "objfiles.h"
37 #include "target.h"
38 #include "gdbcore.h"
39 #include "cli/cli-cmds.h"
40 #include "frame.h"
41 #include "gdbsupport/gdb_regex.h"
42 #include "regcache.h"
43 #include "block.h"
44 #include "infcall.h"
45 #include "valprint.h"
46 #include "cli/cli-utils.h"
47 #include "c-exp.h"
49 #include <ctype.h>
50 #include <algorithm>
52 struct objc_object {
53 CORE_ADDR isa;
56 struct objc_class {
57 CORE_ADDR isa;
58 CORE_ADDR super_class;
59 CORE_ADDR name;
60 long version;
61 long info;
62 long instance_size;
63 CORE_ADDR ivars;
64 CORE_ADDR methods;
65 CORE_ADDR cache;
66 CORE_ADDR protocols;
69 struct objc_super {
70 CORE_ADDR receiver;
71 CORE_ADDR theclass;
74 struct objc_method {
75 CORE_ADDR name;
76 CORE_ADDR types;
77 CORE_ADDR imp;
80 static const registry<objfile>::key<unsigned int> objc_objfile_data;
82 /* Lookup a structure type named "struct NAME", visible in lexical
83 block BLOCK. If NOERR is nonzero, return zero if NAME is not
84 suitably defined. */
86 struct symbol *
87 lookup_struct_typedef (const char *name, const struct block *block, int noerr)
89 struct symbol *sym;
91 sym = lookup_symbol (name, block, SEARCH_STRUCT_DOMAIN, 0).symbol;
93 if (sym == NULL)
95 if (noerr)
96 return 0;
97 else
98 error (_("No struct type named %s."), name);
100 if (sym->type ()->code () != TYPE_CODE_STRUCT)
102 if (noerr)
103 return 0;
104 else
105 error (_("This context has class, union or enum %s, not a struct."),
106 name);
108 return sym;
111 CORE_ADDR
112 lookup_objc_class (struct gdbarch *gdbarch, const char *classname)
114 struct type *char_type = builtin_type (gdbarch)->builtin_char;
115 struct value * function, *classval;
117 if (! target_has_execution ())
119 /* Can't call into inferior to lookup class. */
120 return 0;
123 if (lookup_minimal_symbol (current_program_space,
124 "objc_lookUpClass").minsym != nullptr)
125 function = find_function_in_inferior("objc_lookUpClass", NULL);
126 else if (lookup_minimal_symbol (current_program_space,
127 "objc_lookup_class").minsym != nullptr)
128 function = find_function_in_inferior("objc_lookup_class", NULL);
129 else
131 complaint (_("no way to lookup Objective-C classes"));
132 return 0;
135 classval = value_string (classname, strlen (classname) + 1, char_type);
136 classval = value_coerce_array (classval);
137 return (CORE_ADDR) value_as_long (call_function_by_hand (function,
138 NULL,
139 classval));
142 CORE_ADDR
143 lookup_child_selector (struct gdbarch *gdbarch, const char *selname)
145 struct type *char_type = builtin_type (gdbarch)->builtin_char;
146 struct value * function, *selstring;
148 if (! target_has_execution ())
150 /* Can't call into inferior to lookup selector. */
151 return 0;
154 if (lookup_minimal_symbol (current_program_space, "sel_getUid").minsym
155 != nullptr)
156 function = find_function_in_inferior("sel_getUid", NULL);
157 else if (lookup_minimal_symbol (current_program_space,
158 "sel_get_any_uid").minsym != nullptr)
159 function = find_function_in_inferior("sel_get_any_uid", NULL);
160 else
162 complaint (_("no way to lookup Objective-C selectors"));
163 return 0;
166 selstring = value_coerce_array (value_string (selname,
167 strlen (selname) + 1,
168 char_type));
169 return value_as_long (call_function_by_hand (function, NULL, selstring));
172 struct value *
173 value_nsstring (struct gdbarch *gdbarch, const char *ptr, int len)
175 struct type *char_type = builtin_type (gdbarch)->builtin_char;
176 struct value *stringValue[3];
177 struct value *function, *nsstringValue;
178 struct symbol *sym;
179 struct type *type;
181 if (!target_has_execution ())
182 return 0; /* Can't call into inferior to create NSString. */
184 stringValue[2] = value_string(ptr, len, char_type);
185 stringValue[2] = value_coerce_array(stringValue[2]);
186 /* _NSNewStringFromCString replaces "istr" after Lantern2A. */
187 if (lookup_minimal_symbol (current_program_space,
188 "_NSNewStringFromCString").minsym != nullptr)
190 function = find_function_in_inferior("_NSNewStringFromCString", NULL);
191 nsstringValue = call_function_by_hand(function, NULL, stringValue[2]);
193 else if (lookup_minimal_symbol (current_program_space,
194 "istr").minsym != nullptr)
196 function = find_function_in_inferior("istr", NULL);
197 nsstringValue = call_function_by_hand(function, NULL, stringValue[2]);
199 else if (lookup_minimal_symbol (current_program_space,
200 "+[NSString stringWithCString:]").minsym
201 != nullptr)
203 function
204 = find_function_in_inferior("+[NSString stringWithCString:]", NULL);
205 type = builtin_type (gdbarch)->builtin_long;
207 stringValue[0] = value_from_longest
208 (type, lookup_objc_class (gdbarch, "NSString"));
209 stringValue[1] = value_from_longest
210 (type, lookup_child_selector (gdbarch, "stringWithCString:"));
211 nsstringValue = call_function_by_hand(function, NULL, stringValue);
213 else
214 error (_("NSString: internal error -- no way to create new NSString"));
216 sym = lookup_struct_typedef("NSString", 0, 1);
217 if (sym == NULL)
218 sym = lookup_struct_typedef("NXString", 0, 1);
219 if (sym == NULL)
220 type = builtin_type (gdbarch)->builtin_data_ptr;
221 else
222 type = lookup_pointer_type(sym->type ());
224 nsstringValue->deprecated_set_type (type);
225 return nsstringValue;
228 /* Class representing the Objective-C language. */
230 class objc_language : public language_defn
232 public:
233 objc_language ()
234 : language_defn (language_objc)
235 { /* Nothing. */ }
237 /* See language.h. */
239 const char *name () const override
240 { return "objective-c"; }
242 /* See language.h. */
244 const char *natural_name () const override
245 { return "Objective-C"; }
247 /* See language.h. */
249 const std::vector<const char *> &filename_extensions () const override
251 static const std::vector<const char *> extensions = { ".m" };
252 return extensions;
255 /* See language.h. */
256 void language_arch_info (struct gdbarch *gdbarch,
257 struct language_arch_info *lai) const override
259 c_language_arch_info (gdbarch, lai);
262 /* See language.h. */
263 bool sniff_from_mangled_name
264 (const char *mangled, gdb::unique_xmalloc_ptr<char> *demangled)
265 const override
267 *demangled = demangle_symbol (mangled, 0);
268 return *demangled != NULL;
271 /* See language.h. */
273 gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
274 int options) const override;
276 /* See language.h. */
278 bool can_print_type_offsets () const override
280 return true;
283 /* See language.h. */
285 void print_type (struct type *type, const char *varstring,
286 struct ui_file *stream, int show, int level,
287 const struct type_print_options *flags) const override
289 c_print_type (type, varstring, stream, show, level, la_language, flags);
292 /* See language.h. */
294 CORE_ADDR skip_trampoline (const frame_info_ptr &frame,
295 CORE_ADDR stop_pc) const override
297 struct gdbarch *gdbarch = get_frame_arch (frame);
298 CORE_ADDR real_stop_pc;
299 CORE_ADDR method_stop_pc;
301 /* Determine if we are currently in the Objective-C dispatch function.
302 If so, get the address of the method function that the dispatcher
303 would call and use that as the function to step into instead. Also
304 skip over the trampoline for the function (if any). This is better
305 for the user since they are only interested in stepping into the
306 method function anyway. */
308 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
310 if (real_stop_pc != 0)
311 find_objc_msgcall (real_stop_pc, &method_stop_pc);
312 else
313 find_objc_msgcall (stop_pc, &method_stop_pc);
315 if (method_stop_pc)
317 real_stop_pc = gdbarch_skip_trampoline_code
318 (gdbarch, frame, method_stop_pc);
319 if (real_stop_pc == 0)
320 real_stop_pc = method_stop_pc;
323 return real_stop_pc;
326 /* See language.h. */
328 const char *name_of_this () const override
329 { return "self"; }
331 /* See language.h. */
333 enum macro_expansion macro_expansion () const override
334 { return macro_expansion_c; }
337 /* See declaration of objc_language::demangle_symbol above. */
339 gdb::unique_xmalloc_ptr<char>
340 objc_language::demangle_symbol (const char *mangled, int options) const
342 char *demangled, *cp;
344 if (mangled[0] == '_'
345 && (mangled[1] == 'i' || mangled[1] == 'c')
346 && mangled[2] == '_')
348 cp = demangled = (char *) xmalloc (strlen (mangled) + 2);
350 if (mangled[1] == 'i')
351 *cp++ = '-'; /* for instance method */
352 else
353 *cp++ = '+'; /* for class method */
355 *cp++ = '['; /* opening left brace */
356 strcpy(cp, mangled+3); /* Tack on the rest of the mangled name. */
358 while (*cp != '\0' && *cp == '_')
359 cp++; /* Skip any initial underbars in class
360 name. */
362 cp = strchr(cp, '_');
363 if (cp == nullptr) /* Find first non-initial underbar. */
365 xfree(demangled); /* not mangled name */
366 return nullptr;
368 if (cp[1] == '_') /* Easy case: no category name. */
370 *cp++ = ' '; /* Replace two '_' with one ' '. */
371 strcpy(cp, mangled + (cp - demangled) + 2);
373 else
375 *cp++ = '('; /* Less easy case: category name. */
376 cp = strchr(cp, '_');
377 if (cp == nullptr)
379 xfree(demangled); /* not mangled name */
380 return nullptr;
382 *cp++ = ')';
383 *cp++ = ' '; /* Overwriting 1st char of method name... */
384 strcpy(cp, mangled + (cp - demangled)); /* Get it back. */
387 while (*cp != '\0' && *cp == '_')
388 cp++; /* Skip any initial underbars in
389 method name. */
391 for (; *cp != '\0'; cp++)
392 if (*cp == '_')
393 *cp = ':'; /* Replace remaining '_' with ':'. */
395 *cp++ = ']'; /* closing right brace */
396 *cp++ = 0; /* string terminator */
397 return gdb::unique_xmalloc_ptr<char> (demangled);
399 else
400 return nullptr; /* Not an objc mangled name. */
403 /* Single instance of the class representing the Objective-C language. */
405 static objc_language objc_language_defn;
408 * ObjC:
409 * Following functions help construct Objective-C message calls.
412 struct selname /* For parsing Objective-C. */
414 struct selname *next;
415 char *msglist_sel;
416 int msglist_len;
419 static int msglist_len;
420 static struct selname *selname_chain;
421 static char *msglist_sel;
423 void
424 start_msglist(void)
426 struct selname *newobj = XNEW (struct selname);
428 newobj->next = selname_chain;
429 newobj->msglist_len = msglist_len;
430 newobj->msglist_sel = msglist_sel;
431 msglist_len = 0;
432 msglist_sel = (char *)xmalloc(1);
433 *msglist_sel = 0;
434 selname_chain = newobj;
437 void
438 add_msglist(struct stoken *str, int addcolon)
440 char *s;
441 const char *p;
442 int len, plen;
444 if (str == 0) /* Unnamed arg, or... */
446 if (addcolon == 0) /* variable number of args. */
448 msglist_len++;
449 return;
451 p = "";
452 plen = 0;
454 else
456 p = str->ptr;
457 plen = str->length;
459 len = plen + strlen(msglist_sel) + 2;
460 s = (char *)xmalloc(len);
461 strcpy(s, msglist_sel);
462 strncat(s, p, plen);
463 xfree(msglist_sel);
464 msglist_sel = s;
465 if (addcolon)
467 s[len-2] = ':';
468 s[len-1] = 0;
469 msglist_len++;
471 else
472 s[len-2] = '\0';
476 end_msglist (struct parser_state *ps)
478 int val = msglist_len;
479 struct selname *sel = selname_chain;
480 char *p = msglist_sel;
481 CORE_ADDR selid;
483 std::vector<expr::operation_up> args = ps->pop_vector (val);
484 expr::operation_up target = ps->pop ();
486 selname_chain = sel->next;
487 msglist_len = sel->msglist_len;
488 msglist_sel = sel->msglist_sel;
489 selid = lookup_child_selector (ps->gdbarch (), p);
490 if (!selid)
491 error (_("Can't find selector \"%s\""), p);
493 ps->push_new<expr::objc_msgcall_operation> (selid, std::move (target),
494 std::move (args));
496 xfree(p);
497 xfree(sel);
499 return val;
503 * Function: specialcmp (const char *a, const char *b)
505 * Special strcmp: treats ']' and ' ' as end-of-string.
506 * Used for qsorting lists of objc methods (either by class or selector).
509 static int
510 specialcmp (const char *a, const char *b)
512 while (*a && *a != ' ' && *a != ']' && *b && *b != ' ' && *b != ']')
514 if (*a != *b)
515 return *a - *b;
516 a++, b++;
518 if (*a && *a != ' ' && *a != ']')
519 return 1; /* a is longer therefore greater. */
520 if (*b && *b != ' ' && *b != ']')
521 return -1; /* a is shorter therefore lesser. */
522 return 0; /* a and b are identical. */
526 * Function: compare_selectors (const void *, const void *)
528 * Comparison function for use with qsort. Arguments are symbols or
529 * msymbols Compares selector part of objc method name alphabetically.
532 static int
533 compare_selectors (const void *a, const void *b)
535 const char *aname, *bname;
537 aname = (*(struct symbol **) a)->print_name ();
538 bname = (*(struct symbol **) b)->print_name ();
539 if (aname == NULL || bname == NULL)
540 error (_("internal: compare_selectors(1)"));
542 aname = strchr(aname, ' ');
543 bname = strchr(bname, ' ');
544 if (aname == NULL || bname == NULL)
545 error (_("internal: compare_selectors(2)"));
547 return specialcmp (aname+1, bname+1);
551 * Function: selectors_info (regexp, from_tty)
553 * Implements the "Info selectors" command. Takes an optional regexp
554 * arg. Lists all objective c selectors that match the regexp. Works
555 * by grepping through all symbols for objective c methods. Output list
556 * is sorted and uniqued.
559 static void
560 info_selectors_command (const char *regexp, int from_tty)
562 const char *name;
563 char *val;
564 int matches = 0;
565 int maxlen = 0;
566 int ix;
567 char myregexp[2048];
568 char asel[256];
569 struct symbol **sym_arr;
570 int plusminus = 0;
572 if (regexp == NULL)
573 strcpy(myregexp, ".*]"); /* Null input, match all objc methods. */
574 else
576 if (*regexp == '+' || *regexp == '-')
577 { /* User wants only class methods or only instance methods. */
578 plusminus = *regexp++;
579 while (*regexp == ' ' || *regexp == '\t')
580 regexp++;
582 if (*regexp == '\0')
583 strcpy(myregexp, ".*]");
584 else
586 /* Allow a few extra bytes because of the strcat below. */
587 if (sizeof (myregexp) < strlen (regexp) + 4)
588 error (_("Regexp is too long: %s"), regexp);
589 strcpy(myregexp, regexp);
590 if (myregexp[strlen(myregexp) - 1] == '$') /* end of selector */
591 myregexp[strlen(myregexp) - 1] = ']'; /* end of method name */
592 else
593 strcat(myregexp, ".*]");
597 if (regexp != NULL)
599 val = re_comp (myregexp);
600 if (val != 0)
601 error (_("Invalid regexp (%s): %s"), val, regexp);
604 /* First time through is JUST to get max length and count. */
605 for (objfile *objfile : current_program_space->objfiles ())
607 for (minimal_symbol *msymbol : objfile->msymbols ())
609 QUIT;
610 name = msymbol->natural_name ();
611 if (name
612 && (name[0] == '-' || name[0] == '+')
613 && name[1] == '[') /* Got a method name. */
615 /* Filter for class/instance methods. */
616 if (plusminus && name[0] != plusminus)
617 continue;
618 /* Find selector part. */
619 name = (char *) strchr (name+2, ' ');
620 if (name == NULL)
622 complaint (_("Bad method name '%s'"),
623 msymbol->natural_name ());
624 continue;
626 if (regexp == NULL || re_exec(++name) != 0)
628 const char *mystart = name;
629 const char *myend = strchr (mystart, ']');
631 if (myend && (myend - mystart > maxlen))
632 maxlen = myend - mystart; /* Get longest selector. */
633 matches++;
638 if (matches)
640 gdb_printf (_("Selectors matching \"%s\":\n\n"),
641 regexp ? regexp : "*");
643 sym_arr = XALLOCAVEC (struct symbol *, matches);
644 matches = 0;
645 for (objfile *objfile : current_program_space->objfiles ())
647 for (minimal_symbol *msymbol : objfile->msymbols ())
649 QUIT;
650 name = msymbol->natural_name ();
651 if (name &&
652 (name[0] == '-' || name[0] == '+') &&
653 name[1] == '[') /* Got a method name. */
655 /* Filter for class/instance methods. */
656 if (plusminus && name[0] != plusminus)
657 continue;
658 /* Find selector part. */
659 name = (char *) strchr(name+2, ' ');
660 if (regexp == NULL || re_exec(++name) != 0)
661 sym_arr[matches++] = (struct symbol *) msymbol;
666 qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
667 compare_selectors);
668 /* Prevent compare on first iteration. */
669 asel[0] = 0;
670 for (ix = 0; ix < matches; ix++) /* Now do the output. */
672 char *p = asel;
674 QUIT;
675 name = sym_arr[ix]->natural_name ();
676 name = strchr (name, ' ') + 1;
677 if (p[0] && specialcmp(name, p) == 0)
678 continue; /* Seen this one already (not unique). */
680 /* Copy selector part. */
681 while (*name && *name != ']')
682 *p++ = *name++;
683 *p++ = '\0';
684 /* Print in columns. */
685 puts_tabular(asel, maxlen + 1, 0);
687 begin_line();
689 else
690 gdb_printf (_("No selectors matching \"%s\"\n"),
691 regexp ? regexp : "*");
695 * Function: compare_classes (const void *, const void *)
697 * Comparison function for use with qsort. Arguments are symbols or
698 * msymbols Compares class part of objc method name alphabetically.
701 static int
702 compare_classes (const void *a, const void *b)
704 const char *aname, *bname;
706 aname = (*(struct symbol **) a)->print_name ();
707 bname = (*(struct symbol **) b)->print_name ();
708 if (aname == NULL || bname == NULL)
709 error (_("internal: compare_classes(1)"));
711 return specialcmp (aname+1, bname+1);
715 * Function: classes_info(regexp, from_tty)
717 * Implements the "info classes" command for objective c classes.
718 * Lists all objective c classes that match the optional regexp.
719 * Works by grepping through the list of objective c methods. List will
720 * be sorted and uniqued (since one class may have many methods).
721 * BUGS: will not list a class that has no methods.
724 static void
725 info_classes_command (const char *regexp, int from_tty)
727 const char *name;
728 char *val;
729 int matches = 0;
730 int maxlen = 0;
731 int ix;
732 char myregexp[2048];
733 char aclass[256];
734 struct symbol **sym_arr;
736 if (regexp == NULL)
737 strcpy(myregexp, ".* "); /* Null input: match all objc classes. */
738 else
740 /* Allow a few extra bytes because of the strcat below. */
741 if (sizeof (myregexp) < strlen (regexp) + 4)
742 error (_("Regexp is too long: %s"), regexp);
743 strcpy(myregexp, regexp);
744 if (myregexp[strlen(myregexp) - 1] == '$')
745 /* In the method name, the end of the class name is marked by ' '. */
746 myregexp[strlen(myregexp) - 1] = ' ';
747 else
748 strcat(myregexp, ".* ");
751 if (regexp != NULL)
753 val = re_comp (myregexp);
754 if (val != 0)
755 error (_("Invalid regexp (%s): %s"), val, regexp);
758 /* First time through is JUST to get max length and count. */
759 for (objfile *objfile : current_program_space->objfiles ())
761 for (minimal_symbol *msymbol : objfile->msymbols ())
763 QUIT;
764 name = msymbol->natural_name ();
765 if (name &&
766 (name[0] == '-' || name[0] == '+') &&
767 name[1] == '[') /* Got a method name. */
768 if (regexp == NULL || re_exec(name+2) != 0)
770 /* Compute length of classname part. */
771 const char *mystart = name + 2;
772 const char *myend = strchr (mystart, ' ');
774 if (myend && (myend - mystart > maxlen))
775 maxlen = myend - mystart;
776 matches++;
780 if (matches)
782 gdb_printf (_("Classes matching \"%s\":\n\n"),
783 regexp ? regexp : "*");
784 sym_arr = XALLOCAVEC (struct symbol *, matches);
785 matches = 0;
786 for (objfile *objfile : current_program_space->objfiles ())
788 for (minimal_symbol *msymbol : objfile->msymbols ())
790 QUIT;
791 name = msymbol->natural_name ();
792 if (name &&
793 (name[0] == '-' || name[0] == '+') &&
794 name[1] == '[') /* Got a method name. */
795 if (regexp == NULL || re_exec(name+2) != 0)
796 sym_arr[matches++] = (struct symbol *) msymbol;
800 qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
801 compare_classes);
802 /* Prevent compare on first iteration. */
803 aclass[0] = 0;
804 for (ix = 0; ix < matches; ix++) /* Now do the output. */
806 char *p = aclass;
808 QUIT;
809 name = sym_arr[ix]->natural_name ();
810 name += 2;
811 if (p[0] && specialcmp(name, p) == 0)
812 continue; /* Seen this one already (not unique). */
814 /* Copy class part of method name. */
815 while (*name && *name != ' ')
816 *p++ = *name++;
817 *p++ = '\0';
818 /* Print in columns. */
819 puts_tabular(aclass, maxlen + 1, 0);
821 begin_line();
823 else
824 gdb_printf (_("No classes matching \"%s\"\n"), regexp ? regexp : "*");
827 static char *
828 parse_selector (char *method, char **selector)
830 char *s1 = NULL;
831 char *s2 = NULL;
832 int found_quote = 0;
834 char *nselector = NULL;
836 gdb_assert (selector != NULL);
838 s1 = method;
840 s1 = skip_spaces (s1);
841 if (*s1 == '\'')
843 found_quote = 1;
844 s1++;
846 s1 = skip_spaces (s1);
848 nselector = s1;
849 s2 = s1;
851 for (;;)
853 if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
854 *s1++ = *s2;
855 else if (isspace (*s2))
857 else if ((*s2 == '\0') || (*s2 == '\''))
858 break;
859 else
860 return NULL;
861 s2++;
863 *s1++ = '\0';
865 s2 = skip_spaces (s2);
866 if (found_quote)
868 if (*s2 == '\'')
869 s2++;
870 s2 = skip_spaces (s2);
873 if (selector != NULL)
874 *selector = nselector;
876 return s2;
879 static char *
880 parse_method (char *method, char *type, char **theclass,
881 char **category, char **selector)
883 char *s1 = NULL;
884 char *s2 = NULL;
885 int found_quote = 0;
887 char ntype = '\0';
888 char *nclass = NULL;
889 char *ncategory = NULL;
890 char *nselector = NULL;
892 gdb_assert (type != NULL);
893 gdb_assert (theclass != NULL);
894 gdb_assert (category != NULL);
895 gdb_assert (selector != NULL);
897 s1 = method;
899 s1 = skip_spaces (s1);
900 if (*s1 == '\'')
902 found_quote = 1;
903 s1++;
905 s1 = skip_spaces (s1);
907 if ((s1[0] == '+') || (s1[0] == '-'))
908 ntype = *s1++;
910 s1 = skip_spaces (s1);
912 if (*s1 != '[')
913 return NULL;
914 s1++;
916 nclass = s1;
917 while (isalnum (*s1) || (*s1 == '_'))
918 s1++;
920 s2 = s1;
921 s2 = skip_spaces (s2);
923 if (*s2 == '(')
925 s2++;
926 s2 = skip_spaces (s2);
927 ncategory = s2;
928 while (isalnum (*s2) || (*s2 == '_'))
929 s2++;
930 *s2++ = '\0';
933 /* Truncate the class name now that we're not using the open paren. */
934 *s1++ = '\0';
936 nselector = s2;
937 s1 = s2;
939 for (;;)
941 if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
942 *s1++ = *s2;
943 else if (isspace (*s2))
945 else if (*s2 == ']')
946 break;
947 else
948 return NULL;
949 s2++;
951 *s1++ = '\0';
952 s2++;
954 s2 = skip_spaces (s2);
955 if (found_quote)
957 if (*s2 != '\'')
958 return NULL;
959 s2++;
960 s2 = skip_spaces (s2);
963 if (type != NULL)
964 *type = ntype;
965 if (theclass != NULL)
966 *theclass = nclass;
967 if (category != NULL)
968 *category = ncategory;
969 if (selector != NULL)
970 *selector = nselector;
972 return s2;
975 static void
976 find_methods (char type, const char *theclass, const char *category,
977 const char *selector,
978 std::vector<const char *> *symbol_names)
980 const char *symname = NULL;
982 char ntype = '\0';
983 char *nclass = NULL;
984 char *ncategory = NULL;
985 char *nselector = NULL;
987 static char *tmp = NULL;
988 static unsigned int tmplen = 0;
990 gdb_assert (symbol_names != NULL);
992 for (objfile *objfile : current_program_space->objfiles ())
994 unsigned int *objc_csym;
996 /* The objfile_csym variable counts the number of ObjC methods
997 that this objfile defines. We save that count as a private
998 objfile data. If we have already determined that this objfile
999 provides no ObjC methods, we can skip it entirely. */
1001 unsigned int objfile_csym = 0;
1003 objc_csym = objc_objfile_data.get (objfile);
1004 if (objc_csym != NULL && *objc_csym == 0)
1005 /* There are no ObjC symbols in this objfile. Skip it entirely. */
1006 continue;
1008 for (minimal_symbol *msymbol : objfile->msymbols ())
1010 QUIT;
1012 /* Check the symbol name first as this can be done entirely without
1013 sending any query to the target. */
1014 symname = msymbol->natural_name ();
1015 if (symname == NULL)
1016 continue;
1018 if ((symname[0] != '-' && symname[0] != '+') || (symname[1] != '['))
1019 /* Not a method name. */
1020 continue;
1022 objfile_csym++;
1024 /* Now that thinks are a bit sane, clean up the symname. */
1025 while ((strlen (symname) + 1) >= tmplen)
1027 tmplen = (tmplen == 0) ? 1024 : tmplen * 2;
1028 tmp = (char *) xrealloc (tmp, tmplen);
1030 strcpy (tmp, symname);
1032 if (parse_method (tmp, &ntype, &nclass,
1033 &ncategory, &nselector) == NULL)
1034 continue;
1036 if ((type != '\0') && (ntype != type))
1037 continue;
1039 if ((theclass != NULL)
1040 && ((nclass == NULL) || (strcmp (theclass, nclass) != 0)))
1041 continue;
1043 if ((category != NULL) &&
1044 ((ncategory == NULL) || (strcmp (category, ncategory) != 0)))
1045 continue;
1047 if ((selector != NULL) &&
1048 ((nselector == NULL) || (strcmp (selector, nselector) != 0)))
1049 continue;
1051 symbol_names->push_back (symname);
1054 if (objc_csym == NULL)
1055 objc_csym = objc_objfile_data.emplace (objfile, objfile_csym);
1056 else
1057 /* Count of ObjC methods in this objfile should be constant. */
1058 gdb_assert (*objc_csym == objfile_csym);
1062 /* Uniquify a vector of strings. */
1064 static void
1065 uniquify_strings (std::vector<const char *> *strings)
1067 if (strings->empty ())
1068 return;
1070 std::sort (strings->begin (), strings->end (), compare_cstrings);
1071 strings->erase (std::unique (strings->begin (), strings->end (), streq),
1072 strings->end ());
1076 * Function: find_imps (const char *selector, struct symbol **sym_arr)
1078 * Input: a string representing a selector
1079 * a pointer to an array of symbol pointers
1080 * possibly a pointer to a symbol found by the caller.
1082 * Output: number of methods that implement that selector. Side
1083 * effects: The array of symbol pointers is filled with matching syms.
1085 * By analogy with function "find_methods" (symtab.c), builds a list
1086 * of symbols matching the ambiguous input, so that "decode_line_2"
1087 * (symtab.c) can list them and ask the user to choose one or more.
1088 * In this case the matches are objective c methods
1089 * ("implementations") matching an objective c selector.
1091 * Note that it is possible for a normal (c-style) function to have
1092 * the same name as an objective c selector. To prevent the selector
1093 * from eclipsing the function, we allow the caller (decode_line_1) to
1094 * search for such a function first, and if it finds one, pass it in
1095 * to us. We will then integrate it into the list. We also search
1096 * for one here, among the minsyms.
1098 * NOTE: if NUM_DEBUGGABLE is non-zero, the sym_arr will be divided
1099 * into two parts: debuggable (struct symbol) syms, and
1100 * non_debuggable (struct minimal_symbol) syms. The debuggable
1101 * ones will come first, before NUM_DEBUGGABLE (which will thus
1102 * be the index of the first non-debuggable one).
1105 const char *
1106 find_imps (const char *method, std::vector<const char *> *symbol_names)
1108 char type = '\0';
1109 char *theclass = NULL;
1110 char *category = NULL;
1111 char *selector = NULL;
1113 char *buf = NULL;
1114 char *tmp = NULL;
1116 int selector_case = 0;
1118 gdb_assert (symbol_names != NULL);
1120 buf = (char *) alloca (strlen (method) + 1);
1121 strcpy (buf, method);
1122 tmp = parse_method (buf, &type, &theclass, &category, &selector);
1124 if (tmp == NULL)
1126 strcpy (buf, method);
1127 tmp = parse_selector (buf, &selector);
1129 if (tmp == NULL)
1130 return NULL;
1132 selector_case = 1;
1135 find_methods (type, theclass, category, selector, symbol_names);
1137 /* If we hit the "selector" case, and we found some methods, then
1138 add the selector itself as a symbol, if it exists. */
1139 if (selector_case && !symbol_names->empty ())
1141 struct symbol *sym = lookup_symbol (selector, NULL, SEARCH_VFT,
1142 0).symbol;
1144 if (sym != NULL)
1145 symbol_names->push_back (sym->natural_name ());
1146 else
1148 bound_minimal_symbol msym
1149 = lookup_minimal_symbol (current_program_space, selector);
1151 if (msym.minsym != NULL)
1152 symbol_names->push_back (msym.minsym->natural_name ());
1156 uniquify_strings (symbol_names);
1158 return method + (tmp - buf);
1161 static void
1162 print_object_command (const char *args, int from_tty)
1164 struct value *object, *function, *description;
1165 CORE_ADDR string_addr, object_addr;
1166 int i = 0;
1167 gdb_byte c = 0;
1169 if (!args || !*args)
1170 error (
1171 "The 'print-object' command requires an argument (an Objective-C object)");
1174 expression_up expr = parse_expression (args);
1176 object = expr->evaluate (builtin_type (expr->gdbarch)->builtin_data_ptr);
1179 /* Validate the address for sanity. */
1180 object_addr = value_as_long (object);
1181 read_memory (object_addr, &c, 1);
1183 function = find_function_in_inferior ("_NSPrintForDebugger", NULL);
1184 if (function == NULL)
1185 error (_("Unable to locate _NSPrintForDebugger in child process"));
1187 description = call_function_by_hand (function, NULL, object);
1189 string_addr = value_as_long (description);
1190 if (string_addr == 0)
1191 error (_("object returns null description"));
1193 read_memory (string_addr + i++, &c, 1);
1194 if (c != 0)
1196 { /* Read and print characters up to EOS. */
1197 QUIT;
1198 gdb_printf ("%c", c);
1199 read_memory (string_addr + i++, &c, 1);
1200 } while (c != 0);
1201 else
1202 gdb_printf(_("<object returns empty description>"));
1203 gdb_printf ("\n");
1206 /* The data structure 'methcalls' is used to detect method calls (through
1207 * ObjC runtime lib functions objc_msgSend, objc_msgSendSuper, etc.),
1208 * and ultimately find the method being called.
1211 struct objc_methcall {
1212 const char *name;
1213 /* Return instance method to be called. */
1214 int (*stop_at) (CORE_ADDR, CORE_ADDR *);
1215 /* Start of pc range corresponding to method invocation. */
1216 CORE_ADDR begin;
1217 /* End of pc range corresponding to method invocation. */
1218 CORE_ADDR end;
1221 static int resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc);
1222 static int resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
1223 static int resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc);
1224 static int resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
1226 static struct objc_methcall methcalls[] = {
1227 { "_objc_msgSend", resolve_msgsend, 0, 0},
1228 { "_objc_msgSend_stret", resolve_msgsend_stret, 0, 0},
1229 { "_objc_msgSendSuper", resolve_msgsend_super, 0, 0},
1230 { "_objc_msgSendSuper_stret", resolve_msgsend_super_stret, 0, 0},
1231 { "_objc_getClass", NULL, 0, 0},
1232 { "_objc_getMetaClass", NULL, 0, 0}
1235 #define nmethcalls (sizeof (methcalls) / sizeof (methcalls[0]))
1237 /* The following function, "find_objc_msgsend", fills in the data
1238 * structure "objc_msgs" by finding the addresses of each of the
1239 * (currently four) functions that it holds (of which objc_msgSend is
1240 * the first). This must be called each time symbols are loaded, in
1241 * case the functions have moved for some reason.
1244 static void
1245 find_objc_msgsend (void)
1247 unsigned int i;
1249 for (i = 0; i < nmethcalls; i++)
1251 /* Try both with and without underscore. */
1252 bound_minimal_symbol func
1253 = lookup_minimal_symbol (current_program_space, methcalls[i].name);
1254 if ((func.minsym == NULL) && (methcalls[i].name[0] == '_'))
1256 func = lookup_minimal_symbol (current_program_space,
1257 methcalls[i].name + 1);
1259 if (func.minsym == NULL)
1261 methcalls[i].begin = 0;
1262 methcalls[i].end = 0;
1263 continue;
1266 methcalls[i].begin = func.value_address ();
1267 methcalls[i].end = minimal_symbol_upper_bound (func);
1271 /* find_objc_msgcall (replaces pc_off_limits)
1273 * ALL that this function now does is to determine whether the input
1274 * address ("pc") is the address of one of the Objective-C message
1275 * dispatch functions (mainly objc_msgSend or objc_msgSendSuper), and
1276 * if so, it returns the address of the method that will be called.
1278 * The old function "pc_off_limits" used to do a lot of other things
1279 * in addition, such as detecting shared library jump stubs and
1280 * returning the address of the shlib function that would be called.
1281 * That functionality has been moved into the gdbarch_skip_trampoline_code and
1282 * IN_SOLIB_TRAMPOLINE macros, which are resolved in the target-
1283 * dependent modules.
1286 static int
1287 find_objc_msgcall_submethod (int (*f) (CORE_ADDR, CORE_ADDR *),
1288 CORE_ADDR pc,
1289 CORE_ADDR *new_pc)
1293 if (f (pc, new_pc) == 0)
1294 return 1;
1296 catch (const gdb_exception_error &ex)
1298 exception_fprintf (gdb_stderr, ex,
1299 "Unable to determine target of "
1300 "Objective-C method call (ignoring):\n");
1303 return 0;
1306 int
1307 find_objc_msgcall (CORE_ADDR pc, CORE_ADDR *new_pc)
1309 unsigned int i;
1311 find_objc_msgsend ();
1312 if (new_pc != NULL)
1314 *new_pc = 0;
1317 for (i = 0; i < nmethcalls; i++)
1318 if ((pc >= methcalls[i].begin) && (pc < methcalls[i].end))
1320 if (methcalls[i].stop_at != NULL)
1321 return find_objc_msgcall_submethod (methcalls[i].stop_at,
1322 pc, new_pc);
1323 else
1324 return 0;
1327 return 0;
1330 void _initialize_objc_language ();
1331 void
1332 _initialize_objc_language ()
1334 add_info ("selectors", info_selectors_command,
1335 _("All Objective-C selectors, or those matching REGEXP."));
1336 add_info ("classes", info_classes_command,
1337 _("All Objective-C classes, or those matching REGEXP."));
1338 cmd_list_element *print_object_cmd
1339 = add_com ("print-object", class_vars, print_object_command,
1340 _("Ask an Objective-C object to print itself."));
1341 add_com_alias ("po", print_object_cmd, class_vars, 1);
1344 static void
1345 read_objc_method (struct gdbarch *gdbarch, CORE_ADDR addr,
1346 struct objc_method *method)
1348 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1350 method->name = read_memory_unsigned_integer (addr + 0, 4, byte_order);
1351 method->types = read_memory_unsigned_integer (addr + 4, 4, byte_order);
1352 method->imp = read_memory_unsigned_integer (addr + 8, 4, byte_order);
1355 static unsigned long
1356 read_objc_methlist_nmethods (struct gdbarch *gdbarch, CORE_ADDR addr)
1358 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1360 return read_memory_unsigned_integer (addr + 4, 4, byte_order);
1363 static void
1364 read_objc_methlist_method (struct gdbarch *gdbarch, CORE_ADDR addr,
1365 unsigned long num, struct objc_method *method)
1367 gdb_assert (num < read_objc_methlist_nmethods (gdbarch, addr));
1368 read_objc_method (gdbarch, addr + 8 + (12 * num), method);
1371 static void
1372 read_objc_object (struct gdbarch *gdbarch, CORE_ADDR addr,
1373 struct objc_object *object)
1375 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1377 object->isa = read_memory_unsigned_integer (addr, 4, byte_order);
1380 static void
1381 read_objc_super (struct gdbarch *gdbarch, CORE_ADDR addr,
1382 struct objc_super *super)
1384 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1386 super->receiver = read_memory_unsigned_integer (addr, 4, byte_order);
1387 super->theclass = read_memory_unsigned_integer (addr + 4, 4, byte_order);
1390 static void
1391 read_objc_class (struct gdbarch *gdbarch, CORE_ADDR addr,
1392 struct objc_class *theclass)
1394 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1396 theclass->isa = read_memory_unsigned_integer (addr, 4, byte_order);
1397 theclass->super_class = read_memory_unsigned_integer (addr + 4, 4, byte_order);
1398 theclass->name = read_memory_unsigned_integer (addr + 8, 4, byte_order);
1399 theclass->version = read_memory_unsigned_integer (addr + 12, 4, byte_order);
1400 theclass->info = read_memory_unsigned_integer (addr + 16, 4, byte_order);
1401 theclass->instance_size = read_memory_unsigned_integer (addr + 18, 4,
1402 byte_order);
1403 theclass->ivars = read_memory_unsigned_integer (addr + 24, 4, byte_order);
1404 theclass->methods = read_memory_unsigned_integer (addr + 28, 4, byte_order);
1405 theclass->cache = read_memory_unsigned_integer (addr + 32, 4, byte_order);
1406 theclass->protocols = read_memory_unsigned_integer (addr + 36, 4, byte_order);
1409 static CORE_ADDR
1410 find_implementation_from_class (struct gdbarch *gdbarch,
1411 CORE_ADDR theclass, CORE_ADDR sel)
1413 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1414 CORE_ADDR subclass = theclass;
1416 while (subclass != 0)
1419 struct objc_class class_str;
1420 unsigned mlistnum = 0;
1422 read_objc_class (gdbarch, subclass, &class_str);
1424 for (;;)
1426 CORE_ADDR mlist;
1427 unsigned long nmethods;
1428 unsigned long i;
1430 mlist = read_memory_unsigned_integer (class_str.methods +
1431 (4 * mlistnum),
1432 4, byte_order);
1433 if (mlist == 0)
1434 break;
1436 nmethods = read_objc_methlist_nmethods (gdbarch, mlist);
1438 for (i = 0; i < nmethods; i++)
1440 struct objc_method meth_str;
1442 read_objc_methlist_method (gdbarch, mlist, i, &meth_str);
1444 if (meth_str.name == sel)
1445 /* FIXME: hppa arch was doing a pointer dereference
1446 here. There needs to be a better way to do that. */
1447 return meth_str.imp;
1449 mlistnum++;
1451 subclass = class_str.super_class;
1454 return 0;
1457 static CORE_ADDR
1458 find_implementation (struct gdbarch *gdbarch,
1459 CORE_ADDR object, CORE_ADDR sel)
1461 struct objc_object ostr;
1463 if (object == 0)
1464 return 0;
1465 read_objc_object (gdbarch, object, &ostr);
1466 if (ostr.isa == 0)
1467 return 0;
1469 return find_implementation_from_class (gdbarch, ostr.isa, sel);
1472 static int
1473 resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc)
1475 frame_info_ptr frame = get_current_frame ();
1476 struct gdbarch *gdbarch = get_frame_arch (frame);
1477 struct type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;
1479 CORE_ADDR object;
1480 CORE_ADDR sel;
1481 CORE_ADDR res;
1483 object = gdbarch_fetch_pointer_argument (gdbarch, frame, 0, ptr_type);
1484 sel = gdbarch_fetch_pointer_argument (gdbarch, frame, 1, ptr_type);
1486 res = find_implementation (gdbarch, object, sel);
1487 if (new_pc != 0)
1488 *new_pc = res;
1489 if (res == 0)
1490 return 1;
1491 return 0;
1494 static int
1495 resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
1497 frame_info_ptr frame = get_current_frame ();
1498 struct gdbarch *gdbarch = get_frame_arch (frame);
1499 struct type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;
1501 CORE_ADDR object;
1502 CORE_ADDR sel;
1503 CORE_ADDR res;
1505 object = gdbarch_fetch_pointer_argument (gdbarch, frame, 1, ptr_type);
1506 sel = gdbarch_fetch_pointer_argument (gdbarch, frame, 2, ptr_type);
1508 res = find_implementation (gdbarch, object, sel);
1509 if (new_pc != 0)
1510 *new_pc = res;
1511 if (res == 0)
1512 return 1;
1513 return 0;
1516 static int
1517 resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc)
1519 frame_info_ptr frame = get_current_frame ();
1520 struct gdbarch *gdbarch = get_frame_arch (frame);
1521 struct type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;
1523 struct objc_super sstr;
1525 CORE_ADDR super;
1526 CORE_ADDR sel;
1527 CORE_ADDR res;
1529 super = gdbarch_fetch_pointer_argument (gdbarch, frame, 0, ptr_type);
1530 sel = gdbarch_fetch_pointer_argument (gdbarch, frame, 1, ptr_type);
1532 read_objc_super (gdbarch, super, &sstr);
1533 if (sstr.theclass == 0)
1534 return 0;
1536 res = find_implementation_from_class (gdbarch, sstr.theclass, sel);
1537 if (new_pc != 0)
1538 *new_pc = res;
1539 if (res == 0)
1540 return 1;
1541 return 0;
1544 static int
1545 resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
1547 frame_info_ptr frame = get_current_frame ();
1548 struct gdbarch *gdbarch = get_frame_arch (frame);
1549 struct type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;
1551 struct objc_super sstr;
1553 CORE_ADDR super;
1554 CORE_ADDR sel;
1555 CORE_ADDR res;
1557 super = gdbarch_fetch_pointer_argument (gdbarch, frame, 1, ptr_type);
1558 sel = gdbarch_fetch_pointer_argument (gdbarch, frame, 2, ptr_type);
1560 read_objc_super (gdbarch, super, &sstr);
1561 if (sstr.theclass == 0)
1562 return 0;
1564 res = find_implementation_from_class (gdbarch, sstr.theclass, sel);
1565 if (new_pc != 0)
1566 *new_pc = res;
1567 if (res == 0)
1568 return 1;
1569 return 0;