1 /* 2 * Copyright (c) 2022-2023 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 DFX_SYMBOLS_H 17 #define DFX_SYMBOLS_H 18 19 #include <cstdint> 20 #include <dlfcn.h> 21 #include <string> 22 #include <vector> 23 #include <unordered_map> 24 #include "dfx_memory.h" 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 typedef struct SymbolInfo { 30 uint64_t start; 31 uint64_t end; 32 uint32_t name; 33 uint16_t ndx; 34 uint8_t type; 35 std::string funcName; 36 } SymbolInfo; 37 #ifdef __cplusplus 38 }; 39 #endif 40 struct unw_addr_space; 41 42 namespace OHOS { 43 namespace HiviewDFX { 44 using RustDemangleFn = char*(*)(const char *); 45 class DfxSymbols final { 46 public: 47 DfxSymbols(); ~DfxSymbols()48 ~DfxSymbols() 49 { 50 if (rustDemangleFn_ != nullptr) { 51 rustDemangleFn_ = nullptr; 52 } 53 54 if (rustDemangleLibHandle_ != nullptr) { 55 dlclose(rustDemangleLibHandle_); 56 rustDemangleLibHandle_ = nullptr; 57 } 58 }; 59 60 bool GetNameAndOffsetByPc(struct unw_addr_space *as, uint64_t pc, std::string& name, uint64_t& offset); 61 bool GetNameAndOffsetByPc(std::shared_ptr<DfxMemory> memory, uint64_t pc, std::string& name, uint64_t& offset); 62 63 private: 64 bool GetNameAndOffsetByPc(uint64_t pc, std::string& name, uint64_t& offset); 65 bool Demangle(const char* buf, const int len, std::string& funcName); 66 bool FindRustDemangleFunction(); 67 68 private: 69 std::vector<SymbolInfo> symbols_; 70 bool hasTryLoadRustDemangleLib_ = false; 71 RustDemangleFn rustDemangleFn_ = nullptr; 72 void* rustDemangleLibHandle_ = nullptr; 73 }; 74 } // namespace HiviewDFX 75 } // namespace OHOS 76 #endif 77