jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame^] | 24 | #include "base/macros.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 25 | #include "globals.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 26 | #include "safe_map.h" |
| 27 | #include "trace.h" |
| 28 | #include "UniquePtr.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | class AbstractMethod; |
| 33 | class Thread; |
| 34 | |
| 35 | uint32_t InstrumentationMethodUnwindFromCode(Thread* self); |
| 36 | |
| 37 | struct InstrumentationStackFrame { |
| 38 | InstrumentationStackFrame(AbstractMethod* method, uintptr_t return_pc) |
| 39 | : method_(method), return_pc_(return_pc) { |
| 40 | } |
| 41 | |
| 42 | AbstractMethod* method_; |
| 43 | uintptr_t return_pc_; |
| 44 | }; |
| 45 | |
| 46 | class Instrumentation { |
| 47 | public: |
| 48 | Instrumentation() {} |
| 49 | ~Instrumentation(); |
| 50 | |
| 51 | // Replaces code of each method with a pointer to a stub for method tracing. |
| 52 | void InstallStubs(); |
| 53 | |
| 54 | // Restores original code for each method and fixes the return values of each thread's stack. |
| 55 | void UninstallStubs() LOCKS_EXCLUDED(Locks::thread_list_lock_); |
| 56 | |
| 57 | const void* GetSavedCodeFromMap(const AbstractMethod* method); |
| 58 | void SaveAndUpdateCode(AbstractMethod* method); |
| 59 | void ResetSavedCode(AbstractMethod* method); |
| 60 | |
| 61 | Trace* GetTrace() const; |
| 62 | void SetTrace(Trace* trace); |
| 63 | void RemoveTrace(); |
| 64 | |
| 65 | private: |
| 66 | void AddSavedCodeToMap(const AbstractMethod* method, const void* code); |
| 67 | void RemoveSavedCodeFromMap(const AbstractMethod* method); |
| 68 | |
| 69 | // Maps a method to its original code pointer. |
| 70 | SafeMap<const AbstractMethod*, const void*> saved_code_map_; |
| 71 | |
| 72 | Trace* trace_; |
| 73 | |
| 74 | DISALLOW_COPY_AND_ASSIGN(Instrumentation); |
| 75 | }; |
| 76 | |
| 77 | } // namespace art |
| 78 | |
| 79 | #endif // ART_SRC_INSTRUMENTATION_H_ |