1 /*
2  * Copyright (C) 2021 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 
17 #include <stdio.h>
18 #include <fstream>
19 #include <string>
20 
21 #include <android-base/logging.h>
22 #include <android-base/strings.h>
23 #include <gflags/gflags.h>
24 
25 #include "common/libs/fs/shared_fd.h"
26 #include "common/libs/fs/shared_select.h"
27 #include "common/libs/utils/files.h"
28 #include "host/libs/config/cuttlefish_config.h"
29 #include "ziparchive/zip_writer.h"
30 
31 DEFINE_string(output, "host_bugreport.zip", "Where to write the output");
32 
33 namespace cuttlefish {
34 namespace {
35 
SaveFile(ZipWriter & writer,const std::string & zip_path,const std::string & file_path)36 void SaveFile(ZipWriter& writer, const std::string& zip_path,
37               const std::string& file_path) {
38   writer.StartEntry(zip_path, ZipWriter::kCompress | ZipWriter::kAlign32);
39   std::fstream file(file_path, std::fstream::in | std::fstream::binary);
40   do {
41     char data[1024 * 10];
42     file.read(data, sizeof(data));
43     writer.WriteBytes(data, file.gcount());
44   } while (file);
45   writer.FinishEntry();
46   if (file.bad()) {
47     LOG(ERROR) << "Error in logging " << file_path << " to " << zip_path;
48   }
49 }
50 
CvdHostBugreportMain(int argc,char ** argv)51 Result<void> CvdHostBugreportMain(int argc, char** argv) {
52   ::android::base::InitLogging(argv, android::base::StderrLogger);
53   google::ParseCommandLineFlags(&argc, &argv, true);
54 
55   auto config = CuttlefishConfig::Get();
56   CHECK(config) << "Unable to find the config";
57 
58   auto out_path = FLAGS_output.c_str();
59   std::unique_ptr<FILE, decltype(&fclose)> out(fopen(out_path, "wbe"), &fclose);
60   ZipWriter writer(out.get());
61 
62   auto save = [&writer, config](const std::string& path) {
63     SaveFile(writer, "cuttlefish_assembly/" + path, config->AssemblyPath(path));
64   };
65   save("assemble_cvd.log");
66   save("cuttlefish_config.json");
67 
68   for (const auto& instance : config->Instances()) {
69     auto save = [&writer, instance](const std::string& path) {
70       const auto& zip_name = instance.instance_name() + "/" + path;
71       const auto& file_name = instance.PerInstancePath(path.c_str());
72       SaveFile(writer, zip_name, file_name);
73     };
74     save("cuttlefish_config.json");
75     save("disk_config.txt");
76     save("kernel.log");
77     save("launcher.log");
78     save("logcat");
79     save("metrics.log");
80     auto tombstones =
81         CF_EXPECT(DirectoryContents(instance.PerInstancePath("tombstones")),
82                   "Cannot read from tombstones directory.");
83     for (const auto& tombstone : tombstones) {
84       if (tombstone == "." || tombstone == "..") {
85         continue;
86       }
87       save("tombstones/" + tombstone);
88     }
89     auto recordings =
90         CF_EXPECT(DirectoryContents(instance.PerInstancePath("recording")),
91                   "Cannot read from recording directory.");
92     for (const auto& recording : recordings) {
93       if (recording == "." || recording == "..") {
94         continue;
95       }
96       save("recording/" + recording);
97     }
98   }
99 
100   writer.Finish();
101 
102   LOG(INFO) << "Saved to \"" << FLAGS_output << "\"";
103 
104   return {};
105 }
106 
107 }  // namespace
108 }  // namespace cuttlefish
109 
main(int argc,char ** argv)110 int main(int argc, char** argv) {
111   auto result = cuttlefish::CvdHostBugreportMain(argc, argv);
112   CHECK(result.ok()) << result.error().FormatForEnv();
113   return 0;
114 }
115