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