blob: 6a23d1f7d99f3bf847ae15222784e37e43d2d702 [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
Ian Rogersd81871c2011-10-03 13:57:23 -070013#define REG(method, reg_bitmap, reg) \
Shih-wei Liao9407c602011-09-16 10:36:43 -070014 ( ((reg) < (method)->NumRegisters()) && \
Ian Rogersd81871c2011-10-03 13:57:23 -070015 (( *((reg_bitmap) + (reg)/8) >> ((reg) % 8) ) & 0x01) )
Shih-wei Liao9407c602011-09-16 10:36:43 -070016
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) \
Ian Rogersd81871c2011-10-03 13:57:23 -070021 CHECK(REG(m, reg_bitmap, 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();
Ian Rogersd81871c2011-10-03 13:57:23 -070032 CHECK(m != NULL);
Shih-wei Liao9407c602011-09-16 10:36:43 -070033 LOG(INFO) << "At " << PrettyMethod(m, false);
34
Ian Rogersd81871c2011-10-03 13:57:23 -070035 if (m->IsCalleeSaveMethod() || m->IsNative()) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070036 LOG(WARNING) << "no PC for " << PrettyMethod(m);
Ian Rogersd81871c2011-10-03 13:57:23 -070037 CHECK_EQ(pc, 0u);
Shih-wei Liao9407c602011-09-16 10:36:43 -070038 return;
39 }
Ian Rogersd81871c2011-10-03 13:57:23 -070040 verifier::PcToReferenceMap map(m);
41 const uint8_t* reg_bitmap = map.FindBitMap(m->ToDexPC(pc));
42 String* m_name = m->GetName();
Shih-wei Liao9407c602011-09-16 10:36:43 -070043
44 // Given the method name and the number of times the method has been called,
45 // we know the Dex registers with live reference values. Assert that what we
46 // find is what is expected.
Ian Rogersd81871c2011-10-03 13:57:23 -070047 if (m_name->Equals("f") == 0) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070048 if (gJava_StackWalk_refmap_calls == 1) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070049 CHECK_EQ(1U, m->ToDexPC(pc));
50 CHECK_REGS(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070051 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070052 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070053 CHECK_EQ(5U, m->ToDexPC(pc));
54 CHECK_REGS(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070055 }
Ian Rogersd81871c2011-10-03 13:57:23 -070056 } else if (m_name->Equals("g") == 0) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070057 if (gJava_StackWalk_refmap_calls == 1) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070058 CHECK_EQ(0xcU, m->ToDexPC(pc));
59 CHECK_REGS(0, 2); // Note that v1 is not in the minimal root set
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(0xcU, m->ToDexPC(pc));
63 CHECK_REGS(0, 2);
Shih-wei Liao9407c602011-09-16 10:36:43 -070064 }
Ian Rogersd81871c2011-10-03 13:57:23 -070065 } else if (m_name->Equals("shlemiel") == 0) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070066 if (gJava_StackWalk_refmap_calls == 1) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070067 CHECK_EQ(0x380U, m->ToDexPC(pc));
68 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 -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(0x380U, m->ToDexPC(pc));
72 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 -070073 }
74 }
Ian Rogersd81871c2011-10-03 13:57:23 -070075 LOG(INFO) << reg_bitmap;
Shih-wei Liao9407c602011-09-16 10:36:43 -070076 }
77};
78
79extern "C"
80JNIEXPORT jint JNICALL Java_StackWalk_refmap(JNIEnv* env, jobject thisObj, jint count) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070081 CHECK_EQ(count, 0);
Shih-wei Liao9407c602011-09-16 10:36:43 -070082 gJava_StackWalk_refmap_calls++;
83
84 // Visitor
85 ReferenceMapVisitor mapper;
86 Thread::Current()->WalkStack(&mapper);
87
88 return count + 1;
89}
90
91extern "C"
Brian Carlstrom92827a52011-10-10 15:50:01 -070092JNIEXPORT jint JNICALL Java_StackWalk2_refmap2(JNIEnv* env, jobject thisObj, jint count) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070093 gJava_StackWalk_refmap_calls++;
94
95 // Visitor
96 ReferenceMapVisitor mapper;
97 Thread::Current()->WalkStack(&mapper);
98
99 return count + 1;
100}
101
102}