OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdio.h> | 5 #include <stdio.h> |
6 #include <string.h> | 6 #include <string.h> |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <iostream> | 9 #include <iostream> |
10 | 10 |
11 #include "base/at_exit.h" | 11 #include "base/at_exit.h" |
12 #include "base/base_switches.h" | 12 #include "base/base_switches.h" |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/debug/profiler.h" | 15 #include "base/debug/profiler.h" |
16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
18 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
19 #include "base/trace_event/trace_event.h" | 19 #include "base/trace_event/trace_event.h" |
20 #include "shell/child_main.h" | |
21 #include "shell/command_line_util.h" | 20 #include "shell/command_line_util.h" |
22 #include "shell/context.h" | 21 #include "shell/context.h" |
23 #include "shell/init.h" | 22 #include "shell/init.h" |
24 #include "shell/switches.h" | 23 #include "shell/switches.h" |
25 | 24 |
26 namespace { | 25 namespace { |
27 | 26 |
28 void Usage() { | 27 void Usage() { |
29 std::cerr << "Launch Mojo applications.\n"; | 28 std::cerr << "Launch Mojo applications.\n"; |
30 std::cerr | 29 std::cerr |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 } // namespace | 104 } // namespace |
106 | 105 |
107 int main(int argc, char** argv) { | 106 int main(int argc, char** argv) { |
108 base::AtExitManager at_exit; | 107 base::AtExitManager at_exit; |
109 base::CommandLine::Init(argc, argv); | 108 base::CommandLine::Init(argc, argv); |
110 | 109 |
111 mojo::shell::InitializeLogging(); | 110 mojo::shell::InitializeLogging(); |
112 | 111 |
113 const base::CommandLine& command_line = | 112 const base::CommandLine& command_line = |
114 *base::CommandLine::ForCurrentProcess(); | 113 *base::CommandLine::ForCurrentProcess(); |
| 114 if (command_line.HasSwitch(switches::kHelp) || |
| 115 command_line.GetArgs().empty()) { |
| 116 Usage(); |
| 117 return 0; |
| 118 } |
115 | 119 |
116 // TODO(vtl): Unify parent and child process cases to the extent possible. | 120 const std::set<std::string> all_switches = switches::GetAllSwitches(); |
117 int exit_code = 0; | 121 const base::CommandLine::SwitchMap switches = command_line.GetSwitches(); |
118 if (command_line.HasSwitch(switches::kChildProcess)) { | 122 for (const auto& s : switches) { |
119 exit_code = mojo::shell::ChildMain(); | 123 if (all_switches.find(s.first) == all_switches.end()) { |
120 } else { | 124 std::cerr << "Unknown switch: " << s.first << std::endl; |
121 // Only check the command line for the main process. | |
122 const std::set<std::string> all_switches = switches::GetAllSwitches(); | |
123 const base::CommandLine::SwitchMap switches = command_line.GetSwitches(); | |
124 bool found_unknown_switch = false; | |
125 for (const auto& s : switches) { | |
126 if (all_switches.find(s.first) == all_switches.end()) { | |
127 std::cerr << "unknown switch: " << s.first << std::endl; | |
128 found_unknown_switch = true; | |
129 } | |
130 } | |
131 | |
132 if (found_unknown_switch || command_line.HasSwitch(switches::kHelp) || | |
133 command_line.GetArgs().empty()) { | |
134 Usage(); | 125 Usage(); |
135 return 0; | 126 return 1; |
136 } | |
137 | |
138 if (command_line.HasSwitch(switches::kTraceStartup)) { | |
139 g_tracing = true; | |
140 base::trace_event::CategoryFilter category_filter( | |
141 command_line.GetSwitchValueASCII(switches::kTraceStartup)); | |
142 base::trace_event::TraceLog::GetInstance()->SetEnabled( | |
143 category_filter, base::trace_event::TraceLog::RECORDING_MODE, | |
144 base::trace_event::TraceOptions( | |
145 base::trace_event::RECORD_UNTIL_FULL)); | |
146 } | |
147 | |
148 if (command_line.HasSwitch(switches::kCPUProfile)) { | |
149 #if !defined(NDEBUG) || !defined(ENABLE_PROFILING) | |
150 LOG(ERROR) << "Profiling requires is_debug=false and " | |
151 << "enable_profiling=true. Continuing without profiling."; | |
152 // StartProfiling() and StopProfiling() are a no-op. | |
153 #endif | |
154 base::debug::StartProfiling("mojo_shell.pprof"); | |
155 } | |
156 | |
157 // We want the shell::Context to outlive the MessageLoop so that pipes are | |
158 // all gracefully closed / error-out before we try to shut the Context down. | |
159 mojo::shell::Context shell_context; | |
160 { | |
161 base::MessageLoop message_loop; | |
162 if (!shell_context.Init()) { | |
163 Usage(); | |
164 return 0; | |
165 } | |
166 if (g_tracing) { | |
167 message_loop.PostDelayedTask(FROM_HERE, | |
168 base::Bind(StopTracingAndFlushToDisk), | |
169 base::TimeDelta::FromSeconds(5)); | |
170 } | |
171 | |
172 // The mojo_shell --args-for command-line switch is handled specially | |
173 // because it can appear more than once. The base::CommandLine class | |
174 // collapses multiple occurrences of the same switch. | |
175 for (int i = 1; i < argc; i++) { | |
176 ApplyApplicationArgs(&shell_context, argv[i]); | |
177 } | |
178 | |
179 message_loop.PostTask( | |
180 FROM_HERE, | |
181 base::Bind(&mojo::shell::RunCommandLineApps, &shell_context)); | |
182 message_loop.Run(); | |
183 | |
184 // Must be called before |message_loop| is destroyed. | |
185 shell_context.Shutdown(); | |
186 } | |
187 | |
188 if (command_line.HasSwitch(switches::kCPUProfile)) { | |
189 base::debug::StopProfiling(); | |
190 } | 127 } |
191 } | 128 } |
192 | 129 |
| 130 if (command_line.HasSwitch(switches::kTraceStartup)) { |
| 131 g_tracing = true; |
| 132 base::trace_event::CategoryFilter category_filter( |
| 133 command_line.GetSwitchValueASCII(switches::kTraceStartup)); |
| 134 base::trace_event::TraceLog::GetInstance()->SetEnabled( |
| 135 category_filter, base::trace_event::TraceLog::RECORDING_MODE, |
| 136 base::trace_event::TraceOptions(base::trace_event::RECORD_UNTIL_FULL)); |
| 137 } |
| 138 |
| 139 if (command_line.HasSwitch(switches::kCPUProfile)) { |
| 140 #if !defined(NDEBUG) || !defined(ENABLE_PROFILING) |
| 141 LOG(ERROR) << "Profiling requires is_debug=false and " |
| 142 << "enable_profiling=true. Continuing without profiling."; |
| 143 // StartProfiling() and StopProfiling() are a no-op. |
| 144 #endif |
| 145 base::debug::StartProfiling("mojo_shell.pprof"); |
| 146 } |
| 147 |
| 148 // We want the shell::Context to outlive the MessageLoop so that pipes are all |
| 149 // gracefully closed / error-out before we try to shut the Context down. |
| 150 mojo::shell::Context shell_context; |
| 151 { |
| 152 base::MessageLoop message_loop; |
| 153 if (!shell_context.Init()) { |
| 154 Usage(); |
| 155 return 1; |
| 156 } |
| 157 if (g_tracing) { |
| 158 message_loop.PostDelayedTask(FROM_HERE, |
| 159 base::Bind(StopTracingAndFlushToDisk), |
| 160 base::TimeDelta::FromSeconds(5)); |
| 161 } |
| 162 |
| 163 // The mojo_shell --args-for command-line switch is handled specially |
| 164 // because it can appear more than once. The base::CommandLine class |
| 165 // collapses multiple occurrences of the same switch. |
| 166 for (int i = 1; i < argc; i++) |
| 167 ApplyApplicationArgs(&shell_context, argv[i]); |
| 168 |
| 169 message_loop.PostTask( |
| 170 FROM_HERE, |
| 171 base::Bind(&mojo::shell::RunCommandLineApps, &shell_context)); |
| 172 message_loop.Run(); |
| 173 |
| 174 // Must be called before |message_loop| is destroyed. |
| 175 shell_context.Shutdown(); |
| 176 } |
| 177 |
| 178 if (command_line.HasSwitch(switches::kCPUProfile)) |
| 179 base::debug::StopProfiling(); |
193 if (g_tracing) | 180 if (g_tracing) |
194 StopTracingAndFlushToDisk(); | 181 StopTracingAndFlushToDisk(); |
195 return exit_code; | 182 return 0; |
196 } | 183 } |
OLD | NEW |