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