blob: e4f5c5d710bb4b6461f8dabc2ab91059254b66de [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
15#include "process_with_output.h"
16
17namespace debugd {
18
19const char *kShell = "/bin/sh";
20
21using std::string;
22using std::vector;
23
24typedef vector<string> Strings;
25
Ben Chanf6cd93a2012-10-14 19:37:00 -070026// Returns |value| if |value| is a valid UTF-8 string or a base64-encoded
27// string of |value| otherwise.
28string EnsureUTF8String(const string& value) {
29 if (IsStringUTF8(value))
30 return value;
31
32 gchar* base64_value = g_base64_encode(
33 reinterpret_cast<const guchar*>(value.c_str()), value.length());
34 if (base64_value) {
35 string encoded_value = "<base64>: ";
36 encoded_value += base64_value;
37 g_free(base64_value);
38 return encoded_value;
39 }
40 return "<invalid>";
41}
42
Elly Fong-Jones57f018c2012-10-08 14:22:01 -040043// TODO(ellyjones): sandbox. crosbug.com/35122
Elly Jones03cd6d72012-06-11 13:04:28 -040044string Run(const string& cmdline) {
45 string output;
46 ProcessWithOutput p;
Elly Jonese0518ff2012-08-13 13:22:46 -040047 string tailed_cmdline = cmdline + " | tail -c 256K";
Elly Jones03cd6d72012-06-11 13:04:28 -040048 if (!p.Init())
49 return "<not available>";
50 p.AddArg(kShell);
51 p.AddStringOption("-c", tailed_cmdline);
52 if (p.Run())
53 return "<not available>";
54 p.GetOutput(&output);
55 if (!output.size())
56 return "<empty>";
Ben Chanf6cd93a2012-10-14 19:37:00 -070057 return EnsureUTF8String(output);
Elly Jones03cd6d72012-06-11 13:04:28 -040058}
59
60struct Log {
61 const char *name;
62 const char *command;
63};
64
Elly Jones533c7c42012-08-10 15:07:05 -040065static const Log common_logs[] = {
Elly Jones03cd6d72012-06-11 13:04:28 -040066 { "CLIENT_ID", "/bin/cat '/home/chronos/Consent To Send Stats'" },
67 { "LOGDATE", "/bin/date" },
68 { "Xorg.0.log", "/bin/cat /var/log/Xorg.0.log" },
Elly Jones03cd6d72012-06-11 13:04:28 -040069 { "bios_info", "/bin/cat /var/log/bios_info.txt" },
70 { "board-specific",
71 "/usr/share/userfeedback/scripts/get_board_specific_info" },
72 { "boot_times", "/bin/cat /tmp/boot-times-sent" },
Elly Jones03cd6d72012-06-11 13:04:28 -040073 { "chrome_system_log", "/bin/cat /var/log/chrome/chrome" },
Elly Fong-Jones57f018c2012-10-08 14:22:01 -040074 { "console-ramoops", "/bin/cat /dev/pstore/console-ramoops" },
Elly Jones03cd6d72012-06-11 13:04:28 -040075 { "cpu", "/usr/bin/uname -p" },
76 { "cpuinfo", "/bin/cat /proc/cpuinfo" },
Elly Jones03cd6d72012-06-11 13:04:28 -040077 { "dmesg", "/bin/dmesg" },
78 { "ec_info", "/bin/cat /var/log/ec_info.txt" },
79 { "font_info", "/usr/share/userfeedback/scripts/font_info" },
80 { "hardware_class", "/usr/bin/crossystem hwid" },
81 { "hostname", "/bin/hostname" },
82 { "hw_platform", "/usr/bin/uname -i" },
83 { "ifconfig", "/sbin/ifconfig -a" },
Elly Jones2bde94d2012-07-31 18:07:46 -040084 { "kernel-crashes", "/bin/cat /var/spool/crash/kernel.*.kcrash" },
Elly Jones03cd6d72012-06-11 13:04:28 -040085 { "lsmod", "lsmod" },
86 { "lspci", "/usr/sbin/lspci" },
87 { "lsusb", "lsusb" },
88 { "meminfo", "cat /proc/meminfo" },
89 { "memory_spd_info", "/bin/cat /var/log/memory_spd_info.txt" },
Elly Jones03cd6d72012-06-11 13:04:28 -040090 { "power-supply-info", "/usr/bin/power-supply-info" },
91 { "powerd.LATEST", "/bin/cat /var/log/power_manager/powerd.LATEST" },
92 { "powerd.out", "/bin/cat /var/log/power_manager/powerd.out" },
93 { "powerm.LATEST", "/bin/cat /var/log/power_manager/powerm.LATEST" },
94 { "powerm.out", "/bin/cat /var/log/power_manager/powerm.out" },
95 // Changed from 'ps ux' to 'ps aux' since we're running as debugd, not chronos
96 { "ps", "/bin/ps aux" },
97 { "syslog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'"
Elly Jonesf8a67ee2012-06-19 13:48:38 -040098 " /var/log/messages" },
Elly Jones03cd6d72012-06-11 13:04:28 -040099 { "touchpad", "/opt/google/touchpad/tpcontrol status" },
Elly Jonesd31aee22012-07-02 11:09:10 -0400100 { "touchpad_activity", "/opt/google/touchpad/generate_userfeedback alt" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400101 { "ui_log", "/usr/share/userfeedback/scripts/get_log /var/log/ui/ui.LATEST" },
102 { "uname", "/bin/uname -a" },
103 { "update_engine.log", "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
Elly Jonesd31aee22012-07-02 11:09:10 -0400104 " s.^./var/log/update_engine/.)" },
Elly Jones03cd6d72012-06-11 13:04:28 -0400105 { "verified boot", "/bin/cat /var/log/debug_vboot_noisy.log" },
106 { "vpd_2.0", "/bin/cat /var/log/vpd_2.0.txt" },
107 { "wifi_status", "/usr/bin/network_diagnostics --wifi --no-log" },
108
Gaurav Shah6e8fd892012-10-01 11:51:08 -0700109
110 // Stuff pulled out of the original list because debugd is running under
111 // a VFS namespace and does not have access to later cryptohome mounts.
112 // This includes anything under /home/chronos/user/*
113 // { "chrome_log", "/bin/cat /home/chronos/user/log/chrome" },
114 // { "login-times", "/bin/cat /home/chronos/user/login-times" },
115 // { "logout-times", "/bin/cat /home/chronos/user/logout-times" },
116
Elly Jones03cd6d72012-06-11 13:04:28 -0400117 // Stuff pulled out of the original list. These need access to the running X
118 // session, which we'd rather not give to debugd, or return info specific to
119 // the current session (in the setsid(2) sense), which is not useful for
120 // debugd
121 // { "env", "set" },
122 // { "setxkbmap", "/usr/bin/setxkbmap -print -query" },
123 // { "xrandr", "/usr/bin/xrandr --verbose" }
Elly Jones533c7c42012-08-10 15:07:05 -0400124 { NULL, NULL }
125};
126
127static const Log extra_logs[] = {
128 { "mm-status", "/usr/bin/modem status" },
129 { "network-devices", "/usr/bin/connectivity show devices" },
130 { "network-services", "/usr/bin/connectivity show services" },
131 { NULL, NULL }
132};
133
134static struct Log feedback_logs[] = {
135 { "mm-status", "/usr/bin/modem status-feedback" },
136 { "network-devices", "/usr/bin/connectivity show-feedback devices" },
137 { "network-services", "/usr/bin/connectivity show-feedback services" },
138 { NULL, NULL }
Elly Jones03cd6d72012-06-11 13:04:28 -0400139};
140
141LogTool::LogTool() { }
142LogTool::~LogTool() { }
143
Elly Jones533c7c42012-08-10 15:07:05 -0400144bool GetNamedLogFrom(const string& name, const struct Log* logs,
145 string* result) {
146 for (size_t i = 0; logs[i].name; i++) {
147 if (name == logs[i].name) {
148 *result = Run(logs[i].command);
149 return true;
150 }
151 }
152 *result = "<invalid log name>";
153 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400154}
155
Elly Jones533c7c42012-08-10 15:07:05 -0400156string LogTool::GetLog(const string& name, DBus::Error& error) { // NOLINT
157 string result;
158 GetNamedLogFrom(name, common_logs, &result)
159 || GetNamedLogFrom(name, extra_logs, &result)
160 || GetNamedLogFrom(name, feedback_logs, &result);
161 return result;
162}
163
164void GetLogsFrom(const struct Log* logs, LogTool::LogMap* map) {
165 for (size_t i = 0; logs[i].name; i++)
166 (*map)[logs[i].name] = Run(logs[i].command);
167}
168
169LogTool::LogMap LogTool::GetAllLogs(DBus::Error& error) { // NOLINT
170 LogMap result;
171 GetLogsFrom(common_logs, &result);
172 GetLogsFrom(extra_logs, &result);
173 return result;
174}
175
176LogTool::LogMap LogTool::GetFeedbackLogs(DBus::Error& error) { // NOLINT
177 LogMap result;
178 GetLogsFrom(common_logs, &result);
179 GetLogsFrom(feedback_logs, &result);
Elly Jones03cd6d72012-06-11 13:04:28 -0400180 return result;
181}
182
183}; // namespace debugd