• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "dfx_elf.h"
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 struct DfxElfMap {
26     static std::shared_ptr<DfxElfMap> Create(const std::string mapBuf, int size);
27     static void PermsToProts(const std::string perms, uint64_t& flags);
28 
29     bool IsValidPath();
30     std::string PrintMap();
31     uint64_t GetRelPc(uint64_t pc);
32     uint32_t GetPcAdjustment(uint64_t pc);
33 
34     uint64_t begin = 0;
35     uint64_t end = 0;
36     uint64_t offset = 0;
37     uint64_t prots = 0;
38     std::string perms = ""; // 5:rwxp
39     std::string path = "";
40     std::shared_ptr<DfxElf> elf = nullptr;
41 };
42 
43 class DfxElfMaps {
44 public:
45     DfxElfMaps() = default;
46     ~DfxElfMaps() = default;
47     static std::shared_ptr<DfxElfMaps> Create(pid_t pid);
48     static std::shared_ptr<DfxElfMaps> CreateFromLocal();
49     static std::shared_ptr<DfxElfMaps> Create(const std::string path);
50     static std::shared_ptr<DfxElfMaps> Create(const char* buffer);
51 
52     void AddMap(std::shared_ptr<DfxElfMap> map);
53     bool FindMapByAddr(uintptr_t address, std::shared_ptr<DfxElfMap>& map) const;
54     const std::vector<std::shared_ptr<DfxElfMap>>& GetMaps() const;
55 
56     void Sort(bool less = true);
57 private:
58     std::vector<std::shared_ptr<DfxElfMap>> maps_;
59 };
60 } // namespace HiviewDFX
61 } // namespace OHOS
62 
63 #endif
64