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