1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#include <gtest/gtest.h>
17800b99b8Sopenharmony_ci
18800b99b8Sopenharmony_ci#include <cstdio>
19800b99b8Sopenharmony_ci#include <thread>
20800b99b8Sopenharmony_ci#include <unistd.h>
21800b99b8Sopenharmony_ci#include <malloc.h>
22800b99b8Sopenharmony_ci#include <securec.h>
23800b99b8Sopenharmony_ci#include "dfx_instr_statistic.h"
24800b99b8Sopenharmony_ci#include "dfx_ptrace.h"
25800b99b8Sopenharmony_ci#include "dfx_regs_get.h"
26800b99b8Sopenharmony_ci#include "procinfo.h"
27800b99b8Sopenharmony_ci#include "unwinder.h"
28800b99b8Sopenharmony_ci
29800b99b8Sopenharmony_ciusing namespace testing;
30800b99b8Sopenharmony_ciusing namespace testing::ext;
31800b99b8Sopenharmony_ci
32800b99b8Sopenharmony_cinamespace OHOS {
33800b99b8Sopenharmony_cinamespace HiviewDFX {
34800b99b8Sopenharmony_ci#undef LOG_DOMAIN
35800b99b8Sopenharmony_ci#undef LOG_TAG
36800b99b8Sopenharmony_ci#define LOG_TAG "DfxInstrStatisticTest"
37800b99b8Sopenharmony_ci#define LOG_DOMAIN 0xD002D11
38800b99b8Sopenharmony_ci
39800b99b8Sopenharmony_ciclass InstrStatisticTest : public testing::Test {
40800b99b8Sopenharmony_cipublic:
41800b99b8Sopenharmony_ci    static void SetUpTestCase() {}
42800b99b8Sopenharmony_ci    static void TearDownTestCase() {}
43800b99b8Sopenharmony_ci    void SetUp() {}
44800b99b8Sopenharmony_ci    void TearDown() {}
45800b99b8Sopenharmony_ci};
46800b99b8Sopenharmony_ci
47800b99b8Sopenharmony_ci/**
48800b99b8Sopenharmony_ci * @tc.name: InstrStatisticTest001
49800b99b8Sopenharmony_ci * @tc.desc: test InstrStatistic interface
50800b99b8Sopenharmony_ci * @tc.type: FUNC
51800b99b8Sopenharmony_ci */
52800b99b8Sopenharmony_ciHWTEST_F(InstrStatisticTest, InstrStatisticTest001, TestSize.Level2)
53800b99b8Sopenharmony_ci{
54800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "InstrStatisticTest001: start.";
55800b99b8Sopenharmony_ci    static pid_t pid = getpid();
56800b99b8Sopenharmony_ci    static std::string elfName;
57800b99b8Sopenharmony_ci    ReadProcessName(pid, elfName);
58800b99b8Sopenharmony_ci    pid_t child = fork();
59800b99b8Sopenharmony_ci    if (child == 0) {
60800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "elfName: " << elfName;
61800b99b8Sopenharmony_ci        DfxInstrStatistic::GetInstance().SetCurrentStatLib(elfName);
62800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "pid: " << pid << ", ppid:" << getppid();
63800b99b8Sopenharmony_ci        auto unwinder = std::make_shared<Unwinder>(pid);
64800b99b8Sopenharmony_ci        bool unwRet = DfxPtrace::Attach(pid);
65800b99b8Sopenharmony_ci        EXPECT_EQ(true, unwRet) << "InstrStatisticTest001: Attach:" << unwRet;
66800b99b8Sopenharmony_ci        auto regs = DfxRegs::CreateRemoteRegs(pid);
67800b99b8Sopenharmony_ci        unwinder->SetRegs(regs);
68800b99b8Sopenharmony_ci        auto maps = DfxMaps::Create(pid);
69800b99b8Sopenharmony_ci        UnwindContext context;
70800b99b8Sopenharmony_ci        context.pid = pid;
71800b99b8Sopenharmony_ci        context.regs = regs;
72800b99b8Sopenharmony_ci        context.maps = maps;
73800b99b8Sopenharmony_ci        unwRet = unwinder->Unwind(&context);
74800b99b8Sopenharmony_ci        EXPECT_EQ(true, unwRet) << "InstrStatisticTest001: Unwind:" << unwRet;
75800b99b8Sopenharmony_ci        auto frames = unwinder->GetFrames();
76800b99b8Sopenharmony_ci        ASSERT_GT(frames.size(), 1);
77800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "frames:\n" << Unwinder::GetFramesStr(frames);
78800b99b8Sopenharmony_ci        DfxPtrace::Detach(pid);
79800b99b8Sopenharmony_ci        std::vector<std::pair<uint32_t, uint32_t>> result;
80800b99b8Sopenharmony_ci        DfxInstrStatistic::GetInstance().DumpInstrStatResult(result);
81800b99b8Sopenharmony_ci        ASSERT_GT(result.size(), 0);
82800b99b8Sopenharmony_ci        for (size_t i = 0; i < result.size(); ++i) {
83800b99b8Sopenharmony_ci            GTEST_LOG_(INFO) << result[i].first << result[i].second;
84800b99b8Sopenharmony_ci        }
85800b99b8Sopenharmony_ci        _exit(0);
86800b99b8Sopenharmony_ci    }
87800b99b8Sopenharmony_ci
88800b99b8Sopenharmony_ci    int status;
89800b99b8Sopenharmony_ci    int ret = wait(&status);
90800b99b8Sopenharmony_ci    ASSERT_EQ(status, 0);
91800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "Status:" << status << " Result:" << ret;
92800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "InstrStatisticTest001: end.";
93800b99b8Sopenharmony_ci}
94800b99b8Sopenharmony_ci} // namespace HiviewDFX
95800b99b8Sopenharmony_ci} // namepsace OHOS