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" |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame^] | 21 | #include "thread_list.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 22 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 23 | int oatVRegOffsetFromMethod(art::Method* method, int reg); |
| 24 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 25 | namespace art { |
| 26 | |
| 27 | bool Frame::HasMethod() const { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 28 | return GetMethod() != NULL && (!GetMethod()->IsCalleeSaveMethod()); |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void Frame::Next() { |
| 32 | size_t frame_size = GetMethod()->GetFrameSizeInBytes(); |
| 33 | DCHECK_NE(frame_size, 0u); |
| 34 | DCHECK_LT(frame_size, 1024u); |
| 35 | byte* next_sp = reinterpret_cast<byte*>(sp_) + frame_size; |
| 36 | sp_ = reinterpret_cast<Method**>(next_sp); |
| 37 | if (*sp_ != NULL) { |
| 38 | DCHECK((*sp_)->GetClass() == Method::GetMethodClass() || |
| 39 | (*sp_)->GetClass() == Method::GetConstructorClass()); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | uintptr_t Frame::GetReturnPC() const { |
| 44 | byte* pc_addr = reinterpret_cast<byte*>(sp_) + GetMethod()->GetReturnPcOffsetInBytes(); |
| 45 | return *reinterpret_cast<uintptr_t*>(pc_addr); |
| 46 | } |
| 47 | |
| 48 | uintptr_t Frame::GetVReg(Method* method, int vreg) const { |
| 49 | DCHECK(method == GetMethod()); |
| 50 | int offset = oatVRegOffsetFromMethod(method, vreg); |
| 51 | byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset; |
| 52 | return *reinterpret_cast<uintptr_t*>(vreg_addr); |
| 53 | } |
| 54 | |
| 55 | uintptr_t Frame::LoadCalleeSave(int num) const { |
| 56 | // Callee saves are held at the top of the frame |
| 57 | Method* method = GetMethod(); |
| 58 | DCHECK(method != NULL); |
| 59 | size_t frame_size = method->GetFrameSizeInBytes(); |
| 60 | byte* save_addr = reinterpret_cast<byte*>(sp_) + frame_size - ((num + 1) * kPointerSize); |
| 61 | #if defined(__i386__) |
| 62 | save_addr -= kPointerSize; // account for return address |
| 63 | #endif |
| 64 | return *reinterpret_cast<uintptr_t*>(save_addr); |
| 65 | } |
| 66 | |
| 67 | Method* Frame::NextMethod() const { |
| 68 | byte* next_sp = reinterpret_cast<byte*>(sp_) + GetMethod()->GetFrameSizeInBytes(); |
| 69 | return *reinterpret_cast<Method**>(next_sp); |
| 70 | } |
| 71 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame^] | 72 | class StackGetter { |
| 73 | public: |
| 74 | StackGetter(JNIEnv* env, Thread* thread) : env_(env), thread_(thread), trace_(NULL) { |
| 75 | } |
| 76 | |
| 77 | static void Callback(void* arg) { |
| 78 | reinterpret_cast<StackGetter*>(arg)->Callback(); |
| 79 | } |
| 80 | |
| 81 | jobject GetTrace() { |
| 82 | return trace_; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | void Callback() { |
| 87 | trace_ = thread_->CreateInternalStackTrace(env_); |
| 88 | } |
| 89 | |
| 90 | JNIEnv* env_; |
| 91 | Thread* thread_; |
| 92 | jobject trace_; |
| 93 | }; |
| 94 | |
| 95 | jobject GetThreadStack(JNIEnv* env, Thread* thread) { |
| 96 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 97 | StackGetter stack_getter(env, thread); |
| 98 | thread_list->RunWhileSuspended(thread, StackGetter::Callback, &stack_getter); |
| 99 | return stack_getter.GetTrace(); |
| 100 | } |
| 101 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 102 | } // namespace art |