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