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