blob: fca5629e5e56dc64468102f4fdd05039a206f2fc [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
Ben Chancd8fda42014-09-05 08:21:06 -070011#include <base/files/file_util.h>
Elly Jones03cd6d72012-06-11 13:04:28 -040012#include <base/logging.h>
Ben Chan9953a592014-02-05 23:32:00 -080013#include <base/strings/string_split.h>
14#include <base/strings/string_util.h>
Elly Jones03cd6d72012-06-11 13:04:28 -040015
Rebecca Silbersteine78af402014-10-02 10:55:04 -070016#include <chromeos/dbus/service_constants.h>
Peter Qiued45fda2015-09-09 16:52:50 -070017#include <shill/dbus_proxies/org.chromium.flimflam.Manager.h>
Rebecca Silbersteine78af402014-10-02 10:55:04 -070018
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
22namespace debugd {
23
24const char *kShell = "/bin/sh";
Elly Fong-Jones215b5622013-03-20 14:32:18 -040025const char *kDebugfsGroup = "debugfs-access";
Elly Jones03cd6d72012-06-11 13:04:28 -040026
Rebecca Silbersteine78af402014-10-02 10:55:04 -070027// Minimum time in seconds needed to allow shill to test active connections.
28const int kConnectionTesterTimeoutSeconds = 5;
29
30class ManagerProxy : public org::chromium::flimflam::Manager_proxy,
31 public DBus::ObjectProxy {
32 public:
33 ManagerProxy(DBus::Connection* connection,
34 const char* path,
35 const char* service)
36 : DBus::ObjectProxy(*connection, path, service) {}
37 ~ManagerProxy() override = default;
38 void PropertyChanged(const std::string&, const DBus::Variant&) override {}
39 void StateChanged(const std::string&) override {}
40};
41
Elly Jones03cd6d72012-06-11 13:04:28 -040042using std::string;
43using std::vector;
44
45typedef vector<string> Strings;
46
Ben Chanf6cd93a2012-10-14 19:37:00 -070047// Returns |value| if |value| is a valid UTF-8 string or a base64-encoded
48// string of |value| otherwise.
49string EnsureUTF8String(const string& value) {
Ben Chanc93d7582014-05-21 22:53:43 -070050 if (base::IsStringUTF8(value))
Ben Chanf6cd93a2012-10-14 19:37:00 -070051 return value;
52
53 gchar* base64_value = g_base64_encode(
54 reinterpret_cast<const guchar*>(value.c_str()), value.length());
55 if (base64_value) {
56 string encoded_value = "<base64>: ";
57 encoded_value += base64_value;
58 g_free(base64_value);
59 return encoded_value;
60 }
61 return "<invalid>";
62}
63
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050064struct Log {
65 const char *name;
66 const char *command;
67 const char *user;
68 const char *group;
69};
70
Elly Fong-Jones57f018c2012-10-08 14:22:01 -040071// TODO(ellyjones): sandbox. crosbug.com/35122
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050072string Run(const Log& log) {
Elly Jones03cd6d72012-06-11 13:04:28 -040073 string output;
74 ProcessWithOutput p;
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050075 string tailed_cmdline = std::string(log.command) + " | tail -c 256K";
76 if (log.user && log.group)
77 p.SandboxAs(log.user, log.group);
Elly Jones03cd6d72012-06-11 13:04:28 -040078 if (!p.Init())
79 return "<not available>";
80 p.AddArg(kShell);
81 p.AddStringOption("-c", tailed_cmdline);
82 if (p.Run())
83 return "<not available>";
84 p.GetOutput(&output);
85 if (!output.size())
86 return "<empty>";
Ben Chanf6cd93a2012-10-14 19:37:00 -070087 return EnsureUTF8String(output);
Elly Jones03cd6d72012-06-11 13:04:28 -040088}
89
Elly Jones533c7c42012-08-10 15:07:05 -040090static const Log common_logs[] = {
Elly Jones03cd6d72012-06-11 13:04:28 -040091 { "CLIENT_ID", "/bin/cat '/home/chronos/Consent To Send Stats'" },
92 { "LOGDATE", "/bin/date" },
93 { "Xorg.0.log", "/bin/cat /var/log/Xorg.0.log" },
Elly Jones03cd6d72012-06-11 13:04:28 -040094 { "bios_info", "/bin/cat /var/log/bios_info.txt" },
Vadim Bendebury64aeeed2013-09-16 13:16:49 -070095 { "bios_log",
96 "/bin/cat /sys/firmware/log "
97 "/proc/device-tree/chosen/ap-console-buffer 2> /dev/null" },
Stefan Reinauer4393fa72012-10-18 11:08:09 -070098 { "bios_times", "/bin/cat /var/log/bios_times.txt" },
Elly Jones03cd6d72012-06-11 13:04:28 -040099 { "board-specific",
Thiemo Nagela269ad22014-11-27 17:13:01 +0100100 "/usr/share/userfeedback/scripts/get_board_specific_info" },
Olof Johanssone5d0fd32016-01-29 14:40:34 -0800101 { "cheets_log", "/usr/bin/collect-cheets-logs 2>&1" },
Thiemo Nagela269ad22014-11-27 17:13:01 +0100102 { "clobber.log", "/bin/cat /var/log/clobber.log 2> /dev/null" },
103 { "clobber-state.log", "/bin/cat /var/log/clobber-state.log 2> /dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400104 { "chrome_system_log", "/bin/cat /var/log/chrome/chrome" },
Stefan Reinauer48f883d2014-08-22 14:31:58 -0700105 { "console-ramoops", "/bin/cat /dev/pstore/console-ramoops 2> /dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400106 { "cpu", "/usr/bin/uname -p" },
107 { "cpuinfo", "/bin/cat /proc/cpuinfo" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400108 { "dmesg", "/bin/dmesg" },
109 { "ec_info", "/bin/cat /var/log/ec_info.txt" },
Stefan Reinauer4393fa72012-10-18 11:08:09 -0700110 { "eventlog", "/bin/cat /var/log/eventlog.txt" },
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400111 {
112 "exynos_gem_objects",
Yuly Novikov13ece852013-07-11 17:19:10 -0400113 "/bin/cat /sys/kernel/debug/dri/0/exynos_gem_objects 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400114 SandboxedProcess::kDefaultUser,
115 kDebugfsGroup
116 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400117 { "font_info", "/usr/share/userfeedback/scripts/font_info" },
118 { "hardware_class", "/usr/bin/crossystem hwid" },
119 { "hostname", "/bin/hostname" },
120 { "hw_platform", "/usr/bin/uname -i" },
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400121 {
122 "i915_gem_gtt",
Yuly Novikov13ece852013-07-11 17:19:10 -0400123 "/bin/cat /sys/kernel/debug/dri/0/i915_gem_gtt 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400124 SandboxedProcess::kDefaultUser,
125 kDebugfsGroup
126 },
127 {
128 "i915_gem_objects",
Yuly Novikov13ece852013-07-11 17:19:10 -0400129 "/bin/cat /sys/kernel/debug/dri/0/i915_gem_objects 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400130 SandboxedProcess::kDefaultUser,
131 kDebugfsGroup
132 },
133 {
134 "i915_error_state",
Yuly Novikov4ac12fb2013-07-18 20:08:44 -0400135 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400136 SandboxedProcess::kDefaultUser,
137 kDebugfsGroup,
138 },
Daniel Erat982ab4b2014-04-09 07:32:38 -0700139 { "ifconfig", "/bin/ifconfig -a" },
Stefan Reinauer48f883d2014-08-22 14:31:58 -0700140 { "kernel-crashes",
141 "/bin/cat /var/spool/crash/kernel.*.kcrash 2> /dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400142 { "lsmod", "lsmod" },
143 { "lspci", "/usr/sbin/lspci" },
144 { "lsusb", "lsusb" },
Yuly Novikov13ece852013-07-11 17:19:10 -0400145 {
Thiemo Nagela269ad22014-11-27 17:13:01 +0100146 "mali_memory",
147 "/bin/cat /sys/class/misc/mali0/device/memory 2> /dev/null"
Yuly Novikov13ece852013-07-11 17:19:10 -0400148 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400149 { "meminfo", "cat /proc/meminfo" },
150 { "memory_spd_info", "/bin/cat /var/log/memory_spd_info.txt" },
Kees Cooka8f3e7e2012-11-19 14:16:01 -0800151 { "mount-encrypted", "/bin/cat /var/log/mount-encrypted.log" },
Darin Petkov54743582012-12-10 14:18:32 +0100152 { "net-diags.net.log", "/bin/cat /var/log/net-diags.net.log" },
Wade Guthrie67b672e2012-11-14 13:14:31 -0800153 { "netlog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'"
154 " /var/log/net.log" },
henryhsufa6daa12014-09-24 13:26:30 +0800155 {
156 "nvmap_iovmm",
157 "/bin/cat /sys/kernel/debug/nvmap/iovmm/allocations 2> /dev/null",
158 SandboxedProcess::kDefaultUser,
159 kDebugfsGroup,
160 },
Vincent Palatinc64af9d2013-08-05 16:34:10 -0700161 { "platform_info", "/bin/cat /var/log/platform_info.txt" },
Daniel Erat9b58b6d2013-04-30 14:58:56 -0700162 { "power_supply_info", "/usr/bin/power_supply_info" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400163 { "powerd.LATEST", "/bin/cat /var/log/power_manager/powerd.LATEST" },
Daniel Erat546c3882013-11-22 12:46:01 -0800164 { "powerd.PREVIOUS", "/bin/cat /var/log/power_manager/powerd.PREVIOUS" },
Daniel Erat93da9a12012-12-06 14:14:17 -0800165 { "powerd.out", "/bin/cat /var/log/powerd.out" },
Thiemo Nagela269ad22014-11-27 17:13:01 +0100166 { "powerwash_count", "/bin/cat /var/log/powerwash_count 2> /dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400167 // Changed from 'ps ux' to 'ps aux' since we're running as debugd, not chronos
168 { "ps", "/bin/ps aux" },
Puthikorn Voravootivat6de5e122013-12-06 11:43:13 -0800169 { "storage_info", "/bin/cat /var/log/storage_info.txt" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400170 { "syslog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'"
Elly Jonesf8a67ee2012-06-19 13:48:38 -0400171 " /var/log/messages" },
Elly Fong-Jonesb4f49c42013-02-28 13:45:26 -0500172 { "tlsdate", "/bin/cat /var/log/tlsdate.log" },
Michael Spang387567d2015-03-11 17:16:04 -0400173 { "input_devices", "/bin/cat /proc/bus/input/devices" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400174 { "touchpad", "/opt/google/touchpad/tpcontrol status" },
Chung-yih Wang4b876142014-04-30 17:01:49 +0800175 { "touchpad_activity", "/opt/google/input/cmt_feedback alt" },
Andrew de los Reyesc060c342013-04-10 13:46:30 -0700176 { "touch_fw_version", "grep -E"
Charlie Mooneybb08f982016-02-04 15:35:56 -0800177 " -e 'synaptics: Touchpad model'"
178 " -e 'chromeos-[a-z]*-touch-[a-z]*-update'"
Andrew de los Reyesc060c342013-04-10 13:46:30 -0700179 " /var/log/messages | tail -n 20" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400180 { "ui_log", "/usr/share/userfeedback/scripts/get_log /var/log/ui/ui.LATEST" },
181 { "uname", "/bin/uname -a" },
182 { "update_engine.log", "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
Thiemo Nagela269ad22014-11-27 17:13:01 +0100183 " s.^./var/log/update_engine/.)" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400184 { "verified boot", "/bin/cat /var/log/debug_vboot_noisy.log" },
185 { "vpd_2.0", "/bin/cat /var/log/vpd_2.0.txt" },
Samuel Tan107b6c82014-07-10 15:33:44 -0700186 { "wifi_status", "/usr/bin/network_diag --wifi-internal --no-log" },
Sonny Raoab5f9952013-07-17 14:13:53 -0700187 { "zram compressed data size",
188 "/bin/cat /sys/block/zram0/compr_data_size 2> /dev/null" },
189 { "zram original data size",
190 "/bin/cat /sys/block/zram0/orig_data_size 2> /dev/null" },
191 { "zram total memory used",
192 "/bin/cat /sys/block/zram0/mem_used_total 2> /dev/null" },
193 { "zram total reads",
194 "/bin/cat /sys/block/zram0/num_reads 2> /dev/null" },
195 { "zram total writes",
196 "/bin/cat /sys/block/zram0/num_writes 2> /dev/null" },
Gaurav Shah6e8fd892012-10-01 11:51:08 -0700197
Elly Jones03cd6d72012-06-11 13:04:28 -0400198 // Stuff pulled out of the original list. These need access to the running X
199 // session, which we'd rather not give to debugd, or return info specific to
200 // the current session (in the setsid(2) sense), which is not useful for
201 // debugd
202 // { "env", "set" },
203 // { "setxkbmap", "/usr/bin/setxkbmap -print -query" },
204 // { "xrandr", "/usr/bin/xrandr --verbose" }
Elly Jones533c7c42012-08-10 15:07:05 -0400205 { NULL, NULL }
206};
207
208static const Log extra_logs[] = {
Ben Chan36e42282014-02-12 22:32:34 -0800209#if USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400210 { "mm-status", "/usr/bin/modem status" },
Ben Chan36e42282014-02-12 22:32:34 -0800211#endif // USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400212 { "network-devices", "/usr/bin/connectivity show devices" },
213 { "network-services", "/usr/bin/connectivity show services" },
214 { NULL, NULL }
215};
216
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700217static const Log feedback_logs[] = {
Ben Chan36e42282014-02-12 22:32:34 -0800218#if USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400219 { "mm-status", "/usr/bin/modem status-feedback" },
Ben Chan36e42282014-02-12 22:32:34 -0800220#endif // USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400221 { "network-devices", "/usr/bin/connectivity show-feedback devices" },
222 { "network-services", "/usr/bin/connectivity show-feedback services" },
223 { NULL, NULL }
Elly Jones03cd6d72012-06-11 13:04:28 -0400224};
225
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700226// List of log files that must directly be collected by Chrome. This is because
227// debugd is running under a VFS namespace and does not have access to later
228// cryptohome mounts.
229static const Log user_logs[] = {
Elly Fong-Jones6456ce52013-04-17 13:31:13 -0400230 {"chrome_user_log", "log/chrome"},
231 {"login-times", "login-times"},
232 {"logout-times", "logout-times"},
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700233 { NULL, NULL}
234};
235
Elly Jones533c7c42012-08-10 15:07:05 -0400236bool GetNamedLogFrom(const string& name, const struct Log* logs,
237 string* result) {
238 for (size_t i = 0; logs[i].name; i++) {
239 if (name == logs[i].name) {
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -0500240 *result = Run(logs[i]);
Elly Jones533c7c42012-08-10 15:07:05 -0400241 return true;
242 }
243 }
244 *result = "<invalid log name>";
245 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400246}
247
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700248void LogTool::CreateConnectivityReport(DBus::Connection* connection) {
249 // Perform ConnectivityTrial to report connection state in feedback log.
250 ManagerProxy shill(connection,
251 shill::kFlimflamServicePath,
252 shill::kFlimflamServiceName);
253 shill.CreateConnectivityReport();
254 // Give the connection trial time to test the connection and log the results
255 // before collecting the logs for feedback.
256 // TODO(silberst): Replace the simple approach of a single timeout with a more
257 // coordinated effort.
258 sleep(kConnectionTesterTimeoutSeconds);
259}
260
Ben Chana0011d82014-05-13 00:19:29 -0700261string LogTool::GetLog(const string& name, DBus::Error* error) {
Elly Jones533c7c42012-08-10 15:07:05 -0400262 string result;
263 GetNamedLogFrom(name, common_logs, &result)
264 || GetNamedLogFrom(name, extra_logs, &result)
265 || GetNamedLogFrom(name, feedback_logs, &result);
266 return result;
267}
268
269void GetLogsFrom(const struct Log* logs, LogTool::LogMap* map) {
270 for (size_t i = 0; logs[i].name; i++)
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -0500271 (*map)[logs[i].name] = Run(logs[i]);
Elly Jones533c7c42012-08-10 15:07:05 -0400272}
273
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700274LogTool::LogMap LogTool::GetAllLogs(DBus::Connection* connection,
275 DBus::Error* error) {
276 CreateConnectivityReport(connection);
Elly Jones533c7c42012-08-10 15:07:05 -0400277 LogMap result;
278 GetLogsFrom(common_logs, &result);
279 GetLogsFrom(extra_logs, &result);
280 return result;
281}
282
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700283LogTool::LogMap LogTool::GetFeedbackLogs(DBus::Connection* connection,
284 DBus::Error* error) {
285 CreateConnectivityReport(connection);
Elly Jones533c7c42012-08-10 15:07:05 -0400286 LogMap result;
287 GetLogsFrom(common_logs, &result);
288 GetLogsFrom(feedback_logs, &result);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100289 AnonymizeLogMap(&result);
Elly Jones03cd6d72012-06-11 13:04:28 -0400290 return result;
291}
292
Ben Chana0011d82014-05-13 00:19:29 -0700293LogTool::LogMap LogTool::GetUserLogFiles(DBus::Error* error) {
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700294 LogMap result;
295 for (size_t i = 0; user_logs[i].name; ++i)
296 result[user_logs[i].name] = user_logs[i].command;
297 return result;
298}
299
Darin Petkovce9b3a12013-01-10 16:38:54 +0100300void LogTool::AnonymizeLogMap(LogMap* log_map) {
301 AnonymizerTool anonymizer;
302 for (LogMap::iterator it = log_map->begin();
303 it != log_map->end(); ++it) {
304 it->second = anonymizer.Anonymize(it->second);
305 }
306}
307
Ben Chana0011d82014-05-13 00:19:29 -0700308} // namespace debugd