Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_STACK_H_ |
| 18 | #define ART_RUNTIME_STACK_H_ |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 21 | #include <string> |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 22 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 23 | #include "dex_file.h" |
| 24 | #include "instruction_set.h" |
| 25 | #include "mirror/object_reference.h" |
| 26 | #include "throw_location.h" |
| 27 | #include "utils.h" |
| 28 | #include "verify_object.h" |
| 29 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 30 | namespace art { |
| 31 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | namespace mirror { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 33 | class ArtMethod; |
| 34 | class Object; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | } // namespace mirror |
| 36 | |
| 37 | class Context; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 38 | class ShadowFrame; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 39 | class HandleScope; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 40 | class ScopedObjectAccess; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 41 | class Thread; |
| 42 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 43 | // The kind of vreg being accessed in calls to Set/GetVReg. |
| 44 | enum VRegKind { |
| 45 | kReferenceVReg, |
| 46 | kIntVReg, |
| 47 | kFloatVReg, |
| 48 | kLongLoVReg, |
| 49 | kLongHiVReg, |
| 50 | kDoubleLoVReg, |
| 51 | kDoubleHiVReg, |
| 52 | kConstant, |
| 53 | kImpreciseConstant, |
| 54 | kUndefined, |
| 55 | }; |
| 56 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 57 | // A reference from the shadow stack to a MirrorType object within the Java heap. |
| 58 | template<class MirrorType> |
| 59 | class MANAGED StackReference : public mirror::ObjectReference<false, MirrorType> { |
| 60 | public: |
| 61 | StackReference<MirrorType>() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 62 | : mirror::ObjectReference<false, MirrorType>(nullptr) {} |
| 63 | |
| 64 | static StackReference<MirrorType> FromMirrorPtr(MirrorType* p) |
| 65 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 66 | return StackReference<MirrorType>(p); |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | StackReference<MirrorType>(MirrorType* p) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 71 | : mirror::ObjectReference<false, MirrorType>(p) {} |
| 72 | }; |
| 73 | |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 74 | // ShadowFrame has 3 possible layouts: |
| 75 | // - portable - a unified array of VRegs and references. Precise references need GC maps. |
| 76 | // - interpreter - separate VRegs and reference arrays. References are in the reference array. |
| 77 | // - JNI - just VRegs, but where every VReg holds a reference. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 78 | class ShadowFrame { |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 79 | public: |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 80 | // Compute size of ShadowFrame in bytes assuming it has a reference array. |
Jeff Hao | 6613519 | 2013-05-14 11:02:41 -0700 | [diff] [blame] | 81 | static size_t ComputeSize(uint32_t num_vregs) { |
| 82 | return sizeof(ShadowFrame) + (sizeof(uint32_t) * num_vregs) + |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 83 | (sizeof(StackReference<mirror::Object>) * num_vregs); |
Jeff Hao | 6613519 | 2013-05-14 11:02:41 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // Create ShadowFrame in heap for deoptimization. |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 87 | static ShadowFrame* Create(uint32_t num_vregs, ShadowFrame* link, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 88 | mirror::ArtMethod* method, uint32_t dex_pc) { |
Jeff Hao | 6613519 | 2013-05-14 11:02:41 -0700 | [diff] [blame] | 89 | uint8_t* memory = new uint8_t[ComputeSize(num_vregs)]; |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 90 | return Create(num_vregs, link, method, dex_pc, memory); |
Jeff Hao | 6613519 | 2013-05-14 11:02:41 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // Create ShadowFrame for interpreter using provided memory. |
| 94 | static ShadowFrame* Create(uint32_t num_vregs, ShadowFrame* link, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 95 | mirror::ArtMethod* method, uint32_t dex_pc, void* memory) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 96 | ShadowFrame* sf = new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true); |
| 97 | return sf; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 98 | } |
| 99 | ~ShadowFrame() {} |
| 100 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 101 | bool HasReferenceArray() const { |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 102 | #if defined(ART_USE_PORTABLE_COMPILER) |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 103 | return (number_of_vregs_ & kHasReferenceArray) != 0; |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 104 | #else |
| 105 | return true; |
| 106 | #endif |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 107 | } |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 108 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 109 | uint32_t NumberOfVRegs() const { |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 110 | #if defined(ART_USE_PORTABLE_COMPILER) |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 111 | return number_of_vregs_ & ~kHasReferenceArray; |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 112 | #else |
| 113 | return number_of_vregs_; |
| 114 | #endif |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 115 | } |
| 116 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 117 | void SetNumberOfVRegs(uint32_t number_of_vregs) { |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 118 | #if defined(ART_USE_PORTABLE_COMPILER) |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 119 | number_of_vregs_ = number_of_vregs | (number_of_vregs_ & kHasReferenceArray); |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 120 | #else |
| 121 | UNUSED(number_of_vregs); |
| 122 | UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled"; |
| 123 | #endif |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 126 | uint32_t GetDexPC() const { |
| 127 | return dex_pc_; |
| 128 | } |
| 129 | |
| 130 | void SetDexPC(uint32_t dex_pc) { |
| 131 | dex_pc_ = dex_pc; |
| 132 | } |
| 133 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 134 | ShadowFrame* GetLink() const { |
| 135 | return link_; |
| 136 | } |
| 137 | |
| 138 | void SetLink(ShadowFrame* frame) { |
| 139 | DCHECK_NE(this, frame); |
| 140 | link_ = frame; |
| 141 | } |
| 142 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 143 | int32_t GetVReg(size_t i) const { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 144 | DCHECK_LT(i, NumberOfVRegs()); |
| 145 | const uint32_t* vreg = &vregs_[i]; |
| 146 | return *reinterpret_cast<const int32_t*>(vreg); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | float GetVRegFloat(size_t i) const { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 150 | DCHECK_LT(i, NumberOfVRegs()); |
| 151 | // NOTE: Strict-aliasing? |
| 152 | const uint32_t* vreg = &vregs_[i]; |
| 153 | return *reinterpret_cast<const float*>(vreg); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | int64_t GetVRegLong(size_t i) const { |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 157 | DCHECK_LT(i, NumberOfVRegs()); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 158 | const uint32_t* vreg = &vregs_[i]; |
Jeff Hao | e47637c | 2013-09-19 15:13:16 -0700 | [diff] [blame] | 159 | // Alignment attribute required for GCC 4.8 |
| 160 | typedef const int64_t unaligned_int64 __attribute__ ((aligned (4))); |
| 161 | return *reinterpret_cast<unaligned_int64*>(vreg); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | double GetVRegDouble(size_t i) const { |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 165 | DCHECK_LT(i, NumberOfVRegs()); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 166 | const uint32_t* vreg = &vregs_[i]; |
Jeff Hao | e47637c | 2013-09-19 15:13:16 -0700 | [diff] [blame] | 167 | // Alignment attribute required for GCC 4.8 |
| 168 | typedef const double unaligned_double __attribute__ ((aligned (4))); |
| 169 | return *reinterpret_cast<unaligned_double*>(vreg); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 172 | template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 173 | mirror::Object* GetVRegReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 174 | DCHECK_LT(i, NumberOfVRegs()); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 175 | mirror::Object* ref; |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 176 | if (HasReferenceArray()) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 177 | ref = References()[i].AsMirrorPtr(); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 178 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 179 | const uint32_t* vreg_ptr = &vregs_[i]; |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 180 | ref = reinterpret_cast<const StackReference<mirror::Object>*>(vreg_ptr)->AsMirrorPtr(); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 181 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 182 | if (kVerifyFlags & kVerifyReads) { |
| 183 | VerifyObject(ref); |
| 184 | } |
| 185 | return ref; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 188 | // Get view of vregs as range of consecutive arguments starting at i. |
| 189 | uint32_t* GetVRegArgs(size_t i) { |
| 190 | return &vregs_[i]; |
| 191 | } |
| 192 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 193 | void SetVReg(size_t i, int32_t val) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 194 | DCHECK_LT(i, NumberOfVRegs()); |
| 195 | uint32_t* vreg = &vregs_[i]; |
| 196 | *reinterpret_cast<int32_t*>(vreg) = val; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 197 | // This is needed for moving collectors since these can update the vreg references if they |
| 198 | // happen to agree with references in the reference array. |
| 199 | if (kMovingCollector && HasReferenceArray()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 200 | References()[i].Clear(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 201 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void SetVRegFloat(size_t i, float val) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 205 | DCHECK_LT(i, NumberOfVRegs()); |
| 206 | uint32_t* vreg = &vregs_[i]; |
| 207 | *reinterpret_cast<float*>(vreg) = val; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 208 | // This is needed for moving collectors since these can update the vreg references if they |
| 209 | // happen to agree with references in the reference array. |
| 210 | if (kMovingCollector && HasReferenceArray()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 211 | References()[i].Clear(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 212 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void SetVRegLong(size_t i, int64_t val) { |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 216 | DCHECK_LT(i, NumberOfVRegs()); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 217 | uint32_t* vreg = &vregs_[i]; |
Jeff Hao | e47637c | 2013-09-19 15:13:16 -0700 | [diff] [blame] | 218 | // Alignment attribute required for GCC 4.8 |
| 219 | typedef int64_t unaligned_int64 __attribute__ ((aligned (4))); |
| 220 | *reinterpret_cast<unaligned_int64*>(vreg) = val; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 221 | // This is needed for moving collectors since these can update the vreg references if they |
| 222 | // happen to agree with references in the reference array. |
| 223 | if (kMovingCollector && HasReferenceArray()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 224 | References()[i].Clear(); |
| 225 | References()[i + 1].Clear(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 226 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void SetVRegDouble(size_t i, double val) { |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 230 | DCHECK_LT(i, NumberOfVRegs()); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 231 | uint32_t* vreg = &vregs_[i]; |
Jeff Hao | e47637c | 2013-09-19 15:13:16 -0700 | [diff] [blame] | 232 | // Alignment attribute required for GCC 4.8 |
| 233 | typedef double unaligned_double __attribute__ ((aligned (4))); |
| 234 | *reinterpret_cast<unaligned_double*>(vreg) = val; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 235 | // This is needed for moving collectors since these can update the vreg references if they |
| 236 | // happen to agree with references in the reference array. |
| 237 | if (kMovingCollector && HasReferenceArray()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 238 | References()[i].Clear(); |
| 239 | References()[i + 1].Clear(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 240 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 243 | template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 244 | void SetVRegReference(size_t i, mirror::Object* val) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 245 | DCHECK_LT(i, NumberOfVRegs()); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 246 | if (kVerifyFlags & kVerifyWrites) { |
| 247 | VerifyObject(val); |
| 248 | } |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 249 | uint32_t* vreg = &vregs_[i]; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 250 | reinterpret_cast<StackReference<mirror::Object>*>(vreg)->Assign(val); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 251 | if (HasReferenceArray()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 252 | References()[i].Assign(val); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 253 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 256 | mirror::ArtMethod* GetMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 257 | DCHECK(method_ != nullptr); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 258 | return method_; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Hiroshi Yamauchi | 92d1a66 | 2014-05-15 21:43:59 -0700 | [diff] [blame] | 261 | mirror::ArtMethod** GetMethodAddress() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 262 | DCHECK(method_ != nullptr); |
| 263 | return &method_; |
| 264 | } |
| 265 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 266 | mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 267 | |
Jeff Hao | e701f48 | 2013-05-24 11:50:49 -0700 | [diff] [blame] | 268 | mirror::Object* GetThisObject(uint16_t num_ins) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 269 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 270 | ThrowLocation GetCurrentLocationForThrow() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 271 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 272 | void SetMethod(mirror::ArtMethod* method) { |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 273 | #if defined(ART_USE_PORTABLE_COMPILER) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 274 | DCHECK(method != nullptr); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 275 | method_ = method; |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 276 | #else |
| 277 | UNUSED(method); |
| 278 | UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled"; |
| 279 | #endif |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 282 | bool Contains(StackReference<mirror::Object>* shadow_frame_entry_obj) const { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 283 | if (HasReferenceArray()) { |
| 284 | return ((&References()[0] <= shadow_frame_entry_obj) && |
| 285 | (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1]))); |
| 286 | } else { |
| 287 | uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj); |
| 288 | return ((&vregs_[0] <= shadow_frame_entry) && |
| 289 | (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1]))); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 290 | } |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 293 | static size_t LinkOffset() { |
| 294 | return OFFSETOF_MEMBER(ShadowFrame, link_); |
| 295 | } |
| 296 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 297 | static size_t MethodOffset() { |
| 298 | return OFFSETOF_MEMBER(ShadowFrame, method_); |
| 299 | } |
| 300 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 301 | static size_t DexPCOffset() { |
| 302 | return OFFSETOF_MEMBER(ShadowFrame, dex_pc_); |
| 303 | } |
| 304 | |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 305 | static size_t NumberOfVRegsOffset() { |
| 306 | return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_); |
| 307 | } |
| 308 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 309 | static size_t VRegsOffset() { |
| 310 | return OFFSETOF_MEMBER(ShadowFrame, vregs_); |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 313 | private: |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 314 | ShadowFrame(uint32_t num_vregs, ShadowFrame* link, mirror::ArtMethod* method, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 315 | uint32_t dex_pc, bool has_reference_array) |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 316 | : number_of_vregs_(num_vregs), link_(link), method_(method), dex_pc_(dex_pc) { |
| 317 | if (has_reference_array) { |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 318 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 319 | CHECK_LT(num_vregs, static_cast<uint32_t>(kHasReferenceArray)); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 320 | number_of_vregs_ |= kHasReferenceArray; |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 321 | #endif |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 322 | memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(StackReference<mirror::Object>))); |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 323 | } else { |
Jeff Hao | e701f48 | 2013-05-24 11:50:49 -0700 | [diff] [blame] | 324 | memset(vregs_, 0, num_vregs * sizeof(uint32_t)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 327 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 328 | const StackReference<mirror::Object>* References() const { |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 329 | DCHECK(HasReferenceArray()); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 330 | const uint32_t* vreg_end = &vregs_[NumberOfVRegs()]; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 331 | return reinterpret_cast<const StackReference<mirror::Object>*>(vreg_end); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 334 | StackReference<mirror::Object>* References() { |
| 335 | return const_cast<StackReference<mirror::Object>*>(const_cast<const ShadowFrame*>(this)->References()); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 338 | #if defined(ART_USE_PORTABLE_COMPILER) |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 339 | enum ShadowFrameFlag { |
| 340 | kHasReferenceArray = 1ul << 31 |
| 341 | }; |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 342 | // TODO: make const in the portable case. |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 343 | uint32_t number_of_vregs_; |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 344 | #else |
| 345 | const uint32_t number_of_vregs_; |
| 346 | #endif |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 347 | // Link to previous shadow frame or NULL. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 348 | ShadowFrame* link_; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 349 | mirror::ArtMethod* method_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 350 | uint32_t dex_pc_; |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 351 | uint32_t vregs_[0]; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 352 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 353 | DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame); |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 354 | }; |
| 355 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 356 | // The managed stack is used to record fragments of managed code stacks. Managed code stacks |
| 357 | // may either be shadow frames or lists of frames using fixed frame sizes. Transition records are |
| 358 | // necessary for transitions between code using different frame layouts and transitions into native |
| 359 | // code. |
Ian Rogers | df1ce91 | 2012-11-27 17:07:11 -0800 | [diff] [blame] | 360 | class PACKED(4) ManagedStack { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 361 | public: |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 362 | ManagedStack() |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 363 | : top_quick_frame_(nullptr), link_(nullptr), top_shadow_frame_(nullptr) {} |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 364 | |
| 365 | void PushManagedStackFragment(ManagedStack* fragment) { |
| 366 | // Copy this top fragment into given fragment. |
| 367 | memcpy(fragment, this, sizeof(ManagedStack)); |
| 368 | // Clear this fragment, which has become the top. |
| 369 | memset(this, 0, sizeof(ManagedStack)); |
| 370 | // Link our top fragment onto the given fragment. |
| 371 | link_ = fragment; |
| 372 | } |
| 373 | |
| 374 | void PopManagedStackFragment(const ManagedStack& fragment) { |
| 375 | DCHECK(&fragment == link_); |
| 376 | // Copy this given fragment back to the top. |
| 377 | memcpy(this, &fragment, sizeof(ManagedStack)); |
| 378 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 379 | |
| 380 | ManagedStack* GetLink() const { |
| 381 | return link_; |
| 382 | } |
| 383 | |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 384 | StackReference<mirror::ArtMethod>* GetTopQuickFrame() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 385 | return top_quick_frame_; |
| 386 | } |
| 387 | |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 388 | void SetTopQuickFrame(StackReference<mirror::ArtMethod>* top) { |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 389 | DCHECK(top_shadow_frame_ == nullptr); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 390 | top_quick_frame_ = top; |
| 391 | } |
| 392 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 393 | static size_t TopQuickFrameOffset() { |
| 394 | return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_); |
| 395 | } |
| 396 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 397 | ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) { |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 398 | DCHECK(top_quick_frame_ == nullptr); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 399 | ShadowFrame* old_frame = top_shadow_frame_; |
| 400 | top_shadow_frame_ = new_top_frame; |
| 401 | new_top_frame->SetLink(old_frame); |
| 402 | return old_frame; |
| 403 | } |
| 404 | |
| 405 | ShadowFrame* PopShadowFrame() { |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 406 | DCHECK(top_quick_frame_ == nullptr); |
| 407 | CHECK(top_shadow_frame_ != nullptr); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 408 | ShadowFrame* frame = top_shadow_frame_; |
| 409 | top_shadow_frame_ = frame->GetLink(); |
| 410 | return frame; |
| 411 | } |
| 412 | |
| 413 | ShadowFrame* GetTopShadowFrame() const { |
| 414 | return top_shadow_frame_; |
| 415 | } |
| 416 | |
Jeff Hao | 11ffc2d | 2013-02-01 11:52:17 -0800 | [diff] [blame] | 417 | void SetTopShadowFrame(ShadowFrame* top) { |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 418 | DCHECK(top_quick_frame_ == nullptr); |
Jeff Hao | 11ffc2d | 2013-02-01 11:52:17 -0800 | [diff] [blame] | 419 | top_shadow_frame_ = top; |
| 420 | } |
| 421 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 422 | static size_t TopShadowFrameOffset() { |
| 423 | return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_); |
| 424 | } |
| 425 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 426 | size_t NumJniShadowFrameReferences() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 427 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 428 | bool ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 429 | |
| 430 | private: |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 431 | StackReference<mirror::ArtMethod>* top_quick_frame_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 432 | ManagedStack* link_; |
| 433 | ShadowFrame* top_shadow_frame_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 434 | }; |
| 435 | |
| 436 | class StackVisitor { |
| 437 | protected: |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 438 | StackVisitor(Thread* thread, Context* context) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 439 | |
| 440 | public: |
| 441 | virtual ~StackVisitor() {} |
| 442 | |
| 443 | // Return 'true' if we should continue to visit more frames, 'false' to stop. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 444 | virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 445 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 446 | void WalkStack(bool include_transitions = false) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 447 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 448 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 449 | mirror::ArtMethod* GetMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 450 | if (cur_shadow_frame_ != nullptr) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 451 | return cur_shadow_frame_->GetMethod(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 452 | } else if (cur_quick_frame_ != nullptr) { |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 453 | return cur_quick_frame_->AsMirrorPtr(); |
Hiroshi Yamauchi | 92d1a66 | 2014-05-15 21:43:59 -0700 | [diff] [blame] | 454 | } else { |
| 455 | return nullptr; |
| 456 | } |
| 457 | } |
| 458 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 459 | bool IsShadowFrame() const { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 460 | return cur_shadow_frame_ != nullptr; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 463 | uint32_t GetDexPc(bool abort_on_failure = true) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 464 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 465 | mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 466 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 467 | size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 468 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 469 | uintptr_t* CalleeSaveAddress(int num, size_t frame_size) const |
| 470 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 471 | // Callee saves are held at the top of the frame |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 472 | DCHECK(GetMethod() != nullptr); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 473 | uint8_t* save_addr = |
| 474 | reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size - ((num + 1) * sizeof(void*)); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 475 | #if defined(__i386__) || defined(__x86_64__) |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 476 | save_addr -= sizeof(void*); // account for return address |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 477 | #endif |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 478 | return reinterpret_cast<uintptr_t*>(save_addr); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 481 | // Returns the height of the stack in the managed stack frames, including transitions. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 482 | size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 483 | return GetNumFrames() - cur_depth_ - 1; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 486 | // Returns a frame ID for JDWP use, starting from 1. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 487 | size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 488 | return GetFrameHeight() + 1; |
| 489 | } |
| 490 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 491 | size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 492 | if (num_frames_ == 0) { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 493 | num_frames_ = ComputeNumFrames(thread_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 494 | } |
| 495 | return num_frames_; |
| 496 | } |
| 497 | |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 498 | size_t GetFrameDepth() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 499 | return cur_depth_; |
| 500 | } |
| 501 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 502 | // Get the method and dex pc immediately after the one that's currently being visited. |
| 503 | bool GetNextMethodAndDexPc(mirror::ArtMethod** next_method, uint32_t* next_dex_pc) |
| 504 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 505 | |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 506 | bool GetVReg(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 507 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 508 | |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 509 | uint32_t GetVReg(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind) const |
| 510 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 511 | uint32_t val; |
| 512 | bool success = GetVReg(m, vreg, kind, &val); |
| 513 | CHECK(success) << "Failed to read vreg " << vreg << " of kind " << kind; |
| 514 | return val; |
| 515 | } |
| 516 | |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 517 | bool GetVRegPair(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind_lo, VRegKind kind_hi, |
| 518 | uint64_t* val) const |
| 519 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 520 | |
| 521 | uint64_t GetVRegPair(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind_lo, |
| 522 | VRegKind kind_hi) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 523 | uint64_t val; |
| 524 | bool success = GetVRegPair(m, vreg, kind_lo, kind_hi, &val); |
| 525 | CHECK(success) << "Failed to read vreg pair " << vreg |
| 526 | << " of kind [" << kind_lo << "," << kind_hi << "]"; |
| 527 | return val; |
| 528 | } |
| 529 | |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 530 | bool SetVReg(mirror::ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 531 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 532 | |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 533 | bool SetVRegPair(mirror::ArtMethod* m, uint16_t vreg, uint64_t new_value, |
| 534 | VRegKind kind_lo, VRegKind kind_hi) |
| 535 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 536 | |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 537 | uintptr_t* GetGPRAddress(uint32_t reg) const; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 538 | |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 539 | // This is a fast-path for getting/setting values in a quick frame. |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 540 | uint32_t* GetVRegAddr(StackReference<mirror::ArtMethod>* cur_quick_frame, |
| 541 | const DexFile::CodeItem* code_item, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 542 | uint32_t core_spills, uint32_t fp_spills, size_t frame_size, |
| 543 | uint16_t vreg) const { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 544 | int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg, kRuntimeISA); |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 545 | DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame()); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 546 | uint8_t* vreg_addr = reinterpret_cast<uint8_t*>(cur_quick_frame) + offset; |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 547 | return reinterpret_cast<uint32_t*>(vreg_addr); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 550 | uintptr_t GetReturnPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 551 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 552 | void SetReturnPc(uintptr_t new_ret_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 553 | |
| 554 | /* |
| 555 | * Return sp-relative offset for a Dalvik virtual register, compiler |
| 556 | * spill or Method* in bytes using Method*. |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 557 | * Note that (reg == -1) denotes an invalid Dalvik register. For the |
| 558 | * positive values, the Dalvik registers come first, followed by the |
| 559 | * Method*, followed by other special temporaries if any, followed by |
| 560 | * regular compiler temporary. As of now we only have the Method* as |
| 561 | * as a special compiler temporary. |
| 562 | * A compiler temporary can be thought of as a virtual register that |
| 563 | * does not exist in the dex but holds intermediate values to help |
| 564 | * optimizations and code generation. A special compiler temporary is |
| 565 | * one whose location in frame is well known while non-special ones |
| 566 | * do not have a requirement on location in frame as long as code |
| 567 | * generator itself knows how to access them. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 568 | * |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 569 | * +-------------------------------+ |
| 570 | * | IN[ins-1] | {Note: resides in caller's frame} |
| 571 | * | . | |
| 572 | * | IN[0] | |
| 573 | * | caller's ArtMethod | ... StackReference<ArtMethod> |
| 574 | * +===============================+ {Note: start of callee's frame} |
| 575 | * | core callee-save spill | {variable sized} |
| 576 | * +-------------------------------+ |
| 577 | * | fp callee-save spill | |
| 578 | * +-------------------------------+ |
| 579 | * | filler word | {For compatibility, if V[locals-1] used as wide |
| 580 | * +-------------------------------+ |
| 581 | * | V[locals-1] | |
| 582 | * | V[locals-2] | |
| 583 | * | . | |
| 584 | * | . | ... (reg == 2) |
| 585 | * | V[1] | ... (reg == 1) |
| 586 | * | V[0] | ... (reg == 0) <---- "locals_start" |
| 587 | * +-------------------------------+ |
| 588 | * | stack alignment padding | {0 to (kStackAlignWords-1) of padding} |
| 589 | * +-------------------------------+ |
| 590 | * | Compiler temp region | ... (reg >= max_num_special_temps) |
| 591 | * | . | |
| 592 | * | . | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 593 | * | V[max_num_special_temps + 1] | |
| 594 | * | V[max_num_special_temps + 0] | |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 595 | * +-------------------------------+ |
| 596 | * | OUT[outs-1] | |
| 597 | * | OUT[outs-2] | |
| 598 | * | . | |
| 599 | * | OUT[0] | |
| 600 | * | StackReference<ArtMethod> | ... (reg == num_total_code_regs == special_temp_value) <<== sp, 16-byte aligned |
| 601 | * +===============================+ |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 602 | */ |
| 603 | static int GetVRegOffset(const DexFile::CodeItem* code_item, |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 604 | uint32_t core_spills, uint32_t fp_spills, |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 605 | size_t frame_size, int reg, InstructionSet isa) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 606 | DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U); |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 607 | DCHECK_NE(reg, -1); |
Vladimir Marko | 8194963 | 2014-05-02 11:53:22 +0100 | [diff] [blame] | 608 | int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa) |
| 609 | + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa) |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 610 | + sizeof(uint32_t); // Filler. |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 611 | int num_regs = code_item->registers_size_ - code_item->ins_size_; |
| 612 | int temp_threshold = code_item->registers_size_; |
| 613 | const int max_num_special_temps = 1; |
| 614 | if (reg == temp_threshold) { |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 615 | // The current method pointer corresponds to special location on stack. |
| 616 | return 0; |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 617 | } else if (reg >= temp_threshold + max_num_special_temps) { |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 618 | /* |
| 619 | * Special temporaries may have custom locations and the logic above deals with that. |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 620 | * However, non-special temporaries are placed relative to the outs. |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 621 | */ |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 622 | int temps_start = sizeof(StackReference<mirror::ArtMethod>) + code_item->outs_size_ * sizeof(uint32_t); |
| 623 | int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t); |
| 624 | return temps_start + relative_offset; |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 625 | } else if (reg < num_regs) { |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 626 | int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t); |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 627 | return locals_start + (reg * sizeof(uint32_t)); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 628 | } else { |
Razvan A Lupusoru | 3bc0174 | 2014-02-06 13:18:43 -0800 | [diff] [blame] | 629 | // Handle ins. |
buzbee | 8281864 | 2014-06-04 15:35:41 -0700 | [diff] [blame] | 630 | return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + |
| 631 | sizeof(StackReference<mirror::ArtMethod>); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 635 | static int GetOutVROffset(uint16_t out_num, InstructionSet isa) { |
buzbee | 8281864 | 2014-06-04 15:35:41 -0700 | [diff] [blame] | 636 | // According to stack model, the first out is above the Method referernce. |
| 637 | return sizeof(StackReference<mirror::ArtMethod>) + (out_num * sizeof(uint32_t)); |
Razvan A Lupusoru | 3bc0174 | 2014-02-06 13:18:43 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 640 | uintptr_t GetCurrentQuickFramePc() const { |
| 641 | return cur_quick_frame_pc_; |
| 642 | } |
| 643 | |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 644 | StackReference<mirror::ArtMethod>* GetCurrentQuickFrame() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 645 | return cur_quick_frame_; |
| 646 | } |
| 647 | |
| 648 | ShadowFrame* GetCurrentShadowFrame() const { |
| 649 | return cur_shadow_frame_; |
| 650 | } |
| 651 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 652 | HandleScope* GetCurrentHandleScope() const { |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 653 | StackReference<mirror::ArtMethod>* sp = GetCurrentQuickFrame(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 654 | ++sp; // Skip Method*; handle scope comes next; |
| 655 | return reinterpret_cast<HandleScope*>(sp); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 658 | std::string DescribeLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 659 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 660 | static size_t ComputeNumFrames(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 661 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 662 | static void DescribeStack(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 663 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 664 | private: |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 665 | // Private constructor known in the case that num_frames_ has already been computed. |
| 666 | StackVisitor(Thread* thread, Context* context, size_t num_frames) |
| 667 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 668 | |
Sebastien Hertz | 0bcb290 | 2014-06-17 15:52:45 +0200 | [diff] [blame] | 669 | bool GetGPR(uint32_t reg, uintptr_t* val) const; |
| 670 | bool SetGPR(uint32_t reg, uintptr_t value); |
| 671 | bool GetFPR(uint32_t reg, uintptr_t* val) const; |
| 672 | bool SetFPR(uint32_t reg, uintptr_t value); |
| 673 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 674 | void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 675 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 676 | Thread* const thread_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 677 | ShadowFrame* cur_shadow_frame_; |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 678 | StackReference<mirror::ArtMethod>* cur_quick_frame_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 679 | uintptr_t cur_quick_frame_pc_; |
| 680 | // Lazily computed, number of frames in the stack. |
| 681 | size_t num_frames_; |
| 682 | // Depth of the frame we're currently at. |
| 683 | size_t cur_depth_; |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 684 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 685 | protected: |
| 686 | Context* const context_; |
| 687 | }; |
| 688 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 689 | } // namespace art |
| 690 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 691 | #endif // ART_RUNTIME_STACK_H_ |