blob: dccd69fe0decef89fa65bc211dd0824a6744a574 [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"
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"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070025#include "scoped_thread_state_change.h"
Shih-wei Liao9407c602011-09-16 10:36:43 -070026
27namespace art {
28
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080029#define REG(mh, reg_bitmap, reg) \
30 ( ((reg) < mh.GetCodeItem()->registers_size_) && \
Ian Rogersd81871c2011-10-03 13:57:23 -070031 (( *((reg_bitmap) + (reg)/8) >> ((reg) % 8) ) & 0x01) )
Shih-wei Liao9407c602011-09-16 10:36:43 -070032
Ian Rogers0399dde2012-06-06 17:09:28 -070033#define CHECK_REGS(...) if (!IsShadowFrame()) { \
34 int t[] = {__VA_ARGS__}; \
35 int t_size = sizeof(t) / sizeof(*t); \
36 for (int i = 0; i < t_size; ++i) \
37 CHECK(REG(mh, reg_bitmap, t[i])) << "Error: Reg " << i << " is not in RegisterMap"; \
38 }
Shih-wei Liao9407c602011-09-16 10:36:43 -070039
40static int gJava_StackWalk_refmap_calls = 0;
41
Ian Rogers0399dde2012-06-06 17:09:28 -070042struct TestReferenceMapVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -080043 explicit TestReferenceMapVisitor(Thread* thread)
Ian Rogersb726dcb2012-09-05 08:57:23 -070044 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -080045 : StackVisitor(thread, NULL) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070046 }
47
Ian Rogersb726dcb2012-09-05 08:57:23 -070048 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -070049 AbstractMethod* m = GetMethod();
Ian Rogersd81871c2011-10-03 13:57:23 -070050 CHECK(m != NULL);
Shih-wei Liao9407c602011-09-16 10:36:43 -070051 LOG(INFO) << "At " << PrettyMethod(m, false);
52
Ian Rogersd81871c2011-10-03 13:57:23 -070053 if (m->IsCalleeSaveMethod() || m->IsNative()) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070054 LOG(WARNING) << "no PC for " << PrettyMethod(m);
Ian Rogers0399dde2012-06-06 17:09:28 -070055 CHECK_EQ(GetDexPc(), DexFile::kDexNoIndex);
Elliott Hughes530fa002012-03-12 11:44:49 -070056 return true;
Shih-wei Liao9407c602011-09-16 10:36:43 -070057 }
Ian Rogers0399dde2012-06-06 17:09:28 -070058 const uint8_t* reg_bitmap = NULL;
59 if (!IsShadowFrame()) {
Ian Rogers0c7abda2012-09-19 13:33:42 -070060 NativePcOffsetToReferenceMap map(m->GetNativeGcMap());
61 reg_bitmap = map.FindBitMap(GetNativePcOffset());
Ian Rogers0399dde2012-06-06 17:09:28 -070062 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080063 MethodHelper mh(m);
64 StringPiece m_name(mh.GetName());
Shih-wei Liao9407c602011-09-16 10:36:43 -070065
66 // Given the method name and the number of times the method has been called,
67 // we know the Dex registers with live reference values. Assert that what we
68 // find is what is expected.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080069 if (m_name == "f") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070070 if (gJava_StackWalk_refmap_calls == 1) {
Ian Rogers0399dde2012-06-06 17:09:28 -070071 CHECK_EQ(1U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070072 CHECK_REGS(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070073 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070074 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070075 CHECK_EQ(5U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070076 CHECK_REGS(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070077 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080078 } else if (m_name == "g") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070079 if (gJava_StackWalk_refmap_calls == 1) {
Ian Rogers0399dde2012-06-06 17:09:28 -070080 CHECK_EQ(0xcU, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070081 CHECK_REGS(0, 2); // Note that v1 is not in the minimal root set
Shih-wei Liao9407c602011-09-16 10:36:43 -070082 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070083 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070084 CHECK_EQ(0xcU, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070085 CHECK_REGS(0, 2);
Shih-wei Liao9407c602011-09-16 10:36:43 -070086 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080087 } else if (m_name == "shlemiel") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070088 if (gJava_StackWalk_refmap_calls == 1) {
Ian Rogers0399dde2012-06-06 17:09:28 -070089 CHECK_EQ(0x380U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070090 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 -070091 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070092 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070093 CHECK_EQ(0x380U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070094 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 -070095 }
96 }
Elliott Hughes91250e02011-12-13 22:30:35 -080097 LOG(INFO) << reinterpret_cast<const void*>(reg_bitmap);
Elliott Hughes530fa002012-03-12 11:44:49 -070098
99 return true;
Shih-wei Liao9407c602011-09-16 10:36:43 -0700100 }
101};
102
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700103extern "C" JNIEXPORT jint JNICALL Java_StackWalk_refmap(JNIEnv*, jobject, jint count) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800104 ScopedObjectAccess soa(Thread::Current());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -0700105 CHECK_EQ(count, 0);
Shih-wei Liao9407c602011-09-16 10:36:43 -0700106 gJava_StackWalk_refmap_calls++;
107
108 // Visitor
Ian Rogers7a22fa62013-01-23 12:16:16 -0800109 TestReferenceMapVisitor mapper(soa.Self());
Ian Rogers0399dde2012-06-06 17:09:28 -0700110 mapper.WalkStack();
Shih-wei Liao9407c602011-09-16 10:36:43 -0700111
112 return count + 1;
113}
114
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700115extern "C" JNIEXPORT jint JNICALL Java_StackWalk2_refmap2(JNIEnv*, jobject, jint count) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800116 ScopedObjectAccess soa(Thread::Current());
Shih-wei Liao9407c602011-09-16 10:36:43 -0700117 gJava_StackWalk_refmap_calls++;
118
119 // Visitor
Ian Rogers7a22fa62013-01-23 12:16:16 -0800120 TestReferenceMapVisitor mapper(soa.Self());
Ian Rogers0399dde2012-06-06 17:09:28 -0700121 mapper.WalkStack();
Shih-wei Liao9407c602011-09-16 10:36:43 -0700122
123 return count + 1;
124}
125
126}