* somsolib.c (dld_cache): Replace boolean by int for field is_valid.
[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"
7248f48e 37#include "gdb_string.h" /* for strchr */
b81654f1
MS
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
72367fb4 77static struct deprecated_complaint noclass_lookup_complaint = {
d2e6263c
MS
78 "no way to lookup Objective-C classes", 0, 0
79};
b81654f1 80
72367fb4 81static struct deprecated_complaint nosel_lookup_complaint = {
d2e6263c
MS
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
7248f48e
AF
269 cp = strchr(cp, '_');
270 if (!cp) /* find first non-initial underbar */
b81654f1 271 {
7248f48e 272 xfree(demangled); /* not mangled name */
b81654f1
MS
273 return NULL;
274 }
275 if (cp[1] == '_') { /* easy case: no category name */
276 *cp++ = ' '; /* replace two '_' with one ' ' */
277 strcpy(cp, mangled + (cp - demangled) + 2);
278 }
279 else {
280 *cp++ = '('; /* less easy case: category name */
7248f48e
AF
281 cp = strchr(cp, '_');
282 if (!cp)
b81654f1 283 {
7248f48e 284 xfree(demangled); /* not mangled name */
b81654f1
MS
285 return NULL;
286 }
287 *cp++ = ')';
d2e6263c 288 *cp++ = ' '; /* overwriting 1st char of method name... */
b81654f1
MS
289 strcpy(cp, mangled + (cp - demangled)); /* get it back */
290 }
291
292 while (*cp && *cp == '_')
293 cp++; /* skip any initial underbars in method name */
294
295 for (; *cp; cp++)
296 if (*cp == '_')
297 *cp = ':'; /* replace remaining '_' with ':' */
298
299 *cp++ = ']'; /* closing right brace */
300 *cp++ = 0; /* string terminator */
301 return demangled;
302 }
303 else
d2e6263c 304 return NULL; /* Not an objc mangled name. */
b81654f1
MS
305}
306
d2e6263c
MS
307/* Print the character C on STREAM as part of the contents of a
308 literal string whose delimiter is QUOTER. Note that that format
309 for printing characters and strings is language specific. */
b81654f1
MS
310
311static void
312objc_emit_char (register int c, struct ui_file *stream, int quoter)
313{
314
d2e6263c 315 c &= 0xFF; /* Avoid sign bit follies. */
b81654f1
MS
316
317 if (PRINT_LITERAL_FORM (c))
318 {
319 if (c == '\\' || c == quoter)
320 {
321 fputs_filtered ("\\", stream);
322 }
323 fprintf_filtered (stream, "%c", c);
324 }
325 else
326 {
327 switch (c)
328 {
329 case '\n':
330 fputs_filtered ("\\n", stream);
331 break;
332 case '\b':
333 fputs_filtered ("\\b", stream);
334 break;
335 case '\t':
336 fputs_filtered ("\\t", stream);
337 break;
338 case '\f':
339 fputs_filtered ("\\f", stream);
340 break;
341 case '\r':
342 fputs_filtered ("\\r", stream);
343 break;
344 case '\033':
345 fputs_filtered ("\\e", stream);
346 break;
347 case '\007':
348 fputs_filtered ("\\a", stream);
349 break;
350 default:
351 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
352 break;
353 }
354 }
355}
356
357static void
358objc_printchar (int c, struct ui_file *stream)
359{
360 fputs_filtered ("'", stream);
361 objc_emit_char (c, stream, '\'');
362 fputs_filtered ("'", stream);
363}
364
d2e6263c
MS
365/* Print the character string STRING, printing at most LENGTH
366 characters. Printing stops early if the number hits print_max;
367 repeat counts are printed as appropriate. Print ellipses at the
368 end if we had to stop before printing LENGTH characters, or if
369 FORCE_ELLIPSES. */
b81654f1
MS
370
371static void
d2e6263c
MS
372objc_printstr (struct ui_file *stream, char *string,
373 unsigned int length, int force_ellipses)
b81654f1
MS
374{
375 register unsigned int i;
376 unsigned int things_printed = 0;
377 int in_quotes = 0;
378 int need_comma = 0;
379 extern int inspect_it;
380 extern int repeat_count_threshold;
381 extern int print_max;
382
383 /* If the string was not truncated due to `set print elements', and
d2e6263c
MS
384 the last byte of it is a null, we don't print that, in
385 traditional C style. */
b81654f1
MS
386 if ((!force_ellipses) && length > 0 && string[length-1] == '\0')
387 length--;
388
389 if (length == 0)
390 {
391 fputs_filtered ("\"\"", stream);
392 return;
393 }
394
395 for (i = 0; i < length && things_printed < print_max; ++i)
396 {
d2e6263c
MS
397 /* Position of the character we are examining to see whether it
398 is repeated. */
b81654f1
MS
399 unsigned int rep1;
400 /* Number of repetitions we have detected so far. */
401 unsigned int reps;
402
403 QUIT;
404
405 if (need_comma)
406 {
407 fputs_filtered (", ", stream);
408 need_comma = 0;
409 }
410
411 rep1 = i + 1;
412 reps = 1;
413 while (rep1 < length && string[rep1] == string[i])
414 {
415 ++rep1;
416 ++reps;
417 }
418
419 if (reps > repeat_count_threshold)
420 {
421 if (in_quotes)
422 {
423 if (inspect_it)
424 fputs_filtered ("\\\", ", stream);
425 else
426 fputs_filtered ("\", ", stream);
427 in_quotes = 0;
428 }
429 objc_printchar (string[i], stream);
430 fprintf_filtered (stream, " <repeats %u times>", reps);
431 i = rep1 - 1;
432 things_printed += repeat_count_threshold;
433 need_comma = 1;
434 }
435 else
436 {
437 if (!in_quotes)
438 {
439 if (inspect_it)
440 fputs_filtered ("\\\"", stream);
441 else
442 fputs_filtered ("\"", stream);
443 in_quotes = 1;
444 }
445 objc_emit_char (string[i], stream, '"');
446 ++things_printed;
447 }
448 }
449
450 /* Terminate the quotes if necessary. */
451 if (in_quotes)
452 {
453 if (inspect_it)
454 fputs_filtered ("\\\"", stream);
455 else
456 fputs_filtered ("\"", stream);
457 }
458
459 if (force_ellipses || i < length)
460 fputs_filtered ("...", stream);
461}
462
d2e6263c
MS
463/* Create a fundamental C type using default reasonable for the
464 current target.
465
466 Some object/debugging file formats (DWARF version 1, COFF, etc) do
467 not define fundamental types such as "int" or "double". Others
468 (stabs or DWARF version 2, etc) do define fundamental types. For
469 the formats which don't provide fundamental types, gdb can create
470 such types using this function.
471
472 FIXME: Some compilers distinguish explicitly signed integral types
473 (signed short, signed int, signed long) from "regular" integral
474 types (short, int, long) in the debugging information. There is
475 some disagreement as to how useful this feature is. In particular,
476 gcc does not support this. Also, only some debugging formats allow
477 the distinction to be passed on to a debugger. For now, we always
478 just use "short", "int", or "long" as the type name, for both the
479 implicit and explicitly signed types. This also makes life easier
480 for the gdb test suite since we don't have to account for the
481 differences in output depending upon what the compiler and
482 debugging format support. We will probably have to re-examine the
483 issue when gdb starts taking it's fundamental type information
484 directly from the debugging information supplied by the compiler.
485 fnf@cygnus.com */
b81654f1
MS
486
487static struct type *
488objc_create_fundamental_type (struct objfile *objfile, int typeid)
489{
490 register struct type *type = NULL;
491
492 switch (typeid)
493 {
494 default:
d2e6263c
MS
495 /* FIXME: For now, if we are asked to produce a type not in
496 this language, create the equivalent of a C integer type
497 with the name "<?type?>". When all the dust settles from
498 the type reconstruction work, this should probably become
499 an error. */
b81654f1
MS
500 type = init_type (TYPE_CODE_INT,
501 TARGET_INT_BIT / TARGET_CHAR_BIT,
502 0, "<?type?>", objfile);
503 warning ("internal error: no C/C++ fundamental type %d", typeid);
504 break;
505 case FT_VOID:
506 type = init_type (TYPE_CODE_VOID,
507 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
508 0, "void", objfile);
509 break;
510 case FT_CHAR:
511 type = init_type (TYPE_CODE_INT,
512 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
513 0, "char", objfile);
514 break;
515 case FT_SIGNED_CHAR:
516 type = init_type (TYPE_CODE_INT,
517 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
518 0, "signed char", objfile);
519 break;
520 case FT_UNSIGNED_CHAR:
521 type = init_type (TYPE_CODE_INT,
522 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
523 TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
524 break;
525 case FT_SHORT:
526 type = init_type (TYPE_CODE_INT,
527 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
528 0, "short", objfile);
529 break;
530 case FT_SIGNED_SHORT:
531 type = init_type (TYPE_CODE_INT,
532 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
533 0, "short", objfile); /* FIXME-fnf */
534 break;
535 case FT_UNSIGNED_SHORT:
536 type = init_type (TYPE_CODE_INT,
537 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
538 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
539 break;
540 case FT_INTEGER:
541 type = init_type (TYPE_CODE_INT,
542 TARGET_INT_BIT / TARGET_CHAR_BIT,
543 0, "int", objfile);
544 break;
545 case FT_SIGNED_INTEGER:
546 type = init_type (TYPE_CODE_INT,
547 TARGET_INT_BIT / TARGET_CHAR_BIT,
548 0, "int", objfile); /* FIXME -fnf */
549 break;
550 case FT_UNSIGNED_INTEGER:
551 type = init_type (TYPE_CODE_INT,
552 TARGET_INT_BIT / TARGET_CHAR_BIT,
553 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
554 break;
555 case FT_LONG:
556 type = init_type (TYPE_CODE_INT,
557 TARGET_LONG_BIT / TARGET_CHAR_BIT,
558 0, "long", objfile);
559 break;
560 case FT_SIGNED_LONG:
561 type = init_type (TYPE_CODE_INT,
562 TARGET_LONG_BIT / TARGET_CHAR_BIT,
563 0, "long", objfile); /* FIXME -fnf */
564 break;
565 case FT_UNSIGNED_LONG:
566 type = init_type (TYPE_CODE_INT,
567 TARGET_LONG_BIT / TARGET_CHAR_BIT,
568 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
569 break;
570 case FT_LONG_LONG:
571 type = init_type (TYPE_CODE_INT,
572 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
573 0, "long long", objfile);
574 break;
575 case FT_SIGNED_LONG_LONG:
576 type = init_type (TYPE_CODE_INT,
577 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
578 0, "signed long long", objfile);
579 break;
580 case FT_UNSIGNED_LONG_LONG:
581 type = init_type (TYPE_CODE_INT,
582 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
583 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
584 break;
585 case FT_FLOAT:
586 type = init_type (TYPE_CODE_FLT,
587 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
588 0, "float", objfile);
589 break;
590 case FT_DBL_PREC_FLOAT:
591 type = init_type (TYPE_CODE_FLT,
592 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
593 0, "double", objfile);
594 break;
595 case FT_EXT_PREC_FLOAT:
596 type = init_type (TYPE_CODE_FLT,
597 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
598 0, "long double", objfile);
599 break;
600 }
601 return (type);
602}
603
604
605/* Table mapping opcodes into strings for printing operators
606 and precedences of the operators. */
607
608static const struct op_print objc_op_print_tab[] =
609 {
610 {",", BINOP_COMMA, PREC_COMMA, 0},
611 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
612 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
613 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
614 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
615 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
616 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
617 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
618 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
619 {"<=", BINOP_LEQ, PREC_ORDER, 0},
620 {">=", BINOP_GEQ, PREC_ORDER, 0},
621 {">", BINOP_GTR, PREC_ORDER, 0},
622 {"<", BINOP_LESS, PREC_ORDER, 0},
623 {">>", BINOP_RSH, PREC_SHIFT, 0},
624 {"<<", BINOP_LSH, PREC_SHIFT, 0},
625 {"+", BINOP_ADD, PREC_ADD, 0},
626 {"-", BINOP_SUB, PREC_ADD, 0},
627 {"*", BINOP_MUL, PREC_MUL, 0},
628 {"/", BINOP_DIV, PREC_MUL, 0},
629 {"%", BINOP_REM, PREC_MUL, 0},
630 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
631 {"-", UNOP_NEG, PREC_PREFIX, 0},
632 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
633 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
634 {"*", UNOP_IND, PREC_PREFIX, 0},
635 {"&", UNOP_ADDR, PREC_PREFIX, 0},
636 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
637 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
638 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
639 {NULL, 0, 0, 0}
640};
641
642struct type ** const (objc_builtin_types[]) =
643{
644 &builtin_type_int,
645 &builtin_type_long,
646 &builtin_type_short,
647 &builtin_type_char,
648 &builtin_type_float,
649 &builtin_type_double,
650 &builtin_type_void,
651 &builtin_type_long_long,
652 &builtin_type_signed_char,
653 &builtin_type_unsigned_char,
654 &builtin_type_unsigned_short,
655 &builtin_type_unsigned_int,
656 &builtin_type_unsigned_long,
657 &builtin_type_unsigned_long_long,
658 &builtin_type_long_double,
659 &builtin_type_complex,
660 &builtin_type_double_complex,
661 0
662};
663
664const struct language_defn objc_language_defn = {
d2e6263c 665 "objective-c", /* Language name */
b81654f1
MS
666 language_objc,
667 objc_builtin_types,
668 range_check_off,
669 type_check_off,
670 case_sensitive_on,
671 objc_parse,
672 objc_error,
673 evaluate_subexp_standard,
674 objc_printchar, /* Print a character constant */
675 objc_printstr, /* Function to print string constant */
676 objc_emit_char,
677 objc_create_fundamental_type, /* Create fundamental type in this language */
678 c_print_type, /* Print a type using appropriate syntax */
679 c_val_print, /* Print a value using appropriate syntax */
680 c_value_print, /* Print a top-level value */
681 {"", "", "", ""}, /* Binary format info */
682 {"0%lo", "0", "o", ""}, /* Octal format info */
683 {"%ld", "", "d", ""}, /* Decimal format info */
684 {"0x%lx", "0x", "x", ""}, /* Hex format info */
d2e6263c
MS
685 objc_op_print_tab, /* Expression operators for printing */
686 1, /* C-style arrays */
b81654f1
MS
687 0, /* String lower bound */
688 &builtin_type_char, /* Type of string elements */
689 LANG_MAGIC
690};
691
692/*
693 * ObjC:
d2e6263c 694 * Following functions help construct Objective-C message calls
b81654f1
MS
695 */
696
d2e6263c 697struct selname /* For parsing Objective-C. */
b81654f1
MS
698 {
699 struct selname *next;
700 char *msglist_sel;
701 int msglist_len;
702 };
703
704static int msglist_len;
705static struct selname *selname_chain;
706static char *msglist_sel;
707
708void
709start_msglist(void)
710{
711 register struct selname *new =
712 (struct selname *) xmalloc (sizeof (struct selname));
713
714 new->next = selname_chain;
715 new->msglist_len = msglist_len;
716 new->msglist_sel = msglist_sel;
717 msglist_len = 0;
718 msglist_sel = (char *)xmalloc(1);
719 *msglist_sel = 0;
720 selname_chain = new;
721}
722
723void
724add_msglist(struct stoken *str, int addcolon)
725{
726 char *s, *p;
727 int len, plen;
728
d2e6263c
MS
729 if (str == 0) { /* Unnamed arg, or... */
730 if (addcolon == 0) { /* variable number of args. */
b81654f1
MS
731 msglist_len++;
732 return;
733 }
734 p = "";
735 plen = 0;
736 } else {
737 p = str->ptr;
738 plen = str->length;
739 }
740 len = plen + strlen(msglist_sel) + 2;
741 s = (char *)xmalloc(len);
742 strcpy(s, msglist_sel);
743 strncat(s, p, plen);
7248f48e 744 xfree(msglist_sel);
b81654f1
MS
745 msglist_sel = s;
746 if (addcolon) {
747 s[len-2] = ':';
748 s[len-1] = 0;
749 msglist_len++;
750 } else
751 s[len-2] = '\0';
752}
753
754int
755end_msglist(void)
756{
757 register int val = msglist_len;
758 register struct selname *sel = selname_chain;
759 register char *p = msglist_sel;
760 int selid;
761
762 selname_chain = sel->next;
763 msglist_len = sel->msglist_len;
764 msglist_sel = sel->msglist_sel;
765 selid = lookup_child_selector(p);
766 if (!selid)
767 error("Can't find selector \"%s\"", p);
768 write_exp_elt_longcst (selid);
7248f48e 769 xfree(p);
d2e6263c 770 write_exp_elt_longcst (val); /* Number of args */
7248f48e 771 xfree(sel);
b81654f1
MS
772
773 return val;
774}
775
776/*
777 * Function: specialcmp (char *a, char *b)
778 *
779 * Special strcmp: treats ']' and ' ' as end-of-string.
d2e6263c 780 * Used for qsorting lists of objc methods (either by class or selector).
b81654f1
MS
781 */
782
783int specialcmp(char *a, char *b)
784{
785 while (*a && *a != ' ' && *a != ']' && *b && *b != ' ' && *b != ']')
786 {
787 if (*a != *b)
788 return *a - *b;
789 a++, b++;
790 }
791 if (*a && *a != ' ' && *a != ']')
792 return 1; /* a is longer therefore greater */
793 if (*b && *b != ' ' && *b != ']')
794 return -1; /* a is shorter therefore lesser */
795 return 0; /* a and b are identical */
796}
797
798/*
799 * Function: compare_selectors (void *, void *)
800 *
d2e6263c
MS
801 * Comparison function for use with qsort. Arguments are symbols or
802 * msymbols Compares selector part of objc method name alphabetically.
b81654f1
MS
803 */
804
805static int
d2e6263c 806compare_selectors (void *a, void *b)
b81654f1
MS
807{
808 char *aname, *bname;
809
7248f48e
AF
810 aname = SYMBOL_SOURCE_NAME (*(struct symbol **) a);
811 bname = SYMBOL_SOURCE_NAME (*(struct symbol **) b);
812 if (aname == NULL || bname == NULL)
b81654f1
MS
813 error ("internal: compare_selectors(1)");
814
7248f48e
AF
815 aname = strchr(aname, ' ');
816 bname = strchr(bname, ' ');
817 if (aname == NULL || bname == NULL)
b81654f1
MS
818 error ("internal: compare_selectors(2)");
819
820 return specialcmp (aname+1, bname+1);
821}
822
823/*
824 * Function: selectors_info (regexp, from_tty)
825 *
d2e6263c
MS
826 * Implements the "Info selectors" command. Takes an optional regexp
827 * arg. Lists all objective c selectors that match the regexp. Works
828 * by grepping thru all symbols for objective c methods. Output list
829 * is sorted and uniqued.
b81654f1
MS
830 */
831
832static void
833selectors_info (char *regexp, int from_tty)
834{
835 struct objfile *objfile;
836 struct minimal_symbol *msymbol;
837 char *name;
838 char *val;
839 int matches = 0;
840 int maxlen = 0;
841 int ix;
842 char myregexp[2048];
843 char asel[256];
844 struct symbol **sym_arr;
845 int plusminus = 0;
846
847 if (regexp == NULL)
d2e6263c 848 strcpy(myregexp, ".*]"); /* Null input, match all objc methods. */
b81654f1
MS
849 else
850 {
d2e6263c
MS
851 if (*regexp == '+' || *regexp == '-')
852 { /* User wants only class methods or only instance methods. */
b81654f1
MS
853 plusminus = *regexp++;
854 while (*regexp == ' ' || *regexp == '\t')
855 regexp++;
856 }
857 if (*regexp == '\0')
858 strcpy(myregexp, ".*]");
859 else
860 {
861 strcpy(myregexp, regexp);
862 if (myregexp[strlen(myregexp) - 1] == '$') /* end of selector */
863 myregexp[strlen(myregexp) - 1] = ']'; /* end of method name */
864 else
865 strcat(myregexp, ".*]");
866 }
867 }
868
869 if (regexp != NULL)
870 if (0 != (val = re_comp (myregexp)))
871 error ("Invalid regexp (%s): %s", val, regexp);
872
d2e6263c 873 /* First time thru is JUST to get max length and count. */
b81654f1
MS
874 ALL_MSYMBOLS (objfile, msymbol)
875 {
876 QUIT;
7248f48e
AF
877 name = SYMBOL_DEMANGLED_NAME (msymbol);
878 if (name == NULL)
b81654f1
MS
879 name = SYMBOL_NAME (msymbol);
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 {
902 printf_filtered ("Selectors matching \"%s\":\n\n",
903 regexp ? regexp : "*");
904
905 sym_arr = alloca (matches * sizeof (struct symbol *));
906 matches = 0;
907 ALL_MSYMBOLS (objfile, msymbol)
908 {
909 QUIT;
7248f48e
AF
910 name = SYMBOL_DEMANGLED_NAME (msymbol);
911 if (name == NULL)
b81654f1
MS
912 name = SYMBOL_NAME (msymbol);
913 if (name &&
914 (name[0] == '-' || name[0] == '+') &&
d2e6263c 915 name[1] == '[') /* Got a method name. */
b81654f1 916 {
d2e6263c 917 /* Filter for class/instance methods. */
b81654f1 918 if (plusminus && name[0] != plusminus)
d2e6263c
MS
919 continue;
920 /* Find selector part. */
921 name = (char *) strchr(name+2, ' ');
b81654f1
MS
922 if (regexp == NULL || re_exec(++name) != 0)
923 sym_arr[matches++] = (struct symbol *) msymbol;
924 }
925 }
926
927 qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
928 compare_selectors);
d2e6263c
MS
929 /* Prevent compare on first iteration. */
930 asel[0] = 0;
931 for (ix = 0; ix < matches; ix++) /* Now do the output. */
b81654f1
MS
932 {
933 char *p = asel;
934
935 QUIT;
7248f48e
AF
936 name = SYMBOL_DEMANGLED_NAME (sym_arr[ix]);
937 if (name == NULL)
b81654f1
MS
938 name = SYMBOL_NAME (sym_arr[ix]);
939 name = strchr (name, ' ') + 1;
940 if (p[0] && specialcmp(name, p) == 0)
d2e6263c 941 continue; /* Seen this one already (not unique). */
b81654f1 942
d2e6263c
MS
943 /* Copy selector part. */
944 while (*name && *name != ']')
b81654f1
MS
945 *p++ = *name++;
946 *p++ = '\0';
d2e6263c
MS
947 /* Print in columns. */
948 puts_filtered_tabular(asel, maxlen + 1, 0);
b81654f1
MS
949 }
950 begin_line();
951 }
952 else
953 printf_filtered ("No selectors matching \"%s\"\n", regexp ? regexp : "*");
954}
955
956/*
957 * Function: compare_classes (void *, void *)
958 *
d2e6263c
MS
959 * Comparison function for use with qsort. Arguments are symbols or
960 * msymbols Compares class part of objc method name alphabetically.
b81654f1
MS
961 */
962
963static int
d2e6263c 964compare_classes (void *a, void *b)
b81654f1
MS
965{
966 char *aname, *bname;
967
7248f48e
AF
968 aname = SYMBOL_SOURCE_NAME (*(struct symbol **) a);
969 bname = SYMBOL_SOURCE_NAME (*(struct symbol **) b);
970 if (aname == NULL || bname == NULL)
b81654f1
MS
971 error ("internal: compare_classes(1)");
972
973 return specialcmp (aname+1, bname+1);
974}
975
976/*
977 * Function: classes_info(regexp, from_tty)
978 *
979 * Implements the "info classes" command for objective c classes.
980 * Lists all objective c classes that match the optional regexp.
d2e6263c
MS
981 * Works by grepping thru the list of objective c methods. List will
982 * be sorted and uniqued (since one class may have many methods).
983 * BUGS: will not list a class that has no methods.
b81654f1
MS
984 */
985
986static void
987classes_info (char *regexp, int from_tty)
988{
989 struct objfile *objfile;
990 struct minimal_symbol *msymbol;
991 char *name;
992 char *val;
993 int matches = 0;
994 int maxlen = 0;
995 int ix;
996 char myregexp[2048];
997 char aclass[256];
998 struct symbol **sym_arr;
999
1000 if (regexp == NULL)
d2e6263c 1001 strcpy(myregexp, ".* "); /* Null input: match all objc classes. */
b81654f1
MS
1002 else
1003 {
1004 strcpy(myregexp, regexp);
1005 if (myregexp[strlen(myregexp) - 1] == '$')
d2e6263c 1006 /* In the method name, the end of the class name is marked by ' '. */
b81654f1
MS
1007 myregexp[strlen(myregexp) - 1] = ' ';
1008 else
1009 strcat(myregexp, ".* ");
1010 }
1011
1012 if (regexp != NULL)
1013 if (0 != (val = re_comp (myregexp)))
1014 error ("Invalid regexp (%s): %s", val, regexp);
1015
d2e6263c 1016 /* First time thru is JUST to get max length and count. */
b81654f1
MS
1017 ALL_MSYMBOLS (objfile, msymbol)
1018 {
1019 QUIT;
7248f48e
AF
1020 name = SYMBOL_DEMANGLED_NAME (msymbol);
1021 if (name == NULL)
b81654f1
MS
1022 name = SYMBOL_NAME (msymbol);
1023 if (name &&
1024 (name[0] == '-' || name[0] == '+') &&
d2e6263c 1025 name[1] == '[') /* Got a method name. */
b81654f1
MS
1026 if (regexp == NULL || re_exec(name+2) != 0)
1027 {
d2e6263c
MS
1028 /* Compute length of classname part. */
1029 char *mystart = name + 2;
b81654f1
MS
1030 char *myend = (char *) strchr(mystart, ' ');
1031
1032 if (myend && (myend - mystart > maxlen))
1033 maxlen = myend - mystart;
1034 matches++;
1035 }
1036 }
1037 if (matches)
1038 {
1039 printf_filtered ("Classes matching \"%s\":\n\n",
1040 regexp ? regexp : "*");
1041 sym_arr = alloca (matches * sizeof (struct symbol *));
1042 matches = 0;
1043 ALL_MSYMBOLS (objfile, msymbol)
1044 {
1045 QUIT;
7248f48e
AF
1046 name = SYMBOL_DEMANGLED_NAME (msymbol);
1047 if (name == NULL)
b81654f1
MS
1048 name = SYMBOL_NAME (msymbol);
1049 if (name &&
1050 (name[0] == '-' || name[0] == '+') &&
d2e6263c 1051 name[1] == '[') /* Got a method name. */
b81654f1
MS
1052 if (regexp == NULL || re_exec(name+2) != 0)
1053 sym_arr[matches++] = (struct symbol *) msymbol;
1054 }
1055
1056 qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
1057 compare_classes);
d2e6263c
MS
1058 /* Prevent compare on first iteration. */
1059 aclass[0] = 0;
1060 for (ix = 0; ix < matches; ix++) /* Now do the output. */
b81654f1
MS
1061 {
1062 char *p = aclass;
1063
1064 QUIT;
7248f48e
AF
1065 name = SYMBOL_DEMANGLED_NAME (sym_arr[ix]);
1066 if (name == NULL)
b81654f1
MS
1067 name = SYMBOL_NAME (sym_arr[ix]);
1068 name += 2;
1069 if (p[0] && specialcmp(name, p) == 0)
d2e6263c 1070 continue; /* Seen this one already (not unique). */
b81654f1 1071
d2e6263c
MS
1072 /* Copy class part of method name. */
1073 while (*name && *name != ' ')
b81654f1
MS
1074 *p++ = *name++;
1075 *p++ = '\0';
d2e6263c
MS
1076 /* Print in columns. */
1077 puts_filtered_tabular(aclass, maxlen + 1, 0);
b81654f1
MS
1078 }
1079 begin_line();
1080 }
1081 else
1082 printf_filtered ("No classes matching \"%s\"\n", regexp ? regexp : "*");
1083}
1084
1085/*
1086 * Function: find_imps (char *selector, struct symbol **sym_arr)
1087 *
1088 * Input: a string representing a selector
1089 * a pointer to an array of symbol pointers
1090 * possibly a pointer to a symbol found by the caller.
1091 *
d2e6263c
MS
1092 * Output: number of methods that implement that selector. Side
1093 * effects: The array of symbol pointers is filled with matching syms.
b81654f1 1094 *
d2e6263c
MS
1095 * By analogy with function "find_methods" (symtab.c), builds a list
1096 * of symbols matching the ambiguous input, so that "decode_line_2"
1097 * (symtab.c) can list them and ask the user to choose one or more.
1098 * In this case the matches are objective c methods
1099 * ("implementations") matching an objective c selector.
b81654f1 1100 *
d2e6263c
MS
1101 * Note that it is possible for a normal (c-style) function to have
1102 * the same name as an objective c selector. To prevent the selector
1103 * from eclipsing the function, we allow the caller (decode_line_1) to
1104 * search for such a function first, and if it finds one, pass it in
1105 * to us. We will then integrate it into the list. We also search
1106 * for one here, among the minsyms.
b81654f1 1107 *
d2e6263c
MS
1108 * NOTE: if NUM_DEBUGGABLE is non-zero, the sym_arr will be divided
1109 * into two parts: debuggable (struct symbol) syms, and
1110 * non_debuggable (struct minimal_symbol) syms. The debuggable
1111 * ones will come first, before NUM_DEBUGGABLE (which will thus
1112 * be the index of the first non-debuggable one).
b81654f1
MS
1113 */
1114
1115/*
1116 * Function: total_number_of_imps (char *selector);
1117 *
1118 * Input: a string representing a selector
1119 * Output: number of methods that implement that selector.
1120 *
d2e6263c 1121 * By analogy with function "total_number_of_methods", this allows
b81654f1 1122 * decode_line_1 (symtab.c) to detect if there are objective c methods
d2e6263c
MS
1123 * matching the input, and to allocate an array of pointers to them
1124 * which can be manipulated by "decode_line_2" (also in symtab.c).
b81654f1
MS
1125 */
1126
1127char *
1128parse_selector (char *method, char **selector)
1129{
1130 char *s1 = NULL;
1131 char *s2 = NULL;
1132 int found_quote = 0;
1133
1134 char *nselector = NULL;
1135
1136 CHECK (selector != NULL);
1137
1138 s1 = method;
1139
1140 while (isspace (*s1))
1141 s1++;
1142 if (*s1 == '\'')
1143 {
1144 found_quote = 1;
1145 s1++;
1146 }
1147 while (isspace (*s1))
1148 s1++;
1149
1150 nselector = s1;
1151 s2 = s1;
1152
1153 for (;;) {
1154 if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
1155 *s1++ = *s2;
1156 else if (isspace (*s2))
1157 ;
1158 else if ((*s2 == '\0') || (*s2 == '\''))
1159 break;
1160 else
1161 return NULL;
1162 s2++;
1163 }
1164 *s1++ = '\0';
1165
1166 while (isspace (*s2))
1167 s2++;
1168 if (found_quote)
1169 {
1170 if (*s2 == '\'')
1171 s2++;
1172 while (isspace (*s2))
1173 s2++;
1174 }
1175
1176 if (selector != NULL)
1177 *selector = nselector;
1178
1179 return s2;
1180}
1181
1182char *
d2e6263c
MS
1183parse_method (char *method, char *type, char **class,
1184 char **category, char **selector)
b81654f1
MS
1185{
1186 char *s1 = NULL;
1187 char *s2 = NULL;
1188 int found_quote = 0;
1189
1190 char ntype = '\0';
1191 char *nclass = NULL;
1192 char *ncategory = NULL;
1193 char *nselector = NULL;
1194
1195 CHECK (type != NULL);
1196 CHECK (class != NULL);
1197 CHECK (category != NULL);
1198 CHECK (selector != NULL);
1199
1200 s1 = method;
1201
1202 while (isspace (*s1))
1203 s1++;
1204 if (*s1 == '\'')
1205 {
1206 found_quote = 1;
1207 s1++;
1208 }
1209 while (isspace (*s1))
1210 s1++;
1211
1212 if ((s1[0] == '+') || (s1[0] == '-'))
1213 ntype = *s1++;
1214
1215 while (isspace (*s1))
1216 s1++;
1217
1218 if (*s1 != '[')
1219 return NULL;
1220 s1++;
1221
1222 nclass = s1;
1223 while (isalnum (*s1) || (*s1 == '_'))
1224 s1++;
1225
1226 s2 = s1;
1227 while (isspace (*s2))
1228 s2++;
1229
1230 if (*s2 == '(')
1231 {
1232 s2++;
1233 while (isspace (*s2))
1234 s2++;
1235 ncategory = s2;
1236 while (isalnum (*s2) || (*s2 == '_'))
1237 s2++;
1238 *s2++ = '\0';
1239 }
1240
d2e6263c 1241 /* Truncate the class name now that we're not using the open paren. */
b81654f1
MS
1242 *s1++ = '\0';
1243
1244 nselector = s2;
1245 s1 = s2;
1246
1247 for (;;) {
1248 if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
1249 *s1++ = *s2;
1250 else if (isspace (*s2))
1251 ;
1252 else if (*s2 == ']')
1253 break;
1254 else
1255 return NULL;
1256 s2++;
1257 }
1258 *s1++ = '\0';
1259 s2++;
1260
1261 while (isspace (*s2))
1262 s2++;
1263 if (found_quote)
1264 {
1265 if (*s2 != '\'')
1266 return NULL;
1267 s2++;
1268 while (isspace (*s2))
1269 s2++;
1270 }
1271
1272 if (type != NULL)
1273 *type = ntype;
1274 if (class != NULL)
1275 *class = nclass;
1276 if (category != NULL)
1277 *category = ncategory;
1278 if (selector != NULL)
1279 *selector = nselector;
1280
1281 return s2;
1282}
1283
1284void
d2e6263c
MS
1285find_methods (struct symtab *symtab, char type,
1286 const char *class, const char *category,
1287 const char *selector, struct symbol **syms,
1288 unsigned int *nsym, unsigned int *ndebug)
b81654f1
MS
1289{
1290 struct objfile *objfile = NULL;
1291 struct minimal_symbol *msymbol = NULL;
1292 struct block *block = NULL;
1293 struct symbol *sym = NULL;
1294
1295 char *symname = NULL;
1296
1297 char ntype = '\0';
1298 char *nclass = NULL;
1299 char *ncategory = NULL;
1300 char *nselector = NULL;
1301
1302 unsigned int csym = 0;
1303 unsigned int cdebug = 0;
1304
1305 static char *tmp = NULL;
1306 static unsigned int tmplen = 0;
1307
1308 CHECK (nsym != NULL);
1309 CHECK (ndebug != NULL);
1310
1311 if (symtab)
1312 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1313
1314 ALL_MSYMBOLS (objfile, msymbol)
1315 {
1316 QUIT;
1317
1318 if ((msymbol->type != mst_text) && (msymbol->type != mst_file_text))
d2e6263c 1319 /* Not a function or method. */
b81654f1
MS
1320 continue;
1321
1322 if (symtab)
8da065d5
DC
1323 if ((SYMBOL_VALUE_ADDRESS (msymbol) < BLOCK_START (block)) ||
1324 (SYMBOL_VALUE_ADDRESS (msymbol) >= BLOCK_END (block)))
d2e6263c 1325 /* Not in the specified symtab. */
b81654f1
MS
1326 continue;
1327
1328 symname = SYMBOL_DEMANGLED_NAME (msymbol);
1329 if (symname == NULL)
1330 symname = SYMBOL_NAME (msymbol);
1331 if (symname == NULL)
1332 continue;
1333
1334 if ((symname[0] != '-' && symname[0] != '+') || (symname[1] != '['))
d2e6263c 1335 /* Not a method name. */
b81654f1
MS
1336 continue;
1337
1338 while ((strlen (symname) + 1) >= tmplen)
1339 {
1340 tmplen = (tmplen == 0) ? 1024 : tmplen * 2;
1341 tmp = xrealloc (tmp, tmplen);
1342 }
1343 strcpy (tmp, symname);
1344
1345 if (parse_method (tmp, &ntype, &nclass, &ncategory, &nselector) == NULL)
1346 continue;
1347
1348 if ((type != '\0') && (ntype != type))
1349 continue;
1350
d2e6263c
MS
1351 if ((class != NULL)
1352 && ((nclass == NULL) || (strcmp (class, nclass) != 0)))
b81654f1
MS
1353 continue;
1354
d2e6263c
MS
1355 if ((category != NULL) &&
1356 ((ncategory == NULL) || (strcmp (category, ncategory) != 0)))
b81654f1
MS
1357 continue;
1358
d2e6263c
MS
1359 if ((selector != NULL) &&
1360 ((nselector == NULL) || (strcmp (selector, nselector) != 0)))
b81654f1
MS
1361 continue;
1362
1363 sym = find_pc_function (SYMBOL_VALUE_ADDRESS (msymbol));
1364 if (sym != NULL)
1365 {
1366 const char *newsymname = SYMBOL_DEMANGLED_NAME (sym);
1367
1368 if (newsymname == NULL)
1369 newsymname = SYMBOL_NAME (sym);
1370 if (strcmp (symname, newsymname) == 0)
1371 {
d2e6263c
MS
1372 /* Found a high-level method sym: swap it into the
1373 lower part of sym_arr (below num_debuggable). */
b81654f1
MS
1374 if (syms != NULL)
1375 {
1376 syms[csym] = syms[cdebug];
1377 syms[cdebug] = sym;
1378 }
1379 csym++;
1380 cdebug++;
1381 }
1382 else
1383 {
d2e6263c
MS
1384 warning (
1385"debugging symbol \"%s\" does not match minimal symbol (\"%s\"); ignoring",
b81654f1
MS
1386 newsymname, symname);
1387 if (syms != NULL)
1388 syms[csym] = (struct symbol *) msymbol;
1389 csym++;
1390 }
1391 }
1392 else
1393 {
d2e6263c 1394 /* Found a non-debuggable method symbol. */
b81654f1
MS
1395 if (syms != NULL)
1396 syms[csym] = (struct symbol *) msymbol;
1397 csym++;
1398 }
1399 }
1400
1401 if (nsym != NULL)
1402 *nsym = csym;
1403 if (ndebug != NULL)
1404 *ndebug = cdebug;
1405}
1406
1407char *find_imps (struct symtab *symtab, struct block *block,
d2e6263c
MS
1408 char *method, struct symbol **syms,
1409 unsigned int *nsym, unsigned int *ndebug)
b81654f1
MS
1410{
1411 char type = '\0';
1412 char *class = NULL;
1413 char *category = NULL;
1414 char *selector = NULL;
1415
1416 unsigned int csym = 0;
1417 unsigned int cdebug = 0;
1418
1419 unsigned int ncsym = 0;
1420 unsigned int ncdebug = 0;
1421
1422 char *buf = NULL;
1423 char *tmp = NULL;
1424
1425 CHECK (nsym != NULL);
1426 CHECK (ndebug != NULL);
1427
1428 if (nsym != NULL)
1429 *nsym = 0;
1430 if (ndebug != NULL)
1431 *ndebug = 0;
1432
1433 buf = (char *) alloca (strlen (method) + 1);
1434 strcpy (buf, method);
1435 tmp = parse_method (buf, &type, &class, &category, &selector);
1436
1437 if (tmp == NULL) {
1438
1439 struct symtab *sym_symtab = NULL;
1440 struct symbol *sym = NULL;
1441 struct minimal_symbol *msym = NULL;
1442
1443 strcpy (buf, method);
1444 tmp = parse_selector (buf, &selector);
1445
1446 if (tmp == NULL)
1447 return NULL;
1448
1449 sym = lookup_symbol (selector, block, VAR_NAMESPACE, 0, &sym_symtab);
1450 if (sym != NULL)
1451 {
1452 if (syms)
1453 syms[csym] = sym;
1454 csym++;
1455 cdebug++;
1456 }
1457
1458 if (sym == NULL)
1459 msym = lookup_minimal_symbol (selector, 0, 0);
1460
1461 if (msym != NULL)
1462 {
1463 if (syms)
1464 syms[csym] = msym;
1465 csym++;
1466 }
1467 }
1468
1469 if (syms != NULL)
d2e6263c
MS
1470 find_methods (symtab, type, class, category, selector,
1471 syms + csym, &ncsym, &ncdebug);
b81654f1 1472 else
d2e6263c
MS
1473 find_methods (symtab, type, class, category, selector,
1474 NULL, &ncsym, &ncdebug);
b81654f1 1475
d2e6263c 1476 /* If we didn't find any methods, just return. */
b81654f1
MS
1477 if (ncsym == 0 && ncdebug == 0)
1478 return method;
1479
1480 /* Take debug symbols from the second batch of symbols and swap them
1481 * with debug symbols from the first batch. Repeat until either the
1482 * second section is out of debug symbols or the first section is
1483 * full of debug symbols. Either way we have all debug symbols
d2e6263c
MS
1484 * packed to the beginning of the buffer.
1485 */
b81654f1
MS
1486
1487 if (syms != NULL)
1488 {
1489 while ((cdebug < csym) && (ncdebug > 0))
1490 {
1491 struct symbol *s = NULL;
d2e6263c
MS
1492 /* First non-debugging symbol. */
1493 unsigned int i = cdebug;
1494 /* Last of second batch of debug symbols. */
1495 unsigned int j = csym + ncdebug - 1;
b81654f1
MS
1496
1497 s = syms[j];
1498 syms[j] = syms[i];
1499 syms[i] = s;
1500
d2e6263c
MS
1501 /* We've moved a symbol from the second debug section to the
1502 first one. */
b81654f1
MS
1503 cdebug++;
1504 ncdebug--;
1505 }
1506 }
1507
1508 csym += ncsym;
1509 cdebug += ncdebug;
1510
1511 if (nsym != NULL)
1512 *nsym = csym;
1513 if (ndebug != NULL)
1514 *ndebug = cdebug;
1515
1516 if (syms == NULL)
1517 return method + (tmp - buf);
1518
1519 if (csym > 1)
1520 {
d2e6263c 1521 /* Sort debuggable symbols. */
b81654f1 1522 if (cdebug > 1)
d2e6263c
MS
1523 qsort (syms, cdebug, sizeof (struct minimal_symbol *),
1524 compare_classes);
b81654f1 1525
d2e6263c 1526 /* Sort minimal_symbols. */
b81654f1 1527 if ((csym - cdebug) > 1)
d2e6263c
MS
1528 qsort (&syms[cdebug], csym - cdebug,
1529 sizeof (struct minimal_symbol *), compare_classes);
b81654f1 1530 }
d2e6263c
MS
1531 /* Terminate the sym_arr list. */
1532 syms[csym] = 0;
b81654f1
MS
1533
1534 return method + (tmp - buf);
1535}
1536
1537void
1538print_object_command (char *args, int from_tty)
1539{
1540 struct value *object, *function, *description;
1541 CORE_ADDR string_addr;
1542 int i = 0;
1543 char c = -1;
1544
1545 if (!args || !*args)
d2e6263c
MS
1546 error (
1547"The 'print-object' command requires an argument (an Objective-C object)");
b81654f1
MS
1548
1549 {
1550 struct expression *expr = parse_expression (args);
d2e6263c
MS
1551 register struct cleanup *old_chain =
1552 make_cleanup (free_current_contents, &expr);
b81654f1
MS
1553 int pc = 0;
1554
1555#if 1
1556 object = expr->language_defn->evaluate_exp (builtin_type_void_data_ptr,
1557 expr, &pc, EVAL_NORMAL);
1558#else
d2e6263c
MS
1559 object = evaluate_subexp (builtin_type_void_data_ptr,
1560 expr, &pc, EVAL_NORMAL);
b81654f1
MS
1561#endif
1562 do_cleanups (old_chain);
1563 }
1564
7248f48e
AF
1565 function = find_function_in_inferior ("_NSPrintForDebugger");
1566 if (!function)
b81654f1
MS
1567 error ("Unable to locate _NSPrintForDebugger in child process");
1568
1569 description = call_function_by_hand (function, 1, &object);
1570
7248f48e
AF
1571 string_addr = value_as_long (description);
1572 if (string_addr == 0)
b81654f1
MS
1573 error ("object returns null description");
1574
1575 read_memory (string_addr + i++, &c, 1);
1576 if (c != '\0')
1577 do
d2e6263c 1578 { /* Read and print characters up to EOS. */
b81654f1
MS
1579 QUIT;
1580 printf_filtered ("%c", c);
1581 read_memory (string_addr + i++, &c, 1);
1582 } while (c != 0);
1583 else
1584 printf_filtered("<object returns empty description>");
1585 printf_filtered ("\n");
1586}
1587
d2e6263c
MS
1588/* The data structure 'methcalls' is used to detect method calls (thru
1589 * ObjC runtime lib functions objc_msgSend, objc_msgSendSuper, etc.),
1590 * and ultimately find the method being called.
b81654f1
MS
1591 */
1592
1593struct objc_methcall {
1594 char *name;
d2e6263c
MS
1595 /* Return instance method to be called. */
1596 CORE_ADDR (*stop_at) (CORE_ADDR);
1597 /* Start of pc range corresponding to method invocation. */
1598 CORE_ADDR begin;
1599 /* End of pc range corresponding to method invocation. */
1600 CORE_ADDR end;
b81654f1
MS
1601};
1602
d2e6263c
MS
1603static int resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc);
1604static int resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
1605static int resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc);
1606static int resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
b81654f1
MS
1607
1608static struct objc_methcall methcalls[] = {
1609 { "_objc_msgSend", resolve_msgsend, 0, 0},
1610 { "_objc_msgSend_stret", resolve_msgsend_stret, 0, 0},
1611 { "_objc_msgSendSuper", resolve_msgsend_super, 0, 0},
1612 { "_objc_msgSendSuper_stret", resolve_msgsend_super_stret, 0, 0},
1613 { "_objc_getClass", NULL, 0, 0},
1614 { "_objc_getMetaClass", NULL, 0, 0}
1615};
1616
1617#define nmethcalls (sizeof (methcalls) / sizeof (methcalls[0]))
1618
d2e6263c
MS
1619/* The following function, "find_objc_msgsend", fills in the data
1620 * structure "objc_msgs" by finding the addresses of each of the
1621 * (currently four) functions that it holds (of which objc_msgSend is
1622 * the first). This must be called each time symbols are loaded, in
1623 * case the functions have moved for some reason.
b81654f1
MS
1624 */
1625
1626void
1627find_objc_msgsend (void)
1628{
1629 unsigned int i;
1630 for (i = 0; i < nmethcalls; i++) {
1631
1632 struct minimal_symbol *func;
1633
d2e6263c 1634 /* Try both with and without underscore. */
b81654f1
MS
1635 func = lookup_minimal_symbol (methcalls[i].name, NULL, NULL);
1636 if ((func == NULL) && (methcalls[i].name[0] == '_')) {
1637 func = lookup_minimal_symbol (methcalls[i].name + 1, NULL, NULL);
1638 }
1639 if (func == NULL) {
1640 methcalls[i].begin = 0;
1641 methcalls[i].end = 0;
1642 continue;
1643 }
1644
1645 methcalls[i].begin = SYMBOL_VALUE_ADDRESS (func);
1646 do {
1647 methcalls[i].end = SYMBOL_VALUE_ADDRESS (++func);
1648 } while (methcalls[i].begin == methcalls[i].end);
1649 }
1650}
1651
1652/* find_objc_msgcall (replaces pc_off_limits)
1653 *
d2e6263c
MS
1654 * ALL that this function now does is to determine whether the input
1655 * address ("pc") is the address of one of the Objective-C message
b81654f1
MS
1656 * dispatch functions (mainly objc_msgSend or objc_msgSendSuper), and
1657 * if so, it returns the address of the method that will be called.
1658 *
1659 * The old function "pc_off_limits" used to do a lot of other things
d2e6263c 1660 * in addition, such as detecting shared library jump stubs and
b81654f1 1661 * returning the address of the shlib function that would be called.
d2e6263c
MS
1662 * That functionality has been moved into the SKIP_TRAMPOLINE_CODE and
1663 * IN_SOLIB_TRAMPOLINE macros, which are resolved in the target-
1664 * dependent modules.
b81654f1
MS
1665 */
1666
1667struct objc_submethod_helper_data {
1668 CORE_ADDR (*f) (CORE_ADDR, CORE_ADDR *);
1669 CORE_ADDR pc;
1670 CORE_ADDR *new_pc;
1671};
1672
1673int
7248f48e 1674find_objc_msgcall_submethod_helper (void * arg)
b81654f1 1675{
d2e6263c
MS
1676 struct objc_submethod_helper_data *s =
1677 (struct objc_submethod_helper_data *) arg;
1678
1679 if (s->f (s->pc, s->new_pc) == 0)
b81654f1 1680 return 1;
d2e6263c 1681 else
b81654f1 1682 return 0;
b81654f1
MS
1683}
1684
1685int
d2e6263c
MS
1686find_objc_msgcall_submethod (CORE_ADDR (*f) (CORE_ADDR, CORE_ADDR *),
1687 CORE_ADDR pc,
1688 CORE_ADDR *new_pc)
b81654f1
MS
1689{
1690 struct objc_submethod_helper_data s;
1691
1692 s.f = f;
1693 s.pc = pc;
1694 s.new_pc = new_pc;
1695
1696 if (catch_errors (find_objc_msgcall_submethod_helper,
7248f48e 1697 (void *) &s,
d2e6263c
MS
1698 "Unable to determine target of Objective-C method call (ignoring):\n",
1699 RETURN_MASK_ALL) == 0)
b81654f1 1700 return 1;
d2e6263c 1701 else
b81654f1 1702 return 0;
b81654f1
MS
1703}
1704
1705int
1706find_objc_msgcall (CORE_ADDR pc, CORE_ADDR *new_pc)
1707{
1708 unsigned int i;
1709
1710 find_objc_msgsend ();
1711 if (new_pc != NULL) { *new_pc = 0; }
1712
d2e6263c
MS
1713 for (i = 0; i < nmethcalls; i++)
1714 if ((pc >= methcalls[i].begin) && (pc < methcalls[i].end))
1715 {
1716 if (methcalls[i].stop_at != NULL)
1717 return find_objc_msgcall_submethod (methcalls[i].stop_at,
1718 pc, new_pc);
1719 else
1720 return 0;
b81654f1 1721 }
d2e6263c 1722
b81654f1
MS
1723 return 0;
1724}
1725
1726void
1727_initialize_objc_language (void)
1728{
1729 add_language (&objc_language_defn);
d2e6263c
MS
1730 add_info ("selectors", selectors_info, /* INFO SELECTORS command. */
1731 "All Objective-C selectors, or those matching REGEXP.");
1732 add_info ("classes", classes_info, /* INFO CLASSES command. */
1733 "All Objective-C classes, or those matching REGEXP.");
b81654f1
MS
1734 add_com ("print-object", class_vars, print_object_command,
1735 "Ask an Objective-C object to print itself.\n");
1736 add_com_alias ("po", "print-object", class_vars, 1);
1737}
1738
1739#if defined (__powerpc__) || defined (__ppc__)
1740static unsigned long FETCH_ARGUMENT (int i)
1741{
1742 return read_register (3 + i);
1743}
1744#elif defined (__i386__)
1745static unsigned long FETCH_ARGUMENT (int i)
1746{
1747 CORE_ADDR stack = read_register (SP_REGNUM);
1748 return read_memory_unsigned_integer (stack + (4 * (i + 1)), 4);
1749}
1750#elif defined (__sparc__)
1751static unsigned long FETCH_ARGUMENT (int i)
1752{
1753 return read_register (O0_REGNUM + i);
1754}
1755#elif defined (__hppa__) || defined (__hppa)
1756static unsigned long FETCH_ARGUMENT (int i)
1757{
1758 return read_register (R0_REGNUM + 26 - i);
1759}
1760#else
1761#error unknown architecture
1762#endif
1763
1764#if defined (__hppa__) || defined (__hppa)
1765static CORE_ADDR CONVERT_FUNCPTR (CORE_ADDR pc)
1766{
d2e6263c 1767 if (pc & 0x2)
b81654f1 1768 pc = (CORE_ADDR) read_memory_integer (pc & ~0x3, 4);
d2e6263c 1769
b81654f1
MS
1770 return pc;
1771}
1772#else
1773static CORE_ADDR CONVERT_FUNCPTR (CORE_ADDR pc)
1774{
1775 return pc;
1776}
1777#endif
1778
1779static void
1780read_objc_method (CORE_ADDR addr, struct objc_method *method)
1781{
d2e6263c 1782 method->name = read_memory_unsigned_integer (addr + 0, 4);
b81654f1 1783 method->types = read_memory_unsigned_integer (addr + 4, 4);
d2e6263c 1784 method->imp = read_memory_unsigned_integer (addr + 8, 4);
b81654f1
MS
1785}
1786
1787static
1788unsigned long read_objc_methlist_nmethods (CORE_ADDR addr)
1789{
1790 return read_memory_unsigned_integer (addr + 4, 4);
1791}
1792
1793static void
1794read_objc_methlist_method (CORE_ADDR addr, unsigned long num,
1795 struct objc_method *method)
1796{
1797 CHECK_FATAL (num < read_objc_methlist_nmethods (addr));
1798 read_objc_method (addr + 8 + (12 * num), method);
1799}
1800
1801static void
1802read_objc_object (CORE_ADDR addr, struct objc_object *object)
1803{
1804 object->isa = read_memory_unsigned_integer (addr, 4);
1805}
1806
1807static void
1808read_objc_super (CORE_ADDR addr, struct objc_super *super)
1809{
1810 super->receiver = read_memory_unsigned_integer (addr, 4);
1811 super->class = read_memory_unsigned_integer (addr + 4, 4);
1812};
1813
1814static void
1815read_objc_class (CORE_ADDR addr, struct objc_class *class)
1816{
1817 class->isa = read_memory_unsigned_integer (addr, 4);
1818 class->super_class = read_memory_unsigned_integer (addr + 4, 4);
1819 class->name = read_memory_unsigned_integer (addr + 8, 4);
1820 class->version = read_memory_unsigned_integer (addr + 12, 4);
1821 class->info = read_memory_unsigned_integer (addr + 16, 4);
1822 class->instance_size = read_memory_unsigned_integer (addr + 18, 4);
1823 class->ivars = read_memory_unsigned_integer (addr + 24, 4);
1824 class->methods = read_memory_unsigned_integer (addr + 28, 4);
1825 class->cache = read_memory_unsigned_integer (addr + 32, 4);
1826 class->protocols = read_memory_unsigned_integer (addr + 36, 4);
1827}
1828
1829CORE_ADDR
1830find_implementation_from_class (CORE_ADDR class, CORE_ADDR sel)
1831{
1832 CORE_ADDR subclass = class;
1833
d2e6263c
MS
1834 while (subclass != 0)
1835 {
b81654f1 1836
d2e6263c
MS
1837 struct objc_class class_str;
1838 unsigned mlistnum = 0;
b81654f1 1839
d2e6263c 1840 read_objc_class (subclass, &class_str);
b81654f1 1841
d2e6263c
MS
1842 for (;;)
1843 {
1844 CORE_ADDR mlist;
1845 unsigned long nmethods;
1846 unsigned long i;
b81654f1 1847
d2e6263c
MS
1848 mlist = read_memory_unsigned_integer (class_str.methods +
1849 (4 * mlistnum), 4);
1850 if (mlist == 0)
1851 break;
b81654f1 1852
d2e6263c 1853 nmethods = read_objc_methlist_nmethods (mlist);
b81654f1 1854
d2e6263c
MS
1855 for (i = 0; i < nmethods; i++)
1856 {
1857 struct objc_method meth_str;
1858 read_objc_methlist_method (mlist, i, &meth_str);
b81654f1
MS
1859
1860#if 0
d2e6263c
MS
1861 fprintf (stderr,
1862 "checking method 0x%lx against selector 0x%lx\n",
1863 meth_str.name, sel);
b81654f1
MS
1864#endif
1865
d2e6263c
MS
1866 if (meth_str.name == sel)
1867 return CONVERT_FUNCPTR (meth_str.imp);
1868 }
1869 mlistnum++;
b81654f1 1870 }
d2e6263c 1871 subclass = class_str.super_class;
b81654f1 1872 }
b81654f1
MS
1873
1874 return 0;
1875}
1876
1877CORE_ADDR
1878find_implementation (CORE_ADDR object, CORE_ADDR sel)
1879{
1880 struct objc_object ostr;
1881
d2e6263c
MS
1882 if (object == 0)
1883 return 0;
b81654f1 1884 read_objc_object (object, &ostr);
d2e6263c
MS
1885 if (ostr.isa == 0)
1886 return 0;
b81654f1
MS
1887
1888 return find_implementation_from_class (ostr.isa, sel);
1889}
1890
1891static int
1892resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc)
1893{
1894 CORE_ADDR object;
1895 CORE_ADDR sel;
1896 CORE_ADDR res;
1897
1898 object = FETCH_ARGUMENT (0);
1899 sel = FETCH_ARGUMENT (1);
1900
1901 res = find_implementation (object, sel);
d2e6263c
MS
1902 if (new_pc != 0)
1903 *new_pc = res;
1904 if (res == 0)
1905 return 1;
b81654f1
MS
1906 return 0;
1907}
1908
1909static int
1910resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
1911{
1912 CORE_ADDR object;
1913 CORE_ADDR sel;
1914 CORE_ADDR res;
1915
1916 object = FETCH_ARGUMENT (1);
1917 sel = FETCH_ARGUMENT (2);
1918
1919 res = find_implementation (object, sel);
d2e6263c
MS
1920 if (new_pc != 0)
1921 *new_pc = res;
1922 if (res == 0)
1923 return 1;
b81654f1
MS
1924 return 0;
1925}
1926
1927static int
1928resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc)
1929{
1930 struct objc_super sstr;
1931
1932 CORE_ADDR super;
1933 CORE_ADDR sel;
1934 CORE_ADDR res;
1935
1936 super = FETCH_ARGUMENT (0);
1937 sel = FETCH_ARGUMENT (1);
1938
1939 read_objc_super (super, &sstr);
d2e6263c
MS
1940 if (sstr.class == 0)
1941 return 0;
b81654f1
MS
1942
1943 res = find_implementation_from_class (sstr.class, sel);
d2e6263c
MS
1944 if (new_pc != 0)
1945 *new_pc = res;
1946 if (res == 0)
1947 return 1;
b81654f1
MS
1948 return 0;
1949}
1950
1951static int
1952resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
1953{
1954 struct objc_super sstr;
1955
1956 CORE_ADDR super;
1957 CORE_ADDR sel;
1958 CORE_ADDR res;
1959
1960 super = FETCH_ARGUMENT (1);
1961 sel = FETCH_ARGUMENT (2);
1962
1963 read_objc_super (super, &sstr);
d2e6263c
MS
1964 if (sstr.class == 0)
1965 return 0;
b81654f1
MS
1966
1967 res = find_implementation_from_class (sstr.class, sel);
d2e6263c
MS
1968 if (new_pc != 0)
1969 *new_pc = res;
1970 if (res == 0)
1971 return 1;
b81654f1
MS
1972 return 0;
1973}
This page took 0.155519 seconds and 4 git commands to generate.