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