blob: 9dc9811021359b57d3f2255c0230c64b5dc59123 [file] [log] [blame]
Felipe Lemeb9f1b1c2016-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 Leme1fc578c2016-08-04 12:03:06 -070017#define TRACE_TAG ADB
18
Felipe Leme8582c5d2016-07-29 17:57:00 -070019#include "bugreport.h"
20
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -070021#include <string>
Felipe Leme93fffb82016-07-29 15:47:00 -070022#include <vector>
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -070023
Colin Crosse6794072017-02-23 21:23:05 -080024#include <android-base/file.h>
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -070025#include <android-base/strings.h>
26
Felipe Leme8582c5d2016-07-29 17:57:00 -070027#include "sysdeps.h"
28#include "adb_utils.h"
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -070029#include "file_sync_service.h"
30
Felipe Leme8582c5d2016-07-29 17:57:00 -070031static constexpr char BUGZ_BEGIN_PREFIX[] = "BEGIN:";
Felipe Leme5b786992016-07-26 12:23:40 -070032static constexpr char BUGZ_PROGRESS_PREFIX[] = "PROGRESS:";
33static constexpr char BUGZ_PROGRESS_SEPARATOR[] = "/";
Felipe Leme8582c5d2016-07-29 17:57:00 -070034static constexpr char BUGZ_OK_PREFIX[] = "OK:";
35static constexpr char BUGZ_FAIL_PREFIX[] = "FAIL:";
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -070036
Felipe Lemeed602ed2016-07-26 12:14:39 -070037// Custom callback used to handle the output of zipped bugreports.
38class BugreportStandardStreamsCallback : public StandardStreamsCallbackInterface {
39 public:
Felipe Leme8582c5d2016-07-29 17:57:00 -070040 BugreportStandardStreamsCallback(const std::string& dest_dir, const std::string& dest_file,
41 bool show_progress, Bugreport* br)
Felipe Leme9143a802016-07-27 19:23:09 -070042 : br_(br),
Felipe Leme93fffb82016-07-29 15:47:00 -070043 src_file_(),
Felipe Leme8582c5d2016-07-29 17:57:00 -070044 dest_dir_(dest_dir),
Felipe Leme9143a802016-07-27 19:23:09 -070045 dest_file_(dest_file),
Felipe Leme8582c5d2016-07-29 17:57:00 -070046 line_message_(),
Felipe Leme93fffb82016-07-29 15:47:00 -070047 invalid_lines_(),
Felipe Leme9143a802016-07-27 19:23:09 -070048 show_progress_(show_progress),
Felipe Leme93fffb82016-07-29 15:47:00 -070049 status_(0),
Felipe Leme5dab2b42017-03-20 11:00:11 -070050 line_(),
51 last_progress_(0) {
Felipe Leme4fa67e12016-08-15 16:01:58 -070052 SetLineMessage("generating");
Felipe Lemeed602ed2016-07-26 12:14:39 -070053 }
54
55 void OnStdout(const char* buffer, int length) {
Felipe Leme5b786992016-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 Lemeed602ed2016-07-26 12:14:39 -070065 }
66
67 void OnStderr(const char* buffer, int length) {
68 OnStream(nullptr, stderr, buffer, length);
69 }
Felipe Leme5dab2b42017-03-20 11:00:11 -070070
Felipe Lemeed602ed2016-07-26 12:14:39 -070071 int Done(int unused_) {
Felipe Leme93fffb82016-07-29 15:47:00 -070072 // Process remaining line, if any.
Felipe Leme5b786992016-07-26 12:23:40 -070073 ProcessLine(line_);
Felipe Leme93fffb82016-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 Leme8582c5d2016-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 Leme93fffb82016-07-29 15:47:00 -0700102 std::vector<const char*> srcs{src_file_.c_str()};
Felipe Leme4fa67e12016-08-15 16:01:58 -0700103 SetLineMessage("pulling");
Felipe Leme8582c5d2016-07-29 17:57:00 -0700104 status_ =
105 br_->DoSyncPull(srcs, destination.c_str(), true, line_message_.c_str()) ? 0 : 1;
Felipe Leme93fffb82016-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 Leme8582c5d2016-07-29 17:57:00 -0700111 destination.c_str(), src_file_.c_str());
Felipe Leme93fffb82016-07-29 15:47:00 -0700112 }
113 }
Felipe Leme5b786992016-07-26 12:23:40 -0700114 return status_;
Felipe Lemeed602ed2016-07-26 12:14:39 -0700115 }
116
117 private:
Felipe Leme4fa67e12016-08-15 16:01:58 -0700118 void SetLineMessage(const std::string& action) {
Colin Crosse6794072017-02-23 21:23:05 -0800119 line_message_ = action + " " + android::base::Basename(dest_file_);
Felipe Leme8582c5d2016-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 Crosse6794072017-02-23 21:23:05 -0800126 dest_file_ = android::base::Basename(path);
Felipe Leme4fa67e12016-08-15 16:01:58 -0700127 SetLineMessage("generating");
Felipe Leme8582c5d2016-07-29 17:57:00 -0700128 }
129 }
130
Felipe Leme5b786992016-07-26 12:23:40 -0700131 void ProcessLine(const std::string& line) {
132 if (line.empty()) return;
133
Felipe Leme8582c5d2016-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 Leme5b786992016-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)];
140 fprintf(stderr, "Device failed to take a zipped bugreport: %s\n", error_message);
141 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 Leme5dab2b42017-03-20 11:00:11 -0700150 if (progress <= last_progress_) {
151 // Ignore.
152 return;
153 }
154 last_progress_ = progress;
Felipe Leme5b786992016-07-26 12:23:40 -0700155 int total = std::stoi(line.substr(idx2 + 1));
Felipe Leme8582c5d2016-07-29 17:57:00 -0700156 br_->UpdateProgress(line_message_, progress, total);
Felipe Leme5b786992016-07-26 12:23:40 -0700157 } else {
Felipe Leme93fffb82016-07-29 15:47:00 -0700158 invalid_lines_.push_back(line);
Felipe Leme5b786992016-07-26 12:23:40 -0700159 }
160 }
161
Felipe Lemeed602ed2016-07-26 12:14:39 -0700162 Bugreport* br_;
Felipe Leme8582c5d2016-07-29 17:57:00 -0700163
164 // Path of bugreport on device.
Felipe Leme93fffb82016-07-29 15:47:00 -0700165 std::string src_file_;
Felipe Leme8582c5d2016-07-29 17:57:00 -0700166
167 // Bugreport destination on host, depending on argument passed on constructor:
168 // - if argument is a directory, dest_dir_ is set with it and dest_file_ will be the name
169 // of the bugreport reported by the device.
170 // - if argument is empty, dest_dir is set as the current directory and dest_file_ will be the
171 // name of the bugreport reported by the device.
172 // - otherwise, dest_dir_ is not set and dest_file_ is set with the value passed on constructor.
173 std::string dest_dir_, dest_file_;
174
175 // Message displayed on LinePrinter, it's updated every time the destination above change.
176 std::string line_message_;
177
178 // Lines sent by bugreportz that contain invalid commands; will be displayed at the end.
Felipe Leme93fffb82016-07-29 15:47:00 -0700179 std::vector<std::string> invalid_lines_;
Felipe Leme8582c5d2016-07-29 17:57:00 -0700180
181 // Whether PROGRESS_LINES should be interpreted as progress.
Felipe Leme5b786992016-07-26 12:23:40 -0700182 bool show_progress_;
Felipe Leme8582c5d2016-07-29 17:57:00 -0700183
184 // Overall process of the operation, as returned by Done().
Felipe Leme5b786992016-07-26 12:23:40 -0700185 int status_;
186
Felipe Leme8582c5d2016-07-29 17:57:00 -0700187 // Temporary buffer containing the characters read since the last newline (\n).
Felipe Leme5b786992016-07-26 12:23:40 -0700188 std::string line_;
Felipe Lemeed602ed2016-07-26 12:14:39 -0700189
Felipe Leme5dab2b42017-03-20 11:00:11 -0700190 // Last displayed progress.
191 // Since dumpstate progress can recede, only forward progress should be displayed
192 int last_progress_;
193
Felipe Lemeed602ed2016-07-26 12:14:39 -0700194 DISALLOW_COPY_AND_ASSIGN(BugreportStandardStreamsCallback);
195};
196
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700197int Bugreport::DoIt(TransportType transport_type, const char* serial, int argc, const char** argv) {
Elliott Hughes97e74bb2017-02-06 16:20:30 -0800198 if (argc > 2) return usage("usage: adb bugreport [PATH]");
Felipe Leme5b786992016-07-26 12:23:40 -0700199
200 // Gets bugreportz version.
Felipe Leme1fc578c2016-08-04 12:03:06 -0700201 std::string bugz_stdout, bugz_stderr;
202 DefaultStandardStreamsCallback version_callback(&bugz_stdout, &bugz_stderr);
Felipe Leme5b786992016-07-26 12:23:40 -0700203 int status = SendShellCommand(transport_type, serial, "bugreportz -v", false, &version_callback);
Felipe Leme5b786992016-07-26 12:23:40 -0700204 std::string bugz_version = android::base::Trim(bugz_stderr);
Felipe Leme1fc578c2016-08-04 12:03:06 -0700205 std::string bugz_output = android::base::Trim(bugz_stdout);
Felipe Leme5b786992016-07-26 12:23:40 -0700206
Felipe Leme93fffb82016-07-29 15:47:00 -0700207 if (status != 0 || bugz_version.empty()) {
Felipe Leme1fc578c2016-08-04 12:03:06 -0700208 D("'bugreportz' -v results: status=%d, stdout='%s', stderr='%s'", status,
209 bugz_output.c_str(), bugz_version.c_str());
210 if (argc == 1) {
211 // Device does not support bugreportz: if called as 'adb bugreport', just falls out to
212 // the flat-file version.
213 fprintf(stderr,
214 "Failed to get bugreportz version, which is only available on devices "
215 "running Android 7.0 or later.\nTrying a plain-text bug report instead.\n");
216 return SendShellCommand(transport_type, serial, "bugreport", false);
217 }
Felipe Leme8582c5d2016-07-29 17:57:00 -0700218
219 // But if user explicitly asked for a zipped bug report, fails instead (otherwise calling
Felipe Leme1fc578c2016-08-04 12:03:06 -0700220 // 'bugreport' would generate a lot of output the user might not be prepared to handle).
Felipe Leme93fffb82016-07-29 15:47:00 -0700221 fprintf(stderr,
222 "Failed to get bugreportz version: 'bugreportz -v' returned '%s' (code %d).\n"
Felipe Leme1fc578c2016-08-04 12:03:06 -0700223 "If the device does not run Android 7.0 or above, try 'adb bugreport' instead.\n",
224 bugz_output.c_str(), status);
Felipe Leme93fffb82016-07-29 15:47:00 -0700225 return status != 0 ? status : -1;
226 }
227
Felipe Leme8582c5d2016-07-29 17:57:00 -0700228 std::string dest_file, dest_dir;
229
230 if (argc == 1) {
231 // No args - use current directory
232 if (!getcwd(&dest_dir)) {
233 perror("adb: getcwd failed");
234 return 1;
235 }
236 } else {
237 // Check whether argument is a directory or file
238 if (directory_exists(argv[1])) {
239 dest_dir = argv[1];
240 } else {
241 dest_file = argv[1];
242 }
243 }
244
245 if (dest_file.empty()) {
246 // Uses a default value until device provides the proper name
247 dest_file = "bugreport.zip";
248 } else {
Elliott Hughesdb9a0c02016-10-25 17:24:54 -0700249 if (!android::base::EndsWithIgnoreCase(dest_file, ".zip")) {
Felipe Leme8582c5d2016-07-29 17:57:00 -0700250 dest_file += ".zip";
251 }
252 }
253
Felipe Leme5b786992016-07-26 12:23:40 -0700254 bool show_progress = true;
255 std::string bugz_command = "bugreportz -p";
256 if (bugz_version == "1.0") {
257 // 1.0 does not support progress notifications, so print a disclaimer
258 // message instead.
259 fprintf(stderr,
260 "Bugreport is in progress and it could take minutes to complete.\n"
261 "Please be patient and do not cancel or disconnect your device "
Felipe Leme93fffb82016-07-29 15:47:00 -0700262 "until it completes.\n");
Felipe Leme5b786992016-07-26 12:23:40 -0700263 show_progress = false;
264 bugz_command = "bugreportz";
265 }
Felipe Leme8582c5d2016-07-29 17:57:00 -0700266 BugreportStandardStreamsCallback bugz_callback(dest_dir, dest_file, show_progress, this);
Felipe Leme5b786992016-07-26 12:23:40 -0700267 return SendShellCommand(transport_type, serial, bugz_command, false, &bugz_callback);
268}
269
Felipe Leme9143a802016-07-27 19:23:09 -0700270void Bugreport::UpdateProgress(const std::string& message, int progress, int total) {
Felipe Leme5b786992016-07-26 12:23:40 -0700271 int progress_percentage = (progress * 100 / total);
Felipe Leme9143a802016-07-27 19:23:09 -0700272 line_printer_.Print(
273 android::base::StringPrintf("[%3d%%] %s", progress_percentage, message.c_str()),
274 LinePrinter::INFO);
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700275}
276
277int Bugreport::SendShellCommand(TransportType transport_type, const char* serial,
278 const std::string& command, bool disable_shell_protocol,
Felipe Lemeed602ed2016-07-26 12:14:39 -0700279 StandardStreamsCallbackInterface* callback) {
280 return send_shell_command(transport_type, serial, command, disable_shell_protocol, callback);
Felipe Lemeb9f1b1c2016-07-19 17:07:22 -0700281}
282
283bool Bugreport::DoSyncPull(const std::vector<const char*>& srcs, const char* dst, bool copy_attrs,
284 const char* name) {
285 return do_sync_pull(srcs, dst, copy_attrs, name);
286}