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