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