| 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/command_line_util.h" | 20 #include "shell/command_line_util.h" |
| 21 #include "shell/context.h" | 21 #include "shell/context.h" |
| 22 #include "shell/init.h" | 22 #include "shell/init.h" |
| 23 #include "shell/switches.h" | 23 #include "shell/switches.h" |
| 24 #include "shell/tracer.h" |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 void Usage() { | 28 void Usage() { |
| 28 std::cerr << "Launch Mojo applications.\n"; | 29 std::cerr << "Launch Mojo applications.\n"; |
| 29 std::cerr | 30 std::cerr |
| 30 << "Usage: mojo_shell" | 31 << "Usage: mojo_shell" |
| 31 << " [--" << switches::kArgsFor << "=<mojo-app>]" | 32 << " [--" << switches::kArgsFor << "=<mojo-app>]" |
| 32 << " [--" << switches::kContentHandlers << "=<handlers>]" | 33 << " [--" << switches::kContentHandlers << "=<handlers>]" |
| 33 << " [--" << switches::kCPUProfile << "]" | 34 << " [--" << switches::kCPUProfile << "]" |
| 34 << " [--" << switches::kDisableCache << "]" | 35 << " [--" << switches::kDisableCache << "]" |
| 35 << " [--" << switches::kEnableMultiprocess << "]" | 36 << " [--" << switches::kEnableMultiprocess << "]" |
| 36 << " [--" << switches::kOrigin << "=<url-lib-path>]" | 37 << " [--" << switches::kOrigin << "=<url-lib-path>]" |
| 37 << " [--" << switches::kTraceStartup << "]" | 38 << " [--" << switches::kTraceStartup << "]" |
| 38 << " [--" << switches::kURLMappings << "=from1=to1,from2=to2]" | 39 << " [--" << switches::kURLMappings << "=from1=to1,from2=to2]" |
| 39 << " [--" << switches::kPredictableAppFilenames << "]" | 40 << " [--" << switches::kPredictableAppFilenames << "]" |
| 40 << " [--" << switches::kWaitForDebugger << "]" | 41 << " [--" << switches::kWaitForDebugger << "]" |
| 41 << " <mojo-app> ...\n\n" | 42 << " <mojo-app> ...\n\n" |
| 42 << "A <mojo-app> is a Mojo URL or a Mojo URL and arguments within " | 43 << "A <mojo-app> is a Mojo URL or a Mojo URL and arguments within " |
| 43 << "quotes.\n" | 44 << "quotes.\n" |
| 44 << "Example: mojo_shell \"mojo:js_standalone test.js\".\n" | 45 << "Example: mojo_shell \"mojo:js_standalone test.js\".\n" |
| 45 << "<url-lib-path> is searched for shared libraries named by mojo URLs.\n" | 46 << "<url-lib-path> is searched for shared libraries named by mojo URLs.\n" |
| 46 << "The value of <handlers> is a comma separated list like:\n" | 47 << "The value of <handlers> is a comma separated list like:\n" |
| 47 << "text/html,mojo:html_viewer," | 48 << "text/html,mojo:html_viewer," |
| 48 << "application/javascript,mojo:js_content_handler\n"; | 49 << "application/javascript,mojo:js_content_handler\n"; |
| 49 } | 50 } |
| 50 | 51 |
| 51 // Whether we're currently tracing. | |
| 52 bool g_tracing = false; | |
| 53 | |
| 54 // Number of tracing blocks written. | |
| 55 uint32_t g_blocks = 0; | |
| 56 | |
| 57 // Trace file, if open. | |
| 58 FILE* g_trace_file = nullptr; | |
| 59 | |
| 60 void WriteTraceDataCollected( | |
| 61 base::WaitableEvent* event, | |
| 62 const scoped_refptr<base::RefCountedString>& events_str, | |
| 63 bool has_more_events) { | |
| 64 if (g_blocks) { | |
| 65 fwrite(",", 1, 1, g_trace_file); | |
| 66 } | |
| 67 | |
| 68 ++g_blocks; | |
| 69 fwrite(events_str->data().c_str(), 1, events_str->data().length(), | |
| 70 g_trace_file); | |
| 71 if (!has_more_events) { | |
| 72 static const char kEnd[] = "]}"; | |
| 73 fwrite(kEnd, 1, strlen(kEnd), g_trace_file); | |
| 74 PCHECK(fclose(g_trace_file) == 0); | |
| 75 g_trace_file = nullptr; | |
| 76 event->Signal(); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 void EndTraceAndFlush(base::WaitableEvent* event) { | |
| 81 g_trace_file = fopen("mojo_shell.trace", "w+"); | |
| 82 PCHECK(g_trace_file); | |
| 83 static const char kStart[] = "{\"traceEvents\":["; | |
| 84 fwrite(kStart, 1, strlen(kStart), g_trace_file); | |
| 85 base::trace_event::TraceLog::GetInstance()->SetDisabled(); | |
| 86 base::trace_event::TraceLog::GetInstance()->Flush( | |
| 87 base::Bind(&WriteTraceDataCollected, base::Unretained(event))); | |
| 88 } | |
| 89 | |
| 90 void StopTracingAndFlushToDisk() { | |
| 91 g_tracing = false; | |
| 92 base::trace_event::TraceLog::GetInstance()->SetDisabled(); | |
| 93 base::WaitableEvent flush_complete_event(false, false); | |
| 94 // TraceLog::Flush requires a message loop but we've already shut ours down. | |
| 95 // Spin up a new thread to flush things out. | |
| 96 base::Thread flush_thread("mojo_shell_trace_event_flush"); | |
| 97 flush_thread.Start(); | |
| 98 flush_thread.message_loop()->PostTask( | |
| 99 FROM_HERE, | |
| 100 base::Bind(EndTraceAndFlush, base::Unretained(&flush_complete_event))); | |
| 101 flush_complete_event.Wait(); | |
| 102 } | |
| 103 | |
| 104 } // namespace | 52 } // namespace |
| 105 | 53 |
| 106 int main(int argc, char** argv) { | 54 int main(int argc, char** argv) { |
| 107 base::AtExitManager at_exit; | 55 base::AtExitManager at_exit; |
| 108 base::CommandLine::Init(argc, argv); | 56 base::CommandLine::Init(argc, argv); |
| 109 | 57 |
| 58 shell::Tracer tracer; |
| 110 shell::InitializeLogging(); | 59 shell::InitializeLogging(); |
| 111 | 60 |
| 112 const base::CommandLine& command_line = | 61 const base::CommandLine& command_line = |
| 113 *base::CommandLine::ForCurrentProcess(); | 62 *base::CommandLine::ForCurrentProcess(); |
| 114 if (command_line.HasSwitch(switches::kHelp) || | 63 if (command_line.HasSwitch(switches::kHelp) || |
| 115 command_line.GetArgs().empty()) { | 64 command_line.GetArgs().empty()) { |
| 116 Usage(); | 65 Usage(); |
| 117 return 0; | 66 return 0; |
| 118 } | 67 } |
| 119 | 68 |
| 120 const std::set<std::string> all_switches = switches::GetAllSwitches(); | 69 const std::set<std::string> all_switches = switches::GetAllSwitches(); |
| 121 const base::CommandLine::SwitchMap switches = command_line.GetSwitches(); | 70 const base::CommandLine::SwitchMap switches = command_line.GetSwitches(); |
| 122 for (const auto& s : switches) { | 71 for (const auto& s : switches) { |
| 123 if (all_switches.find(s.first) == all_switches.end()) { | 72 if (all_switches.find(s.first) == all_switches.end()) { |
| 124 std::cerr << "Unknown switch: " << s.first << std::endl; | 73 std::cerr << "Unknown switch: " << s.first << std::endl; |
| 125 Usage(); | 74 Usage(); |
| 126 return 1; | 75 return 1; |
| 127 } | 76 } |
| 128 } | 77 } |
| 129 | 78 |
| 130 if (command_line.HasSwitch(switches::kTraceStartup)) { | 79 bool trace_startup = command_line.HasSwitch(switches::kTraceStartup); |
| 131 g_tracing = true; | 80 if (trace_startup) |
| 132 base::trace_event::CategoryFilter category_filter( | 81 tracer.Start(command_line.GetSwitchValueASCII(switches::kTraceStartup)); |
| 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 | 82 |
| 139 if (command_line.HasSwitch(switches::kCPUProfile)) { | 83 if (command_line.HasSwitch(switches::kCPUProfile)) { |
| 140 #if !defined(NDEBUG) || !defined(ENABLE_PROFILING) | 84 #if !defined(NDEBUG) || !defined(ENABLE_PROFILING) |
| 141 LOG(ERROR) << "Profiling requires is_debug=false and " | 85 LOG(ERROR) << "Profiling requires is_debug=false and " |
| 142 << "enable_profiling=true. Continuing without profiling."; | 86 << "enable_profiling=true. Continuing without profiling."; |
| 143 // StartProfiling() and StopProfiling() are a no-op. | 87 // StartProfiling() and StopProfiling() are a no-op. |
| 144 #endif | 88 #endif |
| 145 base::debug::StartProfiling("mojo_shell.pprof"); | 89 base::debug::StartProfiling("mojo_shell.pprof"); |
| 146 } | 90 } |
| 147 | 91 |
| 148 // We want the shell::Context to outlive the MessageLoop so that pipes are all | 92 // 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. | 93 // gracefully closed / error-out before we try to shut the Context down. |
| 150 shell::Context shell_context; | 94 shell::Context shell_context; |
| 151 { | 95 { |
| 152 base::MessageLoop message_loop; | 96 base::MessageLoop message_loop; |
| 153 if (!shell_context.Init()) { | 97 if (!shell_context.Init()) { |
| 154 Usage(); | 98 Usage(); |
| 155 return 1; | 99 return 1; |
| 156 } | 100 } |
| 157 if (g_tracing) { | 101 |
| 158 message_loop.PostDelayedTask(FROM_HERE, | 102 if (trace_startup) { |
| 159 base::Bind(StopTracingAndFlushToDisk), | 103 message_loop.PostDelayedTask( |
| 160 base::TimeDelta::FromSeconds(5)); | 104 FROM_HERE, base::Bind(&shell::Tracer::StopAndFlushToFile, |
| 105 base::Unretained(&tracer), "mojo_shell.trace"), |
| 106 base::TimeDelta::FromSeconds(5)); |
| 161 } | 107 } |
| 162 | 108 |
| 163 // The mojo_shell --args-for command-line switch is handled specially | 109 // The mojo_shell --args-for command-line switch is handled specially |
| 164 // because it can appear more than once. The base::CommandLine class | 110 // because it can appear more than once. The base::CommandLine class |
| 165 // collapses multiple occurrences of the same switch. | 111 // collapses multiple occurrences of the same switch. |
| 166 for (int i = 1; i < argc; i++) | 112 for (int i = 1; i < argc; i++) |
| 167 ApplyApplicationArgs(&shell_context, argv[i]); | 113 ApplyApplicationArgs(&shell_context, argv[i]); |
| 168 | 114 |
| 169 message_loop.PostTask( | 115 message_loop.PostTask( |
| 170 FROM_HERE, base::Bind(&shell::RunCommandLineApps, &shell_context)); | 116 FROM_HERE, base::Bind(&shell::RunCommandLineApps, &shell_context)); |
| 171 message_loop.Run(); | 117 message_loop.Run(); |
| 172 | 118 |
| 173 // Must be called before |message_loop| is destroyed. | 119 // Must be called before |message_loop| is destroyed. |
| 174 shell_context.Shutdown(); | 120 shell_context.Shutdown(); |
| 175 } | 121 } |
| 176 | 122 |
| 177 if (command_line.HasSwitch(switches::kCPUProfile)) | 123 if (command_line.HasSwitch(switches::kCPUProfile)) |
| 178 base::debug::StopProfiling(); | 124 base::debug::StopProfiling(); |
| 179 if (g_tracing) | 125 if (trace_startup) |
| 180 StopTracingAndFlushToDisk(); | 126 tracer.StopAndFlushToFile("mojo_shell.trace"); |
| 181 return 0; | 127 return 0; |
| 182 } | 128 } |
| OLD | NEW |