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