Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 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 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 32 | #include <unordered_map> |
| 33 | #include <unordered_set> |
| 34 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 35 | #include "transform.h" |
| 36 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 37 | #include "art_method.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 38 | #include "class_linker.h" |
| 39 | #include "dex_file.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 40 | #include "dex_file_types.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 41 | #include "gc_root-inl.h" |
| 42 | #include "globals.h" |
| 43 | #include "jni_env_ext-inl.h" |
| 44 | #include "jvmti.h" |
| 45 | #include "linear_alloc.h" |
| 46 | #include "mem_map.h" |
| 47 | #include "mirror/array.h" |
| 48 | #include "mirror/class-inl.h" |
| 49 | #include "mirror/class_loader-inl.h" |
| 50 | #include "mirror/string-inl.h" |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 51 | #include "oat_file.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 52 | #include "scoped_thread_state_change-inl.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 53 | #include "stack.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 54 | #include "thread_list.h" |
| 55 | #include "transform.h" |
| 56 | #include "utf.h" |
| 57 | #include "utils/dex_cache_arrays_layout-inl.h" |
| 58 | |
| 59 | namespace openjdkjvmti { |
| 60 | |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 61 | jvmtiError GetClassLocation(ArtJvmTiEnv* env, jclass klass, /*out*/std::string* location) { |
| 62 | JNIEnv* jni_env = nullptr; |
| 63 | jint ret = env->art_vm->GetEnv(reinterpret_cast<void**>(&jni_env), JNI_VERSION_1_1); |
| 64 | if (ret != JNI_OK) { |
| 65 | // TODO Different error might be better? |
| 66 | return ERR(INTERNAL); |
| 67 | } |
| 68 | art::ScopedObjectAccess soa(jni_env); |
| 69 | art::StackHandleScope<1> hs(art::Thread::Current()); |
| 70 | art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass))); |
| 71 | const art::DexFile& dex = hs_klass->GetDexFile(); |
| 72 | *location = dex.GetLocation(); |
| 73 | return OK; |
| 74 | } |
| 75 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 76 | // TODO Move this function somewhere more appropriate. |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 77 | // Gets the data surrounding the given class. |
| 78 | jvmtiError GetTransformationData(ArtJvmTiEnv* env, |
| 79 | jclass klass, |
| 80 | /*out*/std::string* location, |
| 81 | /*out*/JNIEnv** jni_env_ptr, |
| 82 | /*out*/jobject* loader, |
| 83 | /*out*/std::string* name, |
| 84 | /*out*/jobject* protection_domain, |
| 85 | /*out*/jint* data_len, |
| 86 | /*out*/unsigned char** dex_data) { |
| 87 | jint ret = env->art_vm->GetEnv(reinterpret_cast<void**>(jni_env_ptr), JNI_VERSION_1_1); |
| 88 | if (ret != JNI_OK) { |
| 89 | // TODO Different error might be better? |
| 90 | return ERR(INTERNAL); |
| 91 | } |
| 92 | JNIEnv* jni_env = *jni_env_ptr; |
| 93 | art::ScopedObjectAccess soa(jni_env); |
| 94 | art::StackHandleScope<3> hs(art::Thread::Current()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 95 | art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass))); |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 96 | *loader = soa.AddLocalReference<jobject>(hs_klass->GetClassLoader()); |
| 97 | *name = art::mirror::Class::ComputeName(hs_klass)->ToModifiedUtf8(); |
| 98 | // TODO is this always null? |
| 99 | *protection_domain = nullptr; |
| 100 | const art::DexFile& dex = hs_klass->GetDexFile(); |
| 101 | *location = dex.GetLocation(); |
| 102 | *data_len = static_cast<jint>(dex.Size()); |
| 103 | // TODO We should maybe change env->Allocate to allow us to mprotect this memory and stop writes. |
| 104 | jvmtiError alloc_error = env->Allocate(*data_len, dex_data); |
| 105 | if (alloc_error != OK) { |
| 106 | return alloc_error; |
| 107 | } |
| 108 | // Copy the data into a temporary buffer. |
| 109 | memcpy(reinterpret_cast<void*>(*dex_data), |
| 110 | reinterpret_cast<const void*>(dex.Begin()), |
| 111 | *data_len); |
| 112 | return OK; |
| 113 | } |
| 114 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 115 | } // namespace openjdkjvmti |