blob: d74e25b80762629bbc53717d549c4ac1fa02c966 [file] [log] [blame]
Andreas Gampedb6dcb62016-09-13 09:05:59 -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
Andreas Gampe06c42a52017-07-26 14:17:14 -070032#ifndef ART_OPENJDKJVMTI_ART_JVMTI_H_
33#define ART_OPENJDKJVMTI_ART_JVMTI_H_
Andreas Gampedb6dcb62016-09-13 09:05:59 -070034
Andreas Gampe77708d92016-10-07 11:48:21 -070035#include <memory>
Andreas Gampe54711412017-02-21 12:41:43 -080036#include <type_traits>
Alex Lighta26e3492017-06-27 17:55:37 -070037#include <unordered_map>
Alex Light084fa372017-06-16 08:58:34 -070038#include <unordered_set>
Andreas Gampe77708d92016-10-07 11:48:21 -070039
Andreas Gampedb6dcb62016-09-13 09:05:59 -070040#include <jni.h>
41
Andreas Gampe77708d92016-10-07 11:48:21 -070042#include "base/casts.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070043#include "base/logging.h"
44#include "base/macros.h"
Andreas Gampef45d61c2017-06-07 10:29:33 -070045#include "base/strlcpy.h"
Andreas Gampe77708d92016-10-07 11:48:21 -070046#include "events.h"
Andreas Gampedb6dcb62016-09-13 09:05:59 -070047#include "java_vm_ext.h"
48#include "jni_env_ext.h"
49#include "jvmti.h"
Alex Lighta26e3492017-06-27 17:55:37 -070050#include "ti_breakpoint.h"
Andreas Gampedb6dcb62016-09-13 09:05:59 -070051
Alex Light084fa372017-06-16 08:58:34 -070052namespace art {
53class ArtField;
Alex Lighta26e3492017-06-27 17:55:37 -070054class ArtMethod;
Alex Lighte814f9d2017-07-31 16:14:39 -070055class ShadowFrame;
Alex Lighta26e3492017-06-27 17:55:37 -070056} // namespace art
Alex Light084fa372017-06-16 08:58:34 -070057
Andreas Gampedb6dcb62016-09-13 09:05:59 -070058namespace openjdkjvmti {
59
Andreas Gampede19eb92017-02-24 16:21:18 -080060class ObjectTagTable;
Andreas Gampedb6dcb62016-09-13 09:05:59 -070061
62// A structure that is a jvmtiEnv with additional information for the runtime.
63struct ArtJvmTiEnv : public jvmtiEnv {
64 art::JavaVMExt* art_vm;
65 void* local_data;
Alex Lighte6574242016-08-17 09:56:24 -070066 jvmtiCapabilities capabilities;
Andreas Gampedb6dcb62016-09-13 09:05:59 -070067
Andreas Gampe77708d92016-10-07 11:48:21 -070068 EventMasks event_masks;
69 std::unique_ptr<jvmtiEventCallbacks> event_callbacks;
70
Andreas Gampede19eb92017-02-24 16:21:18 -080071 // Tagging is specific to the jvmtiEnv.
72 std::unique_ptr<ObjectTagTable> object_tag_table;
73
Alex Light084fa372017-06-16 08:58:34 -070074 // Set of watched fields is unique to each jvmtiEnv.
75 // TODO It might be good to follow the RI and only let one jvmtiEnv ever have the watch caps so
76 // we can record this on the field directly. We could do this either using free access-flag bits
77 // or by putting a list in the ClassExt of a field's DeclaringClass.
78 // TODO Maybe just have an extension to let one put a watch on every field, that would probably be
79 // good enough maybe since you probably want either a few or all/almost all of them.
80 std::unordered_set<art::ArtField*> access_watched_fields;
81 std::unordered_set<art::ArtField*> modify_watched_fields;
82
Alex Lighta26e3492017-06-27 17:55:37 -070083 // Set of breakpoints is unique to each jvmtiEnv.
84 std::unordered_set<Breakpoint> breakpoints;
Alex Lighte814f9d2017-07-31 16:14:39 -070085 std::unordered_set<const art::ShadowFrame*> notify_frames;
Alex Lighta26e3492017-06-27 17:55:37 -070086
Andreas Gampede19eb92017-02-24 16:21:18 -080087 ArtJvmTiEnv(art::JavaVMExt* runtime, EventHandler* event_handler);
Andreas Gampe77708d92016-10-07 11:48:21 -070088
89 static ArtJvmTiEnv* AsArtJvmTiEnv(jvmtiEnv* env) {
90 return art::down_cast<ArtJvmTiEnv*>(env);
91 }
Andreas Gampedb6dcb62016-09-13 09:05:59 -070092};
93
94// Macro and constexpr to make error values less annoying to write.
95#define ERR(e) JVMTI_ERROR_ ## e
96static constexpr jvmtiError OK = JVMTI_ERROR_NONE;
97
98// Special error code for unimplemented functions in JVMTI
99static constexpr jvmtiError ERR(NOT_IMPLEMENTED) = JVMTI_ERROR_NOT_AVAILABLE;
100
101static inline JNIEnv* GetJniEnv(jvmtiEnv* env) {
102 JNIEnv* ret_value = nullptr;
103 jint res = reinterpret_cast<ArtJvmTiEnv*>(env)->art_vm->GetEnv(
104 reinterpret_cast<void**>(&ret_value), JNI_VERSION_1_1);
105 if (res != JNI_OK) {
106 return nullptr;
107 }
108 return ret_value;
109}
110
Andreas Gampe54711412017-02-21 12:41:43 -0800111template <typename T>
Andreas Gampe3c252f02016-10-27 18:25:17 -0700112class JvmtiDeleter {
113 public:
114 JvmtiDeleter() : env_(nullptr) {}
115 explicit JvmtiDeleter(jvmtiEnv* env) : env_(env) {}
116
117 JvmtiDeleter(JvmtiDeleter&) = default;
118 JvmtiDeleter(JvmtiDeleter&&) = default;
119 JvmtiDeleter& operator=(const JvmtiDeleter&) = default;
120
Andreas Gampe54711412017-02-21 12:41:43 -0800121 void operator()(T* ptr) const {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700122 CHECK(env_ != nullptr);
Andreas Gampe54711412017-02-21 12:41:43 -0800123 jvmtiError ret = env_->Deallocate(reinterpret_cast<unsigned char*>(ptr));
Andreas Gampe3c252f02016-10-27 18:25:17 -0700124 CHECK(ret == ERR(NONE));
125 }
126
127 private:
128 mutable jvmtiEnv* env_;
129};
130
Andreas Gampe54711412017-02-21 12:41:43 -0800131template <typename T>
132class JvmtiDeleter<T[]> {
133 public:
134 JvmtiDeleter() : env_(nullptr) {}
135 explicit JvmtiDeleter(jvmtiEnv* env) : env_(env) {}
136
137 JvmtiDeleter(JvmtiDeleter&) = default;
138 JvmtiDeleter(JvmtiDeleter&&) = default;
139 JvmtiDeleter& operator=(const JvmtiDeleter&) = default;
140
141 template <typename U>
142 void operator()(U* ptr) const {
143 CHECK(env_ != nullptr);
144 jvmtiError ret = env_->Deallocate(reinterpret_cast<unsigned char*>(ptr));
145 CHECK(ret == ERR(NONE));
146 }
147
148 private:
149 mutable jvmtiEnv* env_;
150};
151
152template <typename T>
153using JvmtiUniquePtr = std::unique_ptr<T, JvmtiDeleter<T>>;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700154
Andreas Gampe1bdaf732017-01-09 19:21:06 -0800155template <typename T>
Andreas Gampe3c252f02016-10-27 18:25:17 -0700156ALWAYS_INLINE
Andreas Gampe54711412017-02-21 12:41:43 -0800157static inline JvmtiUniquePtr<T> MakeJvmtiUniquePtr(jvmtiEnv* env, T* mem) {
158 return JvmtiUniquePtr<T>(mem, JvmtiDeleter<T>(env));
159}
160
161template <typename T>
162ALWAYS_INLINE
163static inline JvmtiUniquePtr<T> MakeJvmtiUniquePtr(jvmtiEnv* env, unsigned char* mem) {
164 return JvmtiUniquePtr<T>(reinterpret_cast<T*>(mem), JvmtiDeleter<T>(env));
165}
166
167template <typename T>
168ALWAYS_INLINE
169static inline JvmtiUniquePtr<T> AllocJvmtiUniquePtr(jvmtiEnv* env, jvmtiError* error) {
170 unsigned char* tmp;
171 *error = env->Allocate(sizeof(T), &tmp);
172 if (*error != ERR(NONE)) {
173 return JvmtiUniquePtr<T>();
174 }
175 return JvmtiUniquePtr<T>(tmp, JvmtiDeleter<T>(env));
176}
177
178template <typename T>
179ALWAYS_INLINE
180static inline JvmtiUniquePtr<T> AllocJvmtiUniquePtr(jvmtiEnv* env,
181 size_t count,
182 jvmtiError* error) {
183 unsigned char* tmp;
184 *error = env->Allocate(sizeof(typename std::remove_extent<T>::type) * count, &tmp);
185 if (*error != ERR(NONE)) {
186 return JvmtiUniquePtr<T>();
187 }
188 return JvmtiUniquePtr<T>(reinterpret_cast<typename std::remove_extent<T>::type*>(tmp),
189 JvmtiDeleter<T>(env));
Andreas Gampe3c252f02016-10-27 18:25:17 -0700190}
191
Andreas Gampee492ae32016-10-28 19:34:57 -0700192ALWAYS_INLINE
Alex Light440b5d92017-01-24 15:32:25 -0800193static inline jvmtiError CopyDataIntoJvmtiBuffer(ArtJvmTiEnv* env,
194 const unsigned char* source,
195 jint len,
196 /*out*/unsigned char** dest) {
197 jvmtiError res = env->Allocate(len, dest);
198 if (res != OK) {
199 return res;
200 }
201 memcpy(reinterpret_cast<void*>(*dest),
202 reinterpret_cast<const void*>(source),
203 len);
204 return OK;
205}
206
207ALWAYS_INLINE
Andreas Gampe54711412017-02-21 12:41:43 -0800208static inline JvmtiUniquePtr<char[]> CopyString(jvmtiEnv* env, const char* src, jvmtiError* error) {
Alex Lightce68cc62017-07-26 10:30:38 -0700209 if (src == nullptr) {
210 JvmtiUniquePtr<char[]> ret = AllocJvmtiUniquePtr<char[]>(env, 0, error);
211 return ret;
212 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700213 size_t len = strlen(src) + 1;
Andreas Gampe54711412017-02-21 12:41:43 -0800214 JvmtiUniquePtr<char[]> ret = AllocJvmtiUniquePtr<char[]>(env, len, error);
215 if (ret != nullptr) {
Andreas Gampef45d61c2017-06-07 10:29:33 -0700216 strlcpy(ret.get(), src, len);
Andreas Gampee492ae32016-10-28 19:34:57 -0700217 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700218 return ret;
219}
220
Alex Lighte6574242016-08-17 09:56:24 -0700221const jvmtiCapabilities kPotentialCapabilities = {
222 .can_tag_objects = 1,
Alex Light084fa372017-06-16 08:58:34 -0700223 .can_generate_field_modification_events = 1,
224 .can_generate_field_access_events = 1,
Alex Light4c174282017-07-05 10:18:18 -0700225 .can_get_bytecodes = 1,
Alex Light9db679d2017-01-25 15:28:04 -0800226 .can_get_synthetic_attribute = 1,
Alex Light88e1ddd2017-08-21 13:09:55 -0700227 .can_get_owned_monitor_info = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700228 .can_get_current_contended_monitor = 0,
229 .can_get_monitor_info = 0,
230 .can_pop_frame = 0,
Alex Light6ac57502017-01-19 15:05:06 -0800231 .can_redefine_classes = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700232 .can_signal_thread = 0,
Alex Light6fa7b812017-06-16 09:04:29 -0700233 .can_get_source_file_name = 1,
Alex Light9db679d2017-01-25 15:28:04 -0800234 .can_get_line_numbers = 1,
Alex Light6fa7b812017-06-16 09:04:29 -0700235 .can_get_source_debug_extension = 1,
Alex Lightce68cc62017-07-26 10:30:38 -0700236 .can_access_local_variables = 1,
Alex Lightb566fed2017-07-28 15:17:00 -0700237 .can_maintain_original_method_order = 1,
Alex Lighta26e3492017-06-27 17:55:37 -0700238 .can_generate_single_step_events = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700239 .can_generate_exception_events = 0,
Alex Lighte814f9d2017-07-31 16:14:39 -0700240 .can_generate_frame_pop_events = 1,
Alex Lighta26e3492017-06-27 17:55:37 -0700241 .can_generate_breakpoint_events = 1,
Alex Light88fd7202017-06-30 08:31:59 -0700242 .can_suspend = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700243 .can_redefine_any_class = 0,
244 .can_get_current_thread_cpu_time = 0,
245 .can_get_thread_cpu_time = 0,
Alex Lightb7edcda2017-04-27 13:20:31 -0700246 .can_generate_method_entry_events = 1,
247 .can_generate_method_exit_events = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700248 .can_generate_all_class_hook_events = 0,
249 .can_generate_compiled_method_load_events = 0,
250 .can_generate_monitor_events = 0,
Alex Light9db679d2017-01-25 15:28:04 -0800251 .can_generate_vm_object_alloc_events = 1,
Alex Lightd78ddec2017-04-18 15:20:38 -0700252 .can_generate_native_method_bind_events = 1,
Alex Light9db679d2017-01-25 15:28:04 -0800253 .can_generate_garbage_collection_events = 1,
254 .can_generate_object_free_events = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700255 .can_force_early_return = 0,
Alex Light88e1ddd2017-08-21 13:09:55 -0700256 .can_get_owned_monitor_stack_depth_info = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700257 .can_get_constant_pool = 0,
258 .can_set_native_method_prefix = 0,
Alex Light6ac57502017-01-19 15:05:06 -0800259 .can_retransform_classes = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700260 .can_retransform_any_class = 0,
261 .can_generate_resource_exhaustion_heap_events = 0,
262 .can_generate_resource_exhaustion_threads_events = 0,
263};
264
Andreas Gampedb6dcb62016-09-13 09:05:59 -0700265} // namespace openjdkjvmti
266
Andreas Gampe06c42a52017-07-26 14:17:14 -0700267#endif // ART_OPENJDKJVMTI_ART_JVMTI_H_