From dc8ffdb657e87a786fa2065b9aa0b5f51bfbcdf4 Mon Sep 17 00:00:00 2001 From: xlgitee Date: Thu, 31 Jul 2025 22:35:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dfork=E5=9C=BA=E6=99=AF?= =?UTF-8?q?=E4=B8=8B=EF=BC=8C=E5=8D=95=E4=BE=8B=E5=AF=BC=E8=87=B4=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E5=AD=90=E8=BF=9B=E7=A8=8B=E6=A0=88=E8=8C=83=E5=9B=B4?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E6=9B=B4=E6=96=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xlgitee Change-Id: If1c29cffc6324fc7280b3e06f43f4f81bcc55667 --- common/dfxutil/stack_utils.cpp | 30 ++++++++++++++------- common/dfxutil/stack_utils.h | 15 ++++++++--- test/unittest/unwind/memory_test.cpp | 39 +++++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 14 deletions(-) diff --git a/common/dfxutil/stack_utils.cpp b/common/dfxutil/stack_utils.cpp index ae344333b..0a170245b 100644 --- a/common/dfxutil/stack_utils.cpp +++ b/common/dfxutil/stack_utils.cpp @@ -18,25 +18,37 @@ #include #include #include +#include + #include "dfx_define.h" #include "stack_utils.h" namespace OHOS { namespace HiviewDFX { -StackUtils::StackUtils() -{ - ParseSelfMaps(); -} - StackUtils& StackUtils::Instance() { static StackUtils inst; return inst; } -bool StackUtils::GetMainStackRange(uintptr_t& stackBottom, uintptr_t& stackTop) const +bool StackUtils::InitStackRange() +{ + const pid_t selfPid = getpid(); + if (pid_ != selfPid) { + mainStack_.Clear(); + arkCode_.Clear(); + ParseSelfMaps(); + } + if (mainStack_.IsValid()) { + pid_ = selfPid; + return true; + } + return false; +} + +bool StackUtils::GetMainStackRange(uintptr_t& stackBottom, uintptr_t& stackTop) { - if (!mainStack_.IsValid()) { + if (!InitStackRange()) { return false; } stackBottom = mainStack_.start; @@ -44,9 +56,9 @@ bool StackUtils::GetMainStackRange(uintptr_t& stackBottom, uintptr_t& stackTop) return true; } -bool StackUtils::GetArkStackRange(uintptr_t& start, uintptr_t& end) const +bool StackUtils::GetArkStackRange(uintptr_t& start, uintptr_t& end) { - if (!arkCode_.IsValid()) { + if (!InitStackRange() || !arkCode_.IsValid()) { return false; } start = arkCode_.start; diff --git a/common/dfxutil/stack_utils.h b/common/dfxutil/stack_utils.h index 2b47b4517..3d9784556 100644 --- a/common/dfxutil/stack_utils.h +++ b/common/dfxutil/stack_utils.h @@ -30,22 +30,29 @@ public: StackUtils(const StackUtils&) = delete; StackUtils& operator=(const StackUtils&) = delete; - bool GetMainStackRange(uintptr_t& stackBottom, uintptr_t& stackTop) const; - bool GetArkStackRange(uintptr_t& start, uintptr_t& end) const; + bool GetMainStackRange(uintptr_t& stackBottom, uintptr_t& stackTop); + bool GetArkStackRange(uintptr_t& start, uintptr_t& end); static bool GetSelfStackRange(uintptr_t& stackBottom, uintptr_t& stackTop); private: + StackUtils() = default; + void ParseSelfMaps(); + bool InitStackRange(); struct MapRange { bool IsValid() const { return start != 0 && start < end; } + void Clear() + { + start = 0; + end = 0; + } uintptr_t start{0}; uintptr_t end{0}; }; - StackUtils(); - void ParseSelfMaps(); MapRange mainStack_; MapRange arkCode_; + pid_t pid_{0}; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/test/unittest/unwind/memory_test.cpp b/test/unittest/unwind/memory_test.cpp index 2c2ba7739..e79f49253 100644 --- a/test/unittest/unwind/memory_test.cpp +++ b/test/unittest/unwind/memory_test.cpp @@ -549,7 +549,7 @@ HWTEST_F(DfxMemoryTest, DfxMemoryTest015, TestSize.Level2) /** * @tc.name: DfxMemoryTest016 - * @tc.desc: test GetArkStackRange FUNC + * @tc.desc: test GetMainStackRange FUNC * @tc.type: FUNC */ HWTEST_F(DfxMemoryTest, DfxMemoryTest016, TestSize.Level2) @@ -613,6 +613,43 @@ HWTEST_F(DfxMemoryTest, DfxMemoryTest018, TestSize.Level2) ASSERT_EQ(ret, -1); GTEST_LOG_(INFO) << "DfxMemoryTest018: end."; } + +/** + * @tc.name: DfxMemoryTest019 + * @tc.desc: test GetMainStackRange FUNC + * @tc.type: FUNC + */ +HWTEST_F(DfxMemoryTest, DfxMemoryTest019, TestSize.Level2) +{ + GTEST_LOG_(INFO) << "DfxMemoryTest019: start."; + uintptr_t stackBottom; + uintptr_t stackTop; + StackUtils::Instance().GetMainStackRange(stackBottom, stackTop); + pid_t pid = getpid(); + if (pid == 0) { + GTEST_LOG_(INFO) << "pid: " << pid << ", ppid:" << getppid(); + uintptr_t childStackBottom; + uintptr_t childStackTop; + StackUtils::Instance().GetMainStackRange(childStackBottom, childStackTop); + + EXPECT_NE(stackBottom, childStackBottom); + EXPECT_NE(stackTop, childStackTop); + if (stackBottom == childStackBottom && stackTop == childStackTop) { + GTEST_LOG_(INFO) << "DfxMemoryTest019: stack range is same, exit."; + exit(0); + } + exit(1); + } else { + int status; + pid_t wait_ret = wait(&status); + if (wait_ret == -1) { + GTEST_LOG_(ERROR) << "DfxMemoryTest019: wait failed, exit."; + } + + EXPECT_EQ(WEXITSTATUS(status), 0); + GTEST_LOG_(INFO) << "DfxMemoryTest019: end."; + } +} } } // namespace HiviewDFX } // namespace OHOS -- Gitee