diff --git a/common/dfxutil/stack_utils.cpp b/common/dfxutil/stack_utils.cpp index ae344333b8fcfa31c4c2a8c9a41ef9e214a75013..0a170245b92dd822e37eb905266570e9d6f8fbce 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 2b47b4517011481697d6da56e83344df3330578d..3d9784556b7e3ec85519df9d4a84d0819944a213 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 2c2ba7739380c9d128168cabd160a707fa4ab81d..e79f49253376ffeda998a5a1408d40a171661cd8 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