Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: services/debugger/debugger.cc

Issue 1532893003: Tidy up the debugger. (Closed) Base URL: git@github.com:domokit/mojo.git@moz-2
Patch Set: tidy up the debugger Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 <algorithm> 5 #include <algorithm>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/debug/profiler.h" 8 #include "base/debug/profiler.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "mojo/application/application_runner_chromium.h" 11 #include "mojo/application/application_runner_chromium.h"
12 #include "mojo/data_pipe_utils/data_pipe_utils.h" 12 #include "mojo/data_pipe_utils/data_pipe_utils.h"
13 #include "mojo/public/c/system/main.h" 13 #include "mojo/public/c/system/main.h"
14 #include "mojo/public/cpp/application/application_delegate.h" 14 #include "mojo/public/cpp/application/application_delegate.h"
15 #include "mojo/public/cpp/application/application_impl.h" 15 #include "mojo/public/cpp/application/application_impl.h"
16 #include "mojo/public/cpp/bindings/binding.h" 16 #include "mojo/public/cpp/bindings/binding.h"
17 #include "mojo/services/http_server/cpp/http_server_util.h" 17 #include "mojo/services/http_server/cpp/http_server_util.h"
18 #include "mojo/services/http_server/interfaces/http_server.mojom.h" 18 #include "mojo/services/http_server/interfaces/http_server.mojom.h"
19 #include "mojo/services/http_server/interfaces/http_server_factory.mojom.h" 19 #include "mojo/services/http_server/interfaces/http_server_factory.mojom.h"
20 #include "mojo/services/network/interfaces/net_address.mojom.h" 20 #include "mojo/services/network/interfaces/net_address.mojom.h"
21 #include "mojo/services/tracing/interfaces/tracing.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" 22 #include "services/debugger/trace_collector.h"
24 23
25 // Debugger is a Mojo application that exposes an http server and talks to other 24 // 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 25 // mojo apps in response to url requests received by the server. Supported
27 // actions include tracing and profiling, allowing to interactively inspect how 26 // actions include tracing and profiling, allowing to interactively inspect how
28 // the shell is performing. 27 // the shell is performing.
29 28
30 namespace debugger { 29 namespace debugger {
31 30
32 class Debugger : public mojo::ApplicationDelegate, 31 class Debugger : public mojo::ApplicationDelegate,
33 public http_server::HttpHandler { 32 public http_server::HttpHandler {
34 public: 33 public:
35 Debugger() : is_tracing_(false), app_(nullptr), handler_binding_(this) {} 34 Debugger() : is_tracing_(false), app_(nullptr), handler_binding_(this) {}
36 ~Debugger() override {} 35 ~Debugger() override {}
37 36
38 private: 37 private:
39 // mojo::ApplicationDelegate: 38 // mojo::ApplicationDelegate:
40 void Initialize(mojo::ApplicationImpl* app) override { 39 void Initialize(mojo::ApplicationImpl* app) override {
41 app_ = app; 40 app_ = app;
42 41
43 // Format: --args-for="app_url command_port" 42 // Format: --args-for="app_url command_port"
44 if (app->args().size() < 2) { 43 if (app->args().size() < 2) {
45 LOG(ERROR) << "--args-for required to specify command_port"; 44 LOG(ERROR) << "--args-for required to specify command_port";
46 mojo::ApplicationImpl::Terminate(); 45 mojo::ApplicationImpl::Terminate();
47 return; 46 return;
48 } 47 }
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_); 48 base::StringToUint(app->args()[1], &command_port_);
55 http_server::HttpServerFactoryPtr http_server_factory; 49 http_server::HttpServerFactoryPtr http_server_factory;
56 app->ConnectToService("mojo:http_server", &http_server_factory); 50 app->ConnectToService("mojo:http_server", &http_server_factory);
57 51
58 mojo::NetAddressPtr local_address(mojo::NetAddress::New()); 52 mojo::NetAddressPtr local_address(mojo::NetAddress::New());
59 local_address->family = mojo::NetAddressFamily::IPV4; 53 local_address->family = mojo::NetAddressFamily::IPV4;
60 local_address->ipv4 = mojo::NetAddressIPv4::New(); 54 local_address->ipv4 = mojo::NetAddressIPv4::New();
61 local_address->ipv4->addr.resize(4); 55 local_address->ipv4->addr.resize(4);
62 local_address->ipv4->addr[0] = 127; 56 local_address->ipv4->addr[0] = 127;
63 local_address->ipv4->addr[1] = 0; 57 local_address->ipv4->addr[1] = 0;
(...skipping 12 matching lines...) Expand all
76 bool ConfigureIncomingConnection( 70 bool ConfigureIncomingConnection(
77 mojo::ApplicationConnection* connection) override { 71 mojo::ApplicationConnection* connection) override {
78 return true; 72 return true;
79 } 73 }
80 74
81 // http_server::HttpHandler: 75 // http_server::HttpHandler:
82 void HandleRequest(http_server::HttpRequestPtr request, 76 void HandleRequest(http_server::HttpRequestPtr request,
83 const HandleRequestCallback& callback) override { 77 const HandleRequestCallback& callback) override {
84 // FIXME: We should use use a fancier lookup system more like what 78 // FIXME: We should use use a fancier lookup system more like what
85 // services/http_server/http_server.cc does with AddHandler. 79 // services/http_server/http_server.cc does with AddHandler.
86 if (request->relative_url == "/reload") { 80 if (request->relative_url == "/quit") {
87 Load(callback, url_);
88 } else if (request->relative_url == "/quit") {
89 Exit(); 81 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") { 82 } else if (request->relative_url == "/start_tracing") {
99 StartTracing(callback); 83 StartTracing(callback);
100 } else if (request->relative_url == "/stop_tracing") { 84 } else if (request->relative_url == "/stop_tracing") {
101 StopTracing(callback); 85 StopTracing(callback);
102 } else { 86 } else {
103 Help(callback, request->relative_url); 87 Help(callback, request->relative_url);
104 } 88 }
105 } 89 }
106 90
107 void Error(const HandleRequestCallback& callback, std::string message) { 91 void Error(const HandleRequestCallback& callback, std::string message) {
108 callback.Run(http_server::CreateHttpResponse(500, message)); 92 callback.Run(http_server::CreateHttpResponse(500, message));
109 } 93 }
110 94
111 void Respond(const HandleRequestCallback& callback, std::string response) { 95 void Respond(const HandleRequestCallback& callback, std::string response) {
112 callback.Run(http_server::CreateHttpResponse(200, response)); 96 callback.Run(http_server::CreateHttpResponse(200, response));
113 } 97 }
114 98
115 void Help(const HandleRequestCallback& callback, std::string path) { 99 void Help(const HandleRequestCallback& callback, std::string path) {
116 std::string help = base::StringPrintf( 100 std::string help = base::StringPrintf(
117 "Sky Debugger running on port %d\n" 101 "Sky Debugger running on port %d\n"
ppi 2015/12/20 15:36:21 can you also s/Sky // ?
jeffbrown 2015/12/20 20:52:58 Sure.
118 "Supported URLs:\n" 102 "Supported URLs:\n"
119 "/reload -- Reload the current page\n"
120 "/quit -- Quit\n" 103 "/quit -- Quit\n"
121 "/load -- Load a new URL, url in POST body.\n", 104 "/start_tracing -- Start Tracing\n"
105 "/stop_tracing -- Stop Tracing\n",
122 command_port_); 106 command_port_);
123 if (path != "/") 107 if (path != "/")
124 help = "Unknown path: " + path + "\n\n" + help; 108 help = "Unknown path: " + path + "\n\n" + help;
125 Respond(callback, help); 109 Respond(callback, help);
126 } 110 }
127 111
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() { 112 void Exit() {
147 // TODO(eseidel): We should orderly shutdown once mojo can. 113 // TODO(eseidel): We should orderly shutdown once mojo can.
148 exit(0); 114 exit(0);
149 } 115 }
150 116
151 void StartTracing(const HandleRequestCallback& callback) { 117 void StartTracing(const HandleRequestCallback& callback) {
152 if (is_tracing_) { 118 if (is_tracing_) {
153 Error(callback, "Already tracing. Use stop_tracing to stop.\n"); 119 Error(callback, "Already tracing. Use stop_tracing to stop.\n");
154 return; 120 return;
155 } 121 }
(...skipping 17 matching lines...) Expand all
173 tracing_->StopAndFlush(); 139 tracing_->StopAndFlush();
174 trace_collector_->GetTrace(base::Bind(&Debugger::OnTraceAvailable, 140 trace_collector_->GetTrace(base::Bind(&Debugger::OnTraceAvailable,
175 base::Unretained(this), callback)); 141 base::Unretained(this), callback));
176 } 142 }
177 143
178 void OnTraceAvailable(HandleRequestCallback callback, std::string trace) { 144 void OnTraceAvailable(HandleRequestCallback callback, std::string trace) {
179 trace_collector_.reset(); 145 trace_collector_.reset();
180 Respond(callback, trace); 146 Respond(callback, trace);
181 } 147 }
182 148
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_; 149 bool is_tracing_;
204 mojo::ApplicationImpl* app_; 150 mojo::ApplicationImpl* app_;
205 mojo::WindowManagerPtr window_manager_;
206 tracing::TraceCollectorPtr tracing_; 151 tracing::TraceCollectorPtr tracing_;
207 std::string url_;
208 uint32_t command_port_; 152 uint32_t command_port_;
209 153
210 http_server::HttpServerPtr http_server_; 154 http_server::HttpServerPtr http_server_;
211 mojo::Binding<http_server::HttpHandler> handler_binding_; 155 mojo::Binding<http_server::HttpHandler> handler_binding_;
212 156
213 scoped_ptr<TraceCollector> trace_collector_; 157 scoped_ptr<TraceCollector> trace_collector_;
214 158
215 DISALLOW_COPY_AND_ASSIGN(Debugger); 159 DISALLOW_COPY_AND_ASSIGN(Debugger);
216 }; 160 };
217 161
218 } // namespace debugger 162 } // namespace debugger
219 163
220 MojoResult MojoMain(MojoHandle application_request) { 164 MojoResult MojoMain(MojoHandle application_request) {
221 mojo::ApplicationRunnerChromium runner(new debugger::Debugger); 165 mojo::ApplicationRunnerChromium runner(new debugger::Debugger);
222 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); 166 runner.set_message_loop_type(base::MessageLoop::TYPE_IO);
223 return runner.Run(application_request); 167 return runner.Run(application_request);
224 } 168 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698