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