blob: b0366d9459469a82566c75c7db20ce9083efa73a [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
jeffhaoe343b762011-12-05 16:36:44 -080016
17#ifndef ART_SRC_TRACE_H_
18#define ART_SRC_TRACE_H_
19
20#include <map>
jeffhaoa9ef3fd2011-12-13 18:33:43 -080021#include <ostream>
22#include <set>
23#include <string>
jeffhaoe343b762011-12-05 16:36:44 -080024
jeffhaoa9ef3fd2011-12-13 18:33:43 -080025#include "file.h"
jeffhaoe343b762011-12-05 16:36:44 -080026#include "globals.h"
27#include "macros.h"
jeffhao2692b572011-12-16 15:42:28 -080028#include "UniquePtr.h"
jeffhaoe343b762011-12-05 16:36:44 -080029
30namespace art {
31
32class Method;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080033class Thread;
jeffhaoe343b762011-12-05 16:36:44 -080034
Ian Rogers57b86d42012-03-27 16:05:41 -070035uint32_t TraceMethodUnwindFromCode(Thread* self);
36
jeffhaoe343b762011-12-05 16:36:44 -080037struct TraceStackFrame {
38 TraceStackFrame(Method* method, uintptr_t return_pc)
39 : method_(method), return_pc_(return_pc) {
40 }
41
42 Method* method_;
43 uintptr_t return_pc_;
44};
45
46class Trace {
47 public:
jeffhaoa9ef3fd2011-12-13 18:33:43 -080048
49 enum TraceEvent {
50 kMethodTraceEnter = 0,
51 kMethodTraceExit = 1,
52 kMethodTraceUnwind = 2,
53 };
54
jeffhaoe343b762011-12-05 16:36:44 -080055 static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms);
56 static void Stop();
jeffhaob5e81852012-03-12 11:15:45 -070057 static void Shutdown();
jeffhaoe343b762011-12-05 16:36:44 -080058
jeffhao2692b572011-12-16 15:42:28 -080059 void LogMethodTraceEvent(Thread* self, const Method* method, TraceEvent event);
jeffhaoa9ef3fd2011-12-13 18:33:43 -080060
jeffhao2692b572011-12-16 15:42:28 -080061 void AddSavedCodeToMap(const Method* method, const void* code);
62 void RemoveSavedCodeFromMap(const Method* method);
63 const void* GetSavedCodeFromMap(const Method* method);
jeffhaoe343b762011-12-05 16:36:44 -080064
jeffhaob5e81852012-03-12 11:15:45 -070065 void SaveAndUpdateCode(Method* method);
jeffhao2692b572011-12-16 15:42:28 -080066 void ResetSavedCode(Method* method);
jeffhaoe343b762011-12-05 16:36:44 -080067
68 private:
jeffhao2692b572011-12-16 15:42:28 -080069 explicit Trace(File* trace_file, int buffer_size)
70 : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), overflow_(false), buffer_size_(buffer_size),
71 start_time_(0), trace_version_(0), record_size_(0), cur_offset_(0) {
72 }
73
74 void BeginTracing();
75 void FinishTracing();
76
jeffhaoe343b762011-12-05 16:36:44 -080077 // Replaces code of each method with a pointer to a stub for method tracing.
jeffhao2692b572011-12-16 15:42:28 -080078 void InstallStubs();
jeffhaoe343b762011-12-05 16:36:44 -080079
80 // Restores original code for each method and fixes the return values of each thread's stack.
jeffhao2692b572011-12-16 15:42:28 -080081 void UninstallStubs();
jeffhaoe343b762011-12-05 16:36:44 -080082
jeffhaoa9ef3fd2011-12-13 18:33:43 -080083 // Methods to output traced methods and threads.
jeffhao2692b572011-12-16 15:42:28 -080084 void GetVisitedMethods(size_t end_offset);
85 void DumpMethodList(std::ostream& os);
86 void DumpThreadList(std::ostream& os);
jeffhaoa9ef3fd2011-12-13 18:33:43 -080087
jeffhao2692b572011-12-16 15:42:28 -080088 // Maps a method to its original code pointer.
89 std::map<const Method*, const void*> saved_code_map_;
jeffhaoe343b762011-12-05 16:36:44 -080090
jeffhao2692b572011-12-16 15:42:28 -080091 // Set of methods visited by the profiler.
92 std::set<const Method*> visited_methods_;
jeffhaoe343b762011-12-05 16:36:44 -080093
jeffhao2692b572011-12-16 15:42:28 -080094 // Maps a thread to its clock base.
95 std::map<Thread*, uint64_t> thread_clock_base_map_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080096
jeffhao2692b572011-12-16 15:42:28 -080097 // File to write trace data out to, NULL if direct to ddms.
98 UniquePtr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080099
jeffhao2692b572011-12-16 15:42:28 -0800100 // Buffer to store trace data.
101 UniquePtr<uint8_t> buf_;
102
103 bool overflow_;
104 int buffer_size_;
105 uint64_t start_time_;
106 uint16_t trace_version_;
107 uint16_t record_size_;
108
109 volatile int32_t cur_offset_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800110
jeffhaoe343b762011-12-05 16:36:44 -0800111 DISALLOW_COPY_AND_ASSIGN(Trace);
112};
113
114} // namespace art
115
116#endif // ART_SRC_TRACE_H_