1 /*
2  * Copyright (C) 2022 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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_ANDROID_BUGREPORT_ANDROID_BUGREPORT_READER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_ANDROID_BUGREPORT_ANDROID_BUGREPORT_READER_H_
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <set>
23 #include <vector>
24 
25 #include "perfetto/base/status.h"
26 #include "perfetto/ext/base/status_or.h"
27 #include "perfetto/trace_processor/status.h"
28 #include "src/trace_processor/importers/android_bugreport/android_log_reader.h"
29 #include "src/trace_processor/tables/metadata_tables_py.h"
30 #include "src/trace_processor/util/zip_reader.h"
31 
32 namespace perfetto ::trace_processor {
33 
34 namespace util {
35 class ZipReader;
36 }
37 
38 class TraceProcessorContext;
39 
40 // Trace importer for Android bugreport.zip archives.
41 class AndroidBugreportReader {
42  public:
43   static bool IsAndroidBugReport(
44       const std::vector<util::ZipFile>& zip_file_entries);
45   static util::Status Parse(TraceProcessorContext* context,
46                             std::vector<util::ZipFile> zip_file_entries);
47 
48  private:
49   struct BugReportFile {
50     tables::TraceFileTable::Id id;
51     int32_t year;
52     util::ZipFile file;
53   };
54   struct LogFile {
55     tables::TraceFileTable::Id id;
56     int64_t timestamp;
57     util::ZipFile file;
58     // Sort files to ease the job of the line-based sort. Unfortunately
59     // lines within each file are not 100% timestamp-ordered, due to things like
60     // kernel messages where log time != event time.
61     bool operator<(const LogFile& other) const {
62       return timestamp < other.timestamp;
63     }
64   };
65 
66   static std::optional<BugReportFile> ExtractBugReportFile(
67       std::vector<util::ZipFile>& vector);
68 
69   AndroidBugreportReader(TraceProcessorContext* context,
70                          BugReportFile bug_report,
71                          std::set<LogFile> ordered_log_files);
72   ~AndroidBugreportReader();
73   util::Status ParseImpl();
74 
75   base::StatusOr<std::vector<TimestampedAndroidLogEvent>>
76   ParsePersistentLogcat();
77   base::Status ParseDumpstateTxt(std::vector<TimestampedAndroidLogEvent>);
78 
79   TraceProcessorContext* const context_;
80   BugReportFile bug_report_;
81   // Log files conveniently sorted by their file timestamp (see operator< in
82   // LogFile)
83   std::set<LogFile> ordered_log_files_;
84 };
85 
86 }  // namespace perfetto::trace_processor
87 
88 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_ANDROID_BUGREPORT_ANDROID_BUGREPORT_READER_H_
89