blob: 6ef108245705ccd27d26c0e731d91c342c7c0171 [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"
35 "This calls back into debugd using the DumpDebugLogs dbus end point.";
36
37// Returns a dynamic file name with datestamps in it.
38std::string LogName(bool compress) {
39 base::Time::Exploded now;
40 base::Time::Now().LocalExplode(&now);
41
42 return base::StringPrintf(
43 "debug-logs_%04i%02i%02i-%02i%02i%02i.%s",
44 now.year, now.month, now.day_of_month, now.hour, now.minute, now.second,
45 compress ? "tgz" : "tar");
46}
47
48} // namespace
49
50int main(int argc, char* argv[]) {
51 DEFINE_bool(compress, true, "Compress the tarball");
52 DEFINE_string(output, "", "Where to write the output");
53 brillo::FlagHelper::Init(argc, argv, kUsage);
54
55 base::FilePath output(FLAGS_output);
56 if (output.empty())
57 output = base::FilePath{"/tmp/" + LogName(FLAGS_compress)};
58
59 base::ScopedFILE fp(base::OpenFile(output, "w"));
60 if (fp == nullptr) {
61 PLOG(ERROR) << "Could not write output: " << output.value();
62 return EXIT_FAILURE;
63 }
64
65 // Set up dbus proxy for talking to debugd.
66 dbus::Bus::Options options;
67 options.bus_type = dbus::Bus::SYSTEM;
68 scoped_refptr<dbus::Bus> bus(new dbus::Bus(options));
69 CHECK(bus->Connect());
70 dbus::ObjectProxy* debugd_proxy = bus->GetObjectProxy(
71 debugd::kDebugdServiceName,
72 dbus::ObjectPath(debugd::kDebugdServicePath));
73
74 // Send request for debug logs.
75 dbus::MethodCall method_call(
76 debugd::kDebugdInterface,
77 debugd::kDumpDebugLogs);
78 dbus::MessageWriter writer(&method_call);
Mike Frysingerf8ca5322017-02-07 15:54:20 -050079 writer.AppendBool(FLAGS_compress);
Eric Caruso0b241882018-04-04 13:43:46 -070080 writer.AppendFileDescriptor(fileno(fp.get()));
Mike Frysingerf8ca5322017-02-07 15:54:20 -050081
82 // Wait for the response and process the result.
83 LOG(INFO) << "Gathering logs, please wait";
84 std::unique_ptr<dbus::Response> response(
85 debugd_proxy->CallMethodAndBlock(
86 &method_call, kDBusTimeoutMS));
87 CHECK(response) << debugd::kDumpDebugLogs << " failed";
88 LOG(INFO) << "Logs saved to " << output.value();
89
90 return EXIT_SUCCESS;
91}