Skip to content

Commit 4fdb643

Browse files
authored
Merge pull request #11142 from swiftlang/stable-21-wasm
🍒 Cherry-pick WebAssembly debugging changes
2 parents 31e1d9b + f29d90e commit 4fdb643

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1871
-279
lines changed

lldb/docs/resources/lldbgdbremote.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2463,3 +2463,70 @@ omitting them will work fine; these numbers are always base 16.
24632463
24642464
The length of the payload is not provided. A reliable, 8-bit clean,
24652465
transport layer is assumed.
2466+
2467+
## Wasm Packets
2468+
2469+
The packet below are supported by the
2470+
[WAMR](https://github.com/bytecodealliance/wasm-micro-runtime) and
2471+
[V8](https://v8.dev) Wasm runtimes.
2472+
2473+
2474+
### qWasmCallStack
2475+
2476+
Get the Wasm call stack for the given thread id. This returns a hex-encoded
2477+
list of PC values, one for each frame of the call stack. To match the Wasm
2478+
specification, the addresses are encoded in little endian byte order, even if
2479+
the endian of the Wasm runtime's host is not little endian.
2480+
2481+
```
2482+
send packet: $qWasmCallStack:202dbe040#08
2483+
read packet: $9c01000000000040e501000000000040fe01000000000040#
2484+
```
2485+
2486+
**Priority to Implement:** Only required for Wasm support. Necessary to show
2487+
stack traces.
2488+
2489+
### qWasmGlobal
2490+
2491+
Get the value of a Wasm global variable for the given frame index at the given
2492+
variable index. The indexes are encoded as base 10. The result is a hex-encoded
2493+
address from where to read the value.
2494+
2495+
```
2496+
send packet: $qWasmGlobal:0;2#cb
2497+
read packet: $e0030100#b9
2498+
```
2499+
2500+
**Priority to Implement:** Only required for Wasm support. Necessary to show
2501+
variables.
2502+
2503+
2504+
### qWasmLocal
2505+
2506+
Get the value of a Wasm function argument or local variable for the given frame
2507+
index at the given variable index. The indexes are encoded as base 10. The
2508+
result is a hex-encoded address from where to read the value.
2509+
2510+
2511+
```
2512+
send packet: $qWasmLocal:0;2#cb
2513+
read packet: $e0030100#b9
2514+
```
2515+
2516+
**Priority to Implement:** Only required for Wasm support. Necessary to show
2517+
variables.
2518+
2519+
2520+
### qWasmStackValue
2521+
2522+
Get the value of a Wasm local variable from the Wasm operand stack, for the
2523+
given frame index at the given variable index. The indexes are encoded as base
2524+
10. The result is a hex-encoded address from where to read value.
2525+
2526+
```
2527+
send packet: $qWasmStackValue:0;2#cb
2528+
read packet: $e0030100#b9
2529+
```
2530+
2531+
**Priority to Implement:** Only required for Wasm support. Necessary to show
2532+
variables.

lldb/include/lldb/Core/dwarf.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414
// Get the DWARF constant definitions from llvm
1515
#include "llvm/BinaryFormat/Dwarf.h"
1616

17-
namespace lldb_private {
18-
namespace dwarf {
19-
using namespace llvm::dwarf;
20-
}
21-
}
22-
2317
typedef llvm::dwarf::Attribute dw_attr_t;
2418
typedef llvm::dwarf::Form dw_form_t;
2519
typedef llvm::dwarf::Tag dw_tag_t;

lldb/include/lldb/Expression/DWARFExpression.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ class DWARFExpression {
5252
GetVendorDWARFOpcodeSize(const DataExtractor &data,
5353
const lldb::offset_t data_offset,
5454
const uint8_t op) const = 0;
55-
virtual bool ParseVendorDWARFOpcode(uint8_t op,
56-
const DataExtractor &opcodes,
57-
lldb::offset_t &offset,
58-
Stack &stack) const = 0;
55+
virtual bool
56+
ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes,
57+
lldb::offset_t &offset, RegisterContext *reg_ctx,
58+
lldb::RegisterKind reg_kind, Stack &stack) const = 0;
5959

6060
Delegate(const Delegate &) = delete;
6161
Delegate &operator=(const Delegate &) = delete;
@@ -163,6 +163,10 @@ class DWARFExpression {
163163

164164
bool MatchesOperand(StackFrame &frame, const Instruction::Operand &op) const;
165165

166+
static llvm::Error ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
167+
lldb::RegisterKind reg_kind,
168+
uint32_t reg_num, Value &value);
169+
166170
private:
167171
/// A data extractor capable of reading opcode bytes
168172
DataExtractor m_data;

lldb/packages/Python/lldbsuite/test/lldbgdbclient.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def createTarget(self, yaml_path):
4545
self.yaml2obj(yaml_path, obj_path)
4646
return self.dbg.CreateTarget(obj_path)
4747

48-
def connect(self, target):
48+
def connect(self, target, plugin="gdb-remote"):
4949
"""
5050
Create a process by connecting to the mock GDB server.
5151
@@ -54,7 +54,7 @@ def connect(self, target):
5454
listener = self.dbg.GetListener()
5555
error = lldb.SBError()
5656
process = target.ConnectRemote(
57-
listener, self.server.get_connect_url(), "gdb-remote", error
57+
listener, self.server.get_connect_url(), plugin, error
5858
)
5959
self.assertTrue(error.Success(), error.description)
6060
self.assertTrue(process, PROCESS_IS_VALID)

lldb/source/Expression/DWARFExpression.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545

4646
using namespace lldb;
4747
using namespace lldb_private;
48-
using namespace lldb_private::dwarf;
4948
using namespace lldb_private::plugin::dwarf;
49+
using namespace llvm::dwarf;
5050

5151
// DWARFExpression constructor
5252
DWARFExpression::DWARFExpression() : m_data() {}
@@ -95,9 +95,10 @@ void DWARFExpression::SetRegisterKind(RegisterKind reg_kind) {
9595
m_reg_kind = reg_kind;
9696
}
9797

98-
static llvm::Error ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
99-
lldb::RegisterKind reg_kind,
100-
uint32_t reg_num, Value &value) {
98+
llvm::Error
99+
DWARFExpression::ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
100+
lldb::RegisterKind reg_kind,
101+
uint32_t reg_num, Value &value) {
101102
if (reg_ctx == nullptr)
102103
return llvm::createStringError("no register context in frame");
103104

@@ -2386,7 +2387,8 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
23862387

23872388
default:
23882389
if (dwarf_cu) {
2389-
if (dwarf_cu->ParseVendorDWARFOpcode(op, opcodes, offset, stack)) {
2390+
if (dwarf_cu->ParseVendorDWARFOpcode(op, opcodes, offset, reg_ctx,
2391+
reg_kind, stack)) {
23902392
break;
23912393
}
23922394
}

lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,13 @@ DataExtractor ObjectFileWasm::ReadImageData(offset_t offset, uint32_t size) {
376376
DataBufferSP buffer_sp(data_up.release());
377377
data.SetData(buffer_sp, 0, buffer_sp->GetByteSize());
378378
}
379+
} else if (offset < m_data.GetByteSize()) {
380+
size =
381+
std::min(static_cast<uint64_t>(size), m_data.GetByteSize() - offset);
382+
return DataExtractor(m_data.GetDataStart() + offset, size, GetByteOrder(),
383+
GetAddressByteSize());
379384
}
380385
}
381-
382386
data.SetByteOrder(GetByteOrder());
383387
return data;
384388
}

lldb/source/Plugins/Process/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ add_subdirectory(elf-core)
2929
add_subdirectory(mach-core)
3030
add_subdirectory(minidump)
3131
add_subdirectory(FreeBSDKernel)
32+
add_subdirectory(wasm)

lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ ProcessGDBRemote::~ProcessGDBRemote() {
323323
KillDebugserverProcess();
324324
}
325325

326+
std::shared_ptr<ThreadGDBRemote>
327+
ProcessGDBRemote::CreateThread(lldb::tid_t tid) {
328+
return std::make_shared<ThreadGDBRemote>(*this, tid);
329+
}
330+
326331
bool ProcessGDBRemote::ParsePythonTargetDefinition(
327332
const FileSpec &target_definition_fspec) {
328333
ScriptInterpreter *interpreter =
@@ -1594,7 +1599,7 @@ bool ProcessGDBRemote::DoUpdateThreadList(ThreadList &old_thread_list,
15941599
ThreadSP thread_sp(
15951600
old_thread_list_copy.RemoveThreadByProtocolID(tid, false));
15961601
if (!thread_sp) {
1597-
thread_sp = std::make_shared<ThreadGDBRemote>(*this, tid);
1602+
thread_sp = CreateThread(tid);
15981603
LLDB_LOGV(log, "Making new thread: {0} for thread ID: {1:x}.",
15991604
thread_sp.get(), thread_sp->GetID());
16001605
} else {
@@ -1726,7 +1731,7 @@ ThreadSP ProcessGDBRemote::SetThreadStopInfo(
17261731

17271732
if (!thread_sp) {
17281733
// Create the thread if we need to
1729-
thread_sp = std::make_shared<ThreadGDBRemote>(*this, tid);
1734+
thread_sp = CreateThread(tid);
17301735
m_thread_list_real.AddThread(thread_sp);
17311736
}
17321737
}

lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ class ProcessGDBRemote : public Process,
246246

247247
ProcessGDBRemote(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp);
248248

249+
virtual std::shared_ptr<ThreadGDBRemote> CreateThread(lldb::tid_t tid);
250+
249251
bool SupportsMemoryTagging() override;
250252

251253
/// Broadcaster event bits definitions.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_lldb_library(lldbPluginProcessWasm PLUGIN
2+
ProcessWasm.cpp
3+
RegisterContextWasm.cpp
4+
ThreadWasm.cpp
5+
UnwindWasm.cpp
6+
7+
LINK_LIBS
8+
lldbCore
9+
LINK_COMPONENTS
10+
Support
11+
)

0 commit comments

Comments
 (0)