blob: 00dbfdacee9b535ca72b695174357a5ec879deeb [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";
53constexpr char kDaemonStoreBaseDir[] = "/run/daemon-store/debugd/";
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080054
55// Minimum time in seconds needed to allow shill to test active connections.
56const int kConnectionTesterTimeoutSeconds = 5;
Ben Chanf6cd93a2012-10-14 19:37:00 -070057
Chinglin Yuaeb4ec72018-12-10 18:53:30 +080058// Default running perf for 2 seconds.
59constexpr const int kPerfDurationSecs = 2;
Chinglin Yu3c8d0a22019-02-20 11:32:52 +080060// TODO(chinglinyu) Remove after crbug/934702 is fixed.
61// The following description is added to 'perf-data' as a temporary solution
62// before the update of feedback disclosure to users is done in crbug/934702.
63constexpr const char kPerfDataDescription[] =
64 "perf-data contains performance profiling information about how much time "
65 "the system spends on various activities (program execution stack traces). "
66 "This might reveal some information about what system features and "
67 "resources are being used. The full detail of perf-data can be found in "
68 "the PerfDataProto protocol buffer message type in the chromium source "
69 "repository.\n";
Chinglin Yuaeb4ec72018-12-10 18:53:30 +080070
Eric Carusoa879fd92017-10-11 12:57:10 -070071#define CMD_KERNEL_MODULE_PARAMS(module_name) \
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070072 "cd /sys/module/" #module_name "/parameters 2>/dev/null && grep -sH ^ *"
Eric Carusoa879fd92017-10-11 12:57:10 -070073
Fletcher Woodruff07c28532019-01-24 11:08:53 -070074using Log = LogTool::Log;
75constexpr Log::LogType kCommand = Log::kCommand;
76constexpr Log::LogType kFile = Log::kFile;
mhasankaf5251d2020-04-29 18:53:03 -070077
78class ArcBugReportLog : public LogTool::Log {
79 public:
80 ArcBugReportLog()
81 : Log(kCommand,
82 "arc-bugreport",
83 "/usr/bin/nsenter -t1 -m /usr/sbin/android-sh -c "
84 "/system/bin/arc-bugreport",
85 kRoot,
86 kRoot,
87 10 * 1024 * 1024 /*10 MiB*/,
88 LogTool::Encoding::kUtf8) {}
89
90 virtual ~ArcBugReportLog() = default;
91};
mhasank80cbe4d2020-04-02 22:46:08 -070092
Miriam Zimmermand91d8e72019-06-27 12:24:04 -070093// NOTE: IF YOU ADD AN ENTRY TO THIS LIST, PLEASE:
94// * add a row to http://go/cros-feedback-audit and fill it out
95// * email cros-monitoring-forensics@
96// (Eventually we'll have a better process, but for now please do this.)
Fletcher Woodruff07c28532019-01-24 11:08:53 -070097const std::vector<Log> kCommandLogs {
Mike Frysingerb0350992018-09-14 13:45:35 -040098 // We need to enter init's mount namespace because it has /home/chronos
99 // mounted which is where the consent knob lives. We don't have that mount
100 // in our own mount namespace (by design). https://crbug.com/884249
Chris Morin853d3442019-04-01 21:35:13 -0700101 {kCommand, "CLIENT_ID", "/usr/bin/nsenter -t1 -m /usr/bin/metrics_client -i",
102 kRoot, kDebugfsGroup},
103 {kCommand, "LOGDATE", "/bin/date"},
Yusuke Sato27a31672019-04-29 15:26:37 -0700104 // We need to enter init's mount namespace to access /home/root. Also, we use
105 // neither ARC container's mount namespace (with android-sh) nor
106 // /opt/google/containers/android/rootfs/android-data/ so that we can get
107 // results even when the container is down.
108 {kCommand, "android_app_storage", "/usr/bin/nsenter -t1 -m "
109 "/bin/sh -c \"/usr/bin/du -h /home/root/*/android-data/data/\"",
110 kRoot, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700111 {kFile, "atrus_logs", "/var/log/atrus.log"},
112 {kFile, "authpolicy", "/var/log/authpolicy.log"},
Brian Norrisafc9f632019-05-09 14:08:28 -0700113 {kCommand, "bootstat_summary", "/usr/bin/bootstat_summary",
114 SandboxedProcess::kDefaultUser, SandboxedProcess::kDefaultGroup,
115 Log::kDefaultMaxBytes, LogTool::Encoding::kAutodetect, true},
Craig Hesling5c384b52019-04-20 15:18:06 -0700116 {kFile, "bio_crypto_init.LATEST",
117 "/var/log/bio_crypto_init/bio_crypto_init.LATEST"},
118 {kFile, "bio_crypto_init.PREVIOUS",
119 "/var/log/bio_crypto_init/bio_crypto_init.PREVIOUS"},
Chris Morin853d3442019-04-01 21:35:13 -0700120 {kFile, "biod.LATEST", "/var/log/biod/biod.LATEST"},
121 {kFile, "biod.PREVIOUS", "/var/log/biod/biod.PREVIOUS"},
Craig Hesling4c3891e2019-04-20 12:53:54 -0700122 {kFile, "bio_fw_updater.LATEST", "/var/log/biod/bio_fw_updater.LATEST"},
123 {kFile, "bio_fw_updater.PREVIOUS", "/var/log/biod/bio_fw_updater.PREVIOUS"},
Chris Morin853d3442019-04-01 21:35:13 -0700124 {kFile, "bios_info", "/var/log/bios_info.txt"},
125 {kCommand, "bios_log", "cat /sys/firmware/log "
126 "/proc/device-tree/chosen/ap-console-buffer 2>/dev/null"},
127 {kFile, "bios_times", "/var/log/bios_times.txt"},
128 {kCommand, "board-specific",
129 "/usr/share/userfeedback/scripts/get_board_specific_info"},
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 Rao9e65ddd2019-06-06 17:18:26 -0700140 {kCommand, "chromeos-pgmem", "/usr/bin/chromeos-pgmem"},
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"},
161 {kFile, "ec_info", "/var/log/ec_info.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700162 {kCommand, "edid-decode",
163 "for f in /sys/class/drm/card0-*/edid; do "
164 "echo \"----------- ${f}\"; "
Chris Morin853d3442019-04-01 21:35:13 -0700165 // edid-decode's stderr output is redundant, so silence it.
Jeffrey Kardatzke9ba9f322019-08-29 10:23:14 -0700166 "edid-decode \"${f}\" 2>/dev/null; "
Chris Morin853d3442019-04-01 21:35:13 -0700167 "done"},
168 {kFile, "eventlog", "/var/log/eventlog.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700169 {kCommand, "font_info", "/usr/share/userfeedback/scripts/font_info"},
Kuo-Hsin Yang95296e12020-03-06 17:52:35 +0800170 {kCommand, "framebuffer", "cat /sys/kernel/debug/dri/?/framebuffer",
171 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700172 {kFile, "fwupd_state", "/var/lib/fwupd/state.json"},
173 {kCommand, "sensor_info", "/usr/share/userfeedback/scripts/sensor_info"},
174 {kFile, "hammerd", "/var/log/hammerd.log"},
175 {kCommand, "hardware_class", "/usr/bin/crossystem hwid"},
Yong Hong15e4b032020-03-05 15:41:31 +0800176 {kFile, "hardware_verification_report",
177 "/var/cache/hardware_verifier.result"},
Chris Morin853d3442019-04-01 21:35:13 -0700178 {kCommand, "hostname", "/bin/hostname"},
179 {kFile, "i915_gem_gtt", "/sys/kernel/debug/dri/0/i915_gem_gtt",
180 SandboxedProcess::kDefaultUser, kDebugfsGroup},
181 {kFile, "i915_gem_objects", "/sys/kernel/debug/dri/0/i915_gem_objects",
182 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700183 {kCommand, "i915_error_state",
184 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2>/dev/null",
185 SandboxedProcess::kDefaultUser, kDebugfsGroup, Log::kDefaultMaxBytes,
Chris Morin790fd262019-04-03 20:29:36 -0700186 LogTool::Encoding::kBase64},
Chris Morin853d3442019-04-01 21:35:13 -0700187 {kCommand, "ifconfig", "/bin/ifconfig -a"},
188 {kFile, "input_devices", "/proc/bus/input/devices"},
Eric Carusob1820c02017-08-24 15:39:56 -0700189 // Hardware capabilities of the wiphy device.
Alex Levine1c6d572019-09-17 14:45:33 -0700190 {kFile, "interrupts", "/proc/interrupts"},
Chris Morin853d3442019-04-01 21:35:13 -0700191 {kCommand, "iw_list", "/usr/sbin/iw list"},
Eric Carusoa879fd92017-10-11 12:57:10 -0700192#if USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700193 {kCommand, "iwlmvm_module_params", CMD_KERNEL_MODULE_PARAMS(iwlmvm)},
194 {kCommand, "iwlwifi_module_params", CMD_KERNEL_MODULE_PARAMS(iwlwifi)},
Eric Carusoa879fd92017-10-11 12:57:10 -0700195#endif // USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700196 {kCommand, "kernel-crashes",
197 "cat /var/spool/crash/kernel.*.kcrash 2>/dev/null"},
Anand K Mistryccceb1e2020-01-16 14:00:49 +1100198 {kCommand, "lsblk", "timeout -s KILL 5s lsblk -a", kRoot, kRoot,
199 Log::kDefaultMaxBytes, LogTool::Encoding::kAutodetect, true},
Chris Morin853d3442019-04-01 21:35:13 -0700200 {kCommand, "lsmod", "lsmod"},
201 {kCommand, "lspci", "/usr/sbin/lspci"},
202 {kCommand, "lsusb", "lsusb && lsusb -t"},
Kuo-Hsin Yang66d89832020-02-10 17:22:28 +0800203 {kFile, "mali_memory", "/sys/kernel/debug/mali0/gpu_memory",
204 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700205 {kFile, "memd.parameters", "/var/log/memd/memd.parameters"},
206 {kCommand, "memd clips", "cat /var/log/memd/memd.clip* 2>/dev/null"},
207 {kFile, "meminfo", "/proc/meminfo"},
208 {kCommand, "memory_spd_info",
209 // mosys may use 'i2c-dev', which may not be loaded yet.
210 "modprobe i2c-dev 2>/dev/null && mosys -l memory spd print all 2>/dev/null",
211 kRoot, kDebugfsGroup},
Simon Quecb63b9c2017-06-19 14:53:31 -0400212 // The sed command finds the EDID blob (starting the line after "value:") and
213 // replaces the serial number with all zeroes.
214 //
215 // The EDID is printed as a hex dump over several lines, each line containing
216 // the contents of 16 bytes. The first 16 bytes are broken down as follows:
217 // uint64_t fixed_pattern; // Always 00 FF FF FF FF FF FF 00.
218 // uint16_t manufacturer_id; // Manufacturer ID, encoded as PNP IDs.
219 // uint16_t product_code; // Manufacturer product code, little-endian.
220 // uint32_t serial_number; // Serial number, little-endian.
221 // Source: https://en.wikipedia.org/wiki/EDID#EDID_1.3_data_format
222 //
223 // The subsequent substitution command looks for the fixed pattern followed by
224 // two 32-bit fields (manufacturer + product, serial number). It replaces the
225 // latter field with 8 bytes of zeroes.
226 //
227 // TODO(crbug.com/731133): Remove the sed command once modetest itself can
228 // remove serial numbers.
Chris Morin853d3442019-04-01 21:35:13 -0700229 {kCommand, "modetest",
230 "(modetest; modetest -M evdi; modetest -M udl) | "
231 "sed -E '/EDID/ {:a;n;/value:/!ba;n;"
232 "s/(00f{12}00)([0-9a-f]{8})([0-9a-f]{8})/\\1\\200000000/}'",
233 kRoot, kRoot},
234 {kFile, "mount-encrypted", "/var/log/mount-encrypted.log"},
235 {kFile, "mountinfo", "/proc/1/mountinfo"},
236 {kCommand, "netlog",
237 "/usr/share/userfeedback/scripts/getmsgs /var/log/net.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700238 {kFile, "nvmap_iovmm", "/sys/kernel/debug/nvmap/iovmm/allocations",
239 SandboxedProcess::kDefaultUser, kDebugfsGroup},
240 {kCommand, "oemdata", "/usr/share/cros/oemdata.sh", kRoot, kRoot},
Kuo-Hsin Yanga69ecc62020-03-11 17:37:11 +0800241 {kFile, "pagetypeinfo", "/proc/pagetypeinfo", kRoot},
Jack Rosenthal3cf794a2020-02-19 13:32:56 -0700242 {kFile, "platform_identity_name",
243 "/run/chromeos-config/v1/identity/platform-name"},
244 {kFile, "platform_identity_model", "/run/chromeos-config/v1/name"},
245 {kFile, "platform_identity_sku", "/run/chromeos-config/v1/identity/sku-id"},
246 {kFile, "platform_identity_whitelabel_tag",
247 "/run/chromeos-config/v1/identity/whitelabel-tag"},
248 {kFile, "platform_identity_customization_id",
249 "/run/chromeos-config/v1/identity/customization-id"},
Chris Morin853d3442019-04-01 21:35:13 -0700250 {kCommand, "power_supply_info", "/usr/bin/power_supply_info"},
251 {kCommand, "power_supply_sysfs", "/usr/bin/print_sysfs_power_supply_data"},
252 {kFile, "powerd.LATEST", "/var/log/power_manager/powerd.LATEST"},
253 {kFile, "powerd.PREVIOUS", "/var/log/power_manager/powerd.PREVIOUS"},
254 {kFile, "powerd.out", "/var/log/powerd.out"},
255 {kFile, "powerwash_count", "/var/log/powerwash_count"},
Brian Norris4cde3d12019-04-16 10:10:34 -0700256 {kCommand, "ps", "/bin/ps auxZ"},
yusukes34171ba2017-04-27 15:46:01 -0700257 // /proc/slabinfo is owned by root and has 0400 permission.
Chris Morin853d3442019-04-01 21:35:13 -0700258 {kFile, "slabinfo", "/proc/slabinfo", kRoot, kRoot},
259 {kFile, "storage_info", "/var/log/storage_info.txt"},
260 {kCommand, "swap_info", "/usr/share/cros/init/swap.sh status 2>/dev/null",
261 SandboxedProcess::kDefaultUser, kDebugfsGroup},
262 {kCommand, "syslog",
263 "/usr/share/userfeedback/scripts/getmsgs /var/log/messages"},
264 {kCommand, "system_log_stats",
265 "echo 'BLOCK_SIZE=1024'; "
266 "find /var/log/ -type f -exec du --block-size=1024 {} + | sort -n -r",
267 kRoot, kRoot},
268 {kCommand, "threads", "/bin/ps -T axo pid,ppid,spid,pcpu,ni,stat,time,comm"},
269 {kFile, "tlsdate", "/var/log/tlsdate.log"},
Nick Sandersad5dc132019-11-15 15:59:42 -0800270 {kCommand, "top thread", "/usr/bin/top -Hbc -w128 -n 1 | head -n 40"},
271 {kCommand, "top memory",
272 "/usr/bin/top -o \"+%MEM\" -w128 -bcn 1 | head -n 57"},
Chris Morin853d3442019-04-01 21:35:13 -0700273 {kCommand, "touch_fw_version",
274 "grep -E"
275 " -e 'synaptics: Touchpad model'"
276 " -e 'chromeos-[a-z]*-touch-[a-z]*-update'"
277 " /var/log/messages | tail -n 20"},
278 {kCommand, "tpm-firmware-updater", "/usr/share/userfeedback/scripts/getmsgs "
279 "/var/log/tpm-firmware-updater.log"},
Mattias Nissler887dce22017-07-03 14:44:35 +0200280 // TODO(jorgelo,mnissler): Don't run this as root.
281 // On TPM 1.2 devices this will likely require adding a new user to the 'tss'
282 // group.
283 // On TPM 2.0 devices 'get_version_info' uses D-Bus and therefore can run as
284 // any user.
Chris Morin853d3442019-04-01 21:35:13 -0700285 {kCommand, "tpm_version", "/usr/sbin/tpm-manager get_version_info", kRoot,
286 kRoot},
287 {kCommand, "atmel_ts_refs",
288 "/opt/google/touch/scripts/atmel_tools.sh ts r", kRoot, kRoot},
289 {kCommand, "atmel_tp_refs",
290 "/opt/google/touch/scripts/atmel_tools.sh tp r", kRoot, kRoot},
291 {kCommand, "atmel_ts_deltas",
292 "/opt/google/touch/scripts/atmel_tools.sh ts d", kRoot, kRoot},
293 {kCommand, "atmel_tp_deltas",
294 "/opt/google/touch/scripts/atmel_tools.sh tp d", kRoot, kRoot},
295 {kFile, "stateful_trim_state", "/var/lib/trim/stateful_trim_state"},
296 {kFile, "stateful_trim_data", "/var/lib/trim/stateful_trim_data"},
297 {kFile, "ui_log", "/var/log/ui/ui.LATEST"},
298 {kCommand, "uname", "/bin/uname -a"},
299 {kCommand, "update_engine.log",
300 "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
301 " s.^./var/log/update_engine/.)"},
Chris Morinca152712019-05-03 13:17:28 -0700302 {kFile, "upstart", "/var/log/upstart.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700303 {kCommand, "uptime", "/usr/bin/cut -d' ' -f1 /proc/uptime"},
304 {kFile, "verified boot", "/var/log/debug_vboot_noisy.log"},
305 {kFile, "vmlog.1.LATEST", "/var/log/vmlog/vmlog.1.LATEST"},
306 {kFile, "vmlog.1.PREVIOUS", "/var/log/vmlog/vmlog.1.PREVIOUS"},
307 {kFile, "vmlog.LATEST", "/var/log/vmlog/vmlog.LATEST"},
308 {kFile, "vmlog.PREVIOUS", "/var/log/vmlog/vmlog.PREVIOUS"},
309 {kFile, "vmstat", "/proc/vmstat"},
310 {kFile, "vpd_2.0", "/var/log/vpd_2.0.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700311 {kFile, "zram compressed data size", "/sys/block/zram0/compr_data_size"},
312 {kFile, "zram original data size", "/sys/block/zram0/orig_data_size"},
313 {kFile, "zram total memory used", "/sys/block/zram0/mem_used_total"},
314 {kFile, "zram total reads", "/sys/block/zram0/num_reads"},
315 {kFile, "zram total writes", "/sys/block/zram0/num_writes"},
316 {kCommand, "zram new stats names",
317 "echo orig_size compr_size used_total limit used_max zero_pages migrated"},
318 {kFile, "zram new stats values", "/sys/block/zram0/mm_stat"},
319 {kFile, "cros_tp version", "/sys/class/chromeos/cros_tp/version"},
320 {kCommand, "cros_tp console", "/usr/sbin/ectool --name=cros_tp console",
321 kRoot, kRoot},
322 {kCommand, "cros_tp frame", "/usr/sbin/ectool --name=cros_tp tpframeget",
323 kRoot, kRoot},
324 {kCommand, "crostini", "/usr/bin/cicerone_client --get_info"},
Sean Paul5ce118f2019-12-05 08:41:32 -0500325 {kFile, "drm_trace", "/sys/kernel/debug/dri/trace",
326 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Elly Jones03cd6d72012-06-11 13:04:28 -0400327 // Stuff pulled out of the original list. These need access to the running X
328 // session, which we'd rather not give to debugd, or return info specific to
329 // the current session (in the setsid(2) sense), which is not useful for
330 // debugd
Chris Morin853d3442019-04-01 21:35:13 -0700331 // {kCommand, "env", "set"},
332 // {kCommand, "setxkbmap", "/usr/bin/setxkbmap -print -query"},
333 // {kCommand, "xrandr", "/usr/bin/xrandr --verbose}
Elly Jones533c7c42012-08-10 15:07:05 -0400334};
335
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700336// netstat and logcat should appear in chrome://system but not in feedback
337// reports. Open sockets may have privacy implications, and logcat is
338// already incorporated via arc-bugreport.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700339const std::vector<Log> kExtraLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800340#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700341 {kCommand, "mm-status", "/usr/bin/modem status"},
Ben Chan36e42282014-02-12 22:32:34 -0800342#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700343 {kCommand, "network-devices", "/usr/bin/connectivity show devices"},
344 {kCommand, "network-services", "/usr/bin/connectivity show services"},
Jeffrey Kardatzke36791f22019-07-11 11:53:22 -0700345 {kCommand, "wifi_status_no_anonymize",
346 "/usr/bin/network_diag --wifi-internal --no-log"},
Chris Morin253a2b02019-04-12 16:04:25 -0700347 // --processes requires root.
348 {kCommand, "netstat",
349 "/sbin/ss --all --query inet --numeric --processes", kRoot, kRoot},
Kansho Nishida6ae546f2019-08-13 17:14:58 +0900350 {kCommand, "logcat",
351 "/usr/bin/nsenter -t1 -m /usr/sbin/android-sh -c '/system/bin/logcat -d'",
Chris Morin253a2b02019-04-12 16:04:25 -0700352 kRoot, kRoot, Log::kDefaultMaxBytes, LogTool::Encoding::kUtf8},
Elly Jones533c7c42012-08-10 15:07:05 -0400353};
354
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700355const std::vector<Log> kFeedbackLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800356#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700357 {kCommand, "mm-status", "/usr/bin/modem status-feedback"},
Ben Chan36e42282014-02-12 22:32:34 -0800358#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700359 {kCommand, "network-devices",
360 "/usr/bin/connectivity show-feedback devices"},
361 {kCommand, "network-services",
362 "/usr/bin/connectivity show-feedback services"},
Jeffrey Kardatzke36791f22019-07-11 11:53:22 -0700363 {kCommand, "wifi_status",
364 "/usr/bin/network_diag --wifi-internal --no-log --anonymize"},
Elly Jones03cd6d72012-06-11 13:04:28 -0400365};
366
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700367// Fills |dictionary| with the contents of the logs in |logs|.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700368void GetLogsInDictionary(const std::vector<Log>& logs,
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800369 base::DictionaryValue* dictionary) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700370 for (const Log& log : logs) {
Hidehiko Abe22667262019-08-15 01:32:23 +0900371 dictionary->SetKey(log.GetName(), base::Value(log.GetLogData()));
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800372 }
373}
374
375// Serializes the |dictionary| into the file with the given |fd| in a JSON
376// format.
377void SerializeLogsAsJSON(const base::DictionaryValue& dictionary,
Eric Caruso0b241882018-04-04 13:43:46 -0700378 const base::ScopedFD& fd) {
Eric Caruso96d03d32017-04-25 18:01:17 -0700379 string logs_json;
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800380 base::JSONWriter::WriteWithOptions(dictionary,
381 base::JSONWriter::OPTIONS_PRETTY_PRINT,
382 &logs_json);
Eric Caruso0b241882018-04-04 13:43:46 -0700383 base::WriteFileDescriptor(fd.get(), logs_json.c_str(), logs_json.size());
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800384}
385
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700386bool GetNamedLogFrom(const string& name,
387 const std::vector<Log>& logs,
Elly Jones533c7c42012-08-10 15:07:05 -0400388 string* result) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700389 for (const Log& log : logs) {
390 if (name == log.GetName()) {
391 *result = log.GetLogData();
Elly Jones533c7c42012-08-10 15:07:05 -0400392 return true;
393 }
394 }
395 *result = "<invalid log name>";
396 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400397}
398
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700399void GetLogsFrom(const std::vector<Log>& logs, LogTool::LogMap* map) {
400 for (const Log& log : logs)
401 (*map)[log.GetName()] = log.GetLogData();
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800402}
403
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600404void GetLsbReleaseInfo(LogTool::LogMap* map) {
405 const base::FilePath lsb_release(kLsbReleasePath);
406 brillo::KeyValueStore store;
407 if (!store.Load(lsb_release)) {
408 // /etc/lsb-release might not be present (cros deploying a new
409 // configuration or no fields set at all). Just print a debug
410 // message and continue.
411 DLOG(INFO) << "Could not load fields from " << lsb_release.value();
412 } else {
413 for (const auto& key : store.GetKeys()) {
414 std::string value;
415 store.GetString(key, &value);
416 (*map)[key] = value;
417 }
418 }
419}
420
421void GetOsReleaseInfo(LogTool::LogMap* map) {
422 brillo::OsReleaseReader reader;
423 reader.Load();
424 for (const auto& key : reader.GetKeys()) {
425 std::string value;
426 reader.GetString(key, &value);
427 (*map)["os-release " + key] = value;
428 }
429}
430
431void PopulateDictionaryValue(const LogTool::LogMap& map,
432 base::DictionaryValue* dictionary) {
433 for (const auto& kv : map) {
434 dictionary->SetString(kv.first, kv.second);
435 }
436}
437
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800438bool CompressXzBuffer(const std::vector<uint8_t>& in_buffer,
439 std::vector<uint8_t>* out_buffer) {
440 size_t out_size = lzma_stream_buffer_bound(in_buffer.size());
441 out_buffer->resize(out_size);
442 size_t out_pos = 0;
443
444 lzma_ret ret = lzma_easy_buffer_encode(
445 LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64, nullptr, in_buffer.data(),
446 in_buffer.size(), out_buffer->data(), &out_pos, out_size);
447
448 if (ret != LZMA_OK) {
449 out_buffer->clear();
450 return false;
451 }
452
453 out_buffer->resize(out_pos);
454 return true;
455}
456
457void GetPerfData(LogTool::LogMap* map) {
458 // Run perf to collect system-wide performance profile when user triggers
459 // feedback report. Perf runs at sampling frequency of ~500 hz (499 is used
460 // to avoid sampling periodic system activities), with callstack in each
461 // sample (-g).
462 std::vector<std::string> perf_args = {
463 "perf", "record", "-a", "-g", "-F", "499"
464 };
465 std::vector<uint8_t> perf_data;
466 int32_t status;
467
468 debugd::PerfTool perf_tool;
469 if (!perf_tool.GetPerfOutput(kPerfDurationSecs, perf_args, &perf_data,
470 nullptr, &status, nullptr))
471 return;
472
473 // XZ compress the profile data.
474 std::vector<uint8_t> perf_data_xz;
475 if (!CompressXzBuffer(perf_data, &perf_data_xz))
476 return;
477
478 // Base64 encode the compressed data.
479 std::string perf_data_str(reinterpret_cast<const char*>(perf_data_xz.data()),
480 perf_data_xz.size());
481 (*map)["perf-data"] =
Chinglin Yu3c8d0a22019-02-20 11:32:52 +0800482 std::string(kPerfDataDescription) +
Chris Morin790fd262019-04-03 20:29:36 -0700483 LogTool::EncodeString(std::move(perf_data_str),
484 LogTool::Encoding::kBase64);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800485}
486
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800487} // namespace
488
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700489Log::Log(Log::LogType type,
490 std::string name,
491 std::string data,
492 std::string user,
493 std::string group,
494 int64_t max_bytes,
Brian Norrisafc9f632019-05-09 14:08:28 -0700495 LogTool::Encoding encoding,
496 bool access_root_mount_ns)
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700497 : type_(type),
498 name_(name),
499 data_(data),
500 user_(user),
501 group_(group),
502 max_bytes_(max_bytes),
Brian Norrisafc9f632019-05-09 14:08:28 -0700503 encoding_(encoding),
504 access_root_mount_ns_(access_root_mount_ns) {}
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700505
506std::string Log::GetName() const {
507 return name_;
508}
509
510std::string Log::GetLogData() const {
511 // The reason this code uses a switch statement on a type enum rather than
512 // using inheritance/virtual dispatch is so that all of the Log objects can
513 // be constructed statically. Switching to heap allocated subclasses of Log
514 // makes the code that declares all of the log entries much more verbose
515 // and harder to understand.
Chris Morin790fd262019-04-03 20:29:36 -0700516 std::string output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700517 switch (type_) {
518 case kCommand:
Chris Morin790fd262019-04-03 20:29:36 -0700519 output = GetCommandLogData();
520 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700521 case kFile:
Chris Morin790fd262019-04-03 20:29:36 -0700522 output = GetFileLogData();
523 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700524 default:
525 return "<unknown log type>";
526 }
Chris Morin790fd262019-04-03 20:29:36 -0700527
528 if (output.empty())
529 return "<empty>";
530
531 return LogTool::EncodeString(std::move(output), encoding_);
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700532}
533
534// TODO(ellyjones): sandbox. crosbug.com/35122
535std::string Log::GetCommandLogData() const {
536 if (type_ != kCommand)
537 return "<log type mismatch>";
538 std::string tailed_cmdline =
539 base::StringPrintf("%s | tail -c %" PRId64, data_.c_str(), max_bytes_);
540 ProcessWithOutput p;
541 if (minijail_disabled_for_test_)
542 p.set_use_minijail(false);
543 if (!user_.empty() && !group_.empty())
544 p.SandboxAs(user_, group_);
Brian Norrisafc9f632019-05-09 14:08:28 -0700545 if (access_root_mount_ns_)
546 p.AllowAccessRootMountNamespace();
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700547 if (!p.Init())
548 return "<not available>";
549 p.AddArg(kShell);
550 p.AddStringOption("-c", tailed_cmdline);
551 if (p.Run())
552 return "<not available>";
553 std::string output;
554 p.GetOutput(&output);
Chris Morin790fd262019-04-03 20:29:36 -0700555 return output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700556}
557
558std::string Log::GetFileLogData() const {
559 if (type_ != kFile)
560 return "<log type mismatch>";
561
562 uid_t old_euid = geteuid();
563 uid_t new_euid = UidForUser(user_);
564 gid_t old_egid = getegid();
565 gid_t new_egid = GidForGroup(group_);
566
567 if (new_euid == -1 || new_egid == -1) {
568 return "<not available>";
569 }
570
571 // Make sure to set group first, since if we set user first we lose root
572 // and therefore the ability to set our effective gid to arbitrary gids.
573 if (setegid(new_egid)) {
574 PLOG(ERROR) << "Failed to set effective group id to " << new_egid;
575 return "<not available>";
576 }
577 if (seteuid(new_euid)) {
578 PLOG(ERROR) << "Failed to set effective user id to " << new_euid;
579 if (setegid(old_egid))
580 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
581 return "<not available>";
582 }
583
584 std::string contents;
585 const base::FilePath path(data_);
586 // Handle special files that don't properly report length/allow lseek.
587 if (base::FilePath("/dev").IsParent(path) ||
588 base::FilePath("/proc").IsParent(path) ||
589 base::FilePath("/sys").IsParent(path)) {
590 if (!base::ReadFileToString(path, &contents))
591 contents = "<not available>";
592 if (contents.size() > max_bytes_)
593 contents.erase(0, contents.size() - max_bytes_);
594 } else {
595 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
596 if (!file.IsValid()) {
597 contents = "<not available>";
598 } else {
599 int64_t length = file.GetLength();
600 if (length > max_bytes_) {
601 file.Seek(base::File::FROM_END, -max_bytes_);
602 length = max_bytes_;
603 }
604 std::vector<char> buf(length);
605 int read = file.ReadAtCurrentPos(buf.data(), buf.size());
606 if (read < 0) {
607 PLOG(ERROR) << "Could not read from file " << path.value();
608 } else {
609 contents = std::string(buf.begin(), buf.begin() + read);
610 }
611 }
612 }
613
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700614 // Make sure we restore our old euid/egid before returning.
615 if (seteuid(old_euid))
616 PLOG(ERROR) << "Failed to restore effective user id to " << old_euid;
617
618 if (setegid(old_egid))
619 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
620
Chris Morin790fd262019-04-03 20:29:36 -0700621 return contents;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700622}
623
624void Log::DisableMinijailForTest() {
625 minijail_disabled_for_test_ = true;
626}
627
628// static
629uid_t Log::UidForUser(const std::string& user) {
630 struct passwd entry;
631 struct passwd* result;
632 std::vector<char> buf(1024);
633 getpwnam_r(user.c_str(), &entry, &buf[0], buf.size(), &result);
634 if (!result) {
635 LOG(ERROR) << "User not found: " << user;
636 return -1;
637 }
638 return entry.pw_uid;
639}
640
641// static
642gid_t Log::GidForGroup(const std::string& group) {
643 struct group entry;
644 struct group* result;
645 std::vector<char> buf(1024);
646 getgrnam_r(group.c_str(), &entry, &buf[0], buf.size(), &result);
647 if (!result) {
648 LOG(ERROR) << "Group not found: " << group;
649 return -1;
650 }
651 return entry.gr_gid;
652}
653
mhasankaf5251d2020-04-29 18:53:03 -0700654LogTool::LogTool(
655 scoped_refptr<dbus::Bus> bus,
656 std::unique_ptr<org::chromium::CryptohomeInterfaceProxyInterface>
657 cryptohome_proxy,
658 std::unique_ptr<LogTool::Log> arc_bug_report_log,
659 const base::FilePath& daemon_store_base_dir)
660 : bus_(bus),
661 cryptohome_proxy_(std::move(cryptohome_proxy)),
662 arc_bug_report_log_(std::move(arc_bug_report_log)),
663 daemon_store_base_dir_(daemon_store_base_dir) {}
mhasank80cbe4d2020-04-02 22:46:08 -0700664
mhasank4f599d32020-04-09 22:07:35 -0700665LogTool::LogTool(scoped_refptr<dbus::Bus> bus)
mhasankaf5251d2020-04-29 18:53:03 -0700666 : LogTool(bus,
667 std::make_unique<org::chromium::CryptohomeInterfaceProxy>(bus),
668 std::make_unique<ArcBugReportLog>(),
669 base::FilePath(kDaemonStoreBaseDir)) {}
mhasank4f599d32020-04-09 22:07:35 -0700670
mhasank80cbe4d2020-04-02 22:46:08 -0700671base::FilePath LogTool::GetArcBugReportBackupFilePath
672 (const std::string& userhash) {
673 return daemon_store_base_dir_
674 .Append(userhash)
675 .Append(kArcBugReportBackupFileName);
676}
677
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700678void LogTool::CreateConnectivityReport(bool wait_for_results) {
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700679 // Perform ConnectivityTrial to report connection state in feedback log.
Ben Chan8e9f6d02017-09-26 23:04:21 -0700680 auto shill = std::make_unique<org::chromium::flimflam::ManagerProxy>(bus_);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700681 // Give the connection trial time to test the connection and log the results
682 // before collecting the logs for feedback.
683 // TODO(silberst): Replace the simple approach of a single timeout with a more
684 // coordinated effort.
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700685 if (shill && shill->CreateConnectivityReport(nullptr) && wait_for_results)
Eric Carusocc7106c2017-04-27 14:22:42 -0700686 sleep(kConnectionTesterTimeoutSeconds);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700687}
688
Eric Carusoc93a15c2017-04-24 16:15:12 -0700689string LogTool::GetLog(const string& name) {
Elly Jones533c7c42012-08-10 15:07:05 -0400690 string result;
Ben Chancf7d6412017-08-10 22:30:09 -0700691 GetNamedLogFrom(name, kCommandLogs, &result)
692 || GetNamedLogFrom(name, kExtraLogs, &result)
693 || GetNamedLogFrom(name, kFeedbackLogs, &result);
Elly Jones533c7c42012-08-10 15:07:05 -0400694 return result;
695}
696
Eric Carusof9091f82017-04-28 14:18:59 -0700697LogTool::LogMap LogTool::GetAllLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700698 CreateConnectivityReport(false);
Elly Jones533c7c42012-08-10 15:07:05 -0400699 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700700 GetLogsFrom(kCommandLogs, &result);
701 GetLogsFrom(kExtraLogs, &result);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600702 GetLsbReleaseInfo(&result);
703 GetOsReleaseInfo(&result);
Elly Jones533c7c42012-08-10 15:07:05 -0400704 return result;
705}
706
Brian Norrisca4fc042018-04-03 00:24:26 -0700707LogTool::LogMap LogTool::GetAllDebugLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700708 CreateConnectivityReport(true);
Brian Norrisca4fc042018-04-03 00:24:26 -0700709 LogMap result;
710 GetLogsFrom(kCommandLogs, &result);
711 GetLogsFrom(kExtraLogs, &result);
mhasankaf5251d2020-04-29 18:53:03 -0700712 result[arc_bug_report_log_->GetName()] = GetArcBugReport("");
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600713 GetLsbReleaseInfo(&result);
714 GetOsReleaseInfo(&result);
Brian Norrisca4fc042018-04-03 00:24:26 -0700715 return result;
716}
717
mhasank4f599d32020-04-09 22:07:35 -0700718void LogTool::GetBigFeedbackLogs(const base::ScopedFD& fd,
719 const std::string& username) {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700720 CreateConnectivityReport(true);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800721 LogMap map;
722 GetPerfData(&map);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800723 base::DictionaryValue dictionary;
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700724 GetLogsInDictionary(kCommandLogs, &dictionary);
725 GetLogsInDictionary(kFeedbackLogs, &dictionary);
mhasankaf5251d2020-04-29 18:53:03 -0700726 dictionary.SetKey(arc_bug_report_log_->GetName(),
mhasank4f599d32020-04-09 22:07:35 -0700727 base::Value(GetArcBugReport(username)));
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600728 GetLsbReleaseInfo(&map);
729 GetOsReleaseInfo(&map);
730 PopulateDictionaryValue(map, &dictionary);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800731 SerializeLogsAsJSON(dictionary, fd);
732}
733
mhasank4f599d32020-04-09 22:07:35 -0700734std::string GetSanitizedUsername(
735 org::chromium::CryptohomeInterfaceProxyInterface* cryptohome_proxy,
mhasankaf5251d2020-04-29 18:53:03 -0700736 const std::string& username) {
mhasank4f599d32020-04-09 22:07:35 -0700737 if (username.empty()) {
738 return std::string();
739 }
740
741 std::string sanitized_username;
742 brillo::ErrorPtr error;
743 if (!cryptohome_proxy->GetSanitizedUsername(username, &sanitized_username,
744 &error)) {
745 LOG(ERROR) << "Failed to call GetSanitizedUsername, error: "
746 << error->GetMessage();
747 return std::string();
748 }
749
750 return sanitized_username;
751}
752
753std::string LogTool::GetArcBugReport(const std::string& username) {
754 std::string userhash =
755 GetSanitizedUsername(cryptohome_proxy_.get(), username);
756
757 std::string contents;
758 if (userhash.empty() ||
759 arc_bug_report_backups_.find(userhash) == arc_bug_report_backups_.end() ||
760 !base::ReadFileToString(GetArcBugReportBackupFilePath(userhash),
761 &contents)) {
762 // If |userhash| was not empty, but was not found in the backup set
763 // or the file did not exist, attempt to delete the file.
764 if (!userhash.empty()) {
765 DeleteArcBugReportBackup(userhash);
766 }
mhasankaf5251d2020-04-29 18:53:03 -0700767 contents = arc_bug_report_log_->GetLogData();
mhasank4f599d32020-04-09 22:07:35 -0700768 }
769
770 return contents;
771}
772
mhasank80cbe4d2020-04-02 22:46:08 -0700773void LogTool::BackupArcBugReport(const std::string& userhash) {
774 DLOG(INFO) << "Backing up ARC bug report";
775
776 const base::FilePath reportPath = GetArcBugReportBackupFilePath(userhash);
mhasankaf5251d2020-04-29 18:53:03 -0700777 const std::string logData = arc_bug_report_log_->GetLogData();
mhasank4f599d32020-04-09 22:07:35 -0700778 if (base::WriteFile(reportPath, logData.c_str(), logData.length())) {
779 arc_bug_report_backups_.insert(userhash);
780 } else {
mhasank80cbe4d2020-04-02 22:46:08 -0700781 PLOG(ERROR) << "Failed to backup ARC bug report";
782 }
783}
784
785void LogTool::DeleteArcBugReportBackup(const std::string& userhash) {
786 DLOG(INFO) << "Deleting the ARC bug report backup";
787
788 const base::FilePath reportPath = GetArcBugReportBackupFilePath(userhash);
mhasank4f599d32020-04-09 22:07:35 -0700789 arc_bug_report_backups_.erase(userhash);
mhasank80cbe4d2020-04-02 22:46:08 -0700790 if (!base::DeleteFile(reportPath, false)) {
791 PLOG(ERROR) << "Failed to delete ARC bug report backup";
792 }
793}
794
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700795void LogTool::GetJournalLog(const base::ScopedFD& fd) {
Chris Morin790fd262019-04-03 20:29:36 -0700796 Log journal(kCommand, "journal.export", "journalctl -n 10000 -o export",
797 "syslog", "syslog", 10 * 1024 * 1024, LogTool::Encoding::kBinary);
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700798 std::string output = journal.GetLogData();
Chris Morin790fd262019-04-03 20:29:36 -0700799 base::WriteFileDescriptor(fd.get(), output.data(), output.size());
800}
801
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700802// static
Chris Morin790fd262019-04-03 20:29:36 -0700803string LogTool::EncodeString(string value,
804 LogTool::Encoding source_encoding) {
805 if (source_encoding == LogTool::Encoding::kBinary)
806 return value;
807
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700808 if (source_encoding == LogTool::Encoding::kAutodetect) {
809 if (base::IsStringUTF8(value))
810 return value;
Chris Morin790fd262019-04-03 20:29:36 -0700811 source_encoding = LogTool::Encoding::kBase64;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700812 }
813
814 if (source_encoding == LogTool::Encoding::kUtf8) {
815 string output;
816 const char* src = value.data();
817 int32_t src_len = static_cast<int32_t>(value.length());
818
819 output.reserve(value.size());
820 for (int32_t char_index = 0; char_index < src_len; char_index++) {
821 uint32_t code_point;
822 if (!base::ReadUnicodeCharacter(src, src_len, &char_index, &code_point) ||
823 !base::IsValidCharacter(code_point)) {
824 // Replace invalid characters with U+FFFD REPLACEMENT CHARACTER.
825 code_point = 0xFFFD;
826 }
827 base::WriteUnicodeCharacter(code_point, &output);
828 }
829 return output;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700830 }
Chris Morin853d3442019-04-01 21:35:13 -0700831
832 base::Base64Encode(value, &value);
833 return "<base64>: " + value;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700834}
835
Ben Chana0011d82014-05-13 00:19:29 -0700836} // namespace debugd