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