100 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLDB_SOURCE_PLUGINS_PROCESS_WASM_PROCESSWASM_H10#define LLDB_SOURCE_PLUGINS_PROCESS_WASM_PROCESSWASM_H11 12#include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"13#include "Utility/WasmVirtualRegisters.h"14 15namespace lldb_private {16namespace wasm {17 18/// Each WebAssembly module has separated address spaces for Code and Memory.19/// A WebAssembly module also has a Data section which, when the module is20/// loaded, gets mapped into a region in the module Memory.21enum WasmAddressType : uint8_t { Memory = 0x00, Object = 0x01, Invalid = 0xff };22 23/// For the purpose of debugging, we can represent all these separated 32-bit24/// address spaces with a single virtual 64-bit address space. The25/// wasm_addr_t provides this encoding using bitfields.26struct wasm_addr_t {27 uint64_t offset : 32;28 uint64_t module_id : 30;29 uint64_t type : 2;30 31 wasm_addr_t(lldb::addr_t addr)32 : offset(addr & 0x00000000ffffffff),33 module_id((addr & 0x00ffffff00000000) >> 32), type(addr >> 62) {}34 35 wasm_addr_t(WasmAddressType type, uint32_t module_id, uint32_t offset)36 : offset(offset), module_id(module_id), type(type) {}37 38 WasmAddressType GetType() { return static_cast<WasmAddressType>(type); }39 40 operator lldb::addr_t() { return *(uint64_t *)this; }41};42 43static_assert(sizeof(wasm_addr_t) == 8, "");44 45/// ProcessWasm provides the access to the Wasm program state46/// retrieved from the Wasm engine.47class ProcessWasm : public process_gdb_remote::ProcessGDBRemote {48public:49 ProcessWasm(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp);50 ~ProcessWasm() override = default;51 52 static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp,53 lldb::ListenerSP listener_sp,54 const FileSpec *crash_file_path,55 bool can_connect);56 57 static void Initialize();58 static void DebuggerInitialize(Debugger &debugger);59 static void Terminate();60 61 static llvm::StringRef GetPluginNameStatic();62 static llvm::StringRef GetPluginDescriptionStatic();63 64 llvm::StringRef GetPluginName() override;65 66 size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,67 Status &error) override;68 69 bool CanDebug(lldb::TargetSP target_sp,70 bool plugin_specified_by_name) override;71 72 /// Retrieve the current call stack from the WebAssembly remote process.73 llvm::Expected<std::vector<lldb::addr_t>> GetWasmCallStack(lldb::tid_t tid);74 75 /// Query the value of a WebAssembly variable from the WebAssembly76 /// remote process.77 llvm::Expected<lldb::DataBufferSP>78 GetWasmVariable(WasmVirtualRegisterKinds kind, int frame_index, int index);79 80protected:81 std::shared_ptr<process_gdb_remote::ThreadGDBRemote>82 CreateThread(lldb::tid_t tid) override;83 84private:85 friend class UnwindWasm;86 friend class ThreadWasm;87 88 process_gdb_remote::GDBRemoteDynamicRegisterInfoSP &GetRegisterInfo() {89 return m_register_info_sp;90 }91 92 ProcessWasm(const ProcessWasm &);93 const ProcessWasm &operator=(const ProcessWasm &) = delete;94};95 96} // namespace wasm97} // namespace lldb_private98 99#endif100