blob: 7b82e7c0751e118c2407321a99f3c6a25cbd2315 [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 Gampe36bcd4f2016-10-28 18:07:18 -070037#include "modifiers.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070038#include "scoped_thread_state_change-inl.h"
39
40namespace openjdkjvmti {
41
42static jvmtiError CopyString(jvmtiEnv* env, const char* src, unsigned char** copy) {
43 size_t len = strlen(src) + 1;
44 unsigned char* buf;
45 jvmtiError ret = env->Allocate(len, &buf);
46 if (ret != ERR(NONE)) {
47 return ret;
48 }
49 strcpy(reinterpret_cast<char*>(buf), src);
50 *copy = buf;
51 return ret;
52}
53
54jvmtiError MethodUtil::GetMethodName(jvmtiEnv* env,
55 jmethodID method,
56 char** name_ptr,
57 char** signature_ptr,
58 char** generic_ptr) {
59 art::ScopedObjectAccess soa(art::Thread::Current());
60 art::ArtMethod* art_method = soa.DecodeMethod(method);
61 art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
62
63 JvmtiUniquePtr name_copy;
64 if (name_ptr != nullptr) {
65 const char* method_name = art_method->GetName();
66 if (method_name == nullptr) {
67 method_name = "<error>";
68 }
69 unsigned char* tmp;
70 jvmtiError ret = CopyString(env, method_name, &tmp);
71 if (ret != ERR(NONE)) {
72 return ret;
73 }
74 name_copy = MakeJvmtiUniquePtr(env, tmp);
75 *name_ptr = reinterpret_cast<char*>(tmp);
76 }
77
78 JvmtiUniquePtr signature_copy;
79 if (signature_ptr != nullptr) {
80 const art::Signature sig = art_method->GetSignature();
81 std::string str = sig.ToString();
82 unsigned char* tmp;
83 jvmtiError ret = CopyString(env, str.c_str(), &tmp);
84 if (ret != ERR(NONE)) {
85 return ret;
86 }
87 signature_copy = MakeJvmtiUniquePtr(env, tmp);
88 *signature_ptr = reinterpret_cast<char*>(tmp);
89 }
90
91 // TODO: Support generic signature.
92 *generic_ptr = nullptr;
93
94 // Everything is fine, release the buffers.
95 name_copy.release();
96 signature_copy.release();
97
98 return ERR(NONE);
99}
100
Andreas Gampe368a2082016-10-28 17:33:13 -0700101jvmtiError MethodUtil::GetMethodDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
102 jmethodID method,
103 jclass* declaring_class_ptr) {
104 if (declaring_class_ptr == nullptr) {
105 return ERR(NULL_POINTER);
106 }
107
108 art::ScopedObjectAccess soa(art::Thread::Current());
109 art::ArtMethod* art_method = soa.DecodeMethod(method);
110 // Note: No GetInterfaceMethodIfProxy, we want to actual class.
111
112 art::mirror::Class* klass = art_method->GetDeclaringClass();
113 *declaring_class_ptr = soa.AddLocalReference<jclass>(klass);
114
115 return ERR(NONE);
116}
117
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700118jvmtiError MethodUtil::GetMethodModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
119 jmethodID method,
120 jint* modifiers_ptr) {
121 if (modifiers_ptr == nullptr) {
122 return ERR(NULL_POINTER);
123 }
124
125 art::ScopedObjectAccess soa(art::Thread::Current());
126 art::ArtMethod* art_method = soa.DecodeMethod(method);
127
128 uint32_t modifiers = art_method->GetAccessFlags();
129
130 // Note: Keep this code in sync with Executable.fixMethodFlags.
131 if ((modifiers & art::kAccAbstract) != 0) {
132 modifiers &= ~art::kAccNative;
133 }
134 modifiers &= ~art::kAccSynchronized;
135 if ((modifiers & art::kAccDeclaredSynchronized) != 0) {
136 modifiers |= art::kAccSynchronized;
137 }
138 modifiers &= art::kAccJavaFlagsMask;
139
140 *modifiers_ptr = modifiers;
141 return ERR(NONE);
142}
143
Andreas Gampe3c252f02016-10-27 18:25:17 -0700144} // namespace openjdkjvmti