blob: 70d443430448bea3804320d4f9472bfac59664b9 [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) \
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -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.)
Fletcher Woodruff07c28532019-01-24 11:08:53 -070099const std::vector<Log> kCommandLogs {
Mike Frysingerb0350992018-09-14 13:45:35 -0400100 // We need to enter init's mount namespace because it has /home/chronos
101 // mounted which is where the consent knob lives. We don't have that mount
102 // in our own mount namespace (by design). https://crbug.com/884249
Chris Morin853d3442019-04-01 21:35:13 -0700103 {kCommand, "CLIENT_ID", "/usr/bin/nsenter -t1 -m /usr/bin/metrics_client -i",
104 kRoot, kDebugfsGroup},
105 {kCommand, "LOGDATE", "/bin/date"},
Yusuke Sato27a31672019-04-29 15:26:37 -0700106 // We need to enter init's mount namespace to access /home/root. Also, we use
107 // neither ARC container's mount namespace (with android-sh) nor
108 // /opt/google/containers/android/rootfs/android-data/ so that we can get
109 // results even when the container is down.
110 {kCommand, "android_app_storage", "/usr/bin/nsenter -t1 -m "
111 "/bin/sh -c \"/usr/bin/du -h /home/root/*/android-data/data/\"",
112 kRoot, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700113 {kFile, "atrus_logs", "/var/log/atrus.log"},
114 {kFile, "authpolicy", "/var/log/authpolicy.log"},
Brian Norrisafc9f632019-05-09 14:08:28 -0700115 {kCommand, "bootstat_summary", "/usr/bin/bootstat_summary",
116 SandboxedProcess::kDefaultUser, SandboxedProcess::kDefaultGroup,
117 Log::kDefaultMaxBytes, LogTool::Encoding::kAutodetect, true},
Craig Hesling5c384b52019-04-20 15:18:06 -0700118 {kFile, "bio_crypto_init.LATEST",
119 "/var/log/bio_crypto_init/bio_crypto_init.LATEST"},
120 {kFile, "bio_crypto_init.PREVIOUS",
121 "/var/log/bio_crypto_init/bio_crypto_init.PREVIOUS"},
Chris Morin853d3442019-04-01 21:35:13 -0700122 {kFile, "biod.LATEST", "/var/log/biod/biod.LATEST"},
123 {kFile, "biod.PREVIOUS", "/var/log/biod/biod.PREVIOUS"},
Craig Hesling4c3891e2019-04-20 12:53:54 -0700124 {kFile, "bio_fw_updater.LATEST", "/var/log/biod/bio_fw_updater.LATEST"},
125 {kFile, "bio_fw_updater.PREVIOUS", "/var/log/biod/bio_fw_updater.PREVIOUS"},
Chris Morin853d3442019-04-01 21:35:13 -0700126 {kFile, "bios_info", "/var/log/bios_info.txt"},
127 {kCommand, "bios_log", "cat /sys/firmware/log "
128 "/proc/device-tree/chosen/ap-console-buffer 2>/dev/null"},
129 {kFile, "bios_times", "/var/log/bios_times.txt"},
Anand K Mistryccceb1e2020-01-16 14:00:49 +1100130 // Slow or non-responsive block devices could cause this command to stall. Use
131 // a timeout to prevent this command from blocking log fetching. This command
132 // is expected to take O(100ms) in the normal case.
133 {kCommand, "blkid", "timeout -s KILL 5s /sbin/blkid", kRoot, kRoot},
Chris Morin853d3442019-04-01 21:35:13 -0700134 {kFile, "buddyinfo", "/proc/buddyinfo"},
135 {kCommand, "cbi_info", "/usr/share/userfeedback/scripts/cbi_info", kRoot,
136 kRoot},
137 {kFile, "cheets_log", "/var/log/arc.log"},
138 {kFile, "clobber.log", "/var/log/clobber.log"},
139 {kFile, "clobber-state.log", "/var/log/clobber-state.log"},
Sonny Raobd3dc002020-05-27 21:40:35 -0700140 {kCommand, "chromeos-pgmem", "/usr/bin/chromeos-pgmem", kRoot, kRoot},
Chris Morin853d3442019-04-01 21:35:13 -0700141 {kFile, "chrome_system_log", "/var/log/chrome/chrome"},
142 {kFile, "chrome_system_log.PREVIOUS", "/var/log/chrome/chrome.PREVIOUS"},
Mike Frysinger32cdf3e2017-08-14 18:17:06 -0400143 // There might be more than one record, so grab them all.
144 // Plus, for <linux-3.19, it's named "console-ramoops", but for newer
145 // versions, it's named "console-ramoops-#".
Chris Morin853d3442019-04-01 21:35:13 -0700146 {kCommand, "console-ramoops",
147 "cat /sys/fs/pstore/console-ramoops* 2>/dev/null"},
148 {kFile, "cpuinfo", "/proc/cpuinfo"},
149 {kFile, "cr50_version", "/var/cache/cr50-version"},
150 {kFile, "cros_ec.log", "/var/log/cros_ec.log"},
151 {kFile, "cros_ec.previous", "/var/log/cros_ec.previous"},
152 {kFile, "cros_ec_panicinfo", "/sys/kernel/debug/cros_ec/panicinfo",
153 SandboxedProcess::kDefaultUser, kDebugfsGroup},
154 {kFile, "cros_ec_pdinfo", "/sys/kernel/debug/cros_ec/pdinfo",
155 SandboxedProcess::kDefaultUser, kDebugfsGroup},
156 {kFile, "cros_fp.previous", "/var/log/cros_fp.previous"},
157 {kFile, "cros_fp.log", "/var/log/cros_fp.log"},
Mathew Kingd7a72622019-06-27 09:56:34 -0600158 {kFile, "cros_ish.previous", "/var/log/cros_ish.previous"},
159 {kFile, "cros_ish.log", "/var/log/cros_ish.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700160 {kCommand, "dmesg", "/bin/dmesg"},
Stephen Boyda8287162020-07-30 18:07:53 -0700161 {kCommand, "drm_gem_objects", "cat /sys/kernel/debug/dri/?/gem",
162 SandboxedProcess::kDefaultUser, kDebugfsGroup},
163 {kCommand, "drm_state", "cat /sys/kernel/debug/dri/?/state",
164 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700165 {kFile, "ec_info", "/var/log/ec_info.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700166 {kCommand, "edid-decode",
Stephen Boydaf3118b2020-08-11 11:42:22 -0700167 "for f in /sys/class/drm/card?-*/edid; do "
Chris Morin853d3442019-04-01 21:35:13 -0700168 "echo \"----------- ${f}\"; "
Chris Morin853d3442019-04-01 21:35:13 -0700169 // edid-decode's stderr output is redundant, so silence it.
Jeffrey Kardatzke9ba9f322019-08-29 10:23:14 -0700170 "edid-decode \"${f}\" 2>/dev/null; "
Chris Morin853d3442019-04-01 21:35:13 -0700171 "done"},
172 {kFile, "eventlog", "/var/log/eventlog.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700173 {kCommand, "font_info", "/usr/share/userfeedback/scripts/font_info"},
Kuo-Hsin Yang95296e12020-03-06 17:52:35 +0800174 {kCommand, "framebuffer", "cat /sys/kernel/debug/dri/?/framebuffer",
175 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700176 {kFile, "fwupd_state", "/var/lib/fwupd/state.json"},
177 {kCommand, "sensor_info", "/usr/share/userfeedback/scripts/sensor_info"},
178 {kFile, "hammerd", "/var/log/hammerd.log"},
179 {kCommand, "hardware_class", "/usr/bin/crossystem hwid"},
Yong Hong15e4b032020-03-05 15:41:31 +0800180 {kFile, "hardware_verification_report",
181 "/var/cache/hardware_verifier.result"},
Chris Morin853d3442019-04-01 21:35:13 -0700182 {kCommand, "hostname", "/bin/hostname"},
183 {kFile, "i915_gem_gtt", "/sys/kernel/debug/dri/0/i915_gem_gtt",
184 SandboxedProcess::kDefaultUser, kDebugfsGroup},
185 {kFile, "i915_gem_objects", "/sys/kernel/debug/dri/0/i915_gem_objects",
186 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700187 {kCommand, "i915_error_state",
188 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2>/dev/null",
189 SandboxedProcess::kDefaultUser, kDebugfsGroup, Log::kDefaultMaxBytes,
Chris Morin790fd262019-04-03 20:29:36 -0700190 LogTool::Encoding::kBase64},
Chris Morin853d3442019-04-01 21:35:13 -0700191 {kCommand, "ifconfig", "/bin/ifconfig -a"},
192 {kFile, "input_devices", "/proc/bus/input/devices"},
Eric Carusob1820c02017-08-24 15:39:56 -0700193 // Hardware capabilities of the wiphy device.
Alex Levine1c6d572019-09-17 14:45:33 -0700194 {kFile, "interrupts", "/proc/interrupts"},
Chris Morin853d3442019-04-01 21:35:13 -0700195 {kCommand, "iw_list", "/usr/sbin/iw list"},
Eric Carusoa879fd92017-10-11 12:57:10 -0700196#if USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700197 {kCommand, "iwlmvm_module_params", CMD_KERNEL_MODULE_PARAMS(iwlmvm)},
198 {kCommand, "iwlwifi_module_params", CMD_KERNEL_MODULE_PARAMS(iwlwifi)},
Eric Carusoa879fd92017-10-11 12:57:10 -0700199#endif // USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700200 {kCommand, "kernel-crashes",
201 "cat /var/spool/crash/kernel.*.kcrash 2>/dev/null"},
Anand K Mistryccceb1e2020-01-16 14:00:49 +1100202 {kCommand, "lsblk", "timeout -s KILL 5s lsblk -a", kRoot, kRoot,
203 Log::kDefaultMaxBytes, LogTool::Encoding::kAutodetect, true},
Chris Morin853d3442019-04-01 21:35:13 -0700204 {kCommand, "lsmod", "lsmod"},
205 {kCommand, "lspci", "/usr/sbin/lspci"},
206 {kCommand, "lsusb", "lsusb && lsusb -t"},
Kuo-Hsin Yang66d89832020-02-10 17:22:28 +0800207 {kFile, "mali_memory", "/sys/kernel/debug/mali0/gpu_memory",
208 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700209 {kFile, "memd.parameters", "/var/log/memd/memd.parameters"},
210 {kCommand, "memd clips", "cat /var/log/memd/memd.clip* 2>/dev/null"},
211 {kFile, "meminfo", "/proc/meminfo"},
212 {kCommand, "memory_spd_info",
213 // mosys may use 'i2c-dev', which may not be loaded yet.
214 "modprobe i2c-dev 2>/dev/null && mosys -l memory spd print all 2>/dev/null",
215 kRoot, kDebugfsGroup},
Simon Quecb63b9c2017-06-19 14:53:31 -0400216 // The sed command finds the EDID blob (starting the line after "value:") and
217 // replaces the serial number with all zeroes.
218 //
219 // The EDID is printed as a hex dump over several lines, each line containing
220 // the contents of 16 bytes. The first 16 bytes are broken down as follows:
221 // uint64_t fixed_pattern; // Always 00 FF FF FF FF FF FF 00.
222 // uint16_t manufacturer_id; // Manufacturer ID, encoded as PNP IDs.
223 // uint16_t product_code; // Manufacturer product code, little-endian.
224 // uint32_t serial_number; // Serial number, little-endian.
225 // Source: https://en.wikipedia.org/wiki/EDID#EDID_1.3_data_format
226 //
227 // The subsequent substitution command looks for the fixed pattern followed by
228 // two 32-bit fields (manufacturer + product, serial number). It replaces the
229 // latter field with 8 bytes of zeroes.
230 //
231 // TODO(crbug.com/731133): Remove the sed command once modetest itself can
232 // remove serial numbers.
Chris Morin853d3442019-04-01 21:35:13 -0700233 {kCommand, "modetest",
234 "(modetest; modetest -M evdi; modetest -M udl) | "
235 "sed -E '/EDID/ {:a;n;/value:/!ba;n;"
236 "s/(00f{12}00)([0-9a-f]{8})([0-9a-f]{8})/\\1\\200000000/}'",
237 kRoot, kRoot},
238 {kFile, "mount-encrypted", "/var/log/mount-encrypted.log"},
239 {kFile, "mountinfo", "/proc/1/mountinfo"},
240 {kCommand, "netlog",
241 "/usr/share/userfeedback/scripts/getmsgs /var/log/net.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700242 {kFile, "nvmap_iovmm", "/sys/kernel/debug/nvmap/iovmm/allocations",
243 SandboxedProcess::kDefaultUser, kDebugfsGroup},
244 {kCommand, "oemdata", "/usr/share/cros/oemdata.sh", kRoot, kRoot},
Kuo-Hsin Yanga69ecc62020-03-11 17:37:11 +0800245 {kFile, "pagetypeinfo", "/proc/pagetypeinfo", kRoot},
Jack Rosenthal3cf794a2020-02-19 13:32:56 -0700246 {kFile, "platform_identity_name",
247 "/run/chromeos-config/v1/identity/platform-name"},
248 {kFile, "platform_identity_model", "/run/chromeos-config/v1/name"},
249 {kFile, "platform_identity_sku", "/run/chromeos-config/v1/identity/sku-id"},
250 {kFile, "platform_identity_whitelabel_tag",
251 "/run/chromeos-config/v1/identity/whitelabel-tag"},
252 {kFile, "platform_identity_customization_id",
253 "/run/chromeos-config/v1/identity/customization-id"},
Chris Morin853d3442019-04-01 21:35:13 -0700254 {kCommand, "power_supply_info", "/usr/bin/power_supply_info"},
255 {kCommand, "power_supply_sysfs", "/usr/bin/print_sysfs_power_supply_data"},
256 {kFile, "powerd.LATEST", "/var/log/power_manager/powerd.LATEST"},
257 {kFile, "powerd.PREVIOUS", "/var/log/power_manager/powerd.PREVIOUS"},
258 {kFile, "powerd.out", "/var/log/powerd.out"},
259 {kFile, "powerwash_count", "/var/log/powerwash_count"},
Brian Norris4cde3d12019-04-16 10:10:34 -0700260 {kCommand, "ps", "/bin/ps auxZ"},
yusukes34171ba2017-04-27 15:46:01 -0700261 // /proc/slabinfo is owned by root and has 0400 permission.
Chris Morin853d3442019-04-01 21:35:13 -0700262 {kFile, "slabinfo", "/proc/slabinfo", kRoot, kRoot},
263 {kFile, "storage_info", "/var/log/storage_info.txt"},
264 {kCommand, "swap_info", "/usr/share/cros/init/swap.sh status 2>/dev/null",
265 SandboxedProcess::kDefaultUser, kDebugfsGroup},
266 {kCommand, "syslog",
267 "/usr/share/userfeedback/scripts/getmsgs /var/log/messages"},
268 {kCommand, "system_log_stats",
269 "echo 'BLOCK_SIZE=1024'; "
270 "find /var/log/ -type f -exec du --block-size=1024 {} + | sort -n -r",
271 kRoot, kRoot},
272 {kCommand, "threads", "/bin/ps -T axo pid,ppid,spid,pcpu,ni,stat,time,comm"},
273 {kFile, "tlsdate", "/var/log/tlsdate.log"},
Nick Sandersad5dc132019-11-15 15:59:42 -0800274 {kCommand, "top thread", "/usr/bin/top -Hbc -w128 -n 1 | head -n 40"},
275 {kCommand, "top memory",
276 "/usr/bin/top -o \"+%MEM\" -w128 -bcn 1 | head -n 57"},
Chris Morin853d3442019-04-01 21:35:13 -0700277 {kCommand, "touch_fw_version",
Stephen Boyddb9eb2f2020-08-11 11:25:41 -0700278 "grep -aE"
Chris Morin853d3442019-04-01 21:35:13 -0700279 " -e 'synaptics: Touchpad model'"
280 " -e 'chromeos-[a-z]*-touch-[a-z]*-update'"
281 " /var/log/messages | tail -n 20"},
282 {kCommand, "tpm-firmware-updater", "/usr/share/userfeedback/scripts/getmsgs "
283 "/var/log/tpm-firmware-updater.log"},
Mattias Nissler887dce22017-07-03 14:44:35 +0200284 // TODO(jorgelo,mnissler): Don't run this as root.
285 // On TPM 1.2 devices this will likely require adding a new user to the 'tss'
286 // group.
287 // On TPM 2.0 devices 'get_version_info' uses D-Bus and therefore can run as
288 // any user.
Chris Morin853d3442019-04-01 21:35:13 -0700289 {kCommand, "tpm_version", "/usr/sbin/tpm-manager get_version_info", kRoot,
290 kRoot},
291 {kCommand, "atmel_ts_refs",
292 "/opt/google/touch/scripts/atmel_tools.sh ts r", kRoot, kRoot},
293 {kCommand, "atmel_tp_refs",
294 "/opt/google/touch/scripts/atmel_tools.sh tp r", kRoot, kRoot},
295 {kCommand, "atmel_ts_deltas",
296 "/opt/google/touch/scripts/atmel_tools.sh ts d", kRoot, kRoot},
297 {kCommand, "atmel_tp_deltas",
298 "/opt/google/touch/scripts/atmel_tools.sh tp d", kRoot, kRoot},
299 {kFile, "stateful_trim_state", "/var/lib/trim/stateful_trim_state"},
300 {kFile, "stateful_trim_data", "/var/lib/trim/stateful_trim_data"},
301 {kFile, "ui_log", "/var/log/ui/ui.LATEST"},
302 {kCommand, "uname", "/bin/uname -a"},
303 {kCommand, "update_engine.log",
304 "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
305 " s.^./var/log/update_engine/.)"},
Chris Morinca152712019-05-03 13:17:28 -0700306 {kFile, "upstart", "/var/log/upstart.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700307 {kCommand, "uptime", "/usr/bin/cut -d' ' -f1 /proc/uptime"},
308 {kFile, "verified boot", "/var/log/debug_vboot_noisy.log"},
309 {kFile, "vmlog.1.LATEST", "/var/log/vmlog/vmlog.1.LATEST"},
310 {kFile, "vmlog.1.PREVIOUS", "/var/log/vmlog/vmlog.1.PREVIOUS"},
311 {kFile, "vmlog.LATEST", "/var/log/vmlog/vmlog.LATEST"},
312 {kFile, "vmlog.PREVIOUS", "/var/log/vmlog/vmlog.PREVIOUS"},
313 {kFile, "vmstat", "/proc/vmstat"},
314 {kFile, "vpd_2.0", "/var/log/vpd_2.0.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700315 {kFile, "zram compressed data size", "/sys/block/zram0/compr_data_size"},
316 {kFile, "zram original data size", "/sys/block/zram0/orig_data_size"},
317 {kFile, "zram total memory used", "/sys/block/zram0/mem_used_total"},
318 {kFile, "zram total reads", "/sys/block/zram0/num_reads"},
319 {kFile, "zram total writes", "/sys/block/zram0/num_writes"},
320 {kCommand, "zram new stats names",
321 "echo orig_size compr_size used_total limit used_max zero_pages migrated"},
322 {kFile, "zram new stats values", "/sys/block/zram0/mm_stat"},
323 {kFile, "cros_tp version", "/sys/class/chromeos/cros_tp/version"},
324 {kCommand, "cros_tp console", "/usr/sbin/ectool --name=cros_tp console",
325 kRoot, kRoot},
326 {kCommand, "cros_tp frame", "/usr/sbin/ectool --name=cros_tp tpframeget",
327 kRoot, kRoot},
328 {kCommand, "crostini", "/usr/bin/cicerone_client --get_info"},
Sean Paul5ce118f2019-12-05 08:41:32 -0500329 {kFile, "drm_trace", "/sys/kernel/debug/dri/trace",
330 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Elly Jones03cd6d72012-06-11 13:04:28 -0400331 // Stuff pulled out of the original list. These need access to the running X
332 // session, which we'd rather not give to debugd, or return info specific to
333 // the current session (in the setsid(2) sense), which is not useful for
334 // debugd
Chris Morin853d3442019-04-01 21:35:13 -0700335 // {kCommand, "env", "set"},
336 // {kCommand, "setxkbmap", "/usr/bin/setxkbmap -print -query"},
337 // {kCommand, "xrandr", "/usr/bin/xrandr --verbose}
Elly Jones533c7c42012-08-10 15:07:05 -0400338};
339
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700340// netstat and logcat should appear in chrome://system but not in feedback
341// reports. Open sockets may have privacy implications, and logcat is
342// already incorporated via arc-bugreport.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700343const std::vector<Log> kExtraLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800344#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700345 {kCommand, "mm-status", "/usr/bin/modem status"},
Ben Chan36e42282014-02-12 22:32:34 -0800346#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700347 {kCommand, "network-devices", "/usr/bin/connectivity show devices"},
348 {kCommand, "network-services", "/usr/bin/connectivity show services"},
Jeffrey Kardatzke36791f22019-07-11 11:53:22 -0700349 {kCommand, "wifi_status_no_anonymize",
350 "/usr/bin/network_diag --wifi-internal --no-log"},
Chris Morin253a2b02019-04-12 16:04:25 -0700351 // --processes requires root.
352 {kCommand, "netstat",
353 "/sbin/ss --all --query inet --numeric --processes", kRoot, kRoot},
Kansho Nishida6ae546f2019-08-13 17:14:58 +0900354 {kCommand, "logcat",
355 "/usr/bin/nsenter -t1 -m /usr/sbin/android-sh -c '/system/bin/logcat -d'",
Chris Morin253a2b02019-04-12 16:04:25 -0700356 kRoot, kRoot, Log::kDefaultMaxBytes, LogTool::Encoding::kUtf8},
Elly Jones533c7c42012-08-10 15:07:05 -0400357};
358
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700359const std::vector<Log> kFeedbackLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800360#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700361 {kCommand, "mm-status", "/usr/bin/modem status-feedback"},
Ben Chan36e42282014-02-12 22:32:34 -0800362#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700363 {kCommand, "network-devices",
364 "/usr/bin/connectivity show-feedback devices"},
365 {kCommand, "network-services",
366 "/usr/bin/connectivity show-feedback services"},
Jeffrey Kardatzke36791f22019-07-11 11:53:22 -0700367 {kCommand, "wifi_status",
368 "/usr/bin/network_diag --wifi-internal --no-log --anonymize"},
Elly Jones03cd6d72012-06-11 13:04:28 -0400369};
370
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700371// Fills |dictionary| with the contents of the logs in |logs|.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700372void GetLogsInDictionary(const std::vector<Log>& logs,
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800373 base::DictionaryValue* dictionary) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700374 for (const Log& log : logs) {
Hidehiko Abe22667262019-08-15 01:32:23 +0900375 dictionary->SetKey(log.GetName(), base::Value(log.GetLogData()));
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800376 }
377}
378
379// Serializes the |dictionary| into the file with the given |fd| in a JSON
380// format.
381void SerializeLogsAsJSON(const base::DictionaryValue& dictionary,
Eric Caruso0b241882018-04-04 13:43:46 -0700382 const base::ScopedFD& fd) {
Eric Caruso96d03d32017-04-25 18:01:17 -0700383 string logs_json;
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800384 base::JSONWriter::WriteWithOptions(dictionary,
385 base::JSONWriter::OPTIONS_PRETTY_PRINT,
386 &logs_json);
Eric Caruso0b241882018-04-04 13:43:46 -0700387 base::WriteFileDescriptor(fd.get(), logs_json.c_str(), logs_json.size());
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800388}
389
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700390bool GetNamedLogFrom(const string& name,
391 const std::vector<Log>& logs,
Elly Jones533c7c42012-08-10 15:07:05 -0400392 string* result) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700393 for (const Log& log : logs) {
394 if (name == log.GetName()) {
395 *result = log.GetLogData();
Elly Jones533c7c42012-08-10 15:07:05 -0400396 return true;
397 }
398 }
399 *result = "<invalid log name>";
400 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400401}
402
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700403void GetLogsFrom(const std::vector<Log>& logs, LogTool::LogMap* map) {
404 for (const Log& log : logs)
405 (*map)[log.GetName()] = log.GetLogData();
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800406}
407
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600408void GetLsbReleaseInfo(LogTool::LogMap* map) {
409 const base::FilePath lsb_release(kLsbReleasePath);
410 brillo::KeyValueStore store;
411 if (!store.Load(lsb_release)) {
412 // /etc/lsb-release might not be present (cros deploying a new
413 // configuration or no fields set at all). Just print a debug
414 // message and continue.
415 DLOG(INFO) << "Could not load fields from " << lsb_release.value();
416 } else {
417 for (const auto& key : store.GetKeys()) {
418 std::string value;
419 store.GetString(key, &value);
420 (*map)[key] = value;
421 }
422 }
423}
424
425void GetOsReleaseInfo(LogTool::LogMap* map) {
426 brillo::OsReleaseReader reader;
427 reader.Load();
428 for (const auto& key : reader.GetKeys()) {
429 std::string value;
430 reader.GetString(key, &value);
431 (*map)["os-release " + key] = value;
432 }
433}
434
435void PopulateDictionaryValue(const LogTool::LogMap& map,
436 base::DictionaryValue* dictionary) {
437 for (const auto& kv : map) {
438 dictionary->SetString(kv.first, kv.second);
439 }
440}
441
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800442bool CompressXzBuffer(const std::vector<uint8_t>& in_buffer,
443 std::vector<uint8_t>* out_buffer) {
444 size_t out_size = lzma_stream_buffer_bound(in_buffer.size());
445 out_buffer->resize(out_size);
446 size_t out_pos = 0;
447
448 lzma_ret ret = lzma_easy_buffer_encode(
449 LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64, nullptr, in_buffer.data(),
450 in_buffer.size(), out_buffer->data(), &out_pos, out_size);
451
452 if (ret != LZMA_OK) {
453 out_buffer->clear();
454 return false;
455 }
456
457 out_buffer->resize(out_pos);
458 return true;
459}
460
461void GetPerfData(LogTool::LogMap* map) {
462 // Run perf to collect system-wide performance profile when user triggers
463 // feedback report. Perf runs at sampling frequency of ~500 hz (499 is used
464 // to avoid sampling periodic system activities), with callstack in each
465 // sample (-g).
466 std::vector<std::string> perf_args = {
467 "perf", "record", "-a", "-g", "-F", "499"
468 };
469 std::vector<uint8_t> perf_data;
470 int32_t status;
471
472 debugd::PerfTool perf_tool;
473 if (!perf_tool.GetPerfOutput(kPerfDurationSecs, perf_args, &perf_data,
474 nullptr, &status, nullptr))
475 return;
476
477 // XZ compress the profile data.
478 std::vector<uint8_t> perf_data_xz;
479 if (!CompressXzBuffer(perf_data, &perf_data_xz))
480 return;
481
482 // Base64 encode the compressed data.
483 std::string perf_data_str(reinterpret_cast<const char*>(perf_data_xz.data()),
484 perf_data_xz.size());
485 (*map)["perf-data"] =
Chinglin Yu3c8d0a22019-02-20 11:32:52 +0800486 std::string(kPerfDataDescription) +
Chris Morin790fd262019-04-03 20:29:36 -0700487 LogTool::EncodeString(std::move(perf_data_str),
488 LogTool::Encoding::kBase64);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800489}
490
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800491} // namespace
492
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700493Log::Log(Log::LogType type,
494 std::string name,
495 std::string data,
496 std::string user,
497 std::string group,
498 int64_t max_bytes,
Brian Norrisafc9f632019-05-09 14:08:28 -0700499 LogTool::Encoding encoding,
500 bool access_root_mount_ns)
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700501 : type_(type),
502 name_(name),
503 data_(data),
504 user_(user),
505 group_(group),
506 max_bytes_(max_bytes),
Brian Norrisafc9f632019-05-09 14:08:28 -0700507 encoding_(encoding),
508 access_root_mount_ns_(access_root_mount_ns) {}
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700509
510std::string Log::GetName() const {
511 return name_;
512}
513
514std::string Log::GetLogData() const {
515 // The reason this code uses a switch statement on a type enum rather than
516 // using inheritance/virtual dispatch is so that all of the Log objects can
517 // be constructed statically. Switching to heap allocated subclasses of Log
518 // makes the code that declares all of the log entries much more verbose
519 // and harder to understand.
Chris Morin790fd262019-04-03 20:29:36 -0700520 std::string output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700521 switch (type_) {
522 case kCommand:
Chris Morin790fd262019-04-03 20:29:36 -0700523 output = GetCommandLogData();
524 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700525 case kFile:
Chris Morin790fd262019-04-03 20:29:36 -0700526 output = GetFileLogData();
527 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700528 default:
529 return "<unknown log type>";
530 }
Chris Morin790fd262019-04-03 20:29:36 -0700531
532 if (output.empty())
533 return "<empty>";
534
535 return LogTool::EncodeString(std::move(output), encoding_);
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700536}
537
538// TODO(ellyjones): sandbox. crosbug.com/35122
539std::string Log::GetCommandLogData() const {
540 if (type_ != kCommand)
541 return "<log type mismatch>";
542 std::string tailed_cmdline =
543 base::StringPrintf("%s | tail -c %" PRId64, data_.c_str(), max_bytes_);
544 ProcessWithOutput p;
545 if (minijail_disabled_for_test_)
546 p.set_use_minijail(false);
547 if (!user_.empty() && !group_.empty())
548 p.SandboxAs(user_, group_);
Brian Norrisafc9f632019-05-09 14:08:28 -0700549 if (access_root_mount_ns_)
550 p.AllowAccessRootMountNamespace();
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700551 if (!p.Init())
552 return "<not available>";
553 p.AddArg(kShell);
554 p.AddStringOption("-c", tailed_cmdline);
555 if (p.Run())
556 return "<not available>";
557 std::string output;
558 p.GetOutput(&output);
Chris Morin790fd262019-04-03 20:29:36 -0700559 return output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700560}
561
562std::string Log::GetFileLogData() const {
563 if (type_ != kFile)
564 return "<log type mismatch>";
565
566 uid_t old_euid = geteuid();
567 uid_t new_euid = UidForUser(user_);
568 gid_t old_egid = getegid();
569 gid_t new_egid = GidForGroup(group_);
570
571 if (new_euid == -1 || new_egid == -1) {
572 return "<not available>";
573 }
574
575 // Make sure to set group first, since if we set user first we lose root
576 // and therefore the ability to set our effective gid to arbitrary gids.
577 if (setegid(new_egid)) {
578 PLOG(ERROR) << "Failed to set effective group id to " << new_egid;
579 return "<not available>";
580 }
581 if (seteuid(new_euid)) {
582 PLOG(ERROR) << "Failed to set effective user id to " << new_euid;
583 if (setegid(old_egid))
584 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
585 return "<not available>";
586 }
587
588 std::string contents;
589 const base::FilePath path(data_);
590 // Handle special files that don't properly report length/allow lseek.
591 if (base::FilePath("/dev").IsParent(path) ||
592 base::FilePath("/proc").IsParent(path) ||
593 base::FilePath("/sys").IsParent(path)) {
594 if (!base::ReadFileToString(path, &contents))
595 contents = "<not available>";
596 if (contents.size() > max_bytes_)
597 contents.erase(0, contents.size() - max_bytes_);
598 } else {
599 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
600 if (!file.IsValid()) {
601 contents = "<not available>";
602 } else {
603 int64_t length = file.GetLength();
604 if (length > max_bytes_) {
605 file.Seek(base::File::FROM_END, -max_bytes_);
606 length = max_bytes_;
607 }
608 std::vector<char> buf(length);
609 int read = file.ReadAtCurrentPos(buf.data(), buf.size());
610 if (read < 0) {
611 PLOG(ERROR) << "Could not read from file " << path.value();
612 } else {
613 contents = std::string(buf.begin(), buf.begin() + read);
614 }
615 }
616 }
617
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700618 // Make sure we restore our old euid/egid before returning.
619 if (seteuid(old_euid))
620 PLOG(ERROR) << "Failed to restore effective user id to " << old_euid;
621
622 if (setegid(old_egid))
623 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
624
Chris Morin790fd262019-04-03 20:29:36 -0700625 return contents;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700626}
627
628void Log::DisableMinijailForTest() {
629 minijail_disabled_for_test_ = true;
630}
631
632// static
633uid_t Log::UidForUser(const std::string& user) {
634 struct passwd entry;
635 struct passwd* result;
636 std::vector<char> buf(1024);
637 getpwnam_r(user.c_str(), &entry, &buf[0], buf.size(), &result);
638 if (!result) {
639 LOG(ERROR) << "User not found: " << user;
640 return -1;
641 }
642 return entry.pw_uid;
643}
644
645// static
646gid_t Log::GidForGroup(const std::string& group) {
647 struct group entry;
648 struct group* result;
649 std::vector<char> buf(1024);
650 getgrnam_r(group.c_str(), &entry, &buf[0], buf.size(), &result);
651 if (!result) {
652 LOG(ERROR) << "Group not found: " << group;
653 return -1;
654 }
655 return entry.gr_gid;
656}
657
mhasankaf5251d2020-04-29 18:53:03 -0700658LogTool::LogTool(
659 scoped_refptr<dbus::Bus> bus,
660 std::unique_ptr<org::chromium::CryptohomeInterfaceProxyInterface>
661 cryptohome_proxy,
662 std::unique_ptr<LogTool::Log> arc_bug_report_log,
663 const base::FilePath& daemon_store_base_dir)
664 : bus_(bus),
665 cryptohome_proxy_(std::move(cryptohome_proxy)),
666 arc_bug_report_log_(std::move(arc_bug_report_log)),
667 daemon_store_base_dir_(daemon_store_base_dir) {}
mhasank80cbe4d2020-04-02 22:46:08 -0700668
mhasank4f599d32020-04-09 22:07:35 -0700669LogTool::LogTool(scoped_refptr<dbus::Bus> bus)
mhasankaf5251d2020-04-29 18:53:03 -0700670 : LogTool(bus,
671 std::make_unique<org::chromium::CryptohomeInterfaceProxy>(bus),
672 std::make_unique<ArcBugReportLog>(),
673 base::FilePath(kDaemonStoreBaseDir)) {}
mhasank4f599d32020-04-09 22:07:35 -0700674
mhasank80cbe4d2020-04-02 22:46:08 -0700675base::FilePath LogTool::GetArcBugReportBackupFilePath
676 (const std::string& userhash) {
mhasank86c46c72020-08-13 15:36:29 -0700677 CHECK(brillo::cryptohome::home::IsSanitizedUserName(userhash))
678 << "Invalid userhash '" << userhash << "'";
679
mhasank80cbe4d2020-04-02 22:46:08 -0700680 return daemon_store_base_dir_
681 .Append(userhash)
682 .Append(kArcBugReportBackupFileName);
683}
684
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700685void LogTool::CreateConnectivityReport(bool wait_for_results) {
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700686 // Perform ConnectivityTrial to report connection state in feedback log.
Ben Chan8e9f6d02017-09-26 23:04:21 -0700687 auto shill = std::make_unique<org::chromium::flimflam::ManagerProxy>(bus_);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700688 // Give the connection trial time to test the connection and log the results
689 // before collecting the logs for feedback.
690 // TODO(silberst): Replace the simple approach of a single timeout with a more
691 // coordinated effort.
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700692 if (shill && shill->CreateConnectivityReport(nullptr) && wait_for_results)
Eric Carusocc7106c2017-04-27 14:22:42 -0700693 sleep(kConnectionTesterTimeoutSeconds);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700694}
695
Eric Carusoc93a15c2017-04-24 16:15:12 -0700696string LogTool::GetLog(const string& name) {
Elly Jones533c7c42012-08-10 15:07:05 -0400697 string result;
Ben Chancf7d6412017-08-10 22:30:09 -0700698 GetNamedLogFrom(name, kCommandLogs, &result)
699 || GetNamedLogFrom(name, kExtraLogs, &result)
700 || GetNamedLogFrom(name, kFeedbackLogs, &result);
Elly Jones533c7c42012-08-10 15:07:05 -0400701 return result;
702}
703
Eric Carusof9091f82017-04-28 14:18:59 -0700704LogTool::LogMap LogTool::GetAllLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700705 CreateConnectivityReport(false);
Elly Jones533c7c42012-08-10 15:07:05 -0400706 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700707 GetLogsFrom(kCommandLogs, &result);
708 GetLogsFrom(kExtraLogs, &result);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600709 GetLsbReleaseInfo(&result);
710 GetOsReleaseInfo(&result);
Elly Jones533c7c42012-08-10 15:07:05 -0400711 return result;
712}
713
Brian Norrisca4fc042018-04-03 00:24:26 -0700714LogTool::LogMap LogTool::GetAllDebugLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700715 CreateConnectivityReport(true);
Brian Norrisca4fc042018-04-03 00:24:26 -0700716 LogMap result;
717 GetLogsFrom(kCommandLogs, &result);
718 GetLogsFrom(kExtraLogs, &result);
mhasankd2b84882020-05-04 17:02:19 -0700719 result[arc_bug_report_log_->GetName()] = GetArcBugReport("", nullptr);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600720 GetLsbReleaseInfo(&result);
721 GetOsReleaseInfo(&result);
Brian Norrisca4fc042018-04-03 00:24:26 -0700722 return result;
723}
724
mhasank4f599d32020-04-09 22:07:35 -0700725void LogTool::GetBigFeedbackLogs(const base::ScopedFD& fd,
726 const std::string& username) {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700727 CreateConnectivityReport(true);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800728 LogMap map;
729 GetPerfData(&map);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800730 base::DictionaryValue dictionary;
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700731 GetLogsInDictionary(kCommandLogs, &dictionary);
732 GetLogsInDictionary(kFeedbackLogs, &dictionary);
mhasankd2b84882020-05-04 17:02:19 -0700733 bool is_backup;
734 std::string arc_bug_report = GetArcBugReport(username, &is_backup);
735 dictionary.SetKey(kArcBugReportBackupKey,
736 base::Value(is_backup ? "true" : "false"));
mhasankaf5251d2020-04-29 18:53:03 -0700737 dictionary.SetKey(arc_bug_report_log_->GetName(),
mhasankd2b84882020-05-04 17:02:19 -0700738 base::Value(arc_bug_report));
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600739 GetLsbReleaseInfo(&map);
740 GetOsReleaseInfo(&map);
741 PopulateDictionaryValue(map, &dictionary);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800742 SerializeLogsAsJSON(dictionary, fd);
743}
744
mhasank4f599d32020-04-09 22:07:35 -0700745std::string GetSanitizedUsername(
746 org::chromium::CryptohomeInterfaceProxyInterface* cryptohome_proxy,
mhasankaf5251d2020-04-29 18:53:03 -0700747 const std::string& username) {
mhasank4f599d32020-04-09 22:07:35 -0700748 if (username.empty()) {
749 return std::string();
750 }
751
752 std::string sanitized_username;
753 brillo::ErrorPtr error;
754 if (!cryptohome_proxy->GetSanitizedUsername(username, &sanitized_username,
755 &error)) {
756 LOG(ERROR) << "Failed to call GetSanitizedUsername, error: "
757 << error->GetMessage();
758 return std::string();
759 }
760
761 return sanitized_username;
762}
763
mhasankd2b84882020-05-04 17:02:19 -0700764std::string LogTool::GetArcBugReport(const std::string& username,
765 bool* is_backup) {
766 if (is_backup) {
767 *is_backup = true;
768 }
mhasank4f599d32020-04-09 22:07:35 -0700769 std::string userhash =
770 GetSanitizedUsername(cryptohome_proxy_.get(), username);
771
772 std::string contents;
773 if (userhash.empty() ||
774 arc_bug_report_backups_.find(userhash) == arc_bug_report_backups_.end() ||
775 !base::ReadFileToString(GetArcBugReportBackupFilePath(userhash),
776 &contents)) {
777 // If |userhash| was not empty, but was not found in the backup set
778 // or the file did not exist, attempt to delete the file.
779 if (!userhash.empty()) {
780 DeleteArcBugReportBackup(userhash);
781 }
mhasankd2b84882020-05-04 17:02:19 -0700782 if (is_backup) {
783 *is_backup = false;
784 }
mhasankaf5251d2020-04-29 18:53:03 -0700785 contents = arc_bug_report_log_->GetLogData();
mhasank4f599d32020-04-09 22:07:35 -0700786 }
787
788 return contents;
789}
790
mhasank80cbe4d2020-04-02 22:46:08 -0700791void LogTool::BackupArcBugReport(const std::string& userhash) {
792 DLOG(INFO) << "Backing up ARC bug report";
793
794 const base::FilePath reportPath = GetArcBugReportBackupFilePath(userhash);
mhasankaf5251d2020-04-29 18:53:03 -0700795 const std::string logData = arc_bug_report_log_->GetLogData();
mhasank4f599d32020-04-09 22:07:35 -0700796 if (base::WriteFile(reportPath, logData.c_str(), logData.length())) {
797 arc_bug_report_backups_.insert(userhash);
798 } else {
mhasank80cbe4d2020-04-02 22:46:08 -0700799 PLOG(ERROR) << "Failed to backup ARC bug report";
800 }
801}
802
803void LogTool::DeleteArcBugReportBackup(const std::string& userhash) {
804 DLOG(INFO) << "Deleting the ARC bug report backup";
805
806 const base::FilePath reportPath = GetArcBugReportBackupFilePath(userhash);
mhasank4f599d32020-04-09 22:07:35 -0700807 arc_bug_report_backups_.erase(userhash);
mhasank80cbe4d2020-04-02 22:46:08 -0700808 if (!base::DeleteFile(reportPath, false)) {
809 PLOG(ERROR) << "Failed to delete ARC bug report backup";
810 }
811}
812
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700813void LogTool::GetJournalLog(const base::ScopedFD& fd) {
Chris Morin790fd262019-04-03 20:29:36 -0700814 Log journal(kCommand, "journal.export", "journalctl -n 10000 -o export",
815 "syslog", "syslog", 10 * 1024 * 1024, LogTool::Encoding::kBinary);
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700816 std::string output = journal.GetLogData();
Chris Morin790fd262019-04-03 20:29:36 -0700817 base::WriteFileDescriptor(fd.get(), output.data(), output.size());
818}
819
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700820// static
Chris Morin790fd262019-04-03 20:29:36 -0700821string LogTool::EncodeString(string value,
822 LogTool::Encoding source_encoding) {
823 if (source_encoding == LogTool::Encoding::kBinary)
824 return value;
825
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700826 if (source_encoding == LogTool::Encoding::kAutodetect) {
827 if (base::IsStringUTF8(value))
828 return value;
Chris Morin790fd262019-04-03 20:29:36 -0700829 source_encoding = LogTool::Encoding::kBase64;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700830 }
831
832 if (source_encoding == LogTool::Encoding::kUtf8) {
833 string output;
834 const char* src = value.data();
835 int32_t src_len = static_cast<int32_t>(value.length());
836
837 output.reserve(value.size());
838 for (int32_t char_index = 0; char_index < src_len; char_index++) {
839 uint32_t code_point;
840 if (!base::ReadUnicodeCharacter(src, src_len, &char_index, &code_point) ||
841 !base::IsValidCharacter(code_point)) {
842 // Replace invalid characters with U+FFFD REPLACEMENT CHARACTER.
843 code_point = 0xFFFD;
844 }
845 base::WriteUnicodeCharacter(code_point, &output);
846 }
847 return output;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700848 }
Chris Morin853d3442019-04-01 21:35:13 -0700849
850 base::Base64Encode(value, &value);
851 return "<base64>: " + value;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700852}
853
Ben Chana0011d82014-05-13 00:19:29 -0700854} // namespace debugd