106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci#ifndef HIPERF_VIRTUAL_THREAD_H
1606f6ba60Sopenharmony_ci#define HIPERF_VIRTUAL_THREAD_H
1706f6ba60Sopenharmony_ci#include <assert.h>
1806f6ba60Sopenharmony_ci#include <cinttypes>
1906f6ba60Sopenharmony_ci#include <functional>
2006f6ba60Sopenharmony_ci#include <pthread.h>
2106f6ba60Sopenharmony_ci#include <set>
2206f6ba60Sopenharmony_ci#include <unordered_set>
2306f6ba60Sopenharmony_ci#include <unordered_map>
2406f6ba60Sopenharmony_ci#include <vector>
2506f6ba60Sopenharmony_ci#include "debug_logger.h"
2606f6ba60Sopenharmony_ci#include "dfx_maps.h"
2706f6ba60Sopenharmony_ci#include "logging.h"
2806f6ba60Sopenharmony_ci#include "register.h"
2906f6ba60Sopenharmony_ci#include "symbols_file.h"
3006f6ba60Sopenharmony_ci#include "utilities.h"
3106f6ba60Sopenharmony_ci
3206f6ba60Sopenharmony_cinamespace OHOS {
3306f6ba60Sopenharmony_cinamespace Developtools {
3406f6ba60Sopenharmony_cinamespace NativeDaemon {
3506f6ba60Sopenharmony_ci/*
3606f6ba60Sopenharmony_ci03284000-03289000 r--p 00000000 b3:05 289        /system/bin/sh
3706f6ba60Sopenharmony_ci032b7000-032b9000 rw-p 00000000 00:00 0
3806f6ba60Sopenharmony_ciaff60000-aff96000 r--p 00000000 b3:05 923        /system/lib/libc++.so
3906f6ba60Sopenharmony_ciaffeb000-affed000 rw-p 00000000 00:00 0
4006f6ba60Sopenharmony_cib0023000-b0024000 r--p 00000000 b3:05 959        /system/lib/libdl.so
4106f6ba60Sopenharmony_ci*/
4206f6ba60Sopenharmony_ciusing namespace OHOS::HiviewDFX;
4306f6ba60Sopenharmony_ciclass MemMaps : public DfxMaps {
4406f6ba60Sopenharmony_cipublic:
4506f6ba60Sopenharmony_ci    MemMaps() {}
4606f6ba60Sopenharmony_ci    MemMaps(uint32_t filePathId) : filePathId_(filePathId) {}
4706f6ba60Sopenharmony_ci    void AddMap(std::shared_ptr<DfxMap> map, bool firstMap)
4806f6ba60Sopenharmony_ci    {
4906f6ba60Sopenharmony_ci        if (firstMap) {
5006f6ba60Sopenharmony_ci            soBegin_ = map->begin;
5106f6ba60Sopenharmony_ci            soEnd_ = map->end;
5206f6ba60Sopenharmony_ci            name_ = map->name;
5306f6ba60Sopenharmony_ci        } else {
5406f6ba60Sopenharmony_ci            maps_.back()->end = map->begin;
5506f6ba60Sopenharmony_ci            map->prevMap = maps_.back();
5606f6ba60Sopenharmony_ci        }
5706f6ba60Sopenharmony_ci        maps_.emplace_back(map);
5806f6ba60Sopenharmony_ci    }
5906f6ba60Sopenharmony_ci    uint64_t soBegin_ {0};
6006f6ba60Sopenharmony_ci    uint64_t soEnd_ {0};
6106f6ba60Sopenharmony_ci    uint32_t filePathId_ {0}; // for maps item filePath id
6206f6ba60Sopenharmony_ci    std::string name_;
6306f6ba60Sopenharmony_ci    bool isReported_ {false}; // indicates whether information about item has been reported
6406f6ba60Sopenharmony_ci};
6506f6ba60Sopenharmony_ci
6606f6ba60Sopenharmony_ciconst std::string MMAP_NAME_HEAP = "[heap]";
6706f6ba60Sopenharmony_ciconst std::string MMAP_NAME_ANON = "[anon]";
6806f6ba60Sopenharmony_ci
6906f6ba60Sopenharmony_ciclass VirtualRuntime;
7006f6ba60Sopenharmony_ci
7106f6ba60Sopenharmony_ciclass VirtualThread {
7206f6ba60Sopenharmony_cipublic:
7306f6ba60Sopenharmony_ci    VirtualThread(const VirtualThread &) = delete;
7406f6ba60Sopenharmony_ci    VirtualThread &operator=(const VirtualThread &) = delete;
7506f6ba60Sopenharmony_ci
7606f6ba60Sopenharmony_ci    VirtualThread(pid_t pid,
7706f6ba60Sopenharmony_ci                  pid_t tid,
7806f6ba60Sopenharmony_ci                  const std::unordered_map<std::string, std::unique_ptr<SymbolsFile>>& symbolsFiles,
7906f6ba60Sopenharmony_ci                  VirtualRuntime* runtime,
8006f6ba60Sopenharmony_ci                  bool parseFlag = true);
8106f6ba60Sopenharmony_ci
8206f6ba60Sopenharmony_ci    virtual ~VirtualThread() {}
8306f6ba60Sopenharmony_ci
8406f6ba60Sopenharmony_ci    std::string ReadThreadName(pid_t tid);
8506f6ba60Sopenharmony_ci
8606f6ba60Sopenharmony_ci    pid_t pid_ {0};
8706f6ba60Sopenharmony_ci    pid_t tid_ {0};
8806f6ba60Sopenharmony_ci    std::string name_;
8906f6ba60Sopenharmony_ci
9006f6ba60Sopenharmony_ci    const std::vector<std::shared_ptr<DfxMap>> &GetMaps() const
9106f6ba60Sopenharmony_ci    {
9206f6ba60Sopenharmony_ci        return *maps_;
9306f6ba60Sopenharmony_ci    }
9406f6ba60Sopenharmony_ci
9506f6ba60Sopenharmony_ci    bool ParseMap(std::vector<std::shared_ptr<DfxMap>> &memMaps, bool update = false);
9606f6ba60Sopenharmony_ci    void CreateMapItem(const std::string filename, uint64_t begin, uint64_t len, uint64_t offset);
9706f6ba60Sopenharmony_ci    const std::shared_ptr<DfxMap> FindMapByAddr(uint64_t addr) const;
9806f6ba60Sopenharmony_ci    const std::pair<std::shared_ptr<MemMaps>, uint32_t> FindMemMapsByAddr(uint64_t addr) const;
9906f6ba60Sopenharmony_ci    const std::shared_ptr<DfxMap> FindMapByFileInfo(const std::string name, uint64_t offset) const;
10006f6ba60Sopenharmony_ci    SymbolsFile *FindSymbolsFileByMap(std::shared_ptr<DfxMap> inMap) const;
10106f6ba60Sopenharmony_ci    SymbolsFile *FindSymbolsFileByName(const std::string &name) const;
10206f6ba60Sopenharmony_ci    bool ReadRoMemory(uint64_t vaddr, uint8_t *data, size_t size) const;
10306f6ba60Sopenharmony_ci#ifdef HIPERF_DEBUG
10406f6ba60Sopenharmony_ci    void ReportVaddrMapMiss(uint64_t vaddr) const;
10506f6ba60Sopenharmony_ci#endif
10606f6ba60Sopenharmony_cipublic:
10706f6ba60Sopenharmony_ci
10806f6ba60Sopenharmony_ciprivate:
10906f6ba60Sopenharmony_ci    void SortMaps();
11006f6ba60Sopenharmony_ci#ifdef DEBUG_TIME
11106f6ba60Sopenharmony_ci    bool IsSorted() const;
11206f6ba60Sopenharmony_ci#endif
11306f6ba60Sopenharmony_ci    const std::unordered_map<std::string, std::unique_ptr<SymbolsFile>>& symbolsFiles_;
11406f6ba60Sopenharmony_ci
11506f6ba60Sopenharmony_ci    // thread must use ref from process
11606f6ba60Sopenharmony_ci
11706f6ba60Sopenharmony_ci    std::vector<std::shared_ptr<DfxMap>>* maps_ = {};
11806f6ba60Sopenharmony_ci#ifdef HIPERF_DEBUG
11906f6ba60Sopenharmony_ci    mutable std::unordered_set<uint64_t> missedRuntimeVaddr_;
12006f6ba60Sopenharmony_ci#endif
12106f6ba60Sopenharmony_ci#ifdef DEBUG_MISS_SYMBOL
12206f6ba60Sopenharmony_ci    mutable std::vector<std::string> missedSymbolFile_;
12306f6ba60Sopenharmony_ci#endif
12406f6ba60Sopenharmony_ci    VirtualRuntime* virtualruntime_;
12506f6ba60Sopenharmony_ci};
12606f6ba60Sopenharmony_ci} // namespace NativeDaemon
12706f6ba60Sopenharmony_ci} // namespace Developtools
12806f6ba60Sopenharmony_ci} // namespace OHOS
12906f6ba60Sopenharmony_ci#endif