1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. 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 #ifndef HIPERF_VIRTUAL_THREAD_H 16 #define HIPERF_VIRTUAL_THREAD_H 17 #include <assert.h> 18 #include <cinttypes> 19 #include <functional> 20 #include <pthread.h> 21 #include <set> 22 #include <unordered_set> 23 #include <unordered_map> 24 #include <vector> 25 #include "debug_logger.h" 26 #include "dfx_maps.h" 27 #include "logging.h" 28 #include "register.h" 29 #include "symbols_file.h" 30 #include "utilities.h" 31 32 namespace OHOS { 33 namespace Developtools { 34 namespace NativeDaemon { 35 /* 36 03284000-03289000 r--p 00000000 b3:05 289 /system/bin/sh 37 032b7000-032b9000 rw-p 00000000 00:00 0 38 aff60000-aff96000 r--p 00000000 b3:05 923 /system/lib/libc++.so 39 affeb000-affed000 rw-p 00000000 00:00 0 40 b0023000-b0024000 r--p 00000000 b3:05 959 /system/lib/libdl.so 41 */ 42 using namespace OHOS::HiviewDFX; 43 class MemMaps : public DfxMaps { 44 public: MemMaps()45 MemMaps() {} MemMaps(uint32_t filePathId)46 MemMaps(uint32_t filePathId) : filePathId_(filePathId) {} AddMap(std::shared_ptr<DfxMap> map,bool firstMap)47 void AddMap(std::shared_ptr<DfxMap> map, bool firstMap) 48 { 49 if (firstMap) { 50 soBegin_ = map->begin; 51 soEnd_ = map->end; 52 name_ = map->name; 53 } else { 54 maps_.back()->end = map->begin; 55 map->prevMap = maps_.back(); 56 } 57 maps_.emplace_back(map); 58 } ReplaceFront(std::shared_ptr<DfxMap> map)59 bool ReplaceFront(std::shared_ptr<DfxMap> map) 60 { 61 if (maps_.size() <= 1) { 62 maps_.clear(); 63 AddMap(map, true); 64 } else { 65 return false; 66 } 67 return true; 68 } 69 uint64_t soBegin_ {0}; 70 uint64_t soEnd_ {0}; 71 uint32_t filePathId_ {0}; // for maps item filePath id 72 std::string name_; 73 bool isReported_ {false}; // indicates whether information about item has been reported 74 }; 75 76 const std::string MMAP_NAME_HEAP = "[heap]"; 77 const std::string MMAP_NAME_ANON = "[anon]"; 78 79 class VirtualRuntime; 80 81 class VirtualThread { 82 public: 83 VirtualThread(const VirtualThread &) = delete; 84 VirtualThread &operator=(const VirtualThread &) = delete; 85 86 VirtualThread(pid_t pid, 87 pid_t tid, 88 const std::unordered_map<std::string, std::unique_ptr<SymbolsFile>>& symbolsFiles, 89 VirtualRuntime* runtime, 90 bool parseFlag = true); 91 ~VirtualThread()92 virtual ~VirtualThread() {} 93 94 std::string ReadThreadName(pid_t tid); 95 96 pid_t pid_ {0}; 97 pid_t tid_ {0}; 98 std::string name_; 99 GetMaps()100 const std::vector<std::shared_ptr<DfxMap>> &GetMaps() const 101 { 102 return *maps_; 103 } 104 void SortMaps(); 105 bool ParseMap(std::vector<std::shared_ptr<DfxMap>> &memMaps, bool update = false); 106 void CreateMapItem(const std::string filename, uint64_t begin, uint64_t len, uint64_t offset); 107 const std::shared_ptr<DfxMap> FindMapByAddr(uint64_t addr) const; 108 const std::pair<std::shared_ptr<MemMaps>, uint32_t> FindMemMapsByAddr(uint64_t addr) const; 109 const std::shared_ptr<DfxMap> FindMapByFileInfo(const std::string name, uint64_t offset) const; 110 SymbolsFile *FindSymbolsFileByMap(std::shared_ptr<DfxMap> inMap) const; 111 SymbolsFile *FindSymbolsFileByName(const std::string &name) const; 112 bool ReadRoMemory(uint64_t vaddr, uint8_t *data, size_t size) const; 113 #ifdef HIPERF_DEBUG 114 void ReportVaddrMapMiss(uint64_t vaddr) const; 115 #endif 116 public: 117 118 private: 119 #ifdef DEBUG_TIME 120 bool IsSorted() const; 121 #endif 122 const std::unordered_map<std::string, std::unique_ptr<SymbolsFile>>& symbolsFiles_; 123 124 // thread must use ref from process 125 126 std::vector<std::shared_ptr<DfxMap>>* maps_ = {}; 127 #ifdef HIPERF_DEBUG 128 mutable std::unordered_set<uint64_t> missedRuntimeVaddr_; 129 #endif 130 #ifdef DEBUG_MISS_SYMBOL 131 mutable std::vector<std::string> missedSymbolFile_; 132 #endif 133 VirtualRuntime* virtualruntime_; 134 }; 135 } // namespace NativeDaemon 136 } // namespace Developtools 137 } // namespace OHOS 138 #endif