1 /* Java language support routines for GDB, the GNU debugger.
3 Copyright (C) 1997-2013 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "expression.h"
24 #include "parser-defs.h"
30 #include "gdb_string.h"
37 #include "dictionary.h"
39 #include "gdb_assert.h"
42 #include "cp-support.h"
46 extern void _initialize_java_language (void);
48 static int java_demangled_signature_length (const char *);
49 static void java_demangled_signature_copy (char *, const char *);
51 static struct symtab *get_java_class_symtab (struct gdbarch *gdbarch);
52 static char *get_java_utf8_name (struct obstack *obstack, struct value *name);
53 static int java_class_is_primitive (struct value *clas);
54 static struct value *java_value_string (char *ptr, int len);
56 static void java_emit_char (int c, struct type *type,
57 struct ui_file * stream, int quoter);
59 static char *java_class_name_from_physname (const char *physname);
61 static const struct objfile_data *jv_dynamics_objfile_data_key;
63 /* The dynamic objfile is kept per-program-space. This key lets us
64 associate the objfile with the program space. */
66 static const struct program_space_data *jv_dynamics_progspace_key;
68 static struct type *java_link_class_type (struct gdbarch *,
69 struct type *, struct value *);
71 /* An instance of this structure is used to store some data that must
74 struct jv_per_objfile_data
76 /* The expandable dictionary we use. */
77 struct dictionary *dict;
80 /* A function called when the dynamics_objfile is freed. We use this
81 to clean up some internal state. */
83 jv_per_objfile_free (struct objfile *objfile, void *data)
85 struct jv_per_objfile_data *jv_data = data;
86 struct objfile *dynamics_objfile;
88 dynamics_objfile = program_space_data (current_program_space,
89 jv_dynamics_progspace_key);
90 gdb_assert (objfile == dynamics_objfile);
93 dict_free (jv_data->dict);
96 set_program_space_data (current_program_space,
97 jv_dynamics_progspace_key,
101 /* FIXME: carlton/2003-02-04: This is the main or only caller of
102 allocate_objfile with first argument NULL; as a result, this code
103 breaks every so often. Somebody should write a test case that
104 exercises GDB in various ways (e.g. something involving loading a
105 dynamic library) after this code has been called. */
107 static struct objfile *
108 get_dynamics_objfile (struct gdbarch *gdbarch)
110 struct objfile *dynamics_objfile;
112 dynamics_objfile = program_space_data (current_program_space,
113 jv_dynamics_progspace_key);
115 if (dynamics_objfile == NULL)
117 struct jv_per_objfile_data *data;
119 /* Mark it as shared so that it is cleared when the inferior is
121 dynamics_objfile = allocate_objfile (NULL, OBJF_SHARED);
122 dynamics_objfile->gdbarch = gdbarch;
124 data = XCNEW (struct jv_per_objfile_data);
125 set_objfile_data (dynamics_objfile, jv_dynamics_objfile_data_key, data);
127 set_program_space_data (current_program_space,
128 jv_dynamics_progspace_key,
131 return dynamics_objfile;
134 static struct symtab *
135 get_java_class_symtab (struct gdbarch *gdbarch)
137 struct objfile *objfile = get_dynamics_objfile (gdbarch);
138 struct symtab *class_symtab = objfile->symtabs;
140 if (class_symtab == NULL)
142 struct blockvector *bv;
144 struct jv_per_objfile_data *jv_data;
146 class_symtab = allocate_symtab ("<java-classes>", objfile);
147 class_symtab->language = language_java;
148 bv = (struct blockvector *)
149 obstack_alloc (&objfile->objfile_obstack,
150 sizeof (struct blockvector) + sizeof (struct block *));
151 BLOCKVECTOR_NBLOCKS (bv) = 1;
152 BLOCKVECTOR (class_symtab) = bv;
154 /* Allocate dummy STATIC_BLOCK. */
155 bl = allocate_block (&objfile->objfile_obstack);
156 BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
158 BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
160 /* Allocate GLOBAL_BLOCK. */
161 bl = allocate_global_block (&objfile->objfile_obstack);
162 BLOCK_DICT (bl) = dict_create_hashed_expandable ();
163 set_block_symtab (bl, class_symtab);
164 BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
166 /* Arrange to free the dict. */
167 jv_data = objfile_data (objfile, jv_dynamics_objfile_data_key);
168 jv_data->dict = BLOCK_DICT (bl);
174 add_class_symtab_symbol (struct symbol *sym)
176 struct symtab *symtab
177 = get_java_class_symtab (get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile));
178 struct blockvector *bv = BLOCKVECTOR (symtab);
180 dict_add_symbol (BLOCK_DICT (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)), sym);
183 static struct symbol *
184 add_class_symbol (struct type *type, CORE_ADDR addr)
187 struct objfile *objfile = get_dynamics_objfile (get_type_arch (type));
189 sym = allocate_symbol (objfile);
190 SYMBOL_SET_LANGUAGE (sym, language_java, &objfile->objfile_obstack);
191 SYMBOL_SET_LINKAGE_NAME (sym, TYPE_TAG_NAME (type));
192 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
193 /* SYMBOL_VALUE (sym) = valu; */
194 SYMBOL_TYPE (sym) = type;
195 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
196 SYMBOL_VALUE_ADDRESS (sym) = addr;
201 java_lookup_class (char *name)
205 sym = lookup_symbol (name, expression_context_block, STRUCT_DOMAIN, NULL);
207 return SYMBOL_TYPE (sym);
208 /* FIXME - should search inferior's symbol table. */
212 /* Return a nul-terminated string (allocated on OBSTACK) for
213 a name given by NAME (which has type Utf8Const*). */
216 get_java_utf8_name (struct obstack *obstack, struct value *name)
219 struct value *temp = name;
223 temp = value_struct_elt (&temp, NULL, "length", NULL, "structure");
224 name_length = (int) value_as_long (temp);
225 data_addr = value_address (temp) + TYPE_LENGTH (value_type (temp));
226 chrs = obstack_alloc (obstack, name_length + 1);
227 chrs[name_length] = '\0';
228 read_memory (data_addr, (gdb_byte *) chrs, name_length);
233 java_class_from_object (struct value *obj_val)
235 /* This is all rather inefficient, since the offsets of vtable and
236 class are fixed. FIXME */
237 struct value *vtable_val;
239 if (TYPE_CODE (value_type (obj_val)) == TYPE_CODE_PTR
240 && TYPE_LENGTH (TYPE_TARGET_TYPE (value_type (obj_val))) == 0)
241 obj_val = value_at (get_java_object_type (),
242 value_as_address (obj_val));
244 vtable_val = value_struct_elt (&obj_val, NULL, "vtable", NULL, "structure");
245 return value_struct_elt (&vtable_val, NULL, "class", NULL, "structure");
248 /* Check if CLASS_IS_PRIMITIVE(value of clas): */
250 java_class_is_primitive (struct value *clas)
252 struct value *vtable = value_struct_elt (&clas, NULL, "vtable",
254 CORE_ADDR i = value_as_address (vtable);
256 return (int) (i & 0x7fffffff) == (int) 0x7fffffff;
259 /* Read a GCJ Class object, and generated a gdb (TYPE_CODE_STRUCT) type. */
262 type_from_class (struct gdbarch *gdbarch, struct value *clas)
267 struct objfile *objfile;
268 struct value *utf8_name;
272 type = check_typedef (value_type (clas));
273 if (TYPE_CODE (type) == TYPE_CODE_PTR)
275 if (value_logical_not (clas))
277 clas = value_ind (clas);
279 addr = value_address (clas);
281 objfile = get_dynamics_objfile (gdbarch);
282 if (java_class_is_primitive (clas))
287 sig = value_struct_elt (&temp, NULL, "method_count", NULL, "structure");
288 return java_primitive_type (gdbarch, value_as_long (sig));
291 /* Get Class name. */
292 /* If clasloader non-null, prepend loader address. FIXME */
294 utf8_name = value_struct_elt (&temp, NULL, "name", NULL, "structure");
295 name = get_java_utf8_name (&objfile->objfile_obstack, utf8_name);
296 for (nptr = name; *nptr != 0; nptr++)
302 type = java_lookup_class (name);
306 type = alloc_type (objfile);
307 TYPE_CODE (type) = TYPE_CODE_STRUCT;
308 INIT_CPLUS_SPECIFIC (type);
312 char *signature = name;
313 int namelen = java_demangled_signature_length (signature);
315 if (namelen > strlen (name))
316 name = obstack_alloc (&objfile->objfile_obstack, namelen + 1);
317 java_demangled_signature_copy (name, signature);
318 name[namelen] = '\0';
320 /* Set array element type. */
321 temp = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
322 deprecated_set_value_type (temp,
323 lookup_pointer_type (value_type (clas)));
324 TYPE_TARGET_TYPE (type) = type_from_class (gdbarch, temp);
327 ALLOCATE_CPLUS_STRUCT_TYPE (type);
328 TYPE_TAG_NAME (type) = name;
330 add_class_symtab_symbol (add_class_symbol (type, addr));
331 return java_link_class_type (gdbarch, type, clas);
334 /* Fill in class TYPE with data from the CLAS value. */
337 java_link_class_type (struct gdbarch *gdbarch,
338 struct type *type, struct value *clas)
341 const char *unqualified_name;
342 const char *name = TYPE_TAG_NAME (type);
343 int ninterfaces, nfields, nmethods;
344 int type_is_object = 0;
345 struct fn_field *fn_fields;
346 struct fn_fieldlist *fn_fieldlists;
347 struct value *fields;
348 struct value *methods;
349 struct value *method = NULL;
350 struct value *field = NULL;
352 struct objfile *objfile = get_dynamics_objfile (gdbarch);
355 gdb_assert (name != NULL);
356 unqualified_name = strrchr (name, '.');
357 if (unqualified_name == NULL)
358 unqualified_name = name;
361 temp = value_struct_elt (&temp, NULL, "superclass", NULL, "structure");
362 if (strcmp (name, "java.lang.Object") == 0)
364 tsuper = get_java_object_type ();
365 if (tsuper && TYPE_CODE (tsuper) == TYPE_CODE_PTR)
366 tsuper = TYPE_TARGET_TYPE (tsuper);
370 tsuper = type_from_class (gdbarch, temp);
376 ninterfaces = value_as_long (value_struct_elt (&temp, NULL, "interface_len",
379 TYPE_N_BASECLASSES (type) = (tsuper == NULL ? 0 : 1) + ninterfaces;
381 nfields = value_as_long (value_struct_elt (&temp, NULL, "field_count",
383 nfields += TYPE_N_BASECLASSES (type);
384 nfields++; /* Add one for dummy "class" field. */
385 TYPE_NFIELDS (type) = nfields;
386 TYPE_FIELDS (type) = (struct field *)
387 TYPE_ALLOC (type, sizeof (struct field) * nfields);
389 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
391 TYPE_FIELD_PRIVATE_BITS (type) =
392 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
393 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
395 TYPE_FIELD_PROTECTED_BITS (type) =
396 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
397 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
399 TYPE_FIELD_IGNORE_BITS (type) =
400 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
401 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
403 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *)
404 TYPE_ALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type)));
405 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
409 TYPE_BASECLASS (type, 0) = tsuper;
411 SET_TYPE_FIELD_PRIVATE (type, 0);
415 if (i > 2 && name[i - 1] == ']' && tsuper != NULL)
418 TYPE_LENGTH (type) = TYPE_LENGTH (tsuper) + 4; /* size with "length" */
423 temp = value_struct_elt (&temp, NULL, "size_in_bytes",
425 TYPE_LENGTH (type) = value_as_long (temp);
429 nfields--; /* First set up dummy "class" field. */
430 SET_FIELD_PHYSADDR (TYPE_FIELD (type, nfields), value_address (clas));
431 TYPE_FIELD_NAME (type, nfields) = "class";
432 TYPE_FIELD_TYPE (type, nfields) = value_type (clas);
433 SET_TYPE_FIELD_PRIVATE (type, nfields);
435 for (i = TYPE_N_BASECLASSES (type); i < nfields; i++)
443 fields = value_struct_elt (&temp, NULL, "fields", NULL, "structure");
444 field = value_ind (fields);
447 { /* Re-use field value for next field. */
449 = value_address (field) + TYPE_LENGTH (value_type (field));
451 set_value_address (field, addr);
452 set_value_lazy (field, 1);
455 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
456 TYPE_FIELD_NAME (type, i) =
457 get_java_utf8_name (&objfile->objfile_obstack, temp);
459 accflags = value_as_long (value_struct_elt (&temp, NULL, "accflags",
462 temp = value_struct_elt (&temp, NULL, "info", NULL, "structure");
463 boffset = value_as_long (value_struct_elt (&temp, NULL, "boffset",
465 if (accflags & 0x0001) /* public access */
469 if (accflags & 0x0002) /* private access */
471 SET_TYPE_FIELD_PRIVATE (type, i);
473 if (accflags & 0x0004) /* protected access */
475 SET_TYPE_FIELD_PROTECTED (type, i);
477 if (accflags & 0x0008) /* ACC_STATIC */
478 SET_FIELD_PHYSADDR (TYPE_FIELD (type, i), boffset);
480 SET_FIELD_BITPOS (TYPE_FIELD (type, i), 8 * boffset);
481 if (accflags & 0x8000) /* FIELD_UNRESOLVED_FLAG */
483 TYPE_FIELD_TYPE (type, i) = get_java_object_type (); /* FIXME */
490 temp = value_struct_elt (&temp, NULL, "type", NULL, "structure");
491 ftype = type_from_class (gdbarch, temp);
492 if (TYPE_CODE (ftype) == TYPE_CODE_STRUCT)
493 ftype = lookup_pointer_type (ftype);
494 TYPE_FIELD_TYPE (type, i) = ftype;
499 nmethods = value_as_long (value_struct_elt (&temp, NULL, "method_count",
501 j = nmethods * sizeof (struct fn_field);
502 fn_fields = (struct fn_field *)
503 obstack_alloc (&objfile->objfile_obstack, j);
504 memset (fn_fields, 0, j);
505 fn_fieldlists = (struct fn_fieldlist *)
506 alloca (nmethods * sizeof (struct fn_fieldlist));
509 for (i = 0; i < nmethods; i++)
517 methods = value_struct_elt (&temp, NULL, "methods",
519 method = value_ind (methods);
522 { /* Re-use method value for next method. */
524 = value_address (method) + TYPE_LENGTH (value_type (method));
526 set_value_address (method, addr);
527 set_value_lazy (method, 1);
530 /* Get method name. */
532 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
533 mname = get_java_utf8_name (&objfile->objfile_obstack, temp);
534 if (strcmp (mname, "<init>") == 0)
535 mname = unqualified_name;
537 /* Check for an existing method with the same name.
538 * This makes building the fn_fieldslists an O(nmethods**2)
539 * operation. That could be using hashing, but I doubt it
540 * is worth it. Note that we do maintain the order of methods
541 * in the inferior's Method table (as long as that is grouped
542 * by method name), which I think is desirable. --PB */
543 for (k = 0, j = TYPE_NFN_FIELDS (type);;)
546 { /* No match - new method name. */
547 j = TYPE_NFN_FIELDS (type)++;
548 fn_fieldlists[j].name = mname;
549 fn_fieldlists[j].length = 1;
550 fn_fieldlists[j].fn_fields = &fn_fields[i];
554 if (strcmp (mname, fn_fieldlists[j].name) == 0)
555 { /* Found an existing method with the same name. */
558 if (mname != unqualified_name)
559 obstack_free (&objfile->objfile_obstack, mname);
560 mname = fn_fieldlists[j].name;
561 fn_fieldlists[j].length++;
562 k = i - k; /* Index of new slot. */
563 /* Shift intervening fn_fields (between k and i) down. */
564 for (l = i; l > k; l--)
565 fn_fields[l] = fn_fields[l - 1];
566 for (l = TYPE_NFN_FIELDS (type); --l > j;)
567 fn_fieldlists[l].fn_fields++;
570 k += fn_fieldlists[j].length;
572 fn_fields[k].physname = "";
573 fn_fields[k].is_stub = 1;
575 fn_fields[k].type = lookup_function_type
576 (builtin_java_type (gdbarch)->builtin_void);
577 TYPE_CODE (fn_fields[k].type) = TYPE_CODE_METHOD;
580 j = TYPE_NFN_FIELDS (type) * sizeof (struct fn_fieldlist);
581 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
582 obstack_alloc (&objfile->objfile_obstack, j);
583 memcpy (TYPE_FN_FIELDLISTS (type), fn_fieldlists, j);
589 get_java_object_type (void)
593 sym = lookup_symbol ("java.lang.Object", NULL, STRUCT_DOMAIN, NULL);
595 error (_("cannot find java.lang.Object"));
596 return SYMBOL_TYPE (sym);
600 get_java_object_header_size (struct gdbarch *gdbarch)
602 struct type *objtype = get_java_object_type ();
605 return (2 * gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT);
607 return TYPE_LENGTH (objtype);
611 is_object_type (struct type *type)
613 CHECK_TYPEDEF (type);
614 if (TYPE_CODE (type) == TYPE_CODE_PTR)
616 struct type *ttype = check_typedef (TYPE_TARGET_TYPE (type));
618 if (TYPE_CODE (ttype) != TYPE_CODE_STRUCT)
620 while (TYPE_N_BASECLASSES (ttype) > 0)
621 ttype = TYPE_BASECLASS (ttype, 0);
622 name = TYPE_TAG_NAME (ttype);
623 if (name != NULL && strcmp (name, "java.lang.Object") == 0)
626 = TYPE_NFIELDS (ttype) > 0 ? TYPE_FIELD_NAME (ttype, 0) : (char *) 0;
627 if (name != NULL && strcmp (name, "vtable") == 0)
634 java_primitive_type (struct gdbarch *gdbarch, int signature)
636 const struct builtin_java_type *builtin = builtin_java_type (gdbarch);
641 return builtin->builtin_byte;
643 return builtin->builtin_short;
645 return builtin->builtin_int;
647 return builtin->builtin_long;
649 return builtin->builtin_boolean;
651 return builtin->builtin_char;
653 return builtin->builtin_float;
655 return builtin->builtin_double;
657 return builtin->builtin_void;
659 error (_("unknown signature '%c' for primitive type"), (char) signature);
662 /* If name[0 .. namelen-1] is the name of a primitive Java type,
663 return that type. Otherwise, return NULL. */
666 java_primitive_type_from_name (struct gdbarch *gdbarch,
667 const char *name, int namelen)
669 const struct builtin_java_type *builtin = builtin_java_type (gdbarch);
674 if (namelen == 4 && memcmp (name, "byte", 4) == 0)
675 return builtin->builtin_byte;
676 if (namelen == 7 && memcmp (name, "boolean", 7) == 0)
677 return builtin->builtin_boolean;
680 if (namelen == 4 && memcmp (name, "char", 4) == 0)
681 return builtin->builtin_char;
684 if (namelen == 6 && memcmp (name, "double", 6) == 0)
685 return builtin->builtin_double;
688 if (namelen == 5 && memcmp (name, "float", 5) == 0)
689 return builtin->builtin_float;
692 if (namelen == 3 && memcmp (name, "int", 3) == 0)
693 return builtin->builtin_int;
696 if (namelen == 4 && memcmp (name, "long", 4) == 0)
697 return builtin->builtin_long;
700 if (namelen == 5 && memcmp (name, "short", 5) == 0)
701 return builtin->builtin_short;
704 if (namelen == 4 && memcmp (name, "void", 4) == 0)
705 return builtin->builtin_void;
712 java_primitive_type_name (int signature)
735 error (_("unknown signature '%c' for primitive type"), (char) signature);
738 /* Return the length (in bytes) of demangled name of the Java type
739 signature string SIGNATURE. */
742 java_demangled_signature_length (const char *signature)
746 for (; *signature == '['; signature++)
747 array += 2; /* Two chars for "[]". */
748 switch (signature[0])
751 /* Subtract 2 for 'L' and ';'. */
752 return strlen (signature) - 2 + array;
754 return strlen (java_primitive_type_name (signature[0])) + array;
758 /* Demangle the Java type signature SIGNATURE, leaving the result in
762 java_demangled_signature_copy (char *result, const char *signature)
768 while (*signature == '[')
773 switch (signature[0])
776 /* Subtract 2 for 'L' and ';', but add 1 for final nul. */
779 for (; *signature != ';' && *signature != '\0'; signature++)
781 if (*signature == '/')
788 ptr = java_primitive_type_name (signature[0]);
790 strcpy (result, ptr);
801 /* Return the demangled name of the Java type signature string SIGNATURE,
802 as a freshly allocated copy. */
805 java_demangle_type_signature (const char *signature)
807 int length = java_demangled_signature_length (signature);
808 char *result = xmalloc (length + 1);
810 java_demangled_signature_copy (result, signature);
811 result[length] = '\0';
815 /* Return the type of TYPE followed by DIMS pairs of [ ].
816 If DIMS == 0, TYPE is returned. */
819 java_array_type (struct type *type, int dims)
823 /* FIXME This is bogus! Java arrays are not gdb arrays! */
824 type = lookup_array_range_type (type, 0, 0);
830 /* Create a Java string in the inferior from a (Utf8) literal. */
832 static struct value *
833 java_value_string (char *ptr, int len)
835 error (_("not implemented - java_value_string")); /* FIXME */
838 /* Return the encoding that should be used for the character type
842 java_get_encoding (struct type *type)
844 struct gdbarch *arch = get_type_arch (type);
845 const char *encoding;
847 if (type == builtin_java_type (arch)->builtin_char)
849 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
850 encoding = "UTF-16BE";
852 encoding = "UTF-16LE";
855 encoding = target_charset (arch);
860 /* Print the character C on STREAM as part of the contents of a literal
861 string whose delimiter is QUOTER. Note that that format for printing
862 characters and strings is language specific. */
865 java_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
867 const char *encoding = java_get_encoding (type);
869 generic_emit_char (c, type, stream, quoter, encoding);
872 /* Implementation of la_printchar method. */
875 java_printchar (int c, struct type *type, struct ui_file *stream)
877 fputs_filtered ("'", stream);
878 LA_EMIT_CHAR (c, type, stream, '\'');
879 fputs_filtered ("'", stream);
882 /* Implementation of la_printstr method. */
885 java_printstr (struct ui_file *stream, struct type *type,
886 const gdb_byte *string,
887 unsigned int length, const char *encoding, int force_ellipses,
888 const struct value_print_options *options)
890 const char *type_encoding = java_get_encoding (type);
892 if (!encoding || !*encoding)
893 encoding = type_encoding;
895 generic_printstr (stream, type, string, length, encoding,
896 force_ellipses, '"', 0, options);
899 static struct value *
900 evaluate_subexp_java (struct type *expect_type, struct expression *exp,
901 int *pos, enum noside noside)
906 enum exp_opcode op = exp->elts[*pos].opcode;
914 if (noside == EVAL_SKIP)
917 arg1 = evaluate_subexp_java (NULL_TYPE, exp, pos, EVAL_NORMAL);
918 if (is_object_type (value_type (arg1)))
922 type = type_from_class (exp->gdbarch, java_class_from_object (arg1));
923 arg1 = value_cast (lookup_pointer_type (type), arg1);
925 return value_ind (arg1);
927 case BINOP_SUBSCRIPT:
929 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
930 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
931 if (noside == EVAL_SKIP)
933 /* If the user attempts to subscript something that is not an
934 array or pointer type (like a plain int variable for example),
935 then report this as an error. */
937 arg1 = coerce_ref (arg1);
938 type = check_typedef (value_type (arg1));
939 if (TYPE_CODE (type) == TYPE_CODE_PTR)
940 type = check_typedef (TYPE_TARGET_TYPE (type));
941 name = TYPE_NAME (type);
943 name = TYPE_TAG_NAME (type);
944 i = name == NULL ? 0 : strlen (name);
945 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
946 && i > 2 && name[i - 1] == ']')
948 enum bfd_endian byte_order = gdbarch_byte_order (exp->gdbarch);
951 struct type *el_type;
954 struct value *clas = java_class_from_object (arg1);
955 struct value *temp = clas;
956 /* Get CLASS_ELEMENT_TYPE of the array type. */
957 temp = value_struct_elt (&temp, NULL, "methods",
959 deprecated_set_value_type (temp, value_type (clas));
960 el_type = type_from_class (exp->gdbarch, temp);
961 if (TYPE_CODE (el_type) == TYPE_CODE_STRUCT)
962 el_type = lookup_pointer_type (el_type);
964 if (noside == EVAL_AVOID_SIDE_EFFECTS)
965 return value_zero (el_type, VALUE_LVAL (arg1));
966 address = value_as_address (arg1);
967 address += get_java_object_header_size (exp->gdbarch);
968 read_memory (address, buf4, 4);
969 length = (long) extract_signed_integer (buf4, 4, byte_order);
970 index = (long) value_as_long (arg2);
971 if (index >= length || index < 0)
972 error (_("array index (%ld) out of bounds (length: %ld)"),
974 address = (address + 4) + index * TYPE_LENGTH (el_type);
975 return value_at (el_type, address);
977 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
979 if (noside == EVAL_AVOID_SIDE_EFFECTS)
980 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
982 return value_subscript (arg1, value_as_long (arg2));
985 error (_("cannot subscript something of type `%s'"), name);
987 error (_("cannot subscript requested type"));
991 i = longest_to_int (exp->elts[pc + 1].longconst);
992 (*pos) += 3 + BYTES_TO_EXP_ELEM (i + 1);
993 if (noside == EVAL_SKIP)
995 return java_value_string (&exp->elts[pc + 2].string, i);
998 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
999 /* Convert object field (such as TYPE.class) to reference. */
1000 if (TYPE_CODE (value_type (arg1)) == TYPE_CODE_STRUCT)
1001 arg1 = value_addr (arg1);
1007 return evaluate_subexp_standard (expect_type, exp, pos, noside);
1009 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1012 static char *java_demangle (const char *mangled, int options)
1014 return gdb_demangle (mangled, options | DMGL_JAVA);
1017 /* Find the member function name of the demangled name NAME. NAME
1018 must be a method name including arguments, in order to correctly
1019 locate the last component.
1021 This function return a pointer to the first dot before the
1022 member function name, or NULL if the name was not of the
1026 java_find_last_component (const char *name)
1030 /* Find argument list. */
1031 p = strchr (name, '(');
1036 /* Back up and find first dot prior to argument list. */
1037 while (p > name && *p != '.')
1046 /* Return the name of the class containing method PHYSNAME. */
1049 java_class_name_from_physname (const char *physname)
1053 char *demangled_name = java_demangle (physname, DMGL_PARAMS | DMGL_ANSI);
1055 if (demangled_name == NULL)
1058 end = java_find_last_component (demangled_name);
1061 ret = xmalloc (end - demangled_name + 1);
1062 memcpy (ret, demangled_name, end - demangled_name);
1063 ret[end - demangled_name] = '\0';
1066 xfree (demangled_name);
1070 /* Table mapping opcodes into strings for printing operators
1071 and precedences of the operators. */
1073 const struct op_print java_op_print_tab[] =
1075 {",", BINOP_COMMA, PREC_COMMA, 0},
1076 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1077 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1078 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1079 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
1080 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
1081 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
1082 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1083 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1084 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1085 {">=", BINOP_GEQ, PREC_ORDER, 0},
1086 {">", BINOP_GTR, PREC_ORDER, 0},
1087 {"<", BINOP_LESS, PREC_ORDER, 0},
1088 {">>", BINOP_RSH, PREC_SHIFT, 0},
1089 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1090 {"+", BINOP_ADD, PREC_ADD, 0},
1091 {"-", BINOP_SUB, PREC_ADD, 0},
1092 {"*", BINOP_MUL, PREC_MUL, 0},
1093 {"/", BINOP_DIV, PREC_MUL, 0},
1094 {"%", BINOP_REM, PREC_MUL, 0},
1095 {"-", UNOP_NEG, PREC_PREFIX, 0},
1096 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1097 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
1098 {"*", UNOP_IND, PREC_PREFIX, 0},
1099 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1100 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1104 enum java_primitive_types
1106 java_primitive_type_int,
1107 java_primitive_type_short,
1108 java_primitive_type_long,
1109 java_primitive_type_byte,
1110 java_primitive_type_boolean,
1111 java_primitive_type_char,
1112 java_primitive_type_float,
1113 java_primitive_type_double,
1114 java_primitive_type_void,
1115 nr_java_primitive_types
1119 java_language_arch_info (struct gdbarch *gdbarch,
1120 struct language_arch_info *lai)
1122 const struct builtin_java_type *builtin = builtin_java_type (gdbarch);
1124 lai->string_char_type = builtin->builtin_char;
1125 lai->primitive_type_vector
1126 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_java_primitive_types + 1,
1128 lai->primitive_type_vector [java_primitive_type_int]
1129 = builtin->builtin_int;
1130 lai->primitive_type_vector [java_primitive_type_short]
1131 = builtin->builtin_short;
1132 lai->primitive_type_vector [java_primitive_type_long]
1133 = builtin->builtin_long;
1134 lai->primitive_type_vector [java_primitive_type_byte]
1135 = builtin->builtin_byte;
1136 lai->primitive_type_vector [java_primitive_type_boolean]
1137 = builtin->builtin_boolean;
1138 lai->primitive_type_vector [java_primitive_type_char]
1139 = builtin->builtin_char;
1140 lai->primitive_type_vector [java_primitive_type_float]
1141 = builtin->builtin_float;
1142 lai->primitive_type_vector [java_primitive_type_double]
1143 = builtin->builtin_double;
1144 lai->primitive_type_vector [java_primitive_type_void]
1145 = builtin->builtin_void;
1147 lai->bool_type_symbol = "boolean";
1148 lai->bool_type_default = builtin->builtin_boolean;
1151 const struct exp_descriptor exp_descriptor_java =
1153 print_subexp_standard,
1154 operator_length_standard,
1155 operator_check_standard,
1157 dump_subexp_body_standard,
1158 evaluate_subexp_java
1161 const struct language_defn java_language_defn =
1163 "java", /* Language name */
1169 &exp_descriptor_java,
1173 java_printchar, /* Print a character constant */
1174 java_printstr, /* Function to print string constant */
1175 java_emit_char, /* Function to print a single character */
1176 java_print_type, /* Print a type using appropriate syntax */
1177 default_print_typedef, /* Print a typedef using appropriate syntax */
1178 java_val_print, /* Print a value using appropriate syntax */
1179 java_value_print, /* Print a top-level value */
1180 default_read_var_value, /* la_read_var_value */
1181 NULL, /* Language specific skip_trampoline */
1182 "this", /* name_of_this */
1183 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1184 basic_lookup_transparent_type,/* lookup_transparent_type */
1185 java_demangle, /* Language specific symbol demangler */
1186 java_class_name_from_physname,/* Language specific class name */
1187 java_op_print_tab, /* expression operators for printing */
1188 0, /* not c-style arrays */
1189 0, /* String lower bound */
1190 default_word_break_characters,
1191 default_make_symbol_completion_list,
1192 java_language_arch_info,
1193 default_print_array_index,
1194 default_pass_by_reference,
1196 NULL, /* la_get_symbol_name_cmp */
1197 iterate_over_symbols,
1202 build_java_types (struct gdbarch *gdbarch)
1204 struct builtin_java_type *builtin_java_type
1205 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_java_type);
1207 builtin_java_type->builtin_int
1208 = arch_integer_type (gdbarch, 32, 0, "int");
1209 builtin_java_type->builtin_short
1210 = arch_integer_type (gdbarch, 16, 0, "short");
1211 builtin_java_type->builtin_long
1212 = arch_integer_type (gdbarch, 64, 0, "long");
1213 builtin_java_type->builtin_byte
1214 = arch_integer_type (gdbarch, 8, 0, "byte");
1215 builtin_java_type->builtin_boolean
1216 = arch_boolean_type (gdbarch, 8, 0, "boolean");
1217 builtin_java_type->builtin_char
1218 = arch_character_type (gdbarch, 16, 1, "char");
1219 builtin_java_type->builtin_float
1220 = arch_float_type (gdbarch, 32, "float", NULL);
1221 builtin_java_type->builtin_double
1222 = arch_float_type (gdbarch, 64, "double", NULL);
1223 builtin_java_type->builtin_void
1224 = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
1226 return builtin_java_type;
1229 static struct gdbarch_data *java_type_data;
1231 const struct builtin_java_type *
1232 builtin_java_type (struct gdbarch *gdbarch)
1234 return gdbarch_data (gdbarch, java_type_data);
1238 _initialize_java_language (void)
1240 jv_dynamics_objfile_data_key
1241 = register_objfile_data_with_cleanup (NULL, jv_per_objfile_free);
1242 jv_dynamics_progspace_key = register_program_space_data ();
1244 java_type_data = gdbarch_data_register_post_init (build_java_types);
1246 add_language (&java_language_defn);