1 /* 2 * Copyright (c) 2022 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 18 #include <cstdio> 19 #include <thread> 20 #include <unistd.h> 21 #include <malloc.h> 22 #include <securec.h> 23 #include "dfx_instr_statistic.h" 24 #include "dfx_ptrace.h" 25 #include "dfx_regs_get.h" 26 #include "procinfo.h" 27 #include "unwinder.h" 28 #include "dfx_test_util.h" 29 30 using namespace testing; 31 using namespace testing::ext; 32 33 namespace OHOS { 34 namespace HiviewDFX { 35 #undef LOG_DOMAIN 36 #undef LOG_TAG 37 #define LOG_TAG "DfxInstrStatisticTest" 38 #define LOG_DOMAIN 0xD002D11 39 40 class InstrStatisticTest : public testing::Test { 41 public: SetUpTestCase()42 static void SetUpTestCase() {} TearDownTestCase()43 static void TearDownTestCase() {} SetUp()44 void SetUp() {} TearDown()45 void TearDown() {} 46 }; 47 48 /** 49 * @tc.name: InstrStatisticTest001 50 * @tc.desc: test InstrStatistic interface 51 * @tc.type: FUNC 52 */ 53 HWTEST_F(InstrStatisticTest, InstrStatisticTest001, TestSize.Level2) 54 { 55 GTEST_LOG_(INFO) << "InstrStatisticTest001: start."; 56 static pid_t pid = getpid(); 57 static std::string elfName; 58 ReadProcessName(pid, elfName); 59 GTEST_LOG_(INFO) << "elfName: " << elfName; 60 DfxInstrStatistic::GetInstance().SetCurrentStatLib(elfName); 61 std::vector<std::pair<uint32_t, uint32_t>> result; 62 DfxInstrStatistic::GetInstance().DumpInstrStatResult(result); 63 pid_t child = fork(); 64 if (child == 0) { 65 GTEST_LOG_(INFO) << "pid: " << pid << ", ppid:" << getppid(); 66 auto unwinder = std::make_shared<Unwinder>(pid); 67 bool unwRet = DfxPtrace::Attach(pid); 68 EXPECT_EQ(true, unwRet) << "InstrStatisticTest001: Attach:" << unwRet; 69 auto regs = DfxRegs::CreateRemoteRegs(pid); 70 unwinder->SetRegs(regs); 71 auto maps = DfxMaps::Create(pid); 72 UnwindContext context; 73 context.pid = pid; 74 context.regs = regs; 75 context.maps = maps; 76 unwRet = unwinder->Unwind(&context); 77 EXPECT_EQ(true, unwRet) << "InstrStatisticTest001: Unwind:" << unwRet; 78 auto frames = unwinder->GetFrames(); 79 EXPECT_GT(frames.size(), 1); 80 GTEST_LOG_(INFO) << "frames:\n" << Unwinder::GetFramesStr(frames); 81 DfxPtrace::Detach(pid); 82 DfxInstrStatistic::GetInstance().DumpInstrStatResult(result); 83 EXPECT_GT(result.size(), 0); 84 for (size_t i = 0; i < result.size(); ++i) { 85 GTEST_LOG_(INFO) << result[i].first << result[i].second; 86 } 87 CheckAndExit(HasFailure()); 88 } 89 int status; 90 wait(&status); 91 ASSERT_EQ(status, 0); 92 GTEST_LOG_(INFO) << "InstrStatisticTest001: end."; 93 } 94 } // namespace HiviewDFX 95 } // namepsace OHOS