blob: 673864e3cfe4469be052fde2fb3f6b0aa4c47774 [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";
21
22using std::string;
23using std::vector;
24
25typedef vector<string> Strings;
26
Ben Chanf6cd93a2012-10-14 19:37:00 -070027// Returns |value| if |value| is a valid UTF-8 string or a base64-encoded
28// string of |value| otherwise.
29string EnsureUTF8String(const string& value) {
30 if (IsStringUTF8(value))
31 return value;
32
33 gchar* base64_value = g_base64_encode(
34 reinterpret_cast<const guchar*>(value.c_str()), value.length());
35 if (base64_value) {
36 string encoded_value = "<base64>: ";
37 encoded_value += base64_value;
38 g_free(base64_value);
39 return encoded_value;
40 }
41 return "<invalid>";
42}
43
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050044struct Log {
45 const char *name;
46 const char *command;
47 const char *user;
48 const char *group;
49};
50
Elly Fong-Jones57f018c2012-10-08 14:22:01 -040051// TODO(ellyjones): sandbox. crosbug.com/35122
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050052string Run(const Log& log) {
Elly Jones03cd6d72012-06-11 13:04:28 -040053 string output;
54 ProcessWithOutput p;
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050055 string tailed_cmdline = std::string(log.command) + " | tail -c 256K";
56 if (log.user && log.group)
57 p.SandboxAs(log.user, log.group);
Elly Jones03cd6d72012-06-11 13:04:28 -040058 if (!p.Init())
59 return "<not available>";
60 p.AddArg(kShell);
61 p.AddStringOption("-c", tailed_cmdline);
62 if (p.Run())
63 return "<not available>";
64 p.GetOutput(&output);
65 if (!output.size())
66 return "<empty>";
Ben Chanf6cd93a2012-10-14 19:37:00 -070067 return EnsureUTF8String(output);
Elly Jones03cd6d72012-06-11 13:04:28 -040068}
69
Elly Jones533c7c42012-08-10 15:07:05 -040070static const Log common_logs[] = {
Elly Jones03cd6d72012-06-11 13:04:28 -040071 { "CLIENT_ID", "/bin/cat '/home/chronos/Consent To Send Stats'" },
72 { "LOGDATE", "/bin/date" },
73 { "Xorg.0.log", "/bin/cat /var/log/Xorg.0.log" },
Elly Jones03cd6d72012-06-11 13:04:28 -040074 { "bios_info", "/bin/cat /var/log/bios_info.txt" },
Stefan Reinauer4393fa72012-10-18 11:08:09 -070075 { "bios_log", "/bin/cat /sys/firmware/log" },
76 { "bios_times", "/bin/cat /var/log/bios_times.txt" },
Elly Jones03cd6d72012-06-11 13:04:28 -040077 { "board-specific",
78 "/usr/share/userfeedback/scripts/get_board_specific_info" },
79 { "boot_times", "/bin/cat /tmp/boot-times-sent" },
Elly Jones03cd6d72012-06-11 13:04:28 -040080 { "chrome_system_log", "/bin/cat /var/log/chrome/chrome" },
Elly Fong-Jones57f018c2012-10-08 14:22:01 -040081 { "console-ramoops", "/bin/cat /dev/pstore/console-ramoops" },
Elly Jones03cd6d72012-06-11 13:04:28 -040082 { "cpu", "/usr/bin/uname -p" },
83 { "cpuinfo", "/bin/cat /proc/cpuinfo" },
Elly Jones03cd6d72012-06-11 13:04:28 -040084 { "dmesg", "/bin/dmesg" },
85 { "ec_info", "/bin/cat /var/log/ec_info.txt" },
Stefan Reinauer4393fa72012-10-18 11:08:09 -070086 { "eventlog", "/bin/cat /var/log/eventlog.txt" },
Sonny Raodb876812012-11-16 16:16:41 -080087 { "exynos_gem_objects",
88 "/bin/cat /sys/kernel/debug/dri/0/exynos_gem_objects" },
Elly Jones03cd6d72012-06-11 13:04:28 -040089 { "font_info", "/usr/share/userfeedback/scripts/font_info" },
90 { "hardware_class", "/usr/bin/crossystem hwid" },
91 { "hostname", "/bin/hostname" },
92 { "hw_platform", "/usr/bin/uname -i" },
Sonny Raofc348f62012-10-24 18:11:04 -070093 { "i915_gem_gtt", "/bin/cat /sys/kernel/debug/dri/0/i915_gem_gtt" },
94 { "i915_gem_objects", "/bin/cat /sys/kernel/debug/dri/0/i915_gem_objects" },
Stéphane Marchesin1f9ff862012-12-21 16:29:53 -080095 { "i915_error_state", "/bin/cat /sys/kernel/debug/dri/0/i915_error_state" },
Elly Jones03cd6d72012-06-11 13:04:28 -040096 { "ifconfig", "/sbin/ifconfig -a" },
Elly Jones2bde94d2012-07-31 18:07:46 -040097 { "kernel-crashes", "/bin/cat /var/spool/crash/kernel.*.kcrash" },
Elly Jones03cd6d72012-06-11 13:04:28 -040098 { "lsmod", "lsmod" },
99 { "lspci", "/usr/sbin/lspci" },
100 { "lsusb", "lsusb" },
Sonny Raodb876812012-11-16 16:16:41 -0800101 { "mali_memory", "/bin/cat /sys/devices/platform/mali.0/memory" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400102 { "meminfo", "cat /proc/meminfo" },
103 { "memory_spd_info", "/bin/cat /var/log/memory_spd_info.txt" },
Kees Cooka8f3e7e2012-11-19 14:16:01 -0800104 { "mount-encrypted", "/bin/cat /var/log/mount-encrypted.log" },
Darin Petkov54743582012-12-10 14:18:32 +0100105 { "net-diags.net.log", "/bin/cat /var/log/net-diags.net.log" },
Wade Guthrie67b672e2012-11-14 13:14:31 -0800106 { "netlog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'"
107 " /var/log/net.log" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400108 { "power-supply-info", "/usr/bin/power-supply-info" },
109 { "powerd.LATEST", "/bin/cat /var/log/power_manager/powerd.LATEST" },
Daniel Erat93da9a12012-12-06 14:14:17 -0800110 { "powerd.out", "/bin/cat /var/log/powerd.out" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400111 // Changed from 'ps ux' to 'ps aux' since we're running as debugd, not chronos
112 { "ps", "/bin/ps aux" },
113 { "syslog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'"
Elly Jonesf8a67ee2012-06-19 13:48:38 -0400114 " /var/log/messages" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400115 { "touchpad", "/opt/google/touchpad/tpcontrol status" },
Elly Jonesd31aee22012-07-02 11:09:10 -0400116 { "touchpad_activity", "/opt/google/touchpad/generate_userfeedback alt" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400117 { "ui_log", "/usr/share/userfeedback/scripts/get_log /var/log/ui/ui.LATEST" },
118 { "uname", "/bin/uname -a" },
119 { "update_engine.log", "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
Elly Jonesd31aee22012-07-02 11:09:10 -0400120 " s.^./var/log/update_engine/.)" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400121 { "verified boot", "/bin/cat /var/log/debug_vboot_noisy.log" },
122 { "vpd_2.0", "/bin/cat /var/log/vpd_2.0.txt" },
123 { "wifi_status", "/usr/bin/network_diagnostics --wifi --no-log" },
124
Gaurav Shah6e8fd892012-10-01 11:51:08 -0700125
Elly Jones03cd6d72012-06-11 13:04:28 -0400126 // Stuff pulled out of the original list. These need access to the running X
127 // session, which we'd rather not give to debugd, or return info specific to
128 // the current session (in the setsid(2) sense), which is not useful for
129 // debugd
130 // { "env", "set" },
131 // { "setxkbmap", "/usr/bin/setxkbmap -print -query" },
132 // { "xrandr", "/usr/bin/xrandr --verbose" }
Elly Jones533c7c42012-08-10 15:07:05 -0400133 { NULL, NULL }
134};
135
136static const Log extra_logs[] = {
137 { "mm-status", "/usr/bin/modem status" },
138 { "network-devices", "/usr/bin/connectivity show devices" },
139 { "network-services", "/usr/bin/connectivity show services" },
140 { NULL, NULL }
141};
142
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700143static const Log feedback_logs[] = {
Elly Jones533c7c42012-08-10 15:07:05 -0400144 { "mm-status", "/usr/bin/modem status-feedback" },
145 { "network-devices", "/usr/bin/connectivity show-feedback devices" },
146 { "network-services", "/usr/bin/connectivity show-feedback services" },
147 { NULL, NULL }
Elly Jones03cd6d72012-06-11 13:04:28 -0400148};
149
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700150// List of log files that must directly be collected by Chrome. This is because
151// debugd is running under a VFS namespace and does not have access to later
152// cryptohome mounts.
153static const Log user_logs[] = {
154 {"chrome_user_log", "/home/chronos/user/log/chrome"},
155 {"login-times", "/home/chronos/user/login-times"},
156 {"logout-times", "/home/chronos/user/logout-times"},
157 { NULL, NULL}
158};
159
Elly Jones03cd6d72012-06-11 13:04:28 -0400160LogTool::LogTool() { }
161LogTool::~LogTool() { }
162
Elly Jones533c7c42012-08-10 15:07:05 -0400163bool GetNamedLogFrom(const string& name, const struct Log* logs,
164 string* result) {
165 for (size_t i = 0; logs[i].name; i++) {
166 if (name == logs[i].name) {
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -0500167 *result = Run(logs[i]);
Elly Jones533c7c42012-08-10 15:07:05 -0400168 return true;
169 }
170 }
171 *result = "<invalid log name>";
172 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400173}
174
Elly Jones533c7c42012-08-10 15:07:05 -0400175string LogTool::GetLog(const string& name, DBus::Error& error) { // NOLINT
176 string result;
177 GetNamedLogFrom(name, common_logs, &result)
178 || GetNamedLogFrom(name, extra_logs, &result)
179 || GetNamedLogFrom(name, feedback_logs, &result);
180 return result;
181}
182
183void GetLogsFrom(const struct Log* logs, LogTool::LogMap* map) {
184 for (size_t i = 0; logs[i].name; i++)
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -0500185 (*map)[logs[i].name] = Run(logs[i]);
Elly Jones533c7c42012-08-10 15:07:05 -0400186}
187
188LogTool::LogMap LogTool::GetAllLogs(DBus::Error& error) { // NOLINT
189 LogMap result;
190 GetLogsFrom(common_logs, &result);
191 GetLogsFrom(extra_logs, &result);
192 return result;
193}
194
195LogTool::LogMap LogTool::GetFeedbackLogs(DBus::Error& error) { // NOLINT
196 LogMap result;
197 GetLogsFrom(common_logs, &result);
198 GetLogsFrom(feedback_logs, &result);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100199 AnonymizeLogMap(&result);
Elly Jones03cd6d72012-06-11 13:04:28 -0400200 return result;
201}
202
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700203LogTool::LogMap LogTool::GetUserLogFiles(DBus::Error& error) { // NOLINT
204 LogMap result;
205 for (size_t i = 0; user_logs[i].name; ++i)
206 result[user_logs[i].name] = user_logs[i].command;
207 return result;
208}
209
Darin Petkovce9b3a12013-01-10 16:38:54 +0100210void LogTool::AnonymizeLogMap(LogMap* log_map) {
211 AnonymizerTool anonymizer;
212 for (LogMap::iterator it = log_map->begin();
213 it != log_map->end(); ++it) {
214 it->second = anonymizer.Anonymize(it->second);
215 }
216}
217
Elly Jones03cd6d72012-06-11 13:04:28 -0400218}; // namespace debugd