blob: d6ff9c6c0a46ac782eea24e4a50ae2c24559453f [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#ifndef ART_SRC_STACK_H_
18#define ART_SRC_STACK_H_
19
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080020#include "dex_file.h"
jeffhao725a9572012-11-13 18:20:12 -080021#include "instrumentation.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "base/macros.h"
Ian Rogers0399dde2012-06-06 17:09:28 -070023#include "oat/runtime/context.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070024
25#include <stdint.h>
Ian Rogers40e3bac2012-11-20 00:09:14 -080026#include <string>
Elliott Hughes68e76522011-10-05 13:22:16 -070027
28namespace art {
29
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030namespace mirror {
Mathieu Chartier66f19252012-09-18 08:57:04 -070031class AbstractMethod;
Ian Rogers0399dde2012-06-06 17:09:28 -070032class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033} // namespace mirror
34
35class Context;
Ian Rogers0399dde2012-06-06 17:09:28 -070036class ShadowFrame;
Elliott Hughes08fc03a2012-06-26 17:34:00 -070037class StackIndirectReferenceTable;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038class ScopedObjectAccess;
Elliott Hughes68e76522011-10-05 13:22:16 -070039class Thread;
40
Ian Rogers2bcb4a42012-11-08 10:39:18 -080041// The kind of vreg being accessed in calls to Set/GetVReg.
42enum VRegKind {
43 kReferenceVReg,
44 kIntVReg,
45 kFloatVReg,
46 kLongLoVReg,
47 kLongHiVReg,
48 kDoubleLoVReg,
49 kDoubleHiVReg,
50 kConstant,
51 kImpreciseConstant,
52 kUndefined,
53};
54
Mathieu Chartier67022432012-11-29 18:04:50 -080055// ShadowFrame has 3 possible layouts:
56// - portable - a unified array of VRegs and references. Precise references need GC maps.
57// - interpreter - separate VRegs and reference arrays. References are in the reference array.
58// - JNI - just VRegs, but where every VReg holds a reference.
Ian Rogers0399dde2012-06-06 17:09:28 -070059class ShadowFrame {
Elliott Hughes68e76522011-10-05 13:22:16 -070060 public:
TDYa127ce4cc0d2012-11-18 16:59:53 -080061 // Create ShadowFrame for interpreter.
62 static ShadowFrame* Create(uint32_t num_vregs, ShadowFrame* link,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 mirror::AbstractMethod* method, uint32_t dex_pc) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080064 size_t sz = sizeof(ShadowFrame) +
65 (sizeof(uint32_t) * num_vregs) +
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 (sizeof(mirror::Object*) * num_vregs);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070067 uint8_t* memory = new uint8_t[sz];
TDYa127ce4cc0d2012-11-18 16:59:53 -080068 ShadowFrame* sf = new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true);
69 return sf;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070070 }
71 ~ShadowFrame() {}
72
TDYa127ce4cc0d2012-11-18 16:59:53 -080073 bool HasReferenceArray() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -070074#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -080075 return (number_of_vregs_ & kHasReferenceArray) != 0;
Ian Rogers8a01a3a2013-05-06 13:25:44 -070076#else
77 return true;
78#endif
Ian Rogers0399dde2012-06-06 17:09:28 -070079 }
Elliott Hughes68e76522011-10-05 13:22:16 -070080
TDYa127ce4cc0d2012-11-18 16:59:53 -080081 uint32_t NumberOfVRegs() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -070082#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -080083 return number_of_vregs_ & ~kHasReferenceArray;
Ian Rogers8a01a3a2013-05-06 13:25:44 -070084#else
85 return number_of_vregs_;
86#endif
Ian Rogers0399dde2012-06-06 17:09:28 -070087 }
88
TDYa127ce4cc0d2012-11-18 16:59:53 -080089 void SetNumberOfVRegs(uint32_t number_of_vregs) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -070090#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -080091 number_of_vregs_ = number_of_vregs | (number_of_vregs_ & kHasReferenceArray);
Ian Rogers8a01a3a2013-05-06 13:25:44 -070092#else
93 UNUSED(number_of_vregs);
94 UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled";
95#endif
Ian Rogers5438ad82012-10-15 17:22:44 -070096 }
97
Ian Rogers0399dde2012-06-06 17:09:28 -070098 uint32_t GetDexPC() const {
99 return dex_pc_;
100 }
101
102 void SetDexPC(uint32_t dex_pc) {
103 dex_pc_ = dex_pc;
104 }
105
Ian Rogers0399dde2012-06-06 17:09:28 -0700106 ShadowFrame* GetLink() const {
107 return link_;
108 }
109
110 void SetLink(ShadowFrame* frame) {
111 DCHECK_NE(this, frame);
112 link_ = frame;
113 }
114
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700115 int32_t GetVReg(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800116 DCHECK_LT(i, NumberOfVRegs());
117 const uint32_t* vreg = &vregs_[i];
118 return *reinterpret_cast<const int32_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700119 }
120
121 float GetVRegFloat(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800122 DCHECK_LT(i, NumberOfVRegs());
123 // NOTE: Strict-aliasing?
124 const uint32_t* vreg = &vregs_[i];
125 return *reinterpret_cast<const float*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700126 }
127
128 int64_t GetVRegLong(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200129 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800130 const uint32_t* vreg = &vregs_[i];
131 return *reinterpret_cast<const int64_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700132 }
133
134 double GetVRegDouble(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200135 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800136 const uint32_t* vreg = &vregs_[i];
137 return *reinterpret_cast<const double*>(vreg);
138 }
139
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140 mirror::Object* GetVRegReference(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800141 DCHECK_LT(i, NumberOfVRegs());
142 if (HasReferenceArray()) {
143 return References()[i];
144 } else {
145 const uint32_t* vreg = &vregs_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146 return *reinterpret_cast<mirror::Object* const*>(vreg);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800147 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700148 }
149
Jeff Hao16743632013-05-08 10:59:04 -0700150 // Get view of vregs as range of consecutive arguments starting at i.
151 uint32_t* GetVRegArgs(size_t i) {
152 return &vregs_[i];
153 }
154
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700155 void SetVReg(size_t i, int32_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800156 DCHECK_LT(i, NumberOfVRegs());
157 uint32_t* vreg = &vregs_[i];
158 *reinterpret_cast<int32_t*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700159 }
160
161 void SetVRegFloat(size_t i, float val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800162 DCHECK_LT(i, NumberOfVRegs());
163 uint32_t* vreg = &vregs_[i];
164 *reinterpret_cast<float*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700165 }
166
167 void SetVRegLong(size_t i, int64_t val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200168 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800169 uint32_t* vreg = &vregs_[i];
170 *reinterpret_cast<int64_t*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700171 }
172
173 void SetVRegDouble(size_t i, double val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200174 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800175 uint32_t* vreg = &vregs_[i];
176 *reinterpret_cast<double*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700177 }
178
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800179 void SetVRegReference(size_t i, mirror::Object* val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800180 DCHECK_LT(i, NumberOfVRegs());
181 uint32_t* vreg = &vregs_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182 *reinterpret_cast<mirror::Object**>(vreg) = val;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800183 if (HasReferenceArray()) {
184 References()[i] = val;
185 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700186 }
187
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 mirror::AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700189 DCHECK_NE(method_, static_cast<void*>(NULL));
190 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700191 }
192
Ian Rogers62d6c772013-02-27 08:32:07 -0800193 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
194
195 ThrowLocation GetCurrentLocationForThrow() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
196
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 void SetMethod(mirror::AbstractMethod* method) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700198#if defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers0399dde2012-06-06 17:09:28 -0700199 DCHECK_NE(method, static_cast<void*>(NULL));
200 method_ = method;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700201#else
202 UNUSED(method);
203 UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled";
204#endif
Elliott Hughes68e76522011-10-05 13:22:16 -0700205 }
206
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207 bool Contains(mirror::Object** shadow_frame_entry_obj) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800208 if (HasReferenceArray()) {
209 return ((&References()[0] <= shadow_frame_entry_obj) &&
210 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
211 } else {
212 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
213 return ((&vregs_[0] <= shadow_frame_entry) &&
214 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
Ian Rogers0399dde2012-06-06 17:09:28 -0700215 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700216 }
217
Ian Rogers0399dde2012-06-06 17:09:28 -0700218 static size_t LinkOffset() {
219 return OFFSETOF_MEMBER(ShadowFrame, link_);
220 }
221
Ian Rogers0399dde2012-06-06 17:09:28 -0700222 static size_t MethodOffset() {
223 return OFFSETOF_MEMBER(ShadowFrame, method_);
224 }
225
Ian Rogers0399dde2012-06-06 17:09:28 -0700226 static size_t DexPCOffset() {
227 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
228 }
229
Ian Rogers5438ad82012-10-15 17:22:44 -0700230 static size_t NumberOfVRegsOffset() {
231 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
232 }
233
TDYa127ce4cc0d2012-11-18 16:59:53 -0800234 static size_t VRegsOffset() {
235 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
Ian Rogers5438ad82012-10-15 17:22:44 -0700236 }
237
Elliott Hughes68e76522011-10-05 13:22:16 -0700238 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, mirror::AbstractMethod* method,
240 uint32_t dex_pc, bool has_reference_array)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800241 : number_of_vregs_(num_vregs), link_(link), method_(method), dex_pc_(dex_pc) {
242 if (has_reference_array) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700243#if defined(ART_USE_PORTABLE_COMPILER)
244 CHECK_LT(num_vregs, static_cast<uint32_t>(kHasReferenceArray));
TDYa127ce4cc0d2012-11-18 16:59:53 -0800245 number_of_vregs_ |= kHasReferenceArray;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700246#endif
TDYa127ce4cc0d2012-11-18 16:59:53 -0800247 for (size_t i = 0; i < num_vregs; ++i) {
248 SetVRegReference(i, NULL);
249 }
Mathieu Chartier67022432012-11-29 18:04:50 -0800250 } else {
251 for (size_t i = 0; i < num_vregs; ++i) {
252 SetVReg(i, 0);
253 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700254 }
255 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700256
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800257 mirror::Object* const* References() const {
Mathieu Chartier67022432012-11-29 18:04:50 -0800258 DCHECK(HasReferenceArray());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800259 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800260 return reinterpret_cast<mirror::Object* const*>(vreg_end);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800261 }
262
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800263 mirror::Object** References() {
264 return const_cast<mirror::Object**>(const_cast<const ShadowFrame*>(this)->References());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800265 }
266
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700267#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800268 enum ShadowFrameFlag {
269 kHasReferenceArray = 1ul << 31
270 };
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700271 // TODO: make const in the portable case.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800272 uint32_t number_of_vregs_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700273#else
274 const uint32_t number_of_vregs_;
275#endif
Ian Rogers5438ad82012-10-15 17:22:44 -0700276 // Link to previous shadow frame or NULL.
Ian Rogers0399dde2012-06-06 17:09:28 -0700277 ShadowFrame* link_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700278#if defined(ART_USE_PORTABLE_COMPILER)
279 // TODO: make const in the portable case.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280 mirror::AbstractMethod* method_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700281#else
282 mirror::AbstractMethod* const method_;
283#endif
Ian Rogers0399dde2012-06-06 17:09:28 -0700284 uint32_t dex_pc_;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800285 uint32_t vregs_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700286
Ian Rogers0399dde2012-06-06 17:09:28 -0700287 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700288};
289
Ian Rogers0399dde2012-06-06 17:09:28 -0700290// The managed stack is used to record fragments of managed code stacks. Managed code stacks
291// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
292// necessary for transitions between code using different frame layouts and transitions into native
293// code.
Ian Rogersdf1ce912012-11-27 17:07:11 -0800294class PACKED(4) ManagedStack {
Ian Rogers0399dde2012-06-06 17:09:28 -0700295 public:
Ian Rogersca190662012-06-26 15:45:57 -0700296 ManagedStack()
297 : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700298
299 void PushManagedStackFragment(ManagedStack* fragment) {
300 // Copy this top fragment into given fragment.
301 memcpy(fragment, this, sizeof(ManagedStack));
302 // Clear this fragment, which has become the top.
303 memset(this, 0, sizeof(ManagedStack));
304 // Link our top fragment onto the given fragment.
305 link_ = fragment;
306 }
307
308 void PopManagedStackFragment(const ManagedStack& fragment) {
309 DCHECK(&fragment == link_);
310 // Copy this given fragment back to the top.
311 memcpy(this, &fragment, sizeof(ManagedStack));
312 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700313
314 ManagedStack* GetLink() const {
315 return link_;
316 }
317
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800318 mirror::AbstractMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700319 return top_quick_frame_;
320 }
321
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322 void SetTopQuickFrame(mirror::AbstractMethod** top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200323 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700324 top_quick_frame_ = top;
325 }
326
327 uintptr_t GetTopQuickFramePc() const {
328 return top_quick_frame_pc_;
329 }
330
331 void SetTopQuickFramePc(uintptr_t pc) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200332 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700333 top_quick_frame_pc_ = pc;
334 }
335
336 static size_t TopQuickFrameOffset() {
337 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
338 }
339
340 static size_t TopQuickFramePcOffset() {
341 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_);
342 }
343
344 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200345 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700346 ShadowFrame* old_frame = top_shadow_frame_;
347 top_shadow_frame_ = new_top_frame;
348 new_top_frame->SetLink(old_frame);
349 return old_frame;
350 }
351
352 ShadowFrame* PopShadowFrame() {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200353 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700354 CHECK(top_shadow_frame_ != NULL);
355 ShadowFrame* frame = top_shadow_frame_;
356 top_shadow_frame_ = frame->GetLink();
357 return frame;
358 }
359
360 ShadowFrame* GetTopShadowFrame() const {
361 return top_shadow_frame_;
362 }
363
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800364 void SetTopShadowFrame(ShadowFrame* top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200365 DCHECK(top_quick_frame_ == NULL);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800366 top_shadow_frame_ = top;
367 }
368
Ian Rogers0399dde2012-06-06 17:09:28 -0700369 static size_t TopShadowFrameOffset() {
370 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
371 }
372
TDYa127ce4cc0d2012-11-18 16:59:53 -0800373 size_t NumJniShadowFrameReferences() const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700374
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800375 bool ShadowFramesContain(mirror::Object** shadow_frame_entry) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700376
377 private:
378 ManagedStack* link_;
379 ShadowFrame* top_shadow_frame_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800380 mirror::AbstractMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700381 uintptr_t top_quick_frame_pc_;
382};
383
384class StackVisitor {
385 protected:
Ian Rogers7a22fa62013-01-23 12:16:16 -0800386 StackVisitor(Thread* thread, Context* context) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700387
388 public:
389 virtual ~StackVisitor() {}
390
391 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700392 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700393
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394 void WalkStack(bool include_transitions = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700395 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700396
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397 mirror::AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700398 if (cur_shadow_frame_ != NULL) {
399 return cur_shadow_frame_->GetMethod();
400 } else if (cur_quick_frame_ != NULL) {
401 return *cur_quick_frame_;
402 } else {
403 return NULL;
404 }
405 }
406
407 bool IsShadowFrame() const {
408 return cur_shadow_frame_ != NULL;
409 }
410
Ian Rogers0c7abda2012-09-19 13:33:42 -0700411 uint32_t GetDexPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
412
Ian Rogers62d6c772013-02-27 08:32:07 -0800413 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
414
Ian Rogers0c7abda2012-09-19 13:33:42 -0700415 size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
416
Mathieu Chartier67022432012-11-29 18:04:50 -0800417 uintptr_t* CalleeSaveAddress(int num, size_t frame_size) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700418 // Callee saves are held at the top of the frame
Mathieu Chartier67022432012-11-29 18:04:50 -0800419 DCHECK(GetMethod() != NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700420 byte* save_addr =
421 reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize);
422#if defined(__i386__)
423 save_addr -= kPointerSize; // account for return address
424#endif
Mathieu Chartier67022432012-11-29 18:04:50 -0800425 return reinterpret_cast<uintptr_t*>(save_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700426 }
427
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700428 // Returns the height of the stack in the managed stack frames, including transitions.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700429 size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800430 return GetNumFrames() - cur_depth_ - 1;
Ian Rogers0399dde2012-06-06 17:09:28 -0700431 }
432
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700433 // Returns a frame ID for JDWP use, starting from 1.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700434 size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700435 return GetFrameHeight() + 1;
436 }
437
Ian Rogersb726dcb2012-09-05 08:57:23 -0700438 size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700439 if (num_frames_ == 0) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800440 num_frames_ = ComputeNumFrames(thread_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700441 }
442 return num_frames_;
443 }
444
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800445 uint32_t GetVReg(mirror::AbstractMethod* m, uint16_t vreg, VRegKind kind) const
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800446 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700447
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800448 void SetVReg(mirror::AbstractMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700449 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700450
451 uintptr_t GetGPR(uint32_t reg) const;
Mathieu Chartier67022432012-11-29 18:04:50 -0800452 void SetGPR(uint32_t reg, uintptr_t value);
Ian Rogers0399dde2012-06-06 17:09:28 -0700453
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800454 uint32_t GetVReg(mirror::AbstractMethod** cur_quick_frame, const DexFile::CodeItem* code_item,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800455 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
456 uint16_t vreg) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700457 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700458 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
459 byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700460 return *reinterpret_cast<uint32_t*>(vreg_addr);
461 }
462
463 uintptr_t GetReturnPc() const;
464
465 void SetReturnPc(uintptr_t new_ret_pc);
466
467 /*
468 * Return sp-relative offset for a Dalvik virtual register, compiler
469 * spill or Method* in bytes using Method*.
470 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
471 * denotes Method* and (reg <= -3) denotes a compiler temp.
472 *
473 * +------------------------+
474 * | IN[ins-1] | {Note: resides in caller's frame}
475 * | . |
476 * | IN[0] |
477 * | caller's Method* |
478 * +========================+ {Note: start of callee's frame}
479 * | core callee-save spill | {variable sized}
480 * +------------------------+
481 * | fp callee-save spill |
482 * +------------------------+
483 * | filler word | {For compatibility, if V[locals-1] used as wide
484 * +------------------------+
485 * | V[locals-1] |
486 * | V[locals-2] |
487 * | . |
488 * | . | ... (reg == 2)
489 * | V[1] | ... (reg == 1)
490 * | V[0] | ... (reg == 0) <---- "locals_start"
491 * +------------------------+
492 * | Compiler temps | ... (reg == -2)
493 * | | ... (reg == -3)
494 * | | ... (reg == -4)
495 * +------------------------+
496 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
497 * +------------------------+
498 * | OUT[outs-1] |
499 * | OUT[outs-2] |
500 * | . |
501 * | OUT[0] |
502 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
503 * +========================+
504 */
505 static int GetVRegOffset(const DexFile::CodeItem* code_item,
Ian Rogersb23a7722012-10-09 16:54:26 -0700506 uint32_t core_spills, uint32_t fp_spills,
507 size_t frame_size, int reg) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700508 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
509 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler.
510 int num_ins = code_item->ins_size_;
511 int num_regs = code_item->registers_size_ - num_ins;
512 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
513 if (reg == -2) {
514 return 0; // Method*
515 } else if (reg <= -3) {
516 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp.
517 } else if (reg < num_regs) {
518 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg.
519 } else {
520 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in.
521 }
522 }
523
524 uintptr_t GetCurrentQuickFramePc() const {
525 return cur_quick_frame_pc_;
526 }
527
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800528 mirror::AbstractMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700529 return cur_quick_frame_;
530 }
531
532 ShadowFrame* GetCurrentShadowFrame() const {
533 return cur_shadow_frame_;
534 }
535
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700536 StackIndirectReferenceTable* GetCurrentSirt() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800537 mirror::AbstractMethod** sp = GetCurrentQuickFrame();
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700538 ++sp; // Skip Method*; SIRT comes next;
539 return reinterpret_cast<StackIndirectReferenceTable*>(sp);
540 }
541
Ian Rogers40e3bac2012-11-20 00:09:14 -0800542 std::string DescribeLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
543
Ian Rogers7a22fa62013-01-23 12:16:16 -0800544 static size_t ComputeNumFrames(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800545
Ian Rogers7a22fa62013-01-23 12:16:16 -0800546 static void DescribeStack(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800547
Ian Rogers0399dde2012-06-06 17:09:28 -0700548 private:
Ian Rogers0399dde2012-06-06 17:09:28 -0700549
Ian Rogers62d6c772013-02-27 08:32:07 -0800550 instrumentation::InstrumentationStackFrame GetInstrumentationStackFrame(uint32_t depth) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700551
Ian Rogersb726dcb2012-09-05 08:57:23 -0700552 void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700553
Ian Rogers7a22fa62013-01-23 12:16:16 -0800554 Thread* const thread_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700555 ShadowFrame* cur_shadow_frame_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800556 mirror::AbstractMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700557 uintptr_t cur_quick_frame_pc_;
558 // Lazily computed, number of frames in the stack.
559 size_t num_frames_;
560 // Depth of the frame we're currently at.
561 size_t cur_depth_;
562 protected:
563 Context* const context_;
564};
565
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800566class VmapTable {
567 public:
568 explicit VmapTable(const uint16_t* table) : table_(table) {
569 }
570
571 uint16_t operator[](size_t i) const {
572 return table_[i + 1];
573 }
574
575 size_t size() const {
576 return table_[0];
577 }
578
579 // Is the dex register 'vreg' in the context or on the stack? Should not be called when the
580 // 'kind' is unknown or constant.
581 bool IsInContext(size_t vreg, uint32_t& vmap_offset, VRegKind kind) const {
582 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
583 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
584 kind == kDoubleHiVReg || kind == kImpreciseConstant);
585 vmap_offset = 0xEBAD0FF5;
586 // TODO: take advantage of the registers being ordered
587 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
588 // are never promoted to floating point registers.
589 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
590 bool in_floats = false;
591 for (size_t i = 0; i < size(); ++i) {
592 // Stop if we find what we are are looking for.
593 if ((table_[i + 1] == vreg) && (in_floats == is_float)) {
594 vmap_offset = i;
595 return true;
596 }
597 // 0xffff is the marker for LR (return PC on x86), following it are spilled float registers.
598 if (table_[i + 1] == 0xffff) {
599 in_floats = true;
600 }
601 }
602 return false;
603 }
604
605 // Compute the register number that corresponds to the entry in the vmap (vmap_offset, computed
606 // by IsInContext above). If the kind is floating point then the result will be a floating point
607 // register number, otherwise it will be an integer register number.
608 uint32_t ComputeRegister(uint32_t spill_mask, uint32_t vmap_offset, VRegKind kind) const {
609 // Compute the register we need to load from the context.
610 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
611 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
612 kind == kDoubleHiVReg || kind == kImpreciseConstant);
613 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
614 // are never promoted to floating point registers.
615 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
616 uint32_t matches = 0;
617 if (is_float) {
618 while (table_[matches] != 0xffff) {
619 matches++;
620 }
621 }
622 CHECK_LT(vmap_offset - matches, static_cast<uint32_t>(__builtin_popcount(spill_mask)));
623 uint32_t spill_shifts = 0;
624 while (matches != (vmap_offset + 1)) {
625 DCHECK_NE(spill_mask, 0u);
626 matches += spill_mask & 1; // Add 1 if the low bit is set
627 spill_mask >>= 1;
628 spill_shifts++;
629 }
630 spill_shifts--; // wind back one as we want the last match
631 return spill_shifts;
632 }
633 private:
634 const uint16_t* table_;
635};
636
Elliott Hughes68e76522011-10-05 13:22:16 -0700637} // namespace art
638
639#endif // ART_SRC_STACK_H_