• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OFFLINE_SYMBOLIZATION_FILTER_H
17 #define OFFLINE_SYMBOLIZATION_FILTER_H
18 #include "double_map.h"
19 #ifndef is_linux
20 #include "dfx_nonlinux_define.h"
21 #else
22 #include <elf.h>
23 #endif
24 #include <unordered_map>
25 #include "native_hook_result.pbreader.h"
26 #include "process_filter.h"
27 #include "proto_reader.h"
28 #include "string_help.h"
29 #include "ts_common.h"
30 namespace SysTuning {
31 namespace TraceStreamer {
32 constexpr uint64_t ALLOC_IP_MASK = 0xFFFFFF0000000000;
33 constexpr uint64_t JS_IP_MASK = 0xFFFFFE0000000000;
34 class FrameInfo {
35 public:
FrameInfo()36     FrameInfo()
37     {
38         filePathId_ = INVALID_UINT32;
39         ip_ = INVALID_UINT64;
40         symbolIndex_ = INVALID_UINT64;
41         offset_ = INVALID_UINT64;
42         symbolOffset_ = INVALID_UINT64;
43         symVaddr_ = INVALID_UINT64;
44     }
45     uint32_t filePathId_;
46     uint64_t ip_;
47     uint64_t symbolIndex_;
48     uint64_t offset_;
49     uint64_t symbolOffset_;
50     uint64_t symVaddr_;
51 };
52 struct NativeHookMetaData {
NativeHookMetaDataNativeHookMetaData53     NativeHookMetaData(const std::shared_ptr<const std::string> &seg,
54                        std::unique_ptr<ProtoReader::NativeHookData_Reader> reader)
55         : seg_(seg), reader_(std::move(reader))
56     {
57     }
58     std::shared_ptr<const std::string> seg_;
59     std::unique_ptr<ProtoReader::NativeHookData_Reader> reader_;
60 };
61 class OfflineSymbolizationFilter : public FilterBase {
62 public:
63     OfflineSymbolizationFilter(TraceDataCache *dataCache, const TraceStreamerFilters *filter);
64     ~OfflineSymbolizationFilter() override = default;
65     std::shared_ptr<FrameInfo> OfflineSymbolizationByIp(uint64_t ipid, uint64_t ip);
66 
67 protected:
68     enum SYSTEM_ENTRY_VALUE { ELF32_SYM = 16, ELF64_SYM = 24 };
69     using StartAddrToMapsInfoType = std::map<uint64_t, std::shared_ptr<ProtoReader::MapsInfo_Reader>>;
70     DoubleMap<uint32_t, uint64_t, const uint8_t *> filePathIdAndStValueToSymAddr_;
71     DoubleMap<std::shared_ptr<ProtoReader::SymbolTable_Reader>, uint64_t, const uint8_t *>
72         symbolTablePtrAndStValueToSymAddr_;
73     // first is ipid, second is startAddr, third is MapsInfo ptr
74     DoubleMap<uint64_t /* ipid */, uint64_t /* startAddr */, std::shared_ptr<ProtoReader::MapsInfo_Reader>>
75         ipidToStartAddrToMapsInfoMap_;
76     // first is ipid, second is ip, third is FrameInfo
77     DoubleMap<uint64_t, uint64_t, std::shared_ptr<FrameInfo>> ipidToIpToFrameInfo_;
78     DoubleMap<uint64_t /* ipid */, uint32_t /* filePathId */, std::shared_ptr<ProtoReader::SymbolTable_Reader>>
79         ipidTofilePathIdToSymbolTableMap_;
80     std::unordered_map<uint32_t, std::shared_ptr<ElfSymbolTable>> filePathIdToImportSymbolTableMap_ = {};
81 
82     using IpToFrameInfoType = std::map<uint64_t, std::shared_ptr<FrameInfo>>;
83 
84     std::vector<std::shared_ptr<const std::string>> segs_ = {};
85     const uint32_t SINGLE_PROC_IPID = 0;
86     bool isSingleProcData_ = true;
87 
88 private:
89     template <class T>
90     static void GetSymbolStartMaybeUpdateFrameInfo(T *elfSym,
91                                                    uint32_t &symbolStart,
92                                                    uint64_t symVaddr,
93                                                    uint64_t ip,
94                                                    FrameInfo *frameInfo);
95     bool FillFrameInfo(const std::shared_ptr<FrameInfo> &frameInfo, uint64_t ip, uint64_t ipid);
96     bool CalcSymInfo(uint64_t ipid,
97                      uint64_t ip,
98                      uint32_t &symbolStart,
99                      std::shared_ptr<FrameInfo> &frameInfo,
100                      std::shared_ptr<ProtoReader::SymbolTable_Reader> &symbolTable);
101     uint64_t vmStart_ = INVALID_UINT64;
102     uint64_t vmOffset_ = INVALID_UINT64;
103 };
104 
105 } // namespace TraceStreamer
106 } // namespace SysTuning
107 #endif
108