objc-lang.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  1. /* Objective-C language support routines for GDB, the GNU debugger.
  2. Copyright (C) 2002-2022 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"
  30. #include "gdbcore.h"
  31. #include "gdbcmd.h"
  32. #include "frame.h"
  33. #include "gdbsupport/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 "c-exp.h"
  40. #include <ctype.h>
  41. #include <algorithm>
  42. struct objc_object {
  43. CORE_ADDR isa;
  44. };
  45. struct objc_class {
  46. CORE_ADDR isa;
  47. CORE_ADDR super_class;
  48. CORE_ADDR name;
  49. long version;
  50. long info;
  51. long instance_size;
  52. CORE_ADDR ivars;
  53. CORE_ADDR methods;
  54. CORE_ADDR cache;
  55. CORE_ADDR protocols;
  56. };
  57. struct objc_super {
  58. CORE_ADDR receiver;
  59. CORE_ADDR theclass;
  60. };
  61. struct objc_method {
  62. CORE_ADDR name;
  63. CORE_ADDR types;
  64. CORE_ADDR imp;
  65. };
  66. static const struct objfile_key<unsigned int> objc_objfile_data;
  67. /* Lookup a structure type named "struct NAME", visible in lexical
  68. block BLOCK. If NOERR is nonzero, return zero if NAME is not
  69. suitably defined. */
  70. struct symbol *
  71. lookup_struct_typedef (const char *name, const struct block *block, int noerr)
  72. {
  73. struct symbol *sym;
  74. sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol;
  75. if (sym == NULL)
  76. {
  77. if (noerr)
  78. return 0;
  79. else
  80. error (_("No struct type named %s."), name);
  81. }
  82. if (sym->type ()->code () != TYPE_CODE_STRUCT)
  83. {
  84. if (noerr)
  85. return 0;
  86. else
  87. error (_("This context has class, union or enum %s, not a struct."),
  88. name);
  89. }
  90. return sym;
  91. }
  92. CORE_ADDR
  93. lookup_objc_class (struct gdbarch *gdbarch, const char *classname)
  94. {
  95. struct type *char_type = builtin_type (gdbarch)->builtin_char;
  96. struct value * function, *classval;
  97. if (! target_has_execution ())
  98. {
  99. /* Can't call into inferior to lookup class. */
  100. return 0;
  101. }
  102. if (lookup_minimal_symbol("objc_lookUpClass", 0, 0).minsym)
  103. function = find_function_in_inferior("objc_lookUpClass", NULL);
  104. else if (lookup_minimal_symbol ("objc_lookup_class", 0, 0).minsym)
  105. function = find_function_in_inferior("objc_lookup_class", NULL);
  106. else
  107. {
  108. complaint (_("no way to lookup Objective-C classes"));
  109. return 0;
  110. }
  111. classval = value_string (classname, strlen (classname) + 1, char_type);
  112. classval = value_coerce_array (classval);
  113. return (CORE_ADDR) value_as_long (call_function_by_hand (function,
  114. NULL,
  115. classval));
  116. }
  117. CORE_ADDR
  118. lookup_child_selector (struct gdbarch *gdbarch, const char *selname)
  119. {
  120. struct type *char_type = builtin_type (gdbarch)->builtin_char;
  121. struct value * function, *selstring;
  122. if (! target_has_execution ())
  123. {
  124. /* Can't call into inferior to lookup selector. */
  125. return 0;
  126. }
  127. if (lookup_minimal_symbol("sel_getUid", 0, 0).minsym)
  128. function = find_function_in_inferior("sel_getUid", NULL);
  129. else if (lookup_minimal_symbol ("sel_get_any_uid", 0, 0).minsym)
  130. function = find_function_in_inferior("sel_get_any_uid", NULL);
  131. else
  132. {
  133. complaint (_("no way to lookup Objective-C selectors"));
  134. return 0;
  135. }
  136. selstring = value_coerce_array (value_string (selname,
  137. strlen (selname) + 1,
  138. char_type));
  139. return value_as_long (call_function_by_hand (function, NULL, selstring));
  140. }
  141. struct value *
  142. value_nsstring (struct gdbarch *gdbarch, const char *ptr, int len)
  143. {
  144. struct type *char_type = builtin_type (gdbarch)->builtin_char;
  145. struct value *stringValue[3];
  146. struct value *function, *nsstringValue;
  147. struct symbol *sym;
  148. struct type *type;
  149. if (!target_has_execution ())
  150. return 0; /* Can't call into inferior to create NSString. */
  151. stringValue[2] = value_string(ptr, len, char_type);
  152. stringValue[2] = value_coerce_array(stringValue[2]);
  153. /* _NSNewStringFromCString replaces "istr" after Lantern2A. */
  154. if (lookup_minimal_symbol("_NSNewStringFromCString", 0, 0).minsym)
  155. {
  156. function = find_function_in_inferior("_NSNewStringFromCString", NULL);
  157. nsstringValue = call_function_by_hand(function, NULL, stringValue[2]);
  158. }
  159. else if (lookup_minimal_symbol("istr", 0, 0).minsym)
  160. {
  161. function = find_function_in_inferior("istr", NULL);
  162. nsstringValue = call_function_by_hand(function, NULL, stringValue[2]);
  163. }
  164. else if (lookup_minimal_symbol("+[NSString stringWithCString:]", 0, 0).minsym)
  165. {
  166. function
  167. = find_function_in_inferior("+[NSString stringWithCString:]", NULL);
  168. type = builtin_type (gdbarch)->builtin_long;
  169. stringValue[0] = value_from_longest
  170. (type, lookup_objc_class (gdbarch, "NSString"));
  171. stringValue[1] = value_from_longest
  172. (type, lookup_child_selector (gdbarch, "stringWithCString:"));
  173. nsstringValue = call_function_by_hand(function, NULL, stringValue);
  174. }
  175. else
  176. error (_("NSString: internal error -- no way to create new NSString"));
  177. sym = lookup_struct_typedef("NSString", 0, 1);
  178. if (sym == NULL)
  179. sym = lookup_struct_typedef("NXString", 0, 1);
  180. if (sym == NULL)
  181. type = builtin_type (gdbarch)->builtin_data_ptr;
  182. else
  183. type = lookup_pointer_type(sym->type ());
  184. deprecated_set_value_type (nsstringValue, type);
  185. return nsstringValue;
  186. }
  187. /* Class representing the Objective-C language. */
  188. class objc_language : public language_defn
  189. {
  190. public:
  191. objc_language ()
  192. : language_defn (language_objc)
  193. { /* Nothing. */ }
  194. /* See language.h. */
  195. const char *name () const override
  196. { return "objective-c"; }
  197. /* See language.h. */
  198. const char *natural_name () const override
  199. { return "Objective-C"; }
  200. /* See language.h. */
  201. const std::vector<const char *> &filename_extensions () const override
  202. {
  203. static const std::vector<const char *> extensions = { ".m" };
  204. return extensions;
  205. }
  206. /* See language.h. */
  207. void language_arch_info (struct gdbarch *gdbarch,
  208. struct language_arch_info *lai) const override
  209. {
  210. c_language_arch_info (gdbarch, lai);
  211. }
  212. /* See language.h. */
  213. bool sniff_from_mangled_name
  214. (const char *mangled, gdb::unique_xmalloc_ptr<char> *demangled)
  215. const override
  216. {
  217. *demangled = demangle_symbol (mangled, 0);
  218. return *demangled != NULL;
  219. }
  220. /* See language.h. */
  221. gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
  222. int options) const override;
  223. /* See language.h. */
  224. void print_type (struct type *type, const char *varstring,
  225. struct ui_file *stream, int show, int level,
  226. const struct type_print_options *flags) const override
  227. {
  228. c_print_type (type, varstring, stream, show, level, flags);
  229. }
  230. /* See language.h. */
  231. CORE_ADDR skip_trampoline (struct frame_info *frame,
  232. CORE_ADDR stop_pc) const override
  233. {
  234. struct gdbarch *gdbarch = get_frame_arch (frame);
  235. CORE_ADDR real_stop_pc;
  236. CORE_ADDR method_stop_pc;
  237. /* Determine if we are currently in the Objective-C dispatch function.
  238. If so, get the address of the method function that the dispatcher
  239. would call and use that as the function to step into instead. Also
  240. skip over the trampoline for the function (if any). This is better
  241. for the user since they are only interested in stepping into the
  242. method function anyway. */
  243. real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
  244. if (real_stop_pc != 0)
  245. find_objc_msgcall (real_stop_pc, &method_stop_pc);
  246. else
  247. find_objc_msgcall (stop_pc, &method_stop_pc);
  248. if (method_stop_pc)
  249. {
  250. real_stop_pc = gdbarch_skip_trampoline_code
  251. (gdbarch, frame, method_stop_pc);
  252. if (real_stop_pc == 0)
  253. real_stop_pc = method_stop_pc;
  254. }
  255. return real_stop_pc;
  256. }
  257. /* See language.h. */
  258. const char *name_of_this () const override
  259. { return "self"; }
  260. /* See language.h. */
  261. enum macro_expansion macro_expansion () const override
  262. { return macro_expansion_c; }
  263. };
  264. /* See declaration of objc_language::demangle_symbol above. */
  265. gdb::unique_xmalloc_ptr<char>
  266. objc_language::demangle_symbol (const char *mangled, int options) const
  267. {
  268. char *demangled, *cp;
  269. if (mangled[0] == '_'
  270. && (mangled[1] == 'i' || mangled[1] == 'c')
  271. && mangled[2] == '_')
  272. {
  273. cp = demangled = (char *) xmalloc (strlen (mangled) + 2);
  274. if (mangled[1] == 'i')
  275. *cp++ = '-'; /* for instance method */
  276. else
  277. *cp++ = '+'; /* for class method */
  278. *cp++ = '['; /* opening left brace */
  279. strcpy(cp, mangled+3); /* Tack on the rest of the mangled name. */
  280. while (*cp != '\0' && *cp == '_')
  281. cp++; /* Skip any initial underbars in class
  282. name. */
  283. cp = strchr(cp, '_');
  284. if (cp == nullptr) /* Find first non-initial underbar. */
  285. {
  286. xfree(demangled); /* not mangled name */
  287. return nullptr;
  288. }
  289. if (cp[1] == '_') /* Easy case: no category name. */
  290. {
  291. *cp++ = ' '; /* Replace two '_' with one ' '. */
  292. strcpy(cp, mangled + (cp - demangled) + 2);
  293. }
  294. else
  295. {
  296. *cp++ = '('; /* Less easy case: category name. */
  297. cp = strchr(cp, '_');
  298. if (cp == nullptr)
  299. {
  300. xfree(demangled); /* not mangled name */
  301. return nullptr;
  302. }
  303. *cp++ = ')';
  304. *cp++ = ' '; /* Overwriting 1st char of method name... */
  305. strcpy(cp, mangled + (cp - demangled)); /* Get it back. */
  306. }
  307. while (*cp != '\0' && *cp == '_')
  308. cp++; /* Skip any initial underbars in
  309. method name. */
  310. for (; *cp != '\0'; cp++)
  311. if (*cp == '_')
  312. *cp = ':'; /* Replace remaining '_' with ':'. */
  313. *cp++ = ']'; /* closing right brace */
  314. *cp++ = 0; /* string terminator */
  315. return gdb::unique_xmalloc_ptr<char> (demangled);
  316. }
  317. else
  318. return nullptr; /* Not an objc mangled name. */
  319. }
  320. /* Single instance of the class representing the Objective-C language. */
  321. static objc_language objc_language_defn;
  322. /*
  323. * ObjC:
  324. * Following functions help construct Objective-C message calls.
  325. */
  326. struct selname /* For parsing Objective-C. */
  327. {
  328. struct selname *next;
  329. char *msglist_sel;
  330. int msglist_len;
  331. };
  332. static int msglist_len;
  333. static struct selname *selname_chain;
  334. static char *msglist_sel;
  335. void
  336. start_msglist(void)
  337. {
  338. struct selname *newobj = XNEW (struct selname);
  339. newobj->next = selname_chain;
  340. newobj->msglist_len = msglist_len;
  341. newobj->msglist_sel = msglist_sel;
  342. msglist_len = 0;
  343. msglist_sel = (char *)xmalloc(1);
  344. *msglist_sel = 0;
  345. selname_chain = newobj;
  346. }
  347. void
  348. add_msglist(struct stoken *str, int addcolon)
  349. {
  350. char *s;
  351. const char *p;
  352. int len, plen;
  353. if (str == 0) /* Unnamed arg, or... */
  354. {
  355. if (addcolon == 0) /* variable number of args. */
  356. {
  357. msglist_len++;
  358. return;
  359. }
  360. p = "";
  361. plen = 0;
  362. }
  363. else
  364. {
  365. p = str->ptr;
  366. plen = str->length;
  367. }
  368. len = plen + strlen(msglist_sel) + 2;
  369. s = (char *)xmalloc(len);
  370. strcpy(s, msglist_sel);
  371. strncat(s, p, plen);
  372. xfree(msglist_sel);
  373. msglist_sel = s;
  374. if (addcolon)
  375. {
  376. s[len-2] = ':';
  377. s[len-1] = 0;
  378. msglist_len++;
  379. }
  380. else
  381. s[len-2] = '\0';
  382. }
  383. int
  384. end_msglist (struct parser_state *ps)
  385. {
  386. int val = msglist_len;
  387. struct selname *sel = selname_chain;
  388. char *p = msglist_sel;
  389. CORE_ADDR selid;
  390. std::vector<expr::operation_up> args = ps->pop_vector (val);
  391. expr::operation_up target = ps->pop ();
  392. selname_chain = sel->next;
  393. msglist_len = sel->msglist_len;
  394. msglist_sel = sel->msglist_sel;
  395. selid = lookup_child_selector (ps->gdbarch (), p);
  396. if (!selid)
  397. error (_("Can't find selector \"%s\""), p);
  398. ps->push_new<expr::objc_msgcall_operation> (selid, std::move (target),
  399. std::move (args));
  400. xfree(p);
  401. xfree(sel);
  402. return val;
  403. }
  404. /*
  405. * Function: specialcmp (const char *a, const char *b)
  406. *
  407. * Special strcmp: treats ']' and ' ' as end-of-string.
  408. * Used for qsorting lists of objc methods (either by class or selector).
  409. */
  410. static int
  411. specialcmp (const char *a, const char *b)
  412. {
  413. while (*a && *a != ' ' && *a != ']' && *b && *b != ' ' && *b != ']')
  414. {
  415. if (*a != *b)
  416. return *a - *b;
  417. a++, b++;
  418. }
  419. if (*a && *a != ' ' && *a != ']')
  420. return 1; /* a is longer therefore greater. */
  421. if (*b && *b != ' ' && *b != ']')
  422. return -1; /* a is shorter therefore lesser. */
  423. return 0; /* a and b are identical. */
  424. }
  425. /*
  426. * Function: compare_selectors (const void *, const void *)
  427. *
  428. * Comparison function for use with qsort. Arguments are symbols or
  429. * msymbols Compares selector part of objc method name alphabetically.
  430. */
  431. static int
  432. compare_selectors (const void *a, const void *b)
  433. {
  434. const char *aname, *bname;
  435. aname = (*(struct symbol **) a)->print_name ();
  436. bname = (*(struct symbol **) b)->print_name ();
  437. if (aname == NULL || bname == NULL)
  438. error (_("internal: compare_selectors(1)"));
  439. aname = strchr(aname, ' ');
  440. bname = strchr(bname, ' ');
  441. if (aname == NULL || bname == NULL)
  442. error (_("internal: compare_selectors(2)"));
  443. return specialcmp (aname+1, bname+1);
  444. }
  445. /*
  446. * Function: selectors_info (regexp, from_tty)
  447. *
  448. * Implements the "Info selectors" command. Takes an optional regexp
  449. * arg. Lists all objective c selectors that match the regexp. Works
  450. * by grepping thru all symbols for objective c methods. Output list
  451. * is sorted and uniqued.
  452. */
  453. static void
  454. info_selectors_command (const char *regexp, int from_tty)
  455. {
  456. const char *name;
  457. char *val;
  458. int matches = 0;
  459. int maxlen = 0;
  460. int ix;
  461. char myregexp[2048];
  462. char asel[256];
  463. struct symbol **sym_arr;
  464. int plusminus = 0;
  465. if (regexp == NULL)
  466. strcpy(myregexp, ".*]"); /* Null input, match all objc methods. */
  467. else
  468. {
  469. if (*regexp == '+' || *regexp == '-')
  470. { /* User wants only class methods or only instance methods. */
  471. plusminus = *regexp++;
  472. while (*regexp == ' ' || *regexp == '\t')
  473. regexp++;
  474. }
  475. if (*regexp == '\0')
  476. strcpy(myregexp, ".*]");
  477. else
  478. {
  479. /* Allow a few extra bytes because of the strcat below. */
  480. if (sizeof (myregexp) < strlen (regexp) + 4)
  481. error (_("Regexp is too long: %s"), regexp);
  482. strcpy(myregexp, regexp);
  483. if (myregexp[strlen(myregexp) - 1] == '$') /* end of selector */
  484. myregexp[strlen(myregexp) - 1] = ']'; /* end of method name */
  485. else
  486. strcat(myregexp, ".*]");
  487. }
  488. }
  489. if (regexp != NULL)
  490. {
  491. val = re_comp (myregexp);
  492. if (val != 0)
  493. error (_("Invalid regexp (%s): %s"), val, regexp);
  494. }
  495. /* First time thru is JUST to get max length and count. */
  496. for (objfile *objfile : current_program_space->objfiles ())
  497. {
  498. for (minimal_symbol *msymbol : objfile->msymbols ())
  499. {
  500. QUIT;
  501. name = msymbol->natural_name ();
  502. if (name
  503. && (name[0] == '-' || name[0] == '+')
  504. && name[1] == '[') /* Got a method name. */
  505. {
  506. /* Filter for class/instance methods. */
  507. if (plusminus && name[0] != plusminus)
  508. continue;
  509. /* Find selector part. */
  510. name = (char *) strchr (name+2, ' ');
  511. if (name == NULL)
  512. {
  513. complaint (_("Bad method name '%s'"),
  514. msymbol->natural_name ());
  515. continue;
  516. }
  517. if (regexp == NULL || re_exec(++name) != 0)
  518. {
  519. const char *mystart = name;
  520. const char *myend = strchr (mystart, ']');
  521. if (myend && (myend - mystart > maxlen))
  522. maxlen = myend - mystart; /* Get longest selector. */
  523. matches++;
  524. }
  525. }
  526. }
  527. }
  528. if (matches)
  529. {
  530. gdb_printf (_("Selectors matching \"%s\":\n\n"),
  531. regexp ? regexp : "*");
  532. sym_arr = XALLOCAVEC (struct symbol *, matches);
  533. matches = 0;
  534. for (objfile *objfile : current_program_space->objfiles ())
  535. {
  536. for (minimal_symbol *msymbol : objfile->msymbols ())
  537. {
  538. QUIT;
  539. name = msymbol->natural_name ();
  540. if (name &&
  541. (name[0] == '-' || name[0] == '+') &&
  542. name[1] == '[') /* Got a method name. */
  543. {
  544. /* Filter for class/instance methods. */
  545. if (plusminus && name[0] != plusminus)
  546. continue;
  547. /* Find selector part. */
  548. name = (char *) strchr(name+2, ' ');
  549. if (regexp == NULL || re_exec(++name) != 0)
  550. sym_arr[matches++] = (struct symbol *) msymbol;
  551. }
  552. }
  553. }
  554. qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
  555. compare_selectors);
  556. /* Prevent compare on first iteration. */
  557. asel[0] = 0;
  558. for (ix = 0; ix < matches; ix++) /* Now do the output. */
  559. {
  560. char *p = asel;
  561. QUIT;
  562. name = sym_arr[ix]->natural_name ();
  563. name = strchr (name, ' ') + 1;
  564. if (p[0] && specialcmp(name, p) == 0)
  565. continue; /* Seen this one already (not unique). */
  566. /* Copy selector part. */
  567. while (*name && *name != ']')
  568. *p++ = *name++;
  569. *p++ = '\0';
  570. /* Print in columns. */
  571. puts_tabular(asel, maxlen + 1, 0);
  572. }
  573. begin_line();
  574. }
  575. else
  576. gdb_printf (_("No selectors matching \"%s\"\n"),
  577. regexp ? regexp : "*");
  578. }
  579. /*
  580. * Function: compare_classes (const void *, const void *)
  581. *
  582. * Comparison function for use with qsort. Arguments are symbols or
  583. * msymbols Compares class part of objc method name alphabetically.
  584. */
  585. static int
  586. compare_classes (const void *a, const void *b)
  587. {
  588. const char *aname, *bname;
  589. aname = (*(struct symbol **) a)->print_name ();
  590. bname = (*(struct symbol **) b)->print_name ();
  591. if (aname == NULL || bname == NULL)
  592. error (_("internal: compare_classes(1)"));
  593. return specialcmp (aname+1, bname+1);
  594. }
  595. /*
  596. * Function: classes_info(regexp, from_tty)
  597. *
  598. * Implements the "info classes" command for objective c classes.
  599. * Lists all objective c classes that match the optional regexp.
  600. * Works by grepping thru the list of objective c methods. List will
  601. * be sorted and uniqued (since one class may have many methods).
  602. * BUGS: will not list a class that has no methods.
  603. */
  604. static void
  605. info_classes_command (const char *regexp, int from_tty)
  606. {
  607. const char *name;
  608. char *val;
  609. int matches = 0;
  610. int maxlen = 0;
  611. int ix;
  612. char myregexp[2048];
  613. char aclass[256];
  614. struct symbol **sym_arr;
  615. if (regexp == NULL)
  616. strcpy(myregexp, ".* "); /* Null input: match all objc classes. */
  617. else
  618. {
  619. /* Allow a few extra bytes because of the strcat below. */
  620. if (sizeof (myregexp) < strlen (regexp) + 4)
  621. error (_("Regexp is too long: %s"), regexp);
  622. strcpy(myregexp, regexp);
  623. if (myregexp[strlen(myregexp) - 1] == '$')
  624. /* In the method name, the end of the class name is marked by ' '. */
  625. myregexp[strlen(myregexp) - 1] = ' ';
  626. else
  627. strcat(myregexp, ".* ");
  628. }
  629. if (regexp != NULL)
  630. {
  631. val = re_comp (myregexp);
  632. if (val != 0)
  633. error (_("Invalid regexp (%s): %s"), val, regexp);
  634. }
  635. /* First time thru is JUST to get max length and count. */
  636. for (objfile *objfile : current_program_space->objfiles ())
  637. {
  638. for (minimal_symbol *msymbol : objfile->msymbols ())
  639. {
  640. QUIT;
  641. name = msymbol->natural_name ();
  642. if (name &&
  643. (name[0] == '-' || name[0] == '+') &&
  644. name[1] == '[') /* Got a method name. */
  645. if (regexp == NULL || re_exec(name+2) != 0)
  646. {
  647. /* Compute length of classname part. */
  648. const char *mystart = name + 2;
  649. const char *myend = strchr (mystart, ' ');
  650. if (myend && (myend - mystart > maxlen))
  651. maxlen = myend - mystart;
  652. matches++;
  653. }
  654. }
  655. }
  656. if (matches)
  657. {
  658. gdb_printf (_("Classes matching \"%s\":\n\n"),
  659. regexp ? regexp : "*");
  660. sym_arr = XALLOCAVEC (struct symbol *, matches);
  661. matches = 0;
  662. for (objfile *objfile : current_program_space->objfiles ())
  663. {
  664. for (minimal_symbol *msymbol : objfile->msymbols ())
  665. {
  666. QUIT;
  667. name = msymbol->natural_name ();
  668. if (name &&
  669. (name[0] == '-' || name[0] == '+') &&
  670. name[1] == '[') /* Got a method name. */
  671. if (regexp == NULL || re_exec(name+2) != 0)
  672. sym_arr[matches++] = (struct symbol *) msymbol;
  673. }
  674. }
  675. qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
  676. compare_classes);
  677. /* Prevent compare on first iteration. */
  678. aclass[0] = 0;
  679. for (ix = 0; ix < matches; ix++) /* Now do the output. */
  680. {
  681. char *p = aclass;
  682. QUIT;
  683. name = sym_arr[ix]->natural_name ();
  684. name += 2;
  685. if (p[0] && specialcmp(name, p) == 0)
  686. continue; /* Seen this one already (not unique). */
  687. /* Copy class part of method name. */
  688. while (*name && *name != ' ')
  689. *p++ = *name++;
  690. *p++ = '\0';
  691. /* Print in columns. */
  692. puts_tabular(aclass, maxlen + 1, 0);
  693. }
  694. begin_line();
  695. }
  696. else
  697. gdb_printf (_("No classes matching \"%s\"\n"), regexp ? regexp : "*");
  698. }
  699. static char *
  700. parse_selector (char *method, char **selector)
  701. {
  702. char *s1 = NULL;
  703. char *s2 = NULL;
  704. int found_quote = 0;
  705. char *nselector = NULL;
  706. gdb_assert (selector != NULL);
  707. s1 = method;
  708. s1 = skip_spaces (s1);
  709. if (*s1 == '\'')
  710. {
  711. found_quote = 1;
  712. s1++;
  713. }
  714. s1 = skip_spaces (s1);
  715. nselector = s1;
  716. s2 = s1;
  717. for (;;)
  718. {
  719. if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
  720. *s1++ = *s2;
  721. else if (isspace (*s2))
  722. ;
  723. else if ((*s2 == '\0') || (*s2 == '\''))
  724. break;
  725. else
  726. return NULL;
  727. s2++;
  728. }
  729. *s1++ = '\0';
  730. s2 = skip_spaces (s2);
  731. if (found_quote)
  732. {
  733. if (*s2 == '\'')
  734. s2++;
  735. s2 = skip_spaces (s2);
  736. }
  737. if (selector != NULL)
  738. *selector = nselector;
  739. return s2;
  740. }
  741. static char *
  742. parse_method (char *method, char *type, char **theclass,
  743. char **category, char **selector)
  744. {
  745. char *s1 = NULL;
  746. char *s2 = NULL;
  747. int found_quote = 0;
  748. char ntype = '\0';
  749. char *nclass = NULL;
  750. char *ncategory = NULL;
  751. char *nselector = NULL;
  752. gdb_assert (type != NULL);
  753. gdb_assert (theclass != NULL);
  754. gdb_assert (category != NULL);
  755. gdb_assert (selector != NULL);
  756. s1 = method;
  757. s1 = skip_spaces (s1);
  758. if (*s1 == '\'')
  759. {
  760. found_quote = 1;
  761. s1++;
  762. }
  763. s1 = skip_spaces (s1);
  764. if ((s1[0] == '+') || (s1[0] == '-'))
  765. ntype = *s1++;
  766. s1 = skip_spaces (s1);
  767. if (*s1 != '[')
  768. return NULL;
  769. s1++;
  770. nclass = s1;
  771. while (isalnum (*s1) || (*s1 == '_'))
  772. s1++;
  773. s2 = s1;
  774. s2 = skip_spaces (s2);
  775. if (*s2 == '(')
  776. {
  777. s2++;
  778. s2 = skip_spaces (s2);
  779. ncategory = s2;
  780. while (isalnum (*s2) || (*s2 == '_'))
  781. s2++;
  782. *s2++ = '\0';
  783. }
  784. /* Truncate the class name now that we're not using the open paren. */
  785. *s1++ = '\0';
  786. nselector = s2;
  787. s1 = s2;
  788. for (;;)
  789. {
  790. if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
  791. *s1++ = *s2;
  792. else if (isspace (*s2))
  793. ;
  794. else if (*s2 == ']')
  795. break;
  796. else
  797. return NULL;
  798. s2++;
  799. }
  800. *s1++ = '\0';
  801. s2++;
  802. s2 = skip_spaces (s2);
  803. if (found_quote)
  804. {
  805. if (*s2 != '\'')
  806. return NULL;
  807. s2++;
  808. s2 = skip_spaces (s2);
  809. }
  810. if (type != NULL)
  811. *type = ntype;
  812. if (theclass != NULL)
  813. *theclass = nclass;
  814. if (category != NULL)
  815. *category = ncategory;
  816. if (selector != NULL)
  817. *selector = nselector;
  818. return s2;
  819. }
  820. static void
  821. find_methods (char type, const char *theclass, const char *category,
  822. const char *selector,
  823. std::vector<const char *> *symbol_names)
  824. {
  825. const char *symname = NULL;
  826. char ntype = '\0';
  827. char *nclass = NULL;
  828. char *ncategory = NULL;
  829. char *nselector = NULL;
  830. static char *tmp = NULL;
  831. static unsigned int tmplen = 0;
  832. gdb_assert (symbol_names != NULL);
  833. for (objfile *objfile : current_program_space->objfiles ())
  834. {
  835. unsigned int *objc_csym;
  836. /* The objfile_csym variable counts the number of ObjC methods
  837. that this objfile defines. We save that count as a private
  838. objfile data. If we have already determined that this objfile
  839. provides no ObjC methods, we can skip it entirely. */
  840. unsigned int objfile_csym = 0;
  841. objc_csym = objc_objfile_data.get (objfile);
  842. if (objc_csym != NULL && *objc_csym == 0)
  843. /* There are no ObjC symbols in this objfile. Skip it entirely. */
  844. continue;
  845. for (minimal_symbol *msymbol : objfile->msymbols ())
  846. {
  847. QUIT;
  848. /* Check the symbol name first as this can be done entirely without
  849. sending any query to the target. */
  850. symname = msymbol->natural_name ();
  851. if (symname == NULL)
  852. continue;
  853. if ((symname[0] != '-' && symname[0] != '+') || (symname[1] != '['))
  854. /* Not a method name. */
  855. continue;
  856. objfile_csym++;
  857. /* Now that thinks are a bit sane, clean up the symname. */
  858. while ((strlen (symname) + 1) >= tmplen)
  859. {
  860. tmplen = (tmplen == 0) ? 1024 : tmplen * 2;
  861. tmp = (char *) xrealloc (tmp, tmplen);
  862. }
  863. strcpy (tmp, symname);
  864. if (parse_method (tmp, &ntype, &nclass,
  865. &ncategory, &nselector) == NULL)
  866. continue;
  867. if ((type != '\0') && (ntype != type))
  868. continue;
  869. if ((theclass != NULL)
  870. && ((nclass == NULL) || (strcmp (theclass, nclass) != 0)))
  871. continue;
  872. if ((category != NULL) &&
  873. ((ncategory == NULL) || (strcmp (category, ncategory) != 0)))
  874. continue;
  875. if ((selector != NULL) &&
  876. ((nselector == NULL) || (strcmp (selector, nselector) != 0)))
  877. continue;
  878. symbol_names->push_back (symname);
  879. }
  880. if (objc_csym == NULL)
  881. objc_csym = objc_objfile_data.emplace (objfile, objfile_csym);
  882. else
  883. /* Count of ObjC methods in this objfile should be constant. */
  884. gdb_assert (*objc_csym == objfile_csym);
  885. }
  886. }
  887. /* Uniquify a vector of strings. */
  888. static void
  889. uniquify_strings (std::vector<const char *> *strings)
  890. {
  891. if (strings->empty ())
  892. return;
  893. std::sort (strings->begin (), strings->end (), compare_cstrings);
  894. strings->erase (std::unique (strings->begin (), strings->end (), streq),
  895. strings->end ());
  896. }
  897. /*
  898. * Function: find_imps (const char *selector, struct symbol **sym_arr)
  899. *
  900. * Input: a string representing a selector
  901. * a pointer to an array of symbol pointers
  902. * possibly a pointer to a symbol found by the caller.
  903. *
  904. * Output: number of methods that implement that selector. Side
  905. * effects: The array of symbol pointers is filled with matching syms.
  906. *
  907. * By analogy with function "find_methods" (symtab.c), builds a list
  908. * of symbols matching the ambiguous input, so that "decode_line_2"
  909. * (symtab.c) can list them and ask the user to choose one or more.
  910. * In this case the matches are objective c methods
  911. * ("implementations") matching an objective c selector.
  912. *
  913. * Note that it is possible for a normal (c-style) function to have
  914. * the same name as an objective c selector. To prevent the selector
  915. * from eclipsing the function, we allow the caller (decode_line_1) to
  916. * search for such a function first, and if it finds one, pass it in
  917. * to us. We will then integrate it into the list. We also search
  918. * for one here, among the minsyms.
  919. *
  920. * NOTE: if NUM_DEBUGGABLE is non-zero, the sym_arr will be divided
  921. * into two parts: debuggable (struct symbol) syms, and
  922. * non_debuggable (struct minimal_symbol) syms. The debuggable
  923. * ones will come first, before NUM_DEBUGGABLE (which will thus
  924. * be the index of the first non-debuggable one).
  925. */
  926. const char *
  927. find_imps (const char *method, std::vector<const char *> *symbol_names)
  928. {
  929. char type = '\0';
  930. char *theclass = NULL;
  931. char *category = NULL;
  932. char *selector = NULL;
  933. char *buf = NULL;
  934. char *tmp = NULL;
  935. int selector_case = 0;
  936. gdb_assert (symbol_names != NULL);
  937. buf = (char *) alloca (strlen (method) + 1);
  938. strcpy (buf, method);
  939. tmp = parse_method (buf, &type, &theclass, &category, &selector);
  940. if (tmp == NULL)
  941. {
  942. strcpy (buf, method);
  943. tmp = parse_selector (buf, &selector);
  944. if (tmp == NULL)
  945. return NULL;
  946. selector_case = 1;
  947. }
  948. find_methods (type, theclass, category, selector, symbol_names);
  949. /* If we hit the "selector" case, and we found some methods, then
  950. add the selector itself as a symbol, if it exists. */
  951. if (selector_case && !symbol_names->empty ())
  952. {
  953. struct symbol *sym = lookup_symbol (selector, NULL, VAR_DOMAIN,
  954. 0).symbol;
  955. if (sym != NULL)
  956. symbol_names->push_back (sym->natural_name ());
  957. else
  958. {
  959. struct bound_minimal_symbol msym
  960. = lookup_minimal_symbol (selector, 0, 0);
  961. if (msym.minsym != NULL)
  962. symbol_names->push_back (msym.minsym->natural_name ());
  963. }
  964. }
  965. uniquify_strings (symbol_names);
  966. return method + (tmp - buf);
  967. }
  968. static void
  969. print_object_command (const char *args, int from_tty)
  970. {
  971. struct value *object, *function, *description;
  972. CORE_ADDR string_addr, object_addr;
  973. int i = 0;
  974. gdb_byte c = 0;
  975. if (!args || !*args)
  976. error (
  977. "The 'print-object' command requires an argument (an Objective-C object)");
  978. {
  979. expression_up expr = parse_expression (args);
  980. object
  981. = evaluate_expression (expr.get (),
  982. builtin_type (expr->gdbarch)->builtin_data_ptr);
  983. }
  984. /* Validate the address for sanity. */
  985. object_addr = value_as_long (object);
  986. read_memory (object_addr, &c, 1);
  987. function = find_function_in_inferior ("_NSPrintForDebugger", NULL);
  988. if (function == NULL)
  989. error (_("Unable to locate _NSPrintForDebugger in child process"));
  990. description = call_function_by_hand (function, NULL, object);
  991. string_addr = value_as_long (description);
  992. if (string_addr == 0)
  993. error (_("object returns null description"));
  994. read_memory (string_addr + i++, &c, 1);
  995. if (c != 0)
  996. do
  997. { /* Read and print characters up to EOS. */
  998. QUIT;
  999. gdb_printf ("%c", c);
  1000. read_memory (string_addr + i++, &c, 1);
  1001. } while (c != 0);
  1002. else
  1003. gdb_printf(_("<object returns empty description>"));
  1004. gdb_printf ("\n");
  1005. }
  1006. /* The data structure 'methcalls' is used to detect method calls (thru
  1007. * ObjC runtime lib functions objc_msgSend, objc_msgSendSuper, etc.),
  1008. * and ultimately find the method being called.
  1009. */
  1010. struct objc_methcall {
  1011. const char *name;
  1012. /* Return instance method to be called. */
  1013. int (*stop_at) (CORE_ADDR, CORE_ADDR *);
  1014. /* Start of pc range corresponding to method invocation. */
  1015. CORE_ADDR begin;
  1016. /* End of pc range corresponding to method invocation. */
  1017. CORE_ADDR end;
  1018. };
  1019. static int resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc);
  1020. static int resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
  1021. static int resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc);
  1022. static int resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
  1023. static struct objc_methcall methcalls[] = {
  1024. { "_objc_msgSend", resolve_msgsend, 0, 0},
  1025. { "_objc_msgSend_stret", resolve_msgsend_stret, 0, 0},
  1026. { "_objc_msgSendSuper", resolve_msgsend_super, 0, 0},
  1027. { "_objc_msgSendSuper_stret", resolve_msgsend_super_stret, 0, 0},
  1028. { "_objc_getClass", NULL, 0, 0},
  1029. { "_objc_getMetaClass", NULL, 0, 0}
  1030. };
  1031. #define nmethcalls (sizeof (methcalls) / sizeof (methcalls[0]))
  1032. /* The following function, "find_objc_msgsend", fills in the data
  1033. * structure "objc_msgs" by finding the addresses of each of the
  1034. * (currently four) functions that it holds (of which objc_msgSend is
  1035. * the first). This must be called each time symbols are loaded, in
  1036. * case the functions have moved for some reason.
  1037. */
  1038. static void
  1039. find_objc_msgsend (void)
  1040. {
  1041. unsigned int i;
  1042. for (i = 0; i < nmethcalls; i++)
  1043. {
  1044. struct bound_minimal_symbol func;
  1045. /* Try both with and without underscore. */
  1046. func = lookup_bound_minimal_symbol (methcalls[i].name);
  1047. if ((func.minsym == NULL) && (methcalls[i].name[0] == '_'))
  1048. {
  1049. func = lookup_bound_minimal_symbol (methcalls[i].name + 1);
  1050. }
  1051. if (func.minsym == NULL)
  1052. {
  1053. methcalls[i].begin = 0;
  1054. methcalls[i].end = 0;
  1055. continue;
  1056. }
  1057. methcalls[i].begin = BMSYMBOL_VALUE_ADDRESS (func);
  1058. methcalls[i].end = minimal_symbol_upper_bound (func);
  1059. }
  1060. }
  1061. /* find_objc_msgcall (replaces pc_off_limits)
  1062. *
  1063. * ALL that this function now does is to determine whether the input
  1064. * address ("pc") is the address of one of the Objective-C message
  1065. * dispatch functions (mainly objc_msgSend or objc_msgSendSuper), and
  1066. * if so, it returns the address of the method that will be called.
  1067. *
  1068. * The old function "pc_off_limits" used to do a lot of other things
  1069. * in addition, such as detecting shared library jump stubs and
  1070. * returning the address of the shlib function that would be called.
  1071. * That functionality has been moved into the gdbarch_skip_trampoline_code and
  1072. * IN_SOLIB_TRAMPOLINE macros, which are resolved in the target-
  1073. * dependent modules.
  1074. */
  1075. static int
  1076. find_objc_msgcall_submethod (int (*f) (CORE_ADDR, CORE_ADDR *),
  1077. CORE_ADDR pc,
  1078. CORE_ADDR *new_pc)
  1079. {
  1080. try
  1081. {
  1082. if (f (pc, new_pc) == 0)
  1083. return 1;
  1084. }
  1085. catch (const gdb_exception &ex)
  1086. {
  1087. exception_fprintf (gdb_stderr, ex,
  1088. "Unable to determine target of "
  1089. "Objective-C method call (ignoring):\n");
  1090. }
  1091. return 0;
  1092. }
  1093. int
  1094. find_objc_msgcall (CORE_ADDR pc, CORE_ADDR *new_pc)
  1095. {
  1096. unsigned int i;
  1097. find_objc_msgsend ();
  1098. if (new_pc != NULL)
  1099. {
  1100. *new_pc = 0;
  1101. }
  1102. for (i = 0; i < nmethcalls; i++)
  1103. if ((pc >= methcalls[i].begin) && (pc < methcalls[i].end))
  1104. {
  1105. if (methcalls[i].stop_at != NULL)
  1106. return find_objc_msgcall_submethod (methcalls[i].stop_at,
  1107. pc, new_pc);
  1108. else
  1109. return 0;
  1110. }
  1111. return 0;
  1112. }
  1113. void _initialize_objc_language ();
  1114. void
  1115. _initialize_objc_language ()
  1116. {
  1117. add_info ("selectors", info_selectors_command,
  1118. _("All Objective-C selectors, or those matching REGEXP."));
  1119. add_info ("classes", info_classes_command,
  1120. _("All Objective-C classes, or those matching REGEXP."));
  1121. cmd_list_element *print_object_cmd
  1122. = add_com ("print-object", class_vars, print_object_command,
  1123. _("Ask an Objective-C object to print itself."));
  1124. add_com_alias ("po", print_object_cmd, class_vars, 1);
  1125. }
  1126. static void
  1127. read_objc_method (struct gdbarch *gdbarch, CORE_ADDR addr,
  1128. struct objc_method *method)
  1129. {
  1130. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1131. method->name = read_memory_unsigned_integer (addr + 0, 4, byte_order);
  1132. method->types = read_memory_unsigned_integer (addr + 4, 4, byte_order);
  1133. method->imp = read_memory_unsigned_integer (addr + 8, 4, byte_order);
  1134. }
  1135. static unsigned long
  1136. read_objc_methlist_nmethods (struct gdbarch *gdbarch, CORE_ADDR addr)
  1137. {
  1138. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1139. return read_memory_unsigned_integer (addr + 4, 4, byte_order);
  1140. }
  1141. static void
  1142. read_objc_methlist_method (struct gdbarch *gdbarch, CORE_ADDR addr,
  1143. unsigned long num, struct objc_method *method)
  1144. {
  1145. gdb_assert (num < read_objc_methlist_nmethods (gdbarch, addr));
  1146. read_objc_method (gdbarch, addr + 8 + (12 * num), method);
  1147. }
  1148. static void
  1149. read_objc_object (struct gdbarch *gdbarch, CORE_ADDR addr,
  1150. struct objc_object *object)
  1151. {
  1152. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1153. object->isa = read_memory_unsigned_integer (addr, 4, byte_order);
  1154. }
  1155. static void
  1156. read_objc_super (struct gdbarch *gdbarch, CORE_ADDR addr,
  1157. struct objc_super *super)
  1158. {
  1159. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1160. super->receiver = read_memory_unsigned_integer (addr, 4, byte_order);
  1161. super->theclass = read_memory_unsigned_integer (addr + 4, 4, byte_order);
  1162. };
  1163. static void
  1164. read_objc_class (struct gdbarch *gdbarch, CORE_ADDR addr,
  1165. struct objc_class *theclass)
  1166. {
  1167. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1168. theclass->isa = read_memory_unsigned_integer (addr, 4, byte_order);
  1169. theclass->super_class = read_memory_unsigned_integer (addr + 4, 4, byte_order);
  1170. theclass->name = read_memory_unsigned_integer (addr + 8, 4, byte_order);
  1171. theclass->version = read_memory_unsigned_integer (addr + 12, 4, byte_order);
  1172. theclass->info = read_memory_unsigned_integer (addr + 16, 4, byte_order);
  1173. theclass->instance_size = read_memory_unsigned_integer (addr + 18, 4,
  1174. byte_order);
  1175. theclass->ivars = read_memory_unsigned_integer (addr + 24, 4, byte_order);
  1176. theclass->methods = read_memory_unsigned_integer (addr + 28, 4, byte_order);
  1177. theclass->cache = read_memory_unsigned_integer (addr + 32, 4, byte_order);
  1178. theclass->protocols = read_memory_unsigned_integer (addr + 36, 4, byte_order);
  1179. }
  1180. static CORE_ADDR
  1181. find_implementation_from_class (struct gdbarch *gdbarch,
  1182. CORE_ADDR theclass, CORE_ADDR sel)
  1183. {
  1184. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1185. CORE_ADDR subclass = theclass;
  1186. while (subclass != 0)
  1187. {
  1188. struct objc_class class_str;
  1189. unsigned mlistnum = 0;
  1190. read_objc_class (gdbarch, subclass, &class_str);
  1191. for (;;)
  1192. {
  1193. CORE_ADDR mlist;
  1194. unsigned long nmethods;
  1195. unsigned long i;
  1196. mlist = read_memory_unsigned_integer (class_str.methods +
  1197. (4 * mlistnum),
  1198. 4, byte_order);
  1199. if (mlist == 0)
  1200. break;
  1201. nmethods = read_objc_methlist_nmethods (gdbarch, mlist);
  1202. for (i = 0; i < nmethods; i++)
  1203. {
  1204. struct objc_method meth_str;
  1205. read_objc_methlist_method (gdbarch, mlist, i, &meth_str);
  1206. if (meth_str.name == sel)
  1207. /* FIXME: hppa arch was doing a pointer dereference
  1208. here. There needs to be a better way to do that. */
  1209. return meth_str.imp;
  1210. }
  1211. mlistnum++;
  1212. }
  1213. subclass = class_str.super_class;
  1214. }
  1215. return 0;
  1216. }
  1217. static CORE_ADDR
  1218. find_implementation (struct gdbarch *gdbarch,
  1219. CORE_ADDR object, CORE_ADDR sel)
  1220. {
  1221. struct objc_object ostr;
  1222. if (object == 0)
  1223. return 0;
  1224. read_objc_object (gdbarch, object, &ostr);
  1225. if (ostr.isa == 0)
  1226. return 0;
  1227. return find_implementation_from_class (gdbarch, ostr.isa, sel);
  1228. }
  1229. static int
  1230. resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc)
  1231. {
  1232. struct frame_info *frame = get_current_frame ();
  1233. struct gdbarch *gdbarch = get_frame_arch (frame);
  1234. struct type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;
  1235. CORE_ADDR object;
  1236. CORE_ADDR sel;
  1237. CORE_ADDR res;
  1238. object = gdbarch_fetch_pointer_argument (gdbarch, frame, 0, ptr_type);
  1239. sel = gdbarch_fetch_pointer_argument (gdbarch, frame, 1, ptr_type);
  1240. res = find_implementation (gdbarch, object, sel);
  1241. if (new_pc != 0)
  1242. *new_pc = res;
  1243. if (res == 0)
  1244. return 1;
  1245. return 0;
  1246. }
  1247. static int
  1248. resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
  1249. {
  1250. struct frame_info *frame = get_current_frame ();
  1251. struct gdbarch *gdbarch = get_frame_arch (frame);
  1252. struct type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;
  1253. CORE_ADDR object;
  1254. CORE_ADDR sel;
  1255. CORE_ADDR res;
  1256. object = gdbarch_fetch_pointer_argument (gdbarch, frame, 1, ptr_type);
  1257. sel = gdbarch_fetch_pointer_argument (gdbarch, frame, 2, ptr_type);
  1258. res = find_implementation (gdbarch, object, sel);
  1259. if (new_pc != 0)
  1260. *new_pc = res;
  1261. if (res == 0)
  1262. return 1;
  1263. return 0;
  1264. }
  1265. static int
  1266. resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc)
  1267. {
  1268. struct frame_info *frame = get_current_frame ();
  1269. struct gdbarch *gdbarch = get_frame_arch (frame);
  1270. struct type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;
  1271. struct objc_super sstr;
  1272. CORE_ADDR super;
  1273. CORE_ADDR sel;
  1274. CORE_ADDR res;
  1275. super = gdbarch_fetch_pointer_argument (gdbarch, frame, 0, ptr_type);
  1276. sel = gdbarch_fetch_pointer_argument (gdbarch, frame, 1, ptr_type);
  1277. read_objc_super (gdbarch, super, &sstr);
  1278. if (sstr.theclass == 0)
  1279. return 0;
  1280. res = find_implementation_from_class (gdbarch, sstr.theclass, sel);
  1281. if (new_pc != 0)
  1282. *new_pc = res;
  1283. if (res == 0)
  1284. return 1;
  1285. return 0;
  1286. }
  1287. static int
  1288. resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
  1289. {
  1290. struct frame_info *frame = get_current_frame ();
  1291. struct gdbarch *gdbarch = get_frame_arch (frame);
  1292. struct type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;
  1293. struct objc_super sstr;
  1294. CORE_ADDR super;
  1295. CORE_ADDR sel;
  1296. CORE_ADDR res;
  1297. super = gdbarch_fetch_pointer_argument (gdbarch, frame, 1, ptr_type);
  1298. sel = gdbarch_fetch_pointer_argument (gdbarch, frame, 2, ptr_type);
  1299. read_objc_super (gdbarch, super, &sstr);
  1300. if (sstr.theclass == 0)
  1301. return 0;
  1302. res = find_implementation_from_class (gdbarch, sstr.theclass, sel);
  1303. if (new_pc != 0)
  1304. *new_pc = res;
  1305. if (res == 0)
  1306. return 1;
  1307. return 0;
  1308. }