Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 16 | |
| 17 | #include <stdio.h> |
| 18 | |
| 19 | #include "UniquePtr.h" |
| 20 | #include "class_linker.h" |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 21 | #include "gc_map.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "mirror/abstract_method.h" |
| 23 | #include "mirror/abstract_method-inl.h" |
| 24 | #include "mirror/object_array-inl.h" |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame^] | 25 | #include "mirror/object-inl.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 26 | #include "object_utils.h" |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 27 | #include "jni.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 28 | #include "scoped_thread_state_change.h" |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 32 | #define REG(mh, reg_bitmap, reg) \ |
| 33 | ( ((reg) < mh.GetCodeItem()->registers_size_) && \ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 34 | (( *((reg_bitmap) + (reg)/8) >> ((reg) % 8) ) & 0x01) ) |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 35 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 36 | #define CHECK_REGS(...) if (!IsShadowFrame()) { \ |
| 37 | int t[] = {__VA_ARGS__}; \ |
| 38 | int t_size = sizeof(t) / sizeof(*t); \ |
| 39 | for (int i = 0; i < t_size; ++i) \ |
| 40 | CHECK(REG(mh, reg_bitmap, t[i])) << "Error: Reg " << i << " is not in RegisterMap"; \ |
| 41 | } |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 42 | |
| 43 | static int gJava_StackWalk_refmap_calls = 0; |
| 44 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 45 | struct TestReferenceMapVisitor : public StackVisitor { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 46 | explicit TestReferenceMapVisitor(Thread* thread) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 47 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 48 | : StackVisitor(thread, NULL) { |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 51 | bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | mirror::AbstractMethod* m = GetMethod(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 53 | CHECK(m != NULL); |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 54 | LOG(INFO) << "At " << PrettyMethod(m, false); |
| 55 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 56 | if (m->IsCalleeSaveMethod() || m->IsNative()) { |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 57 | LOG(WARNING) << "no PC for " << PrettyMethod(m); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 58 | CHECK_EQ(GetDexPc(), DexFile::kDexNoIndex); |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 59 | return true; |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 60 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 61 | const uint8_t* reg_bitmap = NULL; |
| 62 | if (!IsShadowFrame()) { |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 63 | NativePcOffsetToReferenceMap map(m->GetNativeGcMap()); |
| 64 | reg_bitmap = map.FindBitMap(GetNativePcOffset()); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 65 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 66 | MethodHelper mh(m); |
| 67 | StringPiece m_name(mh.GetName()); |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 68 | |
| 69 | // Given the method name and the number of times the method has been called, |
| 70 | // we know the Dex registers with live reference values. Assert that what we |
| 71 | // find is what is expected. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 72 | if (m_name == "f") { |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 73 | if (gJava_StackWalk_refmap_calls == 1) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 74 | CHECK_EQ(1U, GetDexPc()); |
Shih-wei Liao | 0839bdb | 2011-10-12 10:50:44 -0700 | [diff] [blame] | 75 | CHECK_REGS(1); |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 76 | } else { |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 77 | CHECK_EQ(gJava_StackWalk_refmap_calls, 2); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 78 | CHECK_EQ(5U, GetDexPc()); |
Shih-wei Liao | 0839bdb | 2011-10-12 10:50:44 -0700 | [diff] [blame] | 79 | CHECK_REGS(1); |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 80 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 81 | } else if (m_name == "g") { |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 82 | if (gJava_StackWalk_refmap_calls == 1) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 83 | CHECK_EQ(0xcU, GetDexPc()); |
Shih-wei Liao | 0839bdb | 2011-10-12 10:50:44 -0700 | [diff] [blame] | 84 | CHECK_REGS(0, 2); // Note that v1 is not in the minimal root set |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 85 | } else { |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 86 | CHECK_EQ(gJava_StackWalk_refmap_calls, 2); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 87 | CHECK_EQ(0xcU, GetDexPc()); |
Shih-wei Liao | 0839bdb | 2011-10-12 10:50:44 -0700 | [diff] [blame] | 88 | CHECK_REGS(0, 2); |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 89 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 90 | } else if (m_name == "shlemiel") { |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 91 | if (gJava_StackWalk_refmap_calls == 1) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 92 | CHECK_EQ(0x380U, GetDexPc()); |
Shih-wei Liao | 0839bdb | 2011-10-12 10:50:44 -0700 | [diff] [blame] | 93 | CHECK_REGS(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 25); |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 94 | } else { |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 95 | CHECK_EQ(gJava_StackWalk_refmap_calls, 2); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 96 | CHECK_EQ(0x380U, GetDexPc()); |
Shih-wei Liao | 0839bdb | 2011-10-12 10:50:44 -0700 | [diff] [blame] | 97 | CHECK_REGS(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 25); |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 100 | LOG(INFO) << reinterpret_cast<const void*>(reg_bitmap); |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 101 | |
| 102 | return true; |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 103 | } |
| 104 | }; |
| 105 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 106 | extern "C" JNIEXPORT jint JNICALL Java_StackWalk_refmap(JNIEnv*, jobject, jint count) { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 107 | ScopedObjectAccess soa(Thread::Current()); |
Shih-wei Liao | 0839bdb | 2011-10-12 10:50:44 -0700 | [diff] [blame] | 108 | CHECK_EQ(count, 0); |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 109 | gJava_StackWalk_refmap_calls++; |
| 110 | |
| 111 | // Visitor |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 112 | TestReferenceMapVisitor mapper(soa.Self()); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 113 | mapper.WalkStack(); |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 114 | |
| 115 | return count + 1; |
| 116 | } |
| 117 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 118 | extern "C" JNIEXPORT jint JNICALL Java_StackWalk2_refmap2(JNIEnv*, jobject, jint count) { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 119 | ScopedObjectAccess soa(Thread::Current()); |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 120 | gJava_StackWalk_refmap_calls++; |
| 121 | |
| 122 | // Visitor |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 123 | TestReferenceMapVisitor mapper(soa.Self()); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 124 | mapper.WalkStack(); |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 125 | |
| 126 | return count + 1; |
| 127 | } |
| 128 | |
| 129 | } |