blob: 90a0aee35358bca57e71f8ee03291ff491817645 [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
Mingyao Yang063fc772016-08-02 11:02:54 -070069// Size in bytes of the should_deoptimize flag on stack.
70// We just need 4 bytes for our purpose regardless of the architecture. Frame size
71// calculation will automatically do alignment for the final frame size.
72static constexpr size_t kShouldDeoptimizeFlagSize = 4;
73
Andreas Gampe03ec9302015-08-27 17:41:47 -070074// Counting locks by storing object pointers into a vector. Duplicate entries mark recursive locks.
75// The vector will be visited with the ShadowFrame during GC (so all the locked-on objects are
76// thread roots).
77// Note: implementation is split so that the call sites may be optimized to no-ops in case no
78// lock counting is necessary. The actual implementation is in the cc file to avoid
79// dependencies.
80class LockCountData {
81 public:
82 // Add the given object to the list of monitors, that is, objects that have been locked. This
83 // will not throw (but be skipped if there is an exception pending on entry).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070084 void AddMonitor(Thread* self, mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe03ec9302015-08-27 17:41:47 -070085
86 // Try to remove the given object from the monitor list, indicating an unlock operation.
87 // This will throw an IllegalMonitorStateException (clearing any already pending exception), in
88 // case that there wasn't a lock recorded for the object.
Andreas Gampe03ec9302015-08-27 17:41:47 -070089 void RemoveMonitorOrThrow(Thread* self,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070090 const mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe03ec9302015-08-27 17:41:47 -070091
92 // Check whether all acquired monitors have been released. This will potentially throw an
93 // IllegalMonitorStateException, clearing any already pending exception. Returns true if the
94 // check shows that everything is OK wrt/ lock counting, false otherwise.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070095 bool CheckAllMonitorsReleasedOrThrow(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe03ec9302015-08-27 17:41:47 -070096
97 template <typename T, typename... Args>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070098 void VisitMonitors(T visitor, Args&&... args) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe03ec9302015-08-27 17:41:47 -070099 if (monitors_ != nullptr) {
100 // Visitors may change the Object*. Be careful with the foreach loop.
101 for (mirror::Object*& obj : *monitors_) {
102 visitor(/* inout */ &obj, std::forward<Args>(args)...);
103 }
104 }
105 }
106
107 private:
Andreas Gampe03ec9302015-08-27 17:41:47 -0700108 // Stores references to the locked-on objects. As noted, this should be visited during thread
109 // marking.
110 std::unique_ptr<std::vector<mirror::Object*>> monitors_;
111};
112
Elliott Hughes956af0f2014-12-11 14:34:28 -0800113// ShadowFrame has 2 possible layouts:
Mathieu Chartier67022432012-11-29 18:04:50 -0800114// - interpreter - separate VRegs and reference arrays. References are in the reference array.
115// - JNI - just VRegs, but where every VReg holds a reference.
Ian Rogers0399dde2012-06-06 17:09:28 -0700116class ShadowFrame {
Elliott Hughes68e76522011-10-05 13:22:16 -0700117 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800118 // Compute size of ShadowFrame in bytes assuming it has a reference array.
Jeff Hao66135192013-05-14 11:02:41 -0700119 static size_t ComputeSize(uint32_t num_vregs) {
120 return sizeof(ShadowFrame) + (sizeof(uint32_t) * num_vregs) +
Ian Rogersef7d42f2014-01-06 12:55:46 -0800121 (sizeof(StackReference<mirror::Object>) * num_vregs);
Jeff Hao66135192013-05-14 11:02:41 -0700122 }
123
124 // Create ShadowFrame in heap for deoptimization.
Christopher Ferris241a9582015-04-27 15:19:41 -0700125 static ShadowFrame* CreateDeoptimizedFrame(uint32_t num_vregs, ShadowFrame* link,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700126 ArtMethod* method, uint32_t dex_pc) {
Jeff Hao66135192013-05-14 11:02:41 -0700127 uint8_t* memory = new uint8_t[ComputeSize(num_vregs)];
Andreas Gampeb3025922015-09-01 14:45:00 -0700128 return CreateShadowFrameImpl(num_vregs, link, method, dex_pc, memory);
Jeff Hao66135192013-05-14 11:02:41 -0700129 }
130
Christopher Ferris241a9582015-04-27 15:19:41 -0700131 // Delete a ShadowFrame allocated on the heap for deoptimization.
132 static void DeleteDeoptimizedFrame(ShadowFrame* sf) {
Andreas Gampeb3025922015-09-01 14:45:00 -0700133 sf->~ShadowFrame(); // Explicitly destruct.
Christopher Ferris241a9582015-04-27 15:19:41 -0700134 uint8_t* memory = reinterpret_cast<uint8_t*>(sf);
135 delete[] memory;
136 }
137
Andreas Gampeb3025922015-09-01 14:45:00 -0700138 // Create a shadow frame in a fresh alloca. This needs to be in the context of the caller.
139 // Inlining doesn't work, the compiler will still undo the alloca. So this needs to be a macro.
140#define CREATE_SHADOW_FRAME(num_vregs, link, method, dex_pc) ({ \
141 size_t frame_size = ShadowFrame::ComputeSize(num_vregs); \
142 void* alloca_mem = alloca(frame_size); \
143 ShadowFrameAllocaUniquePtr( \
144 ShadowFrame::CreateShadowFrameImpl((num_vregs), (link), (method), (dex_pc), \
145 (alloca_mem))); \
146 })
147
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700148 ~ShadowFrame() {}
149
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700150 // TODO(iam): Clean references array up since they're always there,
151 // we don't need to do conditionals.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800152 bool HasReferenceArray() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700153 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700154 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700155
TDYa127ce4cc0d2012-11-18 16:59:53 -0800156 uint32_t NumberOfVRegs() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700157 return number_of_vregs_;
Ian Rogers5438ad82012-10-15 17:22:44 -0700158 }
159
Ian Rogers0399dde2012-06-06 17:09:28 -0700160 uint32_t GetDexPC() const {
buzbee1452bee2015-03-06 14:43:04 -0800161 return (dex_pc_ptr_ == nullptr) ? dex_pc_ : dex_pc_ptr_ - code_item_->insns_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700162 }
163
Bill Buzbee1d011d92016-04-04 16:59:29 +0000164 int16_t GetCachedHotnessCountdown() const {
165 return cached_hotness_countdown_;
166 }
167
168 void SetCachedHotnessCountdown(int16_t cached_hotness_countdown) {
169 cached_hotness_countdown_ = cached_hotness_countdown;
170 }
171
172 int16_t GetHotnessCountdown() const {
173 return hotness_countdown_;
174 }
175
176 void SetHotnessCountdown(int16_t hotness_countdown) {
177 hotness_countdown_ = hotness_countdown;
178 }
179
Ian Rogers0399dde2012-06-06 17:09:28 -0700180 void SetDexPC(uint32_t dex_pc) {
181 dex_pc_ = dex_pc;
buzbee1452bee2015-03-06 14:43:04 -0800182 dex_pc_ptr_ = nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -0700183 }
184
Ian Rogers0399dde2012-06-06 17:09:28 -0700185 ShadowFrame* GetLink() const {
186 return link_;
187 }
188
189 void SetLink(ShadowFrame* frame) {
190 DCHECK_NE(this, frame);
191 link_ = frame;
192 }
193
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700194 int32_t GetVReg(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800195 DCHECK_LT(i, NumberOfVRegs());
196 const uint32_t* vreg = &vregs_[i];
197 return *reinterpret_cast<const int32_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700198 }
199
buzbee1452bee2015-03-06 14:43:04 -0800200 uint32_t* GetVRegAddr(size_t i) {
201 return &vregs_[i];
202 }
203
204 uint32_t* GetShadowRefAddr(size_t i) {
205 DCHECK(HasReferenceArray());
206 DCHECK_LT(i, NumberOfVRegs());
207 return &vregs_[i + NumberOfVRegs()];
208 }
209
210 void SetCodeItem(const DexFile::CodeItem* code_item) {
211 code_item_ = code_item;
212 }
213
Bill Buzbeed47fd902016-07-07 14:42:43 +0000214 const DexFile::CodeItem* GetCodeItem() const {
215 return code_item_;
216 }
217
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700218 float GetVRegFloat(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800219 DCHECK_LT(i, NumberOfVRegs());
220 // NOTE: Strict-aliasing?
221 const uint32_t* vreg = &vregs_[i];
222 return *reinterpret_cast<const float*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700223 }
224
225 int64_t GetVRegLong(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200226 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800227 const uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700228 typedef const int64_t unaligned_int64 __attribute__ ((aligned (4)));
229 return *reinterpret_cast<unaligned_int64*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700230 }
231
232 double GetVRegDouble(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200233 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800234 const uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700235 typedef const double unaligned_double __attribute__ ((aligned (4)));
236 return *reinterpret_cast<unaligned_double*>(vreg);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800237 }
238
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700239 // Look up the reference given its virtual register number.
240 // If this returns non-null then this does not mean the vreg is currently a reference
241 // 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 -0800242 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700243 mirror::Object* GetVRegReference(size_t i) const REQUIRES_SHARED(Locks::mutator_lock_) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800244 DCHECK_LT(i, NumberOfVRegs());
Mathieu Chartier4e305412014-02-19 10:54:44 -0800245 mirror::Object* ref;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800246 if (HasReferenceArray()) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800247 ref = References()[i].AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800248 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800249 const uint32_t* vreg_ptr = &vregs_[i];
Mathieu Chartier4e305412014-02-19 10:54:44 -0800250 ref = reinterpret_cast<const StackReference<mirror::Object>*>(vreg_ptr)->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800251 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800252 if (kUseReadBarrier) {
253 ReadBarrier::AssertToSpaceInvariant(ref);
254 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800255 if (kVerifyFlags & kVerifyReads) {
256 VerifyObject(ref);
257 }
258 return ref;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700259 }
260
Jeff Hao16743632013-05-08 10:59:04 -0700261 // Get view of vregs as range of consecutive arguments starting at i.
262 uint32_t* GetVRegArgs(size_t i) {
263 return &vregs_[i];
264 }
265
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700266 void SetVReg(size_t i, int32_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800267 DCHECK_LT(i, NumberOfVRegs());
268 uint32_t* vreg = &vregs_[i];
269 *reinterpret_cast<int32_t*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700270 // This is needed for moving collectors since these can update the vreg references if they
271 // happen to agree with references in the reference array.
272 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800273 References()[i].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700274 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700275 }
276
277 void SetVRegFloat(size_t i, float val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800278 DCHECK_LT(i, NumberOfVRegs());
279 uint32_t* vreg = &vregs_[i];
280 *reinterpret_cast<float*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700281 // This is needed for moving collectors since these can update the vreg references if they
282 // happen to agree with references in the reference array.
283 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800284 References()[i].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700285 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700286 }
287
288 void SetVRegLong(size_t i, int64_t val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200289 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800290 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700291 typedef int64_t unaligned_int64 __attribute__ ((aligned (4)));
292 *reinterpret_cast<unaligned_int64*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700293 // This is needed for moving collectors since these can update the vreg references if they
294 // happen to agree with references in the reference array.
295 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800296 References()[i].Clear();
297 References()[i + 1].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700298 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700299 }
300
301 void SetVRegDouble(size_t i, double val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200302 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800303 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700304 typedef double unaligned_double __attribute__ ((aligned (4)));
305 *reinterpret_cast<unaligned_double*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700306 // This is needed for moving collectors since these can update the vreg references if they
307 // happen to agree with references in the reference array.
308 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800309 References()[i].Clear();
310 References()[i + 1].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700311 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700312 }
313
Mathieu Chartier4e305412014-02-19 10:54:44 -0800314 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700315 void SetVRegReference(size_t i, mirror::Object* val) REQUIRES_SHARED(Locks::mutator_lock_) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800316 DCHECK_LT(i, NumberOfVRegs());
Mathieu Chartier4e305412014-02-19 10:54:44 -0800317 if (kVerifyFlags & kVerifyWrites) {
318 VerifyObject(val);
319 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800320 if (kUseReadBarrier) {
321 ReadBarrier::AssertToSpaceInvariant(val);
322 }
TDYa127ce4cc0d2012-11-18 16:59:53 -0800323 uint32_t* vreg = &vregs_[i];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800324 reinterpret_cast<StackReference<mirror::Object>*>(vreg)->Assign(val);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800325 if (HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800326 References()[i].Assign(val);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800327 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700328 }
329
Alex Lightdba61482016-12-21 08:20:29 -0800330 void SetMethod(ArtMethod* method) REQUIRES(Locks::mutator_lock_) {
331 DCHECK(method != nullptr);
332 DCHECK(method_ != nullptr);
333 method_ = method;
334 }
335
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700336 ArtMethod* GetMethod() const REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800337 DCHECK(method_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700338 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700339 }
340
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700341 mirror::Object* GetThisObject() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800342
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700343 mirror::Object* GetThisObject(uint16_t num_ins) const REQUIRES_SHARED(Locks::mutator_lock_);
Jeff Haoe701f482013-05-24 11:50:49 -0700344
Ian Rogersef7d42f2014-01-06 12:55:46 -0800345 bool Contains(StackReference<mirror::Object>* shadow_frame_entry_obj) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800346 if (HasReferenceArray()) {
347 return ((&References()[0] <= shadow_frame_entry_obj) &&
348 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
349 } else {
350 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
351 return ((&vregs_[0] <= shadow_frame_entry) &&
352 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
Ian Rogers0399dde2012-06-06 17:09:28 -0700353 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700354 }
355
Andreas Gampe03ec9302015-08-27 17:41:47 -0700356 LockCountData& GetLockCountData() {
357 return lock_count_data_;
358 }
359
buzbee1452bee2015-03-06 14:43:04 -0800360 static size_t LockCountDataOffset() {
361 return OFFSETOF_MEMBER(ShadowFrame, lock_count_data_);
362 }
363
Ian Rogers0399dde2012-06-06 17:09:28 -0700364 static size_t LinkOffset() {
365 return OFFSETOF_MEMBER(ShadowFrame, link_);
366 }
367
Ian Rogers0399dde2012-06-06 17:09:28 -0700368 static size_t MethodOffset() {
369 return OFFSETOF_MEMBER(ShadowFrame, method_);
370 }
371
Ian Rogers0399dde2012-06-06 17:09:28 -0700372 static size_t DexPCOffset() {
373 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
374 }
375
Ian Rogers5438ad82012-10-15 17:22:44 -0700376 static size_t NumberOfVRegsOffset() {
377 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
378 }
379
TDYa127ce4cc0d2012-11-18 16:59:53 -0800380 static size_t VRegsOffset() {
381 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
Ian Rogers5438ad82012-10-15 17:22:44 -0700382 }
383
buzbee1452bee2015-03-06 14:43:04 -0800384 static size_t ResultRegisterOffset() {
385 return OFFSETOF_MEMBER(ShadowFrame, result_register_);
386 }
387
388 static size_t DexPCPtrOffset() {
389 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_ptr_);
390 }
391
392 static size_t CodeItemOffset() {
393 return OFFSETOF_MEMBER(ShadowFrame, code_item_);
394 }
395
Bill Buzbee1d011d92016-04-04 16:59:29 +0000396 static size_t CachedHotnessCountdownOffset() {
397 return OFFSETOF_MEMBER(ShadowFrame, cached_hotness_countdown_);
398 }
399
400 static size_t HotnessCountdownOffset() {
401 return OFFSETOF_MEMBER(ShadowFrame, hotness_countdown_);
402 }
403
Andreas Gampeb3025922015-09-01 14:45:00 -0700404 // Create ShadowFrame for interpreter using provided memory.
405 static ShadowFrame* CreateShadowFrameImpl(uint32_t num_vregs,
406 ShadowFrame* link,
407 ArtMethod* method,
408 uint32_t dex_pc,
409 void* memory) {
410 return new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true);
411 }
412
Bill Buzbee1d011d92016-04-04 16:59:29 +0000413 const uint16_t* GetDexPCPtr() {
buzbee1452bee2015-03-06 14:43:04 -0800414 return dex_pc_ptr_;
415 }
416
Bill Buzbeed47fd902016-07-07 14:42:43 +0000417 void SetDexPCPtr(uint16_t* dex_pc_ptr) {
418 dex_pc_ptr_ = dex_pc_ptr;
419 }
420
buzbee1452bee2015-03-06 14:43:04 -0800421 JValue* GetResultRegister() {
422 return result_register_;
423 }
424
Elliott Hughes68e76522011-10-05 13:22:16 -0700425 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700426 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, ArtMethod* method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800427 uint32_t dex_pc, bool has_reference_array)
buzbee1452bee2015-03-06 14:43:04 -0800428 : link_(link), method_(method), result_register_(nullptr), dex_pc_ptr_(nullptr),
429 code_item_(nullptr), number_of_vregs_(num_vregs), dex_pc_(dex_pc) {
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700430 // TODO(iam): Remove this parameter, it's an an artifact of portable removal
431 DCHECK(has_reference_array);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800432 if (has_reference_array) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800433 memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(StackReference<mirror::Object>)));
Mathieu Chartier67022432012-11-29 18:04:50 -0800434 } else {
Jeff Haoe701f482013-05-24 11:50:49 -0700435 memset(vregs_, 0, num_vregs * sizeof(uint32_t));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700436 }
437 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700438
Ian Rogersef7d42f2014-01-06 12:55:46 -0800439 const StackReference<mirror::Object>* References() const {
Mathieu Chartier67022432012-11-29 18:04:50 -0800440 DCHECK(HasReferenceArray());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800441 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800442 return reinterpret_cast<const StackReference<mirror::Object>*>(vreg_end);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800443 }
444
Ian Rogersef7d42f2014-01-06 12:55:46 -0800445 StackReference<mirror::Object>* References() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700446 return const_cast<StackReference<mirror::Object>*>(
447 const_cast<const ShadowFrame*>(this)->References());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800448 }
449
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700450 // Link to previous shadow frame or null.
Ian Rogers0399dde2012-06-06 17:09:28 -0700451 ShadowFrame* link_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700452 ArtMethod* method_;
buzbee1452bee2015-03-06 14:43:04 -0800453 JValue* result_register_;
Bill Buzbee1d011d92016-04-04 16:59:29 +0000454 const uint16_t* dex_pc_ptr_;
buzbee1452bee2015-03-06 14:43:04 -0800455 const DexFile::CodeItem* code_item_;
Andreas Gampe03ec9302015-08-27 17:41:47 -0700456 LockCountData lock_count_data_; // This may contain GC roots when lock counting is active.
buzbee1452bee2015-03-06 14:43:04 -0800457 const uint32_t number_of_vregs_;
458 uint32_t dex_pc_;
Bill Buzbee1d011d92016-04-04 16:59:29 +0000459 int16_t cached_hotness_countdown_;
460 int16_t hotness_countdown_;
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700461
462 // This is a two-part array:
463 // - [0..number_of_vregs) holds the raw virtual registers, and each element here is always 4
464 // bytes.
465 // - [number_of_vregs..number_of_vregs*2) holds only reference registers. Each element here is
466 // ptr-sized.
467 // In other words when a primitive is stored in vX, the second (reference) part of the array will
468 // be null. When a reference is stored in vX, the second (reference) part of the array will be a
469 // copy of vX.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800470 uint32_t vregs_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700471
Ian Rogers0399dde2012-06-06 17:09:28 -0700472 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700473};
474
Andreas Gampeb3025922015-09-01 14:45:00 -0700475struct ShadowFrameDeleter {
476 inline void operator()(ShadowFrame* frame) {
477 if (frame != nullptr) {
478 frame->~ShadowFrame();
479 }
480 }
481};
482
Andreas Gampe140da3b2016-11-08 16:01:00 -0800483class JavaFrameRootInfo FINAL : public RootInfo {
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800484 public:
485 JavaFrameRootInfo(uint32_t thread_id, const StackVisitor* stack_visitor, size_t vreg)
486 : RootInfo(kRootJavaFrame, thread_id), stack_visitor_(stack_visitor), vreg_(vreg) {
487 }
Andreas Gampe140da3b2016-11-08 16:01:00 -0800488 void Describe(std::ostream& os) const OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700489 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800490
Andreas Gampe140da3b2016-11-08 16:01:00 -0800491 size_t GetVReg() const {
492 return vreg_;
493 }
494 const StackVisitor* GetVisitor() const {
495 return stack_visitor_;
496 }
497
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800498 private:
499 const StackVisitor* const stack_visitor_;
500 const size_t vreg_;
501};
502
Ian Rogers0399dde2012-06-06 17:09:28 -0700503// The managed stack is used to record fragments of managed code stacks. Managed code stacks
504// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
505// necessary for transitions between code using different frame layouts and transitions into native
506// code.
Ian Rogersdf1ce912012-11-27 17:07:11 -0800507class PACKED(4) ManagedStack {
Ian Rogers0399dde2012-06-06 17:09:28 -0700508 public:
Ian Rogersca190662012-06-26 15:45:57 -0700509 ManagedStack()
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700510 : top_quick_frame_(nullptr), link_(nullptr), top_shadow_frame_(nullptr) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700511
512 void PushManagedStackFragment(ManagedStack* fragment) {
513 // Copy this top fragment into given fragment.
514 memcpy(fragment, this, sizeof(ManagedStack));
515 // Clear this fragment, which has become the top.
516 memset(this, 0, sizeof(ManagedStack));
517 // Link our top fragment onto the given fragment.
518 link_ = fragment;
519 }
520
521 void PopManagedStackFragment(const ManagedStack& fragment) {
522 DCHECK(&fragment == link_);
523 // Copy this given fragment back to the top.
524 memcpy(this, &fragment, sizeof(ManagedStack));
525 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700526
527 ManagedStack* GetLink() const {
528 return link_;
529 }
530
Mathieu Chartiere401d142015-04-22 13:56:20 -0700531 ArtMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700532 return top_quick_frame_;
533 }
534
Mathieu Chartiere401d142015-04-22 13:56:20 -0700535 void SetTopQuickFrame(ArtMethod** top) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700536 DCHECK(top_shadow_frame_ == nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700537 top_quick_frame_ = top;
538 }
539
Ian Rogers0399dde2012-06-06 17:09:28 -0700540 static size_t TopQuickFrameOffset() {
541 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
542 }
543
Ian Rogers0399dde2012-06-06 17:09:28 -0700544 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700545 DCHECK(top_quick_frame_ == nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700546 ShadowFrame* old_frame = top_shadow_frame_;
547 top_shadow_frame_ = new_top_frame;
548 new_top_frame->SetLink(old_frame);
549 return old_frame;
550 }
551
552 ShadowFrame* PopShadowFrame() {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700553 DCHECK(top_quick_frame_ == nullptr);
554 CHECK(top_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700555 ShadowFrame* frame = top_shadow_frame_;
556 top_shadow_frame_ = frame->GetLink();
557 return frame;
558 }
559
560 ShadowFrame* GetTopShadowFrame() const {
561 return top_shadow_frame_;
562 }
563
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800564 void SetTopShadowFrame(ShadowFrame* top) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700565 DCHECK(top_quick_frame_ == nullptr);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800566 top_shadow_frame_ = top;
567 }
568
Ian Rogers0399dde2012-06-06 17:09:28 -0700569 static size_t TopShadowFrameOffset() {
570 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
571 }
572
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700573 size_t NumJniShadowFrameReferences() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700574
Ian Rogersef7d42f2014-01-06 12:55:46 -0800575 bool ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700576
577 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700578 ArtMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700579 ManagedStack* link_;
580 ShadowFrame* top_shadow_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700581};
582
583class StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100584 public:
585 // This enum defines a flag to control whether inlined frames are included
586 // when walking the stack.
587 enum class StackWalkKind {
588 kIncludeInlinedFrames,
589 kSkipInlinedFrames,
590 };
591
Ian Rogers0399dde2012-06-06 17:09:28 -0700592 protected:
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800593 StackVisitor(Thread* thread,
594 Context* context,
595 StackWalkKind walk_kind,
596 bool check_suspended = true);
Ian Rogers0399dde2012-06-06 17:09:28 -0700597
Nicolas Geoffray33856502015-10-20 15:52:58 +0100598 bool GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700599 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray33856502015-10-20 15:52:58 +0100600
Ian Rogers0399dde2012-06-06 17:09:28 -0700601 public:
602 virtual ~StackVisitor() {}
603
604 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700605 virtual bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700606
Andreas Gampe585da952016-12-02 14:52:29 -0800607 enum class CountTransitions {
608 kYes,
609 kNo,
610 };
611
612 template <CountTransitions kCount = CountTransitions::kYes>
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700613 void WalkStack(bool include_transitions = false)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700614 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700615
Sebastien Hertz26f72862015-09-15 09:52:07 +0200616 Thread* GetThread() const {
617 return thread_;
618 }
619
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700620 ArtMethod* GetMethod() const REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi92d1a662014-05-15 21:43:59 -0700621
Alex Lightdba61482016-12-21 08:20:29 -0800622 // Sets this stack frame's method pointer. This requires a full lock of the MutatorLock. This
623 // doesn't work with inlined methods.
624 void SetMethod(ArtMethod* method) REQUIRES(Locks::mutator_lock_);
625
Nicolas Geoffrayccc61972015-10-01 14:34:20 +0100626 ArtMethod* GetOuterMethod() const {
627 return *GetCurrentQuickFrame();
628 }
629
Ian Rogers0399dde2012-06-06 17:09:28 -0700630 bool IsShadowFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800631 return cur_shadow_frame_ != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -0700632 }
633
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700634 uint32_t GetDexPc(bool abort_on_failure = true) const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700635
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700636 mirror::Object* GetThisObject() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800637
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700638 size_t GetNativePcOffset() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700639
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700640 // Returns the height of the stack in the managed stack frames, including transitions.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700641 size_t GetFrameHeight() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800642 return GetNumFrames() - cur_depth_ - 1;
Ian Rogers0399dde2012-06-06 17:09:28 -0700643 }
644
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700645 // Returns a frame ID for JDWP use, starting from 1.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700646 size_t GetFrameId() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700647 return GetFrameHeight() + 1;
648 }
649
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700650 size_t GetNumFrames() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700651 if (num_frames_ == 0) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100652 num_frames_ = ComputeNumFrames(thread_, walk_kind_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700653 }
654 return num_frames_;
655 }
656
Andreas Gampe140da3b2016-11-08 16:01:00 -0800657 size_t GetFrameDepth() const REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700658 return cur_depth_;
659 }
660
Ian Rogers5cf98192014-05-29 21:31:50 -0700661 // Get the method and dex pc immediately after the one that's currently being visited.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700662 bool GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700663 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700664
Mathieu Chartiere401d142015-04-22 13:56:20 -0700665 bool GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700666 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700667
Mathieu Chartiere401d142015-04-22 13:56:20 -0700668 bool GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo, VRegKind kind_hi,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200669 uint64_t* val) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700670 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200671
Mingyao Yang636b9252015-07-31 16:40:24 -0700672 // Values will be set in debugger shadow frames. Debugger will make sure deoptimization
673 // is triggered to make the values effective.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700674 bool SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700675 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700676
Mingyao Yang99170c62015-07-06 11:10:37 -0700677 // Values will be set in debugger shadow frames. Debugger will make sure deoptimization
678 // is triggered to make the values effective.
Mingyao Yang636b9252015-07-31 16:40:24 -0700679 bool SetVRegPair(ArtMethod* m,
680 uint16_t vreg,
681 uint64_t new_value,
682 VRegKind kind_lo,
683 VRegKind kind_hi)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700684 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang99170c62015-07-06 11:10:37 -0700685
Mathieu Chartier815873e2014-02-13 18:02:13 -0800686 uintptr_t* GetGPRAddress(uint32_t reg) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700687
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700688 // This is a fast-path for getting/setting values in a quick frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700689 uint32_t* GetVRegAddrFromQuickCode(ArtMethod** cur_quick_frame,
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000690 const DexFile::CodeItem* code_item,
691 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
692 uint16_t vreg) const {
693 int offset = GetVRegOffsetFromQuickCode(
694 code_item, core_spills, fp_spills, frame_size, vreg, kRuntimeISA);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700695 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
Ian Rogers13735952014-10-08 12:43:28 -0700696 uint8_t* vreg_addr = reinterpret_cast<uint8_t*>(cur_quick_frame) + offset;
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700697 return reinterpret_cast<uint32_t*>(vreg_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700698 }
699
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700700 uintptr_t GetReturnPc() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700701
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700702 void SetReturnPc(uintptr_t new_ret_pc) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700703
704 /*
705 * Return sp-relative offset for a Dalvik virtual register, compiler
706 * spill or Method* in bytes using Method*.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700707 * Note that (reg == -1) denotes an invalid Dalvik register. For the
708 * positive values, the Dalvik registers come first, followed by the
709 * Method*, followed by other special temporaries if any, followed by
710 * regular compiler temporary. As of now we only have the Method* as
711 * as a special compiler temporary.
712 * A compiler temporary can be thought of as a virtual register that
713 * does not exist in the dex but holds intermediate values to help
714 * optimizations and code generation. A special compiler temporary is
715 * one whose location in frame is well known while non-special ones
716 * do not have a requirement on location in frame as long as code
717 * generator itself knows how to access them.
Ian Rogers0399dde2012-06-06 17:09:28 -0700718 *
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700719 * +-------------------------------+
720 * | IN[ins-1] | {Note: resides in caller's frame}
721 * | . |
722 * | IN[0] |
Mathieu Chartiere401d142015-04-22 13:56:20 -0700723 * | caller's ArtMethod | ... ArtMethod*
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700724 * +===============================+ {Note: start of callee's frame}
725 * | core callee-save spill | {variable sized}
726 * +-------------------------------+
727 * | fp callee-save spill |
728 * +-------------------------------+
729 * | filler word | {For compatibility, if V[locals-1] used as wide
730 * +-------------------------------+
731 * | V[locals-1] |
732 * | V[locals-2] |
733 * | . |
734 * | . | ... (reg == 2)
735 * | V[1] | ... (reg == 1)
736 * | V[0] | ... (reg == 0) <---- "locals_start"
737 * +-------------------------------+
738 * | stack alignment padding | {0 to (kStackAlignWords-1) of padding}
739 * +-------------------------------+
740 * | Compiler temp region | ... (reg >= max_num_special_temps)
741 * | . |
742 * | . |
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700743 * | V[max_num_special_temps + 1] |
744 * | V[max_num_special_temps + 0] |
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700745 * +-------------------------------+
746 * | OUT[outs-1] |
747 * | OUT[outs-2] |
748 * | . |
749 * | OUT[0] |
Mathieu Chartiere401d142015-04-22 13:56:20 -0700750 * | ArtMethod* | ... (reg == num_total_code_regs == special_temp_value) <<== sp, 16-byte aligned
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700751 * +===============================+
Ian Rogers0399dde2012-06-06 17:09:28 -0700752 */
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000753 static int GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
754 uint32_t core_spills, uint32_t fp_spills,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700755 size_t frame_size, int reg, InstructionSet isa);
Ian Rogers0399dde2012-06-06 17:09:28 -0700756
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000757 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
buzbee82818642014-06-04 15:35:41 -0700758 // According to stack model, the first out is above the Method referernce.
Andreas Gampe542451c2016-07-26 09:02:02 -0700759 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800760 }
761
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100762 bool IsInInlinedFrame() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100763 return current_inlining_depth_ != 0;
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100764 }
765
David Brazdilefc3f022015-10-28 12:19:06 -0500766 size_t GetCurrentInliningDepth() const {
767 return current_inlining_depth_;
768 }
769
Ian Rogers0399dde2012-06-06 17:09:28 -0700770 uintptr_t GetCurrentQuickFramePc() const {
771 return cur_quick_frame_pc_;
772 }
773
Mathieu Chartiere401d142015-04-22 13:56:20 -0700774 ArtMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700775 return cur_quick_frame_;
776 }
777
778 ShadowFrame* GetCurrentShadowFrame() const {
779 return cur_shadow_frame_;
780 }
781
Mathieu Chartiere401d142015-04-22 13:56:20 -0700782 HandleScope* GetCurrentHandleScope(size_t pointer_size) const {
783 ArtMethod** sp = GetCurrentQuickFrame();
784 // Skip ArtMethod*; handle scope comes next;
785 return reinterpret_cast<HandleScope*>(reinterpret_cast<uintptr_t>(sp) + pointer_size);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700786 }
787
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700788 std::string DescribeLocation() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers40e3bac2012-11-20 00:09:14 -0800789
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100790 static size_t ComputeNumFrames(Thread* thread, StackWalkKind walk_kind)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700791 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800792
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700793 static void DescribeStack(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800794
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100795 const OatQuickMethodHeader* GetCurrentOatQuickMethodHeader() const {
796 return cur_oat_quick_method_header_;
797 }
798
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700799 QuickMethodFrameInfo GetCurrentQuickFrameInfo() const REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100800
Ian Rogers0399dde2012-06-06 17:09:28 -0700801 private:
Ian Rogers5cf98192014-05-29 21:31:50 -0700802 // Private constructor known in the case that num_frames_ has already been computed.
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800803 StackVisitor(Thread* thread,
804 Context* context,
805 StackWalkKind walk_kind,
806 size_t num_frames,
807 bool check_suspended = true)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700808 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700809
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100810 bool IsAccessibleRegister(uint32_t reg, bool is_float) const {
811 return is_float ? IsAccessibleFPR(reg) : IsAccessibleGPR(reg);
812 }
813 uintptr_t GetRegister(uint32_t reg, bool is_float) const {
814 DCHECK(IsAccessibleRegister(reg, is_float));
815 return is_float ? GetFPR(reg) : GetGPR(reg);
816 }
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100817
818 bool IsAccessibleGPR(uint32_t reg) const;
819 uintptr_t GetGPR(uint32_t reg) const;
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100820
821 bool IsAccessibleFPR(uint32_t reg) const;
822 uintptr_t GetFPR(uint32_t reg) const;
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200823
Mingyao Yang99170c62015-07-06 11:10:37 -0700824 bool GetVRegFromDebuggerShadowFrame(uint16_t vreg, VRegKind kind, uint32_t* val) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700825 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700826 bool GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100827 uint32_t* val) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700828 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100829
Mingyao Yang99170c62015-07-06 11:10:37 -0700830 bool GetVRegPairFromDebuggerShadowFrame(uint16_t vreg, VRegKind kind_lo, VRegKind kind_hi,
831 uint64_t* val) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700832 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700833 bool GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100834 VRegKind kind_lo, VRegKind kind_hi,
835 uint64_t* val) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700836 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100837 bool GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi, VRegKind kind_lo,
838 uint64_t* val) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700839 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100840
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700841 void SanityCheckFrame() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700842
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700843 InlineInfo GetCurrentInlineInfo() const REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100844
Ian Rogers7a22fa62013-01-23 12:16:16 -0800845 Thread* const thread_;
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100846 const StackWalkKind walk_kind_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700847 ShadowFrame* cur_shadow_frame_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700848 ArtMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700849 uintptr_t cur_quick_frame_pc_;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100850 const OatQuickMethodHeader* cur_oat_quick_method_header_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700851 // Lazily computed, number of frames in the stack.
852 size_t num_frames_;
853 // Depth of the frame we're currently at.
854 size_t cur_depth_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100855 // Current inlining depth of the method we are currently at.
856 // 0 if there is no inlined frame.
857 size_t current_inlining_depth_;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700858
Ian Rogers0399dde2012-06-06 17:09:28 -0700859 protected:
860 Context* const context_;
Hiroshi Yamauchi02f365f2017-02-03 15:06:00 -0800861 const bool check_suspended_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700862};
863
Elliott Hughes68e76522011-10-05 13:22:16 -0700864} // namespace art
865
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700866#endif // ART_RUNTIME_STACK_H_