blob: 5e4a8a26da197c2c9f2169eeb1b9d593b1c6f6be [file] [log] [blame]
Shih-wei Liao9407c602011-09-16 10:36:43 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include <stdio.h>
4
5#include "UniquePtr.h"
6#include "class_linker.h"
Shih-wei Liao9407c602011-09-16 10:36:43 -07007#include "dex_verifier.h"
Shih-wei Liao9407c602011-09-16 10:36:43 -07008#include "object.h"
9#include "jni.h"
10
11namespace art {
12
13#define REG(method, reg_vector, reg) \
14 ( ((reg) < (method)->NumRegisters()) && \
15 (( *((reg_vector) + (reg)/8) >> ((reg) % 8) ) & 0x01) )
16
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070017#define CHECK_REGS(...) do { \
Shih-wei Liao9407c602011-09-16 10:36:43 -070018 int t[] = {__VA_ARGS__}; \
19 int t_size = sizeof(t) / sizeof(*t); \
20 for (int i = 0; i < t_size; ++i) \
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070021 CHECK(REG(m, reg_vector, t[i])) << "Error: Reg " << i << " is not in RegisterMap"; \
Shih-wei Liao9407c602011-09-16 10:36:43 -070022 } while(false)
23
24static int gJava_StackWalk_refmap_calls = 0;
25
26struct ReferenceMapVisitor : public Thread::StackVisitor {
27 ReferenceMapVisitor() {
28 }
29
30 void VisitFrame(const Frame& frame, uintptr_t pc) {
31 Method* m = frame.GetMethod();
32 if (!m ||m->IsNative()) {
33 return;
34 }
35 LOG(INFO) << "At " << PrettyMethod(m, false);
36
37 art::DexVerifier::RegisterMap* map = new art::DexVerifier::RegisterMap(
38 m->GetRegisterMapHeader(),
39 m->GetRegisterMapData());
40
41 if (!pc) {
42 // pc == NULL: m is either a native method or a phony method
43 return;
44 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -070045 if (m->IsCalleeSaveMethod()) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070046 LOG(WARNING) << "no PC for " << PrettyMethod(m);
47 return;
48 }
49
50 const uint8_t* reg_vector = art::DexVerifier::RegisterMapGetLine(map, m->ToDexPC(pc));
51 std::string m_name = m->GetName()->ToModifiedUtf8();
52
53 // Given the method name and the number of times the method has been called,
54 // we know the Dex registers with live reference values. Assert that what we
55 // find is what is expected.
56 if (m_name.compare("f") == 0) {
57 if (gJava_StackWalk_refmap_calls == 1) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070058 CHECK_EQ(1U, m->ToDexPC(pc));
59 CHECK_REGS(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070060 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070061 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070062 CHECK_EQ(5U, m->ToDexPC(pc));
63 CHECK_REGS(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070064 }
65 } else if (m_name.compare("g") == 0) {
66 if (gJava_StackWalk_refmap_calls == 1) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070067 CHECK_EQ(0xcU, m->ToDexPC(pc));
68 CHECK_REGS(0, 2); // Note that v1 is not in the minimal root set
Shih-wei Liao9407c602011-09-16 10:36:43 -070069 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070070 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070071 CHECK_EQ(0xcU, m->ToDexPC(pc));
72 CHECK_REGS(0, 2);
Shih-wei Liao9407c602011-09-16 10:36:43 -070073 }
74 } else if (m_name.compare("shlemiel") == 0) {
75 if (gJava_StackWalk_refmap_calls == 1) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070076 CHECK_EQ(0x380U, m->ToDexPC(pc));
77 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 -070078 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070079 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070080 CHECK_EQ(0x380U, m->ToDexPC(pc));
81 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 -070082 }
83 }
84
85 LOG(INFO) << reg_vector;
86 }
87};
88
89extern "C"
90JNIEXPORT jint JNICALL Java_StackWalk_refmap(JNIEnv* env, jobject thisObj, jint count) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070091 CHECK_EQ(count, 0);
Shih-wei Liao9407c602011-09-16 10:36:43 -070092 gJava_StackWalk_refmap_calls++;
93
94 // Visitor
95 ReferenceMapVisitor mapper;
96 Thread::Current()->WalkStack(&mapper);
97
98 return count + 1;
99}
100
101extern "C"
Brian Carlstrom92827a52011-10-10 15:50:01 -0700102JNIEXPORT jint JNICALL Java_StackWalk2_refmap2(JNIEnv* env, jobject thisObj, jint count) {
Shih-wei Liao9407c602011-09-16 10:36:43 -0700103 gJava_StackWalk_refmap_calls++;
104
105 // Visitor
106 ReferenceMapVisitor mapper;
107 Thread::Current()->WalkStack(&mapper);
108
109 return count + 1;
110}
111
112}