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