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