blob: 01bf21d53e8f0defed685cb258fc6612301d1fcd [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"
Andreas Gampe13b27842016-11-07 16:48:23 -080038#include "jni_internal.h"
Andreas Gampe27dfa052017-02-16 15:04:36 -080039#include "mirror/object_array-inl.h"
Andreas Gampe36bcd4f2016-10-28 18:07:18 -070040#include "modifiers.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070041#include "scoped_thread_state_change-inl.h"
Andreas Gampeab2f0d02017-01-05 17:23:45 -080042#include "thread-inl.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070043
44namespace openjdkjvmti {
45
Andreas Gampef71832e2017-01-09 11:38:04 -080046jvmtiError MethodUtil::GetArgumentsSize(jvmtiEnv* env ATTRIBUTE_UNUSED,
47 jmethodID method,
48 jint* size_ptr) {
49 if (method == nullptr) {
50 return ERR(INVALID_METHODID);
51 }
52 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
53
54 if (art_method->IsNative()) {
55 return ERR(NATIVE_METHOD);
56 }
57
58 if (size_ptr == nullptr) {
59 return ERR(NULL_POINTER);
60 }
61
62 art::ScopedObjectAccess soa(art::Thread::Current());
63 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
Andreas Gampee1f79b62017-04-12 21:11:28 -070064 // Use the shorty.
65 art::ArtMethod* base_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
66 size_t arg_count = art::ArtMethod::NumArgRegisters(base_method->GetShorty());
67 if (!base_method->IsStatic()) {
68 arg_count++;
69 }
70 *size_ptr = static_cast<jint>(arg_count);
Andreas Gampef71832e2017-01-09 11:38:04 -080071 return ERR(NONE);
72 }
73
74 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
75 *size_ptr = art_method->GetCodeItem()->ins_size_;
76
77 return ERR(NONE);
78}
79
80jvmtiError MethodUtil::GetMaxLocals(jvmtiEnv* env ATTRIBUTE_UNUSED,
81 jmethodID method,
82 jint* max_ptr) {
83 if (method == nullptr) {
84 return ERR(INVALID_METHODID);
85 }
86 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
87
88 if (art_method->IsNative()) {
89 return ERR(NATIVE_METHOD);
90 }
91
92 if (max_ptr == nullptr) {
93 return ERR(NULL_POINTER);
94 }
95
96 art::ScopedObjectAccess soa(art::Thread::Current());
97 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
98 // This isn't specified as an error case, so return 0.
99 *max_ptr = 0;
100 return ERR(NONE);
101 }
102
103 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
104 *max_ptr = art_method->GetCodeItem()->registers_size_;
105
106 return ERR(NONE);
107}
108
Andreas Gampe3c252f02016-10-27 18:25:17 -0700109jvmtiError MethodUtil::GetMethodName(jvmtiEnv* env,
110 jmethodID method,
111 char** name_ptr,
112 char** signature_ptr,
113 char** generic_ptr) {
114 art::ScopedObjectAccess soa(art::Thread::Current());
Andreas Gampe13b27842016-11-07 16:48:23 -0800115 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe3c252f02016-10-27 18:25:17 -0700116 art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
117
Andreas Gampe54711412017-02-21 12:41:43 -0800118 JvmtiUniquePtr<char[]> name_copy;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700119 if (name_ptr != nullptr) {
120 const char* method_name = art_method->GetName();
121 if (method_name == nullptr) {
122 method_name = "<error>";
123 }
Andreas Gampe54711412017-02-21 12:41:43 -0800124 jvmtiError ret;
125 name_copy = CopyString(env, method_name, &ret);
126 if (name_copy == nullptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700127 return ret;
128 }
Andreas Gampe54711412017-02-21 12:41:43 -0800129 *name_ptr = name_copy.get();
Andreas Gampe3c252f02016-10-27 18:25:17 -0700130 }
131
Andreas Gampe54711412017-02-21 12:41:43 -0800132 JvmtiUniquePtr<char[]> signature_copy;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700133 if (signature_ptr != nullptr) {
134 const art::Signature sig = art_method->GetSignature();
135 std::string str = sig.ToString();
Andreas Gampe54711412017-02-21 12:41:43 -0800136 jvmtiError ret;
137 signature_copy = CopyString(env, str.c_str(), &ret);
138 if (signature_copy == nullptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700139 return ret;
140 }
Andreas Gampe54711412017-02-21 12:41:43 -0800141 *signature_ptr = signature_copy.get();
Andreas Gampe3c252f02016-10-27 18:25:17 -0700142 }
143
Andreas Gampe862bdd82016-11-18 13:31:13 -0800144 if (generic_ptr != nullptr) {
145 *generic_ptr = nullptr;
Andreas Gampe27dfa052017-02-16 15:04:36 -0800146 if (!art_method->GetDeclaringClass()->IsProxyClass()) {
147 art::mirror::ObjectArray<art::mirror::String>* str_array =
148 art::annotations::GetSignatureAnnotationForMethod(art_method);
149 if (str_array != nullptr) {
150 std::ostringstream oss;
151 for (int32_t i = 0; i != str_array->GetLength(); ++i) {
152 oss << str_array->Get(i)->ToModifiedUtf8();
153 }
154 std::string output_string = oss.str();
Andreas Gampe54711412017-02-21 12:41:43 -0800155 jvmtiError ret;
156 JvmtiUniquePtr<char[]> generic_copy = CopyString(env, output_string.c_str(), &ret);
157 if (generic_copy == nullptr) {
Andreas Gampe27dfa052017-02-16 15:04:36 -0800158 return ret;
159 }
Andreas Gampe54711412017-02-21 12:41:43 -0800160 *generic_ptr = generic_copy.release();
Andreas Gampe27dfa052017-02-16 15:04:36 -0800161 } else if (soa.Self()->IsExceptionPending()) {
162 // TODO: Should we report an error here?
163 soa.Self()->ClearException();
164 }
165 }
Andreas Gampe862bdd82016-11-18 13:31:13 -0800166 }
Andreas Gampe3c252f02016-10-27 18:25:17 -0700167
168 // Everything is fine, release the buffers.
169 name_copy.release();
170 signature_copy.release();
171
172 return ERR(NONE);
173}
174
Andreas Gampe368a2082016-10-28 17:33:13 -0700175jvmtiError MethodUtil::GetMethodDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
176 jmethodID method,
177 jclass* declaring_class_ptr) {
178 if (declaring_class_ptr == nullptr) {
179 return ERR(NULL_POINTER);
180 }
181
Andreas Gampe13b27842016-11-07 16:48:23 -0800182 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe368a2082016-10-28 17:33:13 -0700183 // Note: No GetInterfaceMethodIfProxy, we want to actual class.
184
Andreas Gampe13b27842016-11-07 16:48:23 -0800185 art::ScopedObjectAccess soa(art::Thread::Current());
Andreas Gampe368a2082016-10-28 17:33:13 -0700186 art::mirror::Class* klass = art_method->GetDeclaringClass();
187 *declaring_class_ptr = soa.AddLocalReference<jclass>(klass);
188
189 return ERR(NONE);
190}
191
Andreas Gampef71832e2017-01-09 11:38:04 -0800192jvmtiError MethodUtil::GetMethodLocation(jvmtiEnv* env ATTRIBUTE_UNUSED,
193 jmethodID method,
194 jlocation* start_location_ptr,
195 jlocation* end_location_ptr) {
196 if (method == nullptr) {
197 return ERR(INVALID_METHODID);
198 }
199 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
200
201 if (art_method->IsNative()) {
202 return ERR(NATIVE_METHOD);
203 }
204
205 if (start_location_ptr == nullptr || end_location_ptr == nullptr) {
206 return ERR(NULL_POINTER);
207 }
208
209 art::ScopedObjectAccess soa(art::Thread::Current());
210 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
Andreas Gampee1f79b62017-04-12 21:11:28 -0700211 // This isn't specified as an error case, so return -1/-1 as the RI does.
212 *start_location_ptr = -1;
213 *end_location_ptr = -1;
Andreas Gampef71832e2017-01-09 11:38:04 -0800214 return ERR(NONE);
215 }
216
217 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
218 *start_location_ptr = 0;
219 *end_location_ptr = art_method->GetCodeItem()->insns_size_in_code_units_ - 1;
220
221 return ERR(NONE);
222}
223
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700224jvmtiError MethodUtil::GetMethodModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
225 jmethodID method,
226 jint* modifiers_ptr) {
227 if (modifiers_ptr == nullptr) {
228 return ERR(NULL_POINTER);
229 }
230
Andreas Gampe13b27842016-11-07 16:48:23 -0800231 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700232 uint32_t modifiers = art_method->GetAccessFlags();
233
234 // Note: Keep this code in sync with Executable.fixMethodFlags.
235 if ((modifiers & art::kAccAbstract) != 0) {
236 modifiers &= ~art::kAccNative;
237 }
238 modifiers &= ~art::kAccSynchronized;
239 if ((modifiers & art::kAccDeclaredSynchronized) != 0) {
240 modifiers |= art::kAccSynchronized;
241 }
242 modifiers &= art::kAccJavaFlagsMask;
243
244 *modifiers_ptr = modifiers;
245 return ERR(NONE);
246}
247
Andreas Gampeda3e5612016-12-13 19:00:53 -0800248using LineNumberContext = std::vector<jvmtiLineNumberEntry>;
249
250static bool CollectLineNumbers(void* void_context, const art::DexFile::PositionInfo& entry) {
251 LineNumberContext* context = reinterpret_cast<LineNumberContext*>(void_context);
252 jvmtiLineNumberEntry jvmti_entry = { static_cast<jlocation>(entry.address_),
253 static_cast<jint>(entry.line_) };
254 context->push_back(jvmti_entry);
255 return false; // Collect all, no early exit.
256}
257
258jvmtiError MethodUtil::GetLineNumberTable(jvmtiEnv* env,
259 jmethodID method,
260 jint* entry_count_ptr,
261 jvmtiLineNumberEntry** table_ptr) {
262 if (method == nullptr) {
263 return ERR(NULL_POINTER);
264 }
265 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
266 DCHECK(!art_method->IsRuntimeMethod());
267
268 const art::DexFile::CodeItem* code_item;
269 const art::DexFile* dex_file;
270 {
271 art::ScopedObjectAccess soa(art::Thread::Current());
272
273 if (art_method->IsProxyMethod()) {
274 return ERR(ABSENT_INFORMATION);
275 }
276 if (art_method->IsNative()) {
277 return ERR(NATIVE_METHOD);
278 }
279 if (entry_count_ptr == nullptr || table_ptr == nullptr) {
280 return ERR(NULL_POINTER);
281 }
282
283 code_item = art_method->GetCodeItem();
284 dex_file = art_method->GetDexFile();
285 DCHECK(code_item != nullptr) << art_method->PrettyMethod() << " " << dex_file->GetLocation();
286 }
287
288 LineNumberContext context;
289 bool success = dex_file->DecodeDebugPositionInfo(code_item, CollectLineNumbers, &context);
290 if (!success) {
291 return ERR(ABSENT_INFORMATION);
292 }
293
294 unsigned char* data;
295 jlong mem_size = context.size() * sizeof(jvmtiLineNumberEntry);
296 jvmtiError alloc_error = env->Allocate(mem_size, &data);
297 if (alloc_error != ERR(NONE)) {
298 return alloc_error;
299 }
300 *table_ptr = reinterpret_cast<jvmtiLineNumberEntry*>(data);
301 memcpy(*table_ptr, context.data(), mem_size);
302 *entry_count_ptr = static_cast<jint>(context.size());
303
304 return ERR(NONE);
305}
306
Andreas Gampefdeef522017-01-09 14:40:25 -0800307template <typename T>
308static jvmtiError IsMethodT(jvmtiEnv* env ATTRIBUTE_UNUSED,
309 jmethodID method,
310 T test,
311 jboolean* is_t_ptr) {
312 if (method == nullptr) {
313 return ERR(INVALID_METHODID);
314 }
315 if (is_t_ptr == nullptr) {
316 return ERR(NULL_POINTER);
317 }
318
319 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
320 *is_t_ptr = test(art_method) ? JNI_TRUE : JNI_FALSE;
321
322 return ERR(NONE);
323}
324
325jvmtiError MethodUtil::IsMethodNative(jvmtiEnv* env, jmethodID m, jboolean* is_native_ptr) {
326 auto test = [](art::ArtMethod* method) {
327 return method->IsNative();
328 };
329 return IsMethodT(env, m, test, is_native_ptr);
330}
331
332jvmtiError MethodUtil::IsMethodObsolete(jvmtiEnv* env, jmethodID m, jboolean* is_obsolete_ptr) {
333 auto test = [](art::ArtMethod* method) {
334 return method->IsObsolete();
335 };
336 return IsMethodT(env, m, test, is_obsolete_ptr);
337}
338
339jvmtiError MethodUtil::IsMethodSynthetic(jvmtiEnv* env, jmethodID m, jboolean* is_synthetic_ptr) {
340 auto test = [](art::ArtMethod* method) {
341 return method->IsSynthetic();
342 };
343 return IsMethodT(env, m, test, is_synthetic_ptr);
344}
345
Andreas Gampe3c252f02016-10-27 18:25:17 -0700346} // namespace openjdkjvmti