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

Unified Diff: chromeos/dbus/arc_trace_agent.cc

Issue 2400163003: arc: enable Android tracing in verified-boot mode (Closed)
Patch Set: Fix according to comments Created 4 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 side-by-side diff with in-line comments
Download patch
Index: chromeos/dbus/arc_trace_agent.cc
diff --git a/chromeos/dbus/arc_trace_agent.cc b/chromeos/dbus/arc_trace_agent.cc
new file mode 100644
index 0000000000000000000000000000000000000000..98c637a561b35610e0ad4ec7f61db947129c4062
--- /dev/null
+++ b/chromeos/dbus/arc_trace_agent.cc
@@ -0,0 +1,172 @@
+// Copyright 2016 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 "chromeos/dbus/arc_trace_agent.h"
+
+#include <string>
+#include <utility>
Yusuke Sato 2017/01/04 23:55:47 unused?
Earl Ou 2017/01/16 14:05:41 Done.
+
+#include "base/callback_helpers.h"
+#include "base/logging.h"
+#include "base/memory/ptr_util.h"
+#include "base/memory/ref_counted.h"
Yusuke Sato 2017/01/04 23:55:47 remove (see my comment on the header side)
Earl Ou 2017/01/16 14:05:42 It's only used in this cc file now.
+#include "base/threading/thread_task_runner_handle.h"
+#include "chromeos/dbus/dbus_thread_manager.h"
+#include "chromeos/dbus/pipe_reader.h"
+#include "dbus/bus.h"
+#include "dbus/message.h"
+#include "dbus/object_path.h"
+#include "dbus/object_proxy.h"
+#include "third_party/cros_system_api/dbus/service_constants.h"
+
+namespace chromeos {
+
+namespace {
+
+constexpr char kArcTracingAgentName[] = "arc";
+constexpr char kArcTraceLabel[] = "ArcTraceEvents";
+
+void OnStart(const ArcTraceAgent::StartAgentTracingCallback& callback,
+ dbus::Response* response) {
+ if (!response)
+ LOG(ERROR) << "Failed to request start";
+ // Use PostTask as the convention of TraceAgent. The caller expects callback
+ // to be called after StartAgentTracing() returns.
+ base::ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE,
+ base::Bind(callback, kArcTracingAgentName, response != nullptr));
+}
+
+// A wrapper for controlling system tracing on Chrome OS.
+class ArcTraceAgentImpl : public ArcTraceAgent {
+ public:
+ ArcTraceAgentImpl() : debugdaemon_proxy_(nullptr), weak_ptr_factory_(this) {}
+
+ ~ArcTraceAgentImpl() override = default;
+
+ // base::trace_event::TracingAgent overrides:
+ std::string GetTracingAgentName() override { return kArcTracingAgentName; }
+
+ std::string GetTraceEventLabel() override { return kArcTraceLabel; }
+
+ void StartAgentTracing(const base::trace_event::TraceConfig& trace_config,
+ const StartAgentTracingCallback& callback) override {
+ // delegate_ may be nullptr if ARC is not enabled on the system. In such
Yusuke Sato 2017/01/04 23:55:47 Apparently delegate_ is not initialized in the cto
Earl Ou 2017/01/16 14:05:42 Done.
+ // case, simply do nothing.
+ if (!delegate_) {
+ // Use PostTask as the convention of TraceAgent. The caller expects
+ // callback to be called after this function returns.
+ base::ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE, base::Bind(callback, GetTracingAgentName(), false));
+ return;
+ }
+
+ delegate_->StartTracing(trace_config);
+
+ dbus::MethodCall method_call(debugd::kDebugdInterface, "ArcTraceStart");
+ debugdaemon_proxy_->CallMethod(&method_call,
+ dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
+ base::Bind(&OnStart, callback));
+ }
+
+ void StopAgentTracing(const StopAgentTracingCallback& callback) override {
+ DCHECK(stop_agent_tracing_task_runner_);
+
+ if (!stop_callback_.is_null()) {
+ LOG(ERROR) << "Previous StopAgentTracing() invocation is still pending.";
+ return;
+ }
+
+ if (!delegate_) {
+ std::string no_data;
+ callback.Run(GetTracingAgentName(), GetTraceEventLabel(),
+ base::RefCountedString::TakeString(&no_data));
+ return;
+ }
+
+ stop_callback_ = callback;
Yusuke Sato 2017/01/04 23:55:47 Can't we pass the |callback| directly to OnIOCompl
Earl Ou 2017/01/16 14:05:41 Done.
+
+ delegate_->StopTracing();
+
+ pipe_reader_ = base::MakeUnique<PipeReaderForString>(
+ stop_agent_tracing_task_runner_,
+ base::Bind(&ArcTraceAgentImpl::OnIOComplete,
+ weak_ptr_factory_.GetWeakPtr()));
+
+ // PIPE reader will run on the given thread
+ // stop_agent_tracing_task_runner_, which should be an I/O thread given by
+ // the caller.
+ base::ScopedFD pipe_write_end = pipe_reader_->StartIO();
+ DCHECK(pipe_write_end.is_valid());
+ // Issue the dbus request to stop system tracing
+ dbus::MethodCall method_call(debugd::kDebugdInterface, "ArcTraceStop");
+ dbus::MessageWriter writer(&method_call);
+ writer.AppendFileDescriptor(pipe_write_end.get());
+
+ debugdaemon_proxy_->CallMethod(
+ &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
+ base::Bind(&ArcTraceAgentImpl::OnStopAgentTracing,
+ weak_ptr_factory_.GetWeakPtr()));
+ }
+
+ // ArcTraceAgent overrides:
+ void SetDelegate(Delegate* delegate) override { delegate_ = delegate; }
+
+ void SetStopAgentTracingTaskRunner(
+ scoped_refptr<base::TaskRunner> task_runner) override {
+ stop_agent_tracing_task_runner_ = task_runner;
+ }
+
+ protected:
+ // DBusClient overrides:
+ void Init(dbus::Bus* bus) override {
+ debugdaemon_proxy_ =
+ bus->GetObjectProxy(debugd::kDebugdServiceName,
+ dbus::ObjectPath(debugd::kDebugdServicePath));
+ }
+
+ private:
+ // Called when pipe I/O completes; pass data on and delete the instance.
+ void OnIOComplete() {
+ std::string pipe_data;
+ pipe_reader_->GetData(&pipe_data);
+ pipe_reader_.reset();
+ base::ResetAndReturn(&stop_callback_)
+ .Run(kArcTracingAgentName, kArcTraceLabel,
+ base::RefCountedString::TakeString(&pipe_data));
+ }
+
+ void OnStopAgentTracing(dbus::Response* response) {
+ if (!response) {
+ LOG(ERROR) << "Failed to request arc stop";
+ // If debugd crashes or completes I/O before this message is processed
+ // then pipe_reader_ can be nullptr, see OnIOComplete().
+ if (pipe_reader_.get())
+ pipe_reader_->OnDataReady(-1); // terminate data stream
+ }
+ // NB: requester is signaled when I/O completes
+ }
+
+ Delegate* delegate_; // Owned by ArcServiceLauncher.
+ dbus::ObjectProxy* debugdaemon_proxy_; // Owned by dbus::Bus.
+ std::unique_ptr<PipeReaderForString> pipe_reader_;
+ scoped_refptr<base::TaskRunner> stop_agent_tracing_task_runner_;
+ StopAgentTracingCallback stop_callback_;
+ base::WeakPtrFactory<ArcTraceAgentImpl> weak_ptr_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(ArcTraceAgentImpl);
+};
+
+} // namespace
+
+// static
+ArcTraceAgent* ArcTraceAgent::Create() {
+ return new ArcTraceAgentImpl();
+}
+
+ArcTraceAgent::ArcTraceAgent() = default;
+
+ArcTraceAgent::~ArcTraceAgent() = default;
+
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698