blob: 4aae4d1d4902472c2999afd58c1a8260847c7cc3 [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"
37#include "scoped_thread_state_change-inl.h"
38
39namespace openjdkjvmti {
40
41static jvmtiError CopyString(jvmtiEnv* env, const char* src, unsigned char** copy) {
42 size_t len = strlen(src) + 1;
43 unsigned char* buf;
44 jvmtiError ret = env->Allocate(len, &buf);
45 if (ret != ERR(NONE)) {
46 return ret;
47 }
48 strcpy(reinterpret_cast<char*>(buf), src);
49 *copy = buf;
50 return ret;
51}
52
53jvmtiError MethodUtil::GetMethodName(jvmtiEnv* env,
54 jmethodID method,
55 char** name_ptr,
56 char** signature_ptr,
57 char** generic_ptr) {
58 art::ScopedObjectAccess soa(art::Thread::Current());
59 art::ArtMethod* art_method = soa.DecodeMethod(method);
60 art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
61
62 JvmtiUniquePtr name_copy;
63 if (name_ptr != nullptr) {
64 const char* method_name = art_method->GetName();
65 if (method_name == nullptr) {
66 method_name = "<error>";
67 }
68 unsigned char* tmp;
69 jvmtiError ret = CopyString(env, method_name, &tmp);
70 if (ret != ERR(NONE)) {
71 return ret;
72 }
73 name_copy = MakeJvmtiUniquePtr(env, tmp);
74 *name_ptr = reinterpret_cast<char*>(tmp);
75 }
76
77 JvmtiUniquePtr signature_copy;
78 if (signature_ptr != nullptr) {
79 const art::Signature sig = art_method->GetSignature();
80 std::string str = sig.ToString();
81 unsigned char* tmp;
82 jvmtiError ret = CopyString(env, str.c_str(), &tmp);
83 if (ret != ERR(NONE)) {
84 return ret;
85 }
86 signature_copy = MakeJvmtiUniquePtr(env, tmp);
87 *signature_ptr = reinterpret_cast<char*>(tmp);
88 }
89
90 // TODO: Support generic signature.
91 *generic_ptr = nullptr;
92
93 // Everything is fine, release the buffers.
94 name_copy.release();
95 signature_copy.release();
96
97 return ERR(NONE);
98}
99
Andreas Gampe368a2082016-10-28 17:33:13 -0700100jvmtiError MethodUtil::GetMethodDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
101 jmethodID method,
102 jclass* declaring_class_ptr) {
103 if (declaring_class_ptr == nullptr) {
104 return ERR(NULL_POINTER);
105 }
106
107 art::ScopedObjectAccess soa(art::Thread::Current());
108 art::ArtMethod* art_method = soa.DecodeMethod(method);
109 // Note: No GetInterfaceMethodIfProxy, we want to actual class.
110
111 art::mirror::Class* klass = art_method->GetDeclaringClass();
112 *declaring_class_ptr = soa.AddLocalReference<jclass>(klass);
113
114 return ERR(NONE);
115}
116
Andreas Gampe3c252f02016-10-27 18:25:17 -0700117} // namespace openjdkjvmti