blob: 07a1cb1fb1905d9a94bc2201c8217cbe92172d28 [file] [log] [blame]
Elliott Hughes68e76522011-10-05 13:22:16 -07001/*
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 */
16
17#include "stack.h"
18
19#include "compiler.h"
Ian Rogers0399dde2012-06-06 17:09:28 -070020#include "oat/runtime/context.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070021#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080022#include "object_utils.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070023#include "thread_list.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070024
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080025namespace art {
26
Elliott Hughesbfe487b2011-10-26 15:48:55 -070027class StackGetter {
28 public:
Ian Rogers365c1022012-06-22 15:05:28 -070029 StackGetter(const ScopedJniThreadState& ts, Thread* thread)
30 : ts_(ts), thread_(thread), trace_(NULL) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -070031 }
32
33 static void Callback(void* arg) {
34 reinterpret_cast<StackGetter*>(arg)->Callback();
35 }
36
37 jobject GetTrace() {
38 return trace_;
39 }
40
41 private:
42 void Callback() {
Ian Rogers365c1022012-06-22 15:05:28 -070043 trace_ = thread_->CreateInternalStackTrace(ts_);
Elliott Hughesbfe487b2011-10-26 15:48:55 -070044 }
45
Ian Rogers365c1022012-06-22 15:05:28 -070046 const ScopedJniThreadState& ts_;
47 Thread* const thread_;
Elliott Hughesbfe487b2011-10-26 15:48:55 -070048 jobject trace_;
49};
50
Ian Rogers365c1022012-06-22 15:05:28 -070051jobject GetThreadStack(const ScopedJniThreadState& ts, Thread* thread) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -070052 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Ian Rogers365c1022012-06-22 15:05:28 -070053 StackGetter stack_getter(ts, thread);
Elliott Hughesbfe487b2011-10-26 15:48:55 -070054 thread_list->RunWhileSuspended(thread, StackGetter::Callback, &stack_getter);
55 return stack_getter.GetTrace();
56}
57
Ian Rogers0399dde2012-06-06 17:09:28 -070058void ManagedStack::PushManagedStackFragment(ManagedStack* fragment) {
59 // Copy this top fragment into given fragment.
60 memcpy(fragment, this, sizeof(ManagedStack));
61 // Clear this fragment, which has become the top.
62 memset(this, 0, sizeof(ManagedStack));
63 // Link our top fragment onto the given fragment.
64 link_ = fragment;
65}
66
67void ManagedStack::PopManagedStackFragment(const ManagedStack& fragment) {
68 DCHECK(&fragment == link_);
69 // Copy this given fragment back to the top.
70 memcpy(this, &fragment, sizeof(ManagedStack));
71}
72
73size_t ManagedStack::NumShadowFrameReferences() const {
74 size_t count = 0;
75 for (const ManagedStack* current_fragment = this; current_fragment != NULL;
76 current_fragment = current_fragment->GetLink()) {
77 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != NULL;
78 current_frame = current_frame->GetLink()) {
79 count += current_frame->NumberOfReferences();
80 }
81 }
82 return count;
83}
84
85bool ManagedStack::ShadowFramesContain(Object** shadow_frame_entry) const {
86 for (const ManagedStack* current_fragment = this; current_fragment != NULL;
87 current_fragment = current_fragment->GetLink()) {
88 for (ShadowFrame* current_frame = current_fragment->top_shadow_frame_; current_frame != NULL;
89 current_frame = current_frame->GetLink()) {
90 if (current_frame->Contains(shadow_frame_entry)) {
91 return true;
92 }
93 }
94 }
95 return false;
96}
97
98uint32_t StackVisitor::GetDexPc() const {
99 if (cur_shadow_frame_ != NULL) {
100 return cur_shadow_frame_->GetDexPC();
101 } else if (cur_quick_frame_ != NULL) {
102 return GetMethod()->ToDexPC(AdjustQuickFramePcForDexPcComputation(cur_quick_frame_pc_));
103 } else {
104 return 0;
105 }
106}
107
108uint32_t StackVisitor::GetVReg(Method* m, int vreg) const {
109 DCHECK(m == GetMethod());
110 uint32_t core_spills = m->GetCoreSpillMask();
111 const VmapTable vmap_table(m->GetVmapTableRaw());
112 uint32_t vmap_offset;
113 // TODO: IsInContext stops before spotting floating point registers.
114 if (vmap_table.IsInContext(vreg, vmap_offset)) {
115 // Compute the register we need to load from the context.
116 uint32_t spill_mask = core_spills;
117 CHECK_LT(vmap_offset, static_cast<uint32_t>(__builtin_popcount(spill_mask)));
118 uint32_t matches = 0;
119 uint32_t spill_shifts = 0;
120 while (matches != (vmap_offset + 1)) {
121 DCHECK_NE(spill_mask, 0u);
122 matches += spill_mask & 1; // Add 1 if the low bit is set.
123 spill_mask >>= 1;
124 spill_shifts++;
125 }
126 spill_shifts--; // Wind back one as we want the last match.
127 return GetGPR(spill_shifts);
128 } else {
129 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
130 DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions?
131 uint32_t fp_spills = m->GetFpSpillMask();
132 size_t frame_size = m->GetFrameSizeInBytes();
133 return GetVReg(code_item, core_spills, fp_spills, frame_size, vreg);
134 }
135}
136
137void StackVisitor::SetVReg(Method* m, int vreg, uint32_t new_value) {
138 DCHECK(m == GetMethod());
139 const VmapTable vmap_table(m->GetVmapTableRaw());
140 uint32_t vmap_offset;
141 // TODO: IsInContext stops before spotting floating point registers.
142 if (vmap_table.IsInContext(vreg, vmap_offset)) {
143 UNIMPLEMENTED(FATAL);
144 }
145 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
146 DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions?
147 uint32_t core_spills = m->GetCoreSpillMask();
148 uint32_t fp_spills = m->GetFpSpillMask();
149 size_t frame_size = m->GetFrameSizeInBytes();
150 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
151 byte* vreg_addr = reinterpret_cast<byte*>(GetCurrentQuickFrame()) + offset;
152 *reinterpret_cast<uint32_t*>(vreg_addr) = new_value;
153}
154
155uintptr_t StackVisitor::GetGPR(uint32_t reg) const {
156 return context_->GetGPR(reg);
157}
158
159uintptr_t StackVisitor::GetReturnPc() const {
160 Method** sp = GetCurrentQuickFrame();
161 CHECK(sp != NULL);
162 byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes();
163 return *reinterpret_cast<uintptr_t*>(pc_addr);
164}
165
166void StackVisitor::SetReturnPc(uintptr_t new_ret_pc) {
167 Method** sp = GetCurrentQuickFrame();
168 CHECK(sp != NULL);
169 byte* pc_addr = reinterpret_cast<byte*>(sp) + GetMethod()->GetReturnPcOffsetInBytes();
170 *reinterpret_cast<uintptr_t*>(pc_addr) = new_ret_pc;
171}
172
173size_t StackVisitor::ComputeNumFrames() const {
174 struct NumFramesVisitor : public StackVisitor {
175 explicit NumFramesVisitor(const ManagedStack* stack,
176 const std::vector<TraceStackFrame>* trace_stack) :
177 StackVisitor(stack, trace_stack), frames(0) {}
178
179 virtual bool VisitFrame() {
180 frames++;
181 return true;
182 }
183 size_t frames;
184 };
185
186 NumFramesVisitor visitor(stack_start_, trace_stack_);
187 visitor.WalkStack(true);
188 return visitor.frames;
189}
190
191void StackVisitor::SanityCheckFrame() {
192#ifndef NDEBUG
193 Method* method = GetMethod();
194 CHECK(method->GetClass() == Method::GetMethodClass() ||
195 method->GetClass() == Method::GetConstructorClass());
196 if (cur_quick_frame_ != NULL) {
197 method->AssertPcIsWithinCode(AdjustQuickFramePcForDexPcComputation(cur_quick_frame_pc_));
198 // Frame sanity.
199 size_t frame_size = method->GetFrameSizeInBytes();
200 CHECK_NE(frame_size, 0u);
201 CHECK_LT(frame_size, 1024u);
202 size_t return_pc_offset = method->GetReturnPcOffsetInBytes();
203 CHECK_LT(return_pc_offset, frame_size);
204 }
205#endif
206}
207
208void StackVisitor::WalkStack(bool include_transitions) {
209 bool method_tracing_active = Runtime::Current()->IsMethodTracingActive();
210 uint32_t trace_stack_depth = 0;
211 for (const ManagedStack* current_fragment = stack_start_; current_fragment != NULL;
212 current_fragment = current_fragment->GetLink()) {
213 cur_shadow_frame_ = current_fragment->GetTopShadowFrame();
214 cur_quick_frame_ = current_fragment->GetTopQuickFrame();
215 cur_quick_frame_pc_ = current_fragment->GetTopQuickFramePc();
216 if (cur_quick_frame_ != NULL) { // Handle quick stack frames.
217 // Can't be both a shadow and a quick fragment.
218 DCHECK(current_fragment->GetTopShadowFrame() == NULL);
219 Method* method = *cur_quick_frame_;
220 do {
221 SanityCheckFrame();
222 bool should_continue = VisitFrame();
223 if (UNLIKELY(!should_continue)) {
224 return;
225 }
226 if (context_ != NULL) {
227 context_->FillCalleeSaves(*this);
228 }
229 size_t frame_size = method->GetFrameSizeInBytes();
230 // Compute PC for next stack frame from return PC.
231 size_t return_pc_offset = method->GetReturnPcOffsetInBytes();
232 byte* return_pc_addr = reinterpret_cast<byte*>(cur_quick_frame_) + return_pc_offset;
233 uintptr_t return_pc = *reinterpret_cast<uintptr_t*>(return_pc_addr);
234 if (UNLIKELY(method_tracing_active)) {
235 // While profiling, the return pc is restored from the side stack, except when walking
236 // the stack for an exception where the side stack will be unwound in VisitFrame.
237 // TODO: stop using include_transitions as a proxy for is this the catch block visitor.
238 if (IsTraceExitPc(return_pc) && !include_transitions) {
239 // TODO: unify trace and managed stack.
240 TraceStackFrame trace_frame = GetTraceStackFrame(trace_stack_depth);
241 trace_stack_depth++;
242 CHECK(trace_frame.method_ == GetMethod()) << "Excepted: " << PrettyMethod(method)
243 << " Found: " << PrettyMethod(GetMethod());
244 return_pc = trace_frame.return_pc_;
245 }
246 }
247 cur_quick_frame_pc_ = return_pc;
248 byte* next_frame = reinterpret_cast<byte*>(cur_quick_frame_) + frame_size;
249 cur_quick_frame_ = reinterpret_cast<Method**>(next_frame);
250 cur_depth_++;
251 method = *cur_quick_frame_;
252 } while (method != NULL);
253 } else if (cur_shadow_frame_ != NULL) {
254 do {
255 SanityCheckFrame();
256 bool should_continue = VisitFrame();
257 if (UNLIKELY(!should_continue)) {
258 return;
259 }
260 cur_depth_++;
261 cur_shadow_frame_ = cur_shadow_frame_->GetLink();
262 } while(cur_shadow_frame_ != NULL);
263 }
264 cur_depth_++;
265 if (include_transitions) {
266 bool should_continue = VisitFrame();
267 if (!should_continue) {
268 return;
269 }
270 }
271 }
272}
273
Elliott Hughes68e76522011-10-05 13:22:16 -0700274} // namespace art