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