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