blob: 36a5ba6b95690c841d483930bef5a80e2579bab4 [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";
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080052
53// Minimum time in seconds needed to allow shill to test active connections.
54const int kConnectionTesterTimeoutSeconds = 5;
Ben Chanf6cd93a2012-10-14 19:37:00 -070055
Chinglin Yuaeb4ec72018-12-10 18:53:30 +080056// Default running perf for 2 seconds.
57constexpr const int kPerfDurationSecs = 2;
Chinglin Yu3c8d0a22019-02-20 11:32:52 +080058// TODO(chinglinyu) Remove after crbug/934702 is fixed.
59// The following description is added to 'perf-data' as a temporary solution
60// before the update of feedback disclosure to users is done in crbug/934702.
61constexpr const char kPerfDataDescription[] =
62 "perf-data contains performance profiling information about how much time "
63 "the system spends on various activities (program execution stack traces). "
64 "This might reveal some information about what system features and "
65 "resources are being used. The full detail of perf-data can be found in "
66 "the PerfDataProto protocol buffer message type in the chromium source "
67 "repository.\n";
Chinglin Yuaeb4ec72018-12-10 18:53:30 +080068
Eric Carusoa879fd92017-10-11 12:57:10 -070069#define CMD_KERNEL_MODULE_PARAMS(module_name) \
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070070 "cd /sys/module/" #module_name "/parameters 2>/dev/null && grep -sH ^ *"
Eric Carusoa879fd92017-10-11 12:57:10 -070071
Fletcher Woodruff07c28532019-01-24 11:08:53 -070072using Log = LogTool::Log;
73constexpr Log::LogType kCommand = Log::kCommand;
74constexpr Log::LogType kFile = Log::kFile;
Miriam Zimmermand91d8e72019-06-27 12:24:04 -070075// NOTE: IF YOU ADD AN ENTRY TO THIS LIST, PLEASE:
76// * add a row to http://go/cros-feedback-audit and fill it out
77// * email cros-monitoring-forensics@
78// (Eventually we'll have a better process, but for now please do this.)
Fletcher Woodruff07c28532019-01-24 11:08:53 -070079const std::vector<Log> kCommandLogs {
Mike Frysingerb0350992018-09-14 13:45:35 -040080 // We need to enter init's mount namespace because it has /home/chronos
81 // mounted which is where the consent knob lives. We don't have that mount
82 // in our own mount namespace (by design). https://crbug.com/884249
Chris Morin853d3442019-04-01 21:35:13 -070083 {kCommand, "CLIENT_ID", "/usr/bin/nsenter -t1 -m /usr/bin/metrics_client -i",
84 kRoot, kDebugfsGroup},
85 {kCommand, "LOGDATE", "/bin/date"},
Yusuke Sato27a31672019-04-29 15:26:37 -070086 // We need to enter init's mount namespace to access /home/root. Also, we use
87 // neither ARC container's mount namespace (with android-sh) nor
88 // /opt/google/containers/android/rootfs/android-data/ so that we can get
89 // results even when the container is down.
90 {kCommand, "android_app_storage", "/usr/bin/nsenter -t1 -m "
91 "/bin/sh -c \"/usr/bin/du -h /home/root/*/android-data/data/\"",
92 kRoot, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -070093 {kFile, "atrus_logs", "/var/log/atrus.log"},
94 {kFile, "authpolicy", "/var/log/authpolicy.log"},
Brian Norrisafc9f632019-05-09 14:08:28 -070095 {kCommand, "bootstat_summary", "/usr/bin/bootstat_summary",
96 SandboxedProcess::kDefaultUser, SandboxedProcess::kDefaultGroup,
97 Log::kDefaultMaxBytes, LogTool::Encoding::kAutodetect, true},
Craig Hesling5c384b52019-04-20 15:18:06 -070098 {kFile, "bio_crypto_init.LATEST",
99 "/var/log/bio_crypto_init/bio_crypto_init.LATEST"},
100 {kFile, "bio_crypto_init.PREVIOUS",
101 "/var/log/bio_crypto_init/bio_crypto_init.PREVIOUS"},
Chris Morin853d3442019-04-01 21:35:13 -0700102 {kFile, "biod.LATEST", "/var/log/biod/biod.LATEST"},
103 {kFile, "biod.PREVIOUS", "/var/log/biod/biod.PREVIOUS"},
Craig Hesling4c3891e2019-04-20 12:53:54 -0700104 {kFile, "bio_fw_updater.LATEST", "/var/log/biod/bio_fw_updater.LATEST"},
105 {kFile, "bio_fw_updater.PREVIOUS", "/var/log/biod/bio_fw_updater.PREVIOUS"},
Chris Morin853d3442019-04-01 21:35:13 -0700106 {kFile, "bios_info", "/var/log/bios_info.txt"},
107 {kCommand, "bios_log", "cat /sys/firmware/log "
108 "/proc/device-tree/chosen/ap-console-buffer 2>/dev/null"},
109 {kFile, "bios_times", "/var/log/bios_times.txt"},
110 {kCommand, "board-specific",
111 "/usr/share/userfeedback/scripts/get_board_specific_info"},
112 {kFile, "buddyinfo", "/proc/buddyinfo"},
113 {kCommand, "cbi_info", "/usr/share/userfeedback/scripts/cbi_info", kRoot,
114 kRoot},
115 {kFile, "cheets_log", "/var/log/arc.log"},
116 {kFile, "clobber.log", "/var/log/clobber.log"},
117 {kFile, "clobber-state.log", "/var/log/clobber-state.log"},
Sonny Rao9e65ddd2019-06-06 17:18:26 -0700118 {kCommand, "chromeos-pgmem", "/usr/bin/chromeos-pgmem"},
Chris Morin853d3442019-04-01 21:35:13 -0700119 {kFile, "chrome_system_log", "/var/log/chrome/chrome"},
120 {kFile, "chrome_system_log.PREVIOUS", "/var/log/chrome/chrome.PREVIOUS"},
Mike Frysinger32cdf3e2017-08-14 18:17:06 -0400121 // There might be more than one record, so grab them all.
122 // Plus, for <linux-3.19, it's named "console-ramoops", but for newer
123 // versions, it's named "console-ramoops-#".
Chris Morin853d3442019-04-01 21:35:13 -0700124 {kCommand, "console-ramoops",
125 "cat /sys/fs/pstore/console-ramoops* 2>/dev/null"},
126 {kFile, "cpuinfo", "/proc/cpuinfo"},
127 {kFile, "cr50_version", "/var/cache/cr50-version"},
128 {kFile, "cros_ec.log", "/var/log/cros_ec.log"},
129 {kFile, "cros_ec.previous", "/var/log/cros_ec.previous"},
130 {kFile, "cros_ec_panicinfo", "/sys/kernel/debug/cros_ec/panicinfo",
131 SandboxedProcess::kDefaultUser, kDebugfsGroup},
132 {kFile, "cros_ec_pdinfo", "/sys/kernel/debug/cros_ec/pdinfo",
133 SandboxedProcess::kDefaultUser, kDebugfsGroup},
134 {kFile, "cros_fp.previous", "/var/log/cros_fp.previous"},
135 {kFile, "cros_fp.log", "/var/log/cros_fp.log"},
Mathew Kingd7a72622019-06-27 09:56:34 -0600136 {kFile, "cros_ish.previous", "/var/log/cros_ish.previous"},
137 {kFile, "cros_ish.log", "/var/log/cros_ish.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700138 {kCommand, "dmesg", "/bin/dmesg"},
139 {kFile, "ec_info", "/var/log/ec_info.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700140 {kCommand, "edid-decode",
141 "for f in /sys/class/drm/card0-*/edid; do "
142 "echo \"----------- ${f}\"; "
Chris Morin853d3442019-04-01 21:35:13 -0700143 // edid-decode's stderr output is redundant, so silence it.
Jeffrey Kardatzke9ba9f322019-08-29 10:23:14 -0700144 "edid-decode \"${f}\" 2>/dev/null; "
Chris Morin853d3442019-04-01 21:35:13 -0700145 "done"},
146 {kFile, "eventlog", "/var/log/eventlog.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700147 {kCommand, "font_info", "/usr/share/userfeedback/scripts/font_info"},
148 {kFile, "fwupd_state", "/var/lib/fwupd/state.json"},
149 {kCommand, "sensor_info", "/usr/share/userfeedback/scripts/sensor_info"},
150 {kFile, "hammerd", "/var/log/hammerd.log"},
151 {kCommand, "hardware_class", "/usr/bin/crossystem hwid"},
Yong Hongee9c5172019-04-18 16:31:26 +0800152 {kCommand, "hardware_verification_report",
153 "/usr/bin/hardware_verifier --output_format=text"},
Chris Morin853d3442019-04-01 21:35:13 -0700154 {kCommand, "hostname", "/bin/hostname"},
155 {kFile, "i915_gem_gtt", "/sys/kernel/debug/dri/0/i915_gem_gtt",
156 SandboxedProcess::kDefaultUser, kDebugfsGroup},
157 {kFile, "i915_gem_objects", "/sys/kernel/debug/dri/0/i915_gem_objects",
158 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700159 {kCommand, "i915_error_state",
160 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2>/dev/null",
161 SandboxedProcess::kDefaultUser, kDebugfsGroup, Log::kDefaultMaxBytes,
Chris Morin790fd262019-04-03 20:29:36 -0700162 LogTool::Encoding::kBase64},
Chris Morin853d3442019-04-01 21:35:13 -0700163 {kCommand, "ifconfig", "/bin/ifconfig -a"},
164 {kFile, "input_devices", "/proc/bus/input/devices"},
Eric Carusob1820c02017-08-24 15:39:56 -0700165 // Hardware capabilities of the wiphy device.
Alex Levine1c6d572019-09-17 14:45:33 -0700166 {kFile, "interrupts", "/proc/interrupts"},
Chris Morin853d3442019-04-01 21:35:13 -0700167 {kCommand, "iw_list", "/usr/sbin/iw list"},
Eric Carusoa879fd92017-10-11 12:57:10 -0700168#if USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700169 {kCommand, "iwlmvm_module_params", CMD_KERNEL_MODULE_PARAMS(iwlmvm)},
170 {kCommand, "iwlwifi_module_params", CMD_KERNEL_MODULE_PARAMS(iwlwifi)},
Eric Carusoa879fd92017-10-11 12:57:10 -0700171#endif // USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700172 {kCommand, "kernel-crashes",
173 "cat /var/spool/crash/kernel.*.kcrash 2>/dev/null"},
Chris Morin853d3442019-04-01 21:35:13 -0700174 {kCommand, "lsmod", "lsmod"},
175 {kCommand, "lspci", "/usr/sbin/lspci"},
176 {kCommand, "lsusb", "lsusb && lsusb -t"},
177 {kFile, "mali_memory", "/sys/class/misc/mali0/device/memory"},
178 {kFile, "memd.parameters", "/var/log/memd/memd.parameters"},
179 {kCommand, "memd clips", "cat /var/log/memd/memd.clip* 2>/dev/null"},
180 {kFile, "meminfo", "/proc/meminfo"},
181 {kCommand, "memory_spd_info",
182 // mosys may use 'i2c-dev', which may not be loaded yet.
183 "modprobe i2c-dev 2>/dev/null && mosys -l memory spd print all 2>/dev/null",
184 kRoot, kDebugfsGroup},
Simon Quecb63b9c2017-06-19 14:53:31 -0400185 // The sed command finds the EDID blob (starting the line after "value:") and
186 // replaces the serial number with all zeroes.
187 //
188 // The EDID is printed as a hex dump over several lines, each line containing
189 // the contents of 16 bytes. The first 16 bytes are broken down as follows:
190 // uint64_t fixed_pattern; // Always 00 FF FF FF FF FF FF 00.
191 // uint16_t manufacturer_id; // Manufacturer ID, encoded as PNP IDs.
192 // uint16_t product_code; // Manufacturer product code, little-endian.
193 // uint32_t serial_number; // Serial number, little-endian.
194 // Source: https://en.wikipedia.org/wiki/EDID#EDID_1.3_data_format
195 //
196 // The subsequent substitution command looks for the fixed pattern followed by
197 // two 32-bit fields (manufacturer + product, serial number). It replaces the
198 // latter field with 8 bytes of zeroes.
199 //
200 // TODO(crbug.com/731133): Remove the sed command once modetest itself can
201 // remove serial numbers.
Chris Morin853d3442019-04-01 21:35:13 -0700202 {kCommand, "modetest",
203 "(modetest; modetest -M evdi; modetest -M udl) | "
204 "sed -E '/EDID/ {:a;n;/value:/!ba;n;"
205 "s/(00f{12}00)([0-9a-f]{8})([0-9a-f]{8})/\\1\\200000000/}'",
206 kRoot, kRoot},
207 {kFile, "mount-encrypted", "/var/log/mount-encrypted.log"},
208 {kFile, "mountinfo", "/proc/1/mountinfo"},
209 {kCommand, "netlog",
210 "/usr/share/userfeedback/scripts/getmsgs /var/log/net.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700211 {kFile, "nvmap_iovmm", "/sys/kernel/debug/nvmap/iovmm/allocations",
212 SandboxedProcess::kDefaultUser, kDebugfsGroup},
213 {kCommand, "oemdata", "/usr/share/cros/oemdata.sh", kRoot, kRoot},
214 {kFile, "pagetypeinfo", "/proc/pagetypeinfo"},
215 {kCommand, "platform_info",
216 // mosys may use 'i2c-dev', which may not be loaded yet.
217 "modprobe i2c-dev 2>/dev/null && "
218 "for param in "
219 "vendor "
220 "name "
221 "version "
222 "family "
223 "model "
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700224
Chris Morin853d3442019-04-01 21:35:13 -0700225 "sku "
226 "customization "
227 "; do "
228 "mosys -l platform \"${param}\" 2>/dev/null; "
229 "done",
230 kRoot, kDebugfsGroup},
231 {kCommand, "power_supply_info", "/usr/bin/power_supply_info"},
232 {kCommand, "power_supply_sysfs", "/usr/bin/print_sysfs_power_supply_data"},
233 {kFile, "powerd.LATEST", "/var/log/power_manager/powerd.LATEST"},
234 {kFile, "powerd.PREVIOUS", "/var/log/power_manager/powerd.PREVIOUS"},
235 {kFile, "powerd.out", "/var/log/powerd.out"},
236 {kFile, "powerwash_count", "/var/log/powerwash_count"},
Brian Norris4cde3d12019-04-16 10:10:34 -0700237 {kCommand, "ps", "/bin/ps auxZ"},
yusukes34171ba2017-04-27 15:46:01 -0700238 // /proc/slabinfo is owned by root and has 0400 permission.
Chris Morin853d3442019-04-01 21:35:13 -0700239 {kFile, "slabinfo", "/proc/slabinfo", kRoot, kRoot},
240 {kFile, "storage_info", "/var/log/storage_info.txt"},
241 {kCommand, "swap_info", "/usr/share/cros/init/swap.sh status 2>/dev/null",
242 SandboxedProcess::kDefaultUser, kDebugfsGroup},
243 {kCommand, "syslog",
244 "/usr/share/userfeedback/scripts/getmsgs /var/log/messages"},
245 {kCommand, "system_log_stats",
246 "echo 'BLOCK_SIZE=1024'; "
247 "find /var/log/ -type f -exec du --block-size=1024 {} + | sort -n -r",
248 kRoot, kRoot},
249 {kCommand, "threads", "/bin/ps -T axo pid,ppid,spid,pcpu,ni,stat,time,comm"},
250 {kFile, "tlsdate", "/var/log/tlsdate.log"},
251 {kCommand, "top thread", "/usr/bin/top -Hb -n 1 | head -n 40"},
252 {kCommand, "top memory", "/usr/bin/top -o \"+%MEM\" -bn 1 | head -n 57"},
253 {kCommand, "touch_fw_version",
254 "grep -E"
255 " -e 'synaptics: Touchpad model'"
256 " -e 'chromeos-[a-z]*-touch-[a-z]*-update'"
257 " /var/log/messages | tail -n 20"},
258 {kCommand, "tpm-firmware-updater", "/usr/share/userfeedback/scripts/getmsgs "
259 "/var/log/tpm-firmware-updater.log"},
Mattias Nissler887dce22017-07-03 14:44:35 +0200260 // TODO(jorgelo,mnissler): Don't run this as root.
261 // On TPM 1.2 devices this will likely require adding a new user to the 'tss'
262 // group.
263 // On TPM 2.0 devices 'get_version_info' uses D-Bus and therefore can run as
264 // any user.
Chris Morin853d3442019-04-01 21:35:13 -0700265 {kCommand, "tpm_version", "/usr/sbin/tpm-manager get_version_info", kRoot,
266 kRoot},
267 {kCommand, "atmel_ts_refs",
268 "/opt/google/touch/scripts/atmel_tools.sh ts r", kRoot, kRoot},
269 {kCommand, "atmel_tp_refs",
270 "/opt/google/touch/scripts/atmel_tools.sh tp r", kRoot, kRoot},
271 {kCommand, "atmel_ts_deltas",
272 "/opt/google/touch/scripts/atmel_tools.sh ts d", kRoot, kRoot},
273 {kCommand, "atmel_tp_deltas",
274 "/opt/google/touch/scripts/atmel_tools.sh tp d", kRoot, kRoot},
275 {kFile, "stateful_trim_state", "/var/lib/trim/stateful_trim_state"},
276 {kFile, "stateful_trim_data", "/var/lib/trim/stateful_trim_data"},
277 {kFile, "ui_log", "/var/log/ui/ui.LATEST"},
278 {kCommand, "uname", "/bin/uname -a"},
279 {kCommand, "update_engine.log",
280 "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
281 " s.^./var/log/update_engine/.)"},
Chris Morinca152712019-05-03 13:17:28 -0700282 {kFile, "upstart", "/var/log/upstart.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700283 {kCommand, "uptime", "/usr/bin/cut -d' ' -f1 /proc/uptime"},
284 {kFile, "verified boot", "/var/log/debug_vboot_noisy.log"},
285 {kFile, "vmlog.1.LATEST", "/var/log/vmlog/vmlog.1.LATEST"},
286 {kFile, "vmlog.1.PREVIOUS", "/var/log/vmlog/vmlog.1.PREVIOUS"},
287 {kFile, "vmlog.LATEST", "/var/log/vmlog/vmlog.LATEST"},
288 {kFile, "vmlog.PREVIOUS", "/var/log/vmlog/vmlog.PREVIOUS"},
289 {kFile, "vmstat", "/proc/vmstat"},
290 {kFile, "vpd_2.0", "/var/log/vpd_2.0.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700291 {kFile, "zram compressed data size", "/sys/block/zram0/compr_data_size"},
292 {kFile, "zram original data size", "/sys/block/zram0/orig_data_size"},
293 {kFile, "zram total memory used", "/sys/block/zram0/mem_used_total"},
294 {kFile, "zram total reads", "/sys/block/zram0/num_reads"},
295 {kFile, "zram total writes", "/sys/block/zram0/num_writes"},
296 {kCommand, "zram new stats names",
297 "echo orig_size compr_size used_total limit used_max zero_pages migrated"},
298 {kFile, "zram new stats values", "/sys/block/zram0/mm_stat"},
299 {kFile, "cros_tp version", "/sys/class/chromeos/cros_tp/version"},
300 {kCommand, "cros_tp console", "/usr/sbin/ectool --name=cros_tp console",
301 kRoot, kRoot},
302 {kCommand, "cros_tp frame", "/usr/sbin/ectool --name=cros_tp tpframeget",
303 kRoot, kRoot},
304 {kCommand, "crostini", "/usr/bin/cicerone_client --get_info"},
Elly Jones03cd6d72012-06-11 13:04:28 -0400305 // Stuff pulled out of the original list. These need access to the running X
306 // session, which we'd rather not give to debugd, or return info specific to
307 // the current session (in the setsid(2) sense), which is not useful for
308 // debugd
Chris Morin853d3442019-04-01 21:35:13 -0700309 // {kCommand, "env", "set"},
310 // {kCommand, "setxkbmap", "/usr/bin/setxkbmap -print -query"},
311 // {kCommand, "xrandr", "/usr/bin/xrandr --verbose}
Elly Jones533c7c42012-08-10 15:07:05 -0400312};
313
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700314// netstat and logcat should appear in chrome://system but not in feedback
315// reports. Open sockets may have privacy implications, and logcat is
316// already incorporated via arc-bugreport.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700317const std::vector<Log> kExtraLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800318#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700319 {kCommand, "mm-status", "/usr/bin/modem status"},
Ben Chan36e42282014-02-12 22:32:34 -0800320#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700321 {kCommand, "network-devices", "/usr/bin/connectivity show devices"},
322 {kCommand, "network-services", "/usr/bin/connectivity show services"},
Jeffrey Kardatzke36791f22019-07-11 11:53:22 -0700323 {kCommand, "wifi_status_no_anonymize",
324 "/usr/bin/network_diag --wifi-internal --no-log"},
Chris Morin253a2b02019-04-12 16:04:25 -0700325 // --processes requires root.
326 {kCommand, "netstat",
327 "/sbin/ss --all --query inet --numeric --processes", kRoot, kRoot},
Kansho Nishida6ae546f2019-08-13 17:14:58 +0900328 {kCommand, "logcat",
329 "/usr/bin/nsenter -t1 -m /usr/sbin/android-sh -c '/system/bin/logcat -d'",
Chris Morin253a2b02019-04-12 16:04:25 -0700330 kRoot, kRoot, Log::kDefaultMaxBytes, LogTool::Encoding::kUtf8},
Elly Jones533c7c42012-08-10 15:07:05 -0400331};
332
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700333const std::vector<Log> kFeedbackLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800334#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700335 {kCommand, "mm-status", "/usr/bin/modem status-feedback"},
Ben Chan36e42282014-02-12 22:32:34 -0800336#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700337 {kCommand, "network-devices",
338 "/usr/bin/connectivity show-feedback devices"},
339 {kCommand, "network-services",
340 "/usr/bin/connectivity show-feedback services"},
Jeffrey Kardatzke36791f22019-07-11 11:53:22 -0700341 {kCommand, "wifi_status",
342 "/usr/bin/network_diag --wifi-internal --no-log --anonymize"},
Elly Jones03cd6d72012-06-11 13:04:28 -0400343};
344
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700345// List of log files needed to be part of the feedback report that are huge and
346// must be sent back to the client via the file descriptor using
347// LogTool::GetBigFeedbackLogs().
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700348const std::vector<Log> kBigFeedbackLogs{
Kansho Nishidadedbf832019-08-02 18:31:59 +0900349 // We need to enter init's mount namespace because android-sh accesses
350 // /run/chrome/is_arcvm and /run/state/logged-in
351
352 // TODO(kansho): Remove this version check after disabling N.
353 // This code extracts the version of Android from /etc/lsb-release and checks
354 // whether it's lower than 28 (Pie).
355 {kCommand, "arc-bugreport",
356 "if [ \"$(sed -n '/^CHROMEOS_ARC_ANDROID_SDK_VERSION=/s:.*=::p'"
357 " /etc/lsb-release)\" -lt 28 ];"
358 "then cat /run/arc/bugreport/pipe 2>/dev/null;" // older than P
359 "else /usr/bin/nsenter -t1 -m /usr/sbin/android-sh -c"
360 " /system/bin/arc-bugreport; fi",
Chris Morin853d3442019-04-01 21:35:13 -0700361 kRoot, kRoot, 10 * 1024 * 1024, LogTool::Encoding::kUtf8},
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700362};
363
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700364// Fills |dictionary| with the contents of the logs in |logs|.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700365void GetLogsInDictionary(const std::vector<Log>& logs,
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800366 base::DictionaryValue* dictionary) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700367 for (const Log& log : logs) {
Hidehiko Abe22667262019-08-15 01:32:23 +0900368 dictionary->SetKey(log.GetName(), base::Value(log.GetLogData()));
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800369 }
370}
371
372// Serializes the |dictionary| into the file with the given |fd| in a JSON
373// format.
374void SerializeLogsAsJSON(const base::DictionaryValue& dictionary,
Eric Caruso0b241882018-04-04 13:43:46 -0700375 const base::ScopedFD& fd) {
Eric Caruso96d03d32017-04-25 18:01:17 -0700376 string logs_json;
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800377 base::JSONWriter::WriteWithOptions(dictionary,
378 base::JSONWriter::OPTIONS_PRETTY_PRINT,
379 &logs_json);
Eric Caruso0b241882018-04-04 13:43:46 -0700380 base::WriteFileDescriptor(fd.get(), logs_json.c_str(), logs_json.size());
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800381}
382
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700383bool GetNamedLogFrom(const string& name,
384 const std::vector<Log>& logs,
Elly Jones533c7c42012-08-10 15:07:05 -0400385 string* result) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700386 for (const Log& log : logs) {
387 if (name == log.GetName()) {
388 *result = log.GetLogData();
Elly Jones533c7c42012-08-10 15:07:05 -0400389 return true;
390 }
391 }
392 *result = "<invalid log name>";
393 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400394}
395
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700396void GetLogsFrom(const std::vector<Log>& logs, LogTool::LogMap* map) {
397 for (const Log& log : logs)
398 (*map)[log.GetName()] = log.GetLogData();
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800399}
400
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600401void GetLsbReleaseInfo(LogTool::LogMap* map) {
402 const base::FilePath lsb_release(kLsbReleasePath);
403 brillo::KeyValueStore store;
404 if (!store.Load(lsb_release)) {
405 // /etc/lsb-release might not be present (cros deploying a new
406 // configuration or no fields set at all). Just print a debug
407 // message and continue.
408 DLOG(INFO) << "Could not load fields from " << lsb_release.value();
409 } else {
410 for (const auto& key : store.GetKeys()) {
411 std::string value;
412 store.GetString(key, &value);
413 (*map)[key] = value;
414 }
415 }
416}
417
418void GetOsReleaseInfo(LogTool::LogMap* map) {
419 brillo::OsReleaseReader reader;
420 reader.Load();
421 for (const auto& key : reader.GetKeys()) {
422 std::string value;
423 reader.GetString(key, &value);
424 (*map)["os-release " + key] = value;
425 }
426}
427
428void PopulateDictionaryValue(const LogTool::LogMap& map,
429 base::DictionaryValue* dictionary) {
430 for (const auto& kv : map) {
431 dictionary->SetString(kv.first, kv.second);
432 }
433}
434
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800435bool CompressXzBuffer(const std::vector<uint8_t>& in_buffer,
436 std::vector<uint8_t>* out_buffer) {
437 size_t out_size = lzma_stream_buffer_bound(in_buffer.size());
438 out_buffer->resize(out_size);
439 size_t out_pos = 0;
440
441 lzma_ret ret = lzma_easy_buffer_encode(
442 LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64, nullptr, in_buffer.data(),
443 in_buffer.size(), out_buffer->data(), &out_pos, out_size);
444
445 if (ret != LZMA_OK) {
446 out_buffer->clear();
447 return false;
448 }
449
450 out_buffer->resize(out_pos);
451 return true;
452}
453
454void GetPerfData(LogTool::LogMap* map) {
455 // Run perf to collect system-wide performance profile when user triggers
456 // feedback report. Perf runs at sampling frequency of ~500 hz (499 is used
457 // to avoid sampling periodic system activities), with callstack in each
458 // sample (-g).
459 std::vector<std::string> perf_args = {
460 "perf", "record", "-a", "-g", "-F", "499"
461 };
462 std::vector<uint8_t> perf_data;
463 int32_t status;
464
465 debugd::PerfTool perf_tool;
466 if (!perf_tool.GetPerfOutput(kPerfDurationSecs, perf_args, &perf_data,
467 nullptr, &status, nullptr))
468 return;
469
470 // XZ compress the profile data.
471 std::vector<uint8_t> perf_data_xz;
472 if (!CompressXzBuffer(perf_data, &perf_data_xz))
473 return;
474
475 // Base64 encode the compressed data.
476 std::string perf_data_str(reinterpret_cast<const char*>(perf_data_xz.data()),
477 perf_data_xz.size());
478 (*map)["perf-data"] =
Chinglin Yu3c8d0a22019-02-20 11:32:52 +0800479 std::string(kPerfDataDescription) +
Chris Morin790fd262019-04-03 20:29:36 -0700480 LogTool::EncodeString(std::move(perf_data_str),
481 LogTool::Encoding::kBase64);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800482}
483
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800484} // namespace
485
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700486Log::Log(Log::LogType type,
487 std::string name,
488 std::string data,
489 std::string user,
490 std::string group,
491 int64_t max_bytes,
Brian Norrisafc9f632019-05-09 14:08:28 -0700492 LogTool::Encoding encoding,
493 bool access_root_mount_ns)
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700494 : type_(type),
495 name_(name),
496 data_(data),
497 user_(user),
498 group_(group),
499 max_bytes_(max_bytes),
Brian Norrisafc9f632019-05-09 14:08:28 -0700500 encoding_(encoding),
501 access_root_mount_ns_(access_root_mount_ns) {}
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700502
503std::string Log::GetName() const {
504 return name_;
505}
506
507std::string Log::GetLogData() const {
508 // The reason this code uses a switch statement on a type enum rather than
509 // using inheritance/virtual dispatch is so that all of the Log objects can
510 // be constructed statically. Switching to heap allocated subclasses of Log
511 // makes the code that declares all of the log entries much more verbose
512 // and harder to understand.
Chris Morin790fd262019-04-03 20:29:36 -0700513 std::string output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700514 switch (type_) {
515 case kCommand:
Chris Morin790fd262019-04-03 20:29:36 -0700516 output = GetCommandLogData();
517 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700518 case kFile:
Chris Morin790fd262019-04-03 20:29:36 -0700519 output = GetFileLogData();
520 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700521 default:
522 return "<unknown log type>";
523 }
Chris Morin790fd262019-04-03 20:29:36 -0700524
525 if (output.empty())
526 return "<empty>";
527
528 return LogTool::EncodeString(std::move(output), encoding_);
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700529}
530
531// TODO(ellyjones): sandbox. crosbug.com/35122
532std::string Log::GetCommandLogData() const {
533 if (type_ != kCommand)
534 return "<log type mismatch>";
535 std::string tailed_cmdline =
536 base::StringPrintf("%s | tail -c %" PRId64, data_.c_str(), max_bytes_);
537 ProcessWithOutput p;
538 if (minijail_disabled_for_test_)
539 p.set_use_minijail(false);
540 if (!user_.empty() && !group_.empty())
541 p.SandboxAs(user_, group_);
Brian Norrisafc9f632019-05-09 14:08:28 -0700542 if (access_root_mount_ns_)
543 p.AllowAccessRootMountNamespace();
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700544 if (!p.Init())
545 return "<not available>";
546 p.AddArg(kShell);
547 p.AddStringOption("-c", tailed_cmdline);
548 if (p.Run())
549 return "<not available>";
550 std::string output;
551 p.GetOutput(&output);
Chris Morin790fd262019-04-03 20:29:36 -0700552 return output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700553}
554
555std::string Log::GetFileLogData() const {
556 if (type_ != kFile)
557 return "<log type mismatch>";
558
559 uid_t old_euid = geteuid();
560 uid_t new_euid = UidForUser(user_);
561 gid_t old_egid = getegid();
562 gid_t new_egid = GidForGroup(group_);
563
564 if (new_euid == -1 || new_egid == -1) {
565 return "<not available>";
566 }
567
568 // Make sure to set group first, since if we set user first we lose root
569 // and therefore the ability to set our effective gid to arbitrary gids.
570 if (setegid(new_egid)) {
571 PLOG(ERROR) << "Failed to set effective group id to " << new_egid;
572 return "<not available>";
573 }
574 if (seteuid(new_euid)) {
575 PLOG(ERROR) << "Failed to set effective user id to " << new_euid;
576 if (setegid(old_egid))
577 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
578 return "<not available>";
579 }
580
581 std::string contents;
582 const base::FilePath path(data_);
583 // Handle special files that don't properly report length/allow lseek.
584 if (base::FilePath("/dev").IsParent(path) ||
585 base::FilePath("/proc").IsParent(path) ||
586 base::FilePath("/sys").IsParent(path)) {
587 if (!base::ReadFileToString(path, &contents))
588 contents = "<not available>";
589 if (contents.size() > max_bytes_)
590 contents.erase(0, contents.size() - max_bytes_);
591 } else {
592 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
593 if (!file.IsValid()) {
594 contents = "<not available>";
595 } else {
596 int64_t length = file.GetLength();
597 if (length > max_bytes_) {
598 file.Seek(base::File::FROM_END, -max_bytes_);
599 length = max_bytes_;
600 }
601 std::vector<char> buf(length);
602 int read = file.ReadAtCurrentPos(buf.data(), buf.size());
603 if (read < 0) {
604 PLOG(ERROR) << "Could not read from file " << path.value();
605 } else {
606 contents = std::string(buf.begin(), buf.begin() + read);
607 }
608 }
609 }
610
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700611 // Make sure we restore our old euid/egid before returning.
612 if (seteuid(old_euid))
613 PLOG(ERROR) << "Failed to restore effective user id to " << old_euid;
614
615 if (setegid(old_egid))
616 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
617
Chris Morin790fd262019-04-03 20:29:36 -0700618 return contents;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700619}
620
621void Log::DisableMinijailForTest() {
622 minijail_disabled_for_test_ = true;
623}
624
625// static
626uid_t Log::UidForUser(const std::string& user) {
627 struct passwd entry;
628 struct passwd* result;
629 std::vector<char> buf(1024);
630 getpwnam_r(user.c_str(), &entry, &buf[0], buf.size(), &result);
631 if (!result) {
632 LOG(ERROR) << "User not found: " << user;
633 return -1;
634 }
635 return entry.pw_uid;
636}
637
638// static
639gid_t Log::GidForGroup(const std::string& group) {
640 struct group entry;
641 struct group* result;
642 std::vector<char> buf(1024);
643 getgrnam_r(group.c_str(), &entry, &buf[0], buf.size(), &result);
644 if (!result) {
645 LOG(ERROR) << "Group not found: " << group;
646 return -1;
647 }
648 return entry.gr_gid;
649}
650
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700651void LogTool::CreateConnectivityReport(bool wait_for_results) {
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700652 // Perform ConnectivityTrial to report connection state in feedback log.
Ben Chan8e9f6d02017-09-26 23:04:21 -0700653 auto shill = std::make_unique<org::chromium::flimflam::ManagerProxy>(bus_);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700654 // Give the connection trial time to test the connection and log the results
655 // before collecting the logs for feedback.
656 // TODO(silberst): Replace the simple approach of a single timeout with a more
657 // coordinated effort.
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700658 if (shill && shill->CreateConnectivityReport(nullptr) && wait_for_results)
Eric Carusocc7106c2017-04-27 14:22:42 -0700659 sleep(kConnectionTesterTimeoutSeconds);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700660}
661
Eric Carusoc93a15c2017-04-24 16:15:12 -0700662string LogTool::GetLog(const string& name) {
Elly Jones533c7c42012-08-10 15:07:05 -0400663 string result;
Ben Chancf7d6412017-08-10 22:30:09 -0700664 GetNamedLogFrom(name, kCommandLogs, &result)
665 || GetNamedLogFrom(name, kExtraLogs, &result)
666 || GetNamedLogFrom(name, kFeedbackLogs, &result);
Elly Jones533c7c42012-08-10 15:07:05 -0400667 return result;
668}
669
Eric Carusof9091f82017-04-28 14:18:59 -0700670LogTool::LogMap LogTool::GetAllLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700671 CreateConnectivityReport(false);
Elly Jones533c7c42012-08-10 15:07:05 -0400672 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700673 GetLogsFrom(kCommandLogs, &result);
674 GetLogsFrom(kExtraLogs, &result);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600675 GetLsbReleaseInfo(&result);
676 GetOsReleaseInfo(&result);
Elly Jones533c7c42012-08-10 15:07:05 -0400677 return result;
678}
679
Brian Norrisca4fc042018-04-03 00:24:26 -0700680LogTool::LogMap LogTool::GetAllDebugLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700681 CreateConnectivityReport(true);
Brian Norrisca4fc042018-04-03 00:24:26 -0700682 LogMap result;
683 GetLogsFrom(kCommandLogs, &result);
684 GetLogsFrom(kExtraLogs, &result);
685 GetLogsFrom(kBigFeedbackLogs, &result);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600686 GetLsbReleaseInfo(&result);
687 GetOsReleaseInfo(&result);
Brian Norrisca4fc042018-04-03 00:24:26 -0700688 return result;
689}
690
Eric Caruso0b241882018-04-04 13:43:46 -0700691void LogTool::GetBigFeedbackLogs(const base::ScopedFD& fd) {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700692 CreateConnectivityReport(true);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800693 LogMap map;
694 GetPerfData(&map);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800695 base::DictionaryValue dictionary;
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700696 GetLogsInDictionary(kCommandLogs, &dictionary);
697 GetLogsInDictionary(kFeedbackLogs, &dictionary);
698 GetLogsInDictionary(kBigFeedbackLogs, &dictionary);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600699 GetLsbReleaseInfo(&map);
700 GetOsReleaseInfo(&map);
701 PopulateDictionaryValue(map, &dictionary);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800702 SerializeLogsAsJSON(dictionary, fd);
703}
704
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700705void LogTool::GetJournalLog(const base::ScopedFD& fd) {
Chris Morin790fd262019-04-03 20:29:36 -0700706 Log journal(kCommand, "journal.export", "journalctl -n 10000 -o export",
707 "syslog", "syslog", 10 * 1024 * 1024, LogTool::Encoding::kBinary);
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700708 std::string output = journal.GetLogData();
Chris Morin790fd262019-04-03 20:29:36 -0700709 base::WriteFileDescriptor(fd.get(), output.data(), output.size());
710}
711
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700712// static
Chris Morin790fd262019-04-03 20:29:36 -0700713string LogTool::EncodeString(string value,
714 LogTool::Encoding source_encoding) {
715 if (source_encoding == LogTool::Encoding::kBinary)
716 return value;
717
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700718 if (source_encoding == LogTool::Encoding::kAutodetect) {
719 if (base::IsStringUTF8(value))
720 return value;
Chris Morin790fd262019-04-03 20:29:36 -0700721 source_encoding = LogTool::Encoding::kBase64;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700722 }
723
724 if (source_encoding == LogTool::Encoding::kUtf8) {
725 string output;
726 const char* src = value.data();
727 int32_t src_len = static_cast<int32_t>(value.length());
728
729 output.reserve(value.size());
730 for (int32_t char_index = 0; char_index < src_len; char_index++) {
731 uint32_t code_point;
732 if (!base::ReadUnicodeCharacter(src, src_len, &char_index, &code_point) ||
733 !base::IsValidCharacter(code_point)) {
734 // Replace invalid characters with U+FFFD REPLACEMENT CHARACTER.
735 code_point = 0xFFFD;
736 }
737 base::WriteUnicodeCharacter(code_point, &output);
738 }
739 return output;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700740 }
Chris Morin853d3442019-04-01 21:35:13 -0700741
742 base::Base64Encode(value, &value);
743 return "<base64>: " + value;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700744}
745
Ben Chana0011d82014-05-13 00:19:29 -0700746} // namespace debugd