Commit | Line | Data |
---|---|---|
d2e6263c | 1 | /* Objective-C language support routines for GDB, the GNU debugger. |
b81654f1 | 2 | |
6aba47ca | 3 | Copyright (C) 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc. |
b81654f1 | 4 | |
437666f8 AC |
5 | Contributed by Apple Computer, Inc. |
6 | Written by Michael Snyder. | |
b81654f1 | 7 | |
437666f8 | 8 | This file is part of GDB. |
b81654f1 | 9 | |
437666f8 AC |
10 | This program is free software; you can redistribute it and/or modify |
11 | it under the terms of the GNU General Public License as published by | |
12 | the Free Software Foundation; either version 2 of the License, or | |
13 | (at your option) any later version. | |
14 | ||
15 | This program is distributed in the hope that it will be useful, | |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
21 | along with this program; if not, write to the Free Software | |
197e01b6 EZ |
22 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 | Boston, MA 02110-1301, USA. */ | |
b81654f1 MS |
24 | |
25 | #include "defs.h" | |
26 | #include "symtab.h" | |
27 | #include "gdbtypes.h" | |
28 | #include "expression.h" | |
29 | #include "parser-defs.h" | |
30 | #include "language.h" | |
d2e6263c | 31 | #include "c-lang.h" |
b81654f1 | 32 | #include "objc-lang.h" |
60250e8b | 33 | #include "exceptions.h" |
b81654f1 MS |
34 | #include "complaints.h" |
35 | #include "value.h" | |
36 | #include "symfile.h" | |
37 | #include "objfiles.h" | |
7248f48e | 38 | #include "gdb_string.h" /* for strchr */ |
b81654f1 MS |
39 | #include "target.h" /* for target_has_execution */ |
40 | #include "gdbcore.h" | |
41 | #include "gdbcmd.h" | |
42 | #include "frame.h" | |
43 | #include "gdb_regex.h" | |
44 | #include "regcache.h" | |
fe898f56 | 45 | #include "block.h" |
04714b91 | 46 | #include "infcall.h" |
4e45ca2e | 47 | #include "valprint.h" |
e8f3fcdd | 48 | #include "gdb_assert.h" |
b81654f1 MS |
49 | |
50 | #include <ctype.h> | |
51 | ||
52 | struct objc_object { | |
53 | CORE_ADDR isa; | |
54 | }; | |
55 | ||
56 | struct objc_class { | |
57 | CORE_ADDR isa; | |
58 | CORE_ADDR super_class; | |
59 | CORE_ADDR name; | |
60 | long version; | |
61 | long info; | |
62 | long instance_size; | |
63 | CORE_ADDR ivars; | |
64 | CORE_ADDR methods; | |
65 | CORE_ADDR cache; | |
66 | CORE_ADDR protocols; | |
67 | }; | |
68 | ||
69 | struct objc_super { | |
70 | CORE_ADDR receiver; | |
71 | CORE_ADDR class; | |
72 | }; | |
73 | ||
74 | struct objc_method { | |
75 | CORE_ADDR name; | |
76 | CORE_ADDR types; | |
77 | CORE_ADDR imp; | |
78 | }; | |
79 | ||
d2e6263c MS |
80 | /* Lookup a structure type named "struct NAME", visible in lexical |
81 | block BLOCK. If NOERR is nonzero, return zero if NAME is not | |
82 | suitably defined. */ | |
b81654f1 MS |
83 | |
84 | struct symbol * | |
85 | lookup_struct_typedef (char *name, struct block *block, int noerr) | |
86 | { | |
f86f5ca3 | 87 | struct symbol *sym; |
b81654f1 | 88 | |
176620f1 | 89 | sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0, |
d2e6263c | 90 | (struct symtab **) NULL); |
b81654f1 MS |
91 | |
92 | if (sym == NULL) | |
93 | { | |
94 | if (noerr) | |
95 | return 0; | |
96 | else | |
8a3fe4f8 | 97 | error (_("No struct type named %s."), name); |
b81654f1 MS |
98 | } |
99 | if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT) | |
100 | { | |
101 | if (noerr) | |
102 | return 0; | |
103 | else | |
8a3fe4f8 | 104 | error (_("This context has class, union or enum %s, not a struct."), |
d2e6263c | 105 | name); |
b81654f1 MS |
106 | } |
107 | return sym; | |
108 | } | |
109 | ||
110 | CORE_ADDR | |
111 | lookup_objc_class (char *classname) | |
112 | { | |
113 | struct value * function, *classval; | |
114 | ||
115 | if (! target_has_execution) | |
116 | { | |
d2e6263c | 117 | /* Can't call into inferior to lookup class. */ |
b81654f1 MS |
118 | return 0; |
119 | } | |
120 | ||
121 | if (lookup_minimal_symbol("objc_lookUpClass", 0, 0)) | |
122 | function = find_function_in_inferior("objc_lookUpClass"); | |
123 | else if (lookup_minimal_symbol ("objc_lookup_class", 0, 0)) | |
124 | function = find_function_in_inferior("objc_lookup_class"); | |
125 | else | |
126 | { | |
e2e0b3e5 | 127 | complaint (&symfile_complaints, _("no way to lookup Objective-C classes")); |
b81654f1 MS |
128 | return 0; |
129 | } | |
130 | ||
131 | classval = value_string (classname, strlen (classname) + 1); | |
132 | classval = value_coerce_array (classval); | |
133 | return (CORE_ADDR) value_as_long (call_function_by_hand (function, | |
134 | 1, &classval)); | |
135 | } | |
136 | ||
c253954e | 137 | CORE_ADDR |
b81654f1 MS |
138 | lookup_child_selector (char *selname) |
139 | { | |
140 | struct value * function, *selstring; | |
141 | ||
142 | if (! target_has_execution) | |
143 | { | |
d2e6263c | 144 | /* Can't call into inferior to lookup selector. */ |
b81654f1 MS |
145 | return 0; |
146 | } | |
147 | ||
148 | if (lookup_minimal_symbol("sel_getUid", 0, 0)) | |
149 | function = find_function_in_inferior("sel_getUid"); | |
150 | else if (lookup_minimal_symbol ("sel_get_any_uid", 0, 0)) | |
151 | function = find_function_in_inferior("sel_get_any_uid"); | |
152 | else | |
153 | { | |
e2e0b3e5 | 154 | complaint (&symfile_complaints, _("no way to lookup Objective-C selectors")); |
b81654f1 MS |
155 | return 0; |
156 | } | |
157 | ||
d2e6263c MS |
158 | selstring = value_coerce_array (value_string (selname, |
159 | strlen (selname) + 1)); | |
b81654f1 MS |
160 | return value_as_long (call_function_by_hand (function, 1, &selstring)); |
161 | } | |
162 | ||
163 | struct value * | |
164 | value_nsstring (char *ptr, int len) | |
165 | { | |
166 | struct value *stringValue[3]; | |
167 | struct value *function, *nsstringValue; | |
168 | struct symbol *sym; | |
169 | struct type *type; | |
170 | ||
171 | if (!target_has_execution) | |
d2e6263c | 172 | return 0; /* Can't call into inferior to create NSString. */ |
b81654f1 | 173 | |
5e488a7b AC |
174 | sym = lookup_struct_typedef("NSString", 0, 1); |
175 | if (sym == NULL) | |
176 | sym = lookup_struct_typedef("NXString", 0, 1); | |
177 | if (sym == NULL) | |
b81654f1 MS |
178 | type = lookup_pointer_type(builtin_type_void); |
179 | else | |
180 | type = lookup_pointer_type(SYMBOL_TYPE (sym)); | |
181 | ||
182 | stringValue[2] = value_string(ptr, len); | |
183 | stringValue[2] = value_coerce_array(stringValue[2]); | |
d2e6263c | 184 | /* _NSNewStringFromCString replaces "istr" after Lantern2A. */ |
b81654f1 MS |
185 | if (lookup_minimal_symbol("_NSNewStringFromCString", 0, 0)) |
186 | { | |
187 | function = find_function_in_inferior("_NSNewStringFromCString"); | |
188 | nsstringValue = call_function_by_hand(function, 1, &stringValue[2]); | |
189 | } | |
190 | else if (lookup_minimal_symbol("istr", 0, 0)) | |
191 | { | |
192 | function = find_function_in_inferior("istr"); | |
193 | nsstringValue = call_function_by_hand(function, 1, &stringValue[2]); | |
194 | } | |
195 | else if (lookup_minimal_symbol("+[NSString stringWithCString:]", 0, 0)) | |
196 | { | |
197 | function = find_function_in_inferior("+[NSString stringWithCString:]"); | |
198 | stringValue[0] = value_from_longest | |
199 | (builtin_type_long, lookup_objc_class ("NSString")); | |
200 | stringValue[1] = value_from_longest | |
201 | (builtin_type_long, lookup_child_selector ("stringWithCString:")); | |
202 | nsstringValue = call_function_by_hand(function, 3, &stringValue[0]); | |
203 | } | |
204 | else | |
8a3fe4f8 | 205 | error (_("NSString: internal error -- no way to create new NSString")); |
b81654f1 | 206 | |
04624583 | 207 | deprecated_set_value_type (nsstringValue, type); |
b81654f1 MS |
208 | return nsstringValue; |
209 | } | |
210 | ||
d2e6263c | 211 | /* Objective-C name demangling. */ |
b81654f1 MS |
212 | |
213 | char * | |
9a3d7dfd | 214 | objc_demangle (const char *mangled, int options) |
b81654f1 MS |
215 | { |
216 | char *demangled, *cp; | |
217 | ||
218 | if (mangled[0] == '_' && | |
219 | (mangled[1] == 'i' || mangled[1] == 'c') && | |
220 | mangled[2] == '_') | |
221 | { | |
222 | cp = demangled = xmalloc(strlen(mangled) + 2); | |
223 | ||
224 | if (mangled[1] == 'i') | |
225 | *cp++ = '-'; /* for instance method */ | |
226 | else | |
227 | *cp++ = '+'; /* for class method */ | |
228 | ||
229 | *cp++ = '['; /* opening left brace */ | |
230 | strcpy(cp, mangled+3); /* tack on the rest of the mangled name */ | |
231 | ||
232 | while (*cp && *cp == '_') | |
233 | cp++; /* skip any initial underbars in class name */ | |
234 | ||
7248f48e AF |
235 | cp = strchr(cp, '_'); |
236 | if (!cp) /* find first non-initial underbar */ | |
b81654f1 | 237 | { |
7248f48e | 238 | xfree(demangled); /* not mangled name */ |
b81654f1 MS |
239 | return NULL; |
240 | } | |
241 | if (cp[1] == '_') { /* easy case: no category name */ | |
242 | *cp++ = ' '; /* replace two '_' with one ' ' */ | |
243 | strcpy(cp, mangled + (cp - demangled) + 2); | |
244 | } | |
245 | else { | |
246 | *cp++ = '('; /* less easy case: category name */ | |
7248f48e AF |
247 | cp = strchr(cp, '_'); |
248 | if (!cp) | |
b81654f1 | 249 | { |
7248f48e | 250 | xfree(demangled); /* not mangled name */ |
b81654f1 MS |
251 | return NULL; |
252 | } | |
253 | *cp++ = ')'; | |
d2e6263c | 254 | *cp++ = ' '; /* overwriting 1st char of method name... */ |
b81654f1 MS |
255 | strcpy(cp, mangled + (cp - demangled)); /* get it back */ |
256 | } | |
257 | ||
258 | while (*cp && *cp == '_') | |
259 | cp++; /* skip any initial underbars in method name */ | |
260 | ||
261 | for (; *cp; cp++) | |
262 | if (*cp == '_') | |
263 | *cp = ':'; /* replace remaining '_' with ':' */ | |
264 | ||
265 | *cp++ = ']'; /* closing right brace */ | |
266 | *cp++ = 0; /* string terminator */ | |
267 | return demangled; | |
268 | } | |
269 | else | |
d2e6263c | 270 | return NULL; /* Not an objc mangled name. */ |
b81654f1 MS |
271 | } |
272 | ||
d2e6263c MS |
273 | /* Print the character C on STREAM as part of the contents of a |
274 | literal string whose delimiter is QUOTER. Note that that format | |
275 | for printing characters and strings is language specific. */ | |
b81654f1 MS |
276 | |
277 | static void | |
f86f5ca3 | 278 | objc_emit_char (int c, struct ui_file *stream, int quoter) |
b81654f1 MS |
279 | { |
280 | ||
d2e6263c | 281 | c &= 0xFF; /* Avoid sign bit follies. */ |
b81654f1 MS |
282 | |
283 | if (PRINT_LITERAL_FORM (c)) | |
284 | { | |
285 | if (c == '\\' || c == quoter) | |
286 | { | |
287 | fputs_filtered ("\\", stream); | |
288 | } | |
289 | fprintf_filtered (stream, "%c", c); | |
290 | } | |
291 | else | |
292 | { | |
293 | switch (c) | |
294 | { | |
295 | case '\n': | |
296 | fputs_filtered ("\\n", stream); | |
297 | break; | |
298 | case '\b': | |
299 | fputs_filtered ("\\b", stream); | |
300 | break; | |
301 | case '\t': | |
302 | fputs_filtered ("\\t", stream); | |
303 | break; | |
304 | case '\f': | |
305 | fputs_filtered ("\\f", stream); | |
306 | break; | |
307 | case '\r': | |
308 | fputs_filtered ("\\r", stream); | |
309 | break; | |
310 | case '\033': | |
311 | fputs_filtered ("\\e", stream); | |
312 | break; | |
313 | case '\007': | |
314 | fputs_filtered ("\\a", stream); | |
315 | break; | |
316 | default: | |
317 | fprintf_filtered (stream, "\\%.3o", (unsigned int) c); | |
318 | break; | |
319 | } | |
320 | } | |
321 | } | |
322 | ||
323 | static void | |
324 | objc_printchar (int c, struct ui_file *stream) | |
325 | { | |
326 | fputs_filtered ("'", stream); | |
327 | objc_emit_char (c, stream, '\''); | |
328 | fputs_filtered ("'", stream); | |
329 | } | |
330 | ||
d2e6263c MS |
331 | /* Print the character string STRING, printing at most LENGTH |
332 | characters. Printing stops early if the number hits print_max; | |
333 | repeat counts are printed as appropriate. Print ellipses at the | |
334 | end if we had to stop before printing LENGTH characters, or if | |
335 | FORCE_ELLIPSES. */ | |
b81654f1 MS |
336 | |
337 | static void | |
fc1a4b47 | 338 | objc_printstr (struct ui_file *stream, const gdb_byte *string, |
36e53c63 | 339 | unsigned int length, int width, int force_ellipses) |
b81654f1 | 340 | { |
f86f5ca3 | 341 | unsigned int i; |
b81654f1 MS |
342 | unsigned int things_printed = 0; |
343 | int in_quotes = 0; | |
344 | int need_comma = 0; | |
b81654f1 MS |
345 | |
346 | /* If the string was not truncated due to `set print elements', and | |
d2e6263c MS |
347 | the last byte of it is a null, we don't print that, in |
348 | traditional C style. */ | |
b81654f1 MS |
349 | if ((!force_ellipses) && length > 0 && string[length-1] == '\0') |
350 | length--; | |
351 | ||
352 | if (length == 0) | |
353 | { | |
354 | fputs_filtered ("\"\"", stream); | |
355 | return; | |
356 | } | |
357 | ||
358 | for (i = 0; i < length && things_printed < print_max; ++i) | |
359 | { | |
d2e6263c MS |
360 | /* Position of the character we are examining to see whether it |
361 | is repeated. */ | |
b81654f1 MS |
362 | unsigned int rep1; |
363 | /* Number of repetitions we have detected so far. */ | |
364 | unsigned int reps; | |
365 | ||
366 | QUIT; | |
367 | ||
368 | if (need_comma) | |
369 | { | |
370 | fputs_filtered (", ", stream); | |
371 | need_comma = 0; | |
372 | } | |
373 | ||
374 | rep1 = i + 1; | |
375 | reps = 1; | |
376 | while (rep1 < length && string[rep1] == string[i]) | |
377 | { | |
378 | ++rep1; | |
379 | ++reps; | |
380 | } | |
381 | ||
382 | if (reps > repeat_count_threshold) | |
383 | { | |
384 | if (in_quotes) | |
385 | { | |
386 | if (inspect_it) | |
387 | fputs_filtered ("\\\", ", stream); | |
388 | else | |
389 | fputs_filtered ("\", ", stream); | |
390 | in_quotes = 0; | |
391 | } | |
392 | objc_printchar (string[i], stream); | |
393 | fprintf_filtered (stream, " <repeats %u times>", reps); | |
394 | i = rep1 - 1; | |
395 | things_printed += repeat_count_threshold; | |
396 | need_comma = 1; | |
397 | } | |
398 | else | |
399 | { | |
400 | if (!in_quotes) | |
401 | { | |
402 | if (inspect_it) | |
403 | fputs_filtered ("\\\"", stream); | |
404 | else | |
405 | fputs_filtered ("\"", stream); | |
406 | in_quotes = 1; | |
407 | } | |
408 | objc_emit_char (string[i], stream, '"'); | |
409 | ++things_printed; | |
410 | } | |
411 | } | |
412 | ||
413 | /* Terminate the quotes if necessary. */ | |
414 | if (in_quotes) | |
415 | { | |
416 | if (inspect_it) | |
417 | fputs_filtered ("\\\"", stream); | |
418 | else | |
419 | fputs_filtered ("\"", stream); | |
420 | } | |
421 | ||
422 | if (force_ellipses || i < length) | |
423 | fputs_filtered ("...", stream); | |
424 | } | |
425 | ||
d2e6263c MS |
426 | /* Create a fundamental C type using default reasonable for the |
427 | current target. | |
428 | ||
429 | Some object/debugging file formats (DWARF version 1, COFF, etc) do | |
430 | not define fundamental types such as "int" or "double". Others | |
431 | (stabs or DWARF version 2, etc) do define fundamental types. For | |
432 | the formats which don't provide fundamental types, gdb can create | |
433 | such types using this function. | |
434 | ||
435 | FIXME: Some compilers distinguish explicitly signed integral types | |
436 | (signed short, signed int, signed long) from "regular" integral | |
437 | types (short, int, long) in the debugging information. There is | |
438 | some disagreement as to how useful this feature is. In particular, | |
439 | gcc does not support this. Also, only some debugging formats allow | |
440 | the distinction to be passed on to a debugger. For now, we always | |
441 | just use "short", "int", or "long" as the type name, for both the | |
442 | implicit and explicitly signed types. This also makes life easier | |
443 | for the gdb test suite since we don't have to account for the | |
444 | differences in output depending upon what the compiler and | |
445 | debugging format support. We will probably have to re-examine the | |
446 | issue when gdb starts taking it's fundamental type information | |
447 | directly from the debugging information supplied by the compiler. | |
448 | fnf@cygnus.com */ | |
b81654f1 MS |
449 | |
450 | static struct type * | |
451 | objc_create_fundamental_type (struct objfile *objfile, int typeid) | |
452 | { | |
f86f5ca3 | 453 | struct type *type = NULL; |
b81654f1 MS |
454 | |
455 | switch (typeid) | |
456 | { | |
457 | default: | |
d2e6263c MS |
458 | /* FIXME: For now, if we are asked to produce a type not in |
459 | this language, create the equivalent of a C integer type | |
460 | with the name "<?type?>". When all the dust settles from | |
461 | the type reconstruction work, this should probably become | |
462 | an error. */ | |
b81654f1 | 463 | type = init_type (TYPE_CODE_INT, |
9a76efb6 | 464 | gdbarch_int_bit (current_gdbarch) / TARGET_CHAR_BIT, |
b81654f1 | 465 | 0, "<?type?>", objfile); |
8a3fe4f8 | 466 | warning (_("internal error: no C/C++ fundamental type %d"), typeid); |
b81654f1 MS |
467 | break; |
468 | case FT_VOID: | |
469 | type = init_type (TYPE_CODE_VOID, | |
470 | TARGET_CHAR_BIT / TARGET_CHAR_BIT, | |
471 | 0, "void", objfile); | |
472 | break; | |
473 | case FT_CHAR: | |
474 | type = init_type (TYPE_CODE_INT, | |
475 | TARGET_CHAR_BIT / TARGET_CHAR_BIT, | |
476 | 0, "char", objfile); | |
477 | break; | |
478 | case FT_SIGNED_CHAR: | |
479 | type = init_type (TYPE_CODE_INT, | |
480 | TARGET_CHAR_BIT / TARGET_CHAR_BIT, | |
481 | 0, "signed char", objfile); | |
482 | break; | |
483 | case FT_UNSIGNED_CHAR: | |
484 | type = init_type (TYPE_CODE_INT, | |
485 | TARGET_CHAR_BIT / TARGET_CHAR_BIT, | |
486 | TYPE_FLAG_UNSIGNED, "unsigned char", objfile); | |
487 | break; | |
488 | case FT_SHORT: | |
489 | type = init_type (TYPE_CODE_INT, | |
9a76efb6 | 490 | gdbarch_short_bit (current_gdbarch) / TARGET_CHAR_BIT, |
b81654f1 MS |
491 | 0, "short", objfile); |
492 | break; | |
493 | case FT_SIGNED_SHORT: | |
494 | type = init_type (TYPE_CODE_INT, | |
9a76efb6 | 495 | gdbarch_short_bit (current_gdbarch) / TARGET_CHAR_BIT, |
b81654f1 MS |
496 | 0, "short", objfile); /* FIXME-fnf */ |
497 | break; | |
498 | case FT_UNSIGNED_SHORT: | |
499 | type = init_type (TYPE_CODE_INT, | |
9a76efb6 | 500 | gdbarch_short_bit (current_gdbarch) / TARGET_CHAR_BIT, |
b81654f1 MS |
501 | TYPE_FLAG_UNSIGNED, "unsigned short", objfile); |
502 | break; | |
503 | case FT_INTEGER: | |
504 | type = init_type (TYPE_CODE_INT, | |
9a76efb6 | 505 | gdbarch_int_bit (current_gdbarch) / TARGET_CHAR_BIT, |
b81654f1 MS |
506 | 0, "int", objfile); |
507 | break; | |
508 | case FT_SIGNED_INTEGER: | |
509 | type = init_type (TYPE_CODE_INT, | |
9a76efb6 | 510 | gdbarch_int_bit (current_gdbarch) / TARGET_CHAR_BIT, |
b81654f1 MS |
511 | 0, "int", objfile); /* FIXME -fnf */ |
512 | break; | |
513 | case FT_UNSIGNED_INTEGER: | |
514 | type = init_type (TYPE_CODE_INT, | |
9a76efb6 | 515 | gdbarch_int_bit (current_gdbarch) / TARGET_CHAR_BIT, |
b81654f1 MS |
516 | TYPE_FLAG_UNSIGNED, "unsigned int", objfile); |
517 | break; | |
518 | case FT_LONG: | |
519 | type = init_type (TYPE_CODE_INT, | |
9a76efb6 | 520 | gdbarch_long_bit (current_gdbarch) / TARGET_CHAR_BIT, |
b81654f1 MS |
521 | 0, "long", objfile); |
522 | break; | |
523 | case FT_SIGNED_LONG: | |
524 | type = init_type (TYPE_CODE_INT, | |
9a76efb6 | 525 | gdbarch_long_bit (current_gdbarch) / TARGET_CHAR_BIT, |
b81654f1 MS |
526 | 0, "long", objfile); /* FIXME -fnf */ |
527 | break; | |
528 | case FT_UNSIGNED_LONG: | |
529 | type = init_type (TYPE_CODE_INT, | |
9a76efb6 | 530 | gdbarch_long_bit (current_gdbarch) / TARGET_CHAR_BIT, |
b81654f1 MS |
531 | TYPE_FLAG_UNSIGNED, "unsigned long", objfile); |
532 | break; | |
533 | case FT_LONG_LONG: | |
534 | type = init_type (TYPE_CODE_INT, | |
9a76efb6 UW |
535 | gdbarch_long_long_bit (current_gdbarch) |
536 | / TARGET_CHAR_BIT, | |
b81654f1 MS |
537 | 0, "long long", objfile); |
538 | break; | |
539 | case FT_SIGNED_LONG_LONG: | |
540 | type = init_type (TYPE_CODE_INT, | |
9a76efb6 UW |
541 | gdbarch_long_long_bit (current_gdbarch) |
542 | / TARGET_CHAR_BIT, | |
b81654f1 MS |
543 | 0, "signed long long", objfile); |
544 | break; | |
545 | case FT_UNSIGNED_LONG_LONG: | |
546 | type = init_type (TYPE_CODE_INT, | |
9a76efb6 UW |
547 | gdbarch_long_long_bit (current_gdbarch) |
548 | / TARGET_CHAR_BIT, | |
b81654f1 MS |
549 | TYPE_FLAG_UNSIGNED, "unsigned long long", objfile); |
550 | break; | |
551 | case FT_FLOAT: | |
552 | type = init_type (TYPE_CODE_FLT, | |
ea06eb3d | 553 | gdbarch_float_bit (current_gdbarch) / TARGET_CHAR_BIT, |
b81654f1 MS |
554 | 0, "float", objfile); |
555 | break; | |
556 | case FT_DBL_PREC_FLOAT: | |
557 | type = init_type (TYPE_CODE_FLT, | |
ea06eb3d UW |
558 | gdbarch_double_bit (current_gdbarch) |
559 | / TARGET_CHAR_BIT, | |
b81654f1 MS |
560 | 0, "double", objfile); |
561 | break; | |
562 | case FT_EXT_PREC_FLOAT: | |
563 | type = init_type (TYPE_CODE_FLT, | |
ea06eb3d UW |
564 | gdbarch_long_double_bit (current_gdbarch) |
565 | / TARGET_CHAR_BIT, | |
b81654f1 MS |
566 | 0, "long double", objfile); |
567 | break; | |
568 | } | |
569 | return (type); | |
570 | } | |
571 | ||
f636b87d AF |
572 | /* Determine if we are currently in the Objective-C dispatch function. |
573 | If so, get the address of the method function that the dispatcher | |
574 | would call and use that as the function to step into instead. Also | |
575 | skip over the trampoline for the function (if any). This is better | |
576 | for the user since they are only interested in stepping into the | |
577 | method function anyway. */ | |
578 | static CORE_ADDR | |
52f729a7 | 579 | objc_skip_trampoline (struct frame_info *frame, CORE_ADDR stop_pc) |
f636b87d AF |
580 | { |
581 | CORE_ADDR real_stop_pc; | |
582 | CORE_ADDR method_stop_pc; | |
583 | ||
52f729a7 UW |
584 | real_stop_pc = gdbarch_skip_trampoline_code |
585 | (current_gdbarch, frame, stop_pc); | |
f636b87d AF |
586 | |
587 | if (real_stop_pc != 0) | |
588 | find_objc_msgcall (real_stop_pc, &method_stop_pc); | |
589 | else | |
590 | find_objc_msgcall (stop_pc, &method_stop_pc); | |
591 | ||
592 | if (method_stop_pc) | |
593 | { | |
e76f05fa | 594 | real_stop_pc = gdbarch_skip_trampoline_code |
52f729a7 | 595 | (current_gdbarch, frame, method_stop_pc); |
f636b87d AF |
596 | if (real_stop_pc == 0) |
597 | real_stop_pc = method_stop_pc; | |
598 | } | |
599 | ||
600 | return real_stop_pc; | |
601 | } | |
602 | ||
b81654f1 MS |
603 | |
604 | /* Table mapping opcodes into strings for printing operators | |
605 | and precedences of the operators. */ | |
606 | ||
607 | static const struct op_print objc_op_print_tab[] = | |
608 | { | |
609 | {",", BINOP_COMMA, PREC_COMMA, 0}, | |
610 | {"=", BINOP_ASSIGN, PREC_ASSIGN, 1}, | |
611 | {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0}, | |
612 | {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0}, | |
613 | {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0}, | |
614 | {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0}, | |
615 | {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0}, | |
616 | {"==", BINOP_EQUAL, PREC_EQUAL, 0}, | |
617 | {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0}, | |
618 | {"<=", BINOP_LEQ, PREC_ORDER, 0}, | |
619 | {">=", BINOP_GEQ, PREC_ORDER, 0}, | |
620 | {">", BINOP_GTR, PREC_ORDER, 0}, | |
621 | {"<", BINOP_LESS, PREC_ORDER, 0}, | |
622 | {">>", BINOP_RSH, PREC_SHIFT, 0}, | |
623 | {"<<", BINOP_LSH, PREC_SHIFT, 0}, | |
624 | {"+", BINOP_ADD, PREC_ADD, 0}, | |
625 | {"-", BINOP_SUB, PREC_ADD, 0}, | |
626 | {"*", BINOP_MUL, PREC_MUL, 0}, | |
627 | {"/", BINOP_DIV, PREC_MUL, 0}, | |
628 | {"%", BINOP_REM, PREC_MUL, 0}, | |
629 | {"@", BINOP_REPEAT, PREC_REPEAT, 0}, | |
630 | {"-", UNOP_NEG, PREC_PREFIX, 0}, | |
631 | {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0}, | |
632 | {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0}, | |
633 | {"*", UNOP_IND, PREC_PREFIX, 0}, | |
634 | {"&", UNOP_ADDR, PREC_PREFIX, 0}, | |
635 | {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0}, | |
636 | {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0}, | |
637 | {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0}, | |
e8f3fcdd | 638 | {NULL, OP_NULL, PREC_NULL, 0} |
b81654f1 MS |
639 | }; |
640 | ||
b81654f1 | 641 | const struct language_defn objc_language_defn = { |
d2e6263c | 642 | "objective-c", /* Language name */ |
b81654f1 | 643 | language_objc, |
cad351d1 | 644 | NULL, |
b81654f1 MS |
645 | range_check_off, |
646 | type_check_off, | |
647 | case_sensitive_on, | |
7ca2d3a3 | 648 | array_row_major, |
5f9769d1 | 649 | &exp_descriptor_standard, |
b81654f1 MS |
650 | objc_parse, |
651 | objc_error, | |
e85c3284 | 652 | null_post_parser, |
b81654f1 MS |
653 | objc_printchar, /* Print a character constant */ |
654 | objc_printstr, /* Function to print string constant */ | |
655 | objc_emit_char, | |
656 | objc_create_fundamental_type, /* Create fundamental type in this language */ | |
657 | c_print_type, /* Print a type using appropriate syntax */ | |
658 | c_val_print, /* Print a value using appropriate syntax */ | |
659 | c_value_print, /* Print a top-level value */ | |
f636b87d | 660 | objc_skip_trampoline, /* Language specific skip_trampoline */ |
5f9a71c3 DC |
661 | value_of_this, /* value_of_this */ |
662 | basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */ | |
b368761e | 663 | basic_lookup_transparent_type,/* lookup_transparent_type */ |
9a3d7dfd | 664 | objc_demangle, /* Language specific symbol demangler */ |
31c27f77 | 665 | NULL, /* Language specific class_name_from_physname */ |
d2e6263c MS |
666 | objc_op_print_tab, /* Expression operators for printing */ |
667 | 1, /* C-style arrays */ | |
b81654f1 | 668 | 0, /* String lower bound */ |
cad351d1 | 669 | NULL, |
6084f43a | 670 | default_word_break_characters, |
cad351d1 | 671 | c_language_arch_info, |
e79af960 | 672 | default_print_array_index, |
b81654f1 MS |
673 | LANG_MAGIC |
674 | }; | |
675 | ||
676 | /* | |
677 | * ObjC: | |
d2e6263c | 678 | * Following functions help construct Objective-C message calls |
b81654f1 MS |
679 | */ |
680 | ||
d2e6263c | 681 | struct selname /* For parsing Objective-C. */ |
b81654f1 MS |
682 | { |
683 | struct selname *next; | |
684 | char *msglist_sel; | |
685 | int msglist_len; | |
686 | }; | |
687 | ||
688 | static int msglist_len; | |
689 | static struct selname *selname_chain; | |
690 | static char *msglist_sel; | |
691 | ||
692 | void | |
693 | start_msglist(void) | |
694 | { | |
f86f5ca3 | 695 | struct selname *new = |
b81654f1 MS |
696 | (struct selname *) xmalloc (sizeof (struct selname)); |
697 | ||
698 | new->next = selname_chain; | |
699 | new->msglist_len = msglist_len; | |
700 | new->msglist_sel = msglist_sel; | |
701 | msglist_len = 0; | |
702 | msglist_sel = (char *)xmalloc(1); | |
703 | *msglist_sel = 0; | |
704 | selname_chain = new; | |
705 | } | |
706 | ||
707 | void | |
708 | add_msglist(struct stoken *str, int addcolon) | |
709 | { | |
710 | char *s, *p; | |
711 | int len, plen; | |
712 | ||
d2e6263c MS |
713 | if (str == 0) { /* Unnamed arg, or... */ |
714 | if (addcolon == 0) { /* variable number of args. */ | |
b81654f1 MS |
715 | msglist_len++; |
716 | return; | |
717 | } | |
718 | p = ""; | |
719 | plen = 0; | |
720 | } else { | |
721 | p = str->ptr; | |
722 | plen = str->length; | |
723 | } | |
724 | len = plen + strlen(msglist_sel) + 2; | |
725 | s = (char *)xmalloc(len); | |
726 | strcpy(s, msglist_sel); | |
727 | strncat(s, p, plen); | |
7248f48e | 728 | xfree(msglist_sel); |
b81654f1 MS |
729 | msglist_sel = s; |
730 | if (addcolon) { | |
731 | s[len-2] = ':'; | |
732 | s[len-1] = 0; | |
733 | msglist_len++; | |
734 | } else | |
735 | s[len-2] = '\0'; | |
736 | } | |
737 | ||
738 | int | |
739 | end_msglist(void) | |
740 | { | |
f86f5ca3 PH |
741 | int val = msglist_len; |
742 | struct selname *sel = selname_chain; | |
743 | char *p = msglist_sel; | |
c253954e | 744 | CORE_ADDR selid; |
b81654f1 MS |
745 | |
746 | selname_chain = sel->next; | |
747 | msglist_len = sel->msglist_len; | |
748 | msglist_sel = sel->msglist_sel; | |
749 | selid = lookup_child_selector(p); | |
750 | if (!selid) | |
8a3fe4f8 | 751 | error (_("Can't find selector \"%s\""), p); |
b81654f1 | 752 | write_exp_elt_longcst (selid); |
7248f48e | 753 | xfree(p); |
d2e6263c | 754 | write_exp_elt_longcst (val); /* Number of args */ |
7248f48e | 755 | xfree(sel); |
b81654f1 MS |
756 | |
757 | return val; | |
758 | } | |
759 | ||
760 | /* | |
761 | * Function: specialcmp (char *a, char *b) | |
762 | * | |
763 | * Special strcmp: treats ']' and ' ' as end-of-string. | |
d2e6263c | 764 | * Used for qsorting lists of objc methods (either by class or selector). |
b81654f1 MS |
765 | */ |
766 | ||
b9362cc7 AC |
767 | static int |
768 | specialcmp (char *a, char *b) | |
b81654f1 MS |
769 | { |
770 | while (*a && *a != ' ' && *a != ']' && *b && *b != ' ' && *b != ']') | |
771 | { | |
772 | if (*a != *b) | |
773 | return *a - *b; | |
774 | a++, b++; | |
775 | } | |
776 | if (*a && *a != ' ' && *a != ']') | |
777 | return 1; /* a is longer therefore greater */ | |
778 | if (*b && *b != ' ' && *b != ']') | |
779 | return -1; /* a is shorter therefore lesser */ | |
780 | return 0; /* a and b are identical */ | |
781 | } | |
782 | ||
783 | /* | |
36e53c63 | 784 | * Function: compare_selectors (const void *, const void *) |
b81654f1 | 785 | * |
d2e6263c MS |
786 | * Comparison function for use with qsort. Arguments are symbols or |
787 | * msymbols Compares selector part of objc method name alphabetically. | |
b81654f1 MS |
788 | */ |
789 | ||
790 | static int | |
36e53c63 | 791 | compare_selectors (const void *a, const void *b) |
b81654f1 MS |
792 | { |
793 | char *aname, *bname; | |
794 | ||
de5ad195 DC |
795 | aname = SYMBOL_PRINT_NAME (*(struct symbol **) a); |
796 | bname = SYMBOL_PRINT_NAME (*(struct symbol **) b); | |
7248f48e | 797 | if (aname == NULL || bname == NULL) |
8a3fe4f8 | 798 | error (_("internal: compare_selectors(1)")); |
b81654f1 | 799 | |
7248f48e AF |
800 | aname = strchr(aname, ' '); |
801 | bname = strchr(bname, ' '); | |
802 | if (aname == NULL || bname == NULL) | |
8a3fe4f8 | 803 | error (_("internal: compare_selectors(2)")); |
b81654f1 MS |
804 | |
805 | return specialcmp (aname+1, bname+1); | |
806 | } | |
807 | ||
808 | /* | |
809 | * Function: selectors_info (regexp, from_tty) | |
810 | * | |
d2e6263c MS |
811 | * Implements the "Info selectors" command. Takes an optional regexp |
812 | * arg. Lists all objective c selectors that match the regexp. Works | |
813 | * by grepping thru all symbols for objective c methods. Output list | |
814 | * is sorted and uniqued. | |
b81654f1 MS |
815 | */ |
816 | ||
817 | static void | |
818 | selectors_info (char *regexp, int from_tty) | |
819 | { | |
820 | struct objfile *objfile; | |
821 | struct minimal_symbol *msymbol; | |
822 | char *name; | |
823 | char *val; | |
824 | int matches = 0; | |
825 | int maxlen = 0; | |
826 | int ix; | |
827 | char myregexp[2048]; | |
828 | char asel[256]; | |
829 | struct symbol **sym_arr; | |
830 | int plusminus = 0; | |
831 | ||
832 | if (regexp == NULL) | |
d2e6263c | 833 | strcpy(myregexp, ".*]"); /* Null input, match all objc methods. */ |
b81654f1 MS |
834 | else |
835 | { | |
d2e6263c MS |
836 | if (*regexp == '+' || *regexp == '-') |
837 | { /* User wants only class methods or only instance methods. */ | |
b81654f1 MS |
838 | plusminus = *regexp++; |
839 | while (*regexp == ' ' || *regexp == '\t') | |
840 | regexp++; | |
841 | } | |
842 | if (*regexp == '\0') | |
843 | strcpy(myregexp, ".*]"); | |
844 | else | |
845 | { | |
846 | strcpy(myregexp, regexp); | |
847 | if (myregexp[strlen(myregexp) - 1] == '$') /* end of selector */ | |
848 | myregexp[strlen(myregexp) - 1] = ']'; /* end of method name */ | |
849 | else | |
850 | strcat(myregexp, ".*]"); | |
851 | } | |
852 | } | |
853 | ||
854 | if (regexp != NULL) | |
5e488a7b AC |
855 | { |
856 | val = re_comp (myregexp); | |
857 | if (val != 0) | |
8a3fe4f8 | 858 | error (_("Invalid regexp (%s): %s"), val, regexp); |
5e488a7b | 859 | } |
b81654f1 | 860 | |
d2e6263c | 861 | /* First time thru is JUST to get max length and count. */ |
b81654f1 MS |
862 | ALL_MSYMBOLS (objfile, msymbol) |
863 | { | |
864 | QUIT; | |
36018d2e | 865 | name = SYMBOL_NATURAL_NAME (msymbol); |
b81654f1 MS |
866 | if (name && |
867 | (name[0] == '-' || name[0] == '+') && | |
d2e6263c | 868 | name[1] == '[') /* Got a method name. */ |
b81654f1 | 869 | { |
d2e6263c | 870 | /* Filter for class/instance methods. */ |
b81654f1 | 871 | if (plusminus && name[0] != plusminus) |
d2e6263c MS |
872 | continue; |
873 | /* Find selector part. */ | |
874 | name = (char *) strchr(name+2, ' '); | |
b81654f1 MS |
875 | if (regexp == NULL || re_exec(++name) != 0) |
876 | { | |
877 | char *mystart = name; | |
878 | char *myend = (char *) strchr(mystart, ']'); | |
879 | ||
880 | if (myend && (myend - mystart > maxlen)) | |
d2e6263c | 881 | maxlen = myend - mystart; /* Get longest selector. */ |
b81654f1 MS |
882 | matches++; |
883 | } | |
884 | } | |
885 | } | |
886 | if (matches) | |
887 | { | |
a3f17187 | 888 | printf_filtered (_("Selectors matching \"%s\":\n\n"), |
b81654f1 MS |
889 | regexp ? regexp : "*"); |
890 | ||
891 | sym_arr = alloca (matches * sizeof (struct symbol *)); | |
892 | matches = 0; | |
893 | ALL_MSYMBOLS (objfile, msymbol) | |
894 | { | |
895 | QUIT; | |
36018d2e | 896 | name = SYMBOL_NATURAL_NAME (msymbol); |
b81654f1 MS |
897 | if (name && |
898 | (name[0] == '-' || name[0] == '+') && | |
d2e6263c | 899 | name[1] == '[') /* Got a method name. */ |
b81654f1 | 900 | { |
d2e6263c | 901 | /* Filter for class/instance methods. */ |
b81654f1 | 902 | if (plusminus && name[0] != plusminus) |
d2e6263c MS |
903 | continue; |
904 | /* Find selector part. */ | |
905 | name = (char *) strchr(name+2, ' '); | |
b81654f1 MS |
906 | if (regexp == NULL || re_exec(++name) != 0) |
907 | sym_arr[matches++] = (struct symbol *) msymbol; | |
908 | } | |
909 | } | |
910 | ||
911 | qsort (sym_arr, matches, sizeof (struct minimal_symbol *), | |
912 | compare_selectors); | |
d2e6263c MS |
913 | /* Prevent compare on first iteration. */ |
914 | asel[0] = 0; | |
915 | for (ix = 0; ix < matches; ix++) /* Now do the output. */ | |
b81654f1 MS |
916 | { |
917 | char *p = asel; | |
918 | ||
919 | QUIT; | |
36018d2e | 920 | name = SYMBOL_NATURAL_NAME (sym_arr[ix]); |
b81654f1 MS |
921 | name = strchr (name, ' ') + 1; |
922 | if (p[0] && specialcmp(name, p) == 0) | |
d2e6263c | 923 | continue; /* Seen this one already (not unique). */ |
b81654f1 | 924 | |
d2e6263c MS |
925 | /* Copy selector part. */ |
926 | while (*name && *name != ']') | |
b81654f1 MS |
927 | *p++ = *name++; |
928 | *p++ = '\0'; | |
d2e6263c MS |
929 | /* Print in columns. */ |
930 | puts_filtered_tabular(asel, maxlen + 1, 0); | |
b81654f1 MS |
931 | } |
932 | begin_line(); | |
933 | } | |
934 | else | |
a3f17187 | 935 | printf_filtered (_("No selectors matching \"%s\"\n"), regexp ? regexp : "*"); |
b81654f1 MS |
936 | } |
937 | ||
938 | /* | |
36e53c63 | 939 | * Function: compare_classes (const void *, const void *) |
b81654f1 | 940 | * |
d2e6263c MS |
941 | * Comparison function for use with qsort. Arguments are symbols or |
942 | * msymbols Compares class part of objc method name alphabetically. | |
b81654f1 MS |
943 | */ |
944 | ||
945 | static int | |
36e53c63 | 946 | compare_classes (const void *a, const void *b) |
b81654f1 MS |
947 | { |
948 | char *aname, *bname; | |
949 | ||
de5ad195 DC |
950 | aname = SYMBOL_PRINT_NAME (*(struct symbol **) a); |
951 | bname = SYMBOL_PRINT_NAME (*(struct symbol **) b); | |
7248f48e | 952 | if (aname == NULL || bname == NULL) |
8a3fe4f8 | 953 | error (_("internal: compare_classes(1)")); |
b81654f1 MS |
954 | |
955 | return specialcmp (aname+1, bname+1); | |
956 | } | |
957 | ||
958 | /* | |
959 | * Function: classes_info(regexp, from_tty) | |
960 | * | |
961 | * Implements the "info classes" command for objective c classes. | |
962 | * Lists all objective c classes that match the optional regexp. | |
d2e6263c MS |
963 | * Works by grepping thru the list of objective c methods. List will |
964 | * be sorted and uniqued (since one class may have many methods). | |
965 | * BUGS: will not list a class that has no methods. | |
b81654f1 MS |
966 | */ |
967 | ||
968 | static void | |
969 | classes_info (char *regexp, int from_tty) | |
970 | { | |
971 | struct objfile *objfile; | |
972 | struct minimal_symbol *msymbol; | |
973 | char *name; | |
974 | char *val; | |
975 | int matches = 0; | |
976 | int maxlen = 0; | |
977 | int ix; | |
978 | char myregexp[2048]; | |
979 | char aclass[256]; | |
980 | struct symbol **sym_arr; | |
981 | ||
982 | if (regexp == NULL) | |
d2e6263c | 983 | strcpy(myregexp, ".* "); /* Null input: match all objc classes. */ |
b81654f1 MS |
984 | else |
985 | { | |
986 | strcpy(myregexp, regexp); | |
987 | if (myregexp[strlen(myregexp) - 1] == '$') | |
d2e6263c | 988 | /* In the method name, the end of the class name is marked by ' '. */ |
b81654f1 MS |
989 | myregexp[strlen(myregexp) - 1] = ' '; |
990 | else | |
991 | strcat(myregexp, ".* "); | |
992 | } | |
993 | ||
994 | if (regexp != NULL) | |
5e488a7b AC |
995 | { |
996 | val = re_comp (myregexp); | |
997 | if (val != 0) | |
8a3fe4f8 | 998 | error (_("Invalid regexp (%s): %s"), val, regexp); |
5e488a7b | 999 | } |
b81654f1 | 1000 | |
d2e6263c | 1001 | /* First time thru is JUST to get max length and count. */ |
b81654f1 MS |
1002 | ALL_MSYMBOLS (objfile, msymbol) |
1003 | { | |
1004 | QUIT; | |
36018d2e | 1005 | name = SYMBOL_NATURAL_NAME (msymbol); |
b81654f1 MS |
1006 | if (name && |
1007 | (name[0] == '-' || name[0] == '+') && | |
d2e6263c | 1008 | name[1] == '[') /* Got a method name. */ |
b81654f1 MS |
1009 | if (regexp == NULL || re_exec(name+2) != 0) |
1010 | { | |
d2e6263c MS |
1011 | /* Compute length of classname part. */ |
1012 | char *mystart = name + 2; | |
b81654f1 MS |
1013 | char *myend = (char *) strchr(mystart, ' '); |
1014 | ||
1015 | if (myend && (myend - mystart > maxlen)) | |
1016 | maxlen = myend - mystart; | |
1017 | matches++; | |
1018 | } | |
1019 | } | |
1020 | if (matches) | |
1021 | { | |
a3f17187 | 1022 | printf_filtered (_("Classes matching \"%s\":\n\n"), |
b81654f1 MS |
1023 | regexp ? regexp : "*"); |
1024 | sym_arr = alloca (matches * sizeof (struct symbol *)); | |
1025 | matches = 0; | |
1026 | ALL_MSYMBOLS (objfile, msymbol) | |
1027 | { | |
1028 | QUIT; | |
36018d2e | 1029 | name = SYMBOL_NATURAL_NAME (msymbol); |
b81654f1 MS |
1030 | if (name && |
1031 | (name[0] == '-' || name[0] == '+') && | |
d2e6263c | 1032 | name[1] == '[') /* Got a method name. */ |
b81654f1 MS |
1033 | if (regexp == NULL || re_exec(name+2) != 0) |
1034 | sym_arr[matches++] = (struct symbol *) msymbol; | |
1035 | } | |
1036 | ||
1037 | qsort (sym_arr, matches, sizeof (struct minimal_symbol *), | |
1038 | compare_classes); | |
d2e6263c MS |
1039 | /* Prevent compare on first iteration. */ |
1040 | aclass[0] = 0; | |
1041 | for (ix = 0; ix < matches; ix++) /* Now do the output. */ | |
b81654f1 MS |
1042 | { |
1043 | char *p = aclass; | |
1044 | ||
1045 | QUIT; | |
36018d2e | 1046 | name = SYMBOL_NATURAL_NAME (sym_arr[ix]); |
b81654f1 MS |
1047 | name += 2; |
1048 | if (p[0] && specialcmp(name, p) == 0) | |
d2e6263c | 1049 | continue; /* Seen this one already (not unique). */ |
b81654f1 | 1050 | |
d2e6263c MS |
1051 | /* Copy class part of method name. */ |
1052 | while (*name && *name != ' ') | |
b81654f1 MS |
1053 | *p++ = *name++; |
1054 | *p++ = '\0'; | |
d2e6263c MS |
1055 | /* Print in columns. */ |
1056 | puts_filtered_tabular(aclass, maxlen + 1, 0); | |
b81654f1 MS |
1057 | } |
1058 | begin_line(); | |
1059 | } | |
1060 | else | |
a3f17187 | 1061 | printf_filtered (_("No classes matching \"%s\"\n"), regexp ? regexp : "*"); |
b81654f1 MS |
1062 | } |
1063 | ||
1064 | /* | |
1065 | * Function: find_imps (char *selector, struct symbol **sym_arr) | |
1066 | * | |
1067 | * Input: a string representing a selector | |
1068 | * a pointer to an array of symbol pointers | |
1069 | * possibly a pointer to a symbol found by the caller. | |
1070 | * | |
d2e6263c MS |
1071 | * Output: number of methods that implement that selector. Side |
1072 | * effects: The array of symbol pointers is filled with matching syms. | |
b81654f1 | 1073 | * |
d2e6263c MS |
1074 | * By analogy with function "find_methods" (symtab.c), builds a list |
1075 | * of symbols matching the ambiguous input, so that "decode_line_2" | |
1076 | * (symtab.c) can list them and ask the user to choose one or more. | |
1077 | * In this case the matches are objective c methods | |
1078 | * ("implementations") matching an objective c selector. | |
b81654f1 | 1079 | * |
d2e6263c MS |
1080 | * Note that it is possible for a normal (c-style) function to have |
1081 | * the same name as an objective c selector. To prevent the selector | |
1082 | * from eclipsing the function, we allow the caller (decode_line_1) to | |
1083 | * search for such a function first, and if it finds one, pass it in | |
1084 | * to us. We will then integrate it into the list. We also search | |
1085 | * for one here, among the minsyms. | |
b81654f1 | 1086 | * |
d2e6263c MS |
1087 | * NOTE: if NUM_DEBUGGABLE is non-zero, the sym_arr will be divided |
1088 | * into two parts: debuggable (struct symbol) syms, and | |
1089 | * non_debuggable (struct minimal_symbol) syms. The debuggable | |
1090 | * ones will come first, before NUM_DEBUGGABLE (which will thus | |
1091 | * be the index of the first non-debuggable one). | |
b81654f1 MS |
1092 | */ |
1093 | ||
1094 | /* | |
1095 | * Function: total_number_of_imps (char *selector); | |
1096 | * | |
1097 | * Input: a string representing a selector | |
1098 | * Output: number of methods that implement that selector. | |
1099 | * | |
d2e6263c | 1100 | * By analogy with function "total_number_of_methods", this allows |
b81654f1 | 1101 | * decode_line_1 (symtab.c) to detect if there are objective c methods |
d2e6263c MS |
1102 | * matching the input, and to allocate an array of pointers to them |
1103 | * which can be manipulated by "decode_line_2" (also in symtab.c). | |
b81654f1 MS |
1104 | */ |
1105 | ||
1106 | char * | |
1107 | parse_selector (char *method, char **selector) | |
1108 | { | |
1109 | char *s1 = NULL; | |
1110 | char *s2 = NULL; | |
1111 | int found_quote = 0; | |
1112 | ||
1113 | char *nselector = NULL; | |
1114 | ||
e8f3fcdd | 1115 | gdb_assert (selector != NULL); |
b81654f1 MS |
1116 | |
1117 | s1 = method; | |
1118 | ||
1119 | while (isspace (*s1)) | |
1120 | s1++; | |
1121 | if (*s1 == '\'') | |
1122 | { | |
1123 | found_quote = 1; | |
1124 | s1++; | |
1125 | } | |
1126 | while (isspace (*s1)) | |
1127 | s1++; | |
1128 | ||
1129 | nselector = s1; | |
1130 | s2 = s1; | |
1131 | ||
1132 | for (;;) { | |
1133 | if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':')) | |
1134 | *s1++ = *s2; | |
1135 | else if (isspace (*s2)) | |
1136 | ; | |
1137 | else if ((*s2 == '\0') || (*s2 == '\'')) | |
1138 | break; | |
1139 | else | |
1140 | return NULL; | |
1141 | s2++; | |
1142 | } | |
1143 | *s1++ = '\0'; | |
1144 | ||
1145 | while (isspace (*s2)) | |
1146 | s2++; | |
1147 | if (found_quote) | |
1148 | { | |
1149 | if (*s2 == '\'') | |
1150 | s2++; | |
1151 | while (isspace (*s2)) | |
1152 | s2++; | |
1153 | } | |
1154 | ||
1155 | if (selector != NULL) | |
1156 | *selector = nselector; | |
1157 | ||
1158 | return s2; | |
1159 | } | |
1160 | ||
1161 | char * | |
d2e6263c MS |
1162 | parse_method (char *method, char *type, char **class, |
1163 | char **category, char **selector) | |
b81654f1 MS |
1164 | { |
1165 | char *s1 = NULL; | |
1166 | char *s2 = NULL; | |
1167 | int found_quote = 0; | |
1168 | ||
1169 | char ntype = '\0'; | |
1170 | char *nclass = NULL; | |
1171 | char *ncategory = NULL; | |
1172 | char *nselector = NULL; | |
1173 | ||
e8f3fcdd AC |
1174 | gdb_assert (type != NULL); |
1175 | gdb_assert (class != NULL); | |
1176 | gdb_assert (category != NULL); | |
1177 | gdb_assert (selector != NULL); | |
b81654f1 MS |
1178 | |
1179 | s1 = method; | |
1180 | ||
1181 | while (isspace (*s1)) | |
1182 | s1++; | |
1183 | if (*s1 == '\'') | |
1184 | { | |
1185 | found_quote = 1; | |
1186 | s1++; | |
1187 | } | |
1188 | while (isspace (*s1)) | |
1189 | s1++; | |
1190 | ||
1191 | if ((s1[0] == '+') || (s1[0] == '-')) | |
1192 | ntype = *s1++; | |
1193 | ||
1194 | while (isspace (*s1)) | |
1195 | s1++; | |
1196 | ||
1197 | if (*s1 != '[') | |
1198 | return NULL; | |
1199 | s1++; | |
1200 | ||
1201 | nclass = s1; | |
1202 | while (isalnum (*s1) || (*s1 == '_')) | |
1203 | s1++; | |
1204 | ||
1205 | s2 = s1; | |
1206 | while (isspace (*s2)) | |
1207 | s2++; | |
1208 | ||
1209 | if (*s2 == '(') | |
1210 | { | |
1211 | s2++; | |
1212 | while (isspace (*s2)) | |
1213 | s2++; | |
1214 | ncategory = s2; | |
1215 | while (isalnum (*s2) || (*s2 == '_')) | |
1216 | s2++; | |
1217 | *s2++ = '\0'; | |
1218 | } | |
1219 | ||
d2e6263c | 1220 | /* Truncate the class name now that we're not using the open paren. */ |
b81654f1 MS |
1221 | *s1++ = '\0'; |
1222 | ||
1223 | nselector = s2; | |
1224 | s1 = s2; | |
1225 | ||
1226 | for (;;) { | |
1227 | if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':')) | |
1228 | *s1++ = *s2; | |
1229 | else if (isspace (*s2)) | |
1230 | ; | |
1231 | else if (*s2 == ']') | |
1232 | break; | |
1233 | else | |
1234 | return NULL; | |
1235 | s2++; | |
1236 | } | |
1237 | *s1++ = '\0'; | |
1238 | s2++; | |
1239 | ||
1240 | while (isspace (*s2)) | |
1241 | s2++; | |
1242 | if (found_quote) | |
1243 | { | |
1244 | if (*s2 != '\'') | |
1245 | return NULL; | |
1246 | s2++; | |
1247 | while (isspace (*s2)) | |
1248 | s2++; | |
1249 | } | |
1250 | ||
1251 | if (type != NULL) | |
1252 | *type = ntype; | |
1253 | if (class != NULL) | |
1254 | *class = nclass; | |
1255 | if (category != NULL) | |
1256 | *category = ncategory; | |
1257 | if (selector != NULL) | |
1258 | *selector = nselector; | |
1259 | ||
1260 | return s2; | |
1261 | } | |
1262 | ||
2f9a90b4 | 1263 | static void |
d2e6263c MS |
1264 | find_methods (struct symtab *symtab, char type, |
1265 | const char *class, const char *category, | |
1266 | const char *selector, struct symbol **syms, | |
1267 | unsigned int *nsym, unsigned int *ndebug) | |
b81654f1 MS |
1268 | { |
1269 | struct objfile *objfile = NULL; | |
1270 | struct minimal_symbol *msymbol = NULL; | |
1271 | struct block *block = NULL; | |
1272 | struct symbol *sym = NULL; | |
1273 | ||
1274 | char *symname = NULL; | |
1275 | ||
1276 | char ntype = '\0'; | |
1277 | char *nclass = NULL; | |
1278 | char *ncategory = NULL; | |
1279 | char *nselector = NULL; | |
1280 | ||
1281 | unsigned int csym = 0; | |
1282 | unsigned int cdebug = 0; | |
1283 | ||
1284 | static char *tmp = NULL; | |
1285 | static unsigned int tmplen = 0; | |
1286 | ||
e8f3fcdd AC |
1287 | gdb_assert (nsym != NULL); |
1288 | gdb_assert (ndebug != NULL); | |
b81654f1 MS |
1289 | |
1290 | if (symtab) | |
1291 | block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK); | |
1292 | ||
1293 | ALL_MSYMBOLS (objfile, msymbol) | |
1294 | { | |
1295 | QUIT; | |
1296 | ||
1297 | if ((msymbol->type != mst_text) && (msymbol->type != mst_file_text)) | |
d2e6263c | 1298 | /* Not a function or method. */ |
b81654f1 MS |
1299 | continue; |
1300 | ||
1301 | if (symtab) | |
8da065d5 DC |
1302 | if ((SYMBOL_VALUE_ADDRESS (msymbol) < BLOCK_START (block)) || |
1303 | (SYMBOL_VALUE_ADDRESS (msymbol) >= BLOCK_END (block))) | |
d2e6263c | 1304 | /* Not in the specified symtab. */ |
b81654f1 MS |
1305 | continue; |
1306 | ||
36018d2e | 1307 | symname = SYMBOL_NATURAL_NAME (msymbol); |
b81654f1 MS |
1308 | if (symname == NULL) |
1309 | continue; | |
1310 | ||
1311 | if ((symname[0] != '-' && symname[0] != '+') || (symname[1] != '[')) | |
d2e6263c | 1312 | /* Not a method name. */ |
b81654f1 MS |
1313 | continue; |
1314 | ||
1315 | while ((strlen (symname) + 1) >= tmplen) | |
1316 | { | |
1317 | tmplen = (tmplen == 0) ? 1024 : tmplen * 2; | |
1318 | tmp = xrealloc (tmp, tmplen); | |
1319 | } | |
1320 | strcpy (tmp, symname); | |
1321 | ||
1322 | if (parse_method (tmp, &ntype, &nclass, &ncategory, &nselector) == NULL) | |
1323 | continue; | |
1324 | ||
1325 | if ((type != '\0') && (ntype != type)) | |
1326 | continue; | |
1327 | ||
d2e6263c MS |
1328 | if ((class != NULL) |
1329 | && ((nclass == NULL) || (strcmp (class, nclass) != 0))) | |
b81654f1 MS |
1330 | continue; |
1331 | ||
d2e6263c MS |
1332 | if ((category != NULL) && |
1333 | ((ncategory == NULL) || (strcmp (category, ncategory) != 0))) | |
b81654f1 MS |
1334 | continue; |
1335 | ||
d2e6263c MS |
1336 | if ((selector != NULL) && |
1337 | ((nselector == NULL) || (strcmp (selector, nselector) != 0))) | |
b81654f1 MS |
1338 | continue; |
1339 | ||
1340 | sym = find_pc_function (SYMBOL_VALUE_ADDRESS (msymbol)); | |
1341 | if (sym != NULL) | |
1342 | { | |
36018d2e | 1343 | const char *newsymname = SYMBOL_NATURAL_NAME (sym); |
b81654f1 | 1344 | |
b81654f1 MS |
1345 | if (strcmp (symname, newsymname) == 0) |
1346 | { | |
d2e6263c MS |
1347 | /* Found a high-level method sym: swap it into the |
1348 | lower part of sym_arr (below num_debuggable). */ | |
b81654f1 MS |
1349 | if (syms != NULL) |
1350 | { | |
1351 | syms[csym] = syms[cdebug]; | |
1352 | syms[cdebug] = sym; | |
1353 | } | |
1354 | csym++; | |
1355 | cdebug++; | |
1356 | } | |
1357 | else | |
1358 | { | |
d2e6263c MS |
1359 | warning ( |
1360 | "debugging symbol \"%s\" does not match minimal symbol (\"%s\"); ignoring", | |
b81654f1 MS |
1361 | newsymname, symname); |
1362 | if (syms != NULL) | |
1363 | syms[csym] = (struct symbol *) msymbol; | |
1364 | csym++; | |
1365 | } | |
1366 | } | |
1367 | else | |
1368 | { | |
d2e6263c | 1369 | /* Found a non-debuggable method symbol. */ |
b81654f1 MS |
1370 | if (syms != NULL) |
1371 | syms[csym] = (struct symbol *) msymbol; | |
1372 | csym++; | |
1373 | } | |
1374 | } | |
1375 | ||
1376 | if (nsym != NULL) | |
1377 | *nsym = csym; | |
1378 | if (ndebug != NULL) | |
1379 | *ndebug = cdebug; | |
1380 | } | |
1381 | ||
1382 | char *find_imps (struct symtab *symtab, struct block *block, | |
d2e6263c MS |
1383 | char *method, struct symbol **syms, |
1384 | unsigned int *nsym, unsigned int *ndebug) | |
b81654f1 MS |
1385 | { |
1386 | char type = '\0'; | |
1387 | char *class = NULL; | |
1388 | char *category = NULL; | |
1389 | char *selector = NULL; | |
1390 | ||
1391 | unsigned int csym = 0; | |
1392 | unsigned int cdebug = 0; | |
1393 | ||
1394 | unsigned int ncsym = 0; | |
1395 | unsigned int ncdebug = 0; | |
1396 | ||
1397 | char *buf = NULL; | |
1398 | char *tmp = NULL; | |
1399 | ||
e8f3fcdd AC |
1400 | gdb_assert (nsym != NULL); |
1401 | gdb_assert (ndebug != NULL); | |
b81654f1 MS |
1402 | |
1403 | if (nsym != NULL) | |
1404 | *nsym = 0; | |
1405 | if (ndebug != NULL) | |
1406 | *ndebug = 0; | |
1407 | ||
1408 | buf = (char *) alloca (strlen (method) + 1); | |
1409 | strcpy (buf, method); | |
1410 | tmp = parse_method (buf, &type, &class, &category, &selector); | |
1411 | ||
1412 | if (tmp == NULL) { | |
1413 | ||
b81654f1 MS |
1414 | struct symbol *sym = NULL; |
1415 | struct minimal_symbol *msym = NULL; | |
1416 | ||
1417 | strcpy (buf, method); | |
1418 | tmp = parse_selector (buf, &selector); | |
1419 | ||
1420 | if (tmp == NULL) | |
1421 | return NULL; | |
1422 | ||
cdef89d0 | 1423 | sym = lookup_symbol (selector, block, VAR_DOMAIN, 0, NULL); |
b81654f1 MS |
1424 | if (sym != NULL) |
1425 | { | |
1426 | if (syms) | |
1427 | syms[csym] = sym; | |
1428 | csym++; | |
1429 | cdebug++; | |
1430 | } | |
1431 | ||
1432 | if (sym == NULL) | |
1433 | msym = lookup_minimal_symbol (selector, 0, 0); | |
1434 | ||
1435 | if (msym != NULL) | |
1436 | { | |
1437 | if (syms) | |
36e53c63 | 1438 | syms[csym] = (struct symbol *)msym; |
b81654f1 MS |
1439 | csym++; |
1440 | } | |
1441 | } | |
1442 | ||
1443 | if (syms != NULL) | |
d2e6263c MS |
1444 | find_methods (symtab, type, class, category, selector, |
1445 | syms + csym, &ncsym, &ncdebug); | |
b81654f1 | 1446 | else |
d2e6263c MS |
1447 | find_methods (symtab, type, class, category, selector, |
1448 | NULL, &ncsym, &ncdebug); | |
b81654f1 | 1449 | |
d2e6263c | 1450 | /* If we didn't find any methods, just return. */ |
b81654f1 MS |
1451 | if (ncsym == 0 && ncdebug == 0) |
1452 | return method; | |
1453 | ||
1454 | /* Take debug symbols from the second batch of symbols and swap them | |
1455 | * with debug symbols from the first batch. Repeat until either the | |
1456 | * second section is out of debug symbols or the first section is | |
1457 | * full of debug symbols. Either way we have all debug symbols | |
d2e6263c MS |
1458 | * packed to the beginning of the buffer. |
1459 | */ | |
b81654f1 MS |
1460 | |
1461 | if (syms != NULL) | |
1462 | { | |
1463 | while ((cdebug < csym) && (ncdebug > 0)) | |
1464 | { | |
1465 | struct symbol *s = NULL; | |
d2e6263c MS |
1466 | /* First non-debugging symbol. */ |
1467 | unsigned int i = cdebug; | |
1468 | /* Last of second batch of debug symbols. */ | |
1469 | unsigned int j = csym + ncdebug - 1; | |
b81654f1 MS |
1470 | |
1471 | s = syms[j]; | |
1472 | syms[j] = syms[i]; | |
1473 | syms[i] = s; | |
1474 | ||
d2e6263c MS |
1475 | /* We've moved a symbol from the second debug section to the |
1476 | first one. */ | |
b81654f1 MS |
1477 | cdebug++; |
1478 | ncdebug--; | |
1479 | } | |
1480 | } | |
1481 | ||
1482 | csym += ncsym; | |
1483 | cdebug += ncdebug; | |
1484 | ||
1485 | if (nsym != NULL) | |
1486 | *nsym = csym; | |
1487 | if (ndebug != NULL) | |
1488 | *ndebug = cdebug; | |
1489 | ||
1490 | if (syms == NULL) | |
1491 | return method + (tmp - buf); | |
1492 | ||
1493 | if (csym > 1) | |
1494 | { | |
d2e6263c | 1495 | /* Sort debuggable symbols. */ |
b81654f1 | 1496 | if (cdebug > 1) |
d2e6263c MS |
1497 | qsort (syms, cdebug, sizeof (struct minimal_symbol *), |
1498 | compare_classes); | |
b81654f1 | 1499 | |
d2e6263c | 1500 | /* Sort minimal_symbols. */ |
b81654f1 | 1501 | if ((csym - cdebug) > 1) |
d2e6263c MS |
1502 | qsort (&syms[cdebug], csym - cdebug, |
1503 | sizeof (struct minimal_symbol *), compare_classes); | |
b81654f1 | 1504 | } |
d2e6263c MS |
1505 | /* Terminate the sym_arr list. */ |
1506 | syms[csym] = 0; | |
b81654f1 MS |
1507 | |
1508 | return method + (tmp - buf); | |
1509 | } | |
1510 | ||
b9362cc7 | 1511 | static void |
b81654f1 MS |
1512 | print_object_command (char *args, int from_tty) |
1513 | { | |
1514 | struct value *object, *function, *description; | |
36e53c63 | 1515 | CORE_ADDR string_addr, object_addr; |
b81654f1 | 1516 | int i = 0; |
22a44745 | 1517 | gdb_byte c = 0; |
b81654f1 MS |
1518 | |
1519 | if (!args || !*args) | |
d2e6263c MS |
1520 | error ( |
1521 | "The 'print-object' command requires an argument (an Objective-C object)"); | |
b81654f1 MS |
1522 | |
1523 | { | |
1524 | struct expression *expr = parse_expression (args); | |
f86f5ca3 | 1525 | struct cleanup *old_chain = |
d2e6263c | 1526 | make_cleanup (free_current_contents, &expr); |
b81654f1 MS |
1527 | int pc = 0; |
1528 | ||
5f9769d1 PH |
1529 | object = expr->language_defn->la_exp_desc->evaluate_exp |
1530 | (builtin_type_void_data_ptr, expr, &pc, EVAL_NORMAL); | |
b81654f1 MS |
1531 | do_cleanups (old_chain); |
1532 | } | |
1533 | ||
36e53c63 AF |
1534 | /* Validate the address for sanity. */ |
1535 | object_addr = value_as_long (object); | |
1536 | read_memory (object_addr, &c, 1); | |
1537 | ||
7248f48e | 1538 | function = find_function_in_inferior ("_NSPrintForDebugger"); |
36e53c63 | 1539 | if (function == NULL) |
8a3fe4f8 | 1540 | error (_("Unable to locate _NSPrintForDebugger in child process")); |
b81654f1 MS |
1541 | |
1542 | description = call_function_by_hand (function, 1, &object); | |
1543 | ||
7248f48e AF |
1544 | string_addr = value_as_long (description); |
1545 | if (string_addr == 0) | |
8a3fe4f8 | 1546 | error (_("object returns null description")); |
b81654f1 MS |
1547 | |
1548 | read_memory (string_addr + i++, &c, 1); | |
22a44745 | 1549 | if (c != 0) |
b81654f1 | 1550 | do |
d2e6263c | 1551 | { /* Read and print characters up to EOS. */ |
b81654f1 MS |
1552 | QUIT; |
1553 | printf_filtered ("%c", c); | |
1554 | read_memory (string_addr + i++, &c, 1); | |
1555 | } while (c != 0); | |
1556 | else | |
a3f17187 | 1557 | printf_filtered(_("<object returns empty description>")); |
b81654f1 MS |
1558 | printf_filtered ("\n"); |
1559 | } | |
1560 | ||
d2e6263c MS |
1561 | /* The data structure 'methcalls' is used to detect method calls (thru |
1562 | * ObjC runtime lib functions objc_msgSend, objc_msgSendSuper, etc.), | |
1563 | * and ultimately find the method being called. | |
b81654f1 MS |
1564 | */ |
1565 | ||
1566 | struct objc_methcall { | |
1567 | char *name; | |
d2e6263c | 1568 | /* Return instance method to be called. */ |
36e53c63 | 1569 | int (*stop_at) (CORE_ADDR, CORE_ADDR *); |
d2e6263c MS |
1570 | /* Start of pc range corresponding to method invocation. */ |
1571 | CORE_ADDR begin; | |
1572 | /* End of pc range corresponding to method invocation. */ | |
1573 | CORE_ADDR end; | |
b81654f1 MS |
1574 | }; |
1575 | ||
d2e6263c MS |
1576 | static int resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc); |
1577 | static int resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc); | |
1578 | static int resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc); | |
1579 | static int resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc); | |
b81654f1 MS |
1580 | |
1581 | static struct objc_methcall methcalls[] = { | |
1582 | { "_objc_msgSend", resolve_msgsend, 0, 0}, | |
1583 | { "_objc_msgSend_stret", resolve_msgsend_stret, 0, 0}, | |
1584 | { "_objc_msgSendSuper", resolve_msgsend_super, 0, 0}, | |
1585 | { "_objc_msgSendSuper_stret", resolve_msgsend_super_stret, 0, 0}, | |
1586 | { "_objc_getClass", NULL, 0, 0}, | |
1587 | { "_objc_getMetaClass", NULL, 0, 0} | |
1588 | }; | |
1589 | ||
1590 | #define nmethcalls (sizeof (methcalls) / sizeof (methcalls[0])) | |
1591 | ||
d2e6263c MS |
1592 | /* The following function, "find_objc_msgsend", fills in the data |
1593 | * structure "objc_msgs" by finding the addresses of each of the | |
1594 | * (currently four) functions that it holds (of which objc_msgSend is | |
1595 | * the first). This must be called each time symbols are loaded, in | |
1596 | * case the functions have moved for some reason. | |
b81654f1 MS |
1597 | */ |
1598 | ||
b9362cc7 | 1599 | static void |
b81654f1 MS |
1600 | find_objc_msgsend (void) |
1601 | { | |
1602 | unsigned int i; | |
1603 | for (i = 0; i < nmethcalls; i++) { | |
1604 | ||
1605 | struct minimal_symbol *func; | |
1606 | ||
d2e6263c | 1607 | /* Try both with and without underscore. */ |
b81654f1 MS |
1608 | func = lookup_minimal_symbol (methcalls[i].name, NULL, NULL); |
1609 | if ((func == NULL) && (methcalls[i].name[0] == '_')) { | |
1610 | func = lookup_minimal_symbol (methcalls[i].name + 1, NULL, NULL); | |
1611 | } | |
1612 | if (func == NULL) { | |
1613 | methcalls[i].begin = 0; | |
1614 | methcalls[i].end = 0; | |
1615 | continue; | |
1616 | } | |
1617 | ||
1618 | methcalls[i].begin = SYMBOL_VALUE_ADDRESS (func); | |
1619 | do { | |
1620 | methcalls[i].end = SYMBOL_VALUE_ADDRESS (++func); | |
1621 | } while (methcalls[i].begin == methcalls[i].end); | |
1622 | } | |
1623 | } | |
1624 | ||
1625 | /* find_objc_msgcall (replaces pc_off_limits) | |
1626 | * | |
d2e6263c MS |
1627 | * ALL that this function now does is to determine whether the input |
1628 | * address ("pc") is the address of one of the Objective-C message | |
b81654f1 MS |
1629 | * dispatch functions (mainly objc_msgSend or objc_msgSendSuper), and |
1630 | * if so, it returns the address of the method that will be called. | |
1631 | * | |
1632 | * The old function "pc_off_limits" used to do a lot of other things | |
d2e6263c | 1633 | * in addition, such as detecting shared library jump stubs and |
b81654f1 | 1634 | * returning the address of the shlib function that would be called. |
e76f05fa | 1635 | * That functionality has been moved into the gdbarch_skip_trampoline_code and |
d2e6263c MS |
1636 | * IN_SOLIB_TRAMPOLINE macros, which are resolved in the target- |
1637 | * dependent modules. | |
b81654f1 MS |
1638 | */ |
1639 | ||
1640 | struct objc_submethod_helper_data { | |
36e53c63 | 1641 | int (*f) (CORE_ADDR, CORE_ADDR *); |
b81654f1 MS |
1642 | CORE_ADDR pc; |
1643 | CORE_ADDR *new_pc; | |
1644 | }; | |
1645 | ||
b9362cc7 | 1646 | static int |
7248f48e | 1647 | find_objc_msgcall_submethod_helper (void * arg) |
b81654f1 | 1648 | { |
d2e6263c MS |
1649 | struct objc_submethod_helper_data *s = |
1650 | (struct objc_submethod_helper_data *) arg; | |
1651 | ||
1652 | if (s->f (s->pc, s->new_pc) == 0) | |
b81654f1 | 1653 | return 1; |
d2e6263c | 1654 | else |
b81654f1 | 1655 | return 0; |
b81654f1 MS |
1656 | } |
1657 | ||
b9362cc7 | 1658 | static int |
36e53c63 | 1659 | find_objc_msgcall_submethod (int (*f) (CORE_ADDR, CORE_ADDR *), |
d2e6263c MS |
1660 | CORE_ADDR pc, |
1661 | CORE_ADDR *new_pc) | |
b81654f1 MS |
1662 | { |
1663 | struct objc_submethod_helper_data s; | |
1664 | ||
1665 | s.f = f; | |
1666 | s.pc = pc; | |
1667 | s.new_pc = new_pc; | |
1668 | ||
1669 | if (catch_errors (find_objc_msgcall_submethod_helper, | |
7248f48e | 1670 | (void *) &s, |
d2e6263c MS |
1671 | "Unable to determine target of Objective-C method call (ignoring):\n", |
1672 | RETURN_MASK_ALL) == 0) | |
b81654f1 | 1673 | return 1; |
d2e6263c | 1674 | else |
b81654f1 | 1675 | return 0; |
b81654f1 MS |
1676 | } |
1677 | ||
1678 | int | |
1679 | find_objc_msgcall (CORE_ADDR pc, CORE_ADDR *new_pc) | |
1680 | { | |
1681 | unsigned int i; | |
1682 | ||
1683 | find_objc_msgsend (); | |
5e488a7b AC |
1684 | if (new_pc != NULL) |
1685 | { | |
1686 | *new_pc = 0; | |
1687 | } | |
b81654f1 | 1688 | |
d2e6263c MS |
1689 | for (i = 0; i < nmethcalls; i++) |
1690 | if ((pc >= methcalls[i].begin) && (pc < methcalls[i].end)) | |
1691 | { | |
1692 | if (methcalls[i].stop_at != NULL) | |
1693 | return find_objc_msgcall_submethod (methcalls[i].stop_at, | |
1694 | pc, new_pc); | |
1695 | else | |
1696 | return 0; | |
b81654f1 | 1697 | } |
d2e6263c | 1698 | |
b81654f1 MS |
1699 | return 0; |
1700 | } | |
1701 | ||
a78f21af | 1702 | extern initialize_file_ftype _initialize_objc_language; /* -Wmissing-prototypes */ |
b9362cc7 | 1703 | |
b81654f1 MS |
1704 | void |
1705 | _initialize_objc_language (void) | |
1706 | { | |
1707 | add_language (&objc_language_defn); | |
d2e6263c | 1708 | add_info ("selectors", selectors_info, /* INFO SELECTORS command. */ |
1bedd215 | 1709 | _("All Objective-C selectors, or those matching REGEXP.")); |
d2e6263c | 1710 | add_info ("classes", classes_info, /* INFO CLASSES command. */ |
1bedd215 | 1711 | _("All Objective-C classes, or those matching REGEXP.")); |
b81654f1 | 1712 | add_com ("print-object", class_vars, print_object_command, |
1bedd215 | 1713 | _("Ask an Objective-C object to print itself.")); |
b81654f1 MS |
1714 | add_com_alias ("po", "print-object", class_vars, 1); |
1715 | } | |
1716 | ||
b81654f1 MS |
1717 | static void |
1718 | read_objc_method (CORE_ADDR addr, struct objc_method *method) | |
1719 | { | |
d2e6263c | 1720 | method->name = read_memory_unsigned_integer (addr + 0, 4); |
b81654f1 | 1721 | method->types = read_memory_unsigned_integer (addr + 4, 4); |
d2e6263c | 1722 | method->imp = read_memory_unsigned_integer (addr + 8, 4); |
b81654f1 MS |
1723 | } |
1724 | ||
1725 | static | |
1726 | unsigned long read_objc_methlist_nmethods (CORE_ADDR addr) | |
1727 | { | |
1728 | return read_memory_unsigned_integer (addr + 4, 4); | |
1729 | } | |
1730 | ||
1731 | static void | |
1732 | read_objc_methlist_method (CORE_ADDR addr, unsigned long num, | |
1733 | struct objc_method *method) | |
1734 | { | |
e8f3fcdd | 1735 | gdb_assert (num < read_objc_methlist_nmethods (addr)); |
b81654f1 MS |
1736 | read_objc_method (addr + 8 + (12 * num), method); |
1737 | } | |
1738 | ||
1739 | static void | |
1740 | read_objc_object (CORE_ADDR addr, struct objc_object *object) | |
1741 | { | |
1742 | object->isa = read_memory_unsigned_integer (addr, 4); | |
1743 | } | |
1744 | ||
1745 | static void | |
1746 | read_objc_super (CORE_ADDR addr, struct objc_super *super) | |
1747 | { | |
1748 | super->receiver = read_memory_unsigned_integer (addr, 4); | |
1749 | super->class = read_memory_unsigned_integer (addr + 4, 4); | |
1750 | }; | |
1751 | ||
1752 | static void | |
1753 | read_objc_class (CORE_ADDR addr, struct objc_class *class) | |
1754 | { | |
1755 | class->isa = read_memory_unsigned_integer (addr, 4); | |
1756 | class->super_class = read_memory_unsigned_integer (addr + 4, 4); | |
1757 | class->name = read_memory_unsigned_integer (addr + 8, 4); | |
1758 | class->version = read_memory_unsigned_integer (addr + 12, 4); | |
1759 | class->info = read_memory_unsigned_integer (addr + 16, 4); | |
1760 | class->instance_size = read_memory_unsigned_integer (addr + 18, 4); | |
1761 | class->ivars = read_memory_unsigned_integer (addr + 24, 4); | |
1762 | class->methods = read_memory_unsigned_integer (addr + 28, 4); | |
1763 | class->cache = read_memory_unsigned_integer (addr + 32, 4); | |
1764 | class->protocols = read_memory_unsigned_integer (addr + 36, 4); | |
1765 | } | |
1766 | ||
b9362cc7 | 1767 | static CORE_ADDR |
b81654f1 MS |
1768 | find_implementation_from_class (CORE_ADDR class, CORE_ADDR sel) |
1769 | { | |
1770 | CORE_ADDR subclass = class; | |
1771 | ||
d2e6263c MS |
1772 | while (subclass != 0) |
1773 | { | |
b81654f1 | 1774 | |
d2e6263c MS |
1775 | struct objc_class class_str; |
1776 | unsigned mlistnum = 0; | |
b81654f1 | 1777 | |
d2e6263c | 1778 | read_objc_class (subclass, &class_str); |
b81654f1 | 1779 | |
d2e6263c MS |
1780 | for (;;) |
1781 | { | |
1782 | CORE_ADDR mlist; | |
1783 | unsigned long nmethods; | |
1784 | unsigned long i; | |
b81654f1 | 1785 | |
d2e6263c MS |
1786 | mlist = read_memory_unsigned_integer (class_str.methods + |
1787 | (4 * mlistnum), 4); | |
1788 | if (mlist == 0) | |
1789 | break; | |
b81654f1 | 1790 | |
d2e6263c | 1791 | nmethods = read_objc_methlist_nmethods (mlist); |
b81654f1 | 1792 | |
d2e6263c MS |
1793 | for (i = 0; i < nmethods; i++) |
1794 | { | |
1795 | struct objc_method meth_str; | |
1796 | read_objc_methlist_method (mlist, i, &meth_str); | |
b81654f1 MS |
1797 | |
1798 | #if 0 | |
d2e6263c MS |
1799 | fprintf (stderr, |
1800 | "checking method 0x%lx against selector 0x%lx\n", | |
1801 | meth_str.name, sel); | |
b81654f1 MS |
1802 | #endif |
1803 | ||
d2e6263c | 1804 | if (meth_str.name == sel) |
1abf022c AF |
1805 | /* FIXME: hppa arch was doing a pointer dereference |
1806 | here. There needs to be a better way to do that. */ | |
1807 | return meth_str.imp; | |
d2e6263c MS |
1808 | } |
1809 | mlistnum++; | |
b81654f1 | 1810 | } |
d2e6263c | 1811 | subclass = class_str.super_class; |
b81654f1 | 1812 | } |
b81654f1 MS |
1813 | |
1814 | return 0; | |
1815 | } | |
1816 | ||
b9362cc7 | 1817 | static CORE_ADDR |
b81654f1 MS |
1818 | find_implementation (CORE_ADDR object, CORE_ADDR sel) |
1819 | { | |
1820 | struct objc_object ostr; | |
1821 | ||
d2e6263c MS |
1822 | if (object == 0) |
1823 | return 0; | |
b81654f1 | 1824 | read_objc_object (object, &ostr); |
d2e6263c MS |
1825 | if (ostr.isa == 0) |
1826 | return 0; | |
b81654f1 MS |
1827 | |
1828 | return find_implementation_from_class (ostr.isa, sel); | |
1829 | } | |
1830 | ||
a0273b2f | 1831 | #define OBJC_FETCH_POINTER_ARGUMENT(argi) \ |
d99344c0 UW |
1832 | gdbarch_fetch_pointer_argument (current_gdbarch, get_current_frame (), \ |
1833 | argi, builtin_type_void_func_ptr) | |
a0273b2f | 1834 | |
b81654f1 MS |
1835 | static int |
1836 | resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc) | |
1837 | { | |
1838 | CORE_ADDR object; | |
1839 | CORE_ADDR sel; | |
1840 | CORE_ADDR res; | |
1841 | ||
a0273b2f AF |
1842 | object = OBJC_FETCH_POINTER_ARGUMENT (0); |
1843 | sel = OBJC_FETCH_POINTER_ARGUMENT (1); | |
b81654f1 MS |
1844 | |
1845 | res = find_implementation (object, sel); | |
d2e6263c MS |
1846 | if (new_pc != 0) |
1847 | *new_pc = res; | |
1848 | if (res == 0) | |
1849 | return 1; | |
b81654f1 MS |
1850 | return 0; |
1851 | } | |
1852 | ||
1853 | static int | |
1854 | resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc) | |
1855 | { | |
1856 | CORE_ADDR object; | |
1857 | CORE_ADDR sel; | |
1858 | CORE_ADDR res; | |
1859 | ||
a0273b2f AF |
1860 | object = OBJC_FETCH_POINTER_ARGUMENT (1); |
1861 | sel = OBJC_FETCH_POINTER_ARGUMENT (2); | |
b81654f1 MS |
1862 | |
1863 | res = find_implementation (object, sel); | |
d2e6263c MS |
1864 | if (new_pc != 0) |
1865 | *new_pc = res; | |
1866 | if (res == 0) | |
1867 | return 1; | |
b81654f1 MS |
1868 | return 0; |
1869 | } | |
1870 | ||
1871 | static int | |
1872 | resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc) | |
1873 | { | |
1874 | struct objc_super sstr; | |
1875 | ||
1876 | CORE_ADDR super; | |
1877 | CORE_ADDR sel; | |
1878 | CORE_ADDR res; | |
1879 | ||
a0273b2f AF |
1880 | super = OBJC_FETCH_POINTER_ARGUMENT (0); |
1881 | sel = OBJC_FETCH_POINTER_ARGUMENT (1); | |
b81654f1 MS |
1882 | |
1883 | read_objc_super (super, &sstr); | |
d2e6263c MS |
1884 | if (sstr.class == 0) |
1885 | return 0; | |
b81654f1 MS |
1886 | |
1887 | res = find_implementation_from_class (sstr.class, sel); | |
d2e6263c MS |
1888 | if (new_pc != 0) |
1889 | *new_pc = res; | |
1890 | if (res == 0) | |
1891 | return 1; | |
b81654f1 MS |
1892 | return 0; |
1893 | } | |
1894 | ||
1895 | static int | |
1896 | resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc) | |
1897 | { | |
1898 | struct objc_super sstr; | |
1899 | ||
1900 | CORE_ADDR super; | |
1901 | CORE_ADDR sel; | |
1902 | CORE_ADDR res; | |
1903 | ||
a0273b2f AF |
1904 | super = OBJC_FETCH_POINTER_ARGUMENT (1); |
1905 | sel = OBJC_FETCH_POINTER_ARGUMENT (2); | |
b81654f1 MS |
1906 | |
1907 | read_objc_super (super, &sstr); | |
d2e6263c MS |
1908 | if (sstr.class == 0) |
1909 | return 0; | |
b81654f1 MS |
1910 | |
1911 | res = find_implementation_from_class (sstr.class, sel); | |
d2e6263c MS |
1912 | if (new_pc != 0) |
1913 | *new_pc = res; | |
1914 | if (res == 0) | |
1915 | return 1; | |
b81654f1 MS |
1916 | return 0; |
1917 | } |