From f22e6861d2da12b5aa4dfea75f1a0b8b125e51cc Mon Sep 17 00:00:00 2001 From: dong-bosi123 Date: Thu, 11 Sep 2025 16:05:35 +0800 Subject: [PATCH] move the mummap in ~dfxhap Signed-off-by: dong-bosi123 --- .../innerkits/unwinder/include/dfx_hap.h | 4 ++ .../innerkits/unwinder/src/ark/dfx_hap.cpp | 40 +++++++++++++------ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/interfaces/innerkits/unwinder/include/dfx_hap.h b/interfaces/innerkits/unwinder/include/dfx_hap.h index 104488e44..922ea92bf 100644 --- a/interfaces/innerkits/unwinder/include/dfx_hap.h +++ b/interfaces/innerkits/unwinder/include/dfx_hap.h @@ -18,6 +18,7 @@ #include #include #include +#include #include "dfx_ark.h" #include "dfx_extractor_utils.h" @@ -40,6 +41,7 @@ private: bool ParseHapFileData(const std::string& name); bool ParseHapMemData(const pid_t pid, std::shared_ptr map); bool ParseHapMemInfoForOffline(const std::string& mapName, uint64_t relPc, JsFunction *jsFunction); + bool MmapForHap(const std::string& mapName); private: MAYBE_UNUSED uintptr_t arkSymbolExtractorPtr_ = 0; @@ -50,6 +52,8 @@ private: MAYBE_UNUSED size_t srcMapDataSize_ = 0; MAYBE_UNUSED uintptr_t srcMapLoadOffset_ = 0; MAYBE_UNUSED std::unique_ptr extractor_ = nullptr; + MAYBE_UNUSED void *mmap_ = MAP_FAILED; + MAYBE_UNUSED off_t hapSize_ = 0; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/interfaces/innerkits/unwinder/src/ark/dfx_hap.cpp b/interfaces/innerkits/unwinder/src/ark/dfx_hap.cpp index d0f3b766c..331c5ad9c 100644 --- a/interfaces/innerkits/unwinder/src/ark/dfx_hap.cpp +++ b/interfaces/innerkits/unwinder/src/ark/dfx_hap.cpp @@ -15,8 +15,6 @@ #include "dfx_hap.h" -#include - #include "dfx_define.h" #include "dfx_log.h" #include "dfx_maps.h" @@ -40,6 +38,11 @@ DfxHap::~DfxHap() DfxArk::Instance().ArkDestoryJsSymbolExtractor(arkSymbolExtractorPtr_); arkSymbolExtractorPtr_ = 0; } + if (mmap_ != MAP_FAILED && hapSize_ > 0) { + munmap(mmap_, hapSize_); + mmap_ = MAP_FAILED; + hapSize_ = 0; + } #endif } @@ -79,10 +82,12 @@ bool DfxHap::ParseHapInfo(pid_t pid, uint64_t pc, std::shared_ptr map, J #endif } -bool DfxHap::ParseHapMemInfoForOffline(const std::string& mapName, uint64_t relPc, JsFunction *jsFunction) +bool DfxHap::MmapForHap(const std::string& mapName) { -#if is_ohos && !is_mingw - if (jsFunction == nullptr || mapName.empty()) { + if (mmap_ != MAP_FAILED && hapSize_ > 0) { + return true; + } + if (mapName.empty()) { return false; } SmartFd smartFd(open(mapName.c_str(), O_RDONLY)); @@ -90,25 +95,36 @@ bool DfxHap::ParseHapMemInfoForOffline(const std::string& mapName, uint64_t relP DFXLOGE("Failed to open file: %{public}s, errno(%{public}d)", mapName.c_str(), errno); return false; } - off_t size = lseek(smartFd.GetFd(), 0, SEEK_END); - if (size <= 0) { + hapSize_ = lseek(smartFd.GetFd(), 0, SEEK_END); + if (hapSize_ <= 0) { DFXLOGE("fd is empty or error, fd(%{public}d)", smartFd.GetFd()); return false; } - void* mptr = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, smartFd.GetFd(), 0); - if (mptr == MAP_FAILED) { + mmap_ = mmap(nullptr, hapSize_, PROT_READ, MAP_PRIVATE, smartFd.GetFd(), 0); + if (mmap_ == MAP_FAILED) { DFXLOGE("mmap failed, fd(%{public}d), errno(%{public}d)", smartFd.GetFd(), errno); return false; } + return true; +} + +bool DfxHap::ParseHapMemInfoForOffline(const std::string& mapName, uint64_t relPc, JsFunction *jsFunction) +{ +#if is_ohos && !is_mingw + if (jsFunction == nullptr) { + return false; + } + if (!MmapForHap(mapName)) { + return false; + } bool isSuccess = DfxArk::Instance().ParseArkFrameInfo( static_cast(relPc), 0, 0, - static_cast(mptr), - size, arkSymbolExtractorPtr_, jsFunction) >= 0; + static_cast(mmap_), + hapSize_, arkSymbolExtractorPtr_, jsFunction) >= 0; if (!isSuccess) { DFXLOGW("Failed to parse ark frame info, relPc: %{private}p, codeName: %{private}s", reinterpret_cast(relPc), mapName.c_str()); } - munmap(mptr, size); return isSuccess; #else return false; -- Gitee