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 | #include "stack.h" |
| 18 | |
| 19 | #include "compiler.h" |
| 20 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 21 | #include "object_utils.h" |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 22 | #include "thread_list.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 23 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 24 | namespace art { |
| 25 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 26 | bool Frame::HasMethod() const { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 27 | return GetMethod() != NULL && (!GetMethod()->IsCalleeSaveMethod()); |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void Frame::Next() { |
| 31 | size_t frame_size = GetMethod()->GetFrameSizeInBytes(); |
| 32 | DCHECK_NE(frame_size, 0u); |
| 33 | DCHECK_LT(frame_size, 1024u); |
| 34 | byte* next_sp = reinterpret_cast<byte*>(sp_) + frame_size; |
| 35 | sp_ = reinterpret_cast<Method**>(next_sp); |
| 36 | if (*sp_ != NULL) { |
| 37 | DCHECK((*sp_)->GetClass() == Method::GetMethodClass() || |
| 38 | (*sp_)->GetClass() == Method::GetConstructorClass()); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | uintptr_t Frame::GetReturnPC() const { |
| 43 | byte* pc_addr = reinterpret_cast<byte*>(sp_) + GetMethod()->GetReturnPcOffsetInBytes(); |
| 44 | return *reinterpret_cast<uintptr_t*>(pc_addr); |
| 45 | } |
| 46 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 47 | void Frame::SetReturnPC(uintptr_t pc) { |
| 48 | byte* pc_addr = reinterpret_cast<byte*>(sp_) + GetMethod()->GetReturnPcOffsetInBytes(); |
| 49 | *reinterpret_cast<uintptr_t*>(pc_addr) = pc; |
| 50 | } |
| 51 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 52 | /* Return sp-relative offset in bytes using Method* */ |
| 53 | static int GetVRegOffset(const DexFile::CodeItem* code_item, |
| 54 | uint32_t core_spills, uint32_t fp_spills, |
| 55 | size_t frame_size, int reg) |
| 56 | { |
| 57 | static const int kStackAlignWords = kStackAlignment/sizeof(uint32_t); |
| 58 | int numIns = code_item->ins_size_; |
| 59 | int numRegs = code_item->registers_size_ - numIns; |
| 60 | int numOuts = code_item->outs_size_; |
| 61 | int numSpills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills); |
| 62 | int numPadding = (kStackAlignWords - (numSpills + numRegs + numOuts + 2)) & (kStackAlignWords - 1); |
| 63 | int regsOffset = (numOuts + numPadding + 1) * 4; |
| 64 | int insOffset = frame_size + 4; |
| 65 | return (reg < numRegs) ? regsOffset + (reg << 2) : |
| 66 | insOffset + ((reg - numRegs) << 2); |
| 67 | } |
| 68 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 69 | uint32_t Frame::GetVReg(const DexFile::CodeItem* code_item, uint32_t core_spills, |
| 70 | uint32_t fp_spills, size_t frame_size, int vreg) const { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 71 | int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg); |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 72 | byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset; |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 73 | return *reinterpret_cast<uint32_t*>(vreg_addr); |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 76 | uint32_t Frame::GetVReg(Method* m, int vreg) const { |
| 77 | DCHECK(m == GetMethod()); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 78 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 79 | DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions? |
| 80 | uint32_t core_spills = m->GetCoreSpillMask(); |
| 81 | uint32_t fp_spills = m->GetFpSpillMask(); |
| 82 | size_t frame_size = m->GetFrameSizeInBytes(); |
| 83 | return GetVReg(code_item, core_spills, fp_spills, frame_size, vreg); |
| 84 | } |
| 85 | |
| 86 | void Frame::SetVReg(Method* m, int vreg, uint32_t new_value) { |
| 87 | DCHECK(m == GetMethod()); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 88 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 89 | DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions? |
| 90 | uint32_t core_spills = m->GetCoreSpillMask(); |
| 91 | uint32_t fp_spills = m->GetFpSpillMask(); |
| 92 | size_t frame_size = m->GetFrameSizeInBytes(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 93 | int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg); |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 94 | byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset; |
| 95 | *reinterpret_cast<uint32_t*>(vreg_addr) = new_value; |
| 96 | } |
| 97 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 98 | uintptr_t Frame::LoadCalleeSave(int num) const { |
| 99 | // Callee saves are held at the top of the frame |
| 100 | Method* method = GetMethod(); |
| 101 | DCHECK(method != NULL); |
| 102 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 103 | byte* save_addr = reinterpret_cast<byte*>(sp_) + frame_size - ((num + 1) * kPointerSize); |
| 104 | #if defined(__i386__) |
| 105 | save_addr -= kPointerSize; // account for return address |
| 106 | #endif |
| 107 | return *reinterpret_cast<uintptr_t*>(save_addr); |
| 108 | } |
| 109 | |
| 110 | Method* Frame::NextMethod() const { |
| 111 | byte* next_sp = reinterpret_cast<byte*>(sp_) + GetMethod()->GetFrameSizeInBytes(); |
| 112 | return *reinterpret_cast<Method**>(next_sp); |
| 113 | } |
| 114 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 115 | class StackGetter { |
| 116 | public: |
| 117 | StackGetter(JNIEnv* env, Thread* thread) : env_(env), thread_(thread), trace_(NULL) { |
| 118 | } |
| 119 | |
| 120 | static void Callback(void* arg) { |
| 121 | reinterpret_cast<StackGetter*>(arg)->Callback(); |
| 122 | } |
| 123 | |
| 124 | jobject GetTrace() { |
| 125 | return trace_; |
| 126 | } |
| 127 | |
| 128 | private: |
| 129 | void Callback() { |
| 130 | trace_ = thread_->CreateInternalStackTrace(env_); |
| 131 | } |
| 132 | |
| 133 | JNIEnv* env_; |
| 134 | Thread* thread_; |
| 135 | jobject trace_; |
| 136 | }; |
| 137 | |
| 138 | jobject GetThreadStack(JNIEnv* env, Thread* thread) { |
| 139 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 140 | StackGetter stack_getter(env, thread); |
| 141 | thread_list->RunWhileSuspended(thread, StackGetter::Callback, &stack_getter); |
| 142 | return stack_getter.GetTrace(); |
| 143 | } |
| 144 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 145 | } // namespace art |