* target.h (enum target_object): Add TARGET_OBJECT_SPU.
[deliverable/binutils-gdb.git] / gdb / objc-lang.c
CommitLineData
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
52struct objc_object {
53 CORE_ADDR isa;
54};
55
56struct 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
69struct objc_super {
70 CORE_ADDR receiver;
71 CORE_ADDR class;
72};
73
74struct 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
84struct symbol *
85lookup_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
110CORE_ADDR
111lookup_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 137CORE_ADDR
b81654f1
MS
138lookup_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
163struct value *
164value_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
213char *
9a3d7dfd 214objc_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
277static void
f86f5ca3 278objc_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
323static void
324objc_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
337static void
fc1a4b47 338objc_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
450static struct type *
451objc_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
MS
463 type = init_type (TYPE_CODE_INT,
464 TARGET_INT_BIT / TARGET_CHAR_BIT,
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,
490 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
491 0, "short", objfile);
492 break;
493 case FT_SIGNED_SHORT:
494 type = init_type (TYPE_CODE_INT,
495 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
496 0, "short", objfile); /* FIXME-fnf */
497 break;
498 case FT_UNSIGNED_SHORT:
499 type = init_type (TYPE_CODE_INT,
500 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
501 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
502 break;
503 case FT_INTEGER:
504 type = init_type (TYPE_CODE_INT,
505 TARGET_INT_BIT / TARGET_CHAR_BIT,
506 0, "int", objfile);
507 break;
508 case FT_SIGNED_INTEGER:
509 type = init_type (TYPE_CODE_INT,
510 TARGET_INT_BIT / TARGET_CHAR_BIT,
511 0, "int", objfile); /* FIXME -fnf */
512 break;
513 case FT_UNSIGNED_INTEGER:
514 type = init_type (TYPE_CODE_INT,
515 TARGET_INT_BIT / TARGET_CHAR_BIT,
516 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
517 break;
518 case FT_LONG:
519 type = init_type (TYPE_CODE_INT,
520 TARGET_LONG_BIT / TARGET_CHAR_BIT,
521 0, "long", objfile);
522 break;
523 case FT_SIGNED_LONG:
524 type = init_type (TYPE_CODE_INT,
525 TARGET_LONG_BIT / TARGET_CHAR_BIT,
526 0, "long", objfile); /* FIXME -fnf */
527 break;
528 case FT_UNSIGNED_LONG:
529 type = init_type (TYPE_CODE_INT,
530 TARGET_LONG_BIT / TARGET_CHAR_BIT,
531 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
532 break;
533 case FT_LONG_LONG:
534 type = init_type (TYPE_CODE_INT,
535 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
536 0, "long long", objfile);
537 break;
538 case FT_SIGNED_LONG_LONG:
539 type = init_type (TYPE_CODE_INT,
540 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
541 0, "signed long long", objfile);
542 break;
543 case FT_UNSIGNED_LONG_LONG:
544 type = init_type (TYPE_CODE_INT,
545 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
546 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
547 break;
548 case FT_FLOAT:
549 type = init_type (TYPE_CODE_FLT,
550 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
551 0, "float", objfile);
552 break;
553 case FT_DBL_PREC_FLOAT:
554 type = init_type (TYPE_CODE_FLT,
555 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
556 0, "double", objfile);
557 break;
558 case FT_EXT_PREC_FLOAT:
559 type = init_type (TYPE_CODE_FLT,
560 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
561 0, "long double", objfile);
562 break;
563 }
564 return (type);
565}
566
f636b87d
AF
567/* Determine if we are currently in the Objective-C dispatch function.
568 If so, get the address of the method function that the dispatcher
569 would call and use that as the function to step into instead. Also
570 skip over the trampoline for the function (if any). This is better
571 for the user since they are only interested in stepping into the
572 method function anyway. */
573static CORE_ADDR
574objc_skip_trampoline (CORE_ADDR stop_pc)
575{
576 CORE_ADDR real_stop_pc;
577 CORE_ADDR method_stop_pc;
578
e76f05fa 579 real_stop_pc = gdbarch_skip_trampoline_code (current_gdbarch, stop_pc);
f636b87d
AF
580
581 if (real_stop_pc != 0)
582 find_objc_msgcall (real_stop_pc, &method_stop_pc);
583 else
584 find_objc_msgcall (stop_pc, &method_stop_pc);
585
586 if (method_stop_pc)
587 {
e76f05fa
UW
588 real_stop_pc = gdbarch_skip_trampoline_code
589 (current_gdbarch, method_stop_pc);
f636b87d
AF
590 if (real_stop_pc == 0)
591 real_stop_pc = method_stop_pc;
592 }
593
594 return real_stop_pc;
595}
596
b81654f1
MS
597
598/* Table mapping opcodes into strings for printing operators
599 and precedences of the operators. */
600
601static const struct op_print objc_op_print_tab[] =
602 {
603 {",", BINOP_COMMA, PREC_COMMA, 0},
604 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
605 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
606 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
607 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
608 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
609 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
610 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
611 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
612 {"<=", BINOP_LEQ, PREC_ORDER, 0},
613 {">=", BINOP_GEQ, PREC_ORDER, 0},
614 {">", BINOP_GTR, PREC_ORDER, 0},
615 {"<", BINOP_LESS, PREC_ORDER, 0},
616 {">>", BINOP_RSH, PREC_SHIFT, 0},
617 {"<<", BINOP_LSH, PREC_SHIFT, 0},
618 {"+", BINOP_ADD, PREC_ADD, 0},
619 {"-", BINOP_SUB, PREC_ADD, 0},
620 {"*", BINOP_MUL, PREC_MUL, 0},
621 {"/", BINOP_DIV, PREC_MUL, 0},
622 {"%", BINOP_REM, PREC_MUL, 0},
623 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
624 {"-", UNOP_NEG, PREC_PREFIX, 0},
625 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
626 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
627 {"*", UNOP_IND, PREC_PREFIX, 0},
628 {"&", UNOP_ADDR, PREC_PREFIX, 0},
629 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
630 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
631 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
e8f3fcdd 632 {NULL, OP_NULL, PREC_NULL, 0}
b81654f1
MS
633};
634
635struct type ** const (objc_builtin_types[]) =
636{
637 &builtin_type_int,
638 &builtin_type_long,
639 &builtin_type_short,
640 &builtin_type_char,
641 &builtin_type_float,
642 &builtin_type_double,
643 &builtin_type_void,
644 &builtin_type_long_long,
645 &builtin_type_signed_char,
646 &builtin_type_unsigned_char,
647 &builtin_type_unsigned_short,
648 &builtin_type_unsigned_int,
649 &builtin_type_unsigned_long,
650 &builtin_type_unsigned_long_long,
651 &builtin_type_long_double,
652 &builtin_type_complex,
653 &builtin_type_double_complex,
654 0
655};
656
657const struct language_defn objc_language_defn = {
d2e6263c 658 "objective-c", /* Language name */
b81654f1
MS
659 language_objc,
660 objc_builtin_types,
661 range_check_off,
662 type_check_off,
663 case_sensitive_on,
7ca2d3a3 664 array_row_major,
5f9769d1 665 &exp_descriptor_standard,
b81654f1
MS
666 objc_parse,
667 objc_error,
e85c3284 668 null_post_parser,
b81654f1
MS
669 objc_printchar, /* Print a character constant */
670 objc_printstr, /* Function to print string constant */
671 objc_emit_char,
672 objc_create_fundamental_type, /* Create fundamental type in this language */
673 c_print_type, /* Print a type using appropriate syntax */
674 c_val_print, /* Print a value using appropriate syntax */
675 c_value_print, /* Print a top-level value */
f636b87d 676 objc_skip_trampoline, /* Language specific skip_trampoline */
5f9a71c3
DC
677 value_of_this, /* value_of_this */
678 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
b368761e 679 basic_lookup_transparent_type,/* lookup_transparent_type */
9a3d7dfd 680 objc_demangle, /* Language specific symbol demangler */
31c27f77 681 NULL, /* Language specific class_name_from_physname */
d2e6263c
MS
682 objc_op_print_tab, /* Expression operators for printing */
683 1, /* C-style arrays */
b81654f1
MS
684 0, /* String lower bound */
685 &builtin_type_char, /* Type of string elements */
6084f43a 686 default_word_break_characters,
f290d38e 687 NULL, /* FIXME: la_language_arch_info. */
e79af960 688 default_print_array_index,
b81654f1
MS
689 LANG_MAGIC
690};
691
692/*
693 * ObjC:
d2e6263c 694 * Following functions help construct Objective-C message calls
b81654f1
MS
695 */
696
d2e6263c 697struct selname /* For parsing Objective-C. */
b81654f1
MS
698 {
699 struct selname *next;
700 char *msglist_sel;
701 int msglist_len;
702 };
703
704static int msglist_len;
705static struct selname *selname_chain;
706static char *msglist_sel;
707
708void
709start_msglist(void)
710{
f86f5ca3 711 struct selname *new =
b81654f1
MS
712 (struct selname *) xmalloc (sizeof (struct selname));
713
714 new->next = selname_chain;
715 new->msglist_len = msglist_len;
716 new->msglist_sel = msglist_sel;
717 msglist_len = 0;
718 msglist_sel = (char *)xmalloc(1);
719 *msglist_sel = 0;
720 selname_chain = new;
721}
722
723void
724add_msglist(struct stoken *str, int addcolon)
725{
726 char *s, *p;
727 int len, plen;
728
d2e6263c
MS
729 if (str == 0) { /* Unnamed arg, or... */
730 if (addcolon == 0) { /* variable number of args. */
b81654f1
MS
731 msglist_len++;
732 return;
733 }
734 p = "";
735 plen = 0;
736 } else {
737 p = str->ptr;
738 plen = str->length;
739 }
740 len = plen + strlen(msglist_sel) + 2;
741 s = (char *)xmalloc(len);
742 strcpy(s, msglist_sel);
743 strncat(s, p, plen);
7248f48e 744 xfree(msglist_sel);
b81654f1
MS
745 msglist_sel = s;
746 if (addcolon) {
747 s[len-2] = ':';
748 s[len-1] = 0;
749 msglist_len++;
750 } else
751 s[len-2] = '\0';
752}
753
754int
755end_msglist(void)
756{
f86f5ca3
PH
757 int val = msglist_len;
758 struct selname *sel = selname_chain;
759 char *p = msglist_sel;
c253954e 760 CORE_ADDR selid;
b81654f1
MS
761
762 selname_chain = sel->next;
763 msglist_len = sel->msglist_len;
764 msglist_sel = sel->msglist_sel;
765 selid = lookup_child_selector(p);
766 if (!selid)
8a3fe4f8 767 error (_("Can't find selector \"%s\""), p);
b81654f1 768 write_exp_elt_longcst (selid);
7248f48e 769 xfree(p);
d2e6263c 770 write_exp_elt_longcst (val); /* Number of args */
7248f48e 771 xfree(sel);
b81654f1
MS
772
773 return val;
774}
775
776/*
777 * Function: specialcmp (char *a, char *b)
778 *
779 * Special strcmp: treats ']' and ' ' as end-of-string.
d2e6263c 780 * Used for qsorting lists of objc methods (either by class or selector).
b81654f1
MS
781 */
782
b9362cc7
AC
783static int
784specialcmp (char *a, char *b)
b81654f1
MS
785{
786 while (*a && *a != ' ' && *a != ']' && *b && *b != ' ' && *b != ']')
787 {
788 if (*a != *b)
789 return *a - *b;
790 a++, b++;
791 }
792 if (*a && *a != ' ' && *a != ']')
793 return 1; /* a is longer therefore greater */
794 if (*b && *b != ' ' && *b != ']')
795 return -1; /* a is shorter therefore lesser */
796 return 0; /* a and b are identical */
797}
798
799/*
36e53c63 800 * Function: compare_selectors (const void *, const void *)
b81654f1 801 *
d2e6263c
MS
802 * Comparison function for use with qsort. Arguments are symbols or
803 * msymbols Compares selector part of objc method name alphabetically.
b81654f1
MS
804 */
805
806static int
36e53c63 807compare_selectors (const void *a, const void *b)
b81654f1
MS
808{
809 char *aname, *bname;
810
de5ad195
DC
811 aname = SYMBOL_PRINT_NAME (*(struct symbol **) a);
812 bname = SYMBOL_PRINT_NAME (*(struct symbol **) b);
7248f48e 813 if (aname == NULL || bname == NULL)
8a3fe4f8 814 error (_("internal: compare_selectors(1)"));
b81654f1 815
7248f48e
AF
816 aname = strchr(aname, ' ');
817 bname = strchr(bname, ' ');
818 if (aname == NULL || bname == NULL)
8a3fe4f8 819 error (_("internal: compare_selectors(2)"));
b81654f1
MS
820
821 return specialcmp (aname+1, bname+1);
822}
823
824/*
825 * Function: selectors_info (regexp, from_tty)
826 *
d2e6263c
MS
827 * Implements the "Info selectors" command. Takes an optional regexp
828 * arg. Lists all objective c selectors that match the regexp. Works
829 * by grepping thru all symbols for objective c methods. Output list
830 * is sorted and uniqued.
b81654f1
MS
831 */
832
833static void
834selectors_info (char *regexp, int from_tty)
835{
836 struct objfile *objfile;
837 struct minimal_symbol *msymbol;
838 char *name;
839 char *val;
840 int matches = 0;
841 int maxlen = 0;
842 int ix;
843 char myregexp[2048];
844 char asel[256];
845 struct symbol **sym_arr;
846 int plusminus = 0;
847
848 if (regexp == NULL)
d2e6263c 849 strcpy(myregexp, ".*]"); /* Null input, match all objc methods. */
b81654f1
MS
850 else
851 {
d2e6263c
MS
852 if (*regexp == '+' || *regexp == '-')
853 { /* User wants only class methods or only instance methods. */
b81654f1
MS
854 plusminus = *regexp++;
855 while (*regexp == ' ' || *regexp == '\t')
856 regexp++;
857 }
858 if (*regexp == '\0')
859 strcpy(myregexp, ".*]");
860 else
861 {
862 strcpy(myregexp, regexp);
863 if (myregexp[strlen(myregexp) - 1] == '$') /* end of selector */
864 myregexp[strlen(myregexp) - 1] = ']'; /* end of method name */
865 else
866 strcat(myregexp, ".*]");
867 }
868 }
869
870 if (regexp != NULL)
5e488a7b
AC
871 {
872 val = re_comp (myregexp);
873 if (val != 0)
8a3fe4f8 874 error (_("Invalid regexp (%s): %s"), val, regexp);
5e488a7b 875 }
b81654f1 876
d2e6263c 877 /* First time thru is JUST to get max length and count. */
b81654f1
MS
878 ALL_MSYMBOLS (objfile, msymbol)
879 {
880 QUIT;
36018d2e 881 name = SYMBOL_NATURAL_NAME (msymbol);
b81654f1
MS
882 if (name &&
883 (name[0] == '-' || name[0] == '+') &&
d2e6263c 884 name[1] == '[') /* Got a method name. */
b81654f1 885 {
d2e6263c 886 /* Filter for class/instance methods. */
b81654f1 887 if (plusminus && name[0] != plusminus)
d2e6263c
MS
888 continue;
889 /* Find selector part. */
890 name = (char *) strchr(name+2, ' ');
b81654f1
MS
891 if (regexp == NULL || re_exec(++name) != 0)
892 {
893 char *mystart = name;
894 char *myend = (char *) strchr(mystart, ']');
895
896 if (myend && (myend - mystart > maxlen))
d2e6263c 897 maxlen = myend - mystart; /* Get longest selector. */
b81654f1
MS
898 matches++;
899 }
900 }
901 }
902 if (matches)
903 {
a3f17187 904 printf_filtered (_("Selectors matching \"%s\":\n\n"),
b81654f1
MS
905 regexp ? regexp : "*");
906
907 sym_arr = alloca (matches * sizeof (struct symbol *));
908 matches = 0;
909 ALL_MSYMBOLS (objfile, msymbol)
910 {
911 QUIT;
36018d2e 912 name = SYMBOL_NATURAL_NAME (msymbol);
b81654f1
MS
913 if (name &&
914 (name[0] == '-' || name[0] == '+') &&
d2e6263c 915 name[1] == '[') /* Got a method name. */
b81654f1 916 {
d2e6263c 917 /* Filter for class/instance methods. */
b81654f1 918 if (plusminus && name[0] != plusminus)
d2e6263c
MS
919 continue;
920 /* Find selector part. */
921 name = (char *) strchr(name+2, ' ');
b81654f1
MS
922 if (regexp == NULL || re_exec(++name) != 0)
923 sym_arr[matches++] = (struct symbol *) msymbol;
924 }
925 }
926
927 qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
928 compare_selectors);
d2e6263c
MS
929 /* Prevent compare on first iteration. */
930 asel[0] = 0;
931 for (ix = 0; ix < matches; ix++) /* Now do the output. */
b81654f1
MS
932 {
933 char *p = asel;
934
935 QUIT;
36018d2e 936 name = SYMBOL_NATURAL_NAME (sym_arr[ix]);
b81654f1
MS
937 name = strchr (name, ' ') + 1;
938 if (p[0] && specialcmp(name, p) == 0)
d2e6263c 939 continue; /* Seen this one already (not unique). */
b81654f1 940
d2e6263c
MS
941 /* Copy selector part. */
942 while (*name && *name != ']')
b81654f1
MS
943 *p++ = *name++;
944 *p++ = '\0';
d2e6263c
MS
945 /* Print in columns. */
946 puts_filtered_tabular(asel, maxlen + 1, 0);
b81654f1
MS
947 }
948 begin_line();
949 }
950 else
a3f17187 951 printf_filtered (_("No selectors matching \"%s\"\n"), regexp ? regexp : "*");
b81654f1
MS
952}
953
954/*
36e53c63 955 * Function: compare_classes (const void *, const void *)
b81654f1 956 *
d2e6263c
MS
957 * Comparison function for use with qsort. Arguments are symbols or
958 * msymbols Compares class part of objc method name alphabetically.
b81654f1
MS
959 */
960
961static int
36e53c63 962compare_classes (const void *a, const void *b)
b81654f1
MS
963{
964 char *aname, *bname;
965
de5ad195
DC
966 aname = SYMBOL_PRINT_NAME (*(struct symbol **) a);
967 bname = SYMBOL_PRINT_NAME (*(struct symbol **) b);
7248f48e 968 if (aname == NULL || bname == NULL)
8a3fe4f8 969 error (_("internal: compare_classes(1)"));
b81654f1
MS
970
971 return specialcmp (aname+1, bname+1);
972}
973
974/*
975 * Function: classes_info(regexp, from_tty)
976 *
977 * Implements the "info classes" command for objective c classes.
978 * Lists all objective c classes that match the optional regexp.
d2e6263c
MS
979 * Works by grepping thru the list of objective c methods. List will
980 * be sorted and uniqued (since one class may have many methods).
981 * BUGS: will not list a class that has no methods.
b81654f1
MS
982 */
983
984static void
985classes_info (char *regexp, int from_tty)
986{
987 struct objfile *objfile;
988 struct minimal_symbol *msymbol;
989 char *name;
990 char *val;
991 int matches = 0;
992 int maxlen = 0;
993 int ix;
994 char myregexp[2048];
995 char aclass[256];
996 struct symbol **sym_arr;
997
998 if (regexp == NULL)
d2e6263c 999 strcpy(myregexp, ".* "); /* Null input: match all objc classes. */
b81654f1
MS
1000 else
1001 {
1002 strcpy(myregexp, regexp);
1003 if (myregexp[strlen(myregexp) - 1] == '$')
d2e6263c 1004 /* In the method name, the end of the class name is marked by ' '. */
b81654f1
MS
1005 myregexp[strlen(myregexp) - 1] = ' ';
1006 else
1007 strcat(myregexp, ".* ");
1008 }
1009
1010 if (regexp != NULL)
5e488a7b
AC
1011 {
1012 val = re_comp (myregexp);
1013 if (val != 0)
8a3fe4f8 1014 error (_("Invalid regexp (%s): %s"), val, regexp);
5e488a7b 1015 }
b81654f1 1016
d2e6263c 1017 /* First time thru is JUST to get max length and count. */
b81654f1
MS
1018 ALL_MSYMBOLS (objfile, msymbol)
1019 {
1020 QUIT;
36018d2e 1021 name = SYMBOL_NATURAL_NAME (msymbol);
b81654f1
MS
1022 if (name &&
1023 (name[0] == '-' || name[0] == '+') &&
d2e6263c 1024 name[1] == '[') /* Got a method name. */
b81654f1
MS
1025 if (regexp == NULL || re_exec(name+2) != 0)
1026 {
d2e6263c
MS
1027 /* Compute length of classname part. */
1028 char *mystart = name + 2;
b81654f1
MS
1029 char *myend = (char *) strchr(mystart, ' ');
1030
1031 if (myend && (myend - mystart > maxlen))
1032 maxlen = myend - mystart;
1033 matches++;
1034 }
1035 }
1036 if (matches)
1037 {
a3f17187 1038 printf_filtered (_("Classes matching \"%s\":\n\n"),
b81654f1
MS
1039 regexp ? regexp : "*");
1040 sym_arr = alloca (matches * sizeof (struct symbol *));
1041 matches = 0;
1042 ALL_MSYMBOLS (objfile, msymbol)
1043 {
1044 QUIT;
36018d2e 1045 name = SYMBOL_NATURAL_NAME (msymbol);
b81654f1
MS
1046 if (name &&
1047 (name[0] == '-' || name[0] == '+') &&
d2e6263c 1048 name[1] == '[') /* Got a method name. */
b81654f1
MS
1049 if (regexp == NULL || re_exec(name+2) != 0)
1050 sym_arr[matches++] = (struct symbol *) msymbol;
1051 }
1052
1053 qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
1054 compare_classes);
d2e6263c
MS
1055 /* Prevent compare on first iteration. */
1056 aclass[0] = 0;
1057 for (ix = 0; ix < matches; ix++) /* Now do the output. */
b81654f1
MS
1058 {
1059 char *p = aclass;
1060
1061 QUIT;
36018d2e 1062 name = SYMBOL_NATURAL_NAME (sym_arr[ix]);
b81654f1
MS
1063 name += 2;
1064 if (p[0] && specialcmp(name, p) == 0)
d2e6263c 1065 continue; /* Seen this one already (not unique). */
b81654f1 1066
d2e6263c
MS
1067 /* Copy class part of method name. */
1068 while (*name && *name != ' ')
b81654f1
MS
1069 *p++ = *name++;
1070 *p++ = '\0';
d2e6263c
MS
1071 /* Print in columns. */
1072 puts_filtered_tabular(aclass, maxlen + 1, 0);
b81654f1
MS
1073 }
1074 begin_line();
1075 }
1076 else
a3f17187 1077 printf_filtered (_("No classes matching \"%s\"\n"), regexp ? regexp : "*");
b81654f1
MS
1078}
1079
1080/*
1081 * Function: find_imps (char *selector, struct symbol **sym_arr)
1082 *
1083 * Input: a string representing a selector
1084 * a pointer to an array of symbol pointers
1085 * possibly a pointer to a symbol found by the caller.
1086 *
d2e6263c
MS
1087 * Output: number of methods that implement that selector. Side
1088 * effects: The array of symbol pointers is filled with matching syms.
b81654f1 1089 *
d2e6263c
MS
1090 * By analogy with function "find_methods" (symtab.c), builds a list
1091 * of symbols matching the ambiguous input, so that "decode_line_2"
1092 * (symtab.c) can list them and ask the user to choose one or more.
1093 * In this case the matches are objective c methods
1094 * ("implementations") matching an objective c selector.
b81654f1 1095 *
d2e6263c
MS
1096 * Note that it is possible for a normal (c-style) function to have
1097 * the same name as an objective c selector. To prevent the selector
1098 * from eclipsing the function, we allow the caller (decode_line_1) to
1099 * search for such a function first, and if it finds one, pass it in
1100 * to us. We will then integrate it into the list. We also search
1101 * for one here, among the minsyms.
b81654f1 1102 *
d2e6263c
MS
1103 * NOTE: if NUM_DEBUGGABLE is non-zero, the sym_arr will be divided
1104 * into two parts: debuggable (struct symbol) syms, and
1105 * non_debuggable (struct minimal_symbol) syms. The debuggable
1106 * ones will come first, before NUM_DEBUGGABLE (which will thus
1107 * be the index of the first non-debuggable one).
b81654f1
MS
1108 */
1109
1110/*
1111 * Function: total_number_of_imps (char *selector);
1112 *
1113 * Input: a string representing a selector
1114 * Output: number of methods that implement that selector.
1115 *
d2e6263c 1116 * By analogy with function "total_number_of_methods", this allows
b81654f1 1117 * decode_line_1 (symtab.c) to detect if there are objective c methods
d2e6263c
MS
1118 * matching the input, and to allocate an array of pointers to them
1119 * which can be manipulated by "decode_line_2" (also in symtab.c).
b81654f1
MS
1120 */
1121
1122char *
1123parse_selector (char *method, char **selector)
1124{
1125 char *s1 = NULL;
1126 char *s2 = NULL;
1127 int found_quote = 0;
1128
1129 char *nselector = NULL;
1130
e8f3fcdd 1131 gdb_assert (selector != NULL);
b81654f1
MS
1132
1133 s1 = method;
1134
1135 while (isspace (*s1))
1136 s1++;
1137 if (*s1 == '\'')
1138 {
1139 found_quote = 1;
1140 s1++;
1141 }
1142 while (isspace (*s1))
1143 s1++;
1144
1145 nselector = s1;
1146 s2 = s1;
1147
1148 for (;;) {
1149 if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
1150 *s1++ = *s2;
1151 else if (isspace (*s2))
1152 ;
1153 else if ((*s2 == '\0') || (*s2 == '\''))
1154 break;
1155 else
1156 return NULL;
1157 s2++;
1158 }
1159 *s1++ = '\0';
1160
1161 while (isspace (*s2))
1162 s2++;
1163 if (found_quote)
1164 {
1165 if (*s2 == '\'')
1166 s2++;
1167 while (isspace (*s2))
1168 s2++;
1169 }
1170
1171 if (selector != NULL)
1172 *selector = nselector;
1173
1174 return s2;
1175}
1176
1177char *
d2e6263c
MS
1178parse_method (char *method, char *type, char **class,
1179 char **category, char **selector)
b81654f1
MS
1180{
1181 char *s1 = NULL;
1182 char *s2 = NULL;
1183 int found_quote = 0;
1184
1185 char ntype = '\0';
1186 char *nclass = NULL;
1187 char *ncategory = NULL;
1188 char *nselector = NULL;
1189
e8f3fcdd
AC
1190 gdb_assert (type != NULL);
1191 gdb_assert (class != NULL);
1192 gdb_assert (category != NULL);
1193 gdb_assert (selector != NULL);
b81654f1
MS
1194
1195 s1 = method;
1196
1197 while (isspace (*s1))
1198 s1++;
1199 if (*s1 == '\'')
1200 {
1201 found_quote = 1;
1202 s1++;
1203 }
1204 while (isspace (*s1))
1205 s1++;
1206
1207 if ((s1[0] == '+') || (s1[0] == '-'))
1208 ntype = *s1++;
1209
1210 while (isspace (*s1))
1211 s1++;
1212
1213 if (*s1 != '[')
1214 return NULL;
1215 s1++;
1216
1217 nclass = s1;
1218 while (isalnum (*s1) || (*s1 == '_'))
1219 s1++;
1220
1221 s2 = s1;
1222 while (isspace (*s2))
1223 s2++;
1224
1225 if (*s2 == '(')
1226 {
1227 s2++;
1228 while (isspace (*s2))
1229 s2++;
1230 ncategory = s2;
1231 while (isalnum (*s2) || (*s2 == '_'))
1232 s2++;
1233 *s2++ = '\0';
1234 }
1235
d2e6263c 1236 /* Truncate the class name now that we're not using the open paren. */
b81654f1
MS
1237 *s1++ = '\0';
1238
1239 nselector = s2;
1240 s1 = s2;
1241
1242 for (;;) {
1243 if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
1244 *s1++ = *s2;
1245 else if (isspace (*s2))
1246 ;
1247 else if (*s2 == ']')
1248 break;
1249 else
1250 return NULL;
1251 s2++;
1252 }
1253 *s1++ = '\0';
1254 s2++;
1255
1256 while (isspace (*s2))
1257 s2++;
1258 if (found_quote)
1259 {
1260 if (*s2 != '\'')
1261 return NULL;
1262 s2++;
1263 while (isspace (*s2))
1264 s2++;
1265 }
1266
1267 if (type != NULL)
1268 *type = ntype;
1269 if (class != NULL)
1270 *class = nclass;
1271 if (category != NULL)
1272 *category = ncategory;
1273 if (selector != NULL)
1274 *selector = nselector;
1275
1276 return s2;
1277}
1278
2f9a90b4 1279static void
d2e6263c
MS
1280find_methods (struct symtab *symtab, char type,
1281 const char *class, const char *category,
1282 const char *selector, struct symbol **syms,
1283 unsigned int *nsym, unsigned int *ndebug)
b81654f1
MS
1284{
1285 struct objfile *objfile = NULL;
1286 struct minimal_symbol *msymbol = NULL;
1287 struct block *block = NULL;
1288 struct symbol *sym = NULL;
1289
1290 char *symname = NULL;
1291
1292 char ntype = '\0';
1293 char *nclass = NULL;
1294 char *ncategory = NULL;
1295 char *nselector = NULL;
1296
1297 unsigned int csym = 0;
1298 unsigned int cdebug = 0;
1299
1300 static char *tmp = NULL;
1301 static unsigned int tmplen = 0;
1302
e8f3fcdd
AC
1303 gdb_assert (nsym != NULL);
1304 gdb_assert (ndebug != NULL);
b81654f1
MS
1305
1306 if (symtab)
1307 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1308
1309 ALL_MSYMBOLS (objfile, msymbol)
1310 {
1311 QUIT;
1312
1313 if ((msymbol->type != mst_text) && (msymbol->type != mst_file_text))
d2e6263c 1314 /* Not a function or method. */
b81654f1
MS
1315 continue;
1316
1317 if (symtab)
8da065d5
DC
1318 if ((SYMBOL_VALUE_ADDRESS (msymbol) < BLOCK_START (block)) ||
1319 (SYMBOL_VALUE_ADDRESS (msymbol) >= BLOCK_END (block)))
d2e6263c 1320 /* Not in the specified symtab. */
b81654f1
MS
1321 continue;
1322
36018d2e 1323 symname = SYMBOL_NATURAL_NAME (msymbol);
b81654f1
MS
1324 if (symname == NULL)
1325 continue;
1326
1327 if ((symname[0] != '-' && symname[0] != '+') || (symname[1] != '['))
d2e6263c 1328 /* Not a method name. */
b81654f1
MS
1329 continue;
1330
1331 while ((strlen (symname) + 1) >= tmplen)
1332 {
1333 tmplen = (tmplen == 0) ? 1024 : tmplen * 2;
1334 tmp = xrealloc (tmp, tmplen);
1335 }
1336 strcpy (tmp, symname);
1337
1338 if (parse_method (tmp, &ntype, &nclass, &ncategory, &nselector) == NULL)
1339 continue;
1340
1341 if ((type != '\0') && (ntype != type))
1342 continue;
1343
d2e6263c
MS
1344 if ((class != NULL)
1345 && ((nclass == NULL) || (strcmp (class, nclass) != 0)))
b81654f1
MS
1346 continue;
1347
d2e6263c
MS
1348 if ((category != NULL) &&
1349 ((ncategory == NULL) || (strcmp (category, ncategory) != 0)))
b81654f1
MS
1350 continue;
1351
d2e6263c
MS
1352 if ((selector != NULL) &&
1353 ((nselector == NULL) || (strcmp (selector, nselector) != 0)))
b81654f1
MS
1354 continue;
1355
1356 sym = find_pc_function (SYMBOL_VALUE_ADDRESS (msymbol));
1357 if (sym != NULL)
1358 {
36018d2e 1359 const char *newsymname = SYMBOL_NATURAL_NAME (sym);
b81654f1 1360
b81654f1
MS
1361 if (strcmp (symname, newsymname) == 0)
1362 {
d2e6263c
MS
1363 /* Found a high-level method sym: swap it into the
1364 lower part of sym_arr (below num_debuggable). */
b81654f1
MS
1365 if (syms != NULL)
1366 {
1367 syms[csym] = syms[cdebug];
1368 syms[cdebug] = sym;
1369 }
1370 csym++;
1371 cdebug++;
1372 }
1373 else
1374 {
d2e6263c
MS
1375 warning (
1376"debugging symbol \"%s\" does not match minimal symbol (\"%s\"); ignoring",
b81654f1
MS
1377 newsymname, symname);
1378 if (syms != NULL)
1379 syms[csym] = (struct symbol *) msymbol;
1380 csym++;
1381 }
1382 }
1383 else
1384 {
d2e6263c 1385 /* Found a non-debuggable method symbol. */
b81654f1
MS
1386 if (syms != NULL)
1387 syms[csym] = (struct symbol *) msymbol;
1388 csym++;
1389 }
1390 }
1391
1392 if (nsym != NULL)
1393 *nsym = csym;
1394 if (ndebug != NULL)
1395 *ndebug = cdebug;
1396}
1397
1398char *find_imps (struct symtab *symtab, struct block *block,
d2e6263c
MS
1399 char *method, struct symbol **syms,
1400 unsigned int *nsym, unsigned int *ndebug)
b81654f1
MS
1401{
1402 char type = '\0';
1403 char *class = NULL;
1404 char *category = NULL;
1405 char *selector = NULL;
1406
1407 unsigned int csym = 0;
1408 unsigned int cdebug = 0;
1409
1410 unsigned int ncsym = 0;
1411 unsigned int ncdebug = 0;
1412
1413 char *buf = NULL;
1414 char *tmp = NULL;
1415
e8f3fcdd
AC
1416 gdb_assert (nsym != NULL);
1417 gdb_assert (ndebug != NULL);
b81654f1
MS
1418
1419 if (nsym != NULL)
1420 *nsym = 0;
1421 if (ndebug != NULL)
1422 *ndebug = 0;
1423
1424 buf = (char *) alloca (strlen (method) + 1);
1425 strcpy (buf, method);
1426 tmp = parse_method (buf, &type, &class, &category, &selector);
1427
1428 if (tmp == NULL) {
1429
b81654f1
MS
1430 struct symbol *sym = NULL;
1431 struct minimal_symbol *msym = NULL;
1432
1433 strcpy (buf, method);
1434 tmp = parse_selector (buf, &selector);
1435
1436 if (tmp == NULL)
1437 return NULL;
1438
cdef89d0 1439 sym = lookup_symbol (selector, block, VAR_DOMAIN, 0, NULL);
b81654f1
MS
1440 if (sym != NULL)
1441 {
1442 if (syms)
1443 syms[csym] = sym;
1444 csym++;
1445 cdebug++;
1446 }
1447
1448 if (sym == NULL)
1449 msym = lookup_minimal_symbol (selector, 0, 0);
1450
1451 if (msym != NULL)
1452 {
1453 if (syms)
36e53c63 1454 syms[csym] = (struct symbol *)msym;
b81654f1
MS
1455 csym++;
1456 }
1457 }
1458
1459 if (syms != NULL)
d2e6263c
MS
1460 find_methods (symtab, type, class, category, selector,
1461 syms + csym, &ncsym, &ncdebug);
b81654f1 1462 else
d2e6263c
MS
1463 find_methods (symtab, type, class, category, selector,
1464 NULL, &ncsym, &ncdebug);
b81654f1 1465
d2e6263c 1466 /* If we didn't find any methods, just return. */
b81654f1
MS
1467 if (ncsym == 0 && ncdebug == 0)
1468 return method;
1469
1470 /* Take debug symbols from the second batch of symbols and swap them
1471 * with debug symbols from the first batch. Repeat until either the
1472 * second section is out of debug symbols or the first section is
1473 * full of debug symbols. Either way we have all debug symbols
d2e6263c
MS
1474 * packed to the beginning of the buffer.
1475 */
b81654f1
MS
1476
1477 if (syms != NULL)
1478 {
1479 while ((cdebug < csym) && (ncdebug > 0))
1480 {
1481 struct symbol *s = NULL;
d2e6263c
MS
1482 /* First non-debugging symbol. */
1483 unsigned int i = cdebug;
1484 /* Last of second batch of debug symbols. */
1485 unsigned int j = csym + ncdebug - 1;
b81654f1
MS
1486
1487 s = syms[j];
1488 syms[j] = syms[i];
1489 syms[i] = s;
1490
d2e6263c
MS
1491 /* We've moved a symbol from the second debug section to the
1492 first one. */
b81654f1
MS
1493 cdebug++;
1494 ncdebug--;
1495 }
1496 }
1497
1498 csym += ncsym;
1499 cdebug += ncdebug;
1500
1501 if (nsym != NULL)
1502 *nsym = csym;
1503 if (ndebug != NULL)
1504 *ndebug = cdebug;
1505
1506 if (syms == NULL)
1507 return method + (tmp - buf);
1508
1509 if (csym > 1)
1510 {
d2e6263c 1511 /* Sort debuggable symbols. */
b81654f1 1512 if (cdebug > 1)
d2e6263c
MS
1513 qsort (syms, cdebug, sizeof (struct minimal_symbol *),
1514 compare_classes);
b81654f1 1515
d2e6263c 1516 /* Sort minimal_symbols. */
b81654f1 1517 if ((csym - cdebug) > 1)
d2e6263c
MS
1518 qsort (&syms[cdebug], csym - cdebug,
1519 sizeof (struct minimal_symbol *), compare_classes);
b81654f1 1520 }
d2e6263c
MS
1521 /* Terminate the sym_arr list. */
1522 syms[csym] = 0;
b81654f1
MS
1523
1524 return method + (tmp - buf);
1525}
1526
b9362cc7 1527static void
b81654f1
MS
1528print_object_command (char *args, int from_tty)
1529{
1530 struct value *object, *function, *description;
36e53c63 1531 CORE_ADDR string_addr, object_addr;
b81654f1 1532 int i = 0;
22a44745 1533 gdb_byte c = 0;
b81654f1
MS
1534
1535 if (!args || !*args)
d2e6263c
MS
1536 error (
1537"The 'print-object' command requires an argument (an Objective-C object)");
b81654f1
MS
1538
1539 {
1540 struct expression *expr = parse_expression (args);
f86f5ca3 1541 struct cleanup *old_chain =
d2e6263c 1542 make_cleanup (free_current_contents, &expr);
b81654f1
MS
1543 int pc = 0;
1544
5f9769d1
PH
1545 object = expr->language_defn->la_exp_desc->evaluate_exp
1546 (builtin_type_void_data_ptr, expr, &pc, EVAL_NORMAL);
b81654f1
MS
1547 do_cleanups (old_chain);
1548 }
1549
36e53c63
AF
1550 /* Validate the address for sanity. */
1551 object_addr = value_as_long (object);
1552 read_memory (object_addr, &c, 1);
1553
7248f48e 1554 function = find_function_in_inferior ("_NSPrintForDebugger");
36e53c63 1555 if (function == NULL)
8a3fe4f8 1556 error (_("Unable to locate _NSPrintForDebugger in child process"));
b81654f1
MS
1557
1558 description = call_function_by_hand (function, 1, &object);
1559
7248f48e
AF
1560 string_addr = value_as_long (description);
1561 if (string_addr == 0)
8a3fe4f8 1562 error (_("object returns null description"));
b81654f1
MS
1563
1564 read_memory (string_addr + i++, &c, 1);
22a44745 1565 if (c != 0)
b81654f1 1566 do
d2e6263c 1567 { /* Read and print characters up to EOS. */
b81654f1
MS
1568 QUIT;
1569 printf_filtered ("%c", c);
1570 read_memory (string_addr + i++, &c, 1);
1571 } while (c != 0);
1572 else
a3f17187 1573 printf_filtered(_("<object returns empty description>"));
b81654f1
MS
1574 printf_filtered ("\n");
1575}
1576
d2e6263c
MS
1577/* The data structure 'methcalls' is used to detect method calls (thru
1578 * ObjC runtime lib functions objc_msgSend, objc_msgSendSuper, etc.),
1579 * and ultimately find the method being called.
b81654f1
MS
1580 */
1581
1582struct objc_methcall {
1583 char *name;
d2e6263c 1584 /* Return instance method to be called. */
36e53c63 1585 int (*stop_at) (CORE_ADDR, CORE_ADDR *);
d2e6263c
MS
1586 /* Start of pc range corresponding to method invocation. */
1587 CORE_ADDR begin;
1588 /* End of pc range corresponding to method invocation. */
1589 CORE_ADDR end;
b81654f1
MS
1590};
1591
d2e6263c
MS
1592static int resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc);
1593static int resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
1594static int resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc);
1595static int resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
b81654f1
MS
1596
1597static struct objc_methcall methcalls[] = {
1598 { "_objc_msgSend", resolve_msgsend, 0, 0},
1599 { "_objc_msgSend_stret", resolve_msgsend_stret, 0, 0},
1600 { "_objc_msgSendSuper", resolve_msgsend_super, 0, 0},
1601 { "_objc_msgSendSuper_stret", resolve_msgsend_super_stret, 0, 0},
1602 { "_objc_getClass", NULL, 0, 0},
1603 { "_objc_getMetaClass", NULL, 0, 0}
1604};
1605
1606#define nmethcalls (sizeof (methcalls) / sizeof (methcalls[0]))
1607
d2e6263c
MS
1608/* The following function, "find_objc_msgsend", fills in the data
1609 * structure "objc_msgs" by finding the addresses of each of the
1610 * (currently four) functions that it holds (of which objc_msgSend is
1611 * the first). This must be called each time symbols are loaded, in
1612 * case the functions have moved for some reason.
b81654f1
MS
1613 */
1614
b9362cc7 1615static void
b81654f1
MS
1616find_objc_msgsend (void)
1617{
1618 unsigned int i;
1619 for (i = 0; i < nmethcalls; i++) {
1620
1621 struct minimal_symbol *func;
1622
d2e6263c 1623 /* Try both with and without underscore. */
b81654f1
MS
1624 func = lookup_minimal_symbol (methcalls[i].name, NULL, NULL);
1625 if ((func == NULL) && (methcalls[i].name[0] == '_')) {
1626 func = lookup_minimal_symbol (methcalls[i].name + 1, NULL, NULL);
1627 }
1628 if (func == NULL) {
1629 methcalls[i].begin = 0;
1630 methcalls[i].end = 0;
1631 continue;
1632 }
1633
1634 methcalls[i].begin = SYMBOL_VALUE_ADDRESS (func);
1635 do {
1636 methcalls[i].end = SYMBOL_VALUE_ADDRESS (++func);
1637 } while (methcalls[i].begin == methcalls[i].end);
1638 }
1639}
1640
1641/* find_objc_msgcall (replaces pc_off_limits)
1642 *
d2e6263c
MS
1643 * ALL that this function now does is to determine whether the input
1644 * address ("pc") is the address of one of the Objective-C message
b81654f1
MS
1645 * dispatch functions (mainly objc_msgSend or objc_msgSendSuper), and
1646 * if so, it returns the address of the method that will be called.
1647 *
1648 * The old function "pc_off_limits" used to do a lot of other things
d2e6263c 1649 * in addition, such as detecting shared library jump stubs and
b81654f1 1650 * returning the address of the shlib function that would be called.
e76f05fa 1651 * That functionality has been moved into the gdbarch_skip_trampoline_code and
d2e6263c
MS
1652 * IN_SOLIB_TRAMPOLINE macros, which are resolved in the target-
1653 * dependent modules.
b81654f1
MS
1654 */
1655
1656struct objc_submethod_helper_data {
36e53c63 1657 int (*f) (CORE_ADDR, CORE_ADDR *);
b81654f1
MS
1658 CORE_ADDR pc;
1659 CORE_ADDR *new_pc;
1660};
1661
b9362cc7 1662static int
7248f48e 1663find_objc_msgcall_submethod_helper (void * arg)
b81654f1 1664{
d2e6263c
MS
1665 struct objc_submethod_helper_data *s =
1666 (struct objc_submethod_helper_data *) arg;
1667
1668 if (s->f (s->pc, s->new_pc) == 0)
b81654f1 1669 return 1;
d2e6263c 1670 else
b81654f1 1671 return 0;
b81654f1
MS
1672}
1673
b9362cc7 1674static int
36e53c63 1675find_objc_msgcall_submethod (int (*f) (CORE_ADDR, CORE_ADDR *),
d2e6263c
MS
1676 CORE_ADDR pc,
1677 CORE_ADDR *new_pc)
b81654f1
MS
1678{
1679 struct objc_submethod_helper_data s;
1680
1681 s.f = f;
1682 s.pc = pc;
1683 s.new_pc = new_pc;
1684
1685 if (catch_errors (find_objc_msgcall_submethod_helper,
7248f48e 1686 (void *) &s,
d2e6263c
MS
1687 "Unable to determine target of Objective-C method call (ignoring):\n",
1688 RETURN_MASK_ALL) == 0)
b81654f1 1689 return 1;
d2e6263c 1690 else
b81654f1 1691 return 0;
b81654f1
MS
1692}
1693
1694int
1695find_objc_msgcall (CORE_ADDR pc, CORE_ADDR *new_pc)
1696{
1697 unsigned int i;
1698
1699 find_objc_msgsend ();
5e488a7b
AC
1700 if (new_pc != NULL)
1701 {
1702 *new_pc = 0;
1703 }
b81654f1 1704
d2e6263c
MS
1705 for (i = 0; i < nmethcalls; i++)
1706 if ((pc >= methcalls[i].begin) && (pc < methcalls[i].end))
1707 {
1708 if (methcalls[i].stop_at != NULL)
1709 return find_objc_msgcall_submethod (methcalls[i].stop_at,
1710 pc, new_pc);
1711 else
1712 return 0;
b81654f1 1713 }
d2e6263c 1714
b81654f1
MS
1715 return 0;
1716}
1717
a78f21af 1718extern initialize_file_ftype _initialize_objc_language; /* -Wmissing-prototypes */
b9362cc7 1719
b81654f1
MS
1720void
1721_initialize_objc_language (void)
1722{
1723 add_language (&objc_language_defn);
d2e6263c 1724 add_info ("selectors", selectors_info, /* INFO SELECTORS command. */
1bedd215 1725 _("All Objective-C selectors, or those matching REGEXP."));
d2e6263c 1726 add_info ("classes", classes_info, /* INFO CLASSES command. */
1bedd215 1727 _("All Objective-C classes, or those matching REGEXP."));
b81654f1 1728 add_com ("print-object", class_vars, print_object_command,
1bedd215 1729 _("Ask an Objective-C object to print itself."));
b81654f1
MS
1730 add_com_alias ("po", "print-object", class_vars, 1);
1731}
1732
b81654f1
MS
1733static void
1734read_objc_method (CORE_ADDR addr, struct objc_method *method)
1735{
d2e6263c 1736 method->name = read_memory_unsigned_integer (addr + 0, 4);
b81654f1 1737 method->types = read_memory_unsigned_integer (addr + 4, 4);
d2e6263c 1738 method->imp = read_memory_unsigned_integer (addr + 8, 4);
b81654f1
MS
1739}
1740
1741static
1742unsigned long read_objc_methlist_nmethods (CORE_ADDR addr)
1743{
1744 return read_memory_unsigned_integer (addr + 4, 4);
1745}
1746
1747static void
1748read_objc_methlist_method (CORE_ADDR addr, unsigned long num,
1749 struct objc_method *method)
1750{
e8f3fcdd 1751 gdb_assert (num < read_objc_methlist_nmethods (addr));
b81654f1
MS
1752 read_objc_method (addr + 8 + (12 * num), method);
1753}
1754
1755static void
1756read_objc_object (CORE_ADDR addr, struct objc_object *object)
1757{
1758 object->isa = read_memory_unsigned_integer (addr, 4);
1759}
1760
1761static void
1762read_objc_super (CORE_ADDR addr, struct objc_super *super)
1763{
1764 super->receiver = read_memory_unsigned_integer (addr, 4);
1765 super->class = read_memory_unsigned_integer (addr + 4, 4);
1766};
1767
1768static void
1769read_objc_class (CORE_ADDR addr, struct objc_class *class)
1770{
1771 class->isa = read_memory_unsigned_integer (addr, 4);
1772 class->super_class = read_memory_unsigned_integer (addr + 4, 4);
1773 class->name = read_memory_unsigned_integer (addr + 8, 4);
1774 class->version = read_memory_unsigned_integer (addr + 12, 4);
1775 class->info = read_memory_unsigned_integer (addr + 16, 4);
1776 class->instance_size = read_memory_unsigned_integer (addr + 18, 4);
1777 class->ivars = read_memory_unsigned_integer (addr + 24, 4);
1778 class->methods = read_memory_unsigned_integer (addr + 28, 4);
1779 class->cache = read_memory_unsigned_integer (addr + 32, 4);
1780 class->protocols = read_memory_unsigned_integer (addr + 36, 4);
1781}
1782
b9362cc7 1783static CORE_ADDR
b81654f1
MS
1784find_implementation_from_class (CORE_ADDR class, CORE_ADDR sel)
1785{
1786 CORE_ADDR subclass = class;
1787
d2e6263c
MS
1788 while (subclass != 0)
1789 {
b81654f1 1790
d2e6263c
MS
1791 struct objc_class class_str;
1792 unsigned mlistnum = 0;
b81654f1 1793
d2e6263c 1794 read_objc_class (subclass, &class_str);
b81654f1 1795
d2e6263c
MS
1796 for (;;)
1797 {
1798 CORE_ADDR mlist;
1799 unsigned long nmethods;
1800 unsigned long i;
b81654f1 1801
d2e6263c
MS
1802 mlist = read_memory_unsigned_integer (class_str.methods +
1803 (4 * mlistnum), 4);
1804 if (mlist == 0)
1805 break;
b81654f1 1806
d2e6263c 1807 nmethods = read_objc_methlist_nmethods (mlist);
b81654f1 1808
d2e6263c
MS
1809 for (i = 0; i < nmethods; i++)
1810 {
1811 struct objc_method meth_str;
1812 read_objc_methlist_method (mlist, i, &meth_str);
b81654f1
MS
1813
1814#if 0
d2e6263c
MS
1815 fprintf (stderr,
1816 "checking method 0x%lx against selector 0x%lx\n",
1817 meth_str.name, sel);
b81654f1
MS
1818#endif
1819
d2e6263c 1820 if (meth_str.name == sel)
1abf022c
AF
1821 /* FIXME: hppa arch was doing a pointer dereference
1822 here. There needs to be a better way to do that. */
1823 return meth_str.imp;
d2e6263c
MS
1824 }
1825 mlistnum++;
b81654f1 1826 }
d2e6263c 1827 subclass = class_str.super_class;
b81654f1 1828 }
b81654f1
MS
1829
1830 return 0;
1831}
1832
b9362cc7 1833static CORE_ADDR
b81654f1
MS
1834find_implementation (CORE_ADDR object, CORE_ADDR sel)
1835{
1836 struct objc_object ostr;
1837
d2e6263c
MS
1838 if (object == 0)
1839 return 0;
b81654f1 1840 read_objc_object (object, &ostr);
d2e6263c
MS
1841 if (ostr.isa == 0)
1842 return 0;
b81654f1
MS
1843
1844 return find_implementation_from_class (ostr.isa, sel);
1845}
1846
a0273b2f 1847#define OBJC_FETCH_POINTER_ARGUMENT(argi) \
d99344c0
UW
1848 gdbarch_fetch_pointer_argument (current_gdbarch, get_current_frame (), \
1849 argi, builtin_type_void_func_ptr)
a0273b2f 1850
b81654f1
MS
1851static int
1852resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc)
1853{
1854 CORE_ADDR object;
1855 CORE_ADDR sel;
1856 CORE_ADDR res;
1857
a0273b2f
AF
1858 object = OBJC_FETCH_POINTER_ARGUMENT (0);
1859 sel = OBJC_FETCH_POINTER_ARGUMENT (1);
b81654f1
MS
1860
1861 res = find_implementation (object, sel);
d2e6263c
MS
1862 if (new_pc != 0)
1863 *new_pc = res;
1864 if (res == 0)
1865 return 1;
b81654f1
MS
1866 return 0;
1867}
1868
1869static int
1870resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
1871{
1872 CORE_ADDR object;
1873 CORE_ADDR sel;
1874 CORE_ADDR res;
1875
a0273b2f
AF
1876 object = OBJC_FETCH_POINTER_ARGUMENT (1);
1877 sel = OBJC_FETCH_POINTER_ARGUMENT (2);
b81654f1
MS
1878
1879 res = find_implementation (object, sel);
d2e6263c
MS
1880 if (new_pc != 0)
1881 *new_pc = res;
1882 if (res == 0)
1883 return 1;
b81654f1
MS
1884 return 0;
1885}
1886
1887static int
1888resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc)
1889{
1890 struct objc_super sstr;
1891
1892 CORE_ADDR super;
1893 CORE_ADDR sel;
1894 CORE_ADDR res;
1895
a0273b2f
AF
1896 super = OBJC_FETCH_POINTER_ARGUMENT (0);
1897 sel = OBJC_FETCH_POINTER_ARGUMENT (1);
b81654f1
MS
1898
1899 read_objc_super (super, &sstr);
d2e6263c
MS
1900 if (sstr.class == 0)
1901 return 0;
b81654f1
MS
1902
1903 res = find_implementation_from_class (sstr.class, sel);
d2e6263c
MS
1904 if (new_pc != 0)
1905 *new_pc = res;
1906 if (res == 0)
1907 return 1;
b81654f1
MS
1908 return 0;
1909}
1910
1911static int
1912resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
1913{
1914 struct objc_super sstr;
1915
1916 CORE_ADDR super;
1917 CORE_ADDR sel;
1918 CORE_ADDR res;
1919
a0273b2f
AF
1920 super = OBJC_FETCH_POINTER_ARGUMENT (1);
1921 sel = OBJC_FETCH_POINTER_ARGUMENT (2);
b81654f1
MS
1922
1923 read_objc_super (super, &sstr);
d2e6263c
MS
1924 if (sstr.class == 0)
1925 return 0;
b81654f1
MS
1926
1927 res = find_implementation_from_class (sstr.class, sel);
d2e6263c
MS
1928 if (new_pc != 0)
1929 *new_pc = res;
1930 if (res == 0)
1931 return 1;
b81654f1
MS
1932 return 0;
1933}
This page took 0.498829 seconds and 4 git commands to generate.