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 40 void AddMap(std::shared_ptr<DfxMap> map, bool enableMapIndex = false); 41 void Sort(bool less = true); EnableMapIndex(bool enableMapIndex)42 void EnableMapIndex(bool enableMapIndex) { enableMapIndex_ = enableMapIndex; } EnableOnlyExec(bool onlyExec)43 void EnableOnlyExec(bool onlyExec) { onlyExec_ = onlyExec; } 44 bool FindMapByAddr(uintptr_t addr, std::shared_ptr<DfxMap>& map) const; 45 bool FindMapByFileInfo(std::string name, uint64_t offset, std::shared_ptr<DfxMap>& map) const; 46 bool FindMapsByName(std::string name, std::vector<std::shared_ptr<DfxMap>>& maps) const; GetMaps() const47 const std::vector<std::shared_ptr<DfxMap>>& GetMaps() const { return maps_; } GetMapIndexVec() const48 const std::vector<int>& GetMapIndexVec() const { return mapIndex_; } GetMapsSize() const49 size_t GetMapsSize() const { return maps_.size(); } 50 bool GetStackRange(uintptr_t& bottom, uintptr_t& top); 51 52 bool IsArkExecutedMap(uintptr_t addr); 53 uint32_t filePathId_ {0}; // for maps item filePath id 54 private: 55 bool Parse(const pid_t pid, const std::string& path); 56 57 protected: 58 std::vector<std::shared_ptr<DfxMap>> maps_ {}; 59 std::vector<int> mapIndex_ {}; 60 private: 61 bool enableMapIndex_ = false; 62 bool onlyExec_ = false; 63 uintptr_t stackBottom_ = 0; 64 uintptr_t stackTop_ = 0; 65 }; 66 } // namespace HiviewDFX 67 } // namespace OHOS 68 69 #endif 70