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/file.h>
22 #include <android-base/logging.h>
23 #include <android-base/strings.h>
24 #include <gflags/gflags.h>
25 
26 #include "common/libs/fs/shared_fd.h"
27 #include "common/libs/fs/shared_select.h"
28 #include "common/libs/utils/files.h"
29 #include "common/libs/utils/subprocess.h"
30 #include "common/libs/utils/tee_logging.h"
31 #include "host/libs/config/cuttlefish_config.h"
32 #include "ziparchive/zip_writer.h"
33 
34 DEFINE_string(output, "host_bugreport.zip", "Where to write the output");
35 
36 namespace cuttlefish {
37 namespace {
38 
SaveFile(ZipWriter & writer,const std::string & zip_path,const std::string & file_path)39 void SaveFile(ZipWriter& writer, const std::string& zip_path,
40               const std::string& file_path) {
41   writer.StartEntry(zip_path, ZipWriter::kCompress | ZipWriter::kAlign32);
42   std::fstream file(file_path, std::fstream::in | std::fstream::binary);
43   do {
44     char data[1024 * 10];
45     file.read(data, sizeof(data));
46     writer.WriteBytes(data, file.gcount());
47   } while (file);
48   writer.FinishEntry();
49   if (file.bad()) {
50     LOG(ERROR) << "Error in logging " << file_path << " to " << zip_path;
51   }
52 }
53 
AddNetsimdLogs(ZipWriter & writer)54 void AddNetsimdLogs(ZipWriter& writer) {
55   // The temp directory name depends on whether the `USER` environment variable
56   // is defined.
57   // https://source.corp.google.com/h/googleplex-android/platform/superproject/main/+/main:tools/netsim/rust/common/src/system/mod.rs;l=37-57;drc=360ddb57df49472a40275b125bb56af2a65395c7
58   std::string user = StringFromEnv("USER", "");
59   std::string dir = user.empty() ? "/tmp/android/netsimd"
60                                  : fmt::format("/tmp/android-{}/netsimd", user);
61   if (!DirectoryExists(dir)) {
62     LOG(INFO) << "netsimd logs directory: `" << dir << "` does not exist.";
63     return;
64   }
65   auto names = DirectoryContents(dir);
66   if (!names.ok()) {
67     LOG(ERROR) << "Cannot read from netsimd directory `" << dir
68                << "`: " << names.error().FormatForEnv(/* color = */ false);
69     return;
70   }
71   for (const auto& name : names.value()) {
72     SaveFile(writer, "netsimd/" + name, dir + "/" + name);
73   }
74 }
75 
CreateDeviceBugreport(const CuttlefishConfig::InstanceSpecific & ins,const std::string & out_dir)76 Result<void> CreateDeviceBugreport(
77     const CuttlefishConfig::InstanceSpecific& ins, const std::string& out_dir) {
78   std::string adb_bin_path = HostBinaryPath("adb");
79   CF_EXPECT(FileExists(adb_bin_path),
80             "adb binary not found at: " << adb_bin_path);
81   Command connect_cmd("timeout");
82   connect_cmd.SetWorkingDirectory(
83       "/");  // Use a deterministic working directory
84   connect_cmd.AddParameter("30s")
85       .AddParameter(adb_bin_path)
86       .AddParameter("connect")
87       .AddParameter(ins.adb_ip_and_port());
88   CF_EXPECT_EQ(connect_cmd.Start().Wait(), 0, "adb connect failed");
89   Command wait_for_device_cmd("timeout");
90   wait_for_device_cmd.SetWorkingDirectory(
91       "/");  // Use a deterministic working directory
92   wait_for_device_cmd.AddParameter("30s")
93       .AddParameter(adb_bin_path)
94       .AddParameter("-s")
95       .AddParameter(ins.adb_ip_and_port())
96       .AddParameter("wait-for-device");
97   CF_EXPECT_EQ(wait_for_device_cmd.Start().Wait(), 0,
98                "adb wait-for-device failed");
99   Command bugreport_cmd("timeout");
100   bugreport_cmd.SetWorkingDirectory(
101       "/");  // Use a deterministic working directory
102   bugreport_cmd.AddParameter("300s")
103       .AddParameter(adb_bin_path)
104       .AddParameter("-s")
105       .AddParameter(ins.adb_ip_and_port())
106       .AddParameter("bugreport")
107       .AddParameter(out_dir);
108   CF_EXPECT_EQ(bugreport_cmd.Start().Wait(), 0, "adb bugreport failed");
109   return {};
110 }
111 
CvdHostBugreportMain(int argc,char ** argv)112 Result<void> CvdHostBugreportMain(int argc, char** argv) {
113   ::android::base::InitLogging(argv, android::base::StderrLogger);
114   google::ParseCommandLineFlags(&argc, &argv, true);
115 
116   std::string log_filename = "/tmp/cvd_hbr.log.XXXXXX";
117   {
118     auto fd = SharedFD::Mkstemp(&log_filename);
119     CF_EXPECT(fd->IsOpen(), "Unable to create log file: " << fd->StrError());
120     android::base::SetLogger(TeeLogger({
121         {ConsoleSeverity(), SharedFD::Dup(2), MetadataLevel::ONLY_MESSAGE},
122         {LogFileSeverity(), fd, MetadataLevel::FULL},
123     }));
124   }
125 
126   auto config = CuttlefishConfig::Get();
127   CHECK(config) << "Unable to find the config";
128 
129   auto out_path = FLAGS_output.c_str();
130   std::unique_ptr<FILE, decltype(&fclose)> out(fopen(out_path, "wbe"), &fclose);
131   ZipWriter writer(out.get());
132 
133   auto save = [&writer, config](const std::string& path) {
134     SaveFile(writer, "cuttlefish_assembly/" + path, config->AssemblyPath(path));
135   };
136   save("assemble_cvd.log");
137   save("cuttlefish_config.json");
138 
139   for (const auto& instance : config->Instances()) {
140     auto save = [&writer, instance](const std::string& path) {
141       const auto& zip_name = instance.instance_name() + "/" + path;
142       const auto& file_name = instance.PerInstancePath(path.c_str());
143       SaveFile(writer, zip_name, file_name);
144     };
145     save("cuttlefish_config.json");
146     save("disk_config.txt");
147     if (DirectoryExists(instance.PerInstancePath("logs"))) {
148       auto result = DirectoryContents(instance.PerInstancePath("logs"));
149       if (result.ok()) {
150         for (const auto& log : result.value()) {
151           save("logs/" + log);
152         }
153       } else {
154         LOG(ERROR) << "Cannot read from logs directory: "
155                    << result.error().FormatForEnv(/* color = */ false);
156       }
157     } else {
158       save("kernel.log");
159       save("launcher.log");
160       save("logcat");
161       save("metrics.log");
162     }
163 
164     {
165       auto result = DirectoryContents(instance.PerInstancePath("tombstones"));
166       if (result.ok()) {
167         for (const auto& tombstone : result.value()) {
168           save("tombstones/" + tombstone);
169         }
170       } else {
171         LOG(ERROR) << "Cannot read from tombstones directory: "
172                    << result.error().FormatForEnv(/* color = */ false);
173       }
174     }
175 
176     {
177       auto result = DirectoryContents(instance.PerInstancePath("recording"));
178       if (result.ok()) {
179         for (const auto& recording : result.value()) {
180           save("recording/" + recording);
181         }
182       } else {
183         LOG(ERROR) << "Cannot read from recording directory: "
184                    << result.error().FormatForEnv(/* color = */ false);
185       }
186     }
187 
188     {
189       // TODO(b/359657254) Create the `adb bugreport` asynchronously.
190       std::string device_br_dir = "/tmp/cvd_dbrXXXXXX";
191       CF_EXPECTF(mkdtemp(device_br_dir.data()) != nullptr,
192                  "mkdtemp failed: '{}'", strerror(errno));
193       auto result = CreateDeviceBugreport(instance, device_br_dir);
194       if (result.ok()) {
195         auto names = DirectoryContents(device_br_dir);
196         if (names.ok()) {
197           for (const auto& name : names.value()) {
198             std::string filename = device_br_dir + "/" + name;
199             SaveFile(writer, android::base::Basename(filename), filename);
200           }
201         } else {
202           LOG(ERROR) << "Cannot read from device bugreport directory: "
203                      << names.error().FormatForEnv(/* color = */ false);
204         }
205       } else {
206         LOG(ERROR) << "Failed to create device bugreport: "
207                    << result.error().FormatForEnv(/* color = */ false);
208       }
209       static_cast<void>(RecursivelyRemoveDirectory(device_br_dir));
210     }
211   }
212 
213   AddNetsimdLogs(writer);
214 
215   LOG(INFO) << "Building cvd bugreport completed";
216 
217   SaveFile(writer, "cvd_bugreport_builder.log", log_filename);
218 
219   writer.Finish();
220 
221   LOG(INFO) << "Saved to \"" << FLAGS_output << "\"";
222 
223   if (!RemoveFile(log_filename)) {
224     LOG(INFO) << "Failed to remove host bug report log file: " << log_filename;
225   }
226 
227   return {};
228 }
229 
230 }  // namespace
231 }  // namespace cuttlefish
232 
main(int argc,char ** argv)233 int main(int argc, char** argv) {
234   auto result = cuttlefish::CvdHostBugreportMain(argc, argv);
235   CHECK(result.ok()) << result.error().FormatForEnv();
236   return 0;
237 }
238