blob: 213fa2e50a411fe288468c53cb85e5720fba00ec [file] [log] [blame]
Felipe Leme218e1ff2016-07-19 17:07:22 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070017#include "bugreport.h"
18
Felipe Leme218e1ff2016-07-19 17:07:22 -070019#include <string>
Felipe Leme5f6eaca2016-07-29 15:47:00 -070020#include <vector>
Felipe Leme218e1ff2016-07-19 17:07:22 -070021
22#include <android-base/strings.h>
23
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070024#include "sysdeps.h"
25#include "adb_utils.h"
Felipe Leme218e1ff2016-07-19 17:07:22 -070026#include "file_sync_service.h"
27
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070028static constexpr char BUGZ_BEGIN_PREFIX[] = "BEGIN:";
Felipe Leme97b73a02016-07-26 12:23:40 -070029static constexpr char BUGZ_PROGRESS_PREFIX[] = "PROGRESS:";
30static constexpr char BUGZ_PROGRESS_SEPARATOR[] = "/";
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070031static constexpr char BUGZ_OK_PREFIX[] = "OK:";
32static constexpr char BUGZ_FAIL_PREFIX[] = "FAIL:";
Felipe Leme218e1ff2016-07-19 17:07:22 -070033
Felipe Lemed1885422016-07-26 12:14:39 -070034// Custom callback used to handle the output of zipped bugreports.
35class BugreportStandardStreamsCallback : public StandardStreamsCallbackInterface {
36 public:
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070037 BugreportStandardStreamsCallback(const std::string& dest_dir, const std::string& dest_file,
38 bool show_progress, Bugreport* br)
Felipe Leme33ae8492016-07-27 19:23:09 -070039 : br_(br),
Felipe Leme5f6eaca2016-07-29 15:47:00 -070040 src_file_(),
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070041 dest_dir_(dest_dir),
Felipe Leme33ae8492016-07-27 19:23:09 -070042 dest_file_(dest_file),
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070043 line_message_(),
Felipe Leme5f6eaca2016-07-29 15:47:00 -070044 invalid_lines_(),
Felipe Leme33ae8492016-07-27 19:23:09 -070045 show_progress_(show_progress),
Felipe Leme5f6eaca2016-07-29 15:47:00 -070046 status_(0),
Felipe Leme33ae8492016-07-27 19:23:09 -070047 line_() {
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070048 SetLineMessage();
Felipe Lemed1885422016-07-26 12:14:39 -070049 }
50
51 void OnStdout(const char* buffer, int length) {
Felipe Leme97b73a02016-07-26 12:23:40 -070052 for (int i = 0; i < length; i++) {
53 char c = buffer[i];
54 if (c == '\n') {
55 ProcessLine(line_);
56 line_.clear();
57 } else {
58 line_.append(1, c);
59 }
60 }
Felipe Lemed1885422016-07-26 12:14:39 -070061 }
62
63 void OnStderr(const char* buffer, int length) {
64 OnStream(nullptr, stderr, buffer, length);
65 }
Felipe Lemed1885422016-07-26 12:14:39 -070066 int Done(int unused_) {
Felipe Leme5f6eaca2016-07-29 15:47:00 -070067 // Process remaining line, if any.
Felipe Leme97b73a02016-07-26 12:23:40 -070068 ProcessLine(line_);
Felipe Leme5f6eaca2016-07-29 15:47:00 -070069
70 // Warn about invalid lines, if any.
71 if (!invalid_lines_.empty()) {
72 fprintf(stderr,
73 "WARNING: bugreportz generated %zu line(s) with unknown commands, "
74 "device might not support zipped bugreports:\n",
75 invalid_lines_.size());
76 for (const auto& line : invalid_lines_) {
77 fprintf(stderr, "\t%s\n", line.c_str());
78 }
79 fprintf(stderr,
80 "If the zipped bugreport was not generated, try 'adb bugreport' instead.\n");
81 }
82
83 // Pull the generated bug report.
84 if (status_ == 0) {
85 if (src_file_.empty()) {
86 fprintf(stderr, "bugreportz did not return a '%s' or '%s' line\n", BUGZ_OK_PREFIX,
87 BUGZ_FAIL_PREFIX);
88 return -1;
89 }
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070090 std::string destination;
91 if (dest_dir_.empty()) {
92 destination = dest_file_;
93 } else {
94 destination = android::base::StringPrintf("%s%c%s", dest_dir_.c_str(),
95 OS_PATH_SEPARATOR, dest_file_.c_str());
96 }
Felipe Leme5f6eaca2016-07-29 15:47:00 -070097 std::vector<const char*> srcs{src_file_.c_str()};
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070098 status_ =
99 br_->DoSyncPull(srcs, destination.c_str(), true, line_message_.c_str()) ? 0 : 1;
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700100 if (status_ != 0) {
101 fprintf(stderr,
102 "Bug report finished but could not be copied to '%s'.\n"
103 "Try to run 'adb pull %s <directory>'\n"
104 "to copy it to a directory that can be written.\n",
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700105 destination.c_str(), src_file_.c_str());
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700106 }
107 }
Felipe Leme97b73a02016-07-26 12:23:40 -0700108 return status_;
Felipe Lemed1885422016-07-26 12:14:39 -0700109 }
110
111 private:
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700112 void SetLineMessage() {
113 line_message_ =
114 android::base::StringPrintf("generating %s", adb_basename(dest_file_).c_str());
115 }
116
117 void SetSrcFile(const std::string path) {
118 src_file_ = path;
119 if (!dest_dir_.empty()) {
120 // Only uses device-provided name when user passed a directory.
121 dest_file_ = adb_basename(path);
122 SetLineMessage();
123 }
124 }
125
Felipe Leme97b73a02016-07-26 12:23:40 -0700126 void ProcessLine(const std::string& line) {
127 if (line.empty()) return;
128
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700129 if (android::base::StartsWith(line, BUGZ_BEGIN_PREFIX)) {
130 SetSrcFile(&line[strlen(BUGZ_BEGIN_PREFIX)]);
131 } else if (android::base::StartsWith(line, BUGZ_OK_PREFIX)) {
132 SetSrcFile(&line[strlen(BUGZ_OK_PREFIX)]);
Felipe Leme97b73a02016-07-26 12:23:40 -0700133 } else if (android::base::StartsWith(line, BUGZ_FAIL_PREFIX)) {
134 const char* error_message = &line[strlen(BUGZ_FAIL_PREFIX)];
135 fprintf(stderr, "Device failed to take a zipped bugreport: %s\n", error_message);
136 status_ = -1;
137 } else if (show_progress_ && android::base::StartsWith(line, BUGZ_PROGRESS_PREFIX)) {
138 // progress_line should have the following format:
139 //
140 // BUGZ_PROGRESS_PREFIX:PROGRESS/TOTAL
141 //
142 size_t idx1 = line.rfind(BUGZ_PROGRESS_PREFIX) + strlen(BUGZ_PROGRESS_PREFIX);
143 size_t idx2 = line.rfind(BUGZ_PROGRESS_SEPARATOR);
144 int progress = std::stoi(line.substr(idx1, (idx2 - idx1)));
145 int total = std::stoi(line.substr(idx2 + 1));
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700146 br_->UpdateProgress(line_message_, progress, total);
Felipe Leme97b73a02016-07-26 12:23:40 -0700147 } else {
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700148 invalid_lines_.push_back(line);
Felipe Leme97b73a02016-07-26 12:23:40 -0700149 }
150 }
151
Felipe Lemed1885422016-07-26 12:14:39 -0700152 Bugreport* br_;
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700153
154 // Path of bugreport on device.
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700155 std::string src_file_;
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700156
157 // Bugreport destination on host, depending on argument passed on constructor:
158 // - if argument is a directory, dest_dir_ is set with it and dest_file_ will be the name
159 // of the bugreport reported by the device.
160 // - if argument is empty, dest_dir is set as the current directory and dest_file_ will be the
161 // name of the bugreport reported by the device.
162 // - otherwise, dest_dir_ is not set and dest_file_ is set with the value passed on constructor.
163 std::string dest_dir_, dest_file_;
164
165 // Message displayed on LinePrinter, it's updated every time the destination above change.
166 std::string line_message_;
167
168 // Lines sent by bugreportz that contain invalid commands; will be displayed at the end.
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700169 std::vector<std::string> invalid_lines_;
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700170
171 // Whether PROGRESS_LINES should be interpreted as progress.
Felipe Leme97b73a02016-07-26 12:23:40 -0700172 bool show_progress_;
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700173
174 // Overall process of the operation, as returned by Done().
Felipe Leme97b73a02016-07-26 12:23:40 -0700175 int status_;
176
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700177 // Temporary buffer containing the characters read since the last newline (\n).
Felipe Leme97b73a02016-07-26 12:23:40 -0700178 std::string line_;
Felipe Lemed1885422016-07-26 12:14:39 -0700179
180 DISALLOW_COPY_AND_ASSIGN(BugreportStandardStreamsCallback);
181};
182
Felipe Leme97b73a02016-07-26 12:23:40 -0700183// Implemented in commandline.cpp
184int usage();
185
Felipe Leme218e1ff2016-07-19 17:07:22 -0700186int Bugreport::DoIt(TransportType transport_type, const char* serial, int argc, const char** argv) {
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700187 if (argc > 2) return usage();
Felipe Leme97b73a02016-07-26 12:23:40 -0700188
189 // Gets bugreportz version.
190 std::string bugz_stderr;
191 DefaultStandardStreamsCallback version_callback(nullptr, &bugz_stderr);
192 int status = SendShellCommand(transport_type, serial, "bugreportz -v", false, &version_callback);
Felipe Leme97b73a02016-07-26 12:23:40 -0700193 std::string bugz_version = android::base::Trim(bugz_stderr);
194
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700195 if (status != 0 || bugz_version.empty()) {
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700196 // Device does not support bugreportz: if called as 'adb bugreport', just falls out to the
197 // flat-file version
198 if (argc == 1) return SendShellCommand(transport_type, serial, "bugreport", false);
199
200 // But if user explicitly asked for a zipped bug report, fails instead (otherwise calling
201 // 'bugreport' would generate a lot of output the user might not be prepared to handle)
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700202 fprintf(stderr,
203 "Failed to get bugreportz version: 'bugreportz -v' returned '%s' (code %d).\n"
204 "If the device runs Android M or below, try 'adb bugreport' instead.\n",
205 bugz_stderr.c_str(), status);
206 return status != 0 ? status : -1;
207 }
208
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700209 std::string dest_file, dest_dir;
210
211 if (argc == 1) {
212 // No args - use current directory
213 if (!getcwd(&dest_dir)) {
214 perror("adb: getcwd failed");
215 return 1;
216 }
217 } else {
218 // Check whether argument is a directory or file
219 if (directory_exists(argv[1])) {
220 dest_dir = argv[1];
221 } else {
222 dest_file = argv[1];
223 }
224 }
225
226 if (dest_file.empty()) {
227 // Uses a default value until device provides the proper name
228 dest_file = "bugreport.zip";
229 } else {
230 if (!android::base::EndsWith(dest_file, ".zip")) {
231 // TODO: use a case-insensitive comparison (like EndsWithIgnoreCase
232 dest_file += ".zip";
233 }
234 }
235
Felipe Leme97b73a02016-07-26 12:23:40 -0700236 bool show_progress = true;
237 std::string bugz_command = "bugreportz -p";
238 if (bugz_version == "1.0") {
239 // 1.0 does not support progress notifications, so print a disclaimer
240 // message instead.
241 fprintf(stderr,
242 "Bugreport is in progress and it could take minutes to complete.\n"
243 "Please be patient and do not cancel or disconnect your device "
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700244 "until it completes.\n");
Felipe Leme97b73a02016-07-26 12:23:40 -0700245 show_progress = false;
246 bugz_command = "bugreportz";
247 }
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700248 BugreportStandardStreamsCallback bugz_callback(dest_dir, dest_file, show_progress, this);
Felipe Leme97b73a02016-07-26 12:23:40 -0700249 return SendShellCommand(transport_type, serial, bugz_command, false, &bugz_callback);
250}
251
Felipe Leme33ae8492016-07-27 19:23:09 -0700252void Bugreport::UpdateProgress(const std::string& message, int progress, int total) {
Felipe Leme97b73a02016-07-26 12:23:40 -0700253 int progress_percentage = (progress * 100 / total);
Felipe Leme33ae8492016-07-27 19:23:09 -0700254 line_printer_.Print(
255 android::base::StringPrintf("[%3d%%] %s", progress_percentage, message.c_str()),
256 LinePrinter::INFO);
Felipe Leme218e1ff2016-07-19 17:07:22 -0700257}
258
259int Bugreport::SendShellCommand(TransportType transport_type, const char* serial,
260 const std::string& command, bool disable_shell_protocol,
Felipe Lemed1885422016-07-26 12:14:39 -0700261 StandardStreamsCallbackInterface* callback) {
262 return send_shell_command(transport_type, serial, command, disable_shell_protocol, callback);
Felipe Leme218e1ff2016-07-19 17:07:22 -0700263}
264
265bool Bugreport::DoSyncPull(const std::vector<const char*>& srcs, const char* dst, bool copy_attrs,
266 const char* name) {
267 return do_sync_pull(srcs, dst, copy_attrs, name);
268}