blob: 2609af6103dd0329f311fa12cde25ccc96f8ff75 [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
Kartik Hegde1c4b97b2018-09-09 19:09:34 -060027#include "brillo/key_value_store.h"
28#include <brillo/osrelease_reader.h>
29
Elly Jones03cd6d72012-06-11 13:04:28 -040030namespace debugd {
31
Elly Jones03cd6d72012-06-11 13:04:28 -040032using std::string;
Elly Jones03cd6d72012-06-11 13:04:28 -040033
Eric Caruso96d03d32017-04-25 18:01:17 -070034using Strings = std::vector<string>;
Elly Jones03cd6d72012-06-11 13:04:28 -040035
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080036namespace {
37
Ben Chanaf125862017-02-08 23:11:18 -080038const char kRoot[] = "root";
39const char kShell[] = "/bin/sh";
Kartik Hegde1c4b97b2018-09-09 19:09:34 -060040constexpr char kLsbReleasePath[] = "/etc/lsb-release";
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080041
42// Minimum time in seconds needed to allow shill to test active connections.
43const int kConnectionTesterTimeoutSeconds = 5;
Ben Chanf6cd93a2012-10-14 19:37:00 -070044
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -070045// Default maximum size of a log entry.
46constexpr const char kDefaultSizeCap[] = "512K";
47
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050048struct Log {
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -070049 const char* name;
50 const char* command;
51 const char* user = nullptr;
52 const char* group = nullptr;
53 const char* size_cap = kDefaultSizeCap; // passed as arg to 'tail -c'
54 LogTool::Encoding encoding = LogTool::Encoding::kAutodetect;
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050055};
56
Eric Carusoa879fd92017-10-11 12:57:10 -070057#define CMD_KERNEL_MODULE_PARAMS(module_name) \
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070058 "cd /sys/module/" #module_name "/parameters 2>/dev/null && grep -sH ^ *"
Eric Carusoa879fd92017-10-11 12:57:10 -070059
Ben Chancf7d6412017-08-10 22:30:09 -070060const Log kCommandLogs[] = {
Mike Frysingerb0350992018-09-14 13:45:35 -040061 // We need to enter init's mount namespace because it has /home/chronos
62 // mounted which is where the consent knob lives. We don't have that mount
63 // in our own mount namespace (by design). https://crbug.com/884249
64 { "CLIENT_ID", "/usr/bin/nsenter -t1 -m /usr/bin/metrics_client -i",
65 kRoot,
66 kDebugfsGroup,
67 },
Elly Jones03cd6d72012-06-11 13:04:28 -040068 { "LOGDATE", "/bin/date" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070069 { "atrus_logs", "cat /var/log/atrus.log 2>/dev/null" },
70 { "authpolicy", "cat /var/log/authpolicy.log" },
Nicolas Norvezd573ff62018-08-08 18:30:13 -070071 { "biod.LATEST", "cat /var/log/biod/biod.LATEST" },
72 { "biod.PREVIOUS", "cat /var/log/biod/biod.PREVIOUS" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070073 { "bios_info", "cat /var/log/bios_info.txt" },
Vadim Bendebury64aeeed2013-09-16 13:16:49 -070074 { "bios_log",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070075 "cat /sys/firmware/log "
76 "/proc/device-tree/chosen/ap-console-buffer 2>/dev/null" },
77 { "bios_times", "cat /var/log/bios_times.txt" },
Elly Jones03cd6d72012-06-11 13:04:28 -040078 { "board-specific",
Thiemo Nagela269ad22014-11-27 17:13:01 +010079 "/usr/share/userfeedback/scripts/get_board_specific_info" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070080 { "buddyinfo", "cat /proc/buddyinfo" },
Daisuke Nojiri3d832ee2017-02-17 14:21:28 -080081 { "cbi_info", "/usr/share/userfeedback/scripts/cbi_info", kRoot, },
Olof Johanssone5d0fd32016-01-29 14:40:34 -080082 { "cheets_log", "/usr/bin/collect-cheets-logs 2>&1" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070083 { "clobber.log", "cat /var/log/clobber.log 2>/dev/null" },
84 { "clobber-state.log", "cat /var/log/clobber-state.log 2>/dev/null" },
85 { "chrome_system_log", "cat /var/log/chrome/chrome" },
Dariusz Marcinkiewicza0597062018-09-11 10:53:53 +020086 { "chrome_system_log.PREVIOUS", "cat /var/log/chrome/chrome.PREVIOUS" },
Mike Frysinger32cdf3e2017-08-14 18:17:06 -040087 // There might be more than one record, so grab them all.
88 // Plus, for <linux-3.19, it's named "console-ramoops", but for newer
89 // versions, it's named "console-ramoops-#".
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070090 { "console-ramoops", "cat /sys/fs/pstore/console-ramoops* 2>/dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -040091 { "cpu", "/usr/bin/uname -p" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070092 { "cpuinfo", "cat /proc/cpuinfo" },
93 { "cr50_version", "cat /var/cache/cr50-version" },
Eric Caruso57333f12015-09-08 12:50:32 -070094 { "cros_ec",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070095 "cat /var/log/cros_ec.previous /var/log/cros_ec.log 2>/dev/null" },
Nicolas Boichat083795f2016-08-16 11:16:49 +020096 { "cros_ec_panicinfo",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -070097 "cat /sys/kernel/debug/cros_ec/panicinfo 2>/dev/null",
Nicolas Boichatd51092b2016-09-01 10:36:46 +080098 SandboxedProcess::kDefaultUser,
99 kDebugfsGroup
100 },
Shawn Nematbakhsh60813732017-07-13 10:46:23 -0700101 { "cros_ec_pdinfo",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700102 "cat /sys/kernel/debug/cros_ec/pdinfo 2>/dev/null",
Shawn Nematbakhsh60813732017-07-13 10:46:23 -0700103 SandboxedProcess::kDefaultUser,
104 kDebugfsGroup
105 },
Vincent Palatine3588042018-04-04 17:02:38 +0200106 { "cros_fp",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700107 "cat /var/log/cros_fp.previous /var/log/cros_fp.log 2>/dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400108 { "dmesg", "/bin/dmesg" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700109 { "ec_info", "cat /var/log/ec_info.txt" },
Miguel Casas8d4e54f2018-03-29 09:42:52 -0400110 // The sed command replaces the EDID serial number (4 bytes at position 12)
111 // with zeroes. See https://en.wikipedia.org/wiki/EDID#EDID_1.4_data_format.
112 { "edid-decode",
Miguel Casas55b7c842018-08-15 13:10:10 -0400113 "for f in /sys/class/drm/card0-*/edid; do "
Miguel Casas8d4e54f2018-03-29 09:42:52 -0400114 "echo \"----------- ${f}\"; "
Miguel Casas1b8d2792018-08-17 20:00:52 -0400115 "sed -E 's/^(.{11}).{4}/\\1\\x0\\x0\\x0\\x0/' \"${f}\" | "
116 // edid-decode's stderr output is redundant, so silence it.
117 "edid-decode 2>/dev/null; "
Miguel Casas8d4e54f2018-03-29 09:42:52 -0400118 "done"
119 },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700120 { "eventlog", "cat /var/log/eventlog.txt" },
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400121 {
122 "exynos_gem_objects",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700123 "cat /sys/kernel/debug/dri/0/exynos_gem_objects 2>/dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400124 SandboxedProcess::kDefaultUser,
125 kDebugfsGroup
126 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400127 { "font_info", "/usr/share/userfeedback/scripts/font_info" },
Daisuke Nojiridebede0e2017-02-17 14:21:28 -0800128 { "sensor_info", "/usr/share/userfeedback/scripts/sensor_info" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400129 { "hardware_class", "/usr/bin/crossystem hwid" },
130 { "hostname", "/bin/hostname" },
131 { "hw_platform", "/usr/bin/uname -i" },
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400132 {
133 "i915_gem_gtt",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700134 "cat /sys/kernel/debug/dri/0/i915_gem_gtt 2>/dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400135 SandboxedProcess::kDefaultUser,
136 kDebugfsGroup
137 },
138 {
139 "i915_gem_objects",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700140 "cat /sys/kernel/debug/dri/0/i915_gem_objects 2>/dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400141 SandboxedProcess::kDefaultUser,
142 kDebugfsGroup
143 },
144 {
145 "i915_error_state",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700146 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2>/dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400147 SandboxedProcess::kDefaultUser,
148 kDebugfsGroup,
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700149 kDefaultSizeCap,
150 LogTool::Encoding::kBinary,
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400151 },
Daniel Erat982ab4b2014-04-09 07:32:38 -0700152 { "ifconfig", "/bin/ifconfig -a" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700153 { "input_devices", "cat /proc/bus/input/devices" },
Eric Carusob1820c02017-08-24 15:39:56 -0700154 // Information about the wiphy device, such as current channel.
155 { "iw_dev", "/usr/sbin/iw dev" },
156 // Hardware capabilities of the wiphy device.
157 { "iw_list", "/usr/sbin/iw list" },
Eric Carusoa879fd92017-10-11 12:57:10 -0700158#if USE_IWLWIFI_DUMP
159 { "iwlmvm_module_params", CMD_KERNEL_MODULE_PARAMS(iwlmvm) },
160 { "iwlwifi_module_params", CMD_KERNEL_MODULE_PARAMS(iwlwifi) },
161#endif // USE_IWLWIFI_DUMP
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700162 { "kernel-crashes", "cat /var/spool/crash/kernel.*.kcrash 2>/dev/null" },
163 { "logcat",
164 "/usr/sbin/android-sh -c '/system/bin/logcat -d'",
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700165 kRoot,
166 kRoot,
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700167 kDefaultSizeCap,
168 LogTool::Encoding::kUtf8,
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700169 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400170 { "lsmod", "lsmod" },
171 { "lspci", "/usr/sbin/lspci" },
Simon Que41c88b52017-06-19 14:05:07 -0400172 { "lsusb", "lsusb && lsusb -t" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700173 { "mali_memory", "cat /sys/class/misc/mali0/device/memory 2>/dev/null" },
Luigi Semenzato7f00dfb2018-06-26 11:34:02 -0700174 { "memd.parameters", "cat /var/log/memd/memd.parameters 2>/dev/null" },
175 { "memd clips", "cat /var/log/memd/memd.clip* 2>/dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400176 { "meminfo", "cat /proc/meminfo" },
Mike Frysinger8fd2eee2017-11-03 14:35:26 -0400177 {
178 "memory_spd_info",
179 // mosys may use 'i2c-dev', which may not be loaded yet.
180 "modprobe i2c-dev 2>/dev/null && "
181 "mosys -l memory spd print all 2>/dev/null",
182 kRoot,
183 kDebugfsGroup,
184 },
Simon Quecb63b9c2017-06-19 14:53:31 -0400185 // The sed command finds the EDID blob (starting the line after "value:") and
186 // replaces the serial number with all zeroes.
187 //
188 // The EDID is printed as a hex dump over several lines, each line containing
189 // the contents of 16 bytes. The first 16 bytes are broken down as follows:
190 // uint64_t fixed_pattern; // Always 00 FF FF FF FF FF FF 00.
191 // uint16_t manufacturer_id; // Manufacturer ID, encoded as PNP IDs.
192 // uint16_t product_code; // Manufacturer product code, little-endian.
193 // uint32_t serial_number; // Serial number, little-endian.
194 // Source: https://en.wikipedia.org/wiki/EDID#EDID_1.3_data_format
195 //
196 // The subsequent substitution command looks for the fixed pattern followed by
197 // two 32-bit fields (manufacturer + product, serial number). It replaces the
198 // latter field with 8 bytes of zeroes.
199 //
200 // TODO(crbug.com/731133): Remove the sed command once modetest itself can
201 // remove serial numbers.
202 {
203 "modetest",
204 "(modetest; modetest -M evdi; modetest -M udl) | "
205 "sed -E '/EDID/ {:a;n;/value:/!ba;n;"
206 "s/(00f{12}00)([0-9a-f]{8})([0-9a-f]{8})/\\1\\200000000/}'",
207 kRoot,
208 kRoot,
209 },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700210 { "mount-encrypted", "cat /var/log/mount-encrypted.log" },
211 { "mountinfo", "cat /proc/1/mountinfo" },
Nicolas Boichat0230b0b2017-08-17 11:20:19 +0800212 { "netlog", "/usr/share/userfeedback/scripts/getmsgs /var/log/net.log" },
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700213 // --processes requires root.
214 { "netstat", "/sbin/ss --all --query inet --numeric --processes",
215 kRoot,
216 kRoot,
217 },
henryhsufa6daa12014-09-24 13:26:30 +0800218 {
219 "nvmap_iovmm",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700220 "cat /sys/kernel/debug/nvmap/iovmm/allocations 2>/dev/null",
henryhsufa6daa12014-09-24 13:26:30 +0800221 SandboxedProcess::kDefaultUser,
222 kDebugfsGroup,
223 },
Todd Brocha0c13762018-05-03 11:31:25 -0700224 { "oemdata", "/usr/share/cros/oemdata.sh", kRoot, kRoot, },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700225 { "pagetypeinfo", "cat /proc/pagetypeinfo" },
Mike Frysinger8fd2eee2017-11-03 14:35:26 -0400226 {
227 "platform_info",
228 // mosys may use 'i2c-dev', which may not be loaded yet.
229 "modprobe i2c-dev 2>/dev/null && "
230 "for param in "
231 "vendor "
232 "name "
233 "version "
234 "family "
235 "model "
236 "sku "
237 "customization "
238 "; do "
239 "mosys -l platform \"${param}\" 2>/dev/null; "
240 "done",
241 kRoot,
242 kDebugfsGroup,
243 },
Daniel Erat9b58b6d2013-04-30 14:58:56 -0700244 { "power_supply_info", "/usr/bin/power_supply_info" },
Daniel Eratd8b84992017-03-13 17:30:00 -0600245 { "power_supply_sysfs", "/usr/bin/print_sysfs_power_supply_data" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700246 { "powerd.LATEST", "cat /var/log/power_manager/powerd.LATEST" },
247 { "powerd.PREVIOUS", "cat /var/log/power_manager/powerd.PREVIOUS" },
248 { "powerd.out", "cat /var/log/powerd.out" },
249 { "powerwash_count", "cat /var/log/powerwash_count 2>/dev/null" },
Mattias Nissler887dce22017-07-03 14:44:35 +0200250 // Changed from 'ps ux' to 'ps aux' since we're running as debugd,
251 // not chronos.
Elly Jones03cd6d72012-06-11 13:04:28 -0400252 { "ps", "/bin/ps aux" },
yusukes34171ba2017-04-27 15:46:01 -0700253 // /proc/slabinfo is owned by root and has 0400 permission.
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700254 { "slabinfo", "cat /proc/slabinfo", kRoot, kRoot, },
255 { "storage_info", "cat /var/log/storage_info.txt" },
Alexis Saverybbb85132018-05-18 17:41:10 -0700256 { "swap_info",
257 "/usr/share/cros/init/swap.sh status 2>/dev/null",
258 SandboxedProcess::kDefaultUser,
259 kDebugfsGroup
260 },
Nicolas Boichat0230b0b2017-08-17 11:20:19 +0800261 { "syslog", "/usr/share/userfeedback/scripts/getmsgs /var/log/messages" },
Brandon Mayere24c7a22017-08-04 13:58:48 -0400262 { "system_log_stats", "echo 'BLOCK_SIZE=1024'; "
263 "find /var/log/ -type f -exec du --block-size=1024 {} + | sort -n -r",
264 kRoot, kRoot},
Kuo-Hsin Yang379846b2018-06-04 11:16:55 +0800265 { "threads", "/bin/ps -T axo pid,ppid,spid,pcpu,ni,stat,time,comm" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700266 { "tlsdate", "cat /var/log/tlsdate.log" },
Hsu-Cheng Tsaie8c31d52017-03-31 12:34:55 +0800267 { "top thread", "/usr/bin/top -Hb -n 1 | head -n 40"},
268 { "top memory", "/usr/bin/top -o \"+%MEM\" -bn 1 | head -n 57"},
Andrew de los Reyesc060c342013-04-10 13:46:30 -0700269 { "touch_fw_version", "grep -E"
Charlie Mooneybb08f982016-02-04 15:35:56 -0800270 " -e 'synaptics: Touchpad model'"
271 " -e 'chromeos-[a-z]*-touch-[a-z]*-update'"
Andrew de los Reyesc060c342013-04-10 13:46:30 -0700272 " /var/log/messages | tail -n 20" },
Mattias Nissler483d76f2017-08-23 16:53:36 +0200273 {
274 "tpm-firmware-updater",
275 "/usr/share/userfeedback/scripts/getmsgs /var/log/tpm-firmware-updater.log"
276 },
Mattias Nissler887dce22017-07-03 14:44:35 +0200277 // TODO(jorgelo,mnissler): Don't run this as root.
278 // On TPM 1.2 devices this will likely require adding a new user to the 'tss'
279 // group.
280 // On TPM 2.0 devices 'get_version_info' uses D-Bus and therefore can run as
281 // any user.
282 { "tpm_version", "/usr/sbin/tpm-manager get_version_info", kRoot, kRoot },
Charlie Mooney69f56702017-05-02 14:55:41 -0700283 { "atmel_ts_refs", "/opt/google/touch/scripts/atmel_tools.sh ts r",
284 kRoot, kRoot},
285 { "atmel_tp_refs", "/opt/google/touch/scripts/atmel_tools.sh tp r",
286 kRoot, kRoot},
287 { "atmel_ts_deltas", "/opt/google/touch/scripts/atmel_tools.sh ts d",
288 kRoot, kRoot},
289 { "atmel_tp_deltas", "/opt/google/touch/scripts/atmel_tools.sh tp d",
290 kRoot, kRoot},
Gwendal Grignou427d94e2016-10-25 11:34:51 -0700291 {
292 "trim",
293 "cat /var/lib/trim/stateful_trim_state /var/lib/trim/stateful_trim_data"
294 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400295 { "ui_log", "/usr/share/userfeedback/scripts/get_log /var/log/ui/ui.LATEST" },
296 { "uname", "/bin/uname -a" },
297 { "update_engine.log", "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
Thiemo Nagela269ad22014-11-27 17:13:01 +0100298 " s.^./var/log/update_engine/.)" },
Dariusz Marcinkiewiczc4126882017-10-13 16:07:36 +0200299 { "uptime", "/usr/bin/cut -d' ' -f1 /proc/uptime" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700300 { "verified boot", "cat /var/log/debug_vboot_noisy.log" },
301 { "vmlog.1.LATEST", "cat /var/log/vmlog/vmlog.1.LATEST" },
302 { "vmlog.1.PREVIOUS", "cat /var/log/vmlog/vmlog.1.PREVIOUS" },
303 { "vmlog.LATEST", "cat /var/log/vmlog/vmlog.LATEST" },
304 { "vmlog.PREVIOUS", "cat /var/log/vmlog/vmlog.PREVIOUS" },
305 { "vmstat", "cat /proc/vmstat" },
306 { "vpd_2.0", "cat /var/log/vpd_2.0.txt" },
Samuel Tan107b6c82014-07-10 15:33:44 -0700307 { "wifi_status", "/usr/bin/network_diag --wifi-internal --no-log" },
Sonny Raoab5f9952013-07-17 14:13:53 -0700308 { "zram compressed data size",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700309 "cat /sys/block/zram0/compr_data_size 2>/dev/null" },
Sonny Raoab5f9952013-07-17 14:13:53 -0700310 { "zram original data size",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700311 "cat /sys/block/zram0/orig_data_size 2>/dev/null" },
Sonny Raoab5f9952013-07-17 14:13:53 -0700312 { "zram total memory used",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700313 "cat /sys/block/zram0/mem_used_total 2>/dev/null" },
Sonny Raoab5f9952013-07-17 14:13:53 -0700314 { "zram total reads",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700315 "cat /sys/block/zram0/num_reads 2>/dev/null" },
Sonny Raoab5f9952013-07-17 14:13:53 -0700316 { "zram total writes",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700317 "cat /sys/block/zram0/num_writes 2>/dev/null" },
318 { "zram new stats names",
319 "echo orig_size compr_size used_total limit "
Luigi Semenzato4164de42017-07-06 10:42:54 -0700320 "used_max zero_pages migrated" },
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700321 { "zram new stats values", "cat /sys/block/zram0/mm_stat 2>/dev/null" },
322 { "cros_tp version", "cat /sys/class/chromeos/cros_tp/version" },
Wei-Ning Huang230b7d82017-07-14 16:19:13 +0800323 { "cros_tp console", "/usr/sbin/ectool --name=cros_tp console",
324 kRoot, kRoot },
Wei-Ning Huang23ffdcf2017-08-04 11:30:39 +0800325 { "cros_tp frame", "/usr/sbin/ectool --name=cros_tp tpframeget",
326 kRoot, kRoot },
Zach Reizner7eab48b2018-06-22 19:26:48 -0700327 { "crostini", "/usr/bin/cicerone_client --get_info" },
Gaurav Shah6e8fd892012-10-01 11:51:08 -0700328
Elly Jones03cd6d72012-06-11 13:04:28 -0400329 // Stuff pulled out of the original list. These need access to the running X
330 // session, which we'd rather not give to debugd, or return info specific to
331 // the current session (in the setsid(2) sense), which is not useful for
332 // debugd
333 // { "env", "set" },
334 // { "setxkbmap", "/usr/bin/setxkbmap -print -query" },
335 // { "xrandr", "/usr/bin/xrandr --verbose" }
Ben Chan64d19b22017-02-06 14:03:47 -0800336 { nullptr, nullptr }
Elly Jones533c7c42012-08-10 15:07:05 -0400337};
338
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700339// netstat and logcat should appear in chrome://system but not in feedback
340// reports. Open sockets may have privacy implications, and logcat is
341// already incorporated via arc-bugreport.
342const std::vector<string> kCommandLogsExclude = {"netstat", "logcat"};
343
Ben Chancf7d6412017-08-10 22:30:09 -0700344const Log kExtraLogs[] = {
Ben Chan36e42282014-02-12 22:32:34 -0800345#if USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400346 { "mm-status", "/usr/bin/modem status" },
Ben Chan36e42282014-02-12 22:32:34 -0800347#endif // USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400348 { "network-devices", "/usr/bin/connectivity show devices" },
349 { "network-services", "/usr/bin/connectivity show services" },
Ben Chan64d19b22017-02-06 14:03:47 -0800350 { nullptr, nullptr }
Elly Jones533c7c42012-08-10 15:07:05 -0400351};
352
Ben Chancf7d6412017-08-10 22:30:09 -0700353const Log kFeedbackLogs[] = {
Ben Chan36e42282014-02-12 22:32:34 -0800354#if USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400355 { "mm-status", "/usr/bin/modem status-feedback" },
Ben Chan36e42282014-02-12 22:32:34 -0800356#endif // USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400357 { "network-devices", "/usr/bin/connectivity show-feedback devices" },
358 { "network-services", "/usr/bin/connectivity show-feedback services" },
Ben Chan64d19b22017-02-06 14:03:47 -0800359 { nullptr, nullptr }
Elly Jones03cd6d72012-06-11 13:04:28 -0400360};
361
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700362// List of log files needed to be part of the feedback report that are huge and
363// must be sent back to the client via the file descriptor using
364// LogTool::GetBigFeedbackLogs().
Ben Chancf7d6412017-08-10 22:30:09 -0700365const Log kBigFeedbackLogs[] = {
Eric Carusoe3166f22016-06-10 15:36:02 -0700366 { "arc-bugreport",
Luigi Semenzato7e2c08f2018-06-26 14:58:49 -0700367 "cat /run/arc/bugreport/pipe 2>/dev/null",
Eric Carusoe3166f22016-06-10 15:36:02 -0700368 // ARC bugreport permissions are weird. Since we're just running cat, this
369 // shouldn't cause any issues.
370 kRoot,
Ricky Zhou4c473ca2016-06-21 17:46:58 -0700371 kRoot,
372 "10M",
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700373 LogTool::Encoding::kUtf8,
Eric Carusoe3166f22016-06-10 15:36:02 -0700374 },
Ben Chan64d19b22017-02-06 14:03:47 -0800375 { nullptr, nullptr }
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700376};
377
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700378// List of log files that must directly be collected by Chrome. This is because
379// debugd is running under a VFS namespace and does not have access to later
380// cryptohome mounts.
Ben Chancf7d6412017-08-10 22:30:09 -0700381const Log kUserLogs[] = {
Elly Fong-Jones6456ce52013-04-17 13:31:13 -0400382 {"chrome_user_log", "log/chrome"},
Xiaohui Chen32b171d2018-08-27 17:18:44 -0700383 {"libassistant_user_log", "log/libassistant.log"},
Elly Fong-Jones6456ce52013-04-17 13:31:13 -0400384 {"login-times", "login-times"},
385 {"logout-times", "logout-times"},
Ben Chan64d19b22017-02-06 14:03:47 -0800386 { nullptr, nullptr}
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700387};
388
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800389// TODO(ellyjones): sandbox. crosbug.com/35122
390string Run(const Log& log) {
391 string output;
392 ProcessWithOutput p;
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700393 string tailed_cmdline =
394 base::StringPrintf("%s | tail -c %s", log.command, log.size_cap);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800395 if (log.user && log.group)
396 p.SandboxAs(log.user, log.group);
397 if (!p.Init())
398 return "<not available>";
399 p.AddArg(kShell);
400 p.AddStringOption("-c", tailed_cmdline);
401 if (p.Run())
402 return "<not available>";
403 p.GetOutput(&output);
404 if (!output.size())
405 return "<empty>";
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700406 return LogTool::EnsureUTF8String(output, log.encoding);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800407}
408
409// Fills |dictionary| with the anonymized contents of the logs in |logs|.
410void GetLogsInDictionary(const struct Log* logs,
411 AnonymizerTool* anonymizer,
412 base::DictionaryValue* dictionary) {
413 for (size_t i = 0; logs[i].name; ++i) {
414 dictionary->SetStringWithoutPathExpansion(
415 logs[i].name, anonymizer->Anonymize(Run(logs[i])));
416 }
417}
418
419// Serializes the |dictionary| into the file with the given |fd| in a JSON
420// format.
421void SerializeLogsAsJSON(const base::DictionaryValue& dictionary,
Eric Caruso0b241882018-04-04 13:43:46 -0700422 const base::ScopedFD& fd) {
Eric Caruso96d03d32017-04-25 18:01:17 -0700423 string logs_json;
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800424 base::JSONWriter::WriteWithOptions(dictionary,
425 base::JSONWriter::OPTIONS_PRETTY_PRINT,
426 &logs_json);
Eric Caruso0b241882018-04-04 13:43:46 -0700427 base::WriteFileDescriptor(fd.get(), logs_json.c_str(), logs_json.size());
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800428}
429
Elly Jones533c7c42012-08-10 15:07:05 -0400430bool GetNamedLogFrom(const string& name, const struct Log* logs,
431 string* result) {
432 for (size_t i = 0; logs[i].name; i++) {
433 if (name == logs[i].name) {
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -0500434 *result = Run(logs[i]);
Elly Jones533c7c42012-08-10 15:07:05 -0400435 return true;
436 }
437 }
438 *result = "<invalid log name>";
439 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400440}
441
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800442void GetLogsFrom(const struct Log* logs, LogTool::LogMap* map) {
443 for (size_t i = 0; logs[i].name; i++)
444 (*map)[logs[i].name] = Run(logs[i]);
445}
446
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600447void GetLsbReleaseInfo(LogTool::LogMap* map) {
448 const base::FilePath lsb_release(kLsbReleasePath);
449 brillo::KeyValueStore store;
450 if (!store.Load(lsb_release)) {
451 // /etc/lsb-release might not be present (cros deploying a new
452 // configuration or no fields set at all). Just print a debug
453 // message and continue.
454 DLOG(INFO) << "Could not load fields from " << lsb_release.value();
455 } else {
456 for (const auto& key : store.GetKeys()) {
457 std::string value;
458 store.GetString(key, &value);
459 (*map)[key] = value;
460 }
461 }
462}
463
464void GetOsReleaseInfo(LogTool::LogMap* map) {
465 brillo::OsReleaseReader reader;
466 reader.Load();
467 for (const auto& key : reader.GetKeys()) {
468 std::string value;
469 reader.GetString(key, &value);
470 (*map)["os-release " + key] = value;
471 }
472}
473
474void PopulateDictionaryValue(const LogTool::LogMap& map,
475 base::DictionaryValue* dictionary) {
476 for (const auto& kv : map) {
477 dictionary->SetString(kv.first, kv.second);
478 }
479}
480
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800481} // namespace
482
Eric Carusof9091f82017-04-28 14:18:59 -0700483void LogTool::CreateConnectivityReport() {
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700484 // Perform ConnectivityTrial to report connection state in feedback log.
Ben Chan8e9f6d02017-09-26 23:04:21 -0700485 auto shill = std::make_unique<org::chromium::flimflam::ManagerProxy>(bus_);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700486 // Give the connection trial time to test the connection and log the results
487 // before collecting the logs for feedback.
488 // TODO(silberst): Replace the simple approach of a single timeout with a more
489 // coordinated effort.
Eric Carusocc7106c2017-04-27 14:22:42 -0700490 if (shill && shill->CreateConnectivityReport(nullptr))
491 sleep(kConnectionTesterTimeoutSeconds);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700492}
493
Eric Carusoc93a15c2017-04-24 16:15:12 -0700494string LogTool::GetLog(const string& name) {
Elly Jones533c7c42012-08-10 15:07:05 -0400495 string result;
Ben Chancf7d6412017-08-10 22:30:09 -0700496 GetNamedLogFrom(name, kCommandLogs, &result)
497 || GetNamedLogFrom(name, kExtraLogs, &result)
498 || GetNamedLogFrom(name, kFeedbackLogs, &result);
Elly Jones533c7c42012-08-10 15:07:05 -0400499 return result;
500}
501
Eric Carusof9091f82017-04-28 14:18:59 -0700502LogTool::LogMap LogTool::GetAllLogs() {
503 CreateConnectivityReport();
Elly Jones533c7c42012-08-10 15:07:05 -0400504 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700505 GetLogsFrom(kCommandLogs, &result);
506 GetLogsFrom(kExtraLogs, &result);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600507 GetLsbReleaseInfo(&result);
508 GetOsReleaseInfo(&result);
Elly Jones533c7c42012-08-10 15:07:05 -0400509 return result;
510}
511
Brian Norrisca4fc042018-04-03 00:24:26 -0700512LogTool::LogMap LogTool::GetAllDebugLogs() {
513 CreateConnectivityReport();
514 LogMap result;
515 GetLogsFrom(kCommandLogs, &result);
516 GetLogsFrom(kExtraLogs, &result);
517 GetLogsFrom(kBigFeedbackLogs, &result);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600518 GetLsbReleaseInfo(&result);
519 GetOsReleaseInfo(&result);
Brian Norrisca4fc042018-04-03 00:24:26 -0700520 return result;
521}
522
Eric Carusof9091f82017-04-28 14:18:59 -0700523LogTool::LogMap LogTool::GetFeedbackLogs() {
524 CreateConnectivityReport();
Elly Jones533c7c42012-08-10 15:07:05 -0400525 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700526 GetLogsFrom(kCommandLogs, &result);
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700527 for (const auto& key : kCommandLogsExclude) {
528 result.erase(key);
529 }
Ben Chancf7d6412017-08-10 22:30:09 -0700530 GetLogsFrom(kFeedbackLogs, &result);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600531 GetLsbReleaseInfo(&result);
532 GetOsReleaseInfo(&result);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100533 AnonymizeLogMap(&result);
Elly Jones03cd6d72012-06-11 13:04:28 -0400534 return result;
535}
536
Eric Caruso0b241882018-04-04 13:43:46 -0700537void LogTool::GetBigFeedbackLogs(const base::ScopedFD& fd) {
Eric Carusof9091f82017-04-28 14:18:59 -0700538 CreateConnectivityReport();
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800539 base::DictionaryValue dictionary;
Ben Chancf7d6412017-08-10 22:30:09 -0700540 GetLogsInDictionary(kCommandLogs, &anonymizer_, &dictionary);
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700541 for (const auto& key : kCommandLogsExclude) {
542 dictionary.Remove(key, nullptr);
543 }
Ben Chancf7d6412017-08-10 22:30:09 -0700544 GetLogsInDictionary(kFeedbackLogs, &anonymizer_, &dictionary);
545 GetLogsInDictionary(kBigFeedbackLogs, &anonymizer_, &dictionary);
Kartik Hegde1c4b97b2018-09-09 19:09:34 -0600546 LogMap map;
547 GetLsbReleaseInfo(&map);
548 GetOsReleaseInfo(&map);
549 PopulateDictionaryValue(map, &dictionary);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800550 SerializeLogsAsJSON(dictionary, fd);
551}
552
Eric Carusoc93a15c2017-04-24 16:15:12 -0700553LogTool::LogMap LogTool::GetUserLogFiles() {
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700554 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700555 for (size_t i = 0; kUserLogs[i].name; ++i)
556 result[kUserLogs[i].name] = kUserLogs[i].command;
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700557 return result;
558}
559
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700560// static
561string LogTool::EnsureUTF8String(const string& value,
562 LogTool::Encoding source_encoding) {
563 if (source_encoding == LogTool::Encoding::kAutodetect) {
564 if (base::IsStringUTF8(value))
565 return value;
566 source_encoding = LogTool::Encoding::kBinary;
567 }
568
569 if (source_encoding == LogTool::Encoding::kUtf8) {
570 string output;
571 const char* src = value.data();
572 int32_t src_len = static_cast<int32_t>(value.length());
573
574 output.reserve(value.size());
575 for (int32_t char_index = 0; char_index < src_len; char_index++) {
576 uint32_t code_point;
577 if (!base::ReadUnicodeCharacter(src, src_len, &char_index, &code_point) ||
578 !base::IsValidCharacter(code_point)) {
579 // Replace invalid characters with U+FFFD REPLACEMENT CHARACTER.
580 code_point = 0xFFFD;
581 }
582 base::WriteUnicodeCharacter(code_point, &output);
583 }
584 return output;
585 } else {
586 string encoded_value;
587 base::Base64Encode(value, &encoded_value);
588 return "<base64>: " + encoded_value;
589 }
590}
591
Darin Petkovce9b3a12013-01-10 16:38:54 +0100592void LogTool::AnonymizeLogMap(LogMap* log_map) {
Ben Chane2fa3572017-02-08 22:46:18 -0800593 for (auto& entry : *log_map)
594 entry.second = anonymizer_.Anonymize(entry.second);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100595}
596
Ben Chana0011d82014-05-13 00:19:29 -0700597} // namespace debugd