blob: e9ed4970947368fb3e43df68dee560833ae445ad [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 Rogersd582fa42014-11-05 23:46:43 -080023#include "arch/instruction_set.h"
Andreas Gampe03ec9302015-08-27 17:41:47 -070024#include "base/macros.h"
25#include "base/mutex.h"
Ian Rogerse63db272014-07-15 15:36:11 -070026#include "dex_file.h"
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080027#include "gc_root.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010028#include "quick/quick_method_frame_info.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080029#include "read_barrier.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010030#include "stack_reference.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;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010043class OatQuickMethodHeader;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070044class ScopedObjectAccess;
Nicolas Geoffray57f61612015-05-15 13:20:41 +010045class ShadowFrame;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000046class StackVisitor;
Elliott Hughes68e76522011-10-05 13:22:16 -070047class Thread;
Vladimir Marko3a21e382016-09-02 12:38:38 +010048union JValue;
Elliott Hughes68e76522011-10-05 13:22:16 -070049
Ian Rogers2bcb4a42012-11-08 10:39:18 -080050// The kind of vreg being accessed in calls to Set/GetVReg.
51enum VRegKind {
52 kReferenceVReg,
53 kIntVReg,
54 kFloatVReg,
55 kLongLoVReg,
56 kLongHiVReg,
57 kDoubleLoVReg,
58 kDoubleHiVReg,
59 kConstant,
60 kImpreciseConstant,
61 kUndefined,
62};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070063std::ostream& operator<<(std::ostream& os, const VRegKind& rhs);
Ian Rogers2bcb4a42012-11-08 10:39:18 -080064
Andreas Gampeb3025922015-09-01 14:45:00 -070065// Forward declaration. Just calls the destructor.
66struct ShadowFrameDeleter;
67using ShadowFrameAllocaUniquePtr = std::unique_ptr<ShadowFrame, ShadowFrameDeleter>;
68
Andreas Gampe03ec9302015-08-27 17:41:47 -070069// Counting locks by storing object pointers into a vector. Duplicate entries mark recursive locks.
70// The vector will be visited with the ShadowFrame during GC (so all the locked-on objects are
71// thread roots).
72// Note: implementation is split so that the call sites may be optimized to no-ops in case no
73// lock counting is necessary. The actual implementation is in the cc file to avoid
74// dependencies.
75class LockCountData {
76 public:
77 // Add the given object to the list of monitors, that is, objects that have been locked. This
78 // will not throw (but be skipped if there is an exception pending on entry).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070079 void AddMonitor(Thread* self, mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe03ec9302015-08-27 17:41:47 -070080
81 // Try to remove the given object from the monitor list, indicating an unlock operation.
82 // This will throw an IllegalMonitorStateException (clearing any already pending exception), in
83 // case that there wasn't a lock recorded for the object.
Andreas Gampe03ec9302015-08-27 17:41:47 -070084 void RemoveMonitorOrThrow(Thread* self,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070085 const mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe03ec9302015-08-27 17:41:47 -070086
87 // Check whether all acquired monitors have been released. This will potentially throw an
88 // IllegalMonitorStateException, clearing any already pending exception. Returns true if the
89 // check shows that everything is OK wrt/ lock counting, false otherwise.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070090 bool CheckAllMonitorsReleasedOrThrow(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe03ec9302015-08-27 17:41:47 -070091
92 template <typename T, typename... Args>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070093 void VisitMonitors(T visitor, Args&&... args) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe03ec9302015-08-27 17:41:47 -070094 if (monitors_ != nullptr) {
95 // Visitors may change the Object*. Be careful with the foreach loop.
96 for (mirror::Object*& obj : *monitors_) {
97 visitor(/* inout */ &obj, std::forward<Args>(args)...);
98 }
99 }
100 }
101
102 private:
Andreas Gampe03ec9302015-08-27 17:41:47 -0700103 // Stores references to the locked-on objects. As noted, this should be visited during thread
104 // marking.
105 std::unique_ptr<std::vector<mirror::Object*>> monitors_;
106};
107
Elliott Hughes956af0f2014-12-11 14:34:28 -0800108// ShadowFrame has 2 possible layouts:
Mathieu Chartier67022432012-11-29 18:04:50 -0800109// - interpreter - separate VRegs and reference arrays. References are in the reference array.
110// - JNI - just VRegs, but where every VReg holds a reference.
Ian Rogers0399dde2012-06-06 17:09:28 -0700111class ShadowFrame {
Elliott Hughes68e76522011-10-05 13:22:16 -0700112 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800113 // Compute size of ShadowFrame in bytes assuming it has a reference array.
Jeff Hao66135192013-05-14 11:02:41 -0700114 static size_t ComputeSize(uint32_t num_vregs) {
115 return sizeof(ShadowFrame) + (sizeof(uint32_t) * num_vregs) +
Ian Rogersef7d42f2014-01-06 12:55:46 -0800116 (sizeof(StackReference<mirror::Object>) * num_vregs);
Jeff Hao66135192013-05-14 11:02:41 -0700117 }
118
119 // Create ShadowFrame in heap for deoptimization.
Christopher Ferris241a9582015-04-27 15:19:41 -0700120 static ShadowFrame* CreateDeoptimizedFrame(uint32_t num_vregs, ShadowFrame* link,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121 ArtMethod* method, uint32_t dex_pc) {
Jeff Hao66135192013-05-14 11:02:41 -0700122 uint8_t* memory = new uint8_t[ComputeSize(num_vregs)];
Andreas Gampeb3025922015-09-01 14:45:00 -0700123 return CreateShadowFrameImpl(num_vregs, link, method, dex_pc, memory);
Jeff Hao66135192013-05-14 11:02:41 -0700124 }
125
Christopher Ferris241a9582015-04-27 15:19:41 -0700126 // Delete a ShadowFrame allocated on the heap for deoptimization.
127 static void DeleteDeoptimizedFrame(ShadowFrame* sf) {
Andreas Gampeb3025922015-09-01 14:45:00 -0700128 sf->~ShadowFrame(); // Explicitly destruct.
Christopher Ferris241a9582015-04-27 15:19:41 -0700129 uint8_t* memory = reinterpret_cast<uint8_t*>(sf);
130 delete[] memory;
131 }
132
Andreas Gampeb3025922015-09-01 14:45:00 -0700133 // Create a shadow frame in a fresh alloca. This needs to be in the context of the caller.
134 // Inlining doesn't work, the compiler will still undo the alloca. So this needs to be a macro.
135#define CREATE_SHADOW_FRAME(num_vregs, link, method, dex_pc) ({ \
136 size_t frame_size = ShadowFrame::ComputeSize(num_vregs); \
137 void* alloca_mem = alloca(frame_size); \
138 ShadowFrameAllocaUniquePtr( \
139 ShadowFrame::CreateShadowFrameImpl((num_vregs), (link), (method), (dex_pc), \
140 (alloca_mem))); \
141 })
142
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700143 ~ShadowFrame() {}
144
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700145 // TODO(iam): Clean references array up since they're always there,
146 // we don't need to do conditionals.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800147 bool HasReferenceArray() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700148 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700149 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700150
TDYa127ce4cc0d2012-11-18 16:59:53 -0800151 uint32_t NumberOfVRegs() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700152 return number_of_vregs_;
Ian Rogers5438ad82012-10-15 17:22:44 -0700153 }
154
Ian Rogers0399dde2012-06-06 17:09:28 -0700155 uint32_t GetDexPC() const {
buzbee1452bee2015-03-06 14:43:04 -0800156 return (dex_pc_ptr_ == nullptr) ? dex_pc_ : dex_pc_ptr_ - code_item_->insns_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700157 }
158
Bill Buzbee1d011d92016-04-04 16:59:29 +0000159 int16_t GetCachedHotnessCountdown() const {
160 return cached_hotness_countdown_;
161 }
162
163 void SetCachedHotnessCountdown(int16_t cached_hotness_countdown) {
164 cached_hotness_countdown_ = cached_hotness_countdown;
165 }
166
167 int16_t GetHotnessCountdown() const {
168 return hotness_countdown_;
169 }
170
171 void SetHotnessCountdown(int16_t hotness_countdown) {
172 hotness_countdown_ = hotness_countdown;
173 }
174
Ian Rogers0399dde2012-06-06 17:09:28 -0700175 void SetDexPC(uint32_t dex_pc) {
176 dex_pc_ = dex_pc;
buzbee1452bee2015-03-06 14:43:04 -0800177 dex_pc_ptr_ = nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -0700178 }
179
Ian Rogers0399dde2012-06-06 17:09:28 -0700180 ShadowFrame* GetLink() const {
181 return link_;
182 }
183
184 void SetLink(ShadowFrame* frame) {
185 DCHECK_NE(this, frame);
186 link_ = frame;
187 }
188
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700189 int32_t GetVReg(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800190 DCHECK_LT(i, NumberOfVRegs());
191 const uint32_t* vreg = &vregs_[i];
192 return *reinterpret_cast<const int32_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700193 }
194
buzbee1452bee2015-03-06 14:43:04 -0800195 uint32_t* GetVRegAddr(size_t i) {
196 return &vregs_[i];
197 }
198
199 uint32_t* GetShadowRefAddr(size_t i) {
200 DCHECK(HasReferenceArray());
201 DCHECK_LT(i, NumberOfVRegs());
202 return &vregs_[i + NumberOfVRegs()];
203 }
204
205 void SetCodeItem(const DexFile::CodeItem* code_item) {
206 code_item_ = code_item;
207 }
208
Bill Buzbeed47fd902016-07-07 14:42:43 +0000209 const DexFile::CodeItem* GetCodeItem() const {
210 return code_item_;
211 }
212
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700213 float GetVRegFloat(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800214 DCHECK_LT(i, NumberOfVRegs());
215 // NOTE: Strict-aliasing?
216 const uint32_t* vreg = &vregs_[i];
217 return *reinterpret_cast<const float*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700218 }
219
220 int64_t GetVRegLong(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200221 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800222 const uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700223 typedef const int64_t unaligned_int64 __attribute__ ((aligned (4)));
224 return *reinterpret_cast<unaligned_int64*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700225 }
226
227 double GetVRegDouble(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200228 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800229 const uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700230 typedef const double unaligned_double __attribute__ ((aligned (4)));
231 return *reinterpret_cast<unaligned_double*>(vreg);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800232 }
233
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700234 // Look up the reference given its virtual register number.
235 // If this returns non-null then this does not mean the vreg is currently a reference
236 // 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 -0800237 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700238 mirror::Object* GetVRegReference(size_t i) const REQUIRES_SHARED(Locks::mutator_lock_) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800239 DCHECK_LT(i, NumberOfVRegs());
Mathieu Chartier4e305412014-02-19 10:54:44 -0800240 mirror::Object* ref;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800241 if (HasReferenceArray()) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800242 ref = References()[i].AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800243 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800244 const uint32_t* vreg_ptr = &vregs_[i];
Mathieu Chartier4e305412014-02-19 10:54:44 -0800245 ref = reinterpret_cast<const StackReference<mirror::Object>*>(vreg_ptr)->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800246 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800247 if (kUseReadBarrier) {
248 ReadBarrier::AssertToSpaceInvariant(ref);
249 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800250 if (kVerifyFlags & kVerifyReads) {
251 VerifyObject(ref);
252 }
253 return ref;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700254 }
255
Jeff Hao16743632013-05-08 10:59:04 -0700256 // Get view of vregs as range of consecutive arguments starting at i.
257 uint32_t* GetVRegArgs(size_t i) {
258 return &vregs_[i];
259 }
260
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700261 void SetVReg(size_t i, int32_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800262 DCHECK_LT(i, NumberOfVRegs());
263 uint32_t* vreg = &vregs_[i];
264 *reinterpret_cast<int32_t*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700265 // This is needed for moving collectors since these can update the vreg references if they
266 // happen to agree with references in the reference array.
267 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800268 References()[i].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700269 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700270 }
271
272 void SetVRegFloat(size_t i, float val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800273 DCHECK_LT(i, NumberOfVRegs());
274 uint32_t* vreg = &vregs_[i];
275 *reinterpret_cast<float*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700276 // This is needed for moving collectors since these can update the vreg references if they
277 // happen to agree with references in the reference array.
278 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800279 References()[i].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700280 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700281 }
282
283 void SetVRegLong(size_t i, int64_t val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200284 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800285 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700286 typedef int64_t unaligned_int64 __attribute__ ((aligned (4)));
287 *reinterpret_cast<unaligned_int64*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700288 // This is needed for moving collectors since these can update the vreg references if they
289 // happen to agree with references in the reference array.
290 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800291 References()[i].Clear();
292 References()[i + 1].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700293 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700294 }
295
296 void SetVRegDouble(size_t i, double val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200297 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800298 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700299 typedef double unaligned_double __attribute__ ((aligned (4)));
300 *reinterpret_cast<unaligned_double*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700301 // This is needed for moving collectors since these can update the vreg references if they
302 // happen to agree with references in the reference array.
303 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800304 References()[i].Clear();
305 References()[i + 1].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700306 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700307 }
308
Mathieu Chartier4e305412014-02-19 10:54:44 -0800309 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700310 void SetVRegReference(size_t i, mirror::Object* val) REQUIRES_SHARED(Locks::mutator_lock_) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800311 DCHECK_LT(i, NumberOfVRegs());
Mathieu Chartier4e305412014-02-19 10:54:44 -0800312 if (kVerifyFlags & kVerifyWrites) {
313 VerifyObject(val);
314 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800315 if (kUseReadBarrier) {
316 ReadBarrier::AssertToSpaceInvariant(val);
317 }
TDYa127ce4cc0d2012-11-18 16:59:53 -0800318 uint32_t* vreg = &vregs_[i];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800319 reinterpret_cast<StackReference<mirror::Object>*>(vreg)->Assign(val);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800320 if (HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800321 References()[i].Assign(val);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800322 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700323 }
324
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700325 ArtMethod* GetMethod() const REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800326 DCHECK(method_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700327 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700328 }
329
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700330 mirror::Object* GetThisObject() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800331
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700332 mirror::Object* GetThisObject(uint16_t num_ins) const REQUIRES_SHARED(Locks::mutator_lock_);
Jeff Haoe701f482013-05-24 11:50:49 -0700333
Ian Rogersef7d42f2014-01-06 12:55:46 -0800334 bool Contains(StackReference<mirror::Object>* shadow_frame_entry_obj) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800335 if (HasReferenceArray()) {
336 return ((&References()[0] <= shadow_frame_entry_obj) &&
337 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
338 } else {
339 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
340 return ((&vregs_[0] <= shadow_frame_entry) &&
341 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
Ian Rogers0399dde2012-06-06 17:09:28 -0700342 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700343 }
344
Andreas Gampe03ec9302015-08-27 17:41:47 -0700345 LockCountData& GetLockCountData() {
346 return lock_count_data_;
347 }
348
buzbee1452bee2015-03-06 14:43:04 -0800349 static size_t LockCountDataOffset() {
350 return OFFSETOF_MEMBER(ShadowFrame, lock_count_data_);
351 }
352
Ian Rogers0399dde2012-06-06 17:09:28 -0700353 static size_t LinkOffset() {
354 return OFFSETOF_MEMBER(ShadowFrame, link_);
355 }
356
Ian Rogers0399dde2012-06-06 17:09:28 -0700357 static size_t MethodOffset() {
358 return OFFSETOF_MEMBER(ShadowFrame, method_);
359 }
360
Ian Rogers0399dde2012-06-06 17:09:28 -0700361 static size_t DexPCOffset() {
362 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
363 }
364
Ian Rogers5438ad82012-10-15 17:22:44 -0700365 static size_t NumberOfVRegsOffset() {
366 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
367 }
368
TDYa127ce4cc0d2012-11-18 16:59:53 -0800369 static size_t VRegsOffset() {
370 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
Ian Rogers5438ad82012-10-15 17:22:44 -0700371 }
372
buzbee1452bee2015-03-06 14:43:04 -0800373 static size_t ResultRegisterOffset() {
374 return OFFSETOF_MEMBER(ShadowFrame, result_register_);
375 }
376
377 static size_t DexPCPtrOffset() {
378 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_ptr_);
379 }
380
381 static size_t CodeItemOffset() {
382 return OFFSETOF_MEMBER(ShadowFrame, code_item_);
383 }
384
Bill Buzbee1d011d92016-04-04 16:59:29 +0000385 static size_t CachedHotnessCountdownOffset() {
386 return OFFSETOF_MEMBER(ShadowFrame, cached_hotness_countdown_);
387 }
388
389 static size_t HotnessCountdownOffset() {
390 return OFFSETOF_MEMBER(ShadowFrame, hotness_countdown_);
391 }
392
Andreas Gampeb3025922015-09-01 14:45:00 -0700393 // Create ShadowFrame for interpreter using provided memory.
394 static ShadowFrame* CreateShadowFrameImpl(uint32_t num_vregs,
395 ShadowFrame* link,
396 ArtMethod* method,
397 uint32_t dex_pc,
398 void* memory) {
399 return new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true);
400 }
401
Bill Buzbee1d011d92016-04-04 16:59:29 +0000402 const uint16_t* GetDexPCPtr() {
buzbee1452bee2015-03-06 14:43:04 -0800403 return dex_pc_ptr_;
404 }
405
Bill Buzbeed47fd902016-07-07 14:42:43 +0000406 void SetDexPCPtr(uint16_t* dex_pc_ptr) {
407 dex_pc_ptr_ = dex_pc_ptr;
408 }
409
buzbee1452bee2015-03-06 14:43:04 -0800410 JValue* GetResultRegister() {
411 return result_register_;
412 }
413
Elliott Hughes68e76522011-10-05 13:22:16 -0700414 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700415 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, ArtMethod* method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800416 uint32_t dex_pc, bool has_reference_array)
buzbee1452bee2015-03-06 14:43:04 -0800417 : link_(link), method_(method), result_register_(nullptr), dex_pc_ptr_(nullptr),
418 code_item_(nullptr), number_of_vregs_(num_vregs), dex_pc_(dex_pc) {
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700419 // TODO(iam): Remove this parameter, it's an an artifact of portable removal
420 DCHECK(has_reference_array);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800421 if (has_reference_array) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800422 memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(StackReference<mirror::Object>)));
Mathieu Chartier67022432012-11-29 18:04:50 -0800423 } else {
Jeff Haoe701f482013-05-24 11:50:49 -0700424 memset(vregs_, 0, num_vregs * sizeof(uint32_t));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700425 }
426 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700427
Ian Rogersef7d42f2014-01-06 12:55:46 -0800428 const StackReference<mirror::Object>* References() const {
Mathieu Chartier67022432012-11-29 18:04:50 -0800429 DCHECK(HasReferenceArray());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800430 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800431 return reinterpret_cast<const StackReference<mirror::Object>*>(vreg_end);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800432 }
433
Ian Rogersef7d42f2014-01-06 12:55:46 -0800434 StackReference<mirror::Object>* References() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700435 return const_cast<StackReference<mirror::Object>*>(
436 const_cast<const ShadowFrame*>(this)->References());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800437 }
438
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700439 // Link to previous shadow frame or null.
Ian Rogers0399dde2012-06-06 17:09:28 -0700440 ShadowFrame* link_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700441 ArtMethod* method_;
buzbee1452bee2015-03-06 14:43:04 -0800442 JValue* result_register_;
Bill Buzbee1d011d92016-04-04 16:59:29 +0000443 const uint16_t* dex_pc_ptr_;
buzbee1452bee2015-03-06 14:43:04 -0800444 const DexFile::CodeItem* code_item_;
Andreas Gampe03ec9302015-08-27 17:41:47 -0700445 LockCountData lock_count_data_; // This may contain GC roots when lock counting is active.
buzbee1452bee2015-03-06 14:43:04 -0800446 const uint32_t number_of_vregs_;
447 uint32_t dex_pc_;
Bill Buzbee1d011d92016-04-04 16:59:29 +0000448 int16_t cached_hotness_countdown_;
449 int16_t hotness_countdown_;
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700450
451 // This is a two-part array:
452 // - [0..number_of_vregs) holds the raw virtual registers, and each element here is always 4
453 // bytes.
454 // - [number_of_vregs..number_of_vregs*2) holds only reference registers. Each element here is
455 // ptr-sized.
456 // In other words when a primitive is stored in vX, the second (reference) part of the array will
457 // be null. When a reference is stored in vX, the second (reference) part of the array will be a
458 // copy of vX.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800459 uint32_t vregs_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700460
Ian Rogers0399dde2012-06-06 17:09:28 -0700461 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700462};
463
Andreas Gampeb3025922015-09-01 14:45:00 -0700464struct ShadowFrameDeleter {
465 inline void operator()(ShadowFrame* frame) {
466 if (frame != nullptr) {
467 frame->~ShadowFrame();
468 }
469 }
470};
471
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800472class JavaFrameRootInfo : public RootInfo {
473 public:
474 JavaFrameRootInfo(uint32_t thread_id, const StackVisitor* stack_visitor, size_t vreg)
475 : RootInfo(kRootJavaFrame, thread_id), stack_visitor_(stack_visitor), vreg_(vreg) {
476 }
477 virtual void Describe(std::ostream& os) const OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700478 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800479
480 private:
481 const StackVisitor* const stack_visitor_;
482 const size_t vreg_;
483};
484
Ian Rogers0399dde2012-06-06 17:09:28 -0700485// The managed stack is used to record fragments of managed code stacks. Managed code stacks
486// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
487// necessary for transitions between code using different frame layouts and transitions into native
488// code.
Ian Rogersdf1ce912012-11-27 17:07:11 -0800489class PACKED(4) ManagedStack {
Ian Rogers0399dde2012-06-06 17:09:28 -0700490 public:
Ian Rogersca190662012-06-26 15:45:57 -0700491 ManagedStack()
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700492 : top_quick_frame_(nullptr), link_(nullptr), top_shadow_frame_(nullptr) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700493
494 void PushManagedStackFragment(ManagedStack* fragment) {
495 // Copy this top fragment into given fragment.
496 memcpy(fragment, this, sizeof(ManagedStack));
497 // Clear this fragment, which has become the top.
498 memset(this, 0, sizeof(ManagedStack));
499 // Link our top fragment onto the given fragment.
500 link_ = fragment;
501 }
502
503 void PopManagedStackFragment(const ManagedStack& fragment) {
504 DCHECK(&fragment == link_);
505 // Copy this given fragment back to the top.
506 memcpy(this, &fragment, sizeof(ManagedStack));
507 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700508
509 ManagedStack* GetLink() const {
510 return link_;
511 }
512
Mathieu Chartiere401d142015-04-22 13:56:20 -0700513 ArtMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700514 return top_quick_frame_;
515 }
516
Mathieu Chartiere401d142015-04-22 13:56:20 -0700517 void SetTopQuickFrame(ArtMethod** top) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700518 DCHECK(top_shadow_frame_ == nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700519 top_quick_frame_ = top;
520 }
521
Ian Rogers0399dde2012-06-06 17:09:28 -0700522 static size_t TopQuickFrameOffset() {
523 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
524 }
525
Ian Rogers0399dde2012-06-06 17:09:28 -0700526 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700527 DCHECK(top_quick_frame_ == nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700528 ShadowFrame* old_frame = top_shadow_frame_;
529 top_shadow_frame_ = new_top_frame;
530 new_top_frame->SetLink(old_frame);
531 return old_frame;
532 }
533
534 ShadowFrame* PopShadowFrame() {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700535 DCHECK(top_quick_frame_ == nullptr);
536 CHECK(top_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700537 ShadowFrame* frame = top_shadow_frame_;
538 top_shadow_frame_ = frame->GetLink();
539 return frame;
540 }
541
542 ShadowFrame* GetTopShadowFrame() const {
543 return top_shadow_frame_;
544 }
545
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800546 void SetTopShadowFrame(ShadowFrame* top) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700547 DCHECK(top_quick_frame_ == nullptr);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800548 top_shadow_frame_ = top;
549 }
550
Ian Rogers0399dde2012-06-06 17:09:28 -0700551 static size_t TopShadowFrameOffset() {
552 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
553 }
554
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700555 size_t NumJniShadowFrameReferences() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700556
Ian Rogersef7d42f2014-01-06 12:55:46 -0800557 bool ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700558
559 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700560 ArtMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700561 ManagedStack* link_;
562 ShadowFrame* top_shadow_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700563};
564
565class StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100566 public:
567 // This enum defines a flag to control whether inlined frames are included
568 // when walking the stack.
569 enum class StackWalkKind {
570 kIncludeInlinedFrames,
571 kSkipInlinedFrames,
572 };
573
Ian Rogers0399dde2012-06-06 17:09:28 -0700574 protected:
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100575 StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700576 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700577
Nicolas Geoffray33856502015-10-20 15:52:58 +0100578 bool GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700579 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray33856502015-10-20 15:52:58 +0100580
Ian Rogers0399dde2012-06-06 17:09:28 -0700581 public:
582 virtual ~StackVisitor() {}
583
584 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700585 virtual bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700586
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700587 void WalkStack(bool include_transitions = false)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700588 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700589
Sebastien Hertz26f72862015-09-15 09:52:07 +0200590 Thread* GetThread() const {
591 return thread_;
592 }
593
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700594 ArtMethod* GetMethod() const REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi92d1a662014-05-15 21:43:59 -0700595
Nicolas Geoffrayccc61972015-10-01 14:34:20 +0100596 ArtMethod* GetOuterMethod() const {
597 return *GetCurrentQuickFrame();
598 }
599
Ian Rogers0399dde2012-06-06 17:09:28 -0700600 bool IsShadowFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800601 return cur_shadow_frame_ != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -0700602 }
603
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700604 uint32_t GetDexPc(bool abort_on_failure = true) const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700605
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700606 mirror::Object* GetThisObject() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800607
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700608 size_t GetNativePcOffset() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700609
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700610 // Returns the height of the stack in the managed stack frames, including transitions.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700611 size_t GetFrameHeight() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800612 return GetNumFrames() - cur_depth_ - 1;
Ian Rogers0399dde2012-06-06 17:09:28 -0700613 }
614
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700615 // Returns a frame ID for JDWP use, starting from 1.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700616 size_t GetFrameId() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700617 return GetFrameHeight() + 1;
618 }
619
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700620 size_t GetNumFrames() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700621 if (num_frames_ == 0) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100622 num_frames_ = ComputeNumFrames(thread_, walk_kind_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700623 }
624 return num_frames_;
625 }
626
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700627 size_t GetFrameDepth() REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700628 return cur_depth_;
629 }
630
Ian Rogers5cf98192014-05-29 21:31:50 -0700631 // Get the method and dex pc immediately after the one that's currently being visited.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700632 bool GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700633 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700634
Mathieu Chartiere401d142015-04-22 13:56:20 -0700635 bool GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700636 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700637
Mathieu Chartiere401d142015-04-22 13:56:20 -0700638 bool GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo, VRegKind kind_hi,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200639 uint64_t* val) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700640 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200641
Mingyao Yang636b9252015-07-31 16:40:24 -0700642 // Values will be set in debugger shadow frames. Debugger will make sure deoptimization
643 // is triggered to make the values effective.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700644 bool SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700645 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700646
Mingyao Yang99170c62015-07-06 11:10:37 -0700647 // Values will be set in debugger shadow frames. Debugger will make sure deoptimization
648 // is triggered to make the values effective.
Mingyao Yang636b9252015-07-31 16:40:24 -0700649 bool SetVRegPair(ArtMethod* m,
650 uint16_t vreg,
651 uint64_t new_value,
652 VRegKind kind_lo,
653 VRegKind kind_hi)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700654 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang99170c62015-07-06 11:10:37 -0700655
Mathieu Chartier815873e2014-02-13 18:02:13 -0800656 uintptr_t* GetGPRAddress(uint32_t reg) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700657
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700658 // This is a fast-path for getting/setting values in a quick frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700659 uint32_t* GetVRegAddrFromQuickCode(ArtMethod** cur_quick_frame,
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000660 const DexFile::CodeItem* code_item,
661 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
662 uint16_t vreg) const {
663 int offset = GetVRegOffsetFromQuickCode(
664 code_item, core_spills, fp_spills, frame_size, vreg, kRuntimeISA);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700665 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
Ian Rogers13735952014-10-08 12:43:28 -0700666 uint8_t* vreg_addr = reinterpret_cast<uint8_t*>(cur_quick_frame) + offset;
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700667 return reinterpret_cast<uint32_t*>(vreg_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700668 }
669
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700670 uintptr_t GetReturnPc() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700671
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700672 void SetReturnPc(uintptr_t new_ret_pc) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700673
674 /*
675 * Return sp-relative offset for a Dalvik virtual register, compiler
676 * spill or Method* in bytes using Method*.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700677 * Note that (reg == -1) denotes an invalid Dalvik register. For the
678 * positive values, the Dalvik registers come first, followed by the
679 * Method*, followed by other special temporaries if any, followed by
680 * regular compiler temporary. As of now we only have the Method* as
681 * as a special compiler temporary.
682 * A compiler temporary can be thought of as a virtual register that
683 * does not exist in the dex but holds intermediate values to help
684 * optimizations and code generation. A special compiler temporary is
685 * one whose location in frame is well known while non-special ones
686 * do not have a requirement on location in frame as long as code
687 * generator itself knows how to access them.
Ian Rogers0399dde2012-06-06 17:09:28 -0700688 *
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700689 * +-------------------------------+
690 * | IN[ins-1] | {Note: resides in caller's frame}
691 * | . |
692 * | IN[0] |
Mathieu Chartiere401d142015-04-22 13:56:20 -0700693 * | caller's ArtMethod | ... ArtMethod*
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700694 * +===============================+ {Note: start of callee's frame}
695 * | core callee-save spill | {variable sized}
696 * +-------------------------------+
697 * | fp callee-save spill |
698 * +-------------------------------+
699 * | filler word | {For compatibility, if V[locals-1] used as wide
700 * +-------------------------------+
701 * | V[locals-1] |
702 * | V[locals-2] |
703 * | . |
704 * | . | ... (reg == 2)
705 * | V[1] | ... (reg == 1)
706 * | V[0] | ... (reg == 0) <---- "locals_start"
707 * +-------------------------------+
708 * | stack alignment padding | {0 to (kStackAlignWords-1) of padding}
709 * +-------------------------------+
710 * | Compiler temp region | ... (reg >= max_num_special_temps)
711 * | . |
712 * | . |
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700713 * | V[max_num_special_temps + 1] |
714 * | V[max_num_special_temps + 0] |
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700715 * +-------------------------------+
716 * | OUT[outs-1] |
717 * | OUT[outs-2] |
718 * | . |
719 * | OUT[0] |
Mathieu Chartiere401d142015-04-22 13:56:20 -0700720 * | ArtMethod* | ... (reg == num_total_code_regs == special_temp_value) <<== sp, 16-byte aligned
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700721 * +===============================+
Ian Rogers0399dde2012-06-06 17:09:28 -0700722 */
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000723 static int GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
724 uint32_t core_spills, uint32_t fp_spills,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700725 size_t frame_size, int reg, InstructionSet isa);
Ian Rogers0399dde2012-06-06 17:09:28 -0700726
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000727 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
buzbee82818642014-06-04 15:35:41 -0700728 // According to stack model, the first out is above the Method referernce.
Andreas Gampe542451c2016-07-26 09:02:02 -0700729 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800730 }
731
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100732 bool IsInInlinedFrame() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100733 return current_inlining_depth_ != 0;
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100734 }
735
David Brazdilefc3f022015-10-28 12:19:06 -0500736 size_t GetCurrentInliningDepth() const {
737 return current_inlining_depth_;
738 }
739
Ian Rogers0399dde2012-06-06 17:09:28 -0700740 uintptr_t GetCurrentQuickFramePc() const {
741 return cur_quick_frame_pc_;
742 }
743
Mathieu Chartiere401d142015-04-22 13:56:20 -0700744 ArtMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700745 return cur_quick_frame_;
746 }
747
748 ShadowFrame* GetCurrentShadowFrame() const {
749 return cur_shadow_frame_;
750 }
751
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100752 bool IsCurrentFrameInInterpreter() const {
753 return cur_shadow_frame_ != nullptr;
754 }
755
Mathieu Chartiere401d142015-04-22 13:56:20 -0700756 HandleScope* GetCurrentHandleScope(size_t pointer_size) const {
757 ArtMethod** sp = GetCurrentQuickFrame();
758 // Skip ArtMethod*; handle scope comes next;
759 return reinterpret_cast<HandleScope*>(reinterpret_cast<uintptr_t>(sp) + pointer_size);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700760 }
761
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700762 std::string DescribeLocation() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers40e3bac2012-11-20 00:09:14 -0800763
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100764 static size_t ComputeNumFrames(Thread* thread, StackWalkKind walk_kind)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700765 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800766
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700767 static void DescribeStack(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800768
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100769 const OatQuickMethodHeader* GetCurrentOatQuickMethodHeader() const {
770 return cur_oat_quick_method_header_;
771 }
772
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700773 QuickMethodFrameInfo GetCurrentQuickFrameInfo() const REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100774
Ian Rogers0399dde2012-06-06 17:09:28 -0700775 private:
Ian Rogers5cf98192014-05-29 21:31:50 -0700776 // Private constructor known in the case that num_frames_ has already been computed.
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100777 StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind, size_t num_frames)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700778 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700779
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100780 bool IsAccessibleRegister(uint32_t reg, bool is_float) const {
781 return is_float ? IsAccessibleFPR(reg) : IsAccessibleGPR(reg);
782 }
783 uintptr_t GetRegister(uint32_t reg, bool is_float) const {
784 DCHECK(IsAccessibleRegister(reg, is_float));
785 return is_float ? GetFPR(reg) : GetGPR(reg);
786 }
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100787
788 bool IsAccessibleGPR(uint32_t reg) const;
789 uintptr_t GetGPR(uint32_t reg) const;
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100790
791 bool IsAccessibleFPR(uint32_t reg) const;
792 uintptr_t GetFPR(uint32_t reg) const;
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200793
Mingyao Yang99170c62015-07-06 11:10:37 -0700794 bool GetVRegFromDebuggerShadowFrame(uint16_t vreg, VRegKind kind, uint32_t* val) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700795 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700796 bool GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100797 uint32_t* val) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700798 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100799
Mingyao Yang99170c62015-07-06 11:10:37 -0700800 bool GetVRegPairFromDebuggerShadowFrame(uint16_t vreg, VRegKind kind_lo, VRegKind kind_hi,
801 uint64_t* val) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700802 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700803 bool GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100804 VRegKind kind_lo, VRegKind kind_hi,
805 uint64_t* val) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700806 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100807 bool GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi, VRegKind kind_lo,
808 uint64_t* val) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700809 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100810
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700811 void SanityCheckFrame() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700812
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700813 InlineInfo GetCurrentInlineInfo() const REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100814
Ian Rogers7a22fa62013-01-23 12:16:16 -0800815 Thread* const thread_;
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100816 const StackWalkKind walk_kind_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700817 ShadowFrame* cur_shadow_frame_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700818 ArtMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700819 uintptr_t cur_quick_frame_pc_;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100820 const OatQuickMethodHeader* cur_oat_quick_method_header_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700821 // Lazily computed, number of frames in the stack.
822 size_t num_frames_;
823 // Depth of the frame we're currently at.
824 size_t cur_depth_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100825 // Current inlining depth of the method we are currently at.
826 // 0 if there is no inlined frame.
827 size_t current_inlining_depth_;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700828
Ian Rogers0399dde2012-06-06 17:09:28 -0700829 protected:
830 Context* const context_;
831};
832
Elliott Hughes68e76522011-10-05 13:22:16 -0700833} // namespace art
834
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700835#endif // ART_RUNTIME_STACK_H_