blob: 7afec78ad6b12b716e1d1502085be581e8c723f8 [file] [log] [blame]
Elly Jones03cd6d72012-06-11 13:04:28 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Vakulenko262be3f2014-07-30 15:25:50 -07005#include "debugd/src/log_tool.h"
Elly Jones03cd6d72012-06-11 13:04:28 -04006
Ben Chana0011d82014-05-13 00:19:29 -07007#include <vector>
8
Ben Chanf6cd93a2012-10-14 19:37:00 -07009#include <glib.h>
10
11#include <base/base64.h>
Ben Chancd8fda42014-09-05 08:21:06 -070012#include <base/files/file_util.h>
Elly Jones03cd6d72012-06-11 13:04:28 -040013#include <base/logging.h>
Ben Chan9953a592014-02-05 23:32:00 -080014#include <base/strings/string_split.h>
15#include <base/strings/string_util.h>
Elly Jones03cd6d72012-06-11 13:04:28 -040016
Rebecca Silbersteine78af402014-10-02 10:55:04 -070017#include <chromeos/dbus/service_constants.h>
18
Alex Vakulenko262be3f2014-07-30 15:25:50 -070019#include "debugd/src/anonymizer_tool.h"
20#include "debugd/src/process_with_output.h"
Elly Jones03cd6d72012-06-11 13:04:28 -040021
Rebecca Silbersteine78af402014-10-02 10:55:04 -070022#include "shill/dbus_proxies/org.chromium.flimflam.Manager.h"
23
Elly Jones03cd6d72012-06-11 13:04:28 -040024namespace debugd {
25
26const char *kShell = "/bin/sh";
Elly Fong-Jones215b5622013-03-20 14:32:18 -040027const char *kDebugfsGroup = "debugfs-access";
Elly Jones03cd6d72012-06-11 13:04:28 -040028
Rebecca Silbersteine78af402014-10-02 10:55:04 -070029// Minimum time in seconds needed to allow shill to test active connections.
30const int kConnectionTesterTimeoutSeconds = 5;
31
32class ManagerProxy : public org::chromium::flimflam::Manager_proxy,
33 public DBus::ObjectProxy {
34 public:
35 ManagerProxy(DBus::Connection* connection,
36 const char* path,
37 const char* service)
38 : DBus::ObjectProxy(*connection, path, service) {}
39 ~ManagerProxy() override = default;
40 void PropertyChanged(const std::string&, const DBus::Variant&) override {}
41 void StateChanged(const std::string&) override {}
42};
43
Elly Jones03cd6d72012-06-11 13:04:28 -040044using std::string;
45using std::vector;
46
47typedef vector<string> Strings;
48
Ben Chanf6cd93a2012-10-14 19:37:00 -070049// Returns |value| if |value| is a valid UTF-8 string or a base64-encoded
50// string of |value| otherwise.
51string EnsureUTF8String(const string& value) {
Ben Chanc93d7582014-05-21 22:53:43 -070052 if (base::IsStringUTF8(value))
Ben Chanf6cd93a2012-10-14 19:37:00 -070053 return value;
54
55 gchar* base64_value = g_base64_encode(
56 reinterpret_cast<const guchar*>(value.c_str()), value.length());
57 if (base64_value) {
58 string encoded_value = "<base64>: ";
59 encoded_value += base64_value;
60 g_free(base64_value);
61 return encoded_value;
62 }
63 return "<invalid>";
64}
65
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050066struct Log {
67 const char *name;
68 const char *command;
69 const char *user;
70 const char *group;
71};
72
Elly Fong-Jones57f018c2012-10-08 14:22:01 -040073// TODO(ellyjones): sandbox. crosbug.com/35122
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050074string Run(const Log& log) {
Elly Jones03cd6d72012-06-11 13:04:28 -040075 string output;
76 ProcessWithOutput p;
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050077 string tailed_cmdline = std::string(log.command) + " | tail -c 256K";
78 if (log.user && log.group)
79 p.SandboxAs(log.user, log.group);
Elly Jones03cd6d72012-06-11 13:04:28 -040080 if (!p.Init())
81 return "<not available>";
82 p.AddArg(kShell);
83 p.AddStringOption("-c", tailed_cmdline);
84 if (p.Run())
85 return "<not available>";
86 p.GetOutput(&output);
87 if (!output.size())
88 return "<empty>";
Ben Chanf6cd93a2012-10-14 19:37:00 -070089 return EnsureUTF8String(output);
Elly Jones03cd6d72012-06-11 13:04:28 -040090}
91
Elly Jones533c7c42012-08-10 15:07:05 -040092static const Log common_logs[] = {
Elly Jones03cd6d72012-06-11 13:04:28 -040093 { "CLIENT_ID", "/bin/cat '/home/chronos/Consent To Send Stats'" },
94 { "LOGDATE", "/bin/date" },
95 { "Xorg.0.log", "/bin/cat /var/log/Xorg.0.log" },
Elly Jones03cd6d72012-06-11 13:04:28 -040096 { "bios_info", "/bin/cat /var/log/bios_info.txt" },
Vadim Bendebury64aeeed2013-09-16 13:16:49 -070097 { "bios_log",
98 "/bin/cat /sys/firmware/log "
99 "/proc/device-tree/chosen/ap-console-buffer 2> /dev/null" },
Stefan Reinauer4393fa72012-10-18 11:08:09 -0700100 { "bios_times", "/bin/cat /var/log/bios_times.txt" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400101 { "board-specific",
102 "/usr/share/userfeedback/scripts/get_board_specific_info" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400103 { "chrome_system_log", "/bin/cat /var/log/chrome/chrome" },
Stefan Reinauer48f883d2014-08-22 14:31:58 -0700104 { "console-ramoops", "/bin/cat /dev/pstore/console-ramoops 2> /dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400105 { "cpu", "/usr/bin/uname -p" },
106 { "cpuinfo", "/bin/cat /proc/cpuinfo" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400107 { "dmesg", "/bin/dmesg" },
108 { "ec_info", "/bin/cat /var/log/ec_info.txt" },
Stefan Reinauer4393fa72012-10-18 11:08:09 -0700109 { "eventlog", "/bin/cat /var/log/eventlog.txt" },
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400110 {
111 "exynos_gem_objects",
Yuly Novikov13ece852013-07-11 17:19:10 -0400112 "/bin/cat /sys/kernel/debug/dri/0/exynos_gem_objects 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400113 SandboxedProcess::kDefaultUser,
114 kDebugfsGroup
115 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400116 { "font_info", "/usr/share/userfeedback/scripts/font_info" },
117 { "hardware_class", "/usr/bin/crossystem hwid" },
118 { "hostname", "/bin/hostname" },
119 { "hw_platform", "/usr/bin/uname -i" },
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400120 {
121 "i915_gem_gtt",
Yuly Novikov13ece852013-07-11 17:19:10 -0400122 "/bin/cat /sys/kernel/debug/dri/0/i915_gem_gtt 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400123 SandboxedProcess::kDefaultUser,
124 kDebugfsGroup
125 },
126 {
127 "i915_gem_objects",
Yuly Novikov13ece852013-07-11 17:19:10 -0400128 "/bin/cat /sys/kernel/debug/dri/0/i915_gem_objects 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400129 SandboxedProcess::kDefaultUser,
130 kDebugfsGroup
131 },
132 {
133 "i915_error_state",
Yuly Novikov4ac12fb2013-07-18 20:08:44 -0400134 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400135 SandboxedProcess::kDefaultUser,
136 kDebugfsGroup,
137 },
Daniel Erat982ab4b2014-04-09 07:32:38 -0700138 { "ifconfig", "/bin/ifconfig -a" },
Stefan Reinauer48f883d2014-08-22 14:31:58 -0700139 { "kernel-crashes",
140 "/bin/cat /var/spool/crash/kernel.*.kcrash 2> /dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400141 { "lsmod", "lsmod" },
142 { "lspci", "/usr/sbin/lspci" },
143 { "lsusb", "lsusb" },
Yuly Novikov13ece852013-07-11 17:19:10 -0400144 {
145 "mali_memory",
Dominik Behr4765e6d2013-08-26 16:34:08 -0700146 "/bin/cat /sys/class/misc/mali0/device/memory 2> /dev/null"
Yuly Novikov13ece852013-07-11 17:19:10 -0400147 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400148 { "meminfo", "cat /proc/meminfo" },
149 { "memory_spd_info", "/bin/cat /var/log/memory_spd_info.txt" },
Kees Cooka8f3e7e2012-11-19 14:16:01 -0800150 { "mount-encrypted", "/bin/cat /var/log/mount-encrypted.log" },
Darin Petkov54743582012-12-10 14:18:32 +0100151 { "net-diags.net.log", "/bin/cat /var/log/net-diags.net.log" },
Wade Guthrie67b672e2012-11-14 13:14:31 -0800152 { "netlog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'"
153 " /var/log/net.log" },
henryhsufa6daa12014-09-24 13:26:30 +0800154 {
155 "nvmap_iovmm",
156 "/bin/cat /sys/kernel/debug/nvmap/iovmm/allocations 2> /dev/null",
157 SandboxedProcess::kDefaultUser,
158 kDebugfsGroup,
159 },
Vincent Palatinc64af9d2013-08-05 16:34:10 -0700160 { "platform_info", "/bin/cat /var/log/platform_info.txt" },
Daniel Erat9b58b6d2013-04-30 14:58:56 -0700161 { "power_supply_info", "/usr/bin/power_supply_info" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400162 { "powerd.LATEST", "/bin/cat /var/log/power_manager/powerd.LATEST" },
Daniel Erat546c3882013-11-22 12:46:01 -0800163 { "powerd.PREVIOUS", "/bin/cat /var/log/power_manager/powerd.PREVIOUS" },
Daniel Erat93da9a12012-12-06 14:14:17 -0800164 { "powerd.out", "/bin/cat /var/log/powerd.out" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400165 // Changed from 'ps ux' to 'ps aux' since we're running as debugd, not chronos
166 { "ps", "/bin/ps aux" },
Puthikorn Voravootivat6de5e122013-12-06 11:43:13 -0800167 { "storage_info", "/bin/cat /var/log/storage_info.txt" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400168 { "syslog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'"
Elly Jonesf8a67ee2012-06-19 13:48:38 -0400169 " /var/log/messages" },
Elly Fong-Jonesb4f49c42013-02-28 13:45:26 -0500170 { "tlsdate", "/bin/cat /var/log/tlsdate.log" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400171 { "touchpad", "/opt/google/touchpad/tpcontrol status" },
Chung-yih Wang4b876142014-04-30 17:01:49 +0800172 { "touchpad_activity", "/opt/google/input/cmt_feedback alt" },
Andrew de los Reyesc060c342013-04-10 13:46:30 -0700173 { "touch_fw_version", "grep -E"
174 " 'synaptics: Touchpad model|chromeos-touch-[a-z]*-update'"
175 " /var/log/messages | tail -n 20" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400176 { "ui_log", "/usr/share/userfeedback/scripts/get_log /var/log/ui/ui.LATEST" },
177 { "uname", "/bin/uname -a" },
178 { "update_engine.log", "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
Elly Jonesd31aee22012-07-02 11:09:10 -0400179 " s.^./var/log/update_engine/.)" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400180 { "verified boot", "/bin/cat /var/log/debug_vboot_noisy.log" },
181 { "vpd_2.0", "/bin/cat /var/log/vpd_2.0.txt" },
Samuel Tan107b6c82014-07-10 15:33:44 -0700182 { "wifi_status", "/usr/bin/network_diag --wifi-internal --no-log" },
Sonny Raoab5f9952013-07-17 14:13:53 -0700183 { "zram compressed data size",
184 "/bin/cat /sys/block/zram0/compr_data_size 2> /dev/null" },
185 { "zram original data size",
186 "/bin/cat /sys/block/zram0/orig_data_size 2> /dev/null" },
187 { "zram total memory used",
188 "/bin/cat /sys/block/zram0/mem_used_total 2> /dev/null" },
189 { "zram total reads",
190 "/bin/cat /sys/block/zram0/num_reads 2> /dev/null" },
191 { "zram total writes",
192 "/bin/cat /sys/block/zram0/num_writes 2> /dev/null" },
Gaurav Shah6e8fd892012-10-01 11:51:08 -0700193
Elly Jones03cd6d72012-06-11 13:04:28 -0400194 // Stuff pulled out of the original list. These need access to the running X
195 // session, which we'd rather not give to debugd, or return info specific to
196 // the current session (in the setsid(2) sense), which is not useful for
197 // debugd
198 // { "env", "set" },
199 // { "setxkbmap", "/usr/bin/setxkbmap -print -query" },
200 // { "xrandr", "/usr/bin/xrandr --verbose" }
Elly Jones533c7c42012-08-10 15:07:05 -0400201 { NULL, NULL }
202};
203
204static const Log extra_logs[] = {
Ben Chan36e42282014-02-12 22:32:34 -0800205#if USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400206 { "mm-status", "/usr/bin/modem status" },
Ben Chan36e42282014-02-12 22:32:34 -0800207#endif // USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400208 { "network-devices", "/usr/bin/connectivity show devices" },
209 { "network-services", "/usr/bin/connectivity show services" },
210 { NULL, NULL }
211};
212
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700213static const Log feedback_logs[] = {
Ben Chan36e42282014-02-12 22:32:34 -0800214#if USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400215 { "mm-status", "/usr/bin/modem status-feedback" },
Ben Chan36e42282014-02-12 22:32:34 -0800216#endif // USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400217 { "network-devices", "/usr/bin/connectivity show-feedback devices" },
218 { "network-services", "/usr/bin/connectivity show-feedback services" },
219 { NULL, NULL }
Elly Jones03cd6d72012-06-11 13:04:28 -0400220};
221
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700222// List of log files that must directly be collected by Chrome. This is because
223// debugd is running under a VFS namespace and does not have access to later
224// cryptohome mounts.
225static const Log user_logs[] = {
Elly Fong-Jones6456ce52013-04-17 13:31:13 -0400226 {"chrome_user_log", "log/chrome"},
227 {"login-times", "login-times"},
228 {"logout-times", "logout-times"},
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700229 { NULL, NULL}
230};
231
Elly Jones533c7c42012-08-10 15:07:05 -0400232bool GetNamedLogFrom(const string& name, const struct Log* logs,
233 string* result) {
234 for (size_t i = 0; logs[i].name; i++) {
235 if (name == logs[i].name) {
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -0500236 *result = Run(logs[i]);
Elly Jones533c7c42012-08-10 15:07:05 -0400237 return true;
238 }
239 }
240 *result = "<invalid log name>";
241 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400242}
243
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700244void LogTool::CreateConnectivityReport(DBus::Connection* connection) {
245 // Perform ConnectivityTrial to report connection state in feedback log.
246 ManagerProxy shill(connection,
247 shill::kFlimflamServicePath,
248 shill::kFlimflamServiceName);
249 shill.CreateConnectivityReport();
250 // Give the connection trial time to test the connection and log the results
251 // before collecting the logs for feedback.
252 // TODO(silberst): Replace the simple approach of a single timeout with a more
253 // coordinated effort.
254 sleep(kConnectionTesterTimeoutSeconds);
255}
256
Ben Chana0011d82014-05-13 00:19:29 -0700257string LogTool::GetLog(const string& name, DBus::Error* error) {
Elly Jones533c7c42012-08-10 15:07:05 -0400258 string result;
259 GetNamedLogFrom(name, common_logs, &result)
260 || GetNamedLogFrom(name, extra_logs, &result)
261 || GetNamedLogFrom(name, feedback_logs, &result);
262 return result;
263}
264
265void GetLogsFrom(const struct Log* logs, LogTool::LogMap* map) {
266 for (size_t i = 0; logs[i].name; i++)
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -0500267 (*map)[logs[i].name] = Run(logs[i]);
Elly Jones533c7c42012-08-10 15:07:05 -0400268}
269
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700270LogTool::LogMap LogTool::GetAllLogs(DBus::Connection* connection,
271 DBus::Error* error) {
272 CreateConnectivityReport(connection);
Elly Jones533c7c42012-08-10 15:07:05 -0400273 LogMap result;
274 GetLogsFrom(common_logs, &result);
275 GetLogsFrom(extra_logs, &result);
276 return result;
277}
278
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700279LogTool::LogMap LogTool::GetFeedbackLogs(DBus::Connection* connection,
280 DBus::Error* error) {
281 CreateConnectivityReport(connection);
Elly Jones533c7c42012-08-10 15:07:05 -0400282 LogMap result;
283 GetLogsFrom(common_logs, &result);
284 GetLogsFrom(feedback_logs, &result);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100285 AnonymizeLogMap(&result);
Elly Jones03cd6d72012-06-11 13:04:28 -0400286 return result;
287}
288
Ben Chana0011d82014-05-13 00:19:29 -0700289LogTool::LogMap LogTool::GetUserLogFiles(DBus::Error* error) {
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700290 LogMap result;
291 for (size_t i = 0; user_logs[i].name; ++i)
292 result[user_logs[i].name] = user_logs[i].command;
293 return result;
294}
295
Darin Petkovce9b3a12013-01-10 16:38:54 +0100296void LogTool::AnonymizeLogMap(LogMap* log_map) {
297 AnonymizerTool anonymizer;
298 for (LogMap::iterator it = log_map->begin();
299 it != log_map->end(); ++it) {
300 it->second = anonymizer.Anonymize(it->second);
301 }
302}
303
Ben Chana0011d82014-05-13 00:19:29 -0700304} // namespace debugd