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