blob: 87d14e8178b6e2ea369c3492641c99d7e7f3de62 [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"},
82 {kFile, "atrus_logs", "/var/log/atrus.log"},
83 {kFile, "authpolicy", "/var/log/authpolicy.log"},
84 {kCommand, "bootstat_summary", "/usr/bin/bootstat_summary"},
85 {kFile, "bio_crypto_init.log",
86 "/var/log/bio_crypto_init/bio_crypto_init.log"},
87 {kFile, "biod.LATEST", "/var/log/biod/biod.LATEST"},
88 {kFile, "biod.PREVIOUS", "/var/log/biod/biod.PREVIOUS"},
89 {kFile, "bios_info", "/var/log/bios_info.txt"},
90 {kCommand, "bios_log", "cat /sys/firmware/log "
91 "/proc/device-tree/chosen/ap-console-buffer 2>/dev/null"},
92 {kFile, "bios_times", "/var/log/bios_times.txt"},
93 {kCommand, "board-specific",
94 "/usr/share/userfeedback/scripts/get_board_specific_info"},
95 {kFile, "buddyinfo", "/proc/buddyinfo"},
96 {kCommand, "cbi_info", "/usr/share/userfeedback/scripts/cbi_info", kRoot,
97 kRoot},
98 {kFile, "cheets_log", "/var/log/arc.log"},
99 {kFile, "clobber.log", "/var/log/clobber.log"},
100 {kFile, "clobber-state.log", "/var/log/clobber-state.log"},
101 {kFile, "chrome_system_log", "/var/log/chrome/chrome"},
102 {kFile, "chrome_system_log.PREVIOUS", "/var/log/chrome/chrome.PREVIOUS"},
Mike Frysinger32cdf3e2017-08-14 18:17:06 -0400103 // There might be more than one record, so grab them all.
104 // Plus, for <linux-3.19, it's named "console-ramoops", but for newer
105 // versions, it's named "console-ramoops-#".
Chris Morin853d3442019-04-01 21:35:13 -0700106 {kCommand, "console-ramoops",
107 "cat /sys/fs/pstore/console-ramoops* 2>/dev/null"},
108 {kFile, "cpuinfo", "/proc/cpuinfo"},
109 {kFile, "cr50_version", "/var/cache/cr50-version"},
110 {kFile, "cros_ec.log", "/var/log/cros_ec.log"},
111 {kFile, "cros_ec.previous", "/var/log/cros_ec.previous"},
112 {kFile, "cros_ec_panicinfo", "/sys/kernel/debug/cros_ec/panicinfo",
113 SandboxedProcess::kDefaultUser, kDebugfsGroup},
114 {kFile, "cros_ec_pdinfo", "/sys/kernel/debug/cros_ec/pdinfo",
115 SandboxedProcess::kDefaultUser, kDebugfsGroup},
116 {kFile, "cros_fp.previous", "/var/log/cros_fp.previous"},
117 {kFile, "cros_fp.log", "/var/log/cros_fp.log"},
118 {kCommand, "dmesg", "/bin/dmesg"},
119 {kFile, "ec_info", "/var/log/ec_info.txt"},
Miguel Casas8d4e54f2018-03-29 09:42:52 -0400120 // The sed command replaces the EDID serial number (4 bytes at position 12)
121 // with zeroes. See https://en.wikipedia.org/wiki/EDID#EDID_1.4_data_format.
Chris Morin853d3442019-04-01 21:35:13 -0700122 {kCommand, "edid-decode",
123 "for f in /sys/class/drm/card0-*/edid; do "
124 "echo \"----------- ${f}\"; "
125 "sed -E 's/^(.{11}).{4}/\\1\\x0\\x0\\x0\\x0/' \"${f}\" | "
126 // edid-decode's stderr output is redundant, so silence it.
127 "edid-decode 2>/dev/null; "
128 "done"},
129 {kFile, "eventlog", "/var/log/eventlog.txt"},
130 {kFile, "exynos_gem_objects", "/sys/kernel/debug/dri/0/exynos_gem_objects",
131 SandboxedProcess::kDefaultUser, kDebugfsGroup},
132 {kCommand, "font_info", "/usr/share/userfeedback/scripts/font_info"},
133 {kFile, "fwupd_state", "/var/lib/fwupd/state.json"},
134 {kCommand, "sensor_info", "/usr/share/userfeedback/scripts/sensor_info"},
135 {kFile, "hammerd", "/var/log/hammerd.log"},
136 {kCommand, "hardware_class", "/usr/bin/crossystem hwid"},
137 {kCommand, "hostname", "/bin/hostname"},
138 {kFile, "i915_gem_gtt", "/sys/kernel/debug/dri/0/i915_gem_gtt",
139 SandboxedProcess::kDefaultUser, kDebugfsGroup},
140 {kFile, "i915_gem_objects", "/sys/kernel/debug/dri/0/i915_gem_objects",
141 SandboxedProcess::kDefaultUser, kDebugfsGroup},
Chris Morin853d3442019-04-01 21:35:13 -0700142 {kCommand, "i915_error_state",
143 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2>/dev/null",
144 SandboxedProcess::kDefaultUser, kDebugfsGroup, Log::kDefaultMaxBytes,
Chris Morin790fd262019-04-03 20:29:36 -0700145 LogTool::Encoding::kBase64},
Chris Morin853d3442019-04-01 21:35:13 -0700146 {kCommand, "ifconfig", "/bin/ifconfig -a"},
147 {kFile, "input_devices", "/proc/bus/input/devices"},
Eric Carusob1820c02017-08-24 15:39:56 -0700148 // Information about the wiphy device, such as current channel.
Chris Morin853d3442019-04-01 21:35:13 -0700149 {kCommand, "iw_dev", "/usr/sbin/iw dev"},
Eric Carusob1820c02017-08-24 15:39:56 -0700150 // Hardware capabilities of the wiphy device.
Chris Morin853d3442019-04-01 21:35:13 -0700151 {kCommand, "iw_list", "/usr/sbin/iw list"},
Eric Carusoa879fd92017-10-11 12:57:10 -0700152#if USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700153 {kCommand, "iwlmvm_module_params", CMD_KERNEL_MODULE_PARAMS(iwlmvm)},
154 {kCommand, "iwlwifi_module_params", CMD_KERNEL_MODULE_PARAMS(iwlwifi)},
Eric Carusoa879fd92017-10-11 12:57:10 -0700155#endif // USE_IWLWIFI_DUMP
Chris Morin853d3442019-04-01 21:35:13 -0700156 {kCommand, "kernel-crashes",
157 "cat /var/spool/crash/kernel.*.kcrash 2>/dev/null"},
Chris Morin853d3442019-04-01 21:35:13 -0700158 {kCommand, "lsmod", "lsmod"},
159 {kCommand, "lspci", "/usr/sbin/lspci"},
160 {kCommand, "lsusb", "lsusb && lsusb -t"},
161 {kFile, "mali_memory", "/sys/class/misc/mali0/device/memory"},
162 {kFile, "memd.parameters", "/var/log/memd/memd.parameters"},
163 {kCommand, "memd clips", "cat /var/log/memd/memd.clip* 2>/dev/null"},
164 {kFile, "meminfo", "/proc/meminfo"},
165 {kCommand, "memory_spd_info",
166 // mosys may use 'i2c-dev', which may not be loaded yet.
167 "modprobe i2c-dev 2>/dev/null && mosys -l memory spd print all 2>/dev/null",
168 kRoot, kDebugfsGroup},
Simon Quecb63b9c2017-06-19 14:53:31 -0400169 // The sed command finds the EDID blob (starting the line after "value:") and
170 // replaces the serial number with all zeroes.
171 //
172 // The EDID is printed as a hex dump over several lines, each line containing
173 // the contents of 16 bytes. The first 16 bytes are broken down as follows:
174 // uint64_t fixed_pattern; // Always 00 FF FF FF FF FF FF 00.
175 // uint16_t manufacturer_id; // Manufacturer ID, encoded as PNP IDs.
176 // uint16_t product_code; // Manufacturer product code, little-endian.
177 // uint32_t serial_number; // Serial number, little-endian.
178 // Source: https://en.wikipedia.org/wiki/EDID#EDID_1.3_data_format
179 //
180 // The subsequent substitution command looks for the fixed pattern followed by
181 // two 32-bit fields (manufacturer + product, serial number). It replaces the
182 // latter field with 8 bytes of zeroes.
183 //
184 // TODO(crbug.com/731133): Remove the sed command once modetest itself can
185 // remove serial numbers.
Chris Morin853d3442019-04-01 21:35:13 -0700186 {kCommand, "modetest",
187 "(modetest; modetest -M evdi; modetest -M udl) | "
188 "sed -E '/EDID/ {:a;n;/value:/!ba;n;"
189 "s/(00f{12}00)([0-9a-f]{8})([0-9a-f]{8})/\\1\\200000000/}'",
190 kRoot, kRoot},
191 {kFile, "mount-encrypted", "/var/log/mount-encrypted.log"},
192 {kFile, "mountinfo", "/proc/1/mountinfo"},
193 {kCommand, "netlog",
194 "/usr/share/userfeedback/scripts/getmsgs /var/log/net.log"},
Chris Morin853d3442019-04-01 21:35:13 -0700195 {kFile, "nvmap_iovmm", "/sys/kernel/debug/nvmap/iovmm/allocations",
196 SandboxedProcess::kDefaultUser, kDebugfsGroup},
197 {kCommand, "oemdata", "/usr/share/cros/oemdata.sh", kRoot, kRoot},
198 {kFile, "pagetypeinfo", "/proc/pagetypeinfo"},
199 {kCommand, "platform_info",
200 // mosys may use 'i2c-dev', which may not be loaded yet.
201 "modprobe i2c-dev 2>/dev/null && "
202 "for param in "
203 "vendor "
204 "name "
205 "version "
206 "family "
207 "model "
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700208
Chris Morin853d3442019-04-01 21:35:13 -0700209 "sku "
210 "customization "
211 "; do "
212 "mosys -l platform \"${param}\" 2>/dev/null; "
213 "done",
214 kRoot, kDebugfsGroup},
215 {kCommand, "power_supply_info", "/usr/bin/power_supply_info"},
216 {kCommand, "power_supply_sysfs", "/usr/bin/print_sysfs_power_supply_data"},
217 {kFile, "powerd.LATEST", "/var/log/power_manager/powerd.LATEST"},
218 {kFile, "powerd.PREVIOUS", "/var/log/power_manager/powerd.PREVIOUS"},
219 {kFile, "powerd.out", "/var/log/powerd.out"},
220 {kFile, "powerwash_count", "/var/log/powerwash_count"},
Brian Norris4cde3d12019-04-16 10:10:34 -0700221 {kCommand, "ps", "/bin/ps auxZ"},
yusukes34171ba2017-04-27 15:46:01 -0700222 // /proc/slabinfo is owned by root and has 0400 permission.
Chris Morin853d3442019-04-01 21:35:13 -0700223 {kFile, "slabinfo", "/proc/slabinfo", kRoot, kRoot},
224 {kFile, "storage_info", "/var/log/storage_info.txt"},
225 {kCommand, "swap_info", "/usr/share/cros/init/swap.sh status 2>/dev/null",
226 SandboxedProcess::kDefaultUser, kDebugfsGroup},
227 {kCommand, "syslog",
228 "/usr/share/userfeedback/scripts/getmsgs /var/log/messages"},
229 {kCommand, "system_log_stats",
230 "echo 'BLOCK_SIZE=1024'; "
231 "find /var/log/ -type f -exec du --block-size=1024 {} + | sort -n -r",
232 kRoot, kRoot},
233 {kCommand, "threads", "/bin/ps -T axo pid,ppid,spid,pcpu,ni,stat,time,comm"},
234 {kFile, "tlsdate", "/var/log/tlsdate.log"},
235 {kCommand, "top thread", "/usr/bin/top -Hb -n 1 | head -n 40"},
236 {kCommand, "top memory", "/usr/bin/top -o \"+%MEM\" -bn 1 | head -n 57"},
237 {kCommand, "touch_fw_version",
238 "grep -E"
239 " -e 'synaptics: Touchpad model'"
240 " -e 'chromeos-[a-z]*-touch-[a-z]*-update'"
241 " /var/log/messages | tail -n 20"},
242 {kCommand, "tpm-firmware-updater", "/usr/share/userfeedback/scripts/getmsgs "
243 "/var/log/tpm-firmware-updater.log"},
Mattias Nissler887dce22017-07-03 14:44:35 +0200244 // TODO(jorgelo,mnissler): Don't run this as root.
245 // On TPM 1.2 devices this will likely require adding a new user to the 'tss'
246 // group.
247 // On TPM 2.0 devices 'get_version_info' uses D-Bus and therefore can run as
248 // any user.
Chris Morin853d3442019-04-01 21:35:13 -0700249 {kCommand, "tpm_version", "/usr/sbin/tpm-manager get_version_info", kRoot,
250 kRoot},
251 {kCommand, "atmel_ts_refs",
252 "/opt/google/touch/scripts/atmel_tools.sh ts r", kRoot, kRoot},
253 {kCommand, "atmel_tp_refs",
254 "/opt/google/touch/scripts/atmel_tools.sh tp r", kRoot, kRoot},
255 {kCommand, "atmel_ts_deltas",
256 "/opt/google/touch/scripts/atmel_tools.sh ts d", kRoot, kRoot},
257 {kCommand, "atmel_tp_deltas",
258 "/opt/google/touch/scripts/atmel_tools.sh tp d", kRoot, kRoot},
259 {kFile, "stateful_trim_state", "/var/lib/trim/stateful_trim_state"},
260 {kFile, "stateful_trim_data", "/var/lib/trim/stateful_trim_data"},
261 {kFile, "ui_log", "/var/log/ui/ui.LATEST"},
262 {kCommand, "uname", "/bin/uname -a"},
263 {kCommand, "update_engine.log",
264 "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
265 " s.^./var/log/update_engine/.)"},
266 {kCommand, "uptime", "/usr/bin/cut -d' ' -f1 /proc/uptime"},
267 {kFile, "verified boot", "/var/log/debug_vboot_noisy.log"},
268 {kFile, "vmlog.1.LATEST", "/var/log/vmlog/vmlog.1.LATEST"},
269 {kFile, "vmlog.1.PREVIOUS", "/var/log/vmlog/vmlog.1.PREVIOUS"},
270 {kFile, "vmlog.LATEST", "/var/log/vmlog/vmlog.LATEST"},
271 {kFile, "vmlog.PREVIOUS", "/var/log/vmlog/vmlog.PREVIOUS"},
272 {kFile, "vmstat", "/proc/vmstat"},
273 {kFile, "vpd_2.0", "/var/log/vpd_2.0.txt"},
Chris Morin853d3442019-04-01 21:35:13 -0700274 {kFile, "zram compressed data size", "/sys/block/zram0/compr_data_size"},
275 {kFile, "zram original data size", "/sys/block/zram0/orig_data_size"},
276 {kFile, "zram total memory used", "/sys/block/zram0/mem_used_total"},
277 {kFile, "zram total reads", "/sys/block/zram0/num_reads"},
278 {kFile, "zram total writes", "/sys/block/zram0/num_writes"},
279 {kCommand, "zram new stats names",
280 "echo orig_size compr_size used_total limit used_max zero_pages migrated"},
281 {kFile, "zram new stats values", "/sys/block/zram0/mm_stat"},
282 {kFile, "cros_tp version", "/sys/class/chromeos/cros_tp/version"},
283 {kCommand, "cros_tp console", "/usr/sbin/ectool --name=cros_tp console",
284 kRoot, kRoot},
285 {kCommand, "cros_tp frame", "/usr/sbin/ectool --name=cros_tp tpframeget",
286 kRoot, kRoot},
287 {kCommand, "crostini", "/usr/bin/cicerone_client --get_info"},
Elly Jones03cd6d72012-06-11 13:04:28 -0400288 // Stuff pulled out of the original list. These need access to the running X
289 // session, which we'd rather not give to debugd, or return info specific to
290 // the current session (in the setsid(2) sense), which is not useful for
291 // debugd
Chris Morin853d3442019-04-01 21:35:13 -0700292 // {kCommand, "env", "set"},
293 // {kCommand, "setxkbmap", "/usr/bin/setxkbmap -print -query"},
294 // {kCommand, "xrandr", "/usr/bin/xrandr --verbose}
Elly Jones533c7c42012-08-10 15:07:05 -0400295};
296
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700297// netstat and logcat should appear in chrome://system but not in feedback
298// reports. Open sockets may have privacy implications, and logcat is
299// already incorporated via arc-bugreport.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700300const std::vector<Log> kExtraLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800301#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700302 {kCommand, "mm-status", "/usr/bin/modem status"},
Ben Chan36e42282014-02-12 22:32:34 -0800303#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700304 {kCommand, "network-devices", "/usr/bin/connectivity show devices"},
305 {kCommand, "network-services", "/usr/bin/connectivity show services"},
Brian Norris2b290502019-04-23 17:17:10 -0700306 {kCommand, "wifi_status_show_macs",
307 "/usr/bin/network_diag --wifi-internal --no-log --show-macs"},
Chris Morin253a2b02019-04-12 16:04:25 -0700308 // --processes requires root.
309 {kCommand, "netstat",
310 "/sbin/ss --all --query inet --numeric --processes", kRoot, kRoot},
311 {kCommand, "logcat", "/usr/sbin/android-sh -c '/system/bin/logcat -d'",
312 kRoot, kRoot, Log::kDefaultMaxBytes, LogTool::Encoding::kUtf8},
Elly Jones533c7c42012-08-10 15:07:05 -0400313};
314
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700315const std::vector<Log> kFeedbackLogs {
Ben Chan36e42282014-02-12 22:32:34 -0800316#if USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700317 {kCommand, "mm-status", "/usr/bin/modem status-feedback"},
Ben Chan36e42282014-02-12 22:32:34 -0800318#endif // USE_CELLULAR
Chris Morin853d3442019-04-01 21:35:13 -0700319 {kCommand, "network-devices",
320 "/usr/bin/connectivity show-feedback devices"},
321 {kCommand, "network-services",
322 "/usr/bin/connectivity show-feedback services"},
Brian Norris2b290502019-04-23 17:17:10 -0700323 {kCommand, "wifi_status", "/usr/bin/network_diag --wifi-internal --no-log"},
Elly Jones03cd6d72012-06-11 13:04:28 -0400324};
325
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700326// List of log files needed to be part of the feedback report that are huge and
327// must be sent back to the client via the file descriptor using
328// LogTool::GetBigFeedbackLogs().
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700329const std::vector<Log> kBigFeedbackLogs{
Fletcher Woodruffc9f36a72019-03-20 08:05:15 -0600330 // ARC bugreport permissions are weird. Since we're just running cat,
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700331 // this shouldn't cause any issues.
Chris Morin853d3442019-04-01 21:35:13 -0700332 {kCommand, "arc-bugreport", "cat /run/arc/bugreport/pipe 2>/dev/null",
333 kRoot, kRoot, 10 * 1024 * 1024, LogTool::Encoding::kUtf8},
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700334};
335
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700336// List of log files that must directly be collected by Chrome. This is because
337// debugd is running under a VFS namespace and does not have access to later
338// cryptohome mounts.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700339using UserLog = std::pair<std::string, std::string>;
340const std::vector<UserLog> kUserLogs = {
Chris Morin853d3442019-04-01 21:35:13 -0700341 {"chrome_user_log", "log/chrome"},
342 {"libassistant_user_log", "log/libassistant.log"},
343 {"login-times", "login-times"},
344 {"logout-times", "logout-times"},
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700345};
346
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800347// Fills |dictionary| with the anonymized contents of the logs in |logs|.
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700348void GetLogsInDictionary(const std::vector<Log>& logs,
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800349 AnonymizerTool* anonymizer,
350 base::DictionaryValue* dictionary) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700351 for (const Log& log : logs) {
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800352 dictionary->SetStringWithoutPathExpansion(
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700353 log.GetName(), anonymizer->Anonymize(log.GetLogData()));
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800354 }
355}
356
357// Serializes the |dictionary| into the file with the given |fd| in a JSON
358// format.
359void SerializeLogsAsJSON(const base::DictionaryValue& dictionary,
Eric Caruso0b241882018-04-04 13:43:46 -0700360 const base::ScopedFD& fd) {
Eric Caruso96d03d32017-04-25 18:01:17 -0700361 string logs_json;
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800362 base::JSONWriter::WriteWithOptions(dictionary,
363 base::JSONWriter::OPTIONS_PRETTY_PRINT,
364 &logs_json);
Eric Caruso0b241882018-04-04 13:43:46 -0700365 base::WriteFileDescriptor(fd.get(), logs_json.c_str(), logs_json.size());
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800366}
367
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700368bool GetNamedLogFrom(const string& name,
369 const std::vector<Log>& logs,
Elly Jones533c7c42012-08-10 15:07:05 -0400370 string* result) {
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700371 for (const Log& log : logs) {
372 if (name == log.GetName()) {
373 *result = log.GetLogData();
Elly Jones533c7c42012-08-10 15:07:05 -0400374 return true;
375 }
376 }
377 *result = "<invalid log name>";
378 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400379}
380
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700381void GetLogsFrom(const std::vector<Log>& logs, LogTool::LogMap* map) {
382 for (const Log& log : logs)
383 (*map)[log.GetName()] = log.GetLogData();
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800384}
385
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600386void GetLsbReleaseInfo(LogTool::LogMap* map) {
387 const base::FilePath lsb_release(kLsbReleasePath);
388 brillo::KeyValueStore store;
389 if (!store.Load(lsb_release)) {
390 // /etc/lsb-release might not be present (cros deploying a new
391 // configuration or no fields set at all). Just print a debug
392 // message and continue.
393 DLOG(INFO) << "Could not load fields from " << lsb_release.value();
394 } else {
395 for (const auto& key : store.GetKeys()) {
396 std::string value;
397 store.GetString(key, &value);
398 (*map)[key] = value;
399 }
400 }
401}
402
403void GetOsReleaseInfo(LogTool::LogMap* map) {
404 brillo::OsReleaseReader reader;
405 reader.Load();
406 for (const auto& key : reader.GetKeys()) {
407 std::string value;
408 reader.GetString(key, &value);
409 (*map)["os-release " + key] = value;
410 }
411}
412
413void PopulateDictionaryValue(const LogTool::LogMap& map,
414 base::DictionaryValue* dictionary) {
415 for (const auto& kv : map) {
416 dictionary->SetString(kv.first, kv.second);
417 }
418}
419
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800420bool CompressXzBuffer(const std::vector<uint8_t>& in_buffer,
421 std::vector<uint8_t>* out_buffer) {
422 size_t out_size = lzma_stream_buffer_bound(in_buffer.size());
423 out_buffer->resize(out_size);
424 size_t out_pos = 0;
425
426 lzma_ret ret = lzma_easy_buffer_encode(
427 LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64, nullptr, in_buffer.data(),
428 in_buffer.size(), out_buffer->data(), &out_pos, out_size);
429
430 if (ret != LZMA_OK) {
431 out_buffer->clear();
432 return false;
433 }
434
435 out_buffer->resize(out_pos);
436 return true;
437}
438
439void GetPerfData(LogTool::LogMap* map) {
440 // Run perf to collect system-wide performance profile when user triggers
441 // feedback report. Perf runs at sampling frequency of ~500 hz (499 is used
442 // to avoid sampling periodic system activities), with callstack in each
443 // sample (-g).
444 std::vector<std::string> perf_args = {
445 "perf", "record", "-a", "-g", "-F", "499"
446 };
447 std::vector<uint8_t> perf_data;
448 int32_t status;
449
450 debugd::PerfTool perf_tool;
451 if (!perf_tool.GetPerfOutput(kPerfDurationSecs, perf_args, &perf_data,
452 nullptr, &status, nullptr))
453 return;
454
455 // XZ compress the profile data.
456 std::vector<uint8_t> perf_data_xz;
457 if (!CompressXzBuffer(perf_data, &perf_data_xz))
458 return;
459
460 // Base64 encode the compressed data.
461 std::string perf_data_str(reinterpret_cast<const char*>(perf_data_xz.data()),
462 perf_data_xz.size());
463 (*map)["perf-data"] =
Chinglin Yu3c8d0a22019-02-20 11:32:52 +0800464 std::string(kPerfDataDescription) +
Chris Morin790fd262019-04-03 20:29:36 -0700465 LogTool::EncodeString(std::move(perf_data_str),
466 LogTool::Encoding::kBase64);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800467}
468
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800469} // namespace
470
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700471Log::Log(Log::LogType type,
472 std::string name,
473 std::string data,
474 std::string user,
475 std::string group,
476 int64_t max_bytes,
477 LogTool::Encoding encoding)
478 : type_(type),
479 name_(name),
480 data_(data),
481 user_(user),
482 group_(group),
483 max_bytes_(max_bytes),
484 encoding_(encoding) {}
485
486std::string Log::GetName() const {
487 return name_;
488}
489
490std::string Log::GetLogData() const {
491 // The reason this code uses a switch statement on a type enum rather than
492 // using inheritance/virtual dispatch is so that all of the Log objects can
493 // be constructed statically. Switching to heap allocated subclasses of Log
494 // makes the code that declares all of the log entries much more verbose
495 // and harder to understand.
Chris Morin790fd262019-04-03 20:29:36 -0700496 std::string output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700497 switch (type_) {
498 case kCommand:
Chris Morin790fd262019-04-03 20:29:36 -0700499 output = GetCommandLogData();
500 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700501 case kFile:
Chris Morin790fd262019-04-03 20:29:36 -0700502 output = GetFileLogData();
503 break;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700504 default:
505 return "<unknown log type>";
506 }
Chris Morin790fd262019-04-03 20:29:36 -0700507
508 if (output.empty())
509 return "<empty>";
510
511 return LogTool::EncodeString(std::move(output), encoding_);
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700512}
513
514// TODO(ellyjones): sandbox. crosbug.com/35122
515std::string Log::GetCommandLogData() const {
516 if (type_ != kCommand)
517 return "<log type mismatch>";
518 std::string tailed_cmdline =
519 base::StringPrintf("%s | tail -c %" PRId64, data_.c_str(), max_bytes_);
520 ProcessWithOutput p;
521 if (minijail_disabled_for_test_)
522 p.set_use_minijail(false);
523 if (!user_.empty() && !group_.empty())
524 p.SandboxAs(user_, group_);
525 if (!p.Init())
526 return "<not available>";
527 p.AddArg(kShell);
528 p.AddStringOption("-c", tailed_cmdline);
529 if (p.Run())
530 return "<not available>";
531 std::string output;
532 p.GetOutput(&output);
Chris Morin790fd262019-04-03 20:29:36 -0700533 return output;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700534}
535
536std::string Log::GetFileLogData() const {
537 if (type_ != kFile)
538 return "<log type mismatch>";
539
540 uid_t old_euid = geteuid();
541 uid_t new_euid = UidForUser(user_);
542 gid_t old_egid = getegid();
543 gid_t new_egid = GidForGroup(group_);
544
545 if (new_euid == -1 || new_egid == -1) {
546 return "<not available>";
547 }
548
549 // Make sure to set group first, since if we set user first we lose root
550 // and therefore the ability to set our effective gid to arbitrary gids.
551 if (setegid(new_egid)) {
552 PLOG(ERROR) << "Failed to set effective group id to " << new_egid;
553 return "<not available>";
554 }
555 if (seteuid(new_euid)) {
556 PLOG(ERROR) << "Failed to set effective user id to " << new_euid;
557 if (setegid(old_egid))
558 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
559 return "<not available>";
560 }
561
562 std::string contents;
563 const base::FilePath path(data_);
564 // Handle special files that don't properly report length/allow lseek.
565 if (base::FilePath("/dev").IsParent(path) ||
566 base::FilePath("/proc").IsParent(path) ||
567 base::FilePath("/sys").IsParent(path)) {
568 if (!base::ReadFileToString(path, &contents))
569 contents = "<not available>";
570 if (contents.size() > max_bytes_)
571 contents.erase(0, contents.size() - max_bytes_);
572 } else {
573 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
574 if (!file.IsValid()) {
575 contents = "<not available>";
576 } else {
577 int64_t length = file.GetLength();
578 if (length > max_bytes_) {
579 file.Seek(base::File::FROM_END, -max_bytes_);
580 length = max_bytes_;
581 }
582 std::vector<char> buf(length);
583 int read = file.ReadAtCurrentPos(buf.data(), buf.size());
584 if (read < 0) {
585 PLOG(ERROR) << "Could not read from file " << path.value();
586 } else {
587 contents = std::string(buf.begin(), buf.begin() + read);
588 }
589 }
590 }
591
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700592 // Make sure we restore our old euid/egid before returning.
593 if (seteuid(old_euid))
594 PLOG(ERROR) << "Failed to restore effective user id to " << old_euid;
595
596 if (setegid(old_egid))
597 PLOG(ERROR) << "Failed to restore effective group id to " << old_egid;
598
Chris Morin790fd262019-04-03 20:29:36 -0700599 return contents;
Fletcher Woodruff07c28532019-01-24 11:08:53 -0700600}
601
602void Log::DisableMinijailForTest() {
603 minijail_disabled_for_test_ = true;
604}
605
606// static
607uid_t Log::UidForUser(const std::string& user) {
608 struct passwd entry;
609 struct passwd* result;
610 std::vector<char> buf(1024);
611 getpwnam_r(user.c_str(), &entry, &buf[0], buf.size(), &result);
612 if (!result) {
613 LOG(ERROR) << "User not found: " << user;
614 return -1;
615 }
616 return entry.pw_uid;
617}
618
619// static
620gid_t Log::GidForGroup(const std::string& group) {
621 struct group entry;
622 struct group* result;
623 std::vector<char> buf(1024);
624 getgrnam_r(group.c_str(), &entry, &buf[0], buf.size(), &result);
625 if (!result) {
626 LOG(ERROR) << "Group not found: " << group;
627 return -1;
628 }
629 return entry.gr_gid;
630}
631
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700632void LogTool::CreateConnectivityReport(bool wait_for_results) {
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700633 // Perform ConnectivityTrial to report connection state in feedback log.
Ben Chan8e9f6d02017-09-26 23:04:21 -0700634 auto shill = std::make_unique<org::chromium::flimflam::ManagerProxy>(bus_);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700635 // Give the connection trial time to test the connection and log the results
636 // before collecting the logs for feedback.
637 // TODO(silberst): Replace the simple approach of a single timeout with a more
638 // coordinated effort.
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700639 if (shill && shill->CreateConnectivityReport(nullptr) && wait_for_results)
Eric Carusocc7106c2017-04-27 14:22:42 -0700640 sleep(kConnectionTesterTimeoutSeconds);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700641}
642
Eric Carusoc93a15c2017-04-24 16:15:12 -0700643string LogTool::GetLog(const string& name) {
Elly Jones533c7c42012-08-10 15:07:05 -0400644 string result;
Ben Chancf7d6412017-08-10 22:30:09 -0700645 GetNamedLogFrom(name, kCommandLogs, &result)
646 || GetNamedLogFrom(name, kExtraLogs, &result)
647 || GetNamedLogFrom(name, kFeedbackLogs, &result);
Elly Jones533c7c42012-08-10 15:07:05 -0400648 return result;
649}
650
Eric Carusof9091f82017-04-28 14:18:59 -0700651LogTool::LogMap LogTool::GetAllLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700652 CreateConnectivityReport(false);
Elly Jones533c7c42012-08-10 15:07:05 -0400653 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700654 GetLogsFrom(kCommandLogs, &result);
655 GetLogsFrom(kExtraLogs, &result);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600656 GetLsbReleaseInfo(&result);
657 GetOsReleaseInfo(&result);
Elly Jones533c7c42012-08-10 15:07:05 -0400658 return result;
659}
660
Brian Norrisca4fc042018-04-03 00:24:26 -0700661LogTool::LogMap LogTool::GetAllDebugLogs() {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700662 CreateConnectivityReport(true);
Brian Norrisca4fc042018-04-03 00:24:26 -0700663 LogMap result;
664 GetLogsFrom(kCommandLogs, &result);
665 GetLogsFrom(kExtraLogs, &result);
666 GetLogsFrom(kBigFeedbackLogs, &result);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600667 GetLsbReleaseInfo(&result);
668 GetOsReleaseInfo(&result);
Brian Norrisca4fc042018-04-03 00:24:26 -0700669 return result;
670}
671
Eric Caruso0b241882018-04-04 13:43:46 -0700672void LogTool::GetBigFeedbackLogs(const base::ScopedFD& fd) {
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700673 CreateConnectivityReport(true);
Chinglin Yuaeb4ec72018-12-10 18:53:30 +0800674 LogMap map;
675 GetPerfData(&map);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800676 base::DictionaryValue dictionary;
Ben Chancf7d6412017-08-10 22:30:09 -0700677 GetLogsInDictionary(kCommandLogs, &anonymizer_, &dictionary);
678 GetLogsInDictionary(kFeedbackLogs, &anonymizer_, &dictionary);
679 GetLogsInDictionary(kBigFeedbackLogs, &anonymizer_, &dictionary);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600680 GetLsbReleaseInfo(&map);
681 GetOsReleaseInfo(&map);
682 PopulateDictionaryValue(map, &dictionary);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800683 SerializeLogsAsJSON(dictionary, fd);
684}
685
Chris Morin790fd262019-04-03 20:29:36 -0700686void LogTool::GetJournalLog(bool scrub, const base::ScopedFD& fd) {
687 Log journal(kCommand, "journal.export", "journalctl -n 10000 -o export",
688 "syslog", "syslog", 10 * 1024 * 1024, LogTool::Encoding::kBinary);
689 std::string output = scrub ? anonymizer_.Anonymize(journal.GetLogData())
690 : journal.GetLogData();
691 base::WriteFileDescriptor(fd.get(), output.data(), output.size());
692}
693
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700694// static
Chris Morin790fd262019-04-03 20:29:36 -0700695string LogTool::EncodeString(string value,
696 LogTool::Encoding source_encoding) {
697 if (source_encoding == LogTool::Encoding::kBinary)
698 return value;
699
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700700 if (source_encoding == LogTool::Encoding::kAutodetect) {
701 if (base::IsStringUTF8(value))
702 return value;
Chris Morin790fd262019-04-03 20:29:36 -0700703 source_encoding = LogTool::Encoding::kBase64;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700704 }
705
706 if (source_encoding == LogTool::Encoding::kUtf8) {
707 string output;
708 const char* src = value.data();
709 int32_t src_len = static_cast<int32_t>(value.length());
710
711 output.reserve(value.size());
712 for (int32_t char_index = 0; char_index < src_len; char_index++) {
713 uint32_t code_point;
714 if (!base::ReadUnicodeCharacter(src, src_len, &char_index, &code_point) ||
715 !base::IsValidCharacter(code_point)) {
716 // Replace invalid characters with U+FFFD REPLACEMENT CHARACTER.
717 code_point = 0xFFFD;
718 }
719 base::WriteUnicodeCharacter(code_point, &output);
720 }
721 return output;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700722 }
Chris Morin853d3442019-04-01 21:35:13 -0700723
724 base::Base64Encode(value, &value);
725 return "<base64>: " + value;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700726}
727
Darin Petkovce9b3a12013-01-10 16:38:54 +0100728void LogTool::AnonymizeLogMap(LogMap* log_map) {
Ben Chane2fa3572017-02-08 22:46:18 -0800729 for (auto& entry : *log_map)
730 entry.second = anonymizer_.Anonymize(entry.second);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100731}
732
Ben Chana0011d82014-05-13 00:19:29 -0700733} // namespace debugd