| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chromeos/dbus/debug_daemon_client.h" | 5 #include "chromeos/dbus/debug_daemon_client.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| 11 |
| 12 #include <memory> |
| 11 #include <string> | 13 #include <string> |
| 12 #include <vector> | 14 #include <vector> |
| 13 | 15 |
| 14 #include "base/bind.h" | 16 #include "base/bind.h" |
| 15 #include "base/bind_helpers.h" | 17 #include "base/bind_helpers.h" |
| 16 #include "base/files/file_path.h" | 18 #include "base/files/file_path.h" |
| 17 #include "base/json/json_string_value_serializer.h" | 19 #include "base/json/json_string_value_serializer.h" |
| 18 #include "base/location.h" | 20 #include "base/location.h" |
| 19 #include "base/macros.h" | 21 #include "base/macros.h" |
| 20 #include "base/message_loop/message_loop.h" | 22 #include "base/message_loop/message_loop.h" |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 std::string GetTracingAgentName() override { return kCrOSTracingAgentName; } | 310 std::string GetTracingAgentName() override { return kCrOSTracingAgentName; } |
| 309 | 311 |
| 310 std::string GetTraceEventLabel() override { return kCrOSTraceLabel; } | 312 std::string GetTraceEventLabel() override { return kCrOSTraceLabel; } |
| 311 | 313 |
| 312 void StartAgentTracing(const base::trace_event::TraceConfig& trace_config, | 314 void StartAgentTracing(const base::trace_event::TraceConfig& trace_config, |
| 313 const StartAgentTracingCallback& callback) override { | 315 const StartAgentTracingCallback& callback) override { |
| 314 dbus::MethodCall method_call( | 316 dbus::MethodCall method_call( |
| 315 debugd::kDebugdInterface, | 317 debugd::kDebugdInterface, |
| 316 debugd::kSystraceStart); | 318 debugd::kSystraceStart); |
| 317 dbus::MessageWriter writer(&method_call); | 319 dbus::MessageWriter writer(&method_call); |
| 318 writer.AppendString("all"); // TODO(sleffler) parameterize category list | 320 writer.AppendString("all"); // TODO(sleffler) parameterize category list |
| 319 | 321 |
| 320 DVLOG(1) << "Requesting a systrace start"; | 322 DVLOG(1) << "Requesting a systrace start"; |
| 321 debugdaemon_proxy_->CallMethod( | 323 debugdaemon_proxy_->CallMethod( |
| 322 &method_call, | 324 &method_call, |
| 323 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 325 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 324 base::Bind(&DebugDaemonClientImpl::OnStartMethod, | 326 base::Bind(&DebugDaemonClientImpl::OnStartMethod, |
| 325 weak_ptr_factory_.GetWeakPtr())); | 327 weak_ptr_factory_.GetWeakPtr())); |
| 326 | 328 |
| 327 base::ThreadTaskRunnerHandle::Get()->PostTask( | 329 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 328 FROM_HERE, | 330 FROM_HERE, |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 base::Bind(&DebugDaemonClientImpl::OnRemoveRootfsVerification, | 455 base::Bind(&DebugDaemonClientImpl::OnRemoveRootfsVerification, |
| 454 weak_ptr_factory_.GetWeakPtr(), | 456 weak_ptr_factory_.GetWeakPtr(), |
| 455 callback)); | 457 callback)); |
| 456 } | 458 } |
| 457 | 459 |
| 458 void WaitForServiceToBeAvailable( | 460 void WaitForServiceToBeAvailable( |
| 459 const WaitForServiceToBeAvailableCallback& callback) override { | 461 const WaitForServiceToBeAvailableCallback& callback) override { |
| 460 debugdaemon_proxy_->WaitForServiceToBeAvailable(callback); | 462 debugdaemon_proxy_->WaitForServiceToBeAvailable(callback); |
| 461 } | 463 } |
| 462 | 464 |
| 465 void SetOomScoreAdj(const std::map<int, int>& pid_to_oom_score_adj, |
| 466 const SetOomScoreAdjCallback& callback) override { |
| 467 dbus::MethodCall method_call(debugd::kDebugdInterface, |
| 468 debugd::kSetOomScoreAdj); |
| 469 dbus::MessageWriter writer(&method_call); |
| 470 |
| 471 dbus::MessageWriter sub_writer(nullptr); |
| 472 writer.OpenArray("{ii}", &sub_writer); |
| 473 |
| 474 dbus::MessageWriter elem_writer(nullptr); |
| 475 for (const auto& entry : pid_to_oom_score_adj) { |
| 476 sub_writer.OpenDictEntry(&elem_writer); |
| 477 elem_writer.AppendInt32(entry.first); |
| 478 elem_writer.AppendInt32(entry.second); |
| 479 sub_writer.CloseContainer(&elem_writer); |
| 480 } |
| 481 writer.CloseContainer(&sub_writer); |
| 482 |
| 483 debugdaemon_proxy_->CallMethod( |
| 484 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 485 base::Bind(&DebugDaemonClientImpl::OnSetOomScoreAdj, |
| 486 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 487 } |
| 488 |
| 463 protected: | 489 protected: |
| 464 void Init(dbus::Bus* bus) override { | 490 void Init(dbus::Bus* bus) override { |
| 465 debugdaemon_proxy_ = | 491 debugdaemon_proxy_ = |
| 466 bus->GetObjectProxy(debugd::kDebugdServiceName, | 492 bus->GetObjectProxy(debugd::kDebugdServiceName, |
| 467 dbus::ObjectPath(debugd::kDebugdServicePath)); | 493 dbus::ObjectPath(debugd::kDebugdServicePath)); |
| 468 } | 494 } |
| 469 | 495 |
| 470 private: | 496 private: |
| 471 // Called when a CheckValidity response is received. | 497 // Called when a CheckValidity response is received. |
| 472 void OnCheckValidityGetDebugLogs(bool is_compressed, | 498 void OnCheckValidityGetDebugLogs(bool is_compressed, |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 std::string status; | 583 std::string status; |
| 558 if (response && dbus::MessageReader(response).PopString(&status)) | 584 if (response && dbus::MessageReader(response).PopString(&status)) |
| 559 callback.Run(true, status); | 585 callback.Run(true, status); |
| 560 else | 586 else |
| 561 callback.Run(false, ""); | 587 callback.Run(false, ""); |
| 562 } | 588 } |
| 563 | 589 |
| 564 void OnGetAllLogs(const GetLogsCallback& callback, | 590 void OnGetAllLogs(const GetLogsCallback& callback, |
| 565 dbus::Response* response) { | 591 dbus::Response* response) { |
| 566 std::map<std::string, std::string> logs; | 592 std::map<std::string, std::string> logs; |
| 567 bool broken = false; // did we see a broken (k,v) pair? | 593 bool broken = false; // did we see a broken (k,v) pair? |
| 568 dbus::MessageReader sub_reader(NULL); | 594 dbus::MessageReader sub_reader(NULL); |
| 569 if (!response || !dbus::MessageReader(response).PopArray(&sub_reader)) { | 595 if (!response || !dbus::MessageReader(response).PopArray(&sub_reader)) { |
| 570 callback.Run(false, logs); | 596 callback.Run(false, logs); |
| 571 return; | 597 return; |
| 572 } | 598 } |
| 573 while (sub_reader.HasMoreData()) { | 599 while (sub_reader.HasMoreData()) { |
| 574 dbus::MessageReader sub_sub_reader(NULL); | 600 dbus::MessageReader sub_sub_reader(NULL); |
| 575 std::string key, value; | 601 std::string key, value; |
| 576 if (!sub_reader.PopDictEntry(&sub_sub_reader) | 602 if (!sub_reader.PopDictEntry(&sub_sub_reader) |
| 577 || !sub_sub_reader.PopString(&key) | 603 || !sub_sub_reader.PopString(&key) |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 691 weak_ptr_factory_.GetWeakPtr())); | 717 weak_ptr_factory_.GetWeakPtr())); |
| 692 } | 718 } |
| 693 | 719 |
| 694 // Called when a response for StopAgentTracing() is received. | 720 // Called when a response for StopAgentTracing() is received. |
| 695 void OnStopAgentTracing(dbus::Response* response) { | 721 void OnStopAgentTracing(dbus::Response* response) { |
| 696 if (!response) { | 722 if (!response) { |
| 697 LOG(ERROR) << "Failed to request systrace stop"; | 723 LOG(ERROR) << "Failed to request systrace stop"; |
| 698 // If debugd crashes or completes I/O before this message is processed | 724 // If debugd crashes or completes I/O before this message is processed |
| 699 // then pipe_reader_ can be NULL, see OnIOComplete(). | 725 // then pipe_reader_ can be NULL, see OnIOComplete(). |
| 700 if (pipe_reader_.get()) | 726 if (pipe_reader_.get()) |
| 701 pipe_reader_->OnDataReady(-1); // terminate data stream | 727 pipe_reader_->OnDataReady(-1); // terminate data stream |
| 702 } | 728 } |
| 703 // NB: requester is signaled when i/o completes | 729 // NB: requester is signaled when i/o completes |
| 704 } | 730 } |
| 705 | 731 |
| 706 void OnTestICMP(const TestICMPCallback& callback, dbus::Response* response) { | 732 void OnTestICMP(const TestICMPCallback& callback, dbus::Response* response) { |
| 707 std::string status; | 733 std::string status; |
| 708 if (response && dbus::MessageReader(response).PopString(&status)) | 734 if (response && dbus::MessageReader(response).PopString(&status)) |
| 709 callback.Run(true, status); | 735 callback.Run(true, status); |
| 710 else | 736 else |
| 711 callback.Run(false, ""); | 737 callback.Run(false, ""); |
| 712 } | 738 } |
| 713 | 739 |
| 714 // Called when pipe i/o completes; pass data on and delete the instance. | 740 // Called when pipe i/o completes; pass data on and delete the instance. |
| 715 void OnIOComplete() { | 741 void OnIOComplete() { |
| 716 std::string pipe_data; | 742 std::string pipe_data; |
| 717 pipe_reader_->GetData(&pipe_data); | 743 pipe_reader_->GetData(&pipe_data); |
| 718 callback_.Run(GetTracingAgentName(), GetTraceEventLabel(), | 744 callback_.Run(GetTracingAgentName(), GetTraceEventLabel(), |
| 719 base::RefCountedString::TakeString(&pipe_data)); | 745 base::RefCountedString::TakeString(&pipe_data)); |
| 720 pipe_reader_.reset(); | 746 pipe_reader_.reset(); |
| 721 } | 747 } |
| 722 | 748 |
| 749 void OnSetOomScoreAdj(const SetOomScoreAdjCallback& callback, |
| 750 dbus::Response* response) { |
| 751 std::string output; |
| 752 if (response && dbus::MessageReader(response).PopString(&output)) |
| 753 callback.Run(true, output); |
| 754 else |
| 755 callback.Run(false, ""); |
| 756 } |
| 757 |
| 723 dbus::ObjectProxy* debugdaemon_proxy_; | 758 dbus::ObjectProxy* debugdaemon_proxy_; |
| 724 std::unique_ptr<PipeReaderForString> pipe_reader_; | 759 std::unique_ptr<PipeReaderForString> pipe_reader_; |
| 725 StopAgentTracingCallback callback_; | 760 StopAgentTracingCallback callback_; |
| 726 scoped_refptr<base::TaskRunner> stop_agent_tracing_task_runner_; | 761 scoped_refptr<base::TaskRunner> stop_agent_tracing_task_runner_; |
| 727 base::WeakPtrFactory<DebugDaemonClientImpl> weak_ptr_factory_; | 762 base::WeakPtrFactory<DebugDaemonClientImpl> weak_ptr_factory_; |
| 728 | 763 |
| 729 DISALLOW_COPY_AND_ASSIGN(DebugDaemonClientImpl); | 764 DISALLOW_COPY_AND_ASSIGN(DebugDaemonClientImpl); |
| 730 }; | 765 }; |
| 731 | 766 |
| 732 DebugDaemonClient::DebugDaemonClient() { | 767 DebugDaemonClient::DebugDaemonClient() { |
| 733 } | 768 } |
| 734 | 769 |
| 735 DebugDaemonClient::~DebugDaemonClient() { | 770 DebugDaemonClient::~DebugDaemonClient() { |
| 736 } | 771 } |
| 737 | 772 |
| 738 // static | 773 // static |
| 739 DebugDaemonClient::StopAgentTracingCallback | 774 DebugDaemonClient::StopAgentTracingCallback |
| 740 DebugDaemonClient::EmptyStopAgentTracingCallback() { | 775 DebugDaemonClient::EmptyStopAgentTracingCallback() { |
| 741 return base::Bind(&EmptyStopAgentTracingCallbackBody); | 776 return base::Bind(&EmptyStopAgentTracingCallbackBody); |
| 742 } | 777 } |
| 743 | 778 |
| 744 // static | 779 // static |
| 745 DebugDaemonClient* DebugDaemonClient::Create() { | 780 DebugDaemonClient* DebugDaemonClient::Create() { |
| 746 return new DebugDaemonClientImpl(); | 781 return new DebugDaemonClientImpl(); |
| 747 } | 782 } |
| 748 | 783 |
| 749 } // namespace chromeos | 784 } // namespace chromeos |
| OLD | NEW |