blob: abef86a680788ac8f70e487676575e79c88db0e3 [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 Lemededcbaa2017-03-20 11:00:11 -070050 line_(),
Felipe Leme4cc03612017-05-02 10:06:33 -070051 last_progress_percentage_(0) {
Felipe Leme321c21e2016-08-15 16:01:58 -070052 SetLineMessage("generating");
Felipe Lemed1885422016-07-26 12:14:39 -070053 }
54
55 void OnStdout(const char* buffer, int length) {
Felipe Leme97b73a02016-07-26 12:23:40 -070056 for (int i = 0; i < length; i++) {
57 char c = buffer[i];
58 if (c == '\n') {
59 ProcessLine(line_);
60 line_.clear();
61 } else {
62 line_.append(1, c);
63 }
64 }
Felipe Lemed1885422016-07-26 12:14:39 -070065 }
66
67 void OnStderr(const char* buffer, int length) {
68 OnStream(nullptr, stderr, buffer, length);
69 }
Felipe Lemededcbaa2017-03-20 11:00:11 -070070
Felipe Lemed1885422016-07-26 12:14:39 -070071 int Done(int unused_) {
Felipe Leme5f6eaca2016-07-29 15:47:00 -070072 // Process remaining line, if any.
Felipe Leme97b73a02016-07-26 12:23:40 -070073 ProcessLine(line_);
Felipe Leme5f6eaca2016-07-29 15:47:00 -070074
75 // Warn about invalid lines, if any.
76 if (!invalid_lines_.empty()) {
77 fprintf(stderr,
78 "WARNING: bugreportz generated %zu line(s) with unknown commands, "
79 "device might not support zipped bugreports:\n",
80 invalid_lines_.size());
81 for (const auto& line : invalid_lines_) {
82 fprintf(stderr, "\t%s\n", line.c_str());
83 }
84 fprintf(stderr,
85 "If the zipped bugreport was not generated, try 'adb bugreport' instead.\n");
86 }
87
88 // Pull the generated bug report.
89 if (status_ == 0) {
90 if (src_file_.empty()) {
91 fprintf(stderr, "bugreportz did not return a '%s' or '%s' line\n", BUGZ_OK_PREFIX,
92 BUGZ_FAIL_PREFIX);
93 return -1;
94 }
Felipe Lemef8d9e4e2016-07-29 17:57:00 -070095 std::string destination;
96 if (dest_dir_.empty()) {
97 destination = dest_file_;
98 } else {
99 destination = android::base::StringPrintf("%s%c%s", dest_dir_.c_str(),
100 OS_PATH_SEPARATOR, dest_file_.c_str());
101 }
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700102 std::vector<const char*> srcs{src_file_.c_str()};
Felipe Leme321c21e2016-08-15 16:01:58 -0700103 SetLineMessage("pulling");
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700104 status_ =
Felipe Leme4bfddbd2017-08-15 18:09:54 -0700105 br_->DoSyncPull(srcs, destination.c_str(), false, line_message_.c_str()) ? 0 : 1;
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700106 if (status_ != 0) {
107 fprintf(stderr,
108 "Bug report finished but could not be copied to '%s'.\n"
109 "Try to run 'adb pull %s <directory>'\n"
110 "to copy it to a directory that can be written.\n",
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700111 destination.c_str(), src_file_.c_str());
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700112 }
113 }
Felipe Leme97b73a02016-07-26 12:23:40 -0700114 return status_;
Felipe Lemed1885422016-07-26 12:14:39 -0700115 }
116
117 private:
Felipe Leme321c21e2016-08-15 16:01:58 -0700118 void SetLineMessage(const std::string& action) {
Colin Cross58021d12017-02-23 21:23:05 -0800119 line_message_ = action + " " + android::base::Basename(dest_file_);
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700120 }
121
122 void SetSrcFile(const std::string path) {
123 src_file_ = path;
124 if (!dest_dir_.empty()) {
125 // Only uses device-provided name when user passed a directory.
Colin Cross58021d12017-02-23 21:23:05 -0800126 dest_file_ = android::base::Basename(path);
Felipe Leme321c21e2016-08-15 16:01:58 -0700127 SetLineMessage("generating");
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700128 }
129 }
130
Felipe Leme97b73a02016-07-26 12:23:40 -0700131 void ProcessLine(const std::string& line) {
132 if (line.empty()) return;
133
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700134 if (android::base::StartsWith(line, BUGZ_BEGIN_PREFIX)) {
135 SetSrcFile(&line[strlen(BUGZ_BEGIN_PREFIX)]);
136 } else if (android::base::StartsWith(line, BUGZ_OK_PREFIX)) {
137 SetSrcFile(&line[strlen(BUGZ_OK_PREFIX)]);
Felipe Leme97b73a02016-07-26 12:23:40 -0700138 } else if (android::base::StartsWith(line, BUGZ_FAIL_PREFIX)) {
139 const char* error_message = &line[strlen(BUGZ_FAIL_PREFIX)];
Elliott Hughes1fc8f6e2017-04-18 14:34:16 -0700140 fprintf(stderr, "adb: device failed to take a zipped bugreport: %s\n", error_message);
Felipe Leme97b73a02016-07-26 12:23:40 -0700141 status_ = -1;
142 } else if (show_progress_ && android::base::StartsWith(line, BUGZ_PROGRESS_PREFIX)) {
143 // progress_line should have the following format:
144 //
145 // BUGZ_PROGRESS_PREFIX:PROGRESS/TOTAL
146 //
147 size_t idx1 = line.rfind(BUGZ_PROGRESS_PREFIX) + strlen(BUGZ_PROGRESS_PREFIX);
148 size_t idx2 = line.rfind(BUGZ_PROGRESS_SEPARATOR);
149 int progress = std::stoi(line.substr(idx1, (idx2 - idx1)));
Felipe Leme4cc03612017-05-02 10:06:33 -0700150 int total = std::stoi(line.substr(idx2 + 1));
151 int progress_percentage = (progress * 100 / total);
Felipe Lemee5d665b2017-05-23 16:56:47 -0700152 if (progress_percentage != 0 && progress_percentage <= last_progress_percentage_) {
Felipe Lemededcbaa2017-03-20 11:00:11 -0700153 // Ignore.
154 return;
155 }
Felipe Leme4cc03612017-05-02 10:06:33 -0700156 last_progress_percentage_ = progress_percentage;
157 br_->UpdateProgress(line_message_, progress_percentage);
Felipe Leme97b73a02016-07-26 12:23:40 -0700158 } else {
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700159 invalid_lines_.push_back(line);
Felipe Leme97b73a02016-07-26 12:23:40 -0700160 }
161 }
162
Felipe Lemed1885422016-07-26 12:14:39 -0700163 Bugreport* br_;
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700164
165 // Path of bugreport on device.
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700166 std::string src_file_;
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700167
168 // Bugreport destination on host, depending on argument passed on constructor:
169 // - if argument is a directory, dest_dir_ is set with it and dest_file_ will be the name
170 // of the bugreport reported by the device.
171 // - if argument is empty, dest_dir is set as the current directory and dest_file_ will be the
172 // name of the bugreport reported by the device.
173 // - otherwise, dest_dir_ is not set and dest_file_ is set with the value passed on constructor.
174 std::string dest_dir_, dest_file_;
175
176 // Message displayed on LinePrinter, it's updated every time the destination above change.
177 std::string line_message_;
178
179 // Lines sent by bugreportz that contain invalid commands; will be displayed at the end.
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700180 std::vector<std::string> invalid_lines_;
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700181
182 // Whether PROGRESS_LINES should be interpreted as progress.
Felipe Leme97b73a02016-07-26 12:23:40 -0700183 bool show_progress_;
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700184
185 // Overall process of the operation, as returned by Done().
Felipe Leme97b73a02016-07-26 12:23:40 -0700186 int status_;
187
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700188 // Temporary buffer containing the characters read since the last newline (\n).
Felipe Leme97b73a02016-07-26 12:23:40 -0700189 std::string line_;
Felipe Lemed1885422016-07-26 12:14:39 -0700190
Felipe Lemededcbaa2017-03-20 11:00:11 -0700191 // Last displayed progress.
192 // Since dumpstate progress can recede, only forward progress should be displayed
Felipe Leme4cc03612017-05-02 10:06:33 -0700193 int last_progress_percentage_;
Felipe Lemededcbaa2017-03-20 11:00:11 -0700194
Felipe Lemed1885422016-07-26 12:14:39 -0700195 DISALLOW_COPY_AND_ASSIGN(BugreportStandardStreamsCallback);
196};
197
Josh Gaob122b172017-08-16 16:57:01 -0700198int Bugreport::DoIt(int argc, const char** argv) {
Elliott Hughes1fc8f6e2017-04-18 14:34:16 -0700199 if (argc > 2) return syntax_error("adb bugreport [PATH]");
Felipe Leme97b73a02016-07-26 12:23:40 -0700200
201 // Gets bugreportz version.
Felipe Lemeb0022b02016-08-04 12:03:06 -0700202 std::string bugz_stdout, bugz_stderr;
203 DefaultStandardStreamsCallback version_callback(&bugz_stdout, &bugz_stderr);
Josh Gaob122b172017-08-16 16:57:01 -0700204 int status = SendShellCommand("bugreportz -v", false, &version_callback);
Felipe Leme97b73a02016-07-26 12:23:40 -0700205 std::string bugz_version = android::base::Trim(bugz_stderr);
Felipe Lemeb0022b02016-08-04 12:03:06 -0700206 std::string bugz_output = android::base::Trim(bugz_stdout);
Felipe Leme97b73a02016-07-26 12:23:40 -0700207
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700208 if (status != 0 || bugz_version.empty()) {
Felipe Lemeb0022b02016-08-04 12:03:06 -0700209 D("'bugreportz' -v results: status=%d, stdout='%s', stderr='%s'", status,
210 bugz_output.c_str(), bugz_version.c_str());
211 if (argc == 1) {
212 // Device does not support bugreportz: if called as 'adb bugreport', just falls out to
213 // the flat-file version.
214 fprintf(stderr,
215 "Failed to get bugreportz version, which is only available on devices "
216 "running Android 7.0 or later.\nTrying a plain-text bug report instead.\n");
Josh Gaob122b172017-08-16 16:57:01 -0700217 return SendShellCommand("bugreport", false);
Felipe Lemeb0022b02016-08-04 12:03:06 -0700218 }
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700219
220 // But if user explicitly asked for a zipped bug report, fails instead (otherwise calling
Felipe Lemeb0022b02016-08-04 12:03:06 -0700221 // 'bugreport' would generate a lot of output the user might not be prepared to handle).
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700222 fprintf(stderr,
223 "Failed to get bugreportz version: 'bugreportz -v' returned '%s' (code %d).\n"
Felipe Lemeb0022b02016-08-04 12:03:06 -0700224 "If the device does not run Android 7.0 or above, try 'adb bugreport' instead.\n",
225 bugz_output.c_str(), status);
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700226 return status != 0 ? status : -1;
227 }
228
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700229 std::string dest_file, dest_dir;
230
231 if (argc == 1) {
232 // No args - use current directory
233 if (!getcwd(&dest_dir)) {
234 perror("adb: getcwd failed");
235 return 1;
236 }
237 } else {
238 // Check whether argument is a directory or file
239 if (directory_exists(argv[1])) {
240 dest_dir = argv[1];
241 } else {
242 dest_file = argv[1];
243 }
244 }
245
246 if (dest_file.empty()) {
247 // Uses a default value until device provides the proper name
248 dest_file = "bugreport.zip";
249 } else {
Elliott Hughes7acce1b2016-10-25 17:24:54 -0700250 if (!android::base::EndsWithIgnoreCase(dest_file, ".zip")) {
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700251 dest_file += ".zip";
252 }
253 }
254
Felipe Leme97b73a02016-07-26 12:23:40 -0700255 bool show_progress = true;
256 std::string bugz_command = "bugreportz -p";
257 if (bugz_version == "1.0") {
258 // 1.0 does not support progress notifications, so print a disclaimer
259 // message instead.
260 fprintf(stderr,
261 "Bugreport is in progress and it could take minutes to complete.\n"
262 "Please be patient and do not cancel or disconnect your device "
Felipe Leme5f6eaca2016-07-29 15:47:00 -0700263 "until it completes.\n");
Felipe Leme97b73a02016-07-26 12:23:40 -0700264 show_progress = false;
265 bugz_command = "bugreportz";
266 }
Felipe Lemef8d9e4e2016-07-29 17:57:00 -0700267 BugreportStandardStreamsCallback bugz_callback(dest_dir, dest_file, show_progress, this);
Josh Gaob122b172017-08-16 16:57:01 -0700268 return SendShellCommand(bugz_command, false, &bugz_callback);
Felipe Leme97b73a02016-07-26 12:23:40 -0700269}
270
Felipe Leme4cc03612017-05-02 10:06:33 -0700271void Bugreport::UpdateProgress(const std::string& message, int progress_percentage) {
Felipe Leme33ae8492016-07-27 19:23:09 -0700272 line_printer_.Print(
273 android::base::StringPrintf("[%3d%%] %s", progress_percentage, message.c_str()),
274 LinePrinter::INFO);
Felipe Leme218e1ff2016-07-19 17:07:22 -0700275}
276
Josh Gaob122b172017-08-16 16:57:01 -0700277int Bugreport::SendShellCommand(const std::string& command, bool disable_shell_protocol,
Felipe Lemed1885422016-07-26 12:14:39 -0700278 StandardStreamsCallbackInterface* callback) {
Josh Gaob122b172017-08-16 16:57:01 -0700279 return send_shell_command(command, disable_shell_protocol, callback);
Felipe Leme218e1ff2016-07-19 17:07:22 -0700280}
281
282bool Bugreport::DoSyncPull(const std::vector<const char*>& srcs, const char* dst, bool copy_attrs,
283 const char* name) {
284 return do_sync_pull(srcs, dst, copy_attrs, name);
285}