1 /* 2 * Copyright (c) 2022 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 #ifndef HIPERF_VIRTUAL_THREAD_H 16 #define HIPERF_VIRTUAL_THREAD_H 17 18 #include <cinttypes> 19 #include <functional> 20 #include <unordered_set> 21 22 #include "debug_logger.h" 23 #include "mem_map_item.h" 24 #include "perf_event_record.h" 25 #include "symbols_file.h" 26 27 namespace OHOS { 28 namespace Developtools { 29 namespace HiPerf { 30 /* 31 03284000-03289000 r--p 00000000 b3:05 289 /system/bin/sh 32 032b7000-032b9000 rw-p 00000000 00:00 0 33 aff60000-aff96000 r--p 00000000 b3:05 923 /system/lib/libc++.so 34 affeb000-affed000 rw-p 00000000 00:00 0 35 b0023000-b0024000 r--p 00000000 b3:05 959 /system/lib/libdl.so 36 */ 37 const std::string MMAP_NAME_HEAP = "[heap]"; 38 const std::string MMAP_NAME_ANON = "[anon]"; 39 40 class VirtualThread { 41 public: 42 VirtualThread(const VirtualThread &) = delete; 43 VirtualThread &operator=(const VirtualThread &) = delete; 44 VirtualThread(pid_t pid,const std::vector<std::unique_ptr<SymbolsFile>> & symbolsFiles)45 VirtualThread(pid_t pid, const std::vector<std::unique_ptr<SymbolsFile>> &symbolsFiles) 46 : pid_(pid), 47 tid_(pid), 48 symbolsFiles_(symbolsFiles), 49 processMemMaps_(), 50 memMaps_(processMemMaps_), 51 parent_(*this) {}; 52 VirtualThread(pid_t pid,pid_t tid,VirtualThread & thread,const std::vector<std::unique_ptr<SymbolsFile>> & symbolsFiles)53 VirtualThread(pid_t pid, pid_t tid, VirtualThread &thread, 54 const std::vector<std::unique_ptr<SymbolsFile>> &symbolsFiles) 55 : pid_(pid), 56 tid_(tid), 57 symbolsFiles_(symbolsFiles), 58 processMemMaps_(), 59 memMaps_(thread.processMemMaps_), 60 parent_(thread) 61 { 62 HLOG_ASSERT(pid != tid); 63 HLOGV("%d %d map from parent size is %zu", pid, tid, memMaps_.size()); 64 }; 65 66 pid_t pid_; 67 pid_t tid_; 68 std::string name_; 69 GetMaps()70 const std::vector<MemMapItem> &GetMaps() const 71 { 72 return memMaps_; 73 } 74 75 void ParseMap(); 76 void CreateMapItem(const std::string filename, uint64_t begin, uint64_t len, uint64_t offset); 77 const MemMapItem *FindMapByAddr(uint64_t addr) const; 78 const MemMapItem *FindMapByAddr2(uint64_t addr) const; 79 const MemMapItem *FindMapByFileInfo(const std::string name, uint64_t offset) const; 80 SymbolsFile *FindSymbolsFileByMap(const MemMapItem &inMap) const; 81 bool ReadRoMemory(uint64_t vaddr, uint8_t *data, size_t size) const; 82 #ifdef HIPERF_DEBUG 83 void ReportVaddrMapMiss(uint64_t vaddr) const; 84 #endif 85 // caller want to check if new mmap is legal 86 static bool IsLegalFileName(const std::string &filename); 87 88 private: 89 void SortMemMaps(); 90 #ifdef DEBUG_TIME 91 bool IsSorted() const; 92 #endif 93 const std::vector<std::unique_ptr<SymbolsFile>> &symbolsFiles_; 94 95 // proc/xx/map 96 // use to put the parent thread's map 97 // only process have memmap 98 std::vector<MemMapItem> processMemMaps_; 99 // thread must use ref from process 100 std::vector<MemMapItem> &memMaps_; 101 VirtualThread &parent_; 102 #ifdef HIPERF_DEBUG 103 mutable std::unordered_set<uint64_t> missedRuntimeVaddr_; 104 #endif 105 #ifdef DEBUG_MISS_SYMBOL 106 mutable std::vector<std::string> missedSymbolFile_; 107 #endif 108 FRIEND_TEST(VirtualThreadTest, ReadRoMemory); 109 }; 110 } // namespace HiPerf 111 } // namespace Developtools 112 } // namespace OHOS 113 #endif