• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <set>
21 #include <string>
22 #include <vector>
23 #include "dfx_map.h"
24 #include "string_util.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 class DfxMaps {
29 public:
30     DfxMaps() = default;
31     ~DfxMaps() = default;
32 
33     static std::shared_ptr<DfxMaps> Create(pid_t pid = 0, bool crash = true);
34     static std::shared_ptr<DfxMaps> Create(pid_t pid, const std::string& path);
35     static bool Create(const pid_t pid, std::vector<std::shared_ptr<DfxMap>>& maps, std::vector<int>& mapIndex);
36 
37     static bool IsArkHapMapItem(const std::string& name);
38     static bool IsArkCodeMapItem(const std::string& name);
39     static bool IsLegalMapItem(const std::string& name, bool withArk = true);
40 
41     void AddMap(std::shared_ptr<DfxMap> map, bool enableMapIndex = false);
42     void Sort(bool less = true);
EnableMapIndex(bool enableMapIndex)43     void EnableMapIndex(bool enableMapIndex) { enableMapIndex_ = enableMapIndex; }
EnableOnlyExec(bool onlyExec)44     void EnableOnlyExec(bool onlyExec) { onlyExec_ = onlyExec; }
45     bool FindMapByAddr(uintptr_t addr, std::shared_ptr<DfxMap>& map) const;
46     bool FindMapGroupByAddr(uintptr_t addr, std::set<DfxMap>& maps) 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     bool GetArkStackRange(uintptr_t& start, uintptr_t& end);
54 
55     bool IsArkExecutedMap(uintptr_t addr);
56     uint32_t filePathId_ {0}; // for maps item filePath id
57 private:
58     bool Parse(const pid_t pid, const std::string& path);
59     void HandleSpecialMap(const std::shared_ptr<DfxMap>& map);
60     int FindMapIndexByAddr(uintptr_t addr) const;
61 
62 protected:
63     std::vector<std::shared_ptr<DfxMap>> maps_ {};
64     std::vector<int> mapIndex_ {};
65 private:
66     bool enableMapIndex_ = false;
67     bool onlyExec_ = false;
68     uintptr_t stackBottom_ = 0;
69     uintptr_t stackTop_ = 0;
70     uintptr_t ArkStackStart_ = 0;
71     uintptr_t ArkStackEnd_ = 0;
72 };
73 } // namespace HiviewDFX
74 } // namespace OHOS
75 
76 #endif
77