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 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 class FrameInfo { 33 public: FrameInfo()34 FrameInfo() 35 { 36 filePathId_ = INVALID_UINT32; 37 ip_ = INVALID_UINT64; 38 symbolIndex_ = INVALID_UINT64; 39 offset_ = INVALID_UINT64; 40 symbolOffset_ = INVALID_UINT64; 41 symVaddr_ = INVALID_UINT64; 42 } 43 uint32_t filePathId_; 44 uint64_t ip_; 45 uint64_t symbolIndex_; 46 uint64_t offset_; 47 uint64_t symbolOffset_; 48 uint64_t symVaddr_; 49 }; 50 struct NativeHookMetaData { NativeHookMetaDataNativeHookMetaData51 NativeHookMetaData(const std::shared_ptr<const std::string>& seg, 52 std::unique_ptr<ProtoReader::NativeHookData_Reader> reader) 53 : seg_(seg), reader_(std::move(reader)) 54 { 55 } 56 std::shared_ptr<const std::string> seg_; 57 std::unique_ptr<ProtoReader::NativeHookData_Reader> reader_; 58 }; 59 class OfflineSymbolizationFilter : public FilterBase { 60 public: 61 OfflineSymbolizationFilter(TraceDataCache* dataCache, const TraceStreamerFilters* filter); 62 ~OfflineSymbolizationFilter() override = default; 63 std::shared_ptr<FrameInfo> OfflineSymbolizationByIp(uint64_t ipid, uint64_t ip); 64 std::shared_ptr<std::vector<std::shared_ptr<FrameInfo>>> OfflineSymbolization( 65 const std::shared_ptr<std::vector<uint64_t>> ips); 66 DataIndex OfflineSymbolizationByVaddr(uint64_t symVaddr, DataIndex filePathIndex); 67 68 protected: 69 enum SYSTEM_ENTRY_VALUE { ELF32_SYM = 16, ELF64_SYM = 24 }; 70 using StartAddrToMapsInfoType = std::map<uint64_t, std::shared_ptr<ProtoReader::MapsInfo_Reader>>; 71 DoubleMap<uint32_t, uint64_t, const uint8_t*> filePathIdAndStValueToSymAddr_; 72 DoubleMap<std::shared_ptr<ProtoReader::SymbolTable_Reader>, uint64_t, const uint8_t*> 73 symbolTablePtrAndStValueToSymAddr_; 74 // first is ipid, second is startAddr, third is MapsInfo ptr 75 DoubleMap<uint64_t /* ipid */, uint64_t /* startAddr */, std::shared_ptr<ProtoReader::MapsInfo_Reader>> 76 ipidToStartAddrToMapsInfoMap_; 77 // first is ipid, second is ip, third is FrameInfo 78 DoubleMap<uint64_t, uint64_t, std::shared_ptr<FrameInfo>> ipidToIpToFrameInfo_; 79 DoubleMap<uint64_t /* ipid */, uint32_t /* filePathId */, std::shared_ptr<ProtoReader::SymbolTable_Reader>> 80 ipidTofilePathIdToSymbolTableMap_; 81 std::unordered_map<uint32_t, std::shared_ptr<ElfSymbolTable>> filePathIdToImportSymbolTableMap_ = {}; 82 83 using IpToFrameInfoType = std::map<uint64_t, std::shared_ptr<FrameInfo>>; 84 85 std::vector<std::shared_ptr<const std::string>> segs_ = {}; 86 const uint32_t SINGLE_PROC_IPID = 0; 87 bool isSingleProcData_ = true; 88 89 private: 90 template <class T> 91 static void GetSymbolStartMaybeUpdateFrameInfo(T* elfSym, 92 uint32_t& symbolStart, 93 uint64_t symVaddr, 94 uint64_t ip, 95 FrameInfo* frameInfo); 96 bool FillFrameInfo(const std::shared_ptr<FrameInfo>& frameInfo, uint64_t ip, uint64_t ipid); 97 bool CalcSymInfo(uint64_t ipid, 98 uint64_t ip, 99 uint32_t& symbolStart, 100 std::shared_ptr<FrameInfo>& frameInfo, 101 std::shared_ptr<ProtoReader::SymbolTable_Reader>& symbolTable); 102 const uint64_t usefulIpMask_ = 0xffffff0000000000; 103 uint64_t vmStart_ = INVALID_UINT64; 104 uint64_t vmOffset_ = INVALID_UINT64; 105 }; 106 107 } // namespace TraceStreamer 108 } // namespace SysTuning 109 #endif 110