1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. 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_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 using ElfDoubleMap = DoubleMap<const ElfEventFixedHeader *, uint64_t, const uint8_t *>; 39 EbpfDataReader(TraceDataCache *dataCache, const TraceStreamerFilters *filter); 40 ~EbpfDataReader() = default; 41 bool InitEbpfData(const std::deque<uint8_t> &dequeBuffer, uint64_t size); 42 const EbpfDataHeader *GetEbpfDataHeader() const; 43 const std::multimap<uint64_t, const FsFixedHeader *> &GetFileSystemEventMap() const; 44 const std::multimap<uint64_t, const PagedMemoryFixedHeader *> &GetPagedMemoryMap() const; 45 const std::multimap<uint64_t, const BIOFixedHeader *> &GetBIOSampleMap() const; 46 const DoubleMap<uint32_t, uint64_t, const MapsFixedHeader *> &GetPidAndStartAddrToMapsAddr() const; 47 const ElfDoubleMap &GetElfAddrAndStartValueToSymAddr() const; 48 const std::map<DataIndex, const ElfEventFixedHeader *> &GetElfPathIndexToElfAddr() const; 49 QuatraMap<uint32_t, uint32_t, uint32_t, uint64_t, DataIndex> &GetTracerEventToStrIndexMap(); 50 EbpfSymbolInfo GetSymbolNameIndexFromElfSym(uint64_t ip); 51 52 private: 53 bool ReadEbpfData(); 54 bool InitEbpfHeader(); 55 bool ReadItemEventMaps(const uint8_t *buffer, uint32_t size); 56 bool ReadItemSymbolInfo(const uint8_t *buffer, uint32_t size); 57 bool ReaItemKernelSymbolInfo(const uint8_t *buffer, uint32_t size); 58 bool ReadItemEventFs(const uint8_t *buffer, uint32_t size); 59 bool ReadItemEventPagedMemory(const uint8_t *buffer, uint32_t size); 60 bool ReadItemEventBIO(const uint8_t *buffer, uint32_t size); 61 bool ReadItemEventStr(const uint8_t *buffer, uint32_t size); 62 bool EbpfTypeHandle(EbpfTypeAndLength *dataTitle, const uint8_t *startAddr); 63 template <class T> 64 void AddSymbolsToTable(T *firstSymbolAddr, const int size, const ElfEventFixedHeader *elfAddr); 65 void UpdateElfAddrAndStValueToSymAddrMap(const ElfEventFixedHeader *elfAddr, uint32_t size); 66 void ReadKernelSymAddrMap(const KernelSymbolInfoHeader *elfAddr, uint32_t size); 67 void UpdateElfPathIndexToElfAddrMap(const ElfEventFixedHeader *elfAddr, uint32_t size); 68 69 public: 70 uint64_t maxKernelAddr_ = 0; 71 uint64_t minKernelAddr_ = std::numeric_limits<uint64_t>::max(); 72 73 private: 74 std::unique_ptr<uint8_t[]> buffer_; 75 uint64_t bufferSize_ = 0; 76 uint64_t unresolvedLen_ = 0; 77 EbpfDataHeader *ebpfDataHeader_; 78 uint8_t *startAddr_ = nullptr; 79 std::multimap<uint64_t, const FsFixedHeader *> endTsToFsFixedHeader_ = {}; 80 std::multimap<uint64_t, const PagedMemoryFixedHeader *> endTsToPagedMemoryFixedHeader_ = {}; 81 std::multimap<uint64_t, const BIOFixedHeader *> endTsToBIOFixedHeader_ = {}; 82 std::map<DataIndex, const ElfEventFixedHeader *> elfPathIndexToElfFixedHeaderAddr_ = {}; 83 DoubleMap<uint32_t, uint64_t, const MapsFixedHeader *> pidAndStartAddrToMapsAddr_; 84 ElfDoubleMap elfAddrAndStValueToSymAddr_; 85 QuatraMap<uint32_t, uint32_t, uint32_t, uint64_t, DataIndex> tracerEventToStrIndex_; 86 DataIndex kernelFilePath_; 87 struct AddrDesc { 88 uint64_t size = 0; 89 DataIndex name = 0; 90 }; 91 std::map<uint64_t, AddrDesc> kernelSymbolMap_ = {}; 92 static const uint32_t maxSymbolLength = 256; 93 char strSymbolName_[maxSymbolLength] = {0}; 94 }; 95 } // namespace TraceStreamer 96 } // namespace SysTuning 97 #endif // EBPF_DATA_READER_H 98