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
32namespace OHOS {
33namespace Developtools {
34namespace NativeDaemon {
35/*
3603284000-03289000 r--p 00000000 b3:05 289        /system/bin/sh
37032b7000-032b9000 rw-p 00000000 00:00 0
38aff60000-aff96000 r--p 00000000 b3:05 923        /system/lib/libc++.so
39affeb000-affed000 rw-p 00000000 00:00 0
40b0023000-b0024000 r--p 00000000 b3:05 959        /system/lib/libdl.so
41*/
42using namespace OHOS::HiviewDFX;
43class MemMaps : public DfxMaps {
44public:
45    MemMaps() {}
46    MemMaps(uint32_t filePathId) : filePathId_(filePathId) {}
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    }
59    uint64_t soBegin_ {0};
60    uint64_t soEnd_ {0};
61    uint32_t filePathId_ {0}; // for maps item filePath id
62    std::string name_;
63    bool isReported_ {false}; // indicates whether information about item has been reported
64};
65
66const std::string MMAP_NAME_HEAP = "[heap]";
67const std::string MMAP_NAME_ANON = "[anon]";
68
69class VirtualRuntime;
70
71class VirtualThread {
72public:
73    VirtualThread(const VirtualThread &) = delete;
74    VirtualThread &operator=(const VirtualThread &) = delete;
75
76    VirtualThread(pid_t pid,
77                  pid_t tid,
78                  const std::unordered_map<std::string, std::unique_ptr<SymbolsFile>>& symbolsFiles,
79                  VirtualRuntime* runtime,
80                  bool parseFlag = true);
81
82    virtual ~VirtualThread() {}
83
84    std::string ReadThreadName(pid_t tid);
85
86    pid_t pid_ {0};
87    pid_t tid_ {0};
88    std::string name_;
89
90    const std::vector<std::shared_ptr<DfxMap>> &GetMaps() const
91    {
92        return *maps_;
93    }
94
95    bool ParseMap(std::vector<std::shared_ptr<DfxMap>> &memMaps, bool update = false);
96    void CreateMapItem(const std::string filename, uint64_t begin, uint64_t len, uint64_t offset);
97    const std::shared_ptr<DfxMap> FindMapByAddr(uint64_t addr) const;
98    const std::pair<std::shared_ptr<MemMaps>, uint32_t> FindMemMapsByAddr(uint64_t addr) const;
99    const std::shared_ptr<DfxMap> FindMapByFileInfo(const std::string name, uint64_t offset) const;
100    SymbolsFile *FindSymbolsFileByMap(std::shared_ptr<DfxMap> inMap) const;
101    SymbolsFile *FindSymbolsFileByName(const std::string &name) const;
102    bool ReadRoMemory(uint64_t vaddr, uint8_t *data, size_t size) const;
103#ifdef HIPERF_DEBUG
104    void ReportVaddrMapMiss(uint64_t vaddr) const;
105#endif
106public:
107
108private:
109    void SortMaps();
110#ifdef DEBUG_TIME
111    bool IsSorted() const;
112#endif
113    const std::unordered_map<std::string, std::unique_ptr<SymbolsFile>>& symbolsFiles_;
114
115    // thread must use ref from process
116
117    std::vector<std::shared_ptr<DfxMap>>* maps_ = {};
118#ifdef HIPERF_DEBUG
119    mutable std::unordered_set<uint64_t> missedRuntimeVaddr_;
120#endif
121#ifdef DEBUG_MISS_SYMBOL
122    mutable std::vector<std::string> missedSymbolFile_;
123#endif
124    VirtualRuntime* virtualruntime_;
125};
126} // namespace NativeDaemon
127} // namespace Developtools
128} // namespace OHOS
129#endif