blob: cb0c1fca65ffc8cd273dedc2def44a2814433cda [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
5#include "log_tool.h"
6
Ben Chanf6cd93a2012-10-14 19:37:00 -07007#include <glib.h>
8
9#include <base/base64.h>
Elly Jones03cd6d72012-06-11 13:04:28 -040010#include <base/file_util.h>
11#include <base/logging.h>
12#include <base/string_split.h>
Ben Chanf6cd93a2012-10-14 19:37:00 -070013#include <base/string_util.h>
Elly Jones03cd6d72012-06-11 13:04:28 -040014
Darin Petkovce9b3a12013-01-10 16:38:54 +010015#include "anonymizer_tool.h"
Elly Jones03cd6d72012-06-11 13:04:28 -040016#include "process_with_output.h"
17
18namespace debugd {
19
20const char *kShell = "/bin/sh";
Elly Fong-Jones215b5622013-03-20 14:32:18 -040021const char *kDebugfsGroup = "debugfs-access";
Elly Jones03cd6d72012-06-11 13:04:28 -040022
23using std::string;
24using std::vector;
25
26typedef vector<string> Strings;
27
Ben Chanf6cd93a2012-10-14 19:37:00 -070028// Returns |value| if |value| is a valid UTF-8 string or a base64-encoded
29// string of |value| otherwise.
30string EnsureUTF8String(const string& value) {
31 if (IsStringUTF8(value))
32 return value;
33
34 gchar* base64_value = g_base64_encode(
35 reinterpret_cast<const guchar*>(value.c_str()), value.length());
36 if (base64_value) {
37 string encoded_value = "<base64>: ";
38 encoded_value += base64_value;
39 g_free(base64_value);
40 return encoded_value;
41 }
42 return "<invalid>";
43}
44
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050045struct Log {
46 const char *name;
47 const char *command;
48 const char *user;
49 const char *group;
50};
51
Elly Fong-Jones57f018c2012-10-08 14:22:01 -040052// TODO(ellyjones): sandbox. crosbug.com/35122
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050053string Run(const Log& log) {
Elly Jones03cd6d72012-06-11 13:04:28 -040054 string output;
55 ProcessWithOutput p;
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050056 string tailed_cmdline = std::string(log.command) + " | tail -c 256K";
57 if (log.user && log.group)
58 p.SandboxAs(log.user, log.group);
Elly Jones03cd6d72012-06-11 13:04:28 -040059 if (!p.Init())
60 return "<not available>";
61 p.AddArg(kShell);
62 p.AddStringOption("-c", tailed_cmdline);
63 if (p.Run())
64 return "<not available>";
65 p.GetOutput(&output);
66 if (!output.size())
67 return "<empty>";
Ben Chanf6cd93a2012-10-14 19:37:00 -070068 return EnsureUTF8String(output);
Elly Jones03cd6d72012-06-11 13:04:28 -040069}
70
Elly Jones533c7c42012-08-10 15:07:05 -040071static const Log common_logs[] = {
Elly Jones03cd6d72012-06-11 13:04:28 -040072 { "CLIENT_ID", "/bin/cat '/home/chronos/Consent To Send Stats'" },
73 { "LOGDATE", "/bin/date" },
74 { "Xorg.0.log", "/bin/cat /var/log/Xorg.0.log" },
Elly Jones03cd6d72012-06-11 13:04:28 -040075 { "bios_info", "/bin/cat /var/log/bios_info.txt" },
Stefan Reinauer4393fa72012-10-18 11:08:09 -070076 { "bios_log", "/bin/cat /sys/firmware/log" },
77 { "bios_times", "/bin/cat /var/log/bios_times.txt" },
Elly Jones03cd6d72012-06-11 13:04:28 -040078 { "board-specific",
79 "/usr/share/userfeedback/scripts/get_board_specific_info" },
80 { "boot_times", "/bin/cat /tmp/boot-times-sent" },
Elly Jones03cd6d72012-06-11 13:04:28 -040081 { "chrome_system_log", "/bin/cat /var/log/chrome/chrome" },
Elly Fong-Jones57f018c2012-10-08 14:22:01 -040082 { "console-ramoops", "/bin/cat /dev/pstore/console-ramoops" },
Elly Jones03cd6d72012-06-11 13:04:28 -040083 { "cpu", "/usr/bin/uname -p" },
84 { "cpuinfo", "/bin/cat /proc/cpuinfo" },
Elly Jones03cd6d72012-06-11 13:04:28 -040085 { "dmesg", "/bin/dmesg" },
86 { "ec_info", "/bin/cat /var/log/ec_info.txt" },
Stefan Reinauer4393fa72012-10-18 11:08:09 -070087 { "eventlog", "/bin/cat /var/log/eventlog.txt" },
Elly Fong-Jones215b5622013-03-20 14:32:18 -040088 {
89 "exynos_gem_objects",
Yuly Novikov13ece852013-07-11 17:19:10 -040090 "/bin/cat /sys/kernel/debug/dri/0/exynos_gem_objects 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -040091 SandboxedProcess::kDefaultUser,
92 kDebugfsGroup
93 },
Elly Jones03cd6d72012-06-11 13:04:28 -040094 { "font_info", "/usr/share/userfeedback/scripts/font_info" },
95 { "hardware_class", "/usr/bin/crossystem hwid" },
96 { "hostname", "/bin/hostname" },
97 { "hw_platform", "/usr/bin/uname -i" },
Elly Fong-Jones215b5622013-03-20 14:32:18 -040098 {
99 "i915_gem_gtt",
Yuly Novikov13ece852013-07-11 17:19:10 -0400100 "/bin/cat /sys/kernel/debug/dri/0/i915_gem_gtt 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400101 SandboxedProcess::kDefaultUser,
102 kDebugfsGroup
103 },
104 {
105 "i915_gem_objects",
Yuly Novikov13ece852013-07-11 17:19:10 -0400106 "/bin/cat /sys/kernel/debug/dri/0/i915_gem_objects 2> /dev/null",
Elly Fong-Jones215b5622013-03-20 14:32:18 -0400107 SandboxedProcess::kDefaultUser,
108 kDebugfsGroup
109 },
110 {
111 "i915_error_state",
Yuly Novikov4ac12fb2013-07-18 20:08:44 -0400112 "/usr/bin/xz -c /sys/kernel/debug/dri/0/i915_error_state 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 { "ifconfig", "/sbin/ifconfig -a" },
Elly Jones2bde94d2012-07-31 18:07:46 -0400117 { "kernel-crashes", "/bin/cat /var/spool/crash/kernel.*.kcrash" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400118 { "lsmod", "lsmod" },
119 { "lspci", "/usr/sbin/lspci" },
120 { "lsusb", "lsusb" },
Yuly Novikov13ece852013-07-11 17:19:10 -0400121 {
122 "mali_memory",
123 "/bin/cat /sys/devices/platform/mali.0/memory 2> /dev/null"
124 },
Elly Jones03cd6d72012-06-11 13:04:28 -0400125 { "meminfo", "cat /proc/meminfo" },
126 { "memory_spd_info", "/bin/cat /var/log/memory_spd_info.txt" },
Kees Cooka8f3e7e2012-11-19 14:16:01 -0800127 { "mount-encrypted", "/bin/cat /var/log/mount-encrypted.log" },
Darin Petkov54743582012-12-10 14:18:32 +0100128 { "net-diags.net.log", "/bin/cat /var/log/net-diags.net.log" },
Wade Guthrie67b672e2012-11-14 13:14:31 -0800129 { "netlog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'"
130 " /var/log/net.log" },
Vincent Palatinc64af9d2013-08-05 16:34:10 -0700131 { "platform_info", "/bin/cat /var/log/platform_info.txt" },
Daniel Erat9b58b6d2013-04-30 14:58:56 -0700132 { "power_supply_info", "/usr/bin/power_supply_info" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400133 { "powerd.LATEST", "/bin/cat /var/log/power_manager/powerd.LATEST" },
Daniel Erat93da9a12012-12-06 14:14:17 -0800134 { "powerd.out", "/bin/cat /var/log/powerd.out" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400135 // Changed from 'ps ux' to 'ps aux' since we're running as debugd, not chronos
136 { "ps", "/bin/ps aux" },
137 { "syslog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'"
Elly Jonesf8a67ee2012-06-19 13:48:38 -0400138 " /var/log/messages" },
Elly Fong-Jonesb4f49c42013-02-28 13:45:26 -0500139 { "tlsdate", "/bin/cat /var/log/tlsdate.log" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400140 { "touchpad", "/opt/google/touchpad/tpcontrol status" },
Elly Jonesd31aee22012-07-02 11:09:10 -0400141 { "touchpad_activity", "/opt/google/touchpad/generate_userfeedback alt" },
Andrew de los Reyesc060c342013-04-10 13:46:30 -0700142 { "touch_fw_version", "grep -E"
143 " 'synaptics: Touchpad model|chromeos-touch-[a-z]*-update'"
144 " /var/log/messages | tail -n 20" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400145 { "ui_log", "/usr/share/userfeedback/scripts/get_log /var/log/ui/ui.LATEST" },
146 { "uname", "/bin/uname -a" },
147 { "update_engine.log", "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
Elly Jonesd31aee22012-07-02 11:09:10 -0400148 " s.^./var/log/update_engine/.)" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400149 { "verified boot", "/bin/cat /var/log/debug_vboot_noisy.log" },
150 { "vpd_2.0", "/bin/cat /var/log/vpd_2.0.txt" },
Kees Cookf39f0e02013-02-05 12:00:10 -0800151 { "wifi_status", "/usr/bin/network_diagnostics --wifi-internal --no-log" },
Sonny Raoab5f9952013-07-17 14:13:53 -0700152 { "zram compressed data size",
153 "/bin/cat /sys/block/zram0/compr_data_size 2> /dev/null" },
154 { "zram original data size",
155 "/bin/cat /sys/block/zram0/orig_data_size 2> /dev/null" },
156 { "zram total memory used",
157 "/bin/cat /sys/block/zram0/mem_used_total 2> /dev/null" },
158 { "zram total reads",
159 "/bin/cat /sys/block/zram0/num_reads 2> /dev/null" },
160 { "zram total writes",
161 "/bin/cat /sys/block/zram0/num_writes 2> /dev/null" },
Gaurav Shah6e8fd892012-10-01 11:51:08 -0700162
Elly Jones03cd6d72012-06-11 13:04:28 -0400163 // Stuff pulled out of the original list. These need access to the running X
164 // session, which we'd rather not give to debugd, or return info specific to
165 // the current session (in the setsid(2) sense), which is not useful for
166 // debugd
167 // { "env", "set" },
168 // { "setxkbmap", "/usr/bin/setxkbmap -print -query" },
169 // { "xrandr", "/usr/bin/xrandr --verbose" }
Elly Jones533c7c42012-08-10 15:07:05 -0400170 { NULL, NULL }
171};
172
173static const Log extra_logs[] = {
174 { "mm-status", "/usr/bin/modem status" },
175 { "network-devices", "/usr/bin/connectivity show devices" },
176 { "network-services", "/usr/bin/connectivity show services" },
177 { NULL, NULL }
178};
179
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700180static const Log feedback_logs[] = {
Elly Jones533c7c42012-08-10 15:07:05 -0400181 { "mm-status", "/usr/bin/modem status-feedback" },
182 { "network-devices", "/usr/bin/connectivity show-feedback devices" },
183 { "network-services", "/usr/bin/connectivity show-feedback services" },
184 { NULL, NULL }
Elly Jones03cd6d72012-06-11 13:04:28 -0400185};
186
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700187// List of log files that must directly be collected by Chrome. This is because
188// debugd is running under a VFS namespace and does not have access to later
189// cryptohome mounts.
190static const Log user_logs[] = {
Elly Fong-Jones6456ce52013-04-17 13:31:13 -0400191 {"chrome_user_log", "log/chrome"},
192 {"login-times", "login-times"},
193 {"logout-times", "logout-times"},
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700194 { NULL, NULL}
195};
196
Elly Jones03cd6d72012-06-11 13:04:28 -0400197LogTool::LogTool() { }
198LogTool::~LogTool() { }
199
Elly Jones533c7c42012-08-10 15:07:05 -0400200bool GetNamedLogFrom(const string& name, const struct Log* logs,
201 string* result) {
202 for (size_t i = 0; logs[i].name; i++) {
203 if (name == logs[i].name) {
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -0500204 *result = Run(logs[i]);
Elly Jones533c7c42012-08-10 15:07:05 -0400205 return true;
206 }
207 }
208 *result = "<invalid log name>";
209 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400210}
211
Elly Jones533c7c42012-08-10 15:07:05 -0400212string LogTool::GetLog(const string& name, DBus::Error& error) { // NOLINT
213 string result;
214 GetNamedLogFrom(name, common_logs, &result)
215 || GetNamedLogFrom(name, extra_logs, &result)
216 || GetNamedLogFrom(name, feedback_logs, &result);
217 return result;
218}
219
220void GetLogsFrom(const struct Log* logs, LogTool::LogMap* map) {
221 for (size_t i = 0; logs[i].name; i++)
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -0500222 (*map)[logs[i].name] = Run(logs[i]);
Elly Jones533c7c42012-08-10 15:07:05 -0400223}
224
225LogTool::LogMap LogTool::GetAllLogs(DBus::Error& error) { // NOLINT
226 LogMap result;
227 GetLogsFrom(common_logs, &result);
228 GetLogsFrom(extra_logs, &result);
229 return result;
230}
231
232LogTool::LogMap LogTool::GetFeedbackLogs(DBus::Error& error) { // NOLINT
233 LogMap result;
234 GetLogsFrom(common_logs, &result);
235 GetLogsFrom(feedback_logs, &result);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100236 AnonymizeLogMap(&result);
Elly Jones03cd6d72012-06-11 13:04:28 -0400237 return result;
238}
239
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700240LogTool::LogMap LogTool::GetUserLogFiles(DBus::Error& error) { // NOLINT
241 LogMap result;
242 for (size_t i = 0; user_logs[i].name; ++i)
243 result[user_logs[i].name] = user_logs[i].command;
244 return result;
245}
246
Darin Petkovce9b3a12013-01-10 16:38:54 +0100247void LogTool::AnonymizeLogMap(LogMap* log_map) {
248 AnonymizerTool anonymizer;
249 for (LogMap::iterator it = log_map->begin();
250 it != log_map->end(); ++it) {
251 it->second = anonymizer.Anonymize(it->second);
252 }
253}
254
Elly Jones03cd6d72012-06-11 13:04:28 -0400255}; // namespace debugd