blob: a967835277023fc4d8309c9f67b4c50325b2d3f3 [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"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08009#include "object_utils.h"
Shih-wei Liao9407c602011-09-16 10:36:43 -070010#include "jni.h"
11
12namespace art {
13
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080014#define REG(mh, reg_bitmap, reg) \
15 ( ((reg) < mh.GetCodeItem()->registers_size_) && \
Ian Rogersd81871c2011-10-03 13:57:23 -070016 (( *((reg_bitmap) + (reg)/8) >> ((reg) % 8) ) & 0x01) )
Shih-wei Liao9407c602011-09-16 10:36:43 -070017
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070018#define CHECK_REGS(...) do { \
Shih-wei Liao9407c602011-09-16 10:36:43 -070019 int t[] = {__VA_ARGS__}; \
20 int t_size = sizeof(t) / sizeof(*t); \
21 for (int i = 0; i < t_size; ++i) \
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080022 CHECK(REG(mh, reg_bitmap, t[i])) << "Error: Reg " << i << " is not in RegisterMap"; \
Shih-wei Liao9407c602011-09-16 10:36:43 -070023 } while(false)
24
25static int gJava_StackWalk_refmap_calls = 0;
26
27struct ReferenceMapVisitor : public Thread::StackVisitor {
28 ReferenceMapVisitor() {
29 }
30
31 void VisitFrame(const Frame& frame, uintptr_t pc) {
32 Method* m = frame.GetMethod();
Ian Rogersd81871c2011-10-03 13:57:23 -070033 CHECK(m != NULL);
Shih-wei Liao9407c602011-09-16 10:36:43 -070034 LOG(INFO) << "At " << PrettyMethod(m, false);
35
Ian Rogersd81871c2011-10-03 13:57:23 -070036 if (m->IsCalleeSaveMethod() || m->IsNative()) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070037 LOG(WARNING) << "no PC for " << PrettyMethod(m);
Ian Rogersd81871c2011-10-03 13:57:23 -070038 CHECK_EQ(pc, 0u);
Shih-wei Liao9407c602011-09-16 10:36:43 -070039 return;
40 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -080041 verifier::PcToReferenceMap map(m->GetGcMap(), m->GetGcMapLength());
Ian Rogersd81871c2011-10-03 13:57:23 -070042 const uint8_t* reg_bitmap = map.FindBitMap(m->ToDexPC(pc));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080043 MethodHelper mh(m);
44 StringPiece m_name(mh.GetName());
Shih-wei Liao9407c602011-09-16 10:36:43 -070045
46 // Given the method name and the number of times the method has been called,
47 // we know the Dex registers with live reference values. Assert that what we
48 // find is what is expected.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080049 if (m_name == "f") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070050 if (gJava_StackWalk_refmap_calls == 1) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070051 CHECK_EQ(1U, m->ToDexPC(pc));
52 CHECK_REGS(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070053 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070054 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070055 CHECK_EQ(5U, m->ToDexPC(pc));
56 CHECK_REGS(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070057 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080058 } else if (m_name == "g") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070059 if (gJava_StackWalk_refmap_calls == 1) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070060 CHECK_EQ(0xcU, m->ToDexPC(pc));
61 CHECK_REGS(0, 2); // Note that v1 is not in the minimal root set
Shih-wei Liao9407c602011-09-16 10:36:43 -070062 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070063 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070064 CHECK_EQ(0xcU, m->ToDexPC(pc));
65 CHECK_REGS(0, 2);
Shih-wei Liao9407c602011-09-16 10:36:43 -070066 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080067 } else if (m_name == "shlemiel") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070068 if (gJava_StackWalk_refmap_calls == 1) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070069 CHECK_EQ(0x380U, m->ToDexPC(pc));
70 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 -070071 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070072 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070073 CHECK_EQ(0x380U, m->ToDexPC(pc));
74 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 -070075 }
76 }
Elliott Hughes91250e02011-12-13 22:30:35 -080077 LOG(INFO) << reinterpret_cast<const void*>(reg_bitmap);
Shih-wei Liao9407c602011-09-16 10:36:43 -070078 }
79};
80
81extern "C"
82JNIEXPORT jint JNICALL Java_StackWalk_refmap(JNIEnv* env, jobject thisObj, jint count) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070083 CHECK_EQ(count, 0);
Shih-wei Liao9407c602011-09-16 10:36:43 -070084 gJava_StackWalk_refmap_calls++;
85
86 // Visitor
87 ReferenceMapVisitor mapper;
88 Thread::Current()->WalkStack(&mapper);
89
90 return count + 1;
91}
92
93extern "C"
Brian Carlstrom92827a52011-10-10 15:50:01 -070094JNIEXPORT jint JNICALL Java_StackWalk2_refmap2(JNIEnv* env, jobject thisObj, jint count) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070095 gJava_StackWalk_refmap_calls++;
96
97 // Visitor
98 ReferenceMapVisitor mapper;
99 Thread::Current()->WalkStack(&mapper);
100
101 return count + 1;
102}
103
104}