OLD | NEW |
| (Empty) |
1 // Copyright 2014 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. | |
4 | |
5 #include <algorithm> | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/debug/profiler.h" | |
9 #include "base/strings/string_number_conversions.h" | |
10 #include "base/strings/stringprintf.h" | |
11 #include "mojo/application/application_runner_chromium.h" | |
12 #include "mojo/data_pipe_utils/data_pipe_utils.h" | |
13 #include "mojo/public/c/system/main.h" | |
14 #include "mojo/public/cpp/application/application_delegate.h" | |
15 #include "mojo/public/cpp/application/application_impl.h" | |
16 #include "mojo/public/cpp/bindings/binding.h" | |
17 #include "mojo/services/http_server/cpp/http_server_util.h" | |
18 #include "mojo/services/http_server/interfaces/http_server.mojom.h" | |
19 #include "mojo/services/http_server/interfaces/http_server_factory.mojom.h" | |
20 #include "mojo/services/network/interfaces/net_address.mojom.h" | |
21 #include "mojo/services/tracing/interfaces/tracing.mojom.h" | |
22 #include "mojo/services/window_manager/interfaces/window_manager.mojom.h" | |
23 #include "services/debugger/trace_collector.h" | |
24 | |
25 // Debugger is a Mojo application that exposes an http server and talks to other | |
26 // mojo apps in response to url requests received by the server. Supported | |
27 // actions include tracing and profiling, allowing to interactively inspect how | |
28 // the shell is performing. | |
29 | |
30 namespace debugger { | |
31 | |
32 class Debugger : public mojo::ApplicationDelegate, | |
33 public http_server::HttpHandler { | |
34 public: | |
35 Debugger() : is_tracing_(false), app_(nullptr), handler_binding_(this) {} | |
36 ~Debugger() override {} | |
37 | |
38 private: | |
39 // mojo::ApplicationDelegate: | |
40 void Initialize(mojo::ApplicationImpl* app) override { | |
41 app_ = app; | |
42 | |
43 // Format: --args-for="app_url command_port" | |
44 if (app->args().size() < 2) { | |
45 LOG(ERROR) << "--args-for required to specify command_port"; | |
46 mojo::ApplicationImpl::Terminate(); | |
47 return; | |
48 } | |
49 if (app->args().size() == 3 && app->args()[2] == "--wm") { | |
50 // Connect to window manager only if requested, as the user might want to | |
51 // run the debugger without spawning one. | |
52 app_->ConnectToService("mojo:window_manager", &window_manager_); | |
53 } | |
54 base::StringToUint(app->args()[1], &command_port_); | |
55 http_server::HttpServerFactoryPtr http_server_factory; | |
56 app->ConnectToService("mojo:http_server", &http_server_factory); | |
57 | |
58 mojo::NetAddressPtr local_address(mojo::NetAddress::New()); | |
59 local_address->family = mojo::NetAddressFamily::IPV4; | |
60 local_address->ipv4 = mojo::NetAddressIPv4::New(); | |
61 local_address->ipv4->addr.resize(4); | |
62 local_address->ipv4->addr[0] = 127; | |
63 local_address->ipv4->addr[1] = 0; | |
64 local_address->ipv4->addr[2] = 0; | |
65 local_address->ipv4->addr[3] = 1; | |
66 local_address->ipv4->port = command_port_; | |
67 http_server_factory->CreateHttpServer(GetProxy(&http_server_).Pass(), | |
68 local_address.Pass()); | |
69 | |
70 http_server::HttpHandlerPtr handler_ptr; | |
71 handler_binding_.Bind(GetProxy(&handler_ptr).Pass()); | |
72 http_server_->SetHandler(".*", handler_ptr.Pass(), | |
73 [](bool result) { DCHECK(result); }); | |
74 } | |
75 | |
76 bool ConfigureIncomingConnection( | |
77 mojo::ApplicationConnection* connection) override { | |
78 return true; | |
79 } | |
80 | |
81 // http_server::HttpHandler: | |
82 void HandleRequest(http_server::HttpRequestPtr request, | |
83 const HandleRequestCallback& callback) override { | |
84 // FIXME: We should use use a fancier lookup system more like what | |
85 // services/http_server/http_server.cc does with AddHandler. | |
86 if (request->relative_url == "/reload") { | |
87 Load(callback, url_); | |
88 } else if (request->relative_url == "/quit") { | |
89 Exit(); | |
90 } else if (request->relative_url == "/load") { | |
91 std::string url; | |
92 mojo::common::BlockingCopyToString(request->body.Pass(), &url); | |
93 Load(callback, url); | |
94 } else if (request->relative_url == "/start_profiling") { | |
95 StartProfiling(callback); | |
96 } else if (request->relative_url == "/stop_profiling") { | |
97 StopProfiling(callback); | |
98 } else if (request->relative_url == "/start_tracing") { | |
99 StartTracing(callback); | |
100 } else if (request->relative_url == "/stop_tracing") { | |
101 StopTracing(callback); | |
102 } else { | |
103 Help(callback, request->relative_url); | |
104 } | |
105 } | |
106 | |
107 void Error(const HandleRequestCallback& callback, std::string message) { | |
108 callback.Run(http_server::CreateHttpResponse(500, message)); | |
109 } | |
110 | |
111 void Respond(const HandleRequestCallback& callback, std::string response) { | |
112 callback.Run(http_server::CreateHttpResponse(200, response)); | |
113 } | |
114 | |
115 void Help(const HandleRequestCallback& callback, std::string path) { | |
116 std::string help = base::StringPrintf( | |
117 "Sky Debugger running on port %d\n" | |
118 "Supported URLs:\n" | |
119 "/reload -- Reload the current page\n" | |
120 "/quit -- Quit\n" | |
121 "/load -- Load a new URL, url in POST body.\n", | |
122 command_port_); | |
123 if (path != "/") | |
124 help = "Unknown path: " + path + "\n\n" + help; | |
125 Respond(callback, help); | |
126 } | |
127 | |
128 void Load(const HandleRequestCallback& callback, std::string url) { | |
129 url_ = url; | |
130 Reload(); | |
131 std::string response = std::string("Loaded ") + url + "\n"; | |
132 Respond(callback, response); | |
133 } | |
134 | |
135 void Reload() { | |
136 if (!window_manager_) { | |
137 // If window_manager_ was not connected to eagerly on startup, we do that | |
138 // on the first demand. | |
139 app_->ConnectToService("mojo:window_manager", &window_manager_); | |
140 } | |
141 | |
142 // SimpleWindowManager will wire up necessary services on our behalf. | |
143 window_manager_->Embed(url_, nullptr, nullptr); | |
144 } | |
145 | |
146 void Exit() { | |
147 // TODO(eseidel): We should orderly shutdown once mojo can. | |
148 exit(0); | |
149 } | |
150 | |
151 void StartTracing(const HandleRequestCallback& callback) { | |
152 if (is_tracing_) { | |
153 Error(callback, "Already tracing. Use stop_tracing to stop.\n"); | |
154 return; | |
155 } | |
156 | |
157 if (!tracing_) | |
158 app_->ConnectToService("mojo:tracing", &tracing_); | |
159 is_tracing_ = true; | |
160 mojo::DataPipe pipe; | |
161 tracing_->Start(pipe.producer_handle.Pass(), mojo::String("*")); | |
162 trace_collector_.reset(new TraceCollector(pipe.consumer_handle.Pass())); | |
163 Respond(callback, "Starting trace (type 'stop_tracing' to stop)\n"); | |
164 } | |
165 | |
166 void StopTracing(const HandleRequestCallback& callback) { | |
167 if (!is_tracing_) { | |
168 Error(callback, "Not tracing yet. Use start_tracing to start.\n"); | |
169 return; | |
170 } | |
171 | |
172 is_tracing_ = false; | |
173 tracing_->StopAndFlush(); | |
174 trace_collector_->GetTrace(base::Bind(&Debugger::OnTraceAvailable, | |
175 base::Unretained(this), callback)); | |
176 } | |
177 | |
178 void OnTraceAvailable(HandleRequestCallback callback, std::string trace) { | |
179 trace_collector_.reset(); | |
180 Respond(callback, trace); | |
181 } | |
182 | |
183 void StartProfiling(const HandleRequestCallback& callback) { | |
184 #if !defined(NDEBUG) || !defined(ENABLE_PROFILING) | |
185 Error(callback, | |
186 "Profiling requires is_debug=false and enable_profiling=true"); | |
187 return; | |
188 #else | |
189 base::debug::StartProfiling("sky_viewer.pprof"); | |
190 Respond(callback, "Starting profiling (stop with 'stop_profiling')"); | |
191 #endif | |
192 } | |
193 | |
194 void StopProfiling(const HandleRequestCallback& callback) { | |
195 if (!base::debug::BeingProfiled()) { | |
196 Error(callback, "Profiling not started"); | |
197 return; | |
198 } | |
199 base::debug::StopProfiling(); | |
200 Respond(callback, "Stopped profiling"); | |
201 } | |
202 | |
203 bool is_tracing_; | |
204 mojo::ApplicationImpl* app_; | |
205 mojo::WindowManagerPtr window_manager_; | |
206 tracing::TraceCollectorPtr tracing_; | |
207 std::string url_; | |
208 uint32_t command_port_; | |
209 | |
210 http_server::HttpServerPtr http_server_; | |
211 mojo::Binding<http_server::HttpHandler> handler_binding_; | |
212 | |
213 scoped_ptr<TraceCollector> trace_collector_; | |
214 | |
215 DISALLOW_COPY_AND_ASSIGN(Debugger); | |
216 }; | |
217 | |
218 } // namespace debugger | |
219 | |
220 MojoResult MojoMain(MojoHandle application_request) { | |
221 mojo::ApplicationRunnerChromium runner(new debugger::Debugger); | |
222 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); | |
223 return runner.Run(application_request); | |
224 } | |
OLD | NEW |