• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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 <vector>
24 #include "debug_logger.h"
25 #include "mem_map_item.h"
26 #include "logging.h"
27 #include "register.h"
28 #include "symbols_file.h"
29 #include "utilities.h"
30 
31 namespace OHOS {
32 namespace Developtools {
33 namespace NativeDaemon {
34 /*
35 03284000-03289000 r--p 00000000 b3:05 289        /system/bin/sh
36 032b7000-032b9000 rw-p 00000000 00:00 0
37 aff60000-aff96000 r--p 00000000 b3:05 923        /system/lib/libc++.so
38 affeb000-affed000 rw-p 00000000 00:00 0
39 b0023000-b0024000 r--p 00000000 b3:05 959        /system/lib/libdl.so
40 */
41 const std::string MMAP_NAME_HEAP = "[heap]";
42 const std::string MMAP_NAME_ANON = "[anon]";
43 
44 class VirtualRuntime;
45 
46 class VirtualThread {
47 public:
48     VirtualThread(const VirtualThread &) = delete;
49     VirtualThread &operator=(const VirtualThread &) = delete;
50 
51     VirtualThread(pid_t pid,
52                   pid_t tid,
53                   const std::set<std::unique_ptr<SymbolsFile>, CCompareSymbolsFile>& symbolsFiles,
54                   VirtualRuntime* runtime,
55                   bool parseFlag = true);
56 
~VirtualThread()57     virtual ~VirtualThread()
58     {
59         if (user_regs != nullptr) {
60             delete []user_regs;
61             user_regs = nullptr;
62         }
63     }
64 
65     std::string ReadThreadName(pid_t tid);
66 
67     pid_t pid_;
68     pid_t tid_;
69     std::string name_;
70 
GetMaps()71     const std::vector<MemMapItem> &GetMaps() const
72     {
73         return *memMaps_;
74     }
75 
76     bool ParseMap(std::vector<MemMapItem> &memMaps, bool update = false);
77     void CreateMapItem(const std::string filename, uint64_t begin, uint64_t len, uint64_t offset);
78     const MemMapItem *FindMapByAddr(uint64_t addr) const;
79     const MemMapItem *FindMapByAddr2(uint64_t addr) const;
80     const MemMapItem *FindMapByFileInfo(const std::string name, uint64_t offset) const;
81     SymbolsFile *FindSymbolsFileByMap(const MemMapItem &inMap) const;
82     bool ReadRoMemory(uint64_t vaddr, uint8_t *data, size_t size) const;
83 #ifdef HIPERF_DEBUG
84     void ReportVaddrMapMiss(uint64_t vaddr) const;
85 #endif
86 public:
87     u64* user_regs;
88     u64 reg_nr;
89     // caller want to check if new mmap is legal
90     static bool IsLegalFileName(const std::string &filename);
91 
92 private:
93     void SortMemMaps();
94 #ifdef DEBUG_TIME
95     bool IsSorted() const;
96 #endif
97     const std::set<std::unique_ptr<SymbolsFile>, CCompareSymbolsFile> & symbolsFiles_;
98 
99     // thread must use ref from process
100     std::vector<MemMapItem>* memMaps_;
101 
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     VirtualRuntime* virtualruntime_;
109     FRIEND_TEST(VirtualThreadTest, ReadRoMemory);
110 };
111 } // namespace NativeDaemon
112 } // namespace Developtools
113 } // namespace OHOS
114 #endif