blob: 2b3a6e100880c57a4fc30757ab43c26889d63094 [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>
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080012#include <base/json/json_writer.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>
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080016#include <base/values.h>
Elly Jones03cd6d72012-06-11 13:04:28 -040017
Rebecca Silbersteine78af402014-10-02 10:55:04 -070018#include <chromeos/dbus/service_constants.h>
Peter Qiued45fda2015-09-09 16:52:50 -070019#include <shill/dbus_proxies/org.chromium.flimflam.Manager.h>
Rebecca Silbersteine78af402014-10-02 10:55:04 -070020
Alex Vakulenko262be3f2014-07-30 15:25:50 -070021#include "debugd/src/process_with_output.h"
Elly Jones03cd6d72012-06-11 13:04:28 -040022
23namespace debugd {
24
Elly Jones03cd6d72012-06-11 13:04:28 -040025using std::string;
26using std::vector;
27
28typedef vector<string> Strings;
29
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080030const char *kDebugfsGroup = "debugfs-access";
Ben Chanf6cd93a2012-10-14 19:37:00 -070031
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080032namespace {
33
34const char *kShell = "/bin/sh";
35
36// Minimum time in seconds needed to allow shill to test active connections.
37const int kConnectionTesterTimeoutSeconds = 5;
Ben Chanf6cd93a2012-10-14 19:37:00 -070038
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050039struct Log {
40 const char *name;
41 const char *command;
42 const char *user;
43 const char *group;
44};
45
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080046const Log common_logs[] = {
Elly Jones03cd6d72012-06-11 13:04:28 -040047 { "CLIENT_ID", "/bin/cat '/home/chronos/Consent To Send Stats'" },
48 { "LOGDATE", "/bin/date" },
49 { "Xorg.0.log", "/bin/cat /var/log/Xorg.0.log" },
Elly Jones03cd6d72012-06-11 13:04:28 -040050 { "bios_info", "/bin/cat /var/log/bios_info.txt" },
Vadim Bendebury64aeeed2013-09-16 13:16:49 -070051 { "bios_log",
52 "/bin/cat /sys/firmware/log "
53 "/proc/device-tree/chosen/ap-console-buffer 2> /dev/null" },
Stefan Reinauer4393fa72012-10-18 11:08:09 -070054 { "bios_times", "/bin/cat /var/log/bios_times.txt" },
Elly Jones03cd6d72012-06-11 13:04:28 -040055 { "board-specific",
Thiemo Nagela269ad22014-11-27 17:13:01 +010056 "/usr/share/userfeedback/scripts/get_board_specific_info" },
Olof Johanssone5d0fd32016-01-29 14:40:34 -080057 { "cheets_log", "/usr/bin/collect-cheets-logs 2>&1" },
Thiemo Nagela269ad22014-11-27 17:13:01 +010058 { "clobber.log", "/bin/cat /var/log/clobber.log 2> /dev/null" },
59 { "clobber-state.log", "/bin/cat /var/log/clobber-state.log 2> /dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -040060 { "chrome_system_log", "/bin/cat /var/log/chrome/chrome" },
Stefan Reinauer48f883d2014-08-22 14:31:58 -070061 { "console-ramoops", "/bin/cat /dev/pstore/console-ramoops 2> /dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -040062 { "cpu", "/usr/bin/uname -p" },
63 { "cpuinfo", "/bin/cat /proc/cpuinfo" },
Elly Jones03cd6d72012-06-11 13:04:28 -040064 { "dmesg", "/bin/dmesg" },
65 { "ec_info", "/bin/cat /var/log/ec_info.txt" },
Stefan Reinauer4393fa72012-10-18 11:08:09 -070066 { "eventlog", "/bin/cat /var/log/eventlog.txt" },
Elly Fong-Jones215b5622013-03-20 14:32:18 -040067 {
68 "exynos_gem_objects",
Yuly Novikov13ece852013-07-11 17:19:10 -040069 "/bin/cat /sys/kernel/debug/dri/0/exynos_gem_objects 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -040070 SandboxedProcess::kDefaultUser,
71 kDebugfsGroup
72 },
Elly Jones03cd6d72012-06-11 13:04:28 -040073 { "font_info", "/usr/share/userfeedback/scripts/font_info" },
74 { "hardware_class", "/usr/bin/crossystem hwid" },
75 { "hostname", "/bin/hostname" },
76 { "hw_platform", "/usr/bin/uname -i" },
Elly Fong-Jones215b5622013-03-20 14:32:18 -040077 {
78 "i915_gem_gtt",
Yuly Novikov13ece852013-07-11 17:19:10 -040079 "/bin/cat /sys/kernel/debug/dri/0/i915_gem_gtt 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -040080 SandboxedProcess::kDefaultUser,
81 kDebugfsGroup
82 },
83 {
84 "i915_gem_objects",
Yuly Novikov13ece852013-07-11 17:19:10 -040085 "/bin/cat /sys/kernel/debug/dri/0/i915_gem_objects 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -040086 SandboxedProcess::kDefaultUser,
87 kDebugfsGroup
88 },
89 {
90 "i915_error_state",
Yuly Novikov4ac12fb2013-07-18 20:08:44 -040091 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -040092 SandboxedProcess::kDefaultUser,
93 kDebugfsGroup,
94 },
Daniel Erat982ab4b2014-04-09 07:32:38 -070095 { "ifconfig", "/bin/ifconfig -a" },
Stefan Reinauer48f883d2014-08-22 14:31:58 -070096 { "kernel-crashes",
97 "/bin/cat /var/spool/crash/kernel.*.kcrash 2> /dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -040098 { "lsmod", "lsmod" },
99 { "lspci", "/usr/sbin/lspci" },
100 { "lsusb", "lsusb" },
Yuly Novikov13ece852013-07-11 17:19:10 -0400101 {
Thiemo Nagela269ad22014-11-27 17:13:01 +0100102 "mali_memory",
103 "/bin/cat /sys/class/misc/mali0/device/memory 2> /dev/null"
Yuly Novikov13ece852013-07-11 17:19:10 -0400104 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400105 { "meminfo", "cat /proc/meminfo" },
106 { "memory_spd_info", "/bin/cat /var/log/memory_spd_info.txt" },
Kees Cooka8f3e7e2012-11-19 14:16:01 -0800107 { "mount-encrypted", "/bin/cat /var/log/mount-encrypted.log" },
Darin Petkov54743582012-12-10 14:18:32 +0100108 { "net-diags.net.log", "/bin/cat /var/log/net-diags.net.log" },
Wade Guthrie67b672e2012-11-14 13:14:31 -0800109 { "netlog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'"
110 " /var/log/net.log" },
henryhsufa6daa12014-09-24 13:26:30 +0800111 {
112 "nvmap_iovmm",
113 "/bin/cat /sys/kernel/debug/nvmap/iovmm/allocations 2> /dev/null",
114 SandboxedProcess::kDefaultUser,
115 kDebugfsGroup,
116 },
Vincent Palatinc64af9d2013-08-05 16:34:10 -0700117 { "platform_info", "/bin/cat /var/log/platform_info.txt" },
Daniel Erat9b58b6d2013-04-30 14:58:56 -0700118 { "power_supply_info", "/usr/bin/power_supply_info" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400119 { "powerd.LATEST", "/bin/cat /var/log/power_manager/powerd.LATEST" },
Daniel Erat546c3882013-11-22 12:46:01 -0800120 { "powerd.PREVIOUS", "/bin/cat /var/log/power_manager/powerd.PREVIOUS" },
Daniel Erat93da9a12012-12-06 14:14:17 -0800121 { "powerd.out", "/bin/cat /var/log/powerd.out" },
Thiemo Nagela269ad22014-11-27 17:13:01 +0100122 { "powerwash_count", "/bin/cat /var/log/powerwash_count 2> /dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400123 // Changed from 'ps ux' to 'ps aux' since we're running as debugd, not chronos
124 { "ps", "/bin/ps aux" },
Puthikorn Voravootivat6de5e122013-12-06 11:43:13 -0800125 { "storage_info", "/bin/cat /var/log/storage_info.txt" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400126 { "syslog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'"
Elly Jonesf8a67ee2012-06-19 13:48:38 -0400127 " /var/log/messages" },
Elly Fong-Jonesb4f49c42013-02-28 13:45:26 -0500128 { "tlsdate", "/bin/cat /var/log/tlsdate.log" },
Michael Spang387567d2015-03-11 17:16:04 -0400129 { "input_devices", "/bin/cat /proc/bus/input/devices" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400130 { "touchpad", "/opt/google/touchpad/tpcontrol status" },
Chung-yih Wang4b876142014-04-30 17:01:49 +0800131 { "touchpad_activity", "/opt/google/input/cmt_feedback alt" },
Andrew de los Reyesc060c342013-04-10 13:46:30 -0700132 { "touch_fw_version", "grep -E"
Charlie Mooneybb08f982016-02-04 15:35:56 -0800133 " -e 'synaptics: Touchpad model'"
134 " -e 'chromeos-[a-z]*-touch-[a-z]*-update'"
Andrew de los Reyesc060c342013-04-10 13:46:30 -0700135 " /var/log/messages | tail -n 20" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400136 { "ui_log", "/usr/share/userfeedback/scripts/get_log /var/log/ui/ui.LATEST" },
137 { "uname", "/bin/uname -a" },
138 { "update_engine.log", "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
Thiemo Nagela269ad22014-11-27 17:13:01 +0100139 " s.^./var/log/update_engine/.)" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400140 { "verified boot", "/bin/cat /var/log/debug_vboot_noisy.log" },
141 { "vpd_2.0", "/bin/cat /var/log/vpd_2.0.txt" },
Samuel Tan107b6c82014-07-10 15:33:44 -0700142 { "wifi_status", "/usr/bin/network_diag --wifi-internal --no-log" },
Sonny Raoab5f9952013-07-17 14:13:53 -0700143 { "zram compressed data size",
144 "/bin/cat /sys/block/zram0/compr_data_size 2> /dev/null" },
145 { "zram original data size",
146 "/bin/cat /sys/block/zram0/orig_data_size 2> /dev/null" },
147 { "zram total memory used",
148 "/bin/cat /sys/block/zram0/mem_used_total 2> /dev/null" },
149 { "zram total reads",
150 "/bin/cat /sys/block/zram0/num_reads 2> /dev/null" },
151 { "zram total writes",
152 "/bin/cat /sys/block/zram0/num_writes 2> /dev/null" },
Gaurav Shah6e8fd892012-10-01 11:51:08 -0700153
Elly Jones03cd6d72012-06-11 13:04:28 -0400154 // Stuff pulled out of the original list. These need access to the running X
155 // session, which we'd rather not give to debugd, or return info specific to
156 // the current session (in the setsid(2) sense), which is not useful for
157 // debugd
158 // { "env", "set" },
159 // { "setxkbmap", "/usr/bin/setxkbmap -print -query" },
160 // { "xrandr", "/usr/bin/xrandr --verbose" }
Elly Jones533c7c42012-08-10 15:07:05 -0400161 { NULL, NULL }
162};
163
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800164const Log extra_logs[] = {
Ben Chan36e42282014-02-12 22:32:34 -0800165#if USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400166 { "mm-status", "/usr/bin/modem status" },
Ben Chan36e42282014-02-12 22:32:34 -0800167#endif // USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400168 { "network-devices", "/usr/bin/connectivity show devices" },
169 { "network-services", "/usr/bin/connectivity show services" },
170 { NULL, NULL }
171};
172
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800173const Log feedback_logs[] = {
Ben Chan36e42282014-02-12 22:32:34 -0800174#if USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400175 { "mm-status", "/usr/bin/modem status-feedback" },
Ben Chan36e42282014-02-12 22:32:34 -0800176#endif // USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400177 { "network-devices", "/usr/bin/connectivity show-feedback devices" },
178 { "network-services", "/usr/bin/connectivity show-feedback services" },
179 { NULL, NULL }
Elly Jones03cd6d72012-06-11 13:04:28 -0400180};
181
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700182// List of log files needed to be part of the feedback report that are huge and
183// must be sent back to the client via the file descriptor using
184// LogTool::GetBigFeedbackLogs().
185const Log big_feedback_logs[] = {
186 // TODO(olofj): Add the huge cheets logs here.
187 { NULL, NULL }
188};
189
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700190// List of log files that must directly be collected by Chrome. This is because
191// debugd is running under a VFS namespace and does not have access to later
192// cryptohome mounts.
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800193const Log user_logs[] = {
Elly Fong-Jones6456ce52013-04-17 13:31:13 -0400194 {"chrome_user_log", "log/chrome"},
195 {"login-times", "login-times"},
196 {"logout-times", "logout-times"},
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700197 { NULL, NULL}
198};
199
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800200class ManagerProxy : public org::chromium::flimflam::Manager_proxy,
201 public DBus::ObjectProxy {
202 public:
203 ManagerProxy(DBus::Connection* connection,
204 const char* path,
205 const char* service)
206 : DBus::ObjectProxy(*connection, path, service) {}
207 ~ManagerProxy() override = default;
208 void PropertyChanged(const std::string&, const DBus::Variant&) override {}
209 void StateChanged(const std::string&) override {}
210};
211
212// Returns |value| if |value| is a valid UTF-8 string or a base64-encoded
213// string of |value| otherwise.
214string EnsureUTF8String(const string& value) {
215 if (base::IsStringUTF8(value))
216 return value;
217
218 gchar* base64_value = g_base64_encode(
219 reinterpret_cast<const guchar*>(value.c_str()), value.length());
220 if (base64_value) {
221 string encoded_value = "<base64>: ";
222 encoded_value += base64_value;
223 g_free(base64_value);
224 return encoded_value;
225 }
226 return "<invalid>";
227}
228
229// TODO(ellyjones): sandbox. crosbug.com/35122
230string Run(const Log& log) {
231 string output;
232 ProcessWithOutput p;
233 string tailed_cmdline = std::string(log.command) + " | tail -c 256K";
234 if (log.user && log.group)
235 p.SandboxAs(log.user, log.group);
236 if (!p.Init())
237 return "<not available>";
238 p.AddArg(kShell);
239 p.AddStringOption("-c", tailed_cmdline);
240 if (p.Run())
241 return "<not available>";
242 p.GetOutput(&output);
243 if (!output.size())
244 return "<empty>";
245 return EnsureUTF8String(output);
246}
247
248// Fills |dictionary| with the anonymized contents of the logs in |logs|.
249void GetLogsInDictionary(const struct Log* logs,
250 AnonymizerTool* anonymizer,
251 base::DictionaryValue* dictionary) {
252 for (size_t i = 0; logs[i].name; ++i) {
253 dictionary->SetStringWithoutPathExpansion(
254 logs[i].name, anonymizer->Anonymize(Run(logs[i])));
255 }
256}
257
258// Serializes the |dictionary| into the file with the given |fd| in a JSON
259// format.
260void SerializeLogsAsJSON(const base::DictionaryValue& dictionary,
261 const DBus::FileDescriptor& fd) {
262 std::string logs_json;
263 base::JSONWriter::WriteWithOptions(dictionary,
264 base::JSONWriter::OPTIONS_PRETTY_PRINT,
265 &logs_json);
266 base::WriteFileDescriptor(fd.get(), logs_json.c_str(), logs_json.size());
267}
268
Elly Jones533c7c42012-08-10 15:07:05 -0400269bool GetNamedLogFrom(const string& name, const struct Log* logs,
270 string* result) {
271 for (size_t i = 0; logs[i].name; i++) {
272 if (name == logs[i].name) {
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -0500273 *result = Run(logs[i]);
Elly Jones533c7c42012-08-10 15:07:05 -0400274 return true;
275 }
276 }
277 *result = "<invalid log name>";
278 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400279}
280
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800281void GetLogsFrom(const struct Log* logs, LogTool::LogMap* map) {
282 for (size_t i = 0; logs[i].name; i++)
283 (*map)[logs[i].name] = Run(logs[i]);
284}
285
286} // namespace
287
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700288void LogTool::CreateConnectivityReport(DBus::Connection* connection) {
289 // Perform ConnectivityTrial to report connection state in feedback log.
290 ManagerProxy shill(connection,
291 shill::kFlimflamServicePath,
292 shill::kFlimflamServiceName);
293 shill.CreateConnectivityReport();
294 // Give the connection trial time to test the connection and log the results
295 // before collecting the logs for feedback.
296 // TODO(silberst): Replace the simple approach of a single timeout with a more
297 // coordinated effort.
298 sleep(kConnectionTesterTimeoutSeconds);
299}
300
Ben Chana0011d82014-05-13 00:19:29 -0700301string LogTool::GetLog(const string& name, DBus::Error* error) {
Elly Jones533c7c42012-08-10 15:07:05 -0400302 string result;
303 GetNamedLogFrom(name, common_logs, &result)
304 || GetNamedLogFrom(name, extra_logs, &result)
305 || GetNamedLogFrom(name, feedback_logs, &result);
306 return result;
307}
308
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700309LogTool::LogMap LogTool::GetAllLogs(DBus::Connection* connection,
310 DBus::Error* error) {
311 CreateConnectivityReport(connection);
Elly Jones533c7c42012-08-10 15:07:05 -0400312 LogMap result;
313 GetLogsFrom(common_logs, &result);
314 GetLogsFrom(extra_logs, &result);
315 return result;
316}
317
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700318LogTool::LogMap LogTool::GetFeedbackLogs(DBus::Connection* connection,
319 DBus::Error* error) {
320 CreateConnectivityReport(connection);
Elly Jones533c7c42012-08-10 15:07:05 -0400321 LogMap result;
322 GetLogsFrom(common_logs, &result);
323 GetLogsFrom(feedback_logs, &result);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100324 AnonymizeLogMap(&result);
Elly Jones03cd6d72012-06-11 13:04:28 -0400325 return result;
326}
327
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800328void LogTool::GetBigFeedbackLogs(DBus::Connection* connection,
329 const DBus::FileDescriptor& fd,
330 DBus::Error* error) {
331 CreateConnectivityReport(connection);
332 base::DictionaryValue dictionary;
333 GetLogsInDictionary(common_logs, &anonymizer_, &dictionary);
334 GetLogsInDictionary(feedback_logs, &anonymizer_, &dictionary);
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700335 GetLogsInDictionary(big_feedback_logs, &anonymizer_, &dictionary);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800336 SerializeLogsAsJSON(dictionary, fd);
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700337
338 // We need to manually close the FD here to enable the client to read the
339 // contents via a pipe.
340 close(fd.get());
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800341}
342
Ben Chana0011d82014-05-13 00:19:29 -0700343LogTool::LogMap LogTool::GetUserLogFiles(DBus::Error* error) {
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700344 LogMap result;
345 for (size_t i = 0; user_logs[i].name; ++i)
346 result[user_logs[i].name] = user_logs[i].command;
347 return result;
348}
349
Darin Petkovce9b3a12013-01-10 16:38:54 +0100350void LogTool::AnonymizeLogMap(LogMap* log_map) {
Darin Petkovce9b3a12013-01-10 16:38:54 +0100351 for (LogMap::iterator it = log_map->begin();
352 it != log_map->end(); ++it) {
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800353 it->second = anonymizer_.Anonymize(it->second);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100354 }
355}
356
Ben Chana0011d82014-05-13 00:19:29 -0700357} // namespace debugd