blob: 1e1f93259aa73f1dd6aff988ca9dc1c95dc22e76 [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
48uintptr_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
55uintptr_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
67Method* Frame::NextMethod() const {
68 byte* next_sp = reinterpret_cast<byte*>(sp_) + GetMethod()->GetFrameSizeInBytes();
69 return *reinterpret_cast<Method**>(next_sp);
70}
71
Elliott Hughesbfe487b2011-10-26 15:48:55 -070072class 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
95jobject 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 Hughes68e76522011-10-05 13:22:16 -0700102} // namespace art