blob: a58806f812ee2f16ef03f30b3d83b0bebffa76c5 [file] [log] [blame]
Elly Jones03cd6d72012-06-11 13:04:28 -04001// Copyright (c) 2012 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
Alex Vakulenko262be3f2014-07-30 15:25:50 -07005#include "debugd/src/log_tool.h"
Elly Jones03cd6d72012-06-11 13:04:28 -04006
Fletcher Woodruff07c28532019-01-24 11:08:53 -07007#include <grp.h>
8#include <inttypes.h>
Chinglin Yuaeb4ec72018-12-10 18:53:30 +08009#include <lzma.h>
Fletcher Woodruff07c28532019-01-24 11:08:53 -070010#include <pwd.h>
11#include <stdint.h>
12#include <sys/types.h>
13#include <unistd.h>
Ben Chan8e9f6d02017-09-26 23:04:21 -070014#include <memory>
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -070015#include <string>
Fletcher Woodruff07c28532019-01-24 11:08:53 -070016#include <utility>
Ben Chana0011d82014-05-13 00:19:29 -070017#include <vector>
18
Ben Chanab93abf2017-01-24 13:32:51 -080019#include <base/base64.h>
Fletcher Woodruff07c28532019-01-24 11:08:53 -070020#include <base/files/file.h>
21#include <base/files/file_path.h>
Ben Chancd8fda42014-09-05 08:21:06 -070022#include <base/files/file_util.h>
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080023#include <base/json/json_writer.h>
Elly Jones03cd6d72012-06-11 13:04:28 -040024#include <base/logging.h>
Ben Chan9953a592014-02-05 23:32:00 -080025#include <base/strings/string_split.h>
26#include <base/strings/string_util.h>
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -070027#include <base/strings/stringprintf.h>
28#include <base/strings/utf_string_conversion_utils.h>
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080029#include <base/values.h>
Elly Jones03cd6d72012-06-11 13:04:28 -040030
Rebecca Silbersteine78af402014-10-02 10:55:04 -070031#include <chromeos/dbus/service_constants.h>
Eric Carusocc7106c2017-04-27 14:22:42 -070032#include <shill/dbus-proxies.h>
Rebecca Silbersteine78af402014-10-02 10:55:04 -070033
Ben Chanaf125862017-02-08 23:11:18 -080034#include "debugd/src/constants.h"
Chinglin Yuaeb4ec72018-12-10 18:53:30 +080035#include "debugd/src/perf_tool.h"
Alex Vakulenko262be3f2014-07-30 15:25:50 -070036#include "debugd/src/process_with_output.h"
Elly Jones03cd6d72012-06-11 13:04:28 -040037
Kartik Hegde1c4b97b2018-09-09 19:09:34 -060038#include "brillo/key_value_store.h"
39#include <brillo/osrelease_reader.h>
mhasank86c46c72020-08-13 15:36:29 -070040#include <brillo/cryptohome.h>
Kartik Hegde1c4b97b2018-09-09 19:09:34 -060041
Elly Jones03cd6d72012-06-11 13:04:28 -040042namespace debugd {
43
Elly Jones03cd6d72012-06-11 13:04:28 -040044using std::string;
Elly Jones03cd6d72012-06-11 13:04:28 -040045
Eric Caruso96d03d32017-04-25 18:01:17 -070046using Strings = std::vector<string>;
Elly Jones03cd6d72012-06-11 13:04:28 -040047
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080048namespace {
49
Ben Chanaf125862017-02-08 23:11:18 -080050const char kRoot[] = "root";
51const char kShell[] = "/bin/sh";
Kartik Hegde1c4b97b2018-09-09 19:09:34 -060052constexpr char kLsbReleasePath[] = "/etc/lsb-release";
mhasank80cbe4d2020-04-02 22:46:08 -070053constexpr char kArcBugReportBackupFileName[] = "arc-bugreport.log";
mhasankd2b84882020-05-04 17:02:19 -070054constexpr char kArcBugReportBackupKey[] = "arc-bugreport-backup";
mhasank80cbe4d2020-04-02 22:46:08 -070055constexpr char kDaemonStoreBaseDir[] = "/run/daemon-store/debugd/";
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080056
57// Minimum time in seconds needed to allow shill to test active connections.
58const int kConnectionTesterTimeoutSeconds = 5;
Ben Chanf6cd93a2012-10-14 19:37:00 -070059
Chinglin Yuaeb4ec72018-12-10 18:53:30 +080060// Default running perf for 2 seconds.
61constexpr const int kPerfDurationSecs = 2;
Chinglin Yu3c8d0a22019-02-20 11:32:52 +080062// TODO(chinglinyu) Remove after crbug/934702 is fixed.
63// The following description is added to 'perf-data' as a temporary solution
64// before the update of feedback disclosure to users is done in crbug/934702.
65constexpr const char kPerfDataDescription[] =
66 "perf-data contains performance profiling information about how much time "
67 "the system spends on various activities (program execution stack traces). "
68 "This might reveal some information about what system features and "
69 "resources are being used. The full detail of perf-data can be found in "
70 "the PerfDataProto protocol buffer message type in the chromium source "
71 "repository.\n";
Chinglin Yuaeb4ec72018-12-10 18:53:30 +080072
Eric Carusoa879fd92017-10-11 12:57:10 -070073#define CMD_KERNEL_MODULE_PARAMS(module_name) \
Tom Hughesd6c2d392020-08-24 18:12:11 -070074 "cd /sys/module/" #module_name "/parameters 2>/dev/null && grep -sH ^ *"
Eric Carusoa879fd92017-10-11 12:57:10 -070075
Fletcher Woodruff07c28532019-01-24 11:08:53 -070076using Log = LogTool::Log;
77constexpr Log::LogType kCommand = Log::kCommand;
78constexpr Log::LogType kFile = Log::kFile;
mhasankaf5251d2020-04-29 18:53:03 -070079
80class ArcBugReportLog : public LogTool::Log {
81 public:
82 ArcBugReportLog()
83 : Log(kCommand,
84 "arc-bugreport",
85 "/usr/bin/nsenter -t1 -m /usr/sbin/android-sh -c "
86 "/system/bin/arc-bugreport",
87 kRoot,
88 kRoot,
89 10 * 1024 * 1024 /*10 MiB*/,
90 LogTool::Encoding::kUtf8) {}
91
92 virtual ~ArcBugReportLog() = default;
93};
mhasank80cbe4d2020-04-02 22:46:08 -070094
Miriam Zimmermand91d8e72019-06-27 12:24:04 -070095// NOTE: IF YOU ADD AN ENTRY TO THIS LIST, PLEASE:
96// * add a row to http://go/cros-feedback-audit and fill it out
Miriam Zimmerman4f142ba2020-06-01 14:15:21 -070097// * email cros-telemetry@
Miriam Zimmermand91d8e72019-06-27 12:24:04 -070098// (Eventually we'll have a better process, but for now please do this.)
Tom Hughesd6c2d392020-08-24 18:12:11 -070099// clang-format off
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700100const std::vector<Log> kCommandLogs {
Mike Frysingerb0350992018-09-14 13:45:35 -0400101 // We need to enter init's mount namespace because it has /home/chronos
102 // mounted which is where the consent knob lives. We don't have that mount
103 // in our own mount namespace (by design). https://crbug.com/884249
Chris Morin853d3442019-04-01 21:35:13 -0700104 {kCommand, "CLIENT_ID", "/usr/bin/nsenter -t1 -m /usr/bin/metrics_client -i",
105 kRoot, kDebugfsGroup},
106 {kCommand, "LOGDATE", "/bin/date"},
Yusuke Sato27a31672019-04-29 15:26:37 -0700107 // We need to enter init's mount namespace to access /home/root. Also, we use
108 // neither ARC container's mount namespace (with android-sh) nor
109 // /opt/google/containers/android/rootfs/android-data/ so that we can get
110 // results even when the container is down.
111 {kCommand, "android_app_storage", "/usr/bin/nsenter -t1 -m "
112 "/bin/sh -c \"/usr/bin/du -h /home/root/*/android-data/data/\"",
113 kRoot, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700114 {kFile, "atrus_logs", "/var/log/atrus.log"},
115 {kFile, "authpolicy", "/var/log/authpolicy.log"},
Brian Norrisafc9f632019-05-09 14:08:28 -0700116 {kCommand, "bootstat_summary", "/usr/bin/bootstat_summary",
117 SandboxedProcess::kDefaultUser, SandboxedProcess::kDefaultGroup,
118 Log::kDefaultMaxBytes, LogTool::Encoding::kAutodetect, true},
Craig Hesling5c384b52019-04-20 15:18:06 -0700119 {kFile, "bio_crypto_init.LATEST",
120 "/var/log/bio_crypto_init/bio_crypto_init.LATEST"},
121 {kFile, "bio_crypto_init.PREVIOUS",
122 "/var/log/bio_crypto_init/bio_crypto_init.PREVIOUS"},
Chris Morin853d3442019-04-01 21:35:13 -0700123 {kFile, "biod.LATEST", "/var/log/biod/biod.LATEST"},
124 {kFile, "biod.PREVIOUS", "/var/log/biod/biod.PREVIOUS"},
Craig Hesling4c3891e2019-04-20 12:53:54 -0700125 {kFile, "bio_fw_updater.LATEST", "/var/log/biod/bio_fw_updater.LATEST"},
126 {kFile, "bio_fw_updater.PREVIOUS", "/var/log/biod/bio_fw_updater.PREVIOUS"},
Chris Morin853d3442019-04-01 21:35:13 -0700127 {kFile, "bios_info", "/var/log/bios_info.txt"},
128 {kCommand, "bios_log", "cat /sys/firmware/log "
129 "/proc/device-tree/chosen/ap-console-buffer 2>/dev/null"},
130 {kFile, "bios_times", "/var/log/bios_times.txt"},
Anand K Mistryccceb1e2020-01-16 14:00:49 +1100131 // Slow or non-responsive block devices could cause this command to stall. Use
132 // a timeout to prevent this command from blocking log fetching. This command
133 // is expected to take O(100ms) in the normal case.
134 {kCommand, "blkid", "timeout -s KILL 5s /sbin/blkid", kRoot, kRoot},
Chris Morin853d3442019-04-01 21:35:13 -0700135 {kFile, "buddyinfo", "/proc/buddyinfo"},
136 {kCommand, "cbi_info", "/usr/share/userfeedback/scripts/cbi_info", kRoot,
137 kRoot},
138 {kFile, "cheets_log", "/var/log/arc.log"},
139 {kFile, "clobber.log", "/var/log/clobber.log"},
140 {kFile, "clobber-state.log", "/var/log/clobber-state.log"},
Sonny Raobd3dc002020-05-27 21:40:35 -0700141 {kCommand, "chromeos-pgmem", "/usr/bin/chromeos-pgmem", kRoot, kRoot},
Chris Morin853d3442019-04-01 21:35:13 -0700142 {kFile, "chrome_system_log", "/var/log/chrome/chrome"},
143 {kFile, "chrome_system_log.PREVIOUS", "/var/log/chrome/chrome.PREVIOUS"},
Mike Frysinger32cdf3e2017-08-14 18:17:06 -0400144 // There might be more than one record, so grab them all.
145 // Plus, for <linux-3.19, it's named "console-ramoops", but for newer
146 // versions, it's named "console-ramoops-#".
Chris Morin853d3442019-04-01 21:35:13 -0700147 {kCommand, "console-ramoops",
148 "cat /sys/fs/pstore/console-ramoops* 2>/dev/null"},
149 {kFile, "cpuinfo", "/proc/cpuinfo"},
150 {kFile, "cr50_version", "/var/cache/cr50-version"},
Nicolas Boichatf3dd82d2020-09-07 15:11:15 +0800151 {kFile, "cros_ec.log", "/var/log/cros_ec.log",
152 SandboxedProcess::kDefaultUser, SandboxedProcess::kDefaultGroup,
153 Log::kDefaultMaxBytes, LogTool::Encoding::kUtf8},
154 {kFile, "cros_ec.previous", "/var/log/cros_ec.previous",
155 SandboxedProcess::kDefaultUser, SandboxedProcess::kDefaultGroup,
156 Log::kDefaultMaxBytes, LogTool::Encoding::kUtf8},
Chris Morin853d3442019-04-01 21:35:13 -0700157 {kFile, "cros_ec_panicinfo", "/sys/kernel/debug/cros_ec/panicinfo",
Nicolas Boichat28272d72020-09-03 09:10:44 +0800158 SandboxedProcess::kDefaultUser, kDebugfsGroup, Log::kDefaultMaxBytes,
159 LogTool::Encoding::kBase64},
Stephen Boydf00c5a02020-09-10 19:11:35 -0700160 {kCommand, "cros_ec_pdinfo",
161 "for port in 0 1 2 3 4 5 6 7 8; do "
162 "echo \"-----------\"; "
163 // stderr output just tells us it failed
164 "ectool usbpd \"${port}\" 2>/dev/null || break; "
165 "done", kRoot, kRoot},
Nicolas Boichatf3dd82d2020-09-07 15:11:15 +0800166 {kFile, "cros_fp.previous", "/var/log/cros_fp.previous",
167 SandboxedProcess::kDefaultUser, SandboxedProcess::kDefaultGroup,
168 Log::kDefaultMaxBytes, LogTool::Encoding::kUtf8},
169 {kFile, "cros_fp.log", "/var/log/cros_fp.log",
170 SandboxedProcess::kDefaultUser, SandboxedProcess::kDefaultGroup,
171 Log::kDefaultMaxBytes, LogTool::Encoding::kUtf8},
172 {kFile, "cros_ish.previous", "/var/log/cros_ish.previous",
173 SandboxedProcess::kDefaultUser, SandboxedProcess::kDefaultGroup,
174 Log::kDefaultMaxBytes, LogTool::Encoding::kUtf8},
175 {kFile, "cros_ish.log", "/var/log/cros_ish.log",
176 SandboxedProcess::kDefaultUser, SandboxedProcess::kDefaultGroup,
177 Log::kDefaultMaxBytes, LogTool::Encoding::kUtf8},
Nicolas Boichat579ccf32020-08-31 11:17:21 +0800178 {kFile, "cros_scp.previous", "/var/log/cros_scp.previous",
179 SandboxedProcess::kDefaultUser, SandboxedProcess::kDefaultGroup,
180 64 * 1024, LogTool::Encoding::kUtf8},
181 {kFile, "cros_scp.log", "/var/log/cros_scp.log",
182 SandboxedProcess::kDefaultUser, SandboxedProcess::kDefaultGroup,
183 64 * 1024, LogTool::Encoding::kUtf8},
David Munro0eb1ac32020-08-19 22:22:56 +1000184 {kCommand, "crosvm.log", "nsenter -t1 -m /bin/sh -c 'tail -n+1"
185 " /run/daemon-store/crosvm/*/log/*.log.1"
186 " /run/daemon-store/crosvm/*/log/*.log'", kRoot, kRoot},
Jorge Lucangeli Obesaf8f79c2020-11-13 08:54:55 -0500187 // 'dmesg' needs CAP_SYSLOG.
188 {kCommand, "dmesg", "/bin/dmesg", kRoot, kRoot},
Stephen Boyda8287162020-07-30 18:07:53 -0700189 {kCommand, "drm_gem_objects", "cat /sys/kernel/debug/dri/?/gem",
190 SandboxedProcess::kDefaultUser, kDebugfsGroup},
191 {kCommand, "drm_state", "cat /sys/kernel/debug/dri/?/state",
192 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700193 {kFile, "ec_info", "/var/log/ec_info.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700194 {kCommand, "edid-decode",
Stephen Boydaf3118b2020-08-11 11:42:22 -0700195 "for f in /sys/class/drm/card?-*/edid; do "
Chris Morin853d3442019-04-01 21:35:13 -0700196 "echo \"----------- ${f}\"; "
Chris Morin853d3442019-04-01 21:35:13 -0700197 // edid-decode's stderr output is redundant, so silence it.
Jeffrey Kardatzke9ba9f322019-08-29 10:23:14 -0700198 "edid-decode \"${f}\" 2>/dev/null; "
Chris Morin853d3442019-04-01 21:35:13 -0700199 "done"},
200 {kFile, "eventlog", "/var/log/eventlog.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700201 {kCommand, "font_info", "/usr/share/userfeedback/scripts/font_info"},
Kuo-Hsin Yang95296e12020-03-06 17:52:35 +0800202 {kCommand, "framebuffer", "cat /sys/kernel/debug/dri/?/framebuffer",
203 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Daniel Campello9f0b2b82020-10-23 11:01:01 -0600204 {kCommand, "fwupd_state", "/sbin/initctl emit fwupdtool-getdevices;"
205 "cat /var/lib/fwupd/state.json", kRoot, kRoot},
Chris Morin853d3442019-04-01 21:35:13 -0700206 {kCommand, "sensor_info", "/usr/share/userfeedback/scripts/sensor_info"},
207 {kFile, "hammerd", "/var/log/hammerd.log"},
208 {kCommand, "hardware_class", "/usr/bin/crossystem hwid"},
Yong Hong15e4b032020-03-05 15:41:31 +0800209 {kFile, "hardware_verification_report",
210 "/var/cache/hardware_verifier.result"},
Chris Morin853d3442019-04-01 21:35:13 -0700211 {kCommand, "hostname", "/bin/hostname"},
212 {kFile, "i915_gem_gtt", "/sys/kernel/debug/dri/0/i915_gem_gtt",
213 SandboxedProcess::kDefaultUser, kDebugfsGroup},
214 {kFile, "i915_gem_objects", "/sys/kernel/debug/dri/0/i915_gem_objects",
215 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700216 {kCommand, "i915_error_state",
217 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2>/dev/null",
218 SandboxedProcess::kDefaultUser, kDebugfsGroup, Log::kDefaultMaxBytes,
Chris Morin790fd262019-04-03 20:29:36 -0700219 LogTool::Encoding::kBase64},
Chris Morin853d3442019-04-01 21:35:13 -0700220 {kCommand, "ifconfig", "/bin/ifconfig -a"},
221 {kFile, "input_devices", "/proc/bus/input/devices"},
Eric Carusob1820c02017-08-24 15:39:56 -0700222 // Hardware capabilities of the wiphy device.
Alex Levine1c6d572019-09-17 14:45:33 -0700223 {kFile, "interrupts", "/proc/interrupts"},
Chris Morin853d3442019-04-01 21:35:13 -0700224 {kCommand, "iw_list", "/usr/sbin/iw list"},
Eric Carusoa879fd92017-10-11 12:57:10 -0700225#if USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700226 {kCommand, "iwlmvm_module_params", CMD_KERNEL_MODULE_PARAMS(iwlmvm)},
227 {kCommand, "iwlwifi_module_params", CMD_KERNEL_MODULE_PARAMS(iwlwifi)},
Eric Carusoa879fd92017-10-11 12:57:10 -0700228#endif // USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700229 {kCommand, "kernel-crashes",
230 "cat /var/spool/crash/kernel.*.kcrash 2>/dev/null"},
Anand K Mistryccceb1e2020-01-16 14:00:49 +1100231 {kCommand, "lsblk", "timeout -s KILL 5s lsblk -a", kRoot, kRoot,
232 Log::kDefaultMaxBytes, LogTool::Encoding::kAutodetect, true},
Chris Morin853d3442019-04-01 21:35:13 -0700233 {kCommand, "lsmod", "lsmod"},
234 {kCommand, "lspci", "/usr/sbin/lspci"},
235 {kCommand, "lsusb", "lsusb && lsusb -t"},
Kuo-Hsin Yang66d89832020-02-10 17:22:28 +0800236 {kFile, "mali_memory", "/sys/kernel/debug/mali0/gpu_memory",
237 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700238 {kFile, "memd.parameters", "/var/log/memd/memd.parameters"},
239 {kCommand, "memd clips", "cat /var/log/memd/memd.clip* 2>/dev/null"},
240 {kFile, "meminfo", "/proc/meminfo"},
241 {kCommand, "memory_spd_info",
242 // mosys may use 'i2c-dev', which may not be loaded yet.
243 "modprobe i2c-dev 2>/dev/null && mosys -l memory spd print all 2>/dev/null",
244 kRoot, kDebugfsGroup},
Simon Quecb63b9c2017-06-19 14:53:31 -0400245 // The sed command finds the EDID blob (starting the line after "value:") and
246 // replaces the serial number with all zeroes.
247 //
248 // The EDID is printed as a hex dump over several lines, each line containing
249 // the contents of 16 bytes. The first 16 bytes are broken down as follows:
250 // uint64_t fixed_pattern; // Always 00 FF FF FF FF FF FF 00.
251 // uint16_t manufacturer_id; // Manufacturer ID, encoded as PNP IDs.
252 // uint16_t product_code; // Manufacturer product code, little-endian.
253 // uint32_t serial_number; // Serial number, little-endian.
254 // Source: https://en.wikipedia.org/wiki/EDID#EDID_1.3_data_format
255 //
256 // The subsequent substitution command looks for the fixed pattern followed by
257 // two 32-bit fields (manufacturer + product, serial number). It replaces the
258 // latter field with 8 bytes of zeroes.
259 //
260 // TODO(crbug.com/731133): Remove the sed command once modetest itself can
261 // remove serial numbers.
Chris Morin853d3442019-04-01 21:35:13 -0700262 {kCommand, "modetest",
263 "(modetest; modetest -M evdi; modetest -M udl) | "
264 "sed -E '/EDID/ {:a;n;/value:/!ba;n;"
265 "s/(00f{12}00)([0-9a-f]{8})([0-9a-f]{8})/\\1\\200000000/}'",
266 kRoot, kRoot},
267 {kFile, "mount-encrypted", "/var/log/mount-encrypted.log"},
268 {kFile, "mountinfo", "/proc/1/mountinfo"},
269 {kCommand, "netlog",
270 "/usr/share/userfeedback/scripts/getmsgs /var/log/net.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700271 {kFile, "nvmap_iovmm", "/sys/kernel/debug/nvmap/iovmm/allocations",
272 SandboxedProcess::kDefaultUser, kDebugfsGroup},
273 {kCommand, "oemdata", "/usr/share/cros/oemdata.sh", kRoot, kRoot},
Kuo-Hsin Yanga69ecc62020-03-11 17:37:11 +0800274 {kFile, "pagetypeinfo", "/proc/pagetypeinfo", kRoot},
Jack Rosenthal3cf794a2020-02-19 13:32:56 -0700275 {kFile, "platform_identity_name",
276 "/run/chromeos-config/v1/identity/platform-name"},
277 {kFile, "platform_identity_model", "/run/chromeos-config/v1/name"},
278 {kFile, "platform_identity_sku", "/run/chromeos-config/v1/identity/sku-id"},
279 {kFile, "platform_identity_whitelabel_tag",
280 "/run/chromeos-config/v1/identity/whitelabel-tag"},
281 {kFile, "platform_identity_customization_id",
282 "/run/chromeos-config/v1/identity/customization-id"},
Chris Morin853d3442019-04-01 21:35:13 -0700283 {kCommand, "power_supply_info", "/usr/bin/power_supply_info"},
284 {kCommand, "power_supply_sysfs", "/usr/bin/print_sysfs_power_supply_data"},
285 {kFile, "powerd.LATEST", "/var/log/power_manager/powerd.LATEST"},
286 {kFile, "powerd.PREVIOUS", "/var/log/power_manager/powerd.PREVIOUS"},
287 {kFile, "powerd.out", "/var/log/powerd.out"},
288 {kFile, "powerwash_count", "/var/log/powerwash_count"},
Brian Norris4cde3d12019-04-16 10:10:34 -0700289 {kCommand, "ps", "/bin/ps auxZ"},
yusukes34171ba2017-04-27 15:46:01 -0700290 // /proc/slabinfo is owned by root and has 0400 permission.
Chris Morin853d3442019-04-01 21:35:13 -0700291 {kFile, "slabinfo", "/proc/slabinfo", kRoot, kRoot},
292 {kFile, "storage_info", "/var/log/storage_info.txt"},
293 {kCommand, "swap_info", "/usr/share/cros/init/swap.sh status 2>/dev/null",
294 SandboxedProcess::kDefaultUser, kDebugfsGroup},
295 {kCommand, "syslog",
296 "/usr/share/userfeedback/scripts/getmsgs /var/log/messages"},
297 {kCommand, "system_log_stats",
298 "echo 'BLOCK_SIZE=1024'; "
299 "find /var/log/ -type f -exec du --block-size=1024 {} + | sort -n -r",
300 kRoot, kRoot},
301 {kCommand, "threads", "/bin/ps -T axo pid,ppid,spid,pcpu,ni,stat,time,comm"},
302 {kFile, "tlsdate", "/var/log/tlsdate.log"},
Nick Sandersad5dc132019-11-15 15:59:42 -0800303 {kCommand, "top thread", "/usr/bin/top -Hbc -w128 -n 1 | head -n 40"},
304 {kCommand, "top memory",
305 "/usr/bin/top -o \"+%MEM\" -w128 -bcn 1 | head -n 57"},
Chris Morin853d3442019-04-01 21:35:13 -0700306 {kCommand, "touch_fw_version",
Stephen Boyddb9eb2f2020-08-11 11:25:41 -0700307 "grep -aE"
Chris Morin853d3442019-04-01 21:35:13 -0700308 " -e 'synaptics: Touchpad model'"
309 " -e 'chromeos-[a-z]*-touch-[a-z]*-update'"
310 " /var/log/messages | tail -n 20"},
311 {kCommand, "tpm-firmware-updater", "/usr/share/userfeedback/scripts/getmsgs "
312 "/var/log/tpm-firmware-updater.log"},
Mattias Nissler887dce22017-07-03 14:44:35 +0200313 // TODO(jorgelo,mnissler): Don't run this as root.
314 // On TPM 1.2 devices this will likely require adding a new user to the 'tss'
315 // group.
316 // On TPM 2.0 devices 'get_version_info' uses D-Bus and therefore can run as
317 // any user.
Chris Morin853d3442019-04-01 21:35:13 -0700318 {kCommand, "tpm_version", "/usr/sbin/tpm-manager get_version_info", kRoot,
319 kRoot},
320 {kCommand, "atmel_ts_refs",
321 "/opt/google/touch/scripts/atmel_tools.sh ts r", kRoot, kRoot},
322 {kCommand, "atmel_tp_refs",
323 "/opt/google/touch/scripts/atmel_tools.sh tp r", kRoot, kRoot},
324 {kCommand, "atmel_ts_deltas",
325 "/opt/google/touch/scripts/atmel_tools.sh ts d", kRoot, kRoot},
326 {kCommand, "atmel_tp_deltas",
327 "/opt/google/touch/scripts/atmel_tools.sh tp d", kRoot, kRoot},
328 {kFile, "stateful_trim_state", "/var/lib/trim/stateful_trim_state"},
329 {kFile, "stateful_trim_data", "/var/lib/trim/stateful_trim_data"},
330 {kFile, "ui_log", "/var/log/ui/ui.LATEST"},
331 {kCommand, "uname", "/bin/uname -a"},
332 {kCommand, "update_engine.log",
333 "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
334 " s.^./var/log/update_engine/.)"},
Chris Morinca152712019-05-03 13:17:28 -0700335 {kFile, "upstart", "/var/log/upstart.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700336 {kCommand, "uptime", "/usr/bin/cut -d' ' -f1 /proc/uptime"},
337 {kFile, "verified boot", "/var/log/debug_vboot_noisy.log"},
338 {kFile, "vmlog.1.LATEST", "/var/log/vmlog/vmlog.1.LATEST"},
339 {kFile, "vmlog.1.PREVIOUS", "/var/log/vmlog/vmlog.1.PREVIOUS"},
340 {kFile, "vmlog.LATEST", "/var/log/vmlog/vmlog.LATEST"},
341 {kFile, "vmlog.PREVIOUS", "/var/log/vmlog/vmlog.PREVIOUS"},
342 {kFile, "vmstat", "/proc/vmstat"},
343 {kFile, "vpd_2.0", "/var/log/vpd_2.0.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700344 {kFile, "zram compressed data size", "/sys/block/zram0/compr_data_size"},
345 {kFile, "zram original data size", "/sys/block/zram0/orig_data_size"},
346 {kFile, "zram total memory used", "/sys/block/zram0/mem_used_total"},
347 {kFile, "zram total reads", "/sys/block/zram0/num_reads"},
348 {kFile, "zram total writes", "/sys/block/zram0/num_writes"},
349 {kCommand, "zram new stats names",
350 "echo orig_size compr_size used_total limit used_max zero_pages migrated"},
351 {kFile, "zram new stats values", "/sys/block/zram0/mm_stat"},
352 {kFile, "cros_tp version", "/sys/class/chromeos/cros_tp/version"},
353 {kCommand, "cros_tp console", "/usr/sbin/ectool --name=cros_tp console",
354 kRoot, kRoot},
355 {kCommand, "cros_tp frame", "/usr/sbin/ectool --name=cros_tp tpframeget",
356 kRoot, kRoot},
357 {kCommand, "crostini", "/usr/bin/cicerone_client --get_info"},
Sean Paulbfc5c422020-07-24 11:03:51 -0400358 // TODO(seanpaul): Once we've finished moving over to the upstream tracefs
359 // implementation, remove drm_trace_legacy. Tracked in
360 // b/163580546.
361 {kFile, "drm_trace_legacy", "/sys/kernel/debug/dri/trace",
362 SandboxedProcess::kDefaultUser, kDebugfsGroup},
363 {kFile, "drm_trace", "/sys/kernel/debug/tracing/instances/drm/trace",
Sean Paul5ce118f2019-12-05 08:41:32 -0500364 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Elly Jones03cd6d72012-06-11 13:04:28 -0400365 // Stuff pulled out of the original list. These need access to the running X
366 // session, which we'd rather not give to debugd, or return info specific to
367 // the current session (in the setsid(2) sense), which is not useful for
368 // debugd
Chris Morin853d3442019-04-01 21:35:13 -0700369 // {kCommand, "env", "set"},
370 // {kCommand, "setxkbmap", "/usr/bin/setxkbmap -print -query"},
371 // {kCommand, "xrandr", "/usr/bin/xrandr --verbose}
Elly Jones533c7c42012-08-10 15:07:05 -0400372};
Tom Hughesd6c2d392020-08-24 18:12:11 -0700373// clang-format on
Elly Jones533c7c42012-08-10 15:07:05 -0400374
Junichi Uekawaba686c82020-09-01 16:24:24 +0900375// Extra logs are logs such as netstat and logcat which should appear in
376// chrome://system but not in feedback reports. Open sockets may have privacy
377// implications, and logcat is already incorporated via arc-bugreport.
378//
Tom Hughesd6c2d392020-08-24 18:12:11 -0700379// clang-format off
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700380const std::vector<Log> kExtraLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800381#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700382 {kCommand, "mm-status", "/usr/bin/modem status"},
Ben Chan36e42282014-02-12 22:32:34 -0800383#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700384 {kCommand, "network-devices", "/usr/bin/connectivity show devices"},
385 {kCommand, "network-services", "/usr/bin/connectivity show services"},
Jeffrey Kardatzke36791f22019-07-11 11:53:22 -0700386 {kCommand, "wifi_status_no_anonymize",
387 "/usr/bin/network_diag --wifi-internal --no-log"},
Chris Morin253a2b02019-04-12 16:04:25 -0700388 // --processes requires root.
389 {kCommand, "netstat",
390 "/sbin/ss --all --query inet --numeric --processes", kRoot, kRoot},
Kansho Nishida6ae546f2019-08-13 17:14:58 +0900391 {kCommand, "logcat",
392 "/usr/bin/nsenter -t1 -m /usr/sbin/android-sh -c '/system/bin/logcat -d'",
Chris Morin253a2b02019-04-12 16:04:25 -0700393 kRoot, kRoot, Log::kDefaultMaxBytes, LogTool::Encoding::kUtf8},
Elly Jones533c7c42012-08-10 15:07:05 -0400394};
Tom Hughesd6c2d392020-08-24 18:12:11 -0700395// clang-format on
Elly Jones533c7c42012-08-10 15:07:05 -0400396
Tom Hughesd6c2d392020-08-24 18:12:11 -0700397// clang-format off
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700398const std::vector<Log> kFeedbackLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800399#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700400 {kCommand, "mm-status", "/usr/bin/modem status-feedback"},
Ben Chan36e42282014-02-12 22:32:34 -0800401#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700402 {kCommand, "network-devices",
403 "/usr/bin/connectivity show-feedback devices"},
404 {kCommand, "network-services",
405 "/usr/bin/connectivity show-feedback services"},
Jeffrey Kardatzke36791f22019-07-11 11:53:22 -0700406 {kCommand, "wifi_status",
407 "/usr/bin/network_diag --wifi-internal --no-log --anonymize"},
Elly Jones03cd6d72012-06-11 13:04:28 -0400408};
Tom Hughesd6c2d392020-08-24 18:12:11 -0700409// clang-format on
Elly Jones03cd6d72012-06-11 13:04:28 -0400410
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700411// Fills |dictionary| with the contents of the logs in |logs|.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700412void GetLogsInDictionary(const std::vector<Log>& logs,
hscham2311cc22020-10-28 12:13:10 +0900413 base::Value* dictionary) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700414 for (const Log& log : logs) {
hscham2311cc22020-10-28 12:13:10 +0900415 dictionary->SetStringKey(log.GetName(), log.GetLogData());
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800416 }
417}
418
419// Serializes the |dictionary| into the file with the given |fd| in a JSON
420// format.
hscham2311cc22020-10-28 12:13:10 +0900421void SerializeLogsAsJSON(const base::Value& dictionary,
Eric Caruso0b241882018-04-04 13:43:46 -0700422 const base::ScopedFD& fd) {
Eric Caruso96d03d32017-04-25 18:01:17 -0700423 string logs_json;
Tom Hughesd6c2d392020-08-24 18:12:11 -0700424 base::JSONWriter::WriteWithOptions(
425 dictionary, base::JSONWriter::OPTIONS_PRETTY_PRINT, &logs_json);
Eric Caruso0b241882018-04-04 13:43:46 -0700426 base::WriteFileDescriptor(fd.get(), logs_json.c_str(), logs_json.size());
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800427}
428
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700429bool GetNamedLogFrom(const string& name,
430 const std::vector<Log>& logs,
Elly Jones533c7c42012-08-10 15:07:05 -0400431 string* result) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700432 for (const Log& log : logs) {
433 if (name == log.GetName()) {
434 *result = log.GetLogData();
Elly Jones533c7c42012-08-10 15:07:05 -0400435 return true;
436 }
437 }
438 *result = "<invalid log name>";
439 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400440}
441
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700442void GetLogsFrom(const std::vector<Log>& logs, LogTool::LogMap* map) {
443 for (const Log& log : logs)
444 (*map)[log.GetName()] = log.GetLogData();
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800445}
446
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600447void GetLsbReleaseInfo(LogTool::LogMap* map) {
448 const base::FilePath lsb_release(kLsbReleasePath);
449 brillo::KeyValueStore store;
450 if (!store.Load(lsb_release)) {
451 // /etc/lsb-release might not be present (cros deploying a new
452 // configuration or no fields set at all). Just print a debug
453 // message and continue.
454 DLOG(INFO) << "Could not load fields from " << lsb_release.value();
455 } else {
456 for (const auto& key : store.GetKeys()) {
457 std::string value;
458 store.GetString(key, &value);
459 (*map)[key] = value;
460 }
461 }
462}
463
464void GetOsReleaseInfo(LogTool::LogMap* map) {
465 brillo::OsReleaseReader reader;
466 reader.Load();
467 for (const auto& key : reader.GetKeys()) {
468 std::string value;
469 reader.GetString(key, &value);
470 (*map)["os-release " + key] = value;
471 }
472}
473
474void PopulateDictionaryValue(const LogTool::LogMap& map,
hscham2311cc22020-10-28 12:13:10 +0900475 base::Value* dictionary) {
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600476 for (const auto& kv : map) {
hscham2311cc22020-10-28 12:13:10 +0900477 dictionary->SetStringKey(kv.first, kv.second);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600478 }
479}
480
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800481bool CompressXzBuffer(const std::vector<uint8_t>& in_buffer,
482 std::vector<uint8_t>* out_buffer) {
483 size_t out_size = lzma_stream_buffer_bound(in_buffer.size());
484 out_buffer->resize(out_size);
485 size_t out_pos = 0;
486
487 lzma_ret ret = lzma_easy_buffer_encode(
488 LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64, nullptr, in_buffer.data(),
489 in_buffer.size(), out_buffer->data(), &out_pos, out_size);
490
491 if (ret != LZMA_OK) {
492 out_buffer->clear();
493 return false;
494 }
495
496 out_buffer->resize(out_pos);
497 return true;
498}
499
500void GetPerfData(LogTool::LogMap* map) {
501 // Run perf to collect system-wide performance profile when user triggers
502 // feedback report. Perf runs at sampling frequency of ~500 hz (499 is used
503 // to avoid sampling periodic system activities), with callstack in each
504 // sample (-g).
505 std::vector<std::string> perf_args = {
Tom Hughesd6c2d392020-08-24 18:12:11 -0700506 "perf", "record", "-a", "-g", "-F", "499",
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800507 };
508 std::vector<uint8_t> perf_data;
509 int32_t status;
510
511 debugd::PerfTool perf_tool;
512 if (!perf_tool.GetPerfOutput(kPerfDurationSecs, perf_args, &perf_data,
513 nullptr, &status, nullptr))
514 return;
515
516 // XZ compress the profile data.
517 std::vector<uint8_t> perf_data_xz;
518 if (!CompressXzBuffer(perf_data, &perf_data_xz))
519 return;
520
521 // Base64 encode the compressed data.
522 std::string perf_data_str(reinterpret_cast<const char*>(perf_data_xz.data()),
523 perf_data_xz.size());
Tom Hughesd6c2d392020-08-24 18:12:11 -0700524 (*map)["perf-data"] = std::string(kPerfDataDescription) +
525 LogTool::EncodeString(std::move(perf_data_str),
526 LogTool::Encoding::kBase64);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800527}
528
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800529} // namespace
530
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700531Log::Log(Log::LogType type,
532 std::string name,
533 std::string data,
534 std::string user,
535 std::string group,
536 int64_t max_bytes,
Brian Norrisafc9f632019-05-09 14:08:28 -0700537 LogTool::Encoding encoding,
538 bool access_root_mount_ns)
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700539 : type_(type),
540 name_(name),
541 data_(data),
542 user_(user),
543 group_(group),
544 max_bytes_(max_bytes),
Brian Norrisafc9f632019-05-09 14:08:28 -0700545 encoding_(encoding),
546 access_root_mount_ns_(access_root_mount_ns) {}
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700547
548std::string Log::GetName() const {
549 return name_;
550}
551
552std::string Log::GetLogData() const {
553 // The reason this code uses a switch statement on a type enum rather than
554 // using inheritance/virtual dispatch is so that all of the Log objects can
555 // be constructed statically. Switching to heap allocated subclasses of Log
556 // makes the code that declares all of the log entries much more verbose
557 // and harder to understand.
Chris Morin790fd262019-04-03 20:29:36 -0700558 std::string output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700559 switch (type_) {
560 case kCommand:
Chris Morin790fd262019-04-03 20:29:36 -0700561 output = GetCommandLogData();
562 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700563 case kFile:
Chris Morin790fd262019-04-03 20:29:36 -0700564 output = GetFileLogData();
565 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700566 default:
567 return "<unknown log type>";
568 }
Chris Morin790fd262019-04-03 20:29:36 -0700569
570 if (output.empty())
571 return "<empty>";
572
573 return LogTool::EncodeString(std::move(output), encoding_);
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700574}
575
576// TODO(ellyjones): sandbox. crosbug.com/35122
577std::string Log::GetCommandLogData() const {
578 if (type_ != kCommand)
579 return "<log type mismatch>";
580 std::string tailed_cmdline =
581 base::StringPrintf("%s | tail -c %" PRId64, data_.c_str(), max_bytes_);
582 ProcessWithOutput p;
583 if (minijail_disabled_for_test_)
584 p.set_use_minijail(false);
585 if (!user_.empty() && !group_.empty())
586 p.SandboxAs(user_, group_);
Brian Norrisafc9f632019-05-09 14:08:28 -0700587 if (access_root_mount_ns_)
588 p.AllowAccessRootMountNamespace();
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700589 if (!p.Init())
590 return "<not available>";
591 p.AddArg(kShell);
592 p.AddStringOption("-c", tailed_cmdline);
593 if (p.Run())
594 return "<not available>";
595 std::string output;
596 p.GetOutput(&output);
Chris Morin790fd262019-04-03 20:29:36 -0700597 return output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700598}
599
600std::string Log::GetFileLogData() const {
601 if (type_ != kFile)
602 return "<log type mismatch>";
603
604 uid_t old_euid = geteuid();
605 uid_t new_euid = UidForUser(user_);
606 gid_t old_egid = getegid();
607 gid_t new_egid = GidForGroup(group_);
608
609 if (new_euid == -1 || new_egid == -1) {
610 return "<not available>";
611 }
612
613 // Make sure to set group first, since if we set user first we lose root
614 // and therefore the ability to set our effective gid to arbitrary gids.
615 if (setegid(new_egid)) {
616 PLOG(ERROR) << "Failed to set effective group id to " << new_egid;
617 return "<not available>";
618 }
619 if (seteuid(new_euid)) {
620 PLOG(ERROR) << "Failed to set effective user id to " << new_euid;
621 if (setegid(old_egid))
622 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
623 return "<not available>";
624 }
625
626 std::string contents;
627 const base::FilePath path(data_);
628 // Handle special files that don't properly report length/allow lseek.
629 if (base::FilePath("/dev").IsParent(path) ||
630 base::FilePath("/proc").IsParent(path) ||
631 base::FilePath("/sys").IsParent(path)) {
632 if (!base::ReadFileToString(path, &contents))
633 contents = "<not available>";
634 if (contents.size() > max_bytes_)
635 contents.erase(0, contents.size() - max_bytes_);
636 } else {
637 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
638 if (!file.IsValid()) {
639 contents = "<not available>";
640 } else {
641 int64_t length = file.GetLength();
642 if (length > max_bytes_) {
643 file.Seek(base::File::FROM_END, -max_bytes_);
644 length = max_bytes_;
645 }
646 std::vector<char> buf(length);
647 int read = file.ReadAtCurrentPos(buf.data(), buf.size());
648 if (read < 0) {
649 PLOG(ERROR) << "Could not read from file " << path.value();
650 } else {
651 contents = std::string(buf.begin(), buf.begin() + read);
652 }
653 }
654 }
655
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700656 // Make sure we restore our old euid/egid before returning.
657 if (seteuid(old_euid))
658 PLOG(ERROR) << "Failed to restore effective user id to " << old_euid;
659
660 if (setegid(old_egid))
661 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
662
Chris Morin790fd262019-04-03 20:29:36 -0700663 return contents;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700664}
665
666void Log::DisableMinijailForTest() {
667 minijail_disabled_for_test_ = true;
668}
669
670// static
671uid_t Log::UidForUser(const std::string& user) {
672 struct passwd entry;
673 struct passwd* result;
674 std::vector<char> buf(1024);
675 getpwnam_r(user.c_str(), &entry, &buf[0], buf.size(), &result);
676 if (!result) {
677 LOG(ERROR) << "User not found: " << user;
678 return -1;
679 }
680 return entry.pw_uid;
681}
682
683// static
684gid_t Log::GidForGroup(const std::string& group) {
685 struct group entry;
686 struct group* result;
687 std::vector<char> buf(1024);
688 getgrnam_r(group.c_str(), &entry, &buf[0], buf.size(), &result);
689 if (!result) {
690 LOG(ERROR) << "Group not found: " << group;
691 return -1;
692 }
693 return entry.gr_gid;
694}
695
mhasankaf5251d2020-04-29 18:53:03 -0700696LogTool::LogTool(
697 scoped_refptr<dbus::Bus> bus,
698 std::unique_ptr<org::chromium::CryptohomeInterfaceProxyInterface>
699 cryptohome_proxy,
700 std::unique_ptr<LogTool::Log> arc_bug_report_log,
701 const base::FilePath& daemon_store_base_dir)
702 : bus_(bus),
703 cryptohome_proxy_(std::move(cryptohome_proxy)),
704 arc_bug_report_log_(std::move(arc_bug_report_log)),
705 daemon_store_base_dir_(daemon_store_base_dir) {}
mhasank80cbe4d2020-04-02 22:46:08 -0700706
mhasank4f599d32020-04-09 22:07:35 -0700707LogTool::LogTool(scoped_refptr<dbus::Bus> bus)
mhasankaf5251d2020-04-29 18:53:03 -0700708 : LogTool(bus,
709 std::make_unique<org::chromium::CryptohomeInterfaceProxy>(bus),
710 std::make_unique<ArcBugReportLog>(),
711 base::FilePath(kDaemonStoreBaseDir)) {}
mhasank4f599d32020-04-09 22:07:35 -0700712
mhasank7186aae2020-09-16 20:06:00 -0700713bool LogTool::IsUserHashValid(const std::string& userhash) {
714 return brillo::cryptohome::home::IsSanitizedUserName(userhash) &&
715 base::PathExists(daemon_store_base_dir_.Append(userhash));
716}
717
Tom Hughesd6c2d392020-08-24 18:12:11 -0700718base::FilePath LogTool::GetArcBugReportBackupFilePath(
719 const std::string& userhash) {
Tom Hughesd6c2d392020-08-24 18:12:11 -0700720 return daemon_store_base_dir_.Append(userhash).Append(
721 kArcBugReportBackupFileName);
mhasank80cbe4d2020-04-02 22:46:08 -0700722}
723
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700724void LogTool::CreateConnectivityReport(bool wait_for_results) {
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700725 // Perform ConnectivityTrial to report connection state in feedback log.
Ben Chan8e9f6d02017-09-26 23:04:21 -0700726 auto shill = std::make_unique<org::chromium::flimflam::ManagerProxy>(bus_);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700727 // Give the connection trial time to test the connection and log the results
728 // before collecting the logs for feedback.
729 // TODO(silberst): Replace the simple approach of a single timeout with a more
730 // coordinated effort.
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700731 if (shill && shill->CreateConnectivityReport(nullptr) && wait_for_results)
Eric Carusocc7106c2017-04-27 14:22:42 -0700732 sleep(kConnectionTesterTimeoutSeconds);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700733}
734
Eric Carusoc93a15c2017-04-24 16:15:12 -0700735string LogTool::GetLog(const string& name) {
Elly Jones533c7c42012-08-10 15:07:05 -0400736 string result;
Tom Hughesd6c2d392020-08-24 18:12:11 -0700737 GetNamedLogFrom(name, kCommandLogs, &result) ||
738 GetNamedLogFrom(name, kExtraLogs, &result) ||
739 GetNamedLogFrom(name, kFeedbackLogs, &result);
Elly Jones533c7c42012-08-10 15:07:05 -0400740 return result;
741}
742
Eric Carusof9091f82017-04-28 14:18:59 -0700743LogTool::LogMap LogTool::GetAllLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700744 CreateConnectivityReport(false);
Elly Jones533c7c42012-08-10 15:07:05 -0400745 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700746 GetLogsFrom(kCommandLogs, &result);
747 GetLogsFrom(kExtraLogs, &result);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600748 GetLsbReleaseInfo(&result);
749 GetOsReleaseInfo(&result);
Elly Jones533c7c42012-08-10 15:07:05 -0400750 return result;
751}
752
Brian Norrisca4fc042018-04-03 00:24:26 -0700753LogTool::LogMap LogTool::GetAllDebugLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700754 CreateConnectivityReport(true);
Brian Norrisca4fc042018-04-03 00:24:26 -0700755 LogMap result;
756 GetLogsFrom(kCommandLogs, &result);
757 GetLogsFrom(kExtraLogs, &result);
mhasankd2b84882020-05-04 17:02:19 -0700758 result[arc_bug_report_log_->GetName()] = GetArcBugReport("", nullptr);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600759 GetLsbReleaseInfo(&result);
760 GetOsReleaseInfo(&result);
Brian Norrisca4fc042018-04-03 00:24:26 -0700761 return result;
762}
763
mhasank4f599d32020-04-09 22:07:35 -0700764void LogTool::GetBigFeedbackLogs(const base::ScopedFD& fd,
765 const std::string& username) {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700766 CreateConnectivityReport(true);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800767 LogMap map;
768 GetPerfData(&map);
hscham2311cc22020-10-28 12:13:10 +0900769 base::Value dictionary(base::Value::Type::DICTIONARY);
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700770 GetLogsInDictionary(kCommandLogs, &dictionary);
771 GetLogsInDictionary(kFeedbackLogs, &dictionary);
mhasankd2b84882020-05-04 17:02:19 -0700772 bool is_backup;
773 std::string arc_bug_report = GetArcBugReport(username, &is_backup);
hscham2311cc22020-10-28 12:13:10 +0900774 dictionary.SetStringKey(kArcBugReportBackupKey,
775 (is_backup ? "true" : "false"));
776 dictionary.SetStringKey(arc_bug_report_log_->GetName(), arc_bug_report);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600777 GetLsbReleaseInfo(&map);
778 GetOsReleaseInfo(&map);
779 PopulateDictionaryValue(map, &dictionary);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800780 SerializeLogsAsJSON(dictionary, fd);
781}
782
mhasank4f599d32020-04-09 22:07:35 -0700783std::string GetSanitizedUsername(
784 org::chromium::CryptohomeInterfaceProxyInterface* cryptohome_proxy,
mhasankaf5251d2020-04-29 18:53:03 -0700785 const std::string& username) {
mhasank4f599d32020-04-09 22:07:35 -0700786 if (username.empty()) {
787 return std::string();
788 }
789
790 std::string sanitized_username;
791 brillo::ErrorPtr error;
792 if (!cryptohome_proxy->GetSanitizedUsername(username, &sanitized_username,
793 &error)) {
794 LOG(ERROR) << "Failed to call GetSanitizedUsername, error: "
795 << error->GetMessage();
796 return std::string();
797 }
798
799 return sanitized_username;
800}
801
mhasankd2b84882020-05-04 17:02:19 -0700802std::string LogTool::GetArcBugReport(const std::string& username,
803 bool* is_backup) {
804 if (is_backup) {
805 *is_backup = true;
806 }
mhasank4f599d32020-04-09 22:07:35 -0700807 std::string userhash =
808 GetSanitizedUsername(cryptohome_proxy_.get(), username);
809
810 std::string contents;
811 if (userhash.empty() ||
812 arc_bug_report_backups_.find(userhash) == arc_bug_report_backups_.end() ||
813 !base::ReadFileToString(GetArcBugReportBackupFilePath(userhash),
814 &contents)) {
815 // If |userhash| was not empty, but was not found in the backup set
816 // or the file did not exist, attempt to delete the file.
817 if (!userhash.empty()) {
mhasank40a80482020-09-09 17:44:24 -0700818 DeleteArcBugReportBackup(username);
mhasank4f599d32020-04-09 22:07:35 -0700819 }
mhasankd2b84882020-05-04 17:02:19 -0700820 if (is_backup) {
821 *is_backup = false;
822 }
mhasankaf5251d2020-04-29 18:53:03 -0700823 contents = arc_bug_report_log_->GetLogData();
mhasank4f599d32020-04-09 22:07:35 -0700824 }
825
826 return contents;
827}
828
mhasank7186aae2020-09-16 20:06:00 -0700829void LogTool::BackupArcBugReport(const std::string& username) {
mhasank80cbe4d2020-04-02 22:46:08 -0700830 DLOG(INFO) << "Backing up ARC bug report";
831
mhasank40a80482020-09-09 17:44:24 -0700832 const std::string userhash =
mhasank7186aae2020-09-16 20:06:00 -0700833 GetSanitizedUsername(cryptohome_proxy_.get(), username);
834 if (!IsUserHashValid(userhash)) {
835 LOG(ERROR) << "Invalid userhash '" << userhash << "'";
836 return;
837 }
mhasank40a80482020-09-09 17:44:24 -0700838
mhasank80cbe4d2020-04-02 22:46:08 -0700839 const base::FilePath reportPath = GetArcBugReportBackupFilePath(userhash);
mhasankaf5251d2020-04-29 18:53:03 -0700840 const std::string logData = arc_bug_report_log_->GetLogData();
mhasank4f599d32020-04-09 22:07:35 -0700841 if (base::WriteFile(reportPath, logData.c_str(), logData.length())) {
842 arc_bug_report_backups_.insert(userhash);
843 } else {
mhasank7186aae2020-09-16 20:06:00 -0700844 PLOG(ERROR) << "Failed to back up ARC bug report";
mhasank80cbe4d2020-04-02 22:46:08 -0700845 }
846}
847
mhasank7186aae2020-09-16 20:06:00 -0700848void LogTool::DeleteArcBugReportBackup(const std::string& username) {
mhasank80cbe4d2020-04-02 22:46:08 -0700849 DLOG(INFO) << "Deleting the ARC bug report backup";
850
mhasank40a80482020-09-09 17:44:24 -0700851 const std::string userhash =
mhasank7186aae2020-09-16 20:06:00 -0700852 GetSanitizedUsername(cryptohome_proxy_.get(), username);
853 if (!IsUserHashValid(userhash)) {
854 LOG(ERROR) << "Invalid userhash '" << userhash << "'";
855 return;
856 }
mhasank40a80482020-09-09 17:44:24 -0700857
mhasank80cbe4d2020-04-02 22:46:08 -0700858 const base::FilePath reportPath = GetArcBugReportBackupFilePath(userhash);
mhasank4f599d32020-04-09 22:07:35 -0700859 arc_bug_report_backups_.erase(userhash);
hscham53cf73a2020-11-30 15:58:42 +0900860 if (!base::DeleteFile(reportPath)) {
mhasank80cbe4d2020-04-02 22:46:08 -0700861 PLOG(ERROR) << "Failed to delete ARC bug report backup";
862 }
863}
864
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700865void LogTool::GetJournalLog(const base::ScopedFD& fd) {
Chris Morin790fd262019-04-03 20:29:36 -0700866 Log journal(kCommand, "journal.export", "journalctl -n 10000 -o export",
867 "syslog", "syslog", 10 * 1024 * 1024, LogTool::Encoding::kBinary);
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700868 std::string output = journal.GetLogData();
Chris Morin790fd262019-04-03 20:29:36 -0700869 base::WriteFileDescriptor(fd.get(), output.data(), output.size());
870}
871
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700872// static
Tom Hughesd6c2d392020-08-24 18:12:11 -0700873string LogTool::EncodeString(string value, LogTool::Encoding source_encoding) {
Chris Morin790fd262019-04-03 20:29:36 -0700874 if (source_encoding == LogTool::Encoding::kBinary)
875 return value;
876
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700877 if (source_encoding == LogTool::Encoding::kAutodetect) {
878 if (base::IsStringUTF8(value))
879 return value;
Chris Morin790fd262019-04-03 20:29:36 -0700880 source_encoding = LogTool::Encoding::kBase64;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700881 }
882
883 if (source_encoding == LogTool::Encoding::kUtf8) {
884 string output;
885 const char* src = value.data();
886 int32_t src_len = static_cast<int32_t>(value.length());
887
888 output.reserve(value.size());
889 for (int32_t char_index = 0; char_index < src_len; char_index++) {
890 uint32_t code_point;
891 if (!base::ReadUnicodeCharacter(src, src_len, &char_index, &code_point) ||
892 !base::IsValidCharacter(code_point)) {
893 // Replace invalid characters with U+FFFD REPLACEMENT CHARACTER.
894 code_point = 0xFFFD;
895 }
896 base::WriteUnicodeCharacter(code_point, &output);
897 }
898 return output;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700899 }
Chris Morin853d3442019-04-01 21:35:13 -0700900
901 base::Base64Encode(value, &value);
902 return "<base64>: " + value;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700903}
904
Ben Chana0011d82014-05-13 00:19:29 -0700905} // namespace debugd