1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef EBPF_DATA_READER_H 17 #define EBPF_DATA_READER_H 18 #include <elf.h> 19 #include <string> 20 #include "ebpf_stdtype.h" 21 #include "htrace_plugin_time_parser.h" 22 #include "process_filter.h" 23 #include "quatra_map.h" 24 #include "string_help.h" 25 #include "trace_data/trace_data_cache.h" 26 #include "trace_streamer_filters.h" 27 #include "unordered_map" 28 29 namespace SysTuning { 30 namespace TraceStreamer { 31 using namespace SysTuning::EbpfStdtype; 32 class EbpfDataReader : private EventParserBase { 33 public: 34 EbpfDataReader(TraceDataCache* dataCache, const TraceStreamerFilters* filter); 35 ~EbpfDataReader() = default; 36 bool InitEbpfData(const std::deque<uint8_t>& dequeBuffer, uint64_t size); 37 const EbpfDataHeader* GetEbpfDataHeader() const; 38 const std::multimap<uint64_t, const FsFixedHeader*>& GetFileSystemEventMap() const; 39 const std::multimap<uint64_t, const PagedMemoryFixedHeader*>& GetPagedMemoryMap() const; 40 const std::multimap<uint64_t, const BIOFixedHeader*>& GetBIOSampleMap() const; 41 const DoubleMap<uint32_t, uint64_t, const MapsFixedHeader*>& GetPidAndStartAddrToMapsAddr() const; 42 const DoubleMap<const ElfEventFixedHeader*, uint64_t, const uint8_t*>& GetElfAddrAndStartValueToSymAddr() const; 43 const std::map<DataIndex, const ElfEventFixedHeader*>& GetElfPathIndexToElfAddr() const; 44 QuatraMap<uint32_t, uint32_t, uint32_t, uint64_t, DataIndex>& GetTracerEventToStrIndexMap(); 45 46 private: 47 bool ReadEbpfData(); 48 bool InitEbpfHeader(); 49 bool ReadItemEventMaps(const uint8_t* buffer, uint32_t size); 50 bool ReadItemSymbolInfo(const uint8_t* buffer, uint32_t size); 51 bool ReadItemEventFs(const uint8_t* buffer, uint32_t size); 52 bool ReadItemEventPagedMemory(const uint8_t* buffer, uint32_t size); 53 bool ReadItemEventBIO(const uint8_t* buffer, uint32_t size); 54 bool ReadItemEventStr(const uint8_t* buffer, uint32_t size); 55 void UpdateElfAddrAndStValueToSymAddrMap(const ElfEventFixedHeader* elfAddr, uint32_t size); 56 void UpdateElfPathIndexToElfAddrMap(const ElfEventFixedHeader* elfAddr, uint32_t size); 57 #if WITH_EBPF_HELP 58 void UpdateEbpfElfSymbolTable(const ElfEventFixedHeader* elfAddr, uint32_t size); 59 #endif 60 61 std::unique_ptr<uint8_t[]> buffer_; 62 uint64_t bufferSize_ = 0; 63 uint64_t unresolvedLen_ = 0; 64 EbpfDataHeader* ebpfDataHeader_; 65 uint8_t* startAddr_ = nullptr; 66 #if WITH_EBPF_HELP 67 uint64_t elfId_ = 0; 68 #endif 69 std::multimap<uint64_t, const FsFixedHeader*> endTsToFsFixedHeader_ = {}; 70 std::multimap<uint64_t, const PagedMemoryFixedHeader*> endTsToPagedMemoryFixedHeader_ = {}; 71 std::multimap<uint64_t, const BIOFixedHeader*> endTsToBIOFixedHeader_ = {}; 72 std::map<DataIndex, const ElfEventFixedHeader*> elfPathIndexToElfFixedHeaderAddr_ = {}; 73 DoubleMap<uint32_t, uint64_t, const MapsFixedHeader*> pidAndStartAddrToMapsAddr_; 74 DoubleMap<const ElfEventFixedHeader*, uint64_t, const uint8_t*> elfAddrAndStValueToSymAddr_; 75 QuatraMap<uint32_t, uint32_t, uint32_t, uint64_t, DataIndex> tracerEventToStrIndex_; 76 }; 77 } // namespace TraceStreamer 78 } // namespace SysTuning 79 #endif // EBPF_DATA_READER_H 80