LLDB mainline
PlatformWasm.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
18#include "lldb/Target/Process.h"
19#include "lldb/Target/Target.h"
22#include "lldb/Utility/Log.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/Support/ErrorExtras.h"
25
26using namespace lldb;
27using namespace lldb_private;
28
30
31namespace {
32#define LLDB_PROPERTIES_platformwasm
33#include "PlatformWasmProperties.inc"
34
35enum {
36#define LLDB_PROPERTIES_platformwasm
37#include "PlatformWasmPropertiesEnum.inc"
38};
39
40class PluginProperties : public Properties {
41public:
42 PluginProperties() {
43 m_collection_sp = std::make_shared<OptionValueProperties>(
45 m_collection_sp->Initialize(g_platformwasm_properties_def);
46 }
47
48 FileSpec GetRuntimePath() const {
49 return GetPropertyAtIndexAs<FileSpec>(ePropertyRuntimePath, {});
50 }
51
52 Args GetRuntimeArgs() const {
53 Args result;
54 m_collection_sp->GetPropertyAtIndexAsArgs(ePropertyRuntimeArgs, result);
55 return result;
56 }
57
58 llvm::StringRef GetPortArg() const {
59 return GetPropertyAtIndexAs<llvm::StringRef>(ePropertyPortArg, {});
60 }
61};
62
63} // namespace
64
65static PluginProperties &GetGlobalProperties() {
66 static PluginProperties g_settings;
67 return g_settings;
68}
69
71 return "Platform for debugging Wasm";
72}
73
80
85
90 debugger, GetGlobalProperties().GetValueProperties(),
91 "Properties for the wasm platform plugin.",
92 /*is_global_property=*/true);
93 }
94}
95
98 LLDB_LOG(log, "force = {0}, arch = ({1}, {2})", force,
99 arch ? arch->GetArchitectureName() : "<null>",
100 arch ? arch->GetTriple().getTriple() : "<null>");
101
102 bool create = force;
103 if (!create && arch && arch->IsValid()) {
104 const llvm::Triple &triple = arch->GetTriple();
105 switch (triple.getArch()) {
106 case llvm::Triple::wasm32:
107 case llvm::Triple::wasm64:
108 create = true;
109 break;
110 default:
111 break;
112 }
113 }
114
115 LLDB_LOG(log, "create = {0}", create);
116 return create ? PlatformSP(new PlatformWasm()) : PlatformSP();
117}
118
119llvm::Expected<uint16_t> PlatformWasm::FindFreeTCPPort() {
120 TCPSocket sock(/*should_close=*/true);
121 Status status = sock.Listen("localhost:0", /*backlog=*/5);
122 if (status.Fail())
123 return status.takeError();
124 return sock.GetLocalPortNumber();
125}
126
127std::vector<ArchSpec>
129 return {ArchSpec("wasm32-unknown-unknown-wasm"),
130 ArchSpec("wasm64-unknown-unknown-wasm")};
131}
132
134 Debugger &debugger, Target *target,
135 Status &status) {
137 return m_remote_platform_sp->Attach(attach_info, debugger, target, status);
138
140 "attaching is only supported when connected to a remote Wasm platform");
141 return nullptr;
142}
143
145 Debugger &debugger, Target &target,
146 Status &error) {
148 return m_remote_platform_sp->DebugProcess(launch_info, debugger, target,
149 error);
150
152
153 const PluginProperties &properties = GetGlobalProperties();
154
155 FileSpec runtime = properties.GetRuntimePath();
157
158 if (!FileSystem::Instance().Exists(runtime)) {
160 "WebAssembly runtime does not exist: {0}", runtime.GetPath());
161 return nullptr;
162 }
163
164 llvm::Expected<uint16_t> expected_port = FindFreeTCPPort();
165 if (!expected_port) {
166 error = Status::FromError(expected_port.takeError());
167 return nullptr;
168 }
169 uint16_t port = *expected_port;
170
171 Args args({runtime.GetPath(),
172 llvm::formatv("{0}{1}", properties.GetPortArg(), port).str()});
173 args.AppendArguments(properties.GetRuntimeArgs());
174 args.AppendArguments(launch_info.GetArguments());
175
176 launch_info.SetArguments(args, true);
177 launch_info.SetLaunchInSeparateProcessGroup(true);
178 // We're launching the Wasm runtime (a native host binary), not the target
179 // being debugged. Clear flags that don't apply to the runtime process.
180 launch_info.GetFlags().Clear(eLaunchFlagDebug | eLaunchFlagDisableASLR);
181 launch_info.GetEnvironment() = Host::GetEnvironment();
182
183 auto exit_code = std::make_shared<std::optional<int>>();
184 launch_info.SetMonitorProcessCallback(
185 [=](lldb::pid_t pid, int signal, int status) {
186 LLDB_LOG(
187 log,
188 "WebAssembly runtime exited: pid = {0}, signal = {1}, status = {2}",
189 pid, signal, status);
190 exit_code->emplace(status);
191 });
192
193 // This is automatically done for host platform in
194 // Target::FinalizeFileActions, but we're not a host platform.
195 llvm::Error Err = launch_info.SetUpPtyRedirection();
196 LLDB_LOG_ERROR(log, std::move(Err), "SetUpPtyRedirection failed: {0}");
197
198 LLDB_LOG(log, "{0}", GetArgRange(launch_info.GetArguments()));
199 error = Host::LaunchProcess(launch_info);
200 if (error.Fail())
201 return nullptr;
202
203 ProcessSP process_sp = target.CreateProcess(
205 nullptr, true);
206 if (!process_sp) {
207 error = Status::FromErrorString("failed to create WebAssembly process");
208 return nullptr;
209 }
210
211 process_sp->HijackProcessEvents(launch_info.GetHijackListener());
212
213 error = process_sp->ConnectRemote(
214 llvm::formatv("connect://localhost:{0}", port).str());
215 if (error.Fail()) {
216 // If we know the runtime has exited, that's a better error message than
217 // failing to connect.
218 if (*exit_code)
219 error = Status::FromError(llvm::joinErrors(
220 llvm::createStringErrorV(
221 "WebAssembly runtime exited with exit code {0}", **exit_code),
222 error.takeError()));
223
224 return nullptr;
225 }
226#ifndef _WIN32
227 if (launch_info.GetPTY().GetPrimaryFileDescriptor() !=
229 process_sp->SetSTDIOFileDescriptor(
230 launch_info.GetPTY().ReleasePrimaryFileDescriptor());
231#endif
232 return process_sp;
233}
234
236 if (IsHost())
238 "can't connect to the host platform, always connected");
239
242
243 return m_remote_platform_sp->ConnectRemote(args);
244}
static llvm::raw_ostream & error(Stream &strm)
static DynamicLoaderDarwinKernelProperties & GetGlobalProperties()
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:364
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:394
static PluginProperties & GetGlobalProperties()
#define LLDB_PLUGIN_DEFINE(PluginName)
An architecture specification class.
Definition ArchSpec.h:32
bool IsValid() const
Tests if this ArchSpec is valid.
Definition ArchSpec.h:367
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:457
const char * GetArchitectureName() const
Returns a static string representing the current architecture.
Definition ArchSpec.cpp:548
A command line argument class.
Definition Args.h:33
void AppendArguments(const Args &rhs)
Definition Args.cpp:307
A class to manage flag bits.
Definition Debugger.h:100
A file utility class.
Definition FileSpec.h:57
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:374
bool ResolveExecutableLocation(FileSpec &file_spec)
Call into the Host to see if it can help find the file.
static FileSystem & Instance()
ValueType Clear(ValueType mask=~static_cast< ValueType >(0))
Clear one or more flags.
Definition Flags.h:61
static Status LaunchProcess(ProcessLaunchInfo &launch_info)
Launch the process specified in launch_info.
static Environment GetEnvironment()
static lldb::PlatformSP CreateInstance(bool force, const ArchSpec *arch)
static auto GetArgRange(const Args &args)
lldb::ProcessSP DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger, Target &target, Status &error) override
Subclasses do not need to implement this function as it uses the Platform::LaunchProcess() followed b...
Status ConnectRemote(Args &args) override
static void DebuggerInitialize(Debugger &debugger)
static llvm::Expected< uint16_t > FindFreeTCPPort()
Find a free TCP port by binding to port 0.
static llvm::StringRef GetPluginNameStatic()
lldb::ProcessSP Attach(ProcessAttachInfo &attach_info, Debugger &debugger, Target *target, Status &status) override
Attach to an existing process using a process ID.
std::vector< ArchSpec > GetSupportedArchitectures(const ArchSpec &process_host_arch) override
Get the platform's supported architectures in the order in which they should be searched.
static llvm::StringRef GetPluginDescriptionStatic()
bool IsHost() const
Definition Platform.h:528
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static lldb::OptionValuePropertiesSP GetSettingForPlatformPlugin(Debugger &debugger, llvm::StringRef setting_name)
static bool CreateSettingForPlatformPlugin(Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp, llvm::StringRef description, bool is_global_property)
static bool UnregisterPlugin(ABICreateInstance create_callback)
lldb::ListenerSP GetHijackListener() const
void SetArguments(const Args &args, bool first_arg_is_executable)
lldb::ListenerSP GetListener() const
Environment & GetEnvironment()
Definition ProcessInfo.h:88
void SetMonitorProcessCallback(Host::MonitorChildProcessCallback callback)
void SetLaunchInSeparateProcessGroup(bool separate)
@ invalid_fd
Invalid file descriptor value.
int GetPrimaryFileDescriptor() const
The primary file descriptor accessor.
int ReleasePrimaryFileDescriptor()
Release the primary file descriptor.
An error handling class.
Definition Status.h:118
llvm::Error takeError()
Definition Status.h:170
static Status FromErrorString(const char *str)
Definition Status.h:141
bool Fail() const
Test for error condition.
Definition Status.cpp:293
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
Definition Status.h:151
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
Definition Status.cpp:136
Status Listen(llvm::StringRef name, int backlog) override
uint16_t GetLocalPortNumber() const
Definition TCPSocket.cpp:86
const lldb::ProcessSP & CreateProcess(lldb::ListenerSP listener_sp, llvm::StringRef plugin_name, const FileSpec *crash_file, bool can_connect)
Definition Target.cpp:302
static llvm::StringRef GetPluginNameStatic()
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:327
std::shared_ptr< lldb_private::Platform > PlatformSP
std::shared_ptr< lldb_private::Process > ProcessSP
uint64_t pid_t
Definition lldb-types.h:83