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"
27 #include "expression.h"
28 #include "parser-defs.h"
32 #include "objc-lang.h"
33 #include "complaints.h"
39 #include "cli/cli-cmds.h"
41 #include "gdbsupport/gdb_regex.h"
46 #include "cli/cli-utils.h"
58 CORE_ADDR super_class
;
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
87 lookup_struct_typedef (const char *name
, const struct block
*block
, int noerr
)
91 sym
= lookup_symbol (name
, block
, SEARCH_STRUCT_DOMAIN
, 0).symbol
;
98 error (_("No struct type named %s."), name
);
100 if (sym
->type ()->code () != TYPE_CODE_STRUCT
)
105 error (_("This context has class, union or enum %s, not a struct."),
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. */
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
);
131 complaint (_("no way to lookup Objective-C classes"));
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
,
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. */
154 if (lookup_minimal_symbol (current_program_space
, "sel_getUid").minsym
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
);
162 complaint (_("no way to lookup Objective-C selectors"));
166 selstring
= value_coerce_array (value_string (selname
,
167 strlen (selname
) + 1,
169 return value_as_long (call_function_by_hand (function
, NULL
, selstring
));
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
;
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
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
);
214 error (_("NSString: internal error -- no way to create new NSString"));
216 sym
= lookup_struct_typedef("NSString", 0, 1);
218 sym
= lookup_struct_typedef("NXString", 0, 1);
220 type
= builtin_type (gdbarch
)->builtin_data_ptr
;
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
234 : language_defn (language_objc
)
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" };
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
)
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
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
);
313 find_objc_msgcall (stop_pc
, &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
;
326 /* See language.h. */
328 const char *name_of_this () const override
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 */
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
362 cp
= strchr(cp
, '_');
363 if (cp
== nullptr) /* Find first non-initial underbar. */
365 xfree(demangled
); /* not mangled name */
368 if (cp
[1] == '_') /* Easy case: no category name. */
370 *cp
++ = ' '; /* Replace two '_' with one ' '. */
371 strcpy(cp
, mangled
+ (cp
- demangled
) + 2);
375 *cp
++ = '('; /* Less easy case: category name. */
376 cp
= strchr(cp
, '_');
379 xfree(demangled
); /* not mangled name */
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
391 for (; *cp
!= '\0'; cp
++)
393 *cp
= ':'; /* Replace remaining '_' with ':'. */
395 *cp
++ = ']'; /* closing right brace */
396 *cp
++ = 0; /* string terminator */
397 return gdb::unique_xmalloc_ptr
<char> (demangled
);
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
;
409 * Following functions help construct Objective-C message calls.
412 struct selname
/* For parsing Objective-C. */
414 struct selname
*next
;
419 static int msglist_len
;
420 static struct selname
*selname_chain
;
421 static char *msglist_sel
;
426 struct selname
*newobj
= XNEW (struct selname
);
428 newobj
->next
= selname_chain
;
429 newobj
->msglist_len
= msglist_len
;
430 newobj
->msglist_sel
= msglist_sel
;
432 msglist_sel
= (char *)xmalloc(1);
434 selname_chain
= newobj
;
438 add_msglist(struct stoken
*str
, int addcolon
)
444 if (str
== 0) /* Unnamed arg, or... */
446 if (addcolon
== 0) /* variable number of args. */
459 len
= plen
+ strlen(msglist_sel
) + 2;
460 s
= (char *)xmalloc(len
);
461 strcpy(s
, msglist_sel
);
476 end_msglist (struct parser_state
*ps
)
478 int val
= msglist_len
;
479 struct selname
*sel
= selname_chain
;
480 char *p
= msglist_sel
;
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
);
491 error (_("Can't find selector \"%s\""), p
);
493 ps
->push_new
<expr::objc_msgcall_operation
> (selid
, std::move (target
),
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).
510 specialcmp (const char *a
, const char *b
)
512 while (*a
&& *a
!= ' ' && *a
!= ']' && *b
&& *b
!= ' ' && *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.
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.
560 info_selectors_command (const char *regexp
, int from_tty
)
569 struct symbol
**sym_arr
;
573 strcpy(myregexp
, ".*]"); /* Null input, match all objc methods. */
576 if (*regexp
== '+' || *regexp
== '-')
577 { /* User wants only class methods or only instance methods. */
578 plusminus
= *regexp
++;
579 while (*regexp
== ' ' || *regexp
== '\t')
583 strcpy(myregexp
, ".*]");
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 */
593 strcat(myregexp
, ".*]");
599 val
= re_comp (myregexp
);
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 ())
610 name
= msymbol
->natural_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
)
618 /* Find selector part. */
619 name
= (char *) strchr (name
+2, ' ');
622 complaint (_("Bad method name '%s'"),
623 msymbol
->natural_name ());
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. */
640 gdb_printf (_("Selectors matching \"%s\":\n\n"),
641 regexp
? regexp
: "*");
643 sym_arr
= XALLOCAVEC (struct symbol
*, matches
);
645 for (objfile
*objfile
: current_program_space
->objfiles ())
647 for (minimal_symbol
*msymbol
: objfile
->msymbols ())
650 name
= msymbol
->natural_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
)
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
*),
668 /* Prevent compare on first iteration. */
670 for (ix
= 0; ix
< matches
; ix
++) /* Now do the output. */
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
!= ']')
684 /* Print in columns. */
685 puts_tabular(asel
, maxlen
+ 1, 0);
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.
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.
725 info_classes_command (const char *regexp
, int from_tty
)
734 struct symbol
**sym_arr
;
737 strcpy(myregexp
, ".* "); /* Null input: match all objc classes. */
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] = ' ';
748 strcat(myregexp
, ".* ");
753 val
= re_comp (myregexp
);
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 ())
764 name
= msymbol
->natural_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
;
782 gdb_printf (_("Classes matching \"%s\":\n\n"),
783 regexp
? regexp
: "*");
784 sym_arr
= XALLOCAVEC (struct symbol
*, matches
);
786 for (objfile
*objfile
: current_program_space
->objfiles ())
788 for (minimal_symbol
*msymbol
: objfile
->msymbols ())
791 name
= msymbol
->natural_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
*),
802 /* Prevent compare on first iteration. */
804 for (ix
= 0; ix
< matches
; ix
++) /* Now do the output. */
809 name
= sym_arr
[ix
]->natural_name ();
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
!= ' ')
818 /* Print in columns. */
819 puts_tabular(aclass
, maxlen
+ 1, 0);
824 gdb_printf (_("No classes matching \"%s\"\n"), regexp
? regexp
: "*");
828 parse_selector (char *method
, char **selector
)
834 char *nselector
= NULL
;
836 gdb_assert (selector
!= NULL
);
840 s1
= skip_spaces (s1
);
846 s1
= skip_spaces (s1
);
853 if (isalnum (*s2
) || (*s2
== '_') || (*s2
== ':'))
855 else if (isspace (*s2
))
857 else if ((*s2
== '\0') || (*s2
== '\''))
865 s2
= skip_spaces (s2
);
870 s2
= skip_spaces (s2
);
873 if (selector
!= NULL
)
874 *selector
= nselector
;
880 parse_method (char *method
, char *type
, char **theclass
,
881 char **category
, char **selector
)
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
);
899 s1
= skip_spaces (s1
);
905 s1
= skip_spaces (s1
);
907 if ((s1
[0] == '+') || (s1
[0] == '-'))
910 s1
= skip_spaces (s1
);
917 while (isalnum (*s1
) || (*s1
== '_'))
921 s2
= skip_spaces (s2
);
926 s2
= skip_spaces (s2
);
928 while (isalnum (*s2
) || (*s2
== '_'))
933 /* Truncate the class name now that we're not using the open paren. */
941 if (isalnum (*s2
) || (*s2
== '_') || (*s2
== ':'))
943 else if (isspace (*s2
))
954 s2
= skip_spaces (s2
);
960 s2
= skip_spaces (s2
);
965 if (theclass
!= NULL
)
967 if (category
!= NULL
)
968 *category
= ncategory
;
969 if (selector
!= NULL
)
970 *selector
= nselector
;
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
;
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. */
1008 for (minimal_symbol
*msymbol
: objfile
->msymbols ())
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
)
1018 if ((symname
[0] != '-' && symname
[0] != '+') || (symname
[1] != '['))
1019 /* Not a method name. */
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
)
1036 if ((type
!= '\0') && (ntype
!= type
))
1039 if ((theclass
!= NULL
)
1040 && ((nclass
== NULL
) || (strcmp (theclass
, nclass
) != 0)))
1043 if ((category
!= NULL
) &&
1044 ((ncategory
== NULL
) || (strcmp (category
, ncategory
) != 0)))
1047 if ((selector
!= NULL
) &&
1048 ((nselector
== NULL
) || (strcmp (selector
, nselector
) != 0)))
1051 symbol_names
->push_back (symname
);
1054 if (objc_csym
== NULL
)
1055 objc_csym
= objc_objfile_data
.emplace (objfile
, objfile_csym
);
1057 /* Count of ObjC methods in this objfile should be constant. */
1058 gdb_assert (*objc_csym
== objfile_csym
);
1062 /* Uniquify a vector of strings. */
1065 uniquify_strings (std::vector
<const char *> *strings
)
1067 if (strings
->empty ())
1070 std::sort (strings
->begin (), strings
->end (), compare_cstrings
);
1071 strings
->erase (std::unique (strings
->begin (), strings
->end (), streq
),
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).
1106 find_imps (const char *method
, std::vector
<const char *> *symbol_names
)
1109 char *theclass
= NULL
;
1110 char *category
= NULL
;
1111 char *selector
= 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
);
1126 strcpy (buf
, method
);
1127 tmp
= parse_selector (buf
, &selector
);
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
,
1145 symbol_names
->push_back (sym
->natural_name ());
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
);
1162 print_object_command (const char *args
, int from_tty
)
1164 struct value
*object
, *function
, *description
;
1165 CORE_ADDR string_addr
, object_addr
;
1169 if (!args
|| !*args
)
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);
1196 { /* Read and print characters up to EOS. */
1198 gdb_printf ("%c", c
);
1199 read_memory (string_addr
+ i
++, &c
, 1);
1202 gdb_printf(_("<object returns empty description>"));
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
{
1213 /* Return instance method to be called. */
1214 int (*stop_at
) (CORE_ADDR
, CORE_ADDR
*);
1215 /* Start of pc range corresponding to method invocation. */
1217 /* End of pc range corresponding to method invocation. */
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.
1245 find_objc_msgsend (void)
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;
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.
1287 find_objc_msgcall_submethod (int (*f
) (CORE_ADDR
, CORE_ADDR
*),
1293 if (f (pc
, new_pc
) == 0)
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");
1307 find_objc_msgcall (CORE_ADDR pc
, CORE_ADDR
*new_pc
)
1311 find_objc_msgsend ();
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
,
1330 void _initialize_objc_language ();
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);
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
);
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
);
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
);
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
);
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,
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
);
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
);
1427 unsigned long nmethods
;
1430 mlist
= read_memory_unsigned_integer (class_str
.methods
+
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
;
1451 subclass
= class_str
.super_class
;
1458 find_implementation (struct gdbarch
*gdbarch
,
1459 CORE_ADDR object
, CORE_ADDR sel
)
1461 struct objc_object ostr
;
1465 read_objc_object (gdbarch
, object
, &ostr
);
1469 return find_implementation_from_class (gdbarch
, ostr
.isa
, sel
);
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
;
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
);
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
;
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
);
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
;
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)
1536 res
= find_implementation_from_class (gdbarch
, sstr
.theclass
, sel
);
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
;
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)
1564 res
= find_implementation_from_class (gdbarch
, sstr
.theclass
, sel
);