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 <string>
18 #include <unistd.h>
19 #include <vector>
20
21 #include "dfx_buffer_writer.h"
22 #include "dfx_cutil.h"
23 #include "dfx_define.h"
24 #include "dfx_test_util.h"
25 #include "dfx_util.h"
26 #include "decorative_dump_info.h"
27
28 using namespace OHOS::HiviewDFX;
29 using namespace testing::ext;
30 using namespace std;
31
32 namespace OHOS {
33 namespace HiviewDFX {
34 class ExtraCrashInfoTest : public testing::Test {
35 public:
36 static void SetUpTestCase(void);
TearDownTestCase(void)37 static void TearDownTestCase(void) {}
38 void SetUp();
TearDown()39 void TearDown() {}
40 static int WriteLogFunc(int32_t fd, const char *buf, size_t len);
41 static std::string result;
42 };
43 } // namespace HiviewDFX
44 } // namespace OHOS
45 std::string ExtraCrashInfoTest::result = "";
46
SetUpTestCase(void)47 void ExtraCrashInfoTest::SetUpTestCase(void)
48 {
49 result = "";
50 }
51
SetUp(void)52 void ExtraCrashInfoTest::SetUp(void)
53 {
54 DfxBufferWriter::GetInstance().SetWriteFunc(ExtraCrashInfoTest::WriteLogFunc);
55 }
56
WriteLogFunc(int32_t fd,const char * buf,size_t len)57 int ExtraCrashInfoTest::WriteLogFunc(int32_t fd, const char *buf, size_t len)
58 {
59 ExtraCrashInfoTest::result.append(std::string(buf, len));
60 return 0;
61 }
62
63 namespace {
64 /**
65 * @tc.name: ExtraCrashInfoTest001
66 * @tc.desc: test KeyThreadDumpInfo dumpcatch
67 * @tc.type: FUNC
68 */
69 HWTEST_F(ExtraCrashInfoTest, ExtraCrashInfoTest001, TestSize.Level2)
70 {
71 GTEST_LOG_(INFO) << "ExtraCrashInfoTest001: start.";
72 uint8_t type = 5;
73 constexpr size_t bufSize = 4096;
74 uintptr_t memory[bufSize];
75 for (size_t i = 0; i < bufSize; i++) {
76 memory[i] = i;
77 }
78 pid_t pid = fork();
79 if (pid < 0) {
80 GTEST_LOG_(ERROR) << "Failed to fork new test process.";
81 } else if (pid == 0) {
82 sleep(3); // 3 : sleep 3 seconds
83 exit(0);
84 }
85 pid_t tid = pid;
86 pid_t nsPid = pid;
87 ProcessDumpRequest request = {
88 .type = ProcessDumpType::DUMP_TYPE_CPP_CRASH,
89 .tid = pid,
90 .pid = tid,
91 .nsPid = nsPid,
92 };
93 const int moveBit = 56;
94 request.crashObj = (static_cast<uintptr_t>(type) << moveBit) |
95 (reinterpret_cast<uintptr_t>(memory) & 0x00ffffffffffffff);
96 DfxProcess process;
97 process.InitProcessInfo(pid, nsPid, getuid(), "");
98 process.InitKeyThread(request);
99 Unwinder unwinder(pid, nsPid, request.type == ProcessDumpType::DUMP_TYPE_CPP_CRASH);
100 ExtraCrashInfo extraCrashInfo;
101 extraCrashInfo.Print(process, request, unwinder);
102 std::vector<std::string> keyWords = {
103 StringPrintf("ExtraCrashInfo(Memory start address %018" PRIx64 "):",
104 reinterpret_cast<uint64_t>(memory)),
105 "0x000:", "0x020:", "0x040:", "0x060:", "0x080:",
106 "0x0a0:", "0x0c0:", "0x0e0:", "0x100:", "0x120:",
107 };
108 for (const std::string& keyWord : keyWords) {
109 ASSERT_TRUE(CheckContent(result, keyWord, true));
110 }
111 process.Detach();
112 GTEST_LOG_(INFO) << "ExtraCrashInfoTest001: end.";
113 }
114 }
115