blob: 8a2ac0a4583427e5930ffc35356b8c29ed1f1cc6 [file] [log] [blame]
jeffhao725a9572012-11-13 18:20:12 -08001/*
2 * Copyright (C) 2012 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
Ian Rogers306057f2012-11-26 12:45:53 -080017#include "base/logging.h"
jeffhao725a9572012-11-13 18:20:12 -080018#include "instrumentation.h"
19#include "runtime.h"
20#include "thread.h"
21#include "trace.h"
22
23namespace art {
24
25extern "C" const void* artInstrumentationMethodEntryFromCode(AbstractMethod* method, Thread* self,
Ian Rogers306057f2012-11-26 12:45:53 -080026 AbstractMethod** sp, uintptr_t lr)
27 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao6641ea12013-01-02 18:13:42 -080028 self->SetTopOfStack(sp, lr);
Ian Rogers306057f2012-11-26 12:45:53 -080029 self->VerifyStack();
jeffhao725a9572012-11-13 18:20:12 -080030 Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
Ian Rogers306057f2012-11-26 12:45:53 -080031 // +1 as frame id's start at 1, +1 as we haven't yet built this method's frame.
Ian Rogers306057f2012-11-26 12:45:53 -080032 size_t frame_id = StackVisitor::ComputeNumFrames(self->GetManagedStack(),
jeffhao6641ea12013-01-02 18:13:42 -080033 self->GetInstrumentationStack()) + 2;
Ian Rogers306057f2012-11-26 12:45:53 -080034 InstrumentationStackFrame instrumentation_frame(method, lr, frame_id);
jeffhao725a9572012-11-13 18:20:12 -080035 self->PushInstrumentationStackFrame(instrumentation_frame);
36
Ian Rogers306057f2012-11-26 12:45:53 -080037 Trace* trace = instrumentation->GetTrace();
38 if (trace != NULL) {
39 trace->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
40 }
jeffhao725a9572012-11-13 18:20:12 -080041
42 return instrumentation->GetSavedCodeFromMap(method);
43}
44
Ian Rogers306057f2012-11-26 12:45:53 -080045extern "C" uint64_t artInstrumentationMethodExitFromCode(Thread* self, AbstractMethod** sp)
46 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao6641ea12013-01-02 18:13:42 -080047 self->SetTopOfStack(sp, 0);
Ian Rogers306057f2012-11-26 12:45:53 -080048 self->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -080049 // +1 as frame id's start at 1, +1 as we want the called frame not the frame being returned into.
Ian Rogers306057f2012-11-26 12:45:53 -080050 size_t frame_id = StackVisitor::ComputeNumFrames(self->GetManagedStack(),
jeffhao6641ea12013-01-02 18:13:42 -080051 self->GetInstrumentationStack()) + 2;
Ian Rogers306057f2012-11-26 12:45:53 -080052 InstrumentationStackFrame instrumentation_frame;
53 instrumentation_frame = self->PopInstrumentationStackFrame();
Ian Rogers306057f2012-11-26 12:45:53 -080054 if (frame_id != instrumentation_frame.frame_id_) {
55 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found " << instrumentation_frame.frame_id_;
56 StackVisitor::DescribeStack(self->GetManagedStack(), self->GetInstrumentationStack());
57 }
Ian Rogers306057f2012-11-26 12:45:53 -080058 Runtime* runtime = Runtime::Current();
59 if (runtime->IsMethodTracingActive()) {
60 Trace* trace = runtime->GetInstrumentation()->GetTrace();
61 trace->LogMethodTraceEvent(self, instrumentation_frame.method_, Trace::kMethodTraceExit);
62 }
63 if (self->ReadFlag(kEnterInterpreter)) {
64 return static_cast<uint64_t>(GetDeoptimizationEntryPoint()) |
65 (static_cast<uint64_t>(instrumentation_frame.return_pc_) << 32);
66 } else {
67 return instrumentation_frame.return_pc_;
68 }
jeffhao725a9572012-11-13 18:20:12 -080069}
70
71} // namespace art