blob: ab434d7d9a9b83a7af75179e093997e36945682c [file] [log] [blame]
Andreas Gampe3c252f02016-10-27 18:25:17 -07001/* 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"
Andreas Gampe27dfa052017-02-16 15:04:36 -080037#include "dex_file_annotations.h"
Alex Lightd78ddec2017-04-18 15:20:38 -070038#include "events-inl.h"
Andreas Gampe13b27842016-11-07 16:48:23 -080039#include "jni_internal.h"
Andreas Gampe27dfa052017-02-16 15:04:36 -080040#include "mirror/object_array-inl.h"
Andreas Gampe36bcd4f2016-10-28 18:07:18 -070041#include "modifiers.h"
Steven Morelande431e272017-07-18 16:53:49 -070042#include "nativehelper/ScopedLocalRef.h"
Alex Lightd78ddec2017-04-18 15:20:38 -070043#include "runtime_callbacks.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070044#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070045#include "thread-current-inl.h"
Alex Lightd78ddec2017-04-18 15:20:38 -070046#include "thread_list.h"
Alex Light0af8cde2017-04-20 13:35:05 -070047#include "ti_phase.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070048
49namespace openjdkjvmti {
50
Alex Lightd78ddec2017-04-18 15:20:38 -070051struct TiMethodCallback : public art::MethodCallback {
52 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();
Alex Light0af8cde2017-04-20 13:35:05 -070058 art::JNIEnvExt* jnienv = thread->GetJniEnv();
Alex Lightd78ddec2017-04-18 15:20:38 -070059 ScopedLocalRef<jthread> thread_jni(
Alex Light0af8cde2017-04-20 13:35:05 -070060 jnienv, PhaseUtil::IsLivePhase() ? jnienv->AddLocalReference<jthread>(thread->GetPeer())
61 : nullptr);
Alex Lightd78ddec2017-04-18 15:20:38 -070062 art::ScopedThreadSuspension sts(thread, art::ThreadState::kNative);
63 event_handler->DispatchEvent<ArtJvmtiEvent::kNativeMethodBind>(
64 thread,
Alex Light0af8cde2017-04-20 13:35:05 -070065 static_cast<JNIEnv*>(jnienv),
Alex Lightd78ddec2017-04-18 15:20:38 -070066 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
76TiMethodCallback gMethodCallback;
77
78void 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
86void 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
Alex Light4c174282017-07-05 10:18:18 -070094jvmtiError 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
Andreas Gampef71832e2017-01-09 11:38:04 -0800128jvmtiError 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()) {
Andreas Gampee1f79b62017-04-12 21:11:28 -0700146 // 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);
Andreas Gampef71832e2017-01-09 11:38:04 -0800153 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
162jvmtiError 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
Andreas Gampe3c252f02016-10-27 18:25:17 -0700191jvmtiError 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());
Andreas Gampe13b27842016-11-07 16:48:23 -0800197 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe3c252f02016-10-27 18:25:17 -0700198 art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
199
Andreas Gampe54711412017-02-21 12:41:43 -0800200 JvmtiUniquePtr<char[]> name_copy;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700201 if (name_ptr != nullptr) {
202 const char* method_name = art_method->GetName();
203 if (method_name == nullptr) {
204 method_name = "<error>";
205 }
Andreas Gampe54711412017-02-21 12:41:43 -0800206 jvmtiError ret;
207 name_copy = CopyString(env, method_name, &ret);
208 if (name_copy == nullptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700209 return ret;
210 }
Andreas Gampe54711412017-02-21 12:41:43 -0800211 *name_ptr = name_copy.get();
Andreas Gampe3c252f02016-10-27 18:25:17 -0700212 }
213
Andreas Gampe54711412017-02-21 12:41:43 -0800214 JvmtiUniquePtr<char[]> signature_copy;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700215 if (signature_ptr != nullptr) {
216 const art::Signature sig = art_method->GetSignature();
217 std::string str = sig.ToString();
Andreas Gampe54711412017-02-21 12:41:43 -0800218 jvmtiError ret;
219 signature_copy = CopyString(env, str.c_str(), &ret);
220 if (signature_copy == nullptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700221 return ret;
222 }
Andreas Gampe54711412017-02-21 12:41:43 -0800223 *signature_ptr = signature_copy.get();
Andreas Gampe3c252f02016-10-27 18:25:17 -0700224 }
225
Andreas Gampe862bdd82016-11-18 13:31:13 -0800226 if (generic_ptr != nullptr) {
227 *generic_ptr = nullptr;
Andreas Gampe27dfa052017-02-16 15:04:36 -0800228 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();
Andreas Gampe54711412017-02-21 12:41:43 -0800237 jvmtiError ret;
238 JvmtiUniquePtr<char[]> generic_copy = CopyString(env, output_string.c_str(), &ret);
239 if (generic_copy == nullptr) {
Andreas Gampe27dfa052017-02-16 15:04:36 -0800240 return ret;
241 }
Andreas Gampe54711412017-02-21 12:41:43 -0800242 *generic_ptr = generic_copy.release();
Andreas Gampe27dfa052017-02-16 15:04:36 -0800243 } else if (soa.Self()->IsExceptionPending()) {
244 // TODO: Should we report an error here?
245 soa.Self()->ClearException();
246 }
247 }
Andreas Gampe862bdd82016-11-18 13:31:13 -0800248 }
Andreas Gampe3c252f02016-10-27 18:25:17 -0700249
250 // Everything is fine, release the buffers.
251 name_copy.release();
252 signature_copy.release();
253
254 return ERR(NONE);
255}
256
Andreas Gampe368a2082016-10-28 17:33:13 -0700257jvmtiError 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
Andreas Gampe13b27842016-11-07 16:48:23 -0800264 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe368a2082016-10-28 17:33:13 -0700265 // Note: No GetInterfaceMethodIfProxy, we want to actual class.
266
Andreas Gampe13b27842016-11-07 16:48:23 -0800267 art::ScopedObjectAccess soa(art::Thread::Current());
Andreas Gampe368a2082016-10-28 17:33:13 -0700268 art::mirror::Class* klass = art_method->GetDeclaringClass();
269 *declaring_class_ptr = soa.AddLocalReference<jclass>(klass);
270
271 return ERR(NONE);
272}
273
Andreas Gampef71832e2017-01-09 11:38:04 -0800274jvmtiError 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()) {
Andreas Gampee1f79b62017-04-12 21:11:28 -0700293 // 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;
Andreas Gampef71832e2017-01-09 11:38:04 -0800296 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
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700306jvmtiError 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
Andreas Gampe13b27842016-11-07 16:48:23 -0800313 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700314 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
Andreas Gampeda3e5612016-12-13 19:00:53 -0800330using LineNumberContext = std::vector<jvmtiLineNumberEntry>;
331
332static 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
340jvmtiError 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
Andreas Gampefdeef522017-01-09 14:40:25 -0800389template <typename T>
390static 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
407jvmtiError 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
414jvmtiError 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
421jvmtiError 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
Andreas Gampe3c252f02016-10-27 18:25:17 -0700428} // namespace openjdkjvmti