1 //===-- ExecutionContextTest.cpp ------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Target/ExecutionContext.h"
10 #include "Plugins/Platform/Linux/PlatformLinux.h"
11 #include "lldb/Core/Debugger.h"
12 #include "lldb/Host/FileSystem.h"
13 #include "lldb/Host/HostInfo.h"
14 #include "lldb/Target/Platform.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Utility/ArchSpec.h"
18 #include "lldb/Utility/Endian.h"
19 #include "lldb/Utility/Reproducer.h"
20 #include "lldb/lldb-enumerations.h"
21 #include "lldb/lldb-forward.h"
22 #include "lldb/lldb-private-enumerations.h"
23 #include "lldb/lldb-private.h"
24 #include "llvm/Support/FormatVariadic.h"
25 #include "gtest/gtest.h"
26 
27 using namespace lldb_private;
28 using namespace lldb_private::repro;
29 using namespace lldb;
30 
31 namespace {
32 class ExecutionContextTest : public ::testing::Test {
33 public:
SetUp()34   void SetUp() override {
35     llvm::cantFail(Reproducer::Initialize(ReproducerMode::Off, llvm::None));
36     FileSystem::Initialize();
37     HostInfo::Initialize();
38     platform_linux::PlatformLinux::Initialize();
39   }
TearDown()40   void TearDown() override {
41     platform_linux::PlatformLinux::Terminate();
42     HostInfo::Terminate();
43     FileSystem::Terminate();
44     Reproducer::Terminate();
45   }
46 };
47 
48 class DummyProcess : public Process {
49 public:
50   using Process::Process;
51 
CanDebug(lldb::TargetSP target,bool plugin_specified_by_name)52   bool CanDebug(lldb::TargetSP target, bool plugin_specified_by_name) override {
53     return true;
54   }
DoDestroy()55   Status DoDestroy() override { return {}; }
RefreshStateAfterStop()56   void RefreshStateAfterStop() override {}
DoReadMemory(lldb::addr_t vm_addr,void * buf,size_t size,Status & error)57   size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
58                       Status &error) override {
59     return 0;
60   }
DoUpdateThreadList(ThreadList & old_thread_list,ThreadList & new_thread_list)61   bool DoUpdateThreadList(ThreadList &old_thread_list,
62                           ThreadList &new_thread_list) override {
63     return false;
64   }
GetPluginName()65   llvm::StringRef GetPluginName() override { return "Dummy"; }
66 };
67 } // namespace
68 
TEST_F(ExecutionContextTest,GetByteOrder)69 TEST_F(ExecutionContextTest, GetByteOrder) {
70   ExecutionContext exe_ctx(nullptr, nullptr, nullptr);
71   EXPECT_EQ(endian::InlHostByteOrder(), exe_ctx.GetByteOrder());
72 }
73 
TEST_F(ExecutionContextTest,GetByteOrderTarget)74 TEST_F(ExecutionContextTest, GetByteOrderTarget) {
75   ArchSpec arch("powerpc64-pc-linux");
76 
77   Platform::SetHostPlatform(
78       platform_linux::PlatformLinux::CreateInstance(true, &arch));
79 
80   DebuggerSP debugger_sp = Debugger::CreateInstance();
81   ASSERT_TRUE(debugger_sp);
82 
83   TargetSP target_sp;
84   PlatformSP platform_sp;
85   Status error = debugger_sp->GetTargetList().CreateTarget(
86       *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp);
87   ASSERT_TRUE(target_sp);
88   ASSERT_TRUE(target_sp->GetArchitecture().IsValid());
89   ASSERT_TRUE(platform_sp);
90 
91   ExecutionContext target_ctx(target_sp, false);
92   EXPECT_EQ(target_sp->GetArchitecture().GetByteOrder(),
93             target_ctx.GetByteOrder());
94 }
95 
TEST_F(ExecutionContextTest,GetByteOrderProcess)96 TEST_F(ExecutionContextTest, GetByteOrderProcess) {
97   ArchSpec arch("powerpc64-pc-linux");
98 
99   Platform::SetHostPlatform(
100       platform_linux::PlatformLinux::CreateInstance(true, &arch));
101 
102   DebuggerSP debugger_sp = Debugger::CreateInstance();
103   ASSERT_TRUE(debugger_sp);
104 
105   TargetSP target_sp;
106   PlatformSP platform_sp;
107   Status error = debugger_sp->GetTargetList().CreateTarget(
108       *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp);
109   ASSERT_TRUE(target_sp);
110   ASSERT_TRUE(target_sp->GetArchitecture().IsValid());
111   ASSERT_TRUE(platform_sp);
112 
113   ListenerSP listener_sp(Listener::MakeListener("dummy"));
114   ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp);
115   ASSERT_TRUE(process_sp);
116 
117   ExecutionContext process_ctx(process_sp);
118   EXPECT_EQ(process_sp->GetByteOrder(), process_ctx.GetByteOrder());
119 }
120