blob: 921efa67991dcd49972cc794103f69e8c3e7ba64 [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"},
91 {kCommand, "bootstat_summary", "/usr/bin/bootstat_summary"},
Craig Hesling5c384b52019-04-20 15:18:06 -070092 {kFile, "bio_crypto_init.LATEST",
93 "/var/log/bio_crypto_init/bio_crypto_init.LATEST"},
94 {kFile, "bio_crypto_init.PREVIOUS",
95 "/var/log/bio_crypto_init/bio_crypto_init.PREVIOUS"},
Chris Morin853d3442019-04-01 21:35:13 -070096 {kFile, "biod.LATEST", "/var/log/biod/biod.LATEST"},
97 {kFile, "biod.PREVIOUS", "/var/log/biod/biod.PREVIOUS"},
Craig Hesling4c3891e2019-04-20 12:53:54 -070098 {kFile, "bio_fw_updater.LATEST", "/var/log/biod/bio_fw_updater.LATEST"},
99 {kFile, "bio_fw_updater.PREVIOUS", "/var/log/biod/bio_fw_updater.PREVIOUS"},
Chris Morin853d3442019-04-01 21:35:13 -0700100 {kFile, "bios_info", "/var/log/bios_info.txt"},
101 {kCommand, "bios_log", "cat /sys/firmware/log "
102 "/proc/device-tree/chosen/ap-console-buffer 2>/dev/null"},
103 {kFile, "bios_times", "/var/log/bios_times.txt"},
104 {kCommand, "board-specific",
105 "/usr/share/userfeedback/scripts/get_board_specific_info"},
106 {kFile, "buddyinfo", "/proc/buddyinfo"},
107 {kCommand, "cbi_info", "/usr/share/userfeedback/scripts/cbi_info", kRoot,
108 kRoot},
109 {kFile, "cheets_log", "/var/log/arc.log"},
110 {kFile, "clobber.log", "/var/log/clobber.log"},
111 {kFile, "clobber-state.log", "/var/log/clobber-state.log"},
112 {kFile, "chrome_system_log", "/var/log/chrome/chrome"},
113 {kFile, "chrome_system_log.PREVIOUS", "/var/log/chrome/chrome.PREVIOUS"},
Mike Frysinger32cdf3e2017-08-14 18:17:06 -0400114 // There might be more than one record, so grab them all.
115 // Plus, for <linux-3.19, it's named "console-ramoops", but for newer
116 // versions, it's named "console-ramoops-#".
Chris Morin853d3442019-04-01 21:35:13 -0700117 {kCommand, "console-ramoops",
118 "cat /sys/fs/pstore/console-ramoops* 2>/dev/null"},
119 {kFile, "cpuinfo", "/proc/cpuinfo"},
120 {kFile, "cr50_version", "/var/cache/cr50-version"},
121 {kFile, "cros_ec.log", "/var/log/cros_ec.log"},
122 {kFile, "cros_ec.previous", "/var/log/cros_ec.previous"},
123 {kFile, "cros_ec_panicinfo", "/sys/kernel/debug/cros_ec/panicinfo",
124 SandboxedProcess::kDefaultUser, kDebugfsGroup},
125 {kFile, "cros_ec_pdinfo", "/sys/kernel/debug/cros_ec/pdinfo",
126 SandboxedProcess::kDefaultUser, kDebugfsGroup},
127 {kFile, "cros_fp.previous", "/var/log/cros_fp.previous"},
128 {kFile, "cros_fp.log", "/var/log/cros_fp.log"},
129 {kCommand, "dmesg", "/bin/dmesg"},
130 {kFile, "ec_info", "/var/log/ec_info.txt"},
Miguel Casas8d4e54f2018-03-29 09:42:52 -0400131 // The sed command replaces the EDID serial number (4 bytes at position 12)
132 // with zeroes. See https://en.wikipedia.org/wiki/EDID#EDID_1.4_data_format.
Chris Morin853d3442019-04-01 21:35:13 -0700133 {kCommand, "edid-decode",
134 "for f in /sys/class/drm/card0-*/edid; do "
135 "echo \"----------- ${f}\"; "
136 "sed -E 's/^(.{11}).{4}/\\1\\x0\\x0\\x0\\x0/' \"${f}\" | "
137 // edid-decode's stderr output is redundant, so silence it.
138 "edid-decode 2>/dev/null; "
139 "done"},
140 {kFile, "eventlog", "/var/log/eventlog.txt"},
141 {kFile, "exynos_gem_objects", "/sys/kernel/debug/dri/0/exynos_gem_objects",
142 SandboxedProcess::kDefaultUser, kDebugfsGroup},
143 {kCommand, "font_info", "/usr/share/userfeedback/scripts/font_info"},
144 {kFile, "fwupd_state", "/var/lib/fwupd/state.json"},
145 {kCommand, "sensor_info", "/usr/share/userfeedback/scripts/sensor_info"},
146 {kFile, "hammerd", "/var/log/hammerd.log"},
147 {kCommand, "hardware_class", "/usr/bin/crossystem hwid"},
148 {kCommand, "hostname", "/bin/hostname"},
149 {kFile, "i915_gem_gtt", "/sys/kernel/debug/dri/0/i915_gem_gtt",
150 SandboxedProcess::kDefaultUser, kDebugfsGroup},
151 {kFile, "i915_gem_objects", "/sys/kernel/debug/dri/0/i915_gem_objects",
152 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700153 {kCommand, "i915_error_state",
154 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2>/dev/null",
155 SandboxedProcess::kDefaultUser, kDebugfsGroup, Log::kDefaultMaxBytes,
Chris Morin790fd262019-04-03 20:29:36 -0700156 LogTool::Encoding::kBase64},
Chris Morin853d3442019-04-01 21:35:13 -0700157 {kCommand, "ifconfig", "/bin/ifconfig -a"},
158 {kFile, "input_devices", "/proc/bus/input/devices"},
Eric Carusob1820c02017-08-24 15:39:56 -0700159 // Hardware capabilities of the wiphy device.
Chris Morin853d3442019-04-01 21:35:13 -0700160 {kCommand, "iw_list", "/usr/sbin/iw list"},
Eric Carusoa879fd92017-10-11 12:57:10 -0700161#if USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700162 {kCommand, "iwlmvm_module_params", CMD_KERNEL_MODULE_PARAMS(iwlmvm)},
163 {kCommand, "iwlwifi_module_params", CMD_KERNEL_MODULE_PARAMS(iwlwifi)},
Eric Carusoa879fd92017-10-11 12:57:10 -0700164#endif // USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700165 {kCommand, "kernel-crashes",
166 "cat /var/spool/crash/kernel.*.kcrash 2>/dev/null"},
Chris Morin853d3442019-04-01 21:35:13 -0700167 {kCommand, "lsmod", "lsmod"},
168 {kCommand, "lspci", "/usr/sbin/lspci"},
169 {kCommand, "lsusb", "lsusb && lsusb -t"},
170 {kFile, "mali_memory", "/sys/class/misc/mali0/device/memory"},
171 {kFile, "memd.parameters", "/var/log/memd/memd.parameters"},
172 {kCommand, "memd clips", "cat /var/log/memd/memd.clip* 2>/dev/null"},
173 {kFile, "meminfo", "/proc/meminfo"},
174 {kCommand, "memory_spd_info",
175 // mosys may use 'i2c-dev', which may not be loaded yet.
176 "modprobe i2c-dev 2>/dev/null && mosys -l memory spd print all 2>/dev/null",
177 kRoot, kDebugfsGroup},
Simon Quecb63b9c2017-06-19 14:53:31 -0400178 // The sed command finds the EDID blob (starting the line after "value:") and
179 // replaces the serial number with all zeroes.
180 //
181 // The EDID is printed as a hex dump over several lines, each line containing
182 // the contents of 16 bytes. The first 16 bytes are broken down as follows:
183 // uint64_t fixed_pattern; // Always 00 FF FF FF FF FF FF 00.
184 // uint16_t manufacturer_id; // Manufacturer ID, encoded as PNP IDs.
185 // uint16_t product_code; // Manufacturer product code, little-endian.
186 // uint32_t serial_number; // Serial number, little-endian.
187 // Source: https://en.wikipedia.org/wiki/EDID#EDID_1.3_data_format
188 //
189 // The subsequent substitution command looks for the fixed pattern followed by
190 // two 32-bit fields (manufacturer + product, serial number). It replaces the
191 // latter field with 8 bytes of zeroes.
192 //
193 // TODO(crbug.com/731133): Remove the sed command once modetest itself can
194 // remove serial numbers.
Chris Morin853d3442019-04-01 21:35:13 -0700195 {kCommand, "modetest",
196 "(modetest; modetest -M evdi; modetest -M udl) | "
197 "sed -E '/EDID/ {:a;n;/value:/!ba;n;"
198 "s/(00f{12}00)([0-9a-f]{8})([0-9a-f]{8})/\\1\\200000000/}'",
199 kRoot, kRoot},
200 {kFile, "mount-encrypted", "/var/log/mount-encrypted.log"},
201 {kFile, "mountinfo", "/proc/1/mountinfo"},
202 {kCommand, "netlog",
203 "/usr/share/userfeedback/scripts/getmsgs /var/log/net.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700204 {kFile, "nvmap_iovmm", "/sys/kernel/debug/nvmap/iovmm/allocations",
205 SandboxedProcess::kDefaultUser, kDebugfsGroup},
206 {kCommand, "oemdata", "/usr/share/cros/oemdata.sh", kRoot, kRoot},
207 {kFile, "pagetypeinfo", "/proc/pagetypeinfo"},
208 {kCommand, "platform_info",
209 // mosys may use 'i2c-dev', which may not be loaded yet.
210 "modprobe i2c-dev 2>/dev/null && "
211 "for param in "
212 "vendor "
213 "name "
214 "version "
215 "family "
216 "model "
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700217
Chris Morin853d3442019-04-01 21:35:13 -0700218 "sku "
219 "customization "
220 "; do "
221 "mosys -l platform \"${param}\" 2>/dev/null; "
222 "done",
223 kRoot, kDebugfsGroup},
224 {kCommand, "power_supply_info", "/usr/bin/power_supply_info"},
225 {kCommand, "power_supply_sysfs", "/usr/bin/print_sysfs_power_supply_data"},
226 {kFile, "powerd.LATEST", "/var/log/power_manager/powerd.LATEST"},
227 {kFile, "powerd.PREVIOUS", "/var/log/power_manager/powerd.PREVIOUS"},
228 {kFile, "powerd.out", "/var/log/powerd.out"},
229 {kFile, "powerwash_count", "/var/log/powerwash_count"},
Brian Norris4cde3d12019-04-16 10:10:34 -0700230 {kCommand, "ps", "/bin/ps auxZ"},
yusukes34171ba2017-04-27 15:46:01 -0700231 // /proc/slabinfo is owned by root and has 0400 permission.
Chris Morin853d3442019-04-01 21:35:13 -0700232 {kFile, "slabinfo", "/proc/slabinfo", kRoot, kRoot},
233 {kFile, "storage_info", "/var/log/storage_info.txt"},
234 {kCommand, "swap_info", "/usr/share/cros/init/swap.sh status 2>/dev/null",
235 SandboxedProcess::kDefaultUser, kDebugfsGroup},
236 {kCommand, "syslog",
237 "/usr/share/userfeedback/scripts/getmsgs /var/log/messages"},
238 {kCommand, "system_log_stats",
239 "echo 'BLOCK_SIZE=1024'; "
240 "find /var/log/ -type f -exec du --block-size=1024 {} + | sort -n -r",
241 kRoot, kRoot},
242 {kCommand, "threads", "/bin/ps -T axo pid,ppid,spid,pcpu,ni,stat,time,comm"},
243 {kFile, "tlsdate", "/var/log/tlsdate.log"},
244 {kCommand, "top thread", "/usr/bin/top -Hb -n 1 | head -n 40"},
245 {kCommand, "top memory", "/usr/bin/top -o \"+%MEM\" -bn 1 | head -n 57"},
246 {kCommand, "touch_fw_version",
247 "grep -E"
248 " -e 'synaptics: Touchpad model'"
249 " -e 'chromeos-[a-z]*-touch-[a-z]*-update'"
250 " /var/log/messages | tail -n 20"},
251 {kCommand, "tpm-firmware-updater", "/usr/share/userfeedback/scripts/getmsgs "
252 "/var/log/tpm-firmware-updater.log"},
Mattias Nissler887dce22017-07-03 14:44:35 +0200253 // TODO(jorgelo,mnissler): Don't run this as root.
254 // On TPM 1.2 devices this will likely require adding a new user to the 'tss'
255 // group.
256 // On TPM 2.0 devices 'get_version_info' uses D-Bus and therefore can run as
257 // any user.
Chris Morin853d3442019-04-01 21:35:13 -0700258 {kCommand, "tpm_version", "/usr/sbin/tpm-manager get_version_info", kRoot,
259 kRoot},
260 {kCommand, "atmel_ts_refs",
261 "/opt/google/touch/scripts/atmel_tools.sh ts r", kRoot, kRoot},
262 {kCommand, "atmel_tp_refs",
263 "/opt/google/touch/scripts/atmel_tools.sh tp r", kRoot, kRoot},
264 {kCommand, "atmel_ts_deltas",
265 "/opt/google/touch/scripts/atmel_tools.sh ts d", kRoot, kRoot},
266 {kCommand, "atmel_tp_deltas",
267 "/opt/google/touch/scripts/atmel_tools.sh tp d", kRoot, kRoot},
268 {kFile, "stateful_trim_state", "/var/lib/trim/stateful_trim_state"},
269 {kFile, "stateful_trim_data", "/var/lib/trim/stateful_trim_data"},
270 {kFile, "ui_log", "/var/log/ui/ui.LATEST"},
271 {kCommand, "uname", "/bin/uname -a"},
272 {kCommand, "update_engine.log",
273 "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
274 " s.^./var/log/update_engine/.)"},
Chris Morinca152712019-05-03 13:17:28 -0700275 {kFile, "upstart", "/var/log/upstart.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700276 {kCommand, "uptime", "/usr/bin/cut -d' ' -f1 /proc/uptime"},
277 {kFile, "verified boot", "/var/log/debug_vboot_noisy.log"},
278 {kFile, "vmlog.1.LATEST", "/var/log/vmlog/vmlog.1.LATEST"},
279 {kFile, "vmlog.1.PREVIOUS", "/var/log/vmlog/vmlog.1.PREVIOUS"},
280 {kFile, "vmlog.LATEST", "/var/log/vmlog/vmlog.LATEST"},
281 {kFile, "vmlog.PREVIOUS", "/var/log/vmlog/vmlog.PREVIOUS"},
282 {kFile, "vmstat", "/proc/vmstat"},
283 {kFile, "vpd_2.0", "/var/log/vpd_2.0.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700284 {kFile, "zram compressed data size", "/sys/block/zram0/compr_data_size"},
285 {kFile, "zram original data size", "/sys/block/zram0/orig_data_size"},
286 {kFile, "zram total memory used", "/sys/block/zram0/mem_used_total"},
287 {kFile, "zram total reads", "/sys/block/zram0/num_reads"},
288 {kFile, "zram total writes", "/sys/block/zram0/num_writes"},
289 {kCommand, "zram new stats names",
290 "echo orig_size compr_size used_total limit used_max zero_pages migrated"},
291 {kFile, "zram new stats values", "/sys/block/zram0/mm_stat"},
292 {kFile, "cros_tp version", "/sys/class/chromeos/cros_tp/version"},
293 {kCommand, "cros_tp console", "/usr/sbin/ectool --name=cros_tp console",
294 kRoot, kRoot},
295 {kCommand, "cros_tp frame", "/usr/sbin/ectool --name=cros_tp tpframeget",
296 kRoot, kRoot},
297 {kCommand, "crostini", "/usr/bin/cicerone_client --get_info"},
Elly Jones03cd6d72012-06-11 13:04:28 -0400298 // Stuff pulled out of the original list. These need access to the running X
299 // session, which we'd rather not give to debugd, or return info specific to
300 // the current session (in the setsid(2) sense), which is not useful for
301 // debugd
Chris Morin853d3442019-04-01 21:35:13 -0700302 // {kCommand, "env", "set"},
303 // {kCommand, "setxkbmap", "/usr/bin/setxkbmap -print -query"},
304 // {kCommand, "xrandr", "/usr/bin/xrandr --verbose}
Elly Jones533c7c42012-08-10 15:07:05 -0400305};
306
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700307// netstat and logcat should appear in chrome://system but not in feedback
308// reports. Open sockets may have privacy implications, and logcat is
309// already incorporated via arc-bugreport.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700310const std::vector<Log> kExtraLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800311#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700312 {kCommand, "mm-status", "/usr/bin/modem status"},
Ben Chan36e42282014-02-12 22:32:34 -0800313#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700314 {kCommand, "network-devices", "/usr/bin/connectivity show devices"},
315 {kCommand, "network-services", "/usr/bin/connectivity show services"},
Brian Norris2b290502019-04-23 17:17:10 -0700316 {kCommand, "wifi_status_show_macs",
317 "/usr/bin/network_diag --wifi-internal --no-log --show-macs"},
Chris Morin253a2b02019-04-12 16:04:25 -0700318 // --processes requires root.
319 {kCommand, "netstat",
320 "/sbin/ss --all --query inet --numeric --processes", kRoot, kRoot},
321 {kCommand, "logcat", "/usr/sbin/android-sh -c '/system/bin/logcat -d'",
322 kRoot, kRoot, Log::kDefaultMaxBytes, LogTool::Encoding::kUtf8},
Elly Jones533c7c42012-08-10 15:07:05 -0400323};
324
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700325const std::vector<Log> kFeedbackLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800326#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700327 {kCommand, "mm-status", "/usr/bin/modem status-feedback"},
Ben Chan36e42282014-02-12 22:32:34 -0800328#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700329 {kCommand, "network-devices",
330 "/usr/bin/connectivity show-feedback devices"},
331 {kCommand, "network-services",
332 "/usr/bin/connectivity show-feedback services"},
Brian Norris2b290502019-04-23 17:17:10 -0700333 {kCommand, "wifi_status", "/usr/bin/network_diag --wifi-internal --no-log"},
Elly Jones03cd6d72012-06-11 13:04:28 -0400334};
335
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700336// List of log files needed to be part of the feedback report that are huge and
337// must be sent back to the client via the file descriptor using
338// LogTool::GetBigFeedbackLogs().
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700339const std::vector<Log> kBigFeedbackLogs{
Fletcher Woodruffc9f36a72019-03-20 08:05:15 -0600340 // ARC bugreport permissions are weird. Since we're just running cat,
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700341 // this shouldn't cause any issues.
Chris Morin853d3442019-04-01 21:35:13 -0700342 {kCommand, "arc-bugreport", "cat /run/arc/bugreport/pipe 2>/dev/null",
343 kRoot, kRoot, 10 * 1024 * 1024, LogTool::Encoding::kUtf8},
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700344};
345
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700346// List of log files that must directly be collected by Chrome. This is because
347// debugd is running under a VFS namespace and does not have access to later
348// cryptohome mounts.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700349using UserLog = std::pair<std::string, std::string>;
350const std::vector<UserLog> kUserLogs = {
Chris Morin853d3442019-04-01 21:35:13 -0700351 {"chrome_user_log", "log/chrome"},
352 {"libassistant_user_log", "log/libassistant.log"},
353 {"login-times", "login-times"},
354 {"logout-times", "logout-times"},
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700355};
356
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800357// Fills |dictionary| with the anonymized contents of the logs in |logs|.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700358void GetLogsInDictionary(const std::vector<Log>& logs,
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800359 AnonymizerTool* anonymizer,
360 base::DictionaryValue* dictionary) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700361 for (const Log& log : logs) {
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800362 dictionary->SetStringWithoutPathExpansion(
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700363 log.GetName(), anonymizer->Anonymize(log.GetLogData()));
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800364 }
365}
366
367// Serializes the |dictionary| into the file with the given |fd| in a JSON
368// format.
369void SerializeLogsAsJSON(const base::DictionaryValue& dictionary,
Eric Caruso0b241882018-04-04 13:43:46 -0700370 const base::ScopedFD& fd) {
Eric Caruso96d03d32017-04-25 18:01:17 -0700371 string logs_json;
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800372 base::JSONWriter::WriteWithOptions(dictionary,
373 base::JSONWriter::OPTIONS_PRETTY_PRINT,
374 &logs_json);
Eric Caruso0b241882018-04-04 13:43:46 -0700375 base::WriteFileDescriptor(fd.get(), logs_json.c_str(), logs_json.size());
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800376}
377
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700378bool GetNamedLogFrom(const string& name,
379 const std::vector<Log>& logs,
Elly Jones533c7c42012-08-10 15:07:05 -0400380 string* result) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700381 for (const Log& log : logs) {
382 if (name == log.GetName()) {
383 *result = log.GetLogData();
Elly Jones533c7c42012-08-10 15:07:05 -0400384 return true;
385 }
386 }
387 *result = "<invalid log name>";
388 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400389}
390
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700391void GetLogsFrom(const std::vector<Log>& logs, LogTool::LogMap* map) {
392 for (const Log& log : logs)
393 (*map)[log.GetName()] = log.GetLogData();
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800394}
395
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600396void GetLsbReleaseInfo(LogTool::LogMap* map) {
397 const base::FilePath lsb_release(kLsbReleasePath);
398 brillo::KeyValueStore store;
399 if (!store.Load(lsb_release)) {
400 // /etc/lsb-release might not be present (cros deploying a new
401 // configuration or no fields set at all). Just print a debug
402 // message and continue.
403 DLOG(INFO) << "Could not load fields from " << lsb_release.value();
404 } else {
405 for (const auto& key : store.GetKeys()) {
406 std::string value;
407 store.GetString(key, &value);
408 (*map)[key] = value;
409 }
410 }
411}
412
413void GetOsReleaseInfo(LogTool::LogMap* map) {
414 brillo::OsReleaseReader reader;
415 reader.Load();
416 for (const auto& key : reader.GetKeys()) {
417 std::string value;
418 reader.GetString(key, &value);
419 (*map)["os-release " + key] = value;
420 }
421}
422
423void PopulateDictionaryValue(const LogTool::LogMap& map,
424 base::DictionaryValue* dictionary) {
425 for (const auto& kv : map) {
426 dictionary->SetString(kv.first, kv.second);
427 }
428}
429
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800430bool CompressXzBuffer(const std::vector<uint8_t>& in_buffer,
431 std::vector<uint8_t>* out_buffer) {
432 size_t out_size = lzma_stream_buffer_bound(in_buffer.size());
433 out_buffer->resize(out_size);
434 size_t out_pos = 0;
435
436 lzma_ret ret = lzma_easy_buffer_encode(
437 LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64, nullptr, in_buffer.data(),
438 in_buffer.size(), out_buffer->data(), &out_pos, out_size);
439
440 if (ret != LZMA_OK) {
441 out_buffer->clear();
442 return false;
443 }
444
445 out_buffer->resize(out_pos);
446 return true;
447}
448
449void GetPerfData(LogTool::LogMap* map) {
450 // Run perf to collect system-wide performance profile when user triggers
451 // feedback report. Perf runs at sampling frequency of ~500 hz (499 is used
452 // to avoid sampling periodic system activities), with callstack in each
453 // sample (-g).
454 std::vector<std::string> perf_args = {
455 "perf", "record", "-a", "-g", "-F", "499"
456 };
457 std::vector<uint8_t> perf_data;
458 int32_t status;
459
460 debugd::PerfTool perf_tool;
461 if (!perf_tool.GetPerfOutput(kPerfDurationSecs, perf_args, &perf_data,
462 nullptr, &status, nullptr))
463 return;
464
465 // XZ compress the profile data.
466 std::vector<uint8_t> perf_data_xz;
467 if (!CompressXzBuffer(perf_data, &perf_data_xz))
468 return;
469
470 // Base64 encode the compressed data.
471 std::string perf_data_str(reinterpret_cast<const char*>(perf_data_xz.data()),
472 perf_data_xz.size());
473 (*map)["perf-data"] =
Chinglin Yu3c8d0a22019-02-20 11:32:52 +0800474 std::string(kPerfDataDescription) +
Chris Morin790fd262019-04-03 20:29:36 -0700475 LogTool::EncodeString(std::move(perf_data_str),
476 LogTool::Encoding::kBase64);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800477}
478
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800479} // namespace
480
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700481Log::Log(Log::LogType type,
482 std::string name,
483 std::string data,
484 std::string user,
485 std::string group,
486 int64_t max_bytes,
487 LogTool::Encoding encoding)
488 : type_(type),
489 name_(name),
490 data_(data),
491 user_(user),
492 group_(group),
493 max_bytes_(max_bytes),
494 encoding_(encoding) {}
495
496std::string Log::GetName() const {
497 return name_;
498}
499
500std::string Log::GetLogData() const {
501 // The reason this code uses a switch statement on a type enum rather than
502 // using inheritance/virtual dispatch is so that all of the Log objects can
503 // be constructed statically. Switching to heap allocated subclasses of Log
504 // makes the code that declares all of the log entries much more verbose
505 // and harder to understand.
Chris Morin790fd262019-04-03 20:29:36 -0700506 std::string output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700507 switch (type_) {
508 case kCommand:
Chris Morin790fd262019-04-03 20:29:36 -0700509 output = GetCommandLogData();
510 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700511 case kFile:
Chris Morin790fd262019-04-03 20:29:36 -0700512 output = GetFileLogData();
513 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700514 default:
515 return "<unknown log type>";
516 }
Chris Morin790fd262019-04-03 20:29:36 -0700517
518 if (output.empty())
519 return "<empty>";
520
521 return LogTool::EncodeString(std::move(output), encoding_);
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700522}
523
524// TODO(ellyjones): sandbox. crosbug.com/35122
525std::string Log::GetCommandLogData() const {
526 if (type_ != kCommand)
527 return "<log type mismatch>";
528 std::string tailed_cmdline =
529 base::StringPrintf("%s | tail -c %" PRId64, data_.c_str(), max_bytes_);
530 ProcessWithOutput p;
531 if (minijail_disabled_for_test_)
532 p.set_use_minijail(false);
533 if (!user_.empty() && !group_.empty())
534 p.SandboxAs(user_, group_);
535 if (!p.Init())
536 return "<not available>";
537 p.AddArg(kShell);
538 p.AddStringOption("-c", tailed_cmdline);
539 if (p.Run())
540 return "<not available>";
541 std::string output;
542 p.GetOutput(&output);
Chris Morin790fd262019-04-03 20:29:36 -0700543 return output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700544}
545
546std::string Log::GetFileLogData() const {
547 if (type_ != kFile)
548 return "<log type mismatch>";
549
550 uid_t old_euid = geteuid();
551 uid_t new_euid = UidForUser(user_);
552 gid_t old_egid = getegid();
553 gid_t new_egid = GidForGroup(group_);
554
555 if (new_euid == -1 || new_egid == -1) {
556 return "<not available>";
557 }
558
559 // Make sure to set group first, since if we set user first we lose root
560 // and therefore the ability to set our effective gid to arbitrary gids.
561 if (setegid(new_egid)) {
562 PLOG(ERROR) << "Failed to set effective group id to " << new_egid;
563 return "<not available>";
564 }
565 if (seteuid(new_euid)) {
566 PLOG(ERROR) << "Failed to set effective user id to " << new_euid;
567 if (setegid(old_egid))
568 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
569 return "<not available>";
570 }
571
572 std::string contents;
573 const base::FilePath path(data_);
574 // Handle special files that don't properly report length/allow lseek.
575 if (base::FilePath("/dev").IsParent(path) ||
576 base::FilePath("/proc").IsParent(path) ||
577 base::FilePath("/sys").IsParent(path)) {
578 if (!base::ReadFileToString(path, &contents))
579 contents = "<not available>";
580 if (contents.size() > max_bytes_)
581 contents.erase(0, contents.size() - max_bytes_);
582 } else {
583 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
584 if (!file.IsValid()) {
585 contents = "<not available>";
586 } else {
587 int64_t length = file.GetLength();
588 if (length > max_bytes_) {
589 file.Seek(base::File::FROM_END, -max_bytes_);
590 length = max_bytes_;
591 }
592 std::vector<char> buf(length);
593 int read = file.ReadAtCurrentPos(buf.data(), buf.size());
594 if (read < 0) {
595 PLOG(ERROR) << "Could not read from file " << path.value();
596 } else {
597 contents = std::string(buf.begin(), buf.begin() + read);
598 }
599 }
600 }
601
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700602 // Make sure we restore our old euid/egid before returning.
603 if (seteuid(old_euid))
604 PLOG(ERROR) << "Failed to restore effective user id to " << old_euid;
605
606 if (setegid(old_egid))
607 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
608
Chris Morin790fd262019-04-03 20:29:36 -0700609 return contents;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700610}
611
612void Log::DisableMinijailForTest() {
613 minijail_disabled_for_test_ = true;
614}
615
616// static
617uid_t Log::UidForUser(const std::string& user) {
618 struct passwd entry;
619 struct passwd* result;
620 std::vector<char> buf(1024);
621 getpwnam_r(user.c_str(), &entry, &buf[0], buf.size(), &result);
622 if (!result) {
623 LOG(ERROR) << "User not found: " << user;
624 return -1;
625 }
626 return entry.pw_uid;
627}
628
629// static
630gid_t Log::GidForGroup(const std::string& group) {
631 struct group entry;
632 struct group* result;
633 std::vector<char> buf(1024);
634 getgrnam_r(group.c_str(), &entry, &buf[0], buf.size(), &result);
635 if (!result) {
636 LOG(ERROR) << "Group not found: " << group;
637 return -1;
638 }
639 return entry.gr_gid;
640}
641
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700642void LogTool::CreateConnectivityReport(bool wait_for_results) {
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700643 // Perform ConnectivityTrial to report connection state in feedback log.
Ben Chan8e9f6d02017-09-26 23:04:21 -0700644 auto shill = std::make_unique<org::chromium::flimflam::ManagerProxy>(bus_);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700645 // Give the connection trial time to test the connection and log the results
646 // before collecting the logs for feedback.
647 // TODO(silberst): Replace the simple approach of a single timeout with a more
648 // coordinated effort.
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700649 if (shill && shill->CreateConnectivityReport(nullptr) && wait_for_results)
Eric Carusocc7106c2017-04-27 14:22:42 -0700650 sleep(kConnectionTesterTimeoutSeconds);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700651}
652
Eric Carusoc93a15c2017-04-24 16:15:12 -0700653string LogTool::GetLog(const string& name) {
Elly Jones533c7c42012-08-10 15:07:05 -0400654 string result;
Ben Chancf7d6412017-08-10 22:30:09 -0700655 GetNamedLogFrom(name, kCommandLogs, &result)
656 || GetNamedLogFrom(name, kExtraLogs, &result)
657 || GetNamedLogFrom(name, kFeedbackLogs, &result);
Elly Jones533c7c42012-08-10 15:07:05 -0400658 return result;
659}
660
Eric Carusof9091f82017-04-28 14:18:59 -0700661LogTool::LogMap LogTool::GetAllLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700662 CreateConnectivityReport(false);
Elly Jones533c7c42012-08-10 15:07:05 -0400663 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700664 GetLogsFrom(kCommandLogs, &result);
665 GetLogsFrom(kExtraLogs, &result);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600666 GetLsbReleaseInfo(&result);
667 GetOsReleaseInfo(&result);
Elly Jones533c7c42012-08-10 15:07:05 -0400668 return result;
669}
670
Brian Norrisca4fc042018-04-03 00:24:26 -0700671LogTool::LogMap LogTool::GetAllDebugLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700672 CreateConnectivityReport(true);
Brian Norrisca4fc042018-04-03 00:24:26 -0700673 LogMap result;
674 GetLogsFrom(kCommandLogs, &result);
675 GetLogsFrom(kExtraLogs, &result);
676 GetLogsFrom(kBigFeedbackLogs, &result);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600677 GetLsbReleaseInfo(&result);
678 GetOsReleaseInfo(&result);
Brian Norrisca4fc042018-04-03 00:24:26 -0700679 return result;
680}
681
Eric Caruso0b241882018-04-04 13:43:46 -0700682void LogTool::GetBigFeedbackLogs(const base::ScopedFD& fd) {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700683 CreateConnectivityReport(true);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800684 LogMap map;
685 GetPerfData(&map);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800686 base::DictionaryValue dictionary;
Ben Chancf7d6412017-08-10 22:30:09 -0700687 GetLogsInDictionary(kCommandLogs, &anonymizer_, &dictionary);
688 GetLogsInDictionary(kFeedbackLogs, &anonymizer_, &dictionary);
689 GetLogsInDictionary(kBigFeedbackLogs, &anonymizer_, &dictionary);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600690 GetLsbReleaseInfo(&map);
691 GetOsReleaseInfo(&map);
692 PopulateDictionaryValue(map, &dictionary);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800693 SerializeLogsAsJSON(dictionary, fd);
694}
695
Chris Morin790fd262019-04-03 20:29:36 -0700696void LogTool::GetJournalLog(bool scrub, const base::ScopedFD& fd) {
697 Log journal(kCommand, "journal.export", "journalctl -n 10000 -o export",
698 "syslog", "syslog", 10 * 1024 * 1024, LogTool::Encoding::kBinary);
699 std::string output = scrub ? anonymizer_.Anonymize(journal.GetLogData())
700 : journal.GetLogData();
701 base::WriteFileDescriptor(fd.get(), output.data(), output.size());
702}
703
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700704// static
Chris Morin790fd262019-04-03 20:29:36 -0700705string LogTool::EncodeString(string value,
706 LogTool::Encoding source_encoding) {
707 if (source_encoding == LogTool::Encoding::kBinary)
708 return value;
709
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700710 if (source_encoding == LogTool::Encoding::kAutodetect) {
711 if (base::IsStringUTF8(value))
712 return value;
Chris Morin790fd262019-04-03 20:29:36 -0700713 source_encoding = LogTool::Encoding::kBase64;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700714 }
715
716 if (source_encoding == LogTool::Encoding::kUtf8) {
717 string output;
718 const char* src = value.data();
719 int32_t src_len = static_cast<int32_t>(value.length());
720
721 output.reserve(value.size());
722 for (int32_t char_index = 0; char_index < src_len; char_index++) {
723 uint32_t code_point;
724 if (!base::ReadUnicodeCharacter(src, src_len, &char_index, &code_point) ||
725 !base::IsValidCharacter(code_point)) {
726 // Replace invalid characters with U+FFFD REPLACEMENT CHARACTER.
727 code_point = 0xFFFD;
728 }
729 base::WriteUnicodeCharacter(code_point, &output);
730 }
731 return output;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700732 }
Chris Morin853d3442019-04-01 21:35:13 -0700733
734 base::Base64Encode(value, &value);
735 return "<base64>: " + value;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700736}
737
Darin Petkovce9b3a12013-01-10 16:38:54 +0100738void LogTool::AnonymizeLogMap(LogMap* log_map) {
Ben Chane2fa3572017-02-08 22:46:18 -0800739 for (auto& entry : *log_map)
740 entry.second = anonymizer_.Anonymize(entry.second);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100741}
742
Ben Chana0011d82014-05-13 00:19:29 -0700743} // namespace debugd