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 virtual bool CanDebug(lldb::TargetSP target, bool plugin_specified_by_name) {
53 return true;
54 }
DoDestroy()55 virtual Status DoDestroy() { return {}; }
RefreshStateAfterStop()56 virtual void RefreshStateAfterStop() {}
DoReadMemory(lldb::addr_t vm_addr,void * buf,size_t size,Status & error)57 virtual size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
58 Status &error) {
59 return 0;
60 }
UpdateThreadList(ThreadList & old_thread_list,ThreadList & new_thread_list)61 virtual bool UpdateThreadList(ThreadList &old_thread_list,
62 ThreadList &new_thread_list) {
63 return false;
64 }
GetPluginName()65 virtual ConstString GetPluginName() { return ConstString("Dummy"); }
GetPluginVersion()66 virtual uint32_t GetPluginVersion() { return 0; }
67 };
68 } // namespace
69
TEST_F(ExecutionContextTest,GetByteOrder)70 TEST_F(ExecutionContextTest, GetByteOrder) {
71 ExecutionContext exe_ctx(nullptr, nullptr, nullptr);
72 EXPECT_EQ(endian::InlHostByteOrder(), exe_ctx.GetByteOrder());
73 }
74
TEST_F(ExecutionContextTest,GetByteOrderTarget)75 TEST_F(ExecutionContextTest, GetByteOrderTarget) {
76 ArchSpec arch("powerpc64-pc-linux");
77
78 Platform::SetHostPlatform(
79 platform_linux::PlatformLinux::CreateInstance(true, &arch));
80
81 DebuggerSP debugger_sp = Debugger::CreateInstance();
82 ASSERT_TRUE(debugger_sp);
83
84 TargetSP target_sp;
85 PlatformSP platform_sp;
86 Status error = debugger_sp->GetTargetList().CreateTarget(
87 *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp);
88 ASSERT_TRUE(target_sp);
89 ASSERT_TRUE(target_sp->GetArchitecture().IsValid());
90 ASSERT_TRUE(platform_sp);
91
92 ExecutionContext target_ctx(target_sp, false);
93 EXPECT_EQ(target_sp->GetArchitecture().GetByteOrder(),
94 target_ctx.GetByteOrder());
95 }
96
TEST_F(ExecutionContextTest,GetByteOrderProcess)97 TEST_F(ExecutionContextTest, GetByteOrderProcess) {
98 ArchSpec arch("powerpc64-pc-linux");
99
100 Platform::SetHostPlatform(
101 platform_linux::PlatformLinux::CreateInstance(true, &arch));
102
103 DebuggerSP debugger_sp = Debugger::CreateInstance();
104 ASSERT_TRUE(debugger_sp);
105
106 TargetSP target_sp;
107 PlatformSP platform_sp;
108 Status error = debugger_sp->GetTargetList().CreateTarget(
109 *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp);
110 ASSERT_TRUE(target_sp);
111 ASSERT_TRUE(target_sp->GetArchitecture().IsValid());
112 ASSERT_TRUE(platform_sp);
113
114 ListenerSP listener_sp(Listener::MakeListener("dummy"));
115 ProcessSP process_sp = std::make_shared<DummyProcess>(target_sp, listener_sp);
116 ASSERT_TRUE(process_sp);
117
118 ExecutionContext process_ctx(process_sp);
119 EXPECT_EQ(process_sp->GetByteOrder(), process_ctx.GetByteOrder());
120 }
121