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