blob: 3aeec85fc2b100ba6d29816dbd95197a36143679 [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>
40
Elly Jones03cd6d72012-06-11 13:04:28 -040041namespace debugd {
42
Elly Jones03cd6d72012-06-11 13:04:28 -040043using std::string;
Elly Jones03cd6d72012-06-11 13:04:28 -040044
Eric Caruso96d03d32017-04-25 18:01:17 -070045using Strings = std::vector<string>;
Elly Jones03cd6d72012-06-11 13:04:28 -040046
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080047namespace {
48
Ben Chanaf125862017-02-08 23:11:18 -080049const char kRoot[] = "root";
50const char kShell[] = "/bin/sh";
Kartik Hegde1c4b97b2018-09-09 19:09:34 -060051constexpr char kLsbReleasePath[] = "/etc/lsb-release";
mhasank80cbe4d2020-04-02 22:46:08 -070052constexpr char kArcBugReportBackupFileName[] = "arc-bugreport.log";
mhasankd2b84882020-05-04 17:02:19 -070053constexpr char kArcBugReportBackupKey[] = "arc-bugreport-backup";
mhasank80cbe4d2020-04-02 22:46:08 -070054constexpr char kDaemonStoreBaseDir[] = "/run/daemon-store/debugd/";
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080055
56// Minimum time in seconds needed to allow shill to test active connections.
57const int kConnectionTesterTimeoutSeconds = 5;
Ben Chanf6cd93a2012-10-14 19:37:00 -070058
Chinglin Yuaeb4ec72018-12-10 18:53:30 +080059// Default running perf for 2 seconds.
60constexpr const int kPerfDurationSecs = 2;
Chinglin Yu3c8d0a22019-02-20 11:32:52 +080061// TODO(chinglinyu) Remove after crbug/934702 is fixed.
62// The following description is added to 'perf-data' as a temporary solution
63// before the update of feedback disclosure to users is done in crbug/934702.
64constexpr const char kPerfDataDescription[] =
65 "perf-data contains performance profiling information about how much time "
66 "the system spends on various activities (program execution stack traces). "
67 "This might reveal some information about what system features and "
68 "resources are being used. The full detail of perf-data can be found in "
69 "the PerfDataProto protocol buffer message type in the chromium source "
70 "repository.\n";
Chinglin Yuaeb4ec72018-12-10 18:53:30 +080071
Eric Carusoa879fd92017-10-11 12:57:10 -070072#define CMD_KERNEL_MODULE_PARAMS(module_name) \
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070073 "cd /sys/module/" #module_name "/parameters 2>/dev/null && grep -sH ^ *"
Eric Carusoa879fd92017-10-11 12:57:10 -070074
Fletcher Woodruff07c28532019-01-24 11:08:53 -070075using Log = LogTool::Log;
76constexpr Log::LogType kCommand = Log::kCommand;
77constexpr Log::LogType kFile = Log::kFile;
mhasankaf5251d2020-04-29 18:53:03 -070078
79class ArcBugReportLog : public LogTool::Log {
80 public:
81 ArcBugReportLog()
82 : Log(kCommand,
83 "arc-bugreport",
84 "/usr/bin/nsenter -t1 -m /usr/sbin/android-sh -c "
85 "/system/bin/arc-bugreport",
86 kRoot,
87 kRoot,
88 10 * 1024 * 1024 /*10 MiB*/,
89 LogTool::Encoding::kUtf8) {}
90
91 virtual ~ArcBugReportLog() = default;
92};
mhasank80cbe4d2020-04-02 22:46:08 -070093
Miriam Zimmermand91d8e72019-06-27 12:24:04 -070094// NOTE: IF YOU ADD AN ENTRY TO THIS LIST, PLEASE:
95// * add a row to http://go/cros-feedback-audit and fill it out
Miriam Zimmerman4f142ba2020-06-01 14:15:21 -070096// * email cros-telemetry@
Miriam Zimmermand91d8e72019-06-27 12:24:04 -070097// (Eventually we'll have a better process, but for now please do this.)
Fletcher Woodruff07c28532019-01-24 11:08:53 -070098const std::vector<Log> kCommandLogs {
Mike Frysingerb0350992018-09-14 13:45:35 -040099 // We need to enter init's mount namespace because it has /home/chronos
100 // mounted which is where the consent knob lives. We don't have that mount
101 // in our own mount namespace (by design). https://crbug.com/884249
Chris Morin853d3442019-04-01 21:35:13 -0700102 {kCommand, "CLIENT_ID", "/usr/bin/nsenter -t1 -m /usr/bin/metrics_client -i",
103 kRoot, kDebugfsGroup},
104 {kCommand, "LOGDATE", "/bin/date"},
Yusuke Sato27a31672019-04-29 15:26:37 -0700105 // We need to enter init's mount namespace to access /home/root. Also, we use
106 // neither ARC container's mount namespace (with android-sh) nor
107 // /opt/google/containers/android/rootfs/android-data/ so that we can get
108 // results even when the container is down.
109 {kCommand, "android_app_storage", "/usr/bin/nsenter -t1 -m "
110 "/bin/sh -c \"/usr/bin/du -h /home/root/*/android-data/data/\"",
111 kRoot, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700112 {kFile, "atrus_logs", "/var/log/atrus.log"},
113 {kFile, "authpolicy", "/var/log/authpolicy.log"},
Brian Norrisafc9f632019-05-09 14:08:28 -0700114 {kCommand, "bootstat_summary", "/usr/bin/bootstat_summary",
115 SandboxedProcess::kDefaultUser, SandboxedProcess::kDefaultGroup,
116 Log::kDefaultMaxBytes, LogTool::Encoding::kAutodetect, true},
Craig Hesling5c384b52019-04-20 15:18:06 -0700117 {kFile, "bio_crypto_init.LATEST",
118 "/var/log/bio_crypto_init/bio_crypto_init.LATEST"},
119 {kFile, "bio_crypto_init.PREVIOUS",
120 "/var/log/bio_crypto_init/bio_crypto_init.PREVIOUS"},
Chris Morin853d3442019-04-01 21:35:13 -0700121 {kFile, "biod.LATEST", "/var/log/biod/biod.LATEST"},
122 {kFile, "biod.PREVIOUS", "/var/log/biod/biod.PREVIOUS"},
Craig Hesling4c3891e2019-04-20 12:53:54 -0700123 {kFile, "bio_fw_updater.LATEST", "/var/log/biod/bio_fw_updater.LATEST"},
124 {kFile, "bio_fw_updater.PREVIOUS", "/var/log/biod/bio_fw_updater.PREVIOUS"},
Chris Morin853d3442019-04-01 21:35:13 -0700125 {kFile, "bios_info", "/var/log/bios_info.txt"},
126 {kCommand, "bios_log", "cat /sys/firmware/log "
127 "/proc/device-tree/chosen/ap-console-buffer 2>/dev/null"},
128 {kFile, "bios_times", "/var/log/bios_times.txt"},
Anand K Mistryccceb1e2020-01-16 14:00:49 +1100129 // Slow or non-responsive block devices could cause this command to stall. Use
130 // a timeout to prevent this command from blocking log fetching. This command
131 // is expected to take O(100ms) in the normal case.
132 {kCommand, "blkid", "timeout -s KILL 5s /sbin/blkid", kRoot, kRoot},
Chris Morin853d3442019-04-01 21:35:13 -0700133 {kFile, "buddyinfo", "/proc/buddyinfo"},
134 {kCommand, "cbi_info", "/usr/share/userfeedback/scripts/cbi_info", kRoot,
135 kRoot},
136 {kFile, "cheets_log", "/var/log/arc.log"},
137 {kFile, "clobber.log", "/var/log/clobber.log"},
138 {kFile, "clobber-state.log", "/var/log/clobber-state.log"},
Sonny Raobd3dc002020-05-27 21:40:35 -0700139 {kCommand, "chromeos-pgmem", "/usr/bin/chromeos-pgmem", kRoot, kRoot},
Chris Morin853d3442019-04-01 21:35:13 -0700140 {kFile, "chrome_system_log", "/var/log/chrome/chrome"},
141 {kFile, "chrome_system_log.PREVIOUS", "/var/log/chrome/chrome.PREVIOUS"},
Mike Frysinger32cdf3e2017-08-14 18:17:06 -0400142 // There might be more than one record, so grab them all.
143 // Plus, for <linux-3.19, it's named "console-ramoops", but for newer
144 // versions, it's named "console-ramoops-#".
Chris Morin853d3442019-04-01 21:35:13 -0700145 {kCommand, "console-ramoops",
146 "cat /sys/fs/pstore/console-ramoops* 2>/dev/null"},
147 {kFile, "cpuinfo", "/proc/cpuinfo"},
148 {kFile, "cr50_version", "/var/cache/cr50-version"},
149 {kFile, "cros_ec.log", "/var/log/cros_ec.log"},
150 {kFile, "cros_ec.previous", "/var/log/cros_ec.previous"},
151 {kFile, "cros_ec_panicinfo", "/sys/kernel/debug/cros_ec/panicinfo",
152 SandboxedProcess::kDefaultUser, kDebugfsGroup},
153 {kFile, "cros_ec_pdinfo", "/sys/kernel/debug/cros_ec/pdinfo",
154 SandboxedProcess::kDefaultUser, kDebugfsGroup},
155 {kFile, "cros_fp.previous", "/var/log/cros_fp.previous"},
156 {kFile, "cros_fp.log", "/var/log/cros_fp.log"},
Mathew Kingd7a72622019-06-27 09:56:34 -0600157 {kFile, "cros_ish.previous", "/var/log/cros_ish.previous"},
158 {kFile, "cros_ish.log", "/var/log/cros_ish.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700159 {kCommand, "dmesg", "/bin/dmesg"},
160 {kFile, "ec_info", "/var/log/ec_info.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700161 {kCommand, "edid-decode",
Stephen Boydaf3118b2020-08-11 11:42:22 -0700162 "for f in /sys/class/drm/card?-*/edid; do "
Chris Morin853d3442019-04-01 21:35:13 -0700163 "echo \"----------- ${f}\"; "
Chris Morin853d3442019-04-01 21:35:13 -0700164 // edid-decode's stderr output is redundant, so silence it.
Jeffrey Kardatzke9ba9f322019-08-29 10:23:14 -0700165 "edid-decode \"${f}\" 2>/dev/null; "
Chris Morin853d3442019-04-01 21:35:13 -0700166 "done"},
167 {kFile, "eventlog", "/var/log/eventlog.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700168 {kCommand, "font_info", "/usr/share/userfeedback/scripts/font_info"},
Kuo-Hsin Yang95296e12020-03-06 17:52:35 +0800169 {kCommand, "framebuffer", "cat /sys/kernel/debug/dri/?/framebuffer",
170 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700171 {kFile, "fwupd_state", "/var/lib/fwupd/state.json"},
172 {kCommand, "sensor_info", "/usr/share/userfeedback/scripts/sensor_info"},
173 {kFile, "hammerd", "/var/log/hammerd.log"},
174 {kCommand, "hardware_class", "/usr/bin/crossystem hwid"},
Yong Hong15e4b032020-03-05 15:41:31 +0800175 {kFile, "hardware_verification_report",
176 "/var/cache/hardware_verifier.result"},
Chris Morin853d3442019-04-01 21:35:13 -0700177 {kCommand, "hostname", "/bin/hostname"},
178 {kFile, "i915_gem_gtt", "/sys/kernel/debug/dri/0/i915_gem_gtt",
179 SandboxedProcess::kDefaultUser, kDebugfsGroup},
180 {kFile, "i915_gem_objects", "/sys/kernel/debug/dri/0/i915_gem_objects",
181 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700182 {kCommand, "i915_error_state",
183 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2>/dev/null",
184 SandboxedProcess::kDefaultUser, kDebugfsGroup, Log::kDefaultMaxBytes,
Chris Morin790fd262019-04-03 20:29:36 -0700185 LogTool::Encoding::kBase64},
Chris Morin853d3442019-04-01 21:35:13 -0700186 {kCommand, "ifconfig", "/bin/ifconfig -a"},
187 {kFile, "input_devices", "/proc/bus/input/devices"},
Eric Carusob1820c02017-08-24 15:39:56 -0700188 // Hardware capabilities of the wiphy device.
Alex Levine1c6d572019-09-17 14:45:33 -0700189 {kFile, "interrupts", "/proc/interrupts"},
Chris Morin853d3442019-04-01 21:35:13 -0700190 {kCommand, "iw_list", "/usr/sbin/iw list"},
Eric Carusoa879fd92017-10-11 12:57:10 -0700191#if USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700192 {kCommand, "iwlmvm_module_params", CMD_KERNEL_MODULE_PARAMS(iwlmvm)},
193 {kCommand, "iwlwifi_module_params", CMD_KERNEL_MODULE_PARAMS(iwlwifi)},
Eric Carusoa879fd92017-10-11 12:57:10 -0700194#endif // USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700195 {kCommand, "kernel-crashes",
196 "cat /var/spool/crash/kernel.*.kcrash 2>/dev/null"},
Anand K Mistryccceb1e2020-01-16 14:00:49 +1100197 {kCommand, "lsblk", "timeout -s KILL 5s lsblk -a", kRoot, kRoot,
198 Log::kDefaultMaxBytes, LogTool::Encoding::kAutodetect, true},
Chris Morin853d3442019-04-01 21:35:13 -0700199 {kCommand, "lsmod", "lsmod"},
200 {kCommand, "lspci", "/usr/sbin/lspci"},
201 {kCommand, "lsusb", "lsusb && lsusb -t"},
Kuo-Hsin Yang66d89832020-02-10 17:22:28 +0800202 {kFile, "mali_memory", "/sys/kernel/debug/mali0/gpu_memory",
203 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700204 {kFile, "memd.parameters", "/var/log/memd/memd.parameters"},
205 {kCommand, "memd clips", "cat /var/log/memd/memd.clip* 2>/dev/null"},
206 {kFile, "meminfo", "/proc/meminfo"},
207 {kCommand, "memory_spd_info",
208 // mosys may use 'i2c-dev', which may not be loaded yet.
209 "modprobe i2c-dev 2>/dev/null && mosys -l memory spd print all 2>/dev/null",
210 kRoot, kDebugfsGroup},
Simon Quecb63b9c2017-06-19 14:53:31 -0400211 // The sed command finds the EDID blob (starting the line after "value:") and
212 // replaces the serial number with all zeroes.
213 //
214 // The EDID is printed as a hex dump over several lines, each line containing
215 // the contents of 16 bytes. The first 16 bytes are broken down as follows:
216 // uint64_t fixed_pattern; // Always 00 FF FF FF FF FF FF 00.
217 // uint16_t manufacturer_id; // Manufacturer ID, encoded as PNP IDs.
218 // uint16_t product_code; // Manufacturer product code, little-endian.
219 // uint32_t serial_number; // Serial number, little-endian.
220 // Source: https://en.wikipedia.org/wiki/EDID#EDID_1.3_data_format
221 //
222 // The subsequent substitution command looks for the fixed pattern followed by
223 // two 32-bit fields (manufacturer + product, serial number). It replaces the
224 // latter field with 8 bytes of zeroes.
225 //
226 // TODO(crbug.com/731133): Remove the sed command once modetest itself can
227 // remove serial numbers.
Chris Morin853d3442019-04-01 21:35:13 -0700228 {kCommand, "modetest",
229 "(modetest; modetest -M evdi; modetest -M udl) | "
230 "sed -E '/EDID/ {:a;n;/value:/!ba;n;"
231 "s/(00f{12}00)([0-9a-f]{8})([0-9a-f]{8})/\\1\\200000000/}'",
232 kRoot, kRoot},
233 {kFile, "mount-encrypted", "/var/log/mount-encrypted.log"},
234 {kFile, "mountinfo", "/proc/1/mountinfo"},
235 {kCommand, "netlog",
236 "/usr/share/userfeedback/scripts/getmsgs /var/log/net.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700237 {kFile, "nvmap_iovmm", "/sys/kernel/debug/nvmap/iovmm/allocations",
238 SandboxedProcess::kDefaultUser, kDebugfsGroup},
239 {kCommand, "oemdata", "/usr/share/cros/oemdata.sh", kRoot, kRoot},
Kuo-Hsin Yanga69ecc62020-03-11 17:37:11 +0800240 {kFile, "pagetypeinfo", "/proc/pagetypeinfo", kRoot},
Jack Rosenthal3cf794a2020-02-19 13:32:56 -0700241 {kFile, "platform_identity_name",
242 "/run/chromeos-config/v1/identity/platform-name"},
243 {kFile, "platform_identity_model", "/run/chromeos-config/v1/name"},
244 {kFile, "platform_identity_sku", "/run/chromeos-config/v1/identity/sku-id"},
245 {kFile, "platform_identity_whitelabel_tag",
246 "/run/chromeos-config/v1/identity/whitelabel-tag"},
247 {kFile, "platform_identity_customization_id",
248 "/run/chromeos-config/v1/identity/customization-id"},
Chris Morin853d3442019-04-01 21:35:13 -0700249 {kCommand, "power_supply_info", "/usr/bin/power_supply_info"},
250 {kCommand, "power_supply_sysfs", "/usr/bin/print_sysfs_power_supply_data"},
251 {kFile, "powerd.LATEST", "/var/log/power_manager/powerd.LATEST"},
252 {kFile, "powerd.PREVIOUS", "/var/log/power_manager/powerd.PREVIOUS"},
253 {kFile, "powerd.out", "/var/log/powerd.out"},
254 {kFile, "powerwash_count", "/var/log/powerwash_count"},
Brian Norris4cde3d12019-04-16 10:10:34 -0700255 {kCommand, "ps", "/bin/ps auxZ"},
yusukes34171ba2017-04-27 15:46:01 -0700256 // /proc/slabinfo is owned by root and has 0400 permission.
Chris Morin853d3442019-04-01 21:35:13 -0700257 {kFile, "slabinfo", "/proc/slabinfo", kRoot, kRoot},
258 {kFile, "storage_info", "/var/log/storage_info.txt"},
259 {kCommand, "swap_info", "/usr/share/cros/init/swap.sh status 2>/dev/null",
260 SandboxedProcess::kDefaultUser, kDebugfsGroup},
261 {kCommand, "syslog",
262 "/usr/share/userfeedback/scripts/getmsgs /var/log/messages"},
263 {kCommand, "system_log_stats",
264 "echo 'BLOCK_SIZE=1024'; "
265 "find /var/log/ -type f -exec du --block-size=1024 {} + | sort -n -r",
266 kRoot, kRoot},
267 {kCommand, "threads", "/bin/ps -T axo pid,ppid,spid,pcpu,ni,stat,time,comm"},
268 {kFile, "tlsdate", "/var/log/tlsdate.log"},
Nick Sandersad5dc132019-11-15 15:59:42 -0800269 {kCommand, "top thread", "/usr/bin/top -Hbc -w128 -n 1 | head -n 40"},
270 {kCommand, "top memory",
271 "/usr/bin/top -o \"+%MEM\" -w128 -bcn 1 | head -n 57"},
Chris Morin853d3442019-04-01 21:35:13 -0700272 {kCommand, "touch_fw_version",
Stephen Boyddb9eb2f2020-08-11 11:25:41 -0700273 "grep -aE"
Chris Morin853d3442019-04-01 21:35:13 -0700274 " -e 'synaptics: Touchpad model'"
275 " -e 'chromeos-[a-z]*-touch-[a-z]*-update'"
276 " /var/log/messages | tail -n 20"},
277 {kCommand, "tpm-firmware-updater", "/usr/share/userfeedback/scripts/getmsgs "
278 "/var/log/tpm-firmware-updater.log"},
Mattias Nissler887dce22017-07-03 14:44:35 +0200279 // TODO(jorgelo,mnissler): Don't run this as root.
280 // On TPM 1.2 devices this will likely require adding a new user to the 'tss'
281 // group.
282 // On TPM 2.0 devices 'get_version_info' uses D-Bus and therefore can run as
283 // any user.
Chris Morin853d3442019-04-01 21:35:13 -0700284 {kCommand, "tpm_version", "/usr/sbin/tpm-manager get_version_info", kRoot,
285 kRoot},
286 {kCommand, "atmel_ts_refs",
287 "/opt/google/touch/scripts/atmel_tools.sh ts r", kRoot, kRoot},
288 {kCommand, "atmel_tp_refs",
289 "/opt/google/touch/scripts/atmel_tools.sh tp r", kRoot, kRoot},
290 {kCommand, "atmel_ts_deltas",
291 "/opt/google/touch/scripts/atmel_tools.sh ts d", kRoot, kRoot},
292 {kCommand, "atmel_tp_deltas",
293 "/opt/google/touch/scripts/atmel_tools.sh tp d", kRoot, kRoot},
294 {kFile, "stateful_trim_state", "/var/lib/trim/stateful_trim_state"},
295 {kFile, "stateful_trim_data", "/var/lib/trim/stateful_trim_data"},
296 {kFile, "ui_log", "/var/log/ui/ui.LATEST"},
297 {kCommand, "uname", "/bin/uname -a"},
298 {kCommand, "update_engine.log",
299 "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
300 " s.^./var/log/update_engine/.)"},
Chris Morinca152712019-05-03 13:17:28 -0700301 {kFile, "upstart", "/var/log/upstart.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700302 {kCommand, "uptime", "/usr/bin/cut -d' ' -f1 /proc/uptime"},
303 {kFile, "verified boot", "/var/log/debug_vboot_noisy.log"},
304 {kFile, "vmlog.1.LATEST", "/var/log/vmlog/vmlog.1.LATEST"},
305 {kFile, "vmlog.1.PREVIOUS", "/var/log/vmlog/vmlog.1.PREVIOUS"},
306 {kFile, "vmlog.LATEST", "/var/log/vmlog/vmlog.LATEST"},
307 {kFile, "vmlog.PREVIOUS", "/var/log/vmlog/vmlog.PREVIOUS"},
308 {kFile, "vmstat", "/proc/vmstat"},
309 {kFile, "vpd_2.0", "/var/log/vpd_2.0.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700310 {kFile, "zram compressed data size", "/sys/block/zram0/compr_data_size"},
311 {kFile, "zram original data size", "/sys/block/zram0/orig_data_size"},
312 {kFile, "zram total memory used", "/sys/block/zram0/mem_used_total"},
313 {kFile, "zram total reads", "/sys/block/zram0/num_reads"},
314 {kFile, "zram total writes", "/sys/block/zram0/num_writes"},
315 {kCommand, "zram new stats names",
316 "echo orig_size compr_size used_total limit used_max zero_pages migrated"},
317 {kFile, "zram new stats values", "/sys/block/zram0/mm_stat"},
318 {kFile, "cros_tp version", "/sys/class/chromeos/cros_tp/version"},
319 {kCommand, "cros_tp console", "/usr/sbin/ectool --name=cros_tp console",
320 kRoot, kRoot},
321 {kCommand, "cros_tp frame", "/usr/sbin/ectool --name=cros_tp tpframeget",
322 kRoot, kRoot},
323 {kCommand, "crostini", "/usr/bin/cicerone_client --get_info"},
Sean Paul5ce118f2019-12-05 08:41:32 -0500324 {kFile, "drm_trace", "/sys/kernel/debug/dri/trace",
325 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Elly Jones03cd6d72012-06-11 13:04:28 -0400326 // Stuff pulled out of the original list. These need access to the running X
327 // session, which we'd rather not give to debugd, or return info specific to
328 // the current session (in the setsid(2) sense), which is not useful for
329 // debugd
Chris Morin853d3442019-04-01 21:35:13 -0700330 // {kCommand, "env", "set"},
331 // {kCommand, "setxkbmap", "/usr/bin/setxkbmap -print -query"},
332 // {kCommand, "xrandr", "/usr/bin/xrandr --verbose}
Elly Jones533c7c42012-08-10 15:07:05 -0400333};
334
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700335// netstat and logcat should appear in chrome://system but not in feedback
336// reports. Open sockets may have privacy implications, and logcat is
337// already incorporated via arc-bugreport.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700338const std::vector<Log> kExtraLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800339#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700340 {kCommand, "mm-status", "/usr/bin/modem status"},
Ben Chan36e42282014-02-12 22:32:34 -0800341#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700342 {kCommand, "network-devices", "/usr/bin/connectivity show devices"},
343 {kCommand, "network-services", "/usr/bin/connectivity show services"},
Jeffrey Kardatzke36791f22019-07-11 11:53:22 -0700344 {kCommand, "wifi_status_no_anonymize",
345 "/usr/bin/network_diag --wifi-internal --no-log"},
Chris Morin253a2b02019-04-12 16:04:25 -0700346 // --processes requires root.
347 {kCommand, "netstat",
348 "/sbin/ss --all --query inet --numeric --processes", kRoot, kRoot},
Kansho Nishida6ae546f2019-08-13 17:14:58 +0900349 {kCommand, "logcat",
350 "/usr/bin/nsenter -t1 -m /usr/sbin/android-sh -c '/system/bin/logcat -d'",
Chris Morin253a2b02019-04-12 16:04:25 -0700351 kRoot, kRoot, Log::kDefaultMaxBytes, LogTool::Encoding::kUtf8},
Elly Jones533c7c42012-08-10 15:07:05 -0400352};
353
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700354const std::vector<Log> kFeedbackLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800355#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700356 {kCommand, "mm-status", "/usr/bin/modem status-feedback"},
Ben Chan36e42282014-02-12 22:32:34 -0800357#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700358 {kCommand, "network-devices",
359 "/usr/bin/connectivity show-feedback devices"},
360 {kCommand, "network-services",
361 "/usr/bin/connectivity show-feedback services"},
Jeffrey Kardatzke36791f22019-07-11 11:53:22 -0700362 {kCommand, "wifi_status",
363 "/usr/bin/network_diag --wifi-internal --no-log --anonymize"},
Elly Jones03cd6d72012-06-11 13:04:28 -0400364};
365
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700366// Fills |dictionary| with the contents of the logs in |logs|.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700367void GetLogsInDictionary(const std::vector<Log>& logs,
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800368 base::DictionaryValue* dictionary) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700369 for (const Log& log : logs) {
Hidehiko Abe22667262019-08-15 01:32:23 +0900370 dictionary->SetKey(log.GetName(), base::Value(log.GetLogData()));
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800371 }
372}
373
374// Serializes the |dictionary| into the file with the given |fd| in a JSON
375// format.
376void SerializeLogsAsJSON(const base::DictionaryValue& dictionary,
Eric Caruso0b241882018-04-04 13:43:46 -0700377 const base::ScopedFD& fd) {
Eric Caruso96d03d32017-04-25 18:01:17 -0700378 string logs_json;
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800379 base::JSONWriter::WriteWithOptions(dictionary,
380 base::JSONWriter::OPTIONS_PRETTY_PRINT,
381 &logs_json);
Eric Caruso0b241882018-04-04 13:43:46 -0700382 base::WriteFileDescriptor(fd.get(), logs_json.c_str(), logs_json.size());
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800383}
384
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700385bool GetNamedLogFrom(const string& name,
386 const std::vector<Log>& logs,
Elly Jones533c7c42012-08-10 15:07:05 -0400387 string* result) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700388 for (const Log& log : logs) {
389 if (name == log.GetName()) {
390 *result = log.GetLogData();
Elly Jones533c7c42012-08-10 15:07:05 -0400391 return true;
392 }
393 }
394 *result = "<invalid log name>";
395 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400396}
397
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700398void GetLogsFrom(const std::vector<Log>& logs, LogTool::LogMap* map) {
399 for (const Log& log : logs)
400 (*map)[log.GetName()] = log.GetLogData();
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800401}
402
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600403void GetLsbReleaseInfo(LogTool::LogMap* map) {
404 const base::FilePath lsb_release(kLsbReleasePath);
405 brillo::KeyValueStore store;
406 if (!store.Load(lsb_release)) {
407 // /etc/lsb-release might not be present (cros deploying a new
408 // configuration or no fields set at all). Just print a debug
409 // message and continue.
410 DLOG(INFO) << "Could not load fields from " << lsb_release.value();
411 } else {
412 for (const auto& key : store.GetKeys()) {
413 std::string value;
414 store.GetString(key, &value);
415 (*map)[key] = value;
416 }
417 }
418}
419
420void GetOsReleaseInfo(LogTool::LogMap* map) {
421 brillo::OsReleaseReader reader;
422 reader.Load();
423 for (const auto& key : reader.GetKeys()) {
424 std::string value;
425 reader.GetString(key, &value);
426 (*map)["os-release " + key] = value;
427 }
428}
429
430void PopulateDictionaryValue(const LogTool::LogMap& map,
431 base::DictionaryValue* dictionary) {
432 for (const auto& kv : map) {
433 dictionary->SetString(kv.first, kv.second);
434 }
435}
436
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800437bool CompressXzBuffer(const std::vector<uint8_t>& in_buffer,
438 std::vector<uint8_t>* out_buffer) {
439 size_t out_size = lzma_stream_buffer_bound(in_buffer.size());
440 out_buffer->resize(out_size);
441 size_t out_pos = 0;
442
443 lzma_ret ret = lzma_easy_buffer_encode(
444 LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64, nullptr, in_buffer.data(),
445 in_buffer.size(), out_buffer->data(), &out_pos, out_size);
446
447 if (ret != LZMA_OK) {
448 out_buffer->clear();
449 return false;
450 }
451
452 out_buffer->resize(out_pos);
453 return true;
454}
455
456void GetPerfData(LogTool::LogMap* map) {
457 // Run perf to collect system-wide performance profile when user triggers
458 // feedback report. Perf runs at sampling frequency of ~500 hz (499 is used
459 // to avoid sampling periodic system activities), with callstack in each
460 // sample (-g).
461 std::vector<std::string> perf_args = {
462 "perf", "record", "-a", "-g", "-F", "499"
463 };
464 std::vector<uint8_t> perf_data;
465 int32_t status;
466
467 debugd::PerfTool perf_tool;
468 if (!perf_tool.GetPerfOutput(kPerfDurationSecs, perf_args, &perf_data,
469 nullptr, &status, nullptr))
470 return;
471
472 // XZ compress the profile data.
473 std::vector<uint8_t> perf_data_xz;
474 if (!CompressXzBuffer(perf_data, &perf_data_xz))
475 return;
476
477 // Base64 encode the compressed data.
478 std::string perf_data_str(reinterpret_cast<const char*>(perf_data_xz.data()),
479 perf_data_xz.size());
480 (*map)["perf-data"] =
Chinglin Yu3c8d0a22019-02-20 11:32:52 +0800481 std::string(kPerfDataDescription) +
Chris Morin790fd262019-04-03 20:29:36 -0700482 LogTool::EncodeString(std::move(perf_data_str),
483 LogTool::Encoding::kBase64);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800484}
485
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800486} // namespace
487
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700488Log::Log(Log::LogType type,
489 std::string name,
490 std::string data,
491 std::string user,
492 std::string group,
493 int64_t max_bytes,
Brian Norrisafc9f632019-05-09 14:08:28 -0700494 LogTool::Encoding encoding,
495 bool access_root_mount_ns)
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700496 : type_(type),
497 name_(name),
498 data_(data),
499 user_(user),
500 group_(group),
501 max_bytes_(max_bytes),
Brian Norrisafc9f632019-05-09 14:08:28 -0700502 encoding_(encoding),
503 access_root_mount_ns_(access_root_mount_ns) {}
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700504
505std::string Log::GetName() const {
506 return name_;
507}
508
509std::string Log::GetLogData() const {
510 // The reason this code uses a switch statement on a type enum rather than
511 // using inheritance/virtual dispatch is so that all of the Log objects can
512 // be constructed statically. Switching to heap allocated subclasses of Log
513 // makes the code that declares all of the log entries much more verbose
514 // and harder to understand.
Chris Morin790fd262019-04-03 20:29:36 -0700515 std::string output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700516 switch (type_) {
517 case kCommand:
Chris Morin790fd262019-04-03 20:29:36 -0700518 output = GetCommandLogData();
519 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700520 case kFile:
Chris Morin790fd262019-04-03 20:29:36 -0700521 output = GetFileLogData();
522 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700523 default:
524 return "<unknown log type>";
525 }
Chris Morin790fd262019-04-03 20:29:36 -0700526
527 if (output.empty())
528 return "<empty>";
529
530 return LogTool::EncodeString(std::move(output), encoding_);
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700531}
532
533// TODO(ellyjones): sandbox. crosbug.com/35122
534std::string Log::GetCommandLogData() const {
535 if (type_ != kCommand)
536 return "<log type mismatch>";
537 std::string tailed_cmdline =
538 base::StringPrintf("%s | tail -c %" PRId64, data_.c_str(), max_bytes_);
539 ProcessWithOutput p;
540 if (minijail_disabled_for_test_)
541 p.set_use_minijail(false);
542 if (!user_.empty() && !group_.empty())
543 p.SandboxAs(user_, group_);
Brian Norrisafc9f632019-05-09 14:08:28 -0700544 if (access_root_mount_ns_)
545 p.AllowAccessRootMountNamespace();
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700546 if (!p.Init())
547 return "<not available>";
548 p.AddArg(kShell);
549 p.AddStringOption("-c", tailed_cmdline);
550 if (p.Run())
551 return "<not available>";
552 std::string output;
553 p.GetOutput(&output);
Chris Morin790fd262019-04-03 20:29:36 -0700554 return output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700555}
556
557std::string Log::GetFileLogData() const {
558 if (type_ != kFile)
559 return "<log type mismatch>";
560
561 uid_t old_euid = geteuid();
562 uid_t new_euid = UidForUser(user_);
563 gid_t old_egid = getegid();
564 gid_t new_egid = GidForGroup(group_);
565
566 if (new_euid == -1 || new_egid == -1) {
567 return "<not available>";
568 }
569
570 // Make sure to set group first, since if we set user first we lose root
571 // and therefore the ability to set our effective gid to arbitrary gids.
572 if (setegid(new_egid)) {
573 PLOG(ERROR) << "Failed to set effective group id to " << new_egid;
574 return "<not available>";
575 }
576 if (seteuid(new_euid)) {
577 PLOG(ERROR) << "Failed to set effective user id to " << new_euid;
578 if (setegid(old_egid))
579 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
580 return "<not available>";
581 }
582
583 std::string contents;
584 const base::FilePath path(data_);
585 // Handle special files that don't properly report length/allow lseek.
586 if (base::FilePath("/dev").IsParent(path) ||
587 base::FilePath("/proc").IsParent(path) ||
588 base::FilePath("/sys").IsParent(path)) {
589 if (!base::ReadFileToString(path, &contents))
590 contents = "<not available>";
591 if (contents.size() > max_bytes_)
592 contents.erase(0, contents.size() - max_bytes_);
593 } else {
594 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
595 if (!file.IsValid()) {
596 contents = "<not available>";
597 } else {
598 int64_t length = file.GetLength();
599 if (length > max_bytes_) {
600 file.Seek(base::File::FROM_END, -max_bytes_);
601 length = max_bytes_;
602 }
603 std::vector<char> buf(length);
604 int read = file.ReadAtCurrentPos(buf.data(), buf.size());
605 if (read < 0) {
606 PLOG(ERROR) << "Could not read from file " << path.value();
607 } else {
608 contents = std::string(buf.begin(), buf.begin() + read);
609 }
610 }
611 }
612
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700613 // Make sure we restore our old euid/egid before returning.
614 if (seteuid(old_euid))
615 PLOG(ERROR) << "Failed to restore effective user id to " << old_euid;
616
617 if (setegid(old_egid))
618 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
619
Chris Morin790fd262019-04-03 20:29:36 -0700620 return contents;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700621}
622
623void Log::DisableMinijailForTest() {
624 minijail_disabled_for_test_ = true;
625}
626
627// static
628uid_t Log::UidForUser(const std::string& user) {
629 struct passwd entry;
630 struct passwd* result;
631 std::vector<char> buf(1024);
632 getpwnam_r(user.c_str(), &entry, &buf[0], buf.size(), &result);
633 if (!result) {
634 LOG(ERROR) << "User not found: " << user;
635 return -1;
636 }
637 return entry.pw_uid;
638}
639
640// static
641gid_t Log::GidForGroup(const std::string& group) {
642 struct group entry;
643 struct group* result;
644 std::vector<char> buf(1024);
645 getgrnam_r(group.c_str(), &entry, &buf[0], buf.size(), &result);
646 if (!result) {
647 LOG(ERROR) << "Group not found: " << group;
648 return -1;
649 }
650 return entry.gr_gid;
651}
652
mhasankaf5251d2020-04-29 18:53:03 -0700653LogTool::LogTool(
654 scoped_refptr<dbus::Bus> bus,
655 std::unique_ptr<org::chromium::CryptohomeInterfaceProxyInterface>
656 cryptohome_proxy,
657 std::unique_ptr<LogTool::Log> arc_bug_report_log,
658 const base::FilePath& daemon_store_base_dir)
659 : bus_(bus),
660 cryptohome_proxy_(std::move(cryptohome_proxy)),
661 arc_bug_report_log_(std::move(arc_bug_report_log)),
662 daemon_store_base_dir_(daemon_store_base_dir) {}
mhasank80cbe4d2020-04-02 22:46:08 -0700663
mhasank4f599d32020-04-09 22:07:35 -0700664LogTool::LogTool(scoped_refptr<dbus::Bus> bus)
mhasankaf5251d2020-04-29 18:53:03 -0700665 : LogTool(bus,
666 std::make_unique<org::chromium::CryptohomeInterfaceProxy>(bus),
667 std::make_unique<ArcBugReportLog>(),
668 base::FilePath(kDaemonStoreBaseDir)) {}
mhasank4f599d32020-04-09 22:07:35 -0700669
mhasank80cbe4d2020-04-02 22:46:08 -0700670base::FilePath LogTool::GetArcBugReportBackupFilePath
671 (const std::string& userhash) {
672 return daemon_store_base_dir_
673 .Append(userhash)
674 .Append(kArcBugReportBackupFileName);
675}
676
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700677void LogTool::CreateConnectivityReport(bool wait_for_results) {
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700678 // Perform ConnectivityTrial to report connection state in feedback log.
Ben Chan8e9f6d02017-09-26 23:04:21 -0700679 auto shill = std::make_unique<org::chromium::flimflam::ManagerProxy>(bus_);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700680 // Give the connection trial time to test the connection and log the results
681 // before collecting the logs for feedback.
682 // TODO(silberst): Replace the simple approach of a single timeout with a more
683 // coordinated effort.
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700684 if (shill && shill->CreateConnectivityReport(nullptr) && wait_for_results)
Eric Carusocc7106c2017-04-27 14:22:42 -0700685 sleep(kConnectionTesterTimeoutSeconds);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700686}
687
Eric Carusoc93a15c2017-04-24 16:15:12 -0700688string LogTool::GetLog(const string& name) {
Elly Jones533c7c42012-08-10 15:07:05 -0400689 string result;
Ben Chancf7d6412017-08-10 22:30:09 -0700690 GetNamedLogFrom(name, kCommandLogs, &result)
691 || GetNamedLogFrom(name, kExtraLogs, &result)
692 || GetNamedLogFrom(name, kFeedbackLogs, &result);
Elly Jones533c7c42012-08-10 15:07:05 -0400693 return result;
694}
695
Eric Carusof9091f82017-04-28 14:18:59 -0700696LogTool::LogMap LogTool::GetAllLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700697 CreateConnectivityReport(false);
Elly Jones533c7c42012-08-10 15:07:05 -0400698 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700699 GetLogsFrom(kCommandLogs, &result);
700 GetLogsFrom(kExtraLogs, &result);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600701 GetLsbReleaseInfo(&result);
702 GetOsReleaseInfo(&result);
Elly Jones533c7c42012-08-10 15:07:05 -0400703 return result;
704}
705
Brian Norrisca4fc042018-04-03 00:24:26 -0700706LogTool::LogMap LogTool::GetAllDebugLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700707 CreateConnectivityReport(true);
Brian Norrisca4fc042018-04-03 00:24:26 -0700708 LogMap result;
709 GetLogsFrom(kCommandLogs, &result);
710 GetLogsFrom(kExtraLogs, &result);
mhasankd2b84882020-05-04 17:02:19 -0700711 result[arc_bug_report_log_->GetName()] = GetArcBugReport("", nullptr);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600712 GetLsbReleaseInfo(&result);
713 GetOsReleaseInfo(&result);
Brian Norrisca4fc042018-04-03 00:24:26 -0700714 return result;
715}
716
mhasank4f599d32020-04-09 22:07:35 -0700717void LogTool::GetBigFeedbackLogs(const base::ScopedFD& fd,
718 const std::string& username) {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700719 CreateConnectivityReport(true);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800720 LogMap map;
721 GetPerfData(&map);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800722 base::DictionaryValue dictionary;
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700723 GetLogsInDictionary(kCommandLogs, &dictionary);
724 GetLogsInDictionary(kFeedbackLogs, &dictionary);
mhasankd2b84882020-05-04 17:02:19 -0700725 bool is_backup;
726 std::string arc_bug_report = GetArcBugReport(username, &is_backup);
727 dictionary.SetKey(kArcBugReportBackupKey,
728 base::Value(is_backup ? "true" : "false"));
mhasankaf5251d2020-04-29 18:53:03 -0700729 dictionary.SetKey(arc_bug_report_log_->GetName(),
mhasankd2b84882020-05-04 17:02:19 -0700730 base::Value(arc_bug_report));
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600731 GetLsbReleaseInfo(&map);
732 GetOsReleaseInfo(&map);
733 PopulateDictionaryValue(map, &dictionary);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800734 SerializeLogsAsJSON(dictionary, fd);
735}
736
mhasank4f599d32020-04-09 22:07:35 -0700737std::string GetSanitizedUsername(
738 org::chromium::CryptohomeInterfaceProxyInterface* cryptohome_proxy,
mhasankaf5251d2020-04-29 18:53:03 -0700739 const std::string& username) {
mhasank4f599d32020-04-09 22:07:35 -0700740 if (username.empty()) {
741 return std::string();
742 }
743
744 std::string sanitized_username;
745 brillo::ErrorPtr error;
746 if (!cryptohome_proxy->GetSanitizedUsername(username, &sanitized_username,
747 &error)) {
748 LOG(ERROR) << "Failed to call GetSanitizedUsername, error: "
749 << error->GetMessage();
750 return std::string();
751 }
752
753 return sanitized_username;
754}
755
mhasankd2b84882020-05-04 17:02:19 -0700756std::string LogTool::GetArcBugReport(const std::string& username,
757 bool* is_backup) {
758 if (is_backup) {
759 *is_backup = true;
760 }
mhasank4f599d32020-04-09 22:07:35 -0700761 std::string userhash =
762 GetSanitizedUsername(cryptohome_proxy_.get(), username);
763
764 std::string contents;
765 if (userhash.empty() ||
766 arc_bug_report_backups_.find(userhash) == arc_bug_report_backups_.end() ||
767 !base::ReadFileToString(GetArcBugReportBackupFilePath(userhash),
768 &contents)) {
769 // If |userhash| was not empty, but was not found in the backup set
770 // or the file did not exist, attempt to delete the file.
771 if (!userhash.empty()) {
772 DeleteArcBugReportBackup(userhash);
773 }
mhasankd2b84882020-05-04 17:02:19 -0700774 if (is_backup) {
775 *is_backup = false;
776 }
mhasankaf5251d2020-04-29 18:53:03 -0700777 contents = arc_bug_report_log_->GetLogData();
mhasank4f599d32020-04-09 22:07:35 -0700778 }
779
780 return contents;
781}
782
mhasank80cbe4d2020-04-02 22:46:08 -0700783void LogTool::BackupArcBugReport(const std::string& userhash) {
784 DLOG(INFO) << "Backing up ARC bug report";
785
786 const base::FilePath reportPath = GetArcBugReportBackupFilePath(userhash);
mhasankaf5251d2020-04-29 18:53:03 -0700787 const std::string logData = arc_bug_report_log_->GetLogData();
mhasank4f599d32020-04-09 22:07:35 -0700788 if (base::WriteFile(reportPath, logData.c_str(), logData.length())) {
789 arc_bug_report_backups_.insert(userhash);
790 } else {
mhasank80cbe4d2020-04-02 22:46:08 -0700791 PLOG(ERROR) << "Failed to backup ARC bug report";
792 }
793}
794
795void LogTool::DeleteArcBugReportBackup(const std::string& userhash) {
796 DLOG(INFO) << "Deleting the ARC bug report backup";
797
798 const base::FilePath reportPath = GetArcBugReportBackupFilePath(userhash);
mhasank4f599d32020-04-09 22:07:35 -0700799 arc_bug_report_backups_.erase(userhash);
mhasank80cbe4d2020-04-02 22:46:08 -0700800 if (!base::DeleteFile(reportPath, false)) {
801 PLOG(ERROR) << "Failed to delete ARC bug report backup";
802 }
803}
804
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700805void LogTool::GetJournalLog(const base::ScopedFD& fd) {
Chris Morin790fd262019-04-03 20:29:36 -0700806 Log journal(kCommand, "journal.export", "journalctl -n 10000 -o export",
807 "syslog", "syslog", 10 * 1024 * 1024, LogTool::Encoding::kBinary);
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700808 std::string output = journal.GetLogData();
Chris Morin790fd262019-04-03 20:29:36 -0700809 base::WriteFileDescriptor(fd.get(), output.data(), output.size());
810}
811
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700812// static
Chris Morin790fd262019-04-03 20:29:36 -0700813string LogTool::EncodeString(string value,
814 LogTool::Encoding source_encoding) {
815 if (source_encoding == LogTool::Encoding::kBinary)
816 return value;
817
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700818 if (source_encoding == LogTool::Encoding::kAutodetect) {
819 if (base::IsStringUTF8(value))
820 return value;
Chris Morin790fd262019-04-03 20:29:36 -0700821 source_encoding = LogTool::Encoding::kBase64;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700822 }
823
824 if (source_encoding == LogTool::Encoding::kUtf8) {
825 string output;
826 const char* src = value.data();
827 int32_t src_len = static_cast<int32_t>(value.length());
828
829 output.reserve(value.size());
830 for (int32_t char_index = 0; char_index < src_len; char_index++) {
831 uint32_t code_point;
832 if (!base::ReadUnicodeCharacter(src, src_len, &char_index, &code_point) ||
833 !base::IsValidCharacter(code_point)) {
834 // Replace invalid characters with U+FFFD REPLACEMENT CHARACTER.
835 code_point = 0xFFFD;
836 }
837 base::WriteUnicodeCharacter(code_point, &output);
838 }
839 return output;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700840 }
Chris Morin853d3442019-04-01 21:35:13 -0700841
842 base::Base64Encode(value, &value);
843 return "<base64>: " + value;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700844}
845
Ben Chana0011d82014-05-13 00:19:29 -0700846} // namespace debugd