• 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 "java_vm_ext.h"
51 #include "jni_env_ext.h"
52 #include "jvmti.h"
53 #include "ti_breakpoint.h"
54 
55 namespace art {
56 class ArtField;
57 class ArtMethod;
58 class ShadowFrame;
59 }  // namespace art
60 
61 namespace openjdkjvmti {
62 
63 class ObjectTagTable;
64 
65 // 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.
70 static constexpr jint kArtTiVersion = JVMTI_VERSION_1_2 | 0x40000000;
71 
72 // A structure that is a jvmtiEnv with additional information for the runtime.
73 struct ArtJvmTiEnv : public jvmtiEnv {
74   art::JavaVMExt* art_vm;
75   void* local_data;
76 
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 
81   jvmtiCapabilities capabilities;
82 
83   EventMasks event_masks;
84   std::unique_ptr<ArtJvmtiEventCallbacks> event_callbacks;
85 
86   // Tagging is specific to the jvmtiEnv.
87   std::unique_ptr<ObjectTagTable> object_tag_table;
88 
89   // 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.
95   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_);
97 
98   // Set of breakpoints is unique to each jvmtiEnv.
99   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;
104 
105   ArtJvmTiEnv(art::JavaVMExt* runtime, EventHandler* event_handler, jint ti_version);
106 
AsArtJvmTiEnvArtJvmTiEnv107   static ArtJvmTiEnv* AsArtJvmTiEnv(jvmtiEnv* env) {
108     return art::down_cast<ArtJvmTiEnv*>(env);
109   }
110 
111   // Top level lock. Nothing can be held when we get this except for mutator lock for full
112   // thread-suspension.
113   static art::Mutex *gEnvMutex ACQUIRED_AFTER(art::Locks::mutator_lock_);
114 };
115 
116 // Macro and constexpr to make error values less annoying to write.
117 #define ERR(e) JVMTI_ERROR_ ## e
118 static constexpr jvmtiError OK = JVMTI_ERROR_NONE;
119 
120 // Special error code for unimplemented functions in JVMTI
121 static constexpr jvmtiError ERR(NOT_IMPLEMENTED) = JVMTI_ERROR_NOT_AVAILABLE;
122 
GetJniEnv(jvmtiEnv * env)123 static inline JNIEnv* GetJniEnv(jvmtiEnv* env) {
124   JNIEnv* ret_value = nullptr;
125   jint res = reinterpret_cast<ArtJvmTiEnv*>(env)->art_vm->GetEnv(
126       reinterpret_cast<void**>(&ret_value), JNI_VERSION_1_1);
127   if (res != JNI_OK) {
128     return nullptr;
129   }
130   return ret_value;
131 }
132 
133 template <typename T>
134 class JvmtiDeleter {
135  public:
JvmtiDeleter()136   JvmtiDeleter() : env_(nullptr) {}
JvmtiDeleter(jvmtiEnv * env)137   explicit JvmtiDeleter(jvmtiEnv* env) : env_(env) {}
138 
139   JvmtiDeleter(JvmtiDeleter&) = default;
140   JvmtiDeleter(JvmtiDeleter&&) = default;
141   JvmtiDeleter& operator=(const JvmtiDeleter&) = default;
142 
operator()143   void operator()(T* ptr) const {
144     CHECK(env_ != nullptr);
145     jvmtiError ret = env_->Deallocate(reinterpret_cast<unsigned char*>(ptr));
146     CHECK(ret == ERR(NONE));
147   }
148 
149  private:
150   mutable jvmtiEnv* env_;
151 };
152 
153 template <typename T>
154 class JvmtiDeleter<T[]> {
155  public:
JvmtiDeleter()156   JvmtiDeleter() : env_(nullptr) {}
JvmtiDeleter(jvmtiEnv * env)157   explicit JvmtiDeleter(jvmtiEnv* env) : env_(env) {}
158 
159   JvmtiDeleter(JvmtiDeleter&) = default;
160   JvmtiDeleter(JvmtiDeleter&&) = default;
161   JvmtiDeleter& operator=(const JvmtiDeleter&) = default;
162 
163   template <typename U>
operator()164   void operator()(U* ptr) const {
165     CHECK(env_ != nullptr);
166     jvmtiError ret = env_->Deallocate(reinterpret_cast<unsigned char*>(ptr));
167     CHECK(ret == ERR(NONE));
168   }
169 
170  private:
171   mutable jvmtiEnv* env_;
172 };
173 
174 template <typename T>
175 using JvmtiUniquePtr = std::unique_ptr<T, JvmtiDeleter<T>>;
176 
177 template <typename T>
178 ALWAYS_INLINE
MakeJvmtiUniquePtr(jvmtiEnv * env,T * mem)179 static inline JvmtiUniquePtr<T> MakeJvmtiUniquePtr(jvmtiEnv* env, T* mem) {
180   return JvmtiUniquePtr<T>(mem, JvmtiDeleter<T>(env));
181 }
182 
183 template <typename T>
184 ALWAYS_INLINE
MakeJvmtiUniquePtr(jvmtiEnv * env,unsigned char * mem)185 static inline JvmtiUniquePtr<T> MakeJvmtiUniquePtr(jvmtiEnv* env, unsigned char* mem) {
186   return JvmtiUniquePtr<T>(reinterpret_cast<T*>(mem), JvmtiDeleter<T>(env));
187 }
188 
189 template <typename T>
190 ALWAYS_INLINE
AllocJvmtiUniquePtr(jvmtiEnv * env,jvmtiError * error)191 static inline JvmtiUniquePtr<T> AllocJvmtiUniquePtr(jvmtiEnv* env, jvmtiError* error) {
192   unsigned char* tmp;
193   *error = env->Allocate(sizeof(T), &tmp);
194   if (*error != ERR(NONE)) {
195     return JvmtiUniquePtr<T>();
196   }
197   return JvmtiUniquePtr<T>(tmp, JvmtiDeleter<T>(env));
198 }
199 
200 template <typename T>
201 ALWAYS_INLINE
AllocJvmtiUniquePtr(jvmtiEnv * env,size_t count,jvmtiError * error)202 static inline JvmtiUniquePtr<T> AllocJvmtiUniquePtr(jvmtiEnv* env,
203                                                     size_t count,
204                                                     jvmtiError* error) {
205   unsigned char* tmp;
206   *error = env->Allocate(sizeof(typename std::remove_extent<T>::type) * count, &tmp);
207   if (*error != ERR(NONE)) {
208     return JvmtiUniquePtr<T>();
209   }
210   return JvmtiUniquePtr<T>(reinterpret_cast<typename std::remove_extent<T>::type*>(tmp),
211                            JvmtiDeleter<T>(env));
212 }
213 
214 ALWAYS_INLINE
CopyDataIntoJvmtiBuffer(ArtJvmTiEnv * env,const unsigned char * source,jint len,unsigned char ** dest)215 static inline jvmtiError CopyDataIntoJvmtiBuffer(ArtJvmTiEnv* env,
216                                                  const unsigned char* source,
217                                                  jint len,
218                                                  /*out*/unsigned char** dest) {
219   jvmtiError res = env->Allocate(len, dest);
220   if (res != OK) {
221     return res;
222   }
223   memcpy(reinterpret_cast<void*>(*dest),
224          reinterpret_cast<const void*>(source),
225          len);
226   return OK;
227 }
228 
229 ALWAYS_INLINE
CopyString(jvmtiEnv * env,const char * src,jvmtiError * error)230 static inline JvmtiUniquePtr<char[]> CopyString(jvmtiEnv* env, const char* src, jvmtiError* error) {
231   if (src == nullptr) {
232     JvmtiUniquePtr<char[]> ret = AllocJvmtiUniquePtr<char[]>(env, 0, error);
233     return ret;
234   }
235   size_t len = strlen(src) + 1;
236   JvmtiUniquePtr<char[]> ret = AllocJvmtiUniquePtr<char[]>(env, len, error);
237   if (ret != nullptr) {
238     strlcpy(ret.get(), src, len);
239   }
240   return ret;
241 }
242 
243 const jvmtiCapabilities kPotentialCapabilities = {
244     .can_tag_objects                                 = 1,
245     .can_generate_field_modification_events          = 1,
246     .can_generate_field_access_events                = 1,
247     .can_get_bytecodes                               = 1,
248     .can_get_synthetic_attribute                     = 1,
249     .can_get_owned_monitor_info                      = 1,
250     .can_get_current_contended_monitor               = 1,
251     .can_get_monitor_info                            = 1,
252     .can_pop_frame                                   = 0,
253     .can_redefine_classes                            = 1,
254     .can_signal_thread                               = 1,
255     .can_get_source_file_name                        = 1,
256     .can_get_line_numbers                            = 1,
257     .can_get_source_debug_extension                  = 1,
258     .can_access_local_variables                      = 1,
259     .can_maintain_original_method_order              = 1,
260     .can_generate_single_step_events                 = 1,
261     .can_generate_exception_events                   = 1,
262     .can_generate_frame_pop_events                   = 1,
263     .can_generate_breakpoint_events                  = 1,
264     .can_suspend                                     = 1,
265     .can_redefine_any_class                          = 0,
266     .can_get_current_thread_cpu_time                 = 0,
267     .can_get_thread_cpu_time                         = 0,
268     .can_generate_method_entry_events                = 1,
269     .can_generate_method_exit_events                 = 1,
270     .can_generate_all_class_hook_events              = 0,
271     .can_generate_compiled_method_load_events        = 0,
272     .can_generate_monitor_events                     = 1,
273     .can_generate_vm_object_alloc_events             = 1,
274     .can_generate_native_method_bind_events          = 1,
275     .can_generate_garbage_collection_events          = 1,
276     .can_generate_object_free_events                 = 1,
277     .can_force_early_return                          = 0,
278     .can_get_owned_monitor_stack_depth_info          = 1,
279     .can_get_constant_pool                           = 0,
280     .can_set_native_method_prefix                    = 0,
281     .can_retransform_classes                         = 1,
282     .can_retransform_any_class                       = 0,
283     .can_generate_resource_exhaustion_heap_events    = 0,
284     .can_generate_resource_exhaustion_threads_events = 0,
285 };
286 
287 // These are capabilities that are disabled if we were loaded without being debuggable.
288 //
289 // This includes the following capabilities:
290 //   can_retransform_any_class:
291 //   can_retransform_classes:
292 //   can_redefine_any_class:
293 //   can_redefine_classes:
294 //     We need to ensure that inlined code is either not present or can always be deoptimized. This
295 //     is not guaranteed for non-debuggable processes since we might have inlined bootclasspath code
296 //     on a threads stack.
297 const jvmtiCapabilities kNonDebuggableUnsupportedCapabilities = {
298     .can_tag_objects                                 = 0,
299     .can_generate_field_modification_events          = 0,
300     .can_generate_field_access_events                = 0,
301     .can_get_bytecodes                               = 0,
302     .can_get_synthetic_attribute                     = 0,
303     .can_get_owned_monitor_info                      = 0,
304     .can_get_current_contended_monitor               = 0,
305     .can_get_monitor_info                            = 0,
306     .can_pop_frame                                   = 0,
307     .can_redefine_classes                            = 1,
308     .can_signal_thread                               = 0,
309     .can_get_source_file_name                        = 0,
310     .can_get_line_numbers                            = 0,
311     .can_get_source_debug_extension                  = 0,
312     .can_access_local_variables                      = 0,
313     .can_maintain_original_method_order              = 0,
314     .can_generate_single_step_events                 = 0,
315     .can_generate_exception_events                   = 0,
316     .can_generate_frame_pop_events                   = 0,
317     .can_generate_breakpoint_events                  = 0,
318     .can_suspend                                     = 0,
319     .can_redefine_any_class                          = 1,
320     .can_get_current_thread_cpu_time                 = 0,
321     .can_get_thread_cpu_time                         = 0,
322     .can_generate_method_entry_events                = 0,
323     .can_generate_method_exit_events                 = 0,
324     .can_generate_all_class_hook_events              = 0,
325     .can_generate_compiled_method_load_events        = 0,
326     .can_generate_monitor_events                     = 0,
327     .can_generate_vm_object_alloc_events             = 0,
328     .can_generate_native_method_bind_events          = 0,
329     .can_generate_garbage_collection_events          = 0,
330     .can_generate_object_free_events                 = 0,
331     .can_force_early_return                          = 0,
332     .can_get_owned_monitor_stack_depth_info          = 0,
333     .can_get_constant_pool                           = 0,
334     .can_set_native_method_prefix                    = 0,
335     .can_retransform_classes                         = 1,
336     .can_retransform_any_class                       = 1,
337     .can_generate_resource_exhaustion_heap_events    = 0,
338     .can_generate_resource_exhaustion_threads_events = 0,
339 };
340 
341 }  // namespace openjdkjvmti
342 
343 #endif  // ART_OPENJDKJVMTI_ART_JVMTI_H_
344