blob: 2adabbaff49daf7c192fcd5ba2b5916c3b785507 [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"
Alex Lightd78ddec2017-04-18 15:20:38 -070042#include "runtime_callbacks.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070043#include "scoped_thread_state_change-inl.h"
Alex Lightd78ddec2017-04-18 15:20:38 -070044#include "ScopedLocalRef.h"
Andreas Gampeab2f0d02017-01-05 17:23:45 -080045#include "thread-inl.h"
Alex Lightd78ddec2017-04-18 15:20:38 -070046#include "thread_list.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070047
48namespace openjdkjvmti {
49
Alex Lightd78ddec2017-04-18 15:20:38 -070050struct TiMethodCallback : public art::MethodCallback {
51 void RegisterNativeMethod(art::ArtMethod* method,
52 const void* cur_method,
53 /*out*/void** new_method)
54 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
55 if (event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kNativeMethodBind)) {
56 art::Thread* thread = art::Thread::Current();
57 ScopedLocalRef<jthread> thread_jni(
58 thread->GetJniEnv(), thread->GetJniEnv()->AddLocalReference<jthread>(thread->GetPeer()));
59 art::ScopedThreadSuspension sts(thread, art::ThreadState::kNative);
60 event_handler->DispatchEvent<ArtJvmtiEvent::kNativeMethodBind>(
61 thread,
62 static_cast<JNIEnv*>(thread->GetJniEnv()),
63 thread_jni.get(),
64 art::jni::EncodeArtMethod(method),
65 const_cast<void*>(cur_method),
66 new_method);
67 }
68 }
69
70 EventHandler* event_handler = nullptr;
71};
72
73TiMethodCallback gMethodCallback;
74
75void MethodUtil::Register(EventHandler* handler) {
76 gMethodCallback.event_handler = handler;
77 art::ScopedThreadStateChange stsc(art::Thread::Current(),
78 art::ThreadState::kWaitingForDebuggerToAttach);
79 art::ScopedSuspendAll ssa("Add method callback");
80 art::Runtime::Current()->GetRuntimeCallbacks()->AddMethodCallback(&gMethodCallback);
81}
82
83void MethodUtil::Unregister() {
84 art::ScopedThreadStateChange stsc(art::Thread::Current(),
85 art::ThreadState::kWaitingForDebuggerToAttach);
86 art::ScopedSuspendAll ssa("Remove method callback");
87 art::Runtime* runtime = art::Runtime::Current();
88 runtime->GetRuntimeCallbacks()->RemoveMethodCallback(&gMethodCallback);
89}
90
Andreas Gampef71832e2017-01-09 11:38:04 -080091jvmtiError MethodUtil::GetArgumentsSize(jvmtiEnv* env ATTRIBUTE_UNUSED,
92 jmethodID method,
93 jint* size_ptr) {
94 if (method == nullptr) {
95 return ERR(INVALID_METHODID);
96 }
97 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
98
99 if (art_method->IsNative()) {
100 return ERR(NATIVE_METHOD);
101 }
102
103 if (size_ptr == nullptr) {
104 return ERR(NULL_POINTER);
105 }
106
107 art::ScopedObjectAccess soa(art::Thread::Current());
108 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
Andreas Gampee1f79b62017-04-12 21:11:28 -0700109 // Use the shorty.
110 art::ArtMethod* base_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
111 size_t arg_count = art::ArtMethod::NumArgRegisters(base_method->GetShorty());
112 if (!base_method->IsStatic()) {
113 arg_count++;
114 }
115 *size_ptr = static_cast<jint>(arg_count);
Andreas Gampef71832e2017-01-09 11:38:04 -0800116 return ERR(NONE);
117 }
118
119 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
120 *size_ptr = art_method->GetCodeItem()->ins_size_;
121
122 return ERR(NONE);
123}
124
125jvmtiError MethodUtil::GetMaxLocals(jvmtiEnv* env ATTRIBUTE_UNUSED,
126 jmethodID method,
127 jint* max_ptr) {
128 if (method == nullptr) {
129 return ERR(INVALID_METHODID);
130 }
131 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
132
133 if (art_method->IsNative()) {
134 return ERR(NATIVE_METHOD);
135 }
136
137 if (max_ptr == nullptr) {
138 return ERR(NULL_POINTER);
139 }
140
141 art::ScopedObjectAccess soa(art::Thread::Current());
142 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
143 // This isn't specified as an error case, so return 0.
144 *max_ptr = 0;
145 return ERR(NONE);
146 }
147
148 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
149 *max_ptr = art_method->GetCodeItem()->registers_size_;
150
151 return ERR(NONE);
152}
153
Andreas Gampe3c252f02016-10-27 18:25:17 -0700154jvmtiError MethodUtil::GetMethodName(jvmtiEnv* env,
155 jmethodID method,
156 char** name_ptr,
157 char** signature_ptr,
158 char** generic_ptr) {
159 art::ScopedObjectAccess soa(art::Thread::Current());
Andreas Gampe13b27842016-11-07 16:48:23 -0800160 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe3c252f02016-10-27 18:25:17 -0700161 art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
162
Andreas Gampe54711412017-02-21 12:41:43 -0800163 JvmtiUniquePtr<char[]> name_copy;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700164 if (name_ptr != nullptr) {
165 const char* method_name = art_method->GetName();
166 if (method_name == nullptr) {
167 method_name = "<error>";
168 }
Andreas Gampe54711412017-02-21 12:41:43 -0800169 jvmtiError ret;
170 name_copy = CopyString(env, method_name, &ret);
171 if (name_copy == nullptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700172 return ret;
173 }
Andreas Gampe54711412017-02-21 12:41:43 -0800174 *name_ptr = name_copy.get();
Andreas Gampe3c252f02016-10-27 18:25:17 -0700175 }
176
Andreas Gampe54711412017-02-21 12:41:43 -0800177 JvmtiUniquePtr<char[]> signature_copy;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700178 if (signature_ptr != nullptr) {
179 const art::Signature sig = art_method->GetSignature();
180 std::string str = sig.ToString();
Andreas Gampe54711412017-02-21 12:41:43 -0800181 jvmtiError ret;
182 signature_copy = CopyString(env, str.c_str(), &ret);
183 if (signature_copy == nullptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700184 return ret;
185 }
Andreas Gampe54711412017-02-21 12:41:43 -0800186 *signature_ptr = signature_copy.get();
Andreas Gampe3c252f02016-10-27 18:25:17 -0700187 }
188
Andreas Gampe862bdd82016-11-18 13:31:13 -0800189 if (generic_ptr != nullptr) {
190 *generic_ptr = nullptr;
Andreas Gampe27dfa052017-02-16 15:04:36 -0800191 if (!art_method->GetDeclaringClass()->IsProxyClass()) {
192 art::mirror::ObjectArray<art::mirror::String>* str_array =
193 art::annotations::GetSignatureAnnotationForMethod(art_method);
194 if (str_array != nullptr) {
195 std::ostringstream oss;
196 for (int32_t i = 0; i != str_array->GetLength(); ++i) {
197 oss << str_array->Get(i)->ToModifiedUtf8();
198 }
199 std::string output_string = oss.str();
Andreas Gampe54711412017-02-21 12:41:43 -0800200 jvmtiError ret;
201 JvmtiUniquePtr<char[]> generic_copy = CopyString(env, output_string.c_str(), &ret);
202 if (generic_copy == nullptr) {
Andreas Gampe27dfa052017-02-16 15:04:36 -0800203 return ret;
204 }
Andreas Gampe54711412017-02-21 12:41:43 -0800205 *generic_ptr = generic_copy.release();
Andreas Gampe27dfa052017-02-16 15:04:36 -0800206 } else if (soa.Self()->IsExceptionPending()) {
207 // TODO: Should we report an error here?
208 soa.Self()->ClearException();
209 }
210 }
Andreas Gampe862bdd82016-11-18 13:31:13 -0800211 }
Andreas Gampe3c252f02016-10-27 18:25:17 -0700212
213 // Everything is fine, release the buffers.
214 name_copy.release();
215 signature_copy.release();
216
217 return ERR(NONE);
218}
219
Andreas Gampe368a2082016-10-28 17:33:13 -0700220jvmtiError MethodUtil::GetMethodDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
221 jmethodID method,
222 jclass* declaring_class_ptr) {
223 if (declaring_class_ptr == nullptr) {
224 return ERR(NULL_POINTER);
225 }
226
Andreas Gampe13b27842016-11-07 16:48:23 -0800227 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe368a2082016-10-28 17:33:13 -0700228 // Note: No GetInterfaceMethodIfProxy, we want to actual class.
229
Andreas Gampe13b27842016-11-07 16:48:23 -0800230 art::ScopedObjectAccess soa(art::Thread::Current());
Andreas Gampe368a2082016-10-28 17:33:13 -0700231 art::mirror::Class* klass = art_method->GetDeclaringClass();
232 *declaring_class_ptr = soa.AddLocalReference<jclass>(klass);
233
234 return ERR(NONE);
235}
236
Andreas Gampef71832e2017-01-09 11:38:04 -0800237jvmtiError MethodUtil::GetMethodLocation(jvmtiEnv* env ATTRIBUTE_UNUSED,
238 jmethodID method,
239 jlocation* start_location_ptr,
240 jlocation* end_location_ptr) {
241 if (method == nullptr) {
242 return ERR(INVALID_METHODID);
243 }
244 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
245
246 if (art_method->IsNative()) {
247 return ERR(NATIVE_METHOD);
248 }
249
250 if (start_location_ptr == nullptr || end_location_ptr == nullptr) {
251 return ERR(NULL_POINTER);
252 }
253
254 art::ScopedObjectAccess soa(art::Thread::Current());
255 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
Andreas Gampee1f79b62017-04-12 21:11:28 -0700256 // This isn't specified as an error case, so return -1/-1 as the RI does.
257 *start_location_ptr = -1;
258 *end_location_ptr = -1;
Andreas Gampef71832e2017-01-09 11:38:04 -0800259 return ERR(NONE);
260 }
261
262 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
263 *start_location_ptr = 0;
264 *end_location_ptr = art_method->GetCodeItem()->insns_size_in_code_units_ - 1;
265
266 return ERR(NONE);
267}
268
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700269jvmtiError MethodUtil::GetMethodModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
270 jmethodID method,
271 jint* modifiers_ptr) {
272 if (modifiers_ptr == nullptr) {
273 return ERR(NULL_POINTER);
274 }
275
Andreas Gampe13b27842016-11-07 16:48:23 -0800276 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700277 uint32_t modifiers = art_method->GetAccessFlags();
278
279 // Note: Keep this code in sync with Executable.fixMethodFlags.
280 if ((modifiers & art::kAccAbstract) != 0) {
281 modifiers &= ~art::kAccNative;
282 }
283 modifiers &= ~art::kAccSynchronized;
284 if ((modifiers & art::kAccDeclaredSynchronized) != 0) {
285 modifiers |= art::kAccSynchronized;
286 }
287 modifiers &= art::kAccJavaFlagsMask;
288
289 *modifiers_ptr = modifiers;
290 return ERR(NONE);
291}
292
Andreas Gampeda3e5612016-12-13 19:00:53 -0800293using LineNumberContext = std::vector<jvmtiLineNumberEntry>;
294
295static bool CollectLineNumbers(void* void_context, const art::DexFile::PositionInfo& entry) {
296 LineNumberContext* context = reinterpret_cast<LineNumberContext*>(void_context);
297 jvmtiLineNumberEntry jvmti_entry = { static_cast<jlocation>(entry.address_),
298 static_cast<jint>(entry.line_) };
299 context->push_back(jvmti_entry);
300 return false; // Collect all, no early exit.
301}
302
303jvmtiError MethodUtil::GetLineNumberTable(jvmtiEnv* env,
304 jmethodID method,
305 jint* entry_count_ptr,
306 jvmtiLineNumberEntry** table_ptr) {
307 if (method == nullptr) {
308 return ERR(NULL_POINTER);
309 }
310 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
311 DCHECK(!art_method->IsRuntimeMethod());
312
313 const art::DexFile::CodeItem* code_item;
314 const art::DexFile* dex_file;
315 {
316 art::ScopedObjectAccess soa(art::Thread::Current());
317
318 if (art_method->IsProxyMethod()) {
319 return ERR(ABSENT_INFORMATION);
320 }
321 if (art_method->IsNative()) {
322 return ERR(NATIVE_METHOD);
323 }
324 if (entry_count_ptr == nullptr || table_ptr == nullptr) {
325 return ERR(NULL_POINTER);
326 }
327
328 code_item = art_method->GetCodeItem();
329 dex_file = art_method->GetDexFile();
330 DCHECK(code_item != nullptr) << art_method->PrettyMethod() << " " << dex_file->GetLocation();
331 }
332
333 LineNumberContext context;
334 bool success = dex_file->DecodeDebugPositionInfo(code_item, CollectLineNumbers, &context);
335 if (!success) {
336 return ERR(ABSENT_INFORMATION);
337 }
338
339 unsigned char* data;
340 jlong mem_size = context.size() * sizeof(jvmtiLineNumberEntry);
341 jvmtiError alloc_error = env->Allocate(mem_size, &data);
342 if (alloc_error != ERR(NONE)) {
343 return alloc_error;
344 }
345 *table_ptr = reinterpret_cast<jvmtiLineNumberEntry*>(data);
346 memcpy(*table_ptr, context.data(), mem_size);
347 *entry_count_ptr = static_cast<jint>(context.size());
348
349 return ERR(NONE);
350}
351
Andreas Gampefdeef522017-01-09 14:40:25 -0800352template <typename T>
353static jvmtiError IsMethodT(jvmtiEnv* env ATTRIBUTE_UNUSED,
354 jmethodID method,
355 T test,
356 jboolean* is_t_ptr) {
357 if (method == nullptr) {
358 return ERR(INVALID_METHODID);
359 }
360 if (is_t_ptr == nullptr) {
361 return ERR(NULL_POINTER);
362 }
363
364 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
365 *is_t_ptr = test(art_method) ? JNI_TRUE : JNI_FALSE;
366
367 return ERR(NONE);
368}
369
370jvmtiError MethodUtil::IsMethodNative(jvmtiEnv* env, jmethodID m, jboolean* is_native_ptr) {
371 auto test = [](art::ArtMethod* method) {
372 return method->IsNative();
373 };
374 return IsMethodT(env, m, test, is_native_ptr);
375}
376
377jvmtiError MethodUtil::IsMethodObsolete(jvmtiEnv* env, jmethodID m, jboolean* is_obsolete_ptr) {
378 auto test = [](art::ArtMethod* method) {
379 return method->IsObsolete();
380 };
381 return IsMethodT(env, m, test, is_obsolete_ptr);
382}
383
384jvmtiError MethodUtil::IsMethodSynthetic(jvmtiEnv* env, jmethodID m, jboolean* is_synthetic_ptr) {
385 auto test = [](art::ArtMethod* method) {
386 return method->IsSynthetic();
387 };
388 return IsMethodT(env, m, test, is_synthetic_ptr);
389}
390
Andreas Gampe3c252f02016-10-27 18:25:17 -0700391} // namespace openjdkjvmti