• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* 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 #ifndef ART_OPENJDKJVMTI_ART_JVMTI_H_
33 #define ART_OPENJDKJVMTI_ART_JVMTI_H_
34 
35 #include <memory>
36 #include <type_traits>
37 #include <unordered_map>
38 #include <unordered_set>
39 
40 #include <jni.h>
41 
42 #include <android-base/logging.h>
43 
44 #include "deopt_manager.h"
45 #include "base/casts.h"
46 #include "base/macros.h"
47 #include "base/strlcpy.h"
48 #include "base/mutex.h"
49 #include "events.h"
50 #include "instrumentation.h"
51 #include "jni/java_vm_ext.h"
52 #include "jni/jni_env_ext.h"
53 #include "jvmti.h"
54 #include "runtime.h"
55 #include "ti_breakpoint.h"
56 
57 namespace art {
58 class ArtField;
59 class ArtMethod;
60 class ShadowFrame;
61 }  // namespace art
62 
63 namespace openjdkjvmti {
64 
65 class ObjectTagTable;
66 
67 // A special version that we use to identify special tooling interface versions which mostly matches
68 // the jvmti spec but everything is best effort. This is used to implement the userdebug
69 // 'debug-anything' behavior.
70 //
71 // This is the value 0x70010200.
72 static constexpr jint kArtTiVersion = JVMTI_VERSION_1_2 | 0x40000000;
73 
74 // Returns whether we are able to use all jvmti features.
IsFullJvmtiAvailable()75 static inline bool IsFullJvmtiAvailable() {
76   art::Runtime* runtime = art::Runtime::Current();
77   return runtime->GetInstrumentation()->IsForcedInterpretOnly() ||
78          runtime->IsJavaDebuggableAtInit();
79 }
80 
81 // A structure that is a jvmtiEnv with additional information for the runtime.
82 struct ArtJvmTiEnv : public jvmtiEnv {
83   art::JavaVMExt* art_vm;
84   void* local_data;
85 
86   // The ti_version we are compatible with. This is only for giving the correct value for GetVersion
87   // when running on a userdebug/eng device.
88   jint ti_version;
89 
90   jvmtiCapabilities capabilities;
91 
92   EventMasks event_masks;
93   std::unique_ptr<ArtJvmtiEventCallbacks> event_callbacks;
94 
95   // Tagging is specific to the jvmtiEnv.
96   std::unique_ptr<ObjectTagTable> object_tag_table;
97 
98   // Set of watched fields is unique to each jvmtiEnv.
99   // TODO It might be good to follow the RI and only let one jvmtiEnv ever have the watch caps so
100   // we can record this on the field directly. We could do this either using free access-flag bits
101   // or by putting a list in the ClassExt of a field's DeclaringClass.
102   // TODO Maybe just have an extension to let one put a watch on every field, that would probably be
103   // good enough maybe since you probably want either a few or all/almost all of them.
104   std::unordered_set<art::ArtField*> access_watched_fields GUARDED_BY(event_info_mutex_);
105   std::unordered_set<art::ArtField*> modify_watched_fields GUARDED_BY(event_info_mutex_);
106 
107   // Set of breakpoints is unique to each jvmtiEnv.
108   std::unordered_set<Breakpoint> breakpoints GUARDED_BY(event_info_mutex_);
109   std::unordered_set<const art::ShadowFrame*> notify_frames GUARDED_BY(event_info_mutex_);
110 
111   // RW lock to protect access to all of the event data.
112   art::ReaderWriterMutex event_info_mutex_ DEFAULT_MUTEX_ACQUIRED_AFTER;
113 
114   std::string last_error_ GUARDED_BY(last_error_mutex_);
115   // Lock to touch the last-error-message.
116   art::Mutex last_error_mutex_ BOTTOM_MUTEX_ACQUIRED_AFTER;
117 
118   ArtJvmTiEnv(art::JavaVMExt* runtime, EventHandler* event_handler, jint ti_version);
119 
AsArtJvmTiEnvArtJvmTiEnv120   static ArtJvmTiEnv* AsArtJvmTiEnv(jvmtiEnv* env) {
121     return art::down_cast<ArtJvmTiEnv*>(env);
122   }
123 
124   // Top level lock. Nothing can be held when we get this except for mutator lock for full
125   // thread-suspension.
126   static art::Mutex *gEnvMutex ACQUIRED_AFTER(art::Locks::mutator_lock_);
127 };
128 
129 // Macro and constexpr to make error values less annoying to write.
130 #define ERR(e) JVMTI_ERROR_ ## e
131 static constexpr jvmtiError OK = JVMTI_ERROR_NONE;
132 
133 // Special error code for unimplemented functions in JVMTI
134 static constexpr jvmtiError ERR(NOT_IMPLEMENTED) = JVMTI_ERROR_NOT_AVAILABLE;
135 
GetJniEnv(jvmtiEnv * env)136 static inline JNIEnv* GetJniEnv(jvmtiEnv* env) {
137   JNIEnv* ret_value = nullptr;
138   jint res = reinterpret_cast<ArtJvmTiEnv*>(env)->art_vm->GetEnv(
139       reinterpret_cast<void**>(&ret_value), JNI_VERSION_1_1);
140   if (res != JNI_OK) {
141     return nullptr;
142   }
143   return ret_value;
144 }
145 
146 template <typename T>
147 class JvmtiDeleter {
148  public:
JvmtiDeleter()149   JvmtiDeleter() : env_(nullptr) {}
JvmtiDeleter(jvmtiEnv * env)150   explicit JvmtiDeleter(jvmtiEnv* env) : env_(env) {}
151 
152   JvmtiDeleter(JvmtiDeleter&) = default;
153   JvmtiDeleter(JvmtiDeleter&&) noexcept = default;
154   JvmtiDeleter& operator=(const JvmtiDeleter&) = default;
155 
operator()156   void operator()(T* ptr) const {
157     CHECK(env_ != nullptr);
158     jvmtiError ret = env_->Deallocate(reinterpret_cast<unsigned char*>(ptr));
159     CHECK(ret == ERR(NONE));
160   }
161 
162  private:
163   mutable jvmtiEnv* env_;
164 };
165 
166 template <typename T>
167 class JvmtiDeleter<T[]> {
168  public:
JvmtiDeleter()169   JvmtiDeleter() : env_(nullptr) {}
JvmtiDeleter(jvmtiEnv * env)170   explicit JvmtiDeleter(jvmtiEnv* env) : env_(env) {}
171 
172   JvmtiDeleter(JvmtiDeleter&) = default;
173   JvmtiDeleter(JvmtiDeleter&&) noexcept = default;
174   JvmtiDeleter& operator=(const JvmtiDeleter&) = default;
175 
176   template <typename U>
operator()177   void operator()(U* ptr) const {
178     CHECK(env_ != nullptr);
179     jvmtiError ret = env_->Deallocate(reinterpret_cast<unsigned char*>(ptr));
180     CHECK(ret == ERR(NONE));
181   }
182 
183  private:
184   mutable jvmtiEnv* env_;
185 };
186 
187 template <typename T>
188 using JvmtiUniquePtr = std::unique_ptr<T, JvmtiDeleter<T>>;
189 
190 template <typename T>
191 ALWAYS_INLINE
MakeJvmtiUniquePtr(jvmtiEnv * env,T * mem)192 static inline JvmtiUniquePtr<T> MakeJvmtiUniquePtr(jvmtiEnv* env, T* mem) {
193   return JvmtiUniquePtr<T>(mem, JvmtiDeleter<T>(env));
194 }
195 
196 template <typename T>
197 ALWAYS_INLINE
MakeJvmtiUniquePtr(jvmtiEnv * env,unsigned char * mem)198 static inline JvmtiUniquePtr<T> MakeJvmtiUniquePtr(jvmtiEnv* env, unsigned char* mem) {
199   return JvmtiUniquePtr<T>(reinterpret_cast<T*>(mem), JvmtiDeleter<T>(env));
200 }
201 
202 template <typename T>
203 ALWAYS_INLINE
AllocJvmtiUniquePtr(jvmtiEnv * env,jvmtiError * error)204 static inline JvmtiUniquePtr<T> AllocJvmtiUniquePtr(jvmtiEnv* env, jvmtiError* error) {
205   unsigned char* tmp;
206   *error = env->Allocate(sizeof(T), &tmp);
207   if (*error != ERR(NONE)) {
208     return JvmtiUniquePtr<T>();
209   }
210   return JvmtiUniquePtr<T>(tmp, JvmtiDeleter<T>(env));
211 }
212 
213 template <typename T>
214 ALWAYS_INLINE
AllocJvmtiUniquePtr(jvmtiEnv * env,size_t count,jvmtiError * error)215 static inline JvmtiUniquePtr<T> AllocJvmtiUniquePtr(jvmtiEnv* env,
216                                                     size_t count,
217                                                     jvmtiError* error) {
218   unsigned char* tmp;
219   *error = env->Allocate(sizeof(typename std::remove_extent<T>::type) * count, &tmp);
220   if (*error != ERR(NONE)) {
221     return JvmtiUniquePtr<T>();
222   }
223   return JvmtiUniquePtr<T>(reinterpret_cast<typename std::remove_extent<T>::type*>(tmp),
224                            JvmtiDeleter<T>(env));
225 }
226 
227 ALWAYS_INLINE
CopyDataIntoJvmtiBuffer(ArtJvmTiEnv * env,const unsigned char * source,jint len,unsigned char ** dest)228 static inline jvmtiError CopyDataIntoJvmtiBuffer(ArtJvmTiEnv* env,
229                                                  const unsigned char* source,
230                                                  jint len,
231                                                  /*out*/unsigned char** dest) {
232   jvmtiError res = env->Allocate(len, dest);
233   if (res != OK) {
234     return res;
235   }
236   memcpy(reinterpret_cast<void*>(*dest),
237          reinterpret_cast<const void*>(source),
238          len);
239   return OK;
240 }
241 
242 ALWAYS_INLINE
CopyString(jvmtiEnv * env,const char * src,jvmtiError * error)243 static inline JvmtiUniquePtr<char[]> CopyString(jvmtiEnv* env, const char* src, jvmtiError* error) {
244   if (src == nullptr) {
245     JvmtiUniquePtr<char[]> ret = AllocJvmtiUniquePtr<char[]>(env, 0, error);
246     return ret;
247   }
248   size_t len = strlen(src) + 1;
249   JvmtiUniquePtr<char[]> ret = AllocJvmtiUniquePtr<char[]>(env, len, error);
250   if (ret != nullptr) {
251     strlcpy(ret.get(), src, len);
252   }
253   return ret;
254 }
255 
256 const jvmtiCapabilities kPotentialCapabilities = {
257     .can_tag_objects                                 = 1,
258     .can_generate_field_modification_events          = 1,
259     .can_generate_field_access_events                = 1,
260     .can_get_bytecodes                               = 1,
261     .can_get_synthetic_attribute                     = 1,
262     .can_get_owned_monitor_info                      = 1,
263     .can_get_current_contended_monitor               = 1,
264     .can_get_monitor_info                            = 1,
265     .can_pop_frame                                   = 1,
266     .can_redefine_classes                            = 1,
267     .can_signal_thread                               = 1,
268     .can_get_source_file_name                        = 1,
269     .can_get_line_numbers                            = 1,
270     .can_get_source_debug_extension                  = 1,
271     .can_access_local_variables                      = 1,
272     .can_maintain_original_method_order              = 1,
273     .can_generate_single_step_events                 = 1,
274     .can_generate_exception_events                   = 1,
275     .can_generate_frame_pop_events                   = 1,
276     .can_generate_breakpoint_events                  = 1,
277     .can_suspend                                     = 1,
278     .can_redefine_any_class                          = 0,
279     .can_get_current_thread_cpu_time                 = 0,
280     .can_get_thread_cpu_time                         = 0,
281     .can_generate_method_entry_events                = 1,
282     .can_generate_method_exit_events                 = 1,
283     .can_generate_all_class_hook_events              = 0,
284     .can_generate_compiled_method_load_events        = 0,
285     .can_generate_monitor_events                     = 1,
286     .can_generate_vm_object_alloc_events             = 1,
287     .can_generate_native_method_bind_events          = 1,
288     .can_generate_garbage_collection_events          = 1,
289     .can_generate_object_free_events                 = 1,
290     .can_force_early_return                          = 1,
291     .can_get_owned_monitor_stack_depth_info          = 1,
292     .can_get_constant_pool                           = 0,
293     .can_set_native_method_prefix                    = 0,
294     .can_retransform_classes                         = 1,
295     .can_retransform_any_class                       = 0,
296     .can_generate_resource_exhaustion_heap_events    = 0,
297     .can_generate_resource_exhaustion_threads_events = 0,
298 };
299 
300 // These are capabilities that are disabled if we were loaded without being debuggable.
301 //
302 // This includes the following capabilities:
303 //   can_retransform_any_class:
304 //   can_retransform_classes:
305 //   can_redefine_any_class:
306 //   can_redefine_classes:
307 //   can_pop_frame:
308 //   can_force_early_return:
309 //     We need to ensure that inlined code is either not present or can always be deoptimized. This
310 //     is not guaranteed for non-debuggable processes since we might have inlined bootclasspath code
311 //     on a threads stack.
312 const jvmtiCapabilities kNonDebuggableUnsupportedCapabilities = {
313     .can_tag_objects                                 = 0,
314     .can_generate_field_modification_events          = 0,
315     .can_generate_field_access_events                = 0,
316     .can_get_bytecodes                               = 0,
317     .can_get_synthetic_attribute                     = 0,
318     .can_get_owned_monitor_info                      = 0,
319     .can_get_current_contended_monitor               = 0,
320     .can_get_monitor_info                            = 0,
321     .can_pop_frame                                   = 1,
322     .can_redefine_classes                            = 1,
323     .can_signal_thread                               = 0,
324     .can_get_source_file_name                        = 0,
325     .can_get_line_numbers                            = 0,
326     .can_get_source_debug_extension                  = 0,
327     .can_access_local_variables                      = 0,
328     .can_maintain_original_method_order              = 0,
329     .can_generate_single_step_events                 = 0,
330     .can_generate_exception_events                   = 0,
331     .can_generate_frame_pop_events                   = 0,
332     .can_generate_breakpoint_events                  = 0,
333     .can_suspend                                     = 0,
334     .can_redefine_any_class                          = 1,
335     .can_get_current_thread_cpu_time                 = 0,
336     .can_get_thread_cpu_time                         = 0,
337     .can_generate_method_entry_events                = 0,
338     .can_generate_method_exit_events                 = 0,
339     .can_generate_all_class_hook_events              = 0,
340     .can_generate_compiled_method_load_events        = 0,
341     .can_generate_monitor_events                     = 0,
342     .can_generate_vm_object_alloc_events             = 0,
343     .can_generate_native_method_bind_events          = 0,
344     .can_generate_garbage_collection_events          = 0,
345     .can_generate_object_free_events                 = 0,
346     .can_force_early_return                          = 1,
347     .can_get_owned_monitor_stack_depth_info          = 0,
348     .can_get_constant_pool                           = 0,
349     .can_set_native_method_prefix                    = 0,
350     .can_retransform_classes                         = 1,
351     .can_retransform_any_class                       = 1,
352     .can_generate_resource_exhaustion_heap_events    = 0,
353     .can_generate_resource_exhaustion_threads_events = 0,
354 };
355 
356 }  // namespace openjdkjvmti
357 
358 #endif  // ART_OPENJDKJVMTI_ART_JVMTI_H_
359