• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "event_parser_base.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     SymbolAndFilePathIndex GetSymbolNameIndexFromElfSym(uint64_t ip);
46 
47 private:
48     bool ReadEbpfData();
49     bool InitEbpfHeader();
50     bool ReadItemEventMaps(const uint8_t* buffer, uint32_t size);
51     bool ReadItemSymbolInfo(const uint8_t* buffer, uint32_t size);
52     bool ReaItemKernelSymbolInfo(const uint8_t* buffer, uint32_t size);
53     bool ReadItemEventFs(const uint8_t* buffer, uint32_t size);
54     bool ReadItemEventPagedMemory(const uint8_t* buffer, uint32_t size);
55     bool ReadItemEventBIO(const uint8_t* buffer, uint32_t size);
56     bool ReadItemEventStr(const uint8_t* buffer, uint32_t size);
57     template <class T>
58     void AddSymbolsToTable(T* firstSymbolAddr, const int size, const ElfEventFixedHeader* elfAddr);
59     void UpdateElfAddrAndStValueToSymAddrMap(const ElfEventFixedHeader* elfAddr, uint32_t size);
60     void ReadKernelSymAddrMap(const KernelSymbolInfoHeader* elfAddr, uint32_t size);
61     void UpdateElfPathIndexToElfAddrMap(const ElfEventFixedHeader* elfAddr, uint32_t size);
62 #if WITH_EBPF_HELP
63     template <class T>
64     void AppendSymbolsToTable(T* firstSymbolAddr, const int size);
65     void UpdateEbpfElfSymbolTable(const ElfEventFixedHeader* elfAddr, uint32_t size);
66 #endif
67 public:
68     uint64_t maxKernelAddr_ = 0;
69     uint64_t minKernelAddr_ = std::numeric_limits<uint64_t>::max();
70 
71 private:
72     std::unique_ptr<uint8_t[]> buffer_;
73     uint64_t bufferSize_ = 0;
74     uint64_t unresolvedLen_ = 0;
75     EbpfDataHeader* ebpfDataHeader_;
76     uint8_t* startAddr_ = nullptr;
77 #if WITH_EBPF_HELP
78     uint64_t elfId_ = 0;
79 #endif
80     std::multimap<uint64_t, const FsFixedHeader*> endTsToFsFixedHeader_ = {};
81     std::multimap<uint64_t, const PagedMemoryFixedHeader*> endTsToPagedMemoryFixedHeader_ = {};
82     std::multimap<uint64_t, const BIOFixedHeader*> endTsToBIOFixedHeader_ = {};
83     std::map<DataIndex, const ElfEventFixedHeader*> elfPathIndexToElfFixedHeaderAddr_ = {};
84     DoubleMap<uint32_t, uint64_t, const MapsFixedHeader*> pidAndStartAddrToMapsAddr_;
85     DoubleMap<const ElfEventFixedHeader*, uint64_t, const uint8_t*> elfAddrAndStValueToSymAddr_;
86     QuatraMap<uint32_t, uint32_t, uint32_t, uint64_t, DataIndex> tracerEventToStrIndex_;
87     DataIndex kernelFilePath_;
88     struct AddrDesc {
89         uint64_t size = 0;
90         DataIndex name = 0;
91     };
92     std::map<uint64_t, AddrDesc> kernelSymbolMap_ = {};
93     static const uint32_t MAX_SYMBOL_LENGTH = 256;
94     char strSymbolName_[MAX_SYMBOL_LENGTH] = {0};
95 };
96 } // namespace TraceStreamer
97 } // namespace SysTuning
98 #endif // EBPF_DATA_READER_H
99