blob: d044d8a8c691144a63edaa442b81995c35f680fc [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-Jones57f018c2012-10-08 14:22:01 -040044// TODO(ellyjones): sandbox. crosbug.com/35122
Elly Jones03cd6d72012-06-11 13:04:28 -040045string Run(const string& cmdline) {
46 string output;
47 ProcessWithOutput p;
Elly Jonese0518ff2012-08-13 13:22:46 -040048 string tailed_cmdline = cmdline + " | tail -c 256K";
Elly Jones03cd6d72012-06-11 13:04:28 -040049 if (!p.Init())
50 return "<not available>";
51 p.AddArg(kShell);
52 p.AddStringOption("-c", tailed_cmdline);
53 if (p.Run())
54 return "<not available>";
55 p.GetOutput(&output);
56 if (!output.size())
57 return "<empty>";
Ben Chanf6cd93a2012-10-14 19:37:00 -070058 return EnsureUTF8String(output);
Elly Jones03cd6d72012-06-11 13:04:28 -040059}
60
61struct Log {
62 const char *name;
63 const char *command;
64};
65
Elly Jones533c7c42012-08-10 15:07:05 -040066static const Log common_logs[] = {
Elly Jones03cd6d72012-06-11 13:04:28 -040067 { "CLIENT_ID", "/bin/cat '/home/chronos/Consent To Send Stats'" },
68 { "LOGDATE", "/bin/date" },
69 { "Xorg.0.log", "/bin/cat /var/log/Xorg.0.log" },
Elly Jones03cd6d72012-06-11 13:04:28 -040070 { "bios_info", "/bin/cat /var/log/bios_info.txt" },
Stefan Reinauer4393fa72012-10-18 11:08:09 -070071 { "bios_log", "/bin/cat /sys/firmware/log" },
72 { "bios_times", "/bin/cat /var/log/bios_times.txt" },
Elly Jones03cd6d72012-06-11 13:04:28 -040073 { "board-specific",
74 "/usr/share/userfeedback/scripts/get_board_specific_info" },
75 { "boot_times", "/bin/cat /tmp/boot-times-sent" },
Elly Jones03cd6d72012-06-11 13:04:28 -040076 { "chrome_system_log", "/bin/cat /var/log/chrome/chrome" },
Elly Fong-Jones57f018c2012-10-08 14:22:01 -040077 { "console-ramoops", "/bin/cat /dev/pstore/console-ramoops" },
Elly Jones03cd6d72012-06-11 13:04:28 -040078 { "cpu", "/usr/bin/uname -p" },
79 { "cpuinfo", "/bin/cat /proc/cpuinfo" },
Elly Jones03cd6d72012-06-11 13:04:28 -040080 { "dmesg", "/bin/dmesg" },
81 { "ec_info", "/bin/cat /var/log/ec_info.txt" },
Stefan Reinauer4393fa72012-10-18 11:08:09 -070082 { "eventlog", "/bin/cat /var/log/eventlog.txt" },
Sonny Raodb876812012-11-16 16:16:41 -080083 { "exynos_gem_objects",
84 "/bin/cat /sys/kernel/debug/dri/0/exynos_gem_objects" },
Elly Jones03cd6d72012-06-11 13:04:28 -040085 { "font_info", "/usr/share/userfeedback/scripts/font_info" },
86 { "hardware_class", "/usr/bin/crossystem hwid" },
87 { "hostname", "/bin/hostname" },
88 { "hw_platform", "/usr/bin/uname -i" },
Sonny Raofc348f62012-10-24 18:11:04 -070089 { "i915_gem_gtt", "/bin/cat /sys/kernel/debug/dri/0/i915_gem_gtt" },
90 { "i915_gem_objects", "/bin/cat /sys/kernel/debug/dri/0/i915_gem_objects" },
Stéphane Marchesin1f9ff862012-12-21 16:29:53 -080091 { "i915_error_state", "/bin/cat /sys/kernel/debug/dri/0/i915_error_state" },
Elly Jones03cd6d72012-06-11 13:04:28 -040092 { "ifconfig", "/sbin/ifconfig -a" },
Elly Jones2bde94d2012-07-31 18:07:46 -040093 { "kernel-crashes", "/bin/cat /var/spool/crash/kernel.*.kcrash" },
Elly Jones03cd6d72012-06-11 13:04:28 -040094 { "lsmod", "lsmod" },
95 { "lspci", "/usr/sbin/lspci" },
96 { "lsusb", "lsusb" },
Sonny Raodb876812012-11-16 16:16:41 -080097 { "mali_memory", "/bin/cat /sys/devices/platform/mali.0/memory" },
Elly Jones03cd6d72012-06-11 13:04:28 -040098 { "meminfo", "cat /proc/meminfo" },
99 { "memory_spd_info", "/bin/cat /var/log/memory_spd_info.txt" },
Kees Cooka8f3e7e2012-11-19 14:16:01 -0800100 { "mount-encrypted", "/bin/cat /var/log/mount-encrypted.log" },
Darin Petkov54743582012-12-10 14:18:32 +0100101 { "net-diags.net.log", "/bin/cat /var/log/net-diags.net.log" },
Wade Guthrie67b672e2012-11-14 13:14:31 -0800102 { "netlog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'"
103 " /var/log/net.log" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400104 { "power-supply-info", "/usr/bin/power-supply-info" },
105 { "powerd.LATEST", "/bin/cat /var/log/power_manager/powerd.LATEST" },
Daniel Erat93da9a12012-12-06 14:14:17 -0800106 { "powerd.out", "/bin/cat /var/log/powerd.out" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400107 { "powerm.LATEST", "/bin/cat /var/log/power_manager/powerm.LATEST" },
Daniel Erat93da9a12012-12-06 14:14:17 -0800108 { "powerm.out", "/bin/cat /var/log/powerm.out" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400109 // Changed from 'ps ux' to 'ps aux' since we're running as debugd, not chronos
110 { "ps", "/bin/ps aux" },
111 { "syslog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'"
Elly Jonesf8a67ee2012-06-19 13:48:38 -0400112 " /var/log/messages" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400113 { "touchpad", "/opt/google/touchpad/tpcontrol status" },
Elly Jonesd31aee22012-07-02 11:09:10 -0400114 { "touchpad_activity", "/opt/google/touchpad/generate_userfeedback alt" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400115 { "ui_log", "/usr/share/userfeedback/scripts/get_log /var/log/ui/ui.LATEST" },
116 { "uname", "/bin/uname -a" },
117 { "update_engine.log", "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
Elly Jonesd31aee22012-07-02 11:09:10 -0400118 " s.^./var/log/update_engine/.)" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400119 { "verified boot", "/bin/cat /var/log/debug_vboot_noisy.log" },
120 { "vpd_2.0", "/bin/cat /var/log/vpd_2.0.txt" },
121 { "wifi_status", "/usr/bin/network_diagnostics --wifi --no-log" },
122
Gaurav Shah6e8fd892012-10-01 11:51:08 -0700123
Elly Jones03cd6d72012-06-11 13:04:28 -0400124 // Stuff pulled out of the original list. These need access to the running X
125 // session, which we'd rather not give to debugd, or return info specific to
126 // the current session (in the setsid(2) sense), which is not useful for
127 // debugd
128 // { "env", "set" },
129 // { "setxkbmap", "/usr/bin/setxkbmap -print -query" },
130 // { "xrandr", "/usr/bin/xrandr --verbose" }
Elly Jones533c7c42012-08-10 15:07:05 -0400131 { NULL, NULL }
132};
133
134static const Log extra_logs[] = {
135 { "mm-status", "/usr/bin/modem status" },
136 { "network-devices", "/usr/bin/connectivity show devices" },
137 { "network-services", "/usr/bin/connectivity show services" },
138 { NULL, NULL }
139};
140
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700141static const Log feedback_logs[] = {
Elly Jones533c7c42012-08-10 15:07:05 -0400142 { "mm-status", "/usr/bin/modem status-feedback" },
143 { "network-devices", "/usr/bin/connectivity show-feedback devices" },
144 { "network-services", "/usr/bin/connectivity show-feedback services" },
145 { NULL, NULL }
Elly Jones03cd6d72012-06-11 13:04:28 -0400146};
147
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700148// List of log files that must directly be collected by Chrome. This is because
149// debugd is running under a VFS namespace and does not have access to later
150// cryptohome mounts.
151static const Log user_logs[] = {
152 {"chrome_user_log", "/home/chronos/user/log/chrome"},
153 {"login-times", "/home/chronos/user/login-times"},
154 {"logout-times", "/home/chronos/user/logout-times"},
155 { NULL, NULL}
156};
157
Elly Jones03cd6d72012-06-11 13:04:28 -0400158LogTool::LogTool() { }
159LogTool::~LogTool() { }
160
Elly Jones533c7c42012-08-10 15:07:05 -0400161bool GetNamedLogFrom(const string& name, const struct Log* logs,
162 string* result) {
163 for (size_t i = 0; logs[i].name; i++) {
164 if (name == logs[i].name) {
165 *result = Run(logs[i].command);
166 return true;
167 }
168 }
169 *result = "<invalid log name>";
170 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400171}
172
Elly Jones533c7c42012-08-10 15:07:05 -0400173string LogTool::GetLog(const string& name, DBus::Error& error) { // NOLINT
174 string result;
175 GetNamedLogFrom(name, common_logs, &result)
176 || GetNamedLogFrom(name, extra_logs, &result)
177 || GetNamedLogFrom(name, feedback_logs, &result);
178 return result;
179}
180
181void GetLogsFrom(const struct Log* logs, LogTool::LogMap* map) {
182 for (size_t i = 0; logs[i].name; i++)
183 (*map)[logs[i].name] = Run(logs[i].command);
184}
185
186LogTool::LogMap LogTool::GetAllLogs(DBus::Error& error) { // NOLINT
187 LogMap result;
188 GetLogsFrom(common_logs, &result);
189 GetLogsFrom(extra_logs, &result);
190 return result;
191}
192
193LogTool::LogMap LogTool::GetFeedbackLogs(DBus::Error& error) { // NOLINT
194 LogMap result;
195 GetLogsFrom(common_logs, &result);
196 GetLogsFrom(feedback_logs, &result);
Darin Petkovce9b3a12013-01-10 16:38:54 +0100197 AnonymizeLogMap(&result);
Elly Jones03cd6d72012-06-11 13:04:28 -0400198 return result;
199}
200
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -0700201LogTool::LogMap LogTool::GetUserLogFiles(DBus::Error& error) { // NOLINT
202 LogMap result;
203 for (size_t i = 0; user_logs[i].name; ++i)
204 result[user_logs[i].name] = user_logs[i].command;
205 return result;
206}
207
Darin Petkovce9b3a12013-01-10 16:38:54 +0100208void LogTool::AnonymizeLogMap(LogMap* log_map) {
209 AnonymizerTool anonymizer;
210 for (LogMap::iterator it = log_map->begin();
211 it != log_map->end(); ++it) {
212 it->second = anonymizer.Anonymize(it->second);
213 }
214}
215
Elly Jones03cd6d72012-06-11 13:04:28 -0400216}; // namespace debugd