blob: e8e62c2b40d106250ec85d24b53d270dfdba8095 [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
Alex Light0fa17862017-10-24 13:43:05 -070042#include "deopt_manager.h"
Andreas Gampe77708d92016-10-07 11:48:21 -070043#include "base/casts.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070044#include "base/logging.h"
45#include "base/macros.h"
Andreas Gampef45d61c2017-06-07 10:29:33 -070046#include "base/strlcpy.h"
Alex Lightb6106d52017-10-18 15:02:15 -070047#include "base/mutex.h"
Andreas Gampe77708d92016-10-07 11:48:21 -070048#include "events.h"
Andreas Gampedb6dcb62016-09-13 09:05:59 -070049#include "java_vm_ext.h"
50#include "jni_env_ext.h"
51#include "jvmti.h"
Alex Lighta26e3492017-06-27 17:55:37 -070052#include "ti_breakpoint.h"
Andreas Gampedb6dcb62016-09-13 09:05:59 -070053
Alex Light084fa372017-06-16 08:58:34 -070054namespace art {
55class ArtField;
Alex Lighta26e3492017-06-27 17:55:37 -070056class ArtMethod;
Alex Lighte814f9d2017-07-31 16:14:39 -070057class ShadowFrame;
Alex Lighta26e3492017-06-27 17:55:37 -070058} // namespace art
Alex Light084fa372017-06-16 08:58:34 -070059
Andreas Gampedb6dcb62016-09-13 09:05:59 -070060namespace openjdkjvmti {
61
Andreas Gampede19eb92017-02-24 16:21:18 -080062class ObjectTagTable;
Andreas Gampedb6dcb62016-09-13 09:05:59 -070063
64// A structure that is a jvmtiEnv with additional information for the runtime.
65struct ArtJvmTiEnv : public jvmtiEnv {
66 art::JavaVMExt* art_vm;
67 void* local_data;
Alex Lighte6574242016-08-17 09:56:24 -070068 jvmtiCapabilities capabilities;
Andreas Gampedb6dcb62016-09-13 09:05:59 -070069
Andreas Gampe77708d92016-10-07 11:48:21 -070070 EventMasks event_masks;
Alex Light8c2b9292017-11-09 13:21:01 -080071 std::unique_ptr<ArtJvmtiEventCallbacks> event_callbacks;
Andreas Gampe77708d92016-10-07 11:48:21 -070072
Andreas Gampede19eb92017-02-24 16:21:18 -080073 // Tagging is specific to the jvmtiEnv.
74 std::unique_ptr<ObjectTagTable> object_tag_table;
75
Alex Light084fa372017-06-16 08:58:34 -070076 // Set of watched fields is unique to each jvmtiEnv.
77 // TODO It might be good to follow the RI and only let one jvmtiEnv ever have the watch caps so
78 // we can record this on the field directly. We could do this either using free access-flag bits
79 // or by putting a list in the ClassExt of a field's DeclaringClass.
80 // TODO Maybe just have an extension to let one put a watch on every field, that would probably be
81 // good enough maybe since you probably want either a few or all/almost all of them.
Alex Lightb6106d52017-10-18 15:02:15 -070082 std::unordered_set<art::ArtField*> access_watched_fields GUARDED_BY(event_info_mutex_);
83 std::unordered_set<art::ArtField*> modify_watched_fields GUARDED_BY(event_info_mutex_);
Alex Light084fa372017-06-16 08:58:34 -070084
Alex Lighta26e3492017-06-27 17:55:37 -070085 // Set of breakpoints is unique to each jvmtiEnv.
Alex Lightb6106d52017-10-18 15:02:15 -070086 std::unordered_set<Breakpoint> breakpoints GUARDED_BY(event_info_mutex_);
87 std::unordered_set<const art::ShadowFrame*> notify_frames GUARDED_BY(event_info_mutex_);
88
89 // RW lock to protect access to all of the event data.
90 art::ReaderWriterMutex event_info_mutex_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Alex Lighta26e3492017-06-27 17:55:37 -070091
Andreas Gampede19eb92017-02-24 16:21:18 -080092 ArtJvmTiEnv(art::JavaVMExt* runtime, EventHandler* event_handler);
Andreas Gampe77708d92016-10-07 11:48:21 -070093
94 static ArtJvmTiEnv* AsArtJvmTiEnv(jvmtiEnv* env) {
95 return art::down_cast<ArtJvmTiEnv*>(env);
96 }
Alex Lightb284f8d2017-11-21 00:00:48 +000097
98 // Top level lock. Nothing can be held when we get this except for mutator lock for full
99 // thread-suspension.
100 static art::Mutex *gEnvMutex ACQUIRED_AFTER(art::Locks::mutator_lock_);
Andreas Gampedb6dcb62016-09-13 09:05:59 -0700101};
102
103// Macro and constexpr to make error values less annoying to write.
104#define ERR(e) JVMTI_ERROR_ ## e
105static constexpr jvmtiError OK = JVMTI_ERROR_NONE;
106
107// Special error code for unimplemented functions in JVMTI
108static constexpr jvmtiError ERR(NOT_IMPLEMENTED) = JVMTI_ERROR_NOT_AVAILABLE;
109
110static inline JNIEnv* GetJniEnv(jvmtiEnv* env) {
111 JNIEnv* ret_value = nullptr;
112 jint res = reinterpret_cast<ArtJvmTiEnv*>(env)->art_vm->GetEnv(
113 reinterpret_cast<void**>(&ret_value), JNI_VERSION_1_1);
114 if (res != JNI_OK) {
115 return nullptr;
116 }
117 return ret_value;
118}
119
Andreas Gampe54711412017-02-21 12:41:43 -0800120template <typename T>
Andreas Gampe3c252f02016-10-27 18:25:17 -0700121class JvmtiDeleter {
122 public:
123 JvmtiDeleter() : env_(nullptr) {}
124 explicit JvmtiDeleter(jvmtiEnv* env) : env_(env) {}
125
126 JvmtiDeleter(JvmtiDeleter&) = default;
127 JvmtiDeleter(JvmtiDeleter&&) = default;
128 JvmtiDeleter& operator=(const JvmtiDeleter&) = default;
129
Andreas Gampe54711412017-02-21 12:41:43 -0800130 void operator()(T* ptr) const {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700131 CHECK(env_ != nullptr);
Andreas Gampe54711412017-02-21 12:41:43 -0800132 jvmtiError ret = env_->Deallocate(reinterpret_cast<unsigned char*>(ptr));
Andreas Gampe3c252f02016-10-27 18:25:17 -0700133 CHECK(ret == ERR(NONE));
134 }
135
136 private:
137 mutable jvmtiEnv* env_;
138};
139
Andreas Gampe54711412017-02-21 12:41:43 -0800140template <typename T>
141class JvmtiDeleter<T[]> {
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800142 public:
Andreas Gampe54711412017-02-21 12:41:43 -0800143 JvmtiDeleter() : env_(nullptr) {}
144 explicit JvmtiDeleter(jvmtiEnv* env) : env_(env) {}
145
146 JvmtiDeleter(JvmtiDeleter&) = default;
147 JvmtiDeleter(JvmtiDeleter&&) = default;
148 JvmtiDeleter& operator=(const JvmtiDeleter&) = default;
149
150 template <typename U>
151 void operator()(U* ptr) const {
152 CHECK(env_ != nullptr);
153 jvmtiError ret = env_->Deallocate(reinterpret_cast<unsigned char*>(ptr));
154 CHECK(ret == ERR(NONE));
155 }
156
157 private:
158 mutable jvmtiEnv* env_;
159};
160
161template <typename T>
162using JvmtiUniquePtr = std::unique_ptr<T, JvmtiDeleter<T>>;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700163
Andreas Gampe1bdaf732017-01-09 19:21:06 -0800164template <typename T>
Andreas Gampe3c252f02016-10-27 18:25:17 -0700165ALWAYS_INLINE
Andreas Gampe54711412017-02-21 12:41:43 -0800166static inline JvmtiUniquePtr<T> MakeJvmtiUniquePtr(jvmtiEnv* env, T* mem) {
167 return JvmtiUniquePtr<T>(mem, JvmtiDeleter<T>(env));
168}
169
170template <typename T>
171ALWAYS_INLINE
172static inline JvmtiUniquePtr<T> MakeJvmtiUniquePtr(jvmtiEnv* env, unsigned char* mem) {
173 return JvmtiUniquePtr<T>(reinterpret_cast<T*>(mem), JvmtiDeleter<T>(env));
174}
175
176template <typename T>
177ALWAYS_INLINE
178static inline JvmtiUniquePtr<T> AllocJvmtiUniquePtr(jvmtiEnv* env, jvmtiError* error) {
179 unsigned char* tmp;
180 *error = env->Allocate(sizeof(T), &tmp);
181 if (*error != ERR(NONE)) {
182 return JvmtiUniquePtr<T>();
183 }
184 return JvmtiUniquePtr<T>(tmp, JvmtiDeleter<T>(env));
185}
186
187template <typename T>
188ALWAYS_INLINE
189static inline JvmtiUniquePtr<T> AllocJvmtiUniquePtr(jvmtiEnv* env,
190 size_t count,
191 jvmtiError* error) {
192 unsigned char* tmp;
193 *error = env->Allocate(sizeof(typename std::remove_extent<T>::type) * count, &tmp);
194 if (*error != ERR(NONE)) {
195 return JvmtiUniquePtr<T>();
196 }
197 return JvmtiUniquePtr<T>(reinterpret_cast<typename std::remove_extent<T>::type*>(tmp),
198 JvmtiDeleter<T>(env));
Andreas Gampe3c252f02016-10-27 18:25:17 -0700199}
200
Andreas Gampee492ae32016-10-28 19:34:57 -0700201ALWAYS_INLINE
Alex Light440b5d92017-01-24 15:32:25 -0800202static inline jvmtiError CopyDataIntoJvmtiBuffer(ArtJvmTiEnv* env,
203 const unsigned char* source,
204 jint len,
205 /*out*/unsigned char** dest) {
206 jvmtiError res = env->Allocate(len, dest);
207 if (res != OK) {
208 return res;
209 }
210 memcpy(reinterpret_cast<void*>(*dest),
211 reinterpret_cast<const void*>(source),
212 len);
213 return OK;
214}
215
216ALWAYS_INLINE
Andreas Gampe54711412017-02-21 12:41:43 -0800217static inline JvmtiUniquePtr<char[]> CopyString(jvmtiEnv* env, const char* src, jvmtiError* error) {
Alex Lightce68cc62017-07-26 10:30:38 -0700218 if (src == nullptr) {
219 JvmtiUniquePtr<char[]> ret = AllocJvmtiUniquePtr<char[]>(env, 0, error);
220 return ret;
221 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700222 size_t len = strlen(src) + 1;
Andreas Gampe54711412017-02-21 12:41:43 -0800223 JvmtiUniquePtr<char[]> ret = AllocJvmtiUniquePtr<char[]>(env, len, error);
224 if (ret != nullptr) {
Andreas Gampef45d61c2017-06-07 10:29:33 -0700225 strlcpy(ret.get(), src, len);
Andreas Gampee492ae32016-10-28 19:34:57 -0700226 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700227 return ret;
228}
229
Alex Lighte6574242016-08-17 09:56:24 -0700230const jvmtiCapabilities kPotentialCapabilities = {
231 .can_tag_objects = 1,
Alex Light084fa372017-06-16 08:58:34 -0700232 .can_generate_field_modification_events = 1,
233 .can_generate_field_access_events = 1,
Alex Light4c174282017-07-05 10:18:18 -0700234 .can_get_bytecodes = 1,
Alex Light9db679d2017-01-25 15:28:04 -0800235 .can_get_synthetic_attribute = 1,
Alex Light88e1ddd2017-08-21 13:09:55 -0700236 .can_get_owned_monitor_info = 1,
Alex Light41006c62017-09-14 09:51:14 -0700237 .can_get_current_contended_monitor = 1,
Alex Lightce568642017-09-05 16:54:25 -0700238 .can_get_monitor_info = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700239 .can_pop_frame = 0,
Alex Light6ac57502017-01-19 15:05:06 -0800240 .can_redefine_classes = 1,
Alex Light54d39dc2017-09-25 17:00:16 -0700241 .can_signal_thread = 1,
Alex Light6fa7b812017-06-16 09:04:29 -0700242 .can_get_source_file_name = 1,
Alex Light9db679d2017-01-25 15:28:04 -0800243 .can_get_line_numbers = 1,
Alex Light6fa7b812017-06-16 09:04:29 -0700244 .can_get_source_debug_extension = 1,
Alex Lightce68cc62017-07-26 10:30:38 -0700245 .can_access_local_variables = 1,
Alex Lightb566fed2017-07-28 15:17:00 -0700246 .can_maintain_original_method_order = 1,
Alex Lighta26e3492017-06-27 17:55:37 -0700247 .can_generate_single_step_events = 1,
Alex Light9fb1ab12017-09-05 09:32:49 -0700248 .can_generate_exception_events = 1,
Alex Lighte814f9d2017-07-31 16:14:39 -0700249 .can_generate_frame_pop_events = 1,
Alex Lighta26e3492017-06-27 17:55:37 -0700250 .can_generate_breakpoint_events = 1,
Alex Light88fd7202017-06-30 08:31:59 -0700251 .can_suspend = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700252 .can_redefine_any_class = 0,
253 .can_get_current_thread_cpu_time = 0,
254 .can_get_thread_cpu_time = 0,
Alex Lightb7edcda2017-04-27 13:20:31 -0700255 .can_generate_method_entry_events = 1,
256 .can_generate_method_exit_events = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700257 .can_generate_all_class_hook_events = 0,
258 .can_generate_compiled_method_load_events = 0,
Alex Light77fee872017-09-05 14:51:49 -0700259 .can_generate_monitor_events = 1,
Alex Light9db679d2017-01-25 15:28:04 -0800260 .can_generate_vm_object_alloc_events = 1,
Alex Lightd78ddec2017-04-18 15:20:38 -0700261 .can_generate_native_method_bind_events = 1,
Alex Light9db679d2017-01-25 15:28:04 -0800262 .can_generate_garbage_collection_events = 1,
263 .can_generate_object_free_events = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700264 .can_force_early_return = 0,
Alex Light88e1ddd2017-08-21 13:09:55 -0700265 .can_get_owned_monitor_stack_depth_info = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700266 .can_get_constant_pool = 0,
267 .can_set_native_method_prefix = 0,
Alex Light6ac57502017-01-19 15:05:06 -0800268 .can_retransform_classes = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700269 .can_retransform_any_class = 0,
270 .can_generate_resource_exhaustion_heap_events = 0,
271 .can_generate_resource_exhaustion_threads_events = 0,
272};
273
Andreas Gampedb6dcb62016-09-13 09:05:59 -0700274} // namespace openjdkjvmti
275
Andreas Gampe06c42a52017-07-26 14:17:14 -0700276#endif // ART_OPENJDKJVMTI_ART_JVMTI_H_