blob: 9944ec3d7927bc68a04dfc7818bf5c8fcec2837b [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#include "stack.h"
18
19#include "compiler.h"
20#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include "object_utils.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070022#include "thread_list.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070023
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080024int oatVRegOffset(const art::DexFile::CodeItem* code_item,
25 uint32_t core_spills, uint32_t fp_spills,
26 size_t frame_size, int reg);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070027
Elliott Hughes68e76522011-10-05 13:22:16 -070028namespace art {
29
30bool Frame::HasMethod() const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070031 return GetMethod() != NULL && (!GetMethod()->IsCalleeSaveMethod());
Elliott Hughes68e76522011-10-05 13:22:16 -070032}
33
34void 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
46uintptr_t Frame::GetReturnPC() const {
47 byte* pc_addr = reinterpret_cast<byte*>(sp_) + GetMethod()->GetReturnPcOffsetInBytes();
48 return *reinterpret_cast<uintptr_t*>(pc_addr);
49}
50
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080051uint32_t Frame::GetVReg(const DexFile::CodeItem* code_item, uint32_t core_spills,
52 uint32_t fp_spills, size_t frame_size, int vreg) const {
53 int offset = oatVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Elliott Hughes68e76522011-10-05 13:22:16 -070054 byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset;
Elliott Hughes1bba14f2011-12-01 18:00:36 -080055 return *reinterpret_cast<uint32_t*>(vreg_addr);
Elliott Hughes68e76522011-10-05 13:22:16 -070056}
57
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080058uint32_t Frame::GetVReg(Method* m, int vreg) const {
59 DCHECK(m == GetMethod());
60 const art::DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
61 DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions?
62 uint32_t core_spills = m->GetCoreSpillMask();
63 uint32_t fp_spills = m->GetFpSpillMask();
64 size_t frame_size = m->GetFrameSizeInBytes();
65 return GetVReg(code_item, core_spills, fp_spills, frame_size, vreg);
66}
67
68void Frame::SetVReg(Method* m, int vreg, uint32_t new_value) {
69 DCHECK(m == GetMethod());
70 const art::DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
71 DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions?
72 uint32_t core_spills = m->GetCoreSpillMask();
73 uint32_t fp_spills = m->GetFpSpillMask();
74 size_t frame_size = m->GetFrameSizeInBytes();
75 int offset = oatVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Elliott Hughescccd84f2011-12-05 16:51:54 -080076 byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset;
77 *reinterpret_cast<uint32_t*>(vreg_addr) = new_value;
78}
79
Elliott Hughes68e76522011-10-05 13:22:16 -070080uintptr_t Frame::LoadCalleeSave(int num) const {
81 // Callee saves are held at the top of the frame
82 Method* method = GetMethod();
83 DCHECK(method != NULL);
84 size_t frame_size = method->GetFrameSizeInBytes();
85 byte* save_addr = reinterpret_cast<byte*>(sp_) + frame_size - ((num + 1) * kPointerSize);
86#if defined(__i386__)
87 save_addr -= kPointerSize; // account for return address
88#endif
89 return *reinterpret_cast<uintptr_t*>(save_addr);
90}
91
92Method* Frame::NextMethod() const {
93 byte* next_sp = reinterpret_cast<byte*>(sp_) + GetMethod()->GetFrameSizeInBytes();
94 return *reinterpret_cast<Method**>(next_sp);
95}
96
Elliott Hughesbfe487b2011-10-26 15:48:55 -070097class StackGetter {
98 public:
99 StackGetter(JNIEnv* env, Thread* thread) : env_(env), thread_(thread), trace_(NULL) {
100 }
101
102 static void Callback(void* arg) {
103 reinterpret_cast<StackGetter*>(arg)->Callback();
104 }
105
106 jobject GetTrace() {
107 return trace_;
108 }
109
110 private:
111 void Callback() {
112 trace_ = thread_->CreateInternalStackTrace(env_);
113 }
114
115 JNIEnv* env_;
116 Thread* thread_;
117 jobject trace_;
118};
119
120jobject GetThreadStack(JNIEnv* env, Thread* thread) {
121 ThreadList* thread_list = Runtime::Current()->GetThreadList();
122 StackGetter stack_getter(env, thread);
123 thread_list->RunWhileSuspended(thread, StackGetter::Callback, &stack_getter);
124 return stack_getter.GetTrace();
125}
126
Elliott Hughes68e76522011-10-05 13:22:16 -0700127} // namespace art