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