LLDB mainline
ProcessWasm.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
9#include "ProcessWasm.h"
10#include "ThreadWasm.h"
11#include "lldb/Core/Module.h"
13#include "lldb/Core/Value.h"
15
17
18using namespace lldb;
19using namespace lldb_private;
21using namespace lldb_private::wasm;
22
24
26 : ProcessGDBRemote(target_sp, listener_sp) {
27 assert(target_sp);
28 // Wasm doesn't have any Unix-like signals as a platform concept, but pretend
29 // like it does to appease LLDB.
30 m_unix_signals_sp = UnixSignals::Create(target_sp->GetArchitecture());
31 // FIXME: LLVM's RuntimeDyld doesn't support the Wasm object format, so we
32 // can't JIT expressions for this target.
33 SetCanJIT(false);
34}
35
41
45
46llvm::StringRef ProcessWasm::GetPluginName() { return GetPluginNameStatic(); }
47
48llvm::StringRef ProcessWasm::GetPluginNameStatic() { return "wasm"; }
49
51 return "GDB Remote protocol based WebAssembly debugging plug-in.";
52}
53
57
59 ListenerSP listener_sp,
60 const FileSpec *crash_file_path,
61 bool can_connect) {
62 if (crash_file_path == nullptr)
63 return std::make_shared<ProcessWasm>(target_sp, listener_sp);
64 return {};
65}
66
68 bool plugin_specified_by_name) {
69 if (plugin_specified_by_name)
70 return true;
71
72 if (Module *exe_module = target_sp->GetExecutableModulePointer()) {
73 if (ObjectFile *exe_objfile = exe_module->GetObjectFile())
74 return exe_objfile->GetArchitecture().GetMachine() ==
75 llvm::Triple::wasm32;
76 }
77
78 // However, if there is no wasm module, we return false, otherwise,
79 // we might use ProcessWasm to attach gdb remote.
80 return false;
81}
82
83std::shared_ptr<ThreadGDBRemote> ProcessWasm::CreateThread(lldb::tid_t tid) {
84 return std::make_shared<ThreadWasm>(*this, tid);
85}
86
87size_t ProcessWasm::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
88 Status &error) {
89 wasm_addr_t wasm_addr(vm_addr);
90
91 switch (wasm_addr.GetType()) {
94 return ProcessGDBRemote::ReadMemory(vm_addr, buf, size, error);
96 break;
97 }
98
99 error.FromErrorStringWithFormatv(
100 "Wasm read failed for invalid address {0:x} (type = {1:x}, module = "
101 "{2:x}, offset = {3:x})",
102 vm_addr, wasm_addr.GetType(), wasm_addr.GetModuleID(),
103 wasm_addr.GetOffset());
104 return 0;
105}
106
107llvm::Expected<std::vector<lldb::addr_t>>
109 StreamString packet;
110 packet.Printf("qWasmCallStack:");
111 packet.Printf("%" PRIx64, tid);
112
114 if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString(), response) !=
116 return llvm::createStringError("failed to send qWasmCallStack");
117
118 if (!response.IsNormalResponse())
119 return llvm::createStringError("failed to get response for qWasmCallStack");
120
121 WritableDataBufferSP data_buffer_sp =
122 std::make_shared<DataBufferHeap>(response.GetStringRef().size() / 2, 0);
123 const size_t bytes = response.GetHexBytes(data_buffer_sp->GetData(), '\xcc');
124 if (bytes == 0 || bytes % sizeof(uint64_t) != 0)
125 return llvm::createStringError("invalid response for qWasmCallStack");
126
127 // To match the Wasm specification, the addresses are encoded in little endian
128 // byte order.
129 DataExtractor data(data_buffer_sp, lldb::eByteOrderLittle,
131 lldb::offset_t offset = 0;
132 std::vector<lldb::addr_t> call_stack_pcs;
133 while (offset < bytes)
134 call_stack_pcs.push_back(data.GetU64(&offset));
135
136 return call_stack_pcs;
137}
138
139llvm::Expected<lldb::DataBufferSP>
141 int index) {
142 StreamString packet;
143 switch (kind) {
144 case eWasmTagLocal:
145 packet.Printf("qWasmLocal:");
146 break;
147 case eWasmTagGlobal:
148 packet.Printf("qWasmGlobal:");
149 break;
151 packet.PutCString("qWasmStackValue:");
152 break;
154 return llvm::createStringError("not a Wasm location");
155 }
156 packet.Printf("%d;%d", frame_index, index);
157
159 if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString(), response) !=
161 return llvm::createStringError("failed to send Wasm variable");
162
163 if (!response.IsNormalResponse())
164 return llvm::createStringError("failed to get response for Wasm variable");
165
166 WritableDataBufferSP buffer_sp(
167 new DataBufferHeap(response.GetStringRef().size() / 2, 0));
168 response.GetHexBytes(buffer_sp->GetData(), '\xcc');
169 return buffer_sp;
170}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_PLUGIN_DEFINE(PluginName)
size_t GetHexBytes(llvm::MutableArrayRef< uint8_t > dest, uint8_t fail_fill_value)
llvm::StringRef GetStringRef() const
A subclass of DataBuffer that stores a data buffer on the heap.
An data extractor class.
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
A file utility class.
Definition FileSpec.h:57
A class that describes an executable image and its associated object and symbol files.
Definition Module.h:90
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
void SetCanJIT(bool can_jit)
Sets whether executing JIT-compiled code in this process is possible.
Definition Process.cpp:2611
virtual size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
Definition Process.cpp:1911
lldb::UnixSignalsSP m_unix_signals_sp
Definition Process.h:3456
uint32_t GetAddressByteSize() const
Definition Process.cpp:3774
friend class Debugger
Definition Process.h:357
An error handling class.
Definition Status.h:118
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:132
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
static lldb::UnixSignalsSP Create(const ArchSpec &arch)
ProcessGDBRemote(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp)
ProcessWasm provides the access to the Wasm program state retrieved from the Wasm engine.
Definition ProcessWasm.h:49
llvm::StringRef GetPluginName() override
std::shared_ptr< process_gdb_remote::ThreadGDBRemote > CreateThread(lldb::tid_t tid) override
static llvm::StringRef GetPluginNameStatic()
bool CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) override
Check if a plug-in instance can debug the file in module.
static void DebuggerInitialize(Debugger &debugger)
static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const FileSpec *crash_file_path, bool can_connect)
llvm::Expected< std::vector< lldb::addr_t > > GetWasmCallStack(lldb::tid_t tid)
Retrieve the current call stack from the WebAssembly remote process.
ProcessWasm(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp)
llvm::Expected< lldb::DataBufferSP > GetWasmVariable(WasmVirtualRegisterKinds kind, int frame_index, int index)
Query the value of a WebAssembly variable from the WebAssembly remote process.
static llvm::StringRef GetPluginDescriptionStatic()
size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, Status &error) override
Read of memory from a process.
A class that represents a running process on the host machine.
uint64_t offset_t
Definition lldb-types.h:85
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::Listener > ListenerSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP
uint64_t tid_t
Definition lldb-types.h:84
For the purpose of debugging, we can represent all these separated 32-bit address spaces with a singl...
Definition ProcessWasm.h:26
WasmAddressType GetType() const
Definition ProcessWasm.h:38