Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 1 | // 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 Chan | f6cd93a | 2012-10-14 19:37:00 -0700 | [diff] [blame^] | 7 | #include <glib.h> |
| 8 | |
| 9 | #include <base/base64.h> |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 10 | #include <base/file_util.h> |
| 11 | #include <base/logging.h> |
| 12 | #include <base/string_split.h> |
Ben Chan | f6cd93a | 2012-10-14 19:37:00 -0700 | [diff] [blame^] | 13 | #include <base/string_util.h> |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 14 | |
| 15 | #include "process_with_output.h" |
| 16 | |
| 17 | namespace debugd { |
| 18 | |
| 19 | const char *kShell = "/bin/sh"; |
| 20 | |
| 21 | using std::string; |
| 22 | using std::vector; |
| 23 | |
| 24 | typedef vector<string> Strings; |
| 25 | |
Ben Chan | f6cd93a | 2012-10-14 19:37:00 -0700 | [diff] [blame^] | 26 | // Returns |value| if |value| is a valid UTF-8 string or a base64-encoded |
| 27 | // string of |value| otherwise. |
| 28 | string 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-Jones | 57f018c | 2012-10-08 14:22:01 -0400 | [diff] [blame] | 43 | // TODO(ellyjones): sandbox. crosbug.com/35122 |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 44 | string Run(const string& cmdline) { |
| 45 | string output; |
| 46 | ProcessWithOutput p; |
Elly Jones | e0518ff | 2012-08-13 13:22:46 -0400 | [diff] [blame] | 47 | string tailed_cmdline = cmdline + " | tail -c 256K"; |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 48 | 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 Chan | f6cd93a | 2012-10-14 19:37:00 -0700 | [diff] [blame^] | 57 | return EnsureUTF8String(output); |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | struct Log { |
| 61 | const char *name; |
| 62 | const char *command; |
| 63 | }; |
| 64 | |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 65 | static const Log common_logs[] = { |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 66 | { "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 Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 69 | { "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 Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 73 | { "chrome_system_log", "/bin/cat /var/log/chrome/chrome" }, |
Elly Fong-Jones | 57f018c | 2012-10-08 14:22:01 -0400 | [diff] [blame] | 74 | { "console-ramoops", "/bin/cat /dev/pstore/console-ramoops" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 75 | { "cpu", "/usr/bin/uname -p" }, |
| 76 | { "cpuinfo", "/bin/cat /proc/cpuinfo" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 77 | { "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 Jones | 2bde94d | 2012-07-31 18:07:46 -0400 | [diff] [blame] | 84 | { "kernel-crashes", "/bin/cat /var/spool/crash/kernel.*.kcrash" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 85 | { "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 Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 90 | { "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 Jones | f8a67ee | 2012-06-19 13:48:38 -0400 | [diff] [blame] | 98 | " /var/log/messages" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 99 | { "touchpad", "/opt/google/touchpad/tpcontrol status" }, |
Elly Jones | d31aee2 | 2012-07-02 11:09:10 -0400 | [diff] [blame] | 100 | { "touchpad_activity", "/opt/google/touchpad/generate_userfeedback alt" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 101 | { "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 Jones | d31aee2 | 2012-07-02 11:09:10 -0400 | [diff] [blame] | 104 | " s.^./var/log/update_engine/.)" }, |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 105 | { "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 Shah | 6e8fd89 | 2012-10-01 11:51:08 -0700 | [diff] [blame] | 109 | |
| 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 Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 117 | // 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 Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 124 | { NULL, NULL } |
| 125 | }; |
| 126 | |
| 127 | static 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 | |
| 134 | static 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 Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | LogTool::LogTool() { } |
| 142 | LogTool::~LogTool() { } |
| 143 | |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 144 | bool 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 Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 156 | string 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 | |
| 164 | void 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 | |
| 169 | LogTool::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 | |
| 176 | LogTool::LogMap LogTool::GetFeedbackLogs(DBus::Error& error) { // NOLINT |
| 177 | LogMap result; |
| 178 | GetLogsFrom(common_logs, &result); |
| 179 | GetLogsFrom(feedback_logs, &result); |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 180 | return result; |
| 181 | } |
| 182 | |
| 183 | }; // namespace debugd |