blob: 94e0786257b29d39650da2f418d9591f6833cbdb [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;
75const std::vector<Log> kCommandLogs {
Mike Frysingerb0350992018-09-14 13:45:35 -040076 // We need to enter init's mount namespace because it has /home/chronos
77 // mounted which is where the consent knob lives. We don't have that mount
78 // in our own mount namespace (by design). https://crbug.com/884249
Chris Morin853d3442019-04-01 21:35:13 -070079 {kCommand, "CLIENT_ID", "/usr/bin/nsenter -t1 -m /usr/bin/metrics_client -i",
80 kRoot, kDebugfsGroup},
81 {kCommand, "LOGDATE", "/bin/date"},
Yusuke Sato27a31672019-04-29 15:26:37 -070082 // We need to enter init's mount namespace to access /home/root. Also, we use
83 // neither ARC container's mount namespace (with android-sh) nor
84 // /opt/google/containers/android/rootfs/android-data/ so that we can get
85 // results even when the container is down.
86 {kCommand, "android_app_storage", "/usr/bin/nsenter -t1 -m "
87 "/bin/sh -c \"/usr/bin/du -h /home/root/*/android-data/data/\"",
88 kRoot, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -070089 {kFile, "atrus_logs", "/var/log/atrus.log"},
90 {kFile, "authpolicy", "/var/log/authpolicy.log"},
Brian Norrisafc9f632019-05-09 14:08:28 -070091 {kCommand, "bootstat_summary", "/usr/bin/bootstat_summary",
92 SandboxedProcess::kDefaultUser, SandboxedProcess::kDefaultGroup,
93 Log::kDefaultMaxBytes, LogTool::Encoding::kAutodetect, true},
Craig Hesling5c384b52019-04-20 15:18:06 -070094 {kFile, "bio_crypto_init.LATEST",
95 "/var/log/bio_crypto_init/bio_crypto_init.LATEST"},
96 {kFile, "bio_crypto_init.PREVIOUS",
97 "/var/log/bio_crypto_init/bio_crypto_init.PREVIOUS"},
Chris Morin853d3442019-04-01 21:35:13 -070098 {kFile, "biod.LATEST", "/var/log/biod/biod.LATEST"},
99 {kFile, "biod.PREVIOUS", "/var/log/biod/biod.PREVIOUS"},
Craig Hesling4c3891e2019-04-20 12:53:54 -0700100 {kFile, "bio_fw_updater.LATEST", "/var/log/biod/bio_fw_updater.LATEST"},
101 {kFile, "bio_fw_updater.PREVIOUS", "/var/log/biod/bio_fw_updater.PREVIOUS"},
Chris Morin853d3442019-04-01 21:35:13 -0700102 {kFile, "bios_info", "/var/log/bios_info.txt"},
103 {kCommand, "bios_log", "cat /sys/firmware/log "
104 "/proc/device-tree/chosen/ap-console-buffer 2>/dev/null"},
105 {kFile, "bios_times", "/var/log/bios_times.txt"},
106 {kCommand, "board-specific",
107 "/usr/share/userfeedback/scripts/get_board_specific_info"},
108 {kFile, "buddyinfo", "/proc/buddyinfo"},
109 {kCommand, "cbi_info", "/usr/share/userfeedback/scripts/cbi_info", kRoot,
110 kRoot},
111 {kFile, "cheets_log", "/var/log/arc.log"},
112 {kFile, "clobber.log", "/var/log/clobber.log"},
113 {kFile, "clobber-state.log", "/var/log/clobber-state.log"},
114 {kFile, "chrome_system_log", "/var/log/chrome/chrome"},
115 {kFile, "chrome_system_log.PREVIOUS", "/var/log/chrome/chrome.PREVIOUS"},
Mike Frysinger32cdf3e2017-08-14 18:17:06 -0400116 // There might be more than one record, so grab them all.
117 // Plus, for <linux-3.19, it's named "console-ramoops", but for newer
118 // versions, it's named "console-ramoops-#".
Chris Morin853d3442019-04-01 21:35:13 -0700119 {kCommand, "console-ramoops",
120 "cat /sys/fs/pstore/console-ramoops* 2>/dev/null"},
121 {kFile, "cpuinfo", "/proc/cpuinfo"},
122 {kFile, "cr50_version", "/var/cache/cr50-version"},
123 {kFile, "cros_ec.log", "/var/log/cros_ec.log"},
124 {kFile, "cros_ec.previous", "/var/log/cros_ec.previous"},
125 {kFile, "cros_ec_panicinfo", "/sys/kernel/debug/cros_ec/panicinfo",
126 SandboxedProcess::kDefaultUser, kDebugfsGroup},
127 {kFile, "cros_ec_pdinfo", "/sys/kernel/debug/cros_ec/pdinfo",
128 SandboxedProcess::kDefaultUser, kDebugfsGroup},
129 {kFile, "cros_fp.previous", "/var/log/cros_fp.previous"},
130 {kFile, "cros_fp.log", "/var/log/cros_fp.log"},
131 {kCommand, "dmesg", "/bin/dmesg"},
132 {kFile, "ec_info", "/var/log/ec_info.txt"},
Miguel Casas8d4e54f2018-03-29 09:42:52 -0400133 // The sed command replaces the EDID serial number (4 bytes at position 12)
134 // with zeroes. See https://en.wikipedia.org/wiki/EDID#EDID_1.4_data_format.
Chris Morin853d3442019-04-01 21:35:13 -0700135 {kCommand, "edid-decode",
136 "for f in /sys/class/drm/card0-*/edid; do "
137 "echo \"----------- ${f}\"; "
138 "sed -E 's/^(.{11}).{4}/\\1\\x0\\x0\\x0\\x0/' \"${f}\" | "
139 // edid-decode's stderr output is redundant, so silence it.
140 "edid-decode 2>/dev/null; "
141 "done"},
142 {kFile, "eventlog", "/var/log/eventlog.txt"},
143 {kFile, "exynos_gem_objects", "/sys/kernel/debug/dri/0/exynos_gem_objects",
144 SandboxedProcess::kDefaultUser, kDebugfsGroup},
145 {kCommand, "font_info", "/usr/share/userfeedback/scripts/font_info"},
146 {kFile, "fwupd_state", "/var/lib/fwupd/state.json"},
147 {kCommand, "sensor_info", "/usr/share/userfeedback/scripts/sensor_info"},
148 {kFile, "hammerd", "/var/log/hammerd.log"},
149 {kCommand, "hardware_class", "/usr/bin/crossystem hwid"},
Yong Hongee9c5172019-04-18 16:31:26 +0800150 {kCommand, "hardware_verification_report",
151 "/usr/bin/hardware_verifier --output_format=text"},
Chris Morin853d3442019-04-01 21:35:13 -0700152 {kCommand, "hostname", "/bin/hostname"},
153 {kFile, "i915_gem_gtt", "/sys/kernel/debug/dri/0/i915_gem_gtt",
154 SandboxedProcess::kDefaultUser, kDebugfsGroup},
155 {kFile, "i915_gem_objects", "/sys/kernel/debug/dri/0/i915_gem_objects",
156 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700157 {kCommand, "i915_error_state",
158 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2>/dev/null",
159 SandboxedProcess::kDefaultUser, kDebugfsGroup, Log::kDefaultMaxBytes,
Chris Morin790fd262019-04-03 20:29:36 -0700160 LogTool::Encoding::kBase64},
Chris Morin853d3442019-04-01 21:35:13 -0700161 {kCommand, "ifconfig", "/bin/ifconfig -a"},
162 {kFile, "input_devices", "/proc/bus/input/devices"},
Eric Carusob1820c02017-08-24 15:39:56 -0700163 // Hardware capabilities of the wiphy device.
Chris Morin853d3442019-04-01 21:35:13 -0700164 {kCommand, "iw_list", "/usr/sbin/iw list"},
Eric Carusoa879fd92017-10-11 12:57:10 -0700165#if USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700166 {kCommand, "iwlmvm_module_params", CMD_KERNEL_MODULE_PARAMS(iwlmvm)},
167 {kCommand, "iwlwifi_module_params", CMD_KERNEL_MODULE_PARAMS(iwlwifi)},
Eric Carusoa879fd92017-10-11 12:57:10 -0700168#endif // USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700169 {kCommand, "kernel-crashes",
170 "cat /var/spool/crash/kernel.*.kcrash 2>/dev/null"},
Chris Morin853d3442019-04-01 21:35:13 -0700171 {kCommand, "lsmod", "lsmod"},
172 {kCommand, "lspci", "/usr/sbin/lspci"},
173 {kCommand, "lsusb", "lsusb && lsusb -t"},
174 {kFile, "mali_memory", "/sys/class/misc/mali0/device/memory"},
175 {kFile, "memd.parameters", "/var/log/memd/memd.parameters"},
176 {kCommand, "memd clips", "cat /var/log/memd/memd.clip* 2>/dev/null"},
177 {kFile, "meminfo", "/proc/meminfo"},
178 {kCommand, "memory_spd_info",
179 // mosys may use 'i2c-dev', which may not be loaded yet.
180 "modprobe i2c-dev 2>/dev/null && mosys -l memory spd print all 2>/dev/null",
181 kRoot, kDebugfsGroup},
Simon Quecb63b9c2017-06-19 14:53:31 -0400182 // The sed command finds the EDID blob (starting the line after "value:") and
183 // replaces the serial number with all zeroes.
184 //
185 // The EDID is printed as a hex dump over several lines, each line containing
186 // the contents of 16 bytes. The first 16 bytes are broken down as follows:
187 // uint64_t fixed_pattern; // Always 00 FF FF FF FF FF FF 00.
188 // uint16_t manufacturer_id; // Manufacturer ID, encoded as PNP IDs.
189 // uint16_t product_code; // Manufacturer product code, little-endian.
190 // uint32_t serial_number; // Serial number, little-endian.
191 // Source: https://en.wikipedia.org/wiki/EDID#EDID_1.3_data_format
192 //
193 // The subsequent substitution command looks for the fixed pattern followed by
194 // two 32-bit fields (manufacturer + product, serial number). It replaces the
195 // latter field with 8 bytes of zeroes.
196 //
197 // TODO(crbug.com/731133): Remove the sed command once modetest itself can
198 // remove serial numbers.
Chris Morin853d3442019-04-01 21:35:13 -0700199 {kCommand, "modetest",
200 "(modetest; modetest -M evdi; modetest -M udl) | "
201 "sed -E '/EDID/ {:a;n;/value:/!ba;n;"
202 "s/(00f{12}00)([0-9a-f]{8})([0-9a-f]{8})/\\1\\200000000/}'",
203 kRoot, kRoot},
204 {kFile, "mount-encrypted", "/var/log/mount-encrypted.log"},
205 {kFile, "mountinfo", "/proc/1/mountinfo"},
206 {kCommand, "netlog",
207 "/usr/share/userfeedback/scripts/getmsgs /var/log/net.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700208 {kFile, "nvmap_iovmm", "/sys/kernel/debug/nvmap/iovmm/allocations",
209 SandboxedProcess::kDefaultUser, kDebugfsGroup},
210 {kCommand, "oemdata", "/usr/share/cros/oemdata.sh", kRoot, kRoot},
211 {kFile, "pagetypeinfo", "/proc/pagetypeinfo"},
212 {kCommand, "platform_info",
213 // mosys may use 'i2c-dev', which may not be loaded yet.
214 "modprobe i2c-dev 2>/dev/null && "
215 "for param in "
216 "vendor "
217 "name "
218 "version "
219 "family "
220 "model "
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700221
Chris Morin853d3442019-04-01 21:35:13 -0700222 "sku "
223 "customization "
224 "; do "
225 "mosys -l platform \"${param}\" 2>/dev/null; "
226 "done",
227 kRoot, kDebugfsGroup},
228 {kCommand, "power_supply_info", "/usr/bin/power_supply_info"},
229 {kCommand, "power_supply_sysfs", "/usr/bin/print_sysfs_power_supply_data"},
230 {kFile, "powerd.LATEST", "/var/log/power_manager/powerd.LATEST"},
231 {kFile, "powerd.PREVIOUS", "/var/log/power_manager/powerd.PREVIOUS"},
232 {kFile, "powerd.out", "/var/log/powerd.out"},
233 {kFile, "powerwash_count", "/var/log/powerwash_count"},
Brian Norris4cde3d12019-04-16 10:10:34 -0700234 {kCommand, "ps", "/bin/ps auxZ"},
yusukes34171ba2017-04-27 15:46:01 -0700235 // /proc/slabinfo is owned by root and has 0400 permission.
Chris Morin853d3442019-04-01 21:35:13 -0700236 {kFile, "slabinfo", "/proc/slabinfo", kRoot, kRoot},
237 {kFile, "storage_info", "/var/log/storage_info.txt"},
238 {kCommand, "swap_info", "/usr/share/cros/init/swap.sh status 2>/dev/null",
239 SandboxedProcess::kDefaultUser, kDebugfsGroup},
240 {kCommand, "syslog",
241 "/usr/share/userfeedback/scripts/getmsgs /var/log/messages"},
242 {kCommand, "system_log_stats",
243 "echo 'BLOCK_SIZE=1024'; "
244 "find /var/log/ -type f -exec du --block-size=1024 {} + | sort -n -r",
245 kRoot, kRoot},
246 {kCommand, "threads", "/bin/ps -T axo pid,ppid,spid,pcpu,ni,stat,time,comm"},
247 {kFile, "tlsdate", "/var/log/tlsdate.log"},
248 {kCommand, "top thread", "/usr/bin/top -Hb -n 1 | head -n 40"},
249 {kCommand, "top memory", "/usr/bin/top -o \"+%MEM\" -bn 1 | head -n 57"},
250 {kCommand, "touch_fw_version",
251 "grep -E"
252 " -e 'synaptics: Touchpad model'"
253 " -e 'chromeos-[a-z]*-touch-[a-z]*-update'"
254 " /var/log/messages | tail -n 20"},
255 {kCommand, "tpm-firmware-updater", "/usr/share/userfeedback/scripts/getmsgs "
256 "/var/log/tpm-firmware-updater.log"},
Mattias Nissler887dce22017-07-03 14:44:35 +0200257 // TODO(jorgelo,mnissler): Don't run this as root.
258 // On TPM 1.2 devices this will likely require adding a new user to the 'tss'
259 // group.
260 // On TPM 2.0 devices 'get_version_info' uses D-Bus and therefore can run as
261 // any user.
Chris Morin853d3442019-04-01 21:35:13 -0700262 {kCommand, "tpm_version", "/usr/sbin/tpm-manager get_version_info", kRoot,
263 kRoot},
264 {kCommand, "atmel_ts_refs",
265 "/opt/google/touch/scripts/atmel_tools.sh ts r", kRoot, kRoot},
266 {kCommand, "atmel_tp_refs",
267 "/opt/google/touch/scripts/atmel_tools.sh tp r", kRoot, kRoot},
268 {kCommand, "atmel_ts_deltas",
269 "/opt/google/touch/scripts/atmel_tools.sh ts d", kRoot, kRoot},
270 {kCommand, "atmel_tp_deltas",
271 "/opt/google/touch/scripts/atmel_tools.sh tp d", kRoot, kRoot},
272 {kFile, "stateful_trim_state", "/var/lib/trim/stateful_trim_state"},
273 {kFile, "stateful_trim_data", "/var/lib/trim/stateful_trim_data"},
274 {kFile, "ui_log", "/var/log/ui/ui.LATEST"},
275 {kCommand, "uname", "/bin/uname -a"},
276 {kCommand, "update_engine.log",
277 "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
278 " s.^./var/log/update_engine/.)"},
Chris Morinca152712019-05-03 13:17:28 -0700279 {kFile, "upstart", "/var/log/upstart.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700280 {kCommand, "uptime", "/usr/bin/cut -d' ' -f1 /proc/uptime"},
281 {kFile, "verified boot", "/var/log/debug_vboot_noisy.log"},
282 {kFile, "vmlog.1.LATEST", "/var/log/vmlog/vmlog.1.LATEST"},
283 {kFile, "vmlog.1.PREVIOUS", "/var/log/vmlog/vmlog.1.PREVIOUS"},
284 {kFile, "vmlog.LATEST", "/var/log/vmlog/vmlog.LATEST"},
285 {kFile, "vmlog.PREVIOUS", "/var/log/vmlog/vmlog.PREVIOUS"},
286 {kFile, "vmstat", "/proc/vmstat"},
287 {kFile, "vpd_2.0", "/var/log/vpd_2.0.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700288 {kFile, "zram compressed data size", "/sys/block/zram0/compr_data_size"},
289 {kFile, "zram original data size", "/sys/block/zram0/orig_data_size"},
290 {kFile, "zram total memory used", "/sys/block/zram0/mem_used_total"},
291 {kFile, "zram total reads", "/sys/block/zram0/num_reads"},
292 {kFile, "zram total writes", "/sys/block/zram0/num_writes"},
293 {kCommand, "zram new stats names",
294 "echo orig_size compr_size used_total limit used_max zero_pages migrated"},
295 {kFile, "zram new stats values", "/sys/block/zram0/mm_stat"},
296 {kFile, "cros_tp version", "/sys/class/chromeos/cros_tp/version"},
297 {kCommand, "cros_tp console", "/usr/sbin/ectool --name=cros_tp console",
298 kRoot, kRoot},
299 {kCommand, "cros_tp frame", "/usr/sbin/ectool --name=cros_tp tpframeget",
300 kRoot, kRoot},
301 {kCommand, "crostini", "/usr/bin/cicerone_client --get_info"},
Elly Jones03cd6d72012-06-11 13:04:28 -0400302 // Stuff pulled out of the original list. These need access to the running X
303 // session, which we'd rather not give to debugd, or return info specific to
304 // the current session (in the setsid(2) sense), which is not useful for
305 // debugd
Chris Morin853d3442019-04-01 21:35:13 -0700306 // {kCommand, "env", "set"},
307 // {kCommand, "setxkbmap", "/usr/bin/setxkbmap -print -query"},
308 // {kCommand, "xrandr", "/usr/bin/xrandr --verbose}
Elly Jones533c7c42012-08-10 15:07:05 -0400309};
310
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700311// netstat and logcat should appear in chrome://system but not in feedback
312// reports. Open sockets may have privacy implications, and logcat is
313// already incorporated via arc-bugreport.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700314const std::vector<Log> kExtraLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800315#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700316 {kCommand, "mm-status", "/usr/bin/modem status"},
Ben Chan36e42282014-02-12 22:32:34 -0800317#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700318 {kCommand, "network-devices", "/usr/bin/connectivity show devices"},
319 {kCommand, "network-services", "/usr/bin/connectivity show services"},
Brian Norris2b290502019-04-23 17:17:10 -0700320 {kCommand, "wifi_status_show_macs",
321 "/usr/bin/network_diag --wifi-internal --no-log --show-macs"},
Chris Morin253a2b02019-04-12 16:04:25 -0700322 // --processes requires root.
323 {kCommand, "netstat",
324 "/sbin/ss --all --query inet --numeric --processes", kRoot, kRoot},
325 {kCommand, "logcat", "/usr/sbin/android-sh -c '/system/bin/logcat -d'",
326 kRoot, kRoot, Log::kDefaultMaxBytes, LogTool::Encoding::kUtf8},
Elly Jones533c7c42012-08-10 15:07:05 -0400327};
328
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700329const std::vector<Log> kFeedbackLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800330#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700331 {kCommand, "mm-status", "/usr/bin/modem status-feedback"},
Ben Chan36e42282014-02-12 22:32:34 -0800332#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700333 {kCommand, "network-devices",
334 "/usr/bin/connectivity show-feedback devices"},
335 {kCommand, "network-services",
336 "/usr/bin/connectivity show-feedback services"},
Brian Norris2b290502019-04-23 17:17:10 -0700337 {kCommand, "wifi_status", "/usr/bin/network_diag --wifi-internal --no-log"},
Elly Jones03cd6d72012-06-11 13:04:28 -0400338};
339
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700340// List of log files needed to be part of the feedback report that are huge and
341// must be sent back to the client via the file descriptor using
342// LogTool::GetBigFeedbackLogs().
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700343const std::vector<Log> kBigFeedbackLogs{
Fletcher Woodruffc9f36a72019-03-20 08:05:15 -0600344 // ARC bugreport permissions are weird. Since we're just running cat,
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700345 // this shouldn't cause any issues.
Chris Morin853d3442019-04-01 21:35:13 -0700346 {kCommand, "arc-bugreport", "cat /run/arc/bugreport/pipe 2>/dev/null",
347 kRoot, kRoot, 10 * 1024 * 1024, LogTool::Encoding::kUtf8},
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700348};
349
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700350// List of log files that must directly be collected by Chrome. This is because
351// debugd is running under a VFS namespace and does not have access to later
352// cryptohome mounts.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700353using UserLog = std::pair<std::string, std::string>;
354const std::vector<UserLog> kUserLogs = {
Chris Morin853d3442019-04-01 21:35:13 -0700355 {"chrome_user_log", "log/chrome"},
356 {"libassistant_user_log", "log/libassistant.log"},
357 {"login-times", "login-times"},
358 {"logout-times", "logout-times"},
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700359};
360
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800361// Fills |dictionary| with the anonymized contents of the logs in |logs|.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700362void GetLogsInDictionary(const std::vector<Log>& logs,
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800363 AnonymizerTool* anonymizer,
364 base::DictionaryValue* dictionary) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700365 for (const Log& log : logs) {
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800366 dictionary->SetStringWithoutPathExpansion(
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700367 log.GetName(), anonymizer->Anonymize(log.GetLogData()));
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800368 }
369}
370
371// Serializes the |dictionary| into the file with the given |fd| in a JSON
372// format.
373void SerializeLogsAsJSON(const base::DictionaryValue& dictionary,
Eric Caruso0b241882018-04-04 13:43:46 -0700374 const base::ScopedFD& fd) {
Eric Caruso96d03d32017-04-25 18:01:17 -0700375 string logs_json;
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800376 base::JSONWriter::WriteWithOptions(dictionary,
377 base::JSONWriter::OPTIONS_PRETTY_PRINT,
378 &logs_json);
Eric Caruso0b241882018-04-04 13:43:46 -0700379 base::WriteFileDescriptor(fd.get(), logs_json.c_str(), logs_json.size());
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800380}
381
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700382bool GetNamedLogFrom(const string& name,
383 const std::vector<Log>& logs,
Elly Jones533c7c42012-08-10 15:07:05 -0400384 string* result) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700385 for (const Log& log : logs) {
386 if (name == log.GetName()) {
387 *result = log.GetLogData();
Elly Jones533c7c42012-08-10 15:07:05 -0400388 return true;
389 }
390 }
391 *result = "<invalid log name>";
392 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400393}
394
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700395void GetLogsFrom(const std::vector<Log>& logs, LogTool::LogMap* map) {
396 for (const Log& log : logs)
397 (*map)[log.GetName()] = log.GetLogData();
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800398}
399
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600400void GetLsbReleaseInfo(LogTool::LogMap* map) {
401 const base::FilePath lsb_release(kLsbReleasePath);
402 brillo::KeyValueStore store;
403 if (!store.Load(lsb_release)) {
404 // /etc/lsb-release might not be present (cros deploying a new
405 // configuration or no fields set at all). Just print a debug
406 // message and continue.
407 DLOG(INFO) << "Could not load fields from " << lsb_release.value();
408 } else {
409 for (const auto& key : store.GetKeys()) {
410 std::string value;
411 store.GetString(key, &value);
412 (*map)[key] = value;
413 }
414 }
415}
416
417void GetOsReleaseInfo(LogTool::LogMap* map) {
418 brillo::OsReleaseReader reader;
419 reader.Load();
420 for (const auto& key : reader.GetKeys()) {
421 std::string value;
422 reader.GetString(key, &value);
423 (*map)["os-release " + key] = value;
424 }
425}
426
427void PopulateDictionaryValue(const LogTool::LogMap& map,
428 base::DictionaryValue* dictionary) {
429 for (const auto& kv : map) {
430 dictionary->SetString(kv.first, kv.second);
431 }
432}
433
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800434bool CompressXzBuffer(const std::vector<uint8_t>& in_buffer,
435 std::vector<uint8_t>* out_buffer) {
436 size_t out_size = lzma_stream_buffer_bound(in_buffer.size());
437 out_buffer->resize(out_size);
438 size_t out_pos = 0;
439
440 lzma_ret ret = lzma_easy_buffer_encode(
441 LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64, nullptr, in_buffer.data(),
442 in_buffer.size(), out_buffer->data(), &out_pos, out_size);
443
444 if (ret != LZMA_OK) {
445 out_buffer->clear();
446 return false;
447 }
448
449 out_buffer->resize(out_pos);
450 return true;
451}
452
453void GetPerfData(LogTool::LogMap* map) {
454 // Run perf to collect system-wide performance profile when user triggers
455 // feedback report. Perf runs at sampling frequency of ~500 hz (499 is used
456 // to avoid sampling periodic system activities), with callstack in each
457 // sample (-g).
458 std::vector<std::string> perf_args = {
459 "perf", "record", "-a", "-g", "-F", "499"
460 };
461 std::vector<uint8_t> perf_data;
462 int32_t status;
463
464 debugd::PerfTool perf_tool;
465 if (!perf_tool.GetPerfOutput(kPerfDurationSecs, perf_args, &perf_data,
466 nullptr, &status, nullptr))
467 return;
468
469 // XZ compress the profile data.
470 std::vector<uint8_t> perf_data_xz;
471 if (!CompressXzBuffer(perf_data, &perf_data_xz))
472 return;
473
474 // Base64 encode the compressed data.
475 std::string perf_data_str(reinterpret_cast<const char*>(perf_data_xz.data()),
476 perf_data_xz.size());
477 (*map)["perf-data"] =
Chinglin Yu3c8d0a22019-02-20 11:32:52 +0800478 std::string(kPerfDataDescription) +
Chris Morin790fd262019-04-03 20:29:36 -0700479 LogTool::EncodeString(std::move(perf_data_str),
480 LogTool::Encoding::kBase64);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800481}
482
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800483} // namespace
484
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700485Log::Log(Log::LogType type,
486 std::string name,
487 std::string data,
488 std::string user,
489 std::string group,
490 int64_t max_bytes,
Brian Norrisafc9f632019-05-09 14:08:28 -0700491 LogTool::Encoding encoding,
492 bool access_root_mount_ns)
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700493 : type_(type),
494 name_(name),
495 data_(data),
496 user_(user),
497 group_(group),
498 max_bytes_(max_bytes),
Brian Norrisafc9f632019-05-09 14:08:28 -0700499 encoding_(encoding),
500 access_root_mount_ns_(access_root_mount_ns) {}
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700501
502std::string Log::GetName() const {
503 return name_;
504}
505
506std::string Log::GetLogData() const {
507 // The reason this code uses a switch statement on a type enum rather than
508 // using inheritance/virtual dispatch is so that all of the Log objects can
509 // be constructed statically. Switching to heap allocated subclasses of Log
510 // makes the code that declares all of the log entries much more verbose
511 // and harder to understand.
Chris Morin790fd262019-04-03 20:29:36 -0700512 std::string output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700513 switch (type_) {
514 case kCommand:
Chris Morin790fd262019-04-03 20:29:36 -0700515 output = GetCommandLogData();
516 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700517 case kFile:
Chris Morin790fd262019-04-03 20:29:36 -0700518 output = GetFileLogData();
519 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700520 default:
521 return "<unknown log type>";
522 }
Chris Morin790fd262019-04-03 20:29:36 -0700523
524 if (output.empty())
525 return "<empty>";
526
527 return LogTool::EncodeString(std::move(output), encoding_);
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700528}
529
530// TODO(ellyjones): sandbox. crosbug.com/35122
531std::string Log::GetCommandLogData() const {
532 if (type_ != kCommand)
533 return "<log type mismatch>";
534 std::string tailed_cmdline =
535 base::StringPrintf("%s | tail -c %" PRId64, data_.c_str(), max_bytes_);
536 ProcessWithOutput p;
537 if (minijail_disabled_for_test_)
538 p.set_use_minijail(false);
539 if (!user_.empty() && !group_.empty())
540 p.SandboxAs(user_, group_);
Brian Norrisafc9f632019-05-09 14:08:28 -0700541 if (access_root_mount_ns_)
542 p.AllowAccessRootMountNamespace();
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700543 if (!p.Init())
544 return "<not available>";
545 p.AddArg(kShell);
546 p.AddStringOption("-c", tailed_cmdline);
547 if (p.Run())
548 return "<not available>";
549 std::string output;
550 p.GetOutput(&output);
Chris Morin790fd262019-04-03 20:29:36 -0700551 return output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700552}
553
554std::string Log::GetFileLogData() const {
555 if (type_ != kFile)
556 return "<log type mismatch>";
557
558 uid_t old_euid = geteuid();
559 uid_t new_euid = UidForUser(user_);
560 gid_t old_egid = getegid();
561 gid_t new_egid = GidForGroup(group_);
562
563 if (new_euid == -1 || new_egid == -1) {
564 return "<not available>";
565 }
566
567 // Make sure to set group first, since if we set user first we lose root
568 // and therefore the ability to set our effective gid to arbitrary gids.
569 if (setegid(new_egid)) {
570 PLOG(ERROR) << "Failed to set effective group id to " << new_egid;
571 return "<not available>";
572 }
573 if (seteuid(new_euid)) {
574 PLOG(ERROR) << "Failed to set effective user id to " << new_euid;
575 if (setegid(old_egid))
576 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
577 return "<not available>";
578 }
579
580 std::string contents;
581 const base::FilePath path(data_);
582 // Handle special files that don't properly report length/allow lseek.
583 if (base::FilePath("/dev").IsParent(path) ||
584 base::FilePath("/proc").IsParent(path) ||
585 base::FilePath("/sys").IsParent(path)) {
586 if (!base::ReadFileToString(path, &contents))
587 contents = "<not available>";
588 if (contents.size() > max_bytes_)
589 contents.erase(0, contents.size() - max_bytes_);
590 } else {
591 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
592 if (!file.IsValid()) {
593 contents = "<not available>";
594 } else {
595 int64_t length = file.GetLength();
596 if (length > max_bytes_) {
597 file.Seek(base::File::FROM_END, -max_bytes_);
598 length = max_bytes_;
599 }
600 std::vector<char> buf(length);
601 int read = file.ReadAtCurrentPos(buf.data(), buf.size());
602 if (read < 0) {
603 PLOG(ERROR) << "Could not read from file " << path.value();
604 } else {
605 contents = std::string(buf.begin(), buf.begin() + read);
606 }
607 }
608 }
609
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700610 // Make sure we restore our old euid/egid before returning.
611 if (seteuid(old_euid))
612 PLOG(ERROR) << "Failed to restore effective user id to " << old_euid;
613
614 if (setegid(old_egid))
615 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
616
Chris Morin790fd262019-04-03 20:29:36 -0700617 return contents;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700618}
619
620void Log::DisableMinijailForTest() {
621 minijail_disabled_for_test_ = true;
622}
623
624// static
625uid_t Log::UidForUser(const std::string& user) {
626 struct passwd entry;
627 struct passwd* result;
628 std::vector<char> buf(1024);
629 getpwnam_r(user.c_str(), &entry, &buf[0], buf.size(), &result);
630 if (!result) {
631 LOG(ERROR) << "User not found: " << user;
632 return -1;
633 }
634 return entry.pw_uid;
635}
636
637// static
638gid_t Log::GidForGroup(const std::string& group) {
639 struct group entry;
640 struct group* result;
641 std::vector<char> buf(1024);
642 getgrnam_r(group.c_str(), &entry, &buf[0], buf.size(), &result);
643 if (!result) {
644 LOG(ERROR) << "Group not found: " << group;
645 return -1;
646 }
647 return entry.gr_gid;
648}
649
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700650void LogTool::CreateConnectivityReport(bool wait_for_results) {
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700651 // Perform ConnectivityTrial to report connection state in feedback log.
Ben Chan8e9f6d02017-09-26 23:04:21 -0700652 auto shill = std::make_unique<org::chromium::flimflam::ManagerProxy>(bus_);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700653 // Give the connection trial time to test the connection and log the results
654 // before collecting the logs for feedback.
655 // TODO(silberst): Replace the simple approach of a single timeout with a more
656 // coordinated effort.
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700657 if (shill && shill->CreateConnectivityReport(nullptr) && wait_for_results)
Eric Carusocc7106c2017-04-27 14:22:42 -0700658 sleep(kConnectionTesterTimeoutSeconds);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700659}
660
Eric Carusoc93a15c2017-04-24 16:15:12 -0700661string LogTool::GetLog(const string& name) {
Elly Jones533c7c42012-08-10 15:07:05 -0400662 string result;
Ben Chancf7d6412017-08-10 22:30:09 -0700663 GetNamedLogFrom(name, kCommandLogs, &result)
664 || GetNamedLogFrom(name, kExtraLogs, &result)
665 || GetNamedLogFrom(name, kFeedbackLogs, &result);
Elly Jones533c7c42012-08-10 15:07:05 -0400666 return result;
667}
668
Eric Carusof9091f82017-04-28 14:18:59 -0700669LogTool::LogMap LogTool::GetAllLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700670 CreateConnectivityReport(false);
Elly Jones533c7c42012-08-10 15:07:05 -0400671 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700672 GetLogsFrom(kCommandLogs, &result);
673 GetLogsFrom(kExtraLogs, &result);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600674 GetLsbReleaseInfo(&result);
675 GetOsReleaseInfo(&result);
Elly Jones533c7c42012-08-10 15:07:05 -0400676 return result;
677}
678
Brian Norrisca4fc042018-04-03 00:24:26 -0700679LogTool::LogMap LogTool::GetAllDebugLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700680 CreateConnectivityReport(true);
Brian Norrisca4fc042018-04-03 00:24:26 -0700681 LogMap result;
682 GetLogsFrom(kCommandLogs, &result);
683 GetLogsFrom(kExtraLogs, &result);
684 GetLogsFrom(kBigFeedbackLogs, &result);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600685 GetLsbReleaseInfo(&result);
686 GetOsReleaseInfo(&result);
Brian Norrisca4fc042018-04-03 00:24:26 -0700687 return result;
688}
689
Eric Caruso0b241882018-04-04 13:43:46 -0700690void LogTool::GetBigFeedbackLogs(const base::ScopedFD& fd) {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700691 CreateConnectivityReport(true);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800692 LogMap map;
693 GetPerfData(&map);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800694 base::DictionaryValue dictionary;
Ben Chancf7d6412017-08-10 22:30:09 -0700695 GetLogsInDictionary(kCommandLogs, &anonymizer_, &dictionary);
696 GetLogsInDictionary(kFeedbackLogs, &anonymizer_, &dictionary);
697 GetLogsInDictionary(kBigFeedbackLogs, &anonymizer_, &dictionary);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600698 GetLsbReleaseInfo(&map);
699 GetOsReleaseInfo(&map);
700 PopulateDictionaryValue(map, &dictionary);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800701 SerializeLogsAsJSON(dictionary, fd);
702}
703
Chris Morin790fd262019-04-03 20:29:36 -0700704void LogTool::GetJournalLog(bool scrub, const base::ScopedFD& fd) {
705 Log journal(kCommand, "journal.export", "journalctl -n 10000 -o export",
706 "syslog", "syslog", 10 * 1024 * 1024, LogTool::Encoding::kBinary);
707 std::string output = scrub ? anonymizer_.Anonymize(journal.GetLogData())
708 : journal.GetLogData();
709 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
Darin Petkovce9b3a12013-01-10 16:38:54 +0100746void LogTool::AnonymizeLogMap(LogMap* log_map) {
Ben Chane2fa3572017-02-08 22:46:18 -0800747 for (auto& entry : *log_map)
748 entry.second = anonymizer_.Anonymize(entry.second);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100749}
750
Ben Chana0011d82014-05-13 00:19:29 -0700751} // namespace debugd