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() { |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 31 | #if defined(ART_USE_LLVM_COMPILER) |
| 32 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 33 | #else |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 34 | size_t frame_size = GetMethod()->GetFrameSizeInBytes(); |
| 35 | DCHECK_NE(frame_size, 0u); |
| 36 | DCHECK_LT(frame_size, 1024u); |
| 37 | byte* next_sp = reinterpret_cast<byte*>(sp_) + frame_size; |
| 38 | sp_ = reinterpret_cast<Method**>(next_sp); |
| 39 | if (*sp_ != NULL) { |
| 40 | DCHECK((*sp_)->GetClass() == Method::GetMethodClass() || |
| 41 | (*sp_)->GetClass() == Method::GetConstructorClass()); |
| 42 | } |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 43 | #endif |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | uintptr_t Frame::GetReturnPC() const { |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 47 | #if defined(ART_USE_LLVM_COMPILER) |
| 48 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 49 | return 0; |
| 50 | #else |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 51 | byte* pc_addr = reinterpret_cast<byte*>(sp_) + GetMethod()->GetReturnPcOffsetInBytes(); |
| 52 | return *reinterpret_cast<uintptr_t*>(pc_addr); |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 53 | #endif |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 54 | } |
| 55 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 56 | void Frame::SetReturnPC(uintptr_t pc) { |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 57 | #if defined(ART_USE_LLVM_COMPILER) |
| 58 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 59 | #else |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 60 | byte* pc_addr = reinterpret_cast<byte*>(sp_) + GetMethod()->GetReturnPcOffsetInBytes(); |
| 61 | *reinterpret_cast<uintptr_t*>(pc_addr) = pc; |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 62 | #endif |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 63 | } |
| 64 | |
buzbee | efccc56 | 2012-03-11 11:19:28 -0700 | [diff] [blame] | 65 | /* |
| 66 | * Return sp-relative offset for a Dalvik virtual register, compiler |
| 67 | * spill or Method* in bytes using Method*. |
buzbee | 9c044ce | 2012-03-18 13:24:07 -0700 | [diff] [blame] | 68 | * Note that (reg >= 0) refers to a Dalvik register, (reg == -2) |
| 69 | * denotes Method* and (reg <= -3) denotes a compiler temp. |
buzbee | efccc56 | 2012-03-11 11:19:28 -0700 | [diff] [blame] | 70 | * |
| 71 | * +------------------------+ |
| 72 | * | IN[ins-1] | {Note: resides in caller's frame} |
| 73 | * | . | |
| 74 | * | IN[0] | |
| 75 | * | caller's Method* | |
| 76 | * +========================+ {Note: start of callee's frame} |
| 77 | * | core callee-save spill | {variable sized} |
| 78 | * +------------------------+ |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 79 | * | fp callee-save spill | |
buzbee | efccc56 | 2012-03-11 11:19:28 -0700 | [diff] [blame] | 80 | * +------------------------+ |
buzbee | 70c96d4 | 2012-03-15 15:27:56 -0700 | [diff] [blame] | 81 | * | filler word | {For compatibility, if V[locals-1] used as wide |
| 82 | * +------------------------+ |
buzbee | efccc56 | 2012-03-11 11:19:28 -0700 | [diff] [blame] | 83 | * | V[locals-1] | |
| 84 | * | V[locals-2] | |
| 85 | * | . | |
| 86 | * | . | ... (reg == 2) |
| 87 | * | V[1] | ... (reg == 1) |
| 88 | * | V[0] | ... (reg == 0) <---- "locals_start" |
| 89 | * +------------------------+ |
| 90 | * | Compiler temps | ... (reg == -2) |
| 91 | * | | ... (reg == -3) |
| 92 | * | | ... (reg == -4) |
| 93 | * +------------------------+ |
| 94 | * | stack alignment padding| {0 to (kStackAlignWords-1) of padding} |
| 95 | * +------------------------+ |
| 96 | * | OUT[outs-1] | |
| 97 | * | OUT[outs-2] | |
| 98 | * | . | |
| 99 | * | OUT[0] | |
| 100 | * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned |
| 101 | * +========================+ |
| 102 | */ |
| 103 | int Frame::GetVRegOffset(const DexFile::CodeItem* code_item, |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 104 | uint32_t core_spills, uint32_t fp_spills, |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 105 | size_t frame_size, int reg) |
| 106 | { |
| 107 | #if defined(ART_USE_LLVM_COMPILER) |
| 108 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 109 | return 0; |
| 110 | #else |
| 111 | DCHECK_EQ( frame_size & (kStackAlignment - 1), 0U); |
buzbee | 70c96d4 | 2012-03-15 15:27:56 -0700 | [diff] [blame] | 112 | int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1 /* filler */; |
buzbee | efccc56 | 2012-03-11 11:19:28 -0700 | [diff] [blame] | 113 | int num_ins = code_item->ins_size_; |
| 114 | int num_regs = code_item->registers_size_ - num_ins; |
| 115 | int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t)); |
buzbee | 9c044ce | 2012-03-18 13:24:07 -0700 | [diff] [blame] | 116 | if (reg == -2) { |
buzbee | efccc56 | 2012-03-11 11:19:28 -0700 | [diff] [blame] | 117 | return 0; // Method* |
buzbee | 9c044ce | 2012-03-18 13:24:07 -0700 | [diff] [blame] | 118 | } else if (reg <= -3) { |
buzbee | efccc56 | 2012-03-11 11:19:28 -0700 | [diff] [blame] | 119 | return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp |
| 120 | } else if (reg < num_regs) { |
| 121 | return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg |
| 122 | } else { |
| 123 | return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in |
| 124 | } |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 125 | #endif |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 128 | uint32_t Frame::GetVReg(const DexFile::CodeItem* code_item, uint32_t core_spills, |
| 129 | uint32_t fp_spills, size_t frame_size, int vreg) const { |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 130 | #if defined(ART_USE_LLVM_COMPILER) |
| 131 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 132 | return 0; |
| 133 | #else |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 134 | int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg); |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 135 | byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset; |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 136 | return *reinterpret_cast<uint32_t*>(vreg_addr); |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 137 | #endif |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 140 | uint32_t Frame::GetVReg(Method* m, int vreg) const { |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 141 | #if defined(ART_USE_LLVM_COMPILER) |
| 142 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 143 | return 0; |
| 144 | #else |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 145 | DCHECK(m == GetMethod()); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 146 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 147 | DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions? |
| 148 | uint32_t core_spills = m->GetCoreSpillMask(); |
| 149 | uint32_t fp_spills = m->GetFpSpillMask(); |
| 150 | size_t frame_size = m->GetFrameSizeInBytes(); |
| 151 | return GetVReg(code_item, core_spills, fp_spills, frame_size, vreg); |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 152 | #endif |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void Frame::SetVReg(Method* m, int vreg, uint32_t new_value) { |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 156 | #if defined(ART_USE_LLVM_COMPILER) |
| 157 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 158 | #else |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 159 | DCHECK(m == GetMethod()); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 160 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 161 | DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions? |
| 162 | uint32_t core_spills = m->GetCoreSpillMask(); |
| 163 | uint32_t fp_spills = m->GetFpSpillMask(); |
| 164 | size_t frame_size = m->GetFrameSizeInBytes(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 165 | int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg); |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 166 | byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset; |
| 167 | *reinterpret_cast<uint32_t*>(vreg_addr) = new_value; |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 168 | #endif |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 171 | uintptr_t Frame::LoadCalleeSave(int num) const { |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 172 | #if defined(ART_USE_LLVM_COMPILER) |
| 173 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 174 | return 0; |
| 175 | #else |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 176 | // Callee saves are held at the top of the frame |
| 177 | Method* method = GetMethod(); |
| 178 | DCHECK(method != NULL); |
| 179 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 180 | byte* save_addr = reinterpret_cast<byte*>(sp_) + frame_size - ((num + 1) * kPointerSize); |
| 181 | #if defined(__i386__) |
| 182 | save_addr -= kPointerSize; // account for return address |
| 183 | #endif |
| 184 | return *reinterpret_cast<uintptr_t*>(save_addr); |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 185 | #endif |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | Method* Frame::NextMethod() const { |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 189 | #if defined(ART_USE_LLVM_COMPILER) |
| 190 | LOG(FATAL) << "LLVM compiler don't support this function"; |
| 191 | return NULL; |
| 192 | #else |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 193 | byte* next_sp = reinterpret_cast<byte*>(sp_) + GetMethod()->GetFrameSizeInBytes(); |
| 194 | return *reinterpret_cast<Method**>(next_sp); |
TDYa127 | 9b2ba2e | 2012-03-19 10:12:32 -0700 | [diff] [blame] | 195 | #endif |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 198 | class StackGetter { |
| 199 | public: |
| 200 | StackGetter(JNIEnv* env, Thread* thread) : env_(env), thread_(thread), trace_(NULL) { |
| 201 | } |
| 202 | |
| 203 | static void Callback(void* arg) { |
| 204 | reinterpret_cast<StackGetter*>(arg)->Callback(); |
| 205 | } |
| 206 | |
| 207 | jobject GetTrace() { |
| 208 | return trace_; |
| 209 | } |
| 210 | |
| 211 | private: |
| 212 | void Callback() { |
| 213 | trace_ = thread_->CreateInternalStackTrace(env_); |
| 214 | } |
| 215 | |
| 216 | JNIEnv* env_; |
| 217 | Thread* thread_; |
| 218 | jobject trace_; |
| 219 | }; |
| 220 | |
| 221 | jobject GetThreadStack(JNIEnv* env, Thread* thread) { |
| 222 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 223 | StackGetter stack_getter(env, thread); |
| 224 | thread_list->RunWhileSuspended(thread, StackGetter::Callback, &stack_getter); |
| 225 | return stack_getter.GetTrace(); |
| 226 | } |
| 227 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 228 | } // namespace art |