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