jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_TRACE_H_ |
| 4 | #define ART_SRC_TRACE_H_ |
| 5 | |
| 6 | #include <map> |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame^] | 7 | #include <ostream> |
| 8 | #include <set> |
| 9 | #include <string> |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 10 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame^] | 11 | #include "file.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 12 | #include "globals.h" |
| 13 | #include "macros.h" |
| 14 | |
| 15 | namespace art { |
| 16 | |
| 17 | class Method; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame^] | 18 | class Thread; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 19 | |
| 20 | struct TraceStackFrame { |
| 21 | TraceStackFrame(Method* method, uintptr_t return_pc) |
| 22 | : method_(method), return_pc_(return_pc) { |
| 23 | } |
| 24 | |
| 25 | Method* method_; |
| 26 | uintptr_t return_pc_; |
| 27 | }; |
| 28 | |
| 29 | class Trace { |
| 30 | public: |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame^] | 31 | |
| 32 | enum TraceEvent { |
| 33 | kMethodTraceEnter = 0, |
| 34 | kMethodTraceExit = 1, |
| 35 | kMethodTraceUnwind = 2, |
| 36 | }; |
| 37 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 38 | static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms); |
| 39 | static void Stop(); |
| 40 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame^] | 41 | static void LogMethodTraceEvent(Thread* self, const Method* method, TraceEvent event); |
| 42 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 43 | static bool IsMethodTracingActive(); |
| 44 | static void SetMethodTracingActive(bool value); |
| 45 | |
| 46 | static void AddSavedCodeToMap(const Method* method, const void* code); |
| 47 | static void RemoveSavedCodeFromMap(const Method* method); |
| 48 | static const void* GetSavedCodeFromMap(const Method* method); |
| 49 | |
| 50 | static void SaveAndUpdateCode(Method* method, const void* new_code); |
| 51 | static void ResetSavedCode(Method* method); |
| 52 | |
| 53 | private: |
| 54 | // Replaces code of each method with a pointer to a stub for method tracing. |
| 55 | static void InstallStubs(); |
| 56 | |
| 57 | // Restores original code for each method and fixes the return values of each thread's stack. |
| 58 | static void UninstallStubs(); |
| 59 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame^] | 60 | // Methods to output traced methods and threads. |
| 61 | static void GetVisitedMethods(size_t end_offset); |
| 62 | static void DumpMethodList(std::ostream& os); |
| 63 | static void DumpThreadList(std::ostream& os); |
| 64 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 65 | static bool method_tracing_active_; |
| 66 | |
| 67 | // Maps a method to its original code pointer |
| 68 | static std::map<const Method*, const void*> saved_code_map_; |
| 69 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame^] | 70 | // Set of methods visited by the profiler |
| 71 | static std::set<const Method*> visited_methods_; |
| 72 | |
| 73 | // Maps a thread to its clock base |
| 74 | static std::map<Thread*, uint64_t> thread_clock_base_map_; |
| 75 | |
| 76 | static uint8_t* buf_; |
| 77 | static File* trace_file_; |
| 78 | static bool direct_to_ddms_; |
| 79 | static int buffer_size_; |
| 80 | static uint64_t start_time_; |
| 81 | static bool overflow_; |
| 82 | static uint16_t trace_version_; |
| 83 | static uint16_t record_size_; |
| 84 | static volatile int32_t cur_offset_; |
| 85 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 86 | DISALLOW_COPY_AND_ASSIGN(Trace); |
| 87 | }; |
| 88 | |
| 89 | } // namespace art |
| 90 | |
| 91 | #endif // ART_SRC_TRACE_H_ |