blob: a5c312bbe6c2b28a29aff25b7d3b1b8ad9aff8b6 [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 Lemeb0022b02016-08-04 12:03:06 -070017#define TRACE_TAG ADB
18
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070019#include "bugreport.h"
20
Felipe Leme218e1ff2016-07-19 17:07:22 -070021#include <string>
Felipe Leme5f6eaca2016-07-29 15:47:00 -070022#include <vector>
Felipe Leme218e1ff2016-07-19 17:07:22 -070023
Colin Cross58021d12017-02-23 21:23:05 -080024#include <android-base/file.h>
Felipe Leme218e1ff2016-07-19 17:07:22 -070025#include <android-base/strings.h>
26
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070027#include "sysdeps.h"
28#include "adb_utils.h"
Felipe Leme218e1ff2016-07-19 17:07:22 -070029#include "file_sync_service.h"
30
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070031static constexpr char BUGZ_BEGIN_PREFIX[] = "BEGIN:";
Felipe Leme97b73a02016-07-26 12:23:40 -070032static constexpr char BUGZ_PROGRESS_PREFIX[] = "PROGRESS:";
33static constexpr char BUGZ_PROGRESS_SEPARATOR[] = "/";
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070034static constexpr char BUGZ_OK_PREFIX[] = "OK:";
35static constexpr char BUGZ_FAIL_PREFIX[] = "FAIL:";
Felipe Leme218e1ff2016-07-19 17:07:22 -070036
Felipe Lemed1885422016-07-26 12:14:39 -070037// Custom callback used to handle the output of zipped bugreports.
38class BugreportStandardStreamsCallback : public StandardStreamsCallbackInterface {
39 public:
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070040 BugreportStandardStreamsCallback(const std::string& dest_dir, const std::string& dest_file,
41 bool show_progress, Bugreport* br)
Felipe Leme33ae8492016-07-27 19:23:09 -070042 : br_(br),
Felipe Leme5f6eaca2016-07-29 15:47:00 -070043 src_file_(),
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070044 dest_dir_(dest_dir),
Felipe Leme33ae8492016-07-27 19:23:09 -070045 dest_file_(dest_file),
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070046 line_message_(),
Felipe Leme5f6eaca2016-07-29 15:47:00 -070047 invalid_lines_(),
Felipe Leme33ae8492016-07-27 19:23:09 -070048 show_progress_(show_progress),
Felipe Leme5f6eaca2016-07-29 15:47:00 -070049 status_(0),
Felipe Leme33ae8492016-07-27 19:23:09 -070050 line_() {
Felipe Leme321c21e2016-08-15 16:01:58 -070051 SetLineMessage("generating");
Felipe Lemed1885422016-07-26 12:14:39 -070052 }
53
54 void OnStdout(const char* buffer, int length) {
Felipe Leme97b73a02016-07-26 12:23:40 -070055 for (int i = 0; i < length; i++) {
56 char c = buffer[i];
57 if (c == '\n') {
58 ProcessLine(line_);
59 line_.clear();
60 } else {
61 line_.append(1, c);
62 }
63 }
Felipe Lemed1885422016-07-26 12:14:39 -070064 }
65
66 void OnStderr(const char* buffer, int length) {
67 OnStream(nullptr, stderr, buffer, length);
68 }
Felipe Lemed1885422016-07-26 12:14:39 -070069 int Done(int unused_) {
Felipe Leme5f6eaca2016-07-29 15:47:00 -070070 // Process remaining line, if any.
Felipe Leme97b73a02016-07-26 12:23:40 -070071 ProcessLine(line_);
Felipe Leme5f6eaca2016-07-29 15:47:00 -070072
73 // Warn about invalid lines, if any.
74 if (!invalid_lines_.empty()) {
75 fprintf(stderr,
76 "WARNING: bugreportz generated %zu line(s) with unknown commands, "
77 "device might not support zipped bugreports:\n",
78 invalid_lines_.size());
79 for (const auto& line : invalid_lines_) {
80 fprintf(stderr, "\t%s\n", line.c_str());
81 }
82 fprintf(stderr,
83 "If the zipped bugreport was not generated, try 'adb bugreport' instead.\n");
84 }
85
86 // Pull the generated bug report.
87 if (status_ == 0) {
88 if (src_file_.empty()) {
89 fprintf(stderr, "bugreportz did not return a '%s' or '%s' line\n", BUGZ_OK_PREFIX,
90 BUGZ_FAIL_PREFIX);
91 return -1;
92 }
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070093 std::string destination;
94 if (dest_dir_.empty()) {
95 destination = dest_file_;
96 } else {
97 destination = android::base::StringPrintf("%s%c%s", dest_dir_.c_str(),
98 OS_PATH_SEPARATOR, dest_file_.c_str());
99 }
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700100 std::vector<const char*> srcs{src_file_.c_str()};
Felipe Leme321c21e2016-08-15 16:01:58 -0700101 SetLineMessage("pulling");
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700102 status_ =
103 br_->DoSyncPull(srcs, destination.c_str(), true, line_message_.c_str()) ? 0 : 1;
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700104 if (status_ != 0) {
105 fprintf(stderr,
106 "Bug report finished but could not be copied to '%s'.\n"
107 "Try to run 'adb pull %s <directory>'\n"
108 "to copy it to a directory that can be written.\n",
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700109 destination.c_str(), src_file_.c_str());
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700110 }
111 }
Felipe Leme97b73a02016-07-26 12:23:40 -0700112 return status_;
Felipe Lemed1885422016-07-26 12:14:39 -0700113 }
114
115 private:
Felipe Leme321c21e2016-08-15 16:01:58 -0700116 void SetLineMessage(const std::string& action) {
Colin Cross58021d12017-02-23 21:23:05 -0800117 line_message_ = action + " " + android::base::Basename(dest_file_);
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700118 }
119
120 void SetSrcFile(const std::string path) {
121 src_file_ = path;
122 if (!dest_dir_.empty()) {
123 // Only uses device-provided name when user passed a directory.
Colin Cross58021d12017-02-23 21:23:05 -0800124 dest_file_ = android::base::Basename(path);
Felipe Leme321c21e2016-08-15 16:01:58 -0700125 SetLineMessage("generating");
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700126 }
127 }
128
Felipe Leme97b73a02016-07-26 12:23:40 -0700129 void ProcessLine(const std::string& line) {
130 if (line.empty()) return;
131
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700132 if (android::base::StartsWith(line, BUGZ_BEGIN_PREFIX)) {
133 SetSrcFile(&line[strlen(BUGZ_BEGIN_PREFIX)]);
134 } else if (android::base::StartsWith(line, BUGZ_OK_PREFIX)) {
135 SetSrcFile(&line[strlen(BUGZ_OK_PREFIX)]);
Felipe Leme97b73a02016-07-26 12:23:40 -0700136 } else if (android::base::StartsWith(line, BUGZ_FAIL_PREFIX)) {
137 const char* error_message = &line[strlen(BUGZ_FAIL_PREFIX)];
138 fprintf(stderr, "Device failed to take a zipped bugreport: %s\n", error_message);
139 status_ = -1;
140 } else if (show_progress_ && android::base::StartsWith(line, BUGZ_PROGRESS_PREFIX)) {
141 // progress_line should have the following format:
142 //
143 // BUGZ_PROGRESS_PREFIX:PROGRESS/TOTAL
144 //
145 size_t idx1 = line.rfind(BUGZ_PROGRESS_PREFIX) + strlen(BUGZ_PROGRESS_PREFIX);
146 size_t idx2 = line.rfind(BUGZ_PROGRESS_SEPARATOR);
147 int progress = std::stoi(line.substr(idx1, (idx2 - idx1)));
148 int total = std::stoi(line.substr(idx2 + 1));
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700149 br_->UpdateProgress(line_message_, progress, total);
Felipe Leme97b73a02016-07-26 12:23:40 -0700150 } else {
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700151 invalid_lines_.push_back(line);
Felipe Leme97b73a02016-07-26 12:23:40 -0700152 }
153 }
154
Felipe Lemed1885422016-07-26 12:14:39 -0700155 Bugreport* br_;
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700156
157 // Path of bugreport on device.
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700158 std::string src_file_;
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700159
160 // Bugreport destination on host, depending on argument passed on constructor:
161 // - if argument is a directory, dest_dir_ is set with it and dest_file_ will be the name
162 // of the bugreport reported by the device.
163 // - if argument is empty, dest_dir is set as the current directory and dest_file_ will be the
164 // name of the bugreport reported by the device.
165 // - otherwise, dest_dir_ is not set and dest_file_ is set with the value passed on constructor.
166 std::string dest_dir_, dest_file_;
167
168 // Message displayed on LinePrinter, it's updated every time the destination above change.
169 std::string line_message_;
170
171 // Lines sent by bugreportz that contain invalid commands; will be displayed at the end.
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700172 std::vector<std::string> invalid_lines_;
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700173
174 // Whether PROGRESS_LINES should be interpreted as progress.
Felipe Leme97b73a02016-07-26 12:23:40 -0700175 bool show_progress_;
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700176
177 // Overall process of the operation, as returned by Done().
Felipe Leme97b73a02016-07-26 12:23:40 -0700178 int status_;
179
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700180 // Temporary buffer containing the characters read since the last newline (\n).
Felipe Leme97b73a02016-07-26 12:23:40 -0700181 std::string line_;
Felipe Lemed1885422016-07-26 12:14:39 -0700182
183 DISALLOW_COPY_AND_ASSIGN(BugreportStandardStreamsCallback);
184};
185
Felipe Leme218e1ff2016-07-19 17:07:22 -0700186int Bugreport::DoIt(TransportType transport_type, const char* serial, int argc, const char** argv) {
Elliott Hughes2ec36b02017-02-06 16:20:30 -0800187 if (argc > 2) return usage("usage: adb bugreport [PATH]");
Felipe Leme97b73a02016-07-26 12:23:40 -0700188
189 // Gets bugreportz version.
Felipe Lemeb0022b02016-08-04 12:03:06 -0700190 std::string bugz_stdout, bugz_stderr;
191 DefaultStandardStreamsCallback version_callback(&bugz_stdout, &bugz_stderr);
Felipe Leme97b73a02016-07-26 12:23:40 -0700192 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);
Felipe Lemeb0022b02016-08-04 12:03:06 -0700194 std::string bugz_output = android::base::Trim(bugz_stdout);
Felipe Leme97b73a02016-07-26 12:23:40 -0700195
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700196 if (status != 0 || bugz_version.empty()) {
Felipe Lemeb0022b02016-08-04 12:03:06 -0700197 D("'bugreportz' -v results: status=%d, stdout='%s', stderr='%s'", status,
198 bugz_output.c_str(), bugz_version.c_str());
199 if (argc == 1) {
200 // Device does not support bugreportz: if called as 'adb bugreport', just falls out to
201 // the flat-file version.
202 fprintf(stderr,
203 "Failed to get bugreportz version, which is only available on devices "
204 "running Android 7.0 or later.\nTrying a plain-text bug report instead.\n");
205 return SendShellCommand(transport_type, serial, "bugreport", false);
206 }
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700207
208 // But if user explicitly asked for a zipped bug report, fails instead (otherwise calling
Felipe Lemeb0022b02016-08-04 12:03:06 -0700209 // 'bugreport' would generate a lot of output the user might not be prepared to handle).
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700210 fprintf(stderr,
211 "Failed to get bugreportz version: 'bugreportz -v' returned '%s' (code %d).\n"
Felipe Lemeb0022b02016-08-04 12:03:06 -0700212 "If the device does not run Android 7.0 or above, try 'adb bugreport' instead.\n",
213 bugz_output.c_str(), status);
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700214 return status != 0 ? status : -1;
215 }
216
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700217 std::string dest_file, dest_dir;
218
219 if (argc == 1) {
220 // No args - use current directory
221 if (!getcwd(&dest_dir)) {
222 perror("adb: getcwd failed");
223 return 1;
224 }
225 } else {
226 // Check whether argument is a directory or file
227 if (directory_exists(argv[1])) {
228 dest_dir = argv[1];
229 } else {
230 dest_file = argv[1];
231 }
232 }
233
234 if (dest_file.empty()) {
235 // Uses a default value until device provides the proper name
236 dest_file = "bugreport.zip";
237 } else {
Elliott Hughes7acce1b2016-10-25 17:24:54 -0700238 if (!android::base::EndsWithIgnoreCase(dest_file, ".zip")) {
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700239 dest_file += ".zip";
240 }
241 }
242
Felipe Leme97b73a02016-07-26 12:23:40 -0700243 bool show_progress = true;
244 std::string bugz_command = "bugreportz -p";
245 if (bugz_version == "1.0") {
246 // 1.0 does not support progress notifications, so print a disclaimer
247 // message instead.
248 fprintf(stderr,
249 "Bugreport is in progress and it could take minutes to complete.\n"
250 "Please be patient and do not cancel or disconnect your device "
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700251 "until it completes.\n");
Felipe Leme97b73a02016-07-26 12:23:40 -0700252 show_progress = false;
253 bugz_command = "bugreportz";
254 }
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700255 BugreportStandardStreamsCallback bugz_callback(dest_dir, dest_file, show_progress, this);
Felipe Leme97b73a02016-07-26 12:23:40 -0700256 return SendShellCommand(transport_type, serial, bugz_command, false, &bugz_callback);
257}
258
Felipe Leme33ae8492016-07-27 19:23:09 -0700259void Bugreport::UpdateProgress(const std::string& message, int progress, int total) {
Felipe Leme97b73a02016-07-26 12:23:40 -0700260 int progress_percentage = (progress * 100 / total);
Felipe Leme33ae8492016-07-27 19:23:09 -0700261 line_printer_.Print(
262 android::base::StringPrintf("[%3d%%] %s", progress_percentage, message.c_str()),
263 LinePrinter::INFO);
Felipe Leme218e1ff2016-07-19 17:07:22 -0700264}
265
266int Bugreport::SendShellCommand(TransportType transport_type, const char* serial,
267 const std::string& command, bool disable_shell_protocol,
Felipe Lemed1885422016-07-26 12:14:39 -0700268 StandardStreamsCallbackInterface* callback) {
269 return send_shell_command(transport_type, serial, command, disable_shell_protocol, callback);
Felipe Leme218e1ff2016-07-19 17:07:22 -0700270}
271
272bool Bugreport::DoSyncPull(const std::vector<const char*>& srcs, const char* dst, bool copy_attrs,
273 const char* name) {
274 return do_sync_pull(srcs, dst, copy_attrs, name);
275}