1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#ifndef DFX_MAPS_H
17800b99b8Sopenharmony_ci#define DFX_MAPS_H
18800b99b8Sopenharmony_ci
19800b99b8Sopenharmony_ci#include <mutex>
20800b99b8Sopenharmony_ci#include <string>
21800b99b8Sopenharmony_ci#include <vector>
22800b99b8Sopenharmony_ci#include "dfx_map.h"
23800b99b8Sopenharmony_ci#include "string_util.h"
24800b99b8Sopenharmony_ci
25800b99b8Sopenharmony_cinamespace OHOS {
26800b99b8Sopenharmony_cinamespace HiviewDFX {
27800b99b8Sopenharmony_ciclass DfxMaps {
28800b99b8Sopenharmony_cipublic:
29800b99b8Sopenharmony_ci    DfxMaps() = default;
30800b99b8Sopenharmony_ci    ~DfxMaps() = default;
31800b99b8Sopenharmony_ci
32800b99b8Sopenharmony_ci    static std::shared_ptr<DfxMaps> Create(pid_t pid = 0, bool crash = true);
33800b99b8Sopenharmony_ci    static std::shared_ptr<DfxMaps> Create(pid_t pid, const std::string& path);
34800b99b8Sopenharmony_ci    static bool Create(const pid_t pid, std::vector<std::shared_ptr<DfxMap>>& maps, std::vector<int>& mapIndex);
35800b99b8Sopenharmony_ci
36800b99b8Sopenharmony_ci    static bool IsArkHapMapItem(const std::string& name);
37800b99b8Sopenharmony_ci    static bool IsArkCodeMapItem(const std::string& name);
38800b99b8Sopenharmony_ci    static bool IsLegalMapItem(const std::string& name, bool withArk = true);
39800b99b8Sopenharmony_ci
40800b99b8Sopenharmony_ci    void AddMap(std::shared_ptr<DfxMap> map, bool enableMapIndex = false);
41800b99b8Sopenharmony_ci    void Sort(bool less = true);
42800b99b8Sopenharmony_ci    void EnableMapIndex(bool enableMapIndex) { enableMapIndex_ = enableMapIndex; }
43800b99b8Sopenharmony_ci    void EnableOnlyExec(bool onlyExec) { onlyExec_ = onlyExec; }
44800b99b8Sopenharmony_ci    bool FindMapByAddr(uintptr_t addr, std::shared_ptr<DfxMap>& map) const;
45800b99b8Sopenharmony_ci    bool FindMapByFileInfo(std::string name, uint64_t offset, std::shared_ptr<DfxMap>& map) const;
46800b99b8Sopenharmony_ci    bool FindMapsByName(std::string name, std::vector<std::shared_ptr<DfxMap>>& maps) const;
47800b99b8Sopenharmony_ci    const std::vector<std::shared_ptr<DfxMap>>& GetMaps() const { return maps_; }
48800b99b8Sopenharmony_ci    const std::vector<int>& GetMapIndexVec() const { return mapIndex_; }
49800b99b8Sopenharmony_ci    size_t GetMapsSize() const { return maps_.size(); }
50800b99b8Sopenharmony_ci    bool GetStackRange(uintptr_t& bottom, uintptr_t& top);
51800b99b8Sopenharmony_ci
52800b99b8Sopenharmony_ci    bool IsArkExecutedMap(uintptr_t addr);
53800b99b8Sopenharmony_ci    uint32_t filePathId_ {0}; // for maps item filePath id
54800b99b8Sopenharmony_ciprivate:
55800b99b8Sopenharmony_ci    bool Parse(const pid_t pid, const std::string& path);
56800b99b8Sopenharmony_ci
57800b99b8Sopenharmony_ciprotected:
58800b99b8Sopenharmony_ci    std::vector<std::shared_ptr<DfxMap>> maps_ {};
59800b99b8Sopenharmony_ci    std::vector<int> mapIndex_ {};
60800b99b8Sopenharmony_ciprivate:
61800b99b8Sopenharmony_ci    bool enableMapIndex_ = false;
62800b99b8Sopenharmony_ci    bool onlyExec_ = false;
63800b99b8Sopenharmony_ci    uintptr_t stackBottom_ = 0;
64800b99b8Sopenharmony_ci    uintptr_t stackTop_ = 0;
65800b99b8Sopenharmony_ci};
66800b99b8Sopenharmony_ci} // namespace HiviewDFX
67800b99b8Sopenharmony_ci} // namespace OHOS
68800b99b8Sopenharmony_ci
69800b99b8Sopenharmony_ci#endif
70