Skip to content

Commit 6c5e884

Browse files
[swift] Fix EvaluateExpression to use the same process instance for the same stop_id
1 parent f81ac99 commit 6c5e884

File tree

8 files changed

+57
-13
lines changed

8 files changed

+57
-13
lines changed

extensions/cxx_debugging/lib/ApiContext.cc

+35-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Expressions.h"
77
#include "Variables.h"
88
#include "WasmModule.h"
9+
#include "WasmVendorPlugins.h"
910
#include "api.h"
1011

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

629630
static SBValueArena arena;
630631

632+
llvm::Expected<lldb::ProcessSP> ApiContext::GetProcess(
633+
std::string stop_id,
634+
std::shared_ptr<WasmModule> module,
635+
const api::DebuggerProxy& proxy,
636+
size_t frame_offset) {
637+
lldb::ProcessSP process;
638+
if (last_process_ && last_process_->process &&
639+
last_process_->stop_id == stop_id) {
640+
process = last_process_->process;
641+
} else {
642+
auto maybe_process = ::symbols_backend::CreateProcess(*module);
643+
if (!maybe_process) {
644+
return maybe_process.takeError();
645+
}
646+
process = maybe_process.get();
647+
last_process_ = LastProcessInfo{process, stop_id};
648+
}
649+
static_cast<WasmProcess*>(process.get())
650+
->SetProxyAndFrameOffset(proxy, frame_offset);
651+
return process;
652+
}
631653

632654
api::EvaluateExpressionResponse ApiContext::EvaluateExpression(
633655
RawLocation location,
656+
std::string stop_id,
634657
std::string expression,
635658
emscripten::val debug_proxy) {
636659
std::string raw_module_id = location.GetRawModuleId();
@@ -640,8 +663,7 @@ api::EvaluateExpressionResponse ApiContext::EvaluateExpression(
640663
MakeNotFoundError(raw_module_id));
641664
}
642665
DebuggerProxy proxy{debug_proxy};
643-
auto process = ::symbols_backend::CreateProcess(*module, proxy,
644-
location.GetCodeOffset());
666+
auto process = GetProcess(stop_id, module, proxy, location.GetCodeOffset());
645667
if (!process) {
646668
return api::EvaluateExpressionResponse().SetError(
647669
MakeError(Error::Code::kEvalError, process.takeError()));
@@ -672,6 +694,12 @@ api::GetValueSummaryResponse ApiContext::GetValueSummary(api::Sbvalue rawValue)
672694
.SetError(MakeError(Error::Code::kEvalError, "Invalid SBValue passed to GetValueSummary"));
673695
}
674696

697+
if (!value.IsInScope()) {
698+
return api::GetValueSummaryResponse()
699+
.SetDisplayValue(std::nullopt)
700+
.SetError(MakeError(Error::Code::kEvalError, "SBValue is not in scope"));
701+
}
702+
675703
auto tryStringify = [&](lldb::SBValue value) -> std::optional<std::string> {
676704
// Use GetSummary() if available.
677705
if (const char *summary = value.GetSummary()) {
@@ -689,6 +717,11 @@ api::GetValueSummaryResponse ApiContext::GetValueSummary(api::Sbvalue rawValue)
689717
return api::GetValueSummaryResponse().SetDisplayValue(display_value);
690718
}
691719

720+
if (!value.GetError().Success()) {
721+
auto err = value.GetError();
722+
return api::GetValueSummaryResponse().SetError(MakeError(Error::Code::kInternalError, err.GetCString()));
723+
}
724+
692725
// If the value is a pointer or reference, dereference it and try again.
693726
if (value.GetType().IsPointerType() || value.GetType().IsReferenceType()) {
694727
if (auto display_value = tryStringify(value.Dereference())) {

extensions/cxx_debugging/lib/ApiContext.h

+13
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,26 @@ class ApiContext : public DWARFSymbolsApi {
9393

9494
EvaluateExpressionResponse EvaluateExpression(
9595
RawLocation location,
96+
std::string stop_id,
9697
std::string expression,
9798
emscripten::val debug_proxy) final;
9899

99100
private:
100101
llvm::StringMap<std::shared_ptr<WasmModule>> modules_;
101102
llvm::StringMap<lldb_private::CompilerType> types_;
102103

104+
struct LastProcessInfo {
105+
lldb::ProcessSP process;
106+
std::string stop_id;
107+
};
108+
/// The last process and stop id used to evaluate an expression.
109+
std::optional<LastProcessInfo> last_process_;
110+
111+
llvm::Expected<lldb::ProcessSP> GetProcess(std::string stop_id,
112+
std::shared_ptr<WasmModule> module,
113+
const api::DebuggerProxy& proxy,
114+
size_t frame_offset);
115+
103116
std::shared_ptr<WasmModule> AddModule(llvm::StringRef id,
104117
llvm::StringRef path);
105118
std::shared_ptr<WasmModule> FindModule(llvm::StringRef id) const;

extensions/cxx_debugging/lib/Expressions.cc

+4-6
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ std::optional<llvm::Error> CheckError(lldb::SBValue value) {
9595
return llvm::createStringError(llvm::inconvertibleErrorCode(), message);
9696
}
9797

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

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

115-
static_cast<WasmProcess*>(process.get())
116-
->SetProxyAndFrameOffset(proxy, frame_offset);
117113
process->UpdateThreadListIfNeeded();
118114
process->GetThreadList().SetSelectedThreadByID(0);
119115
return process;
@@ -136,8 +132,10 @@ llvm::Expected<ExpressionResult> InterpretExpression(
136132
WasmValueLoaderContext loader(proxy, *llvm::cast<SymbolFileWasmDWARF>(
137133
module.Module()->GetSymbolFile()));
138134

135+
lldb::SBFrame frame = thread->GetFrame();
136+
139137
auto sm = lldb_eval::SourceManager::Create(expression.str());
140-
auto ctx = lldb_eval::Context::Create(sm, lldb::SBFrame{thread->GetFrame()});
138+
auto ctx = lldb_eval::Context::Create(sm, frame);
141139
lldb_eval::Parser parser(ctx);
142140
lldb_eval::Error e;
143141
auto tree = parser.Run(e);

extensions/cxx_debugging/lib/Expressions.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ struct ExpressionResult {
4141
std::optional<lldb::SBValue> value_object = std::nullopt;
4242
};
4343

44-
llvm::Expected<lldb::ProcessSP> CreateProcess(const WasmModule& module,
45-
const api::DebuggerProxy& proxy,
46-
size_t frame_offset);
44+
llvm::Expected<lldb::ProcessSP> CreateProcess(const WasmModule& module);
4745

4846
llvm::Expected<ExpressionResult> InterpretExpression(
4947
const WasmModule& module,

extensions/cxx_debugging/lib/api.h

+1
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ class DWARFSymbolsApi {
715715

716716
virtual EvaluateExpressionResponse EvaluateExpression(
717717
RawLocation location,
718+
std::string stop_id,
718719
std::string expression,
719720
emscripten::val debug_proxy
720721
) = 0;

extensions/cxx_debugging/src/DWARFSymbols.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ export class DWARFLanguageExtensionPlugin implements Chrome.DevTools.LanguageExt
404404
const wasm = new Formatters.HostWasmInterface(this.hostInterface, stopId);
405405
const proxy = new Formatters.DebuggerProxy(wasm, moduleInfo.backend);
406406
const typeInfoResult =
407-
manage(moduleInfo.dwarfSymbolsPlugin.EvaluateExpression(apiRawLocation, expression, proxy));
407+
manage(moduleInfo.dwarfSymbolsPlugin.EvaluateExpression(apiRawLocation, String(stopId), expression, proxy));
408408
const error = manage(typeInfoResult.error);
409409
if (error) {
410410
if (error.code === moduleInfo.backend.ErrorCode.MODULE_NOT_FOUND_ERROR) {

extensions/cxx_debugging/src/SymbolsBackend.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export interface DWARFSymbolsPlugin extends EmbindObject {
166166
GetValueSummary(value: Sbvalue): GetValueSummaryResponse;
167167
GetValueInfo(value: Sbvalue): GetValueInfoResponse;
168168
GetValueChildren(value: Sbvalue): GetValueChildrenResponse;
169-
EvaluateExpression(location: RawLocation,expression: string,debugProxy: unknown): EvaluateExpressionResponse;
169+
EvaluateExpression(location: RawLocation,stopId: string,expression: string,debugProxy: unknown): EvaluateExpressionResponse;
170170
}
171171

172172
export interface Module extends EmscriptenModule {

extensions/cxx_debugging/tools/api.pdl

+1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ domain symbols_backend
245245
command evaluateExpression
246246
parameters
247247
RawLocation location
248+
string stopId
248249
string expression
249250
object debugProxy
250251
returns

0 commit comments

Comments
 (0)