diff options
Diffstat (limited to 'simpleperf/record_file.h')
-rw-r--r-- | simpleperf/record_file.h | 60 |
1 files changed, 43 insertions, 17 deletions
diff --git a/simpleperf/record_file.h b/simpleperf/record_file.h index a9fa1c32..76f70436 100644 --- a/simpleperf/record_file.h +++ b/simpleperf/record_file.h @@ -22,6 +22,7 @@ #include <functional> #include <map> #include <memory> +#include <optional> #include <string> #include <unordered_map> #include <vector> @@ -36,6 +37,29 @@ #include "record_file_format.h" #include "thread_tree.h" +namespace simpleperf { + +struct FileFeature { + std::string path; + DsoType type; + uint64_t min_vaddr; + uint64_t file_offset_of_min_vaddr; + std::vector<Symbol> symbols; // used for reading symbols + std::vector<const Symbol*> symbol_ptrs; // used for writing symbols + std::vector<uint64_t> dex_file_offsets; + + FileFeature() {} + + DISALLOW_COPY_AND_ASSIGN(FileFeature); +}; + +struct DebugUnwindFile { + std::string path; + uint64_t size; +}; + +using DebugUnwindFeature = std::vector<DebugUnwindFile>; + // RecordFileWriter writes to a perf record file, like perf.data. // User should call RecordFileWriter::Close() to finish writing the file, otherwise the file will // be removed in RecordFileWriter::~RecordFileWriter(). @@ -43,10 +67,13 @@ class RecordFileWriter { public: static std::unique_ptr<RecordFileWriter> CreateInstance(const std::string& filename); + // If own_fp = true, close fp when we finish writing. + RecordFileWriter(const std::string& filename, FILE* fp, bool own_fp); ~RecordFileWriter(); bool WriteAttrSection(const std::vector<EventAttrWithId>& attr_ids); bool WriteRecord(const Record& record); + bool WriteData(const void* buf, size_t len); uint64_t GetDataSectionSize() const { return data_section_size_; } bool ReadDataSection(const std::function<void(const Record*)>& callback); @@ -57,35 +84,30 @@ class RecordFileWriter { bool WriteCmdlineFeature(const std::vector<std::string>& cmdline); bool WriteBranchStackFeature(); bool WriteAuxTraceFeature(const std::vector<uint64_t>& auxtrace_offset); - bool WriteFileFeatures(const std::vector<Dso*>& files); + bool WriteFileFeatures(const std::vector<Dso*>& dsos); + bool WriteFileFeature(const FileFeature& file); bool WriteMetaInfoFeature(const std::unordered_map<std::string, std::string>& info_map); - bool WriteFeature(int feature, const std::vector<char>& data); + bool WriteDebugUnwindFeature(const DebugUnwindFeature& debug_unwind); + bool WriteFeature(int feature, const char* data, size_t size); bool EndWriteFeatures(); bool Close(); private: - RecordFileWriter(const std::string& filename, FILE* fp); void GetHitModulesInBuffer(const char* p, const char* end, std::vector<std::string>* hit_kernel_modules, std::vector<std::string>* hit_user_files); bool WriteFileHeader(); - bool WriteData(const void* buf, size_t len); bool Write(const void* buf, size_t len); bool Read(void* buf, size_t len); bool GetFilePos(uint64_t* file_pos); bool WriteStringWithLength(const std::string& s); - bool WriteFileFeature(const std::string& file_path, - uint32_t file_type, - uint64_t min_vaddr, - uint64_t file_offset_of_min_vaddr, - const std::vector<const Symbol*>& symbols, - const std::vector<uint64_t>* dex_file_offsets); bool WriteFeatureBegin(int feature); bool WriteFeatureEnd(int feature); const std::string filename_; FILE* record_fp_; + bool own_fp_; perf_event_attr event_attr_; uint64_t attr_section_offset_; @@ -107,9 +129,7 @@ class RecordFileReader { ~RecordFileReader(); - const PerfFileFormat::FileHeader& FileHeader() const { - return header_; - } + const PerfFileFormat::FileHeader& FileHeader() const { return header_; } std::vector<EventAttrWithId> AttrSection() const { std::vector<EventAttrWithId> result(file_attrs_.size()); @@ -120,6 +140,8 @@ class RecordFileReader { return result; } + const std::unordered_map<uint64_t, size_t>& EventIdMap() const { return event_id_to_attr_map_; } + const std::map<int, PerfFileFormat::SectionDesc>& FeatureSectionDescriptors() const { return feature_section_descriptors_; } @@ -127,6 +149,7 @@ class RecordFileReader { return feature_section_descriptors_.find(feature) != feature_section_descriptors_.end(); } bool ReadFeatureSection(int feature, std::vector<char>* data); + bool ReadFeatureSection(int feature, std::string* data); // There are two ways to read records in data section: one is by calling // ReadDataSection(), and [callback] is called for each Record. the other @@ -134,6 +157,7 @@ class RecordFileReader { // If sorted is true, sort records before passing them to callback function. bool ReadDataSection(const std::function<bool(std::unique_ptr<Record>)>& callback); + bool ReadAtOffset(uint64_t offset, void* buf, size_t len); // Read next record. If read successfully, set [record] and return true. // If there is no more records, set [record] to nullptr and return true. @@ -152,11 +176,10 @@ class RecordFileReader { // call, and is updated to point to the next file information. Return true // if read successfully, and return false if there is no more file // information. - bool ReadFileFeature(size_t& read_pos, std::string* file_path, uint32_t* file_type, - uint64_t* min_vaddr, uint64_t* file_offset_of_min_vaddr, - std::vector<Symbol>* symbols, std::vector<uint64_t>* dex_file_offsets); + bool ReadFileFeature(size_t& read_pos, FileFeature* file); const std::unordered_map<std::string, std::string>& GetMetaInfoFeature() { return meta_info_; } + std::optional<DebugUnwindFeature> ReadDebugUnwindFeature(); void LoadBuildIdAndFileFeatures(ThreadTree& thread_tree); @@ -177,7 +200,6 @@ class RecordFileReader { void UseRecordingEnvironment(); std::unique_ptr<Record> ReadRecord(); bool Read(void* buf, size_t len); - bool ReadAtOffset(uint64_t offset, void* buf, size_t len); void ProcessEventIdRecord(const EventIdRecord& r); bool BuildAuxDataLocation(); @@ -214,4 +236,8 @@ class RecordFileReader { DISALLOW_COPY_AND_ASSIGN(RecordFileReader); }; +bool IsPerfDataFile(const std::string& filename); + +} // namespace simpleperf + #endif // SIMPLE_PERF_RECORD_FILE_H_ |