xref: /llvm-project/lldb/unittests/Target/ExecutionContextTest.cpp (revision d667840465b0ea0dbd39dcbd56788e73698dc853)
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/lldb-enumerations.h"
20 #include "lldb/lldb-forward.h"
21 #include "lldb/lldb-private-enumerations.h"
22 #include "lldb/lldb-private.h"
23 #include "llvm/Support/FormatVariadic.h"
24 #include "gtest/gtest.h"
25 
26 using namespace lldb_private;
27 using namespace lldb_private::repro;
28 using namespace lldb;
29 
30 namespace {
31 class ExecutionContextTest : public ::testing::Test {
32 public:
SetUp()33   void SetUp() override {
34     FileSystem::Initialize();
35     HostInfo::Initialize();
36     platform_linux::PlatformLinux::Initialize();
37   }
TearDown()38   void TearDown() override {
39     platform_linux::PlatformLinux::Terminate();
40     HostInfo::Terminate();
41     FileSystem::Terminate();
42   }
43 };
44 
45 class DummyProcess : public Process {
46 public:
DummyProcess(lldb::TargetSP target_sp,lldb::ListenerSP listener_sp)47   DummyProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp)
48       : Process(target_sp, listener_sp) {}
49 
CanDebug(lldb::TargetSP target,bool plugin_specified_by_name)50   bool CanDebug(lldb::TargetSP target, bool plugin_specified_by_name) override {
51     return true;
52   }
DoDestroy()53   Status DoDestroy() override { return {}; }
RefreshStateAfterStop()54   void RefreshStateAfterStop() override {}
DoReadMemory(lldb::addr_t vm_addr,void * buf,size_t size,Status & error)55   size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
56                       Status &error) override {
57     return 0;
58   }
DoUpdateThreadList(ThreadList & old_thread_list,ThreadList & new_thread_list)59   bool DoUpdateThreadList(ThreadList &old_thread_list,
60                           ThreadList &new_thread_list) override {
61     return false;
62   }
GetPluginName()63   llvm::StringRef GetPluginName() override { return "Dummy"; }
64 };
65 } // namespace
66 
TEST_F(ExecutionContextTest,GetByteOrder)67 TEST_F(ExecutionContextTest, GetByteOrder) {
68   ExecutionContext exe_ctx(nullptr, nullptr, nullptr);
69   EXPECT_EQ(endian::InlHostByteOrder(), exe_ctx.GetByteOrder());
70 }
71 
TEST_F(ExecutionContextTest,GetByteOrderTarget)72 TEST_F(ExecutionContextTest, GetByteOrderTarget) {
73   ArchSpec arch("powerpc64-pc-linux");
74 
75   Platform::SetHostPlatform(
76       platform_linux::PlatformLinux::CreateInstance(true, &arch));
77 
78   DebuggerSP debugger_sp = Debugger::CreateInstance();
79   ASSERT_TRUE(debugger_sp);
80 
81   TargetSP target_sp;
82   PlatformSP platform_sp;
83   Status error = debugger_sp->GetTargetList().CreateTarget(
84       *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp);
85   ASSERT_TRUE(target_sp);
86   ASSERT_TRUE(target_sp->GetArchitecture().IsValid());
87   ASSERT_TRUE(platform_sp);
88 
89   ExecutionContext target_ctx(target_sp, false);
90   EXPECT_EQ(target_sp->GetArchitecture().GetByteOrder(),
91             target_ctx.GetByteOrder());
92 }
93 
TEST_F(ExecutionContextTest,GetByteOrderProcess)94 TEST_F(ExecutionContextTest, GetByteOrderProcess) {
95   ArchSpec arch("powerpc64-pc-linux");
96 
97   Platform::SetHostPlatform(
98       platform_linux::PlatformLinux::CreateInstance(true, &arch));
99 
100   DebuggerSP debugger_sp = Debugger::CreateInstance();
101   ASSERT_TRUE(debugger_sp);
102 
103   TargetSP target_sp;
104   PlatformSP platform_sp;
105   Status error = debugger_sp->GetTargetList().CreateTarget(
106       *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp);
107   ASSERT_TRUE(target_sp);
108   ASSERT_TRUE(target_sp->GetArchitecture().IsValid());
109   ASSERT_TRUE(platform_sp);
110 
111   ListenerSP listener_sp(Listener::MakeListener("dummy"));
112   ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp);
113   ASSERT_TRUE(process_sp);
114 
115   ExecutionContext process_ctx(process_sp);
116   EXPECT_EQ(process_sp->GetByteOrder(), process_ctx.GetByteOrder());
117 }
118