1 /* Objective-C language support routines for GDB, the GNU debugger.
3 Copyright 2002 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 2 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, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
28 #include "expression.h"
29 #include "parser-defs.h"
32 #include "objc-lang.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"
53 CORE_ADDR super_class;
75 /* Complaints about ObjC classes, selectors, etc. */
77 static struct deprecated_complaint noclass_lookup_complaint = {
78 "no way to lookup Objective-C classes", 0, 0
81 static struct deprecated_complaint nosel_lookup_complaint = {
82 "no way to lookup Objective-C selectors", 0, 0
86 #if (!defined __GNUC__ || __GNUC__ < 2 || __GNUC_MINOR__ < (defined __cplusplus ? 6 : 4))
87 #define __CHECK_FUNCTION ((__const char *) 0)
89 #define __CHECK_FUNCTION __PRETTY_FUNCTION__
92 #define CHECK(expression) \
93 ((void) ((expression) ? 0 : gdb_check (#expression, __FILE__, __LINE__, \
96 #define CHECK_FATAL(expression) \
97 ((void) ((expression) ? 0 : gdb_check_fatal (#expression, __FILE__, \
98 __LINE__, __CHECK_FUNCTION)))
101 gdb_check (const char *str, const char *file,
102 unsigned int line, const char *func)
104 error ("assertion failure on line %u of \"%s\" in function \"%s\": %s\n",
105 line, file, func, str);
109 gdb_check_fatal (const char *str, const char *file,
110 unsigned int line, const char *func)
112 internal_error (file, line,
113 "assertion failure in function \"%s\": %s\n", func, str);
116 /* Lookup a structure type named "struct NAME", visible in lexical
117 block BLOCK. If NOERR is nonzero, return zero if NAME is not
121 lookup_struct_typedef (char *name, struct block *block, int noerr)
123 register struct symbol *sym;
125 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
126 (struct symtab **) NULL);
133 error ("No struct type named %s.", name);
135 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
140 error ("This context has class, union or enum %s, not a struct.",
147 lookup_objc_class (char *classname)
149 struct value * function, *classval;
151 if (! target_has_execution)
153 /* Can't call into inferior to lookup class. */
157 if (lookup_minimal_symbol("objc_lookUpClass", 0, 0))
158 function = find_function_in_inferior("objc_lookUpClass");
159 else if (lookup_minimal_symbol ("objc_lookup_class", 0, 0))
160 function = find_function_in_inferior("objc_lookup_class");
163 complain (&noclass_lookup_complaint, 0);
167 classval = value_string (classname, strlen (classname) + 1);
168 classval = value_coerce_array (classval);
169 return (CORE_ADDR) value_as_long (call_function_by_hand (function,
174 lookup_child_selector (char *selname)
176 struct value * function, *selstring;
178 if (! target_has_execution)
180 /* Can't call into inferior to lookup selector. */
184 if (lookup_minimal_symbol("sel_getUid", 0, 0))
185 function = find_function_in_inferior("sel_getUid");
186 else if (lookup_minimal_symbol ("sel_get_any_uid", 0, 0))
187 function = find_function_in_inferior("sel_get_any_uid");
190 complain (&nosel_lookup_complaint, 0);
194 selstring = value_coerce_array (value_string (selname,
195 strlen (selname) + 1));
196 return value_as_long (call_function_by_hand (function, 1, &selstring));
200 value_nsstring (char *ptr, int len)
202 struct value *stringValue[3];
203 struct value *function, *nsstringValue;
207 if (!target_has_execution)
208 return 0; /* Can't call into inferior to create NSString. */
210 if (!(sym = lookup_struct_typedef("NSString", 0, 1)) &&
211 !(sym = lookup_struct_typedef("NXString", 0, 1)))
212 type = lookup_pointer_type(builtin_type_void);
214 type = lookup_pointer_type(SYMBOL_TYPE (sym));
216 stringValue[2] = value_string(ptr, len);
217 stringValue[2] = value_coerce_array(stringValue[2]);
218 /* _NSNewStringFromCString replaces "istr" after Lantern2A. */
219 if (lookup_minimal_symbol("_NSNewStringFromCString", 0, 0))
221 function = find_function_in_inferior("_NSNewStringFromCString");
222 nsstringValue = call_function_by_hand(function, 1, &stringValue[2]);
224 else if (lookup_minimal_symbol("istr", 0, 0))
226 function = find_function_in_inferior("istr");
227 nsstringValue = call_function_by_hand(function, 1, &stringValue[2]);
229 else if (lookup_minimal_symbol("+[NSString stringWithCString:]", 0, 0))
231 function = find_function_in_inferior("+[NSString stringWithCString:]");
232 stringValue[0] = value_from_longest
233 (builtin_type_long, lookup_objc_class ("NSString"));
234 stringValue[1] = value_from_longest
235 (builtin_type_long, lookup_child_selector ("stringWithCString:"));
236 nsstringValue = call_function_by_hand(function, 3, &stringValue[0]);
239 error ("NSString: internal error -- no way to create new NSString");
241 VALUE_TYPE(nsstringValue) = type;
242 return nsstringValue;
245 /* Objective-C name demangling. */
248 objc_demangle (const char *mangled)
250 char *demangled, *cp;
252 if (mangled[0] == '_' &&
253 (mangled[1] == 'i' || mangled[1] == 'c') &&
256 cp = demangled = xmalloc(strlen(mangled) + 2);
258 if (mangled[1] == 'i')
259 *cp++ = '-'; /* for instance method */
261 *cp++ = '+'; /* for class method */
263 *cp++ = '['; /* opening left brace */
264 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
266 while (*cp && *cp == '_')
267 cp++; /* skip any initial underbars in class name */
269 cp = strchr(cp, '_');
270 if (!cp) /* find first non-initial underbar */
272 xfree(demangled); /* not mangled name */
275 if (cp[1] == '_') { /* easy case: no category name */
276 *cp++ = ' '; /* replace two '_' with one ' ' */
277 strcpy(cp, mangled + (cp - demangled) + 2);
280 *cp++ = '('; /* less easy case: category name */
281 cp = strchr(cp, '_');
284 xfree(demangled); /* not mangled name */
288 *cp++ = ' '; /* overwriting 1st char of method name... */
289 strcpy(cp, mangled + (cp - demangled)); /* get it back */
292 while (*cp && *cp == '_')
293 cp++; /* skip any initial underbars in method name */
297 *cp = ':'; /* replace remaining '_' with ':' */
299 *cp++ = ']'; /* closing right brace */
300 *cp++ = 0; /* string terminator */
304 return NULL; /* Not an objc mangled name. */
307 /* Print the character C on STREAM as part of the contents of a
308 literal string whose delimiter is QUOTER. Note that that format
309 for printing characters and strings is language specific. */
312 objc_emit_char (register int c, struct ui_file *stream, int quoter)
315 c &= 0xFF; /* Avoid sign bit follies. */
317 if (PRINT_LITERAL_FORM (c))
319 if (c == '\\' || c == quoter)
321 fputs_filtered ("\\", stream);
323 fprintf_filtered (stream, "%c", c);
330 fputs_filtered ("\\n", stream);
333 fputs_filtered ("\\b", stream);
336 fputs_filtered ("\\t", stream);
339 fputs_filtered ("\\f", stream);
342 fputs_filtered ("\\r", stream);
345 fputs_filtered ("\\e", stream);
348 fputs_filtered ("\\a", stream);
351 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
358 objc_printchar (int c, struct ui_file *stream)
360 fputs_filtered ("'", stream);
361 objc_emit_char (c, stream, '\'');
362 fputs_filtered ("'", stream);
365 /* Print the character string STRING, printing at most LENGTH
366 characters. Printing stops early if the number hits print_max;
367 repeat counts are printed as appropriate. Print ellipses at the
368 end if we had to stop before printing LENGTH characters, or if
372 objc_printstr (struct ui_file *stream, char *string,
373 unsigned int length, int force_ellipses)
375 register unsigned int i;
376 unsigned int things_printed = 0;
379 extern int inspect_it;
380 extern int repeat_count_threshold;
381 extern int print_max;
383 /* If the string was not truncated due to `set print elements', and
384 the last byte of it is a null, we don't print that, in
385 traditional C style. */
386 if ((!force_ellipses) && length > 0 && string[length-1] == '\0')
391 fputs_filtered ("\"\"", stream);
395 for (i = 0; i < length && things_printed < print_max; ++i)
397 /* Position of the character we are examining to see whether it
400 /* Number of repetitions we have detected so far. */
407 fputs_filtered (", ", stream);
413 while (rep1 < length && string[rep1] == string[i])
419 if (reps > repeat_count_threshold)
424 fputs_filtered ("\\\", ", stream);
426 fputs_filtered ("\", ", stream);
429 objc_printchar (string[i], stream);
430 fprintf_filtered (stream, " <repeats %u times>", reps);
432 things_printed += repeat_count_threshold;
440 fputs_filtered ("\\\"", stream);
442 fputs_filtered ("\"", stream);
445 objc_emit_char (string[i], stream, '"');
450 /* Terminate the quotes if necessary. */
454 fputs_filtered ("\\\"", stream);
456 fputs_filtered ("\"", stream);
459 if (force_ellipses || i < length)
460 fputs_filtered ("...", stream);
463 /* Create a fundamental C type using default reasonable for the
466 Some object/debugging file formats (DWARF version 1, COFF, etc) do
467 not define fundamental types such as "int" or "double". Others
468 (stabs or DWARF version 2, etc) do define fundamental types. For
469 the formats which don't provide fundamental types, gdb can create
470 such types using this function.
472 FIXME: Some compilers distinguish explicitly signed integral types
473 (signed short, signed int, signed long) from "regular" integral
474 types (short, int, long) in the debugging information. There is
475 some disagreement as to how useful this feature is. In particular,
476 gcc does not support this. Also, only some debugging formats allow
477 the distinction to be passed on to a debugger. For now, we always
478 just use "short", "int", or "long" as the type name, for both the
479 implicit and explicitly signed types. This also makes life easier
480 for the gdb test suite since we don't have to account for the
481 differences in output depending upon what the compiler and
482 debugging format support. We will probably have to re-examine the
483 issue when gdb starts taking it's fundamental type information
484 directly from the debugging information supplied by the compiler.
488 objc_create_fundamental_type (struct objfile *objfile, int typeid)
490 register struct type *type = NULL;
495 /* FIXME: For now, if we are asked to produce a type not in
496 this language, create the equivalent of a C integer type
497 with the name "<?type?>". When all the dust settles from
498 the type reconstruction work, this should probably become
500 type = init_type (TYPE_CODE_INT,
501 TARGET_INT_BIT / TARGET_CHAR_BIT,
502 0, "<?type?>", objfile);
503 warning ("internal error: no C/C++ fundamental type %d", typeid);
506 type = init_type (TYPE_CODE_VOID,
507 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
511 type = init_type (TYPE_CODE_INT,
512 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
516 type = init_type (TYPE_CODE_INT,
517 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
518 0, "signed char", objfile);
520 case FT_UNSIGNED_CHAR:
521 type = init_type (TYPE_CODE_INT,
522 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
523 TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
526 type = init_type (TYPE_CODE_INT,
527 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
528 0, "short", objfile);
530 case FT_SIGNED_SHORT:
531 type = init_type (TYPE_CODE_INT,
532 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
533 0, "short", objfile); /* FIXME-fnf */
535 case FT_UNSIGNED_SHORT:
536 type = init_type (TYPE_CODE_INT,
537 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
538 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
541 type = init_type (TYPE_CODE_INT,
542 TARGET_INT_BIT / TARGET_CHAR_BIT,
545 case FT_SIGNED_INTEGER:
546 type = init_type (TYPE_CODE_INT,
547 TARGET_INT_BIT / TARGET_CHAR_BIT,
548 0, "int", objfile); /* FIXME -fnf */
550 case FT_UNSIGNED_INTEGER:
551 type = init_type (TYPE_CODE_INT,
552 TARGET_INT_BIT / TARGET_CHAR_BIT,
553 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
556 type = init_type (TYPE_CODE_INT,
557 TARGET_LONG_BIT / TARGET_CHAR_BIT,
561 type = init_type (TYPE_CODE_INT,
562 TARGET_LONG_BIT / TARGET_CHAR_BIT,
563 0, "long", objfile); /* FIXME -fnf */
565 case FT_UNSIGNED_LONG:
566 type = init_type (TYPE_CODE_INT,
567 TARGET_LONG_BIT / TARGET_CHAR_BIT,
568 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
571 type = init_type (TYPE_CODE_INT,
572 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
573 0, "long long", objfile);
575 case FT_SIGNED_LONG_LONG:
576 type = init_type (TYPE_CODE_INT,
577 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
578 0, "signed long long", objfile);
580 case FT_UNSIGNED_LONG_LONG:
581 type = init_type (TYPE_CODE_INT,
582 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
583 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
586 type = init_type (TYPE_CODE_FLT,
587 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
588 0, "float", objfile);
590 case FT_DBL_PREC_FLOAT:
591 type = init_type (TYPE_CODE_FLT,
592 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
593 0, "double", objfile);
595 case FT_EXT_PREC_FLOAT:
596 type = init_type (TYPE_CODE_FLT,
597 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
598 0, "long double", objfile);
605 /* Table mapping opcodes into strings for printing operators
606 and precedences of the operators. */
608 static const struct op_print objc_op_print_tab[] =
610 {",", BINOP_COMMA, PREC_COMMA, 0},
611 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
612 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
613 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
614 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
615 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
616 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
617 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
618 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
619 {"<=", BINOP_LEQ, PREC_ORDER, 0},
620 {">=", BINOP_GEQ, PREC_ORDER, 0},
621 {">", BINOP_GTR, PREC_ORDER, 0},
622 {"<", BINOP_LESS, PREC_ORDER, 0},
623 {">>", BINOP_RSH, PREC_SHIFT, 0},
624 {"<<", BINOP_LSH, PREC_SHIFT, 0},
625 {"+", BINOP_ADD, PREC_ADD, 0},
626 {"-", BINOP_SUB, PREC_ADD, 0},
627 {"*", BINOP_MUL, PREC_MUL, 0},
628 {"/", BINOP_DIV, PREC_MUL, 0},
629 {"%", BINOP_REM, PREC_MUL, 0},
630 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
631 {"-", UNOP_NEG, PREC_PREFIX, 0},
632 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
633 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
634 {"*", UNOP_IND, PREC_PREFIX, 0},
635 {"&", UNOP_ADDR, PREC_PREFIX, 0},
636 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
637 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
638 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
642 struct type ** const (objc_builtin_types[]) =
649 &builtin_type_double,
651 &builtin_type_long_long,
652 &builtin_type_signed_char,
653 &builtin_type_unsigned_char,
654 &builtin_type_unsigned_short,
655 &builtin_type_unsigned_int,
656 &builtin_type_unsigned_long,
657 &builtin_type_unsigned_long_long,
658 &builtin_type_long_double,
659 &builtin_type_complex,
660 &builtin_type_double_complex,
664 const struct language_defn objc_language_defn = {
665 "objective-c", /* Language name */
673 evaluate_subexp_standard,
674 objc_printchar, /* Print a character constant */
675 objc_printstr, /* Function to print string constant */
677 objc_create_fundamental_type, /* Create fundamental type in this language */
678 c_print_type, /* Print a type using appropriate syntax */
679 c_val_print, /* Print a value using appropriate syntax */
680 c_value_print, /* Print a top-level value */
681 {"", "", "", ""}, /* Binary format info */
682 {"0%lo", "0", "o", ""}, /* Octal format info */
683 {"%ld", "", "d", ""}, /* Decimal format info */
684 {"0x%lx", "0x", "x", ""}, /* Hex format info */
685 objc_op_print_tab, /* Expression operators for printing */
686 1, /* C-style arrays */
687 0, /* String lower bound */
688 &builtin_type_char, /* Type of string elements */
694 * Following functions help construct Objective-C message calls
697 struct selname /* For parsing Objective-C. */
699 struct selname *next;
704 static int msglist_len;
705 static struct selname *selname_chain;
706 static char *msglist_sel;
711 register struct selname *new =
712 (struct selname *) xmalloc (sizeof (struct selname));
714 new->next = selname_chain;
715 new->msglist_len = msglist_len;
716 new->msglist_sel = msglist_sel;
718 msglist_sel = (char *)xmalloc(1);
724 add_msglist(struct stoken *str, int addcolon)
729 if (str == 0) { /* Unnamed arg, or... */
730 if (addcolon == 0) { /* variable number of args. */
740 len = plen + strlen(msglist_sel) + 2;
741 s = (char *)xmalloc(len);
742 strcpy(s, msglist_sel);
757 register int val = msglist_len;
758 register struct selname *sel = selname_chain;
759 register char *p = msglist_sel;
762 selname_chain = sel->next;
763 msglist_len = sel->msglist_len;
764 msglist_sel = sel->msglist_sel;
765 selid = lookup_child_selector(p);
767 error("Can't find selector \"%s\"", p);
768 write_exp_elt_longcst (selid);
770 write_exp_elt_longcst (val); /* Number of args */
777 * Function: specialcmp (char *a, char *b)
779 * Special strcmp: treats ']' and ' ' as end-of-string.
780 * Used for qsorting lists of objc methods (either by class or selector).
783 int specialcmp(char *a, char *b)
785 while (*a && *a != ' ' && *a != ']' && *b && *b != ' ' && *b != ']')
791 if (*a && *a != ' ' && *a != ']')
792 return 1; /* a is longer therefore greater */
793 if (*b && *b != ' ' && *b != ']')
794 return -1; /* a is shorter therefore lesser */
795 return 0; /* a and b are identical */
799 * Function: compare_selectors (void *, void *)
801 * Comparison function for use with qsort. Arguments are symbols or
802 * msymbols Compares selector part of objc method name alphabetically.
806 compare_selectors (void *a, void *b)
810 aname = SYMBOL_SOURCE_NAME (*(struct symbol **) a);
811 bname = SYMBOL_SOURCE_NAME (*(struct symbol **) b);
812 if (aname == NULL || bname == NULL)
813 error ("internal: compare_selectors(1)");
815 aname = strchr(aname, ' ');
816 bname = strchr(bname, ' ');
817 if (aname == NULL || bname == NULL)
818 error ("internal: compare_selectors(2)");
820 return specialcmp (aname+1, bname+1);
824 * Function: selectors_info (regexp, from_tty)
826 * Implements the "Info selectors" command. Takes an optional regexp
827 * arg. Lists all objective c selectors that match the regexp. Works
828 * by grepping thru all symbols for objective c methods. Output list
829 * is sorted and uniqued.
833 selectors_info (char *regexp, int from_tty)
835 struct objfile *objfile;
836 struct minimal_symbol *msymbol;
844 struct symbol **sym_arr;
848 strcpy(myregexp, ".*]"); /* Null input, match all objc methods. */
851 if (*regexp == '+' || *regexp == '-')
852 { /* User wants only class methods or only instance methods. */
853 plusminus = *regexp++;
854 while (*regexp == ' ' || *regexp == '\t')
858 strcpy(myregexp, ".*]");
861 strcpy(myregexp, regexp);
862 if (myregexp[strlen(myregexp) - 1] == '$') /* end of selector */
863 myregexp[strlen(myregexp) - 1] = ']'; /* end of method name */
865 strcat(myregexp, ".*]");
870 if (0 != (val = re_comp (myregexp)))
871 error ("Invalid regexp (%s): %s", val, regexp);
873 /* First time thru is JUST to get max length and count. */
874 ALL_MSYMBOLS (objfile, msymbol)
877 name = SYMBOL_DEMANGLED_NAME (msymbol);
879 name = SYMBOL_NAME (msymbol);
881 (name[0] == '-' || name[0] == '+') &&
882 name[1] == '[') /* Got a method name. */
884 /* Filter for class/instance methods. */
885 if (plusminus && name[0] != plusminus)
887 /* Find selector part. */
888 name = (char *) strchr(name+2, ' ');
889 if (regexp == NULL || re_exec(++name) != 0)
891 char *mystart = name;
892 char *myend = (char *) strchr(mystart, ']');
894 if (myend && (myend - mystart > maxlen))
895 maxlen = myend - mystart; /* Get longest selector. */
902 printf_filtered ("Selectors matching \"%s\":\n\n",
903 regexp ? regexp : "*");
905 sym_arr = alloca (matches * sizeof (struct symbol *));
907 ALL_MSYMBOLS (objfile, msymbol)
910 name = SYMBOL_DEMANGLED_NAME (msymbol);
912 name = SYMBOL_NAME (msymbol);
914 (name[0] == '-' || name[0] == '+') &&
915 name[1] == '[') /* Got a method name. */
917 /* Filter for class/instance methods. */
918 if (plusminus && name[0] != plusminus)
920 /* Find selector part. */
921 name = (char *) strchr(name+2, ' ');
922 if (regexp == NULL || re_exec(++name) != 0)
923 sym_arr[matches++] = (struct symbol *) msymbol;
927 qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
929 /* Prevent compare on first iteration. */
931 for (ix = 0; ix < matches; ix++) /* Now do the output. */
936 name = SYMBOL_DEMANGLED_NAME (sym_arr[ix]);
938 name = SYMBOL_NAME (sym_arr[ix]);
939 name = strchr (name, ' ') + 1;
940 if (p[0] && specialcmp(name, p) == 0)
941 continue; /* Seen this one already (not unique). */
943 /* Copy selector part. */
944 while (*name && *name != ']')
947 /* Print in columns. */
948 puts_filtered_tabular(asel, maxlen + 1, 0);
953 printf_filtered ("No selectors matching \"%s\"\n", regexp ? regexp : "*");
957 * Function: compare_classes (void *, void *)
959 * Comparison function for use with qsort. Arguments are symbols or
960 * msymbols Compares class part of objc method name alphabetically.
964 compare_classes (void *a, void *b)
968 aname = SYMBOL_SOURCE_NAME (*(struct symbol **) a);
969 bname = SYMBOL_SOURCE_NAME (*(struct symbol **) b);
970 if (aname == NULL || bname == NULL)
971 error ("internal: compare_classes(1)");
973 return specialcmp (aname+1, bname+1);
977 * Function: classes_info(regexp, from_tty)
979 * Implements the "info classes" command for objective c classes.
980 * Lists all objective c classes that match the optional regexp.
981 * Works by grepping thru the list of objective c methods. List will
982 * be sorted and uniqued (since one class may have many methods).
983 * BUGS: will not list a class that has no methods.
987 classes_info (char *regexp, int from_tty)
989 struct objfile *objfile;
990 struct minimal_symbol *msymbol;
998 struct symbol **sym_arr;
1001 strcpy(myregexp, ".* "); /* Null input: match all objc classes. */
1004 strcpy(myregexp, regexp);
1005 if (myregexp[strlen(myregexp) - 1] == '$')
1006 /* In the method name, the end of the class name is marked by ' '. */
1007 myregexp[strlen(myregexp) - 1] = ' ';
1009 strcat(myregexp, ".* ");
1013 if (0 != (val = re_comp (myregexp)))
1014 error ("Invalid regexp (%s): %s", val, regexp);
1016 /* First time thru is JUST to get max length and count. */
1017 ALL_MSYMBOLS (objfile, msymbol)
1020 name = SYMBOL_DEMANGLED_NAME (msymbol);
1022 name = SYMBOL_NAME (msymbol);
1024 (name[0] == '-' || name[0] == '+') &&
1025 name[1] == '[') /* Got a method name. */
1026 if (regexp == NULL || re_exec(name+2) != 0)
1028 /* Compute length of classname part. */
1029 char *mystart = name + 2;
1030 char *myend = (char *) strchr(mystart, ' ');
1032 if (myend && (myend - mystart > maxlen))
1033 maxlen = myend - mystart;
1039 printf_filtered ("Classes matching \"%s\":\n\n",
1040 regexp ? regexp : "*");
1041 sym_arr = alloca (matches * sizeof (struct symbol *));
1043 ALL_MSYMBOLS (objfile, msymbol)
1046 name = SYMBOL_DEMANGLED_NAME (msymbol);
1048 name = SYMBOL_NAME (msymbol);
1050 (name[0] == '-' || name[0] == '+') &&
1051 name[1] == '[') /* Got a method name. */
1052 if (regexp == NULL || re_exec(name+2) != 0)
1053 sym_arr[matches++] = (struct symbol *) msymbol;
1056 qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
1058 /* Prevent compare on first iteration. */
1060 for (ix = 0; ix < matches; ix++) /* Now do the output. */
1065 name = SYMBOL_DEMANGLED_NAME (sym_arr[ix]);
1067 name = SYMBOL_NAME (sym_arr[ix]);
1069 if (p[0] && specialcmp(name, p) == 0)
1070 continue; /* Seen this one already (not unique). */
1072 /* Copy class part of method name. */
1073 while (*name && *name != ' ')
1076 /* Print in columns. */
1077 puts_filtered_tabular(aclass, maxlen + 1, 0);
1082 printf_filtered ("No classes matching \"%s\"\n", regexp ? regexp : "*");
1086 * Function: find_imps (char *selector, struct symbol **sym_arr)
1088 * Input: a string representing a selector
1089 * a pointer to an array of symbol pointers
1090 * possibly a pointer to a symbol found by the caller.
1092 * Output: number of methods that implement that selector. Side
1093 * effects: The array of symbol pointers is filled with matching syms.
1095 * By analogy with function "find_methods" (symtab.c), builds a list
1096 * of symbols matching the ambiguous input, so that "decode_line_2"
1097 * (symtab.c) can list them and ask the user to choose one or more.
1098 * In this case the matches are objective c methods
1099 * ("implementations") matching an objective c selector.
1101 * Note that it is possible for a normal (c-style) function to have
1102 * the same name as an objective c selector. To prevent the selector
1103 * from eclipsing the function, we allow the caller (decode_line_1) to
1104 * search for such a function first, and if it finds one, pass it in
1105 * to us. We will then integrate it into the list. We also search
1106 * for one here, among the minsyms.
1108 * NOTE: if NUM_DEBUGGABLE is non-zero, the sym_arr will be divided
1109 * into two parts: debuggable (struct symbol) syms, and
1110 * non_debuggable (struct minimal_symbol) syms. The debuggable
1111 * ones will come first, before NUM_DEBUGGABLE (which will thus
1112 * be the index of the first non-debuggable one).
1116 * Function: total_number_of_imps (char *selector);
1118 * Input: a string representing a selector
1119 * Output: number of methods that implement that selector.
1121 * By analogy with function "total_number_of_methods", this allows
1122 * decode_line_1 (symtab.c) to detect if there are objective c methods
1123 * matching the input, and to allocate an array of pointers to them
1124 * which can be manipulated by "decode_line_2" (also in symtab.c).
1128 parse_selector (char *method, char **selector)
1132 int found_quote = 0;
1134 char *nselector = NULL;
1136 CHECK (selector != NULL);
1140 while (isspace (*s1))
1147 while (isspace (*s1))
1154 if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
1156 else if (isspace (*s2))
1158 else if ((*s2 == '\0') || (*s2 == '\''))
1166 while (isspace (*s2))
1172 while (isspace (*s2))
1176 if (selector != NULL)
1177 *selector = nselector;
1183 parse_method (char *method, char *type, char **class,
1184 char **category, char **selector)
1188 int found_quote = 0;
1191 char *nclass = NULL;
1192 char *ncategory = NULL;
1193 char *nselector = NULL;
1195 CHECK (type != NULL);
1196 CHECK (class != NULL);
1197 CHECK (category != NULL);
1198 CHECK (selector != NULL);
1202 while (isspace (*s1))
1209 while (isspace (*s1))
1212 if ((s1[0] == '+') || (s1[0] == '-'))
1215 while (isspace (*s1))
1223 while (isalnum (*s1) || (*s1 == '_'))
1227 while (isspace (*s2))
1233 while (isspace (*s2))
1236 while (isalnum (*s2) || (*s2 == '_'))
1241 /* Truncate the class name now that we're not using the open paren. */
1248 if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
1250 else if (isspace (*s2))
1252 else if (*s2 == ']')
1261 while (isspace (*s2))
1268 while (isspace (*s2))
1276 if (category != NULL)
1277 *category = ncategory;
1278 if (selector != NULL)
1279 *selector = nselector;
1285 find_methods (struct symtab *symtab, char type,
1286 const char *class, const char *category,
1287 const char *selector, struct symbol **syms,
1288 unsigned int *nsym, unsigned int *ndebug)
1290 struct objfile *objfile = NULL;
1291 struct minimal_symbol *msymbol = NULL;
1292 struct block *block = NULL;
1293 struct symbol *sym = NULL;
1295 char *symname = NULL;
1298 char *nclass = NULL;
1299 char *ncategory = NULL;
1300 char *nselector = NULL;
1302 unsigned int csym = 0;
1303 unsigned int cdebug = 0;
1305 static char *tmp = NULL;
1306 static unsigned int tmplen = 0;
1308 CHECK (nsym != NULL);
1309 CHECK (ndebug != NULL);
1312 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1314 ALL_MSYMBOLS (objfile, msymbol)
1318 if ((msymbol->type != mst_text) && (msymbol->type != mst_file_text))
1319 /* Not a function or method. */
1323 if ((SYMBOL_VALUE_ADDRESS (msymbol) < BLOCK_START (block)) ||
1324 (SYMBOL_VALUE_ADDRESS (msymbol) >= BLOCK_END (block)))
1325 /* Not in the specified symtab. */
1328 symname = SYMBOL_DEMANGLED_NAME (msymbol);
1329 if (symname == NULL)
1330 symname = SYMBOL_NAME (msymbol);
1331 if (symname == NULL)
1334 if ((symname[0] != '-' && symname[0] != '+') || (symname[1] != '['))
1335 /* Not a method name. */
1338 while ((strlen (symname) + 1) >= tmplen)
1340 tmplen = (tmplen == 0) ? 1024 : tmplen * 2;
1341 tmp = xrealloc (tmp, tmplen);
1343 strcpy (tmp, symname);
1345 if (parse_method (tmp, &ntype, &nclass, &ncategory, &nselector) == NULL)
1348 if ((type != '\0') && (ntype != type))
1352 && ((nclass == NULL) || (strcmp (class, nclass) != 0)))
1355 if ((category != NULL) &&
1356 ((ncategory == NULL) || (strcmp (category, ncategory) != 0)))
1359 if ((selector != NULL) &&
1360 ((nselector == NULL) || (strcmp (selector, nselector) != 0)))
1363 sym = find_pc_function (SYMBOL_VALUE_ADDRESS (msymbol));
1366 const char *newsymname = SYMBOL_DEMANGLED_NAME (sym);
1368 if (newsymname == NULL)
1369 newsymname = SYMBOL_NAME (sym);
1370 if (strcmp (symname, newsymname) == 0)
1372 /* Found a high-level method sym: swap it into the
1373 lower part of sym_arr (below num_debuggable). */
1376 syms[csym] = syms[cdebug];
1385 "debugging symbol \"%s\" does not match minimal symbol (\"%s\"); ignoring",
1386 newsymname, symname);
1388 syms[csym] = (struct symbol *) msymbol;
1394 /* Found a non-debuggable method symbol. */
1396 syms[csym] = (struct symbol *) msymbol;
1407 char *find_imps (struct symtab *symtab, struct block *block,
1408 char *method, struct symbol **syms,
1409 unsigned int *nsym, unsigned int *ndebug)
1413 char *category = NULL;
1414 char *selector = NULL;
1416 unsigned int csym = 0;
1417 unsigned int cdebug = 0;
1419 unsigned int ncsym = 0;
1420 unsigned int ncdebug = 0;
1425 CHECK (nsym != NULL);
1426 CHECK (ndebug != NULL);
1433 buf = (char *) alloca (strlen (method) + 1);
1434 strcpy (buf, method);
1435 tmp = parse_method (buf, &type, &class, &category, &selector);
1439 struct symtab *sym_symtab = NULL;
1440 struct symbol *sym = NULL;
1441 struct minimal_symbol *msym = NULL;
1443 strcpy (buf, method);
1444 tmp = parse_selector (buf, &selector);
1449 sym = lookup_symbol (selector, block, VAR_NAMESPACE, 0, &sym_symtab);
1459 msym = lookup_minimal_symbol (selector, 0, 0);
1470 find_methods (symtab, type, class, category, selector,
1471 syms + csym, &ncsym, &ncdebug);
1473 find_methods (symtab, type, class, category, selector,
1474 NULL, &ncsym, &ncdebug);
1476 /* If we didn't find any methods, just return. */
1477 if (ncsym == 0 && ncdebug == 0)
1480 /* Take debug symbols from the second batch of symbols and swap them
1481 * with debug symbols from the first batch. Repeat until either the
1482 * second section is out of debug symbols or the first section is
1483 * full of debug symbols. Either way we have all debug symbols
1484 * packed to the beginning of the buffer.
1489 while ((cdebug < csym) && (ncdebug > 0))
1491 struct symbol *s = NULL;
1492 /* First non-debugging symbol. */
1493 unsigned int i = cdebug;
1494 /* Last of second batch of debug symbols. */
1495 unsigned int j = csym + ncdebug - 1;
1501 /* We've moved a symbol from the second debug section to the
1517 return method + (tmp - buf);
1521 /* Sort debuggable symbols. */
1523 qsort (syms, cdebug, sizeof (struct minimal_symbol *),
1526 /* Sort minimal_symbols. */
1527 if ((csym - cdebug) > 1)
1528 qsort (&syms[cdebug], csym - cdebug,
1529 sizeof (struct minimal_symbol *), compare_classes);
1531 /* Terminate the sym_arr list. */
1534 return method + (tmp - buf);
1538 print_object_command (char *args, int from_tty)
1540 struct value *object, *function, *description;
1541 CORE_ADDR string_addr;
1545 if (!args || !*args)
1547 "The 'print-object' command requires an argument (an Objective-C object)");
1550 struct expression *expr = parse_expression (args);
1551 register struct cleanup *old_chain =
1552 make_cleanup (free_current_contents, &expr);
1556 object = expr->language_defn->evaluate_exp (builtin_type_void_data_ptr,
1557 expr, &pc, EVAL_NORMAL);
1559 object = evaluate_subexp (builtin_type_void_data_ptr,
1560 expr, &pc, EVAL_NORMAL);
1562 do_cleanups (old_chain);
1565 function = find_function_in_inferior ("_NSPrintForDebugger");
1567 error ("Unable to locate _NSPrintForDebugger in child process");
1569 description = call_function_by_hand (function, 1, &object);
1571 string_addr = value_as_long (description);
1572 if (string_addr == 0)
1573 error ("object returns null description");
1575 read_memory (string_addr + i++, &c, 1);
1578 { /* Read and print characters up to EOS. */
1580 printf_filtered ("%c", c);
1581 read_memory (string_addr + i++, &c, 1);
1584 printf_filtered("<object returns empty description>");
1585 printf_filtered ("\n");
1588 /* The data structure 'methcalls' is used to detect method calls (thru
1589 * ObjC runtime lib functions objc_msgSend, objc_msgSendSuper, etc.),
1590 * and ultimately find the method being called.
1593 struct objc_methcall {
1595 /* Return instance method to be called. */
1596 CORE_ADDR (*stop_at) (CORE_ADDR);
1597 /* Start of pc range corresponding to method invocation. */
1599 /* End of pc range corresponding to method invocation. */
1603 static int resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc);
1604 static int resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
1605 static int resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc);
1606 static int resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
1608 static struct objc_methcall methcalls[] = {
1609 { "_objc_msgSend", resolve_msgsend, 0, 0},
1610 { "_objc_msgSend_stret", resolve_msgsend_stret, 0, 0},
1611 { "_objc_msgSendSuper", resolve_msgsend_super, 0, 0},
1612 { "_objc_msgSendSuper_stret", resolve_msgsend_super_stret, 0, 0},
1613 { "_objc_getClass", NULL, 0, 0},
1614 { "_objc_getMetaClass", NULL, 0, 0}
1617 #define nmethcalls (sizeof (methcalls) / sizeof (methcalls[0]))
1619 /* The following function, "find_objc_msgsend", fills in the data
1620 * structure "objc_msgs" by finding the addresses of each of the
1621 * (currently four) functions that it holds (of which objc_msgSend is
1622 * the first). This must be called each time symbols are loaded, in
1623 * case the functions have moved for some reason.
1627 find_objc_msgsend (void)
1630 for (i = 0; i < nmethcalls; i++) {
1632 struct minimal_symbol *func;
1634 /* Try both with and without underscore. */
1635 func = lookup_minimal_symbol (methcalls[i].name, NULL, NULL);
1636 if ((func == NULL) && (methcalls[i].name[0] == '_')) {
1637 func = lookup_minimal_symbol (methcalls[i].name + 1, NULL, NULL);
1640 methcalls[i].begin = 0;
1641 methcalls[i].end = 0;
1645 methcalls[i].begin = SYMBOL_VALUE_ADDRESS (func);
1647 methcalls[i].end = SYMBOL_VALUE_ADDRESS (++func);
1648 } while (methcalls[i].begin == methcalls[i].end);
1652 /* find_objc_msgcall (replaces pc_off_limits)
1654 * ALL that this function now does is to determine whether the input
1655 * address ("pc") is the address of one of the Objective-C message
1656 * dispatch functions (mainly objc_msgSend or objc_msgSendSuper), and
1657 * if so, it returns the address of the method that will be called.
1659 * The old function "pc_off_limits" used to do a lot of other things
1660 * in addition, such as detecting shared library jump stubs and
1661 * returning the address of the shlib function that would be called.
1662 * That functionality has been moved into the SKIP_TRAMPOLINE_CODE and
1663 * IN_SOLIB_TRAMPOLINE macros, which are resolved in the target-
1664 * dependent modules.
1667 struct objc_submethod_helper_data {
1668 CORE_ADDR (*f) (CORE_ADDR, CORE_ADDR *);
1674 find_objc_msgcall_submethod_helper (void * arg)
1676 struct objc_submethod_helper_data *s =
1677 (struct objc_submethod_helper_data *) arg;
1679 if (s->f (s->pc, s->new_pc) == 0)
1686 find_objc_msgcall_submethod (CORE_ADDR (*f) (CORE_ADDR, CORE_ADDR *),
1690 struct objc_submethod_helper_data s;
1696 if (catch_errors (find_objc_msgcall_submethod_helper,
1698 "Unable to determine target of Objective-C method call (ignoring):\n",
1699 RETURN_MASK_ALL) == 0)
1706 find_objc_msgcall (CORE_ADDR pc, CORE_ADDR *new_pc)
1710 find_objc_msgsend ();
1711 if (new_pc != NULL) { *new_pc = 0; }
1713 for (i = 0; i < nmethcalls; i++)
1714 if ((pc >= methcalls[i].begin) && (pc < methcalls[i].end))
1716 if (methcalls[i].stop_at != NULL)
1717 return find_objc_msgcall_submethod (methcalls[i].stop_at,
1727 _initialize_objc_language (void)
1729 add_language (&objc_language_defn);
1730 add_info ("selectors", selectors_info, /* INFO SELECTORS command. */
1731 "All Objective-C selectors, or those matching REGEXP.");
1732 add_info ("classes", classes_info, /* INFO CLASSES command. */
1733 "All Objective-C classes, or those matching REGEXP.");
1734 add_com ("print-object", class_vars, print_object_command,
1735 "Ask an Objective-C object to print itself.\n");
1736 add_com_alias ("po", "print-object", class_vars, 1);
1739 #if defined (__powerpc__) || defined (__ppc__)
1740 static unsigned long FETCH_ARGUMENT (int i)
1742 return read_register (3 + i);
1744 #elif defined (__i386__)
1745 static unsigned long FETCH_ARGUMENT (int i)
1747 CORE_ADDR stack = read_register (SP_REGNUM);
1748 return read_memory_unsigned_integer (stack + (4 * (i + 1)), 4);
1750 #elif defined (__sparc__)
1751 static unsigned long FETCH_ARGUMENT (int i)
1753 return read_register (O0_REGNUM + i);
1755 #elif defined (__hppa__) || defined (__hppa)
1756 static unsigned long FETCH_ARGUMENT (int i)
1758 return read_register (R0_REGNUM + 26 - i);
1761 #error unknown architecture
1764 #if defined (__hppa__) || defined (__hppa)
1765 static CORE_ADDR CONVERT_FUNCPTR (CORE_ADDR pc)
1768 pc = (CORE_ADDR) read_memory_integer (pc & ~0x3, 4);
1773 static CORE_ADDR CONVERT_FUNCPTR (CORE_ADDR pc)
1780 read_objc_method (CORE_ADDR addr, struct objc_method *method)
1782 method->name = read_memory_unsigned_integer (addr + 0, 4);
1783 method->types = read_memory_unsigned_integer (addr + 4, 4);
1784 method->imp = read_memory_unsigned_integer (addr + 8, 4);
1788 unsigned long read_objc_methlist_nmethods (CORE_ADDR addr)
1790 return read_memory_unsigned_integer (addr + 4, 4);
1794 read_objc_methlist_method (CORE_ADDR addr, unsigned long num,
1795 struct objc_method *method)
1797 CHECK_FATAL (num < read_objc_methlist_nmethods (addr));
1798 read_objc_method (addr + 8 + (12 * num), method);
1802 read_objc_object (CORE_ADDR addr, struct objc_object *object)
1804 object->isa = read_memory_unsigned_integer (addr, 4);
1808 read_objc_super (CORE_ADDR addr, struct objc_super *super)
1810 super->receiver = read_memory_unsigned_integer (addr, 4);
1811 super->class = read_memory_unsigned_integer (addr + 4, 4);
1815 read_objc_class (CORE_ADDR addr, struct objc_class *class)
1817 class->isa = read_memory_unsigned_integer (addr, 4);
1818 class->super_class = read_memory_unsigned_integer (addr + 4, 4);
1819 class->name = read_memory_unsigned_integer (addr + 8, 4);
1820 class->version = read_memory_unsigned_integer (addr + 12, 4);
1821 class->info = read_memory_unsigned_integer (addr + 16, 4);
1822 class->instance_size = read_memory_unsigned_integer (addr + 18, 4);
1823 class->ivars = read_memory_unsigned_integer (addr + 24, 4);
1824 class->methods = read_memory_unsigned_integer (addr + 28, 4);
1825 class->cache = read_memory_unsigned_integer (addr + 32, 4);
1826 class->protocols = read_memory_unsigned_integer (addr + 36, 4);
1830 find_implementation_from_class (CORE_ADDR class, CORE_ADDR sel)
1832 CORE_ADDR subclass = class;
1834 while (subclass != 0)
1837 struct objc_class class_str;
1838 unsigned mlistnum = 0;
1840 read_objc_class (subclass, &class_str);
1845 unsigned long nmethods;
1848 mlist = read_memory_unsigned_integer (class_str.methods +
1853 nmethods = read_objc_methlist_nmethods (mlist);
1855 for (i = 0; i < nmethods; i++)
1857 struct objc_method meth_str;
1858 read_objc_methlist_method (mlist, i, &meth_str);
1862 "checking method 0x%lx against selector 0x%lx\n",
1863 meth_str.name, sel);
1866 if (meth_str.name == sel)
1867 return CONVERT_FUNCPTR (meth_str.imp);
1871 subclass = class_str.super_class;
1878 find_implementation (CORE_ADDR object, CORE_ADDR sel)
1880 struct objc_object ostr;
1884 read_objc_object (object, &ostr);
1888 return find_implementation_from_class (ostr.isa, sel);
1892 resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc)
1898 object = FETCH_ARGUMENT (0);
1899 sel = FETCH_ARGUMENT (1);
1901 res = find_implementation (object, sel);
1910 resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
1916 object = FETCH_ARGUMENT (1);
1917 sel = FETCH_ARGUMENT (2);
1919 res = find_implementation (object, sel);
1928 resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc)
1930 struct objc_super sstr;
1936 super = FETCH_ARGUMENT (0);
1937 sel = FETCH_ARGUMENT (1);
1939 read_objc_super (super, &sstr);
1940 if (sstr.class == 0)
1943 res = find_implementation_from_class (sstr.class, sel);
1952 resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
1954 struct objc_super sstr;
1960 super = FETCH_ARGUMENT (1);
1961 sel = FETCH_ARGUMENT (2);
1963 read_objc_super (super, &sstr);
1964 if (sstr.class == 0)
1967 res = find_implementation_from_class (sstr.class, sel);