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 | |
| 5 | #include "log_tool.h" |
| 6 | |
Ben Chan | f6cd93a | 2012-10-14 19:37:00 -0700 | [diff] [blame] | 7 | #include <glib.h> |
| 8 | |
| 9 | #include <base/base64.h> |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 10 | #include <base/file_util.h> |
| 11 | #include <base/logging.h> |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 12 | #include <base/strings/string_split.h> |
| 13 | #include <base/strings/string_util.h> |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 14 | |
Darin Petkov | ce9b3a1 | 2013-01-10 16:38:54 +0100 | [diff] [blame] | 15 | #include "anonymizer_tool.h" |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 16 | #include "process_with_output.h" |
| 17 | |
| 18 | namespace debugd { |
| 19 | |
| 20 | const char *kShell = "/bin/sh"; |
Elly Fong-Jones | 215b562 | 2013-03-20 14:32:18 -0400 | [diff] [blame] | 21 | const char *kDebugfsGroup = "debugfs-access"; |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 22 | |
| 23 | using std::string; |
| 24 | using std::vector; |
| 25 | |
| 26 | typedef vector<string> Strings; |
| 27 | |
Ben Chan | f6cd93a | 2012-10-14 19:37:00 -0700 | [diff] [blame] | 28 | // Returns |value| if |value| is a valid UTF-8 string or a base64-encoded |
| 29 | // string of |value| otherwise. |
| 30 | string EnsureUTF8String(const string& value) { |
| 31 | if (IsStringUTF8(value)) |
| 32 | return value; |
| 33 | |
| 34 | gchar* base64_value = g_base64_encode( |
| 35 | reinterpret_cast<const guchar*>(value.c_str()), value.length()); |
| 36 | if (base64_value) { |
| 37 | string encoded_value = "<base64>: "; |
| 38 | encoded_value += base64_value; |
| 39 | g_free(base64_value); |
| 40 | return encoded_value; |
| 41 | } |
| 42 | return "<invalid>"; |
| 43 | } |
| 44 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 45 | struct Log { |
| 46 | const char *name; |
| 47 | const char *command; |
| 48 | const char *user; |
| 49 | const char *group; |
| 50 | }; |
| 51 | |
Elly Fong-Jones | 57f018c | 2012-10-08 14:22:01 -0400 | [diff] [blame] | 52 | // TODO(ellyjones): sandbox. crosbug.com/35122 |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 53 | string Run(const Log& log) { |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 54 | string output; |
| 55 | ProcessWithOutput p; |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 56 | string tailed_cmdline = std::string(log.command) + " | tail -c 256K"; |
| 57 | if (log.user && log.group) |
| 58 | p.SandboxAs(log.user, log.group); |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 59 | if (!p.Init()) |
| 60 | return "<not available>"; |
| 61 | p.AddArg(kShell); |
| 62 | p.AddStringOption("-c", tailed_cmdline); |
| 63 | if (p.Run()) |
| 64 | return "<not available>"; |
| 65 | p.GetOutput(&output); |
| 66 | if (!output.size()) |
| 67 | return "<empty>"; |
Ben Chan | f6cd93a | 2012-10-14 19:37:00 -0700 | [diff] [blame] | 68 | return EnsureUTF8String(output); |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 71 | static const Log common_logs[] = { |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 72 | { "CLIENT_ID", "/bin/cat '/home/chronos/Consent To Send Stats'" }, |
| 73 | { "LOGDATE", "/bin/date" }, |
| 74 | { "Xorg.0.log", "/bin/cat /var/log/Xorg.0.log" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 75 | { "bios_info", "/bin/cat /var/log/bios_info.txt" }, |
Vadim Bendebury | 64aeeed | 2013-09-16 13:16:49 -0700 | [diff] [blame] | 76 | { "bios_log", |
| 77 | "/bin/cat /sys/firmware/log " |
| 78 | "/proc/device-tree/chosen/ap-console-buffer 2> /dev/null" }, |
Stefan Reinauer | 4393fa7 | 2012-10-18 11:08:09 -0700 | [diff] [blame] | 79 | { "bios_times", "/bin/cat /var/log/bios_times.txt" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 80 | { "board-specific", |
| 81 | "/usr/share/userfeedback/scripts/get_board_specific_info" }, |
| 82 | { "boot_times", "/bin/cat /tmp/boot-times-sent" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 83 | { "chrome_system_log", "/bin/cat /var/log/chrome/chrome" }, |
Elly Fong-Jones | 57f018c | 2012-10-08 14:22:01 -0400 | [diff] [blame] | 84 | { "console-ramoops", "/bin/cat /dev/pstore/console-ramoops" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 85 | { "cpu", "/usr/bin/uname -p" }, |
| 86 | { "cpuinfo", "/bin/cat /proc/cpuinfo" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 87 | { "dmesg", "/bin/dmesg" }, |
| 88 | { "ec_info", "/bin/cat /var/log/ec_info.txt" }, |
Stefan Reinauer | 4393fa7 | 2012-10-18 11:08:09 -0700 | [diff] [blame] | 89 | { "eventlog", "/bin/cat /var/log/eventlog.txt" }, |
Elly Fong-Jones | 215b562 | 2013-03-20 14:32:18 -0400 | [diff] [blame] | 90 | { |
| 91 | "exynos_gem_objects", |
Yuly Novikov | 13ece85 | 2013-07-11 17:19:10 -0400 | [diff] [blame] | 92 | "/bin/cat /sys/kernel/debug/dri/0/exynos_gem_objects 2> /dev/null", |
Elly Fong-Jones | 215b562 | 2013-03-20 14:32:18 -0400 | [diff] [blame] | 93 | SandboxedProcess::kDefaultUser, |
| 94 | kDebugfsGroup |
| 95 | }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 96 | { "font_info", "/usr/share/userfeedback/scripts/font_info" }, |
| 97 | { "hardware_class", "/usr/bin/crossystem hwid" }, |
| 98 | { "hostname", "/bin/hostname" }, |
| 99 | { "hw_platform", "/usr/bin/uname -i" }, |
Elly Fong-Jones | 215b562 | 2013-03-20 14:32:18 -0400 | [diff] [blame] | 100 | { |
| 101 | "i915_gem_gtt", |
Yuly Novikov | 13ece85 | 2013-07-11 17:19:10 -0400 | [diff] [blame] | 102 | "/bin/cat /sys/kernel/debug/dri/0/i915_gem_gtt 2> /dev/null", |
Elly Fong-Jones | 215b562 | 2013-03-20 14:32:18 -0400 | [diff] [blame] | 103 | SandboxedProcess::kDefaultUser, |
| 104 | kDebugfsGroup |
| 105 | }, |
| 106 | { |
| 107 | "i915_gem_objects", |
Yuly Novikov | 13ece85 | 2013-07-11 17:19:10 -0400 | [diff] [blame] | 108 | "/bin/cat /sys/kernel/debug/dri/0/i915_gem_objects 2> /dev/null", |
Elly Fong-Jones | 215b562 | 2013-03-20 14:32:18 -0400 | [diff] [blame] | 109 | SandboxedProcess::kDefaultUser, |
| 110 | kDebugfsGroup |
| 111 | }, |
| 112 | { |
| 113 | "i915_error_state", |
Yuly Novikov | 4ac12fb | 2013-07-18 20:08:44 -0400 | [diff] [blame] | 114 | "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2> /dev/null", |
Elly Fong-Jones | 215b562 | 2013-03-20 14:32:18 -0400 | [diff] [blame] | 115 | SandboxedProcess::kDefaultUser, |
| 116 | kDebugfsGroup, |
| 117 | }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 118 | { "ifconfig", "/sbin/ifconfig -a" }, |
Elly Jones | 2bde94d | 2012-07-31 18:07:46 -0400 | [diff] [blame] | 119 | { "kernel-crashes", "/bin/cat /var/spool/crash/kernel.*.kcrash" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 120 | { "lsmod", "lsmod" }, |
| 121 | { "lspci", "/usr/sbin/lspci" }, |
| 122 | { "lsusb", "lsusb" }, |
Yuly Novikov | 13ece85 | 2013-07-11 17:19:10 -0400 | [diff] [blame] | 123 | { |
| 124 | "mali_memory", |
Dominik Behr | 4765e6d | 2013-08-26 16:34:08 -0700 | [diff] [blame] | 125 | "/bin/cat /sys/class/misc/mali0/device/memory 2> /dev/null" |
Yuly Novikov | 13ece85 | 2013-07-11 17:19:10 -0400 | [diff] [blame] | 126 | }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 127 | { "meminfo", "cat /proc/meminfo" }, |
| 128 | { "memory_spd_info", "/bin/cat /var/log/memory_spd_info.txt" }, |
Kees Cook | a8f3e7e | 2012-11-19 14:16:01 -0800 | [diff] [blame] | 129 | { "mount-encrypted", "/bin/cat /var/log/mount-encrypted.log" }, |
Darin Petkov | 5474358 | 2012-12-10 14:18:32 +0100 | [diff] [blame] | 130 | { "net-diags.net.log", "/bin/cat /var/log/net-diags.net.log" }, |
Wade Guthrie | 67b672e | 2012-11-14 13:14:31 -0800 | [diff] [blame] | 131 | { "netlog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'" |
| 132 | " /var/log/net.log" }, |
Vincent Palatin | c64af9d | 2013-08-05 16:34:10 -0700 | [diff] [blame] | 133 | { "platform_info", "/bin/cat /var/log/platform_info.txt" }, |
Daniel Erat | 9b58b6d | 2013-04-30 14:58:56 -0700 | [diff] [blame] | 134 | { "power_supply_info", "/usr/bin/power_supply_info" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 135 | { "powerd.LATEST", "/bin/cat /var/log/power_manager/powerd.LATEST" }, |
Daniel Erat | 546c388 | 2013-11-22 12:46:01 -0800 | [diff] [blame] | 136 | { "powerd.PREVIOUS", "/bin/cat /var/log/power_manager/powerd.PREVIOUS" }, |
Daniel Erat | 93da9a1 | 2012-12-06 14:14:17 -0800 | [diff] [blame] | 137 | { "powerd.out", "/bin/cat /var/log/powerd.out" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 138 | // Changed from 'ps ux' to 'ps aux' since we're running as debugd, not chronos |
| 139 | { "ps", "/bin/ps aux" }, |
Puthikorn Voravootivat | 6de5e12 | 2013-12-06 11:43:13 -0800 | [diff] [blame] | 140 | { "storage_info", "/bin/cat /var/log/storage_info.txt" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 141 | { "syslog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'" |
Elly Jones | f8a67ee | 2012-06-19 13:48:38 -0400 | [diff] [blame] | 142 | " /var/log/messages" }, |
Elly Fong-Jones | b4f49c4 | 2013-02-28 13:45:26 -0500 | [diff] [blame] | 143 | { "tlsdate", "/bin/cat /var/log/tlsdate.log" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 144 | { "touchpad", "/opt/google/touchpad/tpcontrol status" }, |
Elly Jones | d31aee2 | 2012-07-02 11:09:10 -0400 | [diff] [blame] | 145 | { "touchpad_activity", "/opt/google/touchpad/generate_userfeedback alt" }, |
Andrew de los Reyes | c060c34 | 2013-04-10 13:46:30 -0700 | [diff] [blame] | 146 | { "touch_fw_version", "grep -E" |
| 147 | " 'synaptics: Touchpad model|chromeos-touch-[a-z]*-update'" |
| 148 | " /var/log/messages | tail -n 20" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 149 | { "ui_log", "/usr/share/userfeedback/scripts/get_log /var/log/ui/ui.LATEST" }, |
| 150 | { "uname", "/bin/uname -a" }, |
| 151 | { "update_engine.log", "cat $(ls -1tr /var/log/update_engine | tail -5 | sed" |
Elly Jones | d31aee2 | 2012-07-02 11:09:10 -0400 | [diff] [blame] | 152 | " s.^./var/log/update_engine/.)" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 153 | { "verified boot", "/bin/cat /var/log/debug_vboot_noisy.log" }, |
| 154 | { "vpd_2.0", "/bin/cat /var/log/vpd_2.0.txt" }, |
Kees Cook | f39f0e0 | 2013-02-05 12:00:10 -0800 | [diff] [blame] | 155 | { "wifi_status", "/usr/bin/network_diagnostics --wifi-internal --no-log" }, |
Sonny Rao | ab5f995 | 2013-07-17 14:13:53 -0700 | [diff] [blame] | 156 | { "zram compressed data size", |
| 157 | "/bin/cat /sys/block/zram0/compr_data_size 2> /dev/null" }, |
| 158 | { "zram original data size", |
| 159 | "/bin/cat /sys/block/zram0/orig_data_size 2> /dev/null" }, |
| 160 | { "zram total memory used", |
| 161 | "/bin/cat /sys/block/zram0/mem_used_total 2> /dev/null" }, |
| 162 | { "zram total reads", |
| 163 | "/bin/cat /sys/block/zram0/num_reads 2> /dev/null" }, |
| 164 | { "zram total writes", |
| 165 | "/bin/cat /sys/block/zram0/num_writes 2> /dev/null" }, |
Gaurav Shah | 6e8fd89 | 2012-10-01 11:51:08 -0700 | [diff] [blame] | 166 | |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 167 | // Stuff pulled out of the original list. These need access to the running X |
| 168 | // session, which we'd rather not give to debugd, or return info specific to |
| 169 | // the current session (in the setsid(2) sense), which is not useful for |
| 170 | // debugd |
| 171 | // { "env", "set" }, |
| 172 | // { "setxkbmap", "/usr/bin/setxkbmap -print -query" }, |
| 173 | // { "xrandr", "/usr/bin/xrandr --verbose" } |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 174 | { NULL, NULL } |
| 175 | }; |
| 176 | |
| 177 | static const Log extra_logs[] = { |
Ben Chan | 36e4228 | 2014-02-12 22:32:34 -0800 | [diff] [blame^] | 178 | #if USE_CELLULAR |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 179 | { "mm-status", "/usr/bin/modem status" }, |
Ben Chan | 36e4228 | 2014-02-12 22:32:34 -0800 | [diff] [blame^] | 180 | #endif // USE_CELLULAR |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 181 | { "network-devices", "/usr/bin/connectivity show devices" }, |
| 182 | { "network-services", "/usr/bin/connectivity show services" }, |
| 183 | { NULL, NULL } |
| 184 | }; |
| 185 | |
Gaurav Shah | f6c8f2a | 2012-10-11 17:22:43 -0700 | [diff] [blame] | 186 | static const Log feedback_logs[] = { |
Ben Chan | 36e4228 | 2014-02-12 22:32:34 -0800 | [diff] [blame^] | 187 | #if USE_CELLULAR |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 188 | { "mm-status", "/usr/bin/modem status-feedback" }, |
Ben Chan | 36e4228 | 2014-02-12 22:32:34 -0800 | [diff] [blame^] | 189 | #endif // USE_CELLULAR |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 190 | { "network-devices", "/usr/bin/connectivity show-feedback devices" }, |
| 191 | { "network-services", "/usr/bin/connectivity show-feedback services" }, |
| 192 | { NULL, NULL } |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 193 | }; |
| 194 | |
Gaurav Shah | f6c8f2a | 2012-10-11 17:22:43 -0700 | [diff] [blame] | 195 | // List of log files that must directly be collected by Chrome. This is because |
| 196 | // debugd is running under a VFS namespace and does not have access to later |
| 197 | // cryptohome mounts. |
| 198 | static const Log user_logs[] = { |
Elly Fong-Jones | 6456ce5 | 2013-04-17 13:31:13 -0400 | [diff] [blame] | 199 | {"chrome_user_log", "log/chrome"}, |
| 200 | {"login-times", "login-times"}, |
| 201 | {"logout-times", "logout-times"}, |
Gaurav Shah | f6c8f2a | 2012-10-11 17:22:43 -0700 | [diff] [blame] | 202 | { NULL, NULL} |
| 203 | }; |
| 204 | |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 205 | LogTool::LogTool() { } |
| 206 | LogTool::~LogTool() { } |
| 207 | |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 208 | bool GetNamedLogFrom(const string& name, const struct Log* logs, |
| 209 | string* result) { |
| 210 | for (size_t i = 0; logs[i].name; i++) { |
| 211 | if (name == logs[i].name) { |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 212 | *result = Run(logs[i]); |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 213 | return true; |
| 214 | } |
| 215 | } |
| 216 | *result = "<invalid log name>"; |
| 217 | return false; |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 220 | string LogTool::GetLog(const string& name, DBus::Error& error) { // NOLINT |
| 221 | string result; |
| 222 | GetNamedLogFrom(name, common_logs, &result) |
| 223 | || GetNamedLogFrom(name, extra_logs, &result) |
| 224 | || GetNamedLogFrom(name, feedback_logs, &result); |
| 225 | return result; |
| 226 | } |
| 227 | |
| 228 | void GetLogsFrom(const struct Log* logs, LogTool::LogMap* map) { |
| 229 | for (size_t i = 0; logs[i].name; i++) |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 230 | (*map)[logs[i].name] = Run(logs[i]); |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | LogTool::LogMap LogTool::GetAllLogs(DBus::Error& error) { // NOLINT |
| 234 | LogMap result; |
| 235 | GetLogsFrom(common_logs, &result); |
| 236 | GetLogsFrom(extra_logs, &result); |
| 237 | return result; |
| 238 | } |
| 239 | |
| 240 | LogTool::LogMap LogTool::GetFeedbackLogs(DBus::Error& error) { // NOLINT |
| 241 | LogMap result; |
| 242 | GetLogsFrom(common_logs, &result); |
| 243 | GetLogsFrom(feedback_logs, &result); |
Darin Petkov | ce9b3a1 | 2013-01-10 16:38:54 +0100 | [diff] [blame] | 244 | AnonymizeLogMap(&result); |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 245 | return result; |
| 246 | } |
| 247 | |
Gaurav Shah | f6c8f2a | 2012-10-11 17:22:43 -0700 | [diff] [blame] | 248 | LogTool::LogMap LogTool::GetUserLogFiles(DBus::Error& error) { // NOLINT |
| 249 | LogMap result; |
| 250 | for (size_t i = 0; user_logs[i].name; ++i) |
| 251 | result[user_logs[i].name] = user_logs[i].command; |
| 252 | return result; |
| 253 | } |
| 254 | |
Darin Petkov | ce9b3a1 | 2013-01-10 16:38:54 +0100 | [diff] [blame] | 255 | void LogTool::AnonymizeLogMap(LogMap* log_map) { |
| 256 | AnonymizerTool anonymizer; |
| 257 | for (LogMap::iterator it = log_map->begin(); |
| 258 | it != log_map->end(); ++it) { |
| 259 | it->second = anonymizer.Anonymize(it->second); |
| 260 | } |
| 261 | } |
| 262 | |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 263 | }; // namespace debugd |