blob: 7188d30268dc2caa41bb693fcb4d6e663102ec7d [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_STACK_H_
18#define ART_RUNTIME_STACK_H_
Elliott Hughes68e76522011-10-05 13:22:16 -070019
Elliott Hughes68e76522011-10-05 13:22:16 -070020#include <stdint.h>
Ian Rogers40e3bac2012-11-20 00:09:14 -080021#include <string>
Elliott Hughes68e76522011-10-05 13:22:16 -070022
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "dex_file.h"
Mathieu Chartier12f74232015-01-14 14:55:47 -080024#include "gc_root.h"
Ian Rogerse63db272014-07-15 15:36:11 -070025#include "instruction_set.h"
26#include "mirror/object_reference.h"
27#include "throw_location.h"
28#include "utils.h"
29#include "verify_object.h"
30
Elliott Hughes68e76522011-10-05 13:22:16 -070031namespace art {
32
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070034 class ArtMethod;
35 class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036} // namespace mirror
37
38class Context;
Ian Rogers0399dde2012-06-06 17:09:28 -070039class ShadowFrame;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070040class HandleScope;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041class ScopedObjectAccess;
Elliott Hughes68e76522011-10-05 13:22:16 -070042class Thread;
43
Ian Rogers2bcb4a42012-11-08 10:39:18 -080044// The kind of vreg being accessed in calls to Set/GetVReg.
45enum VRegKind {
46 kReferenceVReg,
47 kIntVReg,
48 kFloatVReg,
49 kLongLoVReg,
50 kLongHiVReg,
51 kDoubleLoVReg,
52 kDoubleHiVReg,
53 kConstant,
54 kImpreciseConstant,
55 kUndefined,
56};
57
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -080058/**
59 * @brief Represents the virtual register numbers that denote special meaning.
60 * @details This is used to make some virtual register numbers to have specific
61 * semantic meaning. This is done so that the compiler can treat all virtual
62 * registers the same way and only special case when needed. For example,
63 * calculating SSA does not care whether a virtual register is a normal one or
64 * a compiler temporary, so it can deal with them in a consistent manner. But,
65 * for example if backend cares about temporaries because it has custom spill
66 * location, then it can special case them only then.
67 */
68enum VRegBaseRegNum : int {
69 /**
70 * @brief Virtual registers originating from dex have number >= 0.
71 */
72 kVRegBaseReg = 0,
73
74 /**
75 * @brief Invalid virtual register number.
76 */
77 kVRegInvalid = -1,
78
79 /**
80 * @brief Used to denote the base register for compiler temporaries.
81 * @details Compiler temporaries are virtual registers not originating
82 * from dex but that are created by compiler. All virtual register numbers
83 * that are <= kVRegTempBaseReg are categorized as compiler temporaries.
84 */
85 kVRegTempBaseReg = -2,
86
87 /**
88 * @brief Base register of temporary that holds the method pointer.
89 * @details This is a special compiler temporary because it has a specific
90 * location on stack.
91 */
92 kVRegMethodPtrBaseReg = kVRegTempBaseReg,
93
94 /**
95 * @brief Base register of non-special compiler temporary.
96 * @details A non-special compiler temporary is one whose spill location
97 * is flexible.
98 */
99 kVRegNonSpecialTempBaseReg = -3,
100};
101
Ian Rogersef7d42f2014-01-06 12:55:46 -0800102// A reference from the shadow stack to a MirrorType object within the Java heap.
103template<class MirrorType>
104class MANAGED StackReference : public mirror::ObjectReference<false, MirrorType> {
105 public:
106 StackReference<MirrorType>() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
107 : mirror::ObjectReference<false, MirrorType>(nullptr) {}
108
109 static StackReference<MirrorType> FromMirrorPtr(MirrorType* p)
110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
111 return StackReference<MirrorType>(p);
112 }
113
114 private:
115 StackReference<MirrorType>(MirrorType* p) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
116 : mirror::ObjectReference<false, MirrorType>(p) {}
117};
118
Mathieu Chartier67022432012-11-29 18:04:50 -0800119// ShadowFrame has 3 possible layouts:
120// - portable - a unified array of VRegs and references. Precise references need GC maps.
121// - interpreter - separate VRegs and reference arrays. References are in the reference array.
122// - JNI - just VRegs, but where every VReg holds a reference.
Ian Rogers0399dde2012-06-06 17:09:28 -0700123class ShadowFrame {
Elliott Hughes68e76522011-10-05 13:22:16 -0700124 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800125 // Compute size of ShadowFrame in bytes assuming it has a reference array.
Jeff Hao66135192013-05-14 11:02:41 -0700126 static size_t ComputeSize(uint32_t num_vregs) {
127 return sizeof(ShadowFrame) + (sizeof(uint32_t) * num_vregs) +
Ian Rogersef7d42f2014-01-06 12:55:46 -0800128 (sizeof(StackReference<mirror::Object>) * num_vregs);
Jeff Hao66135192013-05-14 11:02:41 -0700129 }
130
131 // Create ShadowFrame in heap for deoptimization.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800132 static ShadowFrame* Create(uint32_t num_vregs, ShadowFrame* link,
Brian Carlstromea46f952013-07-30 01:26:50 -0700133 mirror::ArtMethod* method, uint32_t dex_pc) {
Jeff Hao66135192013-05-14 11:02:41 -0700134 uint8_t* memory = new uint8_t[ComputeSize(num_vregs)];
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200135 return Create(num_vregs, link, method, dex_pc, memory);
Jeff Hao66135192013-05-14 11:02:41 -0700136 }
137
138 // Create ShadowFrame for interpreter using provided memory.
139 static ShadowFrame* Create(uint32_t num_vregs, ShadowFrame* link,
Brian Carlstromea46f952013-07-30 01:26:50 -0700140 mirror::ArtMethod* method, uint32_t dex_pc, void* memory) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800141 ShadowFrame* sf = new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true);
142 return sf;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700143 }
144 ~ShadowFrame() {}
145
TDYa127ce4cc0d2012-11-18 16:59:53 -0800146 bool HasReferenceArray() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700147#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800148 return (number_of_vregs_ & kHasReferenceArray) != 0;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700149#else
150 return true;
151#endif
Ian Rogers0399dde2012-06-06 17:09:28 -0700152 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700153
TDYa127ce4cc0d2012-11-18 16:59:53 -0800154 uint32_t NumberOfVRegs() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700155#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800156 return number_of_vregs_ & ~kHasReferenceArray;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700157#else
158 return number_of_vregs_;
159#endif
Ian Rogers0399dde2012-06-06 17:09:28 -0700160 }
161
TDYa127ce4cc0d2012-11-18 16:59:53 -0800162 void SetNumberOfVRegs(uint32_t number_of_vregs) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700163#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800164 number_of_vregs_ = number_of_vregs | (number_of_vregs_ & kHasReferenceArray);
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700165#else
166 UNUSED(number_of_vregs);
167 UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled";
168#endif
Ian Rogers5438ad82012-10-15 17:22:44 -0700169 }
170
Ian Rogers0399dde2012-06-06 17:09:28 -0700171 uint32_t GetDexPC() const {
172 return dex_pc_;
173 }
174
175 void SetDexPC(uint32_t dex_pc) {
176 dex_pc_ = dex_pc;
177 }
178
Ian Rogers0399dde2012-06-06 17:09:28 -0700179 ShadowFrame* GetLink() const {
180 return link_;
181 }
182
183 void SetLink(ShadowFrame* frame) {
184 DCHECK_NE(this, frame);
185 link_ = frame;
186 }
187
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700188 int32_t GetVReg(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800189 DCHECK_LT(i, NumberOfVRegs());
190 const uint32_t* vreg = &vregs_[i];
191 return *reinterpret_cast<const int32_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700192 }
193
194 float GetVRegFloat(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800195 DCHECK_LT(i, NumberOfVRegs());
196 // NOTE: Strict-aliasing?
197 const uint32_t* vreg = &vregs_[i];
198 return *reinterpret_cast<const float*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700199 }
200
201 int64_t GetVRegLong(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200202 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800203 const uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700204 // Alignment attribute required for GCC 4.8
205 typedef const int64_t unaligned_int64 __attribute__ ((aligned (4)));
206 return *reinterpret_cast<unaligned_int64*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700207 }
208
209 double GetVRegDouble(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200210 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800211 const uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700212 // Alignment attribute required for GCC 4.8
213 typedef const double unaligned_double __attribute__ ((aligned (4)));
214 return *reinterpret_cast<unaligned_double*>(vreg);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800215 }
216
Mathieu Chartier4e305412014-02-19 10:54:44 -0800217 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800218 mirror::Object* GetVRegReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800219 DCHECK_LT(i, NumberOfVRegs());
Mathieu Chartier4e305412014-02-19 10:54:44 -0800220 mirror::Object* ref;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800221 if (HasReferenceArray()) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800222 ref = References()[i].AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800223 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800224 const uint32_t* vreg_ptr = &vregs_[i];
Mathieu Chartier4e305412014-02-19 10:54:44 -0800225 ref = reinterpret_cast<const StackReference<mirror::Object>*>(vreg_ptr)->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800226 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800227 if (kVerifyFlags & kVerifyReads) {
228 VerifyObject(ref);
229 }
230 return ref;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700231 }
232
Jeff Hao16743632013-05-08 10:59:04 -0700233 // Get view of vregs as range of consecutive arguments starting at i.
234 uint32_t* GetVRegArgs(size_t i) {
235 return &vregs_[i];
236 }
237
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700238 void SetVReg(size_t i, int32_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800239 DCHECK_LT(i, NumberOfVRegs());
240 uint32_t* vreg = &vregs_[i];
241 *reinterpret_cast<int32_t*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700242 // This is needed for moving collectors since these can update the vreg references if they
243 // happen to agree with references in the reference array.
244 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800245 References()[i].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700246 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700247 }
248
249 void SetVRegFloat(size_t i, float val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800250 DCHECK_LT(i, NumberOfVRegs());
251 uint32_t* vreg = &vregs_[i];
252 *reinterpret_cast<float*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700253 // This is needed for moving collectors since these can update the vreg references if they
254 // happen to agree with references in the reference array.
255 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800256 References()[i].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700257 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700258 }
259
260 void SetVRegLong(size_t i, int64_t val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200261 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800262 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700263 // Alignment attribute required for GCC 4.8
264 typedef int64_t unaligned_int64 __attribute__ ((aligned (4)));
265 *reinterpret_cast<unaligned_int64*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700266 // This is needed for moving collectors since these can update the vreg references if they
267 // happen to agree with references in the reference array.
268 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800269 References()[i].Clear();
270 References()[i + 1].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700271 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700272 }
273
274 void SetVRegDouble(size_t i, double val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200275 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800276 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700277 // Alignment attribute required for GCC 4.8
278 typedef double unaligned_double __attribute__ ((aligned (4)));
279 *reinterpret_cast<unaligned_double*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700280 // This is needed for moving collectors since these can update the vreg references if they
281 // happen to agree with references in the reference array.
282 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800283 References()[i].Clear();
284 References()[i + 1].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700285 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700286 }
287
Mathieu Chartier4e305412014-02-19 10:54:44 -0800288 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800289 void SetVRegReference(size_t i, mirror::Object* val) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800290 DCHECK_LT(i, NumberOfVRegs());
Mathieu Chartier4e305412014-02-19 10:54:44 -0800291 if (kVerifyFlags & kVerifyWrites) {
292 VerifyObject(val);
293 }
TDYa127ce4cc0d2012-11-18 16:59:53 -0800294 uint32_t* vreg = &vregs_[i];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800295 reinterpret_cast<StackReference<mirror::Object>*>(vreg)->Assign(val);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800296 if (HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800297 References()[i].Assign(val);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800298 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700299 }
300
Ian Rogersef7d42f2014-01-06 12:55:46 -0800301 mirror::ArtMethod* GetMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
302 DCHECK(method_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700303 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700304 }
305
Hiroshi Yamauchi92d1a662014-05-15 21:43:59 -0700306 mirror::ArtMethod** GetMethodAddress() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
307 DCHECK(method_ != nullptr);
308 return &method_;
309 }
310
Ian Rogers62d6c772013-02-27 08:32:07 -0800311 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
312
Jeff Haoe701f482013-05-24 11:50:49 -0700313 mirror::Object* GetThisObject(uint16_t num_ins) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
314
Ian Rogers62d6c772013-02-27 08:32:07 -0800315 ThrowLocation GetCurrentLocationForThrow() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
316
Brian Carlstromea46f952013-07-30 01:26:50 -0700317 void SetMethod(mirror::ArtMethod* method) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700318#if defined(ART_USE_PORTABLE_COMPILER)
Ian Rogersef7d42f2014-01-06 12:55:46 -0800319 DCHECK(method != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700320 method_ = method;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700321#else
322 UNUSED(method);
323 UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled";
324#endif
Elliott Hughes68e76522011-10-05 13:22:16 -0700325 }
326
Ian Rogersef7d42f2014-01-06 12:55:46 -0800327 bool Contains(StackReference<mirror::Object>* shadow_frame_entry_obj) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800328 if (HasReferenceArray()) {
329 return ((&References()[0] <= shadow_frame_entry_obj) &&
330 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
331 } else {
332 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
333 return ((&vregs_[0] <= shadow_frame_entry) &&
334 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
Ian Rogers0399dde2012-06-06 17:09:28 -0700335 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700336 }
337
Ian Rogers0399dde2012-06-06 17:09:28 -0700338 static size_t LinkOffset() {
339 return OFFSETOF_MEMBER(ShadowFrame, link_);
340 }
341
Ian Rogers0399dde2012-06-06 17:09:28 -0700342 static size_t MethodOffset() {
343 return OFFSETOF_MEMBER(ShadowFrame, method_);
344 }
345
Ian Rogers0399dde2012-06-06 17:09:28 -0700346 static size_t DexPCOffset() {
347 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
348 }
349
Ian Rogers5438ad82012-10-15 17:22:44 -0700350 static size_t NumberOfVRegsOffset() {
351 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
352 }
353
TDYa127ce4cc0d2012-11-18 16:59:53 -0800354 static size_t VRegsOffset() {
355 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
Ian Rogers5438ad82012-10-15 17:22:44 -0700356 }
357
Elliott Hughes68e76522011-10-05 13:22:16 -0700358 private:
Brian Carlstromea46f952013-07-30 01:26:50 -0700359 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, mirror::ArtMethod* method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800360 uint32_t dex_pc, bool has_reference_array)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800361 : number_of_vregs_(num_vregs), link_(link), method_(method), dex_pc_(dex_pc) {
362 if (has_reference_array) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700363#if defined(ART_USE_PORTABLE_COMPILER)
364 CHECK_LT(num_vregs, static_cast<uint32_t>(kHasReferenceArray));
TDYa127ce4cc0d2012-11-18 16:59:53 -0800365 number_of_vregs_ |= kHasReferenceArray;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700366#endif
Ian Rogersef7d42f2014-01-06 12:55:46 -0800367 memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(StackReference<mirror::Object>)));
Mathieu Chartier67022432012-11-29 18:04:50 -0800368 } else {
Jeff Haoe701f482013-05-24 11:50:49 -0700369 memset(vregs_, 0, num_vregs * sizeof(uint32_t));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700370 }
371 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700372
Ian Rogersef7d42f2014-01-06 12:55:46 -0800373 const StackReference<mirror::Object>* References() const {
Mathieu Chartier67022432012-11-29 18:04:50 -0800374 DCHECK(HasReferenceArray());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800375 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800376 return reinterpret_cast<const StackReference<mirror::Object>*>(vreg_end);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800377 }
378
Ian Rogersef7d42f2014-01-06 12:55:46 -0800379 StackReference<mirror::Object>* References() {
380 return const_cast<StackReference<mirror::Object>*>(const_cast<const ShadowFrame*>(this)->References());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800381 }
382
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700383#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800384 enum ShadowFrameFlag {
385 kHasReferenceArray = 1ul << 31
386 };
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700387 // TODO: make const in the portable case.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800388 uint32_t number_of_vregs_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700389#else
390 const uint32_t number_of_vregs_;
391#endif
Ian Rogers5438ad82012-10-15 17:22:44 -0700392 // Link to previous shadow frame or NULL.
Ian Rogers0399dde2012-06-06 17:09:28 -0700393 ShadowFrame* link_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700394 mirror::ArtMethod* method_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700395 uint32_t dex_pc_;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800396 uint32_t vregs_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700397
Ian Rogers0399dde2012-06-06 17:09:28 -0700398 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700399};
400
Mathieu Chartier12f74232015-01-14 14:55:47 -0800401class JavaFrameRootInfo : public RootInfo {
402 public:
403 JavaFrameRootInfo(uint32_t thread_id, const StackVisitor* stack_visitor, size_t vreg)
404 : RootInfo(kRootJavaFrame, thread_id), stack_visitor_(stack_visitor), vreg_(vreg) {
405 }
406 virtual void Describe(std::ostream& os) const OVERRIDE
407 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
408
409 private:
410 const StackVisitor* const stack_visitor_;
411 const size_t vreg_;
412};
413
Ian Rogers0399dde2012-06-06 17:09:28 -0700414// The managed stack is used to record fragments of managed code stacks. Managed code stacks
415// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
416// necessary for transitions between code using different frame layouts and transitions into native
417// code.
Ian Rogersdf1ce912012-11-27 17:07:11 -0800418class PACKED(4) ManagedStack {
Ian Rogers0399dde2012-06-06 17:09:28 -0700419 public:
Ian Rogersca190662012-06-26 15:45:57 -0700420 ManagedStack()
421 : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700422
423 void PushManagedStackFragment(ManagedStack* fragment) {
424 // Copy this top fragment into given fragment.
425 memcpy(fragment, this, sizeof(ManagedStack));
426 // Clear this fragment, which has become the top.
427 memset(this, 0, sizeof(ManagedStack));
428 // Link our top fragment onto the given fragment.
429 link_ = fragment;
430 }
431
432 void PopManagedStackFragment(const ManagedStack& fragment) {
433 DCHECK(&fragment == link_);
434 // Copy this given fragment back to the top.
435 memcpy(this, &fragment, sizeof(ManagedStack));
436 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700437
438 ManagedStack* GetLink() const {
439 return link_;
440 }
441
Andreas Gampecf4035a2014-05-28 22:43:01 -0700442 StackReference<mirror::ArtMethod>* GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700443 return top_quick_frame_;
444 }
445
Andreas Gampecf4035a2014-05-28 22:43:01 -0700446 void SetTopQuickFrame(StackReference<mirror::ArtMethod>* top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200447 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700448 top_quick_frame_ = top;
449 }
450
451 uintptr_t GetTopQuickFramePc() const {
452 return top_quick_frame_pc_;
453 }
454
455 void SetTopQuickFramePc(uintptr_t pc) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200456 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700457 top_quick_frame_pc_ = pc;
458 }
459
460 static size_t TopQuickFrameOffset() {
461 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
462 }
463
464 static size_t TopQuickFramePcOffset() {
465 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_);
466 }
467
468 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200469 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700470 ShadowFrame* old_frame = top_shadow_frame_;
471 top_shadow_frame_ = new_top_frame;
472 new_top_frame->SetLink(old_frame);
473 return old_frame;
474 }
475
476 ShadowFrame* PopShadowFrame() {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200477 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700478 CHECK(top_shadow_frame_ != NULL);
479 ShadowFrame* frame = top_shadow_frame_;
480 top_shadow_frame_ = frame->GetLink();
481 return frame;
482 }
483
484 ShadowFrame* GetTopShadowFrame() const {
485 return top_shadow_frame_;
486 }
487
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800488 void SetTopShadowFrame(ShadowFrame* top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200489 DCHECK(top_quick_frame_ == NULL);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800490 top_shadow_frame_ = top;
491 }
492
Ian Rogers0399dde2012-06-06 17:09:28 -0700493 static size_t TopShadowFrameOffset() {
494 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
495 }
496
Ian Rogersef7d42f2014-01-06 12:55:46 -0800497 size_t NumJniShadowFrameReferences() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700498
Ian Rogersef7d42f2014-01-06 12:55:46 -0800499 bool ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700500
501 private:
502 ManagedStack* link_;
503 ShadowFrame* top_shadow_frame_;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700504 StackReference<mirror::ArtMethod>* top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700505 uintptr_t top_quick_frame_pc_;
506};
507
508class StackVisitor {
509 protected:
Ian Rogers7a22fa62013-01-23 12:16:16 -0800510 StackVisitor(Thread* thread, Context* context) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700511
512 public:
513 virtual ~StackVisitor() {}
514
515 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700516 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700517
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700518 void WalkStack(bool include_transitions = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700519 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700520
Ian Rogersef7d42f2014-01-06 12:55:46 -0800521 mirror::ArtMethod* GetMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
522 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700523 return cur_shadow_frame_->GetMethod();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800524 } else if (cur_quick_frame_ != nullptr) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700525 return cur_quick_frame_->AsMirrorPtr();
Hiroshi Yamauchi92d1a662014-05-15 21:43:59 -0700526 } else {
527 return nullptr;
528 }
529 }
530
Ian Rogers0399dde2012-06-06 17:09:28 -0700531 bool IsShadowFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800532 return cur_shadow_frame_ != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -0700533 }
534
Dave Allisonb373e092014-02-20 16:06:36 -0800535 uint32_t GetDexPc(bool abort_on_failure = true) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700536
Ian Rogers62d6c772013-02-27 08:32:07 -0800537 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
538
Ian Rogers0c7abda2012-09-19 13:33:42 -0700539 size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
540
Ian Rogersef7d42f2014-01-06 12:55:46 -0800541 uintptr_t* CalleeSaveAddress(int num, size_t frame_size) const
542 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700543 // Callee saves are held at the top of the frame
Ian Rogersef7d42f2014-01-06 12:55:46 -0800544 DCHECK(GetMethod() != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700545 byte* save_addr =
546 reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800547#if defined(__i386__) || defined(__x86_64__)
Ian Rogers0399dde2012-06-06 17:09:28 -0700548 save_addr -= kPointerSize; // account for return address
549#endif
Mathieu Chartier67022432012-11-29 18:04:50 -0800550 return reinterpret_cast<uintptr_t*>(save_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700551 }
552
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700553 // Returns the height of the stack in the managed stack frames, including transitions.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700554 size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800555 return GetNumFrames() - cur_depth_ - 1;
Ian Rogers0399dde2012-06-06 17:09:28 -0700556 }
557
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700558 // Returns a frame ID for JDWP use, starting from 1.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700559 size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700560 return GetFrameHeight() + 1;
561 }
562
Ian Rogersb726dcb2012-09-05 08:57:23 -0700563 size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700564 if (num_frames_ == 0) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800565 num_frames_ = ComputeNumFrames(thread_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700566 }
567 return num_frames_;
568 }
569
Hiroshi Yamauchi2e981cb2014-08-13 11:12:22 -0700570 size_t GetFrameDepth() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
571 return cur_depth_;
572 }
573
Ian Rogers5cf98192014-05-29 21:31:50 -0700574 // Get the method and dex pc immediately after the one that's currently being visited.
575 bool GetNextMethodAndDexPc(mirror::ArtMethod** next_method, uint32_t* next_dex_pc)
576 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
577
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200578 bool GetVReg(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800579 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700580
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200581 uint32_t GetVReg(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind) const
582 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
583 uint32_t val;
584 bool success = GetVReg(m, vreg, kind, &val);
585 CHECK(success) << "Failed to read vreg " << vreg << " of kind " << kind;
586 return val;
587 }
588
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200589 bool GetVRegPair(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind_lo, VRegKind kind_hi,
590 uint64_t* val) const
591 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
592
593 uint64_t GetVRegPair(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
594 VRegKind kind_hi) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
595 uint64_t val;
596 bool success = GetVRegPair(m, vreg, kind_lo, kind_hi, &val);
597 CHECK(success) << "Failed to read vreg pair " << vreg
598 << " of kind [" << kind_lo << "," << kind_hi << "]";
599 return val;
600 }
601
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200602 bool SetVReg(mirror::ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700603 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700604
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200605 bool SetVRegPair(mirror::ArtMethod* m, uint16_t vreg, uint64_t new_value,
606 VRegKind kind_lo, VRegKind kind_hi)
607 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
608
Mathieu Chartier815873e2014-02-13 18:02:13 -0800609 uintptr_t* GetGPRAddress(uint32_t reg) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700610
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700611 // This is a fast-path for getting/setting values in a quick frame.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700612 uint32_t* GetVRegAddr(StackReference<mirror::ArtMethod>* cur_quick_frame,
613 const DexFile::CodeItem* code_item,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800614 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
615 uint16_t vreg) const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000616 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg, kRuntimeISA);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700617 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
618 byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset;
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700619 return reinterpret_cast<uint32_t*>(vreg_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700620 }
621
Ian Rogersef7d42f2014-01-06 12:55:46 -0800622 uintptr_t GetReturnPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700623
Ian Rogersef7d42f2014-01-06 12:55:46 -0800624 void SetReturnPc(uintptr_t new_ret_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700625
626 /*
627 * Return sp-relative offset for a Dalvik virtual register, compiler
628 * spill or Method* in bytes using Method*.
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800629 * Note that (reg >= 0) refers to a Dalvik register, (reg == -1)
630 * denotes an invalid Dalvik register, (reg == -2) denotes Method*
631 * and (reg <= -3) denotes a compiler temporary. A compiler temporary
632 * can be thought of as a virtual register that does not exist in the
633 * dex but holds intermediate values to help optimizations and code
634 * generation. A special compiler temporary is one whose location
635 * in frame is well known while non-special ones do not have a requirement
636 * on location in frame as long as code generator itself knows how
637 * to access them.
Ian Rogers0399dde2012-06-06 17:09:28 -0700638 *
Zheng Xu511c8a62014-06-03 16:22:23 +0800639 * +---------------------------+
640 * | IN[ins-1] | {Note: resides in caller's frame}
641 * | . |
642 * | IN[0] |
643 * | caller's ArtMethod | ... StackReference<ArtMethod>
644 * +===========================+ {Note: start of callee's frame}
645 * | core callee-save spill | {variable sized}
646 * +---------------------------+
647 * | fp callee-save spill |
648 * +---------------------------+
649 * | filler word | {For compatibility, if V[locals-1] used as wide
650 * +---------------------------+
651 * | V[locals-1] |
652 * | V[locals-2] |
653 * | . |
654 * | . | ... (reg == 2)
655 * | V[1] | ... (reg == 1)
656 * | V[0] | ... (reg == 0) <---- "locals_start"
657 * +---------------------------+
658 * | Compiler temp region | ... (reg <= -3)
659 * | |
660 * | |
661 * +---------------------------+
662 * | stack alignment padding | {0 to (kStackAlignWords-1) of padding}
663 * +---------------------------+
664 * | OUT[outs-1] |
665 * | OUT[outs-2] |
666 * | . |
667 * | OUT[0] |
668 * | StackReference<ArtMethod> | ... (reg == -2) <<== sp, 16-byte aligned
669 * +===========================+
Ian Rogers0399dde2012-06-06 17:09:28 -0700670 */
671 static int GetVRegOffset(const DexFile::CodeItem* code_item,
Ian Rogersb23a7722012-10-09 16:54:26 -0700672 uint32_t core_spills, uint32_t fp_spills,
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000673 size_t frame_size, int reg, InstructionSet isa) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700674 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800675 DCHECK_NE(reg, static_cast<int>(kVRegInvalid));
Vladimir Marko81949632014-05-02 11:53:22 +0100676 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
677 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000678 + sizeof(uint32_t); // Filler.
Ian Rogers0399dde2012-06-06 17:09:28 -0700679 int num_ins = code_item->ins_size_;
680 int num_regs = code_item->registers_size_ - num_ins;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000681 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800682 if (reg == static_cast<int>(kVRegMethodPtrBaseReg)) {
683 // The current method pointer corresponds to special location on stack.
684 return 0;
685 } else if (reg <= static_cast<int>(kVRegNonSpecialTempBaseReg)) {
686 /*
687 * Special temporaries may have custom locations and the logic above deals with that.
688 * However, non-special temporaries are placed relative to the locals. Since the
689 * virtual register numbers for temporaries "grow" in negative direction, reg number
690 * will always be <= to the temp base reg. Thus, the logic ensures that the first
691 * temp is at offset -4 bytes from locals, the second is at -8 bytes from locals,
692 * and so on.
693 */
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000694 int relative_offset =
695 (reg + std::abs(static_cast<int>(kVRegNonSpecialTempBaseReg)) - 1) * sizeof(uint32_t);
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800696 return locals_start + relative_offset;
697 } else if (reg < num_regs) {
698 return locals_start + (reg * sizeof(uint32_t));
Ian Rogers0399dde2012-06-06 17:09:28 -0700699 } else {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800700 // Handle ins.
buzbee82818642014-06-04 15:35:41 -0700701 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) +
702 sizeof(StackReference<mirror::ArtMethod>);
Ian Rogers0399dde2012-06-06 17:09:28 -0700703 }
704 }
705
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000706 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
buzbee82818642014-06-04 15:35:41 -0700707 // According to stack model, the first out is above the Method referernce.
708 return sizeof(StackReference<mirror::ArtMethod>) + (out_num * sizeof(uint32_t));
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800709 }
710
Ian Rogers0399dde2012-06-06 17:09:28 -0700711 uintptr_t GetCurrentQuickFramePc() const {
712 return cur_quick_frame_pc_;
713 }
714
Andreas Gampecf4035a2014-05-28 22:43:01 -0700715 StackReference<mirror::ArtMethod>* GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700716 return cur_quick_frame_;
717 }
718
719 ShadowFrame* GetCurrentShadowFrame() const {
720 return cur_shadow_frame_;
721 }
722
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700723 HandleScope* GetCurrentHandleScope() const {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700724 StackReference<mirror::ArtMethod>* sp = GetCurrentQuickFrame();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700725 ++sp; // Skip Method*; handle scope comes next;
726 return reinterpret_cast<HandleScope*>(sp);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700727 }
728
Ian Rogers40e3bac2012-11-20 00:09:14 -0800729 std::string DescribeLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
730
Ian Rogers7a22fa62013-01-23 12:16:16 -0800731 static size_t ComputeNumFrames(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800732
Ian Rogers7a22fa62013-01-23 12:16:16 -0800733 static void DescribeStack(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800734
Ian Rogers0399dde2012-06-06 17:09:28 -0700735 private:
Ian Rogers5cf98192014-05-29 21:31:50 -0700736 // Private constructor known in the case that num_frames_ has already been computed.
737 StackVisitor(Thread* thread, Context* context, size_t num_frames)
738 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
739
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200740 bool GetGPR(uint32_t reg, uintptr_t* val) const;
741 bool SetGPR(uint32_t reg, uintptr_t value);
742 bool GetFPR(uint32_t reg, uintptr_t* val) const;
743 bool SetFPR(uint32_t reg, uintptr_t value);
744
Ian Rogersb726dcb2012-09-05 08:57:23 -0700745 void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700746
Ian Rogers7a22fa62013-01-23 12:16:16 -0800747 Thread* const thread_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700748 ShadowFrame* cur_shadow_frame_;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700749 StackReference<mirror::ArtMethod>* cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700750 uintptr_t cur_quick_frame_pc_;
751 // Lazily computed, number of frames in the stack.
752 size_t num_frames_;
753 // Depth of the frame we're currently at.
754 size_t cur_depth_;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700755
Ian Rogers0399dde2012-06-06 17:09:28 -0700756 protected:
757 Context* const context_;
758};
759
Elliott Hughes68e76522011-10-05 13:22:16 -0700760} // namespace art
761
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700762#endif // ART_RUNTIME_STACK_H_