1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/test/trace_to_file.h"
6
7 #include "base/base_switches.h"
8 #include "base/command_line.h"
9 #include "base/files/file_util.h"
10 #include "base/functional/bind.h"
11 #include "base/memory/ref_counted_memory.h"
12 #include "base/run_loop.h"
13 #include "base/task/single_thread_task_runner.h"
14 #include "base/test/task_environment.h"
15 #include "base/trace_event/trace_buffer.h"
16 #include "base/trace_event/trace_log.h"
17
18 namespace base {
19 namespace test {
20
TraceToFile()21 TraceToFile::TraceToFile() : started_(false) {
22 }
23
~TraceToFile()24 TraceToFile::~TraceToFile() {
25 EndTracingIfNeeded();
26 }
27
BeginTracingFromCommandLineOptions()28 void TraceToFile::BeginTracingFromCommandLineOptions() {
29 DCHECK(CommandLine::InitializedForCurrentProcess());
30 DCHECK(!started_);
31
32 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTraceToFile))
33 return;
34
35 // Empty filter (i.e. just --trace-to-file) turns into default categories in
36 // TraceEventImpl
37 std::string filter = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
38 switches::kTraceToFile);
39
40 FilePath path;
41 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTraceToFileName)) {
42 path = FilePath(CommandLine::ForCurrentProcess()
43 ->GetSwitchValuePath(switches::kTraceToFileName));
44 } else {
45 path = FilePath(FILE_PATH_LITERAL("trace.json"));
46 }
47
48 BeginTracing(path, filter);
49 }
50
BeginTracing(const FilePath & path,const std::string & categories)51 void TraceToFile::BeginTracing(const FilePath& path,
52 const std::string& categories) {
53 DCHECK(!started_);
54 started_ = true;
55 path_ = path;
56 WriteFileHeader();
57
58 trace_event::TraceLog::GetInstance()->SetEnabled(
59 trace_event::TraceConfig(categories, trace_event::RECORD_UNTIL_FULL),
60 trace_event::TraceLog::RECORDING_MODE);
61 }
62
WriteFileHeader()63 void TraceToFile::WriteFileHeader() {
64 WriteFile(path_, "{\"traceEvents\": [");
65 }
66
AppendFileFooter()67 void TraceToFile::AppendFileFooter() {
68 const char str[] = "]}";
69 AppendToFile(path_, str);
70 }
71
TraceOutputCallback(const std::string & data)72 void TraceToFile::TraceOutputCallback(const std::string& data) {
73 bool ret = AppendToFile(path_, data);
74 DCHECK(ret);
75 }
76
OnTraceDataCollected(OnceClosure quit_closure,trace_event::TraceResultBuffer * buffer,const scoped_refptr<RefCountedString> & json_events_str,bool has_more_events)77 static void OnTraceDataCollected(
78 OnceClosure quit_closure,
79 trace_event::TraceResultBuffer* buffer,
80 const scoped_refptr<RefCountedString>& json_events_str,
81 bool has_more_events) {
82 buffer->AddFragment(json_events_str->data());
83 if (!has_more_events)
84 std::move(quit_closure).Run();
85 }
86
EndTracingIfNeeded()87 void TraceToFile::EndTracingIfNeeded() {
88 if (!started_)
89 return;
90 started_ = false;
91
92 trace_event::TraceLog::GetInstance()->SetDisabled();
93
94 trace_event::TraceResultBuffer buffer;
95 buffer.SetOutputCallback(
96 BindRepeating(&TraceToFile::TraceOutputCallback, Unretained(this)));
97
98 // In tests we might not have a TaskEnvironment, create one if needed.
99 std::unique_ptr<SingleThreadTaskEnvironment> task_environment;
100 if (!SingleThreadTaskRunner::HasCurrentDefault())
101 task_environment = std::make_unique<SingleThreadTaskEnvironment>();
102
103 RunLoop run_loop;
104 trace_event::TraceLog::GetInstance()->Flush(BindRepeating(
105 &OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer)));
106 run_loop.Run();
107
108 AppendFileFooter();
109 }
110
111 } // namespace test
112 } // namespace base
113