blob: cb6c36542661eedce04da9bbd46f052ff5882b3c [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>
Ben Chana0011d82014-05-13 00:19:29 -07008#include <vector>
9
Ben Chanab93abf2017-01-24 13:32:51 -080010#include <base/base64.h>
Ben Chancd8fda42014-09-05 08:21:06 -070011#include <base/files/file_util.h>
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080012#include <base/json/json_writer.h>
Elly Jones03cd6d72012-06-11 13:04:28 -040013#include <base/logging.h>
Ben Chan9953a592014-02-05 23:32:00 -080014#include <base/strings/string_split.h>
15#include <base/strings/string_util.h>
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080016#include <base/values.h>
Elly Jones03cd6d72012-06-11 13:04:28 -040017
Rebecca Silbersteine78af402014-10-02 10:55:04 -070018#include <chromeos/dbus/service_constants.h>
Eric Carusocc7106c2017-04-27 14:22:42 -070019#include <shill/dbus-proxies.h>
Rebecca Silbersteine78af402014-10-02 10:55:04 -070020
Ben Chanaf125862017-02-08 23:11:18 -080021#include "debugd/src/constants.h"
Alex Vakulenko262be3f2014-07-30 15:25:50 -070022#include "debugd/src/process_with_output.h"
Elly Jones03cd6d72012-06-11 13:04:28 -040023
24namespace debugd {
25
Elly Jones03cd6d72012-06-11 13:04:28 -040026using std::string;
Elly Jones03cd6d72012-06-11 13:04:28 -040027
Eric Caruso96d03d32017-04-25 18:01:17 -070028using Strings = std::vector<string>;
Elly Jones03cd6d72012-06-11 13:04:28 -040029
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080030namespace {
31
Ben Chanaf125862017-02-08 23:11:18 -080032const char kRoot[] = "root";
33const char kShell[] = "/bin/sh";
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080034
35// Minimum time in seconds needed to allow shill to test active connections.
36const int kConnectionTesterTimeoutSeconds = 5;
Ben Chanf6cd93a2012-10-14 19:37:00 -070037
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050038struct Log {
39 const char *name;
40 const char *command;
41 const char *user;
42 const char *group;
Ricky Zhou4c473ca2016-06-21 17:46:58 -070043 const char *size_cap; // passed as arg to 'tail'
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050044};
45
Eric Carusoa879fd92017-10-11 12:57:10 -070046#define CMD_KERNEL_MODULE_PARAMS(module_name) \
47 "cd /sys/module/" #module_name "/parameters 2> /dev/null && grep -sH ^ *"
48
Ben Chancf7d6412017-08-10 22:30:09 -070049const Log kCommandLogs[] = {
Mike Frysingerc3e835a2017-01-06 04:09:34 -050050 { "CLIENT_ID", "/usr/bin/metrics_client -i"},
Elly Jones03cd6d72012-06-11 13:04:28 -040051 { "LOGDATE", "/bin/date" },
Ahmed Fakhryb7d12cd2017-05-03 11:05:13 -070052 { "atrus_logs", "/bin/cat /var/log/atrus.log 2> /dev/null" },
Lutz Justenc2265662017-06-27 10:13:23 +020053 { "authpolicy", "/bin/cat /var/log/authpolicy.log" },
Elly Jones03cd6d72012-06-11 13:04:28 -040054 { "bios_info", "/bin/cat /var/log/bios_info.txt" },
Vadim Bendebury64aeeed2013-09-16 13:16:49 -070055 { "bios_log",
56 "/bin/cat /sys/firmware/log "
57 "/proc/device-tree/chosen/ap-console-buffer 2> /dev/null" },
Stefan Reinauer4393fa72012-10-18 11:08:09 -070058 { "bios_times", "/bin/cat /var/log/bios_times.txt" },
Elly Jones03cd6d72012-06-11 13:04:28 -040059 { "board-specific",
Thiemo Nagela269ad22014-11-27 17:13:01 +010060 "/usr/share/userfeedback/scripts/get_board_specific_info" },
Kuo-Hsin Yangc45e6302018-02-01 16:52:12 +080061 { "buddyinfo", "/bin/cat /proc/buddyinfo" },
Olof Johanssone5d0fd32016-01-29 14:40:34 -080062 { "cheets_log", "/usr/bin/collect-cheets-logs 2>&1" },
Thiemo Nagela269ad22014-11-27 17:13:01 +010063 { "clobber.log", "/bin/cat /var/log/clobber.log 2> /dev/null" },
64 { "clobber-state.log", "/bin/cat /var/log/clobber-state.log 2> /dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -040065 { "chrome_system_log", "/bin/cat /var/log/chrome/chrome" },
Mike Frysinger32cdf3e2017-08-14 18:17:06 -040066 // There might be more than one record, so grab them all.
67 // Plus, for <linux-3.19, it's named "console-ramoops", but for newer
68 // versions, it's named "console-ramoops-#".
Mike Frysingerd4eefa32017-11-04 02:22:51 -040069 { "console-ramoops", "/bin/cat /sys/fs/pstore/console-ramoops* 2>/dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -040070 { "cpu", "/usr/bin/uname -p" },
71 { "cpuinfo", "/bin/cat /proc/cpuinfo" },
Benson Leung1526c8f2017-01-22 20:38:04 -080072 { "cr50_version", "/bin/cat /var/cache/cr50-version" },
Eric Caruso57333f12015-09-08 12:50:32 -070073 { "cros_ec",
74 "/bin/cat /var/log/cros_ec.previous /var/log/cros_ec.log 2> /dev/null" },
Nicolas Boichat083795f2016-08-16 11:16:49 +020075 { "cros_ec_panicinfo",
Nicolas Boichatd51092b2016-09-01 10:36:46 +080076 "/bin/cat /sys/kernel/debug/cros_ec/panicinfo 2> /dev/null",
77 SandboxedProcess::kDefaultUser,
78 kDebugfsGroup
79 },
Shawn Nematbakhsh60813732017-07-13 10:46:23 -070080 { "cros_ec_pdinfo",
81 "/bin/cat /sys/kernel/debug/cros_ec/pdinfo 2> /dev/null",
82 SandboxedProcess::kDefaultUser,
83 kDebugfsGroup
84 },
Elly Jones03cd6d72012-06-11 13:04:28 -040085 { "dmesg", "/bin/dmesg" },
86 { "ec_info", "/bin/cat /var/log/ec_info.txt" },
Miguel Casas8d4e54f2018-03-29 09:42:52 -040087 // The sed command replaces the EDID serial number (4 bytes at position 12)
88 // with zeroes. See https://en.wikipedia.org/wiki/EDID#EDID_1.4_data_format.
89 { "edid-decode",
90 "for f in /sys/class/drm/card0-*/edid; do"
91 "echo \"----------- ${f}\"; "
92 "sed -E 's/.{4}/\x00\x00\x00\x00/4' \"${f}\" | edid-decode;"
93 "done"
94 },
Stefan Reinauer4393fa72012-10-18 11:08:09 -070095 { "eventlog", "/bin/cat /var/log/eventlog.txt" },
Elly Fong-Jones215b5622013-03-20 14:32:18 -040096 {
97 "exynos_gem_objects",
Yuly Novikov13ece852013-07-11 17:19:10 -040098 "/bin/cat /sys/kernel/debug/dri/0/exynos_gem_objects 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -040099 SandboxedProcess::kDefaultUser,
100 kDebugfsGroup
101 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400102 { "font_info", "/usr/share/userfeedback/scripts/font_info" },
Daisuke Nojiridebede0e2017-02-17 14:21:28 -0800103 { "sensor_info", "/usr/share/userfeedback/scripts/sensor_info" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400104 { "hardware_class", "/usr/bin/crossystem hwid" },
105 { "hostname", "/bin/hostname" },
106 { "hw_platform", "/usr/bin/uname -i" },
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400107 {
108 "i915_gem_gtt",
Yuly Novikov13ece852013-07-11 17:19:10 -0400109 "/bin/cat /sys/kernel/debug/dri/0/i915_gem_gtt 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400110 SandboxedProcess::kDefaultUser,
111 kDebugfsGroup
112 },
113 {
114 "i915_gem_objects",
Yuly Novikov13ece852013-07-11 17:19:10 -0400115 "/bin/cat /sys/kernel/debug/dri/0/i915_gem_objects 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400116 SandboxedProcess::kDefaultUser,
117 kDebugfsGroup
118 },
119 {
120 "i915_error_state",
Yuly Novikov4ac12fb2013-07-18 20:08:44 -0400121 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400122 SandboxedProcess::kDefaultUser,
123 kDebugfsGroup,
124 },
Daniel Erat982ab4b2014-04-09 07:32:38 -0700125 { "ifconfig", "/bin/ifconfig -a" },
Lutz Justenc2265662017-06-27 10:13:23 +0200126 { "input_devices", "/bin/cat /proc/bus/input/devices" },
Eric Carusob1820c02017-08-24 15:39:56 -0700127 // Information about the wiphy device, such as current channel.
128 { "iw_dev", "/usr/sbin/iw dev" },
129 // Hardware capabilities of the wiphy device.
130 { "iw_list", "/usr/sbin/iw list" },
Eric Carusoa879fd92017-10-11 12:57:10 -0700131#if USE_IWLWIFI_DUMP
132 { "iwlmvm_module_params", CMD_KERNEL_MODULE_PARAMS(iwlmvm) },
133 { "iwlwifi_module_params", CMD_KERNEL_MODULE_PARAMS(iwlwifi) },
134#endif // USE_IWLWIFI_DUMP
Stefan Reinauer48f883d2014-08-22 14:31:58 -0700135 { "kernel-crashes",
136 "/bin/cat /var/spool/crash/kernel.*.kcrash 2> /dev/null" },
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700137 { "logcat", "/usr/sbin/android-sh -c '/system/bin/logcat -d'",
138 kRoot,
139 kRoot,
140 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400141 { "lsmod", "lsmod" },
142 { "lspci", "/usr/sbin/lspci" },
Simon Que41c88b52017-06-19 14:05:07 -0400143 { "lsusb", "lsusb && lsusb -t" },
Yuly Novikov13ece852013-07-11 17:19:10 -0400144 {
Thiemo Nagela269ad22014-11-27 17:13:01 +0100145 "mali_memory",
146 "/bin/cat /sys/class/misc/mali0/device/memory 2> /dev/null"
Yuly Novikov13ece852013-07-11 17:19:10 -0400147 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400148 { "meminfo", "cat /proc/meminfo" },
149 { "memory_spd_info", "/bin/cat /var/log/memory_spd_info.txt" },
Simon Quecb63b9c2017-06-19 14:53:31 -0400150 // The sed command finds the EDID blob (starting the line after "value:") and
151 // replaces the serial number with all zeroes.
152 //
153 // The EDID is printed as a hex dump over several lines, each line containing
154 // the contents of 16 bytes. The first 16 bytes are broken down as follows:
155 // uint64_t fixed_pattern; // Always 00 FF FF FF FF FF FF 00.
156 // uint16_t manufacturer_id; // Manufacturer ID, encoded as PNP IDs.
157 // uint16_t product_code; // Manufacturer product code, little-endian.
158 // uint32_t serial_number; // Serial number, little-endian.
159 // Source: https://en.wikipedia.org/wiki/EDID#EDID_1.3_data_format
160 //
161 // The subsequent substitution command looks for the fixed pattern followed by
162 // two 32-bit fields (manufacturer + product, serial number). It replaces the
163 // latter field with 8 bytes of zeroes.
164 //
165 // TODO(crbug.com/731133): Remove the sed command once modetest itself can
166 // remove serial numbers.
167 {
168 "modetest",
169 "(modetest; modetest -M evdi; modetest -M udl) | "
170 "sed -E '/EDID/ {:a;n;/value:/!ba;n;"
171 "s/(00f{12}00)([0-9a-f]{8})([0-9a-f]{8})/\\1\\200000000/}'",
172 kRoot,
173 kRoot,
174 },
Kees Cooka8f3e7e2012-11-19 14:16:01 -0800175 { "mount-encrypted", "/bin/cat /var/log/mount-encrypted.log" },
Junichi Uekawa710cb4b2017-04-10 16:05:27 +0900176 { "mountinfo", "/bin/cat /proc/1/mountinfo" },
Nicolas Boichat0230b0b2017-08-17 11:20:19 +0800177 { "netlog", "/usr/share/userfeedback/scripts/getmsgs /var/log/net.log" },
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700178 // --processes requires root.
179 { "netstat", "/sbin/ss --all --query inet --numeric --processes",
180 kRoot,
181 kRoot,
182 },
henryhsufa6daa12014-09-24 13:26:30 +0800183 {
184 "nvmap_iovmm",
185 "/bin/cat /sys/kernel/debug/nvmap/iovmm/allocations 2> /dev/null",
186 SandboxedProcess::kDefaultUser,
187 kDebugfsGroup,
188 },
Vincent Palatinc64af9d2013-08-05 16:34:10 -0700189 { "platform_info", "/bin/cat /var/log/platform_info.txt" },
Daniel Erat9b58b6d2013-04-30 14:58:56 -0700190 { "power_supply_info", "/usr/bin/power_supply_info" },
Daniel Eratd8b84992017-03-13 17:30:00 -0600191 { "power_supply_sysfs", "/usr/bin/print_sysfs_power_supply_data" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400192 { "powerd.LATEST", "/bin/cat /var/log/power_manager/powerd.LATEST" },
Daniel Erat546c3882013-11-22 12:46:01 -0800193 { "powerd.PREVIOUS", "/bin/cat /var/log/power_manager/powerd.PREVIOUS" },
Daniel Erat93da9a12012-12-06 14:14:17 -0800194 { "powerd.out", "/bin/cat /var/log/powerd.out" },
Thiemo Nagela269ad22014-11-27 17:13:01 +0100195 { "powerwash_count", "/bin/cat /var/log/powerwash_count 2> /dev/null" },
Mattias Nissler887dce22017-07-03 14:44:35 +0200196 // Changed from 'ps ux' to 'ps aux' since we're running as debugd,
197 // not chronos.
Elly Jones03cd6d72012-06-11 13:04:28 -0400198 { "ps", "/bin/ps aux" },
yusukes34171ba2017-04-27 15:46:01 -0700199 // /proc/slabinfo is owned by root and has 0400 permission.
200 { "slabinfo", "/bin/cat /proc/slabinfo", kRoot, kRoot, },
Puthikorn Voravootivat6de5e122013-12-06 11:43:13 -0800201 { "storage_info", "/bin/cat /var/log/storage_info.txt" },
Nicolas Boichat0230b0b2017-08-17 11:20:19 +0800202 { "syslog", "/usr/share/userfeedback/scripts/getmsgs /var/log/messages" },
Brandon Mayere24c7a22017-08-04 13:58:48 -0400203 { "system_log_stats", "echo 'BLOCK_SIZE=1024'; "
204 "find /var/log/ -type f -exec du --block-size=1024 {} + | sort -n -r",
205 kRoot, kRoot},
Elly Fong-Jonesb4f49c42013-02-28 13:45:26 -0500206 { "tlsdate", "/bin/cat /var/log/tlsdate.log" },
Hsu-Cheng Tsaie8c31d52017-03-31 12:34:55 +0800207 { "top thread", "/usr/bin/top -Hb -n 1 | head -n 40"},
208 { "top memory", "/usr/bin/top -o \"+%MEM\" -bn 1 | head -n 57"},
Elly Jones03cd6d72012-06-11 13:04:28 -0400209 { "touchpad", "/opt/google/touchpad/tpcontrol status" },
Chung-yih Wang4b876142014-04-30 17:01:49 +0800210 { "touchpad_activity", "/opt/google/input/cmt_feedback alt" },
Andrew de los Reyesc060c342013-04-10 13:46:30 -0700211 { "touch_fw_version", "grep -E"
Charlie Mooneybb08f982016-02-04 15:35:56 -0800212 " -e 'synaptics: Touchpad model'"
213 " -e 'chromeos-[a-z]*-touch-[a-z]*-update'"
Andrew de los Reyesc060c342013-04-10 13:46:30 -0700214 " /var/log/messages | tail -n 20" },
Mattias Nissler483d76f2017-08-23 16:53:36 +0200215 {
216 "tpm-firmware-updater",
217 "/usr/share/userfeedback/scripts/getmsgs /var/log/tpm-firmware-updater.log"
218 },
Mattias Nissler887dce22017-07-03 14:44:35 +0200219 // TODO(jorgelo,mnissler): Don't run this as root.
220 // On TPM 1.2 devices this will likely require adding a new user to the 'tss'
221 // group.
222 // On TPM 2.0 devices 'get_version_info' uses D-Bus and therefore can run as
223 // any user.
224 { "tpm_version", "/usr/sbin/tpm-manager get_version_info", kRoot, kRoot },
Charlie Mooney69f56702017-05-02 14:55:41 -0700225 { "atmel_ts_refs", "/opt/google/touch/scripts/atmel_tools.sh ts r",
226 kRoot, kRoot},
227 { "atmel_tp_refs", "/opt/google/touch/scripts/atmel_tools.sh tp r",
228 kRoot, kRoot},
229 { "atmel_ts_deltas", "/opt/google/touch/scripts/atmel_tools.sh ts d",
230 kRoot, kRoot},
231 { "atmel_tp_deltas", "/opt/google/touch/scripts/atmel_tools.sh tp d",
232 kRoot, kRoot},
Gwendal Grignou427d94e2016-10-25 11:34:51 -0700233 {
234 "trim",
235 "cat /var/lib/trim/stateful_trim_state /var/lib/trim/stateful_trim_data"
236 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400237 { "ui_log", "/usr/share/userfeedback/scripts/get_log /var/log/ui/ui.LATEST" },
238 { "uname", "/bin/uname -a" },
239 { "update_engine.log", "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
Thiemo Nagela269ad22014-11-27 17:13:01 +0100240 " s.^./var/log/update_engine/.)" },
Dariusz Marcinkiewiczc4126882017-10-13 16:07:36 +0200241 { "uptime", "/usr/bin/cut -d' ' -f1 /proc/uptime" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400242 { "verified boot", "/bin/cat /var/log/debug_vboot_noisy.log" },
Justin TerAvestbf921a32017-05-11 14:31:44 -0600243 { "vmlog.1.LATEST", "/bin/cat /var/log/vmlog/vmlog.1.LATEST" },
244 { "vmlog.1.PREVIOUS", "/bin/cat /var/log/vmlog/vmlog.1.PREVIOUS" },
245 { "vmlog.LATEST", "/bin/cat /var/log/vmlog/vmlog.LATEST" },
246 { "vmlog.PREVIOUS", "/bin/cat /var/log/vmlog/vmlog.PREVIOUS" },
Kuo-Hsin Yange1611f62018-02-26 10:49:01 +0800247 { "vmstat", "/bin/cat /proc/vmstat" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400248 { "vpd_2.0", "/bin/cat /var/log/vpd_2.0.txt" },
Samuel Tan107b6c82014-07-10 15:33:44 -0700249 { "wifi_status", "/usr/bin/network_diag --wifi-internal --no-log" },
Sonny Raoab5f9952013-07-17 14:13:53 -0700250 { "zram compressed data size",
251 "/bin/cat /sys/block/zram0/compr_data_size 2> /dev/null" },
252 { "zram original data size",
253 "/bin/cat /sys/block/zram0/orig_data_size 2> /dev/null" },
254 { "zram total memory used",
255 "/bin/cat /sys/block/zram0/mem_used_total 2> /dev/null" },
256 { "zram total reads",
257 "/bin/cat /sys/block/zram0/num_reads 2> /dev/null" },
258 { "zram total writes",
259 "/bin/cat /sys/block/zram0/num_writes 2> /dev/null" },
Luigi Semenzato0253b142017-07-07 15:59:48 -0700260 { "zram new stats names", "/bin/echo orig_size compr_size used_total limit "
Luigi Semenzato4164de42017-07-06 10:42:54 -0700261 "used_max zero_pages migrated" },
Luigi Semenzato0253b142017-07-07 15:59:48 -0700262 { "zram new stats values", "/bin/cat /sys/block/zram0/mm_stat 2> /dev/null" },
Wei-Ning Huang230b7d82017-07-14 16:19:13 +0800263 { "cros_tp version", "/bin/cat /sys/class/chromeos/cros_tp/version" },
264 { "cros_tp console", "/usr/sbin/ectool --name=cros_tp console",
265 kRoot, kRoot },
Wei-Ning Huang23ffdcf2017-08-04 11:30:39 +0800266 { "cros_tp frame", "/usr/sbin/ectool --name=cros_tp tpframeget",
267 kRoot, kRoot },
Gaurav Shah6e8fd892012-10-01 11:51:08 -0700268
Elly Jones03cd6d72012-06-11 13:04:28 -0400269 // Stuff pulled out of the original list. These need access to the running X
270 // session, which we'd rather not give to debugd, or return info specific to
271 // the current session (in the setsid(2) sense), which is not useful for
272 // debugd
273 // { "env", "set" },
274 // { "setxkbmap", "/usr/bin/setxkbmap -print -query" },
275 // { "xrandr", "/usr/bin/xrandr --verbose" }
Ben Chan64d19b22017-02-06 14:03:47 -0800276 { nullptr, nullptr }
Elly Jones533c7c42012-08-10 15:07:05 -0400277};
278
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700279// netstat and logcat should appear in chrome://system but not in feedback
280// reports. Open sockets may have privacy implications, and logcat is
281// already incorporated via arc-bugreport.
282const std::vector<string> kCommandLogsExclude = {"netstat", "logcat"};
283
Ben Chancf7d6412017-08-10 22:30:09 -0700284const Log kExtraLogs[] = {
Ben Chan36e42282014-02-12 22:32:34 -0800285#if USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400286 { "mm-status", "/usr/bin/modem status" },
Ben Chan36e42282014-02-12 22:32:34 -0800287#endif // USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400288 { "network-devices", "/usr/bin/connectivity show devices" },
289 { "network-services", "/usr/bin/connectivity show services" },
Ben Chan64d19b22017-02-06 14:03:47 -0800290 { nullptr, nullptr }
Elly Jones533c7c42012-08-10 15:07:05 -0400291};
292
Ben Chancf7d6412017-08-10 22:30:09 -0700293const Log kFeedbackLogs[] = {
Ben Chan36e42282014-02-12 22:32:34 -0800294#if USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400295 { "mm-status", "/usr/bin/modem status-feedback" },
Ben Chan36e42282014-02-12 22:32:34 -0800296#endif // USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400297 { "network-devices", "/usr/bin/connectivity show-feedback devices" },
298 { "network-services", "/usr/bin/connectivity show-feedback services" },
Ben Chan64d19b22017-02-06 14:03:47 -0800299 { nullptr, nullptr }
Elly Jones03cd6d72012-06-11 13:04:28 -0400300};
301
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700302// List of log files needed to be part of the feedback report that are huge and
303// must be sent back to the client via the file descriptor using
304// LogTool::GetBigFeedbackLogs().
Ben Chancf7d6412017-08-10 22:30:09 -0700305const Log kBigFeedbackLogs[] = {
Eric Carusoe3166f22016-06-10 15:36:02 -0700306 { "arc-bugreport",
Yusuke Sato874c0a82016-10-13 15:41:37 -0700307 "/bin/cat /run/arc/bugreport/pipe 2> /dev/null",
Eric Carusoe3166f22016-06-10 15:36:02 -0700308 // ARC bugreport permissions are weird. Since we're just running cat, this
309 // shouldn't cause any issues.
310 kRoot,
Ricky Zhou4c473ca2016-06-21 17:46:58 -0700311 kRoot,
312 "10M",
Eric Carusoe3166f22016-06-10 15:36:02 -0700313 },
Ben Chan64d19b22017-02-06 14:03:47 -0800314 { nullptr, nullptr }
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700315};
316
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700317// List of log files that must directly be collected by Chrome. This is because
318// debugd is running under a VFS namespace and does not have access to later
319// cryptohome mounts.
Ben Chancf7d6412017-08-10 22:30:09 -0700320const Log kUserLogs[] = {
Elly Fong-Jones6456ce52013-04-17 13:31:13 -0400321 {"chrome_user_log", "log/chrome"},
322 {"login-times", "login-times"},
323 {"logout-times", "logout-times"},
Ben Chan64d19b22017-02-06 14:03:47 -0800324 { nullptr, nullptr}
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700325};
326
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800327// Returns |value| if |value| is a valid UTF-8 string or a base64-encoded
328// string of |value| otherwise.
329string EnsureUTF8String(const string& value) {
330 if (base::IsStringUTF8(value))
331 return value;
332
Eric Caruso96d03d32017-04-25 18:01:17 -0700333 string encoded_value;
Ben Chanab93abf2017-01-24 13:32:51 -0800334 base::Base64Encode(value, &encoded_value);
335 return "<base64>: " + encoded_value;
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800336}
337
338// TODO(ellyjones): sandbox. crosbug.com/35122
339string Run(const Log& log) {
340 string output;
341 ProcessWithOutput p;
Eric Caruso96d03d32017-04-25 18:01:17 -0700342 string tailed_cmdline = string(log.command) + " | tail -c " +
Ahmed Fakhrye4a0b502016-09-19 18:18:49 -0700343 (log.size_cap ? log.size_cap : "512K");
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800344 if (log.user && log.group)
345 p.SandboxAs(log.user, log.group);
346 if (!p.Init())
347 return "<not available>";
348 p.AddArg(kShell);
349 p.AddStringOption("-c", tailed_cmdline);
350 if (p.Run())
351 return "<not available>";
352 p.GetOutput(&output);
353 if (!output.size())
354 return "<empty>";
355 return EnsureUTF8String(output);
356}
357
358// Fills |dictionary| with the anonymized contents of the logs in |logs|.
359void GetLogsInDictionary(const struct Log* logs,
360 AnonymizerTool* anonymizer,
361 base::DictionaryValue* dictionary) {
362 for (size_t i = 0; logs[i].name; ++i) {
363 dictionary->SetStringWithoutPathExpansion(
364 logs[i].name, anonymizer->Anonymize(Run(logs[i])));
365 }
366}
367
368// Serializes the |dictionary| into the file with the given |fd| in a JSON
369// format.
370void SerializeLogsAsJSON(const base::DictionaryValue& dictionary,
Eric Caruso0b241882018-04-04 13:43:46 -0700371 const base::ScopedFD& fd) {
Eric Caruso96d03d32017-04-25 18:01:17 -0700372 string logs_json;
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800373 base::JSONWriter::WriteWithOptions(dictionary,
374 base::JSONWriter::OPTIONS_PRETTY_PRINT,
375 &logs_json);
Eric Caruso0b241882018-04-04 13:43:46 -0700376 base::WriteFileDescriptor(fd.get(), logs_json.c_str(), logs_json.size());
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800377}
378
Elly Jones533c7c42012-08-10 15:07:05 -0400379bool GetNamedLogFrom(const string& name, const struct Log* logs,
380 string* result) {
381 for (size_t i = 0; logs[i].name; i++) {
382 if (name == logs[i].name) {
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -0500383 *result = Run(logs[i]);
Elly Jones533c7c42012-08-10 15:07:05 -0400384 return true;
385 }
386 }
387 *result = "<invalid log name>";
388 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400389}
390
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800391void GetLogsFrom(const struct Log* logs, LogTool::LogMap* map) {
392 for (size_t i = 0; logs[i].name; i++)
393 (*map)[logs[i].name] = Run(logs[i]);
394}
395
396} // namespace
397
Eric Carusof9091f82017-04-28 14:18:59 -0700398void LogTool::CreateConnectivityReport() {
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700399 // Perform ConnectivityTrial to report connection state in feedback log.
Ben Chan8e9f6d02017-09-26 23:04:21 -0700400 auto shill = std::make_unique<org::chromium::flimflam::ManagerProxy>(bus_);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700401 // Give the connection trial time to test the connection and log the results
402 // before collecting the logs for feedback.
403 // TODO(silberst): Replace the simple approach of a single timeout with a more
404 // coordinated effort.
Eric Carusocc7106c2017-04-27 14:22:42 -0700405 if (shill && shill->CreateConnectivityReport(nullptr))
406 sleep(kConnectionTesterTimeoutSeconds);
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700407}
408
Eric Carusoc93a15c2017-04-24 16:15:12 -0700409string LogTool::GetLog(const string& name) {
Elly Jones533c7c42012-08-10 15:07:05 -0400410 string result;
Ben Chancf7d6412017-08-10 22:30:09 -0700411 GetNamedLogFrom(name, kCommandLogs, &result)
412 || GetNamedLogFrom(name, kExtraLogs, &result)
413 || GetNamedLogFrom(name, kFeedbackLogs, &result);
Elly Jones533c7c42012-08-10 15:07:05 -0400414 return result;
415}
416
Eric Carusof9091f82017-04-28 14:18:59 -0700417LogTool::LogMap LogTool::GetAllLogs() {
418 CreateConnectivityReport();
Elly Jones533c7c42012-08-10 15:07:05 -0400419 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700420 GetLogsFrom(kCommandLogs, &result);
421 GetLogsFrom(kExtraLogs, &result);
Elly Jones533c7c42012-08-10 15:07:05 -0400422 return result;
423}
424
Eric Carusof9091f82017-04-28 14:18:59 -0700425LogTool::LogMap LogTool::GetFeedbackLogs() {
426 CreateConnectivityReport();
Elly Jones533c7c42012-08-10 15:07:05 -0400427 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700428 GetLogsFrom(kCommandLogs, &result);
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700429 for (const auto& key : kCommandLogsExclude) {
430 result.erase(key);
431 }
Ben Chancf7d6412017-08-10 22:30:09 -0700432 GetLogsFrom(kFeedbackLogs, &result);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100433 AnonymizeLogMap(&result);
Elly Jones03cd6d72012-06-11 13:04:28 -0400434 return result;
435}
436
Eric Caruso0b241882018-04-04 13:43:46 -0700437void LogTool::GetBigFeedbackLogs(const base::ScopedFD& fd) {
Eric Carusof9091f82017-04-28 14:18:59 -0700438 CreateConnectivityReport();
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800439 base::DictionaryValue dictionary;
Ben Chancf7d6412017-08-10 22:30:09 -0700440 GetLogsInDictionary(kCommandLogs, &anonymizer_, &dictionary);
Kevin Cernekee143e2af2018-03-20 13:28:20 -0700441 for (const auto& key : kCommandLogsExclude) {
442 dictionary.Remove(key, nullptr);
443 }
Ben Chancf7d6412017-08-10 22:30:09 -0700444 GetLogsInDictionary(kFeedbackLogs, &anonymizer_, &dictionary);
445 GetLogsInDictionary(kBigFeedbackLogs, &anonymizer_, &dictionary);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800446 SerializeLogsAsJSON(dictionary, fd);
447}
448
Eric Carusoc93a15c2017-04-24 16:15:12 -0700449LogTool::LogMap LogTool::GetUserLogFiles() {
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700450 LogMap result;
Ben Chancf7d6412017-08-10 22:30:09 -0700451 for (size_t i = 0; kUserLogs[i].name; ++i)
452 result[kUserLogs[i].name] = kUserLogs[i].command;
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700453 return result;
454}
455
Darin Petkovce9b3a12013-01-10 16:38:54 +0100456void LogTool::AnonymizeLogMap(LogMap* log_map) {
Ben Chane2fa3572017-02-08 22:46:18 -0800457 for (auto& entry : *log_map)
458 entry.second = anonymizer_.Anonymize(entry.second);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100459}
460
Ben Chana0011d82014-05-13 00:19:29 -0700461} // namespace debugd