blob: fb49bf820888797ec32cff003cd99706763a412a [file] [log] [blame]
jeffhao725a9572012-11-13 18:20:12 -08001/*
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#ifndef ART_SRC_INSTRUMENTATION_H_
18#define ART_SRC_INSTRUMENTATION_H_
19
Elliott Hughes76160052012-12-12 16:31:20 -080020#include "base/macros.h"
jeffhao725a9572012-11-13 18:20:12 -080021#include "safe_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022
23#include <stdint.h>
jeffhao725a9572012-11-13 18:20:12 -080024
25namespace art {
26
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027namespace mirror {
jeffhao725a9572012-11-13 18:20:12 -080028class AbstractMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029}
jeffhao725a9572012-11-13 18:20:12 -080030class Thread;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031class Trace;
jeffhao725a9572012-11-13 18:20:12 -080032
33uint32_t InstrumentationMethodUnwindFromCode(Thread* self);
34
35struct InstrumentationStackFrame {
Ian Rogers306057f2012-11-26 12:45:53 -080036 InstrumentationStackFrame() : method_(NULL), return_pc_(0), frame_id_(0) {}
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037 InstrumentationStackFrame(mirror::AbstractMethod* method, uintptr_t return_pc, size_t frame_id)
Ian Rogers306057f2012-11-26 12:45:53 -080038 : method_(method), return_pc_(return_pc), frame_id_(frame_id) {
jeffhao725a9572012-11-13 18:20:12 -080039 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040 mirror::AbstractMethod* method_;
jeffhao725a9572012-11-13 18:20:12 -080041 uintptr_t return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -080042 size_t frame_id_;
jeffhao725a9572012-11-13 18:20:12 -080043};
44
45class Instrumentation {
46 public:
47 Instrumentation() {}
48 ~Instrumentation();
49
50 // Replaces code of each method with a pointer to a stub for method tracing.
Ian Rogers306057f2012-11-26 12:45:53 -080051 void InstallStubs() LOCKS_EXCLUDED(Locks::thread_list_lock_);
jeffhao725a9572012-11-13 18:20:12 -080052
53 // Restores original code for each method and fixes the return values of each thread's stack.
54 void UninstallStubs() LOCKS_EXCLUDED(Locks::thread_list_lock_);
55
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056 const void* GetSavedCodeFromMap(const mirror::AbstractMethod* method);
57 void SaveAndUpdateCode(mirror::AbstractMethod* method);
58 void ResetSavedCode(mirror::AbstractMethod* method);
jeffhao725a9572012-11-13 18:20:12 -080059
60 Trace* GetTrace() const;
61 void SetTrace(Trace* trace);
62 void RemoveTrace();
63
64 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065 void AddSavedCodeToMap(const mirror::AbstractMethod* method, const void* code);
66 void RemoveSavedCodeFromMap(const mirror::AbstractMethod* method);
jeffhao725a9572012-11-13 18:20:12 -080067
68 // Maps a method to its original code pointer.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 SafeMap<const mirror::AbstractMethod*, const void*> saved_code_map_;
jeffhao725a9572012-11-13 18:20:12 -080070
71 Trace* trace_;
72
73 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
74};
75
76} // namespace art
77
78#endif // ART_SRC_INSTRUMENTATION_H_