blob: 08703fad4bfc645c8a2e1dced05fc0ddf8c01afc [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
Ben Chan8e9f6d02017-09-26 23:04:21 -07007#include <memory>
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -07008#include <string>
Ben Chana0011d82014-05-13 00:19:29 -07009#include <vector>
10
Ben Chanab93abf2017-01-24 13:32:51 -080011#include <base/base64.h>
Ben Chancd8fda42014-09-05 08:21:06 -070012#include <base/files/file_util.h>
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080013#include <base/json/json_writer.h>
Elly Jones03cd6d72012-06-11 13:04:28 -040014#include <base/logging.h>
Ben Chan9953a592014-02-05 23:32:00 -080015#include <base/strings/string_split.h>
16#include <base/strings/string_util.h>
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -070017#include <base/strings/stringprintf.h>
18#include <base/strings/utf_string_conversion_utils.h>
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080019#include <base/values.h>
Elly Jones03cd6d72012-06-11 13:04:28 -040020
Rebecca Silbersteine78af402014-10-02 10:55:04 -070021#include <chromeos/dbus/service_constants.h>
Eric Carusocc7106c2017-04-27 14:22:42 -070022#include <shill/dbus-proxies.h>
Rebecca Silbersteine78af402014-10-02 10:55:04 -070023
Ben Chanaf125862017-02-08 23:11:18 -080024#include "debugd/src/constants.h"
Alex Vakulenko262be3f2014-07-30 15:25:50 -070025#include "debugd/src/process_with_output.h"
Elly Jones03cd6d72012-06-11 13:04:28 -040026
27namespace debugd {
28
Elly Jones03cd6d72012-06-11 13:04:28 -040029using std::string;
Elly Jones03cd6d72012-06-11 13:04:28 -040030
Eric Caruso96d03d32017-04-25 18:01:17 -070031using Strings = std::vector<string>;
Elly Jones03cd6d72012-06-11 13:04:28 -040032
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080033namespace {
34
Ben Chanaf125862017-02-08 23:11:18 -080035const char kRoot[] = "root";
36const char kShell[] = "/bin/sh";
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080037
38// Minimum time in seconds needed to allow shill to test active connections.
39const int kConnectionTesterTimeoutSeconds = 5;
Ben Chanf6cd93a2012-10-14 19:37:00 -070040
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -070041// Default maximum size of a log entry.
42constexpr const char kDefaultSizeCap[] = "512K";
43
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050044struct Log {
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -070045 const char* name;
46 const char* command;
47 const char* user = nullptr;
48 const char* group = nullptr;
49 const char* size_cap = kDefaultSizeCap; // passed as arg to 'tail -c'
50 LogTool::Encoding encoding = LogTool::Encoding::kAutodetect;
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050051};
52
Eric Carusoa879fd92017-10-11 12:57:10 -070053#define CMD_KERNEL_MODULE_PARAMS(module_name) \
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070054 "cd /sys/module/" #module_name "/parameters 2>/dev/null && grep -sH ^ *"
Eric Carusoa879fd92017-10-11 12:57:10 -070055
Ben Chancf7d6412017-08-10 22:30:09 -070056const Log kCommandLogs[] = {
Mike Frysingerb0350992018-09-14 13:45:35 -040057 // We need to enter init's mount namespace because it has /home/chronos
58 // mounted which is where the consent knob lives. We don't have that mount
59 // in our own mount namespace (by design). https://crbug.com/884249
60 { "CLIENT_ID", "/usr/bin/nsenter -t1 -m /usr/bin/metrics_client -i",
61 kRoot,
62 kDebugfsGroup,
63 },
Elly Jones03cd6d72012-06-11 13:04:28 -040064 { "LOGDATE", "/bin/date" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070065 { "atrus_logs", "cat /var/log/atrus.log 2>/dev/null" },
66 { "authpolicy", "cat /var/log/authpolicy.log" },
Nicolas Norvezd573ff62018-08-08 18:30:13 -070067 { "biod.LATEST", "cat /var/log/biod/biod.LATEST" },
68 { "biod.PREVIOUS", "cat /var/log/biod/biod.PREVIOUS" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070069 { "bios_info", "cat /var/log/bios_info.txt" },
Vadim Bendebury64aeeed2013-09-16 13:16:49 -070070 { "bios_log",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070071 "cat /sys/firmware/log "
72 "/proc/device-tree/chosen/ap-console-buffer 2>/dev/null" },
73 { "bios_times", "cat /var/log/bios_times.txt" },
Elly Jones03cd6d72012-06-11 13:04:28 -040074 { "board-specific",
Thiemo Nagela269ad22014-11-27 17:13:01 +010075 "/usr/share/userfeedback/scripts/get_board_specific_info" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070076 { "buddyinfo", "cat /proc/buddyinfo" },
Daisuke Nojiri3d832ee2017-02-17 14:21:28 -080077 { "cbi_info", "/usr/share/userfeedback/scripts/cbi_info", kRoot, },
Olof Johanssone5d0fd32016-01-29 14:40:34 -080078 { "cheets_log", "/usr/bin/collect-cheets-logs 2>&1" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070079 { "clobber.log", "cat /var/log/clobber.log 2>/dev/null" },
80 { "clobber-state.log", "cat /var/log/clobber-state.log 2>/dev/null" },
81 { "chrome_system_log", "cat /var/log/chrome/chrome" },
Mike Frysinger32cdf3e2017-08-14 18:17:06 -040082 // There might be more than one record, so grab them all.
83 // Plus, for <linux-3.19, it's named "console-ramoops", but for newer
84 // versions, it's named "console-ramoops-#".
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070085 { "console-ramoops", "cat /sys/fs/pstore/console-ramoops* 2>/dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -040086 { "cpu", "/usr/bin/uname -p" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070087 { "cpuinfo", "cat /proc/cpuinfo" },
88 { "cr50_version", "cat /var/cache/cr50-version" },
Eric Caruso57333f12015-09-08 12:50:32 -070089 { "cros_ec",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070090 "cat /var/log/cros_ec.previous /var/log/cros_ec.log 2>/dev/null" },
Nicolas Boichat083795f2016-08-16 11:16:49 +020091 { "cros_ec_panicinfo",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070092 "cat /sys/kernel/debug/cros_ec/panicinfo 2>/dev/null",
Nicolas Boichatd51092b2016-09-01 10:36:46 +080093 SandboxedProcess::kDefaultUser,
94 kDebugfsGroup
95 },
Shawn Nematbakhsh60813732017-07-13 10:46:23 -070096 { "cros_ec_pdinfo",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070097 "cat /sys/kernel/debug/cros_ec/pdinfo 2>/dev/null",
Shawn Nematbakhsh60813732017-07-13 10:46:23 -070098 SandboxedProcess::kDefaultUser,
99 kDebugfsGroup
100 },
Vincent Palatine3588042018-04-04 17:02:38 +0200101 { "cros_fp",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700102 "cat /var/log/cros_fp.previous /var/log/cros_fp.log 2>/dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400103 { "dmesg", "/bin/dmesg" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700104 { "ec_info", "cat /var/log/ec_info.txt" },
Miguel Casas8d4e54f2018-03-29 09:42:52 -0400105 // The sed command replaces the EDID serial number (4 bytes at position 12)
106 // with zeroes. See https://en.wikipedia.org/wiki/EDID#EDID_1.4_data_format.
107 { "edid-decode",
Miguel Casas55b7c842018-08-15 13:10:10 -0400108 "for f in /sys/class/drm/card0-*/edid; do "
Miguel Casas8d4e54f2018-03-29 09:42:52 -0400109 "echo \"----------- ${f}\"; "
Miguel Casas1b8d2792018-08-17 20:00:52 -0400110 "sed -E 's/^(.{11}).{4}/\\1\\x0\\x0\\x0\\x0/' \"${f}\" | "
111 // edid-decode's stderr output is redundant, so silence it.
112 "edid-decode 2>/dev/null; "
Miguel Casas8d4e54f2018-03-29 09:42:52 -0400113 "done"
114 },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700115 { "eventlog", "cat /var/log/eventlog.txt" },
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400116 {
117 "exynos_gem_objects",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700118 "cat /sys/kernel/debug/dri/0/exynos_gem_objects 2>/dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400119 SandboxedProcess::kDefaultUser,
120 kDebugfsGroup
121 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400122 { "font_info", "/usr/share/userfeedback/scripts/font_info" },
Daisuke Nojiridebede0e2017-02-17 14:21:28 -0800123 { "sensor_info", "/usr/share/userfeedback/scripts/sensor_info" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400124 { "hardware_class", "/usr/bin/crossystem hwid" },
125 { "hostname", "/bin/hostname" },
126 { "hw_platform", "/usr/bin/uname -i" },
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400127 {
128 "i915_gem_gtt",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700129 "cat /sys/kernel/debug/dri/0/i915_gem_gtt 2>/dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400130 SandboxedProcess::kDefaultUser,
131 kDebugfsGroup
132 },
133 {
134 "i915_gem_objects",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700135 "cat /sys/kernel/debug/dri/0/i915_gem_objects 2>/dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400136 SandboxedProcess::kDefaultUser,
137 kDebugfsGroup
138 },
139 {
140 "i915_error_state",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700141 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2>/dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400142 SandboxedProcess::kDefaultUser,
143 kDebugfsGroup,
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700144 kDefaultSizeCap,
145 LogTool::Encoding::kBinary,
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400146 },
Daniel Erat982ab4b2014-04-09 07:32:38 -0700147 { "ifconfig", "/bin/ifconfig -a" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700148 { "input_devices", "cat /proc/bus/input/devices" },
Eric Carusob1820c02017-08-24 15:39:56 -0700149 // Information about the wiphy device, such as current channel.
150 { "iw_dev", "/usr/sbin/iw dev" },
151 // Hardware capabilities of the wiphy device.
152 { "iw_list", "/usr/sbin/iw list" },
Eric Carusoa879fd92017-10-11 12:57:10 -0700153#if USE_IWLWIFI_DUMP
154 { "iwlmvm_module_params", CMD_KERNEL_MODULE_PARAMS(iwlmvm) },
155 { "iwlwifi_module_params", CMD_KERNEL_MODULE_PARAMS(iwlwifi) },
156#endif // USE_IWLWIFI_DUMP
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700157 { "kernel-crashes", "cat /var/spool/crash/kernel.*.kcrash 2>/dev/null" },
158 { "logcat",
159 "/usr/sbin/android-sh -c '/system/bin/logcat -d'",
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700160 kRoot,
161 kRoot,
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700162 kDefaultSizeCap,
163 LogTool::Encoding::kUtf8,
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700164 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400165 { "lsmod", "lsmod" },
166 { "lspci", "/usr/sbin/lspci" },
Simon Que41c88b52017-06-19 14:05:07 -0400167 { "lsusb", "lsusb && lsusb -t" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700168 { "mali_memory", "cat /sys/class/misc/mali0/device/memory 2>/dev/null" },
Luigi Semenzato7f00dfb2018-06-26 11:34:02 -0700169 { "memd.parameters", "cat /var/log/memd/memd.parameters 2>/dev/null" },
170 { "memd clips", "cat /var/log/memd/memd.clip* 2>/dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400171 { "meminfo", "cat /proc/meminfo" },
Mike Frysinger8fd2eee2017-11-03 14:35:26 -0400172 {
173 "memory_spd_info",
174 // mosys may use 'i2c-dev', which may not be loaded yet.
175 "modprobe i2c-dev 2>/dev/null && "
176 "mosys -l memory spd print all 2>/dev/null",
177 kRoot,
178 kDebugfsGroup,
179 },
Simon Quecb63b9c2017-06-19 14:53:31 -0400180 // The sed command finds the EDID blob (starting the line after "value:") and
181 // replaces the serial number with all zeroes.
182 //
183 // The EDID is printed as a hex dump over several lines, each line containing
184 // the contents of 16 bytes. The first 16 bytes are broken down as follows:
185 // uint64_t fixed_pattern; // Always 00 FF FF FF FF FF FF 00.
186 // uint16_t manufacturer_id; // Manufacturer ID, encoded as PNP IDs.
187 // uint16_t product_code; // Manufacturer product code, little-endian.
188 // uint32_t serial_number; // Serial number, little-endian.
189 // Source: https://en.wikipedia.org/wiki/EDID#EDID_1.3_data_format
190 //
191 // The subsequent substitution command looks for the fixed pattern followed by
192 // two 32-bit fields (manufacturer + product, serial number). It replaces the
193 // latter field with 8 bytes of zeroes.
194 //
195 // TODO(crbug.com/731133): Remove the sed command once modetest itself can
196 // remove serial numbers.
197 {
198 "modetest",
199 "(modetest; modetest -M evdi; modetest -M udl) | "
200 "sed -E '/EDID/ {:a;n;/value:/!ba;n;"
201 "s/(00f{12}00)([0-9a-f]{8})([0-9a-f]{8})/\\1\\200000000/}'",
202 kRoot,
203 kRoot,
204 },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700205 { "mount-encrypted", "cat /var/log/mount-encrypted.log" },
206 { "mountinfo", "cat /proc/1/mountinfo" },
Nicolas Boichat0230b0b2017-08-17 11:20:19 +0800207 { "netlog", "/usr/share/userfeedback/scripts/getmsgs /var/log/net.log" },
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700208 // --processes requires root.
209 { "netstat", "/sbin/ss --all --query inet --numeric --processes",
210 kRoot,
211 kRoot,
212 },
henryhsufa6daa12014-09-24 13:26:30 +0800213 {
214 "nvmap_iovmm",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700215 "cat /sys/kernel/debug/nvmap/iovmm/allocations 2>/dev/null",
henryhsufa6daa12014-09-24 13:26:30 +0800216 SandboxedProcess::kDefaultUser,
217 kDebugfsGroup,
218 },
Todd Brocha0c13762018-05-03 11:31:25 -0700219 { "oemdata", "/usr/share/cros/oemdata.sh", kRoot, kRoot, },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700220 { "pagetypeinfo", "cat /proc/pagetypeinfo" },
Mike Frysinger8fd2eee2017-11-03 14:35:26 -0400221 {
222 "platform_info",
223 // mosys may use 'i2c-dev', which may not be loaded yet.
224 "modprobe i2c-dev 2>/dev/null && "
225 "for param in "
226 "vendor "
227 "name "
228 "version "
229 "family "
230 "model "
231 "sku "
232 "customization "
233 "; do "
234 "mosys -l platform \"${param}\" 2>/dev/null; "
235 "done",
236 kRoot,
237 kDebugfsGroup,
238 },
Daniel Erat9b58b6d2013-04-30 14:58:56 -0700239 { "power_supply_info", "/usr/bin/power_supply_info" },
Daniel Eratd8b84992017-03-13 17:30:00 -0600240 { "power_supply_sysfs", "/usr/bin/print_sysfs_power_supply_data" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700241 { "powerd.LATEST", "cat /var/log/power_manager/powerd.LATEST" },
242 { "powerd.PREVIOUS", "cat /var/log/power_manager/powerd.PREVIOUS" },
243 { "powerd.out", "cat /var/log/powerd.out" },
244 { "powerwash_count", "cat /var/log/powerwash_count 2>/dev/null" },
Mattias Nissler887dce22017-07-03 14:44:35 +0200245 // Changed from 'ps ux' to 'ps aux' since we're running as debugd,
246 // not chronos.
Elly Jones03cd6d72012-06-11 13:04:28 -0400247 { "ps", "/bin/ps aux" },
yusukes34171ba2017-04-27 15:46:01 -0700248 // /proc/slabinfo is owned by root and has 0400 permission.
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700249 { "slabinfo", "cat /proc/slabinfo", kRoot, kRoot, },
250 { "storage_info", "cat /var/log/storage_info.txt" },
Alexis Saverybbb85132018-05-18 17:41:10 -0700251 { "swap_info",
252 "/usr/share/cros/init/swap.sh status 2>/dev/null",
253 SandboxedProcess::kDefaultUser,
254 kDebugfsGroup
255 },
Nicolas Boichat0230b0b2017-08-17 11:20:19 +0800256 { "syslog", "/usr/share/userfeedback/scripts/getmsgs /var/log/messages" },
Brandon Mayere24c7a22017-08-04 13:58:48 -0400257 { "system_log_stats", "echo 'BLOCK_SIZE=1024'; "
258 "find /var/log/ -type f -exec du --block-size=1024 {} + | sort -n -r",
259 kRoot, kRoot},
Kuo-Hsin Yang379846b2018-06-04 11:16:55 +0800260 { "threads", "/bin/ps -T axo pid,ppid,spid,pcpu,ni,stat,time,comm" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700261 { "tlsdate", "cat /var/log/tlsdate.log" },
Hsu-Cheng Tsaie8c31d52017-03-31 12:34:55 +0800262 { "top thread", "/usr/bin/top -Hb -n 1 | head -n 40"},
263 { "top memory", "/usr/bin/top -o \"+%MEM\" -bn 1 | head -n 57"},
Andrew de los Reyesc060c342013-04-10 13:46:30 -0700264 { "touch_fw_version", "grep -E"
Charlie Mooneybb08f982016-02-04 15:35:56 -0800265 " -e 'synaptics: Touchpad model'"
266 " -e 'chromeos-[a-z]*-touch-[a-z]*-update'"
Andrew de los Reyesc060c342013-04-10 13:46:30 -0700267 " /var/log/messages | tail -n 20" },
Mattias Nissler483d76f2017-08-23 16:53:36 +0200268 {
269 "tpm-firmware-updater",
270 "/usr/share/userfeedback/scripts/getmsgs /var/log/tpm-firmware-updater.log"
271 },
Mattias Nissler887dce22017-07-03 14:44:35 +0200272 // TODO(jorgelo,mnissler): Don't run this as root.
273 // On TPM 1.2 devices this will likely require adding a new user to the 'tss'
274 // group.
275 // On TPM 2.0 devices 'get_version_info' uses D-Bus and therefore can run as
276 // any user.
277 { "tpm_version", "/usr/sbin/tpm-manager get_version_info", kRoot, kRoot },
Charlie Mooney69f56702017-05-02 14:55:41 -0700278 { "atmel_ts_refs", "/opt/google/touch/scripts/atmel_tools.sh ts r",
279 kRoot, kRoot},
280 { "atmel_tp_refs", "/opt/google/touch/scripts/atmel_tools.sh tp r",
281 kRoot, kRoot},
282 { "atmel_ts_deltas", "/opt/google/touch/scripts/atmel_tools.sh ts d",
283 kRoot, kRoot},
284 { "atmel_tp_deltas", "/opt/google/touch/scripts/atmel_tools.sh tp d",
285 kRoot, kRoot},
Gwendal Grignou427d94e2016-10-25 11:34:51 -0700286 {
287 "trim",
288 "cat /var/lib/trim/stateful_trim_state /var/lib/trim/stateful_trim_data"
289 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400290 { "ui_log", "/usr/share/userfeedback/scripts/get_log /var/log/ui/ui.LATEST" },
291 { "uname", "/bin/uname -a" },
292 { "update_engine.log", "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
Thiemo Nagela269ad22014-11-27 17:13:01 +0100293 " s.^./var/log/update_engine/.)" },
Dariusz Marcinkiewiczc4126882017-10-13 16:07:36 +0200294 { "uptime", "/usr/bin/cut -d' ' -f1 /proc/uptime" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700295 { "verified boot", "cat /var/log/debug_vboot_noisy.log" },
296 { "vmlog.1.LATEST", "cat /var/log/vmlog/vmlog.1.LATEST" },
297 { "vmlog.1.PREVIOUS", "cat /var/log/vmlog/vmlog.1.PREVIOUS" },
298 { "vmlog.LATEST", "cat /var/log/vmlog/vmlog.LATEST" },
299 { "vmlog.PREVIOUS", "cat /var/log/vmlog/vmlog.PREVIOUS" },
300 { "vmstat", "cat /proc/vmstat" },
301 { "vpd_2.0", "cat /var/log/vpd_2.0.txt" },
Samuel Tan107b6c82014-07-10 15:33:44 -0700302 { "wifi_status", "/usr/bin/network_diag --wifi-internal --no-log" },
Sonny Raoab5f9952013-07-17 14:13:53 -0700303 { "zram compressed data size",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700304 "cat /sys/block/zram0/compr_data_size 2>/dev/null" },
Sonny Raoab5f9952013-07-17 14:13:53 -0700305 { "zram original data size",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700306 "cat /sys/block/zram0/orig_data_size 2>/dev/null" },
Sonny Raoab5f9952013-07-17 14:13:53 -0700307 { "zram total memory used",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700308 "cat /sys/block/zram0/mem_used_total 2>/dev/null" },
Sonny Raoab5f9952013-07-17 14:13:53 -0700309 { "zram total reads",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700310 "cat /sys/block/zram0/num_reads 2>/dev/null" },
Sonny Raoab5f9952013-07-17 14:13:53 -0700311 { "zram total writes",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700312 "cat /sys/block/zram0/num_writes 2>/dev/null" },
313 { "zram new stats names",
314 "echo orig_size compr_size used_total limit "
Luigi Semenzato4164de42017-07-06 10:42:54 -0700315 "used_max zero_pages migrated" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700316 { "zram new stats values", "cat /sys/block/zram0/mm_stat 2>/dev/null" },
317 { "cros_tp version", "cat /sys/class/chromeos/cros_tp/version" },
Wei-Ning Huang230b7d82017-07-14 16:19:13 +0800318 { "cros_tp console", "/usr/sbin/ectool --name=cros_tp console",
319 kRoot, kRoot },
Wei-Ning Huang23ffdcf2017-08-04 11:30:39 +0800320 { "cros_tp frame", "/usr/sbin/ectool --name=cros_tp tpframeget",
321 kRoot, kRoot },
Zach Reizner7eab48b2018-06-22 19:26:48 -0700322 { "crostini", "/usr/bin/cicerone_client --get_info" },
Gaurav Shah6e8fd892012-10-01 11:51:08 -0700323
Elly Jones03cd6d72012-06-11 13:04:28 -0400324 // Stuff pulled out of the original list. These need access to the running X
325 // session, which we'd rather not give to debugd, or return info specific to
326 // the current session (in the setsid(2) sense), which is not useful for
327 // debugd
328 // { "env", "set" },
329 // { "setxkbmap", "/usr/bin/setxkbmap -print -query" },
330 // { "xrandr", "/usr/bin/xrandr --verbose" }
Ben Chan64d19b22017-02-06 14:03:47 -0800331 { nullptr, nullptr }
Elly Jones533c7c42012-08-10 15:07:05 -0400332};
333
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700334// netstat and logcat should appear in chrome://system but not in feedback
335// reports. Open sockets may have privacy implications, and logcat is
336// already incorporated via arc-bugreport.
337const std::vector<string> kCommandLogsExclude = {"netstat", "logcat"};
338
Ben Chancf7d6412017-08-10 22:30:09 -0700339const Log kExtraLogs[] = {
Ben Chan36e42282014-02-12 22:32:34 -0800340#if USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400341 { "mm-status", "/usr/bin/modem status" },
Ben Chan36e42282014-02-12 22:32:34 -0800342#endif // USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400343 { "network-devices", "/usr/bin/connectivity show devices" },
344 { "network-services", "/usr/bin/connectivity show services" },
Ben Chan64d19b22017-02-06 14:03:47 -0800345 { nullptr, nullptr }
Elly Jones533c7c42012-08-10 15:07:05 -0400346};
347
Ben Chancf7d6412017-08-10 22:30:09 -0700348const Log kFeedbackLogs[] = {
Ben Chan36e42282014-02-12 22:32:34 -0800349#if USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400350 { "mm-status", "/usr/bin/modem status-feedback" },
Ben Chan36e42282014-02-12 22:32:34 -0800351#endif // USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400352 { "network-devices", "/usr/bin/connectivity show-feedback devices" },
353 { "network-services", "/usr/bin/connectivity show-feedback services" },
Ben Chan64d19b22017-02-06 14:03:47 -0800354 { nullptr, nullptr }
Elly Jones03cd6d72012-06-11 13:04:28 -0400355};
356
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700357// List of log files needed to be part of the feedback report that are huge and
358// must be sent back to the client via the file descriptor using
359// LogTool::GetBigFeedbackLogs().
Ben Chancf7d6412017-08-10 22:30:09 -0700360const Log kBigFeedbackLogs[] = {
Eric Carusoe3166f22016-06-10 15:36:02 -0700361 { "arc-bugreport",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700362 "cat /run/arc/bugreport/pipe 2>/dev/null",
Eric Carusoe3166f22016-06-10 15:36:02 -0700363 // ARC bugreport permissions are weird. Since we're just running cat, this
364 // shouldn't cause any issues.
365 kRoot,
Ricky Zhou4c473ca2016-06-21 17:46:58 -0700366 kRoot,
367 "10M",
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700368 LogTool::Encoding::kUtf8,
Eric Carusoe3166f22016-06-10 15:36:02 -0700369 },
Ben Chan64d19b22017-02-06 14:03:47 -0800370 { nullptr, nullptr }
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700371};
372
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700373// List of log files that must directly be collected by Chrome. This is because
374// debugd is running under a VFS namespace and does not have access to later
375// cryptohome mounts.
Ben Chancf7d6412017-08-10 22:30:09 -0700376const Log kUserLogs[] = {
Elly Fong-Jones6456ce52013-04-17 13:31:13 -0400377 {"chrome_user_log", "log/chrome"},
Xiaohui Chen32b171d2018-08-27 17:18:44 -0700378 {"libassistant_user_log", "log/libassistant.log"},
Elly Fong-Jones6456ce52013-04-17 13:31:13 -0400379 {"login-times", "login-times"},
380 {"logout-times", "logout-times"},
Ben Chan64d19b22017-02-06 14:03:47 -0800381 { nullptr, nullptr}
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700382};
383
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800384// TODO(ellyjones): sandbox. crosbug.com/35122
385string Run(const Log& log) {
386 string output;
387 ProcessWithOutput p;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700388 string tailed_cmdline =
389 base::StringPrintf("%s | tail -c %s", log.command, log.size_cap);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800390 if (log.user && log.group)
391 p.SandboxAs(log.user, log.group);
392 if (!p.Init())
393 return "<not available>";
394 p.AddArg(kShell);
395 p.AddStringOption("-c", tailed_cmdline);
396 if (p.Run())
397 return "<not available>";
398 p.GetOutput(&output);
399 if (!output.size())
400 return "<empty>";
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700401 return LogTool::EnsureUTF8String(output, log.encoding);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800402}
403
404// Fills |dictionary| with the anonymized contents of the logs in |logs|.
405void GetLogsInDictionary(const struct Log* logs,
406 AnonymizerTool* anonymizer,
407 base::DictionaryValue* dictionary) {
408 for (size_t i = 0; logs[i].name; ++i) {
409 dictionary->SetStringWithoutPathExpansion(
410 logs[i].name, anonymizer->Anonymize(Run(logs[i])));
411 }
412}
413
414// Serializes the |dictionary| into the file with the given |fd| in a JSON
415// format.
416void SerializeLogsAsJSON(const base::DictionaryValue& dictionary,
Eric Caruso0b241882018-04-04 13:43:46 -0700417 const base::ScopedFD& fd) {
Eric Caruso96d03d32017-04-25 18:01:17 -0700418 string logs_json;
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800419 base::JSONWriter::WriteWithOptions(dictionary,
420 base::JSONWriter::OPTIONS_PRETTY_PRINT,
421 &logs_json);
Eric Caruso0b241882018-04-04 13:43:46 -0700422 base::WriteFileDescriptor(fd.get(), logs_json.c_str(), logs_json.size());
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800423}
424
Elly Jones533c7c42012-08-10 15:07:05 -0400425bool GetNamedLogFrom(const string& name, const struct Log* logs,
426 string* result) {
427 for (size_t i = 0; logs[i].name; i++) {
428 if (name == logs[i].name) {
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -0500429 *result = Run(logs[i]);
Elly Jones533c7c42012-08-10 15:07:05 -0400430 return true;
431 }
432 }
433 *result = "<invalid log name>";
434 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400435}
436
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800437void GetLogsFrom(const struct Log* logs, LogTool::LogMap* map) {
438 for (size_t i = 0; logs[i].name; i++)
439 (*map)[logs[i].name] = Run(logs[i]);
440}
441
442} // namespace
443
Eric Carusof9091f82017-04-28 14:18:59 -0700444void LogTool::CreateConnectivityReport() {
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700445 // Perform ConnectivityTrial to report connection state in feedback log.
Ben Chan8e9f6d02017-09-26 23:04:21 -0700446 auto shill = std::make_unique<org::chromium::flimflam::ManagerProxy>(bus_);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700447 // Give the connection trial time to test the connection and log the results
448 // before collecting the logs for feedback.
449 // TODO(silberst): Replace the simple approach of a single timeout with a more
450 // coordinated effort.
Eric Carusocc7106c2017-04-27 14:22:42 -0700451 if (shill && shill->CreateConnectivityReport(nullptr))
452 sleep(kConnectionTesterTimeoutSeconds);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700453}
454
Eric Carusoc93a15c2017-04-24 16:15:12 -0700455string LogTool::GetLog(const string& name) {
Elly Jones533c7c42012-08-10 15:07:05 -0400456 string result;
Ben Chancf7d6412017-08-10 22:30:09 -0700457 GetNamedLogFrom(name, kCommandLogs, &result)
458 || GetNamedLogFrom(name, kExtraLogs, &result)
459 || GetNamedLogFrom(name, kFeedbackLogs, &result);
Elly Jones533c7c42012-08-10 15:07:05 -0400460 return result;
461}
462
Eric Carusof9091f82017-04-28 14:18:59 -0700463LogTool::LogMap LogTool::GetAllLogs() {
464 CreateConnectivityReport();
Elly Jones533c7c42012-08-10 15:07:05 -0400465 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700466 GetLogsFrom(kCommandLogs, &result);
467 GetLogsFrom(kExtraLogs, &result);
Elly Jones533c7c42012-08-10 15:07:05 -0400468 return result;
469}
470
Brian Norrisca4fc042018-04-03 00:24:26 -0700471LogTool::LogMap LogTool::GetAllDebugLogs() {
472 CreateConnectivityReport();
473 LogMap result;
474 GetLogsFrom(kCommandLogs, &result);
475 GetLogsFrom(kExtraLogs, &result);
476 GetLogsFrom(kBigFeedbackLogs, &result);
477 return result;
478}
479
Eric Carusof9091f82017-04-28 14:18:59 -0700480LogTool::LogMap LogTool::GetFeedbackLogs() {
481 CreateConnectivityReport();
Elly Jones533c7c42012-08-10 15:07:05 -0400482 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700483 GetLogsFrom(kCommandLogs, &result);
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700484 for (const auto& key : kCommandLogsExclude) {
485 result.erase(key);
486 }
Ben Chancf7d6412017-08-10 22:30:09 -0700487 GetLogsFrom(kFeedbackLogs, &result);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100488 AnonymizeLogMap(&result);
Elly Jones03cd6d72012-06-11 13:04:28 -0400489 return result;
490}
491
Eric Caruso0b241882018-04-04 13:43:46 -0700492void LogTool::GetBigFeedbackLogs(const base::ScopedFD& fd) {
Eric Carusof9091f82017-04-28 14:18:59 -0700493 CreateConnectivityReport();
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800494 base::DictionaryValue dictionary;
Ben Chancf7d6412017-08-10 22:30:09 -0700495 GetLogsInDictionary(kCommandLogs, &anonymizer_, &dictionary);
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700496 for (const auto& key : kCommandLogsExclude) {
497 dictionary.Remove(key, nullptr);
498 }
Ben Chancf7d6412017-08-10 22:30:09 -0700499 GetLogsInDictionary(kFeedbackLogs, &anonymizer_, &dictionary);
500 GetLogsInDictionary(kBigFeedbackLogs, &anonymizer_, &dictionary);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800501 SerializeLogsAsJSON(dictionary, fd);
502}
503
Eric Carusoc93a15c2017-04-24 16:15:12 -0700504LogTool::LogMap LogTool::GetUserLogFiles() {
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700505 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700506 for (size_t i = 0; kUserLogs[i].name; ++i)
507 result[kUserLogs[i].name] = kUserLogs[i].command;
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700508 return result;
509}
510
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700511// static
512string LogTool::EnsureUTF8String(const string& value,
513 LogTool::Encoding source_encoding) {
514 if (source_encoding == LogTool::Encoding::kAutodetect) {
515 if (base::IsStringUTF8(value))
516 return value;
517 source_encoding = LogTool::Encoding::kBinary;
518 }
519
520 if (source_encoding == LogTool::Encoding::kUtf8) {
521 string output;
522 const char* src = value.data();
523 int32_t src_len = static_cast<int32_t>(value.length());
524
525 output.reserve(value.size());
526 for (int32_t char_index = 0; char_index < src_len; char_index++) {
527 uint32_t code_point;
528 if (!base::ReadUnicodeCharacter(src, src_len, &char_index, &code_point) ||
529 !base::IsValidCharacter(code_point)) {
530 // Replace invalid characters with U+FFFD REPLACEMENT CHARACTER.
531 code_point = 0xFFFD;
532 }
533 base::WriteUnicodeCharacter(code_point, &output);
534 }
535 return output;
536 } else {
537 string encoded_value;
538 base::Base64Encode(value, &encoded_value);
539 return "<base64>: " + encoded_value;
540 }
541}
542
Darin Petkovce9b3a12013-01-10 16:38:54 +0100543void LogTool::AnonymizeLogMap(LogMap* log_map) {
Ben Chane2fa3572017-02-08 22:46:18 -0800544 for (auto& entry : *log_map)
545 entry.second = anonymizer_.Anonymize(entry.second);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100546}
547
Ben Chana0011d82014-05-13 00:19:29 -0700548} // namespace debugd