1 /*
2  * Copyright (C) 2015 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 SIMPLE_PERF_RECORD_FILE_H_
18 #define SIMPLE_PERF_RECORD_FILE_H_
19 
20 #include <stdio.h>
21 
22 #include <functional>
23 #include <map>
24 #include <memory>
25 #include <optional>
26 #include <string>
27 #include <unordered_map>
28 #include <vector>
29 
30 #include <android-base/macros.h>
31 
32 #include "dso.h"
33 #include "event_attr.h"
34 #include "event_type.h"
35 #include "perf_event.h"
36 #include "record.h"
37 #include "record_file_format.h"
38 #include "thread_tree.h"
39 
40 namespace simpleperf {
41 
42 struct FileFeature {
43   std::string path;
44   DsoType type;
45   uint64_t min_vaddr;
46   uint64_t file_offset_of_min_vaddr;
47   std::vector<Symbol> symbols;             // used for reading symbols
48   std::vector<const Symbol*> symbol_ptrs;  // used for writing symbols
49   std::vector<uint64_t> dex_file_offsets;
50 
FileFeatureFileFeature51   FileFeature() {}
52 
53   DISALLOW_COPY_AND_ASSIGN(FileFeature);
54 };
55 
56 struct DebugUnwindFile {
57   std::string path;
58   uint64_t size;
59 };
60 
61 using DebugUnwindFeature = std::vector<DebugUnwindFile>;
62 
63 // RecordFileWriter writes to a perf record file, like perf.data.
64 // User should call RecordFileWriter::Close() to finish writing the file, otherwise the file will
65 // be removed in RecordFileWriter::~RecordFileWriter().
66 class RecordFileWriter {
67  public:
68   static std::unique_ptr<RecordFileWriter> CreateInstance(const std::string& filename);
69 
70   // If own_fp = true, close fp when we finish writing.
71   RecordFileWriter(const std::string& filename, FILE* fp, bool own_fp);
72   ~RecordFileWriter();
73 
74   bool WriteAttrSection(const std::vector<EventAttrWithId>& attr_ids);
75   bool WriteRecord(const Record& record);
76   bool WriteData(const void* buf, size_t len);
77 
GetDataSectionSize()78   uint64_t GetDataSectionSize() const { return data_section_size_; }
79   bool ReadDataSection(const std::function<void(const Record*)>& callback);
80 
81   bool BeginWriteFeatures(size_t feature_count);
82   bool WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records);
83   bool WriteFeatureString(int feature, const std::string& s);
84   bool WriteCmdlineFeature(const std::vector<std::string>& cmdline);
85   bool WriteBranchStackFeature();
86   bool WriteAuxTraceFeature(const std::vector<uint64_t>& auxtrace_offset);
87   bool WriteFileFeatures(const std::vector<Dso*>& dsos);
88   bool WriteFileFeature(const FileFeature& file);
89   bool WriteMetaInfoFeature(const std::unordered_map<std::string, std::string>& info_map);
90   bool WriteDebugUnwindFeature(const DebugUnwindFeature& debug_unwind);
91   bool WriteFeature(int feature, const char* data, size_t size);
92   bool EndWriteFeatures();
93 
94   bool Close();
95 
96  private:
97   void GetHitModulesInBuffer(const char* p, const char* end,
98                              std::vector<std::string>* hit_kernel_modules,
99                              std::vector<std::string>* hit_user_files);
100   bool WriteFileHeader();
101   bool Write(const void* buf, size_t len);
102   bool Read(void* buf, size_t len);
103   bool GetFilePos(uint64_t* file_pos);
104   bool WriteStringWithLength(const std::string& s);
105   bool WriteFeatureBegin(int feature);
106   bool WriteFeatureEnd(int feature);
107 
108   const std::string filename_;
109   FILE* record_fp_;
110   bool own_fp_;
111 
112   perf_event_attr event_attr_;
113   uint64_t attr_section_offset_;
114   uint64_t attr_section_size_;
115   uint64_t data_section_offset_;
116   uint64_t data_section_size_;
117   uint64_t feature_section_offset_;
118 
119   std::map<int, PerfFileFormat::SectionDesc> features_;
120   size_t feature_count_;
121 
122   DISALLOW_COPY_AND_ASSIGN(RecordFileWriter);
123 };
124 
125 // RecordFileReader read contents from a perf record file, like perf.data.
126 class RecordFileReader {
127  public:
128   static std::unique_ptr<RecordFileReader> CreateInstance(const std::string& filename);
129 
130   ~RecordFileReader();
131 
FileHeader()132   const PerfFileFormat::FileHeader& FileHeader() const { return header_; }
133 
AttrSection()134   std::vector<EventAttrWithId> AttrSection() const {
135     std::vector<EventAttrWithId> result(file_attrs_.size());
136     for (size_t i = 0; i < file_attrs_.size(); ++i) {
137       result[i].attr = &file_attrs_[i].attr;
138       result[i].ids = event_ids_for_file_attrs_[i];
139     }
140     return result;
141   }
142 
EventIdMap()143   const std::unordered_map<uint64_t, size_t>& EventIdMap() const { return event_id_to_attr_map_; }
144 
FeatureSectionDescriptors()145   const std::map<int, PerfFileFormat::SectionDesc>& FeatureSectionDescriptors() const {
146     return feature_section_descriptors_;
147   }
HasFeature(int feature)148   bool HasFeature(int feature) const {
149     return feature_section_descriptors_.find(feature) != feature_section_descriptors_.end();
150   }
151   bool ReadFeatureSection(int feature, std::vector<char>* data);
152   bool ReadFeatureSection(int feature, std::string* data);
153 
154   // There are two ways to read records in data section: one is by calling
155   // ReadDataSection(), and [callback] is called for each Record. the other
156   // is by calling ReadRecord() in a loop.
157 
158   // If sorted is true, sort records before passing them to callback function.
159   bool ReadDataSection(const std::function<bool(std::unique_ptr<Record>)>& callback);
160   bool ReadAtOffset(uint64_t offset, void* buf, size_t len);
161 
162   // Read next record. If read successfully, set [record] and return true.
163   // If there is no more records, set [record] to nullptr and return true.
164   // Otherwise return false.
165   bool ReadRecord(std::unique_ptr<Record>& record);
166 
167   size_t GetAttrIndexOfRecord(const Record* record);
168 
169   std::vector<std::string> ReadCmdlineFeature();
170   std::vector<BuildIdRecord> ReadBuildIdFeature();
171   std::string ReadFeatureString(int feature);
172   std::vector<uint64_t> ReadAuxTraceFeature();
173 
174   // File feature section contains many file information. This function reads
175   // one file information located at [read_pos]. [read_pos] is 0 at the first
176   // call, and is updated to point to the next file information. Return true
177   // if read successfully, and return false if there is no more file
178   // information.
179   bool ReadFileFeature(size_t& read_pos, FileFeature* file);
180 
GetMetaInfoFeature()181   const std::unordered_map<std::string, std::string>& GetMetaInfoFeature() { return meta_info_; }
182   std::optional<DebugUnwindFeature> ReadDebugUnwindFeature();
183 
184   void LoadBuildIdAndFileFeatures(ThreadTree& thread_tree);
185 
186   bool ReadAuxData(uint32_t cpu, uint64_t aux_offset, void* buf, size_t size);
187 
188   bool Close();
189 
190   // For testing only.
191   std::vector<std::unique_ptr<Record>> DataSection();
192 
193  private:
194   RecordFileReader(const std::string& filename, FILE* fp);
195   bool ReadHeader();
196   bool ReadAttrSection();
197   bool ReadIdsForAttr(const PerfFileFormat::FileAttr& attr, std::vector<uint64_t>* ids);
198   bool ReadFeatureSectionDescriptors();
199   bool ReadMetaInfoFeature();
200   void UseRecordingEnvironment();
201   std::unique_ptr<Record> ReadRecord();
202   bool Read(void* buf, size_t len);
203   void ProcessEventIdRecord(const EventIdRecord& r);
204   bool BuildAuxDataLocation();
205 
206   const std::string filename_;
207   FILE* record_fp_;
208 
209   PerfFileFormat::FileHeader header_;
210   std::vector<PerfFileFormat::FileAttr> file_attrs_;
211   std::vector<std::vector<uint64_t>> event_ids_for_file_attrs_;
212   std::unordered_map<uint64_t, size_t> event_id_to_attr_map_;
213   std::map<int, PerfFileFormat::SectionDesc> feature_section_descriptors_;
214 
215   size_t event_id_pos_in_sample_records_;
216   size_t event_id_reverse_pos_in_non_sample_records_;
217 
218   uint64_t read_record_size_;
219 
220   std::unordered_map<std::string, std::string> meta_info_;
221   std::unique_ptr<ScopedCurrentArch> scoped_arch_;
222   std::unique_ptr<ScopedEventTypes> scoped_event_types_;
223 
224   struct AuxDataLocation {
225     uint64_t aux_offset;
226     uint64_t aux_size;
227     uint64_t file_offset;
228 
AuxDataLocationAuxDataLocation229     AuxDataLocation(uint64_t aux_offset, uint64_t aux_size, uint64_t file_offset)
230         : aux_offset(aux_offset), aux_size(aux_size), file_offset(file_offset) {}
231   };
232   // It maps from a cpu id to the locations (file offsets in perf.data) of aux data received from
233   // that cpu's aux buffer. It is used to locate aux data in perf.data.
234   std::unordered_map<uint32_t, std::vector<AuxDataLocation>> aux_data_location_;
235 
236   DISALLOW_COPY_AND_ASSIGN(RecordFileReader);
237 };
238 
239 bool IsPerfDataFile(const std::string& filename);
240 
241 }  // namespace simpleperf
242 
243 #endif  // SIMPLE_PERF_RECORD_FILE_H_
244