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
17#include "lldb/Target/Process.h"
18#include "lldb/Target/Target.h"
21#include "lldb/Utility/Log.h"
22#include "llvm/ADT/StringExtras.h"
23#include "llvm/Support/ErrorExtras.h"
24
25using namespace lldb;
26using namespace lldb_private;
27
29
30namespace {
31#define LLDB_PROPERTIES_platformwasm
32#include "PlatformWasmProperties.inc"
33
34enum {
35#define LLDB_PROPERTIES_platformwasm
36#include "PlatformWasmPropertiesEnum.inc"
37};
38
39class PluginProperties : public Properties {
40public:
41 PluginProperties() {
42 m_collection_sp = std::make_shared<OptionValueProperties>(
44 m_collection_sp->Initialize(g_platformwasm_properties);
45 }
46
47 FileSpec GetRuntimePath() const {
48 return GetPropertyAtIndexAs<FileSpec>(ePropertyRuntimePath, {});
49 }
50
51 Args GetRuntimeArgs() const {
52 Args result;
53 m_collection_sp->GetPropertyAtIndexAsArgs(ePropertyRuntimeArgs, result);
54 return result;
55 }
56
57 llvm::StringRef GetPortArg() const {
58 return GetPropertyAtIndexAs<llvm::StringRef>(ePropertyPortArg, {});
59 }
60};
61
62} // namespace
63
64static PluginProperties &GetGlobalProperties() {
65 static PluginProperties g_settings;
66 return g_settings;
67}
68
70 return "Platform for debugging Wasm";
71}
72
78
82
87 debugger, GetGlobalProperties().GetValueProperties(),
88 "Properties for the wasm platform plugin.",
89 /*is_global_property=*/true);
90 }
91}
92
95 LLDB_LOG(log, "force = {0}, arch = ({1}, {2})", force,
96 arch ? arch->GetArchitectureName() : "<null>",
97 arch ? arch->GetTriple().getTriple() : "<null>");
98
99 bool create = force;
100 if (!create && arch && arch->IsValid()) {
101 const llvm::Triple &triple = arch->GetTriple();
102 switch (triple.getArch()) {
103 case llvm::Triple::wasm32:
104 case llvm::Triple::wasm64:
105 create = true;
106 break;
107 default:
108 break;
109 }
110 }
111
112 LLDB_LOG(log, "create = {0}", create);
113 return create ? PlatformSP(new PlatformWasm()) : PlatformSP();
114}
115
116std::vector<ArchSpec>
118 return {ArchSpec("wasm32-unknown-unknown-wasm"),
119 ArchSpec("wasm64-unknown-unknown-wasm")};
120}
121
122static auto get_arg_range(const Args &args) {
123 return llvm::make_range(args.GetArgumentArrayRef().begin(),
124 args.GetArgumentArrayRef().end());
125}
126
128 Debugger &debugger, Target *target,
129 Status &status) {
131 return m_remote_platform_sp->Attach(attach_info, debugger, target, status);
132
134 "attaching is only supported when connected to a remote Wasm platform");
135 return nullptr;
136}
137
139 Debugger &debugger, Target &target,
140 Status &error) {
142 return m_remote_platform_sp->DebugProcess(launch_info, debugger, target,
143 error);
144
146
147 const PluginProperties &properties = GetGlobalProperties();
148
149 FileSpec runtime = properties.GetRuntimePath();
151
152 if (!FileSystem::Instance().Exists(runtime)) {
154 "WebAssembly runtime does not exist: {0}", runtime.GetPath());
155 return nullptr;
156 }
157
158 uint16_t port = 0;
159 {
160 // Get the next available port by binding a socket to port 0.
161 TCPSocket listen_socket(true);
162 error = listen_socket.Listen("localhost:0", /*backlog=*/5);
163 if (error.Fail())
164 return nullptr;
165 port = listen_socket.GetLocalPortNumber();
166 }
167
168 if (error.Fail())
169 return nullptr;
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 launch_info.GetFlags().Clear(eLaunchFlagDebug);
179
180 auto exit_code = std::make_shared<std::optional<int>>();
181 launch_info.SetMonitorProcessCallback(
182 [=](lldb::pid_t pid, int signal, int status) {
183 LLDB_LOG(
184 log,
185 "WebAssembly runtime exited: pid = {0}, signal = {1}, status = {2}",
186 pid, signal, status);
187 exit_code->emplace(status);
188 });
189
190 // This is automatically done for host platform in
191 // Target::FinalizeFileActions, but we're not a host platform.
192 llvm::Error Err = launch_info.SetUpPtyRedirection();
193 LLDB_LOG_ERROR(log, std::move(Err), "SetUpPtyRedirection failed: {0}");
194
195 LLDB_LOG(log, "{0}", get_arg_range(launch_info.GetArguments()));
196 error = Host::LaunchProcess(launch_info);
197 if (error.Fail())
198 return nullptr;
199
200 ProcessSP process_sp = target.CreateProcess(
202 nullptr, true);
203 if (!process_sp) {
204 error = Status::FromErrorString("failed to create WebAssembly process");
205 return nullptr;
206 }
207
208 process_sp->HijackProcessEvents(launch_info.GetHijackListener());
209
210 error = process_sp->ConnectRemote(
211 llvm::formatv("connect://localhost:{0}", port).str());
212 if (error.Fail()) {
213 // If we know the runtime has exited, that's a better error message than
214 // failing to connect.
215 if (*exit_code)
216 error = Status::FromError(llvm::joinErrors(
217 llvm::createStringErrorV(
218 "WebAssembly runtime exited with exit code {0}", **exit_code),
219 error.takeError()));
220
221 return nullptr;
222 }
223#ifndef _WIN32
224 if (launch_info.GetPTY().GetPrimaryFileDescriptor() !=
226 process_sp->SetSTDIOFileDescriptor(
227 launch_info.GetPTY().ReleasePrimaryFileDescriptor());
228#endif
229 return process_sp;
230}
231
233 if (IsHost())
235 "can't connect to the host platform, always connected");
236
239
240 return m_remote_platform_sp->ConnectRemote(args);
241}
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:369
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:392
static auto get_arg_range(const Args &args)
static PluginProperties & GetGlobalProperties()
#define LLDB_PLUGIN_DEFINE(PluginName)
An architecture specification class.
Definition ArchSpec.h:31
bool IsValid() const
Tests if this ArchSpec is valid.
Definition ArchSpec.h:366
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:468
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
llvm::ArrayRef< const char * > GetArgumentArrayRef() const
Gets the argument as an ArrayRef.
Definition Args.h:173
void AppendArguments(const Args &rhs)
Definition Args.cpp:307
A class to manage flag bits.
Definition Debugger.h:87
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 lldb::PlatformSP CreateInstance(bool force, const ArchSpec *arch)
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::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:503
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
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
static Status FromErrorString(const char *str)
Definition Status.h:141
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:137
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:300
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:332
std::shared_ptr< lldb_private::Platform > PlatformSP
std::shared_ptr< lldb_private::Process > ProcessSP
uint64_t pid_t
Definition lldb-types.h:83