blob: beb639e208fb238658b100947f1cf07e055b79ae [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 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
Andreas Gampef71832e2017-01-09 11:38:04 -080094jvmtiError MethodUtil::GetArgumentsSize(jvmtiEnv* env ATTRIBUTE_UNUSED,
95 jmethodID method,
96 jint* size_ptr) {
97 if (method == nullptr) {
98 return ERR(INVALID_METHODID);
99 }
100 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
101
102 if (art_method->IsNative()) {
103 return ERR(NATIVE_METHOD);
104 }
105
106 if (size_ptr == nullptr) {
107 return ERR(NULL_POINTER);
108 }
109
110 art::ScopedObjectAccess soa(art::Thread::Current());
111 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
Andreas Gampee1f79b62017-04-12 21:11:28 -0700112 // Use the shorty.
113 art::ArtMethod* base_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
114 size_t arg_count = art::ArtMethod::NumArgRegisters(base_method->GetShorty());
115 if (!base_method->IsStatic()) {
116 arg_count++;
117 }
118 *size_ptr = static_cast<jint>(arg_count);
Andreas Gampef71832e2017-01-09 11:38:04 -0800119 return ERR(NONE);
120 }
121
122 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
123 *size_ptr = art_method->GetCodeItem()->ins_size_;
124
125 return ERR(NONE);
126}
127
128jvmtiError MethodUtil::GetMaxLocals(jvmtiEnv* env ATTRIBUTE_UNUSED,
129 jmethodID method,
130 jint* max_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 (max_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 // This isn't specified as an error case, so return 0.
147 *max_ptr = 0;
148 return ERR(NONE);
149 }
150
151 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
152 *max_ptr = art_method->GetCodeItem()->registers_size_;
153
154 return ERR(NONE);
155}
156
Andreas Gampe3c252f02016-10-27 18:25:17 -0700157jvmtiError MethodUtil::GetMethodName(jvmtiEnv* env,
158 jmethodID method,
159 char** name_ptr,
160 char** signature_ptr,
161 char** generic_ptr) {
162 art::ScopedObjectAccess soa(art::Thread::Current());
Andreas Gampe13b27842016-11-07 16:48:23 -0800163 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe3c252f02016-10-27 18:25:17 -0700164 art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
165
Andreas Gampe54711412017-02-21 12:41:43 -0800166 JvmtiUniquePtr<char[]> name_copy;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700167 if (name_ptr != nullptr) {
168 const char* method_name = art_method->GetName();
169 if (method_name == nullptr) {
170 method_name = "<error>";
171 }
Andreas Gampe54711412017-02-21 12:41:43 -0800172 jvmtiError ret;
173 name_copy = CopyString(env, method_name, &ret);
174 if (name_copy == nullptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700175 return ret;
176 }
Andreas Gampe54711412017-02-21 12:41:43 -0800177 *name_ptr = name_copy.get();
Andreas Gampe3c252f02016-10-27 18:25:17 -0700178 }
179
Andreas Gampe54711412017-02-21 12:41:43 -0800180 JvmtiUniquePtr<char[]> signature_copy;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700181 if (signature_ptr != nullptr) {
182 const art::Signature sig = art_method->GetSignature();
183 std::string str = sig.ToString();
Andreas Gampe54711412017-02-21 12:41:43 -0800184 jvmtiError ret;
185 signature_copy = CopyString(env, str.c_str(), &ret);
186 if (signature_copy == nullptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700187 return ret;
188 }
Andreas Gampe54711412017-02-21 12:41:43 -0800189 *signature_ptr = signature_copy.get();
Andreas Gampe3c252f02016-10-27 18:25:17 -0700190 }
191
Andreas Gampe862bdd82016-11-18 13:31:13 -0800192 if (generic_ptr != nullptr) {
193 *generic_ptr = nullptr;
Andreas Gampe27dfa052017-02-16 15:04:36 -0800194 if (!art_method->GetDeclaringClass()->IsProxyClass()) {
195 art::mirror::ObjectArray<art::mirror::String>* str_array =
196 art::annotations::GetSignatureAnnotationForMethod(art_method);
197 if (str_array != nullptr) {
198 std::ostringstream oss;
199 for (int32_t i = 0; i != str_array->GetLength(); ++i) {
200 oss << str_array->Get(i)->ToModifiedUtf8();
201 }
202 std::string output_string = oss.str();
Andreas Gampe54711412017-02-21 12:41:43 -0800203 jvmtiError ret;
204 JvmtiUniquePtr<char[]> generic_copy = CopyString(env, output_string.c_str(), &ret);
205 if (generic_copy == nullptr) {
Andreas Gampe27dfa052017-02-16 15:04:36 -0800206 return ret;
207 }
Andreas Gampe54711412017-02-21 12:41:43 -0800208 *generic_ptr = generic_copy.release();
Andreas Gampe27dfa052017-02-16 15:04:36 -0800209 } else if (soa.Self()->IsExceptionPending()) {
210 // TODO: Should we report an error here?
211 soa.Self()->ClearException();
212 }
213 }
Andreas Gampe862bdd82016-11-18 13:31:13 -0800214 }
Andreas Gampe3c252f02016-10-27 18:25:17 -0700215
216 // Everything is fine, release the buffers.
217 name_copy.release();
218 signature_copy.release();
219
220 return ERR(NONE);
221}
222
Andreas Gampe368a2082016-10-28 17:33:13 -0700223jvmtiError MethodUtil::GetMethodDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
224 jmethodID method,
225 jclass* declaring_class_ptr) {
226 if (declaring_class_ptr == nullptr) {
227 return ERR(NULL_POINTER);
228 }
229
Andreas Gampe13b27842016-11-07 16:48:23 -0800230 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe368a2082016-10-28 17:33:13 -0700231 // Note: No GetInterfaceMethodIfProxy, we want to actual class.
232
Andreas Gampe13b27842016-11-07 16:48:23 -0800233 art::ScopedObjectAccess soa(art::Thread::Current());
Andreas Gampe368a2082016-10-28 17:33:13 -0700234 art::mirror::Class* klass = art_method->GetDeclaringClass();
235 *declaring_class_ptr = soa.AddLocalReference<jclass>(klass);
236
237 return ERR(NONE);
238}
239
Andreas Gampef71832e2017-01-09 11:38:04 -0800240jvmtiError MethodUtil::GetMethodLocation(jvmtiEnv* env ATTRIBUTE_UNUSED,
241 jmethodID method,
242 jlocation* start_location_ptr,
243 jlocation* end_location_ptr) {
244 if (method == nullptr) {
245 return ERR(INVALID_METHODID);
246 }
247 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
248
249 if (art_method->IsNative()) {
250 return ERR(NATIVE_METHOD);
251 }
252
253 if (start_location_ptr == nullptr || end_location_ptr == nullptr) {
254 return ERR(NULL_POINTER);
255 }
256
257 art::ScopedObjectAccess soa(art::Thread::Current());
258 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
Andreas Gampee1f79b62017-04-12 21:11:28 -0700259 // This isn't specified as an error case, so return -1/-1 as the RI does.
260 *start_location_ptr = -1;
261 *end_location_ptr = -1;
Andreas Gampef71832e2017-01-09 11:38:04 -0800262 return ERR(NONE);
263 }
264
265 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
266 *start_location_ptr = 0;
267 *end_location_ptr = art_method->GetCodeItem()->insns_size_in_code_units_ - 1;
268
269 return ERR(NONE);
270}
271
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700272jvmtiError MethodUtil::GetMethodModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
273 jmethodID method,
274 jint* modifiers_ptr) {
275 if (modifiers_ptr == nullptr) {
276 return ERR(NULL_POINTER);
277 }
278
Andreas Gampe13b27842016-11-07 16:48:23 -0800279 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700280 uint32_t modifiers = art_method->GetAccessFlags();
281
282 // Note: Keep this code in sync with Executable.fixMethodFlags.
283 if ((modifiers & art::kAccAbstract) != 0) {
284 modifiers &= ~art::kAccNative;
285 }
286 modifiers &= ~art::kAccSynchronized;
287 if ((modifiers & art::kAccDeclaredSynchronized) != 0) {
288 modifiers |= art::kAccSynchronized;
289 }
290 modifiers &= art::kAccJavaFlagsMask;
291
292 *modifiers_ptr = modifiers;
293 return ERR(NONE);
294}
295
Andreas Gampeda3e5612016-12-13 19:00:53 -0800296using LineNumberContext = std::vector<jvmtiLineNumberEntry>;
297
298static bool CollectLineNumbers(void* void_context, const art::DexFile::PositionInfo& entry) {
299 LineNumberContext* context = reinterpret_cast<LineNumberContext*>(void_context);
300 jvmtiLineNumberEntry jvmti_entry = { static_cast<jlocation>(entry.address_),
301 static_cast<jint>(entry.line_) };
302 context->push_back(jvmti_entry);
303 return false; // Collect all, no early exit.
304}
305
306jvmtiError MethodUtil::GetLineNumberTable(jvmtiEnv* env,
307 jmethodID method,
308 jint* entry_count_ptr,
309 jvmtiLineNumberEntry** table_ptr) {
310 if (method == nullptr) {
311 return ERR(NULL_POINTER);
312 }
313 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
314 DCHECK(!art_method->IsRuntimeMethod());
315
316 const art::DexFile::CodeItem* code_item;
317 const art::DexFile* dex_file;
318 {
319 art::ScopedObjectAccess soa(art::Thread::Current());
320
321 if (art_method->IsProxyMethod()) {
322 return ERR(ABSENT_INFORMATION);
323 }
324 if (art_method->IsNative()) {
325 return ERR(NATIVE_METHOD);
326 }
327 if (entry_count_ptr == nullptr || table_ptr == nullptr) {
328 return ERR(NULL_POINTER);
329 }
330
331 code_item = art_method->GetCodeItem();
332 dex_file = art_method->GetDexFile();
333 DCHECK(code_item != nullptr) << art_method->PrettyMethod() << " " << dex_file->GetLocation();
334 }
335
336 LineNumberContext context;
337 bool success = dex_file->DecodeDebugPositionInfo(code_item, CollectLineNumbers, &context);
338 if (!success) {
339 return ERR(ABSENT_INFORMATION);
340 }
341
342 unsigned char* data;
343 jlong mem_size = context.size() * sizeof(jvmtiLineNumberEntry);
344 jvmtiError alloc_error = env->Allocate(mem_size, &data);
345 if (alloc_error != ERR(NONE)) {
346 return alloc_error;
347 }
348 *table_ptr = reinterpret_cast<jvmtiLineNumberEntry*>(data);
349 memcpy(*table_ptr, context.data(), mem_size);
350 *entry_count_ptr = static_cast<jint>(context.size());
351
352 return ERR(NONE);
353}
354
Andreas Gampefdeef522017-01-09 14:40:25 -0800355template <typename T>
356static jvmtiError IsMethodT(jvmtiEnv* env ATTRIBUTE_UNUSED,
357 jmethodID method,
358 T test,
359 jboolean* is_t_ptr) {
360 if (method == nullptr) {
361 return ERR(INVALID_METHODID);
362 }
363 if (is_t_ptr == nullptr) {
364 return ERR(NULL_POINTER);
365 }
366
367 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
368 *is_t_ptr = test(art_method) ? JNI_TRUE : JNI_FALSE;
369
370 return ERR(NONE);
371}
372
373jvmtiError MethodUtil::IsMethodNative(jvmtiEnv* env, jmethodID m, jboolean* is_native_ptr) {
374 auto test = [](art::ArtMethod* method) {
375 return method->IsNative();
376 };
377 return IsMethodT(env, m, test, is_native_ptr);
378}
379
380jvmtiError MethodUtil::IsMethodObsolete(jvmtiEnv* env, jmethodID m, jboolean* is_obsolete_ptr) {
381 auto test = [](art::ArtMethod* method) {
382 return method->IsObsolete();
383 };
384 return IsMethodT(env, m, test, is_obsolete_ptr);
385}
386
387jvmtiError MethodUtil::IsMethodSynthetic(jvmtiEnv* env, jmethodID m, jboolean* is_synthetic_ptr) {
388 auto test = [](art::ArtMethod* method) {
389 return method->IsSynthetic();
390 };
391 return IsMethodT(env, m, test, is_synthetic_ptr);
392}
393
Andreas Gampe3c252f02016-10-27 18:25:17 -0700394} // namespace openjdkjvmti