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 #ifndef is_linux 19 #include "dfx_nonlinux_define.h" 20 #else 21 #include <elf.h> 22 #endif 23 #include <string> 24 #include "ebpf_data_structure.h" 25 #include "event_parser_base.h" 26 #include "process_filter.h" 27 #include "quatra_map.h" 28 #include "string_help.h" 29 #include "trace_data/trace_data_cache.h" 30 #include "trace_streamer_filters.h" 31 #include "unordered_map" 32 33 namespace SysTuning { 34 namespace TraceStreamer { 35 using namespace SysTuning::EbpfStdtype; 36 class EbpfDataReader : private EventParserBase { 37 public: 38 EbpfDataReader(TraceDataCache* dataCache, const TraceStreamerFilters* filter); 39 ~EbpfDataReader() = default; 40 bool InitEbpfData(const std::deque<uint8_t>& dequeBuffer, uint64_t size); 41 const EbpfDataHeader* GetEbpfDataHeader() const; 42 const std::multimap<uint64_t, const FsFixedHeader*>& GetFileSystemEventMap() const; 43 const std::multimap<uint64_t, const PagedMemoryFixedHeader*>& GetPagedMemoryMap() const; 44 const std::multimap<uint64_t, const BIOFixedHeader*>& GetBIOSampleMap() const; 45 const DoubleMap<uint32_t, uint64_t, const MapsFixedHeader*>& GetPidAndStartAddrToMapsAddr() const; 46 const DoubleMap<const ElfEventFixedHeader*, uint64_t, const uint8_t*>& GetElfAddrAndStartValueToSymAddr() const; 47 const std::map<DataIndex, const ElfEventFixedHeader*>& GetElfPathIndexToElfAddr() const; 48 QuatraMap<uint32_t, uint32_t, uint32_t, uint64_t, DataIndex>& GetTracerEventToStrIndexMap(); 49 EbpfSymbolInfo GetSymbolNameIndexFromElfSym(uint64_t ip); 50 51 private: 52 bool ReadEbpfData(); 53 bool InitEbpfHeader(); 54 bool ReadItemEventMaps(const uint8_t* buffer, uint32_t size); 55 bool ReadItemSymbolInfo(const uint8_t* buffer, uint32_t size); 56 bool ReaItemKernelSymbolInfo(const uint8_t* buffer, uint32_t size); 57 bool ReadItemEventFs(const uint8_t* buffer, uint32_t size); 58 bool ReadItemEventPagedMemory(const uint8_t* buffer, uint32_t size); 59 bool ReadItemEventBIO(const uint8_t* buffer, uint32_t size); 60 bool ReadItemEventStr(const uint8_t* buffer, uint32_t size); 61 bool EbpfTypeHandle(EbpfTypeAndLength* dataTitle, const uint8_t* startAddr_); 62 template <class T> 63 void AddSymbolsToTable(T* firstSymbolAddr, const int size, const ElfEventFixedHeader* elfAddr); 64 void UpdateElfAddrAndStValueToSymAddrMap(const ElfEventFixedHeader* elfAddr, uint32_t size); 65 void ReadKernelSymAddrMap(const KernelSymbolInfoHeader* elfAddr, uint32_t size); 66 void UpdateElfPathIndexToElfAddrMap(const ElfEventFixedHeader* elfAddr, uint32_t size); 67 68 public: 69 uint64_t maxKernelAddr_ = 0; 70 uint64_t minKernelAddr_ = std::numeric_limits<uint64_t>::max(); 71 72 private: 73 std::unique_ptr<uint8_t[]> buffer_; 74 uint64_t bufferSize_ = 0; 75 uint64_t unresolvedLen_ = 0; 76 EbpfDataHeader* ebpfDataHeader_; 77 uint8_t* startAddr_ = nullptr; 78 std::multimap<uint64_t, const FsFixedHeader*> endTsToFsFixedHeader_ = {}; 79 std::multimap<uint64_t, const PagedMemoryFixedHeader*> endTsToPagedMemoryFixedHeader_ = {}; 80 std::multimap<uint64_t, const BIOFixedHeader*> endTsToBIOFixedHeader_ = {}; 81 std::map<DataIndex, const ElfEventFixedHeader*> elfPathIndexToElfFixedHeaderAddr_ = {}; 82 DoubleMap<uint32_t, uint64_t, const MapsFixedHeader*> pidAndStartAddrToMapsAddr_; 83 DoubleMap<const ElfEventFixedHeader*, uint64_t, const uint8_t*> elfAddrAndStValueToSymAddr_; 84 QuatraMap<uint32_t, uint32_t, uint32_t, uint64_t, DataIndex> tracerEventToStrIndex_; 85 DataIndex kernelFilePath_; 86 struct AddrDesc { 87 uint64_t size = 0; 88 DataIndex name = 0; 89 }; 90 std::map<uint64_t, AddrDesc> kernelSymbolMap_ = {}; 91 static const uint32_t MAX_SYMBOL_LENGTH = 256; 92 char strSymbolName_[MAX_SYMBOL_LENGTH] = {0}; 93 }; 94 } // namespace TraceStreamer 95 } // namespace SysTuning 96 #endif // EBPF_DATA_READER_H 97