Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 1 | // 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 Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 5 | #include "debugd/src/log_tool.h" |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 6 | |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 7 | #include <grp.h> |
| 8 | #include <inttypes.h> |
Chinglin Yu | aeb4ec7 | 2018-12-10 18:53:30 +0800 | [diff] [blame] | 9 | #include <lzma.h> |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 10 | #include <pwd.h> |
| 11 | #include <stdint.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <unistd.h> |
Ben Chan | 8e9f6d0 | 2017-09-26 23:04:21 -0700 | [diff] [blame] | 14 | #include <memory> |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 15 | #include <string> |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 16 | #include <utility> |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Ben Chan | ab93abf | 2017-01-24 13:32:51 -0800 | [diff] [blame] | 19 | #include <base/base64.h> |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 20 | #include <base/files/file.h> |
| 21 | #include <base/files/file_path.h> |
Ben Chan | cd8fda4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 22 | #include <base/files/file_util.h> |
Ahmed Fakhry | 21140cf | 2016-03-04 17:15:19 -0800 | [diff] [blame] | 23 | #include <base/json/json_writer.h> |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 24 | #include <base/logging.h> |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 25 | #include <base/strings/string_split.h> |
| 26 | #include <base/strings/string_util.h> |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 27 | #include <base/strings/stringprintf.h> |
| 28 | #include <base/strings/utf_string_conversion_utils.h> |
Ahmed Fakhry | 21140cf | 2016-03-04 17:15:19 -0800 | [diff] [blame] | 29 | #include <base/values.h> |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 30 | |
Rebecca Silberstein | e78af40 | 2014-10-02 10:55:04 -0700 | [diff] [blame] | 31 | #include <chromeos/dbus/service_constants.h> |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 32 | #include <shill/dbus-proxies.h> |
Rebecca Silberstein | e78af40 | 2014-10-02 10:55:04 -0700 | [diff] [blame] | 33 | |
Ben Chan | af12586 | 2017-02-08 23:11:18 -0800 | [diff] [blame] | 34 | #include "debugd/src/constants.h" |
Chinglin Yu | aeb4ec7 | 2018-12-10 18:53:30 +0800 | [diff] [blame] | 35 | #include "debugd/src/perf_tool.h" |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 36 | #include "debugd/src/process_with_output.h" |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 37 | |
Kartik Hegde | 1c4b97b | 2018-09-09 19:09:34 -0600 | [diff] [blame] | 38 | #include "brillo/key_value_store.h" |
| 39 | #include <brillo/osrelease_reader.h> |
| 40 | |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 41 | namespace debugd { |
| 42 | |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 43 | using std::string; |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 44 | |
Eric Caruso | 96d03d3 | 2017-04-25 18:01:17 -0700 | [diff] [blame] | 45 | using Strings = std::vector<string>; |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 46 | |
Ahmed Fakhry | 21140cf | 2016-03-04 17:15:19 -0800 | [diff] [blame] | 47 | namespace { |
| 48 | |
Ben Chan | af12586 | 2017-02-08 23:11:18 -0800 | [diff] [blame] | 49 | const char kRoot[] = "root"; |
| 50 | const char kShell[] = "/bin/sh"; |
Kartik Hegde | 1c4b97b | 2018-09-09 19:09:34 -0600 | [diff] [blame] | 51 | constexpr char kLsbReleasePath[] = "/etc/lsb-release"; |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 52 | constexpr char kArcBugReportBackupFileName[] = "arc-bugreport.log"; |
mhasank | d2b8488 | 2020-05-04 17:02:19 -0700 | [diff] [blame] | 53 | constexpr char kArcBugReportBackupKey[] = "arc-bugreport-backup"; |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 54 | constexpr char kDaemonStoreBaseDir[] = "/run/daemon-store/debugd/"; |
Ahmed Fakhry | 21140cf | 2016-03-04 17:15:19 -0800 | [diff] [blame] | 55 | |
| 56 | // Minimum time in seconds needed to allow shill to test active connections. |
| 57 | const int kConnectionTesterTimeoutSeconds = 5; |
Ben Chan | f6cd93a | 2012-10-14 19:37:00 -0700 | [diff] [blame] | 58 | |
Chinglin Yu | aeb4ec7 | 2018-12-10 18:53:30 +0800 | [diff] [blame] | 59 | // Default running perf for 2 seconds. |
| 60 | constexpr const int kPerfDurationSecs = 2; |
Chinglin Yu | 3c8d0a2 | 2019-02-20 11:32:52 +0800 | [diff] [blame] | 61 | // TODO(chinglinyu) Remove after crbug/934702 is fixed. |
| 62 | // The following description is added to 'perf-data' as a temporary solution |
| 63 | // before the update of feedback disclosure to users is done in crbug/934702. |
| 64 | constexpr const char kPerfDataDescription[] = |
| 65 | "perf-data contains performance profiling information about how much time " |
| 66 | "the system spends on various activities (program execution stack traces). " |
| 67 | "This might reveal some information about what system features and " |
| 68 | "resources are being used. The full detail of perf-data can be found in " |
| 69 | "the PerfDataProto protocol buffer message type in the chromium source " |
| 70 | "repository.\n"; |
Chinglin Yu | aeb4ec7 | 2018-12-10 18:53:30 +0800 | [diff] [blame] | 71 | |
Eric Caruso | a879fd9 | 2017-10-11 12:57:10 -0700 | [diff] [blame] | 72 | #define CMD_KERNEL_MODULE_PARAMS(module_name) \ |
Luigi Semenzato | 7e2c08f | 2018-06-26 14:58:49 -0700 | [diff] [blame] | 73 | "cd /sys/module/" #module_name "/parameters 2>/dev/null && grep -sH ^ *" |
Eric Caruso | a879fd9 | 2017-10-11 12:57:10 -0700 | [diff] [blame] | 74 | |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 75 | using Log = LogTool::Log; |
| 76 | constexpr Log::LogType kCommand = Log::kCommand; |
| 77 | constexpr Log::LogType kFile = Log::kFile; |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 78 | |
| 79 | class ArcBugReportLog : public LogTool::Log { |
| 80 | public: |
| 81 | ArcBugReportLog() |
| 82 | : Log(kCommand, |
| 83 | "arc-bugreport", |
| 84 | "/usr/bin/nsenter -t1 -m /usr/sbin/android-sh -c " |
| 85 | "/system/bin/arc-bugreport", |
| 86 | kRoot, |
| 87 | kRoot, |
| 88 | 10 * 1024 * 1024 /*10 MiB*/, |
| 89 | LogTool::Encoding::kUtf8) {} |
| 90 | |
| 91 | virtual ~ArcBugReportLog() = default; |
| 92 | }; |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 93 | |
Miriam Zimmerman | d91d8e7 | 2019-06-27 12:24:04 -0700 | [diff] [blame] | 94 | // NOTE: IF YOU ADD AN ENTRY TO THIS LIST, PLEASE: |
| 95 | // * add a row to http://go/cros-feedback-audit and fill it out |
Miriam Zimmerman | 4f142ba | 2020-06-01 14:15:21 -0700 | [diff] [blame] | 96 | // * email cros-telemetry@ |
Miriam Zimmerman | d91d8e7 | 2019-06-27 12:24:04 -0700 | [diff] [blame] | 97 | // (Eventually we'll have a better process, but for now please do this.) |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 98 | const std::vector<Log> kCommandLogs { |
Mike Frysinger | b035099 | 2018-09-14 13:45:35 -0400 | [diff] [blame] | 99 | // We need to enter init's mount namespace because it has /home/chronos |
| 100 | // mounted which is where the consent knob lives. We don't have that mount |
| 101 | // in our own mount namespace (by design). https://crbug.com/884249 |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 102 | {kCommand, "CLIENT_ID", "/usr/bin/nsenter -t1 -m /usr/bin/metrics_client -i", |
| 103 | kRoot, kDebugfsGroup}, |
| 104 | {kCommand, "LOGDATE", "/bin/date"}, |
Yusuke Sato | 27a3167 | 2019-04-29 15:26:37 -0700 | [diff] [blame] | 105 | // We need to enter init's mount namespace to access /home/root. Also, we use |
| 106 | // neither ARC container's mount namespace (with android-sh) nor |
| 107 | // /opt/google/containers/android/rootfs/android-data/ so that we can get |
| 108 | // results even when the container is down. |
| 109 | {kCommand, "android_app_storage", "/usr/bin/nsenter -t1 -m " |
| 110 | "/bin/sh -c \"/usr/bin/du -h /home/root/*/android-data/data/\"", |
| 111 | kRoot, kDebugfsGroup}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 112 | {kFile, "atrus_logs", "/var/log/atrus.log"}, |
| 113 | {kFile, "authpolicy", "/var/log/authpolicy.log"}, |
Brian Norris | afc9f63 | 2019-05-09 14:08:28 -0700 | [diff] [blame] | 114 | {kCommand, "bootstat_summary", "/usr/bin/bootstat_summary", |
| 115 | SandboxedProcess::kDefaultUser, SandboxedProcess::kDefaultGroup, |
| 116 | Log::kDefaultMaxBytes, LogTool::Encoding::kAutodetect, true}, |
Craig Hesling | 5c384b5 | 2019-04-20 15:18:06 -0700 | [diff] [blame] | 117 | {kFile, "bio_crypto_init.LATEST", |
| 118 | "/var/log/bio_crypto_init/bio_crypto_init.LATEST"}, |
| 119 | {kFile, "bio_crypto_init.PREVIOUS", |
| 120 | "/var/log/bio_crypto_init/bio_crypto_init.PREVIOUS"}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 121 | {kFile, "biod.LATEST", "/var/log/biod/biod.LATEST"}, |
| 122 | {kFile, "biod.PREVIOUS", "/var/log/biod/biod.PREVIOUS"}, |
Craig Hesling | 4c3891e | 2019-04-20 12:53:54 -0700 | [diff] [blame] | 123 | {kFile, "bio_fw_updater.LATEST", "/var/log/biod/bio_fw_updater.LATEST"}, |
| 124 | {kFile, "bio_fw_updater.PREVIOUS", "/var/log/biod/bio_fw_updater.PREVIOUS"}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 125 | {kFile, "bios_info", "/var/log/bios_info.txt"}, |
| 126 | {kCommand, "bios_log", "cat /sys/firmware/log " |
| 127 | "/proc/device-tree/chosen/ap-console-buffer 2>/dev/null"}, |
| 128 | {kFile, "bios_times", "/var/log/bios_times.txt"}, |
| 129 | {kCommand, "board-specific", |
| 130 | "/usr/share/userfeedback/scripts/get_board_specific_info"}, |
Anand K Mistry | ccceb1e | 2020-01-16 14:00:49 +1100 | [diff] [blame] | 131 | // Slow or non-responsive block devices could cause this command to stall. Use |
| 132 | // a timeout to prevent this command from blocking log fetching. This command |
| 133 | // is expected to take O(100ms) in the normal case. |
| 134 | {kCommand, "blkid", "timeout -s KILL 5s /sbin/blkid", kRoot, kRoot}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 135 | {kFile, "buddyinfo", "/proc/buddyinfo"}, |
| 136 | {kCommand, "cbi_info", "/usr/share/userfeedback/scripts/cbi_info", kRoot, |
| 137 | kRoot}, |
| 138 | {kFile, "cheets_log", "/var/log/arc.log"}, |
| 139 | {kFile, "clobber.log", "/var/log/clobber.log"}, |
| 140 | {kFile, "clobber-state.log", "/var/log/clobber-state.log"}, |
Sonny Rao | bd3dc00 | 2020-05-27 21:40:35 -0700 | [diff] [blame^] | 141 | {kCommand, "chromeos-pgmem", "/usr/bin/chromeos-pgmem", kRoot, kRoot}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 142 | {kFile, "chrome_system_log", "/var/log/chrome/chrome"}, |
| 143 | {kFile, "chrome_system_log.PREVIOUS", "/var/log/chrome/chrome.PREVIOUS"}, |
Mike Frysinger | 32cdf3e | 2017-08-14 18:17:06 -0400 | [diff] [blame] | 144 | // There might be more than one record, so grab them all. |
| 145 | // Plus, for <linux-3.19, it's named "console-ramoops", but for newer |
| 146 | // versions, it's named "console-ramoops-#". |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 147 | {kCommand, "console-ramoops", |
| 148 | "cat /sys/fs/pstore/console-ramoops* 2>/dev/null"}, |
| 149 | {kFile, "cpuinfo", "/proc/cpuinfo"}, |
| 150 | {kFile, "cr50_version", "/var/cache/cr50-version"}, |
| 151 | {kFile, "cros_ec.log", "/var/log/cros_ec.log"}, |
| 152 | {kFile, "cros_ec.previous", "/var/log/cros_ec.previous"}, |
| 153 | {kFile, "cros_ec_panicinfo", "/sys/kernel/debug/cros_ec/panicinfo", |
| 154 | SandboxedProcess::kDefaultUser, kDebugfsGroup}, |
| 155 | {kFile, "cros_ec_pdinfo", "/sys/kernel/debug/cros_ec/pdinfo", |
| 156 | SandboxedProcess::kDefaultUser, kDebugfsGroup}, |
| 157 | {kFile, "cros_fp.previous", "/var/log/cros_fp.previous"}, |
| 158 | {kFile, "cros_fp.log", "/var/log/cros_fp.log"}, |
Mathew King | d7a7262 | 2019-06-27 09:56:34 -0600 | [diff] [blame] | 159 | {kFile, "cros_ish.previous", "/var/log/cros_ish.previous"}, |
| 160 | {kFile, "cros_ish.log", "/var/log/cros_ish.log"}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 161 | {kCommand, "dmesg", "/bin/dmesg"}, |
| 162 | {kFile, "ec_info", "/var/log/ec_info.txt"}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 163 | {kCommand, "edid-decode", |
| 164 | "for f in /sys/class/drm/card0-*/edid; do " |
| 165 | "echo \"----------- ${f}\"; " |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 166 | // edid-decode's stderr output is redundant, so silence it. |
Jeffrey Kardatzke | 9ba9f32 | 2019-08-29 10:23:14 -0700 | [diff] [blame] | 167 | "edid-decode \"${f}\" 2>/dev/null; " |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 168 | "done"}, |
| 169 | {kFile, "eventlog", "/var/log/eventlog.txt"}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 170 | {kCommand, "font_info", "/usr/share/userfeedback/scripts/font_info"}, |
Kuo-Hsin Yang | 95296e1 | 2020-03-06 17:52:35 +0800 | [diff] [blame] | 171 | {kCommand, "framebuffer", "cat /sys/kernel/debug/dri/?/framebuffer", |
| 172 | SandboxedProcess::kDefaultUser, kDebugfsGroup}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 173 | {kFile, "fwupd_state", "/var/lib/fwupd/state.json"}, |
| 174 | {kCommand, "sensor_info", "/usr/share/userfeedback/scripts/sensor_info"}, |
| 175 | {kFile, "hammerd", "/var/log/hammerd.log"}, |
| 176 | {kCommand, "hardware_class", "/usr/bin/crossystem hwid"}, |
Yong Hong | 15e4b03 | 2020-03-05 15:41:31 +0800 | [diff] [blame] | 177 | {kFile, "hardware_verification_report", |
| 178 | "/var/cache/hardware_verifier.result"}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 179 | {kCommand, "hostname", "/bin/hostname"}, |
| 180 | {kFile, "i915_gem_gtt", "/sys/kernel/debug/dri/0/i915_gem_gtt", |
| 181 | SandboxedProcess::kDefaultUser, kDebugfsGroup}, |
| 182 | {kFile, "i915_gem_objects", "/sys/kernel/debug/dri/0/i915_gem_objects", |
| 183 | SandboxedProcess::kDefaultUser, kDebugfsGroup}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 184 | {kCommand, "i915_error_state", |
| 185 | "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2>/dev/null", |
| 186 | SandboxedProcess::kDefaultUser, kDebugfsGroup, Log::kDefaultMaxBytes, |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 187 | LogTool::Encoding::kBase64}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 188 | {kCommand, "ifconfig", "/bin/ifconfig -a"}, |
| 189 | {kFile, "input_devices", "/proc/bus/input/devices"}, |
Eric Caruso | b1820c0 | 2017-08-24 15:39:56 -0700 | [diff] [blame] | 190 | // Hardware capabilities of the wiphy device. |
Alex Levin | e1c6d57 | 2019-09-17 14:45:33 -0700 | [diff] [blame] | 191 | {kFile, "interrupts", "/proc/interrupts"}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 192 | {kCommand, "iw_list", "/usr/sbin/iw list"}, |
Eric Caruso | a879fd9 | 2017-10-11 12:57:10 -0700 | [diff] [blame] | 193 | #if USE_IWLWIFI_DUMP |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 194 | {kCommand, "iwlmvm_module_params", CMD_KERNEL_MODULE_PARAMS(iwlmvm)}, |
| 195 | {kCommand, "iwlwifi_module_params", CMD_KERNEL_MODULE_PARAMS(iwlwifi)}, |
Eric Caruso | a879fd9 | 2017-10-11 12:57:10 -0700 | [diff] [blame] | 196 | #endif // USE_IWLWIFI_DUMP |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 197 | {kCommand, "kernel-crashes", |
| 198 | "cat /var/spool/crash/kernel.*.kcrash 2>/dev/null"}, |
Anand K Mistry | ccceb1e | 2020-01-16 14:00:49 +1100 | [diff] [blame] | 199 | {kCommand, "lsblk", "timeout -s KILL 5s lsblk -a", kRoot, kRoot, |
| 200 | Log::kDefaultMaxBytes, LogTool::Encoding::kAutodetect, true}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 201 | {kCommand, "lsmod", "lsmod"}, |
| 202 | {kCommand, "lspci", "/usr/sbin/lspci"}, |
| 203 | {kCommand, "lsusb", "lsusb && lsusb -t"}, |
Kuo-Hsin Yang | 66d8983 | 2020-02-10 17:22:28 +0800 | [diff] [blame] | 204 | {kFile, "mali_memory", "/sys/kernel/debug/mali0/gpu_memory", |
| 205 | SandboxedProcess::kDefaultUser, kDebugfsGroup}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 206 | {kFile, "memd.parameters", "/var/log/memd/memd.parameters"}, |
| 207 | {kCommand, "memd clips", "cat /var/log/memd/memd.clip* 2>/dev/null"}, |
| 208 | {kFile, "meminfo", "/proc/meminfo"}, |
| 209 | {kCommand, "memory_spd_info", |
| 210 | // mosys may use 'i2c-dev', which may not be loaded yet. |
| 211 | "modprobe i2c-dev 2>/dev/null && mosys -l memory spd print all 2>/dev/null", |
| 212 | kRoot, kDebugfsGroup}, |
Simon Que | cb63b9c | 2017-06-19 14:53:31 -0400 | [diff] [blame] | 213 | // The sed command finds the EDID blob (starting the line after "value:") and |
| 214 | // replaces the serial number with all zeroes. |
| 215 | // |
| 216 | // The EDID is printed as a hex dump over several lines, each line containing |
| 217 | // the contents of 16 bytes. The first 16 bytes are broken down as follows: |
| 218 | // uint64_t fixed_pattern; // Always 00 FF FF FF FF FF FF 00. |
| 219 | // uint16_t manufacturer_id; // Manufacturer ID, encoded as PNP IDs. |
| 220 | // uint16_t product_code; // Manufacturer product code, little-endian. |
| 221 | // uint32_t serial_number; // Serial number, little-endian. |
| 222 | // Source: https://en.wikipedia.org/wiki/EDID#EDID_1.3_data_format |
| 223 | // |
| 224 | // The subsequent substitution command looks for the fixed pattern followed by |
| 225 | // two 32-bit fields (manufacturer + product, serial number). It replaces the |
| 226 | // latter field with 8 bytes of zeroes. |
| 227 | // |
| 228 | // TODO(crbug.com/731133): Remove the sed command once modetest itself can |
| 229 | // remove serial numbers. |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 230 | {kCommand, "modetest", |
| 231 | "(modetest; modetest -M evdi; modetest -M udl) | " |
| 232 | "sed -E '/EDID/ {:a;n;/value:/!ba;n;" |
| 233 | "s/(00f{12}00)([0-9a-f]{8})([0-9a-f]{8})/\\1\\200000000/}'", |
| 234 | kRoot, kRoot}, |
| 235 | {kFile, "mount-encrypted", "/var/log/mount-encrypted.log"}, |
| 236 | {kFile, "mountinfo", "/proc/1/mountinfo"}, |
| 237 | {kCommand, "netlog", |
| 238 | "/usr/share/userfeedback/scripts/getmsgs /var/log/net.log"}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 239 | {kFile, "nvmap_iovmm", "/sys/kernel/debug/nvmap/iovmm/allocations", |
| 240 | SandboxedProcess::kDefaultUser, kDebugfsGroup}, |
| 241 | {kCommand, "oemdata", "/usr/share/cros/oemdata.sh", kRoot, kRoot}, |
Kuo-Hsin Yang | a69ecc6 | 2020-03-11 17:37:11 +0800 | [diff] [blame] | 242 | {kFile, "pagetypeinfo", "/proc/pagetypeinfo", kRoot}, |
Jack Rosenthal | 3cf794a | 2020-02-19 13:32:56 -0700 | [diff] [blame] | 243 | {kFile, "platform_identity_name", |
| 244 | "/run/chromeos-config/v1/identity/platform-name"}, |
| 245 | {kFile, "platform_identity_model", "/run/chromeos-config/v1/name"}, |
| 246 | {kFile, "platform_identity_sku", "/run/chromeos-config/v1/identity/sku-id"}, |
| 247 | {kFile, "platform_identity_whitelabel_tag", |
| 248 | "/run/chromeos-config/v1/identity/whitelabel-tag"}, |
| 249 | {kFile, "platform_identity_customization_id", |
| 250 | "/run/chromeos-config/v1/identity/customization-id"}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 251 | {kCommand, "power_supply_info", "/usr/bin/power_supply_info"}, |
| 252 | {kCommand, "power_supply_sysfs", "/usr/bin/print_sysfs_power_supply_data"}, |
| 253 | {kFile, "powerd.LATEST", "/var/log/power_manager/powerd.LATEST"}, |
| 254 | {kFile, "powerd.PREVIOUS", "/var/log/power_manager/powerd.PREVIOUS"}, |
| 255 | {kFile, "powerd.out", "/var/log/powerd.out"}, |
| 256 | {kFile, "powerwash_count", "/var/log/powerwash_count"}, |
Brian Norris | 4cde3d1 | 2019-04-16 10:10:34 -0700 | [diff] [blame] | 257 | {kCommand, "ps", "/bin/ps auxZ"}, |
yusukes | 34171ba | 2017-04-27 15:46:01 -0700 | [diff] [blame] | 258 | // /proc/slabinfo is owned by root and has 0400 permission. |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 259 | {kFile, "slabinfo", "/proc/slabinfo", kRoot, kRoot}, |
| 260 | {kFile, "storage_info", "/var/log/storage_info.txt"}, |
| 261 | {kCommand, "swap_info", "/usr/share/cros/init/swap.sh status 2>/dev/null", |
| 262 | SandboxedProcess::kDefaultUser, kDebugfsGroup}, |
| 263 | {kCommand, "syslog", |
| 264 | "/usr/share/userfeedback/scripts/getmsgs /var/log/messages"}, |
| 265 | {kCommand, "system_log_stats", |
| 266 | "echo 'BLOCK_SIZE=1024'; " |
| 267 | "find /var/log/ -type f -exec du --block-size=1024 {} + | sort -n -r", |
| 268 | kRoot, kRoot}, |
| 269 | {kCommand, "threads", "/bin/ps -T axo pid,ppid,spid,pcpu,ni,stat,time,comm"}, |
| 270 | {kFile, "tlsdate", "/var/log/tlsdate.log"}, |
Nick Sanders | ad5dc13 | 2019-11-15 15:59:42 -0800 | [diff] [blame] | 271 | {kCommand, "top thread", "/usr/bin/top -Hbc -w128 -n 1 | head -n 40"}, |
| 272 | {kCommand, "top memory", |
| 273 | "/usr/bin/top -o \"+%MEM\" -w128 -bcn 1 | head -n 57"}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 274 | {kCommand, "touch_fw_version", |
| 275 | "grep -E" |
| 276 | " -e 'synaptics: Touchpad model'" |
| 277 | " -e 'chromeos-[a-z]*-touch-[a-z]*-update'" |
| 278 | " /var/log/messages | tail -n 20"}, |
| 279 | {kCommand, "tpm-firmware-updater", "/usr/share/userfeedback/scripts/getmsgs " |
| 280 | "/var/log/tpm-firmware-updater.log"}, |
Mattias Nissler | 887dce2 | 2017-07-03 14:44:35 +0200 | [diff] [blame] | 281 | // TODO(jorgelo,mnissler): Don't run this as root. |
| 282 | // On TPM 1.2 devices this will likely require adding a new user to the 'tss' |
| 283 | // group. |
| 284 | // On TPM 2.0 devices 'get_version_info' uses D-Bus and therefore can run as |
| 285 | // any user. |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 286 | {kCommand, "tpm_version", "/usr/sbin/tpm-manager get_version_info", kRoot, |
| 287 | kRoot}, |
| 288 | {kCommand, "atmel_ts_refs", |
| 289 | "/opt/google/touch/scripts/atmel_tools.sh ts r", kRoot, kRoot}, |
| 290 | {kCommand, "atmel_tp_refs", |
| 291 | "/opt/google/touch/scripts/atmel_tools.sh tp r", kRoot, kRoot}, |
| 292 | {kCommand, "atmel_ts_deltas", |
| 293 | "/opt/google/touch/scripts/atmel_tools.sh ts d", kRoot, kRoot}, |
| 294 | {kCommand, "atmel_tp_deltas", |
| 295 | "/opt/google/touch/scripts/atmel_tools.sh tp d", kRoot, kRoot}, |
| 296 | {kFile, "stateful_trim_state", "/var/lib/trim/stateful_trim_state"}, |
| 297 | {kFile, "stateful_trim_data", "/var/lib/trim/stateful_trim_data"}, |
| 298 | {kFile, "ui_log", "/var/log/ui/ui.LATEST"}, |
| 299 | {kCommand, "uname", "/bin/uname -a"}, |
| 300 | {kCommand, "update_engine.log", |
| 301 | "cat $(ls -1tr /var/log/update_engine | tail -5 | sed" |
| 302 | " s.^./var/log/update_engine/.)"}, |
Chris Morin | ca15271 | 2019-05-03 13:17:28 -0700 | [diff] [blame] | 303 | {kFile, "upstart", "/var/log/upstart.log"}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 304 | {kCommand, "uptime", "/usr/bin/cut -d' ' -f1 /proc/uptime"}, |
| 305 | {kFile, "verified boot", "/var/log/debug_vboot_noisy.log"}, |
| 306 | {kFile, "vmlog.1.LATEST", "/var/log/vmlog/vmlog.1.LATEST"}, |
| 307 | {kFile, "vmlog.1.PREVIOUS", "/var/log/vmlog/vmlog.1.PREVIOUS"}, |
| 308 | {kFile, "vmlog.LATEST", "/var/log/vmlog/vmlog.LATEST"}, |
| 309 | {kFile, "vmlog.PREVIOUS", "/var/log/vmlog/vmlog.PREVIOUS"}, |
| 310 | {kFile, "vmstat", "/proc/vmstat"}, |
| 311 | {kFile, "vpd_2.0", "/var/log/vpd_2.0.txt"}, |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 312 | {kFile, "zram compressed data size", "/sys/block/zram0/compr_data_size"}, |
| 313 | {kFile, "zram original data size", "/sys/block/zram0/orig_data_size"}, |
| 314 | {kFile, "zram total memory used", "/sys/block/zram0/mem_used_total"}, |
| 315 | {kFile, "zram total reads", "/sys/block/zram0/num_reads"}, |
| 316 | {kFile, "zram total writes", "/sys/block/zram0/num_writes"}, |
| 317 | {kCommand, "zram new stats names", |
| 318 | "echo orig_size compr_size used_total limit used_max zero_pages migrated"}, |
| 319 | {kFile, "zram new stats values", "/sys/block/zram0/mm_stat"}, |
| 320 | {kFile, "cros_tp version", "/sys/class/chromeos/cros_tp/version"}, |
| 321 | {kCommand, "cros_tp console", "/usr/sbin/ectool --name=cros_tp console", |
| 322 | kRoot, kRoot}, |
| 323 | {kCommand, "cros_tp frame", "/usr/sbin/ectool --name=cros_tp tpframeget", |
| 324 | kRoot, kRoot}, |
| 325 | {kCommand, "crostini", "/usr/bin/cicerone_client --get_info"}, |
Sean Paul | 5ce118f | 2019-12-05 08:41:32 -0500 | [diff] [blame] | 326 | {kFile, "drm_trace", "/sys/kernel/debug/dri/trace", |
| 327 | SandboxedProcess::kDefaultUser, kDebugfsGroup}, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 328 | // Stuff pulled out of the original list. These need access to the running X |
| 329 | // session, which we'd rather not give to debugd, or return info specific to |
| 330 | // the current session (in the setsid(2) sense), which is not useful for |
| 331 | // debugd |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 332 | // {kCommand, "env", "set"}, |
| 333 | // {kCommand, "setxkbmap", "/usr/bin/setxkbmap -print -query"}, |
| 334 | // {kCommand, "xrandr", "/usr/bin/xrandr --verbose} |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 335 | }; |
| 336 | |
Kevin Cernekee | 143e2af | 2018-03-20 13:28:20 -0700 | [diff] [blame] | 337 | // netstat and logcat should appear in chrome://system but not in feedback |
| 338 | // reports. Open sockets may have privacy implications, and logcat is |
| 339 | // already incorporated via arc-bugreport. |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 340 | const std::vector<Log> kExtraLogs { |
Ben Chan | 36e4228 | 2014-02-12 22:32:34 -0800 | [diff] [blame] | 341 | #if USE_CELLULAR |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 342 | {kCommand, "mm-status", "/usr/bin/modem status"}, |
Ben Chan | 36e4228 | 2014-02-12 22:32:34 -0800 | [diff] [blame] | 343 | #endif // USE_CELLULAR |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 344 | {kCommand, "network-devices", "/usr/bin/connectivity show devices"}, |
| 345 | {kCommand, "network-services", "/usr/bin/connectivity show services"}, |
Jeffrey Kardatzke | 36791f2 | 2019-07-11 11:53:22 -0700 | [diff] [blame] | 346 | {kCommand, "wifi_status_no_anonymize", |
| 347 | "/usr/bin/network_diag --wifi-internal --no-log"}, |
Chris Morin | 253a2b0 | 2019-04-12 16:04:25 -0700 | [diff] [blame] | 348 | // --processes requires root. |
| 349 | {kCommand, "netstat", |
| 350 | "/sbin/ss --all --query inet --numeric --processes", kRoot, kRoot}, |
Kansho Nishida | 6ae546f | 2019-08-13 17:14:58 +0900 | [diff] [blame] | 351 | {kCommand, "logcat", |
| 352 | "/usr/bin/nsenter -t1 -m /usr/sbin/android-sh -c '/system/bin/logcat -d'", |
Chris Morin | 253a2b0 | 2019-04-12 16:04:25 -0700 | [diff] [blame] | 353 | kRoot, kRoot, Log::kDefaultMaxBytes, LogTool::Encoding::kUtf8}, |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 354 | }; |
| 355 | |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 356 | const std::vector<Log> kFeedbackLogs { |
Ben Chan | 36e4228 | 2014-02-12 22:32:34 -0800 | [diff] [blame] | 357 | #if USE_CELLULAR |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 358 | {kCommand, "mm-status", "/usr/bin/modem status-feedback"}, |
Ben Chan | 36e4228 | 2014-02-12 22:32:34 -0800 | [diff] [blame] | 359 | #endif // USE_CELLULAR |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 360 | {kCommand, "network-devices", |
| 361 | "/usr/bin/connectivity show-feedback devices"}, |
| 362 | {kCommand, "network-services", |
| 363 | "/usr/bin/connectivity show-feedback services"}, |
Jeffrey Kardatzke | 36791f2 | 2019-07-11 11:53:22 -0700 | [diff] [blame] | 364 | {kCommand, "wifi_status", |
| 365 | "/usr/bin/network_diag --wifi-internal --no-log --anonymize"}, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 366 | }; |
| 367 | |
Jeffrey Kardatzke | e3ec6fd | 2019-08-05 12:25:17 -0700 | [diff] [blame] | 368 | // Fills |dictionary| with the contents of the logs in |logs|. |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 369 | void GetLogsInDictionary(const std::vector<Log>& logs, |
Ahmed Fakhry | 21140cf | 2016-03-04 17:15:19 -0800 | [diff] [blame] | 370 | base::DictionaryValue* dictionary) { |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 371 | for (const Log& log : logs) { |
Hidehiko Abe | 2266726 | 2019-08-15 01:32:23 +0900 | [diff] [blame] | 372 | dictionary->SetKey(log.GetName(), base::Value(log.GetLogData())); |
Ahmed Fakhry | 21140cf | 2016-03-04 17:15:19 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | // Serializes the |dictionary| into the file with the given |fd| in a JSON |
| 377 | // format. |
| 378 | void SerializeLogsAsJSON(const base::DictionaryValue& dictionary, |
Eric Caruso | 0b24188 | 2018-04-04 13:43:46 -0700 | [diff] [blame] | 379 | const base::ScopedFD& fd) { |
Eric Caruso | 96d03d3 | 2017-04-25 18:01:17 -0700 | [diff] [blame] | 380 | string logs_json; |
Ahmed Fakhry | 21140cf | 2016-03-04 17:15:19 -0800 | [diff] [blame] | 381 | base::JSONWriter::WriteWithOptions(dictionary, |
| 382 | base::JSONWriter::OPTIONS_PRETTY_PRINT, |
| 383 | &logs_json); |
Eric Caruso | 0b24188 | 2018-04-04 13:43:46 -0700 | [diff] [blame] | 384 | base::WriteFileDescriptor(fd.get(), logs_json.c_str(), logs_json.size()); |
Ahmed Fakhry | 21140cf | 2016-03-04 17:15:19 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 387 | bool GetNamedLogFrom(const string& name, |
| 388 | const std::vector<Log>& logs, |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 389 | string* result) { |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 390 | for (const Log& log : logs) { |
| 391 | if (name == log.GetName()) { |
| 392 | *result = log.GetLogData(); |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 393 | return true; |
| 394 | } |
| 395 | } |
| 396 | *result = "<invalid log name>"; |
| 397 | return false; |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 398 | } |
| 399 | |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 400 | void GetLogsFrom(const std::vector<Log>& logs, LogTool::LogMap* map) { |
| 401 | for (const Log& log : logs) |
| 402 | (*map)[log.GetName()] = log.GetLogData(); |
Ahmed Fakhry | 21140cf | 2016-03-04 17:15:19 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Kartik Hegde | 1c4b97b | 2018-09-09 19:09:34 -0600 | [diff] [blame] | 405 | void GetLsbReleaseInfo(LogTool::LogMap* map) { |
| 406 | const base::FilePath lsb_release(kLsbReleasePath); |
| 407 | brillo::KeyValueStore store; |
| 408 | if (!store.Load(lsb_release)) { |
| 409 | // /etc/lsb-release might not be present (cros deploying a new |
| 410 | // configuration or no fields set at all). Just print a debug |
| 411 | // message and continue. |
| 412 | DLOG(INFO) << "Could not load fields from " << lsb_release.value(); |
| 413 | } else { |
| 414 | for (const auto& key : store.GetKeys()) { |
| 415 | std::string value; |
| 416 | store.GetString(key, &value); |
| 417 | (*map)[key] = value; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | void GetOsReleaseInfo(LogTool::LogMap* map) { |
| 423 | brillo::OsReleaseReader reader; |
| 424 | reader.Load(); |
| 425 | for (const auto& key : reader.GetKeys()) { |
| 426 | std::string value; |
| 427 | reader.GetString(key, &value); |
| 428 | (*map)["os-release " + key] = value; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | void PopulateDictionaryValue(const LogTool::LogMap& map, |
| 433 | base::DictionaryValue* dictionary) { |
| 434 | for (const auto& kv : map) { |
| 435 | dictionary->SetString(kv.first, kv.second); |
| 436 | } |
| 437 | } |
| 438 | |
Chinglin Yu | aeb4ec7 | 2018-12-10 18:53:30 +0800 | [diff] [blame] | 439 | bool CompressXzBuffer(const std::vector<uint8_t>& in_buffer, |
| 440 | std::vector<uint8_t>* out_buffer) { |
| 441 | size_t out_size = lzma_stream_buffer_bound(in_buffer.size()); |
| 442 | out_buffer->resize(out_size); |
| 443 | size_t out_pos = 0; |
| 444 | |
| 445 | lzma_ret ret = lzma_easy_buffer_encode( |
| 446 | LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64, nullptr, in_buffer.data(), |
| 447 | in_buffer.size(), out_buffer->data(), &out_pos, out_size); |
| 448 | |
| 449 | if (ret != LZMA_OK) { |
| 450 | out_buffer->clear(); |
| 451 | return false; |
| 452 | } |
| 453 | |
| 454 | out_buffer->resize(out_pos); |
| 455 | return true; |
| 456 | } |
| 457 | |
| 458 | void GetPerfData(LogTool::LogMap* map) { |
| 459 | // Run perf to collect system-wide performance profile when user triggers |
| 460 | // feedback report. Perf runs at sampling frequency of ~500 hz (499 is used |
| 461 | // to avoid sampling periodic system activities), with callstack in each |
| 462 | // sample (-g). |
| 463 | std::vector<std::string> perf_args = { |
| 464 | "perf", "record", "-a", "-g", "-F", "499" |
| 465 | }; |
| 466 | std::vector<uint8_t> perf_data; |
| 467 | int32_t status; |
| 468 | |
| 469 | debugd::PerfTool perf_tool; |
| 470 | if (!perf_tool.GetPerfOutput(kPerfDurationSecs, perf_args, &perf_data, |
| 471 | nullptr, &status, nullptr)) |
| 472 | return; |
| 473 | |
| 474 | // XZ compress the profile data. |
| 475 | std::vector<uint8_t> perf_data_xz; |
| 476 | if (!CompressXzBuffer(perf_data, &perf_data_xz)) |
| 477 | return; |
| 478 | |
| 479 | // Base64 encode the compressed data. |
| 480 | std::string perf_data_str(reinterpret_cast<const char*>(perf_data_xz.data()), |
| 481 | perf_data_xz.size()); |
| 482 | (*map)["perf-data"] = |
Chinglin Yu | 3c8d0a2 | 2019-02-20 11:32:52 +0800 | [diff] [blame] | 483 | std::string(kPerfDataDescription) + |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 484 | LogTool::EncodeString(std::move(perf_data_str), |
| 485 | LogTool::Encoding::kBase64); |
Chinglin Yu | aeb4ec7 | 2018-12-10 18:53:30 +0800 | [diff] [blame] | 486 | } |
| 487 | |
Ahmed Fakhry | 21140cf | 2016-03-04 17:15:19 -0800 | [diff] [blame] | 488 | } // namespace |
| 489 | |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 490 | Log::Log(Log::LogType type, |
| 491 | std::string name, |
| 492 | std::string data, |
| 493 | std::string user, |
| 494 | std::string group, |
| 495 | int64_t max_bytes, |
Brian Norris | afc9f63 | 2019-05-09 14:08:28 -0700 | [diff] [blame] | 496 | LogTool::Encoding encoding, |
| 497 | bool access_root_mount_ns) |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 498 | : type_(type), |
| 499 | name_(name), |
| 500 | data_(data), |
| 501 | user_(user), |
| 502 | group_(group), |
| 503 | max_bytes_(max_bytes), |
Brian Norris | afc9f63 | 2019-05-09 14:08:28 -0700 | [diff] [blame] | 504 | encoding_(encoding), |
| 505 | access_root_mount_ns_(access_root_mount_ns) {} |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 506 | |
| 507 | std::string Log::GetName() const { |
| 508 | return name_; |
| 509 | } |
| 510 | |
| 511 | std::string Log::GetLogData() const { |
| 512 | // The reason this code uses a switch statement on a type enum rather than |
| 513 | // using inheritance/virtual dispatch is so that all of the Log objects can |
| 514 | // be constructed statically. Switching to heap allocated subclasses of Log |
| 515 | // makes the code that declares all of the log entries much more verbose |
| 516 | // and harder to understand. |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 517 | std::string output; |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 518 | switch (type_) { |
| 519 | case kCommand: |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 520 | output = GetCommandLogData(); |
| 521 | break; |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 522 | case kFile: |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 523 | output = GetFileLogData(); |
| 524 | break; |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 525 | default: |
| 526 | return "<unknown log type>"; |
| 527 | } |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 528 | |
| 529 | if (output.empty()) |
| 530 | return "<empty>"; |
| 531 | |
| 532 | return LogTool::EncodeString(std::move(output), encoding_); |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | // TODO(ellyjones): sandbox. crosbug.com/35122 |
| 536 | std::string Log::GetCommandLogData() const { |
| 537 | if (type_ != kCommand) |
| 538 | return "<log type mismatch>"; |
| 539 | std::string tailed_cmdline = |
| 540 | base::StringPrintf("%s | tail -c %" PRId64, data_.c_str(), max_bytes_); |
| 541 | ProcessWithOutput p; |
| 542 | if (minijail_disabled_for_test_) |
| 543 | p.set_use_minijail(false); |
| 544 | if (!user_.empty() && !group_.empty()) |
| 545 | p.SandboxAs(user_, group_); |
Brian Norris | afc9f63 | 2019-05-09 14:08:28 -0700 | [diff] [blame] | 546 | if (access_root_mount_ns_) |
| 547 | p.AllowAccessRootMountNamespace(); |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 548 | if (!p.Init()) |
| 549 | return "<not available>"; |
| 550 | p.AddArg(kShell); |
| 551 | p.AddStringOption("-c", tailed_cmdline); |
| 552 | if (p.Run()) |
| 553 | return "<not available>"; |
| 554 | std::string output; |
| 555 | p.GetOutput(&output); |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 556 | return output; |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | std::string Log::GetFileLogData() const { |
| 560 | if (type_ != kFile) |
| 561 | return "<log type mismatch>"; |
| 562 | |
| 563 | uid_t old_euid = geteuid(); |
| 564 | uid_t new_euid = UidForUser(user_); |
| 565 | gid_t old_egid = getegid(); |
| 566 | gid_t new_egid = GidForGroup(group_); |
| 567 | |
| 568 | if (new_euid == -1 || new_egid == -1) { |
| 569 | return "<not available>"; |
| 570 | } |
| 571 | |
| 572 | // Make sure to set group first, since if we set user first we lose root |
| 573 | // and therefore the ability to set our effective gid to arbitrary gids. |
| 574 | if (setegid(new_egid)) { |
| 575 | PLOG(ERROR) << "Failed to set effective group id to " << new_egid; |
| 576 | return "<not available>"; |
| 577 | } |
| 578 | if (seteuid(new_euid)) { |
| 579 | PLOG(ERROR) << "Failed to set effective user id to " << new_euid; |
| 580 | if (setegid(old_egid)) |
| 581 | PLOG(ERROR) << "Failed to restore effective group id to " << old_egid; |
| 582 | return "<not available>"; |
| 583 | } |
| 584 | |
| 585 | std::string contents; |
| 586 | const base::FilePath path(data_); |
| 587 | // Handle special files that don't properly report length/allow lseek. |
| 588 | if (base::FilePath("/dev").IsParent(path) || |
| 589 | base::FilePath("/proc").IsParent(path) || |
| 590 | base::FilePath("/sys").IsParent(path)) { |
| 591 | if (!base::ReadFileToString(path, &contents)) |
| 592 | contents = "<not available>"; |
| 593 | if (contents.size() > max_bytes_) |
| 594 | contents.erase(0, contents.size() - max_bytes_); |
| 595 | } else { |
| 596 | base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 597 | if (!file.IsValid()) { |
| 598 | contents = "<not available>"; |
| 599 | } else { |
| 600 | int64_t length = file.GetLength(); |
| 601 | if (length > max_bytes_) { |
| 602 | file.Seek(base::File::FROM_END, -max_bytes_); |
| 603 | length = max_bytes_; |
| 604 | } |
| 605 | std::vector<char> buf(length); |
| 606 | int read = file.ReadAtCurrentPos(buf.data(), buf.size()); |
| 607 | if (read < 0) { |
| 608 | PLOG(ERROR) << "Could not read from file " << path.value(); |
| 609 | } else { |
| 610 | contents = std::string(buf.begin(), buf.begin() + read); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 615 | // Make sure we restore our old euid/egid before returning. |
| 616 | if (seteuid(old_euid)) |
| 617 | PLOG(ERROR) << "Failed to restore effective user id to " << old_euid; |
| 618 | |
| 619 | if (setegid(old_egid)) |
| 620 | PLOG(ERROR) << "Failed to restore effective group id to " << old_egid; |
| 621 | |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 622 | return contents; |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | void Log::DisableMinijailForTest() { |
| 626 | minijail_disabled_for_test_ = true; |
| 627 | } |
| 628 | |
| 629 | // static |
| 630 | uid_t Log::UidForUser(const std::string& user) { |
| 631 | struct passwd entry; |
| 632 | struct passwd* result; |
| 633 | std::vector<char> buf(1024); |
| 634 | getpwnam_r(user.c_str(), &entry, &buf[0], buf.size(), &result); |
| 635 | if (!result) { |
| 636 | LOG(ERROR) << "User not found: " << user; |
| 637 | return -1; |
| 638 | } |
| 639 | return entry.pw_uid; |
| 640 | } |
| 641 | |
| 642 | // static |
| 643 | gid_t Log::GidForGroup(const std::string& group) { |
| 644 | struct group entry; |
| 645 | struct group* result; |
| 646 | std::vector<char> buf(1024); |
| 647 | getgrnam_r(group.c_str(), &entry, &buf[0], buf.size(), &result); |
| 648 | if (!result) { |
| 649 | LOG(ERROR) << "Group not found: " << group; |
| 650 | return -1; |
| 651 | } |
| 652 | return entry.gr_gid; |
| 653 | } |
| 654 | |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 655 | LogTool::LogTool( |
| 656 | scoped_refptr<dbus::Bus> bus, |
| 657 | std::unique_ptr<org::chromium::CryptohomeInterfaceProxyInterface> |
| 658 | cryptohome_proxy, |
| 659 | std::unique_ptr<LogTool::Log> arc_bug_report_log, |
| 660 | const base::FilePath& daemon_store_base_dir) |
| 661 | : bus_(bus), |
| 662 | cryptohome_proxy_(std::move(cryptohome_proxy)), |
| 663 | arc_bug_report_log_(std::move(arc_bug_report_log)), |
| 664 | daemon_store_base_dir_(daemon_store_base_dir) {} |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 665 | |
mhasank | 4f599d3 | 2020-04-09 22:07:35 -0700 | [diff] [blame] | 666 | LogTool::LogTool(scoped_refptr<dbus::Bus> bus) |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 667 | : LogTool(bus, |
| 668 | std::make_unique<org::chromium::CryptohomeInterfaceProxy>(bus), |
| 669 | std::make_unique<ArcBugReportLog>(), |
| 670 | base::FilePath(kDaemonStoreBaseDir)) {} |
mhasank | 4f599d3 | 2020-04-09 22:07:35 -0700 | [diff] [blame] | 671 | |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 672 | base::FilePath LogTool::GetArcBugReportBackupFilePath |
| 673 | (const std::string& userhash) { |
| 674 | return daemon_store_base_dir_ |
| 675 | .Append(userhash) |
| 676 | .Append(kArcBugReportBackupFileName); |
| 677 | } |
| 678 | |
Fletcher Woodruff | 70f2723 | 2019-01-24 11:41:34 -0700 | [diff] [blame] | 679 | void LogTool::CreateConnectivityReport(bool wait_for_results) { |
Rebecca Silberstein | e78af40 | 2014-10-02 10:55:04 -0700 | [diff] [blame] | 680 | // Perform ConnectivityTrial to report connection state in feedback log. |
Ben Chan | 8e9f6d0 | 2017-09-26 23:04:21 -0700 | [diff] [blame] | 681 | auto shill = std::make_unique<org::chromium::flimflam::ManagerProxy>(bus_); |
Rebecca Silberstein | e78af40 | 2014-10-02 10:55:04 -0700 | [diff] [blame] | 682 | // Give the connection trial time to test the connection and log the results |
| 683 | // before collecting the logs for feedback. |
| 684 | // TODO(silberst): Replace the simple approach of a single timeout with a more |
| 685 | // coordinated effort. |
Fletcher Woodruff | 70f2723 | 2019-01-24 11:41:34 -0700 | [diff] [blame] | 686 | if (shill && shill->CreateConnectivityReport(nullptr) && wait_for_results) |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 687 | sleep(kConnectionTesterTimeoutSeconds); |
Rebecca Silberstein | e78af40 | 2014-10-02 10:55:04 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Eric Caruso | c93a15c | 2017-04-24 16:15:12 -0700 | [diff] [blame] | 690 | string LogTool::GetLog(const string& name) { |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 691 | string result; |
Ben Chan | cf7d641 | 2017-08-10 22:30:09 -0700 | [diff] [blame] | 692 | GetNamedLogFrom(name, kCommandLogs, &result) |
| 693 | || GetNamedLogFrom(name, kExtraLogs, &result) |
| 694 | || GetNamedLogFrom(name, kFeedbackLogs, &result); |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 695 | return result; |
| 696 | } |
| 697 | |
Eric Caruso | f9091f8 | 2017-04-28 14:18:59 -0700 | [diff] [blame] | 698 | LogTool::LogMap LogTool::GetAllLogs() { |
Fletcher Woodruff | 70f2723 | 2019-01-24 11:41:34 -0700 | [diff] [blame] | 699 | CreateConnectivityReport(false); |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 700 | LogMap result; |
Ben Chan | cf7d641 | 2017-08-10 22:30:09 -0700 | [diff] [blame] | 701 | GetLogsFrom(kCommandLogs, &result); |
| 702 | GetLogsFrom(kExtraLogs, &result); |
Kartik Hegde | 1c4b97b | 2018-09-09 19:09:34 -0600 | [diff] [blame] | 703 | GetLsbReleaseInfo(&result); |
| 704 | GetOsReleaseInfo(&result); |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 705 | return result; |
| 706 | } |
| 707 | |
Brian Norris | ca4fc04 | 2018-04-03 00:24:26 -0700 | [diff] [blame] | 708 | LogTool::LogMap LogTool::GetAllDebugLogs() { |
Fletcher Woodruff | 70f2723 | 2019-01-24 11:41:34 -0700 | [diff] [blame] | 709 | CreateConnectivityReport(true); |
Brian Norris | ca4fc04 | 2018-04-03 00:24:26 -0700 | [diff] [blame] | 710 | LogMap result; |
| 711 | GetLogsFrom(kCommandLogs, &result); |
| 712 | GetLogsFrom(kExtraLogs, &result); |
mhasank | d2b8488 | 2020-05-04 17:02:19 -0700 | [diff] [blame] | 713 | result[arc_bug_report_log_->GetName()] = GetArcBugReport("", nullptr); |
Kartik Hegde | 1c4b97b | 2018-09-09 19:09:34 -0600 | [diff] [blame] | 714 | GetLsbReleaseInfo(&result); |
| 715 | GetOsReleaseInfo(&result); |
Brian Norris | ca4fc04 | 2018-04-03 00:24:26 -0700 | [diff] [blame] | 716 | return result; |
| 717 | } |
| 718 | |
mhasank | 4f599d3 | 2020-04-09 22:07:35 -0700 | [diff] [blame] | 719 | void LogTool::GetBigFeedbackLogs(const base::ScopedFD& fd, |
| 720 | const std::string& username) { |
Fletcher Woodruff | 70f2723 | 2019-01-24 11:41:34 -0700 | [diff] [blame] | 721 | CreateConnectivityReport(true); |
Chinglin Yu | aeb4ec7 | 2018-12-10 18:53:30 +0800 | [diff] [blame] | 722 | LogMap map; |
| 723 | GetPerfData(&map); |
Ahmed Fakhry | 21140cf | 2016-03-04 17:15:19 -0800 | [diff] [blame] | 724 | base::DictionaryValue dictionary; |
Jeffrey Kardatzke | e3ec6fd | 2019-08-05 12:25:17 -0700 | [diff] [blame] | 725 | GetLogsInDictionary(kCommandLogs, &dictionary); |
| 726 | GetLogsInDictionary(kFeedbackLogs, &dictionary); |
mhasank | d2b8488 | 2020-05-04 17:02:19 -0700 | [diff] [blame] | 727 | bool is_backup; |
| 728 | std::string arc_bug_report = GetArcBugReport(username, &is_backup); |
| 729 | dictionary.SetKey(kArcBugReportBackupKey, |
| 730 | base::Value(is_backup ? "true" : "false")); |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 731 | dictionary.SetKey(arc_bug_report_log_->GetName(), |
mhasank | d2b8488 | 2020-05-04 17:02:19 -0700 | [diff] [blame] | 732 | base::Value(arc_bug_report)); |
Kartik Hegde | 1c4b97b | 2018-09-09 19:09:34 -0600 | [diff] [blame] | 733 | GetLsbReleaseInfo(&map); |
| 734 | GetOsReleaseInfo(&map); |
| 735 | PopulateDictionaryValue(map, &dictionary); |
Ahmed Fakhry | 21140cf | 2016-03-04 17:15:19 -0800 | [diff] [blame] | 736 | SerializeLogsAsJSON(dictionary, fd); |
| 737 | } |
| 738 | |
mhasank | 4f599d3 | 2020-04-09 22:07:35 -0700 | [diff] [blame] | 739 | std::string GetSanitizedUsername( |
| 740 | org::chromium::CryptohomeInterfaceProxyInterface* cryptohome_proxy, |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 741 | const std::string& username) { |
mhasank | 4f599d3 | 2020-04-09 22:07:35 -0700 | [diff] [blame] | 742 | if (username.empty()) { |
| 743 | return std::string(); |
| 744 | } |
| 745 | |
| 746 | std::string sanitized_username; |
| 747 | brillo::ErrorPtr error; |
| 748 | if (!cryptohome_proxy->GetSanitizedUsername(username, &sanitized_username, |
| 749 | &error)) { |
| 750 | LOG(ERROR) << "Failed to call GetSanitizedUsername, error: " |
| 751 | << error->GetMessage(); |
| 752 | return std::string(); |
| 753 | } |
| 754 | |
| 755 | return sanitized_username; |
| 756 | } |
| 757 | |
mhasank | d2b8488 | 2020-05-04 17:02:19 -0700 | [diff] [blame] | 758 | std::string LogTool::GetArcBugReport(const std::string& username, |
| 759 | bool* is_backup) { |
| 760 | if (is_backup) { |
| 761 | *is_backup = true; |
| 762 | } |
mhasank | 4f599d3 | 2020-04-09 22:07:35 -0700 | [diff] [blame] | 763 | std::string userhash = |
| 764 | GetSanitizedUsername(cryptohome_proxy_.get(), username); |
| 765 | |
| 766 | std::string contents; |
| 767 | if (userhash.empty() || |
| 768 | arc_bug_report_backups_.find(userhash) == arc_bug_report_backups_.end() || |
| 769 | !base::ReadFileToString(GetArcBugReportBackupFilePath(userhash), |
| 770 | &contents)) { |
| 771 | // If |userhash| was not empty, but was not found in the backup set |
| 772 | // or the file did not exist, attempt to delete the file. |
| 773 | if (!userhash.empty()) { |
| 774 | DeleteArcBugReportBackup(userhash); |
| 775 | } |
mhasank | d2b8488 | 2020-05-04 17:02:19 -0700 | [diff] [blame] | 776 | if (is_backup) { |
| 777 | *is_backup = false; |
| 778 | } |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 779 | contents = arc_bug_report_log_->GetLogData(); |
mhasank | 4f599d3 | 2020-04-09 22:07:35 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | return contents; |
| 783 | } |
| 784 | |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 785 | void LogTool::BackupArcBugReport(const std::string& userhash) { |
| 786 | DLOG(INFO) << "Backing up ARC bug report"; |
| 787 | |
| 788 | const base::FilePath reportPath = GetArcBugReportBackupFilePath(userhash); |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 789 | const std::string logData = arc_bug_report_log_->GetLogData(); |
mhasank | 4f599d3 | 2020-04-09 22:07:35 -0700 | [diff] [blame] | 790 | if (base::WriteFile(reportPath, logData.c_str(), logData.length())) { |
| 791 | arc_bug_report_backups_.insert(userhash); |
| 792 | } else { |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 793 | PLOG(ERROR) << "Failed to backup ARC bug report"; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | void LogTool::DeleteArcBugReportBackup(const std::string& userhash) { |
| 798 | DLOG(INFO) << "Deleting the ARC bug report backup"; |
| 799 | |
| 800 | const base::FilePath reportPath = GetArcBugReportBackupFilePath(userhash); |
mhasank | 4f599d3 | 2020-04-09 22:07:35 -0700 | [diff] [blame] | 801 | arc_bug_report_backups_.erase(userhash); |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 802 | if (!base::DeleteFile(reportPath, false)) { |
| 803 | PLOG(ERROR) << "Failed to delete ARC bug report backup"; |
| 804 | } |
| 805 | } |
| 806 | |
Jeffrey Kardatzke | e3ec6fd | 2019-08-05 12:25:17 -0700 | [diff] [blame] | 807 | void LogTool::GetJournalLog(const base::ScopedFD& fd) { |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 808 | Log journal(kCommand, "journal.export", "journalctl -n 10000 -o export", |
| 809 | "syslog", "syslog", 10 * 1024 * 1024, LogTool::Encoding::kBinary); |
Jeffrey Kardatzke | e3ec6fd | 2019-08-05 12:25:17 -0700 | [diff] [blame] | 810 | std::string output = journal.GetLogData(); |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 811 | base::WriteFileDescriptor(fd.get(), output.data(), output.size()); |
| 812 | } |
| 813 | |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 814 | // static |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 815 | string LogTool::EncodeString(string value, |
| 816 | LogTool::Encoding source_encoding) { |
| 817 | if (source_encoding == LogTool::Encoding::kBinary) |
| 818 | return value; |
| 819 | |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 820 | if (source_encoding == LogTool::Encoding::kAutodetect) { |
| 821 | if (base::IsStringUTF8(value)) |
| 822 | return value; |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 823 | source_encoding = LogTool::Encoding::kBase64; |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | if (source_encoding == LogTool::Encoding::kUtf8) { |
| 827 | string output; |
| 828 | const char* src = value.data(); |
| 829 | int32_t src_len = static_cast<int32_t>(value.length()); |
| 830 | |
| 831 | output.reserve(value.size()); |
| 832 | for (int32_t char_index = 0; char_index < src_len; char_index++) { |
| 833 | uint32_t code_point; |
| 834 | if (!base::ReadUnicodeCharacter(src, src_len, &char_index, &code_point) || |
| 835 | !base::IsValidCharacter(code_point)) { |
| 836 | // Replace invalid characters with U+FFFD REPLACEMENT CHARACTER. |
| 837 | code_point = 0xFFFD; |
| 838 | } |
| 839 | base::WriteUnicodeCharacter(code_point, &output); |
| 840 | } |
| 841 | return output; |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 842 | } |
Chris Morin | 853d344 | 2019-04-01 21:35:13 -0700 | [diff] [blame] | 843 | |
| 844 | base::Base64Encode(value, &value); |
| 845 | return "<base64>: " + value; |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 848 | } // namespace debugd |