mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Pull Sky debugger up to services/debugger.
Sky debugger is a native mojo app that puts up an http server and talks to other mojo apps in response to http requests, allowing to interactively start/stop tracing, profile, etc. This is generally useful also when one does not run mojo_shell through skydb, hence this patch pulls the debugger into services/debugger. We can then add plumbing for running with the debugger in devtools. Bug: domokit/devtools/#4. R=abarth@chromium.org Review URL: https://codereview.chromium.org/1151573010
This commit is contained in:
parent
5afac54a8b
commit
3a7e97e696
4
BUILD.gn
4
BUILD.gn
@ -9,11 +9,11 @@ group("sky") {
|
||||
testonly = true
|
||||
|
||||
deps = [
|
||||
"//sky/sdk",
|
||||
"//services/debugger",
|
||||
"//sky/engine/platform:platform_unittests",
|
||||
"//sky/engine/web:sky_unittests",
|
||||
"//sky/engine/wtf:unittests",
|
||||
"//sky/tools/debugger",
|
||||
"//sky/sdk",
|
||||
"//sky/tools/imagediff",
|
||||
"//sky/tools/tester",
|
||||
"//sky/viewer",
|
||||
|
||||
@ -1,38 +0,0 @@
|
||||
# Copyright 2014 The Chromium Authors. All rights reserved.
|
||||
# Use of this source code is governed by a BSD-style license that can be
|
||||
# found in the LICENSE file.
|
||||
|
||||
import("//mojo/public/mojo_application.gni")
|
||||
|
||||
group("debugger") {
|
||||
testonly = true
|
||||
|
||||
deps = [
|
||||
":sky_debugger",
|
||||
]
|
||||
}
|
||||
|
||||
mojo_native_application("sky_debugger") {
|
||||
output_name = "sky_debugger"
|
||||
|
||||
sources = [
|
||||
"debugger.cc",
|
||||
"trace_collector.cc",
|
||||
"trace_collector.h",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//base",
|
||||
"//base/allocator",
|
||||
"//mojo/application",
|
||||
"//mojo/common",
|
||||
"//mojo/public/cpp/bindings",
|
||||
"//mojo/public/cpp/system",
|
||||
"//mojo/public/cpp/utility",
|
||||
"//mojo/services/http_server/public/cpp",
|
||||
"//mojo/services/http_server/public/interfaces",
|
||||
"//mojo/services/network/public/interfaces",
|
||||
"//mojo/services/window_manager/public/interfaces",
|
||||
"//services/tracing:bindings",
|
||||
]
|
||||
}
|
||||
@ -1,211 +0,0 @@
|
||||
// Copyright 2014 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "base/debug/profiler.h"
|
||||
#include "base/strings/string_number_conversions.h"
|
||||
#include "base/strings/stringprintf.h"
|
||||
#include "mojo/application/application_runner_chromium.h"
|
||||
#include "mojo/common/data_pipe_utils.h"
|
||||
#include "mojo/public/c/system/main.h"
|
||||
#include "mojo/public/cpp/application/application_delegate.h"
|
||||
#include "mojo/public/cpp/application/application_impl.h"
|
||||
#include "mojo/public/cpp/bindings/binding.h"
|
||||
#include "mojo/services/http_server/public/cpp/http_server_util.h"
|
||||
#include "mojo/services/http_server/public/interfaces/http_server.mojom.h"
|
||||
#include "mojo/services/http_server/public/interfaces/http_server_factory.mojom.h"
|
||||
#include "mojo/services/network/public/interfaces/net_address.mojom.h"
|
||||
#include "mojo/services/window_manager/public/interfaces/window_manager.mojom.h"
|
||||
#include "services/tracing/tracing.mojom.h"
|
||||
#include "sky/tools/debugger/trace_collector.h"
|
||||
|
||||
namespace sky {
|
||||
namespace debugger {
|
||||
|
||||
class SkyDebugger : public mojo::ApplicationDelegate,
|
||||
public http_server::HttpHandler {
|
||||
public:
|
||||
SkyDebugger() : is_tracing_(false), app_(nullptr), handler_binding_(this) {}
|
||||
~SkyDebugger() override {}
|
||||
|
||||
private:
|
||||
// mojo::ApplicationDelegate:
|
||||
void Initialize(mojo::ApplicationImpl* app) override {
|
||||
app_ = app;
|
||||
app->ConnectToService("mojo:window_manager", &window_manager_);
|
||||
|
||||
// Format: --args-for="app_url command_port"
|
||||
if (app->args().size() < 2) {
|
||||
LOG(ERROR) << "--args-for required to specify command_port";
|
||||
mojo::ApplicationImpl::Terminate();
|
||||
return;
|
||||
}
|
||||
base::StringToUint(app->args()[1], &command_port_);
|
||||
http_server::HttpServerFactoryPtr http_server_factory;
|
||||
app->ConnectToService("mojo:http_server", &http_server_factory);
|
||||
|
||||
mojo::NetAddressPtr local_address(mojo::NetAddress::New());
|
||||
local_address->family = mojo::NET_ADDRESS_FAMILY_IPV4;
|
||||
local_address->ipv4 = mojo::NetAddressIPv4::New();
|
||||
local_address->ipv4->addr.resize(4);
|
||||
local_address->ipv4->addr[0] = 127;
|
||||
local_address->ipv4->addr[1] = 0;
|
||||
local_address->ipv4->addr[2] = 0;
|
||||
local_address->ipv4->addr[3] = 1;
|
||||
local_address->ipv4->port = command_port_;
|
||||
http_server_factory->CreateHttpServer(GetProxy(&http_server_).Pass(),
|
||||
local_address.Pass());
|
||||
|
||||
http_server::HttpHandlerPtr handler_ptr;
|
||||
handler_binding_.Bind(GetProxy(&handler_ptr).Pass());
|
||||
http_server_->SetHandler(".*", handler_ptr.Pass(),
|
||||
[](bool result) { DCHECK(result); });
|
||||
}
|
||||
|
||||
bool ConfigureIncomingConnection(
|
||||
mojo::ApplicationConnection* connection) override {
|
||||
return true;
|
||||
}
|
||||
|
||||
// http_server::HttpHandler:
|
||||
void HandleRequest(http_server::HttpRequestPtr request,
|
||||
const HandleRequestCallback& callback) override {
|
||||
// FIXME: We should use use a fancier lookup system more like what
|
||||
// services/http_server/http_server.cc does with AddHandler.
|
||||
if (request->relative_url == "/reload") {
|
||||
Load(callback, url_);
|
||||
} else if (request->relative_url == "/quit") {
|
||||
Exit();
|
||||
} else if (request->relative_url == "/load") {
|
||||
std::string url;
|
||||
mojo::common::BlockingCopyToString(request->body.Pass(), &url);
|
||||
Load(callback, url);
|
||||
} else if (request->relative_url == "/start_profiling") {
|
||||
StartProfiling(callback);
|
||||
} else if (request->relative_url == "/stop_profiling") {
|
||||
StopProfiling(callback);
|
||||
} else if (request->relative_url == "/start_tracing") {
|
||||
StartTracing(callback);
|
||||
} else if (request->relative_url == "/stop_tracing") {
|
||||
StopTracing(callback);
|
||||
} else {
|
||||
Help(callback, request->relative_url);
|
||||
}
|
||||
}
|
||||
|
||||
void Error(const HandleRequestCallback& callback, std::string message) {
|
||||
callback.Run(http_server::CreateHttpResponse(500, message));
|
||||
}
|
||||
|
||||
void Respond(const HandleRequestCallback& callback, std::string response) {
|
||||
callback.Run(http_server::CreateHttpResponse(200, response));
|
||||
}
|
||||
|
||||
void Help(const HandleRequestCallback& callback, std::string path) {
|
||||
std::string help = base::StringPrintf(
|
||||
"Sky Debugger running on port %d\n"
|
||||
"Supported URLs:\n"
|
||||
"/reload -- Reload the current page\n"
|
||||
"/quit -- Quit\n"
|
||||
"/load -- Load a new URL, url in POST body.\n",
|
||||
command_port_);
|
||||
if (path != "/")
|
||||
help = "Unknown path: " + path + "\n\n" + help;
|
||||
Respond(callback, help);
|
||||
}
|
||||
|
||||
void Load(const HandleRequestCallback& callback, std::string url) {
|
||||
url_ = url;
|
||||
Reload();
|
||||
std::string response = std::string("Loaded ") + url + "\n";
|
||||
Respond(callback, response);
|
||||
}
|
||||
|
||||
void Reload() {
|
||||
// SimpleWindowManager will wire up necessary services on our behalf.
|
||||
window_manager_->Embed(url_, nullptr, nullptr);
|
||||
}
|
||||
|
||||
void Exit() {
|
||||
// TODO(eseidel): We should orderly shutdown once mojo can.
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void StartTracing(const HandleRequestCallback& callback) {
|
||||
if (is_tracing_) {
|
||||
Error(callback, "Already tracing. Use stop_tracing to stop.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!tracing_)
|
||||
app_->ConnectToService("mojo:tracing", &tracing_);
|
||||
is_tracing_ = true;
|
||||
mojo::DataPipe pipe;
|
||||
tracing_->Start(pipe.producer_handle.Pass(), mojo::String("*"));
|
||||
trace_collector_.reset(new TraceCollector(pipe.consumer_handle.Pass()));
|
||||
Respond(callback, "Starting trace (type 'stop_tracing' to stop)\n");
|
||||
}
|
||||
|
||||
void StopTracing(const HandleRequestCallback& callback) {
|
||||
if (!is_tracing_) {
|
||||
Error(callback, "Not tracing yet. Use start_tracing to start.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
is_tracing_ = false;
|
||||
tracing_->StopAndFlush();
|
||||
trace_collector_->GetTrace(base::Bind(&SkyDebugger::OnTraceAvailable,
|
||||
base::Unretained(this), callback));
|
||||
}
|
||||
|
||||
void OnTraceAvailable(HandleRequestCallback callback, std::string trace) {
|
||||
trace_collector_.reset();
|
||||
Respond(callback, trace);
|
||||
}
|
||||
|
||||
void StartProfiling(const HandleRequestCallback& callback) {
|
||||
#if !defined(NDEBUG) || !defined(ENABLE_PROFILING)
|
||||
Error(callback,
|
||||
"Profiling requires is_debug=false and enable_profiling=true");
|
||||
return;
|
||||
#else
|
||||
base::debug::StartProfiling("sky_viewer.pprof");
|
||||
Respond(callback, "Starting profiling (stop with 'stop_profiling')");
|
||||
#endif
|
||||
}
|
||||
|
||||
void StopProfiling(const HandleRequestCallback& callback) {
|
||||
if (!base::debug::BeingProfiled()) {
|
||||
Error(callback, "Profiling not started");
|
||||
return;
|
||||
}
|
||||
base::debug::StopProfiling();
|
||||
Respond(callback, "Stopped profiling");
|
||||
}
|
||||
|
||||
bool is_tracing_;
|
||||
mojo::ApplicationImpl* app_;
|
||||
mojo::WindowManagerPtr window_manager_;
|
||||
tracing::TraceCoordinatorPtr tracing_;
|
||||
std::string url_;
|
||||
uint32_t command_port_;
|
||||
|
||||
http_server::HttpServerPtr http_server_;
|
||||
mojo::Binding<http_server::HttpHandler> handler_binding_;
|
||||
|
||||
scoped_ptr<TraceCollector> trace_collector_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(SkyDebugger);
|
||||
};
|
||||
|
||||
} // namespace debugger
|
||||
} // namespace sky
|
||||
|
||||
MojoResult MojoMain(MojoHandle application_request) {
|
||||
mojo::ApplicationRunnerChromium runner(new sky::debugger::SkyDebugger);
|
||||
runner.set_message_loop_type(base::MessageLoop::TYPE_IO);
|
||||
return runner.Run(application_request);
|
||||
}
|
||||
@ -1,45 +0,0 @@
|
||||
// Copyright 2014 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "sky/tools/debugger/trace_collector.h"
|
||||
|
||||
namespace sky {
|
||||
namespace debugger {
|
||||
|
||||
TraceCollector::TraceCollector(mojo::ScopedDataPipeConsumerHandle source)
|
||||
: drainer_(this, source.Pass()), is_complete_(false) {
|
||||
}
|
||||
|
||||
TraceCollector::~TraceCollector() {
|
||||
}
|
||||
|
||||
void TraceCollector::GetTrace(TraceCallback callback) {
|
||||
DCHECK(callback_.is_null());
|
||||
DCHECK(!callback.is_null());
|
||||
if (is_complete_) {
|
||||
callback.Run(GetTraceAsString());
|
||||
return;
|
||||
}
|
||||
callback_ = callback;
|
||||
}
|
||||
|
||||
void TraceCollector::OnDataAvailable(const void* data, size_t num_bytes) {
|
||||
DCHECK(!is_complete_);
|
||||
const char* chars = static_cast<const char*>(data);
|
||||
trace_.insert(trace_.end(), chars, chars + num_bytes);
|
||||
}
|
||||
|
||||
void TraceCollector::OnDataComplete() {
|
||||
DCHECK(!is_complete_);
|
||||
is_complete_ = true;
|
||||
if (!callback_.is_null())
|
||||
callback_.Run(GetTraceAsString());
|
||||
}
|
||||
|
||||
std::string TraceCollector::GetTraceAsString() {
|
||||
return std::string(&trace_.front(), trace_.size());
|
||||
}
|
||||
|
||||
} // namespace debugger
|
||||
} // namespace sky
|
||||
@ -1,40 +0,0 @@
|
||||
// Copyright 2014 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SKY_TOOLS_DEBUGGER_TRACE_COLLECTOR_H_
|
||||
#define SKY_TOOLS_DEBUGGER_TRACE_COLLECTOR_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "base/callback.h"
|
||||
#include "mojo/common/data_pipe_drainer.h"
|
||||
|
||||
namespace sky {
|
||||
namespace debugger {
|
||||
|
||||
class TraceCollector : public mojo::common::DataPipeDrainer::Client {
|
||||
public:
|
||||
typedef base::Callback<void(std::string)> TraceCallback;
|
||||
|
||||
explicit TraceCollector(mojo::ScopedDataPipeConsumerHandle source);
|
||||
~TraceCollector() override;
|
||||
|
||||
void GetTrace(TraceCallback callback);
|
||||
|
||||
private:
|
||||
void OnDataAvailable(const void* data, size_t num_bytes) override;
|
||||
void OnDataComplete() override;
|
||||
|
||||
std::string GetTraceAsString();
|
||||
|
||||
mojo::common::DataPipeDrainer drainer_;
|
||||
std::vector<char> trace_;
|
||||
bool is_complete_;
|
||||
TraceCallback callback_;
|
||||
};
|
||||
|
||||
} // namespace debugger
|
||||
} // namespace sky
|
||||
|
||||
#endif // SKY_TOOLS_DEBUGGER_TRACE_COLLECTOR_H_
|
||||
@ -119,8 +119,8 @@ class SkyDebugger(object):
|
||||
'--v=1',
|
||||
'--content-handlers=%s' % ','.join(content_handlers),
|
||||
'--url-mappings=mojo:window_manager=mojo:kiosk_wm',
|
||||
'--args-for=mojo:sky_debugger %d' % remote_command_port,
|
||||
'mojo:sky_debugger',
|
||||
'--args-for=mojo:debugger %d' % remote_command_port,
|
||||
'mojo:debugger',
|
||||
]
|
||||
|
||||
if args.url_or_path:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user