blob: c594ec6a39ad54463e2c52b4127d0da48ac122e2 [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"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "mirror/object_reference.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010029#include "quick/quick_method_frame_info.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080030#include "read_barrier.h"
Ian Rogerse63db272014-07-15 15:36:11 -070031#include "verify_object.h"
32
Elliott Hughes68e76522011-10-05 13:22:16 -070033namespace art {
34
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070036 class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037} // namespace mirror
38
Mathieu Chartiere401d142015-04-22 13:56:20 -070039class ArtMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040class Context;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070041class HandleScope;
Nicolas Geoffray57f61612015-05-15 13:20:41 +010042class InlineInfo;
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;
48
Ian Rogers2bcb4a42012-11-08 10:39:18 -080049// The kind of vreg being accessed in calls to Set/GetVReg.
50enum VRegKind {
51 kReferenceVReg,
52 kIntVReg,
53 kFloatVReg,
54 kLongLoVReg,
55 kLongHiVReg,
56 kDoubleLoVReg,
57 kDoubleHiVReg,
58 kConstant,
59 kImpreciseConstant,
60 kUndefined,
61};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070062std::ostream& operator<<(std::ostream& os, const VRegKind& rhs);
Ian Rogers2bcb4a42012-11-08 10:39:18 -080063
Ian Rogersef7d42f2014-01-06 12:55:46 -080064// A reference from the shadow stack to a MirrorType object within the Java heap.
65template<class MirrorType>
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070066class MANAGED StackReference : public mirror::CompressedReference<MirrorType> {
Ian Rogersef7d42f2014-01-06 12:55:46 -080067};
68
Andreas Gampeb3025922015-09-01 14:45:00 -070069// Forward declaration. Just calls the destructor.
70struct ShadowFrameDeleter;
71using ShadowFrameAllocaUniquePtr = std::unique_ptr<ShadowFrame, ShadowFrameDeleter>;
72
Andreas Gampe03ec9302015-08-27 17:41:47 -070073// Counting locks by storing object pointers into a vector. Duplicate entries mark recursive locks.
74// The vector will be visited with the ShadowFrame during GC (so all the locked-on objects are
75// thread roots).
76// Note: implementation is split so that the call sites may be optimized to no-ops in case no
77// lock counting is necessary. The actual implementation is in the cc file to avoid
78// dependencies.
79class LockCountData {
80 public:
81 // Add the given object to the list of monitors, that is, objects that have been locked. This
82 // will not throw (but be skipped if there is an exception pending on entry).
Andreas Gampe56fdd0e2016-04-28 14:56:54 -070083 void AddMonitor(Thread* self, mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampe03ec9302015-08-27 17:41:47 -070084
85 // Try to remove the given object from the monitor list, indicating an unlock operation.
86 // This will throw an IllegalMonitorStateException (clearing any already pending exception), in
87 // case that there wasn't a lock recorded for the object.
Andreas Gampe03ec9302015-08-27 17:41:47 -070088 void RemoveMonitorOrThrow(Thread* self,
Andreas Gampe56fdd0e2016-04-28 14:56:54 -070089 const mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampe03ec9302015-08-27 17:41:47 -070090
91 // Check whether all acquired monitors have been released. This will potentially throw an
92 // IllegalMonitorStateException, clearing any already pending exception. Returns true if the
93 // check shows that everything is OK wrt/ lock counting, false otherwise.
Andreas Gampe56fdd0e2016-04-28 14:56:54 -070094 bool CheckAllMonitorsReleasedOrThrow(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampe03ec9302015-08-27 17:41:47 -070095
96 template <typename T, typename... Args>
97 void VisitMonitors(T visitor, Args&&... args) SHARED_REQUIRES(Locks::mutator_lock_) {
98 if (monitors_ != nullptr) {
99 // Visitors may change the Object*. Be careful with the foreach loop.
100 for (mirror::Object*& obj : *monitors_) {
101 visitor(/* inout */ &obj, std::forward<Args>(args)...);
102 }
103 }
104 }
105
106 private:
Andreas Gampe03ec9302015-08-27 17:41:47 -0700107 // Stores references to the locked-on objects. As noted, this should be visited during thread
108 // marking.
109 std::unique_ptr<std::vector<mirror::Object*>> monitors_;
110};
111
Elliott Hughes956af0f2014-12-11 14:34:28 -0800112// ShadowFrame has 2 possible layouts:
Mathieu Chartier67022432012-11-29 18:04:50 -0800113// - interpreter - separate VRegs and reference arrays. References are in the reference array.
114// - JNI - just VRegs, but where every VReg holds a reference.
Ian Rogers0399dde2012-06-06 17:09:28 -0700115class ShadowFrame {
Elliott Hughes68e76522011-10-05 13:22:16 -0700116 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800117 // Compute size of ShadowFrame in bytes assuming it has a reference array.
Jeff Hao66135192013-05-14 11:02:41 -0700118 static size_t ComputeSize(uint32_t num_vregs) {
119 return sizeof(ShadowFrame) + (sizeof(uint32_t) * num_vregs) +
Ian Rogersef7d42f2014-01-06 12:55:46 -0800120 (sizeof(StackReference<mirror::Object>) * num_vregs);
Jeff Hao66135192013-05-14 11:02:41 -0700121 }
122
123 // Create ShadowFrame in heap for deoptimization.
Christopher Ferris241a9582015-04-27 15:19:41 -0700124 static ShadowFrame* CreateDeoptimizedFrame(uint32_t num_vregs, ShadowFrame* link,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700125 ArtMethod* method, uint32_t dex_pc) {
Jeff Hao66135192013-05-14 11:02:41 -0700126 uint8_t* memory = new uint8_t[ComputeSize(num_vregs)];
Andreas Gampeb3025922015-09-01 14:45:00 -0700127 return CreateShadowFrameImpl(num_vregs, link, method, dex_pc, memory);
Jeff Hao66135192013-05-14 11:02:41 -0700128 }
129
Christopher Ferris241a9582015-04-27 15:19:41 -0700130 // Delete a ShadowFrame allocated on the heap for deoptimization.
131 static void DeleteDeoptimizedFrame(ShadowFrame* sf) {
Andreas Gampeb3025922015-09-01 14:45:00 -0700132 sf->~ShadowFrame(); // Explicitly destruct.
Christopher Ferris241a9582015-04-27 15:19:41 -0700133 uint8_t* memory = reinterpret_cast<uint8_t*>(sf);
134 delete[] memory;
135 }
136
Andreas Gampeb3025922015-09-01 14:45:00 -0700137 // Create a shadow frame in a fresh alloca. This needs to be in the context of the caller.
138 // Inlining doesn't work, the compiler will still undo the alloca. So this needs to be a macro.
139#define CREATE_SHADOW_FRAME(num_vregs, link, method, dex_pc) ({ \
140 size_t frame_size = ShadowFrame::ComputeSize(num_vregs); \
141 void* alloca_mem = alloca(frame_size); \
142 ShadowFrameAllocaUniquePtr( \
143 ShadowFrame::CreateShadowFrameImpl((num_vregs), (link), (method), (dex_pc), \
144 (alloca_mem))); \
145 })
146
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700147 ~ShadowFrame() {}
148
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700149 // TODO(iam): Clean references array up since they're always there,
150 // we don't need to do conditionals.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800151 bool HasReferenceArray() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700152 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700153 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700154
TDYa127ce4cc0d2012-11-18 16:59:53 -0800155 uint32_t NumberOfVRegs() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700156 return number_of_vregs_;
Ian Rogers5438ad82012-10-15 17:22:44 -0700157 }
158
Ian Rogers0399dde2012-06-06 17:09:28 -0700159 uint32_t GetDexPC() const {
buzbee1452bee2015-03-06 14:43:04 -0800160 return (dex_pc_ptr_ == nullptr) ? dex_pc_ : dex_pc_ptr_ - code_item_->insns_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700161 }
162
Bill Buzbee1d011d92016-04-04 16:59:29 +0000163 int16_t GetCachedHotnessCountdown() const {
164 return cached_hotness_countdown_;
165 }
166
167 void SetCachedHotnessCountdown(int16_t cached_hotness_countdown) {
168 cached_hotness_countdown_ = cached_hotness_countdown;
169 }
170
171 int16_t GetHotnessCountdown() const {
172 return hotness_countdown_;
173 }
174
175 void SetHotnessCountdown(int16_t hotness_countdown) {
176 hotness_countdown_ = hotness_countdown;
177 }
178
Ian Rogers0399dde2012-06-06 17:09:28 -0700179 void SetDexPC(uint32_t dex_pc) {
180 dex_pc_ = dex_pc;
buzbee1452bee2015-03-06 14:43:04 -0800181 dex_pc_ptr_ = nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -0700182 }
183
Ian Rogers0399dde2012-06-06 17:09:28 -0700184 ShadowFrame* GetLink() const {
185 return link_;
186 }
187
188 void SetLink(ShadowFrame* frame) {
189 DCHECK_NE(this, frame);
190 link_ = frame;
191 }
192
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700193 int32_t GetVReg(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800194 DCHECK_LT(i, NumberOfVRegs());
195 const uint32_t* vreg = &vregs_[i];
196 return *reinterpret_cast<const int32_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700197 }
198
buzbee1452bee2015-03-06 14:43:04 -0800199 uint32_t* GetVRegAddr(size_t i) {
200 return &vregs_[i];
201 }
202
203 uint32_t* GetShadowRefAddr(size_t i) {
204 DCHECK(HasReferenceArray());
205 DCHECK_LT(i, NumberOfVRegs());
206 return &vregs_[i + NumberOfVRegs()];
207 }
208
209 void SetCodeItem(const DexFile::CodeItem* code_item) {
210 code_item_ = code_item;
211 }
212
Bill Buzbeed47fd902016-07-07 14:42:43 +0000213 const DexFile::CodeItem* GetCodeItem() const {
214 return code_item_;
215 }
216
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700217 float GetVRegFloat(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800218 DCHECK_LT(i, NumberOfVRegs());
219 // NOTE: Strict-aliasing?
220 const uint32_t* vreg = &vregs_[i];
221 return *reinterpret_cast<const float*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700222 }
223
224 int64_t GetVRegLong(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200225 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800226 const uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700227 // Alignment attribute required for GCC 4.8
228 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 // Alignment attribute required for GCC 4.8
236 typedef const double unaligned_double __attribute__ ((aligned (4)));
237 return *reinterpret_cast<unaligned_double*>(vreg);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800238 }
239
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700240 // Look up the reference given its virtual register number.
241 // If this returns non-null then this does not mean the vreg is currently a reference
242 // 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 -0800243 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Mathieu Chartier90443472015-07-16 20:32:27 -0700244 mirror::Object* GetVRegReference(size_t i) const SHARED_REQUIRES(Locks::mutator_lock_) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800245 DCHECK_LT(i, NumberOfVRegs());
Mathieu Chartier4e305412014-02-19 10:54:44 -0800246 mirror::Object* ref;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800247 if (HasReferenceArray()) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800248 ref = References()[i].AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800249 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800250 const uint32_t* vreg_ptr = &vregs_[i];
Mathieu Chartier4e305412014-02-19 10:54:44 -0800251 ref = reinterpret_cast<const StackReference<mirror::Object>*>(vreg_ptr)->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800252 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800253 if (kUseReadBarrier) {
254 ReadBarrier::AssertToSpaceInvariant(ref);
255 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800256 if (kVerifyFlags & kVerifyReads) {
257 VerifyObject(ref);
258 }
259 return ref;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700260 }
261
Jeff Hao16743632013-05-08 10:59:04 -0700262 // Get view of vregs as range of consecutive arguments starting at i.
263 uint32_t* GetVRegArgs(size_t i) {
264 return &vregs_[i];
265 }
266
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700267 void SetVReg(size_t i, int32_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800268 DCHECK_LT(i, NumberOfVRegs());
269 uint32_t* vreg = &vregs_[i];
270 *reinterpret_cast<int32_t*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700271 // This is needed for moving collectors since these can update the vreg references if they
272 // happen to agree with references in the reference array.
273 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800274 References()[i].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700275 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700276 }
277
278 void SetVRegFloat(size_t i, float val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800279 DCHECK_LT(i, NumberOfVRegs());
280 uint32_t* vreg = &vregs_[i];
281 *reinterpret_cast<float*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700282 // This is needed for moving collectors since these can update the vreg references if they
283 // happen to agree with references in the reference array.
284 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800285 References()[i].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700286 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700287 }
288
289 void SetVRegLong(size_t i, int64_t val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200290 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800291 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700292 // Alignment attribute required for GCC 4.8
293 typedef int64_t unaligned_int64 __attribute__ ((aligned (4)));
294 *reinterpret_cast<unaligned_int64*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700295 // This is needed for moving collectors since these can update the vreg references if they
296 // happen to agree with references in the reference array.
297 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800298 References()[i].Clear();
299 References()[i + 1].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700300 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700301 }
302
303 void SetVRegDouble(size_t i, double val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200304 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800305 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700306 // Alignment attribute required for GCC 4.8
307 typedef double unaligned_double __attribute__ ((aligned (4)));
308 *reinterpret_cast<unaligned_double*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700309 // This is needed for moving collectors since these can update the vreg references if they
310 // happen to agree with references in the reference array.
311 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800312 References()[i].Clear();
313 References()[i + 1].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700314 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700315 }
316
Mathieu Chartier4e305412014-02-19 10:54:44 -0800317 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Mathieu Chartier90443472015-07-16 20:32:27 -0700318 void SetVRegReference(size_t i, mirror::Object* val) SHARED_REQUIRES(Locks::mutator_lock_) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800319 DCHECK_LT(i, NumberOfVRegs());
Mathieu Chartier4e305412014-02-19 10:54:44 -0800320 if (kVerifyFlags & kVerifyWrites) {
321 VerifyObject(val);
322 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800323 if (kUseReadBarrier) {
324 ReadBarrier::AssertToSpaceInvariant(val);
325 }
TDYa127ce4cc0d2012-11-18 16:59:53 -0800326 uint32_t* vreg = &vregs_[i];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800327 reinterpret_cast<StackReference<mirror::Object>*>(vreg)->Assign(val);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800328 if (HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800329 References()[i].Assign(val);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800330 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700331 }
332
Mathieu Chartier90443472015-07-16 20:32:27 -0700333 ArtMethod* GetMethod() const SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800334 DCHECK(method_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700335 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700336 }
337
Mathieu Chartier90443472015-07-16 20:32:27 -0700338 mirror::Object* GetThisObject() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800339
Mathieu Chartier90443472015-07-16 20:32:27 -0700340 mirror::Object* GetThisObject(uint16_t num_ins) const SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Haoe701f482013-05-24 11:50:49 -0700341
Ian Rogersef7d42f2014-01-06 12:55:46 -0800342 bool Contains(StackReference<mirror::Object>* shadow_frame_entry_obj) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800343 if (HasReferenceArray()) {
344 return ((&References()[0] <= shadow_frame_entry_obj) &&
345 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
346 } else {
347 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
348 return ((&vregs_[0] <= shadow_frame_entry) &&
349 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
Ian Rogers0399dde2012-06-06 17:09:28 -0700350 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700351 }
352
Andreas Gampe03ec9302015-08-27 17:41:47 -0700353 LockCountData& GetLockCountData() {
354 return lock_count_data_;
355 }
356
buzbee1452bee2015-03-06 14:43:04 -0800357 static size_t LockCountDataOffset() {
358 return OFFSETOF_MEMBER(ShadowFrame, lock_count_data_);
359 }
360
Ian Rogers0399dde2012-06-06 17:09:28 -0700361 static size_t LinkOffset() {
362 return OFFSETOF_MEMBER(ShadowFrame, link_);
363 }
364
Ian Rogers0399dde2012-06-06 17:09:28 -0700365 static size_t MethodOffset() {
366 return OFFSETOF_MEMBER(ShadowFrame, method_);
367 }
368
Ian Rogers0399dde2012-06-06 17:09:28 -0700369 static size_t DexPCOffset() {
370 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
371 }
372
Ian Rogers5438ad82012-10-15 17:22:44 -0700373 static size_t NumberOfVRegsOffset() {
374 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
375 }
376
TDYa127ce4cc0d2012-11-18 16:59:53 -0800377 static size_t VRegsOffset() {
378 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
Ian Rogers5438ad82012-10-15 17:22:44 -0700379 }
380
buzbee1452bee2015-03-06 14:43:04 -0800381 static size_t ResultRegisterOffset() {
382 return OFFSETOF_MEMBER(ShadowFrame, result_register_);
383 }
384
385 static size_t DexPCPtrOffset() {
386 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_ptr_);
387 }
388
389 static size_t CodeItemOffset() {
390 return OFFSETOF_MEMBER(ShadowFrame, code_item_);
391 }
392
Bill Buzbee1d011d92016-04-04 16:59:29 +0000393 static size_t CachedHotnessCountdownOffset() {
394 return OFFSETOF_MEMBER(ShadowFrame, cached_hotness_countdown_);
395 }
396
397 static size_t HotnessCountdownOffset() {
398 return OFFSETOF_MEMBER(ShadowFrame, hotness_countdown_);
399 }
400
Andreas Gampeb3025922015-09-01 14:45:00 -0700401 // Create ShadowFrame for interpreter using provided memory.
402 static ShadowFrame* CreateShadowFrameImpl(uint32_t num_vregs,
403 ShadowFrame* link,
404 ArtMethod* method,
405 uint32_t dex_pc,
406 void* memory) {
407 return new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true);
408 }
409
Bill Buzbee1d011d92016-04-04 16:59:29 +0000410 const uint16_t* GetDexPCPtr() {
buzbee1452bee2015-03-06 14:43:04 -0800411 return dex_pc_ptr_;
412 }
413
Bill Buzbeed47fd902016-07-07 14:42:43 +0000414 void SetDexPCPtr(uint16_t* dex_pc_ptr) {
415 dex_pc_ptr_ = dex_pc_ptr;
416 }
417
buzbee1452bee2015-03-06 14:43:04 -0800418 JValue* GetResultRegister() {
419 return result_register_;
420 }
421
Elliott Hughes68e76522011-10-05 13:22:16 -0700422 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700423 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, ArtMethod* method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800424 uint32_t dex_pc, bool has_reference_array)
buzbee1452bee2015-03-06 14:43:04 -0800425 : link_(link), method_(method), result_register_(nullptr), dex_pc_ptr_(nullptr),
426 code_item_(nullptr), number_of_vregs_(num_vregs), dex_pc_(dex_pc) {
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700427 // TODO(iam): Remove this parameter, it's an an artifact of portable removal
428 DCHECK(has_reference_array);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800429 if (has_reference_array) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800430 memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(StackReference<mirror::Object>)));
Mathieu Chartier67022432012-11-29 18:04:50 -0800431 } else {
Jeff Haoe701f482013-05-24 11:50:49 -0700432 memset(vregs_, 0, num_vregs * sizeof(uint32_t));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700433 }
434 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700435
Ian Rogersef7d42f2014-01-06 12:55:46 -0800436 const StackReference<mirror::Object>* References() const {
Mathieu Chartier67022432012-11-29 18:04:50 -0800437 DCHECK(HasReferenceArray());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800438 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800439 return reinterpret_cast<const StackReference<mirror::Object>*>(vreg_end);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800440 }
441
Ian Rogersef7d42f2014-01-06 12:55:46 -0800442 StackReference<mirror::Object>* References() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700443 return const_cast<StackReference<mirror::Object>*>(
444 const_cast<const ShadowFrame*>(this)->References());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800445 }
446
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700447 // Link to previous shadow frame or null.
Ian Rogers0399dde2012-06-06 17:09:28 -0700448 ShadowFrame* link_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700449 ArtMethod* method_;
buzbee1452bee2015-03-06 14:43:04 -0800450 JValue* result_register_;
Bill Buzbee1d011d92016-04-04 16:59:29 +0000451 const uint16_t* dex_pc_ptr_;
buzbee1452bee2015-03-06 14:43:04 -0800452 const DexFile::CodeItem* code_item_;
Andreas Gampe03ec9302015-08-27 17:41:47 -0700453 LockCountData lock_count_data_; // This may contain GC roots when lock counting is active.
buzbee1452bee2015-03-06 14:43:04 -0800454 const uint32_t number_of_vregs_;
455 uint32_t dex_pc_;
Bill Buzbee1d011d92016-04-04 16:59:29 +0000456 int16_t cached_hotness_countdown_;
457 int16_t hotness_countdown_;
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700458
459 // This is a two-part array:
460 // - [0..number_of_vregs) holds the raw virtual registers, and each element here is always 4
461 // bytes.
462 // - [number_of_vregs..number_of_vregs*2) holds only reference registers. Each element here is
463 // ptr-sized.
464 // In other words when a primitive is stored in vX, the second (reference) part of the array will
465 // be null. When a reference is stored in vX, the second (reference) part of the array will be a
466 // copy of vX.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800467 uint32_t vregs_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700468
Ian Rogers0399dde2012-06-06 17:09:28 -0700469 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700470};
471
Andreas Gampeb3025922015-09-01 14:45:00 -0700472struct ShadowFrameDeleter {
473 inline void operator()(ShadowFrame* frame) {
474 if (frame != nullptr) {
475 frame->~ShadowFrame();
476 }
477 }
478};
479
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800480class JavaFrameRootInfo : public RootInfo {
481 public:
482 JavaFrameRootInfo(uint32_t thread_id, const StackVisitor* stack_visitor, size_t vreg)
483 : RootInfo(kRootJavaFrame, thread_id), stack_visitor_(stack_visitor), vreg_(vreg) {
484 }
485 virtual void Describe(std::ostream& os) const OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700486 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800487
488 private:
489 const StackVisitor* const stack_visitor_;
490 const size_t vreg_;
491};
492
Ian Rogers0399dde2012-06-06 17:09:28 -0700493// The managed stack is used to record fragments of managed code stacks. Managed code stacks
494// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
495// necessary for transitions between code using different frame layouts and transitions into native
496// code.
Ian Rogersdf1ce912012-11-27 17:07:11 -0800497class PACKED(4) ManagedStack {
Ian Rogers0399dde2012-06-06 17:09:28 -0700498 public:
Ian Rogersca190662012-06-26 15:45:57 -0700499 ManagedStack()
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700500 : top_quick_frame_(nullptr), link_(nullptr), top_shadow_frame_(nullptr) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700501
502 void PushManagedStackFragment(ManagedStack* fragment) {
503 // Copy this top fragment into given fragment.
504 memcpy(fragment, this, sizeof(ManagedStack));
505 // Clear this fragment, which has become the top.
506 memset(this, 0, sizeof(ManagedStack));
507 // Link our top fragment onto the given fragment.
508 link_ = fragment;
509 }
510
511 void PopManagedStackFragment(const ManagedStack& fragment) {
512 DCHECK(&fragment == link_);
513 // Copy this given fragment back to the top.
514 memcpy(this, &fragment, sizeof(ManagedStack));
515 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700516
517 ManagedStack* GetLink() const {
518 return link_;
519 }
520
Mathieu Chartiere401d142015-04-22 13:56:20 -0700521 ArtMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700522 return top_quick_frame_;
523 }
524
Mathieu Chartiere401d142015-04-22 13:56:20 -0700525 void SetTopQuickFrame(ArtMethod** top) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700526 DCHECK(top_shadow_frame_ == nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700527 top_quick_frame_ = top;
528 }
529
Ian Rogers0399dde2012-06-06 17:09:28 -0700530 static size_t TopQuickFrameOffset() {
531 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
532 }
533
Ian Rogers0399dde2012-06-06 17:09:28 -0700534 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700535 DCHECK(top_quick_frame_ == nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700536 ShadowFrame* old_frame = top_shadow_frame_;
537 top_shadow_frame_ = new_top_frame;
538 new_top_frame->SetLink(old_frame);
539 return old_frame;
540 }
541
542 ShadowFrame* PopShadowFrame() {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700543 DCHECK(top_quick_frame_ == nullptr);
544 CHECK(top_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700545 ShadowFrame* frame = top_shadow_frame_;
546 top_shadow_frame_ = frame->GetLink();
547 return frame;
548 }
549
550 ShadowFrame* GetTopShadowFrame() const {
551 return top_shadow_frame_;
552 }
553
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800554 void SetTopShadowFrame(ShadowFrame* top) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700555 DCHECK(top_quick_frame_ == nullptr);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800556 top_shadow_frame_ = top;
557 }
558
Ian Rogers0399dde2012-06-06 17:09:28 -0700559 static size_t TopShadowFrameOffset() {
560 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
561 }
562
Mathieu Chartier90443472015-07-16 20:32:27 -0700563 size_t NumJniShadowFrameReferences() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700564
Ian Rogersef7d42f2014-01-06 12:55:46 -0800565 bool ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700566
567 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700568 ArtMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700569 ManagedStack* link_;
570 ShadowFrame* top_shadow_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700571};
572
573class StackVisitor {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100574 public:
575 // This enum defines a flag to control whether inlined frames are included
576 // when walking the stack.
577 enum class StackWalkKind {
578 kIncludeInlinedFrames,
579 kSkipInlinedFrames,
580 };
581
Ian Rogers0399dde2012-06-06 17:09:28 -0700582 protected:
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100583 StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind)
Mathieu Chartier90443472015-07-16 20:32:27 -0700584 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700585
Nicolas Geoffray33856502015-10-20 15:52:58 +0100586 bool GetRegisterIfAccessible(uint32_t reg, VRegKind kind, uint32_t* val) const
587 SHARED_REQUIRES(Locks::mutator_lock_);
588
Ian Rogers0399dde2012-06-06 17:09:28 -0700589 public:
590 virtual ~StackVisitor() {}
591
592 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Mathieu Chartier90443472015-07-16 20:32:27 -0700593 virtual bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700594
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700595 void WalkStack(bool include_transitions = false)
Mathieu Chartier90443472015-07-16 20:32:27 -0700596 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700597
Sebastien Hertz26f72862015-09-15 09:52:07 +0200598 Thread* GetThread() const {
599 return thread_;
600 }
601
Mathieu Chartier90443472015-07-16 20:32:27 -0700602 ArtMethod* GetMethod() const SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi92d1a662014-05-15 21:43:59 -0700603
Nicolas Geoffrayccc61972015-10-01 14:34:20 +0100604 ArtMethod* GetOuterMethod() const {
605 return *GetCurrentQuickFrame();
606 }
607
Ian Rogers0399dde2012-06-06 17:09:28 -0700608 bool IsShadowFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800609 return cur_shadow_frame_ != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -0700610 }
611
Mathieu Chartier90443472015-07-16 20:32:27 -0700612 uint32_t GetDexPc(bool abort_on_failure = true) const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700613
Mathieu Chartier90443472015-07-16 20:32:27 -0700614 mirror::Object* GetThisObject() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800615
Mathieu Chartier90443472015-07-16 20:32:27 -0700616 size_t GetNativePcOffset() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700617
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700618 // Returns the height of the stack in the managed stack frames, including transitions.
Mathieu Chartier90443472015-07-16 20:32:27 -0700619 size_t GetFrameHeight() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800620 return GetNumFrames() - cur_depth_ - 1;
Ian Rogers0399dde2012-06-06 17:09:28 -0700621 }
622
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700623 // Returns a frame ID for JDWP use, starting from 1.
Mathieu Chartier90443472015-07-16 20:32:27 -0700624 size_t GetFrameId() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700625 return GetFrameHeight() + 1;
626 }
627
Mathieu Chartier90443472015-07-16 20:32:27 -0700628 size_t GetNumFrames() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700629 if (num_frames_ == 0) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100630 num_frames_ = ComputeNumFrames(thread_, walk_kind_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700631 }
632 return num_frames_;
633 }
634
Mathieu Chartier90443472015-07-16 20:32:27 -0700635 size_t GetFrameDepth() SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700636 return cur_depth_;
637 }
638
Ian Rogers5cf98192014-05-29 21:31:50 -0700639 // Get the method and dex pc immediately after the one that's currently being visited.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700640 bool GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -0700641 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700642
Mathieu Chartiere401d142015-04-22 13:56:20 -0700643 bool GetVReg(ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700644 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700645
Mathieu Chartiere401d142015-04-22 13:56:20 -0700646 bool GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo, VRegKind kind_hi,
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200647 uint64_t* val) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700648 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200649
Mingyao Yang636b9252015-07-31 16:40:24 -0700650 // Values will be set in debugger shadow frames. Debugger will make sure deoptimization
651 // is triggered to make the values effective.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700652 bool SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Mathieu Chartier90443472015-07-16 20:32:27 -0700653 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700654
Mingyao Yang99170c62015-07-06 11:10:37 -0700655 // Values will be set in debugger shadow frames. Debugger will make sure deoptimization
656 // is triggered to make the values effective.
Mingyao Yang636b9252015-07-31 16:40:24 -0700657 bool SetVRegPair(ArtMethod* m,
658 uint16_t vreg,
659 uint64_t new_value,
660 VRegKind kind_lo,
661 VRegKind kind_hi)
Mingyao Yang99170c62015-07-06 11:10:37 -0700662 SHARED_REQUIRES(Locks::mutator_lock_);
663
Mathieu Chartier815873e2014-02-13 18:02:13 -0800664 uintptr_t* GetGPRAddress(uint32_t reg) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700665
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700666 // This is a fast-path for getting/setting values in a quick frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700667 uint32_t* GetVRegAddrFromQuickCode(ArtMethod** cur_quick_frame,
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000668 const DexFile::CodeItem* code_item,
669 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
670 uint16_t vreg) const {
671 int offset = GetVRegOffsetFromQuickCode(
672 code_item, core_spills, fp_spills, frame_size, vreg, kRuntimeISA);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700673 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
Ian Rogers13735952014-10-08 12:43:28 -0700674 uint8_t* vreg_addr = reinterpret_cast<uint8_t*>(cur_quick_frame) + offset;
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700675 return reinterpret_cast<uint32_t*>(vreg_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700676 }
677
Mathieu Chartier90443472015-07-16 20:32:27 -0700678 uintptr_t GetReturnPc() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700679
Mathieu Chartier90443472015-07-16 20:32:27 -0700680 void SetReturnPc(uintptr_t new_ret_pc) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700681
682 /*
683 * Return sp-relative offset for a Dalvik virtual register, compiler
684 * spill or Method* in bytes using Method*.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700685 * Note that (reg == -1) denotes an invalid Dalvik register. For the
686 * positive values, the Dalvik registers come first, followed by the
687 * Method*, followed by other special temporaries if any, followed by
688 * regular compiler temporary. As of now we only have the Method* as
689 * as a special compiler temporary.
690 * A compiler temporary can be thought of as a virtual register that
691 * does not exist in the dex but holds intermediate values to help
692 * optimizations and code generation. A special compiler temporary is
693 * one whose location in frame is well known while non-special ones
694 * do not have a requirement on location in frame as long as code
695 * generator itself knows how to access them.
Ian Rogers0399dde2012-06-06 17:09:28 -0700696 *
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700697 * +-------------------------------+
698 * | IN[ins-1] | {Note: resides in caller's frame}
699 * | . |
700 * | IN[0] |
Mathieu Chartiere401d142015-04-22 13:56:20 -0700701 * | caller's ArtMethod | ... ArtMethod*
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700702 * +===============================+ {Note: start of callee's frame}
703 * | core callee-save spill | {variable sized}
704 * +-------------------------------+
705 * | fp callee-save spill |
706 * +-------------------------------+
707 * | filler word | {For compatibility, if V[locals-1] used as wide
708 * +-------------------------------+
709 * | V[locals-1] |
710 * | V[locals-2] |
711 * | . |
712 * | . | ... (reg == 2)
713 * | V[1] | ... (reg == 1)
714 * | V[0] | ... (reg == 0) <---- "locals_start"
715 * +-------------------------------+
716 * | stack alignment padding | {0 to (kStackAlignWords-1) of padding}
717 * +-------------------------------+
718 * | Compiler temp region | ... (reg >= max_num_special_temps)
719 * | . |
720 * | . |
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700721 * | V[max_num_special_temps + 1] |
722 * | V[max_num_special_temps + 0] |
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700723 * +-------------------------------+
724 * | OUT[outs-1] |
725 * | OUT[outs-2] |
726 * | . |
727 * | OUT[0] |
Mathieu Chartiere401d142015-04-22 13:56:20 -0700728 * | ArtMethod* | ... (reg == num_total_code_regs == special_temp_value) <<== sp, 16-byte aligned
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700729 * +===============================+
Ian Rogers0399dde2012-06-06 17:09:28 -0700730 */
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000731 static int GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
732 uint32_t core_spills, uint32_t fp_spills,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700733 size_t frame_size, int reg, InstructionSet isa);
Ian Rogers0399dde2012-06-06 17:09:28 -0700734
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000735 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
buzbee82818642014-06-04 15:35:41 -0700736 // According to stack model, the first out is above the Method referernce.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700737 return InstructionSetPointerSize(isa) + out_num * sizeof(uint32_t);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800738 }
739
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100740 bool IsInInlinedFrame() const {
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100741 return current_inlining_depth_ != 0;
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100742 }
743
David Brazdilefc3f022015-10-28 12:19:06 -0500744 size_t GetCurrentInliningDepth() const {
745 return current_inlining_depth_;
746 }
747
Ian Rogers0399dde2012-06-06 17:09:28 -0700748 uintptr_t GetCurrentQuickFramePc() const {
749 return cur_quick_frame_pc_;
750 }
751
Mathieu Chartiere401d142015-04-22 13:56:20 -0700752 ArtMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700753 return cur_quick_frame_;
754 }
755
756 ShadowFrame* GetCurrentShadowFrame() const {
757 return cur_shadow_frame_;
758 }
759
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100760 bool IsCurrentFrameInInterpreter() const {
761 return cur_shadow_frame_ != nullptr;
762 }
763
Mathieu Chartiere401d142015-04-22 13:56:20 -0700764 HandleScope* GetCurrentHandleScope(size_t pointer_size) const {
765 ArtMethod** sp = GetCurrentQuickFrame();
766 // Skip ArtMethod*; handle scope comes next;
767 return reinterpret_cast<HandleScope*>(reinterpret_cast<uintptr_t>(sp) + pointer_size);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700768 }
769
Mathieu Chartier90443472015-07-16 20:32:27 -0700770 std::string DescribeLocation() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers40e3bac2012-11-20 00:09:14 -0800771
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100772 static size_t ComputeNumFrames(Thread* thread, StackWalkKind walk_kind)
Mathieu Chartier90443472015-07-16 20:32:27 -0700773 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800774
Mathieu Chartier90443472015-07-16 20:32:27 -0700775 static void DescribeStack(Thread* thread) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800776
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100777 const OatQuickMethodHeader* GetCurrentOatQuickMethodHeader() const {
778 return cur_oat_quick_method_header_;
779 }
780
781 QuickMethodFrameInfo GetCurrentQuickFrameInfo() const SHARED_REQUIRES(Locks::mutator_lock_);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100782
Ian Rogers0399dde2012-06-06 17:09:28 -0700783 private:
Ian Rogers5cf98192014-05-29 21:31:50 -0700784 // Private constructor known in the case that num_frames_ has already been computed.
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100785 StackVisitor(Thread* thread, Context* context, StackWalkKind walk_kind, size_t num_frames)
Mathieu Chartier90443472015-07-16 20:32:27 -0700786 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700787
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100788 bool IsAccessibleRegister(uint32_t reg, bool is_float) const {
789 return is_float ? IsAccessibleFPR(reg) : IsAccessibleGPR(reg);
790 }
791 uintptr_t GetRegister(uint32_t reg, bool is_float) const {
792 DCHECK(IsAccessibleRegister(reg, is_float));
793 return is_float ? GetFPR(reg) : GetGPR(reg);
794 }
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100795
796 bool IsAccessibleGPR(uint32_t reg) const;
797 uintptr_t GetGPR(uint32_t reg) const;
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +0100798
799 bool IsAccessibleFPR(uint32_t reg) const;
800 uintptr_t GetFPR(uint32_t reg) const;
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200801
Mingyao Yang99170c62015-07-06 11:10:37 -0700802 bool GetVRegFromDebuggerShadowFrame(uint16_t vreg, VRegKind kind, uint32_t* val) const
803 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700804 bool GetVRegFromOptimizedCode(ArtMethod* m, uint16_t vreg, VRegKind kind,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100805 uint32_t* val) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700806 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100807
Mingyao Yang99170c62015-07-06 11:10:37 -0700808 bool GetVRegPairFromDebuggerShadowFrame(uint16_t vreg, VRegKind kind_lo, VRegKind kind_hi,
809 uint64_t* val) const
810 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700811 bool GetVRegPairFromOptimizedCode(ArtMethod* m, uint16_t vreg,
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100812 VRegKind kind_lo, VRegKind kind_hi,
813 uint64_t* val) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700814 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100815 bool GetRegisterPairIfAccessible(uint32_t reg_lo, uint32_t reg_hi, VRegKind kind_lo,
816 uint64_t* val) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700817 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100818
Mathieu Chartier90443472015-07-16 20:32:27 -0700819 void SanityCheckFrame() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700820
Mathieu Chartier90443472015-07-16 20:32:27 -0700821 InlineInfo GetCurrentInlineInfo() const SHARED_REQUIRES(Locks::mutator_lock_);
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100822
Ian Rogers7a22fa62013-01-23 12:16:16 -0800823 Thread* const thread_;
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100824 const StackWalkKind walk_kind_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700825 ShadowFrame* cur_shadow_frame_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700826 ArtMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700827 uintptr_t cur_quick_frame_pc_;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100828 const OatQuickMethodHeader* cur_oat_quick_method_header_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700829 // Lazily computed, number of frames in the stack.
830 size_t num_frames_;
831 // Depth of the frame we're currently at.
832 size_t cur_depth_;
Nicolas Geoffray57f61612015-05-15 13:20:41 +0100833 // Current inlining depth of the method we are currently at.
834 // 0 if there is no inlined frame.
835 size_t current_inlining_depth_;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700836
Ian Rogers0399dde2012-06-06 17:09:28 -0700837 protected:
838 Context* const context_;
839};
840
Elliott Hughes68e76522011-10-05 13:22:16 -0700841} // namespace art
842
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700843#endif // ART_RUNTIME_STACK_H_