]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/objc-lang.c
ChangeLog:
[thirdparty/binutils-gdb.git] / gdb / objc-lang.c
CommitLineData
d2e6263c 1/* Objective-C language support routines for GDB, the GNU debugger.
b81654f1 2
9b254dd1
DJ
3 Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008
4 Free Software Foundation, Inc.
b81654f1 5
437666f8
AC
6 Contributed by Apple Computer, Inc.
7 Written by Michael Snyder.
b81654f1 8
437666f8 9 This file is part of GDB.
b81654f1 10
437666f8
AC
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
a9762ec7 13 the Free Software Foundation; either version 3 of the License, or
437666f8
AC
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
a9762ec7 22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
b81654f1
MS
23
24#include "defs.h"
25#include "symtab.h"
26#include "gdbtypes.h"
27#include "expression.h"
28#include "parser-defs.h"
29#include "language.h"
d2e6263c 30#include "c-lang.h"
b81654f1 31#include "objc-lang.h"
60250e8b 32#include "exceptions.h"
b81654f1
MS
33#include "complaints.h"
34#include "value.h"
35#include "symfile.h"
36#include "objfiles.h"
7248f48e 37#include "gdb_string.h" /* for strchr */
b81654f1
MS
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"
fe898f56 44#include "block.h"
04714b91 45#include "infcall.h"
4e45ca2e 46#include "valprint.h"
e8f3fcdd 47#include "gdb_assert.h"
b81654f1
MS
48
49#include <ctype.h>
50
51struct objc_object {
52 CORE_ADDR isa;
53};
54
55struct 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
68struct objc_super {
69 CORE_ADDR receiver;
70 CORE_ADDR class;
71};
72
73struct objc_method {
74 CORE_ADDR name;
75 CORE_ADDR types;
76 CORE_ADDR imp;
77};
78
d2e6263c
MS
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. */
b81654f1
MS
82
83struct symbol *
84lookup_struct_typedef (char *name, struct block *block, int noerr)
85{
f86f5ca3 86 struct symbol *sym;
b81654f1 87
2570f2b7 88 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
b81654f1
MS
89
90 if (sym == NULL)
91 {
92 if (noerr)
93 return 0;
94 else
8a3fe4f8 95 error (_("No struct type named %s."), name);
b81654f1
MS
96 }
97 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
98 {
99 if (noerr)
100 return 0;
101 else
8a3fe4f8 102 error (_("This context has class, union or enum %s, not a struct."),
d2e6263c 103 name);
b81654f1
MS
104 }
105 return sym;
106}
107
108CORE_ADDR
109lookup_objc_class (char *classname)
110{
111 struct value * function, *classval;
112
113 if (! target_has_execution)
114 {
d2e6263c 115 /* Can't call into inferior to lookup class. */
b81654f1
MS
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 {
e2e0b3e5 125 complaint (&symfile_complaints, _("no way to lookup Objective-C classes"));
b81654f1
MS
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
c253954e 135CORE_ADDR
b81654f1
MS
136lookup_child_selector (char *selname)
137{
138 struct value * function, *selstring;
139
140 if (! target_has_execution)
141 {
d2e6263c 142 /* Can't call into inferior to lookup selector. */
b81654f1
MS
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 {
e2e0b3e5 152 complaint (&symfile_complaints, _("no way to lookup Objective-C selectors"));
b81654f1
MS
153 return 0;
154 }
155
d2e6263c
MS
156 selstring = value_coerce_array (value_string (selname,
157 strlen (selname) + 1));
b81654f1
MS
158 return value_as_long (call_function_by_hand (function, 1, &selstring));
159}
160
161struct value *
162value_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)
d2e6263c 170 return 0; /* Can't call into inferior to create NSString. */
b81654f1 171
5e488a7b
AC
172 sym = lookup_struct_typedef("NSString", 0, 1);
173 if (sym == NULL)
174 sym = lookup_struct_typedef("NXString", 0, 1);
175 if (sym == NULL)
fde6c819 176 type = builtin_type_void_data_ptr;
b81654f1
MS
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]);
d2e6263c 182 /* _NSNewStringFromCString replaces "istr" after Lantern2A. */
b81654f1
MS
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
8a3fe4f8 203 error (_("NSString: internal error -- no way to create new NSString"));
b81654f1 204
04624583 205 deprecated_set_value_type (nsstringValue, type);
b81654f1
MS
206 return nsstringValue;
207}
208
d2e6263c 209/* Objective-C name demangling. */
b81654f1
MS
210
211char *
9a3d7dfd 212objc_demangle (const char *mangled, int options)
b81654f1
MS
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
7248f48e
AF
233 cp = strchr(cp, '_');
234 if (!cp) /* find first non-initial underbar */
b81654f1 235 {
7248f48e 236 xfree(demangled); /* not mangled name */
b81654f1
MS
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 */
7248f48e
AF
245 cp = strchr(cp, '_');
246 if (!cp)
b81654f1 247 {
7248f48e 248 xfree(demangled); /* not mangled name */
b81654f1
MS
249 return NULL;
250 }
251 *cp++ = ')';
d2e6263c 252 *cp++ = ' '; /* overwriting 1st char of method name... */
b81654f1
MS
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
d2e6263c 268 return NULL; /* Not an objc mangled name. */
b81654f1
MS
269}
270
d2e6263c
MS
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. */
b81654f1
MS
274
275static void
f86f5ca3 276objc_emit_char (int c, struct ui_file *stream, int quoter)
b81654f1
MS
277{
278
d2e6263c 279 c &= 0xFF; /* Avoid sign bit follies. */
b81654f1
MS
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
321static void
322objc_printchar (int c, struct ui_file *stream)
323{
324 fputs_filtered ("'", stream);
325 objc_emit_char (c, stream, '\'');
326 fputs_filtered ("'", stream);
327}
328
d2e6263c
MS
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. */
b81654f1
MS
334
335static void
fc1a4b47 336objc_printstr (struct ui_file *stream, const gdb_byte *string,
36e53c63 337 unsigned int length, int width, int force_ellipses)
b81654f1 338{
f86f5ca3 339 unsigned int i;
b81654f1
MS
340 unsigned int things_printed = 0;
341 int in_quotes = 0;
342 int need_comma = 0;
b81654f1
MS
343
344 /* If the string was not truncated due to `set print elements', and
d2e6263c
MS
345 the last byte of it is a null, we don't print that, in
346 traditional C style. */
b81654f1
MS
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 {
d2e6263c
MS
358 /* Position of the character we are examining to see whether it
359 is repeated. */
b81654f1
MS
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
f636b87d
AF
424/* Determine if we are currently in the Objective-C dispatch function.
425 If so, get the address of the method function that the dispatcher
426 would call and use that as the function to step into instead. Also
427 skip over the trampoline for the function (if any). This is better
428 for the user since they are only interested in stepping into the
429 method function anyway. */
430static CORE_ADDR
52f729a7 431objc_skip_trampoline (struct frame_info *frame, CORE_ADDR stop_pc)
f636b87d
AF
432{
433 CORE_ADDR real_stop_pc;
434 CORE_ADDR method_stop_pc;
435
52f729a7
UW
436 real_stop_pc = gdbarch_skip_trampoline_code
437 (current_gdbarch, frame, stop_pc);
f636b87d
AF
438
439 if (real_stop_pc != 0)
440 find_objc_msgcall (real_stop_pc, &method_stop_pc);
441 else
442 find_objc_msgcall (stop_pc, &method_stop_pc);
443
444 if (method_stop_pc)
445 {
e76f05fa 446 real_stop_pc = gdbarch_skip_trampoline_code
52f729a7 447 (current_gdbarch, frame, method_stop_pc);
f636b87d
AF
448 if (real_stop_pc == 0)
449 real_stop_pc = method_stop_pc;
450 }
451
452 return real_stop_pc;
453}
454
b81654f1
MS
455
456/* Table mapping opcodes into strings for printing operators
457 and precedences of the operators. */
458
459static const struct op_print objc_op_print_tab[] =
460 {
461 {",", BINOP_COMMA, PREC_COMMA, 0},
462 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
463 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
464 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
465 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
466 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
467 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
468 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
469 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
470 {"<=", BINOP_LEQ, PREC_ORDER, 0},
471 {">=", BINOP_GEQ, PREC_ORDER, 0},
472 {">", BINOP_GTR, PREC_ORDER, 0},
473 {"<", BINOP_LESS, PREC_ORDER, 0},
474 {">>", BINOP_RSH, PREC_SHIFT, 0},
475 {"<<", BINOP_LSH, PREC_SHIFT, 0},
476 {"+", BINOP_ADD, PREC_ADD, 0},
477 {"-", BINOP_SUB, PREC_ADD, 0},
478 {"*", BINOP_MUL, PREC_MUL, 0},
479 {"/", BINOP_DIV, PREC_MUL, 0},
480 {"%", BINOP_REM, PREC_MUL, 0},
481 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
482 {"-", UNOP_NEG, PREC_PREFIX, 0},
483 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
484 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
485 {"*", UNOP_IND, PREC_PREFIX, 0},
486 {"&", UNOP_ADDR, PREC_PREFIX, 0},
487 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
488 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
489 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
e8f3fcdd 490 {NULL, OP_NULL, PREC_NULL, 0}
b81654f1
MS
491};
492
b81654f1 493const struct language_defn objc_language_defn = {
d2e6263c 494 "objective-c", /* Language name */
b81654f1 495 language_objc,
b81654f1
MS
496 range_check_off,
497 type_check_off,
498 case_sensitive_on,
7ca2d3a3 499 array_row_major,
5f9769d1 500 &exp_descriptor_standard,
b81654f1
MS
501 objc_parse,
502 objc_error,
e85c3284 503 null_post_parser,
b81654f1
MS
504 objc_printchar, /* Print a character constant */
505 objc_printstr, /* Function to print string constant */
506 objc_emit_char,
b81654f1
MS
507 c_print_type, /* Print a type using appropriate syntax */
508 c_val_print, /* Print a value using appropriate syntax */
509 c_value_print, /* Print a top-level value */
f636b87d 510 objc_skip_trampoline, /* Language specific skip_trampoline */
2b2d9e11 511 "self", /* name_of_this */
5f9a71c3 512 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
b368761e 513 basic_lookup_transparent_type,/* lookup_transparent_type */
9a3d7dfd 514 objc_demangle, /* Language specific symbol demangler */
31c27f77 515 NULL, /* Language specific class_name_from_physname */
d2e6263c
MS
516 objc_op_print_tab, /* Expression operators for printing */
517 1, /* C-style arrays */
b81654f1 518 0, /* String lower bound */
6084f43a 519 default_word_break_characters,
41d27058 520 default_make_symbol_completion_list,
cad351d1 521 c_language_arch_info,
e79af960 522 default_print_array_index,
41f1b697 523 default_pass_by_reference,
b81654f1
MS
524 LANG_MAGIC
525};
526
527/*
528 * ObjC:
d2e6263c 529 * Following functions help construct Objective-C message calls
b81654f1
MS
530 */
531
d2e6263c 532struct selname /* For parsing Objective-C. */
b81654f1
MS
533 {
534 struct selname *next;
535 char *msglist_sel;
536 int msglist_len;
537 };
538
539static int msglist_len;
540static struct selname *selname_chain;
541static char *msglist_sel;
542
543void
544start_msglist(void)
545{
f86f5ca3 546 struct selname *new =
b81654f1
MS
547 (struct selname *) xmalloc (sizeof (struct selname));
548
549 new->next = selname_chain;
550 new->msglist_len = msglist_len;
551 new->msglist_sel = msglist_sel;
552 msglist_len = 0;
553 msglist_sel = (char *)xmalloc(1);
554 *msglist_sel = 0;
555 selname_chain = new;
556}
557
558void
559add_msglist(struct stoken *str, int addcolon)
560{
561 char *s, *p;
562 int len, plen;
563
d2e6263c
MS
564 if (str == 0) { /* Unnamed arg, or... */
565 if (addcolon == 0) { /* variable number of args. */
b81654f1
MS
566 msglist_len++;
567 return;
568 }
569 p = "";
570 plen = 0;
571 } else {
572 p = str->ptr;
573 plen = str->length;
574 }
575 len = plen + strlen(msglist_sel) + 2;
576 s = (char *)xmalloc(len);
577 strcpy(s, msglist_sel);
578 strncat(s, p, plen);
7248f48e 579 xfree(msglist_sel);
b81654f1
MS
580 msglist_sel = s;
581 if (addcolon) {
582 s[len-2] = ':';
583 s[len-1] = 0;
584 msglist_len++;
585 } else
586 s[len-2] = '\0';
587}
588
589int
590end_msglist(void)
591{
f86f5ca3
PH
592 int val = msglist_len;
593 struct selname *sel = selname_chain;
594 char *p = msglist_sel;
c253954e 595 CORE_ADDR selid;
b81654f1
MS
596
597 selname_chain = sel->next;
598 msglist_len = sel->msglist_len;
599 msglist_sel = sel->msglist_sel;
600 selid = lookup_child_selector(p);
601 if (!selid)
8a3fe4f8 602 error (_("Can't find selector \"%s\""), p);
b81654f1 603 write_exp_elt_longcst (selid);
7248f48e 604 xfree(p);
d2e6263c 605 write_exp_elt_longcst (val); /* Number of args */
7248f48e 606 xfree(sel);
b81654f1
MS
607
608 return val;
609}
610
611/*
612 * Function: specialcmp (char *a, char *b)
613 *
614 * Special strcmp: treats ']' and ' ' as end-of-string.
d2e6263c 615 * Used for qsorting lists of objc methods (either by class or selector).
b81654f1
MS
616 */
617
b9362cc7
AC
618static int
619specialcmp (char *a, char *b)
b81654f1
MS
620{
621 while (*a && *a != ' ' && *a != ']' && *b && *b != ' ' && *b != ']')
622 {
623 if (*a != *b)
624 return *a - *b;
625 a++, b++;
626 }
627 if (*a && *a != ' ' && *a != ']')
628 return 1; /* a is longer therefore greater */
629 if (*b && *b != ' ' && *b != ']')
630 return -1; /* a is shorter therefore lesser */
631 return 0; /* a and b are identical */
632}
633
634/*
36e53c63 635 * Function: compare_selectors (const void *, const void *)
b81654f1 636 *
d2e6263c
MS
637 * Comparison function for use with qsort. Arguments are symbols or
638 * msymbols Compares selector part of objc method name alphabetically.
b81654f1
MS
639 */
640
641static int
36e53c63 642compare_selectors (const void *a, const void *b)
b81654f1
MS
643{
644 char *aname, *bname;
645
de5ad195
DC
646 aname = SYMBOL_PRINT_NAME (*(struct symbol **) a);
647 bname = SYMBOL_PRINT_NAME (*(struct symbol **) b);
7248f48e 648 if (aname == NULL || bname == NULL)
8a3fe4f8 649 error (_("internal: compare_selectors(1)"));
b81654f1 650
7248f48e
AF
651 aname = strchr(aname, ' ');
652 bname = strchr(bname, ' ');
653 if (aname == NULL || bname == NULL)
8a3fe4f8 654 error (_("internal: compare_selectors(2)"));
b81654f1
MS
655
656 return specialcmp (aname+1, bname+1);
657}
658
659/*
660 * Function: selectors_info (regexp, from_tty)
661 *
d2e6263c
MS
662 * Implements the "Info selectors" command. Takes an optional regexp
663 * arg. Lists all objective c selectors that match the regexp. Works
664 * by grepping thru all symbols for objective c methods. Output list
665 * is sorted and uniqued.
b81654f1
MS
666 */
667
668static void
669selectors_info (char *regexp, int from_tty)
670{
671 struct objfile *objfile;
672 struct minimal_symbol *msymbol;
673 char *name;
674 char *val;
675 int matches = 0;
676 int maxlen = 0;
677 int ix;
678 char myregexp[2048];
679 char asel[256];
680 struct symbol **sym_arr;
681 int plusminus = 0;
682
683 if (regexp == NULL)
d2e6263c 684 strcpy(myregexp, ".*]"); /* Null input, match all objc methods. */
b81654f1
MS
685 else
686 {
d2e6263c
MS
687 if (*regexp == '+' || *regexp == '-')
688 { /* User wants only class methods or only instance methods. */
b81654f1
MS
689 plusminus = *regexp++;
690 while (*regexp == ' ' || *regexp == '\t')
691 regexp++;
692 }
693 if (*regexp == '\0')
694 strcpy(myregexp, ".*]");
695 else
696 {
697 strcpy(myregexp, regexp);
698 if (myregexp[strlen(myregexp) - 1] == '$') /* end of selector */
699 myregexp[strlen(myregexp) - 1] = ']'; /* end of method name */
700 else
701 strcat(myregexp, ".*]");
702 }
703 }
704
705 if (regexp != NULL)
5e488a7b
AC
706 {
707 val = re_comp (myregexp);
708 if (val != 0)
8a3fe4f8 709 error (_("Invalid regexp (%s): %s"), val, regexp);
5e488a7b 710 }
b81654f1 711
d2e6263c 712 /* First time thru is JUST to get max length and count. */
b81654f1
MS
713 ALL_MSYMBOLS (objfile, msymbol)
714 {
715 QUIT;
36018d2e 716 name = SYMBOL_NATURAL_NAME (msymbol);
b81654f1
MS
717 if (name &&
718 (name[0] == '-' || name[0] == '+') &&
d2e6263c 719 name[1] == '[') /* Got a method name. */
b81654f1 720 {
d2e6263c 721 /* Filter for class/instance methods. */
b81654f1 722 if (plusminus && name[0] != plusminus)
d2e6263c
MS
723 continue;
724 /* Find selector part. */
725 name = (char *) strchr(name+2, ' ');
b81654f1
MS
726 if (regexp == NULL || re_exec(++name) != 0)
727 {
728 char *mystart = name;
729 char *myend = (char *) strchr(mystart, ']');
730
731 if (myend && (myend - mystart > maxlen))
d2e6263c 732 maxlen = myend - mystart; /* Get longest selector. */
b81654f1
MS
733 matches++;
734 }
735 }
736 }
737 if (matches)
738 {
a3f17187 739 printf_filtered (_("Selectors matching \"%s\":\n\n"),
b81654f1
MS
740 regexp ? regexp : "*");
741
742 sym_arr = alloca (matches * sizeof (struct symbol *));
743 matches = 0;
744 ALL_MSYMBOLS (objfile, msymbol)
745 {
746 QUIT;
36018d2e 747 name = SYMBOL_NATURAL_NAME (msymbol);
b81654f1
MS
748 if (name &&
749 (name[0] == '-' || name[0] == '+') &&
d2e6263c 750 name[1] == '[') /* Got a method name. */
b81654f1 751 {
d2e6263c 752 /* Filter for class/instance methods. */
b81654f1 753 if (plusminus && name[0] != plusminus)
d2e6263c
MS
754 continue;
755 /* Find selector part. */
756 name = (char *) strchr(name+2, ' ');
b81654f1
MS
757 if (regexp == NULL || re_exec(++name) != 0)
758 sym_arr[matches++] = (struct symbol *) msymbol;
759 }
760 }
761
762 qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
763 compare_selectors);
d2e6263c
MS
764 /* Prevent compare on first iteration. */
765 asel[0] = 0;
766 for (ix = 0; ix < matches; ix++) /* Now do the output. */
b81654f1
MS
767 {
768 char *p = asel;
769
770 QUIT;
36018d2e 771 name = SYMBOL_NATURAL_NAME (sym_arr[ix]);
b81654f1
MS
772 name = strchr (name, ' ') + 1;
773 if (p[0] && specialcmp(name, p) == 0)
d2e6263c 774 continue; /* Seen this one already (not unique). */
b81654f1 775
d2e6263c
MS
776 /* Copy selector part. */
777 while (*name && *name != ']')
b81654f1
MS
778 *p++ = *name++;
779 *p++ = '\0';
d2e6263c
MS
780 /* Print in columns. */
781 puts_filtered_tabular(asel, maxlen + 1, 0);
b81654f1
MS
782 }
783 begin_line();
784 }
785 else
a3f17187 786 printf_filtered (_("No selectors matching \"%s\"\n"), regexp ? regexp : "*");
b81654f1
MS
787}
788
789/*
36e53c63 790 * Function: compare_classes (const void *, const void *)
b81654f1 791 *
d2e6263c
MS
792 * Comparison function for use with qsort. Arguments are symbols or
793 * msymbols Compares class part of objc method name alphabetically.
b81654f1
MS
794 */
795
796static int
36e53c63 797compare_classes (const void *a, const void *b)
b81654f1
MS
798{
799 char *aname, *bname;
800
de5ad195
DC
801 aname = SYMBOL_PRINT_NAME (*(struct symbol **) a);
802 bname = SYMBOL_PRINT_NAME (*(struct symbol **) b);
7248f48e 803 if (aname == NULL || bname == NULL)
8a3fe4f8 804 error (_("internal: compare_classes(1)"));
b81654f1
MS
805
806 return specialcmp (aname+1, bname+1);
807}
808
809/*
810 * Function: classes_info(regexp, from_tty)
811 *
812 * Implements the "info classes" command for objective c classes.
813 * Lists all objective c classes that match the optional regexp.
d2e6263c
MS
814 * Works by grepping thru the list of objective c methods. List will
815 * be sorted and uniqued (since one class may have many methods).
816 * BUGS: will not list a class that has no methods.
b81654f1
MS
817 */
818
819static void
820classes_info (char *regexp, int from_tty)
821{
822 struct objfile *objfile;
823 struct minimal_symbol *msymbol;
824 char *name;
825 char *val;
826 int matches = 0;
827 int maxlen = 0;
828 int ix;
829 char myregexp[2048];
830 char aclass[256];
831 struct symbol **sym_arr;
832
833 if (regexp == NULL)
d2e6263c 834 strcpy(myregexp, ".* "); /* Null input: match all objc classes. */
b81654f1
MS
835 else
836 {
837 strcpy(myregexp, regexp);
838 if (myregexp[strlen(myregexp) - 1] == '$')
d2e6263c 839 /* In the method name, the end of the class name is marked by ' '. */
b81654f1
MS
840 myregexp[strlen(myregexp) - 1] = ' ';
841 else
842 strcat(myregexp, ".* ");
843 }
844
845 if (regexp != NULL)
5e488a7b
AC
846 {
847 val = re_comp (myregexp);
848 if (val != 0)
8a3fe4f8 849 error (_("Invalid regexp (%s): %s"), val, regexp);
5e488a7b 850 }
b81654f1 851
d2e6263c 852 /* First time thru is JUST to get max length and count. */
b81654f1
MS
853 ALL_MSYMBOLS (objfile, msymbol)
854 {
855 QUIT;
36018d2e 856 name = SYMBOL_NATURAL_NAME (msymbol);
b81654f1
MS
857 if (name &&
858 (name[0] == '-' || name[0] == '+') &&
d2e6263c 859 name[1] == '[') /* Got a method name. */
b81654f1
MS
860 if (regexp == NULL || re_exec(name+2) != 0)
861 {
d2e6263c
MS
862 /* Compute length of classname part. */
863 char *mystart = name + 2;
b81654f1
MS
864 char *myend = (char *) strchr(mystart, ' ');
865
866 if (myend && (myend - mystart > maxlen))
867 maxlen = myend - mystart;
868 matches++;
869 }
870 }
871 if (matches)
872 {
a3f17187 873 printf_filtered (_("Classes matching \"%s\":\n\n"),
b81654f1
MS
874 regexp ? regexp : "*");
875 sym_arr = alloca (matches * sizeof (struct symbol *));
876 matches = 0;
877 ALL_MSYMBOLS (objfile, msymbol)
878 {
879 QUIT;
36018d2e 880 name = SYMBOL_NATURAL_NAME (msymbol);
b81654f1
MS
881 if (name &&
882 (name[0] == '-' || name[0] == '+') &&
d2e6263c 883 name[1] == '[') /* Got a method name. */
b81654f1
MS
884 if (regexp == NULL || re_exec(name+2) != 0)
885 sym_arr[matches++] = (struct symbol *) msymbol;
886 }
887
888 qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
889 compare_classes);
d2e6263c
MS
890 /* Prevent compare on first iteration. */
891 aclass[0] = 0;
892 for (ix = 0; ix < matches; ix++) /* Now do the output. */
b81654f1
MS
893 {
894 char *p = aclass;
895
896 QUIT;
36018d2e 897 name = SYMBOL_NATURAL_NAME (sym_arr[ix]);
b81654f1
MS
898 name += 2;
899 if (p[0] && specialcmp(name, p) == 0)
d2e6263c 900 continue; /* Seen this one already (not unique). */
b81654f1 901
d2e6263c
MS
902 /* Copy class part of method name. */
903 while (*name && *name != ' ')
b81654f1
MS
904 *p++ = *name++;
905 *p++ = '\0';
d2e6263c
MS
906 /* Print in columns. */
907 puts_filtered_tabular(aclass, maxlen + 1, 0);
b81654f1
MS
908 }
909 begin_line();
910 }
911 else
a3f17187 912 printf_filtered (_("No classes matching \"%s\"\n"), regexp ? regexp : "*");
b81654f1
MS
913}
914
915/*
916 * Function: find_imps (char *selector, struct symbol **sym_arr)
917 *
918 * Input: a string representing a selector
919 * a pointer to an array of symbol pointers
920 * possibly a pointer to a symbol found by the caller.
921 *
d2e6263c
MS
922 * Output: number of methods that implement that selector. Side
923 * effects: The array of symbol pointers is filled with matching syms.
b81654f1 924 *
d2e6263c
MS
925 * By analogy with function "find_methods" (symtab.c), builds a list
926 * of symbols matching the ambiguous input, so that "decode_line_2"
927 * (symtab.c) can list them and ask the user to choose one or more.
928 * In this case the matches are objective c methods
929 * ("implementations") matching an objective c selector.
b81654f1 930 *
d2e6263c
MS
931 * Note that it is possible for a normal (c-style) function to have
932 * the same name as an objective c selector. To prevent the selector
933 * from eclipsing the function, we allow the caller (decode_line_1) to
934 * search for such a function first, and if it finds one, pass it in
935 * to us. We will then integrate it into the list. We also search
936 * for one here, among the minsyms.
b81654f1 937 *
d2e6263c
MS
938 * NOTE: if NUM_DEBUGGABLE is non-zero, the sym_arr will be divided
939 * into two parts: debuggable (struct symbol) syms, and
940 * non_debuggable (struct minimal_symbol) syms. The debuggable
941 * ones will come first, before NUM_DEBUGGABLE (which will thus
942 * be the index of the first non-debuggable one).
b81654f1
MS
943 */
944
945/*
946 * Function: total_number_of_imps (char *selector);
947 *
948 * Input: a string representing a selector
949 * Output: number of methods that implement that selector.
950 *
d2e6263c 951 * By analogy with function "total_number_of_methods", this allows
b81654f1 952 * decode_line_1 (symtab.c) to detect if there are objective c methods
d2e6263c
MS
953 * matching the input, and to allocate an array of pointers to them
954 * which can be manipulated by "decode_line_2" (also in symtab.c).
b81654f1
MS
955 */
956
957char *
958parse_selector (char *method, char **selector)
959{
960 char *s1 = NULL;
961 char *s2 = NULL;
962 int found_quote = 0;
963
964 char *nselector = NULL;
965
e8f3fcdd 966 gdb_assert (selector != NULL);
b81654f1
MS
967
968 s1 = method;
969
970 while (isspace (*s1))
971 s1++;
972 if (*s1 == '\'')
973 {
974 found_quote = 1;
975 s1++;
976 }
977 while (isspace (*s1))
978 s1++;
979
980 nselector = s1;
981 s2 = s1;
982
983 for (;;) {
984 if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
985 *s1++ = *s2;
986 else if (isspace (*s2))
987 ;
988 else if ((*s2 == '\0') || (*s2 == '\''))
989 break;
990 else
991 return NULL;
992 s2++;
993 }
994 *s1++ = '\0';
995
996 while (isspace (*s2))
997 s2++;
998 if (found_quote)
999 {
1000 if (*s2 == '\'')
1001 s2++;
1002 while (isspace (*s2))
1003 s2++;
1004 }
1005
1006 if (selector != NULL)
1007 *selector = nselector;
1008
1009 return s2;
1010}
1011
1012char *
d2e6263c
MS
1013parse_method (char *method, char *type, char **class,
1014 char **category, char **selector)
b81654f1
MS
1015{
1016 char *s1 = NULL;
1017 char *s2 = NULL;
1018 int found_quote = 0;
1019
1020 char ntype = '\0';
1021 char *nclass = NULL;
1022 char *ncategory = NULL;
1023 char *nselector = NULL;
1024
e8f3fcdd
AC
1025 gdb_assert (type != NULL);
1026 gdb_assert (class != NULL);
1027 gdb_assert (category != NULL);
1028 gdb_assert (selector != NULL);
b81654f1
MS
1029
1030 s1 = method;
1031
1032 while (isspace (*s1))
1033 s1++;
1034 if (*s1 == '\'')
1035 {
1036 found_quote = 1;
1037 s1++;
1038 }
1039 while (isspace (*s1))
1040 s1++;
1041
1042 if ((s1[0] == '+') || (s1[0] == '-'))
1043 ntype = *s1++;
1044
1045 while (isspace (*s1))
1046 s1++;
1047
1048 if (*s1 != '[')
1049 return NULL;
1050 s1++;
1051
1052 nclass = s1;
1053 while (isalnum (*s1) || (*s1 == '_'))
1054 s1++;
1055
1056 s2 = s1;
1057 while (isspace (*s2))
1058 s2++;
1059
1060 if (*s2 == '(')
1061 {
1062 s2++;
1063 while (isspace (*s2))
1064 s2++;
1065 ncategory = s2;
1066 while (isalnum (*s2) || (*s2 == '_'))
1067 s2++;
1068 *s2++ = '\0';
1069 }
1070
d2e6263c 1071 /* Truncate the class name now that we're not using the open paren. */
b81654f1
MS
1072 *s1++ = '\0';
1073
1074 nselector = s2;
1075 s1 = s2;
1076
1077 for (;;) {
1078 if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
1079 *s1++ = *s2;
1080 else if (isspace (*s2))
1081 ;
1082 else if (*s2 == ']')
1083 break;
1084 else
1085 return NULL;
1086 s2++;
1087 }
1088 *s1++ = '\0';
1089 s2++;
1090
1091 while (isspace (*s2))
1092 s2++;
1093 if (found_quote)
1094 {
1095 if (*s2 != '\'')
1096 return NULL;
1097 s2++;
1098 while (isspace (*s2))
1099 s2++;
1100 }
1101
1102 if (type != NULL)
1103 *type = ntype;
1104 if (class != NULL)
1105 *class = nclass;
1106 if (category != NULL)
1107 *category = ncategory;
1108 if (selector != NULL)
1109 *selector = nselector;
1110
1111 return s2;
1112}
1113
2f9a90b4 1114static void
d2e6263c
MS
1115find_methods (struct symtab *symtab, char type,
1116 const char *class, const char *category,
1117 const char *selector, struct symbol **syms,
1118 unsigned int *nsym, unsigned int *ndebug)
b81654f1
MS
1119{
1120 struct objfile *objfile = NULL;
1121 struct minimal_symbol *msymbol = NULL;
1122 struct block *block = NULL;
1123 struct symbol *sym = NULL;
1124
1125 char *symname = NULL;
1126
1127 char ntype = '\0';
1128 char *nclass = NULL;
1129 char *ncategory = NULL;
1130 char *nselector = NULL;
1131
1132 unsigned int csym = 0;
1133 unsigned int cdebug = 0;
1134
1135 static char *tmp = NULL;
1136 static unsigned int tmplen = 0;
1137
e8f3fcdd
AC
1138 gdb_assert (nsym != NULL);
1139 gdb_assert (ndebug != NULL);
b81654f1
MS
1140
1141 if (symtab)
1142 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1143
1144 ALL_MSYMBOLS (objfile, msymbol)
1145 {
1146 QUIT;
1147
1148 if ((msymbol->type != mst_text) && (msymbol->type != mst_file_text))
d2e6263c 1149 /* Not a function or method. */
b81654f1
MS
1150 continue;
1151
1152 if (symtab)
8da065d5
DC
1153 if ((SYMBOL_VALUE_ADDRESS (msymbol) < BLOCK_START (block)) ||
1154 (SYMBOL_VALUE_ADDRESS (msymbol) >= BLOCK_END (block)))
d2e6263c 1155 /* Not in the specified symtab. */
b81654f1
MS
1156 continue;
1157
36018d2e 1158 symname = SYMBOL_NATURAL_NAME (msymbol);
b81654f1
MS
1159 if (symname == NULL)
1160 continue;
1161
1162 if ((symname[0] != '-' && symname[0] != '+') || (symname[1] != '['))
d2e6263c 1163 /* Not a method name. */
b81654f1
MS
1164 continue;
1165
1166 while ((strlen (symname) + 1) >= tmplen)
1167 {
1168 tmplen = (tmplen == 0) ? 1024 : tmplen * 2;
1169 tmp = xrealloc (tmp, tmplen);
1170 }
1171 strcpy (tmp, symname);
1172
1173 if (parse_method (tmp, &ntype, &nclass, &ncategory, &nselector) == NULL)
1174 continue;
1175
1176 if ((type != '\0') && (ntype != type))
1177 continue;
1178
d2e6263c
MS
1179 if ((class != NULL)
1180 && ((nclass == NULL) || (strcmp (class, nclass) != 0)))
b81654f1
MS
1181 continue;
1182
d2e6263c
MS
1183 if ((category != NULL) &&
1184 ((ncategory == NULL) || (strcmp (category, ncategory) != 0)))
b81654f1
MS
1185 continue;
1186
d2e6263c
MS
1187 if ((selector != NULL) &&
1188 ((nselector == NULL) || (strcmp (selector, nselector) != 0)))
b81654f1
MS
1189 continue;
1190
1191 sym = find_pc_function (SYMBOL_VALUE_ADDRESS (msymbol));
1192 if (sym != NULL)
1193 {
36018d2e 1194 const char *newsymname = SYMBOL_NATURAL_NAME (sym);
b81654f1 1195
b81654f1
MS
1196 if (strcmp (symname, newsymname) == 0)
1197 {
d2e6263c
MS
1198 /* Found a high-level method sym: swap it into the
1199 lower part of sym_arr (below num_debuggable). */
b81654f1
MS
1200 if (syms != NULL)
1201 {
1202 syms[csym] = syms[cdebug];
1203 syms[cdebug] = sym;
1204 }
1205 csym++;
1206 cdebug++;
1207 }
1208 else
1209 {
d2e6263c
MS
1210 warning (
1211"debugging symbol \"%s\" does not match minimal symbol (\"%s\"); ignoring",
b81654f1
MS
1212 newsymname, symname);
1213 if (syms != NULL)
1214 syms[csym] = (struct symbol *) msymbol;
1215 csym++;
1216 }
1217 }
1218 else
1219 {
d2e6263c 1220 /* Found a non-debuggable method symbol. */
b81654f1
MS
1221 if (syms != NULL)
1222 syms[csym] = (struct symbol *) msymbol;
1223 csym++;
1224 }
1225 }
1226
1227 if (nsym != NULL)
1228 *nsym = csym;
1229 if (ndebug != NULL)
1230 *ndebug = cdebug;
1231}
1232
1233char *find_imps (struct symtab *symtab, struct block *block,
d2e6263c
MS
1234 char *method, struct symbol **syms,
1235 unsigned int *nsym, unsigned int *ndebug)
b81654f1
MS
1236{
1237 char type = '\0';
1238 char *class = NULL;
1239 char *category = NULL;
1240 char *selector = NULL;
1241
1242 unsigned int csym = 0;
1243 unsigned int cdebug = 0;
1244
1245 unsigned int ncsym = 0;
1246 unsigned int ncdebug = 0;
1247
1248 char *buf = NULL;
1249 char *tmp = NULL;
1250
e8f3fcdd
AC
1251 gdb_assert (nsym != NULL);
1252 gdb_assert (ndebug != NULL);
b81654f1
MS
1253
1254 if (nsym != NULL)
1255 *nsym = 0;
1256 if (ndebug != NULL)
1257 *ndebug = 0;
1258
1259 buf = (char *) alloca (strlen (method) + 1);
1260 strcpy (buf, method);
1261 tmp = parse_method (buf, &type, &class, &category, &selector);
1262
1263 if (tmp == NULL) {
1264
b81654f1
MS
1265 struct symbol *sym = NULL;
1266 struct minimal_symbol *msym = NULL;
1267
1268 strcpy (buf, method);
1269 tmp = parse_selector (buf, &selector);
1270
1271 if (tmp == NULL)
1272 return NULL;
1273
2570f2b7 1274 sym = lookup_symbol (selector, block, VAR_DOMAIN, 0);
b81654f1
MS
1275 if (sym != NULL)
1276 {
1277 if (syms)
1278 syms[csym] = sym;
1279 csym++;
1280 cdebug++;
1281 }
1282
1283 if (sym == NULL)
1284 msym = lookup_minimal_symbol (selector, 0, 0);
1285
1286 if (msym != NULL)
1287 {
1288 if (syms)
36e53c63 1289 syms[csym] = (struct symbol *)msym;
b81654f1
MS
1290 csym++;
1291 }
1292 }
1293
1294 if (syms != NULL)
d2e6263c
MS
1295 find_methods (symtab, type, class, category, selector,
1296 syms + csym, &ncsym, &ncdebug);
b81654f1 1297 else
d2e6263c
MS
1298 find_methods (symtab, type, class, category, selector,
1299 NULL, &ncsym, &ncdebug);
b81654f1 1300
d2e6263c 1301 /* If we didn't find any methods, just return. */
b81654f1
MS
1302 if (ncsym == 0 && ncdebug == 0)
1303 return method;
1304
1305 /* Take debug symbols from the second batch of symbols and swap them
1306 * with debug symbols from the first batch. Repeat until either the
1307 * second section is out of debug symbols or the first section is
1308 * full of debug symbols. Either way we have all debug symbols
d2e6263c
MS
1309 * packed to the beginning of the buffer.
1310 */
b81654f1
MS
1311
1312 if (syms != NULL)
1313 {
1314 while ((cdebug < csym) && (ncdebug > 0))
1315 {
1316 struct symbol *s = NULL;
d2e6263c
MS
1317 /* First non-debugging symbol. */
1318 unsigned int i = cdebug;
1319 /* Last of second batch of debug symbols. */
1320 unsigned int j = csym + ncdebug - 1;
b81654f1
MS
1321
1322 s = syms[j];
1323 syms[j] = syms[i];
1324 syms[i] = s;
1325
d2e6263c
MS
1326 /* We've moved a symbol from the second debug section to the
1327 first one. */
b81654f1
MS
1328 cdebug++;
1329 ncdebug--;
1330 }
1331 }
1332
1333 csym += ncsym;
1334 cdebug += ncdebug;
1335
1336 if (nsym != NULL)
1337 *nsym = csym;
1338 if (ndebug != NULL)
1339 *ndebug = cdebug;
1340
1341 if (syms == NULL)
1342 return method + (tmp - buf);
1343
1344 if (csym > 1)
1345 {
d2e6263c 1346 /* Sort debuggable symbols. */
b81654f1 1347 if (cdebug > 1)
d2e6263c
MS
1348 qsort (syms, cdebug, sizeof (struct minimal_symbol *),
1349 compare_classes);
b81654f1 1350
d2e6263c 1351 /* Sort minimal_symbols. */
b81654f1 1352 if ((csym - cdebug) > 1)
d2e6263c
MS
1353 qsort (&syms[cdebug], csym - cdebug,
1354 sizeof (struct minimal_symbol *), compare_classes);
b81654f1 1355 }
d2e6263c
MS
1356 /* Terminate the sym_arr list. */
1357 syms[csym] = 0;
b81654f1
MS
1358
1359 return method + (tmp - buf);
1360}
1361
b9362cc7 1362static void
b81654f1
MS
1363print_object_command (char *args, int from_tty)
1364{
1365 struct value *object, *function, *description;
36e53c63 1366 CORE_ADDR string_addr, object_addr;
b81654f1 1367 int i = 0;
22a44745 1368 gdb_byte c = 0;
b81654f1
MS
1369
1370 if (!args || !*args)
d2e6263c
MS
1371 error (
1372"The 'print-object' command requires an argument (an Objective-C object)");
b81654f1
MS
1373
1374 {
1375 struct expression *expr = parse_expression (args);
f86f5ca3 1376 struct cleanup *old_chain =
d2e6263c 1377 make_cleanup (free_current_contents, &expr);
b81654f1
MS
1378 int pc = 0;
1379
5f9769d1 1380 object = expr->language_defn->la_exp_desc->evaluate_exp
d4dbb9c7 1381 (builtin_type (expr->gdbarch)->builtin_data_ptr, expr, &pc, EVAL_NORMAL);
b81654f1
MS
1382 do_cleanups (old_chain);
1383 }
1384
36e53c63
AF
1385 /* Validate the address for sanity. */
1386 object_addr = value_as_long (object);
1387 read_memory (object_addr, &c, 1);
1388
7248f48e 1389 function = find_function_in_inferior ("_NSPrintForDebugger");
36e53c63 1390 if (function == NULL)
8a3fe4f8 1391 error (_("Unable to locate _NSPrintForDebugger in child process"));
b81654f1
MS
1392
1393 description = call_function_by_hand (function, 1, &object);
1394
7248f48e
AF
1395 string_addr = value_as_long (description);
1396 if (string_addr == 0)
8a3fe4f8 1397 error (_("object returns null description"));
b81654f1
MS
1398
1399 read_memory (string_addr + i++, &c, 1);
22a44745 1400 if (c != 0)
b81654f1 1401 do
d2e6263c 1402 { /* Read and print characters up to EOS. */
b81654f1
MS
1403 QUIT;
1404 printf_filtered ("%c", c);
1405 read_memory (string_addr + i++, &c, 1);
1406 } while (c != 0);
1407 else
a3f17187 1408 printf_filtered(_("<object returns empty description>"));
b81654f1
MS
1409 printf_filtered ("\n");
1410}
1411
d2e6263c
MS
1412/* The data structure 'methcalls' is used to detect method calls (thru
1413 * ObjC runtime lib functions objc_msgSend, objc_msgSendSuper, etc.),
1414 * and ultimately find the method being called.
b81654f1
MS
1415 */
1416
1417struct objc_methcall {
1418 char *name;
d2e6263c 1419 /* Return instance method to be called. */
36e53c63 1420 int (*stop_at) (CORE_ADDR, CORE_ADDR *);
d2e6263c
MS
1421 /* Start of pc range corresponding to method invocation. */
1422 CORE_ADDR begin;
1423 /* End of pc range corresponding to method invocation. */
1424 CORE_ADDR end;
b81654f1
MS
1425};
1426
d2e6263c
MS
1427static int resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc);
1428static int resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
1429static int resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc);
1430static int resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
b81654f1
MS
1431
1432static struct objc_methcall methcalls[] = {
1433 { "_objc_msgSend", resolve_msgsend, 0, 0},
1434 { "_objc_msgSend_stret", resolve_msgsend_stret, 0, 0},
1435 { "_objc_msgSendSuper", resolve_msgsend_super, 0, 0},
1436 { "_objc_msgSendSuper_stret", resolve_msgsend_super_stret, 0, 0},
1437 { "_objc_getClass", NULL, 0, 0},
1438 { "_objc_getMetaClass", NULL, 0, 0}
1439};
1440
1441#define nmethcalls (sizeof (methcalls) / sizeof (methcalls[0]))
1442
d2e6263c
MS
1443/* The following function, "find_objc_msgsend", fills in the data
1444 * structure "objc_msgs" by finding the addresses of each of the
1445 * (currently four) functions that it holds (of which objc_msgSend is
1446 * the first). This must be called each time symbols are loaded, in
1447 * case the functions have moved for some reason.
b81654f1
MS
1448 */
1449
b9362cc7 1450static void
b81654f1
MS
1451find_objc_msgsend (void)
1452{
1453 unsigned int i;
1454 for (i = 0; i < nmethcalls; i++) {
1455
1456 struct minimal_symbol *func;
1457
d2e6263c 1458 /* Try both with and without underscore. */
b81654f1
MS
1459 func = lookup_minimal_symbol (methcalls[i].name, NULL, NULL);
1460 if ((func == NULL) && (methcalls[i].name[0] == '_')) {
1461 func = lookup_minimal_symbol (methcalls[i].name + 1, NULL, NULL);
1462 }
1463 if (func == NULL) {
1464 methcalls[i].begin = 0;
1465 methcalls[i].end = 0;
1466 continue;
1467 }
1468
1469 methcalls[i].begin = SYMBOL_VALUE_ADDRESS (func);
1470 do {
1471 methcalls[i].end = SYMBOL_VALUE_ADDRESS (++func);
1472 } while (methcalls[i].begin == methcalls[i].end);
1473 }
1474}
1475
1476/* find_objc_msgcall (replaces pc_off_limits)
1477 *
d2e6263c
MS
1478 * ALL that this function now does is to determine whether the input
1479 * address ("pc") is the address of one of the Objective-C message
b81654f1
MS
1480 * dispatch functions (mainly objc_msgSend or objc_msgSendSuper), and
1481 * if so, it returns the address of the method that will be called.
1482 *
1483 * The old function "pc_off_limits" used to do a lot of other things
d2e6263c 1484 * in addition, such as detecting shared library jump stubs and
b81654f1 1485 * returning the address of the shlib function that would be called.
e76f05fa 1486 * That functionality has been moved into the gdbarch_skip_trampoline_code and
d2e6263c
MS
1487 * IN_SOLIB_TRAMPOLINE macros, which are resolved in the target-
1488 * dependent modules.
b81654f1
MS
1489 */
1490
1491struct objc_submethod_helper_data {
36e53c63 1492 int (*f) (CORE_ADDR, CORE_ADDR *);
b81654f1
MS
1493 CORE_ADDR pc;
1494 CORE_ADDR *new_pc;
1495};
1496
b9362cc7 1497static int
7248f48e 1498find_objc_msgcall_submethod_helper (void * arg)
b81654f1 1499{
d2e6263c
MS
1500 struct objc_submethod_helper_data *s =
1501 (struct objc_submethod_helper_data *) arg;
1502
1503 if (s->f (s->pc, s->new_pc) == 0)
b81654f1 1504 return 1;
d2e6263c 1505 else
b81654f1 1506 return 0;
b81654f1
MS
1507}
1508
b9362cc7 1509static int
36e53c63 1510find_objc_msgcall_submethod (int (*f) (CORE_ADDR, CORE_ADDR *),
d2e6263c
MS
1511 CORE_ADDR pc,
1512 CORE_ADDR *new_pc)
b81654f1
MS
1513{
1514 struct objc_submethod_helper_data s;
1515
1516 s.f = f;
1517 s.pc = pc;
1518 s.new_pc = new_pc;
1519
1520 if (catch_errors (find_objc_msgcall_submethod_helper,
7248f48e 1521 (void *) &s,
d2e6263c
MS
1522 "Unable to determine target of Objective-C method call (ignoring):\n",
1523 RETURN_MASK_ALL) == 0)
b81654f1 1524 return 1;
d2e6263c 1525 else
b81654f1 1526 return 0;
b81654f1
MS
1527}
1528
1529int
1530find_objc_msgcall (CORE_ADDR pc, CORE_ADDR *new_pc)
1531{
1532 unsigned int i;
1533
1534 find_objc_msgsend ();
5e488a7b
AC
1535 if (new_pc != NULL)
1536 {
1537 *new_pc = 0;
1538 }
b81654f1 1539
d2e6263c
MS
1540 for (i = 0; i < nmethcalls; i++)
1541 if ((pc >= methcalls[i].begin) && (pc < methcalls[i].end))
1542 {
1543 if (methcalls[i].stop_at != NULL)
1544 return find_objc_msgcall_submethod (methcalls[i].stop_at,
1545 pc, new_pc);
1546 else
1547 return 0;
b81654f1 1548 }
d2e6263c 1549
b81654f1
MS
1550 return 0;
1551}
1552
a78f21af 1553extern initialize_file_ftype _initialize_objc_language; /* -Wmissing-prototypes */
b9362cc7 1554
b81654f1
MS
1555void
1556_initialize_objc_language (void)
1557{
1558 add_language (&objc_language_defn);
d2e6263c 1559 add_info ("selectors", selectors_info, /* INFO SELECTORS command. */
1bedd215 1560 _("All Objective-C selectors, or those matching REGEXP."));
d2e6263c 1561 add_info ("classes", classes_info, /* INFO CLASSES command. */
1bedd215 1562 _("All Objective-C classes, or those matching REGEXP."));
b81654f1 1563 add_com ("print-object", class_vars, print_object_command,
1bedd215 1564 _("Ask an Objective-C object to print itself."));
b81654f1
MS
1565 add_com_alias ("po", "print-object", class_vars, 1);
1566}
1567
b81654f1
MS
1568static void
1569read_objc_method (CORE_ADDR addr, struct objc_method *method)
1570{
d2e6263c 1571 method->name = read_memory_unsigned_integer (addr + 0, 4);
b81654f1 1572 method->types = read_memory_unsigned_integer (addr + 4, 4);
d2e6263c 1573 method->imp = read_memory_unsigned_integer (addr + 8, 4);
b81654f1
MS
1574}
1575
1576static
1577unsigned long read_objc_methlist_nmethods (CORE_ADDR addr)
1578{
1579 return read_memory_unsigned_integer (addr + 4, 4);
1580}
1581
1582static void
1583read_objc_methlist_method (CORE_ADDR addr, unsigned long num,
1584 struct objc_method *method)
1585{
e8f3fcdd 1586 gdb_assert (num < read_objc_methlist_nmethods (addr));
b81654f1
MS
1587 read_objc_method (addr + 8 + (12 * num), method);
1588}
1589
1590static void
1591read_objc_object (CORE_ADDR addr, struct objc_object *object)
1592{
1593 object->isa = read_memory_unsigned_integer (addr, 4);
1594}
1595
1596static void
1597read_objc_super (CORE_ADDR addr, struct objc_super *super)
1598{
1599 super->receiver = read_memory_unsigned_integer (addr, 4);
1600 super->class = read_memory_unsigned_integer (addr + 4, 4);
1601};
1602
1603static void
1604read_objc_class (CORE_ADDR addr, struct objc_class *class)
1605{
1606 class->isa = read_memory_unsigned_integer (addr, 4);
1607 class->super_class = read_memory_unsigned_integer (addr + 4, 4);
1608 class->name = read_memory_unsigned_integer (addr + 8, 4);
1609 class->version = read_memory_unsigned_integer (addr + 12, 4);
1610 class->info = read_memory_unsigned_integer (addr + 16, 4);
1611 class->instance_size = read_memory_unsigned_integer (addr + 18, 4);
1612 class->ivars = read_memory_unsigned_integer (addr + 24, 4);
1613 class->methods = read_memory_unsigned_integer (addr + 28, 4);
1614 class->cache = read_memory_unsigned_integer (addr + 32, 4);
1615 class->protocols = read_memory_unsigned_integer (addr + 36, 4);
1616}
1617
b9362cc7 1618static CORE_ADDR
b81654f1
MS
1619find_implementation_from_class (CORE_ADDR class, CORE_ADDR sel)
1620{
1621 CORE_ADDR subclass = class;
1622
d2e6263c
MS
1623 while (subclass != 0)
1624 {
b81654f1 1625
d2e6263c
MS
1626 struct objc_class class_str;
1627 unsigned mlistnum = 0;
b81654f1 1628
d2e6263c 1629 read_objc_class (subclass, &class_str);
b81654f1 1630
d2e6263c
MS
1631 for (;;)
1632 {
1633 CORE_ADDR mlist;
1634 unsigned long nmethods;
1635 unsigned long i;
b81654f1 1636
d2e6263c
MS
1637 mlist = read_memory_unsigned_integer (class_str.methods +
1638 (4 * mlistnum), 4);
1639 if (mlist == 0)
1640 break;
b81654f1 1641
d2e6263c 1642 nmethods = read_objc_methlist_nmethods (mlist);
b81654f1 1643
d2e6263c
MS
1644 for (i = 0; i < nmethods; i++)
1645 {
1646 struct objc_method meth_str;
1647 read_objc_methlist_method (mlist, i, &meth_str);
b81654f1
MS
1648
1649#if 0
d2e6263c
MS
1650 fprintf (stderr,
1651 "checking method 0x%lx against selector 0x%lx\n",
1652 meth_str.name, sel);
b81654f1
MS
1653#endif
1654
d2e6263c 1655 if (meth_str.name == sel)
1abf022c
AF
1656 /* FIXME: hppa arch was doing a pointer dereference
1657 here. There needs to be a better way to do that. */
1658 return meth_str.imp;
d2e6263c
MS
1659 }
1660 mlistnum++;
b81654f1 1661 }
d2e6263c 1662 subclass = class_str.super_class;
b81654f1 1663 }
b81654f1
MS
1664
1665 return 0;
1666}
1667
b9362cc7 1668static CORE_ADDR
b81654f1
MS
1669find_implementation (CORE_ADDR object, CORE_ADDR sel)
1670{
1671 struct objc_object ostr;
1672
d2e6263c
MS
1673 if (object == 0)
1674 return 0;
b81654f1 1675 read_objc_object (object, &ostr);
d2e6263c
MS
1676 if (ostr.isa == 0)
1677 return 0;
b81654f1
MS
1678
1679 return find_implementation_from_class (ostr.isa, sel);
1680}
1681
1682static int
1683resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc)
1684{
5ed92fa8
UW
1685 struct frame_info *frame = get_current_frame ();
1686 struct gdbarch *gdbarch = get_frame_arch (frame);
1687 struct type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;
1688
b81654f1
MS
1689 CORE_ADDR object;
1690 CORE_ADDR sel;
1691 CORE_ADDR res;
1692
5ed92fa8
UW
1693 object = gdbarch_fetch_pointer_argument (gdbarch, frame, 0, ptr_type);
1694 sel = gdbarch_fetch_pointer_argument (gdbarch, frame, 1, ptr_type);
b81654f1
MS
1695
1696 res = find_implementation (object, sel);
d2e6263c
MS
1697 if (new_pc != 0)
1698 *new_pc = res;
1699 if (res == 0)
1700 return 1;
b81654f1
MS
1701 return 0;
1702}
1703
1704static int
1705resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
1706{
5ed92fa8
UW
1707 struct frame_info *frame = get_current_frame ();
1708 struct gdbarch *gdbarch = get_frame_arch (frame);
1709 struct type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;
1710
b81654f1
MS
1711 CORE_ADDR object;
1712 CORE_ADDR sel;
1713 CORE_ADDR res;
1714
5ed92fa8
UW
1715 object = gdbarch_fetch_pointer_argument (gdbarch, frame, 1, ptr_type);
1716 sel = gdbarch_fetch_pointer_argument (gdbarch, frame, 2, ptr_type);
b81654f1
MS
1717
1718 res = find_implementation (object, sel);
d2e6263c
MS
1719 if (new_pc != 0)
1720 *new_pc = res;
1721 if (res == 0)
1722 return 1;
b81654f1
MS
1723 return 0;
1724}
1725
1726static int
1727resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc)
1728{
5ed92fa8
UW
1729 struct frame_info *frame = get_current_frame ();
1730 struct gdbarch *gdbarch = get_frame_arch (frame);
1731 struct type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;
1732
b81654f1
MS
1733 struct objc_super sstr;
1734
1735 CORE_ADDR super;
1736 CORE_ADDR sel;
1737 CORE_ADDR res;
1738
5ed92fa8
UW
1739 super = gdbarch_fetch_pointer_argument (gdbarch, frame, 0, ptr_type);
1740 sel = gdbarch_fetch_pointer_argument (gdbarch, frame, 1, ptr_type);
b81654f1
MS
1741
1742 read_objc_super (super, &sstr);
d2e6263c
MS
1743 if (sstr.class == 0)
1744 return 0;
b81654f1
MS
1745
1746 res = find_implementation_from_class (sstr.class, sel);
d2e6263c
MS
1747 if (new_pc != 0)
1748 *new_pc = res;
1749 if (res == 0)
1750 return 1;
b81654f1
MS
1751 return 0;
1752}
1753
1754static int
1755resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
1756{
5ed92fa8
UW
1757 struct frame_info *frame = get_current_frame ();
1758 struct gdbarch *gdbarch = get_frame_arch (frame);
1759 struct type *ptr_type = builtin_type (gdbarch)->builtin_func_ptr;
1760
b81654f1
MS
1761 struct objc_super sstr;
1762
1763 CORE_ADDR super;
1764 CORE_ADDR sel;
1765 CORE_ADDR res;
1766
5ed92fa8
UW
1767 super = gdbarch_fetch_pointer_argument (gdbarch, frame, 1, ptr_type);
1768 sel = gdbarch_fetch_pointer_argument (gdbarch, frame, 2, ptr_type);
b81654f1
MS
1769
1770 read_objc_super (super, &sstr);
d2e6263c
MS
1771 if (sstr.class == 0)
1772 return 0;
b81654f1
MS
1773
1774 res = find_implementation_from_class (sstr.class, sel);
d2e6263c
MS
1775 if (new_pc != 0)
1776 *new_pc = res;
1777 if (res == 0)
1778 return 1;
b81654f1
MS
1779 return 0;
1780}