• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
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/hwext/gtest-ext.h>
17 #include <gtest/hwext/gtest-tag.h>
18 
19 #include "hidebug/hidebug.h"
20 
21 using namespace testing::ext;
22 
23 class HidebugBacktraceTest : public ::testing::Test {};
24 /**
25  * @tc.name: BacktraceFromFpTest
26  * @tc.desc: test OH_HiDebug_BacktraceFromFp
27  * @tc.type: FUNC
28  */
29 HWTEST_F(HidebugBacktraceTest, BacktraceFromFpTest, TestSize.Level0)
30 {
31     EXPECT_EQ(OH_HiDebug_BacktraceFromFp(nullptr, nullptr, nullptr, 0), 0);
32     HiDebug_Backtrace_Object obj = OH_HiDebug_CreateBacktraceObject();
33 #if is_ohos && !is_mingw && __aarch64__
34     EXPECT_NE(obj, nullptr);
35     EXPECT_EQ(OH_HiDebug_BacktraceFromFp(obj, nullptr, nullptr, 0), 0);
36     EXPECT_EQ(OH_HiDebug_BacktraceFromFp(obj, __builtin_frame_address(0), nullptr, 0), 0);
37     constexpr auto testArraySize = 10;
38     void *pcArray[testArraySize]{};
39     EXPECT_EQ(OH_HiDebug_BacktraceFromFp(obj, __builtin_frame_address(0), pcArray, 0), 0);
40     EXPECT_GT(OH_HiDebug_BacktraceFromFp(obj, __builtin_frame_address(0), pcArray, testArraySize), 0);
41     EXPECT_LE(OH_HiDebug_BacktraceFromFp(obj, __builtin_frame_address(0), pcArray, testArraySize), testArraySize);
42     EXPECT_EQ(pcArray[0], __builtin_return_address(0));
43     OH_HiDebug_DestroyBacktraceObject(obj);
44 #else
45     EXPECT_EQ(obj, nullptr);
46 #endif
47 }
48 
49 #if is_ohos && !is_mingw && __aarch64__
50 /**
51  * @tc.name: SymbolicAddressTest
52  * @tc.desc: test OH_HiDebug_SymbolicAddress
53  * @tc.type: FUNC
54  */
55 HWTEST_F(HidebugBacktraceTest, SymbolicAddressTest, TestSize.Level0)
56 {
57     EXPECT_EQ(OH_HiDebug_SymbolicAddress(nullptr, nullptr, nullptr, nullptr), HIDEBUG_INVALID_ARGUMENT);
58     HiDebug_Backtrace_Object obj = OH_HiDebug_CreateBacktraceObject();
59     EXPECT_NE(obj, nullptr);
60     EXPECT_EQ(OH_HiDebug_SymbolicAddress(obj, nullptr, nullptr, nullptr), HIDEBUG_INVALID_ARGUMENT);
61     EXPECT_EQ(OH_HiDebug_SymbolicAddress(obj, __builtin_return_address(0), nullptr, nullptr), HIDEBUG_INVALID_ARGUMENT);
__anonc00a800e0102(void* pc, void* arg, const HiDebug_StackFrame* frame) 62     auto callback = [] (void* pc, void* arg, const HiDebug_StackFrame* frame) {
63         EXPECT_EQ(frame->type, HiDebug_StackFrameType::HIDEBUG_STACK_FRAME_TYPE_NATIVE);
64         (*static_cast<int*>(arg))++;
65     };
66     EXPECT_EQ(OH_HiDebug_SymbolicAddress(obj, &callback, nullptr, callback), HIDEBUG_INVALID_SYMBOLIC_PC_ADDRESS);
67     int testNum = 0;
68     EXPECT_EQ(OH_HiDebug_SymbolicAddress(obj, __builtin_return_address(0), &testNum, callback), HIDEBUG_SUCCESS);
69     EXPECT_EQ(testNum, 1);
70     OH_HiDebug_DestroyBacktraceObject(obj);
71 }
72 #endif
73