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