blob: 85d328cb71471fce93937ae308a3bc5a3ecba8bf [file] [log] [blame]
jeffhaoe343b762011-12-05 16:36:44 -08001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_TRACE_H_
4#define ART_SRC_TRACE_H_
5
6#include <map>
jeffhaoa9ef3fd2011-12-13 18:33:43 -08007#include <ostream>
8#include <set>
9#include <string>
jeffhaoe343b762011-12-05 16:36:44 -080010
jeffhaoa9ef3fd2011-12-13 18:33:43 -080011#include "file.h"
jeffhaoe343b762011-12-05 16:36:44 -080012#include "globals.h"
13#include "macros.h"
jeffhao2692b572011-12-16 15:42:28 -080014#include "UniquePtr.h"
jeffhaoe343b762011-12-05 16:36:44 -080015
16namespace art {
17
18class Method;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080019class Thread;
jeffhaoe343b762011-12-05 16:36:44 -080020
21struct 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
30class Trace {
31 public:
jeffhaoa9ef3fd2011-12-13 18:33:43 -080032
33 enum TraceEvent {
34 kMethodTraceEnter = 0,
35 kMethodTraceExit = 1,
36 kMethodTraceUnwind = 2,
37 };
38
jeffhaoe343b762011-12-05 16:36:44 -080039 static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms);
40 static void Stop();
41
jeffhao2692b572011-12-16 15:42:28 -080042 void LogMethodTraceEvent(Thread* self, const Method* method, TraceEvent event);
jeffhaoa9ef3fd2011-12-13 18:33:43 -080043
jeffhao2692b572011-12-16 15:42:28 -080044 void AddSavedCodeToMap(const Method* method, const void* code);
45 void RemoveSavedCodeFromMap(const Method* method);
46 const void* GetSavedCodeFromMap(const Method* method);
jeffhaoe343b762011-12-05 16:36:44 -080047
jeffhao2692b572011-12-16 15:42:28 -080048 void SaveAndUpdateCode(Method* method, const void* new_code);
49 void ResetSavedCode(Method* method);
jeffhaoe343b762011-12-05 16:36:44 -080050
51 private:
jeffhao2692b572011-12-16 15:42:28 -080052 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
jeffhaoe343b762011-12-05 16:36:44 -080060 // Replaces code of each method with a pointer to a stub for method tracing.
jeffhao2692b572011-12-16 15:42:28 -080061 void InstallStubs();
jeffhaoe343b762011-12-05 16:36:44 -080062
63 // Restores original code for each method and fixes the return values of each thread's stack.
jeffhao2692b572011-12-16 15:42:28 -080064 void UninstallStubs();
jeffhaoe343b762011-12-05 16:36:44 -080065
jeffhaoa9ef3fd2011-12-13 18:33:43 -080066 // Methods to output traced methods and threads.
jeffhao2692b572011-12-16 15:42:28 -080067 void GetVisitedMethods(size_t end_offset);
68 void DumpMethodList(std::ostream& os);
69 void DumpThreadList(std::ostream& os);
jeffhaoa9ef3fd2011-12-13 18:33:43 -080070
jeffhao2692b572011-12-16 15:42:28 -080071 // Maps a method to its original code pointer.
72 std::map<const Method*, const void*> saved_code_map_;
jeffhaoe343b762011-12-05 16:36:44 -080073
jeffhao2692b572011-12-16 15:42:28 -080074 // Set of methods visited by the profiler.
75 std::set<const Method*> visited_methods_;
jeffhaoe343b762011-12-05 16:36:44 -080076
jeffhao2692b572011-12-16 15:42:28 -080077 // Maps a thread to its clock base.
78 std::map<Thread*, uint64_t> thread_clock_base_map_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080079
jeffhao2692b572011-12-16 15:42:28 -080080 // File to write trace data out to, NULL if direct to ddms.
81 UniquePtr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080082
jeffhao2692b572011-12-16 15:42:28 -080083 // 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_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080093
jeffhaoe343b762011-12-05 16:36:44 -080094 DISALLOW_COPY_AND_ASSIGN(Trace);
95};
96
97} // namespace art
98
99#endif // ART_SRC_TRACE_H_