blob: e404f6ab7dd2437a91ffb0874d9e75f70159a1f8 [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>
Ian Rogers700a4022014-05-19 16:49:03 -070018#include <memory>
Shih-wei Liao9407c602011-09-16 10:36:43 -070019
Shih-wei Liao9407c602011-09-16 10:36:43 -070020#include "class_linker.h"
Ian Rogers0c7abda2012-09-19 13:33:42 -070021#include "gc_map.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070022#include "mirror/art_method-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080023#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "mirror/object_array-inl.h"
Ian Rogers04d7aa92013-03-16 14:29:17 -070025#include "mirror/object-inl.h"
Shih-wei Liao9407c602011-09-16 10:36:43 -070026#include "jni.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070027#include "scoped_thread_state_change.h"
Shih-wei Liao9407c602011-09-16 10:36:43 -070028
29namespace art {
30
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070031#define REG(reg_bitmap, reg) \
32 (((reg) < m->GetCodeItem()->registers_size_) && \
Brian Carlstrom6f485c62013-07-18 15:35:35 -070033 ((*((reg_bitmap) + (reg)/8) >> ((reg) % 8) ) & 0x01))
Shih-wei Liao9407c602011-09-16 10:36:43 -070034
Ian Rogers0399dde2012-06-06 17:09:28 -070035#define CHECK_REGS(...) if (!IsShadowFrame()) { \
36 int t[] = {__VA_ARGS__}; \
37 int t_size = sizeof(t) / sizeof(*t); \
38 for (int i = 0; i < t_size; ++i) \
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070039 CHECK(REG(reg_bitmap, t[i])) << "Error: Reg " << i << " is not in RegisterMap"; \
Ian Rogers0399dde2012-06-06 17:09:28 -070040 }
Shih-wei Liao9407c602011-09-16 10:36:43 -070041
42static int gJava_StackWalk_refmap_calls = 0;
43
Ian Rogers0399dde2012-06-06 17:09:28 -070044struct TestReferenceMapVisitor : public StackVisitor {
Ian Rogers7a22fa62013-01-23 12:16:16 -080045 explicit TestReferenceMapVisitor(Thread* thread)
Ian Rogersb726dcb2012-09-05 08:57:23 -070046 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers7a22fa62013-01-23 12:16:16 -080047 : StackVisitor(thread, NULL) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070048 }
49
Ian Rogersb726dcb2012-09-05 08:57:23 -070050 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -070051 mirror::ArtMethod* m = GetMethod();
Ian Rogersd81871c2011-10-03 13:57:23 -070052 CHECK(m != NULL);
Shih-wei Liao9407c602011-09-16 10:36:43 -070053 LOG(INFO) << "At " << PrettyMethod(m, false);
54
Ian Rogersd81871c2011-10-03 13:57:23 -070055 if (m->IsCalleeSaveMethod() || m->IsNative()) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070056 LOG(WARNING) << "no PC for " << PrettyMethod(m);
Ian Rogers0399dde2012-06-06 17:09:28 -070057 CHECK_EQ(GetDexPc(), DexFile::kDexNoIndex);
Elliott Hughes530fa002012-03-12 11:44:49 -070058 return true;
Shih-wei Liao9407c602011-09-16 10:36:43 -070059 }
Ian Rogers0399dde2012-06-06 17:09:28 -070060 const uint8_t* reg_bitmap = NULL;
61 if (!IsShadowFrame()) {
Ian Rogers0c7abda2012-09-19 13:33:42 -070062 NativePcOffsetToReferenceMap map(m->GetNativeGcMap());
63 reg_bitmap = map.FindBitMap(GetNativePcOffset());
Ian Rogers0399dde2012-06-06 17:09:28 -070064 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070065 StringPiece m_name(m->GetName());
Shih-wei Liao9407c602011-09-16 10:36:43 -070066
67 // Given the method name and the number of times the method has been called,
68 // we know the Dex registers with live reference values. Assert that what we
69 // find is what is expected.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080070 if (m_name == "f") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070071 if (gJava_StackWalk_refmap_calls == 1) {
Ian Rogers0399dde2012-06-06 17:09:28 -070072 CHECK_EQ(1U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070073 CHECK_REGS(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070074 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070075 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070076 CHECK_EQ(5U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070077 CHECK_REGS(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070078 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080079 } else if (m_name == "g") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070080 if (gJava_StackWalk_refmap_calls == 1) {
Ian Rogers0399dde2012-06-06 17:09:28 -070081 CHECK_EQ(0xcU, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070082 CHECK_REGS(0, 2); // Note that v1 is not in the minimal root set
Shih-wei Liao9407c602011-09-16 10:36:43 -070083 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070084 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070085 CHECK_EQ(0xcU, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070086 CHECK_REGS(0, 2);
Shih-wei Liao9407c602011-09-16 10:36:43 -070087 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080088 } else if (m_name == "shlemiel") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070089 if (gJava_StackWalk_refmap_calls == 1) {
Ian Rogers0399dde2012-06-06 17:09:28 -070090 CHECK_EQ(0x380U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070091 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 -070092 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070093 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070094 CHECK_EQ(0x380U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070095 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 -070096 }
97 }
Elliott Hughes91250e02011-12-13 22:30:35 -080098 LOG(INFO) << reinterpret_cast<const void*>(reg_bitmap);
Elliott Hughes530fa002012-03-12 11:44:49 -070099
100 return true;
Shih-wei Liao9407c602011-09-16 10:36:43 -0700101 }
102};
103
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700104extern "C" JNIEXPORT jint JNICALL Java_StackWalk_refmap(JNIEnv*, jobject, jint count) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800105 ScopedObjectAccess soa(Thread::Current());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -0700106 CHECK_EQ(count, 0);
Shih-wei Liao9407c602011-09-16 10:36:43 -0700107 gJava_StackWalk_refmap_calls++;
108
109 // Visitor
Ian Rogers7a22fa62013-01-23 12:16:16 -0800110 TestReferenceMapVisitor mapper(soa.Self());
Ian Rogers0399dde2012-06-06 17:09:28 -0700111 mapper.WalkStack();
Shih-wei Liao9407c602011-09-16 10:36:43 -0700112
113 return count + 1;
114}
115
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700116extern "C" JNIEXPORT jint JNICALL Java_StackWalk2_refmap2(JNIEnv*, jobject, jint count) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800117 ScopedObjectAccess soa(Thread::Current());
Shih-wei Liao9407c602011-09-16 10:36:43 -0700118 gJava_StackWalk_refmap_calls++;
119
120 // Visitor
Ian Rogers7a22fa62013-01-23 12:16:16 -0800121 TestReferenceMapVisitor mapper(soa.Self());
Ian Rogers0399dde2012-06-06 17:09:28 -0700122 mapper.WalkStack();
Shih-wei Liao9407c602011-09-16 10:36:43 -0700123
124 return count + 1;
125}
126
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700127} // namespace art