• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2016 The Android Open Source Project
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This file implements interfaces from the file jvmti.h. This implementation
5  * is licensed under the same terms as the file jvmti.h.  The
6  * copyright and license information for the file jvmti.h follows.
7  *
8  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10  *
11  * This code is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License version 2 only, as
13  * published by the Free Software Foundation.  Oracle designates this
14  * particular file as subject to the "Classpath" exception as provided
15  * by Oracle in the LICENSE file that accompanied this code.
16  *
17  * This code is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20  * version 2 for more details (a copy is included in the LICENSE file that
21  * accompanied this code).
22  *
23  * You should have received a copy of the GNU General Public License version
24  * 2 along with this work; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26  *
27  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28  * or visit www.oracle.com if you need additional information or have any
29  * questions.
30  */
31 
32 #include "ti_method.h"
33 
34 #include "art_jvmti.h"
35 #include "art_method-inl.h"
36 #include "base/enums.h"
37 #include "dex_file_annotations.h"
38 #include "events-inl.h"
39 #include "jni_internal.h"
40 #include "mirror/object_array-inl.h"
41 #include "modifiers.h"
42 #include "nativehelper/ScopedLocalRef.h"
43 #include "runtime_callbacks.h"
44 #include "scoped_thread_state_change-inl.h"
45 #include "thread-current-inl.h"
46 #include "thread_list.h"
47 #include "ti_phase.h"
48 
49 namespace openjdkjvmti {
50 
51 struct TiMethodCallback : public art::MethodCallback {
RegisterNativeMethodopenjdkjvmti::TiMethodCallback52   void RegisterNativeMethod(art::ArtMethod* method,
53                             const void* cur_method,
54                             /*out*/void** new_method)
55       OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
56     if (event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kNativeMethodBind)) {
57       art::Thread* thread = art::Thread::Current();
58       art::JNIEnvExt* jnienv = thread->GetJniEnv();
59       ScopedLocalRef<jthread> thread_jni(
60           jnienv, PhaseUtil::IsLivePhase() ? jnienv->AddLocalReference<jthread>(thread->GetPeer())
61                                            : nullptr);
62       art::ScopedThreadSuspension sts(thread, art::ThreadState::kNative);
63       event_handler->DispatchEvent<ArtJvmtiEvent::kNativeMethodBind>(
64           thread,
65           static_cast<JNIEnv*>(jnienv),
66           thread_jni.get(),
67           art::jni::EncodeArtMethod(method),
68           const_cast<void*>(cur_method),
69           new_method);
70     }
71   }
72 
73   EventHandler* event_handler = nullptr;
74 };
75 
76 TiMethodCallback gMethodCallback;
77 
Register(EventHandler * handler)78 void MethodUtil::Register(EventHandler* handler) {
79   gMethodCallback.event_handler = handler;
80   art::ScopedThreadStateChange stsc(art::Thread::Current(),
81                                     art::ThreadState::kWaitingForDebuggerToAttach);
82   art::ScopedSuspendAll ssa("Add method callback");
83   art::Runtime::Current()->GetRuntimeCallbacks()->AddMethodCallback(&gMethodCallback);
84 }
85 
Unregister()86 void MethodUtil::Unregister() {
87   art::ScopedThreadStateChange stsc(art::Thread::Current(),
88                                     art::ThreadState::kWaitingForDebuggerToAttach);
89   art::ScopedSuspendAll ssa("Remove method callback");
90   art::Runtime* runtime = art::Runtime::Current();
91   runtime->GetRuntimeCallbacks()->RemoveMethodCallback(&gMethodCallback);
92 }
93 
GetBytecodes(jvmtiEnv * env,jmethodID method,jint * size_ptr,unsigned char ** bytecode_ptr)94 jvmtiError MethodUtil::GetBytecodes(jvmtiEnv* env,
95                                     jmethodID method,
96                                     jint* size_ptr,
97                                     unsigned char** bytecode_ptr) {
98   if (method == nullptr) {
99     return ERR(INVALID_METHODID);
100   }
101   art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
102 
103   if (art_method->IsNative()) {
104     return ERR(NATIVE_METHOD);
105   }
106 
107   if (size_ptr == nullptr || bytecode_ptr == nullptr) {
108     return ERR(NULL_POINTER);
109   }
110 
111   art::ScopedObjectAccess soa(art::Thread::Current());
112   const art::DexFile::CodeItem* code_item = art_method->GetCodeItem();
113   if (code_item == nullptr) {
114     *size_ptr = 0;
115     *bytecode_ptr = nullptr;
116     return OK;
117   }
118   // 2 bytes per instruction for dex code.
119   *size_ptr = code_item->insns_size_in_code_units_ * 2;
120   jvmtiError err = env->Allocate(*size_ptr, bytecode_ptr);
121   if (err != OK) {
122     return err;
123   }
124   memcpy(*bytecode_ptr, code_item->insns_, *size_ptr);
125   return OK;
126 }
127 
GetArgumentsSize(jvmtiEnv * env ATTRIBUTE_UNUSED,jmethodID method,jint * size_ptr)128 jvmtiError MethodUtil::GetArgumentsSize(jvmtiEnv* env ATTRIBUTE_UNUSED,
129                                         jmethodID method,
130                                         jint* size_ptr) {
131   if (method == nullptr) {
132     return ERR(INVALID_METHODID);
133   }
134   art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
135 
136   if (art_method->IsNative()) {
137     return ERR(NATIVE_METHOD);
138   }
139 
140   if (size_ptr == nullptr) {
141     return ERR(NULL_POINTER);
142   }
143 
144   art::ScopedObjectAccess soa(art::Thread::Current());
145   if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
146     // Use the shorty.
147     art::ArtMethod* base_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
148     size_t arg_count = art::ArtMethod::NumArgRegisters(base_method->GetShorty());
149     if (!base_method->IsStatic()) {
150       arg_count++;
151     }
152     *size_ptr = static_cast<jint>(arg_count);
153     return ERR(NONE);
154   }
155 
156   DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
157   *size_ptr = art_method->GetCodeItem()->ins_size_;
158 
159   return ERR(NONE);
160 }
161 
GetMaxLocals(jvmtiEnv * env ATTRIBUTE_UNUSED,jmethodID method,jint * max_ptr)162 jvmtiError MethodUtil::GetMaxLocals(jvmtiEnv* env ATTRIBUTE_UNUSED,
163                                     jmethodID method,
164                                     jint* max_ptr) {
165   if (method == nullptr) {
166     return ERR(INVALID_METHODID);
167   }
168   art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
169 
170   if (art_method->IsNative()) {
171     return ERR(NATIVE_METHOD);
172   }
173 
174   if (max_ptr == nullptr) {
175     return ERR(NULL_POINTER);
176   }
177 
178   art::ScopedObjectAccess soa(art::Thread::Current());
179   if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
180     // This isn't specified as an error case, so return 0.
181     *max_ptr = 0;
182     return ERR(NONE);
183   }
184 
185   DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
186   *max_ptr = art_method->GetCodeItem()->registers_size_;
187 
188   return ERR(NONE);
189 }
190 
GetMethodName(jvmtiEnv * env,jmethodID method,char ** name_ptr,char ** signature_ptr,char ** generic_ptr)191 jvmtiError MethodUtil::GetMethodName(jvmtiEnv* env,
192                                      jmethodID method,
193                                      char** name_ptr,
194                                      char** signature_ptr,
195                                      char** generic_ptr) {
196   art::ScopedObjectAccess soa(art::Thread::Current());
197   art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
198   art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
199 
200   JvmtiUniquePtr<char[]> name_copy;
201   if (name_ptr != nullptr) {
202     const char* method_name = art_method->GetName();
203     if (method_name == nullptr) {
204       method_name = "<error>";
205     }
206     jvmtiError ret;
207     name_copy = CopyString(env, method_name, &ret);
208     if (name_copy == nullptr) {
209       return ret;
210     }
211     *name_ptr = name_copy.get();
212   }
213 
214   JvmtiUniquePtr<char[]> signature_copy;
215   if (signature_ptr != nullptr) {
216     const art::Signature sig = art_method->GetSignature();
217     std::string str = sig.ToString();
218     jvmtiError ret;
219     signature_copy = CopyString(env, str.c_str(), &ret);
220     if (signature_copy == nullptr) {
221       return ret;
222     }
223     *signature_ptr = signature_copy.get();
224   }
225 
226   if (generic_ptr != nullptr) {
227     *generic_ptr = nullptr;
228     if (!art_method->GetDeclaringClass()->IsProxyClass()) {
229       art::mirror::ObjectArray<art::mirror::String>* str_array =
230           art::annotations::GetSignatureAnnotationForMethod(art_method);
231       if (str_array != nullptr) {
232         std::ostringstream oss;
233         for (int32_t i = 0; i != str_array->GetLength(); ++i) {
234           oss << str_array->Get(i)->ToModifiedUtf8();
235         }
236         std::string output_string = oss.str();
237         jvmtiError ret;
238         JvmtiUniquePtr<char[]> generic_copy = CopyString(env, output_string.c_str(), &ret);
239         if (generic_copy == nullptr) {
240           return ret;
241         }
242         *generic_ptr = generic_copy.release();
243       } else if (soa.Self()->IsExceptionPending()) {
244         // TODO: Should we report an error here?
245         soa.Self()->ClearException();
246       }
247     }
248   }
249 
250   // Everything is fine, release the buffers.
251   name_copy.release();
252   signature_copy.release();
253 
254   return ERR(NONE);
255 }
256 
GetMethodDeclaringClass(jvmtiEnv * env ATTRIBUTE_UNUSED,jmethodID method,jclass * declaring_class_ptr)257 jvmtiError MethodUtil::GetMethodDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
258                                                jmethodID method,
259                                                jclass* declaring_class_ptr) {
260   if (declaring_class_ptr == nullptr) {
261     return ERR(NULL_POINTER);
262   }
263 
264   art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
265   // Note: No GetInterfaceMethodIfProxy, we want to actual class.
266 
267   art::ScopedObjectAccess soa(art::Thread::Current());
268   art::mirror::Class* klass = art_method->GetDeclaringClass();
269   *declaring_class_ptr = soa.AddLocalReference<jclass>(klass);
270 
271   return ERR(NONE);
272 }
273 
GetMethodLocation(jvmtiEnv * env ATTRIBUTE_UNUSED,jmethodID method,jlocation * start_location_ptr,jlocation * end_location_ptr)274 jvmtiError MethodUtil::GetMethodLocation(jvmtiEnv* env ATTRIBUTE_UNUSED,
275                                          jmethodID method,
276                                          jlocation* start_location_ptr,
277                                          jlocation* end_location_ptr) {
278   if (method == nullptr) {
279     return ERR(INVALID_METHODID);
280   }
281   art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
282 
283   if (art_method->IsNative()) {
284     return ERR(NATIVE_METHOD);
285   }
286 
287   if (start_location_ptr == nullptr || end_location_ptr == nullptr) {
288     return ERR(NULL_POINTER);
289   }
290 
291   art::ScopedObjectAccess soa(art::Thread::Current());
292   if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
293     // This isn't specified as an error case, so return -1/-1 as the RI does.
294     *start_location_ptr = -1;
295     *end_location_ptr = -1;
296     return ERR(NONE);
297   }
298 
299   DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
300   *start_location_ptr = 0;
301   *end_location_ptr = art_method->GetCodeItem()->insns_size_in_code_units_ - 1;
302 
303   return ERR(NONE);
304 }
305 
GetMethodModifiers(jvmtiEnv * env ATTRIBUTE_UNUSED,jmethodID method,jint * modifiers_ptr)306 jvmtiError MethodUtil::GetMethodModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
307                                           jmethodID method,
308                                           jint* modifiers_ptr) {
309   if (modifiers_ptr == nullptr) {
310     return ERR(NULL_POINTER);
311   }
312 
313   art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
314   uint32_t modifiers = art_method->GetAccessFlags();
315 
316   // Note: Keep this code in sync with Executable.fixMethodFlags.
317   if ((modifiers & art::kAccAbstract) != 0) {
318     modifiers &= ~art::kAccNative;
319   }
320   modifiers &= ~art::kAccSynchronized;
321   if ((modifiers & art::kAccDeclaredSynchronized) != 0) {
322     modifiers |= art::kAccSynchronized;
323   }
324   modifiers &= art::kAccJavaFlagsMask;
325 
326   *modifiers_ptr = modifiers;
327   return ERR(NONE);
328 }
329 
330 using LineNumberContext = std::vector<jvmtiLineNumberEntry>;
331 
CollectLineNumbers(void * void_context,const art::DexFile::PositionInfo & entry)332 static bool CollectLineNumbers(void* void_context, const art::DexFile::PositionInfo& entry) {
333   LineNumberContext* context = reinterpret_cast<LineNumberContext*>(void_context);
334   jvmtiLineNumberEntry jvmti_entry = { static_cast<jlocation>(entry.address_),
335                                        static_cast<jint>(entry.line_) };
336   context->push_back(jvmti_entry);
337   return false;  // Collect all, no early exit.
338 }
339 
GetLineNumberTable(jvmtiEnv * env,jmethodID method,jint * entry_count_ptr,jvmtiLineNumberEntry ** table_ptr)340 jvmtiError MethodUtil::GetLineNumberTable(jvmtiEnv* env,
341                                           jmethodID method,
342                                           jint* entry_count_ptr,
343                                           jvmtiLineNumberEntry** table_ptr) {
344   if (method == nullptr) {
345     return ERR(NULL_POINTER);
346   }
347   art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
348   DCHECK(!art_method->IsRuntimeMethod());
349 
350   const art::DexFile::CodeItem* code_item;
351   const art::DexFile* dex_file;
352   {
353     art::ScopedObjectAccess soa(art::Thread::Current());
354 
355     if (art_method->IsProxyMethod()) {
356       return ERR(ABSENT_INFORMATION);
357     }
358     if (art_method->IsNative()) {
359       return ERR(NATIVE_METHOD);
360     }
361     if (entry_count_ptr == nullptr || table_ptr == nullptr) {
362       return ERR(NULL_POINTER);
363     }
364 
365     code_item = art_method->GetCodeItem();
366     dex_file = art_method->GetDexFile();
367     DCHECK(code_item != nullptr) << art_method->PrettyMethod() << " " << dex_file->GetLocation();
368   }
369 
370   LineNumberContext context;
371   bool success = dex_file->DecodeDebugPositionInfo(code_item, CollectLineNumbers, &context);
372   if (!success) {
373     return ERR(ABSENT_INFORMATION);
374   }
375 
376   unsigned char* data;
377   jlong mem_size = context.size() * sizeof(jvmtiLineNumberEntry);
378   jvmtiError alloc_error = env->Allocate(mem_size, &data);
379   if (alloc_error != ERR(NONE)) {
380     return alloc_error;
381   }
382   *table_ptr = reinterpret_cast<jvmtiLineNumberEntry*>(data);
383   memcpy(*table_ptr, context.data(), mem_size);
384   *entry_count_ptr = static_cast<jint>(context.size());
385 
386   return ERR(NONE);
387 }
388 
389 template <typename T>
IsMethodT(jvmtiEnv * env ATTRIBUTE_UNUSED,jmethodID method,T test,jboolean * is_t_ptr)390 static jvmtiError IsMethodT(jvmtiEnv* env ATTRIBUTE_UNUSED,
391                             jmethodID method,
392                             T test,
393                             jboolean* is_t_ptr) {
394   if (method == nullptr) {
395     return ERR(INVALID_METHODID);
396   }
397   if (is_t_ptr == nullptr) {
398     return ERR(NULL_POINTER);
399   }
400 
401   art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
402   *is_t_ptr = test(art_method) ? JNI_TRUE : JNI_FALSE;
403 
404   return ERR(NONE);
405 }
406 
IsMethodNative(jvmtiEnv * env,jmethodID m,jboolean * is_native_ptr)407 jvmtiError MethodUtil::IsMethodNative(jvmtiEnv* env, jmethodID m, jboolean* is_native_ptr) {
408   auto test = [](art::ArtMethod* method) {
409     return method->IsNative();
410   };
411   return IsMethodT(env, m, test, is_native_ptr);
412 }
413 
IsMethodObsolete(jvmtiEnv * env,jmethodID m,jboolean * is_obsolete_ptr)414 jvmtiError MethodUtil::IsMethodObsolete(jvmtiEnv* env, jmethodID m, jboolean* is_obsolete_ptr) {
415   auto test = [](art::ArtMethod* method) {
416     return method->IsObsolete();
417   };
418   return IsMethodT(env, m, test, is_obsolete_ptr);
419 }
420 
IsMethodSynthetic(jvmtiEnv * env,jmethodID m,jboolean * is_synthetic_ptr)421 jvmtiError MethodUtil::IsMethodSynthetic(jvmtiEnv* env, jmethodID m, jboolean* is_synthetic_ptr) {
422   auto test = [](art::ArtMethod* method) {
423     return method->IsSynthetic();
424   };
425   return IsMethodT(env, m, test, is_synthetic_ptr);
426 }
427 
428 }  // namespace openjdkjvmti
429