1800b99b8Sopenharmony_ci/* 2800b99b8Sopenharmony_ci * Copyright (c) 2023 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_MAP_H 17800b99b8Sopenharmony_ci#define DFX_MAP_H 18800b99b8Sopenharmony_ci 19800b99b8Sopenharmony_ci#include <atomic> 20800b99b8Sopenharmony_ci#include <memory> 21800b99b8Sopenharmony_ci#include <mutex> 22800b99b8Sopenharmony_ci#include <string> 23800b99b8Sopenharmony_ci#include <sys/stat.h> 24800b99b8Sopenharmony_ci 25800b99b8Sopenharmony_cinamespace OHOS { 26800b99b8Sopenharmony_cinamespace HiviewDFX { 27800b99b8Sopenharmony_ciclass DfxElf; 28800b99b8Sopenharmony_ciclass DfxHap; 29800b99b8Sopenharmony_ci 30800b99b8Sopenharmony_ciclass DfxMap { 31800b99b8Sopenharmony_cipublic: 32800b99b8Sopenharmony_ci static std::shared_ptr<DfxMap> Create(std::string buf, size_t size); 33800b99b8Sopenharmony_ci static void PermsToProts(const std::string perms, uint32_t& prots, uint32_t& flag); 34800b99b8Sopenharmony_ci static void FormatMapName(pid_t pid, std::string& mapName); 35800b99b8Sopenharmony_ci static void UnFormatMapName(std::string& mapName); 36800b99b8Sopenharmony_ci 37800b99b8Sopenharmony_ci DfxMap() = default; 38800b99b8Sopenharmony_ci DfxMap(uint64_t begin, uint64_t end, uint64_t offset, 39800b99b8Sopenharmony_ci const std::string& perms, const std::string& name) 40800b99b8Sopenharmony_ci : begin(begin), end(end), offset(offset), perms(perms), name(name) {} 41800b99b8Sopenharmony_ci DfxMap(uint64_t begin, uint64_t end, uint64_t offset, 42800b99b8Sopenharmony_ci uint32_t prots, const std::string& name) 43800b99b8Sopenharmony_ci : begin(begin), end(end), offset(offset), prots(prots), name(name) {} 44800b99b8Sopenharmony_ci 45800b99b8Sopenharmony_ci bool Parse(char* buf, size_t size); 46800b99b8Sopenharmony_ci bool IsMapExec(); 47800b99b8Sopenharmony_ci bool IsArkExecutable(); 48800b99b8Sopenharmony_ci bool IsVdsoMap(); 49800b99b8Sopenharmony_ci const std::shared_ptr<DfxHap> GetHap(); 50800b99b8Sopenharmony_ci const std::shared_ptr<DfxElf> GetElf(pid_t pid = 0); 51800b99b8Sopenharmony_ci std::string GetElfName(); 52800b99b8Sopenharmony_ci uint64_t GetRelPc(uint64_t pc); 53800b99b8Sopenharmony_ci std::string ToString(); 54800b99b8Sopenharmony_ci 55800b99b8Sopenharmony_ci uint64_t begin = 0; 56800b99b8Sopenharmony_ci uint64_t end = 0; 57800b99b8Sopenharmony_ci uint64_t offset = 0; 58800b99b8Sopenharmony_ci uint32_t prots = 0; 59800b99b8Sopenharmony_ci uint32_t flag = 0; 60800b99b8Sopenharmony_ci uint64_t major = 0; 61800b99b8Sopenharmony_ci uint64_t minor = 0; 62800b99b8Sopenharmony_ci ino_t inode = 0; 63800b99b8Sopenharmony_ci std::string perms = ""; // 5:rwxp 64800b99b8Sopenharmony_ci std::string name = ""; 65800b99b8Sopenharmony_ci std::shared_ptr<DfxElf> elf = nullptr; 66800b99b8Sopenharmony_ci std::shared_ptr<DfxHap> hap = nullptr; 67800b99b8Sopenharmony_ci std::shared_ptr<DfxMap> prevMap = nullptr; 68800b99b8Sopenharmony_ci uint64_t elfOffset = 0; 69800b99b8Sopenharmony_ci uint64_t elfStartOffset = 0; 70800b99b8Sopenharmony_ci int32_t symbolFileIndex = -1; // symbols file index 71800b99b8Sopenharmony_ci#if is_ohos && !is_mingw 72800b99b8Sopenharmony_ci std::shared_ptr<std::vector<uint8_t>> shmmData = nullptr; 73800b99b8Sopenharmony_ci#endif 74800b99b8Sopenharmony_ci // use for find 75800b99b8Sopenharmony_ci inline bool operator==(const std::string &sname) const 76800b99b8Sopenharmony_ci { 77800b99b8Sopenharmony_ci return this->name == sname; 78800b99b8Sopenharmony_ci } 79800b99b8Sopenharmony_ci 80800b99b8Sopenharmony_ci inline bool operator<(const DfxMap &other) const 81800b99b8Sopenharmony_ci { 82800b99b8Sopenharmony_ci return this->end < other.end; 83800b99b8Sopenharmony_ci } 84800b99b8Sopenharmony_ci 85800b99b8Sopenharmony_ci bool Contain(uint64_t pc) const 86800b99b8Sopenharmony_ci { 87800b99b8Sopenharmony_ci return (pc >= begin && pc < end); 88800b99b8Sopenharmony_ci } 89800b99b8Sopenharmony_ci 90800b99b8Sopenharmony_ci // The range [first, last) must be partitioned with respect to the expression 91800b99b8Sopenharmony_ci // !(value < element) or !comp(value, element) 92800b99b8Sopenharmony_ci static bool ValueLessThen(uint64_t vaddr, const DfxMap &a) 93800b99b8Sopenharmony_ci { 94800b99b8Sopenharmony_ci return vaddr < a.begin; 95800b99b8Sopenharmony_ci } 96800b99b8Sopenharmony_ci static bool ValueLessEqual(uint64_t vaddr, const DfxMap &a) 97800b99b8Sopenharmony_ci { 98800b99b8Sopenharmony_ci return vaddr <= a.begin; 99800b99b8Sopenharmony_ci } 100800b99b8Sopenharmony_ci uint64_t FileOffsetFromAddr(uint64_t vaddr) const 101800b99b8Sopenharmony_ci { 102800b99b8Sopenharmony_ci // real vaddr - real map begin = addr offset in section 103800b99b8Sopenharmony_ci // section offset + page off set = file offset 104800b99b8Sopenharmony_ci return vaddr - begin + offset; 105800b99b8Sopenharmony_ci } 106800b99b8Sopenharmony_ci}; 107800b99b8Sopenharmony_ci} // namespace HiviewDFX 108800b99b8Sopenharmony_ci} // namespace OHOS 109800b99b8Sopenharmony_ci 110800b99b8Sopenharmony_ci#endif 111