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" |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame^] | 14 | #include "UniquePtr.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 15 | |
| 16 | namespace art { |
| 17 | |
| 18 | class Method; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 19 | class Thread; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 20 | |
| 21 | struct TraceStackFrame { |
| 22 | TraceStackFrame(Method* method, uintptr_t return_pc) |
| 23 | : method_(method), return_pc_(return_pc) { |
| 24 | } |
| 25 | |
| 26 | Method* method_; |
| 27 | uintptr_t return_pc_; |
| 28 | }; |
| 29 | |
| 30 | class Trace { |
| 31 | public: |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 32 | |
| 33 | enum TraceEvent { |
| 34 | kMethodTraceEnter = 0, |
| 35 | kMethodTraceExit = 1, |
| 36 | kMethodTraceUnwind = 2, |
| 37 | }; |
| 38 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 39 | static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms); |
| 40 | static void Stop(); |
| 41 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame^] | 42 | void LogMethodTraceEvent(Thread* self, const Method* method, TraceEvent event); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 43 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame^] | 44 | void AddSavedCodeToMap(const Method* method, const void* code); |
| 45 | void RemoveSavedCodeFromMap(const Method* method); |
| 46 | const void* GetSavedCodeFromMap(const Method* method); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 47 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame^] | 48 | void SaveAndUpdateCode(Method* method, const void* new_code); |
| 49 | void ResetSavedCode(Method* method); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 50 | |
| 51 | private: |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame^] | 52 | explicit Trace(File* trace_file, int buffer_size) |
| 53 | : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), overflow_(false), buffer_size_(buffer_size), |
| 54 | start_time_(0), trace_version_(0), record_size_(0), cur_offset_(0) { |
| 55 | } |
| 56 | |
| 57 | void BeginTracing(); |
| 58 | void FinishTracing(); |
| 59 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 60 | // 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^] | 61 | void InstallStubs(); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 62 | |
| 63 | // 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^] | 64 | void UninstallStubs(); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 65 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 66 | // Methods to output traced methods and threads. |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame^] | 67 | void GetVisitedMethods(size_t end_offset); |
| 68 | void DumpMethodList(std::ostream& os); |
| 69 | void DumpThreadList(std::ostream& os); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 70 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame^] | 71 | // Maps a method to its original code pointer. |
| 72 | std::map<const Method*, const void*> saved_code_map_; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 73 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame^] | 74 | // Set of methods visited by the profiler. |
| 75 | std::set<const Method*> visited_methods_; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 76 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame^] | 77 | // Maps a thread to its clock base. |
| 78 | std::map<Thread*, uint64_t> thread_clock_base_map_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 79 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame^] | 80 | // File to write trace data out to, NULL if direct to ddms. |
| 81 | UniquePtr<File> trace_file_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 82 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame^] | 83 | // Buffer to store trace data. |
| 84 | UniquePtr<uint8_t> buf_; |
| 85 | |
| 86 | bool overflow_; |
| 87 | int buffer_size_; |
| 88 | uint64_t start_time_; |
| 89 | uint16_t trace_version_; |
| 90 | uint16_t record_size_; |
| 91 | |
| 92 | volatile int32_t cur_offset_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 93 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 94 | DISALLOW_COPY_AND_ASSIGN(Trace); |
| 95 | }; |
| 96 | |
| 97 | } // namespace art |
| 98 | |
| 99 | #endif // ART_SRC_TRACE_H_ |