blob: 92cfa99b857f10a77654b4c323869c2fb2afda78 [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"
Ian Rogers0c7abda2012-09-19 13:33:42 -070021#include "gc_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/abstract_method.h"
23#include "mirror/abstract_method-inl.h"
24#include "mirror/object_array-inl.h"
Ian Rogers04d7aa92013-03-16 14:29:17 -070025#include "mirror/object-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080026#include "object_utils.h"
Shih-wei Liao9407c602011-09-16 10:36:43 -070027#include "jni.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070028#include "scoped_thread_state_change.h"
Shih-wei Liao9407c602011-09-16 10:36:43 -070029
30namespace art {
31
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032#define REG(mh, reg_bitmap, reg) \
33 ( ((reg) < mh.GetCodeItem()->registers_size_) && \
Ian Rogersd81871c2011-10-03 13:57:23 -070034 (( *((reg_bitmap) + (reg)/8) >> ((reg) % 8) ) & 0x01) )
Shih-wei Liao9407c602011-09-16 10:36:43 -070035
Ian Rogers0399dde2012-06-06 17:09:28 -070036#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 Liao9407c602011-09-16 10:36:43 -070042
43static int gJava_StackWalk_refmap_calls = 0;
44
Ian Rogers0399dde2012-06-06 17:09:28 -070045struct TestReferenceMapVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -080046 explicit TestReferenceMapVisitor(Thread* thread)
Ian Rogersb726dcb2012-09-05 08:57:23 -070047 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -080048 : StackVisitor(thread, NULL) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070049 }
50
Ian Rogersb726dcb2012-09-05 08:57:23 -070051 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052 mirror::AbstractMethod* m = GetMethod();
Ian Rogersd81871c2011-10-03 13:57:23 -070053 CHECK(m != NULL);
Shih-wei Liao9407c602011-09-16 10:36:43 -070054 LOG(INFO) << "At " << PrettyMethod(m, false);
55
Ian Rogersd81871c2011-10-03 13:57:23 -070056 if (m->IsCalleeSaveMethod() || m->IsNative()) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070057 LOG(WARNING) << "no PC for " << PrettyMethod(m);
Ian Rogers0399dde2012-06-06 17:09:28 -070058 CHECK_EQ(GetDexPc(), DexFile::kDexNoIndex);
Elliott Hughes530fa002012-03-12 11:44:49 -070059 return true;
Shih-wei Liao9407c602011-09-16 10:36:43 -070060 }
Ian Rogers0399dde2012-06-06 17:09:28 -070061 const uint8_t* reg_bitmap = NULL;
62 if (!IsShadowFrame()) {
Ian Rogers0c7abda2012-09-19 13:33:42 -070063 NativePcOffsetToReferenceMap map(m->GetNativeGcMap());
64 reg_bitmap = map.FindBitMap(GetNativePcOffset());
Ian Rogers0399dde2012-06-06 17:09:28 -070065 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080066 MethodHelper mh(m);
67 StringPiece m_name(mh.GetName());
Shih-wei Liao9407c602011-09-16 10:36:43 -070068
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 Rogers6d4d9fc2011-11-30 16:24:48 -080072 if (m_name == "f") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070073 if (gJava_StackWalk_refmap_calls == 1) {
Ian Rogers0399dde2012-06-06 17:09:28 -070074 CHECK_EQ(1U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070075 CHECK_REGS(1);
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);
Ian Rogers0399dde2012-06-06 17:09:28 -070078 CHECK_EQ(5U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070079 CHECK_REGS(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070080 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080081 } else if (m_name == "g") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070082 if (gJava_StackWalk_refmap_calls == 1) {
Ian Rogers0399dde2012-06-06 17:09:28 -070083 CHECK_EQ(0xcU, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070084 CHECK_REGS(0, 2); // Note that v1 is not in the minimal root set
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);
Ian Rogers0399dde2012-06-06 17:09:28 -070087 CHECK_EQ(0xcU, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070088 CHECK_REGS(0, 2);
Shih-wei Liao9407c602011-09-16 10:36:43 -070089 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080090 } else if (m_name == "shlemiel") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070091 if (gJava_StackWalk_refmap_calls == 1) {
Ian Rogers0399dde2012-06-06 17:09:28 -070092 CHECK_EQ(0x380U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070093 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 -070094 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070095 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070096 CHECK_EQ(0x380U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070097 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 -070098 }
99 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800100 LOG(INFO) << reinterpret_cast<const void*>(reg_bitmap);
Elliott Hughes530fa002012-03-12 11:44:49 -0700101
102 return true;
Shih-wei Liao9407c602011-09-16 10:36:43 -0700103 }
104};
105
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700106extern "C" JNIEXPORT jint JNICALL Java_StackWalk_refmap(JNIEnv*, jobject, jint count) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800107 ScopedObjectAccess soa(Thread::Current());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -0700108 CHECK_EQ(count, 0);
Shih-wei Liao9407c602011-09-16 10:36:43 -0700109 gJava_StackWalk_refmap_calls++;
110
111 // Visitor
Ian Rogers7a22fa62013-01-23 12:16:16 -0800112 TestReferenceMapVisitor mapper(soa.Self());
Ian Rogers0399dde2012-06-06 17:09:28 -0700113 mapper.WalkStack();
Shih-wei Liao9407c602011-09-16 10:36:43 -0700114
115 return count + 1;
116}
117
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700118extern "C" JNIEXPORT jint JNICALL Java_StackWalk2_refmap2(JNIEnv*, jobject, jint count) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800119 ScopedObjectAccess soa(Thread::Current());
Shih-wei Liao9407c602011-09-16 10:36:43 -0700120 gJava_StackWalk_refmap_calls++;
121
122 // Visitor
Ian Rogers7a22fa62013-01-23 12:16:16 -0800123 TestReferenceMapVisitor mapper(soa.Self());
Ian Rogers0399dde2012-06-06 17:09:28 -0700124 mapper.WalkStack();
Shih-wei Liao9407c602011-09-16 10:36:43 -0700125
126 return count + 1;
127}
128
129}