summaryrefslogtreecommitdiff
path: root/bugreport.cpp
diff options
context:
space:
mode:
authorFelipe Leme <felipeal@google.com>2016-07-19 17:07:22 -0700
committerJosh Gao <jmgao@google.com>2016-08-30 13:06:23 -0700
commit86ff99f1fb86467b762838a0fcbeb9df9e943077 (patch)
tree694ca45a8cdb7b5d67dbd9be0ce5b86ccd129f4a /bugreport.cpp
parent342b4eb402245492790450fdc0e498dc9bcd1dfa (diff)
downloadadb-86ff99f1fb86467b762838a0fcbeb9df9e943077.tar.gz
DO NOT MERGE: Split bugreport() into its own file and added unit tests.
bugreport() will be soon refactored to track progress, which will require more comprehensive unit tests. As such, it's better to move it to its own files, which in turn also requires moving send_shell_command() and usage() to commandline.h. Fixes: 30100363 Bug: 30268737 Change-Id: I3cdf114a0b5547293320042ff0749a60886440b0 (cherry picked from commit 78e0963e4bce9cc9f0bbf0b686004ba15b1e3929) (cherry picked from commit 218e1ff75998052c7bb30b483c15e75a853283a8)
Diffstat (limited to 'bugreport.cpp')
-rw-r--r--bugreport.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/bugreport.cpp b/bugreport.cpp
new file mode 100644
index 0000000..1af5843
--- /dev/null
+++ b/bugreport.cpp
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string>
+
+#include <android-base/strings.h>
+
+#include "bugreport.h"
+#include "commandline.h"
+#include "file_sync_service.h"
+
+static constexpr char BUGZ_OK_PREFIX[] = "OK:";
+static constexpr char BUGZ_FAIL_PREFIX[] = "FAIL:";
+
+int Bugreport::DoIt(TransportType transport_type, const char* serial, int argc, const char** argv) {
+ if (argc == 1) return SendShellCommand(transport_type, serial, "bugreport", false);
+ if (argc != 2) return usage();
+
+ // Zipped bugreport option - will call 'bugreportz', which prints the location
+ // of the generated
+ // file, then pull it to the destination file provided by the user.
+ std::string dest_file = argv[1];
+ if (!android::base::EndsWith(argv[1], ".zip")) {
+ // TODO: use a case-insensitive comparison (like EndsWithIgnoreCase
+ dest_file += ".zip";
+ }
+ std::string output;
+
+ fprintf(stderr,
+ "Bugreport is in progress and it could take minutes to complete.\n"
+ "Please be patient and do not cancel or disconnect your device until "
+ "it completes.\n");
+ int status = SendShellCommand(transport_type, serial, "bugreportz", false, &output, nullptr);
+ if (status != 0 || output.empty()) return status;
+ output = android::base::Trim(output);
+
+ if (android::base::StartsWith(output, BUGZ_OK_PREFIX)) {
+ const char* zip_file = &output[strlen(BUGZ_OK_PREFIX)];
+ std::vector<const char*> srcs{zip_file};
+ status = DoSyncPull(srcs, dest_file.c_str(), true, dest_file.c_str()) ? 0 : 1;
+ if (status != 0) {
+ fprintf(stderr, "Could not copy file '%s' to '%s'\n", zip_file, dest_file.c_str());
+ }
+ return status;
+ }
+ if (android::base::StartsWith(output, BUGZ_FAIL_PREFIX)) {
+ const char* error_message = &output[strlen(BUGZ_FAIL_PREFIX)];
+ fprintf(stderr, "Device failed to take a zipped bugreport: %s\n", error_message);
+ return -1;
+ }
+ fprintf(stderr,
+ "Unexpected string (%s) returned by bugreportz, "
+ "device probably does not support it\n",
+ output.c_str());
+ return -1;
+}
+
+int Bugreport::SendShellCommand(TransportType transport_type, const char* serial,
+ const std::string& command, bool disable_shell_protocol,
+ std::string* output, std::string* err) {
+ return send_shell_command(transport_type, serial, command, disable_shell_protocol, output, err);
+}
+
+bool Bugreport::DoSyncPull(const std::vector<const char*>& srcs, const char* dst, bool copy_attrs,
+ const char* name) {
+ return do_sync_pull(srcs, dst, copy_attrs, name);
+}