blob: f03d54ee55575534d145dd1837bbc3fd988baca7 [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";
Eric Carusoe3166f22016-06-10 15:36:02 -070031const char *kRoot = "root";
Ben Chanf6cd93a2012-10-14 19:37:00 -070032
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080033namespace {
34
35const char *kShell = "/bin/sh";
36
37// Minimum time in seconds needed to allow shill to test active connections.
38const int kConnectionTesterTimeoutSeconds = 5;
Ben Chanf6cd93a2012-10-14 19:37:00 -070039
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050040struct Log {
41 const char *name;
42 const char *command;
43 const char *user;
44 const char *group;
Ricky Zhou4c473ca2016-06-21 17:46:58 -070045 const char *size_cap; // passed as arg to 'tail'
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050046};
47
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080048const Log common_logs[] = {
Elly Jones03cd6d72012-06-11 13:04:28 -040049 { "CLIENT_ID", "/bin/cat '/home/chronos/Consent To Send Stats'" },
50 { "LOGDATE", "/bin/date" },
51 { "Xorg.0.log", "/bin/cat /var/log/Xorg.0.log" },
Elly Jones03cd6d72012-06-11 13:04:28 -040052 { "bios_info", "/bin/cat /var/log/bios_info.txt" },
Vadim Bendebury64aeeed2013-09-16 13:16:49 -070053 { "bios_log",
54 "/bin/cat /sys/firmware/log "
55 "/proc/device-tree/chosen/ap-console-buffer 2> /dev/null" },
Stefan Reinauer4393fa72012-10-18 11:08:09 -070056 { "bios_times", "/bin/cat /var/log/bios_times.txt" },
Elly Jones03cd6d72012-06-11 13:04:28 -040057 { "board-specific",
Thiemo Nagela269ad22014-11-27 17:13:01 +010058 "/usr/share/userfeedback/scripts/get_board_specific_info" },
Olof Johanssone5d0fd32016-01-29 14:40:34 -080059 { "cheets_log", "/usr/bin/collect-cheets-logs 2>&1" },
Thiemo Nagela269ad22014-11-27 17:13:01 +010060 { "clobber.log", "/bin/cat /var/log/clobber.log 2> /dev/null" },
61 { "clobber-state.log", "/bin/cat /var/log/clobber-state.log 2> /dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -040062 { "chrome_system_log", "/bin/cat /var/log/chrome/chrome" },
Stefan Reinauer48f883d2014-08-22 14:31:58 -070063 { "console-ramoops", "/bin/cat /dev/pstore/console-ramoops 2> /dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -040064 { "cpu", "/usr/bin/uname -p" },
65 { "cpuinfo", "/bin/cat /proc/cpuinfo" },
Eric Caruso57333f12015-09-08 12:50:32 -070066 { "cros_ec",
67 "/bin/cat /var/log/cros_ec.previous /var/log/cros_ec.log 2> /dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -040068 { "dmesg", "/bin/dmesg" },
69 { "ec_info", "/bin/cat /var/log/ec_info.txt" },
Stefan Reinauer4393fa72012-10-18 11:08:09 -070070 { "eventlog", "/bin/cat /var/log/eventlog.txt" },
Elly Fong-Jones215b5622013-03-20 14:32:18 -040071 {
72 "exynos_gem_objects",
Yuly Novikov13ece852013-07-11 17:19:10 -040073 "/bin/cat /sys/kernel/debug/dri/0/exynos_gem_objects 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -040074 SandboxedProcess::kDefaultUser,
75 kDebugfsGroup
76 },
Elly Jones03cd6d72012-06-11 13:04:28 -040077 { "font_info", "/usr/share/userfeedback/scripts/font_info" },
78 { "hardware_class", "/usr/bin/crossystem hwid" },
79 { "hostname", "/bin/hostname" },
80 { "hw_platform", "/usr/bin/uname -i" },
Elly Fong-Jones215b5622013-03-20 14:32:18 -040081 {
82 "i915_gem_gtt",
Yuly Novikov13ece852013-07-11 17:19:10 -040083 "/bin/cat /sys/kernel/debug/dri/0/i915_gem_gtt 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -040084 SandboxedProcess::kDefaultUser,
85 kDebugfsGroup
86 },
87 {
88 "i915_gem_objects",
Yuly Novikov13ece852013-07-11 17:19:10 -040089 "/bin/cat /sys/kernel/debug/dri/0/i915_gem_objects 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -040090 SandboxedProcess::kDefaultUser,
91 kDebugfsGroup
92 },
93 {
94 "i915_error_state",
Yuly Novikov4ac12fb2013-07-18 20:08:44 -040095 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -040096 SandboxedProcess::kDefaultUser,
97 kDebugfsGroup,
98 },
Daniel Erat982ab4b2014-04-09 07:32:38 -070099 { "ifconfig", "/bin/ifconfig -a" },
Stefan Reinauer48f883d2014-08-22 14:31:58 -0700100 { "kernel-crashes",
101 "/bin/cat /var/spool/crash/kernel.*.kcrash 2> /dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400102 { "lsmod", "lsmod" },
103 { "lspci", "/usr/sbin/lspci" },
104 { "lsusb", "lsusb" },
Yuly Novikov13ece852013-07-11 17:19:10 -0400105 {
Thiemo Nagela269ad22014-11-27 17:13:01 +0100106 "mali_memory",
107 "/bin/cat /sys/class/misc/mali0/device/memory 2> /dev/null"
Yuly Novikov13ece852013-07-11 17:19:10 -0400108 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400109 { "meminfo", "cat /proc/meminfo" },
110 { "memory_spd_info", "/bin/cat /var/log/memory_spd_info.txt" },
Kees Cooka8f3e7e2012-11-19 14:16:01 -0800111 { "mount-encrypted", "/bin/cat /var/log/mount-encrypted.log" },
Darin Petkov54743582012-12-10 14:18:32 +0100112 { "net-diags.net.log", "/bin/cat /var/log/net-diags.net.log" },
Wade Guthrie67b672e2012-11-14 13:14:31 -0800113 { "netlog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'"
114 " /var/log/net.log" },
henryhsufa6daa12014-09-24 13:26:30 +0800115 {
116 "nvmap_iovmm",
117 "/bin/cat /sys/kernel/debug/nvmap/iovmm/allocations 2> /dev/null",
118 SandboxedProcess::kDefaultUser,
119 kDebugfsGroup,
120 },
Vincent Palatinc64af9d2013-08-05 16:34:10 -0700121 { "platform_info", "/bin/cat /var/log/platform_info.txt" },
Daniel Erat9b58b6d2013-04-30 14:58:56 -0700122 { "power_supply_info", "/usr/bin/power_supply_info" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400123 { "powerd.LATEST", "/bin/cat /var/log/power_manager/powerd.LATEST" },
Daniel Erat546c3882013-11-22 12:46:01 -0800124 { "powerd.PREVIOUS", "/bin/cat /var/log/power_manager/powerd.PREVIOUS" },
Daniel Erat93da9a12012-12-06 14:14:17 -0800125 { "powerd.out", "/bin/cat /var/log/powerd.out" },
Thiemo Nagela269ad22014-11-27 17:13:01 +0100126 { "powerwash_count", "/bin/cat /var/log/powerwash_count 2> /dev/null" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400127 // Changed from 'ps ux' to 'ps aux' since we're running as debugd, not chronos
128 { "ps", "/bin/ps aux" },
Puthikorn Voravootivat6de5e122013-12-06 11:43:13 -0800129 { "storage_info", "/bin/cat /var/log/storage_info.txt" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400130 { "syslog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'"
Elly Jonesf8a67ee2012-06-19 13:48:38 -0400131 " /var/log/messages" },
Elly Fong-Jonesb4f49c42013-02-28 13:45:26 -0500132 { "tlsdate", "/bin/cat /var/log/tlsdate.log" },
Michael Spang387567d2015-03-11 17:16:04 -0400133 { "input_devices", "/bin/cat /proc/bus/input/devices" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400134 { "touchpad", "/opt/google/touchpad/tpcontrol status" },
Chung-yih Wang4b876142014-04-30 17:01:49 +0800135 { "touchpad_activity", "/opt/google/input/cmt_feedback alt" },
Andrew de los Reyesc060c342013-04-10 13:46:30 -0700136 { "touch_fw_version", "grep -E"
Charlie Mooneybb08f982016-02-04 15:35:56 -0800137 " -e 'synaptics: Touchpad model'"
138 " -e 'chromeos-[a-z]*-touch-[a-z]*-update'"
Andrew de los Reyesc060c342013-04-10 13:46:30 -0700139 " /var/log/messages | tail -n 20" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400140 { "ui_log", "/usr/share/userfeedback/scripts/get_log /var/log/ui/ui.LATEST" },
141 { "uname", "/bin/uname -a" },
142 { "update_engine.log", "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
Thiemo Nagela269ad22014-11-27 17:13:01 +0100143 " s.^./var/log/update_engine/.)" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400144 { "verified boot", "/bin/cat /var/log/debug_vboot_noisy.log" },
145 { "vpd_2.0", "/bin/cat /var/log/vpd_2.0.txt" },
Samuel Tan107b6c82014-07-10 15:33:44 -0700146 { "wifi_status", "/usr/bin/network_diag --wifi-internal --no-log" },
Sonny Raoab5f9952013-07-17 14:13:53 -0700147 { "zram compressed data size",
148 "/bin/cat /sys/block/zram0/compr_data_size 2> /dev/null" },
149 { "zram original data size",
150 "/bin/cat /sys/block/zram0/orig_data_size 2> /dev/null" },
151 { "zram total memory used",
152 "/bin/cat /sys/block/zram0/mem_used_total 2> /dev/null" },
153 { "zram total reads",
154 "/bin/cat /sys/block/zram0/num_reads 2> /dev/null" },
155 { "zram total writes",
156 "/bin/cat /sys/block/zram0/num_writes 2> /dev/null" },
Gaurav Shah6e8fd892012-10-01 11:51:08 -0700157
Elly Jones03cd6d72012-06-11 13:04:28 -0400158 // Stuff pulled out of the original list. These need access to the running X
159 // session, which we'd rather not give to debugd, or return info specific to
160 // the current session (in the setsid(2) sense), which is not useful for
161 // debugd
162 // { "env", "set" },
163 // { "setxkbmap", "/usr/bin/setxkbmap -print -query" },
164 // { "xrandr", "/usr/bin/xrandr --verbose" }
Elly Jones533c7c42012-08-10 15:07:05 -0400165 { NULL, NULL }
166};
167
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800168const Log extra_logs[] = {
Ben Chan36e42282014-02-12 22:32:34 -0800169#if USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400170 { "mm-status", "/usr/bin/modem status" },
Ben Chan36e42282014-02-12 22:32:34 -0800171#endif // USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400172 { "network-devices", "/usr/bin/connectivity show devices" },
173 { "network-services", "/usr/bin/connectivity show services" },
174 { NULL, NULL }
175};
176
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800177const Log feedback_logs[] = {
Ben Chan36e42282014-02-12 22:32:34 -0800178#if USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400179 { "mm-status", "/usr/bin/modem status-feedback" },
Ben Chan36e42282014-02-12 22:32:34 -0800180#endif // USE_CELLULAR
Elly Jones533c7c42012-08-10 15:07:05 -0400181 { "network-devices", "/usr/bin/connectivity show-feedback devices" },
182 { "network-services", "/usr/bin/connectivity show-feedback services" },
183 { NULL, NULL }
Elly Jones03cd6d72012-06-11 13:04:28 -0400184};
185
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700186// List of log files needed to be part of the feedback report that are huge and
187// must be sent back to the client via the file descriptor using
188// LogTool::GetBigFeedbackLogs().
189const Log big_feedback_logs[] = {
Eric Carusoe3166f22016-06-10 15:36:02 -0700190 { "arc-bugreport",
191 "/bin/cat /var/run/arc/bugreport/pipe 2> /dev/null",
192 // ARC bugreport permissions are weird. Since we're just running cat, this
193 // shouldn't cause any issues.
194 kRoot,
Ricky Zhou4c473ca2016-06-21 17:46:58 -0700195 kRoot,
196 "10M",
Eric Carusoe3166f22016-06-10 15:36:02 -0700197 },
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700198 { NULL, NULL }
199};
200
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700201// List of log files that must directly be collected by Chrome. This is because
202// debugd is running under a VFS namespace and does not have access to later
203// cryptohome mounts.
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800204const Log user_logs[] = {
Elly Fong-Jones6456ce52013-04-17 13:31:13 -0400205 {"chrome_user_log", "log/chrome"},
206 {"login-times", "login-times"},
207 {"logout-times", "logout-times"},
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700208 { NULL, NULL}
209};
210
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800211class ManagerProxy : public org::chromium::flimflam::Manager_proxy,
212 public DBus::ObjectProxy {
213 public:
214 ManagerProxy(DBus::Connection* connection,
215 const char* path,
216 const char* service)
217 : DBus::ObjectProxy(*connection, path, service) {}
218 ~ManagerProxy() override = default;
219 void PropertyChanged(const std::string&, const DBus::Variant&) override {}
220 void StateChanged(const std::string&) override {}
221};
222
223// Returns |value| if |value| is a valid UTF-8 string or a base64-encoded
224// string of |value| otherwise.
225string EnsureUTF8String(const string& value) {
226 if (base::IsStringUTF8(value))
227 return value;
228
229 gchar* base64_value = g_base64_encode(
230 reinterpret_cast<const guchar*>(value.c_str()), value.length());
231 if (base64_value) {
232 string encoded_value = "<base64>: ";
233 encoded_value += base64_value;
234 g_free(base64_value);
235 return encoded_value;
236 }
237 return "<invalid>";
238}
239
240// TODO(ellyjones): sandbox. crosbug.com/35122
241string Run(const Log& log) {
242 string output;
243 ProcessWithOutput p;
Ricky Zhou4c473ca2016-06-21 17:46:58 -0700244 string tailed_cmdline = std::string(log.command) + " | tail -c " +
245 (log.size_cap ? log.size_cap : "256K");
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800246 if (log.user && log.group)
247 p.SandboxAs(log.user, log.group);
248 if (!p.Init())
249 return "<not available>";
250 p.AddArg(kShell);
251 p.AddStringOption("-c", tailed_cmdline);
252 if (p.Run())
253 return "<not available>";
254 p.GetOutput(&output);
255 if (!output.size())
256 return "<empty>";
257 return EnsureUTF8String(output);
258}
259
260// Fills |dictionary| with the anonymized contents of the logs in |logs|.
261void GetLogsInDictionary(const struct Log* logs,
262 AnonymizerTool* anonymizer,
263 base::DictionaryValue* dictionary) {
264 for (size_t i = 0; logs[i].name; ++i) {
265 dictionary->SetStringWithoutPathExpansion(
266 logs[i].name, anonymizer->Anonymize(Run(logs[i])));
267 }
268}
269
270// Serializes the |dictionary| into the file with the given |fd| in a JSON
271// format.
272void SerializeLogsAsJSON(const base::DictionaryValue& dictionary,
273 const DBus::FileDescriptor& fd) {
274 std::string logs_json;
275 base::JSONWriter::WriteWithOptions(dictionary,
276 base::JSONWriter::OPTIONS_PRETTY_PRINT,
277 &logs_json);
278 base::WriteFileDescriptor(fd.get(), logs_json.c_str(), logs_json.size());
279}
280
Elly Jones533c7c42012-08-10 15:07:05 -0400281bool GetNamedLogFrom(const string& name, const struct Log* logs,
282 string* result) {
283 for (size_t i = 0; logs[i].name; i++) {
284 if (name == logs[i].name) {
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -0500285 *result = Run(logs[i]);
Elly Jones533c7c42012-08-10 15:07:05 -0400286 return true;
287 }
288 }
289 *result = "<invalid log name>";
290 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400291}
292
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800293void GetLogsFrom(const struct Log* logs, LogTool::LogMap* map) {
294 for (size_t i = 0; logs[i].name; i++)
295 (*map)[logs[i].name] = Run(logs[i]);
296}
297
298} // namespace
299
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700300void LogTool::CreateConnectivityReport(DBus::Connection* connection) {
301 // Perform ConnectivityTrial to report connection state in feedback log.
302 ManagerProxy shill(connection,
303 shill::kFlimflamServicePath,
304 shill::kFlimflamServiceName);
305 shill.CreateConnectivityReport();
306 // Give the connection trial time to test the connection and log the results
307 // before collecting the logs for feedback.
308 // TODO(silberst): Replace the simple approach of a single timeout with a more
309 // coordinated effort.
310 sleep(kConnectionTesterTimeoutSeconds);
311}
312
Ben Chana0011d82014-05-13 00:19:29 -0700313string LogTool::GetLog(const string& name, DBus::Error* error) {
Elly Jones533c7c42012-08-10 15:07:05 -0400314 string result;
315 GetNamedLogFrom(name, common_logs, &result)
316 || GetNamedLogFrom(name, extra_logs, &result)
317 || GetNamedLogFrom(name, feedback_logs, &result);
318 return result;
319}
320
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700321LogTool::LogMap LogTool::GetAllLogs(DBus::Connection* connection,
322 DBus::Error* error) {
323 CreateConnectivityReport(connection);
Elly Jones533c7c42012-08-10 15:07:05 -0400324 LogMap result;
325 GetLogsFrom(common_logs, &result);
326 GetLogsFrom(extra_logs, &result);
327 return result;
328}
329
Rebecca Silbersteine78af402014-10-02 10:55:04 -0700330LogTool::LogMap LogTool::GetFeedbackLogs(DBus::Connection* connection,
331 DBus::Error* error) {
332 CreateConnectivityReport(connection);
Elly Jones533c7c42012-08-10 15:07:05 -0400333 LogMap result;
334 GetLogsFrom(common_logs, &result);
335 GetLogsFrom(feedback_logs, &result);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100336 AnonymizeLogMap(&result);
Elly Jones03cd6d72012-06-11 13:04:28 -0400337 return result;
338}
339
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800340void LogTool::GetBigFeedbackLogs(DBus::Connection* connection,
341 const DBus::FileDescriptor& fd,
342 DBus::Error* error) {
343 CreateConnectivityReport(connection);
344 base::DictionaryValue dictionary;
345 GetLogsInDictionary(common_logs, &anonymizer_, &dictionary);
346 GetLogsInDictionary(feedback_logs, &anonymizer_, &dictionary);
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700347 GetLogsInDictionary(big_feedback_logs, &anonymizer_, &dictionary);
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800348 SerializeLogsAsJSON(dictionary, fd);
Ahmed Fakhry1498b4e2016-03-30 12:42:20 -0700349
350 // We need to manually close the FD here to enable the client to read the
351 // contents via a pipe.
352 close(fd.get());
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800353}
354
Ben Chana0011d82014-05-13 00:19:29 -0700355LogTool::LogMap LogTool::GetUserLogFiles(DBus::Error* error) {
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700356 LogMap result;
357 for (size_t i = 0; user_logs[i].name; ++i)
358 result[user_logs[i].name] = user_logs[i].command;
359 return result;
360}
361
Darin Petkovce9b3a12013-01-10 16:38:54 +0100362void LogTool::AnonymizeLogMap(LogMap* log_map) {
Darin Petkovce9b3a12013-01-10 16:38:54 +0100363 for (LogMap::iterator it = log_map->begin();
364 it != log_map->end(); ++it) {
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800365 it->second = anonymizer_.Anonymize(it->second);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100366 }
367}
368
Ben Chana0011d82014-05-13 00:19:29 -0700369} // namespace debugd