blob: 7433e54eda5f38d3f29b91e9604ccb297dc5f1c3 [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 Gampe57943812017-12-06 21:39:13 -080042#include <android-base/logging.h>
43
Alex Light0fa17862017-10-24 13:43:05 -070044#include "deopt_manager.h"
Andreas Gampe77708d92016-10-07 11:48:21 -070045#include "base/casts.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070046#include "base/macros.h"
Andreas Gampef45d61c2017-06-07 10:29:33 -070047#include "base/strlcpy.h"
Alex Lightb6106d52017-10-18 15:02:15 -070048#include "base/mutex.h"
Andreas Gampe77708d92016-10-07 11:48:21 -070049#include "events.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010050#include "jni/java_vm_ext.h"
51#include "jni/jni_env_ext.h"
Andreas Gampedb6dcb62016-09-13 09:05:59 -070052#include "jvmti.h"
Alex Lighta26e3492017-06-27 17:55:37 -070053#include "ti_breakpoint.h"
Andreas Gampedb6dcb62016-09-13 09:05:59 -070054
Alex Light084fa372017-06-16 08:58:34 -070055namespace art {
56class ArtField;
Alex Lighta26e3492017-06-27 17:55:37 -070057class ArtMethod;
Alex Lighte814f9d2017-07-31 16:14:39 -070058class ShadowFrame;
Alex Lighta26e3492017-06-27 17:55:37 -070059} // namespace art
Alex Light084fa372017-06-16 08:58:34 -070060
Andreas Gampedb6dcb62016-09-13 09:05:59 -070061namespace openjdkjvmti {
62
Andreas Gampede19eb92017-02-24 16:21:18 -080063class ObjectTagTable;
Andreas Gampedb6dcb62016-09-13 09:05:59 -070064
Alex Light2ce6fc82017-12-18 16:42:36 -080065// A special version that we use to identify special tooling interface versions which mostly matches
66// the jvmti spec but everything is best effort. This is used to implement the userdebug
67// 'debug-anything' behavior.
68//
69// This is the value 0x70010200.
70static constexpr jint kArtTiVersion = JVMTI_VERSION_1_2 | 0x40000000;
71
Andreas Gampedb6dcb62016-09-13 09:05:59 -070072// A structure that is a jvmtiEnv with additional information for the runtime.
73struct ArtJvmTiEnv : public jvmtiEnv {
74 art::JavaVMExt* art_vm;
75 void* local_data;
Alex Light2ce6fc82017-12-18 16:42:36 -080076
77 // The ti_version we are compatible with. This is only for giving the correct value for GetVersion
78 // when running on a userdebug/eng device.
79 jint ti_version;
80
Alex Lighte6574242016-08-17 09:56:24 -070081 jvmtiCapabilities capabilities;
Andreas Gampedb6dcb62016-09-13 09:05:59 -070082
Andreas Gampe77708d92016-10-07 11:48:21 -070083 EventMasks event_masks;
Alex Light8c2b9292017-11-09 13:21:01 -080084 std::unique_ptr<ArtJvmtiEventCallbacks> event_callbacks;
Andreas Gampe77708d92016-10-07 11:48:21 -070085
Andreas Gampede19eb92017-02-24 16:21:18 -080086 // Tagging is specific to the jvmtiEnv.
87 std::unique_ptr<ObjectTagTable> object_tag_table;
88
Alex Light084fa372017-06-16 08:58:34 -070089 // Set of watched fields is unique to each jvmtiEnv.
90 // TODO It might be good to follow the RI and only let one jvmtiEnv ever have the watch caps so
91 // we can record this on the field directly. We could do this either using free access-flag bits
92 // or by putting a list in the ClassExt of a field's DeclaringClass.
93 // TODO Maybe just have an extension to let one put a watch on every field, that would probably be
94 // good enough maybe since you probably want either a few or all/almost all of them.
Alex Lightb6106d52017-10-18 15:02:15 -070095 std::unordered_set<art::ArtField*> access_watched_fields GUARDED_BY(event_info_mutex_);
96 std::unordered_set<art::ArtField*> modify_watched_fields GUARDED_BY(event_info_mutex_);
Alex Light084fa372017-06-16 08:58:34 -070097
Alex Lighta26e3492017-06-27 17:55:37 -070098 // Set of breakpoints is unique to each jvmtiEnv.
Alex Lightb6106d52017-10-18 15:02:15 -070099 std::unordered_set<Breakpoint> breakpoints GUARDED_BY(event_info_mutex_);
100 std::unordered_set<const art::ShadowFrame*> notify_frames GUARDED_BY(event_info_mutex_);
101
102 // RW lock to protect access to all of the event data.
103 art::ReaderWriterMutex event_info_mutex_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Alex Lighta26e3492017-06-27 17:55:37 -0700104
Alex Lightae45cbb2018-10-18 15:49:56 -0700105 std::string last_error_ GUARDED_BY(last_error_mutex_);
106 // Lock to touch the last-error-message.
107 art::Mutex last_error_mutex_ BOTTOM_MUTEX_ACQUIRED_AFTER;
108
Alex Light2ce6fc82017-12-18 16:42:36 -0800109 ArtJvmTiEnv(art::JavaVMExt* runtime, EventHandler* event_handler, jint ti_version);
Andreas Gampe77708d92016-10-07 11:48:21 -0700110
111 static ArtJvmTiEnv* AsArtJvmTiEnv(jvmtiEnv* env) {
112 return art::down_cast<ArtJvmTiEnv*>(env);
113 }
Alex Lightb284f8d2017-11-21 00:00:48 +0000114
115 // Top level lock. Nothing can be held when we get this except for mutator lock for full
116 // thread-suspension.
117 static art::Mutex *gEnvMutex ACQUIRED_AFTER(art::Locks::mutator_lock_);
Andreas Gampedb6dcb62016-09-13 09:05:59 -0700118};
119
120// Macro and constexpr to make error values less annoying to write.
121#define ERR(e) JVMTI_ERROR_ ## e
122static constexpr jvmtiError OK = JVMTI_ERROR_NONE;
123
124// Special error code for unimplemented functions in JVMTI
125static constexpr jvmtiError ERR(NOT_IMPLEMENTED) = JVMTI_ERROR_NOT_AVAILABLE;
126
127static inline JNIEnv* GetJniEnv(jvmtiEnv* env) {
128 JNIEnv* ret_value = nullptr;
129 jint res = reinterpret_cast<ArtJvmTiEnv*>(env)->art_vm->GetEnv(
130 reinterpret_cast<void**>(&ret_value), JNI_VERSION_1_1);
131 if (res != JNI_OK) {
132 return nullptr;
133 }
134 return ret_value;
135}
136
Andreas Gampe54711412017-02-21 12:41:43 -0800137template <typename T>
Andreas Gampe3c252f02016-10-27 18:25:17 -0700138class JvmtiDeleter {
139 public:
140 JvmtiDeleter() : env_(nullptr) {}
141 explicit JvmtiDeleter(jvmtiEnv* env) : env_(env) {}
142
143 JvmtiDeleter(JvmtiDeleter&) = default;
144 JvmtiDeleter(JvmtiDeleter&&) = default;
145 JvmtiDeleter& operator=(const JvmtiDeleter&) = default;
146
Andreas Gampe54711412017-02-21 12:41:43 -0800147 void operator()(T* ptr) const {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700148 CHECK(env_ != nullptr);
Andreas Gampe54711412017-02-21 12:41:43 -0800149 jvmtiError ret = env_->Deallocate(reinterpret_cast<unsigned char*>(ptr));
Andreas Gampe3c252f02016-10-27 18:25:17 -0700150 CHECK(ret == ERR(NONE));
151 }
152
153 private:
154 mutable jvmtiEnv* env_;
155};
156
Andreas Gampe54711412017-02-21 12:41:43 -0800157template <typename T>
158class JvmtiDeleter<T[]> {
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800159 public:
Andreas Gampe54711412017-02-21 12:41:43 -0800160 JvmtiDeleter() : env_(nullptr) {}
161 explicit JvmtiDeleter(jvmtiEnv* env) : env_(env) {}
162
163 JvmtiDeleter(JvmtiDeleter&) = default;
164 JvmtiDeleter(JvmtiDeleter&&) = default;
165 JvmtiDeleter& operator=(const JvmtiDeleter&) = default;
166
167 template <typename U>
168 void operator()(U* ptr) const {
169 CHECK(env_ != nullptr);
170 jvmtiError ret = env_->Deallocate(reinterpret_cast<unsigned char*>(ptr));
171 CHECK(ret == ERR(NONE));
172 }
173
174 private:
175 mutable jvmtiEnv* env_;
176};
177
178template <typename T>
179using JvmtiUniquePtr = std::unique_ptr<T, JvmtiDeleter<T>>;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700180
Andreas Gampe1bdaf732017-01-09 19:21:06 -0800181template <typename T>
Andreas Gampe3c252f02016-10-27 18:25:17 -0700182ALWAYS_INLINE
Andreas Gampe54711412017-02-21 12:41:43 -0800183static inline JvmtiUniquePtr<T> MakeJvmtiUniquePtr(jvmtiEnv* env, T* mem) {
184 return JvmtiUniquePtr<T>(mem, JvmtiDeleter<T>(env));
185}
186
187template <typename T>
188ALWAYS_INLINE
189static inline JvmtiUniquePtr<T> MakeJvmtiUniquePtr(jvmtiEnv* env, unsigned char* mem) {
190 return JvmtiUniquePtr<T>(reinterpret_cast<T*>(mem), JvmtiDeleter<T>(env));
191}
192
193template <typename T>
194ALWAYS_INLINE
195static inline JvmtiUniquePtr<T> AllocJvmtiUniquePtr(jvmtiEnv* env, jvmtiError* error) {
196 unsigned char* tmp;
197 *error = env->Allocate(sizeof(T), &tmp);
198 if (*error != ERR(NONE)) {
199 return JvmtiUniquePtr<T>();
200 }
201 return JvmtiUniquePtr<T>(tmp, JvmtiDeleter<T>(env));
202}
203
204template <typename T>
205ALWAYS_INLINE
206static inline JvmtiUniquePtr<T> AllocJvmtiUniquePtr(jvmtiEnv* env,
207 size_t count,
208 jvmtiError* error) {
209 unsigned char* tmp;
210 *error = env->Allocate(sizeof(typename std::remove_extent<T>::type) * count, &tmp);
211 if (*error != ERR(NONE)) {
212 return JvmtiUniquePtr<T>();
213 }
214 return JvmtiUniquePtr<T>(reinterpret_cast<typename std::remove_extent<T>::type*>(tmp),
215 JvmtiDeleter<T>(env));
Andreas Gampe3c252f02016-10-27 18:25:17 -0700216}
217
Andreas Gampee492ae32016-10-28 19:34:57 -0700218ALWAYS_INLINE
Alex Light440b5d92017-01-24 15:32:25 -0800219static inline jvmtiError CopyDataIntoJvmtiBuffer(ArtJvmTiEnv* env,
220 const unsigned char* source,
221 jint len,
222 /*out*/unsigned char** dest) {
223 jvmtiError res = env->Allocate(len, dest);
224 if (res != OK) {
225 return res;
226 }
227 memcpy(reinterpret_cast<void*>(*dest),
228 reinterpret_cast<const void*>(source),
229 len);
230 return OK;
231}
232
233ALWAYS_INLINE
Andreas Gampe54711412017-02-21 12:41:43 -0800234static inline JvmtiUniquePtr<char[]> CopyString(jvmtiEnv* env, const char* src, jvmtiError* error) {
Alex Lightce68cc62017-07-26 10:30:38 -0700235 if (src == nullptr) {
236 JvmtiUniquePtr<char[]> ret = AllocJvmtiUniquePtr<char[]>(env, 0, error);
237 return ret;
238 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700239 size_t len = strlen(src) + 1;
Andreas Gampe54711412017-02-21 12:41:43 -0800240 JvmtiUniquePtr<char[]> ret = AllocJvmtiUniquePtr<char[]>(env, len, error);
241 if (ret != nullptr) {
Andreas Gampef45d61c2017-06-07 10:29:33 -0700242 strlcpy(ret.get(), src, len);
Andreas Gampee492ae32016-10-28 19:34:57 -0700243 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700244 return ret;
245}
246
Alex Lighte6574242016-08-17 09:56:24 -0700247const jvmtiCapabilities kPotentialCapabilities = {
248 .can_tag_objects = 1,
Alex Light084fa372017-06-16 08:58:34 -0700249 .can_generate_field_modification_events = 1,
250 .can_generate_field_access_events = 1,
Alex Light4c174282017-07-05 10:18:18 -0700251 .can_get_bytecodes = 1,
Alex Light9db679d2017-01-25 15:28:04 -0800252 .can_get_synthetic_attribute = 1,
Alex Light88e1ddd2017-08-21 13:09:55 -0700253 .can_get_owned_monitor_info = 1,
Alex Light41006c62017-09-14 09:51:14 -0700254 .can_get_current_contended_monitor = 1,
Alex Lightce568642017-09-05 16:54:25 -0700255 .can_get_monitor_info = 1,
Alex Light0aa7a5a2018-10-10 15:58:14 +0000256 .can_pop_frame = 1,
Alex Light6ac57502017-01-19 15:05:06 -0800257 .can_redefine_classes = 1,
Alex Light54d39dc2017-09-25 17:00:16 -0700258 .can_signal_thread = 1,
Alex Light6fa7b812017-06-16 09:04:29 -0700259 .can_get_source_file_name = 1,
Alex Light9db679d2017-01-25 15:28:04 -0800260 .can_get_line_numbers = 1,
Alex Light6fa7b812017-06-16 09:04:29 -0700261 .can_get_source_debug_extension = 1,
Alex Lightce68cc62017-07-26 10:30:38 -0700262 .can_access_local_variables = 1,
Alex Lightb566fed2017-07-28 15:17:00 -0700263 .can_maintain_original_method_order = 1,
Alex Lighta26e3492017-06-27 17:55:37 -0700264 .can_generate_single_step_events = 1,
Alex Light9fb1ab12017-09-05 09:32:49 -0700265 .can_generate_exception_events = 1,
Alex Lighte814f9d2017-07-31 16:14:39 -0700266 .can_generate_frame_pop_events = 1,
Alex Lighta26e3492017-06-27 17:55:37 -0700267 .can_generate_breakpoint_events = 1,
Alex Light88fd7202017-06-30 08:31:59 -0700268 .can_suspend = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700269 .can_redefine_any_class = 0,
270 .can_get_current_thread_cpu_time = 0,
271 .can_get_thread_cpu_time = 0,
Alex Lightb7edcda2017-04-27 13:20:31 -0700272 .can_generate_method_entry_events = 1,
273 .can_generate_method_exit_events = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700274 .can_generate_all_class_hook_events = 0,
275 .can_generate_compiled_method_load_events = 0,
Alex Light77fee872017-09-05 14:51:49 -0700276 .can_generate_monitor_events = 1,
Alex Light9db679d2017-01-25 15:28:04 -0800277 .can_generate_vm_object_alloc_events = 1,
Alex Lightd78ddec2017-04-18 15:20:38 -0700278 .can_generate_native_method_bind_events = 1,
Alex Light9db679d2017-01-25 15:28:04 -0800279 .can_generate_garbage_collection_events = 1,
280 .can_generate_object_free_events = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700281 .can_force_early_return = 0,
Alex Light88e1ddd2017-08-21 13:09:55 -0700282 .can_get_owned_monitor_stack_depth_info = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700283 .can_get_constant_pool = 0,
284 .can_set_native_method_prefix = 0,
Alex Light6ac57502017-01-19 15:05:06 -0800285 .can_retransform_classes = 1,
Alex Lighte6574242016-08-17 09:56:24 -0700286 .can_retransform_any_class = 0,
287 .can_generate_resource_exhaustion_heap_events = 0,
288 .can_generate_resource_exhaustion_threads_events = 0,
289};
290
Alex Light2ce6fc82017-12-18 16:42:36 -0800291// These are capabilities that are disabled if we were loaded without being debuggable.
292//
293// This includes the following capabilities:
294// can_retransform_any_class:
295// can_retransform_classes:
296// can_redefine_any_class:
297// can_redefine_classes:
Alex Light0aa7a5a2018-10-10 15:58:14 +0000298// can_pop_frame:
Alex Light2ce6fc82017-12-18 16:42:36 -0800299// We need to ensure that inlined code is either not present or can always be deoptimized. This
300// is not guaranteed for non-debuggable processes since we might have inlined bootclasspath code
301// on a threads stack.
302const jvmtiCapabilities kNonDebuggableUnsupportedCapabilities = {
303 .can_tag_objects = 0,
304 .can_generate_field_modification_events = 0,
305 .can_generate_field_access_events = 0,
306 .can_get_bytecodes = 0,
307 .can_get_synthetic_attribute = 0,
308 .can_get_owned_monitor_info = 0,
309 .can_get_current_contended_monitor = 0,
310 .can_get_monitor_info = 0,
Alex Light0aa7a5a2018-10-10 15:58:14 +0000311 .can_pop_frame = 1,
Alex Light2ce6fc82017-12-18 16:42:36 -0800312 .can_redefine_classes = 1,
313 .can_signal_thread = 0,
314 .can_get_source_file_name = 0,
315 .can_get_line_numbers = 0,
316 .can_get_source_debug_extension = 0,
317 .can_access_local_variables = 0,
318 .can_maintain_original_method_order = 0,
319 .can_generate_single_step_events = 0,
320 .can_generate_exception_events = 0,
321 .can_generate_frame_pop_events = 0,
322 .can_generate_breakpoint_events = 0,
323 .can_suspend = 0,
324 .can_redefine_any_class = 1,
325 .can_get_current_thread_cpu_time = 0,
326 .can_get_thread_cpu_time = 0,
327 .can_generate_method_entry_events = 0,
328 .can_generate_method_exit_events = 0,
329 .can_generate_all_class_hook_events = 0,
330 .can_generate_compiled_method_load_events = 0,
331 .can_generate_monitor_events = 0,
332 .can_generate_vm_object_alloc_events = 0,
333 .can_generate_native_method_bind_events = 0,
334 .can_generate_garbage_collection_events = 0,
335 .can_generate_object_free_events = 0,
336 .can_force_early_return = 0,
337 .can_get_owned_monitor_stack_depth_info = 0,
338 .can_get_constant_pool = 0,
339 .can_set_native_method_prefix = 0,
340 .can_retransform_classes = 1,
341 .can_retransform_any_class = 1,
342 .can_generate_resource_exhaustion_heap_events = 0,
343 .can_generate_resource_exhaustion_threads_events = 0,
344};
345
Andreas Gampedb6dcb62016-09-13 09:05:59 -0700346} // namespace openjdkjvmti
347
Andreas Gampe06c42a52017-07-26 14:17:14 -0700348#endif // ART_OPENJDKJVMTI_ART_JVMTI_H_