blob: eb187b2b84e58f2264fafb4768bc7cc2a21abe13 [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
17#ifndef ART_SRC_STACK_H_
18#define ART_SRC_STACK_H_
19
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080020#include "dex_file.h"
jeffhao725a9572012-11-13 18:20:12 -080021#include "instrumentation.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "base/macros.h"
Ian Rogers0399dde2012-06-06 17:09:28 -070023#include "oat/runtime/context.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070024
25#include <stdint.h>
Ian Rogers40e3bac2012-11-20 00:09:14 -080026#include <string>
Elliott Hughes68e76522011-10-05 13:22:16 -070027
28namespace art {
29
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030namespace mirror {
Mathieu Chartier66f19252012-09-18 08:57:04 -070031class AbstractMethod;
Ian Rogers0399dde2012-06-06 17:09:28 -070032class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033} // namespace mirror
34
35class Context;
Ian Rogers0399dde2012-06-06 17:09:28 -070036class ShadowFrame;
Elliott Hughes08fc03a2012-06-26 17:34:00 -070037class StackIndirectReferenceTable;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038class ScopedObjectAccess;
Elliott Hughes68e76522011-10-05 13:22:16 -070039class Thread;
40
Ian Rogers2bcb4a42012-11-08 10:39:18 -080041// The kind of vreg being accessed in calls to Set/GetVReg.
42enum VRegKind {
43 kReferenceVReg,
44 kIntVReg,
45 kFloatVReg,
46 kLongLoVReg,
47 kLongHiVReg,
48 kDoubleLoVReg,
49 kDoubleHiVReg,
50 kConstant,
51 kImpreciseConstant,
52 kUndefined,
53};
54
Mathieu Chartier67022432012-11-29 18:04:50 -080055// ShadowFrame has 3 possible layouts:
56// - portable - a unified array of VRegs and references. Precise references need GC maps.
57// - interpreter - separate VRegs and reference arrays. References are in the reference array.
58// - JNI - just VRegs, but where every VReg holds a reference.
Ian Rogers0399dde2012-06-06 17:09:28 -070059class ShadowFrame {
Elliott Hughes68e76522011-10-05 13:22:16 -070060 public:
TDYa127ce4cc0d2012-11-18 16:59:53 -080061 // Create ShadowFrame for interpreter.
62 static ShadowFrame* Create(uint32_t num_vregs, ShadowFrame* link,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 mirror::AbstractMethod* method, uint32_t dex_pc) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080064 size_t sz = sizeof(ShadowFrame) +
65 (sizeof(uint32_t) * num_vregs) +
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 (sizeof(mirror::Object*) * num_vregs);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070067 uint8_t* memory = new uint8_t[sz];
TDYa127ce4cc0d2012-11-18 16:59:53 -080068 ShadowFrame* sf = new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true);
69 return sf;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070070 }
71 ~ShadowFrame() {}
72
TDYa127ce4cc0d2012-11-18 16:59:53 -080073 bool HasReferenceArray() const {
74 return (number_of_vregs_ & kHasReferenceArray) != 0;
Ian Rogers0399dde2012-06-06 17:09:28 -070075 }
Elliott Hughes68e76522011-10-05 13:22:16 -070076
TDYa127ce4cc0d2012-11-18 16:59:53 -080077 uint32_t NumberOfVRegs() const {
78 return number_of_vregs_ & ~kHasReferenceArray;
Ian Rogers0399dde2012-06-06 17:09:28 -070079 }
80
TDYa127ce4cc0d2012-11-18 16:59:53 -080081 void SetNumberOfVRegs(uint32_t number_of_vregs) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080082 number_of_vregs_ = number_of_vregs | (number_of_vregs_ & kHasReferenceArray);
Ian Rogers5438ad82012-10-15 17:22:44 -070083 }
84
Ian Rogers0399dde2012-06-06 17:09:28 -070085 uint32_t GetDexPC() const {
86 return dex_pc_;
87 }
88
89 void SetDexPC(uint32_t dex_pc) {
90 dex_pc_ = dex_pc;
91 }
92
Ian Rogers0399dde2012-06-06 17:09:28 -070093 ShadowFrame* GetLink() const {
94 return link_;
95 }
96
97 void SetLink(ShadowFrame* frame) {
98 DCHECK_NE(this, frame);
99 link_ = frame;
100 }
101
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700102 int32_t GetVReg(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800103 DCHECK_LT(i, NumberOfVRegs());
104 const uint32_t* vreg = &vregs_[i];
105 return *reinterpret_cast<const int32_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700106 }
107
108 float GetVRegFloat(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800109 DCHECK_LT(i, NumberOfVRegs());
110 // NOTE: Strict-aliasing?
111 const uint32_t* vreg = &vregs_[i];
112 return *reinterpret_cast<const float*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700113 }
114
115 int64_t GetVRegLong(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800116 const uint32_t* vreg = &vregs_[i];
117 return *reinterpret_cast<const int64_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700118 }
119
120 double GetVRegDouble(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800121 const uint32_t* vreg = &vregs_[i];
122 return *reinterpret_cast<const double*>(vreg);
123 }
124
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125 mirror::Object* GetVRegReference(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800126 DCHECK_LT(i, NumberOfVRegs());
127 if (HasReferenceArray()) {
128 return References()[i];
129 } else {
130 const uint32_t* vreg = &vregs_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 return *reinterpret_cast<mirror::Object* const*>(vreg);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800132 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700133 }
134
135 void SetVReg(size_t i, int32_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800136 DCHECK_LT(i, NumberOfVRegs());
137 uint32_t* vreg = &vregs_[i];
138 *reinterpret_cast<int32_t*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700139 }
140
141 void SetVRegFloat(size_t i, float val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800142 DCHECK_LT(i, NumberOfVRegs());
143 uint32_t* vreg = &vregs_[i];
144 *reinterpret_cast<float*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700145 }
146
147 void SetVRegLong(size_t i, int64_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800148 uint32_t* vreg = &vregs_[i];
149 *reinterpret_cast<int64_t*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700150 }
151
152 void SetVRegDouble(size_t i, double val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800153 uint32_t* vreg = &vregs_[i];
154 *reinterpret_cast<double*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700155 }
156
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157 void SetVRegReference(size_t i, mirror::Object* val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800158 DCHECK_LT(i, NumberOfVRegs());
159 uint32_t* vreg = &vregs_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160 *reinterpret_cast<mirror::Object**>(vreg) = val;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800161 if (HasReferenceArray()) {
162 References()[i] = val;
163 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700164 }
165
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166 mirror::AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700167 DCHECK_NE(method_, static_cast<void*>(NULL));
168 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700169 }
170
Ian Rogers62d6c772013-02-27 08:32:07 -0800171 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
172
173 ThrowLocation GetCurrentLocationForThrow() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
174
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 void SetMethod(mirror::AbstractMethod* method) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700176 DCHECK_NE(method, static_cast<void*>(NULL));
177 method_ = method;
Elliott Hughes68e76522011-10-05 13:22:16 -0700178 }
179
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 bool Contains(mirror::Object** shadow_frame_entry_obj) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800181 if (HasReferenceArray()) {
182 return ((&References()[0] <= shadow_frame_entry_obj) &&
183 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
184 } else {
185 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
186 return ((&vregs_[0] <= shadow_frame_entry) &&
187 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
Ian Rogers0399dde2012-06-06 17:09:28 -0700188 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700189 }
190
Ian Rogers0399dde2012-06-06 17:09:28 -0700191 static size_t LinkOffset() {
192 return OFFSETOF_MEMBER(ShadowFrame, link_);
193 }
194
Ian Rogers0399dde2012-06-06 17:09:28 -0700195 static size_t MethodOffset() {
196 return OFFSETOF_MEMBER(ShadowFrame, method_);
197 }
198
Ian Rogers0399dde2012-06-06 17:09:28 -0700199 static size_t DexPCOffset() {
200 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
201 }
202
Ian Rogers5438ad82012-10-15 17:22:44 -0700203 static size_t NumberOfVRegsOffset() {
204 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
205 }
206
TDYa127ce4cc0d2012-11-18 16:59:53 -0800207 static size_t VRegsOffset() {
208 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
Ian Rogers5438ad82012-10-15 17:22:44 -0700209 }
210
Elliott Hughes68e76522011-10-05 13:22:16 -0700211 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800212 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, mirror::AbstractMethod* method,
213 uint32_t dex_pc, bool has_reference_array)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800214 : number_of_vregs_(num_vregs), link_(link), method_(method), dex_pc_(dex_pc) {
Mathieu Chartier67022432012-11-29 18:04:50 -0800215 CHECK_LT(num_vregs, static_cast<uint32_t>(kHasReferenceArray));
TDYa127ce4cc0d2012-11-18 16:59:53 -0800216 if (has_reference_array) {
217 number_of_vregs_ |= kHasReferenceArray;
218 for (size_t i = 0; i < num_vregs; ++i) {
219 SetVRegReference(i, NULL);
220 }
Mathieu Chartier67022432012-11-29 18:04:50 -0800221 } else {
222 for (size_t i = 0; i < num_vregs; ++i) {
223 SetVReg(i, 0);
224 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700225 }
226 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700227
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228 mirror::Object* const* References() const {
Mathieu Chartier67022432012-11-29 18:04:50 -0800229 DCHECK(HasReferenceArray());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800230 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800231 return reinterpret_cast<mirror::Object* const*>(vreg_end);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800232 }
233
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234 mirror::Object** References() {
235 return const_cast<mirror::Object**>(const_cast<const ShadowFrame*>(this)->References());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800236 }
237
238 enum ShadowFrameFlag {
239 kHasReferenceArray = 1ul << 31
240 };
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700241 // TODO: make the majority of these fields const.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800242 uint32_t number_of_vregs_;
Ian Rogers5438ad82012-10-15 17:22:44 -0700243 // Link to previous shadow frame or NULL.
Ian Rogers0399dde2012-06-06 17:09:28 -0700244 ShadowFrame* link_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800245 mirror::AbstractMethod* method_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700246 uint32_t dex_pc_;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800247 uint32_t vregs_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700248
Ian Rogers0399dde2012-06-06 17:09:28 -0700249 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700250};
251
Ian Rogers0399dde2012-06-06 17:09:28 -0700252// The managed stack is used to record fragments of managed code stacks. Managed code stacks
253// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
254// necessary for transitions between code using different frame layouts and transitions into native
255// code.
Ian Rogersdf1ce912012-11-27 17:07:11 -0800256class PACKED(4) ManagedStack {
Ian Rogers0399dde2012-06-06 17:09:28 -0700257 public:
Ian Rogersca190662012-06-26 15:45:57 -0700258 ManagedStack()
259 : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700260
261 void PushManagedStackFragment(ManagedStack* fragment) {
262 // Copy this top fragment into given fragment.
263 memcpy(fragment, this, sizeof(ManagedStack));
264 // Clear this fragment, which has become the top.
265 memset(this, 0, sizeof(ManagedStack));
266 // Link our top fragment onto the given fragment.
267 link_ = fragment;
268 }
269
270 void PopManagedStackFragment(const ManagedStack& fragment) {
271 DCHECK(&fragment == link_);
272 // Copy this given fragment back to the top.
273 memcpy(this, &fragment, sizeof(ManagedStack));
274 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700275
276 ManagedStack* GetLink() const {
277 return link_;
278 }
279
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280 mirror::AbstractMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700281 return top_quick_frame_;
282 }
283
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800284 void SetTopQuickFrame(mirror::AbstractMethod** top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200285 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700286 top_quick_frame_ = top;
287 }
288
289 uintptr_t GetTopQuickFramePc() const {
290 return top_quick_frame_pc_;
291 }
292
293 void SetTopQuickFramePc(uintptr_t pc) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200294 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700295 top_quick_frame_pc_ = pc;
296 }
297
298 static size_t TopQuickFrameOffset() {
299 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
300 }
301
302 static size_t TopQuickFramePcOffset() {
303 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_);
304 }
305
306 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200307 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700308 ShadowFrame* old_frame = top_shadow_frame_;
309 top_shadow_frame_ = new_top_frame;
310 new_top_frame->SetLink(old_frame);
311 return old_frame;
312 }
313
314 ShadowFrame* PopShadowFrame() {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200315 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700316 CHECK(top_shadow_frame_ != NULL);
317 ShadowFrame* frame = top_shadow_frame_;
318 top_shadow_frame_ = frame->GetLink();
319 return frame;
320 }
321
322 ShadowFrame* GetTopShadowFrame() const {
323 return top_shadow_frame_;
324 }
325
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800326 void SetTopShadowFrame(ShadowFrame* top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200327 DCHECK(top_quick_frame_ == NULL);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800328 top_shadow_frame_ = top;
329 }
330
Ian Rogers0399dde2012-06-06 17:09:28 -0700331 static size_t TopShadowFrameOffset() {
332 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
333 }
334
TDYa127ce4cc0d2012-11-18 16:59:53 -0800335 size_t NumJniShadowFrameReferences() const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700336
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800337 bool ShadowFramesContain(mirror::Object** shadow_frame_entry) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700338
339 private:
340 ManagedStack* link_;
341 ShadowFrame* top_shadow_frame_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342 mirror::AbstractMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700343 uintptr_t top_quick_frame_pc_;
344};
345
346class StackVisitor {
347 protected:
Ian Rogers7a22fa62013-01-23 12:16:16 -0800348 StackVisitor(Thread* thread, Context* context) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700349
350 public:
351 virtual ~StackVisitor() {}
352
353 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700354 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700355
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700356 void WalkStack(bool include_transitions = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700357 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700358
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800359 mirror::AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700360 if (cur_shadow_frame_ != NULL) {
361 return cur_shadow_frame_->GetMethod();
362 } else if (cur_quick_frame_ != NULL) {
363 return *cur_quick_frame_;
364 } else {
365 return NULL;
366 }
367 }
368
369 bool IsShadowFrame() const {
370 return cur_shadow_frame_ != NULL;
371 }
372
Ian Rogers0c7abda2012-09-19 13:33:42 -0700373 uint32_t GetDexPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
374
Ian Rogers62d6c772013-02-27 08:32:07 -0800375 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
376
Ian Rogers0c7abda2012-09-19 13:33:42 -0700377 size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
378
Mathieu Chartier67022432012-11-29 18:04:50 -0800379 uintptr_t* CalleeSaveAddress(int num, size_t frame_size) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700380 // Callee saves are held at the top of the frame
Mathieu Chartier67022432012-11-29 18:04:50 -0800381 DCHECK(GetMethod() != NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700382 byte* save_addr =
383 reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize);
384#if defined(__i386__)
385 save_addr -= kPointerSize; // account for return address
386#endif
Mathieu Chartier67022432012-11-29 18:04:50 -0800387 return reinterpret_cast<uintptr_t*>(save_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700388 }
389
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700390 // Returns the height of the stack in the managed stack frames, including transitions.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700391 size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800392 return GetNumFrames() - cur_depth_ - 1;
Ian Rogers0399dde2012-06-06 17:09:28 -0700393 }
394
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700395 // Returns a frame ID for JDWP use, starting from 1.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700396 size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700397 return GetFrameHeight() + 1;
398 }
399
Ian Rogersb726dcb2012-09-05 08:57:23 -0700400 size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700401 if (num_frames_ == 0) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800402 num_frames_ = ComputeNumFrames(thread_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700403 }
404 return num_frames_;
405 }
406
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800407 uint32_t GetVReg(mirror::AbstractMethod* m, uint16_t vreg, VRegKind kind) const
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800408 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700409
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410 void SetVReg(mirror::AbstractMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700411 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700412
413 uintptr_t GetGPR(uint32_t reg) const;
Mathieu Chartier67022432012-11-29 18:04:50 -0800414 void SetGPR(uint32_t reg, uintptr_t value);
Ian Rogers0399dde2012-06-06 17:09:28 -0700415
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800416 uint32_t GetVReg(mirror::AbstractMethod** cur_quick_frame, const DexFile::CodeItem* code_item,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800417 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
418 uint16_t vreg) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700419 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700420 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
421 byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700422 return *reinterpret_cast<uint32_t*>(vreg_addr);
423 }
424
425 uintptr_t GetReturnPc() const;
426
427 void SetReturnPc(uintptr_t new_ret_pc);
428
429 /*
430 * Return sp-relative offset for a Dalvik virtual register, compiler
431 * spill or Method* in bytes using Method*.
432 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
433 * denotes Method* and (reg <= -3) denotes a compiler temp.
434 *
435 * +------------------------+
436 * | IN[ins-1] | {Note: resides in caller's frame}
437 * | . |
438 * | IN[0] |
439 * | caller's Method* |
440 * +========================+ {Note: start of callee's frame}
441 * | core callee-save spill | {variable sized}
442 * +------------------------+
443 * | fp callee-save spill |
444 * +------------------------+
445 * | filler word | {For compatibility, if V[locals-1] used as wide
446 * +------------------------+
447 * | V[locals-1] |
448 * | V[locals-2] |
449 * | . |
450 * | . | ... (reg == 2)
451 * | V[1] | ... (reg == 1)
452 * | V[0] | ... (reg == 0) <---- "locals_start"
453 * +------------------------+
454 * | Compiler temps | ... (reg == -2)
455 * | | ... (reg == -3)
456 * | | ... (reg == -4)
457 * +------------------------+
458 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
459 * +------------------------+
460 * | OUT[outs-1] |
461 * | OUT[outs-2] |
462 * | . |
463 * | OUT[0] |
464 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
465 * +========================+
466 */
467 static int GetVRegOffset(const DexFile::CodeItem* code_item,
Ian Rogersb23a7722012-10-09 16:54:26 -0700468 uint32_t core_spills, uint32_t fp_spills,
469 size_t frame_size, int reg) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700470 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
471 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler.
472 int num_ins = code_item->ins_size_;
473 int num_regs = code_item->registers_size_ - num_ins;
474 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
475 if (reg == -2) {
476 return 0; // Method*
477 } else if (reg <= -3) {
478 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp.
479 } else if (reg < num_regs) {
480 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg.
481 } else {
482 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in.
483 }
484 }
485
486 uintptr_t GetCurrentQuickFramePc() const {
487 return cur_quick_frame_pc_;
488 }
489
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800490 mirror::AbstractMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700491 return cur_quick_frame_;
492 }
493
494 ShadowFrame* GetCurrentShadowFrame() const {
495 return cur_shadow_frame_;
496 }
497
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700498 StackIndirectReferenceTable* GetCurrentSirt() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800499 mirror::AbstractMethod** sp = GetCurrentQuickFrame();
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700500 ++sp; // Skip Method*; SIRT comes next;
501 return reinterpret_cast<StackIndirectReferenceTable*>(sp);
502 }
503
Ian Rogers40e3bac2012-11-20 00:09:14 -0800504 std::string DescribeLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
505
Ian Rogers7a22fa62013-01-23 12:16:16 -0800506 static size_t ComputeNumFrames(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800507
Ian Rogers7a22fa62013-01-23 12:16:16 -0800508 static void DescribeStack(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800509
Ian Rogers0399dde2012-06-06 17:09:28 -0700510 private:
Ian Rogers0399dde2012-06-06 17:09:28 -0700511
Ian Rogers62d6c772013-02-27 08:32:07 -0800512 instrumentation::InstrumentationStackFrame GetInstrumentationStackFrame(uint32_t depth) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700513
Ian Rogersb726dcb2012-09-05 08:57:23 -0700514 void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700515
Ian Rogers7a22fa62013-01-23 12:16:16 -0800516 Thread* const thread_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700517 ShadowFrame* cur_shadow_frame_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800518 mirror::AbstractMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700519 uintptr_t cur_quick_frame_pc_;
520 // Lazily computed, number of frames in the stack.
521 size_t num_frames_;
522 // Depth of the frame we're currently at.
523 size_t cur_depth_;
524 protected:
525 Context* const context_;
526};
527
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800528class VmapTable {
529 public:
530 explicit VmapTable(const uint16_t* table) : table_(table) {
531 }
532
533 uint16_t operator[](size_t i) const {
534 return table_[i + 1];
535 }
536
537 size_t size() const {
538 return table_[0];
539 }
540
541 // Is the dex register 'vreg' in the context or on the stack? Should not be called when the
542 // 'kind' is unknown or constant.
543 bool IsInContext(size_t vreg, uint32_t& vmap_offset, VRegKind kind) const {
544 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
545 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
546 kind == kDoubleHiVReg || kind == kImpreciseConstant);
547 vmap_offset = 0xEBAD0FF5;
548 // TODO: take advantage of the registers being ordered
549 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
550 // are never promoted to floating point registers.
551 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
552 bool in_floats = false;
553 for (size_t i = 0; i < size(); ++i) {
554 // Stop if we find what we are are looking for.
555 if ((table_[i + 1] == vreg) && (in_floats == is_float)) {
556 vmap_offset = i;
557 return true;
558 }
559 // 0xffff is the marker for LR (return PC on x86), following it are spilled float registers.
560 if (table_[i + 1] == 0xffff) {
561 in_floats = true;
562 }
563 }
564 return false;
565 }
566
567 // Compute the register number that corresponds to the entry in the vmap (vmap_offset, computed
568 // by IsInContext above). If the kind is floating point then the result will be a floating point
569 // register number, otherwise it will be an integer register number.
570 uint32_t ComputeRegister(uint32_t spill_mask, uint32_t vmap_offset, VRegKind kind) const {
571 // Compute the register we need to load from the context.
572 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
573 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
574 kind == kDoubleHiVReg || kind == kImpreciseConstant);
575 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
576 // are never promoted to floating point registers.
577 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
578 uint32_t matches = 0;
579 if (is_float) {
580 while (table_[matches] != 0xffff) {
581 matches++;
582 }
583 }
584 CHECK_LT(vmap_offset - matches, static_cast<uint32_t>(__builtin_popcount(spill_mask)));
585 uint32_t spill_shifts = 0;
586 while (matches != (vmap_offset + 1)) {
587 DCHECK_NE(spill_mask, 0u);
588 matches += spill_mask & 1; // Add 1 if the low bit is set
589 spill_mask >>= 1;
590 spill_shifts++;
591 }
592 spill_shifts--; // wind back one as we want the last match
593 return spill_shifts;
594 }
595 private:
596 const uint16_t* table_;
597};
598
Elliott Hughes68e76522011-10-05 13:22:16 -0700599} // namespace art
600
601#endif // ART_SRC_STACK_H_