blob: 9637163cfb23233a1c624b8234f0682b093b776d [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"
14
15namespace art {
16
17class Method;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080018class Thread;
jeffhaoe343b762011-12-05 16:36:44 -080019
20struct 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
29class Trace {
30 public:
jeffhaoa9ef3fd2011-12-13 18:33:43 -080031
32 enum TraceEvent {
33 kMethodTraceEnter = 0,
34 kMethodTraceExit = 1,
35 kMethodTraceUnwind = 2,
36 };
37
jeffhaoe343b762011-12-05 16:36:44 -080038 static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms);
39 static void Stop();
40
jeffhaoa9ef3fd2011-12-13 18:33:43 -080041 static void LogMethodTraceEvent(Thread* self, const Method* method, TraceEvent event);
42
jeffhaoe343b762011-12-05 16:36:44 -080043 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
jeffhaoa9ef3fd2011-12-13 18:33:43 -080060 // 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
jeffhaoe343b762011-12-05 16:36:44 -080065 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
jeffhaoa9ef3fd2011-12-13 18:33:43 -080070 // 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
jeffhaoe343b762011-12-05 16:36:44 -080086 DISALLOW_COPY_AND_ASSIGN(Trace);
87};
88
89} // namespace art
90
91#endif // ART_SRC_TRACE_H_