blob: 542ac47734aeed82dea50ffecd752fdc1c8b4b7f [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"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070021#include "thread_list.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070022
Brian Carlstrom3320cf42011-10-04 14:58:28 -070023int oatVRegOffsetFromMethod(art::Method* method, int reg);
24
Elliott Hughes68e76522011-10-05 13:22:16 -070025namespace art {
26
27bool Frame::HasMethod() const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070028 return GetMethod() != NULL && (!GetMethod()->IsCalleeSaveMethod());
Elliott Hughes68e76522011-10-05 13:22:16 -070029}
30
31void 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
43uintptr_t Frame::GetReturnPC() const {
44 byte* pc_addr = reinterpret_cast<byte*>(sp_) + GetMethod()->GetReturnPcOffsetInBytes();
45 return *reinterpret_cast<uintptr_t*>(pc_addr);
46}
47
Elliott Hughes1bba14f2011-12-01 18:00:36 -080048uint32_t Frame::GetVReg(Method* method, int vreg) const {
Elliott Hughes68e76522011-10-05 13:22:16 -070049 DCHECK(method == GetMethod());
50 int offset = oatVRegOffsetFromMethod(method, vreg);
51 byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset;
Elliott Hughes1bba14f2011-12-01 18:00:36 -080052 return *reinterpret_cast<uint32_t*>(vreg_addr);
Elliott Hughes68e76522011-10-05 13:22:16 -070053}
54
Elliott Hughescccd84f2011-12-05 16:51:54 -080055void Frame::SetVReg(Method* method, int vreg, uint32_t new_value) {
56 DCHECK(method == GetMethod());
57 int offset = oatVRegOffsetFromMethod(method, vreg);
58 byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset;
59 *reinterpret_cast<uint32_t*>(vreg_addr) = new_value;
60}
61
Elliott Hughes68e76522011-10-05 13:22:16 -070062uintptr_t Frame::LoadCalleeSave(int num) const {
63 // Callee saves are held at the top of the frame
64 Method* method = GetMethod();
65 DCHECK(method != NULL);
66 size_t frame_size = method->GetFrameSizeInBytes();
67 byte* save_addr = reinterpret_cast<byte*>(sp_) + frame_size - ((num + 1) * kPointerSize);
68#if defined(__i386__)
69 save_addr -= kPointerSize; // account for return address
70#endif
71 return *reinterpret_cast<uintptr_t*>(save_addr);
72}
73
74Method* Frame::NextMethod() const {
75 byte* next_sp = reinterpret_cast<byte*>(sp_) + GetMethod()->GetFrameSizeInBytes();
76 return *reinterpret_cast<Method**>(next_sp);
77}
78
Elliott Hughesbfe487b2011-10-26 15:48:55 -070079class StackGetter {
80 public:
81 StackGetter(JNIEnv* env, Thread* thread) : env_(env), thread_(thread), trace_(NULL) {
82 }
83
84 static void Callback(void* arg) {
85 reinterpret_cast<StackGetter*>(arg)->Callback();
86 }
87
88 jobject GetTrace() {
89 return trace_;
90 }
91
92 private:
93 void Callback() {
94 trace_ = thread_->CreateInternalStackTrace(env_);
95 }
96
97 JNIEnv* env_;
98 Thread* thread_;
99 jobject trace_;
100};
101
102jobject GetThreadStack(JNIEnv* env, Thread* thread) {
103 ThreadList* thread_list = Runtime::Current()->GetThreadList();
104 StackGetter stack_getter(env, thread);
105 thread_list->RunWhileSuspended(thread, StackGetter::Callback, &stack_getter);
106 return stack_getter.GetTrace();
107}
108
Elliott Hughes68e76522011-10-05 13:22:16 -0700109} // namespace art