blob: 3e0566d2f0dace7c314c6bd9701a1b20a5b3a7c8 [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
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010023#include "art_code.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080024#include "arch/instruction_set.h"
Andreas Gampe03ec9302015-08-27 17:41:47 -070025#include "base/macros.h"
26#include "base/mutex.h"
Ian Rogerse63db272014-07-15 15:36:11 -070027#include "dex_file.h"
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080028#include "gc_root.h"
Ian Rogerse63db272014-07-15 15:36:11 -070029#include "mirror/object_reference.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080030#include "read_barrier.h"
Ian Rogerse63db272014-07-15 15:36:11 -070031#include "verify_object.h"
32
Elliott Hughes68e76522011-10-05 13:22:16 -070033namespace art {
34
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070036 class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037} // namespace mirror
38
Mathieu Chartiere401d142015-04-22 13:56:20 -070039class ArtMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040class Context;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070041class HandleScope;
Nicolas Geoffray57f61612015-05-15 13:20:41 +010042class InlineInfo;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070043class ScopedObjectAccess;
Nicolas Geoffray57f61612015-05-15 13:20:41 +010044class ShadowFrame;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000045class StackVisitor;
Elliott Hughes68e76522011-10-05 13:22:16 -070046class Thread;
47
Ian Rogers2bcb4a42012-11-08 10:39:18 -080048// The kind of vreg being accessed in calls to Set/GetVReg.
49enum VRegKind {
50 kReferenceVReg,
51 kIntVReg,
52 kFloatVReg,
53 kLongLoVReg,
54 kLongHiVReg,
55 kDoubleLoVReg,
56 kDoubleHiVReg,
57 kConstant,
58 kImpreciseConstant,
59 kUndefined,
60};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070061std::ostream& operator<<(std::ostream& os, const VRegKind& rhs);
Ian Rogers2bcb4a42012-11-08 10:39:18 -080062
Ian Rogersef7d42f2014-01-06 12:55:46 -080063// A reference from the shadow stack to a MirrorType object within the Java heap.
64template<class MirrorType>
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070065class MANAGED StackReference : public mirror::CompressedReference<MirrorType> {
Ian Rogersef7d42f2014-01-06 12:55:46 -080066};
67
Andreas Gampeb3025922015-09-01 14:45:00 -070068// Forward declaration. Just calls the destructor.
69struct ShadowFrameDeleter;
70using ShadowFrameAllocaUniquePtr = std::unique_ptr<ShadowFrame, ShadowFrameDeleter>;
71
Andreas Gampe03ec9302015-08-27 17:41:47 -070072// Counting locks by storing object pointers into a vector. Duplicate entries mark recursive locks.
73// The vector will be visited with the ShadowFrame during GC (so all the locked-on objects are
74// thread roots).
75// Note: implementation is split so that the call sites may be optimized to no-ops in case no
76// lock counting is necessary. The actual implementation is in the cc file to avoid
77// dependencies.
78class LockCountData {
79 public:
80 // Add the given object to the list of monitors, that is, objects that have been locked. This
81 // will not throw (but be skipped if there is an exception pending on entry).
82 template <bool kLockCounting>
83 void AddMonitor(Thread* self, mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_) {
84 DCHECK(self != nullptr);
85 if (!kLockCounting) {
86 return;
87 }
88 AddMonitorInternal(self, obj);
89 }
90
91 // Try to remove the given object from the monitor list, indicating an unlock operation.
92 // This will throw an IllegalMonitorStateException (clearing any already pending exception), in
93 // case that there wasn't a lock recorded for the object.
94 template <bool kLockCounting>
95 void RemoveMonitorOrThrow(Thread* self,
96 const mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_) {
97 DCHECK(self != nullptr);
98 if (!kLockCounting) {
99 return;
100 }
101 RemoveMonitorInternal(self, obj);
102 }
103
104 // Check whether all acquired monitors have been released. This will potentially throw an
105 // IllegalMonitorStateException, clearing any already pending exception. Returns true if the
106 // check shows that everything is OK wrt/ lock counting, false otherwise.
107 template <bool kLockCounting>
108 bool CheckAllMonitorsReleasedOrThrow(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_) {
109 DCHECK(self != nullptr);
110 if (!kLockCounting) {
111 return true;
112 }
113 return CheckAllMonitorsReleasedInternal(self);
114 }
115
116 template <typename T, typename... Args>
117 void VisitMonitors(T visitor, Args&&... args) SHARED_REQUIRES(Locks::mutator_lock_) {
118 if (monitors_ != nullptr) {
119 // Visitors may change the Object*. Be careful with the foreach loop.
120 for (mirror::Object*& obj : *monitors_) {
121 visitor(/* inout */ &obj, std::forward<Args>(args)...);
122 }
123 }
124 }
125
126 private:
127 // Internal implementations.
128 void AddMonitorInternal(Thread* self, mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_);
129 void RemoveMonitorInternal(Thread* self, const mirror::Object* obj)
130 SHARED_REQUIRES(Locks::mutator_lock_);
131 bool CheckAllMonitorsReleasedInternal(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_);
132
133 // Stores references to the locked-on objects. As noted, this should be visited during thread
134 // marking.
135 std::unique_ptr<std::vector<mirror::Object*>> monitors_;
136};
137
Elliott Hughes956af0f2014-12-11 14:34:28 -0800138// ShadowFrame has 2 possible layouts:
Mathieu Chartier67022432012-11-29 18:04:50 -0800139// - interpreter - separate VRegs and reference arrays. References are in the reference array.
140// - JNI - just VRegs, but where every VReg holds a reference.
Ian Rogers0399dde2012-06-06 17:09:28 -0700141class ShadowFrame {
Elliott Hughes68e76522011-10-05 13:22:16 -0700142 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800143 // Compute size of ShadowFrame in bytes assuming it has a reference array.
Jeff Hao66135192013-05-14 11:02:41 -0700144 static size_t ComputeSize(uint32_t num_vregs) {
145 return sizeof(ShadowFrame) + (sizeof(uint32_t) * num_vregs) +
Ian Rogersef7d42f2014-01-06 12:55:46 -0800146 (sizeof(StackReference<mirror::Object>) * num_vregs);
Jeff Hao66135192013-05-14 11:02:41 -0700147 }
148
149 // Create ShadowFrame in heap for deoptimization.
Christopher Ferris241a9582015-04-27 15:19:41 -0700150 static ShadowFrame* CreateDeoptimizedFrame(uint32_t num_vregs, ShadowFrame* link,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700151 ArtMethod* method, uint32_t dex_pc) {
Jeff Hao66135192013-05-14 11:02:41 -0700152 uint8_t* memory = new uint8_t[ComputeSize(num_vregs)];
Andreas Gampeb3025922015-09-01 14:45:00 -0700153 return CreateShadowFrameImpl(num_vregs, link, method, dex_pc, memory);
Jeff Hao66135192013-05-14 11:02:41 -0700154 }
155
Christopher Ferris241a9582015-04-27 15:19:41 -0700156 // Delete a ShadowFrame allocated on the heap for deoptimization.
157 static void DeleteDeoptimizedFrame(ShadowFrame* sf) {
Andreas Gampeb3025922015-09-01 14:45:00 -0700158 sf->~ShadowFrame(); // Explicitly destruct.
Christopher Ferris241a9582015-04-27 15:19:41 -0700159 uint8_t* memory = reinterpret_cast<uint8_t*>(sf);
160 delete[] memory;
161 }
162
Andreas Gampeb3025922015-09-01 14:45:00 -0700163 // Create a shadow frame in a fresh alloca. This needs to be in the context of the caller.
164 // Inlining doesn't work, the compiler will still undo the alloca. So this needs to be a macro.
165#define CREATE_SHADOW_FRAME(num_vregs, link, method, dex_pc) ({ \
166 size_t frame_size = ShadowFrame::ComputeSize(num_vregs); \
167 void* alloca_mem = alloca(frame_size); \
168 ShadowFrameAllocaUniquePtr( \
169 ShadowFrame::CreateShadowFrameImpl((num_vregs), (link), (method), (dex_pc), \
170 (alloca_mem))); \
171 })
172
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700173 ~ShadowFrame() {}
174
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700175 // TODO(iam): Clean references array up since they're always there,
176 // we don't need to do conditionals.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800177 bool HasReferenceArray() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700178 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700179 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700180
TDYa127ce4cc0d2012-11-18 16:59:53 -0800181 uint32_t NumberOfVRegs() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700182 return number_of_vregs_;
Ian Rogers5438ad82012-10-15 17:22:44 -0700183 }
184
Ian Rogers0399dde2012-06-06 17:09:28 -0700185 uint32_t GetDexPC() const {
186 return dex_pc_;
187 }
188
189 void SetDexPC(uint32_t dex_pc) {
190 dex_pc_ = dex_pc;
191 }
192
Ian Rogers0399dde2012-06-06 17:09:28 -0700193 ShadowFrame* GetLink() const {
194 return link_;
195 }
196
197 void SetLink(ShadowFrame* frame) {
198 DCHECK_NE(this, frame);
199 link_ = frame;
200 }
201
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700202 int32_t GetVReg(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800203 DCHECK_LT(i, NumberOfVRegs());
204 const uint32_t* vreg = &vregs_[i];
205 return *reinterpret_cast<const int32_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700206 }
207
208 float GetVRegFloat(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800209 DCHECK_LT(i, NumberOfVRegs());
210 // NOTE: Strict-aliasing?
211 const uint32_t* vreg = &vregs_[i];
212 return *reinterpret_cast<const float*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700213 }
214
215 int64_t GetVRegLong(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200216 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800217 const uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700218 // Alignment attribute required for GCC 4.8
219 typedef const int64_t unaligned_int64 __attribute__ ((aligned (4)));
220 return *reinterpret_cast<unaligned_int64*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700221 }
222
223 double GetVRegDouble(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200224 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800225 const uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700226 // Alignment attribute required for GCC 4.8
227 typedef const double unaligned_double __attribute__ ((aligned (4)));
228 return *reinterpret_cast<unaligned_double*>(vreg);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800229 }
230
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700231 // Look up the reference given its virtual register number.
232 // If this returns non-null then this does not mean the vreg is currently a reference
233 // on non-moving collectors. Check that the raw reg with GetVReg is equal to this if not certain.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800234 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Mathieu Chartier90443472015-07-16 20:32:27 -0700235 mirror::Object* GetVRegReference(size_t i) const SHARED_REQUIRES(Locks::mutator_lock_) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800236 DCHECK_LT(i, NumberOfVRegs());
Mathieu Chartier4e305412014-02-19 10:54:44 -0800237 mirror::Object* ref;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800238 if (HasReferenceArray()) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800239 ref = References()[i].AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800240 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800241 const uint32_t* vreg_ptr = &vregs_[i];
Mathieu Chartier4e305412014-02-19 10:54:44 -0800242 ref = reinterpret_cast<const StackReference<mirror::Object>*>(vreg_ptr)->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800243 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800244 if (kUseReadBarrier) {
245 ReadBarrier::AssertToSpaceInvariant(ref);
246 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800247 if (kVerifyFlags & kVerifyReads) {
248 VerifyObject(ref);
249 }
250 return ref;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700251 }
252
Jeff Hao16743632013-05-08 10:59:04 -0700253 // Get view of vregs as range of consecutive arguments starting at i.
254 uint32_t* GetVRegArgs(size_t i) {
255 return &vregs_[i];
256 }
257
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700258 void SetVReg(size_t i, int32_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800259 DCHECK_LT(i, NumberOfVRegs());
260 uint32_t* vreg = &vregs_[i];
261 *reinterpret_cast<int32_t*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700262 // This is needed for moving collectors since these can update the vreg references if they
263 // happen to agree with references in the reference array.
264 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800265 References()[i].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700266 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700267 }
268
269 void SetVRegFloat(size_t i, float val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800270 DCHECK_LT(i, NumberOfVRegs());
271 uint32_t* vreg = &vregs_[i];
272 *reinterpret_cast<float*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700273 // This is needed for moving collectors since these can update the vreg references if they
274 // happen to agree with references in the reference array.
275 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800276 References()[i].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700277 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700278 }
279
280 void SetVRegLong(size_t i, int64_t val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200281 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800282 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700283 // Alignment attribute required for GCC 4.8
284 typedef int64_t unaligned_int64 __attribute__ ((aligned (4)));
285 *reinterpret_cast<unaligned_int64*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700286 // This is needed for moving collectors since these can update the vreg references if they
287 // happen to agree with references in the reference array.
288 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800289 References()[i].Clear();
290 References()[i + 1].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700291 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700292 }
293
294 void SetVRegDouble(size_t i, double val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200295 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800296 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700297 // Alignment attribute required for GCC 4.8
298 typedef double unaligned_double __attribute__ ((aligned (4)));
299 *reinterpret_cast<unaligned_double*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700300 // This is needed for moving collectors since these can update the vreg references if they
301 // happen to agree with references in the reference array.
302 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800303 References()[i].Clear();
304 References()[i + 1].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700305 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700306 }
307
Mathieu Chartier4e305412014-02-19 10:54:44 -0800308 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Mathieu Chartier90443472015-07-16 20:32:27 -0700309 void SetVRegReference(size_t i, mirror::Object* val) SHARED_REQUIRES(Locks::mutator_lock_) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800310 DCHECK_LT(i, NumberOfVRegs());
Mathieu Chartier4e305412014-02-19 10:54:44 -0800311 if (kVerifyFlags & kVerifyWrites) {
312 VerifyObject(val);
313 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800314 if (kUseReadBarrier) {
315 ReadBarrier::AssertToSpaceInvariant(val);
316 }
TDYa127ce4cc0d2012-11-18 16:59:53 -0800317 uint32_t* vreg = &vregs_[i];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800318 reinterpret_cast<StackReference<mirror::Object>*>(vreg)->Assign(val);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800319 if (HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800320 References()[i].Assign(val);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800321 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700322 }
323
Mathieu Chartier90443472015-07-16 20:32:27 -0700324 ArtMethod* GetMethod() const SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800325 DCHECK(method_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700326 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700327 }
328
Mathieu Chartier90443472015-07-16 20:32:27 -0700329 mirror::Object* GetThisObject() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800330
Mathieu Chartier90443472015-07-16 20:32:27 -0700331 mirror::Object* GetThisObject(uint16_t num_ins) const SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Haoe701f482013-05-24 11:50:49 -0700332
Ian Rogersef7d42f2014-01-06 12:55:46 -0800333 bool Contains(StackReference<mirror::Object>* shadow_frame_entry_obj) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800334 if (HasReferenceArray()) {
335 return ((&References()[0] <= shadow_frame_entry_obj) &&
336 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
337 } else {
338 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
339 return ((&vregs_[0] <= shadow_frame_entry) &&
340 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
Ian Rogers0399dde2012-06-06 17:09:28 -0700341 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700342 }
343
Andreas Gampe03ec9302015-08-27 17:41:47 -0700344 LockCountData& GetLockCountData() {
345 return lock_count_data_;
346 }
347
Ian Rogers0399dde2012-06-06 17:09:28 -0700348 static size_t LinkOffset() {
349 return OFFSETOF_MEMBER(ShadowFrame, link_);
350 }
351
Ian Rogers0399dde2012-06-06 17:09:28 -0700352 static size_t MethodOffset() {
353 return OFFSETOF_MEMBER(ShadowFrame, method_);
354 }
355
Ian Rogers0399dde2012-06-06 17:09:28 -0700356 static size_t DexPCOffset() {
357 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
358 }
359
Ian Rogers5438ad82012-10-15 17:22:44 -0700360 static size_t NumberOfVRegsOffset() {
361 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
362 }
363
TDYa127ce4cc0d2012-11-18 16:59:53 -0800364 static size_t VRegsOffset() {
365 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
Ian Rogers5438ad82012-10-15 17:22:44 -0700366 }
367
Andreas Gampeb3025922015-09-01 14:45:00 -0700368 // Create ShadowFrame for interpreter using provided memory.
369 static ShadowFrame* CreateShadowFrameImpl(uint32_t num_vregs,
370 ShadowFrame* link,
371 ArtMethod* method,
372 uint32_t dex_pc,
373 void* memory) {
374 return new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true);
375 }
376
Elliott Hughes68e76522011-10-05 13:22:16 -0700377 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700378 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, ArtMethod* method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800379 uint32_t dex_pc, bool has_reference_array)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800380 : number_of_vregs_(num_vregs), link_(link), method_(method), dex_pc_(dex_pc) {
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700381 // TODO(iam): Remove this parameter, it's an an artifact of portable removal
382 DCHECK(has_reference_array);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800383 if (has_reference_array) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800384 memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(StackReference<mirror::Object>)));
Mathieu Chartier67022432012-11-29 18:04:50 -0800385 } else {
Jeff Haoe701f482013-05-24 11:50:49 -0700386 memset(vregs_, 0, num_vregs * sizeof(uint32_t));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700387 }
388 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700389
Ian Rogersef7d42f2014-01-06 12:55:46 -0800390 const StackReference<mirror::Object>* References() const {
Mathieu Chartier67022432012-11-29 18:04:50 -0800391 DCHECK(HasReferenceArray());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800392 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800393 return reinterpret_cast<const StackReference<mirror::Object>*>(vreg_end);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800394 }
395
Ian Rogersef7d42f2014-01-06 12:55:46 -0800396 StackReference<mirror::Object>* References() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700397 return const_cast<StackReference<mirror::Object>*>(
398 const_cast<const ShadowFrame*>(this)->References());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800399 }
400
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700401 const uint32_t number_of_vregs_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700402 // Link to previous shadow frame or null.
Ian Rogers0399dde2012-06-06 17:09:28 -0700403 ShadowFrame* link_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700404 ArtMethod* method_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700405 uint32_t dex_pc_;
Andreas Gampe03ec9302015-08-27 17:41:47 -0700406 LockCountData lock_count_data_; // This may contain GC roots when lock counting is active.
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700407
408 // This is a two-part array:
409 // - [0..number_of_vregs) holds the raw virtual registers, and each element here is always 4
410 // bytes.
411 // - [number_of_vregs..number_of_vregs*2) holds only reference registers. Each element here is
412 // ptr-sized.
413 // In other words when a primitive is stored in vX, the second (reference) part of the array will
414 // be null. When a reference is stored in vX, the second (reference) part of the array will be a
415 // copy of vX.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800416 uint32_t vregs_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700417
Ian Rogers0399dde2012-06-06 17:09:28 -0700418 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700419};
420
Andreas Gampeb3025922015-09-01 14:45:00 -0700421struct ShadowFrameDeleter {
422 inline void operator()(ShadowFrame* frame) {
423 if (frame != nullptr) {
424 frame->~ShadowFrame();
425 }
426 }
427};
428
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800429class JavaFrameRootInfo : public RootInfo {
430 public:
431 JavaFrameRootInfo(uint32_t thread_id, const StackVisitor* stack_visitor, size_t vreg)
432 : RootInfo(kRootJavaFrame, thread_id), stack_visitor_(stack_visitor), vreg_(vreg) {
433 }
434 virtual void Describe(std::ostream& os) const OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700435 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800436
437 private:
438 const StackVisitor* const stack_visitor_;
439 const size_t vreg_;
440};
441
Ian Rogers0399dde2012-06-06 17:09:28 -0700442// The managed stack is used to record fragments of managed code stacks. Managed code stacks
443// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
444// necessary for transitions between code using different frame layouts and transitions into native
445// code.
Ian Rogersdf1ce912012-11-27 17:07:11 -0800446class PACKED(4) ManagedStack {
Ian Rogers0399dde2012-06-06 17:09:28 -0700447 public:
Ian Rogersca190662012-06-26 15:45:57 -0700448 ManagedStack()
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700449 : top_quick_frame_(nullptr), link_(nullptr), top_shadow_frame_(nullptr) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700450
451 void PushManagedStackFragment(ManagedStack* fragment) {
452 // Copy this top fragment into given fragment.
453 memcpy(fragment, this, sizeof(ManagedStack));
454 // Clear this fragment, which has become the top.
455 memset(this, 0, sizeof(ManagedStack));
456 // Link our top fragment onto the given fragment.
457 link_ = fragment;
458 }
459
460 void PopManagedStackFragment(const ManagedStack& fragment) {
461 DCHECK(&fragment == link_);
462 // Copy this given fragment back to the top.
463 memcpy(this, &fragment, sizeof(ManagedStack));
464 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700465
466 ManagedStack* GetLink() const {
467 return link_;
468 }
469
Mathieu Chartiere401d142015-04-22 13:56:20 -0700470 ArtMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700471 return top_quick_frame_;
472 }
473
Mathieu Chartiere401d142015-04-22 13:56:20 -0700474 void SetTopQuickFrame(ArtMethod** top) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700475 DCHECK(top_shadow_frame_ == nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700476 top_quick_frame_ = top;
477 }
478
Ian Rogers0399dde2012-06-06 17:09:28 -0700479 static size_t TopQuickFrameOffset() {
480 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
481 }
482
Ian Rogers0399dde2012-06-06 17:09:28 -0700483 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700484 DCHECK(top_quick_frame_ == nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700485 ShadowFrame* old_frame = top_shadow_frame_;
486 top_shadow_frame_ = new_top_frame;
487 new_top_frame->SetLink(old_frame);
488 return old_frame;
489 }
490
491 ShadowFrame* PopShadowFrame() {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700492 DCHECK(top_quick_frame_ == nullptr);
493 CHECK(top_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700494 ShadowFrame* frame = top_shadow_frame_;
495 top_shadow_frame_ = frame->GetLink();
496 return frame;
497 }
498
499 ShadowFrame* GetTopShadowFrame() const {
500 return top_shadow_frame_;
501 }
502
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800503 void SetTopShadowFrame(ShadowFrame* top) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700504 DCHECK(top_quick_frame_ == nullptr);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800505 top_shadow_frame_ = top;
506 }
507
Ian Rogers0399dde2012-06-06 17:09:28 -0700508 static size_t TopShadowFrameOffset() {
509 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
510 }
511
Mathieu Chartier90443472015-07-16 20:32:27 -0700512 size_t NumJniShadowFrameReferences() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700513
Ian Rogersef7d42f2014-01-06 12:55:46 -0800514 bool ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700515
516 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700517 ArtMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700518 ManagedStack* link_;
519 ShadowFrame* top_shadow_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700520};
521
522class StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100523 public:
524 // This enum defines a flag to control whether inlined frames are included
525 // when walking the stack.
526 enum class StackWalkKind {
527 kIncludeInlinedFrames,
528 kSkipInlinedFrames,
529 };
530
Ian Rogers0399dde2012-06-06 17:09:28 -0700531 protected:
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100532 StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind)
Mathieu Chartier90443472015-07-16 20:32:27 -0700533 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700534
535 public:
536 virtual ~StackVisitor() {}
537
538 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Mathieu Chartier90443472015-07-16 20:32:27 -0700539 virtual bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700540
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700541 void WalkStack(bool include_transitions = false)
Mathieu Chartier90443472015-07-16 20:32:27 -0700542 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700543
Sebastien Hertz26f72862015-09-15 09:52:07 +0200544 Thread* GetThread() const {
545 return thread_;
546 }
547
Mathieu Chartier90443472015-07-16 20:32:27 -0700548 ArtMethod* GetMethod() const SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi92d1a662014-05-15 21:43:59 -0700549
Nicolas Geoffrayccc61972015-10-01 14:34:20 +0100550 ArtMethod* GetOuterMethod() const {
551 return *GetCurrentQuickFrame();
552 }
553
Ian Rogers0399dde2012-06-06 17:09:28 -0700554 bool IsShadowFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800555 return cur_shadow_frame_ != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -0700556 }
557
Mathieu Chartier90443472015-07-16 20:32:27 -0700558 uint32_t GetDexPc(bool abort_on_failure = true) const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700559
Mathieu Chartier90443472015-07-16 20:32:27 -0700560 mirror::Object* GetThisObject() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800561
Mathieu Chartier90443472015-07-16 20:32:27 -0700562 size_t GetNativePcOffset() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700563
Ian Rogersef7d42f2014-01-06 12:55:46 -0800564 uintptr_t* CalleeSaveAddress(int num, size_t frame_size) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700565 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700566 // Callee saves are held at the top of the frame
Ian Rogersef7d42f2014-01-06 12:55:46 -0800567 DCHECK(GetMethod() != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700568 uint8_t* save_addr =
569 reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size - ((num + 1) * sizeof(void*));
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800570#if defined(__i386__) || defined(__x86_64__)
Ian Rogers13735952014-10-08 12:43:28 -0700571 save_addr -= sizeof(void*); // account for return address
Ian Rogers0399dde2012-06-06 17:09:28 -0700572#endif
Mathieu Chartier67022432012-11-29 18:04:50 -0800573 return reinterpret_cast<uintptr_t*>(save_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700574 }
575
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700576 // Returns the height of the stack in the managed stack frames, including transitions.
Mathieu Chartier90443472015-07-16 20:32:27 -0700577 size_t GetFrameHeight() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800578 return GetNumFrames() - cur_depth_ - 1;
Ian Rogers0399dde2012-06-06 17:09:28 -0700579 }
580
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700581 // Returns a frame ID for JDWP use, starting from 1.
Mathieu Chartier90443472015-07-16 20:32:27 -0700582 size_t GetFrameId() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700583 return GetFrameHeight() + 1;
584 }
585
Mathieu Chartier90443472015-07-16 20:32:27 -0700586 size_t GetNumFrames() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700587 if (num_frames_ == 0) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100588 num_frames_ = ComputeNumFrames(thread_, walk_kind_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700589 }
590 return num_frames_;
591 }
592
Mathieu Chartier90443472015-07-16 20:32:27 -0700593 size_t GetFrameDepth() SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700594 return cur_depth_;
595 }
596
Ian Rogers5cf98192014-05-29 21:31:50 -0700597 // Get the method and dex pc immediately after the one that's currently being visited.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700598 bool GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -0700599 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700600
Mathieu Chartiere401d142015-04-22 13:56:20 -0700601 bool IsReferenceVReg(ArtMethod* m, uint16_t vreg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700602 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700603
Mathieu Chartiere401d142015-04-22 13:56:20 -0700604 bool GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700605 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700606
Mathieu Chartiere401d142015-04-22 13:56:20 -0700607 bool GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo, VRegKind kind_hi,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200608 uint64_t* val) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700609 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200610
Mathieu Chartiere401d142015-04-22 13:56:20 -0700611 bool SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Mathieu Chartier90443472015-07-16 20:32:27 -0700612 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700613
Mingyao Yang99170c62015-07-06 11:10:37 -0700614 // Values will be set in debugger shadow frames. Debugger will make sure deoptimization
615 // is triggered to make the values effective.
616 bool SetVRegFromDebugger(ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
617 SHARED_REQUIRES(Locks::mutator_lock_);
618
Mathieu Chartiere401d142015-04-22 13:56:20 -0700619 bool SetVRegPair(ArtMethod* m, uint16_t vreg, uint64_t new_value,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200620 VRegKind kind_lo, VRegKind kind_hi)
Mathieu Chartier90443472015-07-16 20:32:27 -0700621 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200622
Mingyao Yang99170c62015-07-06 11:10:37 -0700623 // Values will be set in debugger shadow frames. Debugger will make sure deoptimization
624 // is triggered to make the values effective.
625 bool SetVRegPairFromDebugger(ArtMethod* m, uint16_t vreg, uint64_t new_value,
626 VRegKind kind_lo, VRegKind kind_hi)
627 SHARED_REQUIRES(Locks::mutator_lock_);
628
Mathieu Chartier815873e2014-02-13 18:02:13 -0800629 uintptr_t* GetGPRAddress(uint32_t reg) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700630
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700631 // This is a fast-path for getting/setting values in a quick frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700632 uint32_t* GetVRegAddrFromQuickCode(ArtMethod** cur_quick_frame,
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000633 const DexFile::CodeItem* code_item,
634 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
635 uint16_t vreg) const {
636 int offset = GetVRegOffsetFromQuickCode(
637 code_item, core_spills, fp_spills, frame_size, vreg, kRuntimeISA);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700638 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
Ian Rogers13735952014-10-08 12:43:28 -0700639 uint8_t* vreg_addr = reinterpret_cast<uint8_t*>(cur_quick_frame) + offset;
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700640 return reinterpret_cast<uint32_t*>(vreg_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700641 }
642
Mathieu Chartier90443472015-07-16 20:32:27 -0700643 uintptr_t GetReturnPc() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700644
Mathieu Chartier90443472015-07-16 20:32:27 -0700645 void SetReturnPc(uintptr_t new_ret_pc) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700646
647 /*
648 * Return sp-relative offset for a Dalvik virtual register, compiler
649 * spill or Method* in bytes using Method*.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700650 * Note that (reg == -1) denotes an invalid Dalvik register. For the
651 * positive values, the Dalvik registers come first, followed by the
652 * Method*, followed by other special temporaries if any, followed by
653 * regular compiler temporary. As of now we only have the Method* as
654 * as a special compiler temporary.
655 * A compiler temporary can be thought of as a virtual register that
656 * does not exist in the dex but holds intermediate values to help
657 * optimizations and code generation. A special compiler temporary is
658 * one whose location in frame is well known while non-special ones
659 * do not have a requirement on location in frame as long as code
660 * generator itself knows how to access them.
Ian Rogers0399dde2012-06-06 17:09:28 -0700661 *
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700662 * +-------------------------------+
663 * | IN[ins-1] | {Note: resides in caller's frame}
664 * | . |
665 * | IN[0] |
Mathieu Chartiere401d142015-04-22 13:56:20 -0700666 * | caller's ArtMethod | ... ArtMethod*
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700667 * +===============================+ {Note: start of callee's frame}
668 * | core callee-save spill | {variable sized}
669 * +-------------------------------+
670 * | fp callee-save spill |
671 * +-------------------------------+
672 * | filler word | {For compatibility, if V[locals-1] used as wide
673 * +-------------------------------+
674 * | V[locals-1] |
675 * | V[locals-2] |
676 * | . |
677 * | . | ... (reg == 2)
678 * | V[1] | ... (reg == 1)
679 * | V[0] | ... (reg == 0) <---- "locals_start"
680 * +-------------------------------+
681 * | stack alignment padding | {0 to (kStackAlignWords-1) of padding}
682 * +-------------------------------+
683 * | Compiler temp region | ... (reg >= max_num_special_temps)
684 * | . |
685 * | . |
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700686 * | V[max_num_special_temps + 1] |
687 * | V[max_num_special_temps + 0] |
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700688 * +-------------------------------+
689 * | OUT[outs-1] |
690 * | OUT[outs-2] |
691 * | . |
692 * | OUT[0] |
Mathieu Chartiere401d142015-04-22 13:56:20 -0700693 * | ArtMethod* | ... (reg == num_total_code_regs == special_temp_value) <<== sp, 16-byte aligned
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700694 * +===============================+
Ian Rogers0399dde2012-06-06 17:09:28 -0700695 */
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000696 static int GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
697 uint32_t core_spills, uint32_t fp_spills,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700698 size_t frame_size, int reg, InstructionSet isa);
Ian Rogers0399dde2012-06-06 17:09:28 -0700699
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000700 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
buzbee82818642014-06-04 15:35:41 -0700701 // According to stack model, the first out is above the Method referernce.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700702 return InstructionSetPointerSize(isa) + out_num * sizeof(uint32_t);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800703 }
704
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100705 bool IsInInlinedFrame() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100706 return current_inlining_depth_ != 0;
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100707 }
708
Ian Rogers0399dde2012-06-06 17:09:28 -0700709 uintptr_t GetCurrentQuickFramePc() const {
710 return cur_quick_frame_pc_;
711 }
712
Mathieu Chartiere401d142015-04-22 13:56:20 -0700713 ArtMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700714 return cur_quick_frame_;
715 }
716
717 ShadowFrame* GetCurrentShadowFrame() const {
718 return cur_shadow_frame_;
719 }
720
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100721 bool IsCurrentFrameInInterpreter() const {
722 return cur_shadow_frame_ != nullptr;
723 }
724
Mathieu Chartiere401d142015-04-22 13:56:20 -0700725 HandleScope* GetCurrentHandleScope(size_t pointer_size) const {
726 ArtMethod** sp = GetCurrentQuickFrame();
727 // Skip ArtMethod*; handle scope comes next;
728 return reinterpret_cast<HandleScope*>(reinterpret_cast<uintptr_t>(sp) + pointer_size);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700729 }
730
Mathieu Chartier90443472015-07-16 20:32:27 -0700731 std::string DescribeLocation() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers40e3bac2012-11-20 00:09:14 -0800732
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100733 static size_t ComputeNumFrames(Thread* thread, StackWalkKind walk_kind)
Mathieu Chartier90443472015-07-16 20:32:27 -0700734 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800735
Mathieu Chartier90443472015-07-16 20:32:27 -0700736 static void DescribeStack(Thread* thread) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800737
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100738 ArtCode GetCurrentCode() const { return ArtCode(cur_quick_frame_); }
739
Ian Rogers0399dde2012-06-06 17:09:28 -0700740 private:
Ian Rogers5cf98192014-05-29 21:31:50 -0700741 // Private constructor known in the case that num_frames_ has already been computed.
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100742 StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind, size_t num_frames)
Mathieu Chartier90443472015-07-16 20:32:27 -0700743 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700744
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100745 bool IsAccessibleRegister(uint32_t reg, bool is_float) const {
746 return is_float ? IsAccessibleFPR(reg) : IsAccessibleGPR(reg);
747 }
748 uintptr_t GetRegister(uint32_t reg, bool is_float) const {
749 DCHECK(IsAccessibleRegister(reg, is_float));
750 return is_float ? GetFPR(reg) : GetGPR(reg);
751 }
752 void SetRegister(uint32_t reg, uintptr_t value, bool is_float) {
753 DCHECK(IsAccessibleRegister(reg, is_float));
754 if (is_float) {
755 SetFPR(reg, value);
756 } else {
757 SetGPR(reg, value);
758 }
759 }
760
761 bool IsAccessibleGPR(uint32_t reg) const;
762 uintptr_t GetGPR(uint32_t reg) const;
763 void SetGPR(uint32_t reg, uintptr_t value);
764
765 bool IsAccessibleFPR(uint32_t reg) const;
766 uintptr_t GetFPR(uint32_t reg) const;
767 void SetFPR(uint32_t reg, uintptr_t value);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200768
Mingyao Yang99170c62015-07-06 11:10:37 -0700769 bool GetVRegFromDebuggerShadowFrame(uint16_t vreg, VRegKind kind, uint32_t* val) const
770 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700771 bool GetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100772 uint32_t* val) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700773 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700774 bool GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100775 uint32_t* val) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700776 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100777 bool GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700778 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100779
Mingyao Yang99170c62015-07-06 11:10:37 -0700780 bool GetVRegPairFromDebuggerShadowFrame(uint16_t vreg, VRegKind kind_lo, VRegKind kind_hi,
781 uint64_t* val) const
782 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700783 bool GetVRegPairFromQuickCode(ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100784 VRegKind kind_hi, uint64_t* val) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700785 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700786 bool GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100787 VRegKind kind_lo, VRegKind kind_hi,
788 uint64_t* val) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700789 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100790 bool GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi, VRegKind kind_lo,
791 uint64_t* val) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700792 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100793
Mathieu Chartiere401d142015-04-22 13:56:20 -0700794 bool SetVRegFromQuickCode(ArtMethod* m, uint16_t vreg, uint32_t new_value,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100795 VRegKind kind)
Mathieu Chartier90443472015-07-16 20:32:27 -0700796 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100797 bool SetRegisterIfAccessible(uint32_t reg, uint32_t new_value, VRegKind kind)
Mathieu Chartier90443472015-07-16 20:32:27 -0700798 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100799
Mathieu Chartiere401d142015-04-22 13:56:20 -0700800 bool SetVRegPairFromQuickCode(ArtMethod* m, uint16_t vreg, uint64_t new_value,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100801 VRegKind kind_lo, VRegKind kind_hi)
Mathieu Chartier90443472015-07-16 20:32:27 -0700802 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100803 bool SetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi, uint64_t new_value,
804 bool is_float)
Mathieu Chartier90443472015-07-16 20:32:27 -0700805 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100806
Mathieu Chartier90443472015-07-16 20:32:27 -0700807 void SanityCheckFrame() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700808
Mathieu Chartier90443472015-07-16 20:32:27 -0700809 InlineInfo GetCurrentInlineInfo() const SHARED_REQUIRES(Locks::mutator_lock_);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100810
Ian Rogers7a22fa62013-01-23 12:16:16 -0800811 Thread* const thread_;
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100812 const StackWalkKind walk_kind_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700813 ShadowFrame* cur_shadow_frame_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700814 ArtMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700815 uintptr_t cur_quick_frame_pc_;
816 // Lazily computed, number of frames in the stack.
817 size_t num_frames_;
818 // Depth of the frame we're currently at.
819 size_t cur_depth_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100820 // Current inlining depth of the method we are currently at.
821 // 0 if there is no inlined frame.
822 size_t current_inlining_depth_;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700823
Ian Rogers0399dde2012-06-06 17:09:28 -0700824 protected:
825 Context* const context_;
826};
827
Elliott Hughes68e76522011-10-05 13:22:16 -0700828} // namespace art
829
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700830#endif // ART_RUNTIME_STACK_H_