Skip to content

Commit

Permalink
[swift] Fix EvaluateExpression to use the same process instance for t…
Browse files Browse the repository at this point in the history
…he same stop_id
  • Loading branch information
kateinoigakukun committed Feb 25, 2025
1 parent f81ac99 commit 6c5e884
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 13 deletions.
37 changes: 35 additions & 2 deletions extensions/cxx_debugging/lib/ApiContext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Expressions.h"
#include "Variables.h"
#include "WasmModule.h"
#include "WasmVendorPlugins.h"
#include "api.h"

#include "lldb/API/SBAddress.h"
Expand Down Expand Up @@ -628,9 +629,31 @@ class SBValueArena {

static SBValueArena arena;

llvm::Expected<lldb::ProcessSP> ApiContext::GetProcess(
std::string stop_id,
std::shared_ptr<WasmModule> module,
const api::DebuggerProxy& proxy,
size_t frame_offset) {
lldb::ProcessSP process;
if (last_process_ && last_process_->process &&
last_process_->stop_id == stop_id) {
process = last_process_->process;
} else {
auto maybe_process = ::symbols_backend::CreateProcess(*module);
if (!maybe_process) {
return maybe_process.takeError();
}
process = maybe_process.get();
last_process_ = LastProcessInfo{process, stop_id};
}
static_cast<WasmProcess*>(process.get())
->SetProxyAndFrameOffset(proxy, frame_offset);
return process;
}

api::EvaluateExpressionResponse ApiContext::EvaluateExpression(
RawLocation location,
std::string stop_id,
std::string expression,
emscripten::val debug_proxy) {
std::string raw_module_id = location.GetRawModuleId();
Expand All @@ -640,8 +663,7 @@ api::EvaluateExpressionResponse ApiContext::EvaluateExpression(
MakeNotFoundError(raw_module_id));
}
DebuggerProxy proxy{debug_proxy};
auto process = ::symbols_backend::CreateProcess(*module, proxy,
location.GetCodeOffset());
auto process = GetProcess(stop_id, module, proxy, location.GetCodeOffset());
if (!process) {
return api::EvaluateExpressionResponse().SetError(
MakeError(Error::Code::kEvalError, process.takeError()));
Expand Down Expand Up @@ -672,6 +694,12 @@ api::GetValueSummaryResponse ApiContext::GetValueSummary(api::Sbvalue rawValue)
.SetError(MakeError(Error::Code::kEvalError, "Invalid SBValue passed to GetValueSummary"));
}

if (!value.IsInScope()) {
return api::GetValueSummaryResponse()
.SetDisplayValue(std::nullopt)
.SetError(MakeError(Error::Code::kEvalError, "SBValue is not in scope"));
}

auto tryStringify = [&](lldb::SBValue value) -> std::optional<std::string> {
// Use GetSummary() if available.
if (const char *summary = value.GetSummary()) {
Expand All @@ -689,6 +717,11 @@ api::GetValueSummaryResponse ApiContext::GetValueSummary(api::Sbvalue rawValue)
return api::GetValueSummaryResponse().SetDisplayValue(display_value);
}

if (!value.GetError().Success()) {
auto err = value.GetError();
return api::GetValueSummaryResponse().SetError(MakeError(Error::Code::kInternalError, err.GetCString()));
}

// If the value is a pointer or reference, dereference it and try again.
if (value.GetType().IsPointerType() || value.GetType().IsReferenceType()) {
if (auto display_value = tryStringify(value.Dereference())) {
Expand Down
13 changes: 13 additions & 0 deletions extensions/cxx_debugging/lib/ApiContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,26 @@ class ApiContext : public DWARFSymbolsApi {

EvaluateExpressionResponse EvaluateExpression(
RawLocation location,
std::string stop_id,
std::string expression,
emscripten::val debug_proxy) final;

private:
llvm::StringMap<std::shared_ptr<WasmModule>> modules_;
llvm::StringMap<lldb_private::CompilerType> types_;

struct LastProcessInfo {
lldb::ProcessSP process;
std::string stop_id;
};
/// The last process and stop id used to evaluate an expression.
std::optional<LastProcessInfo> last_process_;

llvm::Expected<lldb::ProcessSP> GetProcess(std::string stop_id,
std::shared_ptr<WasmModule> module,
const api::DebuggerProxy& proxy,
size_t frame_offset);

std::shared_ptr<WasmModule> AddModule(llvm::StringRef id,
llvm::StringRef path);
std::shared_ptr<WasmModule> FindModule(llvm::StringRef id) const;
Expand Down
10 changes: 4 additions & 6 deletions extensions/cxx_debugging/lib/Expressions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ std::optional<llvm::Error> CheckError(lldb::SBValue value) {
return llvm::createStringError(llvm::inconvertibleErrorCode(), message);
}

llvm::Expected<lldb::ProcessSP> CreateProcess(const WasmModule& module,
const api::DebuggerProxy& proxy,
size_t frame_offset) {
llvm::Expected<lldb::ProcessSP> CreateProcess(const WasmModule& module) {
auto target = module.Target()->shared_from_this();
lldb::ListenerSP listener = lldb_private::Listener::MakeListener("wasm32");

Expand All @@ -112,8 +110,6 @@ llvm::Expected<lldb::ProcessSP> CreateProcess(const WasmModule& module,
lldb::eSectionTypeCode, false);
target->SetSectionLoadAddress(code_section, 0);

static_cast<WasmProcess*>(process.get())
->SetProxyAndFrameOffset(proxy, frame_offset);
process->UpdateThreadListIfNeeded();
process->GetThreadList().SetSelectedThreadByID(0);
return process;
Expand All @@ -136,8 +132,10 @@ llvm::Expected<ExpressionResult> InterpretExpression(
WasmValueLoaderContext loader(proxy, *llvm::cast<SymbolFileWasmDWARF>(
module.Module()->GetSymbolFile()));

lldb::SBFrame frame = thread->GetFrame();

auto sm = lldb_eval::SourceManager::Create(expression.str());
auto ctx = lldb_eval::Context::Create(sm, lldb::SBFrame{thread->GetFrame()});
auto ctx = lldb_eval::Context::Create(sm, frame);
lldb_eval::Parser parser(ctx);
lldb_eval::Error e;
auto tree = parser.Run(e);
Expand Down
4 changes: 1 addition & 3 deletions extensions/cxx_debugging/lib/Expressions.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ struct ExpressionResult {
std::optional<lldb::SBValue> value_object = std::nullopt;
};

llvm::Expected<lldb::ProcessSP> CreateProcess(const WasmModule& module,
const api::DebuggerProxy& proxy,
size_t frame_offset);
llvm::Expected<lldb::ProcessSP> CreateProcess(const WasmModule& module);

llvm::Expected<ExpressionResult> InterpretExpression(
const WasmModule& module,
Expand Down
1 change: 1 addition & 0 deletions extensions/cxx_debugging/lib/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ class DWARFSymbolsApi {

virtual EvaluateExpressionResponse EvaluateExpression(
RawLocation location,
std::string stop_id,
std::string expression,
emscripten::val debug_proxy
) = 0;
Expand Down
2 changes: 1 addition & 1 deletion extensions/cxx_debugging/src/DWARFSymbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ export class DWARFLanguageExtensionPlugin implements Chrome.DevTools.LanguageExt
const wasm = new Formatters.HostWasmInterface(this.hostInterface, stopId);
const proxy = new Formatters.DebuggerProxy(wasm, moduleInfo.backend);
const typeInfoResult =
manage(moduleInfo.dwarfSymbolsPlugin.EvaluateExpression(apiRawLocation, expression, proxy));
manage(moduleInfo.dwarfSymbolsPlugin.EvaluateExpression(apiRawLocation, String(stopId), expression, proxy));
const error = manage(typeInfoResult.error);
if (error) {
if (error.code === moduleInfo.backend.ErrorCode.MODULE_NOT_FOUND_ERROR) {
Expand Down
2 changes: 1 addition & 1 deletion extensions/cxx_debugging/src/SymbolsBackend.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export interface DWARFSymbolsPlugin extends EmbindObject {
GetValueSummary(value: Sbvalue): GetValueSummaryResponse;
GetValueInfo(value: Sbvalue): GetValueInfoResponse;
GetValueChildren(value: Sbvalue): GetValueChildrenResponse;
EvaluateExpression(location: RawLocation,expression: string,debugProxy: unknown): EvaluateExpressionResponse;
EvaluateExpression(location: RawLocation,stopId: string,expression: string,debugProxy: unknown): EvaluateExpressionResponse;
}

export interface Module extends EmscriptenModule {
Expand Down
1 change: 1 addition & 0 deletions extensions/cxx_debugging/tools/api.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ domain symbols_backend
command evaluateExpression
parameters
RawLocation location
string stopId
string expression
object debugProxy
returns
Expand Down

0 comments on commit 6c5e884

Please sign in to comment.