blob: 02b609049fed86734a8c604d2cb4ace53c07fa20 [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 Gampe13b27842016-11-07 16:48:23 -080037#include "jni_internal.h"
Andreas Gampe36bcd4f2016-10-28 18:07:18 -070038#include "modifiers.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070039#include "scoped_thread_state_change-inl.h"
Andreas Gampeab2f0d02017-01-05 17:23:45 -080040#include "thread-inl.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070041
42namespace openjdkjvmti {
43
Andreas Gampe3c252f02016-10-27 18:25:17 -070044jvmtiError MethodUtil::GetMethodName(jvmtiEnv* env,
45 jmethodID method,
46 char** name_ptr,
47 char** signature_ptr,
48 char** generic_ptr) {
49 art::ScopedObjectAccess soa(art::Thread::Current());
Andreas Gampe13b27842016-11-07 16:48:23 -080050 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe3c252f02016-10-27 18:25:17 -070051 art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
52
53 JvmtiUniquePtr name_copy;
54 if (name_ptr != nullptr) {
55 const char* method_name = art_method->GetName();
56 if (method_name == nullptr) {
57 method_name = "<error>";
58 }
59 unsigned char* tmp;
60 jvmtiError ret = CopyString(env, method_name, &tmp);
61 if (ret != ERR(NONE)) {
62 return ret;
63 }
64 name_copy = MakeJvmtiUniquePtr(env, tmp);
65 *name_ptr = reinterpret_cast<char*>(tmp);
66 }
67
68 JvmtiUniquePtr signature_copy;
69 if (signature_ptr != nullptr) {
70 const art::Signature sig = art_method->GetSignature();
71 std::string str = sig.ToString();
72 unsigned char* tmp;
73 jvmtiError ret = CopyString(env, str.c_str(), &tmp);
74 if (ret != ERR(NONE)) {
75 return ret;
76 }
77 signature_copy = MakeJvmtiUniquePtr(env, tmp);
78 *signature_ptr = reinterpret_cast<char*>(tmp);
79 }
80
81 // TODO: Support generic signature.
Andreas Gampe862bdd82016-11-18 13:31:13 -080082 if (generic_ptr != nullptr) {
83 *generic_ptr = nullptr;
84 }
Andreas Gampe3c252f02016-10-27 18:25:17 -070085
86 // Everything is fine, release the buffers.
87 name_copy.release();
88 signature_copy.release();
89
90 return ERR(NONE);
91}
92
Andreas Gampe368a2082016-10-28 17:33:13 -070093jvmtiError MethodUtil::GetMethodDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
94 jmethodID method,
95 jclass* declaring_class_ptr) {
96 if (declaring_class_ptr == nullptr) {
97 return ERR(NULL_POINTER);
98 }
99
Andreas Gampe13b27842016-11-07 16:48:23 -0800100 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe368a2082016-10-28 17:33:13 -0700101 // Note: No GetInterfaceMethodIfProxy, we want to actual class.
102
Andreas Gampe13b27842016-11-07 16:48:23 -0800103 art::ScopedObjectAccess soa(art::Thread::Current());
Andreas Gampe368a2082016-10-28 17:33:13 -0700104 art::mirror::Class* klass = art_method->GetDeclaringClass();
105 *declaring_class_ptr = soa.AddLocalReference<jclass>(klass);
106
107 return ERR(NONE);
108}
109
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700110jvmtiError MethodUtil::GetMethodModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
111 jmethodID method,
112 jint* modifiers_ptr) {
113 if (modifiers_ptr == nullptr) {
114 return ERR(NULL_POINTER);
115 }
116
Andreas Gampe13b27842016-11-07 16:48:23 -0800117 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700118 uint32_t modifiers = art_method->GetAccessFlags();
119
120 // Note: Keep this code in sync with Executable.fixMethodFlags.
121 if ((modifiers & art::kAccAbstract) != 0) {
122 modifiers &= ~art::kAccNative;
123 }
124 modifiers &= ~art::kAccSynchronized;
125 if ((modifiers & art::kAccDeclaredSynchronized) != 0) {
126 modifiers |= art::kAccSynchronized;
127 }
128 modifiers &= art::kAccJavaFlagsMask;
129
130 *modifiers_ptr = modifiers;
131 return ERR(NONE);
132}
133
Andreas Gampeda3e5612016-12-13 19:00:53 -0800134using LineNumberContext = std::vector<jvmtiLineNumberEntry>;
135
136static bool CollectLineNumbers(void* void_context, const art::DexFile::PositionInfo& entry) {
137 LineNumberContext* context = reinterpret_cast<LineNumberContext*>(void_context);
138 jvmtiLineNumberEntry jvmti_entry = { static_cast<jlocation>(entry.address_),
139 static_cast<jint>(entry.line_) };
140 context->push_back(jvmti_entry);
141 return false; // Collect all, no early exit.
142}
143
144jvmtiError MethodUtil::GetLineNumberTable(jvmtiEnv* env,
145 jmethodID method,
146 jint* entry_count_ptr,
147 jvmtiLineNumberEntry** table_ptr) {
148 if (method == nullptr) {
149 return ERR(NULL_POINTER);
150 }
151 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
152 DCHECK(!art_method->IsRuntimeMethod());
153
154 const art::DexFile::CodeItem* code_item;
155 const art::DexFile* dex_file;
156 {
157 art::ScopedObjectAccess soa(art::Thread::Current());
158
159 if (art_method->IsProxyMethod()) {
160 return ERR(ABSENT_INFORMATION);
161 }
162 if (art_method->IsNative()) {
163 return ERR(NATIVE_METHOD);
164 }
165 if (entry_count_ptr == nullptr || table_ptr == nullptr) {
166 return ERR(NULL_POINTER);
167 }
168
169 code_item = art_method->GetCodeItem();
170 dex_file = art_method->GetDexFile();
171 DCHECK(code_item != nullptr) << art_method->PrettyMethod() << " " << dex_file->GetLocation();
172 }
173
174 LineNumberContext context;
175 bool success = dex_file->DecodeDebugPositionInfo(code_item, CollectLineNumbers, &context);
176 if (!success) {
177 return ERR(ABSENT_INFORMATION);
178 }
179
180 unsigned char* data;
181 jlong mem_size = context.size() * sizeof(jvmtiLineNumberEntry);
182 jvmtiError alloc_error = env->Allocate(mem_size, &data);
183 if (alloc_error != ERR(NONE)) {
184 return alloc_error;
185 }
186 *table_ptr = reinterpret_cast<jvmtiLineNumberEntry*>(data);
187 memcpy(*table_ptr, context.data(), mem_size);
188 *entry_count_ptr = static_cast<jint>(context.size());
189
190 return ERR(NONE);
191}
192
Andreas Gampe3c252f02016-10-27 18:25:17 -0700193} // namespace openjdkjvmti