blob: 00060ce1685aa5a3e17a2158d23627a23fae0278 [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
20#include <ostream>
21#include <set>
22#include <string>
23
Elliott Hughes76160052012-12-12 16:31:20 -080024#include "base/macros.h"
jeffhao725a9572012-11-13 18:20:12 -080025#include "globals.h"
jeffhao725a9572012-11-13 18:20:12 -080026#include "safe_map.h"
27#include "trace.h"
28#include "UniquePtr.h"
29
30namespace art {
31
32class AbstractMethod;
33class Thread;
34
35uint32_t InstrumentationMethodUnwindFromCode(Thread* self);
36
37struct InstrumentationStackFrame {
Ian Rogers306057f2012-11-26 12:45:53 -080038 InstrumentationStackFrame() : method_(NULL), return_pc_(0), frame_id_(0) {}
39 InstrumentationStackFrame(AbstractMethod* method, uintptr_t return_pc, size_t frame_id)
40 : method_(method), return_pc_(return_pc), frame_id_(frame_id) {
jeffhao725a9572012-11-13 18:20:12 -080041 }
jeffhao725a9572012-11-13 18:20:12 -080042 AbstractMethod* method_;
43 uintptr_t return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -080044 size_t frame_id_;
jeffhao725a9572012-11-13 18:20:12 -080045};
46
47class Instrumentation {
48 public:
49 Instrumentation() {}
50 ~Instrumentation();
51
52 // Replaces code of each method with a pointer to a stub for method tracing.
Ian Rogers306057f2012-11-26 12:45:53 -080053 void InstallStubs() LOCKS_EXCLUDED(Locks::thread_list_lock_);
jeffhao725a9572012-11-13 18:20:12 -080054
55 // Restores original code for each method and fixes the return values of each thread's stack.
56 void UninstallStubs() LOCKS_EXCLUDED(Locks::thread_list_lock_);
57
58 const void* GetSavedCodeFromMap(const AbstractMethod* method);
59 void SaveAndUpdateCode(AbstractMethod* method);
60 void ResetSavedCode(AbstractMethod* method);
61
62 Trace* GetTrace() const;
63 void SetTrace(Trace* trace);
64 void RemoveTrace();
65
66 private:
67 void AddSavedCodeToMap(const AbstractMethod* method, const void* code);
68 void RemoveSavedCodeFromMap(const AbstractMethod* method);
69
70 // Maps a method to its original code pointer.
71 SafeMap<const AbstractMethod*, const void*> saved_code_map_;
72
73 Trace* trace_;
74
75 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
76};
77
78} // namespace art
79
80#endif // ART_SRC_INSTRUMENTATION_H_