LLDB mainline
UnwindWasm.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 "UnwindWasm.h"
11#include "ProcessWasm.h"
12#include "ThreadWasm.h"
14#include "lldb/Utility/Log.h"
15
16using namespace lldb;
17using namespace lldb_private;
18using namespace process_gdb_remote;
19using namespace wasm;
20
22public:
24 uint32_t concrete_frame_idx,
26 uint64_t pc)
27 : GDBRemoteRegisterContext(thread, concrete_frame_idx, reg_info_sp, false,
28 false) {
29 // Wasm does not have a fixed set of registers but relies on a mechanism
30 // named local and global variables to store information such as the stack
31 // pointer. The only actual register is the PC.
33 }
34};
35
38 if (m_frames.size() <= frame->GetFrameIndex())
40
41 ThreadSP thread = frame->GetThread();
42 ThreadGDBRemote *gdb_thread = static_cast<ThreadGDBRemote *>(thread.get());
43 ProcessWasm *wasm_process =
44 static_cast<ProcessWasm *>(thread->GetProcess().get());
45
46 return std::make_shared<WasmGDBRemoteRegisterContext>(
47 *gdb_thread, frame->GetConcreteFrameIndex(),
48 wasm_process->GetRegisterInfo(), m_frames[frame->GetFrameIndex()]);
49}
50
53 return m_frames.size();
54
55 m_unwind_complete = true;
56 m_frames.clear();
57
58 ThreadWasm &wasm_thread = static_cast<ThreadWasm &>(GetThread());
59 llvm::Expected<std::vector<lldb::addr_t>> call_stack_pcs =
60 wasm_thread.GetWasmCallStack();
61 if (!call_stack_pcs) {
62 LLDB_LOG_ERROR(GetLog(LLDBLog::Unwind), call_stack_pcs.takeError(),
63 "Failed to get Wasm callstack: {0}");
64 m_frames.clear();
65 return 0;
66 }
67
68 m_frames = *call_stack_pcs;
69 return m_frames.size();
70}
71
72bool UnwindWasm::DoGetFrameInfoAtIndex(uint32_t frame_idx, lldb::addr_t &cfa,
74 bool &behaves_like_zeroth_frame) {
75 if (m_frames.size() == 0)
77
78 if (frame_idx >= m_frames.size())
79 return false;
80
81 behaves_like_zeroth_frame = (frame_idx == 0);
82 cfa = 0;
83 pc = m_frames[frame_idx];
84 return true;
85}
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:392
WasmGDBRemoteRegisterContext(ThreadGDBRemote &thread, uint32_t concrete_frame_idx, GDBRemoteDynamicRegisterInfoSP &reg_info_sp, uint64_t pc)
This base class provides an interface to stack frames.
Definition StackFrame.h:44
lldb::ThreadSP GetThread() const
Definition StackFrame.h:128
uint32_t GetConcreteFrameIndex() const
Query this frame to find what frame it is in this Thread's StackFrameList, not counting inlined frame...
Definition StackFrame.h:455
uint32_t GetFrameIndex() const
Query this frame to find what frame it is in this Thread's StackFrameList.
Thread & GetThread()
Definition Unwind.h:61
GDBRemoteRegisterContext(ThreadGDBRemote &thread, uint32_t concrete_frame_idx, GDBRemoteDynamicRegisterInfoSP reg_info_sp, bool read_all_at_once, bool write_all_at_once)
bool PrivateSetRegisterValue(uint32_t reg, llvm::ArrayRef< uint8_t > data)
ProcessWasm provides the access to the Wasm program state retrieved from the Wasm engine.
Definition ProcessWasm.h:47
process_gdb_remote::GDBRemoteDynamicRegisterInfoSP & GetRegisterInfo()
Definition ProcessWasm.h:88
ProcessWasm provides the access to the Wasm program state retrieved from the Wasm engine.
Definition ThreadWasm.h:19
llvm::Expected< std::vector< lldb::addr_t > > GetWasmCallStack()
Retrieve the current call stack from the WebAssembly remote process.
std::vector< lldb::addr_t > m_frames
Definition UnwindWasm.h:41
lldb::RegisterContextSP DoCreateRegisterContextForFrame(lldb_private::StackFrame *frame) override
bool DoGetFrameInfoAtIndex(uint32_t frame_idx, lldb::addr_t &cfa, lldb::addr_t &pc, bool &behaves_like_zeroth_frame) override
uint32_t DoGetFrameCount() override
std::shared_ptr< GDBRemoteDynamicRegisterInfo > GDBRemoteDynamicRegisterInfoSP
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::Thread > ThreadSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP