1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
11 #include "base/base_switches.h"
12 #include "base/bind.h"
13 #include "base/command_line.h"
14 #include "base/files/file_util.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/trace_event/trace_event.h"
18 #include "components/tracing/trace_config_file.h"
19 #include "components/tracing/tracing_switches.h"
20 #include "mandoline/app/core_services_initialization.h"
21 #include "mandoline/app/desktop/launcher_process.h"
22 #include "mojo/runner/context.h"
23 #include "mojo/runner/switches.h"
28 // Whether we're currently tracing.
29 bool g_tracing
= false;
31 // Number of tracing blocks written.
32 uint32_t g_blocks
= 0;
34 // Trace file, if open.
35 FILE* g_trace_file
= nullptr;
37 void WriteTraceDataCollected(
38 base::WaitableEvent
* event
,
39 const scoped_refptr
<base::RefCountedString
>& events_str
,
40 bool has_more_events
) {
42 fwrite(",", 1, 1, g_trace_file
);
46 fwrite(events_str
->data().c_str(), 1, events_str
->data().length(),
48 if (!has_more_events
) {
49 static const char kEnd
[] = "]}";
50 fwrite(kEnd
, 1, strlen(kEnd
), g_trace_file
);
51 PCHECK(fclose(g_trace_file
) == 0);
52 g_trace_file
= nullptr;
57 void EndTraceAndFlush(base::WaitableEvent
* event
) {
58 g_trace_file
= fopen("mojo_shell.trace", "w+");
60 static const char kStart
[] = "{\"traceEvents\":[";
61 fwrite(kStart
, 1, strlen(kStart
), g_trace_file
);
62 base::trace_event::TraceLog::GetInstance()->SetDisabled();
63 base::trace_event::TraceLog::GetInstance()->Flush(
64 base::Bind(&WriteTraceDataCollected
, base::Unretained(event
)));
67 void StopTracingAndFlushToDisk() {
69 base::trace_event::TraceLog::GetInstance()->SetDisabled();
70 base::WaitableEvent
flush_complete_event(false, false);
71 // TraceLog::Flush requires a message loop but we've already shut ours down.
72 // Spin up a new thread to flush things out.
73 base::Thread
flush_thread("mojo_shell_trace_event_flush");
75 flush_thread
.message_loop()->PostTask(
77 base::Bind(EndTraceAndFlush
, base::Unretained(&flush_complete_event
)));
78 flush_complete_event
.Wait();
83 int LauncherProcessMain(int argc
, char** argv
) {
84 const base::CommandLine
& command_line
=
85 *base::CommandLine::ForCurrentProcess();
86 if (command_line
.HasSwitch(switches::kTraceStartup
)) {
88 base::trace_event::TraceConfig
trace_config(
89 command_line
.GetSwitchValueASCII(switches::kTraceStartup
),
90 base::trace_event::RECORD_UNTIL_FULL
);
91 base::trace_event::TraceLog::GetInstance()->SetEnabled(
92 trace_config
, base::trace_event::TraceLog::RECORDING_MODE
);
93 } else if (tracing::TraceConfigFile::GetInstance()->IsEnabled()) {
95 base::trace_event::TraceLog::GetInstance()->SetEnabled(
96 tracing::TraceConfigFile::GetInstance()->GetTraceConfig(),
97 base::trace_event::TraceLog::RECORDING_MODE
);
100 // We want the runner::Context to outlive the MessageLoop so that pipes are
101 // all gracefully closed / error-out before we try to shut the Context down.
102 mojo::runner::Context shell_context
;
103 InitCoreServicesForContext(&shell_context
);
105 base::MessageLoop message_loop
;
106 if (!shell_context
.Init()) {
110 message_loop
.PostDelayedTask(FROM_HERE
,
111 base::Bind(StopTracingAndFlushToDisk
),
112 base::TimeDelta::FromSeconds(5));
115 message_loop
.PostTask(FROM_HERE
,
116 base::Bind(&mojo::runner::Context::Run
,
117 base::Unretained(&shell_context
),
118 GURL("mojo:desktop_ui")));
121 // Must be called before |message_loop| is destroyed.
122 shell_context
.Shutdown();
126 StopTracingAndFlushToDisk();
130 } // namespace mandoline