blob: d1c863d552a981f7f3b18ad786c0e405d8c8a8df [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
7#include <base/file_util.h>
8#include <base/logging.h>
9#include <base/string_split.h>
10
11#include "process_with_output.h"
12
13namespace debugd {
14
15const char *kShell = "/bin/sh";
16
17using std::string;
18using std::vector;
19
20typedef vector<string> Strings;
21
Elly Fong-Jones57f018c2012-10-08 14:22:01 -040022// TODO(ellyjones): sandbox. crosbug.com/35122
Elly Jones03cd6d72012-06-11 13:04:28 -040023string Run(const string& cmdline) {
24 string output;
25 ProcessWithOutput p;
Elly Jonese0518ff2012-08-13 13:22:46 -040026 string tailed_cmdline = cmdline + " | tail -c 256K";
Elly Jones03cd6d72012-06-11 13:04:28 -040027 if (!p.Init())
28 return "<not available>";
29 p.AddArg(kShell);
30 p.AddStringOption("-c", tailed_cmdline);
31 if (p.Run())
32 return "<not available>";
33 p.GetOutput(&output);
34 if (!output.size())
35 return "<empty>";
36 return output;
37}
38
39struct Log {
40 const char *name;
41 const char *command;
42};
43
Elly Jones533c7c42012-08-10 15:07:05 -040044static const Log common_logs[] = {
Elly Jones03cd6d72012-06-11 13:04:28 -040045 { "CLIENT_ID", "/bin/cat '/home/chronos/Consent To Send Stats'" },
46 { "LOGDATE", "/bin/date" },
47 { "Xorg.0.log", "/bin/cat /var/log/Xorg.0.log" },
Elly Jones03cd6d72012-06-11 13:04:28 -040048 { "bios_info", "/bin/cat /var/log/bios_info.txt" },
49 { "board-specific",
50 "/usr/share/userfeedback/scripts/get_board_specific_info" },
51 { "boot_times", "/bin/cat /tmp/boot-times-sent" },
Elly Jones03cd6d72012-06-11 13:04:28 -040052 { "chrome_system_log", "/bin/cat /var/log/chrome/chrome" },
Elly Fong-Jones57f018c2012-10-08 14:22:01 -040053 { "console-ramoops", "/bin/cat /dev/pstore/console-ramoops" },
Elly Jones03cd6d72012-06-11 13:04:28 -040054 { "cpu", "/usr/bin/uname -p" },
55 { "cpuinfo", "/bin/cat /proc/cpuinfo" },
Elly Jones03cd6d72012-06-11 13:04:28 -040056 { "dmesg", "/bin/dmesg" },
57 { "ec_info", "/bin/cat /var/log/ec_info.txt" },
58 { "font_info", "/usr/share/userfeedback/scripts/font_info" },
59 { "hardware_class", "/usr/bin/crossystem hwid" },
60 { "hostname", "/bin/hostname" },
61 { "hw_platform", "/usr/bin/uname -i" },
62 { "ifconfig", "/sbin/ifconfig -a" },
Elly Jones2bde94d2012-07-31 18:07:46 -040063 { "kernel-crashes", "/bin/cat /var/spool/crash/kernel.*.kcrash" },
Elly Jones03cd6d72012-06-11 13:04:28 -040064 { "lsmod", "lsmod" },
65 { "lspci", "/usr/sbin/lspci" },
66 { "lsusb", "lsusb" },
67 { "meminfo", "cat /proc/meminfo" },
68 { "memory_spd_info", "/bin/cat /var/log/memory_spd_info.txt" },
Elly Jones03cd6d72012-06-11 13:04:28 -040069 { "power-supply-info", "/usr/bin/power-supply-info" },
70 { "powerd.LATEST", "/bin/cat /var/log/power_manager/powerd.LATEST" },
71 { "powerd.out", "/bin/cat /var/log/power_manager/powerd.out" },
72 { "powerm.LATEST", "/bin/cat /var/log/power_manager/powerm.LATEST" },
73 { "powerm.out", "/bin/cat /var/log/power_manager/powerm.out" },
74 // Changed from 'ps ux' to 'ps aux' since we're running as debugd, not chronos
75 { "ps", "/bin/ps aux" },
76 { "syslog", "/usr/share/userfeedback/scripts/getmsgs --last '2 hours'"
Elly Jonesf8a67ee2012-06-19 13:48:38 -040077 " /var/log/messages" },
Elly Jones03cd6d72012-06-11 13:04:28 -040078 { "touchpad", "/opt/google/touchpad/tpcontrol status" },
Elly Jonesd31aee22012-07-02 11:09:10 -040079 { "touchpad_activity", "/opt/google/touchpad/generate_userfeedback alt" },
Elly Jones03cd6d72012-06-11 13:04:28 -040080 { "ui_log", "/usr/share/userfeedback/scripts/get_log /var/log/ui/ui.LATEST" },
81 { "uname", "/bin/uname -a" },
82 { "update_engine.log", "cat $(ls -1tr /var/log/update_engine | tail -5 | sed"
Elly Jonesd31aee22012-07-02 11:09:10 -040083 " s.^./var/log/update_engine/.)" },
Elly Jones03cd6d72012-06-11 13:04:28 -040084 { "verified boot", "/bin/cat /var/log/debug_vboot_noisy.log" },
85 { "vpd_2.0", "/bin/cat /var/log/vpd_2.0.txt" },
86 { "wifi_status", "/usr/bin/network_diagnostics --wifi --no-log" },
87
Gaurav Shah6e8fd892012-10-01 11:51:08 -070088
89 // Stuff pulled out of the original list because debugd is running under
90 // a VFS namespace and does not have access to later cryptohome mounts.
91 // This includes anything under /home/chronos/user/*
92 // { "chrome_log", "/bin/cat /home/chronos/user/log/chrome" },
93 // { "login-times", "/bin/cat /home/chronos/user/login-times" },
94 // { "logout-times", "/bin/cat /home/chronos/user/logout-times" },
95
Elly Jones03cd6d72012-06-11 13:04:28 -040096 // Stuff pulled out of the original list. These need access to the running X
97 // session, which we'd rather not give to debugd, or return info specific to
98 // the current session (in the setsid(2) sense), which is not useful for
99 // debugd
100 // { "env", "set" },
101 // { "setxkbmap", "/usr/bin/setxkbmap -print -query" },
102 // { "xrandr", "/usr/bin/xrandr --verbose" }
Elly Jones533c7c42012-08-10 15:07:05 -0400103 { NULL, NULL }
104};
105
106static const Log extra_logs[] = {
107 { "mm-status", "/usr/bin/modem status" },
108 { "network-devices", "/usr/bin/connectivity show devices" },
109 { "network-services", "/usr/bin/connectivity show services" },
110 { NULL, NULL }
111};
112
113static struct Log feedback_logs[] = {
114 { "mm-status", "/usr/bin/modem status-feedback" },
115 { "network-devices", "/usr/bin/connectivity show-feedback devices" },
116 { "network-services", "/usr/bin/connectivity show-feedback services" },
117 { NULL, NULL }
Elly Jones03cd6d72012-06-11 13:04:28 -0400118};
119
120LogTool::LogTool() { }
121LogTool::~LogTool() { }
122
Elly Jones533c7c42012-08-10 15:07:05 -0400123bool GetNamedLogFrom(const string& name, const struct Log* logs,
124 string* result) {
125 for (size_t i = 0; logs[i].name; i++) {
126 if (name == logs[i].name) {
127 *result = Run(logs[i].command);
128 return true;
129 }
130 }
131 *result = "<invalid log name>";
132 return false;
Elly Jones03cd6d72012-06-11 13:04:28 -0400133}
134
Elly Jones533c7c42012-08-10 15:07:05 -0400135string LogTool::GetLog(const string& name, DBus::Error& error) { // NOLINT
136 string result;
137 GetNamedLogFrom(name, common_logs, &result)
138 || GetNamedLogFrom(name, extra_logs, &result)
139 || GetNamedLogFrom(name, feedback_logs, &result);
140 return result;
141}
142
143void GetLogsFrom(const struct Log* logs, LogTool::LogMap* map) {
144 for (size_t i = 0; logs[i].name; i++)
145 (*map)[logs[i].name] = Run(logs[i].command);
146}
147
148LogTool::LogMap LogTool::GetAllLogs(DBus::Error& error) { // NOLINT
149 LogMap result;
150 GetLogsFrom(common_logs, &result);
151 GetLogsFrom(extra_logs, &result);
152 return result;
153}
154
155LogTool::LogMap LogTool::GetFeedbackLogs(DBus::Error& error) { // NOLINT
156 LogMap result;
157 GetLogsFrom(common_logs, &result);
158 GetLogsFrom(feedback_logs, &result);
Elly Jones03cd6d72012-06-11 13:04:28 -0400159 return result;
160}
161
162}; // namespace debugd