2011-03-18 Phil Muldoon <pmuldoon@redhat.com>
[deliverable/binutils-gdb.git] / gdb / jv-lang.c
1 /* Java language support routines for GDB, the GNU debugger.
2
3 Copyright (C) 1997, 1998, 1999, 2000, 2003, 2004, 2005, 2007, 2008, 2009,
4 2010, 2011 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "parser-defs.h"
26 #include "language.h"
27 #include "gdbtypes.h"
28 #include "symtab.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "gdb_string.h"
32 #include "value.h"
33 #include "c-lang.h"
34 #include "jv-lang.h"
35 #include "gdbcore.h"
36 #include "block.h"
37 #include "demangle.h"
38 #include "dictionary.h"
39 #include <ctype.h>
40 #include "gdb_assert.h"
41
42 /* Local functions */
43
44 extern void _initialize_java_language (void);
45
46 static int java_demangled_signature_length (char *);
47 static void java_demangled_signature_copy (char *, char *);
48
49 static struct symtab *get_java_class_symtab (struct gdbarch *gdbarch);
50 static char *get_java_utf8_name (struct obstack *obstack, struct value *name);
51 static int java_class_is_primitive (struct value *clas);
52 static struct value *java_value_string (char *ptr, int len);
53
54 static void java_emit_char (int c, struct type *type,
55 struct ui_file * stream, int quoter);
56
57 static char *java_class_name_from_physname (const char *physname);
58
59 static const struct objfile_data *jv_dynamics_objfile_data_key;
60 static const struct objfile_data *jv_type_objfile_data_key;
61
62 /* This objfile contains symtabs that have been dynamically created
63 to record dynamically loaded Java classes and dynamically
64 compiled java methods. */
65
66 static struct objfile *dynamics_objfile = NULL;
67
68 /* symtab contains classes read from the inferior. */
69
70 static struct symtab *class_symtab = NULL;
71
72 static struct type *java_link_class_type (struct gdbarch *,
73 struct type *, struct value *);
74
75 /* A function called when the dynamics_objfile is freed. We use this
76 to clean up some internal state. */
77 static void
78 jv_per_objfile_free (struct objfile *objfile, void *ignore)
79 {
80 gdb_assert (objfile == dynamics_objfile);
81 /* Clean up all our cached state. These objects are all allocated
82 in the dynamics_objfile, so we don't need to actually free
83 anything. */
84 dynamics_objfile = NULL;
85 class_symtab = NULL;
86 }
87
88 /* FIXME: carlton/2003-02-04: This is the main or only caller of
89 allocate_objfile with first argument NULL; as a result, this code
90 breaks every so often. Somebody should write a test case that
91 exercises GDB in various ways (e.g. something involving loading a
92 dynamic library) after this code has been called. */
93
94 static struct objfile *
95 get_dynamics_objfile (struct gdbarch *gdbarch)
96 {
97 if (dynamics_objfile == NULL)
98 {
99 /* Mark it as shared so that it is cleared when the inferior is
100 re-run. */
101 dynamics_objfile = allocate_objfile (NULL, OBJF_SHARED);
102 dynamics_objfile->gdbarch = gdbarch;
103 /* We don't have any data to store, but this lets us get a
104 notification when the objfile is destroyed. Since we have to
105 store a non-NULL value, we just pick something arbitrary and
106 safe. */
107 set_objfile_data (dynamics_objfile, jv_dynamics_objfile_data_key,
108 &dynamics_objfile);
109 }
110 return dynamics_objfile;
111 }
112
113 static void free_class_block (struct symtab *symtab);
114
115 static struct symtab *
116 get_java_class_symtab (struct gdbarch *gdbarch)
117 {
118 if (class_symtab == NULL)
119 {
120 struct objfile *objfile = get_dynamics_objfile (gdbarch);
121 struct blockvector *bv;
122 struct block *bl;
123
124 class_symtab = allocate_symtab ("<java-classes>", objfile);
125 class_symtab->language = language_java;
126 bv = (struct blockvector *)
127 obstack_alloc (&objfile->objfile_obstack,
128 sizeof (struct blockvector) + sizeof (struct block *));
129 BLOCKVECTOR_NBLOCKS (bv) = 1;
130 BLOCKVECTOR (class_symtab) = bv;
131
132 /* Allocate dummy STATIC_BLOCK. */
133 bl = allocate_block (&objfile->objfile_obstack);
134 BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
135 NULL);
136 BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
137
138 /* Allocate GLOBAL_BLOCK. */
139 bl = allocate_block (&objfile->objfile_obstack);
140 BLOCK_DICT (bl) = dict_create_hashed_expandable ();
141 BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
142 class_symtab->free_func = free_class_block;
143 }
144 return class_symtab;
145 }
146
147 static void
148 add_class_symtab_symbol (struct symbol *sym)
149 {
150 struct symtab *symtab
151 = get_java_class_symtab (get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile));
152 struct blockvector *bv = BLOCKVECTOR (symtab);
153
154 dict_add_symbol (BLOCK_DICT (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)), sym);
155 }
156
157 static struct symbol *
158 add_class_symbol (struct type *type, CORE_ADDR addr)
159 {
160 struct symbol *sym;
161
162 sym = (struct symbol *)
163 obstack_alloc (&dynamics_objfile->objfile_obstack, sizeof (struct symbol));
164 memset (sym, 0, sizeof (struct symbol));
165 SYMBOL_SET_LANGUAGE (sym, language_java);
166 SYMBOL_SET_LINKAGE_NAME (sym, TYPE_TAG_NAME (type));
167 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
168 /* SYMBOL_VALUE (sym) = valu; */
169 SYMBOL_TYPE (sym) = type;
170 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
171 SYMBOL_VALUE_ADDRESS (sym) = addr;
172 return sym;
173 }
174
175 /* Free the dynamic symbols block. */
176 static void
177 free_class_block (struct symtab *symtab)
178 {
179 struct blockvector *bv = BLOCKVECTOR (symtab);
180 struct block *bl = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
181
182 dict_free (BLOCK_DICT (bl));
183 }
184
185 struct type *
186 java_lookup_class (char *name)
187 {
188 struct symbol *sym;
189
190 sym = lookup_symbol (name, expression_context_block, STRUCT_DOMAIN, NULL);
191 if (sym != NULL)
192 return SYMBOL_TYPE (sym);
193 /* FIXME - should search inferior's symbol table. */
194 return NULL;
195 }
196
197 /* Return a nul-terminated string (allocated on OBSTACK) for
198 a name given by NAME (which has type Utf8Const*). */
199
200 char *
201 get_java_utf8_name (struct obstack *obstack, struct value *name)
202 {
203 char *chrs;
204 struct value *temp = name;
205 int name_length;
206 CORE_ADDR data_addr;
207
208 temp = value_struct_elt (&temp, NULL, "length", NULL, "structure");
209 name_length = (int) value_as_long (temp);
210 data_addr = value_address (temp) + TYPE_LENGTH (value_type (temp));
211 chrs = obstack_alloc (obstack, name_length + 1);
212 chrs[name_length] = '\0';
213 read_memory (data_addr, (gdb_byte *) chrs, name_length);
214 return chrs;
215 }
216
217 struct value *
218 java_class_from_object (struct value *obj_val)
219 {
220 /* This is all rather inefficient, since the offsets of vtable and
221 class are fixed. FIXME */
222 struct value *vtable_val;
223
224 if (TYPE_CODE (value_type (obj_val)) == TYPE_CODE_PTR
225 && TYPE_LENGTH (TYPE_TARGET_TYPE (value_type (obj_val))) == 0)
226 obj_val = value_at (get_java_object_type (),
227 value_as_address (obj_val));
228
229 vtable_val = value_struct_elt (&obj_val, NULL, "vtable", NULL, "structure");
230 return value_struct_elt (&vtable_val, NULL, "class", NULL, "structure");
231 }
232
233 /* Check if CLASS_IS_PRIMITIVE(value of clas): */
234 static int
235 java_class_is_primitive (struct value *clas)
236 {
237 struct value *vtable = value_struct_elt (&clas, NULL, "vtable",
238 NULL, "struct");
239 CORE_ADDR i = value_as_address (vtable);
240
241 return (int) (i & 0x7fffffff) == (int) 0x7fffffff;
242 }
243
244 /* Read a GCJ Class object, and generated a gdb (TYPE_CODE_STRUCT) type. */
245
246 struct type *
247 type_from_class (struct gdbarch *gdbarch, struct value *clas)
248 {
249 struct type *type;
250 char *name;
251 struct value *temp;
252 struct objfile *objfile;
253 struct value *utf8_name;
254 char *nptr;
255 CORE_ADDR addr;
256 int is_array = 0;
257
258 type = check_typedef (value_type (clas));
259 if (TYPE_CODE (type) == TYPE_CODE_PTR)
260 {
261 if (value_logical_not (clas))
262 return NULL;
263 clas = value_ind (clas);
264 }
265 addr = value_address (clas);
266
267 objfile = get_dynamics_objfile (gdbarch);
268 if (java_class_is_primitive (clas))
269 {
270 struct value *sig;
271
272 temp = clas;
273 sig = value_struct_elt (&temp, NULL, "method_count", NULL, "structure");
274 return java_primitive_type (gdbarch, value_as_long (sig));
275 }
276
277 /* Get Class name. */
278 /* If clasloader non-null, prepend loader address. FIXME */
279 temp = clas;
280 utf8_name = value_struct_elt (&temp, NULL, "name", NULL, "structure");
281 name = get_java_utf8_name (&objfile->objfile_obstack, utf8_name);
282 for (nptr = name; *nptr != 0; nptr++)
283 {
284 if (*nptr == '/')
285 *nptr = '.';
286 }
287
288 type = java_lookup_class (name);
289 if (type != NULL)
290 return type;
291
292 type = alloc_type (objfile);
293 TYPE_CODE (type) = TYPE_CODE_STRUCT;
294 INIT_CPLUS_SPECIFIC (type);
295
296 if (name[0] == '[')
297 {
298 char *signature = name;
299 int namelen = java_demangled_signature_length (signature);
300
301 if (namelen > strlen (name))
302 name = obstack_alloc (&objfile->objfile_obstack, namelen + 1);
303 java_demangled_signature_copy (name, signature);
304 name[namelen] = '\0';
305 is_array = 1;
306 temp = clas;
307 /* Set array element type. */
308 temp = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
309 deprecated_set_value_type (temp,
310 lookup_pointer_type (value_type (clas)));
311 TYPE_TARGET_TYPE (type) = type_from_class (gdbarch, temp);
312 }
313
314 ALLOCATE_CPLUS_STRUCT_TYPE (type);
315 TYPE_TAG_NAME (type) = name;
316
317 add_class_symtab_symbol (add_class_symbol (type, addr));
318 return java_link_class_type (gdbarch, type, clas);
319 }
320
321 /* Fill in class TYPE with data from the CLAS value. */
322
323 static struct type *
324 java_link_class_type (struct gdbarch *gdbarch,
325 struct type *type, struct value *clas)
326 {
327 struct value *temp;
328 char *unqualified_name;
329 char *name = TYPE_TAG_NAME (type);
330 int ninterfaces, nfields, nmethods;
331 int type_is_object = 0;
332 struct fn_field *fn_fields;
333 struct fn_fieldlist *fn_fieldlists;
334 struct value *fields;
335 struct value *methods;
336 struct value *method = NULL;
337 struct value *field = NULL;
338 int i, j;
339 struct objfile *objfile = get_dynamics_objfile (gdbarch);
340 struct type *tsuper;
341
342 gdb_assert (name != NULL);
343 unqualified_name = strrchr (name, '.');
344 if (unqualified_name == NULL)
345 unqualified_name = name;
346
347 temp = clas;
348 temp = value_struct_elt (&temp, NULL, "superclass", NULL, "structure");
349 if (strcmp (name, "java.lang.Object") == 0)
350 {
351 tsuper = get_java_object_type ();
352 if (tsuper && TYPE_CODE (tsuper) == TYPE_CODE_PTR)
353 tsuper = TYPE_TARGET_TYPE (tsuper);
354 type_is_object = 1;
355 }
356 else
357 tsuper = type_from_class (gdbarch, temp);
358
359 #if 1
360 ninterfaces = 0;
361 #else
362 temp = clas;
363 ninterfaces = value_as_long (value_struct_elt (&temp, NULL, "interface_len",
364 NULL, "structure"));
365 #endif
366 TYPE_N_BASECLASSES (type) = (tsuper == NULL ? 0 : 1) + ninterfaces;
367 temp = clas;
368 nfields = value_as_long (value_struct_elt (&temp, NULL, "field_count",
369 NULL, "structure"));
370 nfields += TYPE_N_BASECLASSES (type);
371 nfields++; /* Add one for dummy "class" field. */
372 TYPE_NFIELDS (type) = nfields;
373 TYPE_FIELDS (type) = (struct field *)
374 TYPE_ALLOC (type, sizeof (struct field) * nfields);
375
376 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
377
378 TYPE_FIELD_PRIVATE_BITS (type) =
379 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
380 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
381
382 TYPE_FIELD_PROTECTED_BITS (type) =
383 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
384 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
385
386 TYPE_FIELD_IGNORE_BITS (type) =
387 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
388 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
389
390 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *)
391 TYPE_ALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type)));
392 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
393
394 if (tsuper != NULL)
395 {
396 TYPE_BASECLASS (type, 0) = tsuper;
397 if (type_is_object)
398 SET_TYPE_FIELD_PRIVATE (type, 0);
399 }
400
401 i = strlen (name);
402 if (i > 2 && name[i - 1] == ']' && tsuper != NULL)
403 {
404 /* FIXME */
405 TYPE_LENGTH (type) = TYPE_LENGTH (tsuper) + 4; /* size with "length" */
406 }
407 else
408 {
409 temp = clas;
410 temp = value_struct_elt (&temp, NULL, "size_in_bytes",
411 NULL, "structure");
412 TYPE_LENGTH (type) = value_as_long (temp);
413 }
414
415 fields = NULL;
416 nfields--; /* First set up dummy "class" field. */
417 SET_FIELD_PHYSADDR (TYPE_FIELD (type, nfields), value_address (clas));
418 TYPE_FIELD_NAME (type, nfields) = "class";
419 TYPE_FIELD_TYPE (type, nfields) = value_type (clas);
420 SET_TYPE_FIELD_PRIVATE (type, nfields);
421
422 for (i = TYPE_N_BASECLASSES (type); i < nfields; i++)
423 {
424 int accflags;
425 int boffset;
426
427 if (fields == NULL)
428 {
429 temp = clas;
430 fields = value_struct_elt (&temp, NULL, "fields", NULL, "structure");
431 field = value_ind (fields);
432 }
433 else
434 { /* Re-use field value for next field. */
435 CORE_ADDR addr
436 = value_address (field) + TYPE_LENGTH (value_type (field));
437
438 set_value_address (field, addr);
439 set_value_lazy (field, 1);
440 }
441 temp = field;
442 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
443 TYPE_FIELD_NAME (type, i) =
444 get_java_utf8_name (&objfile->objfile_obstack, temp);
445 temp = field;
446 accflags = value_as_long (value_struct_elt (&temp, NULL, "accflags",
447 NULL, "structure"));
448 temp = field;
449 temp = value_struct_elt (&temp, NULL, "info", NULL, "structure");
450 boffset = value_as_long (value_struct_elt (&temp, NULL, "boffset",
451 NULL, "structure"));
452 if (accflags & 0x0001) /* public access */
453 {
454 /* ??? */
455 }
456 if (accflags & 0x0002) /* private access */
457 {
458 SET_TYPE_FIELD_PRIVATE (type, i);
459 }
460 if (accflags & 0x0004) /* protected access */
461 {
462 SET_TYPE_FIELD_PROTECTED (type, i);
463 }
464 if (accflags & 0x0008) /* ACC_STATIC */
465 SET_FIELD_PHYSADDR (TYPE_FIELD (type, i), boffset);
466 else
467 TYPE_FIELD_BITPOS (type, i) = 8 * boffset;
468 if (accflags & 0x8000) /* FIELD_UNRESOLVED_FLAG */
469 {
470 TYPE_FIELD_TYPE (type, i) = get_java_object_type (); /* FIXME */
471 }
472 else
473 {
474 struct type *ftype;
475
476 temp = field;
477 temp = value_struct_elt (&temp, NULL, "type", NULL, "structure");
478 ftype = type_from_class (gdbarch, temp);
479 if (TYPE_CODE (ftype) == TYPE_CODE_STRUCT)
480 ftype = lookup_pointer_type (ftype);
481 TYPE_FIELD_TYPE (type, i) = ftype;
482 }
483 }
484
485 temp = clas;
486 nmethods = value_as_long (value_struct_elt (&temp, NULL, "method_count",
487 NULL, "structure"));
488 TYPE_NFN_FIELDS_TOTAL (type) = nmethods;
489 j = nmethods * sizeof (struct fn_field);
490 fn_fields = (struct fn_field *)
491 obstack_alloc (&dynamics_objfile->objfile_obstack, j);
492 memset (fn_fields, 0, j);
493 fn_fieldlists = (struct fn_fieldlist *)
494 alloca (nmethods * sizeof (struct fn_fieldlist));
495
496 methods = NULL;
497 for (i = 0; i < nmethods; i++)
498 {
499 char *mname;
500 int k;
501
502 if (methods == NULL)
503 {
504 temp = clas;
505 methods = value_struct_elt (&temp, NULL, "methods",
506 NULL, "structure");
507 method = value_ind (methods);
508 }
509 else
510 { /* Re-use method value for next method. */
511 CORE_ADDR addr
512 = value_address (method) + TYPE_LENGTH (value_type (method));
513
514 set_value_address (method, addr);
515 set_value_lazy (method, 1);
516 }
517
518 /* Get method name. */
519 temp = method;
520 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
521 mname = get_java_utf8_name (&objfile->objfile_obstack, temp);
522 if (strcmp (mname, "<init>") == 0)
523 mname = unqualified_name;
524
525 /* Check for an existing method with the same name.
526 * This makes building the fn_fieldslists an O(nmethods**2)
527 * operation. That could be using hashing, but I doubt it
528 * is worth it. Note that we do maintain the order of methods
529 * in the inferior's Method table (as long as that is grouped
530 * by method name), which I think is desirable. --PB */
531 for (k = 0, j = TYPE_NFN_FIELDS (type);;)
532 {
533 if (--j < 0)
534 { /* No match - new method name. */
535 j = TYPE_NFN_FIELDS (type)++;
536 fn_fieldlists[j].name = mname;
537 fn_fieldlists[j].length = 1;
538 fn_fieldlists[j].fn_fields = &fn_fields[i];
539 k = i;
540 break;
541 }
542 if (strcmp (mname, fn_fieldlists[j].name) == 0)
543 { /* Found an existing method with the same name. */
544 int l;
545
546 if (mname != unqualified_name)
547 obstack_free (&objfile->objfile_obstack, mname);
548 mname = fn_fieldlists[j].name;
549 fn_fieldlists[j].length++;
550 k = i - k; /* Index of new slot. */
551 /* Shift intervening fn_fields (between k and i) down. */
552 for (l = i; l > k; l--)
553 fn_fields[l] = fn_fields[l - 1];
554 for (l = TYPE_NFN_FIELDS (type); --l > j;)
555 fn_fieldlists[l].fn_fields++;
556 break;
557 }
558 k += fn_fieldlists[j].length;
559 }
560 fn_fields[k].physname = "";
561 fn_fields[k].is_stub = 1;
562 /* FIXME */
563 fn_fields[k].type = lookup_function_type
564 (builtin_java_type (gdbarch)->builtin_void);
565 TYPE_CODE (fn_fields[k].type) = TYPE_CODE_METHOD;
566 }
567
568 j = TYPE_NFN_FIELDS (type) * sizeof (struct fn_fieldlist);
569 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
570 obstack_alloc (&dynamics_objfile->objfile_obstack, j);
571 memcpy (TYPE_FN_FIELDLISTS (type), fn_fieldlists, j);
572
573 return type;
574 }
575
576 static struct type *java_object_type;
577
578 /* A free function that is attached to the objfile defining
579 java_object_type. This is used to clear the cached type whenever
580 its owning objfile is destroyed. */
581 static void
582 jv_clear_object_type (struct objfile *objfile, void *ignore)
583 {
584 java_object_type = NULL;
585 }
586
587 static void
588 set_java_object_type (struct type *type)
589 {
590 struct objfile *owner;
591
592 gdb_assert (java_object_type == NULL);
593
594 owner = TYPE_OBJFILE (type);
595 if (owner)
596 set_objfile_data (owner, jv_type_objfile_data_key, &java_object_type);
597 java_object_type = type;
598 }
599
600 struct type *
601 get_java_object_type (void)
602 {
603 if (java_object_type == NULL)
604 {
605 struct symbol *sym;
606
607 sym = lookup_symbol ("java.lang.Object", NULL, STRUCT_DOMAIN, NULL);
608 if (sym == NULL)
609 error (_("cannot find java.lang.Object"));
610 set_java_object_type (SYMBOL_TYPE (sym));
611 }
612 return java_object_type;
613 }
614
615 int
616 get_java_object_header_size (struct gdbarch *gdbarch)
617 {
618 struct type *objtype = get_java_object_type ();
619
620 if (objtype == NULL)
621 return (2 * gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT);
622 else
623 return TYPE_LENGTH (objtype);
624 }
625
626 int
627 is_object_type (struct type *type)
628 {
629 CHECK_TYPEDEF (type);
630 if (TYPE_CODE (type) == TYPE_CODE_PTR)
631 {
632 struct type *ttype = check_typedef (TYPE_TARGET_TYPE (type));
633 char *name;
634 if (TYPE_CODE (ttype) != TYPE_CODE_STRUCT)
635 return 0;
636 while (TYPE_N_BASECLASSES (ttype) > 0)
637 ttype = TYPE_BASECLASS (ttype, 0);
638 name = TYPE_TAG_NAME (ttype);
639 if (name != NULL && strcmp (name, "java.lang.Object") == 0)
640 return 1;
641 name
642 = TYPE_NFIELDS (ttype) > 0 ? TYPE_FIELD_NAME (ttype, 0) : (char *) 0;
643 if (name != NULL && strcmp (name, "vtable") == 0)
644 {
645 if (java_object_type == NULL)
646 set_java_object_type (type);
647 return 1;
648 }
649 }
650 return 0;
651 }
652
653 struct type *
654 java_primitive_type (struct gdbarch *gdbarch, int signature)
655 {
656 const struct builtin_java_type *builtin = builtin_java_type (gdbarch);
657
658 switch (signature)
659 {
660 case 'B':
661 return builtin->builtin_byte;
662 case 'S':
663 return builtin->builtin_short;
664 case 'I':
665 return builtin->builtin_int;
666 case 'J':
667 return builtin->builtin_long;
668 case 'Z':
669 return builtin->builtin_boolean;
670 case 'C':
671 return builtin->builtin_char;
672 case 'F':
673 return builtin->builtin_float;
674 case 'D':
675 return builtin->builtin_double;
676 case 'V':
677 return builtin->builtin_void;
678 }
679 error (_("unknown signature '%c' for primitive type"), (char) signature);
680 }
681
682 /* If name[0 .. namelen-1] is the name of a primitive Java type,
683 return that type. Otherwise, return NULL. */
684
685 struct type *
686 java_primitive_type_from_name (struct gdbarch *gdbarch,
687 char *name, int namelen)
688 {
689 const struct builtin_java_type *builtin = builtin_java_type (gdbarch);
690
691 switch (name[0])
692 {
693 case 'b':
694 if (namelen == 4 && memcmp (name, "byte", 4) == 0)
695 return builtin->builtin_byte;
696 if (namelen == 7 && memcmp (name, "boolean", 7) == 0)
697 return builtin->builtin_boolean;
698 break;
699 case 'c':
700 if (namelen == 4 && memcmp (name, "char", 4) == 0)
701 return builtin->builtin_char;
702 break;
703 case 'd':
704 if (namelen == 6 && memcmp (name, "double", 6) == 0)
705 return builtin->builtin_double;
706 break;
707 case 'f':
708 if (namelen == 5 && memcmp (name, "float", 5) == 0)
709 return builtin->builtin_float;
710 break;
711 case 'i':
712 if (namelen == 3 && memcmp (name, "int", 3) == 0)
713 return builtin->builtin_int;
714 break;
715 case 'l':
716 if (namelen == 4 && memcmp (name, "long", 4) == 0)
717 return builtin->builtin_long;
718 break;
719 case 's':
720 if (namelen == 5 && memcmp (name, "short", 5) == 0)
721 return builtin->builtin_short;
722 break;
723 case 'v':
724 if (namelen == 4 && memcmp (name, "void", 4) == 0)
725 return builtin->builtin_void;
726 break;
727 }
728 return NULL;
729 }
730
731 static char *
732 java_primitive_type_name (int signature)
733 {
734 switch (signature)
735 {
736 case 'B':
737 return "byte";
738 case 'S':
739 return "short";
740 case 'I':
741 return "int";
742 case 'J':
743 return "long";
744 case 'Z':
745 return "boolean";
746 case 'C':
747 return "char";
748 case 'F':
749 return "float";
750 case 'D':
751 return "double";
752 case 'V':
753 return "void";
754 }
755 error (_("unknown signature '%c' for primitive type"), (char) signature);
756 }
757
758 /* Return the length (in bytes) of demangled name of the Java type
759 signature string SIGNATURE. */
760
761 static int
762 java_demangled_signature_length (char *signature)
763 {
764 int array = 0;
765
766 for (; *signature == '['; signature++)
767 array += 2; /* Two chars for "[]". */
768 switch (signature[0])
769 {
770 case 'L':
771 /* Subtract 2 for 'L' and ';'. */
772 return strlen (signature) - 2 + array;
773 default:
774 return strlen (java_primitive_type_name (signature[0])) + array;
775 }
776 }
777
778 /* Demangle the Java type signature SIGNATURE, leaving the result in
779 RESULT. */
780
781 static void
782 java_demangled_signature_copy (char *result, char *signature)
783 {
784 int array = 0;
785 char *ptr;
786 int i;
787
788 while (*signature == '[')
789 {
790 array++;
791 signature++;
792 }
793 switch (signature[0])
794 {
795 case 'L':
796 /* Subtract 2 for 'L' and ';', but add 1 for final nul. */
797 signature++;
798 ptr = result;
799 for (; *signature != ';' && *signature != '\0'; signature++)
800 {
801 if (*signature == '/')
802 *ptr++ = '.';
803 else
804 *ptr++ = *signature;
805 }
806 break;
807 default:
808 ptr = java_primitive_type_name (signature[0]);
809 i = strlen (ptr);
810 strcpy (result, ptr);
811 ptr = result + i;
812 break;
813 }
814 while (--array >= 0)
815 {
816 *ptr++ = '[';
817 *ptr++ = ']';
818 }
819 }
820
821 /* Return the demangled name of the Java type signature string SIGNATURE,
822 as a freshly allocated copy. */
823
824 char *
825 java_demangle_type_signature (char *signature)
826 {
827 int length = java_demangled_signature_length (signature);
828 char *result = xmalloc (length + 1);
829
830 java_demangled_signature_copy (result, signature);
831 result[length] = '\0';
832 return result;
833 }
834
835 /* Return the type of TYPE followed by DIMS pairs of [ ].
836 If DIMS == 0, TYPE is returned. */
837
838 struct type *
839 java_array_type (struct type *type, int dims)
840 {
841 while (dims-- > 0)
842 {
843 /* FIXME This is bogus! Java arrays are not gdb arrays! */
844 type = lookup_array_range_type (type, 0, 0);
845 }
846
847 return type;
848 }
849
850 /* Create a Java string in the inferior from a (Utf8) literal. */
851
852 static struct value *
853 java_value_string (char *ptr, int len)
854 {
855 error (_("not implemented - java_value_string")); /* FIXME */
856 }
857
858 /* Print the character C on STREAM as part of the contents of a literal
859 string whose delimiter is QUOTER. Note that that format for printing
860 characters and strings is language specific. */
861
862 static void
863 java_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
864 {
865 switch (c)
866 {
867 case '\\':
868 case '\'':
869 fprintf_filtered (stream, "\\%c", c);
870 break;
871 case '\b':
872 fputs_filtered ("\\b", stream);
873 break;
874 case '\t':
875 fputs_filtered ("\\t", stream);
876 break;
877 case '\n':
878 fputs_filtered ("\\n", stream);
879 break;
880 case '\f':
881 fputs_filtered ("\\f", stream);
882 break;
883 case '\r':
884 fputs_filtered ("\\r", stream);
885 break;
886 default:
887 if (isprint (c))
888 fputc_filtered (c, stream);
889 else
890 fprintf_filtered (stream, "\\u%.4x", (unsigned int) c);
891 break;
892 }
893 }
894
895 static struct value *
896 evaluate_subexp_java (struct type *expect_type, struct expression *exp,
897 int *pos, enum noside noside)
898 {
899 int pc = *pos;
900 int i;
901 char *name;
902 enum exp_opcode op = exp->elts[*pos].opcode;
903 struct value *arg1;
904 struct value *arg2;
905 struct type *type;
906
907 switch (op)
908 {
909 case UNOP_IND:
910 if (noside == EVAL_SKIP)
911 goto standard;
912 (*pos)++;
913 arg1 = evaluate_subexp_java (NULL_TYPE, exp, pos, EVAL_NORMAL);
914 if (is_object_type (value_type (arg1)))
915 {
916 struct type *type;
917
918 type = type_from_class (exp->gdbarch, java_class_from_object (arg1));
919 arg1 = value_cast (lookup_pointer_type (type), arg1);
920 }
921 return value_ind (arg1);
922
923 case BINOP_SUBSCRIPT:
924 (*pos)++;
925 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
926 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
927 if (noside == EVAL_SKIP)
928 goto nosideret;
929 /* If the user attempts to subscript something that is not an
930 array or pointer type (like a plain int variable for example),
931 then report this as an error. */
932
933 arg1 = coerce_ref (arg1);
934 type = check_typedef (value_type (arg1));
935 if (TYPE_CODE (type) == TYPE_CODE_PTR)
936 type = check_typedef (TYPE_TARGET_TYPE (type));
937 name = TYPE_NAME (type);
938 if (name == NULL)
939 name = TYPE_TAG_NAME (type);
940 i = name == NULL ? 0 : strlen (name);
941 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
942 && i > 2 && name[i - 1] == ']')
943 {
944 enum bfd_endian byte_order = gdbarch_byte_order (exp->gdbarch);
945 CORE_ADDR address;
946 long length, index;
947 struct type *el_type;
948 gdb_byte buf4[4];
949
950 struct value *clas = java_class_from_object (arg1);
951 struct value *temp = clas;
952 /* Get CLASS_ELEMENT_TYPE of the array type. */
953 temp = value_struct_elt (&temp, NULL, "methods",
954 NULL, "structure");
955 deprecated_set_value_type (temp, value_type (clas));
956 el_type = type_from_class (exp->gdbarch, temp);
957 if (TYPE_CODE (el_type) == TYPE_CODE_STRUCT)
958 el_type = lookup_pointer_type (el_type);
959
960 if (noside == EVAL_AVOID_SIDE_EFFECTS)
961 return value_zero (el_type, VALUE_LVAL (arg1));
962 address = value_as_address (arg1);
963 address += get_java_object_header_size (exp->gdbarch);
964 read_memory (address, buf4, 4);
965 length = (long) extract_signed_integer (buf4, 4, byte_order);
966 index = (long) value_as_long (arg2);
967 if (index >= length || index < 0)
968 error (_("array index (%ld) out of bounds (length: %ld)"),
969 index, length);
970 address = (address + 4) + index * TYPE_LENGTH (el_type);
971 return value_at (el_type, address);
972 }
973 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
974 {
975 if (noside == EVAL_AVOID_SIDE_EFFECTS)
976 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
977 else
978 return value_subscript (arg1, value_as_long (arg2));
979 }
980 if (name)
981 error (_("cannot subscript something of type `%s'"), name);
982 else
983 error (_("cannot subscript requested type"));
984
985 case OP_STRING:
986 (*pos)++;
987 i = longest_to_int (exp->elts[pc + 1].longconst);
988 (*pos) += 3 + BYTES_TO_EXP_ELEM (i + 1);
989 if (noside == EVAL_SKIP)
990 goto nosideret;
991 return java_value_string (&exp->elts[pc + 2].string, i);
992
993 case STRUCTOP_PTR:
994 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
995 /* Convert object field (such as TYPE.class) to reference. */
996 if (TYPE_CODE (value_type (arg1)) == TYPE_CODE_STRUCT)
997 arg1 = value_addr (arg1);
998 return arg1;
999 default:
1000 break;
1001 }
1002 standard:
1003 return evaluate_subexp_standard (expect_type, exp, pos, noside);
1004 nosideret:
1005 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1006 }
1007
1008 static char *java_demangle (const char *mangled, int options)
1009 {
1010 return cplus_demangle (mangled, options | DMGL_JAVA);
1011 }
1012
1013 /* Find the member function name of the demangled name NAME. NAME
1014 must be a method name including arguments, in order to correctly
1015 locate the last component.
1016
1017 This function return a pointer to the first dot before the
1018 member function name, or NULL if the name was not of the
1019 expected form. */
1020
1021 static const char *
1022 java_find_last_component (const char *name)
1023 {
1024 const char *p;
1025
1026 /* Find argument list. */
1027 p = strchr (name, '(');
1028
1029 if (p == NULL)
1030 return NULL;
1031
1032 /* Back up and find first dot prior to argument list. */
1033 while (p > name && *p != '.')
1034 p--;
1035
1036 if (p == name)
1037 return NULL;
1038
1039 return p;
1040 }
1041
1042 /* Return the name of the class containing method PHYSNAME. */
1043
1044 static char *
1045 java_class_name_from_physname (const char *physname)
1046 {
1047 char *ret = NULL;
1048 const char *end;
1049 char *demangled_name = java_demangle (physname, DMGL_PARAMS | DMGL_ANSI);
1050
1051 if (demangled_name == NULL)
1052 return NULL;
1053
1054 end = java_find_last_component (demangled_name);
1055 if (end != NULL)
1056 {
1057 ret = xmalloc (end - demangled_name + 1);
1058 memcpy (ret, demangled_name, end - demangled_name);
1059 ret[end - demangled_name] = '\0';
1060 }
1061
1062 xfree (demangled_name);
1063 return ret;
1064 }
1065
1066 /* Table mapping opcodes into strings for printing operators
1067 and precedences of the operators. */
1068
1069 const struct op_print java_op_print_tab[] =
1070 {
1071 {",", BINOP_COMMA, PREC_COMMA, 0},
1072 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1073 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1074 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1075 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
1076 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
1077 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
1078 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1079 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1080 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1081 {">=", BINOP_GEQ, PREC_ORDER, 0},
1082 {">", BINOP_GTR, PREC_ORDER, 0},
1083 {"<", BINOP_LESS, PREC_ORDER, 0},
1084 {">>", BINOP_RSH, PREC_SHIFT, 0},
1085 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1086 {"+", BINOP_ADD, PREC_ADD, 0},
1087 {"-", BINOP_SUB, PREC_ADD, 0},
1088 {"*", BINOP_MUL, PREC_MUL, 0},
1089 {"/", BINOP_DIV, PREC_MUL, 0},
1090 {"%", BINOP_REM, PREC_MUL, 0},
1091 {"-", UNOP_NEG, PREC_PREFIX, 0},
1092 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1093 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
1094 {"*", UNOP_IND, PREC_PREFIX, 0},
1095 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1096 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1097 {NULL, 0, 0, 0}
1098 };
1099
1100 enum java_primitive_types
1101 {
1102 java_primitive_type_int,
1103 java_primitive_type_short,
1104 java_primitive_type_long,
1105 java_primitive_type_byte,
1106 java_primitive_type_boolean,
1107 java_primitive_type_char,
1108 java_primitive_type_float,
1109 java_primitive_type_double,
1110 java_primitive_type_void,
1111 nr_java_primitive_types
1112 };
1113
1114 static void
1115 java_language_arch_info (struct gdbarch *gdbarch,
1116 struct language_arch_info *lai)
1117 {
1118 const struct builtin_java_type *builtin = builtin_java_type (gdbarch);
1119
1120 lai->string_char_type = builtin->builtin_char;
1121 lai->primitive_type_vector
1122 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_java_primitive_types + 1,
1123 struct type *);
1124 lai->primitive_type_vector [java_primitive_type_int]
1125 = builtin->builtin_int;
1126 lai->primitive_type_vector [java_primitive_type_short]
1127 = builtin->builtin_short;
1128 lai->primitive_type_vector [java_primitive_type_long]
1129 = builtin->builtin_long;
1130 lai->primitive_type_vector [java_primitive_type_byte]
1131 = builtin->builtin_byte;
1132 lai->primitive_type_vector [java_primitive_type_boolean]
1133 = builtin->builtin_boolean;
1134 lai->primitive_type_vector [java_primitive_type_char]
1135 = builtin->builtin_char;
1136 lai->primitive_type_vector [java_primitive_type_float]
1137 = builtin->builtin_float;
1138 lai->primitive_type_vector [java_primitive_type_double]
1139 = builtin->builtin_double;
1140 lai->primitive_type_vector [java_primitive_type_void]
1141 = builtin->builtin_void;
1142
1143 lai->bool_type_symbol = "boolean";
1144 lai->bool_type_default = builtin->builtin_boolean;
1145 }
1146
1147 const struct exp_descriptor exp_descriptor_java =
1148 {
1149 print_subexp_standard,
1150 operator_length_standard,
1151 operator_check_standard,
1152 op_name_standard,
1153 dump_subexp_body_standard,
1154 evaluate_subexp_java
1155 };
1156
1157 const struct language_defn java_language_defn =
1158 {
1159 "java", /* Language name */
1160 language_java,
1161 range_check_off,
1162 type_check_off,
1163 case_sensitive_on,
1164 array_row_major,
1165 macro_expansion_no,
1166 &exp_descriptor_java,
1167 java_parse,
1168 java_error,
1169 null_post_parser,
1170 c_printchar, /* Print a character constant */
1171 c_printstr, /* Function to print string constant */
1172 java_emit_char, /* Function to print a single character */
1173 java_print_type, /* Print a type using appropriate syntax */
1174 default_print_typedef, /* Print a typedef using appropriate syntax */
1175 java_val_print, /* Print a value using appropriate syntax */
1176 java_value_print, /* Print a top-level value */
1177 NULL, /* Language specific skip_trampoline */
1178 "this", /* name_of_this */
1179 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1180 basic_lookup_transparent_type,/* lookup_transparent_type */
1181 java_demangle, /* Language specific symbol demangler */
1182 java_class_name_from_physname,/* Language specific class name */
1183 java_op_print_tab, /* expression operators for printing */
1184 0, /* not c-style arrays */
1185 0, /* String lower bound */
1186 default_word_break_characters,
1187 default_make_symbol_completion_list,
1188 java_language_arch_info,
1189 default_print_array_index,
1190 default_pass_by_reference,
1191 default_get_string,
1192 LANG_MAGIC
1193 };
1194
1195 static void *
1196 build_java_types (struct gdbarch *gdbarch)
1197 {
1198 struct builtin_java_type *builtin_java_type
1199 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_java_type);
1200
1201 builtin_java_type->builtin_int
1202 = arch_integer_type (gdbarch, 32, 0, "int");
1203 builtin_java_type->builtin_short
1204 = arch_integer_type (gdbarch, 16, 0, "short");
1205 builtin_java_type->builtin_long
1206 = arch_integer_type (gdbarch, 64, 0, "long");
1207 builtin_java_type->builtin_byte
1208 = arch_integer_type (gdbarch, 8, 0, "byte");
1209 builtin_java_type->builtin_boolean
1210 = arch_boolean_type (gdbarch, 8, 0, "boolean");
1211 builtin_java_type->builtin_char
1212 = arch_character_type (gdbarch, 16, 1, "char");
1213 builtin_java_type->builtin_float
1214 = arch_float_type (gdbarch, 32, "float", NULL);
1215 builtin_java_type->builtin_double
1216 = arch_float_type (gdbarch, 64, "double", NULL);
1217 builtin_java_type->builtin_void
1218 = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
1219
1220 return builtin_java_type;
1221 }
1222
1223 static struct gdbarch_data *java_type_data;
1224
1225 const struct builtin_java_type *
1226 builtin_java_type (struct gdbarch *gdbarch)
1227 {
1228 return gdbarch_data (gdbarch, java_type_data);
1229 }
1230
1231 void
1232 _initialize_java_language (void)
1233 {
1234 jv_dynamics_objfile_data_key
1235 = register_objfile_data_with_cleanup (NULL, jv_per_objfile_free);
1236 jv_type_objfile_data_key
1237 = register_objfile_data_with_cleanup (NULL, jv_clear_object_type);
1238
1239 java_type_data = gdbarch_data_register_post_init (build_java_types);
1240
1241 add_language (&java_language_defn);
1242 }
This page took 0.0824549999999999 seconds and 4 git commands to generate.