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" |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 21 | #include "dex_verifier.h" |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 22 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 23 | #include "object_utils.h" |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 24 | #include "jni.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 28 | #define REG(mh, reg_bitmap, reg) \ |
| 29 | ( ((reg) < mh.GetCodeItem()->registers_size_) && \ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 30 | (( *((reg_bitmap) + (reg)/8) >> ((reg) % 8) ) & 0x01) ) |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 31 | |
Shih-wei Liao | 0839bdb | 2011-10-12 10:50:44 -0700 | [diff] [blame] | 32 | #define CHECK_REGS(...) do { \ |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 33 | int t[] = {__VA_ARGS__}; \ |
| 34 | int t_size = sizeof(t) / sizeof(*t); \ |
| 35 | for (int i = 0; i < t_size; ++i) \ |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 36 | CHECK(REG(mh, reg_bitmap, t[i])) << "Error: Reg " << i << " is not in RegisterMap"; \ |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame^] | 37 | } while (false) |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 38 | |
| 39 | static int gJava_StackWalk_refmap_calls = 0; |
| 40 | |
| 41 | struct ReferenceMapVisitor : public Thread::StackVisitor { |
| 42 | ReferenceMapVisitor() { |
| 43 | } |
| 44 | |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 45 | bool VisitFrame(const Frame& frame, uintptr_t pc) { |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 46 | Method* m = frame.GetMethod(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 47 | CHECK(m != NULL); |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 48 | LOG(INFO) << "At " << PrettyMethod(m, false); |
| 49 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 50 | if (m->IsCalleeSaveMethod() || m->IsNative()) { |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 51 | LOG(WARNING) << "no PC for " << PrettyMethod(m); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 52 | CHECK_EQ(pc, 0u); |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 53 | return true; |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 54 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 55 | verifier::PcToReferenceMap map(m->GetGcMap(), m->GetGcMapLength()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 56 | const uint8_t* reg_bitmap = map.FindBitMap(m->ToDexPC(pc)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 57 | MethodHelper mh(m); |
| 58 | StringPiece m_name(mh.GetName()); |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 59 | |
| 60 | // Given the method name and the number of times the method has been called, |
| 61 | // we know the Dex registers with live reference values. Assert that what we |
| 62 | // find is what is expected. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 63 | if (m_name == "f") { |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 64 | if (gJava_StackWalk_refmap_calls == 1) { |
Shih-wei Liao | 0839bdb | 2011-10-12 10:50:44 -0700 | [diff] [blame] | 65 | CHECK_EQ(1U, m->ToDexPC(pc)); |
| 66 | CHECK_REGS(1); |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 67 | } else { |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 68 | CHECK_EQ(gJava_StackWalk_refmap_calls, 2); |
Shih-wei Liao | 0839bdb | 2011-10-12 10:50:44 -0700 | [diff] [blame] | 69 | CHECK_EQ(5U, m->ToDexPC(pc)); |
| 70 | CHECK_REGS(1); |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 71 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 72 | } else if (m_name == "g") { |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 73 | if (gJava_StackWalk_refmap_calls == 1) { |
Shih-wei Liao | 0839bdb | 2011-10-12 10:50:44 -0700 | [diff] [blame] | 74 | CHECK_EQ(0xcU, m->ToDexPC(pc)); |
| 75 | 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] | 76 | } else { |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 77 | CHECK_EQ(gJava_StackWalk_refmap_calls, 2); |
Shih-wei Liao | 0839bdb | 2011-10-12 10:50:44 -0700 | [diff] [blame] | 78 | CHECK_EQ(0xcU, m->ToDexPC(pc)); |
| 79 | CHECK_REGS(0, 2); |
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 == "shlemiel") { |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 82 | if (gJava_StackWalk_refmap_calls == 1) { |
Shih-wei Liao | 0839bdb | 2011-10-12 10:50:44 -0700 | [diff] [blame] | 83 | CHECK_EQ(0x380U, m->ToDexPC(pc)); |
| 84 | 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] | 85 | } else { |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 86 | CHECK_EQ(gJava_StackWalk_refmap_calls, 2); |
Shih-wei Liao | 0839bdb | 2011-10-12 10:50:44 -0700 | [diff] [blame] | 87 | CHECK_EQ(0x380U, m->ToDexPC(pc)); |
| 88 | 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] | 89 | } |
| 90 | } |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 91 | LOG(INFO) << reinterpret_cast<const void*>(reg_bitmap); |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 92 | |
| 93 | return true; |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 94 | } |
| 95 | }; |
| 96 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 97 | extern "C" JNIEXPORT jint JNICALL Java_StackWalk_refmap(JNIEnv*, jobject, jint count) { |
Shih-wei Liao | 0839bdb | 2011-10-12 10:50:44 -0700 | [diff] [blame] | 98 | CHECK_EQ(count, 0); |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 99 | gJava_StackWalk_refmap_calls++; |
| 100 | |
| 101 | // Visitor |
| 102 | ReferenceMapVisitor mapper; |
| 103 | Thread::Current()->WalkStack(&mapper); |
| 104 | |
| 105 | return count + 1; |
| 106 | } |
| 107 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 108 | extern "C" JNIEXPORT jint JNICALL Java_StackWalk2_refmap2(JNIEnv*, jobject, jint count) { |
Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 109 | gJava_StackWalk_refmap_calls++; |
| 110 | |
| 111 | // Visitor |
| 112 | ReferenceMapVisitor mapper; |
| 113 | Thread::Current()->WalkStack(&mapper); |
| 114 | |
| 115 | return count + 1; |
| 116 | } |
| 117 | |
| 118 | } |