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 "RegisterContextWasm.h"
13#include "ThreadWasm.h"
15#include "lldb/Utility/Log.h"
16
17using namespace lldb;
18using namespace lldb_private;
19using namespace process_gdb_remote;
20using namespace wasm;
21
22/// Engine frames have no address, so their CFAs are in a space without memory.
24 uint64_t(WasmAddressType::Invalid) << kWasmAddressTypeShift;
25
28 const uint32_t concrete_frame_idx = frame->GetConcreteFrameIndex();
29 if (m_frames.size() <= concrete_frame_idx)
31
32 ThreadSP thread = frame->GetThread();
33 ThreadGDBRemote *gdb_thread = static_cast<ThreadGDBRemote *>(thread.get());
34 ProcessWasm *wasm_process =
35 static_cast<ProcessWasm *>(thread->GetProcess().get());
36
37 return std::make_shared<RegisterContextWasm>(*gdb_thread, concrete_frame_idx,
38 wasm_process->GetRegisterInfo());
39}
40
43 return GetVisibleFrameCount();
44
45 m_unwind_complete = true;
46 m_frames.clear();
47
48 ThreadWasm &wasm_thread = static_cast<ThreadWasm &>(GetThread());
49 llvm::Expected<std::vector<lldb::addr_t>> call_stack_pcs =
50 wasm_thread.GetWasmCallStack();
51 if (!call_stack_pcs) {
52 LLDB_LOG_ERROR(GetLog(LLDBLog::Unwind), call_stack_pcs.takeError(),
53 "Failed to get Wasm callstack: {0}");
54 m_frames.clear();
55 return 0;
56 }
57
58 m_frames = *call_stack_pcs;
59 return GetVisibleFrameCount();
60}
61
63 // A backtrace goes no deeper than the target asks for, which is what bounds
64 // the walk of a stack that recurses without end. The depth bounds what a
65 // caller is told rather than what is kept, because it can be raised after a
66 // stack has been fetched.
67 return std::min<uint64_t>(m_frames.size(),
68 GetThread().GetMaxBacktraceDepth());
69}
70
71bool UnwindWasm::DoGetFrameInfoAtIndex(uint32_t frame_idx, lldb::addr_t &cfa,
73 bool &behaves_like_zeroth_frame) {
74 if (m_frames.size() == 0)
76
77 if (frame_idx >= GetVisibleFrameCount())
78 return false;
79
80 behaves_like_zeroth_frame = (frame_idx == 0);
81 // The distance to the outermost frame does not change as frames are pushed
82 // and popped above it.
83 cfa = kWasmSyntheticCFABase + m_frames.size() - 1 - frame_idx;
84 pc = m_frames[frame_idx];
85 return true;
86}
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:405
static constexpr lldb::addr_t kWasmSyntheticCFABase
Engine frames have no address, so their CFAs are in a space without memory.
This base class provides an interface to stack frames.
Definition StackFrame.h:44
lldb::ThreadSP GetThread() const
Definition StackFrame.h:135
virtual uint32_t GetConcreteFrameIndex()
Query this frame to find what frame it is in this Thread's StackFrameList, not counting inlined frame...
Definition StackFrame.h:492
Thread & GetThread()
Definition Unwind.h:61
ProcessWasm provides the access to the Wasm program state retrieved from the Wasm engine.
Definition ProcessWasm.h:21
lldb::DynamicRegisterInfoSP & GetRegisterInfo()
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.
uint32_t GetVisibleFrameCount()
The number of fetched frames a caller is told about, which the target's maximum backtrace depth bound...
std::vector< lldb::addr_t > m_frames
Definition UnwindWasm.h:45
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
static constexpr uint32_t kWasmAddressTypeShift
Definition WasmAddress.h:47
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:338
std::shared_ptr< lldb_private::Thread > ThreadSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP