Felipe Leme | 698e065 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 1 | /* |
| 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 Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 17 | #include "bugreport.h" |
| 18 | |
Felipe Leme | 698e065 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 19 | #include <string> |
Felipe Leme | 6e28696 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 20 | #include <vector> |
Felipe Leme | 698e065 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 21 | |
| 22 | #include <android-base/strings.h> |
| 23 | |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 24 | #include "sysdeps.h" |
| 25 | #include "adb_utils.h" |
Felipe Leme | 698e065 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 26 | #include "file_sync_service.h" |
| 27 | |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 28 | static constexpr char BUGZ_BEGIN_PREFIX[] = "BEGIN:"; |
Felipe Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 29 | static constexpr char BUGZ_PROGRESS_PREFIX[] = "PROGRESS:"; |
| 30 | static constexpr char BUGZ_PROGRESS_SEPARATOR[] = "/"; |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 31 | static constexpr char BUGZ_OK_PREFIX[] = "OK:"; |
| 32 | static constexpr char BUGZ_FAIL_PREFIX[] = "FAIL:"; |
Felipe Leme | 698e065 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 33 | |
Felipe Leme | 0d4f050 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 34 | // Custom callback used to handle the output of zipped bugreports. |
| 35 | class BugreportStandardStreamsCallback : public StandardStreamsCallbackInterface { |
| 36 | public: |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 37 | BugreportStandardStreamsCallback(const std::string& dest_dir, const std::string& dest_file, |
| 38 | bool show_progress, Bugreport* br) |
Felipe Leme | daf4628 | 2016-07-27 19:23:09 -0700 | [diff] [blame] | 39 | : br_(br), |
Felipe Leme | 6e28696 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 40 | src_file_(), |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 41 | dest_dir_(dest_dir), |
Felipe Leme | daf4628 | 2016-07-27 19:23:09 -0700 | [diff] [blame] | 42 | dest_file_(dest_file), |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 43 | line_message_(), |
Felipe Leme | 6e28696 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 44 | invalid_lines_(), |
Felipe Leme | daf4628 | 2016-07-27 19:23:09 -0700 | [diff] [blame] | 45 | show_progress_(show_progress), |
Felipe Leme | 6e28696 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 46 | status_(0), |
Felipe Leme | daf4628 | 2016-07-27 19:23:09 -0700 | [diff] [blame] | 47 | line_() { |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 48 | SetLineMessage(); |
Felipe Leme | 0d4f050 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void OnStdout(const char* buffer, int length) { |
Felipe Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 52 | 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 Leme | 0d4f050 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void OnStderr(const char* buffer, int length) { |
| 64 | OnStream(nullptr, stderr, buffer, length); |
| 65 | } |
Felipe Leme | 0d4f050 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 66 | int Done(int unused_) { |
Felipe Leme | 6e28696 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 67 | // Process remaining line, if any. |
Felipe Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 68 | ProcessLine(line_); |
Felipe Leme | 6e28696 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 69 | |
| 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 Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 90 | 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 Leme | 6e28696 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 97 | std::vector<const char*> srcs{src_file_.c_str()}; |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 98 | status_ = |
| 99 | br_->DoSyncPull(srcs, destination.c_str(), true, line_message_.c_str()) ? 0 : 1; |
Felipe Leme | 6e28696 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 100 | 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 Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 105 | destination.c_str(), src_file_.c_str()); |
Felipe Leme | 6e28696 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
Felipe Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 108 | return status_; |
Felipe Leme | 0d4f050 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | private: |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 112 | 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 Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 126 | void ProcessLine(const std::string& line) { |
| 127 | if (line.empty()) return; |
| 128 | |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 129 | 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 Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 133 | } 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 Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 146 | br_->UpdateProgress(line_message_, progress, total); |
Felipe Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 147 | } else { |
Felipe Leme | 6e28696 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 148 | invalid_lines_.push_back(line); |
Felipe Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Felipe Leme | 0d4f050 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 152 | Bugreport* br_; |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 153 | |
| 154 | // Path of bugreport on device. |
Felipe Leme | 6e28696 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 155 | std::string src_file_; |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 156 | |
| 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 Leme | 6e28696 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 169 | std::vector<std::string> invalid_lines_; |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 170 | |
| 171 | // Whether PROGRESS_LINES should be interpreted as progress. |
Felipe Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 172 | bool show_progress_; |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 173 | |
| 174 | // Overall process of the operation, as returned by Done(). |
Felipe Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 175 | int status_; |
| 176 | |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 177 | // Temporary buffer containing the characters read since the last newline (\n). |
Felipe Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 178 | std::string line_; |
Felipe Leme | 0d4f050 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 179 | |
| 180 | DISALLOW_COPY_AND_ASSIGN(BugreportStandardStreamsCallback); |
| 181 | }; |
| 182 | |
Felipe Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 183 | // Implemented in commandline.cpp |
| 184 | int usage(); |
| 185 | |
Felipe Leme | 698e065 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 186 | int Bugreport::DoIt(TransportType transport_type, const char* serial, int argc, const char** argv) { |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 187 | if (argc > 2) return usage(); |
Felipe Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 188 | |
| 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 Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 193 | std::string bugz_version = android::base::Trim(bugz_stderr); |
| 194 | |
Felipe Leme | 6e28696 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 195 | if (status != 0 || bugz_version.empty()) { |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 196 | // 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 Leme | 6e28696 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 202 | 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 Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 209 | 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 Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 236 | 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 Leme | 6e28696 | 2016-07-29 15:47:00 -0700 | [diff] [blame] | 244 | "until it completes.\n"); |
Felipe Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 245 | show_progress = false; |
| 246 | bugz_command = "bugreportz"; |
| 247 | } |
Felipe Leme | b323972 | 2016-07-29 17:57:00 -0700 | [diff] [blame^] | 248 | BugreportStandardStreamsCallback bugz_callback(dest_dir, dest_file, show_progress, this); |
Felipe Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 249 | return SendShellCommand(transport_type, serial, bugz_command, false, &bugz_callback); |
| 250 | } |
| 251 | |
Felipe Leme | daf4628 | 2016-07-27 19:23:09 -0700 | [diff] [blame] | 252 | void Bugreport::UpdateProgress(const std::string& message, int progress, int total) { |
Felipe Leme | 6f5080f | 2016-07-26 12:23:40 -0700 | [diff] [blame] | 253 | int progress_percentage = (progress * 100 / total); |
Felipe Leme | daf4628 | 2016-07-27 19:23:09 -0700 | [diff] [blame] | 254 | line_printer_.Print( |
| 255 | android::base::StringPrintf("[%3d%%] %s", progress_percentage, message.c_str()), |
| 256 | LinePrinter::INFO); |
Felipe Leme | 698e065 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | int Bugreport::SendShellCommand(TransportType transport_type, const char* serial, |
| 260 | const std::string& command, bool disable_shell_protocol, |
Felipe Leme | 0d4f050 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 261 | StandardStreamsCallbackInterface* callback) { |
| 262 | return send_shell_command(transport_type, serial, command, disable_shell_protocol, callback); |
Felipe Leme | 698e065 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | bool 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 | } |