blob: a52c83b3857d2e59986a2dc46d987699a3f7a17f [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;
mhasank80cbe4d2020-04-02 22:46:08 -070077const Log kArcBugReportLog{
78 kCommand,
79 "arc-bugreport",
80 "/usr/bin/nsenter -t1 -m /usr/sbin/android-sh -c /system/bin/arc-bugreport",
81 kRoot,
82 kRoot,
83 10 * 1024 * 1024 /*10 MiB*/,
84 LogTool::Encoding::kUtf8};
85
Miriam Zimmermand91d8e72019-06-27 12:24:04 -070086// NOTE: IF YOU ADD AN ENTRY TO THIS LIST, PLEASE:
87// * add a row to http://go/cros-feedback-audit and fill it out
88// * email cros-monitoring-forensics@
89// (Eventually we'll have a better process, but for now please do this.)
Fletcher Woodruff07c28532019-01-24 11:08:53 -070090const std::vector<Log> kCommandLogs {
Mike Frysingerb0350992018-09-14 13:45:35 -040091 // We need to enter init's mount namespace because it has /home/chronos
92 // mounted which is where the consent knob lives. We don't have that mount
93 // in our own mount namespace (by design). https://crbug.com/884249
Chris Morin853d3442019-04-01 21:35:13 -070094 {kCommand, "CLIENT_ID", "/usr/bin/nsenter -t1 -m /usr/bin/metrics_client -i",
95 kRoot, kDebugfsGroup},
96 {kCommand, "LOGDATE", "/bin/date"},
Yusuke Sato27a31672019-04-29 15:26:37 -070097 // We need to enter init's mount namespace to access /home/root. Also, we use
98 // neither ARC container's mount namespace (with android-sh) nor
99 // /opt/google/containers/android/rootfs/android-data/ so that we can get
100 // results even when the container is down.
101 {kCommand, "android_app_storage", "/usr/bin/nsenter -t1 -m "
102 "/bin/sh -c \"/usr/bin/du -h /home/root/*/android-data/data/\"",
103 kRoot, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700104 {kFile, "atrus_logs", "/var/log/atrus.log"},
105 {kFile, "authpolicy", "/var/log/authpolicy.log"},
Brian Norrisafc9f632019-05-09 14:08:28 -0700106 {kCommand, "bootstat_summary", "/usr/bin/bootstat_summary",
107 SandboxedProcess::kDefaultUser, SandboxedProcess::kDefaultGroup,
108 Log::kDefaultMaxBytes, LogTool::Encoding::kAutodetect, true},
Craig Hesling5c384b52019-04-20 15:18:06 -0700109 {kFile, "bio_crypto_init.LATEST",
110 "/var/log/bio_crypto_init/bio_crypto_init.LATEST"},
111 {kFile, "bio_crypto_init.PREVIOUS",
112 "/var/log/bio_crypto_init/bio_crypto_init.PREVIOUS"},
Chris Morin853d3442019-04-01 21:35:13 -0700113 {kFile, "biod.LATEST", "/var/log/biod/biod.LATEST"},
114 {kFile, "biod.PREVIOUS", "/var/log/biod/biod.PREVIOUS"},
Craig Hesling4c3891e2019-04-20 12:53:54 -0700115 {kFile, "bio_fw_updater.LATEST", "/var/log/biod/bio_fw_updater.LATEST"},
116 {kFile, "bio_fw_updater.PREVIOUS", "/var/log/biod/bio_fw_updater.PREVIOUS"},
Chris Morin853d3442019-04-01 21:35:13 -0700117 {kFile, "bios_info", "/var/log/bios_info.txt"},
118 {kCommand, "bios_log", "cat /sys/firmware/log "
119 "/proc/device-tree/chosen/ap-console-buffer 2>/dev/null"},
120 {kFile, "bios_times", "/var/log/bios_times.txt"},
121 {kCommand, "board-specific",
122 "/usr/share/userfeedback/scripts/get_board_specific_info"},
Anand K Mistryccceb1e2020-01-16 14:00:49 +1100123 // Slow or non-responsive block devices could cause this command to stall. Use
124 // a timeout to prevent this command from blocking log fetching. This command
125 // is expected to take O(100ms) in the normal case.
126 {kCommand, "blkid", "timeout -s KILL 5s /sbin/blkid", kRoot, kRoot},
Chris Morin853d3442019-04-01 21:35:13 -0700127 {kFile, "buddyinfo", "/proc/buddyinfo"},
128 {kCommand, "cbi_info", "/usr/share/userfeedback/scripts/cbi_info", kRoot,
129 kRoot},
130 {kFile, "cheets_log", "/var/log/arc.log"},
131 {kFile, "clobber.log", "/var/log/clobber.log"},
132 {kFile, "clobber-state.log", "/var/log/clobber-state.log"},
Sonny Rao9e65ddd2019-06-06 17:18:26 -0700133 {kCommand, "chromeos-pgmem", "/usr/bin/chromeos-pgmem"},
Chris Morin853d3442019-04-01 21:35:13 -0700134 {kFile, "chrome_system_log", "/var/log/chrome/chrome"},
135 {kFile, "chrome_system_log.PREVIOUS", "/var/log/chrome/chrome.PREVIOUS"},
Mike Frysinger32cdf3e2017-08-14 18:17:06 -0400136 // There might be more than one record, so grab them all.
137 // Plus, for <linux-3.19, it's named "console-ramoops", but for newer
138 // versions, it's named "console-ramoops-#".
Chris Morin853d3442019-04-01 21:35:13 -0700139 {kCommand, "console-ramoops",
140 "cat /sys/fs/pstore/console-ramoops* 2>/dev/null"},
141 {kFile, "cpuinfo", "/proc/cpuinfo"},
142 {kFile, "cr50_version", "/var/cache/cr50-version"},
143 {kFile, "cros_ec.log", "/var/log/cros_ec.log"},
144 {kFile, "cros_ec.previous", "/var/log/cros_ec.previous"},
145 {kFile, "cros_ec_panicinfo", "/sys/kernel/debug/cros_ec/panicinfo",
146 SandboxedProcess::kDefaultUser, kDebugfsGroup},
147 {kFile, "cros_ec_pdinfo", "/sys/kernel/debug/cros_ec/pdinfo",
148 SandboxedProcess::kDefaultUser, kDebugfsGroup},
149 {kFile, "cros_fp.previous", "/var/log/cros_fp.previous"},
150 {kFile, "cros_fp.log", "/var/log/cros_fp.log"},
Mathew Kingd7a72622019-06-27 09:56:34 -0600151 {kFile, "cros_ish.previous", "/var/log/cros_ish.previous"},
152 {kFile, "cros_ish.log", "/var/log/cros_ish.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700153 {kCommand, "dmesg", "/bin/dmesg"},
154 {kFile, "ec_info", "/var/log/ec_info.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700155 {kCommand, "edid-decode",
156 "for f in /sys/class/drm/card0-*/edid; do "
157 "echo \"----------- ${f}\"; "
Chris Morin853d3442019-04-01 21:35:13 -0700158 // edid-decode's stderr output is redundant, so silence it.
Jeffrey Kardatzke9ba9f322019-08-29 10:23:14 -0700159 "edid-decode \"${f}\" 2>/dev/null; "
Chris Morin853d3442019-04-01 21:35:13 -0700160 "done"},
161 {kFile, "eventlog", "/var/log/eventlog.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700162 {kCommand, "font_info", "/usr/share/userfeedback/scripts/font_info"},
Kuo-Hsin Yang95296e12020-03-06 17:52:35 +0800163 {kCommand, "framebuffer", "cat /sys/kernel/debug/dri/?/framebuffer",
164 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700165 {kFile, "fwupd_state", "/var/lib/fwupd/state.json"},
166 {kCommand, "sensor_info", "/usr/share/userfeedback/scripts/sensor_info"},
167 {kFile, "hammerd", "/var/log/hammerd.log"},
168 {kCommand, "hardware_class", "/usr/bin/crossystem hwid"},
Yong Hong15e4b032020-03-05 15:41:31 +0800169 {kFile, "hardware_verification_report",
170 "/var/cache/hardware_verifier.result"},
Chris Morin853d3442019-04-01 21:35:13 -0700171 {kCommand, "hostname", "/bin/hostname"},
172 {kFile, "i915_gem_gtt", "/sys/kernel/debug/dri/0/i915_gem_gtt",
173 SandboxedProcess::kDefaultUser, kDebugfsGroup},
174 {kFile, "i915_gem_objects", "/sys/kernel/debug/dri/0/i915_gem_objects",
175 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700176 {kCommand, "i915_error_state",
177 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2>/dev/null",
178 SandboxedProcess::kDefaultUser, kDebugfsGroup, Log::kDefaultMaxBytes,
Chris Morin790fd262019-04-03 20:29:36 -0700179 LogTool::Encoding::kBase64},
Chris Morin853d3442019-04-01 21:35:13 -0700180 {kCommand, "ifconfig", "/bin/ifconfig -a"},
181 {kFile, "input_devices", "/proc/bus/input/devices"},
Eric Carusob1820c02017-08-24 15:39:56 -0700182 // Hardware capabilities of the wiphy device.
Alex Levine1c6d572019-09-17 14:45:33 -0700183 {kFile, "interrupts", "/proc/interrupts"},
Chris Morin853d3442019-04-01 21:35:13 -0700184 {kCommand, "iw_list", "/usr/sbin/iw list"},
Eric Carusoa879fd92017-10-11 12:57:10 -0700185#if USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700186 {kCommand, "iwlmvm_module_params", CMD_KERNEL_MODULE_PARAMS(iwlmvm)},
187 {kCommand, "iwlwifi_module_params", CMD_KERNEL_MODULE_PARAMS(iwlwifi)},
Eric Carusoa879fd92017-10-11 12:57:10 -0700188#endif // USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700189 {kCommand, "kernel-crashes",
190 "cat /var/spool/crash/kernel.*.kcrash 2>/dev/null"},
Anand K Mistryccceb1e2020-01-16 14:00:49 +1100191 {kCommand, "lsblk", "timeout -s KILL 5s lsblk -a", kRoot, kRoot,
192 Log::kDefaultMaxBytes, LogTool::Encoding::kAutodetect, true},
Chris Morin853d3442019-04-01 21:35:13 -0700193 {kCommand, "lsmod", "lsmod"},
194 {kCommand, "lspci", "/usr/sbin/lspci"},
195 {kCommand, "lsusb", "lsusb && lsusb -t"},
Kuo-Hsin Yang66d89832020-02-10 17:22:28 +0800196 {kFile, "mali_memory", "/sys/kernel/debug/mali0/gpu_memory",
197 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700198 {kFile, "memd.parameters", "/var/log/memd/memd.parameters"},
199 {kCommand, "memd clips", "cat /var/log/memd/memd.clip* 2>/dev/null"},
200 {kFile, "meminfo", "/proc/meminfo"},
201 {kCommand, "memory_spd_info",
202 // mosys may use 'i2c-dev', which may not be loaded yet.
203 "modprobe i2c-dev 2>/dev/null && mosys -l memory spd print all 2>/dev/null",
204 kRoot, kDebugfsGroup},
Simon Quecb63b9c2017-06-19 14:53:31 -0400205 // The sed command finds the EDID blob (starting the line after "value:") and
206 // replaces the serial number with all zeroes.
207 //
208 // The EDID is printed as a hex dump over several lines, each line containing
209 // the contents of 16 bytes. The first 16 bytes are broken down as follows:
210 // uint64_t fixed_pattern; // Always 00 FF FF FF FF FF FF 00.
211 // uint16_t manufacturer_id; // Manufacturer ID, encoded as PNP IDs.
212 // uint16_t product_code; // Manufacturer product code, little-endian.
213 // uint32_t serial_number; // Serial number, little-endian.
214 // Source: https://en.wikipedia.org/wiki/EDID#EDID_1.3_data_format
215 //
216 // The subsequent substitution command looks for the fixed pattern followed by
217 // two 32-bit fields (manufacturer + product, serial number). It replaces the
218 // latter field with 8 bytes of zeroes.
219 //
220 // TODO(crbug.com/731133): Remove the sed command once modetest itself can
221 // remove serial numbers.
Chris Morin853d3442019-04-01 21:35:13 -0700222 {kCommand, "modetest",
223 "(modetest; modetest -M evdi; modetest -M udl) | "
224 "sed -E '/EDID/ {:a;n;/value:/!ba;n;"
225 "s/(00f{12}00)([0-9a-f]{8})([0-9a-f]{8})/\\1\\200000000/}'",
226 kRoot, kRoot},
227 {kFile, "mount-encrypted", "/var/log/mount-encrypted.log"},
228 {kFile, "mountinfo", "/proc/1/mountinfo"},
229 {kCommand, "netlog",
230 "/usr/share/userfeedback/scripts/getmsgs /var/log/net.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700231 {kFile, "nvmap_iovmm", "/sys/kernel/debug/nvmap/iovmm/allocations",
232 SandboxedProcess::kDefaultUser, kDebugfsGroup},
233 {kCommand, "oemdata", "/usr/share/cros/oemdata.sh", kRoot, kRoot},
Kuo-Hsin Yanga69ecc62020-03-11 17:37:11 +0800234 {kFile, "pagetypeinfo", "/proc/pagetypeinfo", kRoot},
Jack Rosenthal3cf794a2020-02-19 13:32:56 -0700235 {kFile, "platform_identity_name",
236 "/run/chromeos-config/v1/identity/platform-name"},
237 {kFile, "platform_identity_model", "/run/chromeos-config/v1/name"},
238 {kFile, "platform_identity_sku", "/run/chromeos-config/v1/identity/sku-id"},
239 {kFile, "platform_identity_whitelabel_tag",
240 "/run/chromeos-config/v1/identity/whitelabel-tag"},
241 {kFile, "platform_identity_customization_id",
242 "/run/chromeos-config/v1/identity/customization-id"},
Chris Morin853d3442019-04-01 21:35:13 -0700243 {kCommand, "power_supply_info", "/usr/bin/power_supply_info"},
244 {kCommand, "power_supply_sysfs", "/usr/bin/print_sysfs_power_supply_data"},
245 {kFile, "powerd.LATEST", "/var/log/power_manager/powerd.LATEST"},
246 {kFile, "powerd.PREVIOUS", "/var/log/power_manager/powerd.PREVIOUS"},
247 {kFile, "powerd.out", "/var/log/powerd.out"},
248 {kFile, "powerwash_count", "/var/log/powerwash_count"},
Brian Norris4cde3d12019-04-16 10:10:34 -0700249 {kCommand, "ps", "/bin/ps auxZ"},
yusukes34171ba2017-04-27 15:46:01 -0700250 // /proc/slabinfo is owned by root and has 0400 permission.
Chris Morin853d3442019-04-01 21:35:13 -0700251 {kFile, "slabinfo", "/proc/slabinfo", kRoot, kRoot},
252 {kFile, "storage_info", "/var/log/storage_info.txt"},
253 {kCommand, "swap_info", "/usr/share/cros/init/swap.sh status 2>/dev/null",
254 SandboxedProcess::kDefaultUser, kDebugfsGroup},
255 {kCommand, "syslog",
256 "/usr/share/userfeedback/scripts/getmsgs /var/log/messages"},
257 {kCommand, "system_log_stats",
258 "echo 'BLOCK_SIZE=1024'; "
259 "find /var/log/ -type f -exec du --block-size=1024 {} + | sort -n -r",
260 kRoot, kRoot},
261 {kCommand, "threads", "/bin/ps -T axo pid,ppid,spid,pcpu,ni,stat,time,comm"},
262 {kFile, "tlsdate", "/var/log/tlsdate.log"},
Nick Sandersad5dc132019-11-15 15:59:42 -0800263 {kCommand, "top thread", "/usr/bin/top -Hbc -w128 -n 1 | head -n 40"},
264 {kCommand, "top memory",
265 "/usr/bin/top -o \"+%MEM\" -w128 -bcn 1 | head -n 57"},
Chris Morin853d3442019-04-01 21:35:13 -0700266 {kCommand, "touch_fw_version",
267 "grep -E"
268 " -e 'synaptics: Touchpad model'"
269 " -e 'chromeos-[a-z]*-touch-[a-z]*-update'"
270 " /var/log/messages | tail -n 20"},
271 {kCommand, "tpm-firmware-updater", "/usr/share/userfeedback/scripts/getmsgs "
272 "/var/log/tpm-firmware-updater.log"},
Mattias Nissler887dce22017-07-03 14:44:35 +0200273 // TODO(jorgelo,mnissler): Don't run this as root.
274 // On TPM 1.2 devices this will likely require adding a new user to the 'tss'
275 // group.
276 // On TPM 2.0 devices 'get_version_info' uses D-Bus and therefore can run as
277 // any user.
Chris Morin853d3442019-04-01 21:35:13 -0700278 {kCommand, "tpm_version", "/usr/sbin/tpm-manager get_version_info", kRoot,
279 kRoot},
280 {kCommand, "atmel_ts_refs",
281 "/opt/google/touch/scripts/atmel_tools.sh ts r", kRoot, kRoot},
282 {kCommand, "atmel_tp_refs",
283 "/opt/google/touch/scripts/atmel_tools.sh tp r", kRoot, kRoot},
284 {kCommand, "atmel_ts_deltas",
285 "/opt/google/touch/scripts/atmel_tools.sh ts d", kRoot, kRoot},
286 {kCommand, "atmel_tp_deltas",
287 "/opt/google/touch/scripts/atmel_tools.sh tp d", kRoot, kRoot},
288 {kFile, "stateful_trim_state", "/var/lib/trim/stateful_trim_state"},
289 {kFile, "stateful_trim_data", "/var/lib/trim/stateful_trim_data"},
290 {kFile, "ui_log", "/var/log/ui/ui.LATEST"},
291 {kCommand, "uname", "/bin/uname -a"},
292 {kCommand, "update_engine.log",
293 "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
294 " s.^./var/log/update_engine/.)"},
Chris Morinca152712019-05-03 13:17:28 -0700295 {kFile, "upstart", "/var/log/upstart.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700296 {kCommand, "uptime", "/usr/bin/cut -d' ' -f1 /proc/uptime"},
297 {kFile, "verified boot", "/var/log/debug_vboot_noisy.log"},
298 {kFile, "vmlog.1.LATEST", "/var/log/vmlog/vmlog.1.LATEST"},
299 {kFile, "vmlog.1.PREVIOUS", "/var/log/vmlog/vmlog.1.PREVIOUS"},
300 {kFile, "vmlog.LATEST", "/var/log/vmlog/vmlog.LATEST"},
301 {kFile, "vmlog.PREVIOUS", "/var/log/vmlog/vmlog.PREVIOUS"},
302 {kFile, "vmstat", "/proc/vmstat"},
303 {kFile, "vpd_2.0", "/var/log/vpd_2.0.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700304 {kFile, "zram compressed data size", "/sys/block/zram0/compr_data_size"},
305 {kFile, "zram original data size", "/sys/block/zram0/orig_data_size"},
306 {kFile, "zram total memory used", "/sys/block/zram0/mem_used_total"},
307 {kFile, "zram total reads", "/sys/block/zram0/num_reads"},
308 {kFile, "zram total writes", "/sys/block/zram0/num_writes"},
309 {kCommand, "zram new stats names",
310 "echo orig_size compr_size used_total limit used_max zero_pages migrated"},
311 {kFile, "zram new stats values", "/sys/block/zram0/mm_stat"},
312 {kFile, "cros_tp version", "/sys/class/chromeos/cros_tp/version"},
313 {kCommand, "cros_tp console", "/usr/sbin/ectool --name=cros_tp console",
314 kRoot, kRoot},
315 {kCommand, "cros_tp frame", "/usr/sbin/ectool --name=cros_tp tpframeget",
316 kRoot, kRoot},
317 {kCommand, "crostini", "/usr/bin/cicerone_client --get_info"},
Sean Paul5ce118f2019-12-05 08:41:32 -0500318 {kFile, "drm_trace", "/sys/kernel/debug/dri/trace",
319 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Elly Jones03cd6d72012-06-11 13:04:28 -0400320 // Stuff pulled out of the original list. These need access to the running X
321 // session, which we'd rather not give to debugd, or return info specific to
322 // the current session (in the setsid(2) sense), which is not useful for
323 // debugd
Chris Morin853d3442019-04-01 21:35:13 -0700324 // {kCommand, "env", "set"},
325 // {kCommand, "setxkbmap", "/usr/bin/setxkbmap -print -query"},
326 // {kCommand, "xrandr", "/usr/bin/xrandr --verbose}
Elly Jones533c7c42012-08-10 15:07:05 -0400327};
328
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700329// netstat and logcat should appear in chrome://system but not in feedback
330// reports. Open sockets may have privacy implications, and logcat is
331// already incorporated via arc-bugreport.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700332const std::vector<Log> kExtraLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800333#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700334 {kCommand, "mm-status", "/usr/bin/modem status"},
Ben Chan36e42282014-02-12 22:32:34 -0800335#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700336 {kCommand, "network-devices", "/usr/bin/connectivity show devices"},
337 {kCommand, "network-services", "/usr/bin/connectivity show services"},
Jeffrey Kardatzke36791f22019-07-11 11:53:22 -0700338 {kCommand, "wifi_status_no_anonymize",
339 "/usr/bin/network_diag --wifi-internal --no-log"},
Chris Morin253a2b02019-04-12 16:04:25 -0700340 // --processes requires root.
341 {kCommand, "netstat",
342 "/sbin/ss --all --query inet --numeric --processes", kRoot, kRoot},
Kansho Nishida6ae546f2019-08-13 17:14:58 +0900343 {kCommand, "logcat",
344 "/usr/bin/nsenter -t1 -m /usr/sbin/android-sh -c '/system/bin/logcat -d'",
Chris Morin253a2b02019-04-12 16:04:25 -0700345 kRoot, kRoot, Log::kDefaultMaxBytes, LogTool::Encoding::kUtf8},
Elly Jones533c7c42012-08-10 15:07:05 -0400346};
347
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700348const std::vector<Log> kFeedbackLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800349#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700350 {kCommand, "mm-status", "/usr/bin/modem status-feedback"},
Ben Chan36e42282014-02-12 22:32:34 -0800351#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700352 {kCommand, "network-devices",
353 "/usr/bin/connectivity show-feedback devices"},
354 {kCommand, "network-services",
355 "/usr/bin/connectivity show-feedback services"},
Jeffrey Kardatzke36791f22019-07-11 11:53:22 -0700356 {kCommand, "wifi_status",
357 "/usr/bin/network_diag --wifi-internal --no-log --anonymize"},
Elly Jones03cd6d72012-06-11 13:04:28 -0400358};
359
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700360// Fills |dictionary| with the contents of the logs in |logs|.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700361void GetLogsInDictionary(const std::vector<Log>& logs,
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800362 base::DictionaryValue* dictionary) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700363 for (const Log& log : logs) {
Hidehiko Abe22667262019-08-15 01:32:23 +0900364 dictionary->SetKey(log.GetName(), base::Value(log.GetLogData()));
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800365 }
366}
367
368// Serializes the |dictionary| into the file with the given |fd| in a JSON
369// format.
370void SerializeLogsAsJSON(const base::DictionaryValue& dictionary,
Eric Caruso0b241882018-04-04 13:43:46 -0700371 const base::ScopedFD& fd) {
Eric Caruso96d03d32017-04-25 18:01:17 -0700372 string logs_json;
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800373 base::JSONWriter::WriteWithOptions(dictionary,
374 base::JSONWriter::OPTIONS_PRETTY_PRINT,
375 &logs_json);
Eric Caruso0b241882018-04-04 13:43:46 -0700376 base::WriteFileDescriptor(fd.get(), logs_json.c_str(), logs_json.size());
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800377}
378
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700379bool GetNamedLogFrom(const string& name,
380 const std::vector<Log>& logs,
Elly Jones533c7c42012-08-10 15:07:05 -0400381 string* result) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700382 for (const Log& log : logs) {
383 if (name == log.GetName()) {
384 *result = log.GetLogData();
Elly Jones533c7c42012-08-10 15:07:05 -0400385 return true;
386 }
387 }
388 *result = "<invalid log name>";
389 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400390}
391
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700392void GetLogsFrom(const std::vector<Log>& logs, LogTool::LogMap* map) {
393 for (const Log& log : logs)
394 (*map)[log.GetName()] = log.GetLogData();
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800395}
396
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600397void GetLsbReleaseInfo(LogTool::LogMap* map) {
398 const base::FilePath lsb_release(kLsbReleasePath);
399 brillo::KeyValueStore store;
400 if (!store.Load(lsb_release)) {
401 // /etc/lsb-release might not be present (cros deploying a new
402 // configuration or no fields set at all). Just print a debug
403 // message and continue.
404 DLOG(INFO) << "Could not load fields from " << lsb_release.value();
405 } else {
406 for (const auto& key : store.GetKeys()) {
407 std::string value;
408 store.GetString(key, &value);
409 (*map)[key] = value;
410 }
411 }
412}
413
414void GetOsReleaseInfo(LogTool::LogMap* map) {
415 brillo::OsReleaseReader reader;
416 reader.Load();
417 for (const auto& key : reader.GetKeys()) {
418 std::string value;
419 reader.GetString(key, &value);
420 (*map)["os-release " + key] = value;
421 }
422}
423
424void PopulateDictionaryValue(const LogTool::LogMap& map,
425 base::DictionaryValue* dictionary) {
426 for (const auto& kv : map) {
427 dictionary->SetString(kv.first, kv.second);
428 }
429}
430
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800431bool CompressXzBuffer(const std::vector<uint8_t>& in_buffer,
432 std::vector<uint8_t>* out_buffer) {
433 size_t out_size = lzma_stream_buffer_bound(in_buffer.size());
434 out_buffer->resize(out_size);
435 size_t out_pos = 0;
436
437 lzma_ret ret = lzma_easy_buffer_encode(
438 LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64, nullptr, in_buffer.data(),
439 in_buffer.size(), out_buffer->data(), &out_pos, out_size);
440
441 if (ret != LZMA_OK) {
442 out_buffer->clear();
443 return false;
444 }
445
446 out_buffer->resize(out_pos);
447 return true;
448}
449
450void GetPerfData(LogTool::LogMap* map) {
451 // Run perf to collect system-wide performance profile when user triggers
452 // feedback report. Perf runs at sampling frequency of ~500 hz (499 is used
453 // to avoid sampling periodic system activities), with callstack in each
454 // sample (-g).
455 std::vector<std::string> perf_args = {
456 "perf", "record", "-a", "-g", "-F", "499"
457 };
458 std::vector<uint8_t> perf_data;
459 int32_t status;
460
461 debugd::PerfTool perf_tool;
462 if (!perf_tool.GetPerfOutput(kPerfDurationSecs, perf_args, &perf_data,
463 nullptr, &status, nullptr))
464 return;
465
466 // XZ compress the profile data.
467 std::vector<uint8_t> perf_data_xz;
468 if (!CompressXzBuffer(perf_data, &perf_data_xz))
469 return;
470
471 // Base64 encode the compressed data.
472 std::string perf_data_str(reinterpret_cast<const char*>(perf_data_xz.data()),
473 perf_data_xz.size());
474 (*map)["perf-data"] =
Chinglin Yu3c8d0a22019-02-20 11:32:52 +0800475 std::string(kPerfDataDescription) +
Chris Morin790fd262019-04-03 20:29:36 -0700476 LogTool::EncodeString(std::move(perf_data_str),
477 LogTool::Encoding::kBase64);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800478}
479
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800480} // namespace
481
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700482Log::Log(Log::LogType type,
483 std::string name,
484 std::string data,
485 std::string user,
486 std::string group,
487 int64_t max_bytes,
Brian Norrisafc9f632019-05-09 14:08:28 -0700488 LogTool::Encoding encoding,
489 bool access_root_mount_ns)
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700490 : type_(type),
491 name_(name),
492 data_(data),
493 user_(user),
494 group_(group),
495 max_bytes_(max_bytes),
Brian Norrisafc9f632019-05-09 14:08:28 -0700496 encoding_(encoding),
497 access_root_mount_ns_(access_root_mount_ns) {}
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700498
499std::string Log::GetName() const {
500 return name_;
501}
502
503std::string Log::GetLogData() const {
504 // The reason this code uses a switch statement on a type enum rather than
505 // using inheritance/virtual dispatch is so that all of the Log objects can
506 // be constructed statically. Switching to heap allocated subclasses of Log
507 // makes the code that declares all of the log entries much more verbose
508 // and harder to understand.
Chris Morin790fd262019-04-03 20:29:36 -0700509 std::string output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700510 switch (type_) {
511 case kCommand:
Chris Morin790fd262019-04-03 20:29:36 -0700512 output = GetCommandLogData();
513 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700514 case kFile:
Chris Morin790fd262019-04-03 20:29:36 -0700515 output = GetFileLogData();
516 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700517 default:
518 return "<unknown log type>";
519 }
Chris Morin790fd262019-04-03 20:29:36 -0700520
521 if (output.empty())
522 return "<empty>";
523
524 return LogTool::EncodeString(std::move(output), encoding_);
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700525}
526
527// TODO(ellyjones): sandbox. crosbug.com/35122
528std::string Log::GetCommandLogData() const {
529 if (type_ != kCommand)
530 return "<log type mismatch>";
531 std::string tailed_cmdline =
532 base::StringPrintf("%s | tail -c %" PRId64, data_.c_str(), max_bytes_);
533 ProcessWithOutput p;
534 if (minijail_disabled_for_test_)
535 p.set_use_minijail(false);
536 if (!user_.empty() && !group_.empty())
537 p.SandboxAs(user_, group_);
Brian Norrisafc9f632019-05-09 14:08:28 -0700538 if (access_root_mount_ns_)
539 p.AllowAccessRootMountNamespace();
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700540 if (!p.Init())
541 return "<not available>";
542 p.AddArg(kShell);
543 p.AddStringOption("-c", tailed_cmdline);
544 if (p.Run())
545 return "<not available>";
546 std::string output;
547 p.GetOutput(&output);
Chris Morin790fd262019-04-03 20:29:36 -0700548 return output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700549}
550
551std::string Log::GetFileLogData() const {
552 if (type_ != kFile)
553 return "<log type mismatch>";
554
555 uid_t old_euid = geteuid();
556 uid_t new_euid = UidForUser(user_);
557 gid_t old_egid = getegid();
558 gid_t new_egid = GidForGroup(group_);
559
560 if (new_euid == -1 || new_egid == -1) {
561 return "<not available>";
562 }
563
564 // Make sure to set group first, since if we set user first we lose root
565 // and therefore the ability to set our effective gid to arbitrary gids.
566 if (setegid(new_egid)) {
567 PLOG(ERROR) << "Failed to set effective group id to " << new_egid;
568 return "<not available>";
569 }
570 if (seteuid(new_euid)) {
571 PLOG(ERROR) << "Failed to set effective user id to " << new_euid;
572 if (setegid(old_egid))
573 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
574 return "<not available>";
575 }
576
577 std::string contents;
578 const base::FilePath path(data_);
579 // Handle special files that don't properly report length/allow lseek.
580 if (base::FilePath("/dev").IsParent(path) ||
581 base::FilePath("/proc").IsParent(path) ||
582 base::FilePath("/sys").IsParent(path)) {
583 if (!base::ReadFileToString(path, &contents))
584 contents = "<not available>";
585 if (contents.size() > max_bytes_)
586 contents.erase(0, contents.size() - max_bytes_);
587 } else {
588 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
589 if (!file.IsValid()) {
590 contents = "<not available>";
591 } else {
592 int64_t length = file.GetLength();
593 if (length > max_bytes_) {
594 file.Seek(base::File::FROM_END, -max_bytes_);
595 length = max_bytes_;
596 }
597 std::vector<char> buf(length);
598 int read = file.ReadAtCurrentPos(buf.data(), buf.size());
599 if (read < 0) {
600 PLOG(ERROR) << "Could not read from file " << path.value();
601 } else {
602 contents = std::string(buf.begin(), buf.begin() + read);
603 }
604 }
605 }
606
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700607 // Make sure we restore our old euid/egid before returning.
608 if (seteuid(old_euid))
609 PLOG(ERROR) << "Failed to restore effective user id to " << old_euid;
610
611 if (setegid(old_egid))
612 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
613
Chris Morin790fd262019-04-03 20:29:36 -0700614 return contents;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700615}
616
617void Log::DisableMinijailForTest() {
618 minijail_disabled_for_test_ = true;
619}
620
621// static
622uid_t Log::UidForUser(const std::string& user) {
623 struct passwd entry;
624 struct passwd* result;
625 std::vector<char> buf(1024);
626 getpwnam_r(user.c_str(), &entry, &buf[0], buf.size(), &result);
627 if (!result) {
628 LOG(ERROR) << "User not found: " << user;
629 return -1;
630 }
631 return entry.pw_uid;
632}
633
634// static
635gid_t Log::GidForGroup(const std::string& group) {
636 struct group entry;
637 struct group* result;
638 std::vector<char> buf(1024);
639 getgrnam_r(group.c_str(), &entry, &buf[0], buf.size(), &result);
640 if (!result) {
641 LOG(ERROR) << "Group not found: " << group;
642 return -1;
643 }
644 return entry.gr_gid;
645}
646
mhasank80cbe4d2020-04-02 22:46:08 -0700647LogTool::LogTool(scoped_refptr<dbus::Bus> bus,
648 const base::FilePath& daemon_store_base_dir)
mhasank4f599d32020-04-09 22:07:35 -0700649 : bus_(bus), daemon_store_base_dir_(daemon_store_base_dir) {
650 cryptohome_proxy_ =
651 std::make_unique<org::chromium::CryptohomeInterfaceProxy>(bus);
mhasank80cbe4d2020-04-02 22:46:08 -0700652}
653
mhasank4f599d32020-04-09 22:07:35 -0700654LogTool::LogTool(scoped_refptr<dbus::Bus> bus)
655 : LogTool(bus, base::FilePath(kDaemonStoreBaseDir)) {}
656
mhasank80cbe4d2020-04-02 22:46:08 -0700657base::FilePath LogTool::GetArcBugReportBackupFilePath
658 (const std::string& userhash) {
659 return daemon_store_base_dir_
660 .Append(userhash)
661 .Append(kArcBugReportBackupFileName);
662}
663
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700664void LogTool::CreateConnectivityReport(bool wait_for_results) {
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700665 // Perform ConnectivityTrial to report connection state in feedback log.
Ben Chan8e9f6d02017-09-26 23:04:21 -0700666 auto shill = std::make_unique<org::chromium::flimflam::ManagerProxy>(bus_);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700667 // Give the connection trial time to test the connection and log the results
668 // before collecting the logs for feedback.
669 // TODO(silberst): Replace the simple approach of a single timeout with a more
670 // coordinated effort.
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700671 if (shill && shill->CreateConnectivityReport(nullptr) && wait_for_results)
Eric Carusocc7106c2017-04-27 14:22:42 -0700672 sleep(kConnectionTesterTimeoutSeconds);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700673}
674
Eric Carusoc93a15c2017-04-24 16:15:12 -0700675string LogTool::GetLog(const string& name) {
Elly Jones533c7c42012-08-10 15:07:05 -0400676 string result;
Ben Chancf7d6412017-08-10 22:30:09 -0700677 GetNamedLogFrom(name, kCommandLogs, &result)
678 || GetNamedLogFrom(name, kExtraLogs, &result)
679 || GetNamedLogFrom(name, kFeedbackLogs, &result);
Elly Jones533c7c42012-08-10 15:07:05 -0400680 return result;
681}
682
Eric Carusof9091f82017-04-28 14:18:59 -0700683LogTool::LogMap LogTool::GetAllLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700684 CreateConnectivityReport(false);
Elly Jones533c7c42012-08-10 15:07:05 -0400685 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700686 GetLogsFrom(kCommandLogs, &result);
687 GetLogsFrom(kExtraLogs, &result);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600688 GetLsbReleaseInfo(&result);
689 GetOsReleaseInfo(&result);
Elly Jones533c7c42012-08-10 15:07:05 -0400690 return result;
691}
692
Brian Norrisca4fc042018-04-03 00:24:26 -0700693LogTool::LogMap LogTool::GetAllDebugLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700694 CreateConnectivityReport(true);
Brian Norrisca4fc042018-04-03 00:24:26 -0700695 LogMap result;
696 GetLogsFrom(kCommandLogs, &result);
697 GetLogsFrom(kExtraLogs, &result);
mhasank4f599d32020-04-09 22:07:35 -0700698 result[kArcBugReportLog.GetName()] = GetArcBugReport("");
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600699 GetLsbReleaseInfo(&result);
700 GetOsReleaseInfo(&result);
Brian Norrisca4fc042018-04-03 00:24:26 -0700701 return result;
702}
703
mhasank4f599d32020-04-09 22:07:35 -0700704void LogTool::GetBigFeedbackLogs(const base::ScopedFD& fd,
705 const std::string& username) {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700706 CreateConnectivityReport(true);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800707 LogMap map;
708 GetPerfData(&map);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800709 base::DictionaryValue dictionary;
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700710 GetLogsInDictionary(kCommandLogs, &dictionary);
711 GetLogsInDictionary(kFeedbackLogs, &dictionary);
mhasank4f599d32020-04-09 22:07:35 -0700712 dictionary.SetKey(kArcBugReportLog.GetName(),
713 base::Value(GetArcBugReport(username)));
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600714 GetLsbReleaseInfo(&map);
715 GetOsReleaseInfo(&map);
716 PopulateDictionaryValue(map, &dictionary);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800717 SerializeLogsAsJSON(dictionary, fd);
718}
719
mhasank4f599d32020-04-09 22:07:35 -0700720std::string GetSanitizedUsername(
721 org::chromium::CryptohomeInterfaceProxyInterface* cryptohome_proxy,
722 std::string username) {
723 if (username.empty()) {
724 return std::string();
725 }
726
727 std::string sanitized_username;
728 brillo::ErrorPtr error;
729 if (!cryptohome_proxy->GetSanitizedUsername(username, &sanitized_username,
730 &error)) {
731 LOG(ERROR) << "Failed to call GetSanitizedUsername, error: "
732 << error->GetMessage();
733 return std::string();
734 }
735
736 return sanitized_username;
737}
738
739std::string LogTool::GetArcBugReport(const std::string& username) {
740 std::string userhash =
741 GetSanitizedUsername(cryptohome_proxy_.get(), username);
742
743 std::string contents;
744 if (userhash.empty() ||
745 arc_bug_report_backups_.find(userhash) == arc_bug_report_backups_.end() ||
746 !base::ReadFileToString(GetArcBugReportBackupFilePath(userhash),
747 &contents)) {
748 // If |userhash| was not empty, but was not found in the backup set
749 // or the file did not exist, attempt to delete the file.
750 if (!userhash.empty()) {
751 DeleteArcBugReportBackup(userhash);
752 }
753 contents = kArcBugReportLog.GetLogData();
754 }
755
756 return contents;
757}
758
mhasank80cbe4d2020-04-02 22:46:08 -0700759void LogTool::BackupArcBugReport(const std::string& userhash) {
760 DLOG(INFO) << "Backing up ARC bug report";
761
762 const base::FilePath reportPath = GetArcBugReportBackupFilePath(userhash);
763 const std::string logData = kArcBugReportLog.GetLogData();
mhasank4f599d32020-04-09 22:07:35 -0700764 if (base::WriteFile(reportPath, logData.c_str(), logData.length())) {
765 arc_bug_report_backups_.insert(userhash);
766 } else {
mhasank80cbe4d2020-04-02 22:46:08 -0700767 PLOG(ERROR) << "Failed to backup ARC bug report";
768 }
769}
770
771void LogTool::DeleteArcBugReportBackup(const std::string& userhash) {
772 DLOG(INFO) << "Deleting the ARC bug report backup";
773
774 const base::FilePath reportPath = GetArcBugReportBackupFilePath(userhash);
mhasank4f599d32020-04-09 22:07:35 -0700775 arc_bug_report_backups_.erase(userhash);
mhasank80cbe4d2020-04-02 22:46:08 -0700776 if (!base::DeleteFile(reportPath, false)) {
777 PLOG(ERROR) << "Failed to delete ARC bug report backup";
778 }
779}
780
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700781void LogTool::GetJournalLog(const base::ScopedFD& fd) {
Chris Morin790fd262019-04-03 20:29:36 -0700782 Log journal(kCommand, "journal.export", "journalctl -n 10000 -o export",
783 "syslog", "syslog", 10 * 1024 * 1024, LogTool::Encoding::kBinary);
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700784 std::string output = journal.GetLogData();
Chris Morin790fd262019-04-03 20:29:36 -0700785 base::WriteFileDescriptor(fd.get(), output.data(), output.size());
786}
787
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700788// static
Chris Morin790fd262019-04-03 20:29:36 -0700789string LogTool::EncodeString(string value,
790 LogTool::Encoding source_encoding) {
791 if (source_encoding == LogTool::Encoding::kBinary)
792 return value;
793
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700794 if (source_encoding == LogTool::Encoding::kAutodetect) {
795 if (base::IsStringUTF8(value))
796 return value;
Chris Morin790fd262019-04-03 20:29:36 -0700797 source_encoding = LogTool::Encoding::kBase64;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700798 }
799
800 if (source_encoding == LogTool::Encoding::kUtf8) {
801 string output;
802 const char* src = value.data();
803 int32_t src_len = static_cast<int32_t>(value.length());
804
805 output.reserve(value.size());
806 for (int32_t char_index = 0; char_index < src_len; char_index++) {
807 uint32_t code_point;
808 if (!base::ReadUnicodeCharacter(src, src_len, &char_index, &code_point) ||
809 !base::IsValidCharacter(code_point)) {
810 // Replace invalid characters with U+FFFD REPLACEMENT CHARACTER.
811 code_point = 0xFFFD;
812 }
813 base::WriteUnicodeCharacter(code_point, &output);
814 }
815 return output;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700816 }
Chris Morin853d3442019-04-01 21:35:13 -0700817
818 base::Base64Encode(value, &value);
819 return "<base64>: " + value;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700820}
821
Ben Chana0011d82014-05-13 00:19:29 -0700822} // namespace debugd