================
@@ -181,34 +192,63 @@ ProcessWasm::GetWasmCallStack(lldb::tid_t tid) {
 }
 
 llvm::Expected<lldb::DataBufferSP>
-ProcessWasm::GetWasmVariable(WasmVirtualRegisterKinds kind, int frame_index,
-                             int index) {
-  StreamString packet;
-  switch (kind) {
-  case eWasmTagLocal:
-    packet.Printf("qWasmLocal:");
-    break;
-  case eWasmTagGlobal:
-    packet.Printf("qWasmGlobal:");
-    break;
-  case eWasmTagOperandStack:
-    packet.PutCString("qWasmStackValue:");
-    break;
-  case eWasmTagNotAWasmLocation:
-    return llvm::createStringError("not a Wasm location");
-  }
-  packet.Printf("%d;%d", frame_index, index);
-
+ProcessWasm::SendWasmValueQuery(llvm::StringRef packet) {
   StringExtractorGDBRemote response;
-  if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString(), response) !=
+  if (m_gdb_comm.SendPacketAndWaitForResponse(packet, response) !=
       GDBRemoteCommunication::PacketResult::Success)
-    return llvm::createStringError("failed to send Wasm variable");
+    return llvm::createStringErrorV("failed to send {0}", packet);
 
   if (!response.IsNormalResponse())
-    return llvm::createStringError("failed to get response for Wasm variable");
+    return llvm::createStringErrorV("failed to get response for {0}", packet);
 
   WritableDataBufferSP buffer_sp(
       new DataBufferHeap(response.GetStringRef().size() / 2, 0));
   response.GetHexBytes(buffer_sp->GetData(), '\xcc');
   return buffer_sp;
 }
+
+llvm::Expected<lldb::DataBufferSP>
+ProcessWasm::GetWasmVariable(WasmVirtualRegisterKinds kind,
+                             uint32_t frame_index, uint32_t index) {
+  switch (kind) {
+  case eWasmTagLocal:
+    return SendWasmValueQuery(
+        llvm::formatv("qWasmLocal:{0};{1}", frame_index, index).str());
+  case eWasmTagOperandStack:
+    return SendWasmValueQuery(
+        llvm::formatv("qWasmStackValue:{0};{1}", frame_index, index).str());
+  case eWasmTagGlobal:
+    // A global belongs to a module rather than a frame. See GetWasmGlobal.
+    return llvm::createStringError("a Wasm global does not belong to a frame");
+  case eWasmTagNotAWasmLocation:
+    return llvm::createStringError("not a Wasm location");
+  }
+  llvm_unreachable("unhandled Wasm virtual register kind");
+}
+
+llvm::Expected<lldb::DataBufferSP>
+ProcessWasm::GetWasmGlobal(uint32_t module_id, uint32_t index,
+                           uint32_t frame_index) {
+  // A module instance is named as an address space of the process, which is
+  // what the module id identifies.
+  if (CanQueryInstance(module_id))
+    return SendWasmValueQuery(
+        llvm::formatv("qWasmGlobal:{0};address_space:{1};", index, module_id)
+            .str());
+
+  // A frame stands in for the module it is executing only where the stub 
cannot
+  // be told which instance to read.
+  if (frame_index != LLDB_INVALID_INDEX32)
+    return SendWasmValueQuery(
+        llvm::formatv("qWasmGlobal:{0};{1}", frame_index, index).str());
+
+  return llvm::createStringErrorV(
+      "the Wasm stub can only read a global through a frame, and no frame is "
----------------
jasonmolenda wrote:

This is no longer true.

https://github.com/llvm/llvm-project/pull/213176
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to