* mips-tdep.c (mips_find_abi_section): .mdebug.abi64 is the name
[deliverable/binutils-gdb.git] / gdb / objc-lang.c
CommitLineData
d2e6263c 1/* Objective-C language support routines for GDB, the GNU debugger.
b81654f1 2
437666f8 3 Copyright 2002 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
MS
32#include "objc-lang.h"
33#include "complaints.h"
34#include "value.h"
35#include "symfile.h"
36#include "objfiles.h"
37#include "string.h" /* for strchr */
38#include "target.h" /* for target_has_execution */
39#include "gdbcore.h"
40#include "gdbcmd.h"
41#include "frame.h"
42#include "gdb_regex.h"
43#include "regcache.h"
44
45#include <ctype.h>
46
47struct objc_object {
48 CORE_ADDR isa;
49};
50
51struct objc_class {
52 CORE_ADDR isa;
53 CORE_ADDR super_class;
54 CORE_ADDR name;
55 long version;
56 long info;
57 long instance_size;
58 CORE_ADDR ivars;
59 CORE_ADDR methods;
60 CORE_ADDR cache;
61 CORE_ADDR protocols;
62};
63
64struct objc_super {
65 CORE_ADDR receiver;
66 CORE_ADDR class;
67};
68
69struct objc_method {
70 CORE_ADDR name;
71 CORE_ADDR types;
72 CORE_ADDR imp;
73};
74
75/* Complaints about ObjC classes, selectors, etc. */
76
d2e6263c
MS
77static struct complaint noclass_lookup_complaint = {
78 "no way to lookup Objective-C classes", 0, 0
79};
b81654f1 80
d2e6263c
MS
81static struct complaint nosel_lookup_complaint = {
82 "no way to lookup Objective-C selectors", 0, 0
83};
b81654f1
MS
84
85
86#if (!defined __GNUC__ || __GNUC__ < 2 || __GNUC_MINOR__ < (defined __cplusplus ? 6 : 4))
87#define __CHECK_FUNCTION ((__const char *) 0)
88#else
89#define __CHECK_FUNCTION __PRETTY_FUNCTION__
90#endif
91
92#define CHECK(expression) \
93 ((void) ((expression) ? 0 : gdb_check (#expression, __FILE__, __LINE__, \
94 __CHECK_FUNCTION)))
95
96#define CHECK_FATAL(expression) \
97 ((void) ((expression) ? 0 : gdb_check_fatal (#expression, __FILE__, \
98 __LINE__, __CHECK_FUNCTION)))
99
d2e6263c
MS
100static void
101gdb_check (const char *str, const char *file,
102 unsigned int line, const char *func)
b81654f1
MS
103{
104 error ("assertion failure on line %u of \"%s\" in function \"%s\": %s\n",
105 line, file, func, str);
106}
107
d2e6263c
MS
108static void
109gdb_check_fatal (const char *str, const char *file,
110 unsigned int line, const char *func)
b81654f1
MS
111{
112 internal_error (file, line,
113 "assertion failure in function \"%s\": %s\n", func, str);
114}
115
d2e6263c
MS
116/* Lookup a structure type named "struct NAME", visible in lexical
117 block BLOCK. If NOERR is nonzero, return zero if NAME is not
118 suitably defined. */
b81654f1
MS
119
120struct symbol *
121lookup_struct_typedef (char *name, struct block *block, int noerr)
122{
123 register struct symbol *sym;
124
d2e6263c
MS
125 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
126 (struct symtab **) NULL);
b81654f1
MS
127
128 if (sym == NULL)
129 {
130 if (noerr)
131 return 0;
132 else
133 error ("No struct type named %s.", name);
134 }
135 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
136 {
137 if (noerr)
138 return 0;
139 else
d2e6263c
MS
140 error ("This context has class, union or enum %s, not a struct.",
141 name);
b81654f1
MS
142 }
143 return sym;
144}
145
146CORE_ADDR
147lookup_objc_class (char *classname)
148{
149 struct value * function, *classval;
150
151 if (! target_has_execution)
152 {
d2e6263c 153 /* Can't call into inferior to lookup class. */
b81654f1
MS
154 return 0;
155 }
156
157 if (lookup_minimal_symbol("objc_lookUpClass", 0, 0))
158 function = find_function_in_inferior("objc_lookUpClass");
159 else if (lookup_minimal_symbol ("objc_lookup_class", 0, 0))
160 function = find_function_in_inferior("objc_lookup_class");
161 else
162 {
163 complain (&noclass_lookup_complaint, 0);
164 return 0;
165 }
166
167 classval = value_string (classname, strlen (classname) + 1);
168 classval = value_coerce_array (classval);
169 return (CORE_ADDR) value_as_long (call_function_by_hand (function,
170 1, &classval));
171}
172
173int
174lookup_child_selector (char *selname)
175{
176 struct value * function, *selstring;
177
178 if (! target_has_execution)
179 {
d2e6263c 180 /* Can't call into inferior to lookup selector. */
b81654f1
MS
181 return 0;
182 }
183
184 if (lookup_minimal_symbol("sel_getUid", 0, 0))
185 function = find_function_in_inferior("sel_getUid");
186 else if (lookup_minimal_symbol ("sel_get_any_uid", 0, 0))
187 function = find_function_in_inferior("sel_get_any_uid");
188 else
189 {
190 complain (&nosel_lookup_complaint, 0);
191 return 0;
192 }
193
d2e6263c
MS
194 selstring = value_coerce_array (value_string (selname,
195 strlen (selname) + 1));
b81654f1
MS
196 return value_as_long (call_function_by_hand (function, 1, &selstring));
197}
198
199struct value *
200value_nsstring (char *ptr, int len)
201{
202 struct value *stringValue[3];
203 struct value *function, *nsstringValue;
204 struct symbol *sym;
205 struct type *type;
206
207 if (!target_has_execution)
d2e6263c 208 return 0; /* Can't call into inferior to create NSString. */
b81654f1
MS
209
210 if (!(sym = lookup_struct_typedef("NSString", 0, 1)) &&
211 !(sym = lookup_struct_typedef("NXString", 0, 1)))
212 type = lookup_pointer_type(builtin_type_void);
213 else
214 type = lookup_pointer_type(SYMBOL_TYPE (sym));
215
216 stringValue[2] = value_string(ptr, len);
217 stringValue[2] = value_coerce_array(stringValue[2]);
d2e6263c 218 /* _NSNewStringFromCString replaces "istr" after Lantern2A. */
b81654f1
MS
219 if (lookup_minimal_symbol("_NSNewStringFromCString", 0, 0))
220 {
221 function = find_function_in_inferior("_NSNewStringFromCString");
222 nsstringValue = call_function_by_hand(function, 1, &stringValue[2]);
223 }
224 else if (lookup_minimal_symbol("istr", 0, 0))
225 {
226 function = find_function_in_inferior("istr");
227 nsstringValue = call_function_by_hand(function, 1, &stringValue[2]);
228 }
229 else if (lookup_minimal_symbol("+[NSString stringWithCString:]", 0, 0))
230 {
231 function = find_function_in_inferior("+[NSString stringWithCString:]");
232 stringValue[0] = value_from_longest
233 (builtin_type_long, lookup_objc_class ("NSString"));
234 stringValue[1] = value_from_longest
235 (builtin_type_long, lookup_child_selector ("stringWithCString:"));
236 nsstringValue = call_function_by_hand(function, 3, &stringValue[0]);
237 }
238 else
239 error ("NSString: internal error -- no way to create new NSString");
240
241 VALUE_TYPE(nsstringValue) = type;
242 return nsstringValue;
243}
244
d2e6263c 245/* Objective-C name demangling. */
b81654f1
MS
246
247char *
248objc_demangle (const char *mangled)
249{
250 char *demangled, *cp;
251
252 if (mangled[0] == '_' &&
253 (mangled[1] == 'i' || mangled[1] == 'c') &&
254 mangled[2] == '_')
255 {
256 cp = demangled = xmalloc(strlen(mangled) + 2);
257
258 if (mangled[1] == 'i')
259 *cp++ = '-'; /* for instance method */
260 else
261 *cp++ = '+'; /* for class method */
262
263 *cp++ = '['; /* opening left brace */
264 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
265
266 while (*cp && *cp == '_')
267 cp++; /* skip any initial underbars in class name */
268
269 if (!(cp = strchr(cp, '_'))) /* find first non-initial underbar */
270 {
271 free(demangled); /* not mangled name */
272 return NULL;
273 }
274 if (cp[1] == '_') { /* easy case: no category name */
275 *cp++ = ' '; /* replace two '_' with one ' ' */
276 strcpy(cp, mangled + (cp - demangled) + 2);
277 }
278 else {
279 *cp++ = '('; /* less easy case: category name */
280 if (!(cp = strchr(cp, '_')))
281 {
282 free(demangled); /* not mangled name */
283 return NULL;
284 }
285 *cp++ = ')';
d2e6263c 286 *cp++ = ' '; /* overwriting 1st char of method name... */
b81654f1
MS
287 strcpy(cp, mangled + (cp - demangled)); /* get it back */
288 }
289
290 while (*cp && *cp == '_')
291 cp++; /* skip any initial underbars in method name */
292
293 for (; *cp; cp++)
294 if (*cp == '_')
295 *cp = ':'; /* replace remaining '_' with ':' */
296
297 *cp++ = ']'; /* closing right brace */
298 *cp++ = 0; /* string terminator */
299 return demangled;
300 }
301 else
d2e6263c 302 return NULL; /* Not an objc mangled name. */
b81654f1
MS
303}
304
d2e6263c
MS
305/* Print the character C on STREAM as part of the contents of a
306 literal string whose delimiter is QUOTER. Note that that format
307 for printing characters and strings is language specific. */
b81654f1
MS
308
309static void
310objc_emit_char (register int c, struct ui_file *stream, int quoter)
311{
312
d2e6263c 313 c &= 0xFF; /* Avoid sign bit follies. */
b81654f1
MS
314
315 if (PRINT_LITERAL_FORM (c))
316 {
317 if (c == '\\' || c == quoter)
318 {
319 fputs_filtered ("\\", stream);
320 }
321 fprintf_filtered (stream, "%c", c);
322 }
323 else
324 {
325 switch (c)
326 {
327 case '\n':
328 fputs_filtered ("\\n", stream);
329 break;
330 case '\b':
331 fputs_filtered ("\\b", stream);
332 break;
333 case '\t':
334 fputs_filtered ("\\t", stream);
335 break;
336 case '\f':
337 fputs_filtered ("\\f", stream);
338 break;
339 case '\r':
340 fputs_filtered ("\\r", stream);
341 break;
342 case '\033':
343 fputs_filtered ("\\e", stream);
344 break;
345 case '\007':
346 fputs_filtered ("\\a", stream);
347 break;
348 default:
349 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
350 break;
351 }
352 }
353}
354
355static void
356objc_printchar (int c, struct ui_file *stream)
357{
358 fputs_filtered ("'", stream);
359 objc_emit_char (c, stream, '\'');
360 fputs_filtered ("'", stream);
361}
362
d2e6263c
MS
363/* Print the character string STRING, printing at most LENGTH
364 characters. Printing stops early if the number hits print_max;
365 repeat counts are printed as appropriate. Print ellipses at the
366 end if we had to stop before printing LENGTH characters, or if
367 FORCE_ELLIPSES. */
b81654f1
MS
368
369static void
d2e6263c
MS
370objc_printstr (struct ui_file *stream, char *string,
371 unsigned int length, int force_ellipses)
b81654f1
MS
372{
373 register unsigned int i;
374 unsigned int things_printed = 0;
375 int in_quotes = 0;
376 int need_comma = 0;
377 extern int inspect_it;
378 extern int repeat_count_threshold;
379 extern int print_max;
380
381 /* If the string was not truncated due to `set print elements', and
d2e6263c
MS
382 the last byte of it is a null, we don't print that, in
383 traditional C style. */
b81654f1
MS
384 if ((!force_ellipses) && length > 0 && string[length-1] == '\0')
385 length--;
386
387 if (length == 0)
388 {
389 fputs_filtered ("\"\"", stream);
390 return;
391 }
392
393 for (i = 0; i < length && things_printed < print_max; ++i)
394 {
d2e6263c
MS
395 /* Position of the character we are examining to see whether it
396 is repeated. */
b81654f1
MS
397 unsigned int rep1;
398 /* Number of repetitions we have detected so far. */
399 unsigned int reps;
400
401 QUIT;
402
403 if (need_comma)
404 {
405 fputs_filtered (", ", stream);
406 need_comma = 0;
407 }
408
409 rep1 = i + 1;
410 reps = 1;
411 while (rep1 < length && string[rep1] == string[i])
412 {
413 ++rep1;
414 ++reps;
415 }
416
417 if (reps > repeat_count_threshold)
418 {
419 if (in_quotes)
420 {
421 if (inspect_it)
422 fputs_filtered ("\\\", ", stream);
423 else
424 fputs_filtered ("\", ", stream);
425 in_quotes = 0;
426 }
427 objc_printchar (string[i], stream);
428 fprintf_filtered (stream, " <repeats %u times>", reps);
429 i = rep1 - 1;
430 things_printed += repeat_count_threshold;
431 need_comma = 1;
432 }
433 else
434 {
435 if (!in_quotes)
436 {
437 if (inspect_it)
438 fputs_filtered ("\\\"", stream);
439 else
440 fputs_filtered ("\"", stream);
441 in_quotes = 1;
442 }
443 objc_emit_char (string[i], stream, '"');
444 ++things_printed;
445 }
446 }
447
448 /* Terminate the quotes if necessary. */
449 if (in_quotes)
450 {
451 if (inspect_it)
452 fputs_filtered ("\\\"", stream);
453 else
454 fputs_filtered ("\"", stream);
455 }
456
457 if (force_ellipses || i < length)
458 fputs_filtered ("...", stream);
459}
460
d2e6263c
MS
461/* Create a fundamental C type using default reasonable for the
462 current target.
463
464 Some object/debugging file formats (DWARF version 1, COFF, etc) do
465 not define fundamental types such as "int" or "double". Others
466 (stabs or DWARF version 2, etc) do define fundamental types. For
467 the formats which don't provide fundamental types, gdb can create
468 such types using this function.
469
470 FIXME: Some compilers distinguish explicitly signed integral types
471 (signed short, signed int, signed long) from "regular" integral
472 types (short, int, long) in the debugging information. There is
473 some disagreement as to how useful this feature is. In particular,
474 gcc does not support this. Also, only some debugging formats allow
475 the distinction to be passed on to a debugger. For now, we always
476 just use "short", "int", or "long" as the type name, for both the
477 implicit and explicitly signed types. This also makes life easier
478 for the gdb test suite since we don't have to account for the
479 differences in output depending upon what the compiler and
480 debugging format support. We will probably have to re-examine the
481 issue when gdb starts taking it's fundamental type information
482 directly from the debugging information supplied by the compiler.
483 fnf@cygnus.com */
b81654f1
MS
484
485static struct type *
486objc_create_fundamental_type (struct objfile *objfile, int typeid)
487{
488 register struct type *type = NULL;
489
490 switch (typeid)
491 {
492 default:
d2e6263c
MS
493 /* FIXME: For now, if we are asked to produce a type not in
494 this language, create the equivalent of a C integer type
495 with the name "<?type?>". When all the dust settles from
496 the type reconstruction work, this should probably become
497 an error. */
b81654f1
MS
498 type = init_type (TYPE_CODE_INT,
499 TARGET_INT_BIT / TARGET_CHAR_BIT,
500 0, "<?type?>", objfile);
501 warning ("internal error: no C/C++ fundamental type %d", typeid);
502 break;
503 case FT_VOID:
504 type = init_type (TYPE_CODE_VOID,
505 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
506 0, "void", objfile);
507 break;
508 case FT_CHAR:
509 type = init_type (TYPE_CODE_INT,
510 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
511 0, "char", objfile);
512 break;
513 case FT_SIGNED_CHAR:
514 type = init_type (TYPE_CODE_INT,
515 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
516 0, "signed char", objfile);
517 break;
518 case FT_UNSIGNED_CHAR:
519 type = init_type (TYPE_CODE_INT,
520 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
521 TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
522 break;
523 case FT_SHORT:
524 type = init_type (TYPE_CODE_INT,
525 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
526 0, "short", objfile);
527 break;
528 case FT_SIGNED_SHORT:
529 type = init_type (TYPE_CODE_INT,
530 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
531 0, "short", objfile); /* FIXME-fnf */
532 break;
533 case FT_UNSIGNED_SHORT:
534 type = init_type (TYPE_CODE_INT,
535 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
536 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
537 break;
538 case FT_INTEGER:
539 type = init_type (TYPE_CODE_INT,
540 TARGET_INT_BIT / TARGET_CHAR_BIT,
541 0, "int", objfile);
542 break;
543 case FT_SIGNED_INTEGER:
544 type = init_type (TYPE_CODE_INT,
545 TARGET_INT_BIT / TARGET_CHAR_BIT,
546 0, "int", objfile); /* FIXME -fnf */
547 break;
548 case FT_UNSIGNED_INTEGER:
549 type = init_type (TYPE_CODE_INT,
550 TARGET_INT_BIT / TARGET_CHAR_BIT,
551 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
552 break;
553 case FT_LONG:
554 type = init_type (TYPE_CODE_INT,
555 TARGET_LONG_BIT / TARGET_CHAR_BIT,
556 0, "long", objfile);
557 break;
558 case FT_SIGNED_LONG:
559 type = init_type (TYPE_CODE_INT,
560 TARGET_LONG_BIT / TARGET_CHAR_BIT,
561 0, "long", objfile); /* FIXME -fnf */
562 break;
563 case FT_UNSIGNED_LONG:
564 type = init_type (TYPE_CODE_INT,
565 TARGET_LONG_BIT / TARGET_CHAR_BIT,
566 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
567 break;
568 case FT_LONG_LONG:
569 type = init_type (TYPE_CODE_INT,
570 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
571 0, "long long", objfile);
572 break;
573 case FT_SIGNED_LONG_LONG:
574 type = init_type (TYPE_CODE_INT,
575 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
576 0, "signed long long", objfile);
577 break;
578 case FT_UNSIGNED_LONG_LONG:
579 type = init_type (TYPE_CODE_INT,
580 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
581 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
582 break;
583 case FT_FLOAT:
584 type = init_type (TYPE_CODE_FLT,
585 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
586 0, "float", objfile);
587 break;
588 case FT_DBL_PREC_FLOAT:
589 type = init_type (TYPE_CODE_FLT,
590 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
591 0, "double", objfile);
592 break;
593 case FT_EXT_PREC_FLOAT:
594 type = init_type (TYPE_CODE_FLT,
595 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
596 0, "long double", objfile);
597 break;
598 }
599 return (type);
600}
601
602
603/* Table mapping opcodes into strings for printing operators
604 and precedences of the operators. */
605
606static const struct op_print objc_op_print_tab[] =
607 {
608 {",", BINOP_COMMA, PREC_COMMA, 0},
609 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
610 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
611 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
612 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
613 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
614 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
615 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
616 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
617 {"<=", BINOP_LEQ, PREC_ORDER, 0},
618 {">=", BINOP_GEQ, PREC_ORDER, 0},
619 {">", BINOP_GTR, PREC_ORDER, 0},
620 {"<", BINOP_LESS, PREC_ORDER, 0},
621 {">>", BINOP_RSH, PREC_SHIFT, 0},
622 {"<<", BINOP_LSH, PREC_SHIFT, 0},
623 {"+", BINOP_ADD, PREC_ADD, 0},
624 {"-", BINOP_SUB, PREC_ADD, 0},
625 {"*", BINOP_MUL, PREC_MUL, 0},
626 {"/", BINOP_DIV, PREC_MUL, 0},
627 {"%", BINOP_REM, PREC_MUL, 0},
628 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
629 {"-", UNOP_NEG, PREC_PREFIX, 0},
630 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
631 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
632 {"*", UNOP_IND, PREC_PREFIX, 0},
633 {"&", UNOP_ADDR, PREC_PREFIX, 0},
634 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
635 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
636 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
637 {NULL, 0, 0, 0}
638};
639
640struct type ** const (objc_builtin_types[]) =
641{
642 &builtin_type_int,
643 &builtin_type_long,
644 &builtin_type_short,
645 &builtin_type_char,
646 &builtin_type_float,
647 &builtin_type_double,
648 &builtin_type_void,
649 &builtin_type_long_long,
650 &builtin_type_signed_char,
651 &builtin_type_unsigned_char,
652 &builtin_type_unsigned_short,
653 &builtin_type_unsigned_int,
654 &builtin_type_unsigned_long,
655 &builtin_type_unsigned_long_long,
656 &builtin_type_long_double,
657 &builtin_type_complex,
658 &builtin_type_double_complex,
659 0
660};
661
662const struct language_defn objc_language_defn = {
d2e6263c 663 "objective-c", /* Language name */
b81654f1
MS
664 language_objc,
665 objc_builtin_types,
666 range_check_off,
667 type_check_off,
668 case_sensitive_on,
669 objc_parse,
670 objc_error,
671 evaluate_subexp_standard,
672 objc_printchar, /* Print a character constant */
673 objc_printstr, /* Function to print string constant */
674 objc_emit_char,
675 objc_create_fundamental_type, /* Create fundamental type in this language */
676 c_print_type, /* Print a type using appropriate syntax */
677 c_val_print, /* Print a value using appropriate syntax */
678 c_value_print, /* Print a top-level value */
679 {"", "", "", ""}, /* Binary format info */
680 {"0%lo", "0", "o", ""}, /* Octal format info */
681 {"%ld", "", "d", ""}, /* Decimal format info */
682 {"0x%lx", "0x", "x", ""}, /* Hex format info */
d2e6263c
MS
683 objc_op_print_tab, /* Expression operators for printing */
684 1, /* C-style arrays */
b81654f1
MS
685 0, /* String lower bound */
686 &builtin_type_char, /* Type of string elements */
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{
709 register struct selname *new =
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);
742 free(msglist_sel);
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{
755 register int val = msglist_len;
756 register struct selname *sel = selname_chain;
757 register char *p = msglist_sel;
758 int selid;
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)
765 error("Can't find selector \"%s\"", p);
766 write_exp_elt_longcst (selid);
767 free(p);
d2e6263c 768 write_exp_elt_longcst (val); /* Number of args */
b81654f1
MS
769 free(sel);
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
781int specialcmp(char *a, char *b)
782{
783 while (*a && *a != ' ' && *a != ']' && *b && *b != ' ' && *b != ']')
784 {
785 if (*a != *b)
786 return *a - *b;
787 a++, b++;
788 }
789 if (*a && *a != ' ' && *a != ']')
790 return 1; /* a is longer therefore greater */
791 if (*b && *b != ' ' && *b != ']')
792 return -1; /* a is shorter therefore lesser */
793 return 0; /* a and b are identical */
794}
795
796/*
797 * Function: compare_selectors (void *, void *)
798 *
d2e6263c
MS
799 * Comparison function for use with qsort. Arguments are symbols or
800 * msymbols Compares selector part of objc method name alphabetically.
b81654f1
MS
801 */
802
803static int
d2e6263c 804compare_selectors (void *a, void *b)
b81654f1
MS
805{
806 char *aname, *bname;
807
808 if ((aname = SYMBOL_SOURCE_NAME (*(struct symbol **) a)) == NULL ||
809 (bname = SYMBOL_SOURCE_NAME (*(struct symbol **) b)) == NULL)
810 error ("internal: compare_selectors(1)");
811
812 if ((aname = strchr(aname, ' ')) == NULL ||
813 (bname = strchr(bname, ' ')) == NULL)
814 error ("internal: compare_selectors(2)");
815
816 return specialcmp (aname+1, bname+1);
817}
818
819/*
820 * Function: selectors_info (regexp, from_tty)
821 *
d2e6263c
MS
822 * Implements the "Info selectors" command. Takes an optional regexp
823 * arg. Lists all objective c selectors that match the regexp. Works
824 * by grepping thru all symbols for objective c methods. Output list
825 * is sorted and uniqued.
b81654f1
MS
826 */
827
828static void
829selectors_info (char *regexp, int from_tty)
830{
831 struct objfile *objfile;
832 struct minimal_symbol *msymbol;
833 char *name;
834 char *val;
835 int matches = 0;
836 int maxlen = 0;
837 int ix;
838 char myregexp[2048];
839 char asel[256];
840 struct symbol **sym_arr;
841 int plusminus = 0;
842
843 if (regexp == NULL)
d2e6263c 844 strcpy(myregexp, ".*]"); /* Null input, match all objc methods. */
b81654f1
MS
845 else
846 {
d2e6263c
MS
847 if (*regexp == '+' || *regexp == '-')
848 { /* User wants only class methods or only instance methods. */
b81654f1
MS
849 plusminus = *regexp++;
850 while (*regexp == ' ' || *regexp == '\t')
851 regexp++;
852 }
853 if (*regexp == '\0')
854 strcpy(myregexp, ".*]");
855 else
856 {
857 strcpy(myregexp, regexp);
858 if (myregexp[strlen(myregexp) - 1] == '$') /* end of selector */
859 myregexp[strlen(myregexp) - 1] = ']'; /* end of method name */
860 else
861 strcat(myregexp, ".*]");
862 }
863 }
864
865 if (regexp != NULL)
866 if (0 != (val = re_comp (myregexp)))
867 error ("Invalid regexp (%s): %s", val, regexp);
868
d2e6263c 869 /* First time thru is JUST to get max length and count. */
b81654f1
MS
870 ALL_MSYMBOLS (objfile, msymbol)
871 {
872 QUIT;
873 if ((name = SYMBOL_DEMANGLED_NAME (msymbol)) == NULL)
874 name = SYMBOL_NAME (msymbol);
875 if (name &&
876 (name[0] == '-' || name[0] == '+') &&
d2e6263c 877 name[1] == '[') /* Got a method name. */
b81654f1 878 {
d2e6263c 879 /* Filter for class/instance methods. */
b81654f1 880 if (plusminus && name[0] != plusminus)
d2e6263c
MS
881 continue;
882 /* Find selector part. */
883 name = (char *) strchr(name+2, ' ');
b81654f1
MS
884 if (regexp == NULL || re_exec(++name) != 0)
885 {
886 char *mystart = name;
887 char *myend = (char *) strchr(mystart, ']');
888
889 if (myend && (myend - mystart > maxlen))
d2e6263c 890 maxlen = myend - mystart; /* Get longest selector. */
b81654f1
MS
891 matches++;
892 }
893 }
894 }
895 if (matches)
896 {
897 printf_filtered ("Selectors matching \"%s\":\n\n",
898 regexp ? regexp : "*");
899
900 sym_arr = alloca (matches * sizeof (struct symbol *));
901 matches = 0;
902 ALL_MSYMBOLS (objfile, msymbol)
903 {
904 QUIT;
905 if ((name = SYMBOL_DEMANGLED_NAME (msymbol)) == NULL)
906 name = SYMBOL_NAME (msymbol);
907 if (name &&
908 (name[0] == '-' || name[0] == '+') &&
d2e6263c 909 name[1] == '[') /* Got a method name. */
b81654f1 910 {
d2e6263c 911 /* Filter for class/instance methods. */
b81654f1 912 if (plusminus && name[0] != plusminus)
d2e6263c
MS
913 continue;
914 /* Find selector part. */
915 name = (char *) strchr(name+2, ' ');
b81654f1
MS
916 if (regexp == NULL || re_exec(++name) != 0)
917 sym_arr[matches++] = (struct symbol *) msymbol;
918 }
919 }
920
921 qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
922 compare_selectors);
d2e6263c
MS
923 /* Prevent compare on first iteration. */
924 asel[0] = 0;
925 for (ix = 0; ix < matches; ix++) /* Now do the output. */
b81654f1
MS
926 {
927 char *p = asel;
928
929 QUIT;
930 if ((name = SYMBOL_DEMANGLED_NAME (sym_arr[ix])) == NULL)
931 name = SYMBOL_NAME (sym_arr[ix]);
932 name = strchr (name, ' ') + 1;
933 if (p[0] && specialcmp(name, p) == 0)
d2e6263c 934 continue; /* Seen this one already (not unique). */
b81654f1 935
d2e6263c
MS
936 /* Copy selector part. */
937 while (*name && *name != ']')
b81654f1
MS
938 *p++ = *name++;
939 *p++ = '\0';
d2e6263c
MS
940 /* Print in columns. */
941 puts_filtered_tabular(asel, maxlen + 1, 0);
b81654f1
MS
942 }
943 begin_line();
944 }
945 else
946 printf_filtered ("No selectors matching \"%s\"\n", regexp ? regexp : "*");
947}
948
949/*
950 * Function: compare_classes (void *, void *)
951 *
d2e6263c
MS
952 * Comparison function for use with qsort. Arguments are symbols or
953 * msymbols Compares class part of objc method name alphabetically.
b81654f1
MS
954 */
955
956static int
d2e6263c 957compare_classes (void *a, void *b)
b81654f1
MS
958{
959 char *aname, *bname;
960
961 if ((aname = SYMBOL_SOURCE_NAME (*(struct symbol **) a)) == NULL ||
962 (bname = SYMBOL_SOURCE_NAME (*(struct symbol **) b)) == NULL)
963 error ("internal: compare_classes(1)");
964
965 return specialcmp (aname+1, bname+1);
966}
967
968/*
969 * Function: classes_info(regexp, from_tty)
970 *
971 * Implements the "info classes" command for objective c classes.
972 * Lists all objective c classes that match the optional regexp.
d2e6263c
MS
973 * Works by grepping thru the list of objective c methods. List will
974 * be sorted and uniqued (since one class may have many methods).
975 * BUGS: will not list a class that has no methods.
b81654f1
MS
976 */
977
978static void
979classes_info (char *regexp, int from_tty)
980{
981 struct objfile *objfile;
982 struct minimal_symbol *msymbol;
983 char *name;
984 char *val;
985 int matches = 0;
986 int maxlen = 0;
987 int ix;
988 char myregexp[2048];
989 char aclass[256];
990 struct symbol **sym_arr;
991
992 if (regexp == NULL)
d2e6263c 993 strcpy(myregexp, ".* "); /* Null input: match all objc classes. */
b81654f1
MS
994 else
995 {
996 strcpy(myregexp, regexp);
997 if (myregexp[strlen(myregexp) - 1] == '$')
d2e6263c 998 /* In the method name, the end of the class name is marked by ' '. */
b81654f1
MS
999 myregexp[strlen(myregexp) - 1] = ' ';
1000 else
1001 strcat(myregexp, ".* ");
1002 }
1003
1004 if (regexp != NULL)
1005 if (0 != (val = re_comp (myregexp)))
1006 error ("Invalid regexp (%s): %s", val, regexp);
1007
d2e6263c 1008 /* First time thru is JUST to get max length and count. */
b81654f1
MS
1009 ALL_MSYMBOLS (objfile, msymbol)
1010 {
1011 QUIT;
1012 if ((name = SYMBOL_DEMANGLED_NAME (msymbol)) == NULL)
1013 name = SYMBOL_NAME (msymbol);
1014 if (name &&
1015 (name[0] == '-' || name[0] == '+') &&
d2e6263c 1016 name[1] == '[') /* Got a method name. */
b81654f1
MS
1017 if (regexp == NULL || re_exec(name+2) != 0)
1018 {
d2e6263c
MS
1019 /* Compute length of classname part. */
1020 char *mystart = name + 2;
b81654f1
MS
1021 char *myend = (char *) strchr(mystart, ' ');
1022
1023 if (myend && (myend - mystart > maxlen))
1024 maxlen = myend - mystart;
1025 matches++;
1026 }
1027 }
1028 if (matches)
1029 {
1030 printf_filtered ("Classes matching \"%s\":\n\n",
1031 regexp ? regexp : "*");
1032 sym_arr = alloca (matches * sizeof (struct symbol *));
1033 matches = 0;
1034 ALL_MSYMBOLS (objfile, msymbol)
1035 {
1036 QUIT;
1037 if ((name = SYMBOL_DEMANGLED_NAME (msymbol)) == NULL)
1038 name = SYMBOL_NAME (msymbol);
1039 if (name &&
1040 (name[0] == '-' || name[0] == '+') &&
d2e6263c 1041 name[1] == '[') /* Got a method name. */
b81654f1
MS
1042 if (regexp == NULL || re_exec(name+2) != 0)
1043 sym_arr[matches++] = (struct symbol *) msymbol;
1044 }
1045
1046 qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
1047 compare_classes);
d2e6263c
MS
1048 /* Prevent compare on first iteration. */
1049 aclass[0] = 0;
1050 for (ix = 0; ix < matches; ix++) /* Now do the output. */
b81654f1
MS
1051 {
1052 char *p = aclass;
1053
1054 QUIT;
1055 if ((name = SYMBOL_DEMANGLED_NAME (sym_arr[ix])) == NULL)
1056 name = SYMBOL_NAME (sym_arr[ix]);
1057 name += 2;
1058 if (p[0] && specialcmp(name, p) == 0)
d2e6263c 1059 continue; /* Seen this one already (not unique). */
b81654f1 1060
d2e6263c
MS
1061 /* Copy class part of method name. */
1062 while (*name && *name != ' ')
b81654f1
MS
1063 *p++ = *name++;
1064 *p++ = '\0';
d2e6263c
MS
1065 /* Print in columns. */
1066 puts_filtered_tabular(aclass, maxlen + 1, 0);
b81654f1
MS
1067 }
1068 begin_line();
1069 }
1070 else
1071 printf_filtered ("No classes matching \"%s\"\n", regexp ? regexp : "*");
1072}
1073
1074/*
1075 * Function: find_imps (char *selector, struct symbol **sym_arr)
1076 *
1077 * Input: a string representing a selector
1078 * a pointer to an array of symbol pointers
1079 * possibly a pointer to a symbol found by the caller.
1080 *
d2e6263c
MS
1081 * Output: number of methods that implement that selector. Side
1082 * effects: The array of symbol pointers is filled with matching syms.
b81654f1 1083 *
d2e6263c
MS
1084 * By analogy with function "find_methods" (symtab.c), builds a list
1085 * of symbols matching the ambiguous input, so that "decode_line_2"
1086 * (symtab.c) can list them and ask the user to choose one or more.
1087 * In this case the matches are objective c methods
1088 * ("implementations") matching an objective c selector.
b81654f1 1089 *
d2e6263c
MS
1090 * Note that it is possible for a normal (c-style) function to have
1091 * the same name as an objective c selector. To prevent the selector
1092 * from eclipsing the function, we allow the caller (decode_line_1) to
1093 * search for such a function first, and if it finds one, pass it in
1094 * to us. We will then integrate it into the list. We also search
1095 * for one here, among the minsyms.
b81654f1 1096 *
d2e6263c
MS
1097 * NOTE: if NUM_DEBUGGABLE is non-zero, the sym_arr will be divided
1098 * into two parts: debuggable (struct symbol) syms, and
1099 * non_debuggable (struct minimal_symbol) syms. The debuggable
1100 * ones will come first, before NUM_DEBUGGABLE (which will thus
1101 * be the index of the first non-debuggable one).
b81654f1
MS
1102 */
1103
1104/*
1105 * Function: total_number_of_imps (char *selector);
1106 *
1107 * Input: a string representing a selector
1108 * Output: number of methods that implement that selector.
1109 *
d2e6263c 1110 * By analogy with function "total_number_of_methods", this allows
b81654f1 1111 * decode_line_1 (symtab.c) to detect if there are objective c methods
d2e6263c
MS
1112 * matching the input, and to allocate an array of pointers to them
1113 * which can be manipulated by "decode_line_2" (also in symtab.c).
b81654f1
MS
1114 */
1115
1116char *
1117parse_selector (char *method, char **selector)
1118{
1119 char *s1 = NULL;
1120 char *s2 = NULL;
1121 int found_quote = 0;
1122
1123 char *nselector = NULL;
1124
1125 CHECK (selector != NULL);
1126
1127 s1 = method;
1128
1129 while (isspace (*s1))
1130 s1++;
1131 if (*s1 == '\'')
1132 {
1133 found_quote = 1;
1134 s1++;
1135 }
1136 while (isspace (*s1))
1137 s1++;
1138
1139 nselector = s1;
1140 s2 = s1;
1141
1142 for (;;) {
1143 if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
1144 *s1++ = *s2;
1145 else if (isspace (*s2))
1146 ;
1147 else if ((*s2 == '\0') || (*s2 == '\''))
1148 break;
1149 else
1150 return NULL;
1151 s2++;
1152 }
1153 *s1++ = '\0';
1154
1155 while (isspace (*s2))
1156 s2++;
1157 if (found_quote)
1158 {
1159 if (*s2 == '\'')
1160 s2++;
1161 while (isspace (*s2))
1162 s2++;
1163 }
1164
1165 if (selector != NULL)
1166 *selector = nselector;
1167
1168 return s2;
1169}
1170
1171char *
d2e6263c
MS
1172parse_method (char *method, char *type, char **class,
1173 char **category, char **selector)
b81654f1
MS
1174{
1175 char *s1 = NULL;
1176 char *s2 = NULL;
1177 int found_quote = 0;
1178
1179 char ntype = '\0';
1180 char *nclass = NULL;
1181 char *ncategory = NULL;
1182 char *nselector = NULL;
1183
1184 CHECK (type != NULL);
1185 CHECK (class != NULL);
1186 CHECK (category != NULL);
1187 CHECK (selector != NULL);
1188
1189 s1 = method;
1190
1191 while (isspace (*s1))
1192 s1++;
1193 if (*s1 == '\'')
1194 {
1195 found_quote = 1;
1196 s1++;
1197 }
1198 while (isspace (*s1))
1199 s1++;
1200
1201 if ((s1[0] == '+') || (s1[0] == '-'))
1202 ntype = *s1++;
1203
1204 while (isspace (*s1))
1205 s1++;
1206
1207 if (*s1 != '[')
1208 return NULL;
1209 s1++;
1210
1211 nclass = s1;
1212 while (isalnum (*s1) || (*s1 == '_'))
1213 s1++;
1214
1215 s2 = s1;
1216 while (isspace (*s2))
1217 s2++;
1218
1219 if (*s2 == '(')
1220 {
1221 s2++;
1222 while (isspace (*s2))
1223 s2++;
1224 ncategory = s2;
1225 while (isalnum (*s2) || (*s2 == '_'))
1226 s2++;
1227 *s2++ = '\0';
1228 }
1229
d2e6263c 1230 /* Truncate the class name now that we're not using the open paren. */
b81654f1
MS
1231 *s1++ = '\0';
1232
1233 nselector = s2;
1234 s1 = s2;
1235
1236 for (;;) {
1237 if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
1238 *s1++ = *s2;
1239 else if (isspace (*s2))
1240 ;
1241 else if (*s2 == ']')
1242 break;
1243 else
1244 return NULL;
1245 s2++;
1246 }
1247 *s1++ = '\0';
1248 s2++;
1249
1250 while (isspace (*s2))
1251 s2++;
1252 if (found_quote)
1253 {
1254 if (*s2 != '\'')
1255 return NULL;
1256 s2++;
1257 while (isspace (*s2))
1258 s2++;
1259 }
1260
1261 if (type != NULL)
1262 *type = ntype;
1263 if (class != NULL)
1264 *class = nclass;
1265 if (category != NULL)
1266 *category = ncategory;
1267 if (selector != NULL)
1268 *selector = nselector;
1269
1270 return s2;
1271}
1272
1273void
d2e6263c
MS
1274find_methods (struct symtab *symtab, char type,
1275 const char *class, const char *category,
1276 const char *selector, struct symbol **syms,
1277 unsigned int *nsym, unsigned int *ndebug)
b81654f1
MS
1278{
1279 struct objfile *objfile = NULL;
1280 struct minimal_symbol *msymbol = NULL;
1281 struct block *block = NULL;
1282 struct symbol *sym = NULL;
1283
1284 char *symname = NULL;
1285
1286 char ntype = '\0';
1287 char *nclass = NULL;
1288 char *ncategory = NULL;
1289 char *nselector = NULL;
1290
1291 unsigned int csym = 0;
1292 unsigned int cdebug = 0;
1293
1294 static char *tmp = NULL;
1295 static unsigned int tmplen = 0;
1296
1297 CHECK (nsym != NULL);
1298 CHECK (ndebug != NULL);
1299
1300 if (symtab)
1301 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1302
1303 ALL_MSYMBOLS (objfile, msymbol)
1304 {
1305 QUIT;
1306
1307 if ((msymbol->type != mst_text) && (msymbol->type != mst_file_text))
d2e6263c 1308 /* Not a function or method. */
b81654f1
MS
1309 continue;
1310
1311 if (symtab)
1312 if ((SYMBOL_VALUE_ADDRESS (msymbol) < block->startaddr) ||
1313 (SYMBOL_VALUE_ADDRESS (msymbol) >= block->endaddr))
d2e6263c 1314 /* Not in the specified symtab. */
b81654f1
MS
1315 continue;
1316
1317 symname = SYMBOL_DEMANGLED_NAME (msymbol);
1318 if (symname == NULL)
1319 symname = SYMBOL_NAME (msymbol);
1320 if (symname == NULL)
1321 continue;
1322
1323 if ((symname[0] != '-' && symname[0] != '+') || (symname[1] != '['))
d2e6263c 1324 /* Not a method name. */
b81654f1
MS
1325 continue;
1326
1327 while ((strlen (symname) + 1) >= tmplen)
1328 {
1329 tmplen = (tmplen == 0) ? 1024 : tmplen * 2;
1330 tmp = xrealloc (tmp, tmplen);
1331 }
1332 strcpy (tmp, symname);
1333
1334 if (parse_method (tmp, &ntype, &nclass, &ncategory, &nselector) == NULL)
1335 continue;
1336
1337 if ((type != '\0') && (ntype != type))
1338 continue;
1339
d2e6263c
MS
1340 if ((class != NULL)
1341 && ((nclass == NULL) || (strcmp (class, nclass) != 0)))
b81654f1
MS
1342 continue;
1343
d2e6263c
MS
1344 if ((category != NULL) &&
1345 ((ncategory == NULL) || (strcmp (category, ncategory) != 0)))
b81654f1
MS
1346 continue;
1347
d2e6263c
MS
1348 if ((selector != NULL) &&
1349 ((nselector == NULL) || (strcmp (selector, nselector) != 0)))
b81654f1
MS
1350 continue;
1351
1352 sym = find_pc_function (SYMBOL_VALUE_ADDRESS (msymbol));
1353 if (sym != NULL)
1354 {
1355 const char *newsymname = SYMBOL_DEMANGLED_NAME (sym);
1356
1357 if (newsymname == NULL)
1358 newsymname = SYMBOL_NAME (sym);
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
1414 CHECK (nsym != NULL);
1415 CHECK (ndebug != NULL);
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
1428 struct symtab *sym_symtab = NULL;
1429 struct symbol *sym = NULL;
1430 struct minimal_symbol *msym = NULL;
1431
1432 strcpy (buf, method);
1433 tmp = parse_selector (buf, &selector);
1434
1435 if (tmp == NULL)
1436 return NULL;
1437
1438 sym = lookup_symbol (selector, block, VAR_NAMESPACE, 0, &sym_symtab);
1439 if (sym != NULL)
1440 {
1441 if (syms)
1442 syms[csym] = sym;
1443 csym++;
1444 cdebug++;
1445 }
1446
1447 if (sym == NULL)
1448 msym = lookup_minimal_symbol (selector, 0, 0);
1449
1450 if (msym != NULL)
1451 {
1452 if (syms)
1453 syms[csym] = msym;
1454 csym++;
1455 }
1456 }
1457
1458 if (syms != NULL)
d2e6263c
MS
1459 find_methods (symtab, type, class, category, selector,
1460 syms + csym, &ncsym, &ncdebug);
b81654f1 1461 else
d2e6263c
MS
1462 find_methods (symtab, type, class, category, selector,
1463 NULL, &ncsym, &ncdebug);
b81654f1 1464
d2e6263c 1465 /* If we didn't find any methods, just return. */
b81654f1
MS
1466 if (ncsym == 0 && ncdebug == 0)
1467 return method;
1468
1469 /* Take debug symbols from the second batch of symbols and swap them
1470 * with debug symbols from the first batch. Repeat until either the
1471 * second section is out of debug symbols or the first section is
1472 * full of debug symbols. Either way we have all debug symbols
d2e6263c
MS
1473 * packed to the beginning of the buffer.
1474 */
b81654f1
MS
1475
1476 if (syms != NULL)
1477 {
1478 while ((cdebug < csym) && (ncdebug > 0))
1479 {
1480 struct symbol *s = NULL;
d2e6263c
MS
1481 /* First non-debugging symbol. */
1482 unsigned int i = cdebug;
1483 /* Last of second batch of debug symbols. */
1484 unsigned int j = csym + ncdebug - 1;
b81654f1
MS
1485
1486 s = syms[j];
1487 syms[j] = syms[i];
1488 syms[i] = s;
1489
d2e6263c
MS
1490 /* We've moved a symbol from the second debug section to the
1491 first one. */
b81654f1
MS
1492 cdebug++;
1493 ncdebug--;
1494 }
1495 }
1496
1497 csym += ncsym;
1498 cdebug += ncdebug;
1499
1500 if (nsym != NULL)
1501 *nsym = csym;
1502 if (ndebug != NULL)
1503 *ndebug = cdebug;
1504
1505 if (syms == NULL)
1506 return method + (tmp - buf);
1507
1508 if (csym > 1)
1509 {
d2e6263c 1510 /* Sort debuggable symbols. */
b81654f1 1511 if (cdebug > 1)
d2e6263c
MS
1512 qsort (syms, cdebug, sizeof (struct minimal_symbol *),
1513 compare_classes);
b81654f1 1514
d2e6263c 1515 /* Sort minimal_symbols. */
b81654f1 1516 if ((csym - cdebug) > 1)
d2e6263c
MS
1517 qsort (&syms[cdebug], csym - cdebug,
1518 sizeof (struct minimal_symbol *), compare_classes);
b81654f1 1519 }
d2e6263c
MS
1520 /* Terminate the sym_arr list. */
1521 syms[csym] = 0;
b81654f1
MS
1522
1523 return method + (tmp - buf);
1524}
1525
1526void
1527print_object_command (char *args, int from_tty)
1528{
1529 struct value *object, *function, *description;
1530 CORE_ADDR string_addr;
1531 int i = 0;
1532 char c = -1;
1533
1534 if (!args || !*args)
d2e6263c
MS
1535 error (
1536"The 'print-object' command requires an argument (an Objective-C object)");
b81654f1
MS
1537
1538 {
1539 struct expression *expr = parse_expression (args);
d2e6263c
MS
1540 register struct cleanup *old_chain =
1541 make_cleanup (free_current_contents, &expr);
b81654f1
MS
1542 int pc = 0;
1543
1544#if 1
1545 object = expr->language_defn->evaluate_exp (builtin_type_void_data_ptr,
1546 expr, &pc, EVAL_NORMAL);
1547#else
d2e6263c
MS
1548 object = evaluate_subexp (builtin_type_void_data_ptr,
1549 expr, &pc, EVAL_NORMAL);
b81654f1
MS
1550#endif
1551 do_cleanups (old_chain);
1552 }
1553
1554 if (!(function = find_function_in_inferior ("_NSPrintForDebugger")))
1555 error ("Unable to locate _NSPrintForDebugger in child process");
1556
1557 description = call_function_by_hand (function, 1, &object);
1558
1559 if ((string_addr = value_as_long (description)) == 0)
1560 error ("object returns null description");
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
1571 printf_filtered("<object returns empty description>");
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
MS
1582 /* Return instance method to be called. */
1583 CORE_ADDR (*stop_at) (CORE_ADDR);
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
1613void
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 {
1655 CORE_ADDR (*f) (CORE_ADDR, CORE_ADDR *);
1656 CORE_ADDR pc;
1657 CORE_ADDR *new_pc;
1658};
1659
1660int
1661find_objc_msgcall_submethod_helper (PTR arg)
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
1672int
d2e6263c
MS
1673find_objc_msgcall_submethod (CORE_ADDR (*f) (CORE_ADDR, CORE_ADDR *),
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,
d2e6263c
MS
1684 (PTR) &s,
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 ();
1698 if (new_pc != NULL) { *new_pc = 0; }
1699
d2e6263c
MS
1700 for (i = 0; i < nmethcalls; i++)
1701 if ((pc >= methcalls[i].begin) && (pc < methcalls[i].end))
1702 {
1703 if (methcalls[i].stop_at != NULL)
1704 return find_objc_msgcall_submethod (methcalls[i].stop_at,
1705 pc, new_pc);
1706 else
1707 return 0;
b81654f1 1708 }
d2e6263c 1709
b81654f1
MS
1710 return 0;
1711}
1712
1713void
1714_initialize_objc_language (void)
1715{
1716 add_language (&objc_language_defn);
d2e6263c
MS
1717 add_info ("selectors", selectors_info, /* INFO SELECTORS command. */
1718 "All Objective-C selectors, or those matching REGEXP.");
1719 add_info ("classes", classes_info, /* INFO CLASSES command. */
1720 "All Objective-C classes, or those matching REGEXP.");
b81654f1
MS
1721 add_com ("print-object", class_vars, print_object_command,
1722 "Ask an Objective-C object to print itself.\n");
1723 add_com_alias ("po", "print-object", class_vars, 1);
1724}
1725
1726#if defined (__powerpc__) || defined (__ppc__)
1727static unsigned long FETCH_ARGUMENT (int i)
1728{
1729 return read_register (3 + i);
1730}
1731#elif defined (__i386__)
1732static unsigned long FETCH_ARGUMENT (int i)
1733{
1734 CORE_ADDR stack = read_register (SP_REGNUM);
1735 return read_memory_unsigned_integer (stack + (4 * (i + 1)), 4);
1736}
1737#elif defined (__sparc__)
1738static unsigned long FETCH_ARGUMENT (int i)
1739{
1740 return read_register (O0_REGNUM + i);
1741}
1742#elif defined (__hppa__) || defined (__hppa)
1743static unsigned long FETCH_ARGUMENT (int i)
1744{
1745 return read_register (R0_REGNUM + 26 - i);
1746}
1747#else
1748#error unknown architecture
1749#endif
1750
1751#if defined (__hppa__) || defined (__hppa)
1752static CORE_ADDR CONVERT_FUNCPTR (CORE_ADDR pc)
1753{
d2e6263c 1754 if (pc & 0x2)
b81654f1 1755 pc = (CORE_ADDR) read_memory_integer (pc & ~0x3, 4);
d2e6263c 1756
b81654f1
MS
1757 return pc;
1758}
1759#else
1760static CORE_ADDR CONVERT_FUNCPTR (CORE_ADDR pc)
1761{
1762 return pc;
1763}
1764#endif
1765
1766static void
1767read_objc_method (CORE_ADDR addr, struct objc_method *method)
1768{
d2e6263c 1769 method->name = read_memory_unsigned_integer (addr + 0, 4);
b81654f1 1770 method->types = read_memory_unsigned_integer (addr + 4, 4);
d2e6263c 1771 method->imp = read_memory_unsigned_integer (addr + 8, 4);
b81654f1
MS
1772}
1773
1774static
1775unsigned long read_objc_methlist_nmethods (CORE_ADDR addr)
1776{
1777 return read_memory_unsigned_integer (addr + 4, 4);
1778}
1779
1780static void
1781read_objc_methlist_method (CORE_ADDR addr, unsigned long num,
1782 struct objc_method *method)
1783{
1784 CHECK_FATAL (num < read_objc_methlist_nmethods (addr));
1785 read_objc_method (addr + 8 + (12 * num), method);
1786}
1787
1788static void
1789read_objc_object (CORE_ADDR addr, struct objc_object *object)
1790{
1791 object->isa = read_memory_unsigned_integer (addr, 4);
1792}
1793
1794static void
1795read_objc_super (CORE_ADDR addr, struct objc_super *super)
1796{
1797 super->receiver = read_memory_unsigned_integer (addr, 4);
1798 super->class = read_memory_unsigned_integer (addr + 4, 4);
1799};
1800
1801static void
1802read_objc_class (CORE_ADDR addr, struct objc_class *class)
1803{
1804 class->isa = read_memory_unsigned_integer (addr, 4);
1805 class->super_class = read_memory_unsigned_integer (addr + 4, 4);
1806 class->name = read_memory_unsigned_integer (addr + 8, 4);
1807 class->version = read_memory_unsigned_integer (addr + 12, 4);
1808 class->info = read_memory_unsigned_integer (addr + 16, 4);
1809 class->instance_size = read_memory_unsigned_integer (addr + 18, 4);
1810 class->ivars = read_memory_unsigned_integer (addr + 24, 4);
1811 class->methods = read_memory_unsigned_integer (addr + 28, 4);
1812 class->cache = read_memory_unsigned_integer (addr + 32, 4);
1813 class->protocols = read_memory_unsigned_integer (addr + 36, 4);
1814}
1815
1816CORE_ADDR
1817find_implementation_from_class (CORE_ADDR class, CORE_ADDR sel)
1818{
1819 CORE_ADDR subclass = class;
1820
d2e6263c
MS
1821 while (subclass != 0)
1822 {
b81654f1 1823
d2e6263c
MS
1824 struct objc_class class_str;
1825 unsigned mlistnum = 0;
b81654f1 1826
d2e6263c 1827 read_objc_class (subclass, &class_str);
b81654f1 1828
d2e6263c
MS
1829 for (;;)
1830 {
1831 CORE_ADDR mlist;
1832 unsigned long nmethods;
1833 unsigned long i;
b81654f1 1834
d2e6263c
MS
1835 mlist = read_memory_unsigned_integer (class_str.methods +
1836 (4 * mlistnum), 4);
1837 if (mlist == 0)
1838 break;
b81654f1 1839
d2e6263c 1840 nmethods = read_objc_methlist_nmethods (mlist);
b81654f1 1841
d2e6263c
MS
1842 for (i = 0; i < nmethods; i++)
1843 {
1844 struct objc_method meth_str;
1845 read_objc_methlist_method (mlist, i, &meth_str);
b81654f1
MS
1846
1847#if 0
d2e6263c
MS
1848 fprintf (stderr,
1849 "checking method 0x%lx against selector 0x%lx\n",
1850 meth_str.name, sel);
b81654f1
MS
1851#endif
1852
d2e6263c
MS
1853 if (meth_str.name == sel)
1854 return CONVERT_FUNCPTR (meth_str.imp);
1855 }
1856 mlistnum++;
b81654f1 1857 }
d2e6263c 1858 subclass = class_str.super_class;
b81654f1 1859 }
b81654f1
MS
1860
1861 return 0;
1862}
1863
1864CORE_ADDR
1865find_implementation (CORE_ADDR object, CORE_ADDR sel)
1866{
1867 struct objc_object ostr;
1868
d2e6263c
MS
1869 if (object == 0)
1870 return 0;
b81654f1 1871 read_objc_object (object, &ostr);
d2e6263c
MS
1872 if (ostr.isa == 0)
1873 return 0;
b81654f1
MS
1874
1875 return find_implementation_from_class (ostr.isa, sel);
1876}
1877
1878static int
1879resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc)
1880{
1881 CORE_ADDR object;
1882 CORE_ADDR sel;
1883 CORE_ADDR res;
1884
1885 object = FETCH_ARGUMENT (0);
1886 sel = FETCH_ARGUMENT (1);
1887
1888 res = find_implementation (object, sel);
d2e6263c
MS
1889 if (new_pc != 0)
1890 *new_pc = res;
1891 if (res == 0)
1892 return 1;
b81654f1
MS
1893 return 0;
1894}
1895
1896static int
1897resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
1898{
1899 CORE_ADDR object;
1900 CORE_ADDR sel;
1901 CORE_ADDR res;
1902
1903 object = FETCH_ARGUMENT (1);
1904 sel = FETCH_ARGUMENT (2);
1905
1906 res = find_implementation (object, sel);
d2e6263c
MS
1907 if (new_pc != 0)
1908 *new_pc = res;
1909 if (res == 0)
1910 return 1;
b81654f1
MS
1911 return 0;
1912}
1913
1914static int
1915resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc)
1916{
1917 struct objc_super sstr;
1918
1919 CORE_ADDR super;
1920 CORE_ADDR sel;
1921 CORE_ADDR res;
1922
1923 super = FETCH_ARGUMENT (0);
1924 sel = FETCH_ARGUMENT (1);
1925
1926 read_objc_super (super, &sstr);
d2e6263c
MS
1927 if (sstr.class == 0)
1928 return 0;
b81654f1
MS
1929
1930 res = find_implementation_from_class (sstr.class, sel);
d2e6263c
MS
1931 if (new_pc != 0)
1932 *new_pc = res;
1933 if (res == 0)
1934 return 1;
b81654f1
MS
1935 return 0;
1936}
1937
1938static int
1939resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
1940{
1941 struct objc_super sstr;
1942
1943 CORE_ADDR super;
1944 CORE_ADDR sel;
1945 CORE_ADDR res;
1946
1947 super = FETCH_ARGUMENT (1);
1948 sel = FETCH_ARGUMENT (2);
1949
1950 read_objc_super (super, &sstr);
d2e6263c
MS
1951 if (sstr.class == 0)
1952 return 0;
b81654f1
MS
1953
1954 res = find_implementation_from_class (sstr.class, sel);
d2e6263c
MS
1955 if (new_pc != 0)
1956 *new_pc = res;
1957 if (res == 0)
1958 return 1;
b81654f1
MS
1959 return 0;
1960}
This page took 0.101986 seconds and 4 git commands to generate.