Andreas Gampe | 8da6d03 | 2016-10-31 19:31:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "heaps.h" |
| 18 | |
Andreas Gampe | 70bfc8a | 2016-11-03 11:04:15 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Andreas Gampe | 8da6d03 | 2016-10-31 19:31:03 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | |
Andreas Gampe | 70bfc8a | 2016-11-03 11:04:15 -0700 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
| 25 | #include "base/logging.h" |
Andreas Gampe | 8da6d03 | 2016-10-31 19:31:03 -0700 | [diff] [blame] | 26 | #include "base/macros.h" |
Andreas Gampe | 70bfc8a | 2016-11-03 11:04:15 -0700 | [diff] [blame] | 27 | #include "base/stringprintf.h" |
Andreas Gampe | 5d139fc | 2016-11-09 22:54:25 -0800 | [diff] [blame] | 28 | #include "jit/jit.h" |
Andreas Gampe | 8da6d03 | 2016-10-31 19:31:03 -0700 | [diff] [blame] | 29 | #include "jni.h" |
Andreas Gampe | 216090d | 2016-11-15 12:33:18 -0800 | [diff] [blame] | 30 | #include "native_stack_dump.h" |
Andreas Gampe | 8da6d03 | 2016-10-31 19:31:03 -0700 | [diff] [blame] | 31 | #include "openjdkjvmti/jvmti.h" |
Andreas Gampe | 5d139fc | 2016-11-09 22:54:25 -0800 | [diff] [blame] | 32 | #include "runtime.h" |
| 33 | #include "thread-inl.h" |
Andreas Gampe | 216090d | 2016-11-15 12:33:18 -0800 | [diff] [blame] | 34 | #include "thread_list.h" |
Alex Light | e657424 | 2016-08-17 09:56:24 -0700 | [diff] [blame] | 35 | |
Andreas Gampe | 70bfc8a | 2016-11-03 11:04:15 -0700 | [diff] [blame] | 36 | #include "ti-agent/common_helper.h" |
Andreas Gampe | 8da6d03 | 2016-10-31 19:31:03 -0700 | [diff] [blame] | 37 | #include "ti-agent/common_load.h" |
| 38 | |
| 39 | namespace art { |
| 40 | namespace Test913Heaps { |
| 41 | |
| 42 | extern "C" JNIEXPORT void JNICALL Java_Main_forceGarbageCollection(JNIEnv* env ATTRIBUTE_UNUSED, |
| 43 | jclass klass ATTRIBUTE_UNUSED) { |
| 44 | jvmtiError ret = jvmti_env->ForceGarbageCollection(); |
| 45 | if (ret != JVMTI_ERROR_NONE) { |
| 46 | char* err; |
| 47 | jvmti_env->GetErrorName(ret, &err); |
| 48 | printf("Error forcing a garbage collection: %s\n", err); |
| 49 | } |
| 50 | } |
| 51 | |
Andreas Gampe | 70bfc8a | 2016-11-03 11:04:15 -0700 | [diff] [blame] | 52 | class IterationConfig { |
| 53 | public: |
| 54 | IterationConfig() {} |
| 55 | virtual ~IterationConfig() {} |
| 56 | |
| 57 | virtual jint Handle(jvmtiHeapReferenceKind reference_kind, |
| 58 | const jvmtiHeapReferenceInfo* reference_info, |
| 59 | jlong class_tag, |
| 60 | jlong referrer_class_tag, |
| 61 | jlong size, |
| 62 | jlong* tag_ptr, |
| 63 | jlong* referrer_tag_ptr, |
| 64 | jint length, |
| 65 | void* user_data) = 0; |
| 66 | }; |
| 67 | |
| 68 | static jint JNICALL HeapReferenceCallback(jvmtiHeapReferenceKind reference_kind, |
| 69 | const jvmtiHeapReferenceInfo* reference_info, |
| 70 | jlong class_tag, |
| 71 | jlong referrer_class_tag, |
| 72 | jlong size, |
| 73 | jlong* tag_ptr, |
| 74 | jlong* referrer_tag_ptr, |
| 75 | jint length, |
| 76 | void* user_data) { |
| 77 | IterationConfig* config = reinterpret_cast<IterationConfig*>(user_data); |
| 78 | return config->Handle(reference_kind, |
| 79 | reference_info, |
| 80 | class_tag, |
| 81 | referrer_class_tag, |
| 82 | size, |
| 83 | tag_ptr, |
| 84 | referrer_tag_ptr, |
| 85 | length, |
| 86 | user_data); |
| 87 | } |
| 88 | |
| 89 | static bool Run(jint heap_filter, |
| 90 | jclass klass_filter, |
| 91 | jobject initial_object, |
| 92 | IterationConfig* config) { |
| 93 | jvmtiHeapCallbacks callbacks; |
| 94 | memset(&callbacks, 0, sizeof(jvmtiHeapCallbacks)); |
| 95 | callbacks.heap_reference_callback = HeapReferenceCallback; |
| 96 | |
| 97 | jvmtiError ret = jvmti_env->FollowReferences(heap_filter, |
| 98 | klass_filter, |
| 99 | initial_object, |
| 100 | &callbacks, |
| 101 | config); |
| 102 | if (ret != JVMTI_ERROR_NONE) { |
| 103 | char* err; |
| 104 | jvmti_env->GetErrorName(ret, &err); |
| 105 | printf("Failure running FollowReferences: %s\n", err); |
| 106 | return false; |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_followReferences(JNIEnv* env, |
| 112 | jclass klass ATTRIBUTE_UNUSED, |
| 113 | jint heap_filter, |
| 114 | jclass klass_filter, |
| 115 | jobject initial_object, |
| 116 | jint stop_after, |
| 117 | jint follow_set, |
| 118 | jobject jniRef) { |
| 119 | class PrintIterationConfig FINAL : public IterationConfig { |
| 120 | public: |
| 121 | PrintIterationConfig(jint _stop_after, jint _follow_set) |
| 122 | : counter_(0), |
| 123 | stop_after_(_stop_after), |
| 124 | follow_set_(_follow_set) { |
| 125 | } |
| 126 | |
| 127 | jint Handle(jvmtiHeapReferenceKind reference_kind, |
| 128 | const jvmtiHeapReferenceInfo* reference_info, |
| 129 | jlong class_tag, |
| 130 | jlong referrer_class_tag, |
| 131 | jlong size, |
| 132 | jlong* tag_ptr, |
| 133 | jlong* referrer_tag_ptr, |
| 134 | jint length, |
| 135 | void* user_data ATTRIBUTE_UNUSED) OVERRIDE { |
| 136 | jlong tag = *tag_ptr; |
| 137 | // Only check tagged objects. |
| 138 | if (tag == 0) { |
| 139 | return JVMTI_VISIT_OBJECTS; |
| 140 | } |
| 141 | |
| 142 | Print(reference_kind, |
| 143 | reference_info, |
| 144 | class_tag, |
| 145 | referrer_class_tag, |
| 146 | size, |
| 147 | tag_ptr, |
| 148 | referrer_tag_ptr, |
| 149 | length); |
| 150 | |
| 151 | counter_++; |
| 152 | if (counter_ == stop_after_) { |
| 153 | return JVMTI_VISIT_ABORT; |
| 154 | } |
| 155 | |
| 156 | if (tag > 0 && tag < 32) { |
| 157 | bool should_visit_references = (follow_set_ & (1 << static_cast<int32_t>(tag))) != 0; |
| 158 | return should_visit_references ? JVMTI_VISIT_OBJECTS : 0; |
| 159 | } |
| 160 | |
| 161 | return JVMTI_VISIT_OBJECTS; |
| 162 | } |
| 163 | |
| 164 | void Print(jvmtiHeapReferenceKind reference_kind, |
| 165 | const jvmtiHeapReferenceInfo* reference_info, |
| 166 | jlong class_tag, |
| 167 | jlong referrer_class_tag, |
| 168 | jlong size, |
| 169 | jlong* tag_ptr, |
| 170 | jlong* referrer_tag_ptr, |
| 171 | jint length) { |
| 172 | std::string referrer_str; |
| 173 | if (referrer_tag_ptr == nullptr) { |
| 174 | referrer_str = "root@root"; |
| 175 | } else { |
| 176 | referrer_str = StringPrintf("%" PRId64 "@%" PRId64, *referrer_tag_ptr, referrer_class_tag); |
| 177 | } |
| 178 | |
| 179 | jlong adapted_size = size; |
| 180 | if (*tag_ptr >= 1000) { |
| 181 | // This is a class or interface, the size of which will be dependent on the architecture. |
| 182 | // Do not print the size, but detect known values and "normalize" for the golden file. |
| 183 | if ((sizeof(void*) == 4 && size == 180) || (sizeof(void*) == 8 && size == 232)) { |
| 184 | adapted_size = 123; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | lines_.push_back( |
| 189 | StringPrintf("%s --(%s)--> %" PRId64 "@%" PRId64 " [size=%" PRId64 ", length=%d]", |
| 190 | referrer_str.c_str(), |
| 191 | GetReferenceTypeStr(reference_kind, reference_info).c_str(), |
| 192 | *tag_ptr, |
| 193 | class_tag, |
| 194 | adapted_size, |
| 195 | length)); |
Andreas Gampe | 216090d | 2016-11-15 12:33:18 -0800 | [diff] [blame] | 196 | |
| 197 | if (reference_kind == JVMTI_HEAP_REFERENCE_THREAD && *tag_ptr == 1000) { |
| 198 | DumpStacks(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | static void DumpStacks() NO_THREAD_SAFETY_ANALYSIS { |
| 203 | auto dump_function = [](art::Thread* t, void* data ATTRIBUTE_UNUSED) { |
| 204 | std::string name; |
| 205 | t->GetThreadName(name); |
| 206 | LOG(ERROR) << name; |
| 207 | art::DumpNativeStack(LOG_STREAM(ERROR), t->GetTid()); |
| 208 | }; |
| 209 | art::Runtime::Current()->GetThreadList()->ForEach(dump_function, nullptr); |
Andreas Gampe | 70bfc8a | 2016-11-03 11:04:15 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static std::string GetReferenceTypeStr(jvmtiHeapReferenceKind reference_kind, |
| 213 | const jvmtiHeapReferenceInfo* reference_info) { |
| 214 | switch (reference_kind) { |
| 215 | case JVMTI_HEAP_REFERENCE_CLASS: |
| 216 | return "class"; |
| 217 | case JVMTI_HEAP_REFERENCE_FIELD: |
| 218 | return StringPrintf("field@%d", reference_info->field.index); |
| 219 | case JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT: |
| 220 | return StringPrintf("array-element@%d", reference_info->array.index); |
| 221 | case JVMTI_HEAP_REFERENCE_CLASS_LOADER: |
| 222 | return "classloader"; |
| 223 | case JVMTI_HEAP_REFERENCE_SIGNERS: |
| 224 | return "signers"; |
| 225 | case JVMTI_HEAP_REFERENCE_PROTECTION_DOMAIN: |
| 226 | return "protection-domain"; |
| 227 | case JVMTI_HEAP_REFERENCE_INTERFACE: |
| 228 | return "interface"; |
| 229 | case JVMTI_HEAP_REFERENCE_STATIC_FIELD: |
| 230 | return StringPrintf("static-field@%d", reference_info->field.index); |
| 231 | case JVMTI_HEAP_REFERENCE_CONSTANT_POOL: |
| 232 | return "constant-pool"; |
| 233 | case JVMTI_HEAP_REFERENCE_SUPERCLASS: |
| 234 | return "superclass"; |
| 235 | case JVMTI_HEAP_REFERENCE_JNI_GLOBAL: |
| 236 | return "jni-global"; |
| 237 | case JVMTI_HEAP_REFERENCE_SYSTEM_CLASS: |
| 238 | return "system-class"; |
| 239 | case JVMTI_HEAP_REFERENCE_MONITOR: |
| 240 | return "monitor"; |
| 241 | case JVMTI_HEAP_REFERENCE_STACK_LOCAL: |
| 242 | return "stack-local"; |
| 243 | case JVMTI_HEAP_REFERENCE_JNI_LOCAL: |
| 244 | return "jni-local"; |
| 245 | case JVMTI_HEAP_REFERENCE_THREAD: |
| 246 | return "thread"; |
| 247 | case JVMTI_HEAP_REFERENCE_OTHER: |
| 248 | return "other"; |
| 249 | } |
| 250 | return "unknown"; |
| 251 | } |
| 252 | |
| 253 | const std::vector<std::string>& GetLines() const { |
| 254 | return lines_; |
| 255 | } |
| 256 | |
| 257 | private: |
| 258 | jint counter_; |
| 259 | const jint stop_after_; |
| 260 | const jint follow_set_; |
| 261 | std::vector<std::string> lines_; |
| 262 | }; |
| 263 | |
| 264 | // If jniRef isn't null, add a local and a global ref. |
| 265 | ScopedLocalRef<jobject> jni_local_ref(env, nullptr); |
| 266 | jobject jni_global_ref = nullptr; |
| 267 | if (jniRef != nullptr) { |
| 268 | jni_local_ref.reset(env->NewLocalRef(jniRef)); |
| 269 | jni_global_ref = env->NewGlobalRef(jniRef); |
| 270 | } |
| 271 | |
| 272 | PrintIterationConfig config(stop_after, follow_set); |
| 273 | Run(heap_filter, klass_filter, initial_object, &config); |
| 274 | |
| 275 | const std::vector<std::string>& lines = config.GetLines(); |
| 276 | jobjectArray ret = CreateObjectArray(env, |
| 277 | static_cast<jint>(lines.size()), |
| 278 | "java/lang/String", |
| 279 | [&](jint i) { |
| 280 | return env->NewStringUTF(lines[i].c_str()); |
| 281 | }); |
| 282 | |
| 283 | if (jni_global_ref != nullptr) { |
| 284 | env->DeleteGlobalRef(jni_global_ref); |
| 285 | } |
| 286 | |
| 287 | return ret; |
| 288 | } |
| 289 | |
Andreas Gampe | 8da6d03 | 2016-10-31 19:31:03 -0700 | [diff] [blame] | 290 | // Don't do anything |
| 291 | jint OnLoad(JavaVM* vm, |
| 292 | char* options ATTRIBUTE_UNUSED, |
| 293 | void* reserved ATTRIBUTE_UNUSED) { |
| 294 | if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) { |
| 295 | printf("Unable to get jvmti env!\n"); |
| 296 | return 1; |
| 297 | } |
Alex Light | e657424 | 2016-08-17 09:56:24 -0700 | [diff] [blame] | 298 | SetAllCapabilities(jvmti_env); |
Andreas Gampe | 8da6d03 | 2016-10-31 19:31:03 -0700 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
Andreas Gampe | 5d139fc | 2016-11-09 22:54:25 -0800 | [diff] [blame] | 302 | extern "C" JNIEXPORT void JNICALL Java_Main_waitForJitCompilation(JNIEnv*, jclass) { |
| 303 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 304 | if (jit != nullptr) { |
| 305 | jit->WaitForCompilationToFinish(Thread::Current()); |
| 306 | } |
| 307 | } |
| 308 | |
Andreas Gampe | 8da6d03 | 2016-10-31 19:31:03 -0700 | [diff] [blame] | 309 | } // namespace Test913Heaps |
| 310 | } // namespace art |