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 | |
| 17 | #ifndef ART_SRC_STACK_H_ |
| 18 | #define ART_SRC_STACK_H_ |
| 19 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 20 | #include "dex_file.h" |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 21 | #include "heap.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 22 | #include "instrumentation.h" |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 23 | #include "jni.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 24 | #include "macros.h" |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 25 | #include "oat/runtime/context.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 26 | |
| 27 | #include <stdint.h> |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 28 | #include <string> |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 32 | class AbstractMethod; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 33 | class Context; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 34 | class Object; |
| 35 | class ShadowFrame; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 36 | class StackIndirectReferenceTable; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 37 | class ScopedObjectAccess; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 38 | class Thread; |
| 39 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 40 | // The kind of vreg being accessed in calls to Set/GetVReg. |
| 41 | enum VRegKind { |
| 42 | kReferenceVReg, |
| 43 | kIntVReg, |
| 44 | kFloatVReg, |
| 45 | kLongLoVReg, |
| 46 | kLongHiVReg, |
| 47 | kDoubleLoVReg, |
| 48 | kDoubleHiVReg, |
| 49 | kConstant, |
| 50 | kImpreciseConstant, |
| 51 | kUndefined, |
| 52 | }; |
| 53 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 54 | class ShadowFrame { |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 55 | public: |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 56 | static ShadowFrame* Create(uint16_t num_refs, uint16_t num_vregs, ShadowFrame* link, |
| 57 | AbstractMethod* method, uint32_t dex_pc) { |
| 58 | size_t sz = sizeof(ShadowFrame) + (sizeof(Object*) * num_refs) + (sizeof(uint32_t) * num_vregs); |
| 59 | uint8_t* memory = new uint8_t[sz]; |
| 60 | return new (memory) ShadowFrame(num_refs, num_vregs, link, method, dex_pc); |
| 61 | } |
| 62 | ~ShadowFrame() {} |
| 63 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 64 | uint32_t NumberOfReferences() const { |
| 65 | return number_of_references_; |
| 66 | } |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 67 | |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 68 | void SetNumberOfReferences(uint16_t number_of_references) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 69 | number_of_references_ = number_of_references; |
| 70 | } |
| 71 | |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 72 | void SetNumberOfVRegs(uint16_t number_of_vregs) { |
| 73 | number_of_vregs_ = number_of_vregs; |
| 74 | } |
| 75 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 76 | uint32_t GetDexPC() const { |
| 77 | return dex_pc_; |
| 78 | } |
| 79 | |
| 80 | void SetDexPC(uint32_t dex_pc) { |
| 81 | dex_pc_ = dex_pc; |
| 82 | } |
| 83 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 84 | ShadowFrame* GetLink() const { |
| 85 | return link_; |
| 86 | } |
| 87 | |
| 88 | void SetLink(ShadowFrame* frame) { |
| 89 | DCHECK_NE(this, frame); |
| 90 | link_ = frame; |
| 91 | } |
| 92 | |
| 93 | Object* GetReference(size_t i) const { |
| 94 | DCHECK_LT(i, number_of_references_); |
| 95 | return references_[i]; |
| 96 | } |
| 97 | |
| 98 | void SetReference(size_t i, Object* object) { |
| 99 | DCHECK_LT(i, number_of_references_); |
| 100 | references_[i] = object; |
| 101 | } |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 102 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 103 | int32_t GetVReg(size_t i) const { |
| 104 | DCHECK_LT(i, number_of_vregs_); |
| 105 | const int8_t* vregs = reinterpret_cast<const int8_t*>(this) + VRegsOffset(); |
| 106 | return reinterpret_cast<const int32_t*>(vregs)[i]; |
| 107 | } |
| 108 | |
| 109 | float GetVRegFloat(size_t i) const { |
| 110 | DCHECK_LT(i, number_of_vregs_); |
| 111 | const int8_t* vregs = reinterpret_cast<const int8_t*>(this) + VRegsOffset(); |
| 112 | return reinterpret_cast<const float*>(vregs)[i]; |
| 113 | } |
| 114 | |
| 115 | int64_t GetVRegLong(size_t i) const { |
| 116 | const int8_t* vregs = reinterpret_cast<const int8_t*>(this) + VRegsOffset(); |
| 117 | const int32_t* low_half = &reinterpret_cast<const int32_t*>(vregs)[i]; |
| 118 | return *reinterpret_cast<const int64_t*>(low_half); |
| 119 | } |
| 120 | |
| 121 | double GetVRegDouble(size_t i) const { |
| 122 | const int8_t* vregs = reinterpret_cast<const int8_t*>(this) + VRegsOffset(); |
| 123 | const int32_t* low_half = &reinterpret_cast<const int32_t*>(vregs)[i]; |
| 124 | return *reinterpret_cast<const double*>(low_half); |
| 125 | } |
| 126 | |
| 127 | void SetVReg(size_t i, int32_t val) { |
| 128 | DCHECK_LT(i, number_of_vregs_); |
| 129 | int8_t* vregs = reinterpret_cast<int8_t*>(this) + VRegsOffset(); |
| 130 | reinterpret_cast<int32_t*>(vregs)[i] = val; |
| 131 | } |
| 132 | |
| 133 | void SetVRegFloat(size_t i, float val) { |
| 134 | DCHECK_LT(i, number_of_vregs_); |
| 135 | int8_t* vregs = reinterpret_cast<int8_t*>(this) + VRegsOffset(); |
| 136 | reinterpret_cast<float*>(vregs)[i] = val; |
| 137 | } |
| 138 | |
| 139 | void SetVRegLong(size_t i, int64_t val) { |
| 140 | int8_t* vregs = reinterpret_cast<int8_t*>(this) + VRegsOffset(); |
| 141 | int32_t* low_half = &reinterpret_cast<int32_t*>(vregs)[i]; |
| 142 | *reinterpret_cast<int64_t*>(low_half) = val; |
| 143 | } |
| 144 | |
| 145 | void SetVRegDouble(size_t i, double val) { |
| 146 | int8_t* vregs = reinterpret_cast<int8_t*>(this) + VRegsOffset(); |
| 147 | int32_t* low_half = &reinterpret_cast<int32_t*>(vregs)[i]; |
| 148 | *reinterpret_cast<double*>(low_half) = val; |
| 149 | } |
| 150 | |
| 151 | void SetReferenceAndVReg(size_t i, Object* val) { |
| 152 | SetReference(i, val); |
| 153 | SetVReg(i, reinterpret_cast<int32_t>(val)); |
| 154 | } |
| 155 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 156 | AbstractMethod* GetMethod() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 157 | DCHECK_NE(method_, static_cast<void*>(NULL)); |
| 158 | return method_; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 161 | void SetMethod(AbstractMethod* method) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 162 | DCHECK_NE(method, static_cast<void*>(NULL)); |
| 163 | method_ = method; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 166 | bool Contains(Object** shadow_frame_entry) const { |
| 167 | return ((&references_[0] <= shadow_frame_entry) && |
| 168 | (shadow_frame_entry <= (&references_[number_of_references_ - 1]))); |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 171 | template <typename Visitor> |
| 172 | void VisitRoots(const Visitor& visitor) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 173 | size_t num_refs = NumberOfReferences(); |
| 174 | for (size_t j = 0; j < num_refs; j++) { |
| 175 | Object* object = GetReference(j); |
| 176 | if (object != NULL) { |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 177 | visitor(object, j); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 182 | static size_t LinkOffset() { |
| 183 | return OFFSETOF_MEMBER(ShadowFrame, link_); |
| 184 | } |
| 185 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 186 | static size_t MethodOffset() { |
| 187 | return OFFSETOF_MEMBER(ShadowFrame, method_); |
| 188 | } |
| 189 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 190 | static size_t DexPCOffset() { |
| 191 | return OFFSETOF_MEMBER(ShadowFrame, dex_pc_); |
| 192 | } |
| 193 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 194 | static size_t NumberOfReferencesOffset() { |
| 195 | return OFFSETOF_MEMBER(ShadowFrame, number_of_references_); |
| 196 | } |
| 197 | |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 198 | static size_t NumberOfVRegsOffset() { |
| 199 | return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_); |
| 200 | } |
| 201 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 202 | static size_t ReferencesOffset() { |
| 203 | return OFFSETOF_MEMBER(ShadowFrame, references_); |
| 204 | } |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 205 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 206 | size_t VRegsOffset() const { |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 207 | return ReferencesOffset() + (sizeof(Object*) * NumberOfReferences()); |
| 208 | } |
| 209 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 210 | private: |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 211 | ShadowFrame(uint16_t num_refs, uint16_t num_vregs, ShadowFrame* link, AbstractMethod* method, |
| 212 | uint32_t dex_pc) |
| 213 | : number_of_references_ (num_refs), number_of_vregs_(num_vregs), link_(link), |
| 214 | method_(method), dex_pc_(dex_pc) { |
| 215 | for (size_t i = 0; i < num_refs; ++i) { |
| 216 | SetReference(i, NULL); |
| 217 | } |
| 218 | for (size_t i = 0; i < num_vregs; ++i) { |
| 219 | SetVReg(i, 0); |
| 220 | } |
| 221 | } |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 222 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 223 | // TODO: make the majority of these fields const. |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 224 | uint16_t number_of_references_; |
| 225 | uint16_t number_of_vregs_; |
| 226 | // Link to previous shadow frame or NULL. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 227 | ShadowFrame* link_; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 228 | AbstractMethod* method_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 229 | uint32_t dex_pc_; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 230 | Object* references_[0]; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 231 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 232 | DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame); |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 233 | }; |
| 234 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 235 | // The managed stack is used to record fragments of managed code stacks. Managed code stacks |
| 236 | // may either be shadow frames or lists of frames using fixed frame sizes. Transition records are |
| 237 | // necessary for transitions between code using different frame layouts and transitions into native |
| 238 | // code. |
Ian Rogers | df1ce91 | 2012-11-27 17:07:11 -0800 | [diff] [blame^] | 239 | class PACKED(4) ManagedStack { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 240 | public: |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 241 | ManagedStack() |
| 242 | : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {} |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 243 | |
| 244 | void PushManagedStackFragment(ManagedStack* fragment) { |
| 245 | // Copy this top fragment into given fragment. |
| 246 | memcpy(fragment, this, sizeof(ManagedStack)); |
| 247 | // Clear this fragment, which has become the top. |
| 248 | memset(this, 0, sizeof(ManagedStack)); |
| 249 | // Link our top fragment onto the given fragment. |
| 250 | link_ = fragment; |
| 251 | } |
| 252 | |
| 253 | void PopManagedStackFragment(const ManagedStack& fragment) { |
| 254 | DCHECK(&fragment == link_); |
| 255 | // Copy this given fragment back to the top. |
| 256 | memcpy(this, &fragment, sizeof(ManagedStack)); |
| 257 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 258 | |
| 259 | ManagedStack* GetLink() const { |
| 260 | return link_; |
| 261 | } |
| 262 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 263 | AbstractMethod** GetTopQuickFrame() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 264 | return top_quick_frame_; |
| 265 | } |
| 266 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 267 | void SetTopQuickFrame(AbstractMethod** top) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 268 | top_quick_frame_ = top; |
| 269 | } |
| 270 | |
| 271 | uintptr_t GetTopQuickFramePc() const { |
| 272 | return top_quick_frame_pc_; |
| 273 | } |
| 274 | |
| 275 | void SetTopQuickFramePc(uintptr_t pc) { |
| 276 | top_quick_frame_pc_ = pc; |
| 277 | } |
| 278 | |
| 279 | static size_t TopQuickFrameOffset() { |
| 280 | return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_); |
| 281 | } |
| 282 | |
| 283 | static size_t TopQuickFramePcOffset() { |
| 284 | return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_); |
| 285 | } |
| 286 | |
| 287 | ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) { |
| 288 | ShadowFrame* old_frame = top_shadow_frame_; |
| 289 | top_shadow_frame_ = new_top_frame; |
| 290 | new_top_frame->SetLink(old_frame); |
| 291 | return old_frame; |
| 292 | } |
| 293 | |
| 294 | ShadowFrame* PopShadowFrame() { |
| 295 | CHECK(top_shadow_frame_ != NULL); |
| 296 | ShadowFrame* frame = top_shadow_frame_; |
| 297 | top_shadow_frame_ = frame->GetLink(); |
| 298 | return frame; |
| 299 | } |
| 300 | |
| 301 | ShadowFrame* GetTopShadowFrame() const { |
| 302 | return top_shadow_frame_; |
| 303 | } |
| 304 | |
| 305 | static size_t TopShadowFrameOffset() { |
| 306 | return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_); |
| 307 | } |
| 308 | |
| 309 | size_t NumShadowFrameReferences() const; |
| 310 | |
| 311 | bool ShadowFramesContain(Object** shadow_frame_entry) const; |
| 312 | |
| 313 | private: |
| 314 | ManagedStack* link_; |
| 315 | ShadowFrame* top_shadow_frame_; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 316 | AbstractMethod** top_quick_frame_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 317 | uintptr_t top_quick_frame_pc_; |
| 318 | }; |
| 319 | |
| 320 | class StackVisitor { |
| 321 | protected: |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 322 | StackVisitor(const ManagedStack* stack, const std::vector<InstrumentationStackFrame>* instrumentation_stack, |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 323 | Context* context) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 324 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 325 | : stack_start_(stack), instrumentation_stack_(instrumentation_stack), cur_shadow_frame_(NULL), |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 326 | cur_quick_frame_(NULL), cur_quick_frame_pc_(0), num_frames_(0), cur_depth_(0), |
| 327 | context_(context) {} |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 328 | |
| 329 | public: |
| 330 | virtual ~StackVisitor() {} |
| 331 | |
| 332 | // 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] | 333 | virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 334 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 335 | void WalkStack(bool include_transitions = false) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 336 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 337 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 338 | AbstractMethod* GetMethod() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 339 | if (cur_shadow_frame_ != NULL) { |
| 340 | return cur_shadow_frame_->GetMethod(); |
| 341 | } else if (cur_quick_frame_ != NULL) { |
| 342 | return *cur_quick_frame_; |
| 343 | } else { |
| 344 | return NULL; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | bool IsShadowFrame() const { |
| 349 | return cur_shadow_frame_ != NULL; |
| 350 | } |
| 351 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 352 | uint32_t GetDexPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 353 | |
| 354 | size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 355 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 356 | uintptr_t LoadCalleeSave(int num, size_t frame_size) const { |
| 357 | // Callee saves are held at the top of the frame |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 358 | AbstractMethod* method = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 359 | DCHECK(method != NULL); |
| 360 | byte* save_addr = |
| 361 | reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize); |
| 362 | #if defined(__i386__) |
| 363 | save_addr -= kPointerSize; // account for return address |
| 364 | #endif |
| 365 | return *reinterpret_cast<uintptr_t*>(save_addr); |
| 366 | } |
| 367 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 368 | // 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] | 369 | size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 370 | return GetNumFrames() - cur_depth_; |
| 371 | } |
| 372 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 373 | // Returns a frame ID for JDWP use, starting from 1. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 374 | size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 375 | return GetFrameHeight() + 1; |
| 376 | } |
| 377 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 378 | size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 379 | if (num_frames_ == 0) { |
| 380 | num_frames_ = ComputeNumFrames(); |
| 381 | } |
| 382 | return num_frames_; |
| 383 | } |
| 384 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 385 | uint32_t GetVReg(AbstractMethod* m, uint16_t vreg, VRegKind kind) const |
| 386 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 387 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 388 | void SetVReg(AbstractMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 389 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 390 | |
| 391 | uintptr_t GetGPR(uint32_t reg) const; |
| 392 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 393 | uint32_t GetVReg(AbstractMethod** cur_quick_frame, const DexFile::CodeItem* code_item, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 394 | uint32_t core_spills, uint32_t fp_spills, size_t frame_size, |
| 395 | uint16_t vreg) const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 396 | int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg); |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 397 | DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame()); |
| 398 | byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 399 | return *reinterpret_cast<uint32_t*>(vreg_addr); |
| 400 | } |
| 401 | |
| 402 | uintptr_t GetReturnPc() const; |
| 403 | |
| 404 | void SetReturnPc(uintptr_t new_ret_pc); |
| 405 | |
| 406 | /* |
| 407 | * Return sp-relative offset for a Dalvik virtual register, compiler |
| 408 | * spill or Method* in bytes using Method*. |
| 409 | * Note that (reg >= 0) refers to a Dalvik register, (reg == -2) |
| 410 | * denotes Method* and (reg <= -3) denotes a compiler temp. |
| 411 | * |
| 412 | * +------------------------+ |
| 413 | * | IN[ins-1] | {Note: resides in caller's frame} |
| 414 | * | . | |
| 415 | * | IN[0] | |
| 416 | * | caller's Method* | |
| 417 | * +========================+ {Note: start of callee's frame} |
| 418 | * | core callee-save spill | {variable sized} |
| 419 | * +------------------------+ |
| 420 | * | fp callee-save spill | |
| 421 | * +------------------------+ |
| 422 | * | filler word | {For compatibility, if V[locals-1] used as wide |
| 423 | * +------------------------+ |
| 424 | * | V[locals-1] | |
| 425 | * | V[locals-2] | |
| 426 | * | . | |
| 427 | * | . | ... (reg == 2) |
| 428 | * | V[1] | ... (reg == 1) |
| 429 | * | V[0] | ... (reg == 0) <---- "locals_start" |
| 430 | * +------------------------+ |
| 431 | * | Compiler temps | ... (reg == -2) |
| 432 | * | | ... (reg == -3) |
| 433 | * | | ... (reg == -4) |
| 434 | * +------------------------+ |
| 435 | * | stack alignment padding| {0 to (kStackAlignWords-1) of padding} |
| 436 | * +------------------------+ |
| 437 | * | OUT[outs-1] | |
| 438 | * | OUT[outs-2] | |
| 439 | * | . | |
| 440 | * | OUT[0] | |
| 441 | * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned |
| 442 | * +========================+ |
| 443 | */ |
| 444 | static int GetVRegOffset(const DexFile::CodeItem* code_item, |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 445 | uint32_t core_spills, uint32_t fp_spills, |
| 446 | size_t frame_size, int reg) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 447 | DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U); |
| 448 | int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler. |
| 449 | int num_ins = code_item->ins_size_; |
| 450 | int num_regs = code_item->registers_size_ - num_ins; |
| 451 | int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t)); |
| 452 | if (reg == -2) { |
| 453 | return 0; // Method* |
| 454 | } else if (reg <= -3) { |
| 455 | return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp. |
| 456 | } else if (reg < num_regs) { |
| 457 | return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg. |
| 458 | } else { |
| 459 | return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in. |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | uintptr_t GetCurrentQuickFramePc() const { |
| 464 | return cur_quick_frame_pc_; |
| 465 | } |
| 466 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 467 | AbstractMethod** GetCurrentQuickFrame() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 468 | return cur_quick_frame_; |
| 469 | } |
| 470 | |
| 471 | ShadowFrame* GetCurrentShadowFrame() const { |
| 472 | return cur_shadow_frame_; |
| 473 | } |
| 474 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 475 | StackIndirectReferenceTable* GetCurrentSirt() const { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 476 | AbstractMethod** sp = GetCurrentQuickFrame(); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 477 | ++sp; // Skip Method*; SIRT comes next; |
| 478 | return reinterpret_cast<StackIndirectReferenceTable*>(sp); |
| 479 | } |
| 480 | |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 481 | std::string DescribeLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 482 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 483 | private: |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 484 | size_t ComputeNumFrames() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 485 | |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 486 | InstrumentationStackFrame GetInstrumentationStackFrame(uint32_t depth) const { |
| 487 | return instrumentation_stack_->at(instrumentation_stack_->size() - depth - 1); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 490 | void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 491 | |
| 492 | const ManagedStack* const stack_start_; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 493 | const std::vector<InstrumentationStackFrame>* const instrumentation_stack_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 494 | ShadowFrame* cur_shadow_frame_; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 495 | AbstractMethod** cur_quick_frame_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 496 | uintptr_t cur_quick_frame_pc_; |
| 497 | // Lazily computed, number of frames in the stack. |
| 498 | size_t num_frames_; |
| 499 | // Depth of the frame we're currently at. |
| 500 | size_t cur_depth_; |
| 501 | protected: |
| 502 | Context* const context_; |
| 503 | }; |
| 504 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 505 | class VmapTable { |
| 506 | public: |
| 507 | explicit VmapTable(const uint16_t* table) : table_(table) { |
| 508 | } |
| 509 | |
| 510 | uint16_t operator[](size_t i) const { |
| 511 | return table_[i + 1]; |
| 512 | } |
| 513 | |
| 514 | size_t size() const { |
| 515 | return table_[0]; |
| 516 | } |
| 517 | |
| 518 | // Is the dex register 'vreg' in the context or on the stack? Should not be called when the |
| 519 | // 'kind' is unknown or constant. |
| 520 | bool IsInContext(size_t vreg, uint32_t& vmap_offset, VRegKind kind) const { |
| 521 | DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg || |
| 522 | kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg || |
| 523 | kind == kDoubleHiVReg || kind == kImpreciseConstant); |
| 524 | vmap_offset = 0xEBAD0FF5; |
| 525 | // TODO: take advantage of the registers being ordered |
| 526 | // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values |
| 527 | // are never promoted to floating point registers. |
| 528 | bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); |
| 529 | bool in_floats = false; |
| 530 | for (size_t i = 0; i < size(); ++i) { |
| 531 | // Stop if we find what we are are looking for. |
| 532 | if ((table_[i + 1] == vreg) && (in_floats == is_float)) { |
| 533 | vmap_offset = i; |
| 534 | return true; |
| 535 | } |
| 536 | // 0xffff is the marker for LR (return PC on x86), following it are spilled float registers. |
| 537 | if (table_[i + 1] == 0xffff) { |
| 538 | in_floats = true; |
| 539 | } |
| 540 | } |
| 541 | return false; |
| 542 | } |
| 543 | |
| 544 | // Compute the register number that corresponds to the entry in the vmap (vmap_offset, computed |
| 545 | // by IsInContext above). If the kind is floating point then the result will be a floating point |
| 546 | // register number, otherwise it will be an integer register number. |
| 547 | uint32_t ComputeRegister(uint32_t spill_mask, uint32_t vmap_offset, VRegKind kind) const { |
| 548 | // Compute the register we need to load from the context. |
| 549 | DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg || |
| 550 | kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg || |
| 551 | kind == kDoubleHiVReg || kind == kImpreciseConstant); |
| 552 | // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values |
| 553 | // are never promoted to floating point registers. |
| 554 | bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); |
| 555 | uint32_t matches = 0; |
| 556 | if (is_float) { |
| 557 | while (table_[matches] != 0xffff) { |
| 558 | matches++; |
| 559 | } |
| 560 | } |
| 561 | CHECK_LT(vmap_offset - matches, static_cast<uint32_t>(__builtin_popcount(spill_mask))); |
| 562 | uint32_t spill_shifts = 0; |
| 563 | while (matches != (vmap_offset + 1)) { |
| 564 | DCHECK_NE(spill_mask, 0u); |
| 565 | matches += spill_mask & 1; // Add 1 if the low bit is set |
| 566 | spill_mask >>= 1; |
| 567 | spill_shifts++; |
| 568 | } |
| 569 | spill_shifts--; // wind back one as we want the last match |
| 570 | return spill_shifts; |
| 571 | } |
| 572 | private: |
| 573 | const uint16_t* table_; |
| 574 | }; |
| 575 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 576 | } // namespace art |
| 577 | |
| 578 | #endif // ART_SRC_STACK_H_ |