Mike Frysinger | f8ca532 | 2017-02-07 15:54:20 -0500 | [diff] [blame] | 1 | // 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 Frysinger | f8ca532 | 2017-02-07 15:54:20 -0500 | [diff] [blame] | 23 | #include <dbus/message.h> |
| 24 | #include <dbus/object_proxy.h> |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | // Because the logs can be huge, we set the D-Bus timeout to 2 minutes. |
| 29 | const int kDBusTimeoutMS = 120 * 1000; |
| 30 | |
| 31 | const char kUsage[] = |
| 32 | "Developer helper tool for getting extended debug logs from the system." |
| 33 | "\n" |
| 34 | "\n" |
Brian Norris | ca4fc04 | 2018-04-03 00:24:26 -0700 | [diff] [blame^] | 35 | "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 Frysinger | f8ca532 | 2017-02-07 15:54:20 -0500 | [diff] [blame] | 41 | |
| 42 | // Returns a dynamic file name with datestamps in it. |
| 43 | std::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 | |
| 55 | int 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 Frysinger | f8ca532 | 2017-02-07 15:54:20 -0500 | [diff] [blame] | 84 | writer.AppendBool(FLAGS_compress); |
Eric Caruso | 0b24188 | 2018-04-04 13:43:46 -0700 | [diff] [blame] | 85 | writer.AppendFileDescriptor(fileno(fp.get())); |
Mike Frysinger | f8ca532 | 2017-02-07 15:54:20 -0500 | [diff] [blame] | 86 | |
| 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 | } |