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 #if (!defined __GNUC__ || __GNUC__ < 2 || __GNUC_MINOR__ < (defined __cplusplus ? 6 : 4))
78 #define __CHECK_FUNCTION ((__const char *) 0)
80 #define __CHECK_FUNCTION __PRETTY_FUNCTION__
83 #define CHECK(expression) \
84 ((void) ((expression) ? 0 : gdb_check (#expression, __FILE__, __LINE__, \
87 #define CHECK_FATAL(expression) \
88 ((void) ((expression) ? 0 : gdb_check_fatal (#expression, __FILE__, \
89 __LINE__, __CHECK_FUNCTION)))
92 gdb_check (const char *str, const char *file,
93 unsigned int line, const char *func)
95 error ("assertion failure on line %u of \"%s\" in function \"%s\": %s\n",
96 line, file, func, str);
100 gdb_check_fatal (const char *str, const char *file,
101 unsigned int line, const char *func)
103 internal_error (file, line,
104 "assertion failure in function \"%s\": %s\n", func, str);
107 /* Lookup a structure type named "struct NAME", visible in lexical
108 block BLOCK. If NOERR is nonzero, return zero if NAME is not
112 lookup_struct_typedef (char *name, struct block *block, int noerr)
114 register struct symbol *sym;
116 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
117 (struct symtab **) NULL);
124 error ("No struct type named %s.", name);
126 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
131 error ("This context has class, union or enum %s, not a struct.",
138 lookup_objc_class (char *classname)
140 struct value * function, *classval;
142 if (! target_has_execution)
144 /* Can't call into inferior to lookup class. */
148 if (lookup_minimal_symbol("objc_lookUpClass", 0, 0))
149 function = find_function_in_inferior("objc_lookUpClass");
150 else if (lookup_minimal_symbol ("objc_lookup_class", 0, 0))
151 function = find_function_in_inferior("objc_lookup_class");
154 complaint (&symfile_complaints, "no way to lookup Objective-C classes",
159 classval = value_string (classname, strlen (classname) + 1);
160 classval = value_coerce_array (classval);
161 return (CORE_ADDR) value_as_long (call_function_by_hand (function,
166 lookup_child_selector (char *selname)
168 struct value * function, *selstring;
170 if (! target_has_execution)
172 /* Can't call into inferior to lookup selector. */
176 if (lookup_minimal_symbol("sel_getUid", 0, 0))
177 function = find_function_in_inferior("sel_getUid");
178 else if (lookup_minimal_symbol ("sel_get_any_uid", 0, 0))
179 function = find_function_in_inferior("sel_get_any_uid");
182 complaint (&symfile_complaints, "no way to lookup Objective-C selectors",
187 selstring = value_coerce_array (value_string (selname,
188 strlen (selname) + 1));
189 return value_as_long (call_function_by_hand (function, 1, &selstring));
193 value_nsstring (char *ptr, int len)
195 struct value *stringValue[3];
196 struct value *function, *nsstringValue;
200 if (!target_has_execution)
201 return 0; /* Can't call into inferior to create NSString. */
203 if (!(sym = lookup_struct_typedef("NSString", 0, 1)) &&
204 !(sym = lookup_struct_typedef("NXString", 0, 1)))
205 type = lookup_pointer_type(builtin_type_void);
207 type = lookup_pointer_type(SYMBOL_TYPE (sym));
209 stringValue[2] = value_string(ptr, len);
210 stringValue[2] = value_coerce_array(stringValue[2]);
211 /* _NSNewStringFromCString replaces "istr" after Lantern2A. */
212 if (lookup_minimal_symbol("_NSNewStringFromCString", 0, 0))
214 function = find_function_in_inferior("_NSNewStringFromCString");
215 nsstringValue = call_function_by_hand(function, 1, &stringValue[2]);
217 else if (lookup_minimal_symbol("istr", 0, 0))
219 function = find_function_in_inferior("istr");
220 nsstringValue = call_function_by_hand(function, 1, &stringValue[2]);
222 else if (lookup_minimal_symbol("+[NSString stringWithCString:]", 0, 0))
224 function = find_function_in_inferior("+[NSString stringWithCString:]");
225 stringValue[0] = value_from_longest
226 (builtin_type_long, lookup_objc_class ("NSString"));
227 stringValue[1] = value_from_longest
228 (builtin_type_long, lookup_child_selector ("stringWithCString:"));
229 nsstringValue = call_function_by_hand(function, 3, &stringValue[0]);
232 error ("NSString: internal error -- no way to create new NSString");
234 VALUE_TYPE(nsstringValue) = type;
235 return nsstringValue;
238 /* Objective-C name demangling. */
241 objc_demangle (const char *mangled)
243 char *demangled, *cp;
245 if (mangled[0] == '_' &&
246 (mangled[1] == 'i' || mangled[1] == 'c') &&
249 cp = demangled = xmalloc(strlen(mangled) + 2);
251 if (mangled[1] == 'i')
252 *cp++ = '-'; /* for instance method */
254 *cp++ = '+'; /* for class method */
256 *cp++ = '['; /* opening left brace */
257 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
259 while (*cp && *cp == '_')
260 cp++; /* skip any initial underbars in class name */
262 cp = strchr(cp, '_');
263 if (!cp) /* find first non-initial underbar */
265 xfree(demangled); /* not mangled name */
268 if (cp[1] == '_') { /* easy case: no category name */
269 *cp++ = ' '; /* replace two '_' with one ' ' */
270 strcpy(cp, mangled + (cp - demangled) + 2);
273 *cp++ = '('; /* less easy case: category name */
274 cp = strchr(cp, '_');
277 xfree(demangled); /* not mangled name */
281 *cp++ = ' '; /* overwriting 1st char of method name... */
282 strcpy(cp, mangled + (cp - demangled)); /* get it back */
285 while (*cp && *cp == '_')
286 cp++; /* skip any initial underbars in method name */
290 *cp = ':'; /* replace remaining '_' with ':' */
292 *cp++ = ']'; /* closing right brace */
293 *cp++ = 0; /* string terminator */
297 return NULL; /* Not an objc mangled name. */
300 /* Print the character C on STREAM as part of the contents of a
301 literal string whose delimiter is QUOTER. Note that that format
302 for printing characters and strings is language specific. */
305 objc_emit_char (register int c, struct ui_file *stream, int quoter)
308 c &= 0xFF; /* Avoid sign bit follies. */
310 if (PRINT_LITERAL_FORM (c))
312 if (c == '\\' || c == quoter)
314 fputs_filtered ("\\", stream);
316 fprintf_filtered (stream, "%c", c);
323 fputs_filtered ("\\n", stream);
326 fputs_filtered ("\\b", stream);
329 fputs_filtered ("\\t", stream);
332 fputs_filtered ("\\f", stream);
335 fputs_filtered ("\\r", stream);
338 fputs_filtered ("\\e", stream);
341 fputs_filtered ("\\a", stream);
344 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
351 objc_printchar (int c, struct ui_file *stream)
353 fputs_filtered ("'", stream);
354 objc_emit_char (c, stream, '\'');
355 fputs_filtered ("'", stream);
358 /* Print the character string STRING, printing at most LENGTH
359 characters. Printing stops early if the number hits print_max;
360 repeat counts are printed as appropriate. Print ellipses at the
361 end if we had to stop before printing LENGTH characters, or if
365 objc_printstr (struct ui_file *stream, char *string,
366 unsigned int length, int width, int force_ellipses)
368 register unsigned int i;
369 unsigned int things_printed = 0;
372 extern int inspect_it;
373 extern int repeat_count_threshold;
374 extern int print_max;
376 /* If the string was not truncated due to `set print elements', and
377 the last byte of it is a null, we don't print that, in
378 traditional C style. */
379 if ((!force_ellipses) && length > 0 && string[length-1] == '\0')
384 fputs_filtered ("\"\"", stream);
388 for (i = 0; i < length && things_printed < print_max; ++i)
390 /* Position of the character we are examining to see whether it
393 /* Number of repetitions we have detected so far. */
400 fputs_filtered (", ", stream);
406 while (rep1 < length && string[rep1] == string[i])
412 if (reps > repeat_count_threshold)
417 fputs_filtered ("\\\", ", stream);
419 fputs_filtered ("\", ", stream);
422 objc_printchar (string[i], stream);
423 fprintf_filtered (stream, " <repeats %u times>", reps);
425 things_printed += repeat_count_threshold;
433 fputs_filtered ("\\\"", stream);
435 fputs_filtered ("\"", stream);
438 objc_emit_char (string[i], stream, '"');
443 /* Terminate the quotes if necessary. */
447 fputs_filtered ("\\\"", stream);
449 fputs_filtered ("\"", stream);
452 if (force_ellipses || i < length)
453 fputs_filtered ("...", stream);
456 /* Create a fundamental C type using default reasonable for the
459 Some object/debugging file formats (DWARF version 1, COFF, etc) do
460 not define fundamental types such as "int" or "double". Others
461 (stabs or DWARF version 2, etc) do define fundamental types. For
462 the formats which don't provide fundamental types, gdb can create
463 such types using this function.
465 FIXME: Some compilers distinguish explicitly signed integral types
466 (signed short, signed int, signed long) from "regular" integral
467 types (short, int, long) in the debugging information. There is
468 some disagreement as to how useful this feature is. In particular,
469 gcc does not support this. Also, only some debugging formats allow
470 the distinction to be passed on to a debugger. For now, we always
471 just use "short", "int", or "long" as the type name, for both the
472 implicit and explicitly signed types. This also makes life easier
473 for the gdb test suite since we don't have to account for the
474 differences in output depending upon what the compiler and
475 debugging format support. We will probably have to re-examine the
476 issue when gdb starts taking it's fundamental type information
477 directly from the debugging information supplied by the compiler.
481 objc_create_fundamental_type (struct objfile *objfile, int typeid)
483 register struct type *type = NULL;
488 /* FIXME: For now, if we are asked to produce a type not in
489 this language, create the equivalent of a C integer type
490 with the name "<?type?>". When all the dust settles from
491 the type reconstruction work, this should probably become
493 type = init_type (TYPE_CODE_INT,
494 TARGET_INT_BIT / TARGET_CHAR_BIT,
495 0, "<?type?>", objfile);
496 warning ("internal error: no C/C++ fundamental type %d", typeid);
499 type = init_type (TYPE_CODE_VOID,
500 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
504 type = init_type (TYPE_CODE_INT,
505 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
509 type = init_type (TYPE_CODE_INT,
510 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
511 0, "signed char", objfile);
513 case FT_UNSIGNED_CHAR:
514 type = init_type (TYPE_CODE_INT,
515 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
516 TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
519 type = init_type (TYPE_CODE_INT,
520 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
521 0, "short", objfile);
523 case FT_SIGNED_SHORT:
524 type = init_type (TYPE_CODE_INT,
525 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
526 0, "short", objfile); /* FIXME-fnf */
528 case FT_UNSIGNED_SHORT:
529 type = init_type (TYPE_CODE_INT,
530 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
531 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
534 type = init_type (TYPE_CODE_INT,
535 TARGET_INT_BIT / TARGET_CHAR_BIT,
538 case FT_SIGNED_INTEGER:
539 type = init_type (TYPE_CODE_INT,
540 TARGET_INT_BIT / TARGET_CHAR_BIT,
541 0, "int", objfile); /* FIXME -fnf */
543 case FT_UNSIGNED_INTEGER:
544 type = init_type (TYPE_CODE_INT,
545 TARGET_INT_BIT / TARGET_CHAR_BIT,
546 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
549 type = init_type (TYPE_CODE_INT,
550 TARGET_LONG_BIT / TARGET_CHAR_BIT,
554 type = init_type (TYPE_CODE_INT,
555 TARGET_LONG_BIT / TARGET_CHAR_BIT,
556 0, "long", objfile); /* FIXME -fnf */
558 case FT_UNSIGNED_LONG:
559 type = init_type (TYPE_CODE_INT,
560 TARGET_LONG_BIT / TARGET_CHAR_BIT,
561 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
564 type = init_type (TYPE_CODE_INT,
565 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
566 0, "long long", objfile);
568 case FT_SIGNED_LONG_LONG:
569 type = init_type (TYPE_CODE_INT,
570 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
571 0, "signed long long", objfile);
573 case FT_UNSIGNED_LONG_LONG:
574 type = init_type (TYPE_CODE_INT,
575 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
576 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
579 type = init_type (TYPE_CODE_FLT,
580 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
581 0, "float", objfile);
583 case FT_DBL_PREC_FLOAT:
584 type = init_type (TYPE_CODE_FLT,
585 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
586 0, "double", objfile);
588 case FT_EXT_PREC_FLOAT:
589 type = init_type (TYPE_CODE_FLT,
590 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
591 0, "long double", objfile);
598 /* Table mapping opcodes into strings for printing operators
599 and precedences of the operators. */
601 static const struct op_print objc_op_print_tab[] =
603 {",", BINOP_COMMA, PREC_COMMA, 0},
604 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
605 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
606 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
607 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
608 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
609 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
610 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
611 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
612 {"<=", BINOP_LEQ, PREC_ORDER, 0},
613 {">=", BINOP_GEQ, PREC_ORDER, 0},
614 {">", BINOP_GTR, PREC_ORDER, 0},
615 {"<", BINOP_LESS, PREC_ORDER, 0},
616 {">>", BINOP_RSH, PREC_SHIFT, 0},
617 {"<<", BINOP_LSH, PREC_SHIFT, 0},
618 {"+", BINOP_ADD, PREC_ADD, 0},
619 {"-", BINOP_SUB, PREC_ADD, 0},
620 {"*", BINOP_MUL, PREC_MUL, 0},
621 {"/", BINOP_DIV, PREC_MUL, 0},
622 {"%", BINOP_REM, PREC_MUL, 0},
623 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
624 {"-", UNOP_NEG, PREC_PREFIX, 0},
625 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
626 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
627 {"*", UNOP_IND, PREC_PREFIX, 0},
628 {"&", UNOP_ADDR, PREC_PREFIX, 0},
629 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
630 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
631 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
635 struct type ** const (objc_builtin_types[]) =
642 &builtin_type_double,
644 &builtin_type_long_long,
645 &builtin_type_signed_char,
646 &builtin_type_unsigned_char,
647 &builtin_type_unsigned_short,
648 &builtin_type_unsigned_int,
649 &builtin_type_unsigned_long,
650 &builtin_type_unsigned_long_long,
651 &builtin_type_long_double,
652 &builtin_type_complex,
653 &builtin_type_double_complex,
657 const struct language_defn objc_language_defn = {
658 "objective-c", /* Language name */
666 evaluate_subexp_standard,
667 objc_printchar, /* Print a character constant */
668 objc_printstr, /* Function to print string constant */
670 objc_create_fundamental_type, /* Create fundamental type in this language */
671 c_print_type, /* Print a type using appropriate syntax */
672 c_val_print, /* Print a value using appropriate syntax */
673 c_value_print, /* Print a top-level value */
674 {"", "", "", ""}, /* Binary format info */
675 {"0%lo", "0", "o", ""}, /* Octal format info */
676 {"%ld", "", "d", ""}, /* Decimal format info */
677 {"0x%lx", "0x", "x", ""}, /* Hex format info */
678 objc_op_print_tab, /* Expression operators for printing */
679 1, /* C-style arrays */
680 0, /* String lower bound */
681 &builtin_type_char, /* Type of string elements */
687 * Following functions help construct Objective-C message calls
690 struct selname /* For parsing Objective-C. */
692 struct selname *next;
697 static int msglist_len;
698 static struct selname *selname_chain;
699 static char *msglist_sel;
704 register struct selname *new =
705 (struct selname *) xmalloc (sizeof (struct selname));
707 new->next = selname_chain;
708 new->msglist_len = msglist_len;
709 new->msglist_sel = msglist_sel;
711 msglist_sel = (char *)xmalloc(1);
717 add_msglist(struct stoken *str, int addcolon)
722 if (str == 0) { /* Unnamed arg, or... */
723 if (addcolon == 0) { /* variable number of args. */
733 len = plen + strlen(msglist_sel) + 2;
734 s = (char *)xmalloc(len);
735 strcpy(s, msglist_sel);
750 register int val = msglist_len;
751 register struct selname *sel = selname_chain;
752 register char *p = msglist_sel;
755 selname_chain = sel->next;
756 msglist_len = sel->msglist_len;
757 msglist_sel = sel->msglist_sel;
758 selid = lookup_child_selector(p);
760 error("Can't find selector \"%s\"", p);
761 write_exp_elt_longcst (selid);
763 write_exp_elt_longcst (val); /* Number of args */
770 * Function: specialcmp (char *a, char *b)
772 * Special strcmp: treats ']' and ' ' as end-of-string.
773 * Used for qsorting lists of objc methods (either by class or selector).
776 int specialcmp(char *a, char *b)
778 while (*a && *a != ' ' && *a != ']' && *b && *b != ' ' && *b != ']')
784 if (*a && *a != ' ' && *a != ']')
785 return 1; /* a is longer therefore greater */
786 if (*b && *b != ' ' && *b != ']')
787 return -1; /* a is shorter therefore lesser */
788 return 0; /* a and b are identical */
792 * Function: compare_selectors (const void *, const void *)
794 * Comparison function for use with qsort. Arguments are symbols or
795 * msymbols Compares selector part of objc method name alphabetically.
799 compare_selectors (const void *a, const void *b)
803 aname = SYMBOL_SOURCE_NAME (*(struct symbol **) a);
804 bname = SYMBOL_SOURCE_NAME (*(struct symbol **) b);
805 if (aname == NULL || bname == NULL)
806 error ("internal: compare_selectors(1)");
808 aname = strchr(aname, ' ');
809 bname = strchr(bname, ' ');
810 if (aname == NULL || bname == NULL)
811 error ("internal: compare_selectors(2)");
813 return specialcmp (aname+1, bname+1);
817 * Function: selectors_info (regexp, from_tty)
819 * Implements the "Info selectors" command. Takes an optional regexp
820 * arg. Lists all objective c selectors that match the regexp. Works
821 * by grepping thru all symbols for objective c methods. Output list
822 * is sorted and uniqued.
826 selectors_info (char *regexp, int from_tty)
828 struct objfile *objfile;
829 struct minimal_symbol *msymbol;
837 struct symbol **sym_arr;
841 strcpy(myregexp, ".*]"); /* Null input, match all objc methods. */
844 if (*regexp == '+' || *regexp == '-')
845 { /* User wants only class methods or only instance methods. */
846 plusminus = *regexp++;
847 while (*regexp == ' ' || *regexp == '\t')
851 strcpy(myregexp, ".*]");
854 strcpy(myregexp, regexp);
855 if (myregexp[strlen(myregexp) - 1] == '$') /* end of selector */
856 myregexp[strlen(myregexp) - 1] = ']'; /* end of method name */
858 strcat(myregexp, ".*]");
863 if (0 != (val = re_comp (myregexp)))
864 error ("Invalid regexp (%s): %s", val, regexp);
866 /* First time thru is JUST to get max length and count. */
867 ALL_MSYMBOLS (objfile, msymbol)
870 name = SYMBOL_DEMANGLED_NAME (msymbol);
872 name = SYMBOL_NAME (msymbol);
874 (name[0] == '-' || name[0] == '+') &&
875 name[1] == '[') /* Got a method name. */
877 /* Filter for class/instance methods. */
878 if (plusminus && name[0] != plusminus)
880 /* Find selector part. */
881 name = (char *) strchr(name+2, ' ');
882 if (regexp == NULL || re_exec(++name) != 0)
884 char *mystart = name;
885 char *myend = (char *) strchr(mystart, ']');
887 if (myend && (myend - mystart > maxlen))
888 maxlen = myend - mystart; /* Get longest selector. */
895 printf_filtered ("Selectors matching \"%s\":\n\n",
896 regexp ? regexp : "*");
898 sym_arr = alloca (matches * sizeof (struct symbol *));
900 ALL_MSYMBOLS (objfile, msymbol)
903 name = SYMBOL_DEMANGLED_NAME (msymbol);
905 name = SYMBOL_NAME (msymbol);
907 (name[0] == '-' || name[0] == '+') &&
908 name[1] == '[') /* Got a method name. */
910 /* Filter for class/instance methods. */
911 if (plusminus && name[0] != plusminus)
913 /* Find selector part. */
914 name = (char *) strchr(name+2, ' ');
915 if (regexp == NULL || re_exec(++name) != 0)
916 sym_arr[matches++] = (struct symbol *) msymbol;
920 qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
922 /* Prevent compare on first iteration. */
924 for (ix = 0; ix < matches; ix++) /* Now do the output. */
929 name = SYMBOL_DEMANGLED_NAME (sym_arr[ix]);
931 name = SYMBOL_NAME (sym_arr[ix]);
932 name = strchr (name, ' ') + 1;
933 if (p[0] && specialcmp(name, p) == 0)
934 continue; /* Seen this one already (not unique). */
936 /* Copy selector part. */
937 while (*name && *name != ']')
940 /* Print in columns. */
941 puts_filtered_tabular(asel, maxlen + 1, 0);
946 printf_filtered ("No selectors matching \"%s\"\n", regexp ? regexp : "*");
950 * Function: compare_classes (const void *, const void *)
952 * Comparison function for use with qsort. Arguments are symbols or
953 * msymbols Compares class part of objc method name alphabetically.
957 compare_classes (const void *a, const void *b)
961 aname = SYMBOL_SOURCE_NAME (*(struct symbol **) a);
962 bname = SYMBOL_SOURCE_NAME (*(struct symbol **) b);
963 if (aname == NULL || bname == NULL)
964 error ("internal: compare_classes(1)");
966 return specialcmp (aname+1, bname+1);
970 * Function: classes_info(regexp, from_tty)
972 * Implements the "info classes" command for objective c classes.
973 * Lists all objective c classes that match the optional regexp.
974 * Works by grepping thru the list of objective c methods. List will
975 * be sorted and uniqued (since one class may have many methods).
976 * BUGS: will not list a class that has no methods.
980 classes_info (char *regexp, int from_tty)
982 struct objfile *objfile;
983 struct minimal_symbol *msymbol;
991 struct symbol **sym_arr;
994 strcpy(myregexp, ".* "); /* Null input: match all objc classes. */
997 strcpy(myregexp, regexp);
998 if (myregexp[strlen(myregexp) - 1] == '$')
999 /* In the method name, the end of the class name is marked by ' '. */
1000 myregexp[strlen(myregexp) - 1] = ' ';
1002 strcat(myregexp, ".* ");
1006 if (0 != (val = re_comp (myregexp)))
1007 error ("Invalid regexp (%s): %s", val, regexp);
1009 /* First time thru is JUST to get max length and count. */
1010 ALL_MSYMBOLS (objfile, msymbol)
1013 name = SYMBOL_DEMANGLED_NAME (msymbol);
1015 name = SYMBOL_NAME (msymbol);
1017 (name[0] == '-' || name[0] == '+') &&
1018 name[1] == '[') /* Got a method name. */
1019 if (regexp == NULL || re_exec(name+2) != 0)
1021 /* Compute length of classname part. */
1022 char *mystart = name + 2;
1023 char *myend = (char *) strchr(mystart, ' ');
1025 if (myend && (myend - mystart > maxlen))
1026 maxlen = myend - mystart;
1032 printf_filtered ("Classes matching \"%s\":\n\n",
1033 regexp ? regexp : "*");
1034 sym_arr = alloca (matches * sizeof (struct symbol *));
1036 ALL_MSYMBOLS (objfile, msymbol)
1039 name = SYMBOL_DEMANGLED_NAME (msymbol);
1041 name = SYMBOL_NAME (msymbol);
1043 (name[0] == '-' || name[0] == '+') &&
1044 name[1] == '[') /* Got a method name. */
1045 if (regexp == NULL || re_exec(name+2) != 0)
1046 sym_arr[matches++] = (struct symbol *) msymbol;
1049 qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
1051 /* Prevent compare on first iteration. */
1053 for (ix = 0; ix < matches; ix++) /* Now do the output. */
1058 name = SYMBOL_DEMANGLED_NAME (sym_arr[ix]);
1060 name = SYMBOL_NAME (sym_arr[ix]);
1062 if (p[0] && specialcmp(name, p) == 0)
1063 continue; /* Seen this one already (not unique). */
1065 /* Copy class part of method name. */
1066 while (*name && *name != ' ')
1069 /* Print in columns. */
1070 puts_filtered_tabular(aclass, maxlen + 1, 0);
1075 printf_filtered ("No classes matching \"%s\"\n", regexp ? regexp : "*");
1079 * Function: find_imps (char *selector, struct symbol **sym_arr)
1081 * Input: a string representing a selector
1082 * a pointer to an array of symbol pointers
1083 * possibly a pointer to a symbol found by the caller.
1085 * Output: number of methods that implement that selector. Side
1086 * effects: The array of symbol pointers is filled with matching syms.
1088 * By analogy with function "find_methods" (symtab.c), builds a list
1089 * of symbols matching the ambiguous input, so that "decode_line_2"
1090 * (symtab.c) can list them and ask the user to choose one or more.
1091 * In this case the matches are objective c methods
1092 * ("implementations") matching an objective c selector.
1094 * Note that it is possible for a normal (c-style) function to have
1095 * the same name as an objective c selector. To prevent the selector
1096 * from eclipsing the function, we allow the caller (decode_line_1) to
1097 * search for such a function first, and if it finds one, pass it in
1098 * to us. We will then integrate it into the list. We also search
1099 * for one here, among the minsyms.
1101 * NOTE: if NUM_DEBUGGABLE is non-zero, the sym_arr will be divided
1102 * into two parts: debuggable (struct symbol) syms, and
1103 * non_debuggable (struct minimal_symbol) syms. The debuggable
1104 * ones will come first, before NUM_DEBUGGABLE (which will thus
1105 * be the index of the first non-debuggable one).
1109 * Function: total_number_of_imps (char *selector);
1111 * Input: a string representing a selector
1112 * Output: number of methods that implement that selector.
1114 * By analogy with function "total_number_of_methods", this allows
1115 * decode_line_1 (symtab.c) to detect if there are objective c methods
1116 * matching the input, and to allocate an array of pointers to them
1117 * which can be manipulated by "decode_line_2" (also in symtab.c).
1121 parse_selector (char *method, char **selector)
1125 int found_quote = 0;
1127 char *nselector = NULL;
1129 CHECK (selector != NULL);
1133 while (isspace (*s1))
1140 while (isspace (*s1))
1147 if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
1149 else if (isspace (*s2))
1151 else if ((*s2 == '\0') || (*s2 == '\''))
1159 while (isspace (*s2))
1165 while (isspace (*s2))
1169 if (selector != NULL)
1170 *selector = nselector;
1176 parse_method (char *method, char *type, char **class,
1177 char **category, char **selector)
1181 int found_quote = 0;
1184 char *nclass = NULL;
1185 char *ncategory = NULL;
1186 char *nselector = NULL;
1188 CHECK (type != NULL);
1189 CHECK (class != NULL);
1190 CHECK (category != NULL);
1191 CHECK (selector != NULL);
1195 while (isspace (*s1))
1202 while (isspace (*s1))
1205 if ((s1[0] == '+') || (s1[0] == '-'))
1208 while (isspace (*s1))
1216 while (isalnum (*s1) || (*s1 == '_'))
1220 while (isspace (*s2))
1226 while (isspace (*s2))
1229 while (isalnum (*s2) || (*s2 == '_'))
1234 /* Truncate the class name now that we're not using the open paren. */
1241 if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
1243 else if (isspace (*s2))
1245 else if (*s2 == ']')
1254 while (isspace (*s2))
1261 while (isspace (*s2))
1269 if (category != NULL)
1270 *category = ncategory;
1271 if (selector != NULL)
1272 *selector = nselector;
1278 find_methods (struct symtab *symtab, char type,
1279 const char *class, const char *category,
1280 const char *selector, struct symbol **syms,
1281 unsigned int *nsym, unsigned int *ndebug)
1283 struct objfile *objfile = NULL;
1284 struct minimal_symbol *msymbol = NULL;
1285 struct block *block = NULL;
1286 struct symbol *sym = NULL;
1288 char *symname = NULL;
1291 char *nclass = NULL;
1292 char *ncategory = NULL;
1293 char *nselector = NULL;
1295 unsigned int csym = 0;
1296 unsigned int cdebug = 0;
1298 static char *tmp = NULL;
1299 static unsigned int tmplen = 0;
1301 CHECK (nsym != NULL);
1302 CHECK (ndebug != NULL);
1305 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1307 ALL_MSYMBOLS (objfile, msymbol)
1311 if ((msymbol->type != mst_text) && (msymbol->type != mst_file_text))
1312 /* Not a function or method. */
1316 if ((SYMBOL_VALUE_ADDRESS (msymbol) < BLOCK_START (block)) ||
1317 (SYMBOL_VALUE_ADDRESS (msymbol) >= BLOCK_END (block)))
1318 /* Not in the specified symtab. */
1321 symname = SYMBOL_DEMANGLED_NAME (msymbol);
1322 if (symname == NULL)
1323 symname = SYMBOL_NAME (msymbol);
1324 if (symname == NULL)
1327 if ((symname[0] != '-' && symname[0] != '+') || (symname[1] != '['))
1328 /* Not a method name. */
1331 while ((strlen (symname) + 1) >= tmplen)
1333 tmplen = (tmplen == 0) ? 1024 : tmplen * 2;
1334 tmp = xrealloc (tmp, tmplen);
1336 strcpy (tmp, symname);
1338 if (parse_method (tmp, &ntype, &nclass, &ncategory, &nselector) == NULL)
1341 if ((type != '\0') && (ntype != type))
1345 && ((nclass == NULL) || (strcmp (class, nclass) != 0)))
1348 if ((category != NULL) &&
1349 ((ncategory == NULL) || (strcmp (category, ncategory) != 0)))
1352 if ((selector != NULL) &&
1353 ((nselector == NULL) || (strcmp (selector, nselector) != 0)))
1356 sym = find_pc_function (SYMBOL_VALUE_ADDRESS (msymbol));
1359 const char *newsymname = SYMBOL_DEMANGLED_NAME (sym);
1361 if (newsymname == NULL)
1362 newsymname = SYMBOL_NAME (sym);
1363 if (strcmp (symname, newsymname) == 0)
1365 /* Found a high-level method sym: swap it into the
1366 lower part of sym_arr (below num_debuggable). */
1369 syms[csym] = syms[cdebug];
1378 "debugging symbol \"%s\" does not match minimal symbol (\"%s\"); ignoring",
1379 newsymname, symname);
1381 syms[csym] = (struct symbol *) msymbol;
1387 /* Found a non-debuggable method symbol. */
1389 syms[csym] = (struct symbol *) msymbol;
1400 char *find_imps (struct symtab *symtab, struct block *block,
1401 char *method, struct symbol **syms,
1402 unsigned int *nsym, unsigned int *ndebug)
1406 char *category = NULL;
1407 char *selector = NULL;
1409 unsigned int csym = 0;
1410 unsigned int cdebug = 0;
1412 unsigned int ncsym = 0;
1413 unsigned int ncdebug = 0;
1418 CHECK (nsym != NULL);
1419 CHECK (ndebug != NULL);
1426 buf = (char *) alloca (strlen (method) + 1);
1427 strcpy (buf, method);
1428 tmp = parse_method (buf, &type, &class, &category, &selector);
1432 struct symtab *sym_symtab = NULL;
1433 struct symbol *sym = NULL;
1434 struct minimal_symbol *msym = NULL;
1436 strcpy (buf, method);
1437 tmp = parse_selector (buf, &selector);
1442 sym = lookup_symbol (selector, block, VAR_NAMESPACE, 0, &sym_symtab);
1452 msym = lookup_minimal_symbol (selector, 0, 0);
1457 syms[csym] = (struct symbol *)msym;
1463 find_methods (symtab, type, class, category, selector,
1464 syms + csym, &ncsym, &ncdebug);
1466 find_methods (symtab, type, class, category, selector,
1467 NULL, &ncsym, &ncdebug);
1469 /* If we didn't find any methods, just return. */
1470 if (ncsym == 0 && ncdebug == 0)
1473 /* Take debug symbols from the second batch of symbols and swap them
1474 * with debug symbols from the first batch. Repeat until either the
1475 * second section is out of debug symbols or the first section is
1476 * full of debug symbols. Either way we have all debug symbols
1477 * packed to the beginning of the buffer.
1482 while ((cdebug < csym) && (ncdebug > 0))
1484 struct symbol *s = NULL;
1485 /* First non-debugging symbol. */
1486 unsigned int i = cdebug;
1487 /* Last of second batch of debug symbols. */
1488 unsigned int j = csym + ncdebug - 1;
1494 /* We've moved a symbol from the second debug section to the
1510 return method + (tmp - buf);
1514 /* Sort debuggable symbols. */
1516 qsort (syms, cdebug, sizeof (struct minimal_symbol *),
1519 /* Sort minimal_symbols. */
1520 if ((csym - cdebug) > 1)
1521 qsort (&syms[cdebug], csym - cdebug,
1522 sizeof (struct minimal_symbol *), compare_classes);
1524 /* Terminate the sym_arr list. */
1527 return method + (tmp - buf);
1531 print_object_command (char *args, int from_tty)
1533 struct value *object, *function, *description;
1534 CORE_ADDR string_addr, object_addr;
1538 if (!args || !*args)
1540 "The 'print-object' command requires an argument (an Objective-C object)");
1543 struct expression *expr = parse_expression (args);
1544 register struct cleanup *old_chain =
1545 make_cleanup (free_current_contents, &expr);
1548 object = expr->language_defn->evaluate_exp (builtin_type_void_data_ptr,
1549 expr, &pc, EVAL_NORMAL);
1550 do_cleanups (old_chain);
1553 /* Validate the address for sanity. */
1554 object_addr = value_as_long (object);
1555 read_memory (object_addr, &c, 1);
1557 function = find_function_in_inferior ("_NSPrintForDebugger");
1558 if (function == NULL)
1559 error ("Unable to locate _NSPrintForDebugger in child process");
1561 description = call_function_by_hand (function, 1, &object);
1563 string_addr = value_as_long (description);
1564 if (string_addr == 0)
1565 error ("object returns null description");
1567 read_memory (string_addr + i++, &c, 1);
1570 { /* Read and print characters up to EOS. */
1572 printf_filtered ("%c", c);
1573 read_memory (string_addr + i++, &c, 1);
1576 printf_filtered("<object returns empty description>");
1577 printf_filtered ("\n");
1580 /* The data structure 'methcalls' is used to detect method calls (thru
1581 * ObjC runtime lib functions objc_msgSend, objc_msgSendSuper, etc.),
1582 * and ultimately find the method being called.
1585 struct objc_methcall {
1587 /* Return instance method to be called. */
1588 int (*stop_at) (CORE_ADDR, CORE_ADDR *);
1589 /* Start of pc range corresponding to method invocation. */
1591 /* End of pc range corresponding to method invocation. */
1595 static int resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc);
1596 static int resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
1597 static int resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc);
1598 static int resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
1600 static struct objc_methcall methcalls[] = {
1601 { "_objc_msgSend", resolve_msgsend, 0, 0},
1602 { "_objc_msgSend_stret", resolve_msgsend_stret, 0, 0},
1603 { "_objc_msgSendSuper", resolve_msgsend_super, 0, 0},
1604 { "_objc_msgSendSuper_stret", resolve_msgsend_super_stret, 0, 0},
1605 { "_objc_getClass", NULL, 0, 0},
1606 { "_objc_getMetaClass", NULL, 0, 0}
1609 #define nmethcalls (sizeof (methcalls) / sizeof (methcalls[0]))
1611 /* The following function, "find_objc_msgsend", fills in the data
1612 * structure "objc_msgs" by finding the addresses of each of the
1613 * (currently four) functions that it holds (of which objc_msgSend is
1614 * the first). This must be called each time symbols are loaded, in
1615 * case the functions have moved for some reason.
1619 find_objc_msgsend (void)
1622 for (i = 0; i < nmethcalls; i++) {
1624 struct minimal_symbol *func;
1626 /* Try both with and without underscore. */
1627 func = lookup_minimal_symbol (methcalls[i].name, NULL, NULL);
1628 if ((func == NULL) && (methcalls[i].name[0] == '_')) {
1629 func = lookup_minimal_symbol (methcalls[i].name + 1, NULL, NULL);
1632 methcalls[i].begin = 0;
1633 methcalls[i].end = 0;
1637 methcalls[i].begin = SYMBOL_VALUE_ADDRESS (func);
1639 methcalls[i].end = SYMBOL_VALUE_ADDRESS (++func);
1640 } while (methcalls[i].begin == methcalls[i].end);
1644 /* find_objc_msgcall (replaces pc_off_limits)
1646 * ALL that this function now does is to determine whether the input
1647 * address ("pc") is the address of one of the Objective-C message
1648 * dispatch functions (mainly objc_msgSend or objc_msgSendSuper), and
1649 * if so, it returns the address of the method that will be called.
1651 * The old function "pc_off_limits" used to do a lot of other things
1652 * in addition, such as detecting shared library jump stubs and
1653 * returning the address of the shlib function that would be called.
1654 * That functionality has been moved into the SKIP_TRAMPOLINE_CODE and
1655 * IN_SOLIB_TRAMPOLINE macros, which are resolved in the target-
1656 * dependent modules.
1659 struct objc_submethod_helper_data {
1660 int (*f) (CORE_ADDR, CORE_ADDR *);
1666 find_objc_msgcall_submethod_helper (void * arg)
1668 struct objc_submethod_helper_data *s =
1669 (struct objc_submethod_helper_data *) arg;
1671 if (s->f (s->pc, s->new_pc) == 0)
1678 find_objc_msgcall_submethod (int (*f) (CORE_ADDR, CORE_ADDR *),
1682 struct objc_submethod_helper_data s;
1688 if (catch_errors (find_objc_msgcall_submethod_helper,
1690 "Unable to determine target of Objective-C method call (ignoring):\n",
1691 RETURN_MASK_ALL) == 0)
1698 find_objc_msgcall (CORE_ADDR pc, CORE_ADDR *new_pc)
1702 find_objc_msgsend ();
1703 if (new_pc != NULL) { *new_pc = 0; }
1705 for (i = 0; i < nmethcalls; i++)
1706 if ((pc >= methcalls[i].begin) && (pc < methcalls[i].end))
1708 if (methcalls[i].stop_at != NULL)
1709 return find_objc_msgcall_submethod (methcalls[i].stop_at,
1719 _initialize_objc_language (void)
1721 add_language (&objc_language_defn);
1722 add_info ("selectors", selectors_info, /* INFO SELECTORS command. */
1723 "All Objective-C selectors, or those matching REGEXP.");
1724 add_info ("classes", classes_info, /* INFO CLASSES command. */
1725 "All Objective-C classes, or those matching REGEXP.");
1726 add_com ("print-object", class_vars, print_object_command,
1727 "Ask an Objective-C object to print itself.");
1728 add_com_alias ("po", "print-object", class_vars, 1);
1731 #if defined (__powerpc__) || defined (__ppc__)
1732 static unsigned long FETCH_ARGUMENT (int i)
1734 return read_register (3 + i);
1736 #elif defined (__i386__)
1737 static unsigned long FETCH_ARGUMENT (int i)
1739 CORE_ADDR stack = read_register (SP_REGNUM);
1740 return read_memory_unsigned_integer (stack + (4 * (i + 1)), 4);
1742 #elif defined (__sparc__)
1743 static unsigned long FETCH_ARGUMENT (int i)
1745 return read_register (O0_REGNUM + i);
1747 #elif defined (__hppa__) || defined (__hppa)
1748 static unsigned long FETCH_ARGUMENT (int i)
1750 return read_register (R0_REGNUM + 26 - i);
1753 #error unknown architecture
1756 #if defined (__hppa__) || defined (__hppa)
1757 static CORE_ADDR CONVERT_FUNCPTR (CORE_ADDR pc)
1760 pc = (CORE_ADDR) read_memory_integer (pc & ~0x3, 4);
1765 static CORE_ADDR CONVERT_FUNCPTR (CORE_ADDR pc)
1772 read_objc_method (CORE_ADDR addr, struct objc_method *method)
1774 method->name = read_memory_unsigned_integer (addr + 0, 4);
1775 method->types = read_memory_unsigned_integer (addr + 4, 4);
1776 method->imp = read_memory_unsigned_integer (addr + 8, 4);
1780 unsigned long read_objc_methlist_nmethods (CORE_ADDR addr)
1782 return read_memory_unsigned_integer (addr + 4, 4);
1786 read_objc_methlist_method (CORE_ADDR addr, unsigned long num,
1787 struct objc_method *method)
1789 CHECK_FATAL (num < read_objc_methlist_nmethods (addr));
1790 read_objc_method (addr + 8 + (12 * num), method);
1794 read_objc_object (CORE_ADDR addr, struct objc_object *object)
1796 object->isa = read_memory_unsigned_integer (addr, 4);
1800 read_objc_super (CORE_ADDR addr, struct objc_super *super)
1802 super->receiver = read_memory_unsigned_integer (addr, 4);
1803 super->class = read_memory_unsigned_integer (addr + 4, 4);
1807 read_objc_class (CORE_ADDR addr, struct objc_class *class)
1809 class->isa = read_memory_unsigned_integer (addr, 4);
1810 class->super_class = read_memory_unsigned_integer (addr + 4, 4);
1811 class->name = read_memory_unsigned_integer (addr + 8, 4);
1812 class->version = read_memory_unsigned_integer (addr + 12, 4);
1813 class->info = read_memory_unsigned_integer (addr + 16, 4);
1814 class->instance_size = read_memory_unsigned_integer (addr + 18, 4);
1815 class->ivars = read_memory_unsigned_integer (addr + 24, 4);
1816 class->methods = read_memory_unsigned_integer (addr + 28, 4);
1817 class->cache = read_memory_unsigned_integer (addr + 32, 4);
1818 class->protocols = read_memory_unsigned_integer (addr + 36, 4);
1822 find_implementation_from_class (CORE_ADDR class, CORE_ADDR sel)
1824 CORE_ADDR subclass = class;
1826 while (subclass != 0)
1829 struct objc_class class_str;
1830 unsigned mlistnum = 0;
1832 read_objc_class (subclass, &class_str);
1837 unsigned long nmethods;
1840 mlist = read_memory_unsigned_integer (class_str.methods +
1845 nmethods = read_objc_methlist_nmethods (mlist);
1847 for (i = 0; i < nmethods; i++)
1849 struct objc_method meth_str;
1850 read_objc_methlist_method (mlist, i, &meth_str);
1854 "checking method 0x%lx against selector 0x%lx\n",
1855 meth_str.name, sel);
1858 if (meth_str.name == sel)
1859 return CONVERT_FUNCPTR (meth_str.imp);
1863 subclass = class_str.super_class;
1870 find_implementation (CORE_ADDR object, CORE_ADDR sel)
1872 struct objc_object ostr;
1876 read_objc_object (object, &ostr);
1880 return find_implementation_from_class (ostr.isa, sel);
1884 resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc)
1890 object = FETCH_ARGUMENT (0);
1891 sel = FETCH_ARGUMENT (1);
1893 res = find_implementation (object, sel);
1902 resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
1908 object = FETCH_ARGUMENT (1);
1909 sel = FETCH_ARGUMENT (2);
1911 res = find_implementation (object, sel);
1920 resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc)
1922 struct objc_super sstr;
1928 super = FETCH_ARGUMENT (0);
1929 sel = FETCH_ARGUMENT (1);
1931 read_objc_super (super, &sstr);
1932 if (sstr.class == 0)
1935 res = find_implementation_from_class (sstr.class, sel);
1944 resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
1946 struct objc_super sstr;
1952 super = FETCH_ARGUMENT (1);
1953 sel = FETCH_ARGUMENT (2);
1955 read_objc_super (super, &sstr);
1956 if (sstr.class == 0)
1959 res = find_implementation_from_class (sstr.class, sel);