| OLD | NEW |
| 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" | |
| 12 #include "mojo/data_pipe_utils/data_pipe_utils.h" | 11 #include "mojo/data_pipe_utils/data_pipe_utils.h" |
| 12 #include "mojo/environment/scoped_chromium_init.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_impl_base.h" |
| 15 #include "mojo/public/cpp/application/application_impl.h" | |
| 16 #include "mojo/public/cpp/application/connect.h" | 15 #include "mojo/public/cpp/application/connect.h" |
| 16 #include "mojo/public/cpp/application/run_application.h" |
| 17 #include "mojo/public/cpp/bindings/binding.h" | 17 #include "mojo/public/cpp/bindings/binding.h" |
| 18 #include "mojo/services/http_server/cpp/http_server_util.h" | 18 #include "mojo/services/http_server/cpp/http_server_util.h" |
| 19 #include "mojo/services/http_server/interfaces/http_server.mojom.h" | 19 #include "mojo/services/http_server/interfaces/http_server.mojom.h" |
| 20 #include "mojo/services/http_server/interfaces/http_server_factory.mojom.h" | 20 #include "mojo/services/http_server/interfaces/http_server_factory.mojom.h" |
| 21 #include "mojo/services/network/interfaces/net_address.mojom.h" | 21 #include "mojo/services/network/interfaces/net_address.mojom.h" |
| 22 #include "mojo/services/tracing/interfaces/tracing.mojom.h" | 22 #include "mojo/services/tracing/interfaces/tracing.mojom.h" |
| 23 #include "services/debugger/trace_collector.h" | 23 #include "services/debugger/trace_collector.h" |
| 24 | 24 |
| 25 // Debugger is a Mojo application that exposes an http server and talks to other | 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 | 26 // mojo apps in response to url requests received by the server. Supported |
| 27 // actions include tracing and profiling, allowing to interactively inspect how | 27 // actions include tracing and profiling, allowing to interactively inspect how |
| 28 // the shell is performing. | 28 // the shell is performing. |
| 29 | 29 |
| 30 namespace debugger { | 30 namespace debugger { |
| 31 | 31 |
| 32 class Debugger : public mojo::ApplicationDelegate, | 32 class Debugger : public mojo::ApplicationImplBase, |
| 33 public http_server::HttpHandler { | 33 public http_server::HttpHandler { |
| 34 public: | 34 public: |
| 35 Debugger() : is_tracing_(false), app_(nullptr), handler_binding_(this) {} | 35 Debugger() : is_tracing_(false), handler_binding_(this) {} |
| 36 ~Debugger() override {} | 36 ~Debugger() override {} |
| 37 | 37 |
| 38 private: | 38 private: |
| 39 // mojo::ApplicationDelegate: | 39 // mojo::ApplicationImplBase: |
| 40 void Initialize(mojo::ApplicationImpl* app) override { | 40 void OnInitialize() override { |
| 41 app_ = app; | |
| 42 | |
| 43 // Format: --args-for="app_url command_port" | 41 // Format: --args-for="app_url command_port" |
| 44 if (app->args().size() < 2) { | 42 if (args().size() < 2) { |
| 45 LOG(ERROR) << "--args-for required to specify command_port"; | 43 LOG(ERROR) << "--args-for required to specify command_port"; |
| 46 mojo::ApplicationImpl::Terminate(); | 44 mojo::TerminateApplication(MOJO_RESULT_INVALID_ARGUMENT); |
| 47 return; | 45 return; |
| 48 } | 46 } |
| 49 base::StringToUint(app->args()[1], &command_port_); | 47 base::StringToUint(args()[1], &command_port_); |
| 50 http_server::HttpServerFactoryPtr http_server_factory; | 48 http_server::HttpServerFactoryPtr http_server_factory; |
| 51 mojo::ConnectToService(app->shell(), "mojo:http_server", | 49 mojo::ConnectToService(shell(), "mojo:http_server", |
| 52 GetProxy(&http_server_factory)); | 50 GetProxy(&http_server_factory)); |
| 53 | 51 |
| 54 mojo::NetAddressPtr local_address(mojo::NetAddress::New()); | 52 mojo::NetAddressPtr local_address(mojo::NetAddress::New()); |
| 55 local_address->family = mojo::NetAddressFamily::IPV4; | 53 local_address->family = mojo::NetAddressFamily::IPV4; |
| 56 local_address->ipv4 = mojo::NetAddressIPv4::New(); | 54 local_address->ipv4 = mojo::NetAddressIPv4::New(); |
| 57 local_address->ipv4->addr.resize(4); | 55 local_address->ipv4->addr.resize(4); |
| 58 local_address->ipv4->addr[0] = 127; | 56 local_address->ipv4->addr[0] = 127; |
| 59 local_address->ipv4->addr[1] = 0; | 57 local_address->ipv4->addr[1] = 0; |
| 60 local_address->ipv4->addr[2] = 0; | 58 local_address->ipv4->addr[2] = 0; |
| 61 local_address->ipv4->addr[3] = 1; | 59 local_address->ipv4->addr[3] = 1; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 exit(0); | 109 exit(0); |
| 112 } | 110 } |
| 113 | 111 |
| 114 void StartTracing(const HandleRequestCallback& callback) { | 112 void StartTracing(const HandleRequestCallback& callback) { |
| 115 if (is_tracing_) { | 113 if (is_tracing_) { |
| 116 Error(callback, "Already tracing. Use stop_tracing to stop.\n"); | 114 Error(callback, "Already tracing. Use stop_tracing to stop.\n"); |
| 117 return; | 115 return; |
| 118 } | 116 } |
| 119 | 117 |
| 120 if (!tracing_) { | 118 if (!tracing_) { |
| 121 mojo::ConnectToService(app_->shell(), "mojo:tracing", | 119 mojo::ConnectToService(shell(), "mojo:tracing", GetProxy(&tracing_)); |
| 122 GetProxy(&tracing_)); | |
| 123 } | 120 } |
| 124 is_tracing_ = true; | 121 is_tracing_ = true; |
| 125 mojo::DataPipe pipe; | 122 mojo::DataPipe pipe; |
| 126 tracing_->Start(pipe.producer_handle.Pass(), mojo::String("*")); | 123 tracing_->Start(pipe.producer_handle.Pass(), mojo::String("*")); |
| 127 trace_collector_.reset(new TraceCollector(pipe.consumer_handle.Pass())); | 124 trace_collector_.reset(new TraceCollector(pipe.consumer_handle.Pass())); |
| 128 Respond(callback, "Starting trace (type 'stop_tracing' to stop)\n"); | 125 Respond(callback, "Starting trace (type 'stop_tracing' to stop)\n"); |
| 129 } | 126 } |
| 130 | 127 |
| 131 void StopTracing(const HandleRequestCallback& callback) { | 128 void StopTracing(const HandleRequestCallback& callback) { |
| 132 if (!is_tracing_) { | 129 if (!is_tracing_) { |
| 133 Error(callback, "Not tracing yet. Use start_tracing to start.\n"); | 130 Error(callback, "Not tracing yet. Use start_tracing to start.\n"); |
| 134 return; | 131 return; |
| 135 } | 132 } |
| 136 | 133 |
| 137 is_tracing_ = false; | 134 is_tracing_ = false; |
| 138 tracing_->StopAndFlush(); | 135 tracing_->StopAndFlush(); |
| 139 trace_collector_->GetTrace(base::Bind(&Debugger::OnTraceAvailable, | 136 trace_collector_->GetTrace(base::Bind(&Debugger::OnTraceAvailable, |
| 140 base::Unretained(this), callback)); | 137 base::Unretained(this), callback)); |
| 141 } | 138 } |
| 142 | 139 |
| 143 void OnTraceAvailable(HandleRequestCallback callback, std::string trace) { | 140 void OnTraceAvailable(HandleRequestCallback callback, std::string trace) { |
| 144 trace_collector_.reset(); | 141 trace_collector_.reset(); |
| 145 Respond(callback, trace); | 142 Respond(callback, trace); |
| 146 } | 143 } |
| 147 | 144 |
| 148 bool is_tracing_; | 145 bool is_tracing_; |
| 149 mojo::ApplicationImpl* app_; | |
| 150 tracing::TraceCollectorPtr tracing_; | 146 tracing::TraceCollectorPtr tracing_; |
| 151 uint32_t command_port_; | 147 uint32_t command_port_; |
| 152 | 148 |
| 153 http_server::HttpServerPtr http_server_; | 149 http_server::HttpServerPtr http_server_; |
| 154 mojo::Binding<http_server::HttpHandler> handler_binding_; | 150 mojo::Binding<http_server::HttpHandler> handler_binding_; |
| 155 | 151 |
| 156 scoped_ptr<TraceCollector> trace_collector_; | 152 scoped_ptr<TraceCollector> trace_collector_; |
| 157 | 153 |
| 158 DISALLOW_COPY_AND_ASSIGN(Debugger); | 154 DISALLOW_COPY_AND_ASSIGN(Debugger); |
| 159 }; | 155 }; |
| 160 | 156 |
| 161 } // namespace debugger | 157 } // namespace debugger |
| 162 | 158 |
| 163 MojoResult MojoMain(MojoHandle application_request) { | 159 MojoResult MojoMain(MojoHandle application_request) { |
| 164 mojo::ApplicationRunnerChromium runner(new debugger::Debugger); | 160 mojo::ScopedChromiumInit init; |
| 165 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); | 161 debugger::Debugger debugger_app; |
| 166 return runner.Run(application_request); | 162 return mojo::RunApplication(application_request, &debugger_app); |
| 167 } | 163 } |
| OLD | NEW |