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> |
| 12 | #include <base/string_split.h> |
Ben Chan | f6cd93a | 2012-10-14 19:37:00 -0700 | [diff] [blame] | 13 | #include <base/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"; |
| 21 | |
| 22 | using std::string; |
| 23 | using std::vector; |
| 24 | |
| 25 | typedef vector<string> Strings; |
| 26 | |
Ben Chan | f6cd93a | 2012-10-14 19:37:00 -0700 | [diff] [blame] | 27 | // Returns |value| if |value| is a valid UTF-8 string or a base64-encoded |
| 28 | // string of |value| otherwise. |
| 29 | string EnsureUTF8String(const string& value) { |
| 30 | if (IsStringUTF8(value)) |
| 31 | return value; |
| 32 | |
| 33 | gchar* base64_value = g_base64_encode( |
| 34 | reinterpret_cast<const guchar*>(value.c_str()), value.length()); |
| 35 | if (base64_value) { |
| 36 | string encoded_value = "<base64>: "; |
| 37 | encoded_value += base64_value; |
| 38 | g_free(base64_value); |
| 39 | return encoded_value; |
| 40 | } |
| 41 | return "<invalid>"; |
| 42 | } |
| 43 | |
Elly Fong-Jones | 57f018c | 2012-10-08 14:22:01 -0400 | [diff] [blame] | 44 | // TODO(ellyjones): sandbox. crosbug.com/35122 |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 45 | string Run(const string& cmdline) { |
| 46 | string output; |
| 47 | ProcessWithOutput p; |
Elly Jones | e0518ff | 2012-08-13 13:22:46 -0400 | [diff] [blame] | 48 | string tailed_cmdline = cmdline + " | tail -c 256K"; |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 49 | if (!p.Init()) |
| 50 | return "<not available>"; |
| 51 | p.AddArg(kShell); |
| 52 | p.AddStringOption("-c", tailed_cmdline); |
| 53 | if (p.Run()) |
| 54 | return "<not available>"; |
| 55 | p.GetOutput(&output); |
| 56 | if (!output.size()) |
| 57 | return "<empty>"; |
Ben Chan | f6cd93a | 2012-10-14 19:37:00 -0700 | [diff] [blame] | 58 | return EnsureUTF8String(output); |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | struct Log { |
| 62 | const char *name; |
| 63 | const char *command; |
| 64 | }; |
| 65 | |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 66 | static const Log common_logs[] = { |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 67 | { "CLIENT_ID", "/bin/cat '/home/chronos/Consent To Send Stats'" }, |
| 68 | { "LOGDATE", "/bin/date" }, |
| 69 | { "Xorg.0.log", "/bin/cat /var/log/Xorg.0.log" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 70 | { "bios_info", "/bin/cat /var/log/bios_info.txt" }, |
Stefan Reinauer | 4393fa7 | 2012-10-18 11:08:09 -0700 | [diff] [blame] | 71 | { "bios_log", "/bin/cat /sys/firmware/log" }, |
| 72 | { "bios_times", "/bin/cat /var/log/bios_times.txt" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 73 | { "board-specific", |
| 74 | "/usr/share/userfeedback/scripts/get_board_specific_info" }, |
| 75 | { "boot_times", "/bin/cat /tmp/boot-times-sent" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 76 | { "chrome_system_log", "/bin/cat /var/log/chrome/chrome" }, |
Elly Fong-Jones | 57f018c | 2012-10-08 14:22:01 -0400 | [diff] [blame] | 77 | { "console-ramoops", "/bin/cat /dev/pstore/console-ramoops" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 78 | { "cpu", "/usr/bin/uname -p" }, |
| 79 | { "cpuinfo", "/bin/cat /proc/cpuinfo" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 80 | { "dmesg", "/bin/dmesg" }, |
| 81 | { "ec_info", "/bin/cat /var/log/ec_info.txt" }, |
Stefan Reinauer | 4393fa7 | 2012-10-18 11:08:09 -0700 | [diff] [blame] | 82 | { "eventlog", "/bin/cat /var/log/eventlog.txt" }, |
Sonny Rao | db87681 | 2012-11-16 16:16:41 -0800 | [diff] [blame] | 83 | { "exynos_gem_objects", |
| 84 | "/bin/cat /sys/kernel/debug/dri/0/exynos_gem_objects" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 85 | { "font_info", "/usr/share/userfeedback/scripts/font_info" }, |
| 86 | { "hardware_class", "/usr/bin/crossystem hwid" }, |
| 87 | { "hostname", "/bin/hostname" }, |
| 88 | { "hw_platform", "/usr/bin/uname -i" }, |
Sonny Rao | fc348f6 | 2012-10-24 18:11:04 -0700 | [diff] [blame] | 89 | { "i915_gem_gtt", "/bin/cat /sys/kernel/debug/dri/0/i915_gem_gtt" }, |
| 90 | { "i915_gem_objects", "/bin/cat /sys/kernel/debug/dri/0/i915_gem_objects" }, |
Stéphane Marchesin | 1f9ff86 | 2012-12-21 16:29:53 -0800 | [diff] [blame] | 91 | { "i915_error_state", "/bin/cat /sys/kernel/debug/dri/0/i915_error_state" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 92 | { "ifconfig", "/sbin/ifconfig -a" }, |
Elly Jones | 2bde94d | 2012-07-31 18:07:46 -0400 | [diff] [blame] | 93 | { "kernel-crashes", "/bin/cat /var/spool/crash/kernel.*.kcrash" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 94 | { "lsmod", "lsmod" }, |
| 95 | { "lspci", "/usr/sbin/lspci" }, |
| 96 | { "lsusb", "lsusb" }, |
Sonny Rao | db87681 | 2012-11-16 16:16:41 -0800 | [diff] [blame] | 97 | { "mali_memory", "/bin/cat /sys/devices/platform/mali.0/memory" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 98 | { "meminfo", "cat /proc/meminfo" }, |
| 99 | { "memory_spd_info", "/bin/cat /var/log/memory_spd_info.txt" }, |
Kees Cook | a8f3e7e | 2012-11-19 14:16:01 -0800 | [diff] [blame] | 100 | { "mount-encrypted", "/bin/cat /var/log/mount-encrypted.log" }, |
Darin Petkov | 5474358 | 2012-12-10 14:18:32 +0100 | [diff] [blame] | 101 | { "net-diags.net.log", "/bin/cat /var/log/net-diags.net.log" }, |
Wade Guthrie | 67b672e | 2012-11-14 13:14:31 -0800 | [diff] [blame] | 102 | { "netlog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'" |
| 103 | " /var/log/net.log" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 104 | { "power-supply-info", "/usr/bin/power-supply-info" }, |
| 105 | { "powerd.LATEST", "/bin/cat /var/log/power_manager/powerd.LATEST" }, |
Daniel Erat | 93da9a1 | 2012-12-06 14:14:17 -0800 | [diff] [blame] | 106 | { "powerd.out", "/bin/cat /var/log/powerd.out" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 107 | { "powerm.LATEST", "/bin/cat /var/log/power_manager/powerm.LATEST" }, |
Daniel Erat | 93da9a1 | 2012-12-06 14:14:17 -0800 | [diff] [blame] | 108 | { "powerm.out", "/bin/cat /var/log/powerm.out" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 109 | // Changed from 'ps ux' to 'ps aux' since we're running as debugd, not chronos |
| 110 | { "ps", "/bin/ps aux" }, |
| 111 | { "syslog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'" |
Elly Jones | f8a67ee | 2012-06-19 13:48:38 -0400 | [diff] [blame] | 112 | " /var/log/messages" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 113 | { "touchpad", "/opt/google/touchpad/tpcontrol status" }, |
Elly Jones | d31aee2 | 2012-07-02 11:09:10 -0400 | [diff] [blame] | 114 | { "touchpad_activity", "/opt/google/touchpad/generate_userfeedback alt" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 115 | { "ui_log", "/usr/share/userfeedback/scripts/get_log /var/log/ui/ui.LATEST" }, |
| 116 | { "uname", "/bin/uname -a" }, |
| 117 | { "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] | 118 | " s.^./var/log/update_engine/.)" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 119 | { "verified boot", "/bin/cat /var/log/debug_vboot_noisy.log" }, |
| 120 | { "vpd_2.0", "/bin/cat /var/log/vpd_2.0.txt" }, |
| 121 | { "wifi_status", "/usr/bin/network_diagnostics --wifi --no-log" }, |
| 122 | |
Gaurav Shah | 6e8fd89 | 2012-10-01 11:51:08 -0700 | [diff] [blame] | 123 | |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 124 | // Stuff pulled out of the original list. These need access to the running X |
| 125 | // session, which we'd rather not give to debugd, or return info specific to |
| 126 | // the current session (in the setsid(2) sense), which is not useful for |
| 127 | // debugd |
| 128 | // { "env", "set" }, |
| 129 | // { "setxkbmap", "/usr/bin/setxkbmap -print -query" }, |
| 130 | // { "xrandr", "/usr/bin/xrandr --verbose" } |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 131 | { NULL, NULL } |
| 132 | }; |
| 133 | |
| 134 | static const Log extra_logs[] = { |
| 135 | { "mm-status", "/usr/bin/modem status" }, |
| 136 | { "network-devices", "/usr/bin/connectivity show devices" }, |
| 137 | { "network-services", "/usr/bin/connectivity show services" }, |
| 138 | { NULL, NULL } |
| 139 | }; |
| 140 | |
Gaurav Shah | f6c8f2a | 2012-10-11 17:22:43 -0700 | [diff] [blame] | 141 | static const Log feedback_logs[] = { |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 142 | { "mm-status", "/usr/bin/modem status-feedback" }, |
| 143 | { "network-devices", "/usr/bin/connectivity show-feedback devices" }, |
| 144 | { "network-services", "/usr/bin/connectivity show-feedback services" }, |
| 145 | { NULL, NULL } |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 146 | }; |
| 147 | |
Gaurav Shah | f6c8f2a | 2012-10-11 17:22:43 -0700 | [diff] [blame] | 148 | // List of log files that must directly be collected by Chrome. This is because |
| 149 | // debugd is running under a VFS namespace and does not have access to later |
| 150 | // cryptohome mounts. |
| 151 | static const Log user_logs[] = { |
| 152 | {"chrome_user_log", "/home/chronos/user/log/chrome"}, |
| 153 | {"login-times", "/home/chronos/user/login-times"}, |
| 154 | {"logout-times", "/home/chronos/user/logout-times"}, |
| 155 | { NULL, NULL} |
| 156 | }; |
| 157 | |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 158 | LogTool::LogTool() { } |
| 159 | LogTool::~LogTool() { } |
| 160 | |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 161 | bool GetNamedLogFrom(const string& name, const struct Log* logs, |
| 162 | string* result) { |
| 163 | for (size_t i = 0; logs[i].name; i++) { |
| 164 | if (name == logs[i].name) { |
| 165 | *result = Run(logs[i].command); |
| 166 | return true; |
| 167 | } |
| 168 | } |
| 169 | *result = "<invalid log name>"; |
| 170 | return false; |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 171 | } |
| 172 | |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 173 | string LogTool::GetLog(const string& name, DBus::Error& error) { // NOLINT |
| 174 | string result; |
| 175 | GetNamedLogFrom(name, common_logs, &result) |
| 176 | || GetNamedLogFrom(name, extra_logs, &result) |
| 177 | || GetNamedLogFrom(name, feedback_logs, &result); |
| 178 | return result; |
| 179 | } |
| 180 | |
| 181 | void GetLogsFrom(const struct Log* logs, LogTool::LogMap* map) { |
| 182 | for (size_t i = 0; logs[i].name; i++) |
| 183 | (*map)[logs[i].name] = Run(logs[i].command); |
| 184 | } |
| 185 | |
| 186 | LogTool::LogMap LogTool::GetAllLogs(DBus::Error& error) { // NOLINT |
| 187 | LogMap result; |
| 188 | GetLogsFrom(common_logs, &result); |
| 189 | GetLogsFrom(extra_logs, &result); |
| 190 | return result; |
| 191 | } |
| 192 | |
| 193 | LogTool::LogMap LogTool::GetFeedbackLogs(DBus::Error& error) { // NOLINT |
| 194 | LogMap result; |
| 195 | GetLogsFrom(common_logs, &result); |
| 196 | GetLogsFrom(feedback_logs, &result); |
Darin Petkov | ce9b3a1 | 2013-01-10 16:38:54 +0100 | [diff] [blame^] | 197 | AnonymizeLogMap(&result); |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 198 | return result; |
| 199 | } |
| 200 | |
Gaurav Shah | f6c8f2a | 2012-10-11 17:22:43 -0700 | [diff] [blame] | 201 | LogTool::LogMap LogTool::GetUserLogFiles(DBus::Error& error) { // NOLINT |
| 202 | LogMap result; |
| 203 | for (size_t i = 0; user_logs[i].name; ++i) |
| 204 | result[user_logs[i].name] = user_logs[i].command; |
| 205 | return result; |
| 206 | } |
| 207 | |
Darin Petkov | ce9b3a1 | 2013-01-10 16:38:54 +0100 | [diff] [blame^] | 208 | void LogTool::AnonymizeLogMap(LogMap* log_map) { |
| 209 | AnonymizerTool anonymizer; |
| 210 | for (LogMap::iterator it = log_map->begin(); |
| 211 | it != log_map->end(); ++it) { |
| 212 | it->second = anonymizer.Anonymize(it->second); |
| 213 | } |
| 214 | } |
| 215 | |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 216 | }; // namespace debugd |