1 /* Java language support routines for GDB, the GNU debugger.
3 Copyright (C) 1997-2014 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"
36 #include "dictionary.h"
40 #include "cp-support.h"
44 extern void _initialize_java_language (void);
46 static int java_demangled_signature_length (const char *);
47 static void java_demangled_signature_copy (char *, const char *);
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);
54 static void java_emit_char (int c, struct type *type,
55 struct ui_file * stream, int quoter);
57 static char *java_class_name_from_physname (const char *physname);
59 static const struct objfile_data *jv_dynamics_objfile_data_key;
61 /* The dynamic objfile is kept per-program-space. This key lets us
62 associate the objfile with the program space. */
64 static const struct program_space_data *jv_dynamics_progspace_key;
66 static struct type *java_link_class_type (struct gdbarch *,
67 struct type *, struct value *);
69 /* An instance of this structure is used to store some data that must
72 struct jv_per_objfile_data
74 /* The expandable dictionary we use. */
75 struct dictionary *dict;
78 /* A function called when the dynamics_objfile is freed. We use this
79 to clean up some internal state. */
81 jv_per_objfile_free (struct objfile *objfile, void *data)
83 struct jv_per_objfile_data *jv_data = data;
84 struct objfile *dynamics_objfile;
86 dynamics_objfile = program_space_data (current_program_space,
87 jv_dynamics_progspace_key);
88 gdb_assert (objfile == dynamics_objfile);
91 dict_free (jv_data->dict);
94 set_program_space_data (current_program_space,
95 jv_dynamics_progspace_key,
99 /* FIXME: carlton/2003-02-04: This is the main or only caller of
100 allocate_objfile with first argument NULL; as a result, this code
101 breaks every so often. Somebody should write a test case that
102 exercises GDB in various ways (e.g. something involving loading a
103 dynamic library) after this code has been called. */
105 static struct objfile *
106 get_dynamics_objfile (struct gdbarch *gdbarch)
108 struct objfile *dynamics_objfile;
110 dynamics_objfile = program_space_data (current_program_space,
111 jv_dynamics_progspace_key);
113 if (dynamics_objfile == NULL)
115 struct jv_per_objfile_data *data;
117 /* Mark it as shared so that it is cleared when the inferior is
119 dynamics_objfile = allocate_objfile (NULL, NULL,
120 OBJF_SHARED | OBJF_NOT_FILENAME);
121 dynamics_objfile->per_bfd->gdbarch = gdbarch;
123 data = XCNEW (struct jv_per_objfile_data);
124 set_objfile_data (dynamics_objfile, jv_dynamics_objfile_data_key, data);
126 set_program_space_data (current_program_space,
127 jv_dynamics_progspace_key,
130 return dynamics_objfile;
133 static struct symtab *
134 get_java_class_symtab (struct gdbarch *gdbarch)
136 struct objfile *objfile = get_dynamics_objfile (gdbarch);
137 struct symtab *class_symtab = objfile->symtabs;
139 if (class_symtab == NULL)
141 struct blockvector *bv;
143 struct jv_per_objfile_data *jv_data;
145 class_symtab = allocate_symtab ("<java-classes>", objfile);
146 class_symtab->language = language_java;
147 bv = (struct blockvector *)
148 obstack_alloc (&objfile->objfile_obstack,
149 sizeof (struct blockvector) + sizeof (struct block *));
150 BLOCKVECTOR_NBLOCKS (bv) = 1;
151 BLOCKVECTOR (class_symtab) = bv;
153 /* Allocate dummy STATIC_BLOCK. */
154 bl = allocate_block (&objfile->objfile_obstack);
155 BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
157 BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
159 /* Allocate GLOBAL_BLOCK. */
160 bl = allocate_global_block (&objfile->objfile_obstack);
161 BLOCK_DICT (bl) = dict_create_hashed_expandable ();
162 set_block_symtab (bl, class_symtab);
163 BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
165 /* Arrange to free the dict. */
166 jv_data = objfile_data (objfile, jv_dynamics_objfile_data_key);
167 jv_data->dict = BLOCK_DICT (bl);
173 add_class_symtab_symbol (struct symbol *sym)
175 struct symtab *symtab
176 = get_java_class_symtab (get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile));
177 const struct blockvector *bv = BLOCKVECTOR (symtab);
179 dict_add_symbol (BLOCK_DICT (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)), sym);
182 static struct symbol *
183 add_class_symbol (struct type *type, CORE_ADDR addr)
186 struct objfile *objfile = get_dynamics_objfile (get_type_arch (type));
188 sym = allocate_symbol (objfile);
189 SYMBOL_SET_LANGUAGE (sym, language_java, &objfile->objfile_obstack);
190 SYMBOL_SET_LINKAGE_NAME (sym, TYPE_TAG_NAME (type));
191 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
192 /* SYMBOL_VALUE (sym) = valu; */
193 SYMBOL_TYPE (sym) = type;
194 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
195 SYMBOL_VALUE_ADDRESS (sym) = addr;
200 java_lookup_class (char *name)
204 sym = lookup_symbol (name, expression_context_block, STRUCT_DOMAIN, NULL);
206 return SYMBOL_TYPE (sym);
207 /* FIXME - should search inferior's symbol table. */
211 /* Return a nul-terminated string (allocated on OBSTACK) for
212 a name given by NAME (which has type Utf8Const*). */
215 get_java_utf8_name (struct obstack *obstack, struct value *name)
218 struct value *temp = name;
222 temp = value_struct_elt (&temp, NULL, "length", NULL, "structure");
223 name_length = (int) value_as_long (temp);
224 data_addr = value_address (temp) + TYPE_LENGTH (value_type (temp));
225 chrs = obstack_alloc (obstack, name_length + 1);
226 chrs[name_length] = '\0';
227 read_memory (data_addr, (gdb_byte *) chrs, name_length);
232 java_class_from_object (struct value *obj_val)
234 /* This is all rather inefficient, since the offsets of vtable and
235 class are fixed. FIXME */
236 struct value *vtable_val;
238 if (TYPE_CODE (value_type (obj_val)) == TYPE_CODE_PTR
239 && TYPE_LENGTH (TYPE_TARGET_TYPE (value_type (obj_val))) == 0)
240 obj_val = value_at (get_java_object_type (),
241 value_as_address (obj_val));
243 vtable_val = value_struct_elt (&obj_val, NULL, "vtable", NULL, "structure");
244 return value_struct_elt (&vtable_val, NULL, "class", NULL, "structure");
247 /* Check if CLASS_IS_PRIMITIVE(value of clas): */
249 java_class_is_primitive (struct value *clas)
251 struct value *vtable = value_struct_elt (&clas, NULL, "vtable",
253 CORE_ADDR i = value_as_address (vtable);
255 return (int) (i & 0x7fffffff) == (int) 0x7fffffff;
258 /* Read a GCJ Class object, and generated a gdb (TYPE_CODE_STRUCT) type. */
261 type_from_class (struct gdbarch *gdbarch, struct value *clas)
266 struct objfile *objfile;
267 struct value *utf8_name;
271 type = check_typedef (value_type (clas));
272 if (TYPE_CODE (type) == TYPE_CODE_PTR)
274 if (value_logical_not (clas))
276 clas = value_ind (clas);
278 addr = value_address (clas);
280 objfile = get_dynamics_objfile (gdbarch);
281 if (java_class_is_primitive (clas))
286 sig = value_struct_elt (&temp, NULL, "method_count", NULL, "structure");
287 return java_primitive_type (gdbarch, value_as_long (sig));
290 /* Get Class name. */
291 /* If clasloader non-null, prepend loader address. FIXME */
293 utf8_name = value_struct_elt (&temp, NULL, "name", NULL, "structure");
294 name = get_java_utf8_name (&objfile->objfile_obstack, utf8_name);
295 for (nptr = name; *nptr != 0; nptr++)
301 type = java_lookup_class (name);
305 type = alloc_type (objfile);
306 TYPE_CODE (type) = TYPE_CODE_STRUCT;
307 INIT_CPLUS_SPECIFIC (type);
311 char *signature = name;
312 int namelen = java_demangled_signature_length (signature);
314 if (namelen > strlen (name))
315 name = obstack_alloc (&objfile->objfile_obstack, namelen + 1);
316 java_demangled_signature_copy (name, signature);
317 name[namelen] = '\0';
319 /* Set array element type. */
320 temp = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
321 deprecated_set_value_type (temp,
322 lookup_pointer_type (value_type (clas)));
323 TYPE_TARGET_TYPE (type) = type_from_class (gdbarch, temp);
326 ALLOCATE_CPLUS_STRUCT_TYPE (type);
327 TYPE_TAG_NAME (type) = name;
329 add_class_symtab_symbol (add_class_symbol (type, addr));
330 return java_link_class_type (gdbarch, type, clas);
333 /* Fill in class TYPE with data from the CLAS value. */
336 java_link_class_type (struct gdbarch *gdbarch,
337 struct type *type, struct value *clas)
340 const char *unqualified_name;
341 const char *name = TYPE_TAG_NAME (type);
342 int ninterfaces, nfields, nmethods;
343 int type_is_object = 0;
344 struct fn_field *fn_fields;
345 struct fn_fieldlist *fn_fieldlists;
346 struct value *fields;
347 struct value *methods;
348 struct value *method = NULL;
349 struct value *field = NULL;
351 struct objfile *objfile = get_dynamics_objfile (gdbarch);
354 gdb_assert (name != NULL);
355 unqualified_name = strrchr (name, '.');
356 if (unqualified_name == NULL)
357 unqualified_name = name;
360 temp = value_struct_elt (&temp, NULL, "superclass", NULL, "structure");
361 if (strcmp (name, "java.lang.Object") == 0)
363 tsuper = get_java_object_type ();
364 if (tsuper && TYPE_CODE (tsuper) == TYPE_CODE_PTR)
365 tsuper = TYPE_TARGET_TYPE (tsuper);
369 tsuper = type_from_class (gdbarch, temp);
375 ninterfaces = value_as_long (value_struct_elt (&temp, NULL, "interface_len",
378 TYPE_N_BASECLASSES (type) = (tsuper == NULL ? 0 : 1) + ninterfaces;
380 nfields = value_as_long (value_struct_elt (&temp, NULL, "field_count",
382 nfields += TYPE_N_BASECLASSES (type);
383 nfields++; /* Add one for dummy "class" field. */
384 TYPE_NFIELDS (type) = nfields;
385 TYPE_FIELDS (type) = (struct field *)
386 TYPE_ALLOC (type, sizeof (struct field) * nfields);
388 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
390 TYPE_FIELD_PRIVATE_BITS (type) =
391 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
392 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
394 TYPE_FIELD_PROTECTED_BITS (type) =
395 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
396 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
398 TYPE_FIELD_IGNORE_BITS (type) =
399 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
400 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
402 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *)
403 TYPE_ALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type)));
404 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
408 TYPE_BASECLASS (type, 0) = tsuper;
410 SET_TYPE_FIELD_PRIVATE (type, 0);
414 if (i > 2 && name[i - 1] == ']' && tsuper != NULL)
417 TYPE_LENGTH (type) = TYPE_LENGTH (tsuper) + 4; /* size with "length" */
422 temp = value_struct_elt (&temp, NULL, "size_in_bytes",
424 TYPE_LENGTH (type) = value_as_long (temp);
428 nfields--; /* First set up dummy "class" field. */
429 SET_FIELD_PHYSADDR (TYPE_FIELD (type, nfields), value_address (clas));
430 TYPE_FIELD_NAME (type, nfields) = "class";
431 TYPE_FIELD_TYPE (type, nfields) = value_type (clas);
432 SET_TYPE_FIELD_PRIVATE (type, nfields);
434 for (i = TYPE_N_BASECLASSES (type); i < nfields; i++)
442 fields = value_struct_elt (&temp, NULL, "fields", NULL, "structure");
443 field = value_ind (fields);
446 { /* Re-use field value for next field. */
448 = value_address (field) + TYPE_LENGTH (value_type (field));
450 set_value_address (field, addr);
451 set_value_lazy (field, 1);
454 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
455 TYPE_FIELD_NAME (type, i) =
456 get_java_utf8_name (&objfile->objfile_obstack, temp);
458 accflags = value_as_long (value_struct_elt (&temp, NULL, "accflags",
461 temp = value_struct_elt (&temp, NULL, "info", NULL, "structure");
462 boffset = value_as_long (value_struct_elt (&temp, NULL, "boffset",
464 if (accflags & 0x0001) /* public access */
468 if (accflags & 0x0002) /* private access */
470 SET_TYPE_FIELD_PRIVATE (type, i);
472 if (accflags & 0x0004) /* protected access */
474 SET_TYPE_FIELD_PROTECTED (type, i);
476 if (accflags & 0x0008) /* ACC_STATIC */
477 SET_FIELD_PHYSADDR (TYPE_FIELD (type, i), boffset);
479 SET_FIELD_BITPOS (TYPE_FIELD (type, i), 8 * boffset);
480 if (accflags & 0x8000) /* FIELD_UNRESOLVED_FLAG */
482 TYPE_FIELD_TYPE (type, i) = get_java_object_type (); /* FIXME */
489 temp = value_struct_elt (&temp, NULL, "type", NULL, "structure");
490 ftype = type_from_class (gdbarch, temp);
491 if (TYPE_CODE (ftype) == TYPE_CODE_STRUCT)
492 ftype = lookup_pointer_type (ftype);
493 TYPE_FIELD_TYPE (type, i) = ftype;
498 nmethods = value_as_long (value_struct_elt (&temp, NULL, "method_count",
500 j = nmethods * sizeof (struct fn_field);
501 fn_fields = (struct fn_field *)
502 obstack_alloc (&objfile->objfile_obstack, j);
503 memset (fn_fields, 0, j);
504 fn_fieldlists = (struct fn_fieldlist *)
505 alloca (nmethods * sizeof (struct fn_fieldlist));
508 for (i = 0; i < nmethods; i++)
516 methods = value_struct_elt (&temp, NULL, "methods",
518 method = value_ind (methods);
521 { /* Re-use method value for next method. */
523 = value_address (method) + TYPE_LENGTH (value_type (method));
525 set_value_address (method, addr);
526 set_value_lazy (method, 1);
529 /* Get method name. */
531 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
532 mname = get_java_utf8_name (&objfile->objfile_obstack, temp);
533 if (strcmp (mname, "<init>") == 0)
534 mname = unqualified_name;
536 /* Check for an existing method with the same name.
537 * This makes building the fn_fieldslists an O(nmethods**2)
538 * operation. That could be using hashing, but I doubt it
539 * is worth it. Note that we do maintain the order of methods
540 * in the inferior's Method table (as long as that is grouped
541 * by method name), which I think is desirable. --PB */
542 for (k = 0, j = TYPE_NFN_FIELDS (type);;)
545 { /* No match - new method name. */
546 j = TYPE_NFN_FIELDS (type)++;
547 fn_fieldlists[j].name = mname;
548 fn_fieldlists[j].length = 1;
549 fn_fieldlists[j].fn_fields = &fn_fields[i];
553 if (strcmp (mname, fn_fieldlists[j].name) == 0)
554 { /* Found an existing method with the same name. */
557 if (mname != unqualified_name)
558 obstack_free (&objfile->objfile_obstack, mname);
559 mname = fn_fieldlists[j].name;
560 fn_fieldlists[j].length++;
561 k = i - k; /* Index of new slot. */
562 /* Shift intervening fn_fields (between k and i) down. */
563 for (l = i; l > k; l--)
564 fn_fields[l] = fn_fields[l - 1];
565 for (l = TYPE_NFN_FIELDS (type); --l > j;)
566 fn_fieldlists[l].fn_fields++;
569 k += fn_fieldlists[j].length;
571 fn_fields[k].physname = "";
572 fn_fields[k].is_stub = 1;
574 fn_fields[k].type = lookup_function_type
575 (builtin_java_type (gdbarch)->builtin_void);
576 TYPE_CODE (fn_fields[k].type) = TYPE_CODE_METHOD;
579 j = TYPE_NFN_FIELDS (type) * sizeof (struct fn_fieldlist);
580 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
581 obstack_alloc (&objfile->objfile_obstack, j);
582 memcpy (TYPE_FN_FIELDLISTS (type), fn_fieldlists, j);
588 get_java_object_type (void)
592 sym = lookup_symbol ("java.lang.Object", NULL, STRUCT_DOMAIN, NULL);
594 error (_("cannot find java.lang.Object"));
595 return SYMBOL_TYPE (sym);
599 get_java_object_header_size (struct gdbarch *gdbarch)
601 struct type *objtype = get_java_object_type ();
604 return (2 * gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT);
606 return TYPE_LENGTH (objtype);
610 is_object_type (struct type *type)
612 CHECK_TYPEDEF (type);
613 if (TYPE_CODE (type) == TYPE_CODE_PTR)
615 struct type *ttype = check_typedef (TYPE_TARGET_TYPE (type));
617 if (TYPE_CODE (ttype) != TYPE_CODE_STRUCT)
619 while (TYPE_N_BASECLASSES (ttype) > 0)
620 ttype = TYPE_BASECLASS (ttype, 0);
621 name = TYPE_TAG_NAME (ttype);
622 if (name != NULL && strcmp (name, "java.lang.Object") == 0)
625 = TYPE_NFIELDS (ttype) > 0 ? TYPE_FIELD_NAME (ttype, 0) : (char *) 0;
626 if (name != NULL && strcmp (name, "vtable") == 0)
633 java_primitive_type (struct gdbarch *gdbarch, int signature)
635 const struct builtin_java_type *builtin = builtin_java_type (gdbarch);
640 return builtin->builtin_byte;
642 return builtin->builtin_short;
644 return builtin->builtin_int;
646 return builtin->builtin_long;
648 return builtin->builtin_boolean;
650 return builtin->builtin_char;
652 return builtin->builtin_float;
654 return builtin->builtin_double;
656 return builtin->builtin_void;
658 error (_("unknown signature '%c' for primitive type"), (char) signature);
661 /* If name[0 .. namelen-1] is the name of a primitive Java type,
662 return that type. Otherwise, return NULL. */
665 java_primitive_type_from_name (struct gdbarch *gdbarch,
666 const char *name, int namelen)
668 const struct builtin_java_type *builtin = builtin_java_type (gdbarch);
673 if (namelen == 4 && memcmp (name, "byte", 4) == 0)
674 return builtin->builtin_byte;
675 if (namelen == 7 && memcmp (name, "boolean", 7) == 0)
676 return builtin->builtin_boolean;
679 if (namelen == 4 && memcmp (name, "char", 4) == 0)
680 return builtin->builtin_char;
683 if (namelen == 6 && memcmp (name, "double", 6) == 0)
684 return builtin->builtin_double;
687 if (namelen == 5 && memcmp (name, "float", 5) == 0)
688 return builtin->builtin_float;
691 if (namelen == 3 && memcmp (name, "int", 3) == 0)
692 return builtin->builtin_int;
695 if (namelen == 4 && memcmp (name, "long", 4) == 0)
696 return builtin->builtin_long;
699 if (namelen == 5 && memcmp (name, "short", 5) == 0)
700 return builtin->builtin_short;
703 if (namelen == 4 && memcmp (name, "void", 4) == 0)
704 return builtin->builtin_void;
711 java_primitive_type_name (int signature)
734 error (_("unknown signature '%c' for primitive type"), (char) signature);
737 /* Return the length (in bytes) of demangled name of the Java type
738 signature string SIGNATURE. */
741 java_demangled_signature_length (const char *signature)
745 for (; *signature == '['; signature++)
746 array += 2; /* Two chars for "[]". */
747 switch (signature[0])
750 /* Subtract 2 for 'L' and ';'. */
751 return strlen (signature) - 2 + array;
753 return strlen (java_primitive_type_name (signature[0])) + array;
757 /* Demangle the Java type signature SIGNATURE, leaving the result in
761 java_demangled_signature_copy (char *result, const char *signature)
767 while (*signature == '[')
772 switch (signature[0])
775 /* Subtract 2 for 'L' and ';', but add 1 for final nul. */
778 for (; *signature != ';' && *signature != '\0'; signature++)
780 if (*signature == '/')
787 ptr = java_primitive_type_name (signature[0]);
789 strcpy (result, ptr);
800 /* Return the demangled name of the Java type signature string SIGNATURE,
801 as a freshly allocated copy. */
804 java_demangle_type_signature (const char *signature)
806 int length = java_demangled_signature_length (signature);
807 char *result = xmalloc (length + 1);
809 java_demangled_signature_copy (result, signature);
810 result[length] = '\0';
814 /* Return the type of TYPE followed by DIMS pairs of [ ].
815 If DIMS == 0, TYPE is returned. */
818 java_array_type (struct type *type, int dims)
822 /* FIXME This is bogus! Java arrays are not gdb arrays! */
823 type = lookup_array_range_type (type, 0, 0);
829 /* Create a Java string in the inferior from a (Utf8) literal. */
831 static struct value *
832 java_value_string (char *ptr, int len)
834 error (_("not implemented - java_value_string")); /* FIXME */
837 /* Return the encoding that should be used for the character type
841 java_get_encoding (struct type *type)
843 struct gdbarch *arch = get_type_arch (type);
844 const char *encoding;
846 if (type == builtin_java_type (arch)->builtin_char)
848 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
849 encoding = "UTF-16BE";
851 encoding = "UTF-16LE";
854 encoding = target_charset (arch);
859 /* Print the character C on STREAM as part of the contents of a literal
860 string whose delimiter is QUOTER. Note that that format for printing
861 characters and strings is language specific. */
864 java_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
866 const char *encoding = java_get_encoding (type);
868 generic_emit_char (c, type, stream, quoter, encoding);
871 /* Implementation of la_printchar method. */
874 java_printchar (int c, struct type *type, struct ui_file *stream)
876 fputs_filtered ("'", stream);
877 LA_EMIT_CHAR (c, type, stream, '\'');
878 fputs_filtered ("'", stream);
881 /* Implementation of la_printstr method. */
884 java_printstr (struct ui_file *stream, struct type *type,
885 const gdb_byte *string,
886 unsigned int length, const char *encoding, int force_ellipses,
887 const struct value_print_options *options)
889 const char *type_encoding = java_get_encoding (type);
891 if (!encoding || !*encoding)
892 encoding = type_encoding;
894 generic_printstr (stream, type, string, length, encoding,
895 force_ellipses, '"', 0, options);
898 static struct value *
899 evaluate_subexp_java (struct type *expect_type, struct expression *exp,
900 int *pos, enum noside noside)
905 enum exp_opcode op = exp->elts[*pos].opcode;
913 if (noside == EVAL_SKIP)
916 arg1 = evaluate_subexp_java (NULL_TYPE, exp, pos, EVAL_NORMAL);
917 if (is_object_type (value_type (arg1)))
921 type = type_from_class (exp->gdbarch, java_class_from_object (arg1));
922 arg1 = value_cast (lookup_pointer_type (type), arg1);
924 return value_ind (arg1);
926 case BINOP_SUBSCRIPT:
928 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
929 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
930 if (noside == EVAL_SKIP)
932 /* If the user attempts to subscript something that is not an
933 array or pointer type (like a plain int variable for example),
934 then report this as an error. */
936 arg1 = coerce_ref (arg1);
937 type = check_typedef (value_type (arg1));
938 if (TYPE_CODE (type) == TYPE_CODE_PTR)
939 type = check_typedef (TYPE_TARGET_TYPE (type));
940 name = TYPE_NAME (type);
942 name = TYPE_TAG_NAME (type);
943 i = name == NULL ? 0 : strlen (name);
944 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
945 && i > 2 && name[i - 1] == ']')
947 enum bfd_endian byte_order = gdbarch_byte_order (exp->gdbarch);
950 struct type *el_type;
953 struct value *clas = java_class_from_object (arg1);
954 struct value *temp = clas;
955 /* Get CLASS_ELEMENT_TYPE of the array type. */
956 temp = value_struct_elt (&temp, NULL, "methods",
958 deprecated_set_value_type (temp, value_type (clas));
959 el_type = type_from_class (exp->gdbarch, temp);
960 if (TYPE_CODE (el_type) == TYPE_CODE_STRUCT)
961 el_type = lookup_pointer_type (el_type);
963 if (noside == EVAL_AVOID_SIDE_EFFECTS)
964 return value_zero (el_type, VALUE_LVAL (arg1));
965 address = value_as_address (arg1);
966 address += get_java_object_header_size (exp->gdbarch);
967 read_memory (address, buf4, 4);
968 length = (long) extract_signed_integer (buf4, 4, byte_order);
969 index = (long) value_as_long (arg2);
970 if (index >= length || index < 0)
971 error (_("array index (%ld) out of bounds (length: %ld)"),
973 address = (address + 4) + index * TYPE_LENGTH (el_type);
974 return value_at (el_type, address);
976 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
978 if (noside == EVAL_AVOID_SIDE_EFFECTS)
979 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
981 return value_subscript (arg1, value_as_long (arg2));
984 error (_("cannot subscript something of type `%s'"), name);
986 error (_("cannot subscript requested type"));
990 i = longest_to_int (exp->elts[pc + 1].longconst);
991 (*pos) += 3 + BYTES_TO_EXP_ELEM (i + 1);
992 if (noside == EVAL_SKIP)
994 return java_value_string (&exp->elts[pc + 2].string, i);
997 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
998 /* Convert object field (such as TYPE.class) to reference. */
999 if (TYPE_CODE (value_type (arg1)) == TYPE_CODE_STRUCT)
1000 arg1 = value_addr (arg1);
1006 return evaluate_subexp_standard (expect_type, exp, pos, noside);
1008 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1011 static char *java_demangle (const char *mangled, int options)
1013 return gdb_demangle (mangled, options | DMGL_JAVA);
1016 /* Find the member function name of the demangled name NAME. NAME
1017 must be a method name including arguments, in order to correctly
1018 locate the last component.
1020 This function return a pointer to the first dot before the
1021 member function name, or NULL if the name was not of the
1025 java_find_last_component (const char *name)
1029 /* Find argument list. */
1030 p = strchr (name, '(');
1035 /* Back up and find first dot prior to argument list. */
1036 while (p > name && *p != '.')
1045 /* Return the name of the class containing method PHYSNAME. */
1048 java_class_name_from_physname (const char *physname)
1052 char *demangled_name = java_demangle (physname, DMGL_PARAMS | DMGL_ANSI);
1054 if (demangled_name == NULL)
1057 end = java_find_last_component (demangled_name);
1060 ret = xmalloc (end - demangled_name + 1);
1061 memcpy (ret, demangled_name, end - demangled_name);
1062 ret[end - demangled_name] = '\0';
1065 xfree (demangled_name);
1069 /* Table mapping opcodes into strings for printing operators
1070 and precedences of the operators. */
1072 const struct op_print java_op_print_tab[] =
1074 {",", BINOP_COMMA, PREC_COMMA, 0},
1075 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1076 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1077 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1078 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
1079 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
1080 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
1081 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1082 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1083 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1084 {">=", BINOP_GEQ, PREC_ORDER, 0},
1085 {">", BINOP_GTR, PREC_ORDER, 0},
1086 {"<", BINOP_LESS, PREC_ORDER, 0},
1087 {">>", BINOP_RSH, PREC_SHIFT, 0},
1088 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1089 {"+", BINOP_ADD, PREC_ADD, 0},
1090 {"-", BINOP_SUB, PREC_ADD, 0},
1091 {"*", BINOP_MUL, PREC_MUL, 0},
1092 {"/", BINOP_DIV, PREC_MUL, 0},
1093 {"%", BINOP_REM, PREC_MUL, 0},
1094 {"-", UNOP_NEG, PREC_PREFIX, 0},
1095 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1096 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
1097 {"*", UNOP_IND, PREC_PREFIX, 0},
1098 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1099 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1103 enum java_primitive_types
1105 java_primitive_type_int,
1106 java_primitive_type_short,
1107 java_primitive_type_long,
1108 java_primitive_type_byte,
1109 java_primitive_type_boolean,
1110 java_primitive_type_char,
1111 java_primitive_type_float,
1112 java_primitive_type_double,
1113 java_primitive_type_void,
1114 nr_java_primitive_types
1118 java_language_arch_info (struct gdbarch *gdbarch,
1119 struct language_arch_info *lai)
1121 const struct builtin_java_type *builtin = builtin_java_type (gdbarch);
1123 lai->string_char_type = builtin->builtin_char;
1124 lai->primitive_type_vector
1125 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_java_primitive_types + 1,
1127 lai->primitive_type_vector [java_primitive_type_int]
1128 = builtin->builtin_int;
1129 lai->primitive_type_vector [java_primitive_type_short]
1130 = builtin->builtin_short;
1131 lai->primitive_type_vector [java_primitive_type_long]
1132 = builtin->builtin_long;
1133 lai->primitive_type_vector [java_primitive_type_byte]
1134 = builtin->builtin_byte;
1135 lai->primitive_type_vector [java_primitive_type_boolean]
1136 = builtin->builtin_boolean;
1137 lai->primitive_type_vector [java_primitive_type_char]
1138 = builtin->builtin_char;
1139 lai->primitive_type_vector [java_primitive_type_float]
1140 = builtin->builtin_float;
1141 lai->primitive_type_vector [java_primitive_type_double]
1142 = builtin->builtin_double;
1143 lai->primitive_type_vector [java_primitive_type_void]
1144 = builtin->builtin_void;
1146 lai->bool_type_symbol = "boolean";
1147 lai->bool_type_default = builtin->builtin_boolean;
1150 const struct exp_descriptor exp_descriptor_java =
1152 print_subexp_standard,
1153 operator_length_standard,
1154 operator_check_standard,
1156 dump_subexp_body_standard,
1157 evaluate_subexp_java
1160 const struct language_defn java_language_defn =
1162 "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,
1203 build_java_types (struct gdbarch *gdbarch)
1205 struct builtin_java_type *builtin_java_type
1206 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_java_type);
1208 builtin_java_type->builtin_int
1209 = arch_integer_type (gdbarch, 32, 0, "int");
1210 builtin_java_type->builtin_short
1211 = arch_integer_type (gdbarch, 16, 0, "short");
1212 builtin_java_type->builtin_long
1213 = arch_integer_type (gdbarch, 64, 0, "long");
1214 builtin_java_type->builtin_byte
1215 = arch_integer_type (gdbarch, 8, 0, "byte");
1216 builtin_java_type->builtin_boolean
1217 = arch_boolean_type (gdbarch, 8, 0, "boolean");
1218 builtin_java_type->builtin_char
1219 = arch_character_type (gdbarch, 16, 1, "char");
1220 builtin_java_type->builtin_float
1221 = arch_float_type (gdbarch, 32, "float", NULL);
1222 builtin_java_type->builtin_double
1223 = arch_float_type (gdbarch, 64, "double", NULL);
1224 builtin_java_type->builtin_void
1225 = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
1227 return builtin_java_type;
1230 static struct gdbarch_data *java_type_data;
1232 const struct builtin_java_type *
1233 builtin_java_type (struct gdbarch *gdbarch)
1235 return gdbarch_data (gdbarch, java_type_data);
1239 _initialize_java_language (void)
1241 jv_dynamics_objfile_data_key
1242 = register_objfile_data_with_cleanup (NULL, jv_per_objfile_free);
1243 jv_dynamics_progspace_key = register_program_space_data ();
1245 java_type_data = gdbarch_data_register_post_init (build_java_types);
1247 add_language (&java_language_defn);