blob: 95f38071d449c55c16928d656505a61e3b5ccd52 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Liao9407c602011-09-16 10:36:43 -070016
17#include <stdio.h>
18
19#include "UniquePtr.h"
20#include "class_linker.h"
Shih-wei Liao9407c602011-09-16 10:36:43 -070021#include "dex_verifier.h"
Shih-wei Liao9407c602011-09-16 10:36:43 -070022#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080023#include "object_utils.h"
Shih-wei Liao9407c602011-09-16 10:36:43 -070024#include "jni.h"
25
26namespace art {
27
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080028#define REG(mh, reg_bitmap, reg) \
29 ( ((reg) < mh.GetCodeItem()->registers_size_) && \
Ian Rogersd81871c2011-10-03 13:57:23 -070030 (( *((reg_bitmap) + (reg)/8) >> ((reg) % 8) ) & 0x01) )
Shih-wei Liao9407c602011-09-16 10:36:43 -070031
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070032#define CHECK_REGS(...) do { \
Shih-wei Liao9407c602011-09-16 10:36:43 -070033 int t[] = {__VA_ARGS__}; \
34 int t_size = sizeof(t) / sizeof(*t); \
35 for (int i = 0; i < t_size; ++i) \
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080036 CHECK(REG(mh, reg_bitmap, t[i])) << "Error: Reg " << i << " is not in RegisterMap"; \
Shih-wei Liao9407c602011-09-16 10:36:43 -070037 } while(false)
38
39static int gJava_StackWalk_refmap_calls = 0;
40
41struct ReferenceMapVisitor : public Thread::StackVisitor {
42 ReferenceMapVisitor() {
43 }
44
Elliott Hughes530fa002012-03-12 11:44:49 -070045 bool VisitFrame(const Frame& frame, uintptr_t pc) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070046 Method* m = frame.GetMethod();
Ian Rogersd81871c2011-10-03 13:57:23 -070047 CHECK(m != NULL);
Shih-wei Liao9407c602011-09-16 10:36:43 -070048 LOG(INFO) << "At " << PrettyMethod(m, false);
49
Ian Rogersd81871c2011-10-03 13:57:23 -070050 if (m->IsCalleeSaveMethod() || m->IsNative()) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070051 LOG(WARNING) << "no PC for " << PrettyMethod(m);
Ian Rogersd81871c2011-10-03 13:57:23 -070052 CHECK_EQ(pc, 0u);
Elliott Hughes530fa002012-03-12 11:44:49 -070053 return true;
Shih-wei Liao9407c602011-09-16 10:36:43 -070054 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -080055 verifier::PcToReferenceMap map(m->GetGcMap(), m->GetGcMapLength());
Ian Rogersd81871c2011-10-03 13:57:23 -070056 const uint8_t* reg_bitmap = map.FindBitMap(m->ToDexPC(pc));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080057 MethodHelper mh(m);
58 StringPiece m_name(mh.GetName());
Shih-wei Liao9407c602011-09-16 10:36:43 -070059
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 Rogers6d4d9fc2011-11-30 16:24:48 -080063 if (m_name == "f") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070064 if (gJava_StackWalk_refmap_calls == 1) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070065 CHECK_EQ(1U, m->ToDexPC(pc));
66 CHECK_REGS(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070067 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070068 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070069 CHECK_EQ(5U, m->ToDexPC(pc));
70 CHECK_REGS(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070071 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080072 } else if (m_name == "g") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070073 if (gJava_StackWalk_refmap_calls == 1) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070074 CHECK_EQ(0xcU, m->ToDexPC(pc));
75 CHECK_REGS(0, 2); // Note that v1 is not in the minimal root set
Shih-wei Liao9407c602011-09-16 10:36:43 -070076 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070077 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070078 CHECK_EQ(0xcU, m->ToDexPC(pc));
79 CHECK_REGS(0, 2);
Shih-wei Liao9407c602011-09-16 10:36:43 -070080 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080081 } else if (m_name == "shlemiel") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070082 if (gJava_StackWalk_refmap_calls == 1) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070083 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 Liao9407c602011-09-16 10:36:43 -070085 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070086 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070087 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 Liao9407c602011-09-16 10:36:43 -070089 }
90 }
Elliott Hughes91250e02011-12-13 22:30:35 -080091 LOG(INFO) << reinterpret_cast<const void*>(reg_bitmap);
Elliott Hughes530fa002012-03-12 11:44:49 -070092
93 return true;
Shih-wei Liao9407c602011-09-16 10:36:43 -070094 }
95};
96
Elliott Hughes1bac54f2012-03-16 12:48:31 -070097extern "C" JNIEXPORT jint JNICALL Java_StackWalk_refmap(JNIEnv*, jobject, jint count) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070098 CHECK_EQ(count, 0);
Shih-wei Liao9407c602011-09-16 10:36:43 -070099 gJava_StackWalk_refmap_calls++;
100
101 // Visitor
102 ReferenceMapVisitor mapper;
103 Thread::Current()->WalkStack(&mapper);
104
105 return count + 1;
106}
107
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700108extern "C" JNIEXPORT jint JNICALL Java_StackWalk2_refmap2(JNIEnv*, jobject, jint count) {
Shih-wei Liao9407c602011-09-16 10:36:43 -0700109 gJava_StackWalk_refmap_calls++;
110
111 // Visitor
112 ReferenceMapVisitor mapper;
113 Thread::Current()->WalkStack(&mapper);
114
115 return count + 1;
116}
117
118}