1 /*
2  * Copyright (c) 2023-2024 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 
16 #ifndef DFX_MAPS_H
17 #define DFX_MAPS_H
18 
19 #include <mutex>
20 #include <string>
21 #include <vector>
22 #include "dfx_map.h"
23 #include "string_util.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 class DfxMaps {
28 public:
29     DfxMaps() = default;
30     ~DfxMaps() = default;
31 
32     static std::shared_ptr<DfxMaps> Create(pid_t pid = 0, bool crash = true);
33     static std::shared_ptr<DfxMaps> Create(pid_t pid, const std::string& path);
34     static bool Create(const pid_t pid, std::vector<std::shared_ptr<DfxMap>>& maps, std::vector<int>& mapIndex);
35 
36     static bool IsArkHapMapItem(const std::string& name);
37     static bool IsArkCodeMapItem(const std::string& name);
38     static bool IsLegalMapItem(const std::string& name, bool withArk = true);
39     static void FormatMapName(pid_t pid, std::string& mapName);
40     static void UnFormatMapName(std::string& mapName);
41 
42     void AddMap(std::shared_ptr<DfxMap> map, bool enableMapIndex = false);
43     void Sort(bool less = true);
EnableMapIndex(bool enableMapIndex)44     void EnableMapIndex(bool enableMapIndex) { enableMapIndex_ = enableMapIndex; }
EnableOnlyExec(bool onlyExec)45     void EnableOnlyExec(bool onlyExec) { onlyExec_ = onlyExec; }
46     bool FindMapByAddr(uintptr_t addr, std::shared_ptr<DfxMap>& map) const;
47     bool FindMapByFileInfo(std::string name, uint64_t offset, std::shared_ptr<DfxMap>& map) const;
48     bool FindMapsByName(std::string name, std::vector<std::shared_ptr<DfxMap>>& maps) const;
GetMaps()49     const std::vector<std::shared_ptr<DfxMap>>& GetMaps() const { return maps_; }
GetMapIndexVec()50     const std::vector<int>& GetMapIndexVec() const { return mapIndex_; }
GetMapsSize()51     size_t GetMapsSize() const { return maps_.size(); }
52     bool GetStackRange(uintptr_t& bottom, uintptr_t& top);
53 
54     bool IsArkExecutedMap(uintptr_t addr);
55     uint32_t filePathId_ {0}; // for maps item filePath id
56 private:
57     bool Parse(const pid_t pid, const std::string& path);
58 
59 protected:
60     std::vector<std::shared_ptr<DfxMap>> maps_ {};
61     std::vector<int> mapIndex_ {};
62 private:
63     bool enableMapIndex_ = false;
64     bool onlyExec_ = false;
65     uintptr_t stackBottom_ = 0;
66     uintptr_t stackTop_ = 0;
67 };
68 } // namespace HiviewDFX
69 } // namespace OHOS
70 
71 #endif
72