blob: 77ab67bd6deae5fd90c892da7685fae5822fe415 [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>
Mike Frysingerf8ca5322017-02-07 15:54:20 -050023#include <dbus/message.h>
24#include <dbus/object_proxy.h>
25
26namespace {
27
28// Because the logs can be huge, we set the D-Bus timeout to 2 minutes.
29const int kDBusTimeoutMS = 120 * 1000;
30
31const char kUsage[] =
32 "Developer helper tool for getting extended debug logs from the system."
33 "\n"
34 "\n"
Brian Norrisca4fc042018-04-03 00:24:26 -070035 "This calls back into debugd using the DumpDebugLogs dbus end point."
36 "\n"
37 "\n"
38 "WARNING: The exact contents of the generated output may vary depending on"
39 "\n"
40 "developers' whims.";
Mike Frysingerf8ca5322017-02-07 15:54:20 -050041
42// Returns a dynamic file name with datestamps in it.
43std::string LogName(bool compress) {
44 base::Time::Exploded now;
45 base::Time::Now().LocalExplode(&now);
46
47 return base::StringPrintf(
48 "debug-logs_%04i%02i%02i-%02i%02i%02i.%s",
49 now.year, now.month, now.day_of_month, now.hour, now.minute, now.second,
50 compress ? "tgz" : "tar");
51}
52
53} // namespace
54
55int main(int argc, char* argv[]) {
56 DEFINE_bool(compress, true, "Compress the tarball");
57 DEFINE_string(output, "", "Where to write the output");
58 brillo::FlagHelper::Init(argc, argv, kUsage);
59
60 base::FilePath output(FLAGS_output);
61 if (output.empty())
62 output = base::FilePath{"/tmp/" + LogName(FLAGS_compress)};
63
64 base::ScopedFILE fp(base::OpenFile(output, "w"));
65 if (fp == nullptr) {
66 PLOG(ERROR) << "Could not write output: " << output.value();
67 return EXIT_FAILURE;
68 }
69
70 // Set up dbus proxy for talking to debugd.
71 dbus::Bus::Options options;
72 options.bus_type = dbus::Bus::SYSTEM;
73 scoped_refptr<dbus::Bus> bus(new dbus::Bus(options));
74 CHECK(bus->Connect());
75 dbus::ObjectProxy* debugd_proxy = bus->GetObjectProxy(
76 debugd::kDebugdServiceName,
77 dbus::ObjectPath(debugd::kDebugdServicePath));
78
79 // Send request for debug logs.
80 dbus::MethodCall method_call(
81 debugd::kDebugdInterface,
82 debugd::kDumpDebugLogs);
83 dbus::MessageWriter writer(&method_call);
Mike Frysingerf8ca5322017-02-07 15:54:20 -050084 writer.AppendBool(FLAGS_compress);
Eric Caruso0b241882018-04-04 13:43:46 -070085 writer.AppendFileDescriptor(fileno(fp.get()));
Mike Frysingerf8ca5322017-02-07 15:54:20 -050086
87 // Wait for the response and process the result.
88 LOG(INFO) << "Gathering logs, please wait";
89 std::unique_ptr<dbus::Response> response(
90 debugd_proxy->CallMethodAndBlock(
91 &method_call, kDBusTimeoutMS));
92 CHECK(response) << debugd::kDumpDebugLogs << " failed";
93 LOG(INFO) << "Logs saved to " << output.value();
94
95 return EXIT_SUCCESS;
96}