• 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_RUNTIME_OPENJDKJVMTI_ART_JVMTI_H_
33 #define ART_RUNTIME_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 "base/casts.h"
43 #include "base/logging.h"
44 #include "base/macros.h"
45 #include "base/strlcpy.h"
46 #include "events.h"
47 #include "java_vm_ext.h"
48 #include "jni_env_ext.h"
49 #include "jvmti.h"
50 #include "ti_breakpoint.h"
51 
52 namespace art {
53 class ArtField;
54 class ArtMethod;
55 }  // namespace art
56 
57 namespace openjdkjvmti {
58 
59 class ObjectTagTable;
60 
61 // A structure that is a jvmtiEnv with additional information for the runtime.
62 struct ArtJvmTiEnv : public jvmtiEnv {
63   art::JavaVMExt* art_vm;
64   void* local_data;
65   jvmtiCapabilities capabilities;
66 
67   EventMasks event_masks;
68   std::unique_ptr<jvmtiEventCallbacks> event_callbacks;
69 
70   // Tagging is specific to the jvmtiEnv.
71   std::unique_ptr<ObjectTagTable> object_tag_table;
72 
73   // Set of watched fields is unique to each jvmtiEnv.
74   // TODO It might be good to follow the RI and only let one jvmtiEnv ever have the watch caps so
75   // we can record this on the field directly. We could do this either using free access-flag bits
76   // or by putting a list in the ClassExt of a field's DeclaringClass.
77   // TODO Maybe just have an extension to let one put a watch on every field, that would probably be
78   // good enough maybe since you probably want either a few or all/almost all of them.
79   std::unordered_set<art::ArtField*> access_watched_fields;
80   std::unordered_set<art::ArtField*> modify_watched_fields;
81 
82   // Set of breakpoints is unique to each jvmtiEnv.
83   std::unordered_set<Breakpoint> breakpoints;
84 
85   ArtJvmTiEnv(art::JavaVMExt* runtime, EventHandler* event_handler);
86 
AsArtJvmTiEnvArtJvmTiEnv87   static ArtJvmTiEnv* AsArtJvmTiEnv(jvmtiEnv* env) {
88     return art::down_cast<ArtJvmTiEnv*>(env);
89   }
90 };
91 
92 // Macro and constexpr to make error values less annoying to write.
93 #define ERR(e) JVMTI_ERROR_ ## e
94 static constexpr jvmtiError OK = JVMTI_ERROR_NONE;
95 
96 // Special error code for unimplemented functions in JVMTI
97 static constexpr jvmtiError ERR(NOT_IMPLEMENTED) = JVMTI_ERROR_NOT_AVAILABLE;
98 
GetJniEnv(jvmtiEnv * env)99 static inline JNIEnv* GetJniEnv(jvmtiEnv* env) {
100   JNIEnv* ret_value = nullptr;
101   jint res = reinterpret_cast<ArtJvmTiEnv*>(env)->art_vm->GetEnv(
102       reinterpret_cast<void**>(&ret_value), JNI_VERSION_1_1);
103   if (res != JNI_OK) {
104     return nullptr;
105   }
106   return ret_value;
107 }
108 
109 template <typename T>
110 class JvmtiDeleter {
111  public:
JvmtiDeleter()112   JvmtiDeleter() : env_(nullptr) {}
JvmtiDeleter(jvmtiEnv * env)113   explicit JvmtiDeleter(jvmtiEnv* env) : env_(env) {}
114 
115   JvmtiDeleter(JvmtiDeleter&) = default;
116   JvmtiDeleter(JvmtiDeleter&&) = default;
117   JvmtiDeleter& operator=(const JvmtiDeleter&) = default;
118 
operator()119   void operator()(T* ptr) const {
120     CHECK(env_ != nullptr);
121     jvmtiError ret = env_->Deallocate(reinterpret_cast<unsigned char*>(ptr));
122     CHECK(ret == ERR(NONE));
123   }
124 
125  private:
126   mutable jvmtiEnv* env_;
127 };
128 
129 template <typename T>
130 class JvmtiDeleter<T[]> {
131   public:
JvmtiDeleter()132   JvmtiDeleter() : env_(nullptr) {}
JvmtiDeleter(jvmtiEnv * env)133   explicit JvmtiDeleter(jvmtiEnv* env) : env_(env) {}
134 
135   JvmtiDeleter(JvmtiDeleter&) = default;
136   JvmtiDeleter(JvmtiDeleter&&) = default;
137   JvmtiDeleter& operator=(const JvmtiDeleter&) = default;
138 
139   template <typename U>
operator()140   void operator()(U* ptr) const {
141     CHECK(env_ != nullptr);
142     jvmtiError ret = env_->Deallocate(reinterpret_cast<unsigned char*>(ptr));
143     CHECK(ret == ERR(NONE));
144   }
145 
146  private:
147   mutable jvmtiEnv* env_;
148 };
149 
150 template <typename T>
151 using JvmtiUniquePtr = std::unique_ptr<T, JvmtiDeleter<T>>;
152 
153 template <typename T>
154 ALWAYS_INLINE
MakeJvmtiUniquePtr(jvmtiEnv * env,T * mem)155 static inline JvmtiUniquePtr<T> MakeJvmtiUniquePtr(jvmtiEnv* env, T* mem) {
156   return JvmtiUniquePtr<T>(mem, JvmtiDeleter<T>(env));
157 }
158 
159 template <typename T>
160 ALWAYS_INLINE
MakeJvmtiUniquePtr(jvmtiEnv * env,unsigned char * mem)161 static inline JvmtiUniquePtr<T> MakeJvmtiUniquePtr(jvmtiEnv* env, unsigned char* mem) {
162   return JvmtiUniquePtr<T>(reinterpret_cast<T*>(mem), JvmtiDeleter<T>(env));
163 }
164 
165 template <typename T>
166 ALWAYS_INLINE
AllocJvmtiUniquePtr(jvmtiEnv * env,jvmtiError * error)167 static inline JvmtiUniquePtr<T> AllocJvmtiUniquePtr(jvmtiEnv* env, jvmtiError* error) {
168   unsigned char* tmp;
169   *error = env->Allocate(sizeof(T), &tmp);
170   if (*error != ERR(NONE)) {
171     return JvmtiUniquePtr<T>();
172   }
173   return JvmtiUniquePtr<T>(tmp, JvmtiDeleter<T>(env));
174 }
175 
176 template <typename T>
177 ALWAYS_INLINE
AllocJvmtiUniquePtr(jvmtiEnv * env,size_t count,jvmtiError * error)178 static inline JvmtiUniquePtr<T> AllocJvmtiUniquePtr(jvmtiEnv* env,
179                                                     size_t count,
180                                                     jvmtiError* error) {
181   unsigned char* tmp;
182   *error = env->Allocate(sizeof(typename std::remove_extent<T>::type) * count, &tmp);
183   if (*error != ERR(NONE)) {
184     return JvmtiUniquePtr<T>();
185   }
186   return JvmtiUniquePtr<T>(reinterpret_cast<typename std::remove_extent<T>::type*>(tmp),
187                            JvmtiDeleter<T>(env));
188 }
189 
190 ALWAYS_INLINE
CopyDataIntoJvmtiBuffer(ArtJvmTiEnv * env,const unsigned char * source,jint len,unsigned char ** dest)191 static inline jvmtiError CopyDataIntoJvmtiBuffer(ArtJvmTiEnv* env,
192                                                  const unsigned char* source,
193                                                  jint len,
194                                                  /*out*/unsigned char** dest) {
195   jvmtiError res = env->Allocate(len, dest);
196   if (res != OK) {
197     return res;
198   }
199   memcpy(reinterpret_cast<void*>(*dest),
200          reinterpret_cast<const void*>(source),
201          len);
202   return OK;
203 }
204 
205 ALWAYS_INLINE
CopyString(jvmtiEnv * env,const char * src,jvmtiError * error)206 static inline JvmtiUniquePtr<char[]> CopyString(jvmtiEnv* env, const char* src, jvmtiError* error) {
207   size_t len = strlen(src) + 1;
208   JvmtiUniquePtr<char[]> ret = AllocJvmtiUniquePtr<char[]>(env, len, error);
209   if (ret != nullptr) {
210     strlcpy(ret.get(), src, len);
211   }
212   return ret;
213 }
214 
215 const jvmtiCapabilities kPotentialCapabilities = {
216     .can_tag_objects                                 = 1,
217     .can_generate_field_modification_events          = 1,
218     .can_generate_field_access_events                = 1,
219     .can_get_bytecodes                               = 1,
220     .can_get_synthetic_attribute                     = 1,
221     .can_get_owned_monitor_info                      = 0,
222     .can_get_current_contended_monitor               = 0,
223     .can_get_monitor_info                            = 0,
224     .can_pop_frame                                   = 0,
225     .can_redefine_classes                            = 1,
226     .can_signal_thread                               = 0,
227     .can_get_source_file_name                        = 1,
228     .can_get_line_numbers                            = 1,
229     .can_get_source_debug_extension                  = 1,
230     .can_access_local_variables                      = 0,
231     .can_maintain_original_method_order              = 0,
232     .can_generate_single_step_events                 = 1,
233     .can_generate_exception_events                   = 0,
234     .can_generate_frame_pop_events                   = 0,
235     .can_generate_breakpoint_events                  = 1,
236     .can_suspend                                     = 0,
237     .can_redefine_any_class                          = 0,
238     .can_get_current_thread_cpu_time                 = 0,
239     .can_get_thread_cpu_time                         = 0,
240     .can_generate_method_entry_events                = 1,
241     .can_generate_method_exit_events                 = 1,
242     .can_generate_all_class_hook_events              = 0,
243     .can_generate_compiled_method_load_events        = 0,
244     .can_generate_monitor_events                     = 0,
245     .can_generate_vm_object_alloc_events             = 1,
246     .can_generate_native_method_bind_events          = 1,
247     .can_generate_garbage_collection_events          = 1,
248     .can_generate_object_free_events                 = 1,
249     .can_force_early_return                          = 0,
250     .can_get_owned_monitor_stack_depth_info          = 0,
251     .can_get_constant_pool                           = 0,
252     .can_set_native_method_prefix                    = 0,
253     .can_retransform_classes                         = 1,
254     .can_retransform_any_class                       = 0,
255     .can_generate_resource_exhaustion_heap_events    = 0,
256     .can_generate_resource_exhaustion_threads_events = 0,
257 };
258 
259 }  // namespace openjdkjvmti
260 
261 #endif  // ART_RUNTIME_OPENJDKJVMTI_ART_JVMTI_H_
262