xref: /aosp_15_r20/system/unwinding/libunwindstack/tests/RegsRemoteTest.cpp (revision eb293b8f56ee8303637c5595cfcdeef8039e85c6)
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <signal.h>
18 #include <stdint.h>
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
22 
23 #include <memory>
24 
25 #include <gtest/gtest.h>
26 
27 #include <unwindstack/Regs.h>
28 
29 #include "ForkTest.h"
30 #include "PidUtils.h"
31 
32 namespace unwindstack {
33 
34 class RegsRemoteTest : public ForkTest {
35  protected:
SetUp()36   void SetUp() override { Fork(); }
37 };
38 
TEST_F(RegsRemoteTest,remote_get)39 TEST_F(RegsRemoteTest, remote_get) {
40   std::unique_ptr<Regs> regs(Regs::RemoteGet(pid_));
41 #if defined(__arm__)
42   ASSERT_EQ(ARCH_ARM, regs->Arch());
43 #elif defined(__aarch64__)
44   ASSERT_EQ(ARCH_ARM64, regs->Arch());
45 #elif defined(__i386__)
46   ASSERT_EQ(ARCH_X86, regs->Arch());
47 #elif defined(__x86_64__)
48   ASSERT_EQ(ARCH_X86_64, regs->Arch());
49 #elif defined(__riscv)
50   ASSERT_EQ(ARCH_RISCV64, regs->Arch());
51 #else
52   ASSERT_EQ(nullptr, regs.get());
53 #endif
54 }
55 
TEST_F(RegsRemoteTest,remote_get_ptrace_fails)56 TEST_F(RegsRemoteTest, remote_get_ptrace_fails) {
57   ErrorCode error_code;
58   // Use our own pid since that will always fail.
59   std::unique_ptr<Regs> regs(Regs::RemoteGet(getpid(), &error_code));
60   ASSERT_TRUE(regs == nullptr);
61   ASSERT_EQ(ERROR_PTRACE_CALL, error_code);
62 }
63 
TEST_F(RegsRemoteTest,remote_get_arch)64 TEST_F(RegsRemoteTest, remote_get_arch) {
65 #if defined(__arm__)
66   ASSERT_EQ(ARCH_ARM, Regs::RemoteGetArch(pid_));
67 #elif defined(__aarch64__)
68   ASSERT_EQ(ARCH_ARM64, Regs::RemoteGetArch(pid_));
69 #elif defined(__i386__)
70   ASSERT_EQ(ARCH_X86, Regs::RemoteGetArch(pid_));
71 #elif defined(__x86_64__)
72   ASSERT_EQ(ARCH_X86_64, Regs::RemoteGetArch(pid_));
73 #elif defined(__riscv)
74   ASSERT_EQ(ARCH_RISCV64, Regs::RemoteGetArch(pid_));
75 #else
76   ASSERT_EQ(ARCH_NONE, Regs::RemoteGetArch(pid_));
77 #endif
78 }
79 
TEST_F(RegsRemoteTest,remote_get_arch_ptrace_fails)80 TEST_F(RegsRemoteTest, remote_get_arch_ptrace_fails) {
81   ErrorCode error_code;
82   // Use our own pid since that will always fail.
83   ASSERT_EQ(ARCH_UNKNOWN, Regs::RemoteGetArch(getpid(), &error_code));
84   ASSERT_EQ(ERROR_PTRACE_CALL, error_code);
85 }
86 
87 }  // namespace unwindstack
88