blob: 143c62a34ff6b27c774e6c69125380ffe74dd1a4 [file] [log] [blame]
Felipe Leme698e0652016-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 Lemef33fcb62016-08-04 12:03:06 -070017#define TRACE_TAG ADB
18
Felipe Lemeb3239722016-07-29 17:57:00 -070019#include "bugreport.h"
20
Felipe Leme698e0652016-07-19 17:07:22 -070021#include <string>
Felipe Leme6e286962016-07-29 15:47:00 -070022#include <vector>
Felipe Leme698e0652016-07-19 17:07:22 -070023
Elliott Hughesda46b392016-10-11 17:09:00 -070024#include <android-base/parseint.h>
Felipe Leme698e0652016-07-19 17:07:22 -070025#include <android-base/strings.h>
26
Felipe Lemeb3239722016-07-29 17:57:00 -070027#include "sysdeps.h"
28#include "adb_utils.h"
Felipe Leme698e0652016-07-19 17:07:22 -070029#include "file_sync_service.h"
30
Felipe Lemeb3239722016-07-29 17:57:00 -070031static constexpr char BUGZ_BEGIN_PREFIX[] = "BEGIN:";
Felipe Leme6f5080f2016-07-26 12:23:40 -070032static constexpr char BUGZ_PROGRESS_PREFIX[] = "PROGRESS:";
33static constexpr char BUGZ_PROGRESS_SEPARATOR[] = "/";
Felipe Lemeb3239722016-07-29 17:57:00 -070034static constexpr char BUGZ_OK_PREFIX[] = "OK:";
35static constexpr char BUGZ_FAIL_PREFIX[] = "FAIL:";
Felipe Leme698e0652016-07-19 17:07:22 -070036
Felipe Leme0d4f0502016-07-26 12:14:39 -070037// Custom callback used to handle the output of zipped bugreports.
38class BugreportStandardStreamsCallback : public StandardStreamsCallbackInterface {
39 public:
Felipe Lemeb3239722016-07-29 17:57:00 -070040 BugreportStandardStreamsCallback(const std::string& dest_dir, const std::string& dest_file,
41 bool show_progress, Bugreport* br)
Felipe Lemedaf46282016-07-27 19:23:09 -070042 : br_(br),
Felipe Leme6e286962016-07-29 15:47:00 -070043 src_file_(),
Felipe Lemeb3239722016-07-29 17:57:00 -070044 dest_dir_(dest_dir),
Felipe Lemedaf46282016-07-27 19:23:09 -070045 dest_file_(dest_file),
Felipe Lemeb3239722016-07-29 17:57:00 -070046 line_message_(),
Felipe Leme6e286962016-07-29 15:47:00 -070047 invalid_lines_(),
Felipe Lemedaf46282016-07-27 19:23:09 -070048 show_progress_(show_progress),
Felipe Leme6e286962016-07-29 15:47:00 -070049 status_(0),
Felipe Lemedaf46282016-07-27 19:23:09 -070050 line_() {
Felipe Leme680f4a42016-08-15 16:01:58 -070051 SetLineMessage("generating");
Felipe Leme0d4f0502016-07-26 12:14:39 -070052 }
53
54 void OnStdout(const char* buffer, int length) {
Felipe Leme6f5080f2016-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 Leme0d4f0502016-07-26 12:14:39 -070064 }
65
66 void OnStderr(const char* buffer, int length) {
67 OnStream(nullptr, stderr, buffer, length);
68 }
Felipe Leme0d4f0502016-07-26 12:14:39 -070069 int Done(int unused_) {
Felipe Leme6e286962016-07-29 15:47:00 -070070 // Process remaining line, if any.
Felipe Leme6f5080f2016-07-26 12:23:40 -070071 ProcessLine(line_);
Felipe Leme6e286962016-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 Lemeb3239722016-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 Leme6e286962016-07-29 15:47:00 -0700100 std::vector<const char*> srcs{src_file_.c_str()};
Felipe Leme680f4a42016-08-15 16:01:58 -0700101 SetLineMessage("pulling");
Felipe Lemeb3239722016-07-29 17:57:00 -0700102 status_ =
103 br_->DoSyncPull(srcs, destination.c_str(), true, line_message_.c_str()) ? 0 : 1;
Felipe Leme6e286962016-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 Lemeb3239722016-07-29 17:57:00 -0700109 destination.c_str(), src_file_.c_str());
Felipe Leme6e286962016-07-29 15:47:00 -0700110 }
111 }
Felipe Leme6f5080f2016-07-26 12:23:40 -0700112 return status_;
Felipe Leme0d4f0502016-07-26 12:14:39 -0700113 }
114
115 private:
Felipe Leme680f4a42016-08-15 16:01:58 -0700116 void SetLineMessage(const std::string& action) {
117 line_message_ = action + " " + adb_basename(dest_file_);
Felipe Lemeb3239722016-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.
124 dest_file_ = adb_basename(path);
Felipe Leme680f4a42016-08-15 16:01:58 -0700125 SetLineMessage("generating");
Felipe Lemeb3239722016-07-29 17:57:00 -0700126 }
127 }
128
Felipe Leme6f5080f2016-07-26 12:23:40 -0700129 void ProcessLine(const std::string& line) {
130 if (line.empty()) return;
131
Felipe Lemeb3239722016-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 Leme6f5080f2016-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);
Elliott Hughesda46b392016-10-11 17:09:00 -0700147 int progress, total;
148 if (android::base::ParseInt(line.substr(idx1, (idx2 - idx1)), &progress) &&
149 android::base::ParseInt(line.substr(idx2 + 1), &total)) {
150 br_->UpdateProgress(line_message_, progress, total);
151 }
Felipe Leme6f5080f2016-07-26 12:23:40 -0700152 } else {
Felipe Leme6e286962016-07-29 15:47:00 -0700153 invalid_lines_.push_back(line);
Felipe Leme6f5080f2016-07-26 12:23:40 -0700154 }
155 }
156
Felipe Leme0d4f0502016-07-26 12:14:39 -0700157 Bugreport* br_;
Felipe Lemeb3239722016-07-29 17:57:00 -0700158
159 // Path of bugreport on device.
Felipe Leme6e286962016-07-29 15:47:00 -0700160 std::string src_file_;
Felipe Lemeb3239722016-07-29 17:57:00 -0700161
162 // Bugreport destination on host, depending on argument passed on constructor:
163 // - if argument is a directory, dest_dir_ is set with it and dest_file_ will be the name
164 // of the bugreport reported by the device.
165 // - if argument is empty, dest_dir is set as the current directory and dest_file_ will be the
166 // name of the bugreport reported by the device.
167 // - otherwise, dest_dir_ is not set and dest_file_ is set with the value passed on constructor.
168 std::string dest_dir_, dest_file_;
169
170 // Message displayed on LinePrinter, it's updated every time the destination above change.
171 std::string line_message_;
172
173 // Lines sent by bugreportz that contain invalid commands; will be displayed at the end.
Felipe Leme6e286962016-07-29 15:47:00 -0700174 std::vector<std::string> invalid_lines_;
Felipe Lemeb3239722016-07-29 17:57:00 -0700175
176 // Whether PROGRESS_LINES should be interpreted as progress.
Felipe Leme6f5080f2016-07-26 12:23:40 -0700177 bool show_progress_;
Felipe Lemeb3239722016-07-29 17:57:00 -0700178
179 // Overall process of the operation, as returned by Done().
Felipe Leme6f5080f2016-07-26 12:23:40 -0700180 int status_;
181
Felipe Lemeb3239722016-07-29 17:57:00 -0700182 // Temporary buffer containing the characters read since the last newline (\n).
Felipe Leme6f5080f2016-07-26 12:23:40 -0700183 std::string line_;
Felipe Leme0d4f0502016-07-26 12:14:39 -0700184
185 DISALLOW_COPY_AND_ASSIGN(BugreportStandardStreamsCallback);
186};
187
Felipe Leme6f5080f2016-07-26 12:23:40 -0700188// Implemented in commandline.cpp
189int usage();
190
Felipe Leme698e0652016-07-19 17:07:22 -0700191int Bugreport::DoIt(TransportType transport_type, const char* serial, int argc, const char** argv) {
Felipe Lemeb3239722016-07-29 17:57:00 -0700192 if (argc > 2) return usage();
Felipe Leme6f5080f2016-07-26 12:23:40 -0700193
194 // Gets bugreportz version.
Felipe Lemef33fcb62016-08-04 12:03:06 -0700195 std::string bugz_stdout, bugz_stderr;
196 DefaultStandardStreamsCallback version_callback(&bugz_stdout, &bugz_stderr);
Felipe Leme6f5080f2016-07-26 12:23:40 -0700197 int status = SendShellCommand(transport_type, serial, "bugreportz -v", false, &version_callback);
Felipe Leme6f5080f2016-07-26 12:23:40 -0700198 std::string bugz_version = android::base::Trim(bugz_stderr);
Felipe Lemef33fcb62016-08-04 12:03:06 -0700199 std::string bugz_output = android::base::Trim(bugz_stdout);
Felipe Leme6f5080f2016-07-26 12:23:40 -0700200
Felipe Leme6e286962016-07-29 15:47:00 -0700201 if (status != 0 || bugz_version.empty()) {
Felipe Lemef33fcb62016-08-04 12:03:06 -0700202 D("'bugreportz' -v results: status=%d, stdout='%s', stderr='%s'", status,
203 bugz_output.c_str(), bugz_version.c_str());
204 if (argc == 1) {
205 // Device does not support bugreportz: if called as 'adb bugreport', just falls out to
206 // the flat-file version.
207 fprintf(stderr,
208 "Failed to get bugreportz version, which is only available on devices "
209 "running Android 7.0 or later.\nTrying a plain-text bug report instead.\n");
210 return SendShellCommand(transport_type, serial, "bugreport", false);
211 }
Felipe Lemeb3239722016-07-29 17:57:00 -0700212
213 // But if user explicitly asked for a zipped bug report, fails instead (otherwise calling
Felipe Lemef33fcb62016-08-04 12:03:06 -0700214 // 'bugreport' would generate a lot of output the user might not be prepared to handle).
Felipe Leme6e286962016-07-29 15:47:00 -0700215 fprintf(stderr,
216 "Failed to get bugreportz version: 'bugreportz -v' returned '%s' (code %d).\n"
Felipe Lemef33fcb62016-08-04 12:03:06 -0700217 "If the device does not run Android 7.0 or above, try 'adb bugreport' instead.\n",
218 bugz_output.c_str(), status);
Felipe Leme6e286962016-07-29 15:47:00 -0700219 return status != 0 ? status : -1;
220 }
221
Felipe Lemeb3239722016-07-29 17:57:00 -0700222 std::string dest_file, dest_dir;
223
224 if (argc == 1) {
225 // No args - use current directory
226 if (!getcwd(&dest_dir)) {
227 perror("adb: getcwd failed");
228 return 1;
229 }
230 } else {
231 // Check whether argument is a directory or file
232 if (directory_exists(argv[1])) {
233 dest_dir = argv[1];
234 } else {
235 dest_file = argv[1];
236 }
237 }
238
239 if (dest_file.empty()) {
240 // Uses a default value until device provides the proper name
241 dest_file = "bugreport.zip";
242 } else {
Elliott Hughes7acce1b2016-10-25 17:24:54 -0700243 if (!android::base::EndsWithIgnoreCase(dest_file, ".zip")) {
Felipe Lemeb3239722016-07-29 17:57:00 -0700244 dest_file += ".zip";
245 }
246 }
247
Felipe Leme6f5080f2016-07-26 12:23:40 -0700248 bool show_progress = true;
249 std::string bugz_command = "bugreportz -p";
250 if (bugz_version == "1.0") {
251 // 1.0 does not support progress notifications, so print a disclaimer
252 // message instead.
253 fprintf(stderr,
254 "Bugreport is in progress and it could take minutes to complete.\n"
255 "Please be patient and do not cancel or disconnect your device "
Felipe Leme6e286962016-07-29 15:47:00 -0700256 "until it completes.\n");
Felipe Leme6f5080f2016-07-26 12:23:40 -0700257 show_progress = false;
258 bugz_command = "bugreportz";
259 }
Felipe Lemeb3239722016-07-29 17:57:00 -0700260 BugreportStandardStreamsCallback bugz_callback(dest_dir, dest_file, show_progress, this);
Felipe Leme6f5080f2016-07-26 12:23:40 -0700261 return SendShellCommand(transport_type, serial, bugz_command, false, &bugz_callback);
262}
263
Felipe Lemedaf46282016-07-27 19:23:09 -0700264void Bugreport::UpdateProgress(const std::string& message, int progress, int total) {
Felipe Leme6f5080f2016-07-26 12:23:40 -0700265 int progress_percentage = (progress * 100 / total);
Felipe Lemedaf46282016-07-27 19:23:09 -0700266 line_printer_.Print(
267 android::base::StringPrintf("[%3d%%] %s", progress_percentage, message.c_str()),
268 LinePrinter::INFO);
Felipe Leme698e0652016-07-19 17:07:22 -0700269}
270
271int Bugreport::SendShellCommand(TransportType transport_type, const char* serial,
272 const std::string& command, bool disable_shell_protocol,
Felipe Leme0d4f0502016-07-26 12:14:39 -0700273 StandardStreamsCallbackInterface* callback) {
274 return send_shell_command(transport_type, serial, command, disable_shell_protocol, callback);
Felipe Leme698e0652016-07-19 17:07:22 -0700275}
276
277bool Bugreport::DoSyncPull(const std::vector<const char*>& srcs, const char* dst, bool copy_attrs,
278 const char* name) {
279 return do_sync_pull(srcs, dst, copy_attrs, name);
280}