1 //===-- ExecutionContextTest.cpp ------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
27 using namespace lldb_private
;
28 using namespace lldb_private::repro
;
32 class ExecutionContextTest
: public ::testing::Test
{
34 void SetUp() override
{
35 llvm::cantFail(Reproducer::Initialize(ReproducerMode::Off
, llvm::None
));
36 FileSystem::Initialize();
37 HostInfo::Initialize();
38 platform_linux::PlatformLinux::Initialize();
40 void TearDown() override
{
41 platform_linux::PlatformLinux::Terminate();
42 HostInfo::Terminate();
43 FileSystem::Terminate();
44 Reproducer::Terminate();
48 class DummyProcess
: public Process
{
50 using Process::Process
;
52 bool CanDebug(lldb::TargetSP target
, bool plugin_specified_by_name
) override
{
55 Status
DoDestroy() override
{ return {}; }
56 void RefreshStateAfterStop() override
{}
57 size_t DoReadMemory(lldb::addr_t vm_addr
, void *buf
, size_t size
,
58 Status
&error
) override
{
61 bool DoUpdateThreadList(ThreadList
&old_thread_list
,
62 ThreadList
&new_thread_list
) override
{
65 llvm::StringRef
GetPluginName() override
{ return "Dummy"; }
69 TEST_F(ExecutionContextTest
, GetByteOrder
) {
70 ExecutionContext
exe_ctx(nullptr, nullptr, nullptr);
71 EXPECT_EQ(endian::InlHostByteOrder(), exe_ctx
.GetByteOrder());
74 TEST_F(ExecutionContextTest
, GetByteOrderTarget
) {
75 ArchSpec
arch("powerpc64-pc-linux");
77 Platform::SetHostPlatform(
78 platform_linux::PlatformLinux::CreateInstance(true, &arch
));
80 DebuggerSP debugger_sp
= Debugger::CreateInstance();
81 ASSERT_TRUE(debugger_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
);
91 ExecutionContext
target_ctx(target_sp
, false);
92 EXPECT_EQ(target_sp
->GetArchitecture().GetByteOrder(),
93 target_ctx
.GetByteOrder());
96 TEST_F(ExecutionContextTest
, GetByteOrderProcess
) {
97 ArchSpec
arch("powerpc64-pc-linux");
99 Platform::SetHostPlatform(
100 platform_linux::PlatformLinux::CreateInstance(true, &arch
));
102 DebuggerSP debugger_sp
= Debugger::CreateInstance();
103 ASSERT_TRUE(debugger_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
);
113 ListenerSP
listener_sp(Listener::MakeListener("dummy"));
114 ProcessSP process_sp
= std::make_shared
<DummyProcess
>(target_sp
, listener_sp
);
115 ASSERT_TRUE(process_sp
);
117 ExecutionContext
process_ctx(process_sp
);
118 EXPECT_EQ(process_sp
->GetByteOrder(), process_ctx
.GetByteOrder());