1 /* Objective-C language support routines for GDB, the GNU debugger.
3 Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008
4 Free Software Foundation, Inc.
6 Contributed by Apple Computer, Inc.
7 Written by Michael Snyder.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "expression.h"
28 #include "parser-defs.h"
31 #include "objc-lang.h"
32 #include "exceptions.h"
33 #include "complaints.h"
37 #include "gdb_string.h" /* for strchr */
38 #include "target.h" /* for target_has_execution */
42 #include "gdb_regex.h"
47 #include "gdb_assert.h"
57 CORE_ADDR super_class
;
79 /* Lookup a structure type named "struct NAME", visible in lexical
80 block BLOCK. If NOERR is nonzero, return zero if NAME is not
84 lookup_struct_typedef (char *name
, struct block
*block
, int noerr
)
88 sym
= lookup_symbol (name
, block
, STRUCT_DOMAIN
, 0,
89 (struct symtab
**) NULL
);
96 error (_("No struct type named %s."), name
);
98 if (TYPE_CODE (SYMBOL_TYPE (sym
)) != TYPE_CODE_STRUCT
)
103 error (_("This context has class, union or enum %s, not a struct."),
110 lookup_objc_class (char *classname
)
112 struct value
* function
, *classval
;
114 if (! target_has_execution
)
116 /* Can't call into inferior to lookup class. */
120 if (lookup_minimal_symbol("objc_lookUpClass", 0, 0))
121 function
= find_function_in_inferior("objc_lookUpClass");
122 else if (lookup_minimal_symbol ("objc_lookup_class", 0, 0))
123 function
= find_function_in_inferior("objc_lookup_class");
126 complaint (&symfile_complaints
, _("no way to lookup Objective-C classes"));
130 classval
= value_string (classname
, strlen (classname
) + 1);
131 classval
= value_coerce_array (classval
);
132 return (CORE_ADDR
) value_as_long (call_function_by_hand (function
,
137 lookup_child_selector (char *selname
)
139 struct value
* function
, *selstring
;
141 if (! target_has_execution
)
143 /* Can't call into inferior to lookup selector. */
147 if (lookup_minimal_symbol("sel_getUid", 0, 0))
148 function
= find_function_in_inferior("sel_getUid");
149 else if (lookup_minimal_symbol ("sel_get_any_uid", 0, 0))
150 function
= find_function_in_inferior("sel_get_any_uid");
153 complaint (&symfile_complaints
, _("no way to lookup Objective-C selectors"));
157 selstring
= value_coerce_array (value_string (selname
,
158 strlen (selname
) + 1));
159 return value_as_long (call_function_by_hand (function
, 1, &selstring
));
163 value_nsstring (char *ptr
, int len
)
165 struct value
*stringValue
[3];
166 struct value
*function
, *nsstringValue
;
170 if (!target_has_execution
)
171 return 0; /* Can't call into inferior to create NSString. */
173 sym
= lookup_struct_typedef("NSString", 0, 1);
175 sym
= lookup_struct_typedef("NXString", 0, 1);
177 type
= lookup_pointer_type(builtin_type_void
);
179 type
= lookup_pointer_type(SYMBOL_TYPE (sym
));
181 stringValue
[2] = value_string(ptr
, len
);
182 stringValue
[2] = value_coerce_array(stringValue
[2]);
183 /* _NSNewStringFromCString replaces "istr" after Lantern2A. */
184 if (lookup_minimal_symbol("_NSNewStringFromCString", 0, 0))
186 function
= find_function_in_inferior("_NSNewStringFromCString");
187 nsstringValue
= call_function_by_hand(function
, 1, &stringValue
[2]);
189 else if (lookup_minimal_symbol("istr", 0, 0))
191 function
= find_function_in_inferior("istr");
192 nsstringValue
= call_function_by_hand(function
, 1, &stringValue
[2]);
194 else if (lookup_minimal_symbol("+[NSString stringWithCString:]", 0, 0))
196 function
= find_function_in_inferior("+[NSString stringWithCString:]");
197 stringValue
[0] = value_from_longest
198 (builtin_type_long
, lookup_objc_class ("NSString"));
199 stringValue
[1] = value_from_longest
200 (builtin_type_long
, lookup_child_selector ("stringWithCString:"));
201 nsstringValue
= call_function_by_hand(function
, 3, &stringValue
[0]);
204 error (_("NSString: internal error -- no way to create new NSString"));
206 deprecated_set_value_type (nsstringValue
, type
);
207 return nsstringValue
;
210 /* Objective-C name demangling. */
213 objc_demangle (const char *mangled
, int options
)
215 char *demangled
, *cp
;
217 if (mangled
[0] == '_' &&
218 (mangled
[1] == 'i' || mangled
[1] == 'c') &&
221 cp
= demangled
= xmalloc(strlen(mangled
) + 2);
223 if (mangled
[1] == 'i')
224 *cp
++ = '-'; /* for instance method */
226 *cp
++ = '+'; /* for class method */
228 *cp
++ = '['; /* opening left brace */
229 strcpy(cp
, mangled
+3); /* tack on the rest of the mangled name */
231 while (*cp
&& *cp
== '_')
232 cp
++; /* skip any initial underbars in class name */
234 cp
= strchr(cp
, '_');
235 if (!cp
) /* find first non-initial underbar */
237 xfree(demangled
); /* not mangled name */
240 if (cp
[1] == '_') { /* easy case: no category name */
241 *cp
++ = ' '; /* replace two '_' with one ' ' */
242 strcpy(cp
, mangled
+ (cp
- demangled
) + 2);
245 *cp
++ = '('; /* less easy case: category name */
246 cp
= strchr(cp
, '_');
249 xfree(demangled
); /* not mangled name */
253 *cp
++ = ' '; /* overwriting 1st char of method name... */
254 strcpy(cp
, mangled
+ (cp
- demangled
)); /* get it back */
257 while (*cp
&& *cp
== '_')
258 cp
++; /* skip any initial underbars in method name */
262 *cp
= ':'; /* replace remaining '_' with ':' */
264 *cp
++ = ']'; /* closing right brace */
265 *cp
++ = 0; /* string terminator */
269 return NULL
; /* Not an objc mangled name. */
272 /* Print the character C on STREAM as part of the contents of a
273 literal string whose delimiter is QUOTER. Note that that format
274 for printing characters and strings is language specific. */
277 objc_emit_char (int c
, struct ui_file
*stream
, int quoter
)
280 c
&= 0xFF; /* Avoid sign bit follies. */
282 if (PRINT_LITERAL_FORM (c
))
284 if (c
== '\\' || c
== quoter
)
286 fputs_filtered ("\\", stream
);
288 fprintf_filtered (stream
, "%c", c
);
295 fputs_filtered ("\\n", stream
);
298 fputs_filtered ("\\b", stream
);
301 fputs_filtered ("\\t", stream
);
304 fputs_filtered ("\\f", stream
);
307 fputs_filtered ("\\r", stream
);
310 fputs_filtered ("\\e", stream
);
313 fputs_filtered ("\\a", stream
);
316 fprintf_filtered (stream
, "\\%.3o", (unsigned int) c
);
323 objc_printchar (int c
, struct ui_file
*stream
)
325 fputs_filtered ("'", stream
);
326 objc_emit_char (c
, stream
, '\'');
327 fputs_filtered ("'", stream
);
330 /* Print the character string STRING, printing at most LENGTH
331 characters. Printing stops early if the number hits print_max;
332 repeat counts are printed as appropriate. Print ellipses at the
333 end if we had to stop before printing LENGTH characters, or if
337 objc_printstr (struct ui_file
*stream
, const gdb_byte
*string
,
338 unsigned int length
, int width
, int force_ellipses
)
341 unsigned int things_printed
= 0;
345 /* If the string was not truncated due to `set print elements', and
346 the last byte of it is a null, we don't print that, in
347 traditional C style. */
348 if ((!force_ellipses
) && length
> 0 && string
[length
-1] == '\0')
353 fputs_filtered ("\"\"", stream
);
357 for (i
= 0; i
< length
&& things_printed
< print_max
; ++i
)
359 /* Position of the character we are examining to see whether it
362 /* Number of repetitions we have detected so far. */
369 fputs_filtered (", ", stream
);
375 while (rep1
< length
&& string
[rep1
] == string
[i
])
381 if (reps
> repeat_count_threshold
)
386 fputs_filtered ("\\\", ", stream
);
388 fputs_filtered ("\", ", stream
);
391 objc_printchar (string
[i
], stream
);
392 fprintf_filtered (stream
, " <repeats %u times>", reps
);
394 things_printed
+= repeat_count_threshold
;
402 fputs_filtered ("\\\"", stream
);
404 fputs_filtered ("\"", stream
);
407 objc_emit_char (string
[i
], stream
, '"');
412 /* Terminate the quotes if necessary. */
416 fputs_filtered ("\\\"", stream
);
418 fputs_filtered ("\"", stream
);
421 if (force_ellipses
|| i
< length
)
422 fputs_filtered ("...", stream
);
425 /* Determine if we are currently in the Objective-C dispatch function.
426 If so, get the address of the method function that the dispatcher
427 would call and use that as the function to step into instead. Also
428 skip over the trampoline for the function (if any). This is better
429 for the user since they are only interested in stepping into the
430 method function anyway. */
432 objc_skip_trampoline (struct frame_info
*frame
, CORE_ADDR stop_pc
)
434 CORE_ADDR real_stop_pc
;
435 CORE_ADDR method_stop_pc
;
437 real_stop_pc
= gdbarch_skip_trampoline_code
438 (current_gdbarch
, frame
, stop_pc
);
440 if (real_stop_pc
!= 0)
441 find_objc_msgcall (real_stop_pc
, &method_stop_pc
);
443 find_objc_msgcall (stop_pc
, &method_stop_pc
);
447 real_stop_pc
= gdbarch_skip_trampoline_code
448 (current_gdbarch
, frame
, method_stop_pc
);
449 if (real_stop_pc
== 0)
450 real_stop_pc
= method_stop_pc
;
457 /* Table mapping opcodes into strings for printing operators
458 and precedences of the operators. */
460 static const struct op_print objc_op_print_tab
[] =
462 {",", BINOP_COMMA
, PREC_COMMA
, 0},
463 {"=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
464 {"||", BINOP_LOGICAL_OR
, PREC_LOGICAL_OR
, 0},
465 {"&&", BINOP_LOGICAL_AND
, PREC_LOGICAL_AND
, 0},
466 {"|", BINOP_BITWISE_IOR
, PREC_BITWISE_IOR
, 0},
467 {"^", BINOP_BITWISE_XOR
, PREC_BITWISE_XOR
, 0},
468 {"&", BINOP_BITWISE_AND
, PREC_BITWISE_AND
, 0},
469 {"==", BINOP_EQUAL
, PREC_EQUAL
, 0},
470 {"!=", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
471 {"<=", BINOP_LEQ
, PREC_ORDER
, 0},
472 {">=", BINOP_GEQ
, PREC_ORDER
, 0},
473 {">", BINOP_GTR
, PREC_ORDER
, 0},
474 {"<", BINOP_LESS
, PREC_ORDER
, 0},
475 {">>", BINOP_RSH
, PREC_SHIFT
, 0},
476 {"<<", BINOP_LSH
, PREC_SHIFT
, 0},
477 {"+", BINOP_ADD
, PREC_ADD
, 0},
478 {"-", BINOP_SUB
, PREC_ADD
, 0},
479 {"*", BINOP_MUL
, PREC_MUL
, 0},
480 {"/", BINOP_DIV
, PREC_MUL
, 0},
481 {"%", BINOP_REM
, PREC_MUL
, 0},
482 {"@", BINOP_REPEAT
, PREC_REPEAT
, 0},
483 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
484 {"!", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
485 {"~", UNOP_COMPLEMENT
, PREC_PREFIX
, 0},
486 {"*", UNOP_IND
, PREC_PREFIX
, 0},
487 {"&", UNOP_ADDR
, PREC_PREFIX
, 0},
488 {"sizeof ", UNOP_SIZEOF
, PREC_PREFIX
, 0},
489 {"++", UNOP_PREINCREMENT
, PREC_PREFIX
, 0},
490 {"--", UNOP_PREDECREMENT
, PREC_PREFIX
, 0},
491 {NULL
, OP_NULL
, PREC_NULL
, 0}
494 const struct language_defn objc_language_defn
= {
495 "objective-c", /* Language name */
501 &exp_descriptor_standard
,
505 objc_printchar
, /* Print a character constant */
506 objc_printstr
, /* Function to print string constant */
508 c_print_type
, /* Print a type using appropriate syntax */
509 c_val_print
, /* Print a value using appropriate syntax */
510 c_value_print
, /* Print a top-level value */
511 objc_skip_trampoline
, /* Language specific skip_trampoline */
512 "self", /* name_of_this */
513 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
514 basic_lookup_transparent_type
,/* lookup_transparent_type */
515 objc_demangle
, /* Language specific symbol demangler */
516 NULL
, /* Language specific class_name_from_physname */
517 objc_op_print_tab
, /* Expression operators for printing */
518 1, /* C-style arrays */
519 0, /* String lower bound */
520 default_word_break_characters
,
521 default_make_symbol_completion_list
,
522 c_language_arch_info
,
523 default_print_array_index
,
524 default_pass_by_reference
,
530 * Following functions help construct Objective-C message calls
533 struct selname
/* For parsing Objective-C. */
535 struct selname
*next
;
540 static int msglist_len
;
541 static struct selname
*selname_chain
;
542 static char *msglist_sel
;
547 struct selname
*new =
548 (struct selname
*) xmalloc (sizeof (struct selname
));
550 new->next
= selname_chain
;
551 new->msglist_len
= msglist_len
;
552 new->msglist_sel
= msglist_sel
;
554 msglist_sel
= (char *)xmalloc(1);
560 add_msglist(struct stoken
*str
, int addcolon
)
565 if (str
== 0) { /* Unnamed arg, or... */
566 if (addcolon
== 0) { /* variable number of args. */
576 len
= plen
+ strlen(msglist_sel
) + 2;
577 s
= (char *)xmalloc(len
);
578 strcpy(s
, msglist_sel
);
593 int val
= msglist_len
;
594 struct selname
*sel
= selname_chain
;
595 char *p
= msglist_sel
;
598 selname_chain
= sel
->next
;
599 msglist_len
= sel
->msglist_len
;
600 msglist_sel
= sel
->msglist_sel
;
601 selid
= lookup_child_selector(p
);
603 error (_("Can't find selector \"%s\""), p
);
604 write_exp_elt_longcst (selid
);
606 write_exp_elt_longcst (val
); /* Number of args */
613 * Function: specialcmp (char *a, char *b)
615 * Special strcmp: treats ']' and ' ' as end-of-string.
616 * Used for qsorting lists of objc methods (either by class or selector).
620 specialcmp (char *a
, char *b
)
622 while (*a
&& *a
!= ' ' && *a
!= ']' && *b
&& *b
!= ' ' && *b
!= ']')
628 if (*a
&& *a
!= ' ' && *a
!= ']')
629 return 1; /* a is longer therefore greater */
630 if (*b
&& *b
!= ' ' && *b
!= ']')
631 return -1; /* a is shorter therefore lesser */
632 return 0; /* a and b are identical */
636 * Function: compare_selectors (const void *, const void *)
638 * Comparison function for use with qsort. Arguments are symbols or
639 * msymbols Compares selector part of objc method name alphabetically.
643 compare_selectors (const void *a
, const void *b
)
647 aname
= SYMBOL_PRINT_NAME (*(struct symbol
**) a
);
648 bname
= SYMBOL_PRINT_NAME (*(struct symbol
**) b
);
649 if (aname
== NULL
|| bname
== NULL
)
650 error (_("internal: compare_selectors(1)"));
652 aname
= strchr(aname
, ' ');
653 bname
= strchr(bname
, ' ');
654 if (aname
== NULL
|| bname
== NULL
)
655 error (_("internal: compare_selectors(2)"));
657 return specialcmp (aname
+1, bname
+1);
661 * Function: selectors_info (regexp, from_tty)
663 * Implements the "Info selectors" command. Takes an optional regexp
664 * arg. Lists all objective c selectors that match the regexp. Works
665 * by grepping thru all symbols for objective c methods. Output list
666 * is sorted and uniqued.
670 selectors_info (char *regexp
, int from_tty
)
672 struct objfile
*objfile
;
673 struct minimal_symbol
*msymbol
;
681 struct symbol
**sym_arr
;
685 strcpy(myregexp
, ".*]"); /* Null input, match all objc methods. */
688 if (*regexp
== '+' || *regexp
== '-')
689 { /* User wants only class methods or only instance methods. */
690 plusminus
= *regexp
++;
691 while (*regexp
== ' ' || *regexp
== '\t')
695 strcpy(myregexp
, ".*]");
698 strcpy(myregexp
, regexp
);
699 if (myregexp
[strlen(myregexp
) - 1] == '$') /* end of selector */
700 myregexp
[strlen(myregexp
) - 1] = ']'; /* end of method name */
702 strcat(myregexp
, ".*]");
708 val
= re_comp (myregexp
);
710 error (_("Invalid regexp (%s): %s"), val
, regexp
);
713 /* First time thru is JUST to get max length and count. */
714 ALL_MSYMBOLS (objfile
, msymbol
)
717 name
= SYMBOL_NATURAL_NAME (msymbol
);
719 (name
[0] == '-' || name
[0] == '+') &&
720 name
[1] == '[') /* Got a method name. */
722 /* Filter for class/instance methods. */
723 if (plusminus
&& name
[0] != plusminus
)
725 /* Find selector part. */
726 name
= (char *) strchr(name
+2, ' ');
727 if (regexp
== NULL
|| re_exec(++name
) != 0)
729 char *mystart
= name
;
730 char *myend
= (char *) strchr(mystart
, ']');
732 if (myend
&& (myend
- mystart
> maxlen
))
733 maxlen
= myend
- mystart
; /* Get longest selector. */
740 printf_filtered (_("Selectors matching \"%s\":\n\n"),
741 regexp
? regexp
: "*");
743 sym_arr
= alloca (matches
* sizeof (struct symbol
*));
745 ALL_MSYMBOLS (objfile
, msymbol
)
748 name
= SYMBOL_NATURAL_NAME (msymbol
);
750 (name
[0] == '-' || name
[0] == '+') &&
751 name
[1] == '[') /* Got a method name. */
753 /* Filter for class/instance methods. */
754 if (plusminus
&& name
[0] != plusminus
)
756 /* Find selector part. */
757 name
= (char *) strchr(name
+2, ' ');
758 if (regexp
== NULL
|| re_exec(++name
) != 0)
759 sym_arr
[matches
++] = (struct symbol
*) msymbol
;
763 qsort (sym_arr
, matches
, sizeof (struct minimal_symbol
*),
765 /* Prevent compare on first iteration. */
767 for (ix
= 0; ix
< matches
; ix
++) /* Now do the output. */
772 name
= SYMBOL_NATURAL_NAME (sym_arr
[ix
]);
773 name
= strchr (name
, ' ') + 1;
774 if (p
[0] && specialcmp(name
, p
) == 0)
775 continue; /* Seen this one already (not unique). */
777 /* Copy selector part. */
778 while (*name
&& *name
!= ']')
781 /* Print in columns. */
782 puts_filtered_tabular(asel
, maxlen
+ 1, 0);
787 printf_filtered (_("No selectors matching \"%s\"\n"), regexp
? regexp
: "*");
791 * Function: compare_classes (const void *, const void *)
793 * Comparison function for use with qsort. Arguments are symbols or
794 * msymbols Compares class part of objc method name alphabetically.
798 compare_classes (const void *a
, const void *b
)
802 aname
= SYMBOL_PRINT_NAME (*(struct symbol
**) a
);
803 bname
= SYMBOL_PRINT_NAME (*(struct symbol
**) b
);
804 if (aname
== NULL
|| bname
== NULL
)
805 error (_("internal: compare_classes(1)"));
807 return specialcmp (aname
+1, bname
+1);
811 * Function: classes_info(regexp, from_tty)
813 * Implements the "info classes" command for objective c classes.
814 * Lists all objective c classes that match the optional regexp.
815 * Works by grepping thru the list of objective c methods. List will
816 * be sorted and uniqued (since one class may have many methods).
817 * BUGS: will not list a class that has no methods.
821 classes_info (char *regexp
, int from_tty
)
823 struct objfile
*objfile
;
824 struct minimal_symbol
*msymbol
;
832 struct symbol
**sym_arr
;
835 strcpy(myregexp
, ".* "); /* Null input: match all objc classes. */
838 strcpy(myregexp
, regexp
);
839 if (myregexp
[strlen(myregexp
) - 1] == '$')
840 /* In the method name, the end of the class name is marked by ' '. */
841 myregexp
[strlen(myregexp
) - 1] = ' ';
843 strcat(myregexp
, ".* ");
848 val
= re_comp (myregexp
);
850 error (_("Invalid regexp (%s): %s"), val
, regexp
);
853 /* First time thru is JUST to get max length and count. */
854 ALL_MSYMBOLS (objfile
, msymbol
)
857 name
= SYMBOL_NATURAL_NAME (msymbol
);
859 (name
[0] == '-' || name
[0] == '+') &&
860 name
[1] == '[') /* Got a method name. */
861 if (regexp
== NULL
|| re_exec(name
+2) != 0)
863 /* Compute length of classname part. */
864 char *mystart
= name
+ 2;
865 char *myend
= (char *) strchr(mystart
, ' ');
867 if (myend
&& (myend
- mystart
> maxlen
))
868 maxlen
= myend
- mystart
;
874 printf_filtered (_("Classes matching \"%s\":\n\n"),
875 regexp
? regexp
: "*");
876 sym_arr
= alloca (matches
* sizeof (struct symbol
*));
878 ALL_MSYMBOLS (objfile
, msymbol
)
881 name
= SYMBOL_NATURAL_NAME (msymbol
);
883 (name
[0] == '-' || name
[0] == '+') &&
884 name
[1] == '[') /* Got a method name. */
885 if (regexp
== NULL
|| re_exec(name
+2) != 0)
886 sym_arr
[matches
++] = (struct symbol
*) msymbol
;
889 qsort (sym_arr
, matches
, sizeof (struct minimal_symbol
*),
891 /* Prevent compare on first iteration. */
893 for (ix
= 0; ix
< matches
; ix
++) /* Now do the output. */
898 name
= SYMBOL_NATURAL_NAME (sym_arr
[ix
]);
900 if (p
[0] && specialcmp(name
, p
) == 0)
901 continue; /* Seen this one already (not unique). */
903 /* Copy class part of method name. */
904 while (*name
&& *name
!= ' ')
907 /* Print in columns. */
908 puts_filtered_tabular(aclass
, maxlen
+ 1, 0);
913 printf_filtered (_("No classes matching \"%s\"\n"), regexp
? regexp
: "*");
917 * Function: find_imps (char *selector, struct symbol **sym_arr)
919 * Input: a string representing a selector
920 * a pointer to an array of symbol pointers
921 * possibly a pointer to a symbol found by the caller.
923 * Output: number of methods that implement that selector. Side
924 * effects: The array of symbol pointers is filled with matching syms.
926 * By analogy with function "find_methods" (symtab.c), builds a list
927 * of symbols matching the ambiguous input, so that "decode_line_2"
928 * (symtab.c) can list them and ask the user to choose one or more.
929 * In this case the matches are objective c methods
930 * ("implementations") matching an objective c selector.
932 * Note that it is possible for a normal (c-style) function to have
933 * the same name as an objective c selector. To prevent the selector
934 * from eclipsing the function, we allow the caller (decode_line_1) to
935 * search for such a function first, and if it finds one, pass it in
936 * to us. We will then integrate it into the list. We also search
937 * for one here, among the minsyms.
939 * NOTE: if NUM_DEBUGGABLE is non-zero, the sym_arr will be divided
940 * into two parts: debuggable (struct symbol) syms, and
941 * non_debuggable (struct minimal_symbol) syms. The debuggable
942 * ones will come first, before NUM_DEBUGGABLE (which will thus
943 * be the index of the first non-debuggable one).
947 * Function: total_number_of_imps (char *selector);
949 * Input: a string representing a selector
950 * Output: number of methods that implement that selector.
952 * By analogy with function "total_number_of_methods", this allows
953 * decode_line_1 (symtab.c) to detect if there are objective c methods
954 * matching the input, and to allocate an array of pointers to them
955 * which can be manipulated by "decode_line_2" (also in symtab.c).
959 parse_selector (char *method
, char **selector
)
965 char *nselector
= NULL
;
967 gdb_assert (selector
!= NULL
);
971 while (isspace (*s1
))
978 while (isspace (*s1
))
985 if (isalnum (*s2
) || (*s2
== '_') || (*s2
== ':'))
987 else if (isspace (*s2
))
989 else if ((*s2
== '\0') || (*s2
== '\''))
997 while (isspace (*s2
))
1003 while (isspace (*s2
))
1007 if (selector
!= NULL
)
1008 *selector
= nselector
;
1014 parse_method (char *method
, char *type
, char **class,
1015 char **category
, char **selector
)
1019 int found_quote
= 0;
1022 char *nclass
= NULL
;
1023 char *ncategory
= NULL
;
1024 char *nselector
= NULL
;
1026 gdb_assert (type
!= NULL
);
1027 gdb_assert (class != NULL
);
1028 gdb_assert (category
!= NULL
);
1029 gdb_assert (selector
!= NULL
);
1033 while (isspace (*s1
))
1040 while (isspace (*s1
))
1043 if ((s1
[0] == '+') || (s1
[0] == '-'))
1046 while (isspace (*s1
))
1054 while (isalnum (*s1
) || (*s1
== '_'))
1058 while (isspace (*s2
))
1064 while (isspace (*s2
))
1067 while (isalnum (*s2
) || (*s2
== '_'))
1072 /* Truncate the class name now that we're not using the open paren. */
1079 if (isalnum (*s2
) || (*s2
== '_') || (*s2
== ':'))
1081 else if (isspace (*s2
))
1083 else if (*s2
== ']')
1092 while (isspace (*s2
))
1099 while (isspace (*s2
))
1107 if (category
!= NULL
)
1108 *category
= ncategory
;
1109 if (selector
!= NULL
)
1110 *selector
= nselector
;
1116 find_methods (struct symtab
*symtab
, char type
,
1117 const char *class, const char *category
,
1118 const char *selector
, struct symbol
**syms
,
1119 unsigned int *nsym
, unsigned int *ndebug
)
1121 struct objfile
*objfile
= NULL
;
1122 struct minimal_symbol
*msymbol
= NULL
;
1123 struct block
*block
= NULL
;
1124 struct symbol
*sym
= NULL
;
1126 char *symname
= NULL
;
1129 char *nclass
= NULL
;
1130 char *ncategory
= NULL
;
1131 char *nselector
= NULL
;
1133 unsigned int csym
= 0;
1134 unsigned int cdebug
= 0;
1136 static char *tmp
= NULL
;
1137 static unsigned int tmplen
= 0;
1139 gdb_assert (nsym
!= NULL
);
1140 gdb_assert (ndebug
!= NULL
);
1143 block
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab
), STATIC_BLOCK
);
1145 ALL_MSYMBOLS (objfile
, msymbol
)
1149 if ((msymbol
->type
!= mst_text
) && (msymbol
->type
!= mst_file_text
))
1150 /* Not a function or method. */
1154 if ((SYMBOL_VALUE_ADDRESS (msymbol
) < BLOCK_START (block
)) ||
1155 (SYMBOL_VALUE_ADDRESS (msymbol
) >= BLOCK_END (block
)))
1156 /* Not in the specified symtab. */
1159 symname
= SYMBOL_NATURAL_NAME (msymbol
);
1160 if (symname
== NULL
)
1163 if ((symname
[0] != '-' && symname
[0] != '+') || (symname
[1] != '['))
1164 /* Not a method name. */
1167 while ((strlen (symname
) + 1) >= tmplen
)
1169 tmplen
= (tmplen
== 0) ? 1024 : tmplen
* 2;
1170 tmp
= xrealloc (tmp
, tmplen
);
1172 strcpy (tmp
, symname
);
1174 if (parse_method (tmp
, &ntype
, &nclass
, &ncategory
, &nselector
) == NULL
)
1177 if ((type
!= '\0') && (ntype
!= type
))
1181 && ((nclass
== NULL
) || (strcmp (class, nclass
) != 0)))
1184 if ((category
!= NULL
) &&
1185 ((ncategory
== NULL
) || (strcmp (category
, ncategory
) != 0)))
1188 if ((selector
!= NULL
) &&
1189 ((nselector
== NULL
) || (strcmp (selector
, nselector
) != 0)))
1192 sym
= find_pc_function (SYMBOL_VALUE_ADDRESS (msymbol
));
1195 const char *newsymname
= SYMBOL_NATURAL_NAME (sym
);
1197 if (strcmp (symname
, newsymname
) == 0)
1199 /* Found a high-level method sym: swap it into the
1200 lower part of sym_arr (below num_debuggable). */
1203 syms
[csym
] = syms
[cdebug
];
1212 "debugging symbol \"%s\" does not match minimal symbol (\"%s\"); ignoring",
1213 newsymname
, symname
);
1215 syms
[csym
] = (struct symbol
*) msymbol
;
1221 /* Found a non-debuggable method symbol. */
1223 syms
[csym
] = (struct symbol
*) msymbol
;
1234 char *find_imps (struct symtab
*symtab
, struct block
*block
,
1235 char *method
, struct symbol
**syms
,
1236 unsigned int *nsym
, unsigned int *ndebug
)
1240 char *category
= NULL
;
1241 char *selector
= NULL
;
1243 unsigned int csym
= 0;
1244 unsigned int cdebug
= 0;
1246 unsigned int ncsym
= 0;
1247 unsigned int ncdebug
= 0;
1252 gdb_assert (nsym
!= NULL
);
1253 gdb_assert (ndebug
!= NULL
);
1260 buf
= (char *) alloca (strlen (method
) + 1);
1261 strcpy (buf
, method
);
1262 tmp
= parse_method (buf
, &type
, &class, &category
, &selector
);
1266 struct symbol
*sym
= NULL
;
1267 struct minimal_symbol
*msym
= NULL
;
1269 strcpy (buf
, method
);
1270 tmp
= parse_selector (buf
, &selector
);
1275 sym
= lookup_symbol (selector
, block
, VAR_DOMAIN
, 0, NULL
);
1285 msym
= lookup_minimal_symbol (selector
, 0, 0);
1290 syms
[csym
] = (struct symbol
*)msym
;
1296 find_methods (symtab
, type
, class, category
, selector
,
1297 syms
+ csym
, &ncsym
, &ncdebug
);
1299 find_methods (symtab
, type
, class, category
, selector
,
1300 NULL
, &ncsym
, &ncdebug
);
1302 /* If we didn't find any methods, just return. */
1303 if (ncsym
== 0 && ncdebug
== 0)
1306 /* Take debug symbols from the second batch of symbols and swap them
1307 * with debug symbols from the first batch. Repeat until either the
1308 * second section is out of debug symbols or the first section is
1309 * full of debug symbols. Either way we have all debug symbols
1310 * packed to the beginning of the buffer.
1315 while ((cdebug
< csym
) && (ncdebug
> 0))
1317 struct symbol
*s
= NULL
;
1318 /* First non-debugging symbol. */
1319 unsigned int i
= cdebug
;
1320 /* Last of second batch of debug symbols. */
1321 unsigned int j
= csym
+ ncdebug
- 1;
1327 /* We've moved a symbol from the second debug section to the
1343 return method
+ (tmp
- buf
);
1347 /* Sort debuggable symbols. */
1349 qsort (syms
, cdebug
, sizeof (struct minimal_symbol
*),
1352 /* Sort minimal_symbols. */
1353 if ((csym
- cdebug
) > 1)
1354 qsort (&syms
[cdebug
], csym
- cdebug
,
1355 sizeof (struct minimal_symbol
*), compare_classes
);
1357 /* Terminate the sym_arr list. */
1360 return method
+ (tmp
- buf
);
1364 print_object_command (char *args
, int from_tty
)
1366 struct value
*object
, *function
, *description
;
1367 CORE_ADDR string_addr
, object_addr
;
1371 if (!args
|| !*args
)
1373 "The 'print-object' command requires an argument (an Objective-C object)");
1376 struct expression
*expr
= parse_expression (args
);
1377 struct cleanup
*old_chain
=
1378 make_cleanup (free_current_contents
, &expr
);
1381 object
= expr
->language_defn
->la_exp_desc
->evaluate_exp
1382 (builtin_type_void_data_ptr
, expr
, &pc
, EVAL_NORMAL
);
1383 do_cleanups (old_chain
);
1386 /* Validate the address for sanity. */
1387 object_addr
= value_as_long (object
);
1388 read_memory (object_addr
, &c
, 1);
1390 function
= find_function_in_inferior ("_NSPrintForDebugger");
1391 if (function
== NULL
)
1392 error (_("Unable to locate _NSPrintForDebugger in child process"));
1394 description
= call_function_by_hand (function
, 1, &object
);
1396 string_addr
= value_as_long (description
);
1397 if (string_addr
== 0)
1398 error (_("object returns null description"));
1400 read_memory (string_addr
+ i
++, &c
, 1);
1403 { /* Read and print characters up to EOS. */
1405 printf_filtered ("%c", c
);
1406 read_memory (string_addr
+ i
++, &c
, 1);
1409 printf_filtered(_("<object returns empty description>"));
1410 printf_filtered ("\n");
1413 /* The data structure 'methcalls' is used to detect method calls (thru
1414 * ObjC runtime lib functions objc_msgSend, objc_msgSendSuper, etc.),
1415 * and ultimately find the method being called.
1418 struct objc_methcall
{
1420 /* Return instance method to be called. */
1421 int (*stop_at
) (CORE_ADDR
, CORE_ADDR
*);
1422 /* Start of pc range corresponding to method invocation. */
1424 /* End of pc range corresponding to method invocation. */
1428 static int resolve_msgsend (CORE_ADDR pc
, CORE_ADDR
*new_pc
);
1429 static int resolve_msgsend_stret (CORE_ADDR pc
, CORE_ADDR
*new_pc
);
1430 static int resolve_msgsend_super (CORE_ADDR pc
, CORE_ADDR
*new_pc
);
1431 static int resolve_msgsend_super_stret (CORE_ADDR pc
, CORE_ADDR
*new_pc
);
1433 static struct objc_methcall methcalls
[] = {
1434 { "_objc_msgSend", resolve_msgsend
, 0, 0},
1435 { "_objc_msgSend_stret", resolve_msgsend_stret
, 0, 0},
1436 { "_objc_msgSendSuper", resolve_msgsend_super
, 0, 0},
1437 { "_objc_msgSendSuper_stret", resolve_msgsend_super_stret
, 0, 0},
1438 { "_objc_getClass", NULL
, 0, 0},
1439 { "_objc_getMetaClass", NULL
, 0, 0}
1442 #define nmethcalls (sizeof (methcalls) / sizeof (methcalls[0]))
1444 /* The following function, "find_objc_msgsend", fills in the data
1445 * structure "objc_msgs" by finding the addresses of each of the
1446 * (currently four) functions that it holds (of which objc_msgSend is
1447 * the first). This must be called each time symbols are loaded, in
1448 * case the functions have moved for some reason.
1452 find_objc_msgsend (void)
1455 for (i
= 0; i
< nmethcalls
; i
++) {
1457 struct minimal_symbol
*func
;
1459 /* Try both with and without underscore. */
1460 func
= lookup_minimal_symbol (methcalls
[i
].name
, NULL
, NULL
);
1461 if ((func
== NULL
) && (methcalls
[i
].name
[0] == '_')) {
1462 func
= lookup_minimal_symbol (methcalls
[i
].name
+ 1, NULL
, NULL
);
1465 methcalls
[i
].begin
= 0;
1466 methcalls
[i
].end
= 0;
1470 methcalls
[i
].begin
= SYMBOL_VALUE_ADDRESS (func
);
1472 methcalls
[i
].end
= SYMBOL_VALUE_ADDRESS (++func
);
1473 } while (methcalls
[i
].begin
== methcalls
[i
].end
);
1477 /* find_objc_msgcall (replaces pc_off_limits)
1479 * ALL that this function now does is to determine whether the input
1480 * address ("pc") is the address of one of the Objective-C message
1481 * dispatch functions (mainly objc_msgSend or objc_msgSendSuper), and
1482 * if so, it returns the address of the method that will be called.
1484 * The old function "pc_off_limits" used to do a lot of other things
1485 * in addition, such as detecting shared library jump stubs and
1486 * returning the address of the shlib function that would be called.
1487 * That functionality has been moved into the gdbarch_skip_trampoline_code and
1488 * IN_SOLIB_TRAMPOLINE macros, which are resolved in the target-
1489 * dependent modules.
1492 struct objc_submethod_helper_data
{
1493 int (*f
) (CORE_ADDR
, CORE_ADDR
*);
1499 find_objc_msgcall_submethod_helper (void * arg
)
1501 struct objc_submethod_helper_data
*s
=
1502 (struct objc_submethod_helper_data
*) arg
;
1504 if (s
->f (s
->pc
, s
->new_pc
) == 0)
1511 find_objc_msgcall_submethod (int (*f
) (CORE_ADDR
, CORE_ADDR
*),
1515 struct objc_submethod_helper_data s
;
1521 if (catch_errors (find_objc_msgcall_submethod_helper
,
1523 "Unable to determine target of Objective-C method call (ignoring):\n",
1524 RETURN_MASK_ALL
) == 0)
1531 find_objc_msgcall (CORE_ADDR pc
, CORE_ADDR
*new_pc
)
1535 find_objc_msgsend ();
1541 for (i
= 0; i
< nmethcalls
; i
++)
1542 if ((pc
>= methcalls
[i
].begin
) && (pc
< methcalls
[i
].end
))
1544 if (methcalls
[i
].stop_at
!= NULL
)
1545 return find_objc_msgcall_submethod (methcalls
[i
].stop_at
,
1554 extern initialize_file_ftype _initialize_objc_language
; /* -Wmissing-prototypes */
1557 _initialize_objc_language (void)
1559 add_language (&objc_language_defn
);
1560 add_info ("selectors", selectors_info
, /* INFO SELECTORS command. */
1561 _("All Objective-C selectors, or those matching REGEXP."));
1562 add_info ("classes", classes_info
, /* INFO CLASSES command. */
1563 _("All Objective-C classes, or those matching REGEXP."));
1564 add_com ("print-object", class_vars
, print_object_command
,
1565 _("Ask an Objective-C object to print itself."));
1566 add_com_alias ("po", "print-object", class_vars
, 1);
1570 read_objc_method (CORE_ADDR addr
, struct objc_method
*method
)
1572 method
->name
= read_memory_unsigned_integer (addr
+ 0, 4);
1573 method
->types
= read_memory_unsigned_integer (addr
+ 4, 4);
1574 method
->imp
= read_memory_unsigned_integer (addr
+ 8, 4);
1578 unsigned long read_objc_methlist_nmethods (CORE_ADDR addr
)
1580 return read_memory_unsigned_integer (addr
+ 4, 4);
1584 read_objc_methlist_method (CORE_ADDR addr
, unsigned long num
,
1585 struct objc_method
*method
)
1587 gdb_assert (num
< read_objc_methlist_nmethods (addr
));
1588 read_objc_method (addr
+ 8 + (12 * num
), method
);
1592 read_objc_object (CORE_ADDR addr
, struct objc_object
*object
)
1594 object
->isa
= read_memory_unsigned_integer (addr
, 4);
1598 read_objc_super (CORE_ADDR addr
, struct objc_super
*super
)
1600 super
->receiver
= read_memory_unsigned_integer (addr
, 4);
1601 super
->class = read_memory_unsigned_integer (addr
+ 4, 4);
1605 read_objc_class (CORE_ADDR addr
, struct objc_class
*class)
1607 class->isa
= read_memory_unsigned_integer (addr
, 4);
1608 class->super_class
= read_memory_unsigned_integer (addr
+ 4, 4);
1609 class->name
= read_memory_unsigned_integer (addr
+ 8, 4);
1610 class->version
= read_memory_unsigned_integer (addr
+ 12, 4);
1611 class->info
= read_memory_unsigned_integer (addr
+ 16, 4);
1612 class->instance_size
= read_memory_unsigned_integer (addr
+ 18, 4);
1613 class->ivars
= read_memory_unsigned_integer (addr
+ 24, 4);
1614 class->methods
= read_memory_unsigned_integer (addr
+ 28, 4);
1615 class->cache
= read_memory_unsigned_integer (addr
+ 32, 4);
1616 class->protocols
= read_memory_unsigned_integer (addr
+ 36, 4);
1620 find_implementation_from_class (CORE_ADDR
class, CORE_ADDR sel
)
1622 CORE_ADDR subclass
= class;
1624 while (subclass
!= 0)
1627 struct objc_class class_str
;
1628 unsigned mlistnum
= 0;
1630 read_objc_class (subclass
, &class_str
);
1635 unsigned long nmethods
;
1638 mlist
= read_memory_unsigned_integer (class_str
.methods
+
1643 nmethods
= read_objc_methlist_nmethods (mlist
);
1645 for (i
= 0; i
< nmethods
; i
++)
1647 struct objc_method meth_str
;
1648 read_objc_methlist_method (mlist
, i
, &meth_str
);
1652 "checking method 0x%lx against selector 0x%lx\n",
1653 meth_str
.name
, sel
);
1656 if (meth_str
.name
== sel
)
1657 /* FIXME: hppa arch was doing a pointer dereference
1658 here. There needs to be a better way to do that. */
1659 return meth_str
.imp
;
1663 subclass
= class_str
.super_class
;
1670 find_implementation (CORE_ADDR object
, CORE_ADDR sel
)
1672 struct objc_object ostr
;
1676 read_objc_object (object
, &ostr
);
1680 return find_implementation_from_class (ostr
.isa
, sel
);
1683 #define OBJC_FETCH_POINTER_ARGUMENT(argi) \
1684 gdbarch_fetch_pointer_argument (current_gdbarch, get_current_frame (), \
1685 argi, builtin_type_void_func_ptr)
1688 resolve_msgsend (CORE_ADDR pc
, CORE_ADDR
*new_pc
)
1694 object
= OBJC_FETCH_POINTER_ARGUMENT (0);
1695 sel
= OBJC_FETCH_POINTER_ARGUMENT (1);
1697 res
= find_implementation (object
, sel
);
1706 resolve_msgsend_stret (CORE_ADDR pc
, CORE_ADDR
*new_pc
)
1712 object
= OBJC_FETCH_POINTER_ARGUMENT (1);
1713 sel
= OBJC_FETCH_POINTER_ARGUMENT (2);
1715 res
= find_implementation (object
, sel
);
1724 resolve_msgsend_super (CORE_ADDR pc
, CORE_ADDR
*new_pc
)
1726 struct objc_super sstr
;
1732 super
= OBJC_FETCH_POINTER_ARGUMENT (0);
1733 sel
= OBJC_FETCH_POINTER_ARGUMENT (1);
1735 read_objc_super (super
, &sstr
);
1736 if (sstr
.class == 0)
1739 res
= find_implementation_from_class (sstr
.class, sel
);
1748 resolve_msgsend_super_stret (CORE_ADDR pc
, CORE_ADDR
*new_pc
)
1750 struct objc_super sstr
;
1756 super
= OBJC_FETCH_POINTER_ARGUMENT (1);
1757 sel
= OBJC_FETCH_POINTER_ARGUMENT (2);
1759 read_objc_super (super
, &sstr
);
1760 if (sstr
.class == 0)
1763 res
= find_implementation_from_class (sstr
.class, sel
);