blob: 9c30152f56e4d1825c95998808e3c7dc3631923d [file] [log] [blame]
Mike Frysingerf8ca5322017-02-07 15:54:20 -05001// Copyright 2017 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file is meant for debugging use to manually trigger collection of
6// debug logs. Normally this can be done with dbus-send but dbus-send does
7// not support passing file descriptors.
8
9#include <stdio.h>
10#include <stdlib.h>
11
12#include <memory>
13
14#include <base/files/file_path.h>
15#include <base/files/file_util.h>
16#include <base/files/scoped_file.h>
17#include <base/logging.h>
18#include <base/strings/stringprintf.h>
19#include <base/time/time.h>
20#include <brillo/flag_helper.h>
21#include <chromeos/dbus/service_constants.h>
22#include <dbus/bus.h>
23#include <dbus/file_descriptor.h>
24#include <dbus/message.h>
25#include <dbus/object_proxy.h>
26
27namespace {
28
29// Because the logs can be huge, we set the D-Bus timeout to 2 minutes.
30const int kDBusTimeoutMS = 120 * 1000;
31
32const char kUsage[] =
33 "Developer helper tool for getting extended debug logs from the system."
34 "\n"
35 "\n"
36 "This calls back into debugd using the DumpDebugLogs dbus end point.";
37
38// Returns a dynamic file name with datestamps in it.
39std::string LogName(bool compress) {
40 base::Time::Exploded now;
41 base::Time::Now().LocalExplode(&now);
42
43 return base::StringPrintf(
44 "debug-logs_%04i%02i%02i-%02i%02i%02i.%s",
45 now.year, now.month, now.day_of_month, now.hour, now.minute, now.second,
46 compress ? "tgz" : "tar");
47}
48
49} // namespace
50
51int main(int argc, char* argv[]) {
52 DEFINE_bool(compress, true, "Compress the tarball");
53 DEFINE_string(output, "", "Where to write the output");
54 brillo::FlagHelper::Init(argc, argv, kUsage);
55
56 base::FilePath output(FLAGS_output);
57 if (output.empty())
58 output = base::FilePath{"/tmp/" + LogName(FLAGS_compress)};
59
60 base::ScopedFILE fp(base::OpenFile(output, "w"));
61 if (fp == nullptr) {
62 PLOG(ERROR) << "Could not write output: " << output.value();
63 return EXIT_FAILURE;
64 }
65
66 // Set up dbus proxy for talking to debugd.
67 dbus::Bus::Options options;
68 options.bus_type = dbus::Bus::SYSTEM;
69 scoped_refptr<dbus::Bus> bus(new dbus::Bus(options));
70 CHECK(bus->Connect());
71 dbus::ObjectProxy* debugd_proxy = bus->GetObjectProxy(
72 debugd::kDebugdServiceName,
73 dbus::ObjectPath(debugd::kDebugdServicePath));
74
75 // Send request for debug logs.
76 dbus::MethodCall method_call(
77 debugd::kDebugdInterface,
78 debugd::kDumpDebugLogs);
79 dbus::MessageWriter writer(&method_call);
80 dbus::FileDescriptor stdout_fd(fileno(fp.get()));
81 stdout_fd.CheckValidity();
82 writer.AppendBool(FLAGS_compress);
83 writer.AppendFileDescriptor(stdout_fd);
84
85 // Wait for the response and process the result.
86 LOG(INFO) << "Gathering logs, please wait";
87 std::unique_ptr<dbus::Response> response(
88 debugd_proxy->CallMethodAndBlock(
89 &method_call, kDBusTimeoutMS));
90 CHECK(response) << debugd::kDumpDebugLogs << " failed";
91 LOG(INFO) << "Logs saved to " << output.value();
92
93 return EXIT_SUCCESS;
94}