Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -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 | */ |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_TRACE_H_ |
| 18 | #define ART_SRC_TRACE_H_ |
| 19 | |
| 20 | #include <map> |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 21 | #include <ostream> |
| 22 | #include <set> |
| 23 | #include <string> |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 24 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 25 | #include "file.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 26 | #include "globals.h" |
| 27 | #include "macros.h" |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 28 | #include "UniquePtr.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | class Method; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 33 | class Thread; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 34 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 35 | uint32_t TraceMethodUnwindFromCode(Thread* self); |
| 36 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 37 | struct TraceStackFrame { |
| 38 | TraceStackFrame(Method* method, uintptr_t return_pc) |
| 39 | : method_(method), return_pc_(return_pc) { |
| 40 | } |
| 41 | |
| 42 | Method* method_; |
| 43 | uintptr_t return_pc_; |
| 44 | }; |
| 45 | |
| 46 | class Trace { |
| 47 | public: |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 48 | |
| 49 | enum TraceEvent { |
| 50 | kMethodTraceEnter = 0, |
| 51 | kMethodTraceExit = 1, |
| 52 | kMethodTraceUnwind = 2, |
| 53 | }; |
| 54 | |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame^] | 55 | enum TraceFlag { |
| 56 | kTraceCountAllocs = 1, |
| 57 | }; |
| 58 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 59 | static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms); |
| 60 | static void Stop(); |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 61 | static void Shutdown(); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 62 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 63 | void LogMethodTraceEvent(Thread* self, const Method* method, TraceEvent event); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 64 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 65 | void AddSavedCodeToMap(const Method* method, const void* code); |
| 66 | void RemoveSavedCodeFromMap(const Method* method); |
| 67 | const void* GetSavedCodeFromMap(const Method* method); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 68 | |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 69 | void SaveAndUpdateCode(Method* method); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 70 | void ResetSavedCode(Method* method); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 71 | |
| 72 | private: |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame^] | 73 | explicit Trace(File* trace_file, int buffer_size, int flags) |
| 74 | : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags), overflow_(false), |
| 75 | buffer_size_(buffer_size), start_time_(0), trace_version_(0), record_size_(0), cur_offset_(0) { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void BeginTracing(); |
| 79 | void FinishTracing(); |
| 80 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 81 | // Replaces code of each method with a pointer to a stub for method tracing. |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 82 | void InstallStubs(); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 83 | |
| 84 | // Restores original code for each method and fixes the return values of each thread's stack. |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 85 | void UninstallStubs(); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 86 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 87 | // Methods to output traced methods and threads. |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 88 | void GetVisitedMethods(size_t end_offset); |
| 89 | void DumpMethodList(std::ostream& os); |
| 90 | void DumpThreadList(std::ostream& os); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 91 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 92 | // Maps a method to its original code pointer. |
| 93 | std::map<const Method*, const void*> saved_code_map_; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 94 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 95 | // Set of methods visited by the profiler. |
| 96 | std::set<const Method*> visited_methods_; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 97 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 98 | // Maps a thread to its clock base. |
| 99 | std::map<Thread*, uint64_t> thread_clock_base_map_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 100 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 101 | // File to write trace data out to, NULL if direct to ddms. |
| 102 | UniquePtr<File> trace_file_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 103 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 104 | // Buffer to store trace data. |
| 105 | UniquePtr<uint8_t> buf_; |
| 106 | |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame^] | 107 | // Flags enabling extra tracing of things such as alloc counts. |
| 108 | int flags_; |
| 109 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 110 | bool overflow_; |
| 111 | int buffer_size_; |
| 112 | uint64_t start_time_; |
| 113 | uint16_t trace_version_; |
| 114 | uint16_t record_size_; |
| 115 | |
| 116 | volatile int32_t cur_offset_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 117 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 118 | DISALLOW_COPY_AND_ASSIGN(Trace); |
| 119 | }; |
| 120 | |
| 121 | } // namespace art |
| 122 | |
| 123 | #endif // ART_SRC_TRACE_H_ |