1 /*
2 * Copyright (c) 2025 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 #include <gtest/gtest.h>
17 #include <cerrno>
18 #include <unistd.h>
19
20 #include "decorative_dump_info.h"
21 #include "dfx_frame_formatter.h"
22 #include "dfx_maps.h"
23 #include "dfx_regs.h"
24 #include "dfx_buffer_writer.h"
25 #include "dfx_test_util.h"
26 #include "dfx_thread.h"
27 #include "multithread_constructor.h"
28
29 using namespace OHOS::HiviewDFX;
30 using namespace testing::ext;
31 using namespace std;
32
33 namespace OHOS {
34 namespace HiviewDFX {
35 class FaultStackUnittest : public testing::Test {
36 public:
37 static void SetUpTestCase(void);
TearDownTestCase(void)38 static void TearDownTestCase(void) {}
39 void SetUp();
TearDown()40 void TearDown() {}
41
42 static int WriteLogFunc(int32_t fd, const char *buf, size_t len);
43 static std::string result;
44 };
45 } // namespace HiviewDFX
46 } // namespace OHOS
47
48 std::string FaultStackUnittest::result = "";
49
SetUpTestCase(void)50 void FaultStackUnittest::SetUpTestCase(void)
51 {
52 result = "";
53 }
54
SetUp(void)55 void FaultStackUnittest::SetUp(void)
56 {
57 DfxBufferWriter::GetInstance().SetWriteFunc(FaultStackUnittest::WriteLogFunc);
58 }
59
WriteLogFunc(int32_t fd,const char * buf,size_t len)60 int FaultStackUnittest::WriteLogFunc(int32_t fd, const char *buf, size_t len)
61 {
62 FaultStackUnittest::result.append(std::string(buf, len));
63 return 0;
64 }
65
66 namespace {
67 /**
68 * @tc.name: FaultStackUnittest001
69 * @tc.desc: check whether fault stack and register can be print out
70 * @tc.type: FUNC
71 */
72 HWTEST_F(FaultStackUnittest, FaultStackUnittest001, TestSize.Level0)
73 {
74 GTEST_LOG_(INFO) << "FaultStackUnittest001: start.";
75 pid_t testProcess = CreateMultiThreadProcess(10); // 10 : create a process with ten threads
76 bool isSuccess = testProcess >= 0;
77 if (!isSuccess) {
78 ASSERT_FALSE(isSuccess);
79 printf("Failed to fork child process, errno(%d).\n", errno);
80 } else {
81 sleep(1);
82 pid_t tid = testProcess;
83 pid_t nsPid = testProcess;
84 pid_t pid = testProcess;
85 ProcessDumpRequest request = {
86 .type = ProcessDumpType::DUMP_TYPE_CPP_CRASH,
87 .tid = tid,
88 .pid = pid,
89 .nsPid = pid,
90 };
91 DfxProcess process;
92 process.InitProcessInfo(pid, nsPid, getuid(), "");
93 process.InitKeyThread(request);
94 Unwinder unwinder(pid, nsPid, request.type == ProcessDumpType::DUMP_TYPE_CPP_CRASH);
95 unwinder.SetRegs(DfxRegs::CreateRemoteRegs(pid));
96 unwinder.UnwindRemote(tid, true);
97 process.GetKeyThread()->SetFrames(unwinder.GetFrames());
98 FaultStack faultStack;
99 faultStack.Print(process, request, unwinder);
100 EXPECT_TRUE(result.find("FaultStack:") != std::string::npos) << result;
101 EXPECT_TRUE(result.find("sp0") != std::string::npos) << result;
102 GTEST_LOG_(INFO) << "FaultStackUnittest001: end.";
103 }
104 }
105 }