• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 MapsTest : 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 
46 std::string MapsTest::result = "";
47 
SetUpTestCase(void)48 void MapsTest::SetUpTestCase(void)
49 {
50     result = "";
51 }
52 
SetUp(void)53 void MapsTest::SetUp(void)
54 {
55     DfxBufferWriter::GetInstance().SetWriteFunc(MapsTest::WriteLogFunc);
56 }
57 
WriteLogFunc(int32_t fd,const char * buf,size_t len)58 int MapsTest::WriteLogFunc(int32_t fd, const char *buf, size_t len)
59 {
60     MapsTest::result.append(std::string(buf, len));
61     return 0;
62 }
63 
64 namespace {
65 /**
66  * @tc.name: MapsTest001
67  * @tc.desc: test print maps
68  * @tc.type: FUNC
69  */
70 HWTEST_F(MapsTest, MapsTest001, TestSize.Level2)
71 {
72     GTEST_LOG_(INFO) << "MapsTest001: start.";
73     pid_t pid = fork();
74     if (pid < 0) {
75         GTEST_LOG_(ERROR) << "Failed to fork new test process.";
76     } else if (pid == 0) {
77         sleep(3); // 3 : sleep 3 seconds
78         exit(0);
79     }
80     pid_t tid = pid;
81     pid_t nsPid = pid;
82     ProcessDumpRequest request = {
83         .type = ProcessDumpType::DUMP_TYPE_CPP_CRASH,
84         .tid = tid,
85         .pid = pid,
86         .nsPid = pid,
87     };
88     DfxProcess process;
89     process.InitProcessInfo(pid, nsPid, getuid(), "");
90     process.SetVmPid(pid);
91     process.InitKeyThread(request);
92     auto regs = DfxRegs::CreateRemoteRegs(pid); // can not get context, so create regs self
93     process.SetFaultThreadRegisters(regs);
94     Unwinder unwinder(pid, nsPid, request.type == ProcessDumpType::DUMP_TYPE_CPP_CRASH);
95     unwinder.SetRegs(regs);
96     unwinder.UnwindRemote(tid, true);
97     process.GetKeyThread()->SetFrames(unwinder.GetFrames());
98     Maps maps;
99     maps.Print(process, request, unwinder);
100     std::vector<std::string> keyWords = {
101         "Maps:",
102         "test_processdump",
103     };
104     for (const std::string& keyWord : keyWords) {
105         ASSERT_TRUE(CheckContent(result, keyWord, true));
106     }
107     std::string prevResult = result;
108     result = "";
109     CrashLogConfig crashLogConfig = {
110         .simplifyVmaPrinting = true,
111     };
112     process.SetCrashLogConfig(crashLogConfig);
113     maps.Print(process, request, unwinder);
114     for (const std::string& keyWord : keyWords) {
115         ASSERT_TRUE(CheckContent(result, keyWord, true));
116     }
117     ASSERT_GT(prevResult.size(), result.size());
118     process.Detach();
119     GTEST_LOG_(INFO) << "MapsTest001: end.";
120 }
121 }
122