GDB (API)
|
00001 /* Java language support routines for GDB, the GNU debugger. 00002 00003 Copyright (C) 1997-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #include "defs.h" 00021 #include "symtab.h" 00022 #include "gdbtypes.h" 00023 #include "expression.h" 00024 #include "parser-defs.h" 00025 #include "language.h" 00026 #include "gdbtypes.h" 00027 #include "symtab.h" 00028 #include "symfile.h" 00029 #include "objfiles.h" 00030 #include "gdb_string.h" 00031 #include "value.h" 00032 #include "c-lang.h" 00033 #include "jv-lang.h" 00034 #include "gdbcore.h" 00035 #include "block.h" 00036 #include "demangle.h" 00037 #include "dictionary.h" 00038 #include <ctype.h> 00039 #include "gdb_assert.h" 00040 #include "charset.h" 00041 #include "valprint.h" 00042 #include "cp-support.h" 00043 00044 /* Local functions */ 00045 00046 extern void _initialize_java_language (void); 00047 00048 static int java_demangled_signature_length (const char *); 00049 static void java_demangled_signature_copy (char *, const char *); 00050 00051 static struct symtab *get_java_class_symtab (struct gdbarch *gdbarch); 00052 static char *get_java_utf8_name (struct obstack *obstack, struct value *name); 00053 static int java_class_is_primitive (struct value *clas); 00054 static struct value *java_value_string (char *ptr, int len); 00055 00056 static void java_emit_char (int c, struct type *type, 00057 struct ui_file * stream, int quoter); 00058 00059 static char *java_class_name_from_physname (const char *physname); 00060 00061 static const struct objfile_data *jv_dynamics_objfile_data_key; 00062 00063 /* The dynamic objfile is kept per-program-space. This key lets us 00064 associate the objfile with the program space. */ 00065 00066 static const struct program_space_data *jv_dynamics_progspace_key; 00067 00068 static struct type *java_link_class_type (struct gdbarch *, 00069 struct type *, struct value *); 00070 00071 /* An instance of this structure is used to store some data that must 00072 be freed. */ 00073 00074 struct jv_per_objfile_data 00075 { 00076 /* The expandable dictionary we use. */ 00077 struct dictionary *dict; 00078 }; 00079 00080 /* A function called when the dynamics_objfile is freed. We use this 00081 to clean up some internal state. */ 00082 static void 00083 jv_per_objfile_free (struct objfile *objfile, void *data) 00084 { 00085 struct jv_per_objfile_data *jv_data = data; 00086 struct objfile *dynamics_objfile; 00087 00088 dynamics_objfile = program_space_data (current_program_space, 00089 jv_dynamics_progspace_key); 00090 gdb_assert (objfile == dynamics_objfile); 00091 00092 if (jv_data->dict) 00093 dict_free (jv_data->dict); 00094 xfree (jv_data); 00095 00096 set_program_space_data (current_program_space, 00097 jv_dynamics_progspace_key, 00098 NULL); 00099 } 00100 00101 /* FIXME: carlton/2003-02-04: This is the main or only caller of 00102 allocate_objfile with first argument NULL; as a result, this code 00103 breaks every so often. Somebody should write a test case that 00104 exercises GDB in various ways (e.g. something involving loading a 00105 dynamic library) after this code has been called. */ 00106 00107 static struct objfile * 00108 get_dynamics_objfile (struct gdbarch *gdbarch) 00109 { 00110 struct objfile *dynamics_objfile; 00111 00112 dynamics_objfile = program_space_data (current_program_space, 00113 jv_dynamics_progspace_key); 00114 00115 if (dynamics_objfile == NULL) 00116 { 00117 struct jv_per_objfile_data *data; 00118 00119 /* Mark it as shared so that it is cleared when the inferior is 00120 re-run. */ 00121 dynamics_objfile = allocate_objfile (NULL, NULL, 00122 OBJF_SHARED | OBJF_NOT_FILENAME); 00123 dynamics_objfile->per_bfd->gdbarch = gdbarch; 00124 00125 data = XCNEW (struct jv_per_objfile_data); 00126 set_objfile_data (dynamics_objfile, jv_dynamics_objfile_data_key, data); 00127 00128 set_program_space_data (current_program_space, 00129 jv_dynamics_progspace_key, 00130 dynamics_objfile); 00131 } 00132 return dynamics_objfile; 00133 } 00134 00135 static struct symtab * 00136 get_java_class_symtab (struct gdbarch *gdbarch) 00137 { 00138 struct objfile *objfile = get_dynamics_objfile (gdbarch); 00139 struct symtab *class_symtab = objfile->symtabs; 00140 00141 if (class_symtab == NULL) 00142 { 00143 struct blockvector *bv; 00144 struct block *bl; 00145 struct jv_per_objfile_data *jv_data; 00146 00147 class_symtab = allocate_symtab ("<java-classes>", objfile); 00148 class_symtab->language = language_java; 00149 bv = (struct blockvector *) 00150 obstack_alloc (&objfile->objfile_obstack, 00151 sizeof (struct blockvector) + sizeof (struct block *)); 00152 BLOCKVECTOR_NBLOCKS (bv) = 1; 00153 BLOCKVECTOR (class_symtab) = bv; 00154 00155 /* Allocate dummy STATIC_BLOCK. */ 00156 bl = allocate_block (&objfile->objfile_obstack); 00157 BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack, 00158 NULL); 00159 BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl; 00160 00161 /* Allocate GLOBAL_BLOCK. */ 00162 bl = allocate_global_block (&objfile->objfile_obstack); 00163 BLOCK_DICT (bl) = dict_create_hashed_expandable (); 00164 set_block_symtab (bl, class_symtab); 00165 BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl; 00166 00167 /* Arrange to free the dict. */ 00168 jv_data = objfile_data (objfile, jv_dynamics_objfile_data_key); 00169 jv_data->dict = BLOCK_DICT (bl); 00170 } 00171 return class_symtab; 00172 } 00173 00174 static void 00175 add_class_symtab_symbol (struct symbol *sym) 00176 { 00177 struct symtab *symtab 00178 = get_java_class_symtab (get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile)); 00179 struct blockvector *bv = BLOCKVECTOR (symtab); 00180 00181 dict_add_symbol (BLOCK_DICT (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)), sym); 00182 } 00183 00184 static struct symbol * 00185 add_class_symbol (struct type *type, CORE_ADDR addr) 00186 { 00187 struct symbol *sym; 00188 struct objfile *objfile = get_dynamics_objfile (get_type_arch (type)); 00189 00190 sym = allocate_symbol (objfile); 00191 SYMBOL_SET_LANGUAGE (sym, language_java, &objfile->objfile_obstack); 00192 SYMBOL_SET_LINKAGE_NAME (sym, TYPE_TAG_NAME (type)); 00193 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF; 00194 /* SYMBOL_VALUE (sym) = valu; */ 00195 SYMBOL_TYPE (sym) = type; 00196 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN; 00197 SYMBOL_VALUE_ADDRESS (sym) = addr; 00198 return sym; 00199 } 00200 00201 struct type * 00202 java_lookup_class (char *name) 00203 { 00204 struct symbol *sym; 00205 00206 sym = lookup_symbol (name, expression_context_block, STRUCT_DOMAIN, NULL); 00207 if (sym != NULL) 00208 return SYMBOL_TYPE (sym); 00209 /* FIXME - should search inferior's symbol table. */ 00210 return NULL; 00211 } 00212 00213 /* Return a nul-terminated string (allocated on OBSTACK) for 00214 a name given by NAME (which has type Utf8Const*). */ 00215 00216 char * 00217 get_java_utf8_name (struct obstack *obstack, struct value *name) 00218 { 00219 char *chrs; 00220 struct value *temp = name; 00221 int name_length; 00222 CORE_ADDR data_addr; 00223 00224 temp = value_struct_elt (&temp, NULL, "length", NULL, "structure"); 00225 name_length = (int) value_as_long (temp); 00226 data_addr = value_address (temp) + TYPE_LENGTH (value_type (temp)); 00227 chrs = obstack_alloc (obstack, name_length + 1); 00228 chrs[name_length] = '\0'; 00229 read_memory (data_addr, (gdb_byte *) chrs, name_length); 00230 return chrs; 00231 } 00232 00233 struct value * 00234 java_class_from_object (struct value *obj_val) 00235 { 00236 /* This is all rather inefficient, since the offsets of vtable and 00237 class are fixed. FIXME */ 00238 struct value *vtable_val; 00239 00240 if (TYPE_CODE (value_type (obj_val)) == TYPE_CODE_PTR 00241 && TYPE_LENGTH (TYPE_TARGET_TYPE (value_type (obj_val))) == 0) 00242 obj_val = value_at (get_java_object_type (), 00243 value_as_address (obj_val)); 00244 00245 vtable_val = value_struct_elt (&obj_val, NULL, "vtable", NULL, "structure"); 00246 return value_struct_elt (&vtable_val, NULL, "class", NULL, "structure"); 00247 } 00248 00249 /* Check if CLASS_IS_PRIMITIVE(value of clas): */ 00250 static int 00251 java_class_is_primitive (struct value *clas) 00252 { 00253 struct value *vtable = value_struct_elt (&clas, NULL, "vtable", 00254 NULL, "struct"); 00255 CORE_ADDR i = value_as_address (vtable); 00256 00257 return (int) (i & 0x7fffffff) == (int) 0x7fffffff; 00258 } 00259 00260 /* Read a GCJ Class object, and generated a gdb (TYPE_CODE_STRUCT) type. */ 00261 00262 struct type * 00263 type_from_class (struct gdbarch *gdbarch, struct value *clas) 00264 { 00265 struct type *type; 00266 char *name; 00267 struct value *temp; 00268 struct objfile *objfile; 00269 struct value *utf8_name; 00270 char *nptr; 00271 CORE_ADDR addr; 00272 00273 type = check_typedef (value_type (clas)); 00274 if (TYPE_CODE (type) == TYPE_CODE_PTR) 00275 { 00276 if (value_logical_not (clas)) 00277 return NULL; 00278 clas = value_ind (clas); 00279 } 00280 addr = value_address (clas); 00281 00282 objfile = get_dynamics_objfile (gdbarch); 00283 if (java_class_is_primitive (clas)) 00284 { 00285 struct value *sig; 00286 00287 temp = clas; 00288 sig = value_struct_elt (&temp, NULL, "method_count", NULL, "structure"); 00289 return java_primitive_type (gdbarch, value_as_long (sig)); 00290 } 00291 00292 /* Get Class name. */ 00293 /* If clasloader non-null, prepend loader address. FIXME */ 00294 temp = clas; 00295 utf8_name = value_struct_elt (&temp, NULL, "name", NULL, "structure"); 00296 name = get_java_utf8_name (&objfile->objfile_obstack, utf8_name); 00297 for (nptr = name; *nptr != 0; nptr++) 00298 { 00299 if (*nptr == '/') 00300 *nptr = '.'; 00301 } 00302 00303 type = java_lookup_class (name); 00304 if (type != NULL) 00305 return type; 00306 00307 type = alloc_type (objfile); 00308 TYPE_CODE (type) = TYPE_CODE_STRUCT; 00309 INIT_CPLUS_SPECIFIC (type); 00310 00311 if (name[0] == '[') 00312 { 00313 char *signature = name; 00314 int namelen = java_demangled_signature_length (signature); 00315 00316 if (namelen > strlen (name)) 00317 name = obstack_alloc (&objfile->objfile_obstack, namelen + 1); 00318 java_demangled_signature_copy (name, signature); 00319 name[namelen] = '\0'; 00320 temp = clas; 00321 /* Set array element type. */ 00322 temp = value_struct_elt (&temp, NULL, "methods", NULL, "structure"); 00323 deprecated_set_value_type (temp, 00324 lookup_pointer_type (value_type (clas))); 00325 TYPE_TARGET_TYPE (type) = type_from_class (gdbarch, temp); 00326 } 00327 00328 ALLOCATE_CPLUS_STRUCT_TYPE (type); 00329 TYPE_TAG_NAME (type) = name; 00330 00331 add_class_symtab_symbol (add_class_symbol (type, addr)); 00332 return java_link_class_type (gdbarch, type, clas); 00333 } 00334 00335 /* Fill in class TYPE with data from the CLAS value. */ 00336 00337 static struct type * 00338 java_link_class_type (struct gdbarch *gdbarch, 00339 struct type *type, struct value *clas) 00340 { 00341 struct value *temp; 00342 const char *unqualified_name; 00343 const char *name = TYPE_TAG_NAME (type); 00344 int ninterfaces, nfields, nmethods; 00345 int type_is_object = 0; 00346 struct fn_field *fn_fields; 00347 struct fn_fieldlist *fn_fieldlists; 00348 struct value *fields; 00349 struct value *methods; 00350 struct value *method = NULL; 00351 struct value *field = NULL; 00352 int i, j; 00353 struct objfile *objfile = get_dynamics_objfile (gdbarch); 00354 struct type *tsuper; 00355 00356 gdb_assert (name != NULL); 00357 unqualified_name = strrchr (name, '.'); 00358 if (unqualified_name == NULL) 00359 unqualified_name = name; 00360 00361 temp = clas; 00362 temp = value_struct_elt (&temp, NULL, "superclass", NULL, "structure"); 00363 if (strcmp (name, "java.lang.Object") == 0) 00364 { 00365 tsuper = get_java_object_type (); 00366 if (tsuper && TYPE_CODE (tsuper) == TYPE_CODE_PTR) 00367 tsuper = TYPE_TARGET_TYPE (tsuper); 00368 type_is_object = 1; 00369 } 00370 else 00371 tsuper = type_from_class (gdbarch, temp); 00372 00373 #if 1 00374 ninterfaces = 0; 00375 #else 00376 temp = clas; 00377 ninterfaces = value_as_long (value_struct_elt (&temp, NULL, "interface_len", 00378 NULL, "structure")); 00379 #endif 00380 TYPE_N_BASECLASSES (type) = (tsuper == NULL ? 0 : 1) + ninterfaces; 00381 temp = clas; 00382 nfields = value_as_long (value_struct_elt (&temp, NULL, "field_count", 00383 NULL, "structure")); 00384 nfields += TYPE_N_BASECLASSES (type); 00385 nfields++; /* Add one for dummy "class" field. */ 00386 TYPE_NFIELDS (type) = nfields; 00387 TYPE_FIELDS (type) = (struct field *) 00388 TYPE_ALLOC (type, sizeof (struct field) * nfields); 00389 00390 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields); 00391 00392 TYPE_FIELD_PRIVATE_BITS (type) = 00393 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields)); 00394 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields); 00395 00396 TYPE_FIELD_PROTECTED_BITS (type) = 00397 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields)); 00398 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields); 00399 00400 TYPE_FIELD_IGNORE_BITS (type) = 00401 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields)); 00402 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields); 00403 00404 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *) 00405 TYPE_ALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type))); 00406 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type)); 00407 00408 if (tsuper != NULL) 00409 { 00410 TYPE_BASECLASS (type, 0) = tsuper; 00411 if (type_is_object) 00412 SET_TYPE_FIELD_PRIVATE (type, 0); 00413 } 00414 00415 i = strlen (name); 00416 if (i > 2 && name[i - 1] == ']' && tsuper != NULL) 00417 { 00418 /* FIXME */ 00419 TYPE_LENGTH (type) = TYPE_LENGTH (tsuper) + 4; /* size with "length" */ 00420 } 00421 else 00422 { 00423 temp = clas; 00424 temp = value_struct_elt (&temp, NULL, "size_in_bytes", 00425 NULL, "structure"); 00426 TYPE_LENGTH (type) = value_as_long (temp); 00427 } 00428 00429 fields = NULL; 00430 nfields--; /* First set up dummy "class" field. */ 00431 SET_FIELD_PHYSADDR (TYPE_FIELD (type, nfields), value_address (clas)); 00432 TYPE_FIELD_NAME (type, nfields) = "class"; 00433 TYPE_FIELD_TYPE (type, nfields) = value_type (clas); 00434 SET_TYPE_FIELD_PRIVATE (type, nfields); 00435 00436 for (i = TYPE_N_BASECLASSES (type); i < nfields; i++) 00437 { 00438 int accflags; 00439 int boffset; 00440 00441 if (fields == NULL) 00442 { 00443 temp = clas; 00444 fields = value_struct_elt (&temp, NULL, "fields", NULL, "structure"); 00445 field = value_ind (fields); 00446 } 00447 else 00448 { /* Re-use field value for next field. */ 00449 CORE_ADDR addr 00450 = value_address (field) + TYPE_LENGTH (value_type (field)); 00451 00452 set_value_address (field, addr); 00453 set_value_lazy (field, 1); 00454 } 00455 temp = field; 00456 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure"); 00457 TYPE_FIELD_NAME (type, i) = 00458 get_java_utf8_name (&objfile->objfile_obstack, temp); 00459 temp = field; 00460 accflags = value_as_long (value_struct_elt (&temp, NULL, "accflags", 00461 NULL, "structure")); 00462 temp = field; 00463 temp = value_struct_elt (&temp, NULL, "info", NULL, "structure"); 00464 boffset = value_as_long (value_struct_elt (&temp, NULL, "boffset", 00465 NULL, "structure")); 00466 if (accflags & 0x0001) /* public access */ 00467 { 00468 /* ??? */ 00469 } 00470 if (accflags & 0x0002) /* private access */ 00471 { 00472 SET_TYPE_FIELD_PRIVATE (type, i); 00473 } 00474 if (accflags & 0x0004) /* protected access */ 00475 { 00476 SET_TYPE_FIELD_PROTECTED (type, i); 00477 } 00478 if (accflags & 0x0008) /* ACC_STATIC */ 00479 SET_FIELD_PHYSADDR (TYPE_FIELD (type, i), boffset); 00480 else 00481 SET_FIELD_BITPOS (TYPE_FIELD (type, i), 8 * boffset); 00482 if (accflags & 0x8000) /* FIELD_UNRESOLVED_FLAG */ 00483 { 00484 TYPE_FIELD_TYPE (type, i) = get_java_object_type (); /* FIXME */ 00485 } 00486 else 00487 { 00488 struct type *ftype; 00489 00490 temp = field; 00491 temp = value_struct_elt (&temp, NULL, "type", NULL, "structure"); 00492 ftype = type_from_class (gdbarch, temp); 00493 if (TYPE_CODE (ftype) == TYPE_CODE_STRUCT) 00494 ftype = lookup_pointer_type (ftype); 00495 TYPE_FIELD_TYPE (type, i) = ftype; 00496 } 00497 } 00498 00499 temp = clas; 00500 nmethods = value_as_long (value_struct_elt (&temp, NULL, "method_count", 00501 NULL, "structure")); 00502 j = nmethods * sizeof (struct fn_field); 00503 fn_fields = (struct fn_field *) 00504 obstack_alloc (&objfile->objfile_obstack, j); 00505 memset (fn_fields, 0, j); 00506 fn_fieldlists = (struct fn_fieldlist *) 00507 alloca (nmethods * sizeof (struct fn_fieldlist)); 00508 00509 methods = NULL; 00510 for (i = 0; i < nmethods; i++) 00511 { 00512 const char *mname; 00513 int k; 00514 00515 if (methods == NULL) 00516 { 00517 temp = clas; 00518 methods = value_struct_elt (&temp, NULL, "methods", 00519 NULL, "structure"); 00520 method = value_ind (methods); 00521 } 00522 else 00523 { /* Re-use method value for next method. */ 00524 CORE_ADDR addr 00525 = value_address (method) + TYPE_LENGTH (value_type (method)); 00526 00527 set_value_address (method, addr); 00528 set_value_lazy (method, 1); 00529 } 00530 00531 /* Get method name. */ 00532 temp = method; 00533 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure"); 00534 mname = get_java_utf8_name (&objfile->objfile_obstack, temp); 00535 if (strcmp (mname, "<init>") == 0) 00536 mname = unqualified_name; 00537 00538 /* Check for an existing method with the same name. 00539 * This makes building the fn_fieldslists an O(nmethods**2) 00540 * operation. That could be using hashing, but I doubt it 00541 * is worth it. Note that we do maintain the order of methods 00542 * in the inferior's Method table (as long as that is grouped 00543 * by method name), which I think is desirable. --PB */ 00544 for (k = 0, j = TYPE_NFN_FIELDS (type);;) 00545 { 00546 if (--j < 0) 00547 { /* No match - new method name. */ 00548 j = TYPE_NFN_FIELDS (type)++; 00549 fn_fieldlists[j].name = mname; 00550 fn_fieldlists[j].length = 1; 00551 fn_fieldlists[j].fn_fields = &fn_fields[i]; 00552 k = i; 00553 break; 00554 } 00555 if (strcmp (mname, fn_fieldlists[j].name) == 0) 00556 { /* Found an existing method with the same name. */ 00557 int l; 00558 00559 if (mname != unqualified_name) 00560 obstack_free (&objfile->objfile_obstack, mname); 00561 mname = fn_fieldlists[j].name; 00562 fn_fieldlists[j].length++; 00563 k = i - k; /* Index of new slot. */ 00564 /* Shift intervening fn_fields (between k and i) down. */ 00565 for (l = i; l > k; l--) 00566 fn_fields[l] = fn_fields[l - 1]; 00567 for (l = TYPE_NFN_FIELDS (type); --l > j;) 00568 fn_fieldlists[l].fn_fields++; 00569 break; 00570 } 00571 k += fn_fieldlists[j].length; 00572 } 00573 fn_fields[k].physname = ""; 00574 fn_fields[k].is_stub = 1; 00575 /* FIXME */ 00576 fn_fields[k].type = lookup_function_type 00577 (builtin_java_type (gdbarch)->builtin_void); 00578 TYPE_CODE (fn_fields[k].type) = TYPE_CODE_METHOD; 00579 } 00580 00581 j = TYPE_NFN_FIELDS (type) * sizeof (struct fn_fieldlist); 00582 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *) 00583 obstack_alloc (&objfile->objfile_obstack, j); 00584 memcpy (TYPE_FN_FIELDLISTS (type), fn_fieldlists, j); 00585 00586 return type; 00587 } 00588 00589 struct type * 00590 get_java_object_type (void) 00591 { 00592 struct symbol *sym; 00593 00594 sym = lookup_symbol ("java.lang.Object", NULL, STRUCT_DOMAIN, NULL); 00595 if (sym == NULL) 00596 error (_("cannot find java.lang.Object")); 00597 return SYMBOL_TYPE (sym); 00598 } 00599 00600 int 00601 get_java_object_header_size (struct gdbarch *gdbarch) 00602 { 00603 struct type *objtype = get_java_object_type (); 00604 00605 if (objtype == NULL) 00606 return (2 * gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT); 00607 else 00608 return TYPE_LENGTH (objtype); 00609 } 00610 00611 int 00612 is_object_type (struct type *type) 00613 { 00614 CHECK_TYPEDEF (type); 00615 if (TYPE_CODE (type) == TYPE_CODE_PTR) 00616 { 00617 struct type *ttype = check_typedef (TYPE_TARGET_TYPE (type)); 00618 const char *name; 00619 if (TYPE_CODE (ttype) != TYPE_CODE_STRUCT) 00620 return 0; 00621 while (TYPE_N_BASECLASSES (ttype) > 0) 00622 ttype = TYPE_BASECLASS (ttype, 0); 00623 name = TYPE_TAG_NAME (ttype); 00624 if (name != NULL && strcmp (name, "java.lang.Object") == 0) 00625 return 1; 00626 name 00627 = TYPE_NFIELDS (ttype) > 0 ? TYPE_FIELD_NAME (ttype, 0) : (char *) 0; 00628 if (name != NULL && strcmp (name, "vtable") == 0) 00629 return 1; 00630 } 00631 return 0; 00632 } 00633 00634 struct type * 00635 java_primitive_type (struct gdbarch *gdbarch, int signature) 00636 { 00637 const struct builtin_java_type *builtin = builtin_java_type (gdbarch); 00638 00639 switch (signature) 00640 { 00641 case 'B': 00642 return builtin->builtin_byte; 00643 case 'S': 00644 return builtin->builtin_short; 00645 case 'I': 00646 return builtin->builtin_int; 00647 case 'J': 00648 return builtin->builtin_long; 00649 case 'Z': 00650 return builtin->builtin_boolean; 00651 case 'C': 00652 return builtin->builtin_char; 00653 case 'F': 00654 return builtin->builtin_float; 00655 case 'D': 00656 return builtin->builtin_double; 00657 case 'V': 00658 return builtin->builtin_void; 00659 } 00660 error (_("unknown signature '%c' for primitive type"), (char) signature); 00661 } 00662 00663 /* If name[0 .. namelen-1] is the name of a primitive Java type, 00664 return that type. Otherwise, return NULL. */ 00665 00666 struct type * 00667 java_primitive_type_from_name (struct gdbarch *gdbarch, 00668 const char *name, int namelen) 00669 { 00670 const struct builtin_java_type *builtin = builtin_java_type (gdbarch); 00671 00672 switch (name[0]) 00673 { 00674 case 'b': 00675 if (namelen == 4 && memcmp (name, "byte", 4) == 0) 00676 return builtin->builtin_byte; 00677 if (namelen == 7 && memcmp (name, "boolean", 7) == 0) 00678 return builtin->builtin_boolean; 00679 break; 00680 case 'c': 00681 if (namelen == 4 && memcmp (name, "char", 4) == 0) 00682 return builtin->builtin_char; 00683 break; 00684 case 'd': 00685 if (namelen == 6 && memcmp (name, "double", 6) == 0) 00686 return builtin->builtin_double; 00687 break; 00688 case 'f': 00689 if (namelen == 5 && memcmp (name, "float", 5) == 0) 00690 return builtin->builtin_float; 00691 break; 00692 case 'i': 00693 if (namelen == 3 && memcmp (name, "int", 3) == 0) 00694 return builtin->builtin_int; 00695 break; 00696 case 'l': 00697 if (namelen == 4 && memcmp (name, "long", 4) == 0) 00698 return builtin->builtin_long; 00699 break; 00700 case 's': 00701 if (namelen == 5 && memcmp (name, "short", 5) == 0) 00702 return builtin->builtin_short; 00703 break; 00704 case 'v': 00705 if (namelen == 4 && memcmp (name, "void", 4) == 0) 00706 return builtin->builtin_void; 00707 break; 00708 } 00709 return NULL; 00710 } 00711 00712 static char * 00713 java_primitive_type_name (int signature) 00714 { 00715 switch (signature) 00716 { 00717 case 'B': 00718 return "byte"; 00719 case 'S': 00720 return "short"; 00721 case 'I': 00722 return "int"; 00723 case 'J': 00724 return "long"; 00725 case 'Z': 00726 return "boolean"; 00727 case 'C': 00728 return "char"; 00729 case 'F': 00730 return "float"; 00731 case 'D': 00732 return "double"; 00733 case 'V': 00734 return "void"; 00735 } 00736 error (_("unknown signature '%c' for primitive type"), (char) signature); 00737 } 00738 00739 /* Return the length (in bytes) of demangled name of the Java type 00740 signature string SIGNATURE. */ 00741 00742 static int 00743 java_demangled_signature_length (const char *signature) 00744 { 00745 int array = 0; 00746 00747 for (; *signature == '['; signature++) 00748 array += 2; /* Two chars for "[]". */ 00749 switch (signature[0]) 00750 { 00751 case 'L': 00752 /* Subtract 2 for 'L' and ';'. */ 00753 return strlen (signature) - 2 + array; 00754 default: 00755 return strlen (java_primitive_type_name (signature[0])) + array; 00756 } 00757 } 00758 00759 /* Demangle the Java type signature SIGNATURE, leaving the result in 00760 RESULT. */ 00761 00762 static void 00763 java_demangled_signature_copy (char *result, const char *signature) 00764 { 00765 int array = 0; 00766 char *ptr; 00767 int i; 00768 00769 while (*signature == '[') 00770 { 00771 array++; 00772 signature++; 00773 } 00774 switch (signature[0]) 00775 { 00776 case 'L': 00777 /* Subtract 2 for 'L' and ';', but add 1 for final nul. */ 00778 signature++; 00779 ptr = result; 00780 for (; *signature != ';' && *signature != '\0'; signature++) 00781 { 00782 if (*signature == '/') 00783 *ptr++ = '.'; 00784 else 00785 *ptr++ = *signature; 00786 } 00787 break; 00788 default: 00789 ptr = java_primitive_type_name (signature[0]); 00790 i = strlen (ptr); 00791 strcpy (result, ptr); 00792 ptr = result + i; 00793 break; 00794 } 00795 while (--array >= 0) 00796 { 00797 *ptr++ = '['; 00798 *ptr++ = ']'; 00799 } 00800 } 00801 00802 /* Return the demangled name of the Java type signature string SIGNATURE, 00803 as a freshly allocated copy. */ 00804 00805 char * 00806 java_demangle_type_signature (const char *signature) 00807 { 00808 int length = java_demangled_signature_length (signature); 00809 char *result = xmalloc (length + 1); 00810 00811 java_demangled_signature_copy (result, signature); 00812 result[length] = '\0'; 00813 return result; 00814 } 00815 00816 /* Return the type of TYPE followed by DIMS pairs of [ ]. 00817 If DIMS == 0, TYPE is returned. */ 00818 00819 struct type * 00820 java_array_type (struct type *type, int dims) 00821 { 00822 while (dims-- > 0) 00823 { 00824 /* FIXME This is bogus! Java arrays are not gdb arrays! */ 00825 type = lookup_array_range_type (type, 0, 0); 00826 } 00827 00828 return type; 00829 } 00830 00831 /* Create a Java string in the inferior from a (Utf8) literal. */ 00832 00833 static struct value * 00834 java_value_string (char *ptr, int len) 00835 { 00836 error (_("not implemented - java_value_string")); /* FIXME */ 00837 } 00838 00839 /* Return the encoding that should be used for the character type 00840 TYPE. */ 00841 00842 static const char * 00843 java_get_encoding (struct type *type) 00844 { 00845 struct gdbarch *arch = get_type_arch (type); 00846 const char *encoding; 00847 00848 if (type == builtin_java_type (arch)->builtin_char) 00849 { 00850 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG) 00851 encoding = "UTF-16BE"; 00852 else 00853 encoding = "UTF-16LE"; 00854 } 00855 else 00856 encoding = target_charset (arch); 00857 00858 return encoding; 00859 } 00860 00861 /* Print the character C on STREAM as part of the contents of a literal 00862 string whose delimiter is QUOTER. Note that that format for printing 00863 characters and strings is language specific. */ 00864 00865 static void 00866 java_emit_char (int c, struct type *type, struct ui_file *stream, int quoter) 00867 { 00868 const char *encoding = java_get_encoding (type); 00869 00870 generic_emit_char (c, type, stream, quoter, encoding); 00871 } 00872 00873 /* Implementation of la_printchar method. */ 00874 00875 static void 00876 java_printchar (int c, struct type *type, struct ui_file *stream) 00877 { 00878 fputs_filtered ("'", stream); 00879 LA_EMIT_CHAR (c, type, stream, '\''); 00880 fputs_filtered ("'", stream); 00881 } 00882 00883 /* Implementation of la_printstr method. */ 00884 00885 static void 00886 java_printstr (struct ui_file *stream, struct type *type, 00887 const gdb_byte *string, 00888 unsigned int length, const char *encoding, int force_ellipses, 00889 const struct value_print_options *options) 00890 { 00891 const char *type_encoding = java_get_encoding (type); 00892 00893 if (!encoding || !*encoding) 00894 encoding = type_encoding; 00895 00896 generic_printstr (stream, type, string, length, encoding, 00897 force_ellipses, '"', 0, options); 00898 } 00899 00900 static struct value * 00901 evaluate_subexp_java (struct type *expect_type, struct expression *exp, 00902 int *pos, enum noside noside) 00903 { 00904 int pc = *pos; 00905 int i; 00906 const char *name; 00907 enum exp_opcode op = exp->elts[*pos].opcode; 00908 struct value *arg1; 00909 struct value *arg2; 00910 struct type *type; 00911 00912 switch (op) 00913 { 00914 case UNOP_IND: 00915 if (noside == EVAL_SKIP) 00916 goto standard; 00917 (*pos)++; 00918 arg1 = evaluate_subexp_java (NULL_TYPE, exp, pos, EVAL_NORMAL); 00919 if (is_object_type (value_type (arg1))) 00920 { 00921 struct type *type; 00922 00923 type = type_from_class (exp->gdbarch, java_class_from_object (arg1)); 00924 arg1 = value_cast (lookup_pointer_type (type), arg1); 00925 } 00926 return value_ind (arg1); 00927 00928 case BINOP_SUBSCRIPT: 00929 (*pos)++; 00930 arg1 = evaluate_subexp_with_coercion (exp, pos, noside); 00931 arg2 = evaluate_subexp_with_coercion (exp, pos, noside); 00932 if (noside == EVAL_SKIP) 00933 goto nosideret; 00934 /* If the user attempts to subscript something that is not an 00935 array or pointer type (like a plain int variable for example), 00936 then report this as an error. */ 00937 00938 arg1 = coerce_ref (arg1); 00939 type = check_typedef (value_type (arg1)); 00940 if (TYPE_CODE (type) == TYPE_CODE_PTR) 00941 type = check_typedef (TYPE_TARGET_TYPE (type)); 00942 name = TYPE_NAME (type); 00943 if (name == NULL) 00944 name = TYPE_TAG_NAME (type); 00945 i = name == NULL ? 0 : strlen (name); 00946 if (TYPE_CODE (type) == TYPE_CODE_STRUCT 00947 && i > 2 && name[i - 1] == ']') 00948 { 00949 enum bfd_endian byte_order = gdbarch_byte_order (exp->gdbarch); 00950 CORE_ADDR address; 00951 long length, index; 00952 struct type *el_type; 00953 gdb_byte buf4[4]; 00954 00955 struct value *clas = java_class_from_object (arg1); 00956 struct value *temp = clas; 00957 /* Get CLASS_ELEMENT_TYPE of the array type. */ 00958 temp = value_struct_elt (&temp, NULL, "methods", 00959 NULL, "structure"); 00960 deprecated_set_value_type (temp, value_type (clas)); 00961 el_type = type_from_class (exp->gdbarch, temp); 00962 if (TYPE_CODE (el_type) == TYPE_CODE_STRUCT) 00963 el_type = lookup_pointer_type (el_type); 00964 00965 if (noside == EVAL_AVOID_SIDE_EFFECTS) 00966 return value_zero (el_type, VALUE_LVAL (arg1)); 00967 address = value_as_address (arg1); 00968 address += get_java_object_header_size (exp->gdbarch); 00969 read_memory (address, buf4, 4); 00970 length = (long) extract_signed_integer (buf4, 4, byte_order); 00971 index = (long) value_as_long (arg2); 00972 if (index >= length || index < 0) 00973 error (_("array index (%ld) out of bounds (length: %ld)"), 00974 index, length); 00975 address = (address + 4) + index * TYPE_LENGTH (el_type); 00976 return value_at (el_type, address); 00977 } 00978 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY) 00979 { 00980 if (noside == EVAL_AVOID_SIDE_EFFECTS) 00981 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1)); 00982 else 00983 return value_subscript (arg1, value_as_long (arg2)); 00984 } 00985 if (name) 00986 error (_("cannot subscript something of type `%s'"), name); 00987 else 00988 error (_("cannot subscript requested type")); 00989 00990 case OP_STRING: 00991 (*pos)++; 00992 i = longest_to_int (exp->elts[pc + 1].longconst); 00993 (*pos) += 3 + BYTES_TO_EXP_ELEM (i + 1); 00994 if (noside == EVAL_SKIP) 00995 goto nosideret; 00996 return java_value_string (&exp->elts[pc + 2].string, i); 00997 00998 case STRUCTOP_PTR: 00999 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside); 01000 /* Convert object field (such as TYPE.class) to reference. */ 01001 if (TYPE_CODE (value_type (arg1)) == TYPE_CODE_STRUCT) 01002 arg1 = value_addr (arg1); 01003 return arg1; 01004 default: 01005 break; 01006 } 01007 standard: 01008 return evaluate_subexp_standard (expect_type, exp, pos, noside); 01009 nosideret: 01010 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1); 01011 } 01012 01013 static char *java_demangle (const char *mangled, int options) 01014 { 01015 return gdb_demangle (mangled, options | DMGL_JAVA); 01016 } 01017 01018 /* Find the member function name of the demangled name NAME. NAME 01019 must be a method name including arguments, in order to correctly 01020 locate the last component. 01021 01022 This function return a pointer to the first dot before the 01023 member function name, or NULL if the name was not of the 01024 expected form. */ 01025 01026 static const char * 01027 java_find_last_component (const char *name) 01028 { 01029 const char *p; 01030 01031 /* Find argument list. */ 01032 p = strchr (name, '('); 01033 01034 if (p == NULL) 01035 return NULL; 01036 01037 /* Back up and find first dot prior to argument list. */ 01038 while (p > name && *p != '.') 01039 p--; 01040 01041 if (p == name) 01042 return NULL; 01043 01044 return p; 01045 } 01046 01047 /* Return the name of the class containing method PHYSNAME. */ 01048 01049 static char * 01050 java_class_name_from_physname (const char *physname) 01051 { 01052 char *ret = NULL; 01053 const char *end; 01054 char *demangled_name = java_demangle (physname, DMGL_PARAMS | DMGL_ANSI); 01055 01056 if (demangled_name == NULL) 01057 return NULL; 01058 01059 end = java_find_last_component (demangled_name); 01060 if (end != NULL) 01061 { 01062 ret = xmalloc (end - demangled_name + 1); 01063 memcpy (ret, demangled_name, end - demangled_name); 01064 ret[end - demangled_name] = '\0'; 01065 } 01066 01067 xfree (demangled_name); 01068 return ret; 01069 } 01070 01071 /* Table mapping opcodes into strings for printing operators 01072 and precedences of the operators. */ 01073 01074 const struct op_print java_op_print_tab[] = 01075 { 01076 {",", BINOP_COMMA, PREC_COMMA, 0}, 01077 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1}, 01078 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0}, 01079 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0}, 01080 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0}, 01081 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0}, 01082 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0}, 01083 {"==", BINOP_EQUAL, PREC_EQUAL, 0}, 01084 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0}, 01085 {"<=", BINOP_LEQ, PREC_ORDER, 0}, 01086 {">=", BINOP_GEQ, PREC_ORDER, 0}, 01087 {">", BINOP_GTR, PREC_ORDER, 0}, 01088 {"<", BINOP_LESS, PREC_ORDER, 0}, 01089 {">>", BINOP_RSH, PREC_SHIFT, 0}, 01090 {"<<", BINOP_LSH, PREC_SHIFT, 0}, 01091 {"+", BINOP_ADD, PREC_ADD, 0}, 01092 {"-", BINOP_SUB, PREC_ADD, 0}, 01093 {"*", BINOP_MUL, PREC_MUL, 0}, 01094 {"/", BINOP_DIV, PREC_MUL, 0}, 01095 {"%", BINOP_REM, PREC_MUL, 0}, 01096 {"-", UNOP_NEG, PREC_PREFIX, 0}, 01097 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0}, 01098 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0}, 01099 {"*", UNOP_IND, PREC_PREFIX, 0}, 01100 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0}, 01101 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0}, 01102 {NULL, 0, 0, 0} 01103 }; 01104 01105 enum java_primitive_types 01106 { 01107 java_primitive_type_int, 01108 java_primitive_type_short, 01109 java_primitive_type_long, 01110 java_primitive_type_byte, 01111 java_primitive_type_boolean, 01112 java_primitive_type_char, 01113 java_primitive_type_float, 01114 java_primitive_type_double, 01115 java_primitive_type_void, 01116 nr_java_primitive_types 01117 }; 01118 01119 static void 01120 java_language_arch_info (struct gdbarch *gdbarch, 01121 struct language_arch_info *lai) 01122 { 01123 const struct builtin_java_type *builtin = builtin_java_type (gdbarch); 01124 01125 lai->string_char_type = builtin->builtin_char; 01126 lai->primitive_type_vector 01127 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_java_primitive_types + 1, 01128 struct type *); 01129 lai->primitive_type_vector [java_primitive_type_int] 01130 = builtin->builtin_int; 01131 lai->primitive_type_vector [java_primitive_type_short] 01132 = builtin->builtin_short; 01133 lai->primitive_type_vector [java_primitive_type_long] 01134 = builtin->builtin_long; 01135 lai->primitive_type_vector [java_primitive_type_byte] 01136 = builtin->builtin_byte; 01137 lai->primitive_type_vector [java_primitive_type_boolean] 01138 = builtin->builtin_boolean; 01139 lai->primitive_type_vector [java_primitive_type_char] 01140 = builtin->builtin_char; 01141 lai->primitive_type_vector [java_primitive_type_float] 01142 = builtin->builtin_float; 01143 lai->primitive_type_vector [java_primitive_type_double] 01144 = builtin->builtin_double; 01145 lai->primitive_type_vector [java_primitive_type_void] 01146 = builtin->builtin_void; 01147 01148 lai->bool_type_symbol = "boolean"; 01149 lai->bool_type_default = builtin->builtin_boolean; 01150 } 01151 01152 const struct exp_descriptor exp_descriptor_java = 01153 { 01154 print_subexp_standard, 01155 operator_length_standard, 01156 operator_check_standard, 01157 op_name_standard, 01158 dump_subexp_body_standard, 01159 evaluate_subexp_java 01160 }; 01161 01162 const struct language_defn java_language_defn = 01163 { 01164 "java", /* Language name */ 01165 language_java, 01166 range_check_off, 01167 case_sensitive_on, 01168 array_row_major, 01169 macro_expansion_no, 01170 &exp_descriptor_java, 01171 java_parse, 01172 java_error, 01173 null_post_parser, 01174 java_printchar, /* Print a character constant */ 01175 java_printstr, /* Function to print string constant */ 01176 java_emit_char, /* Function to print a single character */ 01177 java_print_type, /* Print a type using appropriate syntax */ 01178 default_print_typedef, /* Print a typedef using appropriate syntax */ 01179 java_val_print, /* Print a value using appropriate syntax */ 01180 java_value_print, /* Print a top-level value */ 01181 default_read_var_value, /* la_read_var_value */ 01182 NULL, /* Language specific skip_trampoline */ 01183 "this", /* name_of_this */ 01184 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */ 01185 basic_lookup_transparent_type,/* lookup_transparent_type */ 01186 java_demangle, /* Language specific symbol demangler */ 01187 java_class_name_from_physname,/* Language specific class name */ 01188 java_op_print_tab, /* expression operators for printing */ 01189 0, /* not c-style arrays */ 01190 0, /* String lower bound */ 01191 default_word_break_characters, 01192 default_make_symbol_completion_list, 01193 java_language_arch_info, 01194 default_print_array_index, 01195 default_pass_by_reference, 01196 default_get_string, 01197 NULL, /* la_get_symbol_name_cmp */ 01198 iterate_over_symbols, 01199 LANG_MAGIC 01200 }; 01201 01202 static void * 01203 build_java_types (struct gdbarch *gdbarch) 01204 { 01205 struct builtin_java_type *builtin_java_type 01206 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_java_type); 01207 01208 builtin_java_type->builtin_int 01209 = arch_integer_type (gdbarch, 32, 0, "int"); 01210 builtin_java_type->builtin_short 01211 = arch_integer_type (gdbarch, 16, 0, "short"); 01212 builtin_java_type->builtin_long 01213 = arch_integer_type (gdbarch, 64, 0, "long"); 01214 builtin_java_type->builtin_byte 01215 = arch_integer_type (gdbarch, 8, 0, "byte"); 01216 builtin_java_type->builtin_boolean 01217 = arch_boolean_type (gdbarch, 8, 0, "boolean"); 01218 builtin_java_type->builtin_char 01219 = arch_character_type (gdbarch, 16, 1, "char"); 01220 builtin_java_type->builtin_float 01221 = arch_float_type (gdbarch, 32, "float", NULL); 01222 builtin_java_type->builtin_double 01223 = arch_float_type (gdbarch, 64, "double", NULL); 01224 builtin_java_type->builtin_void 01225 = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void"); 01226 01227 return builtin_java_type; 01228 } 01229 01230 static struct gdbarch_data *java_type_data; 01231 01232 const struct builtin_java_type * 01233 builtin_java_type (struct gdbarch *gdbarch) 01234 { 01235 return gdbarch_data (gdbarch, java_type_data); 01236 } 01237 01238 void 01239 _initialize_java_language (void) 01240 { 01241 jv_dynamics_objfile_data_key 01242 = register_objfile_data_with_cleanup (NULL, jv_per_objfile_free); 01243 jv_dynamics_progspace_key = register_program_space_data (); 01244 01245 java_type_data = gdbarch_data_register_post_init (build_java_types); 01246 01247 add_language (&java_language_defn); 01248 }