gdb/objc-lang.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Objective-C language support routines for GDB, the GNU debugger.

  2.    Copyright (C) 2002-2015 Free Software Foundation, Inc.

  3.    Contributed by Apple Computer, Inc.
  4.    Written by Michael Snyder.

  5.    This file is part of GDB.

  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (at your option) any later version.

  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.

  14.    You should have received a copy of the GNU General Public License
  15.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  16. #include "defs.h"
  17. #include "symtab.h"
  18. #include "gdbtypes.h"
  19. #include "expression.h"
  20. #include "parser-defs.h"
  21. #include "language.h"
  22. #include "varobj.h"
  23. #include "c-lang.h"
  24. #include "objc-lang.h"
  25. #include "complaints.h"
  26. #include "value.h"
  27. #include "symfile.h"
  28. #include "objfiles.h"
  29. #include "target.h"                /* for target_has_execution */
  30. #include "gdbcore.h"
  31. #include "gdbcmd.h"
  32. #include "frame.h"
  33. #include "gdb_regex.h"
  34. #include "regcache.h"
  35. #include "block.h"
  36. #include "infcall.h"
  37. #include "valprint.h"
  38. #include "cli/cli-utils.h"

  39. #include <ctype.h>

  40. struct objc_object {
  41.   CORE_ADDR isa;
  42. };

  43. struct objc_class {
  44.   CORE_ADDR isa;
  45.   CORE_ADDR super_class;
  46.   CORE_ADDR name;
  47.   long version;
  48.   long info;
  49.   long instance_size;
  50.   CORE_ADDR ivars;
  51.   CORE_ADDR methods;
  52.   CORE_ADDR cache;
  53.   CORE_ADDR protocols;
  54. };

  55. struct objc_super {
  56.   CORE_ADDR receiver;
  57.   CORE_ADDR class;
  58. };

  59. struct objc_method {
  60.   CORE_ADDR name;
  61.   CORE_ADDR types;
  62.   CORE_ADDR imp;
  63. };

  64. static const struct objfile_data *objc_objfile_data;

  65. /* Lookup a structure type named "struct NAME", visible in lexical
  66.    block BLOCK.  If NOERR is nonzero, return zero if NAME is not
  67.    suitably defined.  */

  68. struct symbol *
  69. lookup_struct_typedef (char *name, const struct block *block, int noerr)
  70. {
  71.   struct symbol *sym;

  72.   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);

  73.   if (sym == NULL)
  74.     {
  75.       if (noerr)
  76.         return 0;
  77.       else
  78.         error (_("No struct type named %s."), name);
  79.     }
  80.   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
  81.     {
  82.       if (noerr)
  83.         return 0;
  84.       else
  85.         error (_("This context has class, union or enum %s, not a struct."),
  86.                name);
  87.     }
  88.   return sym;
  89. }

  90. CORE_ADDR
  91. lookup_objc_class (struct gdbarch *gdbarch, char *classname)
  92. {
  93.   struct type *char_type = builtin_type (gdbarch)->builtin_char;
  94.   struct value * function, *classval;

  95.   if (! target_has_execution)
  96.     {
  97.       /* Can't call into inferior to lookup class.  */
  98.       return 0;
  99.     }

  100.   if (lookup_minimal_symbol("objc_lookUpClass", 0, 0).minsym)
  101.     function = find_function_in_inferior("objc_lookUpClass", NULL);
  102.   else if (lookup_minimal_symbol ("objc_lookup_class", 0, 0).minsym)
  103.     function = find_function_in_inferior("objc_lookup_class", NULL);
  104.   else
  105.     {
  106.       complaint (&symfile_complaints,
  107.                  _("no way to lookup Objective-C classes"));
  108.       return 0;
  109.     }

  110.   classval = value_string (classname, strlen (classname) + 1, char_type);
  111.   classval = value_coerce_array (classval);
  112.   return (CORE_ADDR) value_as_long (call_function_by_hand (function,
  113.                                                            1, &classval));
  114. }

  115. CORE_ADDR
  116. lookup_child_selector (struct gdbarch *gdbarch, char *selname)
  117. {
  118.   struct type *char_type = builtin_type (gdbarch)->builtin_char;
  119.   struct value * function, *selstring;

  120.   if (! target_has_execution)
  121.     {
  122.       /* Can't call into inferior to lookup selector.  */
  123.       return 0;
  124.     }

  125.   if (lookup_minimal_symbol("sel_getUid", 0, 0).minsym)
  126.     function = find_function_in_inferior("sel_getUid", NULL);
  127.   else if (lookup_minimal_symbol ("sel_get_any_uid", 0, 0).minsym)
  128.     function = find_function_in_inferior("sel_get_any_uid", NULL);
  129.   else
  130.     {
  131.       complaint (&symfile_complaints,
  132.                  _("no way to lookup Objective-C selectors"));
  133.       return 0;
  134.     }

  135.   selstring = value_coerce_array (value_string (selname,
  136.                                                 strlen (selname) + 1,
  137.                                                 char_type));
  138.   return value_as_long (call_function_by_hand (function, 1, &selstring));
  139. }

  140. struct value *
  141. value_nsstring (struct gdbarch *gdbarch, char *ptr, int len)
  142. {
  143.   struct type *char_type = builtin_type (gdbarch)->builtin_char;
  144.   struct value *stringValue[3];
  145.   struct value *function, *nsstringValue;
  146.   struct symbol *sym;
  147.   struct type *type;

  148.   if (!target_has_execution)
  149.     return 0;                /* Can't call into inferior to create NSString.  */

  150.   stringValue[2] = value_string(ptr, len, char_type);
  151.   stringValue[2] = value_coerce_array(stringValue[2]);
  152.   /* _NSNewStringFromCString replaces "istr" after Lantern2A.  */
  153.   if (lookup_minimal_symbol("_NSNewStringFromCString", 0, 0).minsym)
  154.     {
  155.       function = find_function_in_inferior("_NSNewStringFromCString", NULL);
  156.       nsstringValue = call_function_by_hand(function, 1, &stringValue[2]);
  157.     }
  158.   else if (lookup_minimal_symbol("istr", 0, 0).minsym)
  159.     {
  160.       function = find_function_in_inferior("istr", NULL);
  161.       nsstringValue = call_function_by_hand(function, 1, &stringValue[2]);
  162.     }
  163.   else if (lookup_minimal_symbol("+[NSString stringWithCString:]", 0, 0).minsym)
  164.     {
  165.       function
  166.         = find_function_in_inferior("+[NSString stringWithCString:]", NULL);
  167.       type = builtin_type (gdbarch)->builtin_long;

  168.       stringValue[0] = value_from_longest
  169.         (type, lookup_objc_class (gdbarch, "NSString"));
  170.       stringValue[1] = value_from_longest
  171.         (type, lookup_child_selector (gdbarch, "stringWithCString:"));
  172.       nsstringValue = call_function_by_hand(function, 3, &stringValue[0]);
  173.     }
  174.   else
  175.     error (_("NSString: internal error -- no way to create new NSString"));

  176.   sym = lookup_struct_typedef("NSString", 0, 1);
  177.   if (sym == NULL)
  178.     sym = lookup_struct_typedef("NXString", 0, 1);
  179.   if (sym == NULL)
  180.     type = builtin_type (gdbarch)->builtin_data_ptr;
  181.   else
  182.     type = lookup_pointer_type(SYMBOL_TYPE (sym));

  183.   deprecated_set_value_type (nsstringValue, type);
  184.   return nsstringValue;
  185. }

  186. /* Objective-C name demangling.  */

  187. char *
  188. objc_demangle (const char *mangled, int options)
  189. {
  190.   char *demangled, *cp;

  191.   if (mangled[0] == '_' &&
  192.      (mangled[1] == 'i' || mangled[1] == 'c') &&
  193.       mangled[2] == '_')
  194.     {
  195.       cp = demangled = xmalloc(strlen(mangled) + 2);

  196.       if (mangled[1] == 'i')
  197.         *cp++ = '-';                /* for instance method */
  198.       else
  199.         *cp++ = '+';                /* for class    method */

  200.       *cp++ = '[';                /* opening left brace  */
  201.       strcpy(cp, mangled+3);        /* Tack on the rest of the mangled name.  */

  202.       while (*cp && *cp == '_')
  203.         cp++;                        /* Skip any initial underbars in class
  204.                                    name.  */

  205.       cp = strchr(cp, '_');
  206.       if (!cp)                        /* Find first non-initial underbar.  */
  207.         {
  208.           xfree(demangled);        /* not mangled name */
  209.           return NULL;
  210.         }
  211.       if (cp[1] == '_')                /* Easy case: no category name.    */
  212.         {
  213.           *cp++ = ' ';                /* Replace two '_' with one ' '.   */
  214.           strcpy(cp, mangled + (cp - demangled) + 2);
  215.         }
  216.       else
  217.         {
  218.           *cp++ = '(';                /* Less easy case: category name.  */
  219.           cp = strchr(cp, '_');
  220.           if (!cp)
  221.             {
  222.               xfree(demangled);        /* not mangled name */
  223.               return NULL;
  224.             }
  225.           *cp++ = ')';
  226.           *cp++ = ' ';                /* Overwriting 1st char of method name...  */
  227.           strcpy(cp, mangled + (cp - demangled));        /* Get it back.  */
  228.         }

  229.       while (*cp && *cp == '_')
  230.         cp++;                        /* Skip any initial underbars in
  231.                                    method name.  */

  232.       for (; *cp; cp++)
  233.         if (*cp == '_')
  234.           *cp = ':';                /* Replace remaining '_' with ':'.  */

  235.       *cp++ = ']';                /* closing right brace */
  236.       *cp++ = 0;                /* string terminator */
  237.       return demangled;
  238.     }
  239.   else
  240.     return NULL;        /* Not an objc mangled name.  */
  241. }

  242. /* Determine if we are currently in the Objective-C dispatch function.
  243.    If so, get the address of the method function that the dispatcher
  244.    would call and use that as the function to step into instead.  Also
  245.    skip over the trampoline for the function (if any).  This is better
  246.    for the user since they are only interested in stepping into the
  247.    method function anyway.  */
  248. static CORE_ADDR
  249. objc_skip_trampoline (struct frame_info *frame, CORE_ADDR stop_pc)
  250. {
  251.   struct gdbarch *gdbarch = get_frame_arch (frame);
  252.   CORE_ADDR real_stop_pc;
  253.   CORE_ADDR method_stop_pc;

  254.   real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);

  255.   if (real_stop_pc != 0)
  256.     find_objc_msgcall (real_stop_pc, &method_stop_pc);
  257.   else
  258.     find_objc_msgcall (stop_pc, &method_stop_pc);

  259.   if (method_stop_pc)
  260.     {
  261.       real_stop_pc = gdbarch_skip_trampoline_code
  262.                        (gdbarch, frame, method_stop_pc);
  263.       if (real_stop_pc == 0)
  264.         real_stop_pc = method_stop_pc;
  265.     }

  266.   return real_stop_pc;
  267. }


  268. /* Table mapping opcodes into strings for printing operators
  269.    and precedences of the operators.  */

  270. static const struct op_print objc_op_print_tab[] =
  271.   {
  272.     {",",  BINOP_COMMA, PREC_COMMA, 0},
  273.     {"=",  BINOP_ASSIGN, PREC_ASSIGN, 1},
  274.     {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
  275.     {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
  276.     {"|",  BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
  277.     {"^",  BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
  278.     {"&",  BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
  279.     {"==", BINOP_EQUAL, PREC_EQUAL, 0},
  280.     {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
  281.     {"<=", BINOP_LEQ, PREC_ORDER, 0},
  282.     {">=", BINOP_GEQ, PREC_ORDER, 0},
  283.     {">",  BINOP_GTR, PREC_ORDER, 0},
  284.     {"<",  BINOP_LESS, PREC_ORDER, 0},
  285.     {">>", BINOP_RSH, PREC_SHIFT, 0},
  286.     {"<<", BINOP_LSH, PREC_SHIFT, 0},
  287.     {"+",  BINOP_ADD, PREC_ADD, 0},
  288.     {"-",  BINOP_SUB, PREC_ADD, 0},
  289.     {"*",  BINOP_MUL, PREC_MUL, 0},
  290.     {"/",  BINOP_DIV, PREC_MUL, 0},
  291.     {"%",  BINOP_REM, PREC_MUL, 0},
  292.     {"@",  BINOP_REPEAT, PREC_REPEAT, 0},
  293.     {"-",  UNOP_NEG, PREC_PREFIX, 0},
  294.     {"!",  UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
  295.     {"~",  UNOP_COMPLEMENT, PREC_PREFIX, 0},
  296.     {"*",  UNOP_IND, PREC_PREFIX, 0},
  297.     {"&",  UNOP_ADDR, PREC_PREFIX, 0},
  298.     {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
  299.     {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
  300.     {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
  301.     {NULL, OP_NULL, PREC_NULL, 0}
  302. };

  303. const struct language_defn objc_language_defn = {
  304.   "objective-c",                /* Language name */
  305.   "Objective-C",
  306.   language_objc,
  307.   range_check_off,
  308.   case_sensitive_on,
  309.   array_row_major,
  310.   macro_expansion_c,
  311.   &exp_descriptor_standard,
  312.   c_parse,
  313.   c_error,
  314.   null_post_parser,
  315.   c_printchar,                       /* Print a character constant */
  316.   c_printstr,                       /* Function to print string constant */
  317.   c_emit_char,
  318.   c_print_type,                        /* Print a type using appropriate syntax */
  319.   c_print_typedef,                /* Print a typedef using appropriate syntax */
  320.   c_val_print,                        /* Print a value using appropriate syntax */
  321.   c_value_print,                /* Print a top-level value */
  322.   default_read_var_value,        /* la_read_var_value */
  323.   objc_skip_trampoline,         /* Language specific skip_trampoline */
  324.   "self",                        /* name_of_this */
  325.   basic_lookup_symbol_nonlocal,        /* lookup_symbol_nonlocal */
  326.   basic_lookup_transparent_type,/* lookup_transparent_type */
  327.   objc_demangle,                /* Language specific symbol demangler */
  328.   NULL,                                /* Language specific
  329.                                    class_name_from_physname */
  330.   objc_op_print_tab,                /* Expression operators for printing */
  331.   1,                                /* C-style arrays */
  332.   0,                                /* String lower bound */
  333.   default_word_break_characters,
  334.   default_make_symbol_completion_list,
  335.   c_language_arch_info,
  336.   default_print_array_index,
  337.   default_pass_by_reference,
  338.   default_get_string,
  339.   NULL,                                /* la_get_symbol_name_cmp */
  340.   iterate_over_symbols,
  341.   &default_varobj_ops,
  342.   NULL,
  343.   NULL,
  344.   LANG_MAGIC
  345. };

  346. /*
  347. * ObjC:
  348. * Following functions help construct Objective-C message calls.
  349. */

  350. struct selname                /* For parsing Objective-C.  */
  351.   {
  352.     struct selname *next;
  353.     char *msglist_sel;
  354.     int msglist_len;
  355.   };

  356. static int msglist_len;
  357. static struct selname *selname_chain;
  358. static char *msglist_sel;

  359. void
  360. start_msglist(void)
  361. {
  362.   struct selname *new =
  363.     (struct selname *) xmalloc (sizeof (struct selname));

  364.   new->next = selname_chain;
  365.   new->msglist_len = msglist_len;
  366.   new->msglist_sel = msglist_sel;
  367.   msglist_len = 0;
  368.   msglist_sel = (char *)xmalloc(1);
  369.   *msglist_sel = 0;
  370.   selname_chain = new;
  371. }

  372. void
  373. add_msglist(struct stoken *str, int addcolon)
  374. {
  375.   char *s;
  376.   const char *p;
  377.   int len, plen;

  378.   if (str == 0)                        /* Unnamed arg, or...  */
  379.     {
  380.       if (addcolon == 0)        /* variable number of args.  */
  381.         {
  382.           msglist_len++;
  383.           return;
  384.         }
  385.       p = "";
  386.       plen = 0;
  387.     }
  388.   else
  389.     {
  390.       p = str->ptr;
  391.       plen = str->length;
  392.     }
  393.   len = plen + strlen(msglist_sel) + 2;
  394.   s = (char *)xmalloc(len);
  395.   strcpy(s, msglist_sel);
  396.   strncat(s, p, plen);
  397.   xfree(msglist_sel);
  398.   msglist_sel = s;
  399.   if (addcolon)
  400.     {
  401.       s[len-2] = ':';
  402.       s[len-1] = 0;
  403.       msglist_len++;
  404.     }
  405.   else
  406.     s[len-2] = '\0';
  407. }

  408. int
  409. end_msglist (struct parser_state *ps)
  410. {
  411.   int val = msglist_len;
  412.   struct selname *sel = selname_chain;
  413.   char *p = msglist_sel;
  414.   CORE_ADDR selid;

  415.   selname_chain = sel->next;
  416.   msglist_len = sel->msglist_len;
  417.   msglist_sel = sel->msglist_sel;
  418.   selid = lookup_child_selector (parse_gdbarch (ps), p);
  419.   if (!selid)
  420.     error (_("Can't find selector \"%s\""), p);
  421.   write_exp_elt_longcst (ps, selid);
  422.   xfree(p);
  423.   write_exp_elt_longcst (ps, val);        /* Number of args */
  424.   xfree(sel);

  425.   return val;
  426. }

  427. /*
  428. * Function: specialcmp (const char *a, const char *b)
  429. *
  430. * Special strcmp: treats ']' and ' ' as end-of-string.
  431. * Used for qsorting lists of objc methods (either by class or selector).
  432. */

  433. static int
  434. specialcmp (const char *a, const char *b)
  435. {
  436.   while (*a && *a != ' ' && *a != ']' && *b && *b != ' ' && *b != ']')
  437.     {
  438.       if (*a != *b)
  439.         return *a - *b;
  440.       a++, b++;
  441.     }
  442.   if (*a && *a != ' ' && *a != ']')
  443.     return  1;                /* a is longer therefore greater.  */
  444.   if (*b && *b != ' ' && *b != ']')
  445.     return -1;                /* a is shorter therefore lesser.  */
  446.   return    0;                /* a and b are identical.  */
  447. }

  448. /*
  449. * Function: compare_selectors (const void *, const void *)
  450. *
  451. * Comparison function for use with qsort.  Arguments are symbols or
  452. * msymbols Compares selector part of objc method name alphabetically.
  453. */

  454. static int
  455. compare_selectors (const void *a, const void *b)
  456. {
  457.   const char *aname, *bname;

  458.   aname = SYMBOL_PRINT_NAME (*(struct symbol **) a);
  459.   bname = SYMBOL_PRINT_NAME (*(struct symbol **) b);
  460.   if (aname == NULL || bname == NULL)
  461.     error (_("internal: compare_selectors(1)"));

  462.   aname = strchr(aname, ' ');
  463.   bname = strchr(bname, ' ');
  464.   if (aname == NULL || bname == NULL)
  465.     error (_("internal: compare_selectors(2)"));

  466.   return specialcmp (aname+1, bname+1);
  467. }

  468. /*
  469. * Function: selectors_info (regexp, from_tty)
  470. *
  471. * Implements the "Info selectors" command.  Takes an optional regexp
  472. * arg.  Lists all objective c selectors that match the regexp.  Works
  473. * by grepping thru all symbols for objective c methods.  Output list
  474. * is sorted and uniqued.
  475. */

  476. static void
  477. selectors_info (char *regexp, int from_tty)
  478. {
  479.   struct objfile        *objfile;
  480.   struct minimal_symbol *msymbol;
  481.   const char            *name;
  482.   char                  *val;
  483.   int                    matches = 0;
  484.   int                    maxlen  = 0;
  485.   int                    ix;
  486.   char                   myregexp[2048];
  487.   char                   asel[256];
  488.   struct symbol        **sym_arr;
  489.   int                    plusminus = 0;

  490.   if (regexp == NULL)
  491.     strcpy(myregexp, ".*]");        /* Null input, match all objc methods.  */
  492.   else
  493.     {
  494.       if (*regexp == '+' || *regexp == '-')
  495.         { /* User wants only class methods or only instance methods.  */
  496.           plusminus = *regexp++;
  497.           while (*regexp == ' ' || *regexp == '\t')
  498.             regexp++;
  499.         }
  500.       if (*regexp == '\0')
  501.         strcpy(myregexp, ".*]");
  502.       else
  503.         {
  504.           /* Allow a few extra bytes because of the strcat below.  */
  505.           if (sizeof (myregexp) < strlen (regexp) + 4)
  506.             error (_("Regexp is too long: %s"), regexp);
  507.           strcpy(myregexp, regexp);
  508.           if (myregexp[strlen(myregexp) - 1] == '$') /* end of selector */
  509.             myregexp[strlen(myregexp) - 1] = ']';    /* end of method name */
  510.           else
  511.             strcat(myregexp, ".*]");
  512.         }
  513.     }

  514.   if (regexp != NULL)
  515.     {
  516.       val = re_comp (myregexp);
  517.       if (val != 0)
  518.         error (_("Invalid regexp (%s): %s"), val, regexp);
  519.     }

  520.   /* First time thru is JUST to get max length and count.  */
  521.   ALL_MSYMBOLS (objfile, msymbol)
  522.     {
  523.       QUIT;
  524.       name = MSYMBOL_NATURAL_NAME (msymbol);
  525.       if (name
  526.           && (name[0] == '-' || name[0] == '+')
  527.           && name[1] == '[')                /* Got a method name.  */
  528.         {
  529.           /* Filter for class/instance methods.  */
  530.           if (plusminus && name[0] != plusminus)
  531.             continue;
  532.           /* Find selector part.  */
  533.           name = (char *) strchr (name+2, ' ');
  534.           if (name == NULL)
  535.             {
  536.               complaint (&symfile_complaints,
  537.                          _("Bad method name '%s'"),
  538.                          MSYMBOL_NATURAL_NAME (msymbol));
  539.               continue;
  540.             }
  541.           if (regexp == NULL || re_exec(++name) != 0)
  542.             {
  543.               const char *mystart = name;
  544.               const char *myend   = strchr (mystart, ']');

  545.               if (myend && (myend - mystart > maxlen))
  546.                 maxlen = myend - mystart;        /* Get longest selector.  */
  547.               matches++;
  548.             }
  549.         }
  550.     }
  551.   if (matches)
  552.     {
  553.       printf_filtered (_("Selectors matching \"%s\":\n\n"),
  554.                        regexp ? regexp : "*");

  555.       sym_arr = alloca (matches * sizeof (struct symbol *));
  556.       matches = 0;
  557.       ALL_MSYMBOLS (objfile, msymbol)
  558.         {
  559.           QUIT;
  560.           name = MSYMBOL_NATURAL_NAME (msymbol);
  561.           if (name &&
  562.              (name[0] == '-' || name[0] == '+') &&
  563.               name[1] == '[')                /* Got a method name.  */
  564.             {
  565.               /* Filter for class/instance methods.  */
  566.               if (plusminus && name[0] != plusminus)
  567.                 continue;
  568.               /* Find selector part.  */
  569.               name = (char *) strchr(name+2, ' ');
  570.               if (regexp == NULL || re_exec(++name) != 0)
  571.                 sym_arr[matches++] = (struct symbol *) msymbol;
  572.             }
  573.         }

  574.       qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
  575.              compare_selectors);
  576.       /* Prevent compare on first iteration.  */
  577.       asel[0] = 0;
  578.       for (ix = 0; ix < matches; ix++)        /* Now do the output.  */
  579.         {
  580.           char *p = asel;

  581.           QUIT;
  582.           name = SYMBOL_NATURAL_NAME (sym_arr[ix]);
  583.           name = strchr (name, ' ') + 1;
  584.           if (p[0] && specialcmp(name, p) == 0)
  585.             continue;                /* Seen this one already (not unique).  */

  586.           /* Copy selector part.  */
  587.           while (*name && *name != ']')
  588.             *p++ = *name++;
  589.           *p++ = '\0';
  590.           /* Print in columns.  */
  591.           puts_filtered_tabular(asel, maxlen + 1, 0);
  592.         }
  593.       begin_line();
  594.     }
  595.   else
  596.     printf_filtered (_("No selectors matching \"%s\"\n"),
  597.                      regexp ? regexp : "*");
  598. }

  599. /*
  600. * Function: compare_classes (const void *, const void *)
  601. *
  602. * Comparison function for use with qsort.  Arguments are symbols or
  603. * msymbols Compares class part of objc method name alphabetically.
  604. */

  605. static int
  606. compare_classes (const void *a, const void *b)
  607. {
  608.   const char *aname, *bname;

  609.   aname = SYMBOL_PRINT_NAME (*(struct symbol **) a);
  610.   bname = SYMBOL_PRINT_NAME (*(struct symbol **) b);
  611.   if (aname == NULL || bname == NULL)
  612.     error (_("internal: compare_classes(1)"));

  613.   return specialcmp (aname+1, bname+1);
  614. }

  615. /*
  616. * Function: classes_info(regexp, from_tty)
  617. *
  618. * Implements the "info classes" command for objective c classes.
  619. * Lists all objective c classes that match the optional regexp.
  620. * Works by grepping thru the list of objective c methods.  List will
  621. * be sorted and uniqued (since one class may have many methods).
  622. * BUGS: will not list a class that has no methods.
  623. */

  624. static void
  625. classes_info (char *regexp, int from_tty)
  626. {
  627.   struct objfile        *objfile;
  628.   struct minimal_symbol *msymbol;
  629.   const char            *name;
  630.   char                  *val;
  631.   int                    matches = 0;
  632.   int                    maxlen  = 0;
  633.   int                    ix;
  634.   char                   myregexp[2048];
  635.   char                   aclass[256];
  636.   struct symbol        **sym_arr;

  637.   if (regexp == NULL)
  638.     strcpy(myregexp, ".* ");        /* Null input: match all objc classes.  */
  639.   else
  640.     {
  641.       /* Allow a few extra bytes because of the strcat below.  */
  642.       if (sizeof (myregexp) < strlen (regexp) + 4)
  643.         error (_("Regexp is too long: %s"), regexp);
  644.       strcpy(myregexp, regexp);
  645.       if (myregexp[strlen(myregexp) - 1] == '$')
  646.         /* In the method name, the end of the class name is marked by ' '.  */
  647.         myregexp[strlen(myregexp) - 1] = ' ';
  648.       else
  649.         strcat(myregexp, ".* ");
  650.     }

  651.   if (regexp != NULL)
  652.     {
  653.       val = re_comp (myregexp);
  654.       if (val != 0)
  655.         error (_("Invalid regexp (%s): %s"), val, regexp);
  656.     }

  657.   /* First time thru is JUST to get max length and count.  */
  658.   ALL_MSYMBOLS (objfile, msymbol)
  659.     {
  660.       QUIT;
  661.       name = MSYMBOL_NATURAL_NAME (msymbol);
  662.       if (name &&
  663.          (name[0] == '-' || name[0] == '+') &&
  664.           name[1] == '[')                        /* Got a method name.  */
  665.         if (regexp == NULL || re_exec(name+2) != 0)
  666.           {
  667.             /* Compute length of classname part.  */
  668.             const char *mystart = name + 2;
  669.             const char *myend   = strchr (mystart, ' ');

  670.             if (myend && (myend - mystart > maxlen))
  671.               maxlen = myend - mystart;
  672.             matches++;
  673.           }
  674.     }
  675.   if (matches)
  676.     {
  677.       printf_filtered (_("Classes matching \"%s\":\n\n"),
  678.                        regexp ? regexp : "*");
  679.       sym_arr = alloca (matches * sizeof (struct symbol *));
  680.       matches = 0;
  681.       ALL_MSYMBOLS (objfile, msymbol)
  682.         {
  683.           QUIT;
  684.           name = MSYMBOL_NATURAL_NAME (msymbol);
  685.           if (name &&
  686.              (name[0] == '-' || name[0] == '+') &&
  687.               name[1] == '[')                        /* Got a method name.  */
  688.             if (regexp == NULL || re_exec(name+2) != 0)
  689.                 sym_arr[matches++] = (struct symbol *) msymbol;
  690.         }

  691.       qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
  692.              compare_classes);
  693.       /* Prevent compare on first iteration.  */
  694.       aclass[0] = 0;
  695.       for (ix = 0; ix < matches; ix++)        /* Now do the output.  */
  696.         {
  697.           char *p = aclass;

  698.           QUIT;
  699.           name = SYMBOL_NATURAL_NAME (sym_arr[ix]);
  700.           name += 2;
  701.           if (p[0] && specialcmp(name, p) == 0)
  702.             continue;        /* Seen this one already (not unique).  */

  703.           /* Copy class part of method name.  */
  704.           while (*name && *name != ' ')
  705.             *p++ = *name++;
  706.           *p++ = '\0';
  707.           /* Print in columns.  */
  708.           puts_filtered_tabular(aclass, maxlen + 1, 0);
  709.         }
  710.       begin_line();
  711.     }
  712.   else
  713.     printf_filtered (_("No classes matching \"%s\"\n"), regexp ? regexp : "*");
  714. }

  715. static char *
  716. parse_selector (char *method, char **selector)
  717. {
  718.   char *s1 = NULL;
  719.   char *s2 = NULL;
  720.   int found_quote = 0;

  721.   char *nselector = NULL;

  722.   gdb_assert (selector != NULL);

  723.   s1 = method;

  724.   s1 = skip_spaces (s1);
  725.   if (*s1 == '\'')
  726.     {
  727.       found_quote = 1;
  728.       s1++;
  729.     }
  730.   s1 = skip_spaces (s1);

  731.   nselector = s1;
  732.   s2 = s1;

  733.   for (;;)
  734.     {
  735.       if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
  736.         *s1++ = *s2;
  737.       else if (isspace (*s2))
  738.         ;
  739.       else if ((*s2 == '\0') || (*s2 == '\''))
  740.         break;
  741.       else
  742.         return NULL;
  743.       s2++;
  744.     }
  745.   *s1++ = '\0';

  746.   s2 = skip_spaces (s2);
  747.   if (found_quote)
  748.     {
  749.       if (*s2 == '\'')
  750.         s2++;
  751.       s2 = skip_spaces (s2);
  752.     }

  753.   if (selector != NULL)
  754.     *selector = nselector;

  755.   return s2;
  756. }

  757. static char *
  758. parse_method (char *method, char *type, char **class,
  759.               char **category, char **selector)
  760. {
  761.   char *s1 = NULL;
  762.   char *s2 = NULL;
  763.   int found_quote = 0;

  764.   char ntype = '\0';
  765.   char *nclass = NULL;
  766.   char *ncategory = NULL;
  767.   char *nselector = NULL;

  768.   gdb_assert (type != NULL);
  769.   gdb_assert (class != NULL);
  770.   gdb_assert (category != NULL);
  771.   gdb_assert (selector != NULL);

  772.   s1 = method;

  773.   s1 = skip_spaces (s1);
  774.   if (*s1 == '\'')
  775.     {
  776.       found_quote = 1;
  777.       s1++;
  778.     }
  779.   s1 = skip_spaces (s1);

  780.   if ((s1[0] == '+') || (s1[0] == '-'))
  781.     ntype = *s1++;

  782.   s1 = skip_spaces (s1);

  783.   if (*s1 != '[')
  784.     return NULL;
  785.   s1++;

  786.   nclass = s1;
  787.   while (isalnum (*s1) || (*s1 == '_'))
  788.     s1++;

  789.   s2 = s1;
  790.   s2 = skip_spaces (s2);

  791.   if (*s2 == '(')
  792.     {
  793.       s2++;
  794.       s2 = skip_spaces (s2);
  795.       ncategory = s2;
  796.       while (isalnum (*s2) || (*s2 == '_'))
  797.         s2++;
  798.       *s2++ = '\0';
  799.     }

  800.   /* Truncate the class name now that we're not using the open paren.  */
  801.   *s1++ = '\0';

  802.   nselector = s2;
  803.   s1 = s2;

  804.   for (;;)
  805.     {
  806.       if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
  807.         *s1++ = *s2;
  808.       else if (isspace (*s2))
  809.         ;
  810.       else if (*s2 == ']')
  811.         break;
  812.       else
  813.         return NULL;
  814.       s2++;
  815.     }
  816.   *s1++ = '\0';
  817.   s2++;

  818.   s2 = skip_spaces (s2);
  819.   if (found_quote)
  820.     {
  821.       if (*s2 != '\'')
  822.         return NULL;
  823.       s2++;
  824.       s2 = skip_spaces (s2);
  825.     }

  826.   if (type != NULL)
  827.     *type = ntype;
  828.   if (class != NULL)
  829.     *class = nclass;
  830.   if (category != NULL)
  831.     *category = ncategory;
  832.   if (selector != NULL)
  833.     *selector = nselector;

  834.   return s2;
  835. }

  836. static void
  837. find_methods (char type, const char *class, const char *category,
  838.               const char *selector,
  839.               VEC (const_char_ptr) **symbol_names)
  840. {
  841.   struct objfile *objfile = NULL;

  842.   const char *symname = NULL;

  843.   char ntype = '\0';
  844.   char *nclass = NULL;
  845.   char *ncategory = NULL;
  846.   char *nselector = NULL;

  847.   static char *tmp = NULL;
  848.   static unsigned int tmplen = 0;

  849.   gdb_assert (symbol_names != NULL);

  850.   ALL_OBJFILES (objfile)
  851.     {
  852.       unsigned int *objc_csym;
  853.       struct minimal_symbol *msymbol = NULL;

  854.       /* The objfile_csym variable counts the number of ObjC methods
  855.          that this objfile defines.  We save that count as a private
  856.          objfile data.        If we have already determined that this objfile
  857.          provides no ObjC methods, we can skip it entirely.  */

  858.       unsigned int objfile_csym = 0;

  859.       objc_csym = objfile_data (objfile, objc_objfile_data);
  860.       if (objc_csym != NULL && *objc_csym == 0)
  861.         /* There are no ObjC symbols in this objfile.  Skip it entirely.  */
  862.         continue;

  863.       ALL_OBJFILE_MSYMBOLS (objfile, msymbol)
  864.         {
  865.           QUIT;

  866.           /* Check the symbol name first as this can be done entirely without
  867.              sending any query to the target.  */
  868.           symname = MSYMBOL_NATURAL_NAME (msymbol);
  869.           if (symname == NULL)
  870.             continue;

  871.           if ((symname[0] != '-' && symname[0] != '+') || (symname[1] != '['))
  872.             /* Not a method name.  */
  873.             continue;

  874.           objfile_csym++;

  875.           /* Now that thinks are a bit sane, clean up the symname.  */
  876.           while ((strlen (symname) + 1) >= tmplen)
  877.             {
  878.               tmplen = (tmplen == 0) ? 1024 : tmplen * 2;
  879.               tmp = xrealloc (tmp, tmplen);
  880.             }
  881.           strcpy (tmp, symname);

  882.           if (parse_method (tmp, &ntype, &nclass,
  883.                             &ncategory, &nselector) == NULL)
  884.             continue;

  885.           if ((type != '\0') && (ntype != type))
  886.             continue;

  887.           if ((class != NULL)
  888.               && ((nclass == NULL) || (strcmp (class, nclass) != 0)))
  889.             continue;

  890.           if ((category != NULL) &&
  891.               ((ncategory == NULL) || (strcmp (category, ncategory) != 0)))
  892.             continue;

  893.           if ((selector != NULL) &&
  894.               ((nselector == NULL) || (strcmp (selector, nselector) != 0)))
  895.             continue;

  896.           VEC_safe_push (const_char_ptr, *symbol_names, symname);
  897.         }

  898.       if (objc_csym == NULL)
  899.         {
  900.           objc_csym = obstack_alloc (&objfile->objfile_obstack,
  901.                                      sizeof (*objc_csym));
  902.           *objc_csym = objfile_csym;
  903.           set_objfile_data (objfile, objc_objfile_data, objc_csym);
  904.         }
  905.       else
  906.         /* Count of ObjC methods in this objfile should be constant.  */
  907.         gdb_assert (*objc_csym == objfile_csym);
  908.     }
  909. }

  910. /* Uniquify a VEC of strings.  */

  911. static void
  912. uniquify_strings (VEC (const_char_ptr) **strings)
  913. {
  914.   int ix;
  915.   const char *elem, *last = NULL;
  916.   int out;

  917.   /* If the vector is empty, there's nothing to do.  This explicit
  918.      check is needed to avoid invoking qsort with NULL. */
  919.   if (VEC_empty (const_char_ptr, *strings))
  920.     return;

  921.   qsort (VEC_address (const_char_ptr, *strings),
  922.          VEC_length (const_char_ptr, *strings),
  923.          sizeof (const_char_ptr),
  924.          compare_strings);
  925.   out = 0;
  926.   for (ix = 0; VEC_iterate (const_char_ptr, *strings, ix, elem); ++ix)
  927.     {
  928.       if (last == NULL || strcmp (last, elem) != 0)
  929.         {
  930.           /* Keep ELEM.  */
  931.           VEC_replace (const_char_ptr, *strings, out, elem);
  932.           ++out;
  933.         }
  934.       last = elem;
  935.     }
  936.   VEC_truncate (const_char_ptr, *strings, out);
  937. }

  938. /*
  939. * Function: find_imps (const char *selector, struct symbol **sym_arr)
  940. *
  941. * Input:  a string representing a selector
  942. *         a pointer to an array of symbol pointers
  943. *         possibly a pointer to a symbol found by the caller.
  944. *
  945. * Output: number of methods that implement that selector.  Side
  946. * effects: The array of symbol pointers is filled with matching syms.
  947. *
  948. * By analogy with function "find_methods" (symtab.c), builds a list
  949. * of symbols matching the ambiguous input, so that "decode_line_2"
  950. * (symtab.c) can list them and ask the user to choose one or more.
  951. * In this case the matches are objective c methods
  952. * ("implementations") matching an objective c selector.
  953. *
  954. * Note that it is possible for a normal (c-style) function to have
  955. * the same name as an objective c selector.  To prevent the selector
  956. * from eclipsing the function, we allow the caller (decode_line_1) to
  957. * search for such a function first, and if it finds one, pass it in
  958. * to us.  We will then integrate it into the list.  We also search
  959. * for one here, among the minsyms.
  960. *
  961. * NOTE: if NUM_DEBUGGABLE is non-zero, the sym_arr will be divided
  962. *       into two parts: debuggable (struct symbol) syms, and
  963. *       non_debuggable (struct minimal_symbol) syms.  The debuggable
  964. *       ones will come first, before NUM_DEBUGGABLE (which will thus
  965. *       be the index of the first non-debuggable one).
  966. */

  967. const char *
  968. find_imps (const char *method, VEC (const_char_ptr) **symbol_names)
  969. {
  970.   char type = '\0';
  971.   char *class = NULL;
  972.   char *category = NULL;
  973.   char *selector = NULL;

  974.   char *buf = NULL;
  975.   char *tmp = NULL;

  976.   int selector_case = 0;

  977.   gdb_assert (symbol_names != NULL);

  978.   buf = (char *) alloca (strlen (method) + 1);
  979.   strcpy (buf, method);
  980.   tmp = parse_method (buf, &type, &class, &category, &selector);

  981.   if (tmp == NULL)
  982.     {
  983.       strcpy (buf, method);
  984.       tmp = parse_selector (buf, &selector);

  985.       if (tmp == NULL)
  986.         return NULL;

  987.       selector_case = 1;
  988.     }

  989.   find_methods (type, class, category, selector, symbol_names);

  990.   /* If we hit the "selector" case, and we found some methods, then
  991.      add the selector itself as a symbol, if it exists.  */
  992.   if (selector_case && !VEC_empty (const_char_ptr, *symbol_names))
  993.     {
  994.       struct symbol *sym = lookup_symbol (selector, NULL, VAR_DOMAIN, 0);

  995.       if (sym != NULL)
  996.         VEC_safe_push (const_char_ptr, *symbol_names,
  997.                        SYMBOL_NATURAL_NAME (sym));
  998.       else
  999.         {
  1000.           struct bound_minimal_symbol msym
  1001.             = lookup_minimal_symbol (selector, 0, 0);

  1002.           if (msym.minsym != NULL)
  1003.             VEC_safe_push (const_char_ptr, *symbol_names,
  1004.                            MSYMBOL_NATURAL_NAME (msym.minsym));
  1005.         }
  1006.     }

  1007.   uniquify_strings (symbol_names);

  1008.   return method + (tmp - buf);
  1009. }

  1010. static void
  1011. print_object_command (char *args, int from_tty)
  1012. {
  1013.   struct value *object, *function, *description;
  1014.   CORE_ADDR string_addr, object_addr;
  1015.   int i = 0;
  1016.   gdb_byte c = 0;

  1017.   if (!args || !*args)
  1018.     error (
  1019. "The 'print-object' command requires an argument (an Objective-C object)");

  1020.   {
  1021.     struct expression *expr = parse_expression (args);
  1022.     struct cleanup *old_chain =
  1023.       make_cleanup (free_current_contents, &expr);
  1024.     int pc = 0;

  1025.     object = evaluate_subexp (builtin_type (expr->gdbarch)->builtin_data_ptr,
  1026.                               expr, &pc, EVAL_NORMAL);
  1027.     do_cleanups (old_chain);
  1028.   }

  1029.   /* Validate the address for sanity.  */
  1030.   object_addr = value_as_long (object);
  1031.   read_memory (object_addr, &c, 1);

  1032.   function = find_function_in_inferior ("_NSPrintForDebugger", NULL);
  1033.   if (function == NULL)
  1034.     error (_("Unable to locate _NSPrintForDebugger in child process"));

  1035.   description = call_function_by_hand (function, 1, &object);

  1036.   string_addr = value_as_long (description);
  1037.   if (string_addr == 0)
  1038.     error (_("object returns null description"));

  1039.   read_memory (string_addr + i++, &c, 1);
  1040.   if (c != 0)
  1041.     do
  1042.       { /* Read and print characters up to EOS.  */
  1043.         QUIT;
  1044.         printf_filtered ("%c", c);
  1045.         read_memory (string_addr + i++, &c, 1);
  1046.       } while (c != 0);
  1047.   else
  1048.     printf_filtered(_("<object returns empty description>"));
  1049.   printf_filtered ("\n");
  1050. }

  1051. /* The data structure 'methcalls' is used to detect method calls (thru
  1052. * ObjC runtime lib functions objc_msgSend, objc_msgSendSuper, etc.),
  1053. * and ultimately find the method being called.
  1054. */

  1055. struct objc_methcall {
  1056.   char *name;
  1057. /* Return instance method to be called.  */
  1058.   int (*stop_at) (CORE_ADDR, CORE_ADDR *);
  1059.   /* Start of pc range corresponding to method invocation.  */
  1060.   CORE_ADDR begin;
  1061.   /* End of pc range corresponding to method invocation.  */
  1062.   CORE_ADDR end;
  1063. };

  1064. static int resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc);
  1065. static int resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
  1066. static int resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc);
  1067. static int resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc);

  1068. static struct objc_methcall methcalls[] = {
  1069.   { "_objc_msgSend", resolve_msgsend, 0, 0},
  1070.   { "_objc_msgSend_stret", resolve_msgsend_stret, 0, 0},
  1071.   { "_objc_msgSendSuper", resolve_msgsend_super, 0, 0},
  1072.   { "_objc_msgSendSuper_stret", resolve_msgsend_super_stret, 0, 0},
  1073.   { "_objc_getClass", NULL, 0, 0},
  1074.   { "_objc_getMetaClass", NULL, 0, 0}
  1075. };

  1076. #define nmethcalls (sizeof (methcalls) / sizeof (methcalls[0]))

  1077. /* The following function, "find_objc_msgsend", fills in the data
  1078. * structure "objc_msgs" by finding the addresses of each of the
  1079. * (currently four) functions that it holds (of which objc_msgSend is
  1080. * the first).  This must be called each time symbols are loaded, in
  1081. * case the functions have moved for some reason.
  1082. */

  1083. static void
  1084. find_objc_msgsend (void)
  1085. {
  1086.   unsigned int i;

  1087.   for (i = 0; i < nmethcalls; i++)
  1088.     {
  1089.       struct bound_minimal_symbol func;

  1090.       /* Try both with and without underscore.  */
  1091.       func = lookup_bound_minimal_symbol (methcalls[i].name);
  1092.       if ((func.minsym == NULL) && (methcalls[i].name[0] == '_'))
  1093.         {
  1094.           func = lookup_bound_minimal_symbol (methcalls[i].name + 1);
  1095.         }
  1096.       if (func.minsym == NULL)
  1097.         {
  1098.           methcalls[i].begin = 0;
  1099.           methcalls[i].end = 0;
  1100.           continue;
  1101.         }

  1102.       methcalls[i].begin = BMSYMBOL_VALUE_ADDRESS (func);
  1103.       methcalls[i].end = minimal_symbol_upper_bound (func);
  1104.     }
  1105. }

  1106. /* find_objc_msgcall (replaces pc_off_limits)
  1107. *
  1108. * ALL that this function now does is to determine whether the input
  1109. * address ("pc") is the address of one of the Objective-C message
  1110. * dispatch functions (mainly objc_msgSend or objc_msgSendSuper), and
  1111. * if so, it returns the address of the method that will be called.
  1112. *
  1113. * The old function "pc_off_limits" used to do a lot of other things
  1114. * in addition, such as detecting shared library jump stubs and
  1115. * returning the address of the shlib function that would be called.
  1116. * That functionality has been moved into the gdbarch_skip_trampoline_code and
  1117. * IN_SOLIB_TRAMPOLINE macros, which are resolved in the target-
  1118. * dependent modules.
  1119. */

  1120. struct objc_submethod_helper_data {
  1121.   int (*f) (CORE_ADDR, CORE_ADDR *);
  1122.   CORE_ADDR pc;
  1123.   CORE_ADDR *new_pc;
  1124. };

  1125. static int
  1126. find_objc_msgcall_submethod_helper (void * arg)
  1127. {
  1128.   struct objc_submethod_helper_data *s =
  1129.     (struct objc_submethod_helper_data *) arg;

  1130.   if (s->f (s->pc, s->new_pc) == 0)
  1131.     return 1;
  1132.   else
  1133.     return 0;
  1134. }

  1135. static int
  1136. find_objc_msgcall_submethod (int (*f) (CORE_ADDR, CORE_ADDR *),
  1137.                              CORE_ADDR pc,
  1138.                              CORE_ADDR *new_pc)
  1139. {
  1140.   struct objc_submethod_helper_data s;

  1141.   s.f = f;
  1142.   s.pc = pc;
  1143.   s.new_pc = new_pc;

  1144.   if (catch_errors (find_objc_msgcall_submethod_helper,
  1145.                     (void *) &s,
  1146.                     "Unable to determine target of "
  1147.                     "Objective-C method call (ignoring):\n",
  1148.                     RETURN_MASK_ALL) == 0)
  1149.     return 1;
  1150.   else
  1151.     return 0;
  1152. }

  1153. int
  1154. find_objc_msgcall (CORE_ADDR pc, CORE_ADDR *new_pc)
  1155. {
  1156.   unsigned int i;

  1157.   find_objc_msgsend ();
  1158.   if (new_pc != NULL)
  1159.     {
  1160.       *new_pc = 0;
  1161.     }

  1162.   for (i = 0; i < nmethcalls; i++)
  1163.     if ((pc >= methcalls[i].begin) && (pc < methcalls[i].end))
  1164.       {
  1165.         if (methcalls[i].stop_at != NULL)
  1166.           return find_objc_msgcall_submethod (methcalls[i].stop_at,
  1167.                                               pc, new_pc);
  1168.         else
  1169.           return 0;
  1170.       }

  1171.   return 0;
  1172. }

  1173. /* -Wmissing-prototypes */
  1174. extern initialize_file_ftype _initialize_objc_language;

  1175. void
  1176. _initialize_objc_language (void)
  1177. {
  1178.   add_language (&objc_language_defn);
  1179.   add_info ("selectors", selectors_info,    /* INFO SELECTORS command.  */
  1180.             _("All Objective-C selectors, or those matching REGEXP."));
  1181.   add_info ("classes", classes_info,             /* INFO CLASSES   command.  */
  1182.             _("All Objective-C classes, or those matching REGEXP."));
  1183.   add_com ("print-object", class_vars, print_object_command,
  1184.            _("Ask an Objective-C object to print itself."));
  1185.   add_com_alias ("po", "print-object", class_vars, 1);
  1186. }

  1187. static void
  1188. read_objc_method (struct gdbarch *gdbarch, CORE_ADDR addr,
  1189.                   struct objc_method *method)
  1190. {
  1191.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  1192.   method->name  = read_memory_unsigned_integer (addr + 0, 4, byte_order);
  1193.   method->types = read_memory_unsigned_integer (addr + 4, 4, byte_order);
  1194.   method->imp   = read_memory_unsigned_integer (addr + 8, 4, byte_order);
  1195. }

  1196. static unsigned long
  1197. read_objc_methlist_nmethods (struct gdbarch *gdbarch, CORE_ADDR addr)
  1198. {
  1199.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  1200.   return read_memory_unsigned_integer (addr + 4, 4, byte_order);
  1201. }

  1202. static void
  1203. read_objc_methlist_method (struct gdbarch *gdbarch, CORE_ADDR addr,
  1204.                            unsigned long num, struct objc_method *method)
  1205. {
  1206.   gdb_assert (num < read_objc_methlist_nmethods (gdbarch, addr));
  1207.   read_objc_method (gdbarch, addr + 8 + (12 * num), method);
  1208. }

  1209. static void
  1210. read_objc_object (struct gdbarch *gdbarch, CORE_ADDR addr,
  1211.                   struct objc_object *object)
  1212. {
  1213.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  1214.   object->isa = read_memory_unsigned_integer (addr, 4, byte_order);
  1215. }

  1216. static void
  1217. read_objc_super (struct gdbarch *gdbarch, CORE_ADDR addr,
  1218.                  struct objc_super *super)
  1219. {
  1220.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  1221.   super->receiver = read_memory_unsigned_integer (addr, 4, byte_order);
  1222.   super->class = read_memory_unsigned_integer (addr + 4, 4, byte_order);
  1223. };

  1224. static void
  1225. read_objc_class (struct gdbarch *gdbarch, CORE_ADDR addr,
  1226.                  struct objc_class *class)
  1227. {
  1228.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  1229.   class->isa = read_memory_unsigned_integer (addr, 4, byte_order);
  1230.   class->super_class = read_memory_unsigned_integer (addr + 4, 4, byte_order);
  1231.   class->name = read_memory_unsigned_integer (addr + 8, 4, byte_order);
  1232.   class->version = read_memory_unsigned_integer (addr + 12, 4, byte_order);
  1233.   class->info = read_memory_unsigned_integer (addr + 16, 4, byte_order);
  1234.   class->instance_size = read_memory_unsigned_integer (addr + 18, 4,
  1235.                                                        byte_order);
  1236.   class->ivars = read_memory_unsigned_integer (addr + 24, 4, byte_order);
  1237.   class->methods = read_memory_unsigned_integer (addr + 28, 4, byte_order);
  1238.   class->cache = read_memory_unsigned_integer (addr + 32, 4, byte_order);
  1239.   class->protocols = read_memory_unsigned_integer (addr + 36, 4, byte_order);
  1240. }

  1241. static CORE_ADDR
  1242. find_implementation_from_class (struct gdbarch *gdbarch,
  1243.                                 CORE_ADDR class, CORE_ADDR sel)
  1244. {
  1245.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1246.   CORE_ADDR subclass = class;

  1247.   while (subclass != 0)
  1248.     {

  1249.       struct objc_class class_str;
  1250.       unsigned mlistnum = 0;

  1251.       read_objc_class (gdbarch, subclass, &class_str);

  1252.       for (;;)
  1253.         {
  1254.           CORE_ADDR mlist;
  1255.           unsigned long nmethods;
  1256.           unsigned long i;

  1257.           mlist = read_memory_unsigned_integer (class_str.methods +
  1258.                                                 (4 * mlistnum),
  1259.                                                 4, byte_order);
  1260.           if (mlist == 0)
  1261.             break;

  1262.           nmethods = read_objc_methlist_nmethods (gdbarch, mlist);

  1263.           for (i = 0; i < nmethods; i++)
  1264.             {
  1265.               struct objc_method meth_str;

  1266.               read_objc_methlist_method (gdbarch, mlist, i, &meth_str);

  1267.               if (meth_str.name == sel)
  1268.                 /* FIXME: hppa arch was doing a pointer dereference
  1269.                    here.  There needs to be a better way to do that.  */
  1270.                 return meth_str.imp;
  1271.             }
  1272.           mlistnum++;
  1273.         }
  1274.       subclass = class_str.super_class;
  1275.     }

  1276.   return 0;
  1277. }

  1278. static CORE_ADDR
  1279. find_implementation (struct gdbarch *gdbarch,
  1280.                      CORE_ADDR object, CORE_ADDR sel)
  1281. {
  1282.   struct objc_object ostr;

  1283.   if (object == 0)
  1284.     return 0;
  1285.   read_objc_object (gdbarch, object, &ostr);
  1286.   if (ostr.isa == 0)
  1287.     return 0;

  1288.   return find_implementation_from_class (gdbarch, ostr.isa, sel);
  1289. }

  1290. static int
  1291. resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc)
  1292. {
  1293.   struct frame_info *frame = get_current_frame ();
  1294.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1295.   struct type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;

  1296.   CORE_ADDR object;
  1297.   CORE_ADDR sel;
  1298.   CORE_ADDR res;

  1299.   object = gdbarch_fetch_pointer_argument (gdbarch, frame, 0, ptr_type);
  1300.   sel = gdbarch_fetch_pointer_argument (gdbarch, frame, 1, ptr_type);

  1301.   res = find_implementation (gdbarch, object, sel);
  1302.   if (new_pc != 0)
  1303.     *new_pc = res;
  1304.   if (res == 0)
  1305.     return 1;
  1306.   return 0;
  1307. }

  1308. static int
  1309. resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
  1310. {
  1311.   struct frame_info *frame = get_current_frame ();
  1312.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1313.   struct type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;

  1314.   CORE_ADDR object;
  1315.   CORE_ADDR sel;
  1316.   CORE_ADDR res;

  1317.   object = gdbarch_fetch_pointer_argument (gdbarch, frame, 1, ptr_type);
  1318.   sel = gdbarch_fetch_pointer_argument (gdbarch, frame, 2, ptr_type);

  1319.   res = find_implementation (gdbarch, object, sel);
  1320.   if (new_pc != 0)
  1321.     *new_pc = res;
  1322.   if (res == 0)
  1323.     return 1;
  1324.   return 0;
  1325. }

  1326. static int
  1327. resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc)
  1328. {
  1329.   struct frame_info *frame = get_current_frame ();
  1330.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1331.   struct type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;

  1332.   struct objc_super sstr;

  1333.   CORE_ADDR super;
  1334.   CORE_ADDR sel;
  1335.   CORE_ADDR res;

  1336.   super = gdbarch_fetch_pointer_argument (gdbarch, frame, 0, ptr_type);
  1337.   sel = gdbarch_fetch_pointer_argument (gdbarch, frame, 1, ptr_type);

  1338.   read_objc_super (gdbarch, super, &sstr);
  1339.   if (sstr.class == 0)
  1340.     return 0;

  1341.   res = find_implementation_from_class (gdbarch, sstr.class, sel);
  1342.   if (new_pc != 0)
  1343.     *new_pc = res;
  1344.   if (res == 0)
  1345.     return 1;
  1346.   return 0;
  1347. }

  1348. static int
  1349. resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
  1350. {
  1351.   struct frame_info *frame = get_current_frame ();
  1352.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1353.   struct type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;

  1354.   struct objc_super sstr;

  1355.   CORE_ADDR super;
  1356.   CORE_ADDR sel;
  1357.   CORE_ADDR res;

  1358.   super = gdbarch_fetch_pointer_argument (gdbarch, frame, 1, ptr_type);
  1359.   sel = gdbarch_fetch_pointer_argument (gdbarch, frame, 2, ptr_type);

  1360.   read_objc_super (gdbarch, super, &sstr);
  1361.   if (sstr.class == 0)
  1362.     return 0;

  1363.   res = find_implementation_from_class (gdbarch, sstr.class, sel);
  1364.   if (new_pc != 0)
  1365.     *new_pc = res;
  1366.   if (res == 0)
  1367.     return 1;
  1368.   return 0;
  1369. }

  1370. /* Provide a prototype to silence -Wmissing-prototypes.  */
  1371. extern initialize_file_ftype _initialize_objc_lang;

  1372. void
  1373. _initialize_objc_lang (void)
  1374. {
  1375.   objc_objfile_data = register_objfile_data ();
  1376. }