OSDN Git Service

2007-03-06 Kyle Galloway <kgallowa@redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / gnu / classpath / jdwp / natVMMethod.cc
1 // natVMMethod.cc -- native support for VMMethod
2
3 /* Copyright (C) 2006, 2007 Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 #include <config.h>
12 #include <gcj/cni.h>
13 #include <java-interp.h>
14 #include <jvmti.h>
15 #include "jvmti-int.h"
16
17 #include <gnu/classpath/jdwp/VMMethod.h>
18 #include <gnu/classpath/jdwp/exception/AbsentInformationException.h>
19 #include <gnu/classpath/jdwp/exception/InvalidMethodException.h>
20 #include <gnu/classpath/jdwp/exception/JdwpInternalErrorException.h>
21 #include <gnu/classpath/jdwp/util/LineTable.h>
22 #include <gnu/classpath/jdwp/util/VariableTable.h>
23
24 using namespace java::lang;
25
26 #define CHECK_INTERP_CLASS()    \
27 do                                                              \
28   {                                                             \
29     if (!_Jv_IsInterpretedClass (getDeclaringClass ())) \
30       {                                                                                                 \
31         ::java::lang::String *msg = JvNewStringLatin1 ("native class"); \
32         throw new exception::JdwpInternalErrorException (msg);                  \
33       }                                                                                                 \
34   }                                                             \
35 while (0)
36
37 jstring
38 gnu::classpath::jdwp::VMMethod::getName ()
39 {
40   jvmtiEnv *env = _Jv_GetJDWP_JVMTIEnv ();
41   jmethodID method = reinterpret_cast<jmethodID> (_methodId);
42   char *name;
43   env->GetMethodName (method, &name, NULL, NULL);
44   jstring string = JvNewStringUTF (name);
45   env->Deallocate (reinterpret_cast<unsigned char *> (name));
46   return string;
47 }
48
49 jstring
50 gnu::classpath::jdwp::VMMethod::getSignature ()
51 {
52   jvmtiEnv *env = _Jv_GetJDWP_JVMTIEnv ();
53   jmethodID method = reinterpret_cast<jmethodID> (_methodId);
54   char *signature;
55   env->GetMethodName (method, NULL, &signature, NULL);
56   jstring string = JvNewStringUTF (signature);
57   env->Deallocate (reinterpret_cast<unsigned char *> (signature));
58   return string;
59 }
60
61 jint
62 gnu::classpath::jdwp::VMMethod::getModifiers ()
63 {
64   jvmtiEnv *env = _Jv_GetJDWP_JVMTIEnv ();
65   jmethodID method = reinterpret_cast<jmethodID> (_methodId);
66   jint flags;
67   env->GetMethodModifiers (method, &flags);
68   return flags;
69 }
70
71 gnu::classpath::jdwp::util::LineTable *
72 gnu::classpath::jdwp::VMMethod::getLineTable ()
73 {
74   CHECK_INTERP_CLASS ();
75
76   jmethodID desired_method = reinterpret_cast<jmethodID> (_methodId);
77
78   _Jv_MethodBase *theMethod
79     = _Jv_FindInterpreterMethod (getDeclaringClass (), desired_method);
80
81   if (theMethod == NULL)
82     {
83       // this should not happen
84       ::java::lang::String *msg
85         = JvNewStringLatin1 ("could not find method in class");
86       throw new exception::JdwpInternalErrorException (msg);
87     }
88
89   if (::java::lang::reflect::Modifier::isNative (desired_method->accflags))
90     {
91       jintArray lines = JvNewIntArray (0);
92       jlongArray indices = JvNewLongArray (0);
93       return new util::LineTable (-1, -1, lines, indices);
94     }
95
96   // get the linetable
97   _Jv_InterpMethod *imeth = reinterpret_cast<_Jv_InterpMethod *> (theMethod);
98   jlong start;
99   jlong end;
100   jintArray lines;
101   jlongArray indices;
102   imeth->get_line_table (start, end, lines, indices);
103   return new util::LineTable (start, end, lines, indices);
104 }
105
106
107 gnu::classpath::jdwp::util::VariableTable*
108 gnu::classpath::jdwp::VMMethod::getVariableTable ()
109 {
110   using namespace gnu::classpath::jdwp::util;
111   
112   jvmtiEnv *env = _Jv_GetJDWP_JVMTIEnv ();
113         
114   CHECK_INTERP_CLASS ();
115   
116   jmethodID meth = reinterpret_cast<jmethodID> (_methodId);
117   jvmtiLocalVariableEntry *var_table;
118   jint num_slots, args_len;
119   
120   jvmtiError jerr = env->GetLocalVariableTable (meth, &num_slots, &var_table);
121   
122   if (jerr != JVMTI_ERROR_NONE)
123     goto error;
124   
125   jerr = env->GetArgumentsSize (meth, &args_len);
126   
127   if (jerr != JVMTI_ERROR_NONE)
128     {
129     error:
130       using namespace gnu::classpath::jdwp::exception;
131       char *error;
132       env->GetErrorName (jerr, &error);
133       String *msg = JvNewStringUTF (error);
134       env->Deallocate (reinterpret_cast<unsigned char *> (error));
135       
136       if (jerr == JVMTI_ERROR_NATIVE_METHOD)
137         throw new AbsentInformationException (msg);
138       else if (jerr == JVMTI_ERROR_INVALID_METHODID)
139         throw new InvalidMethodException (_methodId);
140       else
141         throw new JdwpInternalErrorException (msg);
142     }
143   
144   jlongArray start_pcs = JvNewLongArray (num_slots);
145   jlong *start_pcs_ptr = elements (start_pcs);
146   jintArray lengths = JvNewIntArray (num_slots);
147   jint *lengths_ptr = elements (lengths);
148   jintArray slots = JvNewIntArray (num_slots);
149   jint *slots_ptr = elements (slots);
150   JArray<String *> *names = reinterpret_cast<JArray<String *> *> 
151                               (JvNewObjectArray (num_slots, 
152                                                  &String::class$, NULL));
153   jstring *names_ptr = elements (names);
154   JArray<String *> *signatures = reinterpret_cast<JArray<String *> *>
155                                    (JvNewObjectArray (num_slots, 
156                                                       &String::class$, NULL));
157   jstring *signatures_ptr = elements (signatures);
158   
159   // Get the information out of the JVMTI strucutre and Deallocate the strings.
160   for (int i = 0; i < num_slots; i++)
161     {
162       start_pcs_ptr[i] = var_table[i].start_location;
163       lengths_ptr[i] = var_table[i].length;
164       slots_ptr[i] = var_table[i].slot;
165       names_ptr[i] = JvNewStringUTF (var_table[i].name);
166       env->Deallocate (reinterpret_cast<unsigned char *> 
167                          (var_table[i].name));
168       signatures_ptr[i] = JvNewStringUTF (var_table[i].signature);
169       env->Deallocate (reinterpret_cast<unsigned char *> 
170                          (var_table[i].signature));
171       env->Deallocate (reinterpret_cast<unsigned char *>
172                          (var_table[i].generic_signature));
173     }
174   
175   // Now Deallocate the table since it's strings have already been freed.
176   env->Deallocate (reinterpret_cast<unsigned char *> (var_table));
177   
178   // Create the new JDWP VariableTable to return with the now filled arrays.
179   VariableTable* jdwp_vtable = new VariableTable (args_len, num_slots,
180                                                   start_pcs, names, signatures,
181                                                   lengths, slots);
182   
183   return jdwp_vtable;
184 }