Skip to content

Commit d6951e9

Browse files
committed
[lldb-dap] add review changes.
1 parent 69545a2 commit d6951e9

File tree

7 files changed

+61
-45
lines changed

7 files changed

+61
-45
lines changed

lldb/tools/lldb-dap/Breakpoint.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ protocol::Breakpoint Breakpoint::ToProtocolBreakpoint() {
6464
"0x" + llvm::utohexstr(bp_addr.GetLoadAddress(m_bp.GetTarget()));
6565
breakpoint.instructionReference = formatted_addr;
6666

67-
std::optional<protocol::Source> source =
68-
CreateSource(bp_addr, m_dap.target, [this](lldb::addr_t addr) {
69-
return m_dap.CreateSourceReference(addr);
70-
});
67+
std::optional<protocol::Source> source = m_dap.ResolveSource(bp_addr);
7168
if (source && !IsAssemblySource(*source)) {
7269
auto line_entry = bp_addr.GetLineEntry();
7370
const auto line = line_entry.GetLine();

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -498,22 +498,24 @@ DAP::SendFormattedOutput(OutputType o, const char *format, ...) {
498498
}
499499

500500
int32_t DAP::CreateSourceReference(lldb::addr_t address) {
501-
auto iter = llvm::find(source_references, address);
502-
if (iter != source_references.end())
503-
return std::distance(source_references.begin(), iter) + 1;
501+
std::lock_guard<std::mutex> guard(m_source_references_mutex);
502+
auto iter = llvm::find(m_source_references, address);
503+
if (iter != m_source_references.end())
504+
return std::distance(m_source_references.begin(), iter) + 1;
504505

505-
source_references.emplace_back(address);
506-
return static_cast<int32_t>(source_references.size());
506+
m_source_references.emplace_back(address);
507+
return static_cast<int32_t>(m_source_references.size());
507508
}
508509

509510
std::optional<lldb::addr_t> DAP::GetSourceReferenceAddress(int32_t reference) {
511+
std::lock_guard<std::mutex> guard(m_source_references_mutex);
510512
if (reference <= LLDB_DAP_INVALID_SRC_REF)
511513
return std::nullopt;
512514

513-
if (static_cast<size_t>(reference) > source_references.size())
515+
if (static_cast<size_t>(reference) > m_source_references.size())
514516
return std::nullopt;
515517

516-
return source_references[reference - 1];
518+
return m_source_references[reference - 1];
517519
}
518520

519521
ExceptionBreakpoint *DAP::GetExceptionBPFromStopReason(lldb::SBThread &thread) {
@@ -621,6 +623,22 @@ ReplMode DAP::DetectReplMode(lldb::SBFrame frame, std::string &expression,
621623
llvm_unreachable("enum cases exhausted.");
622624
}
623625

626+
std::optional<protocol::Source> DAP::ResolveSource(lldb::SBAddress address) {
627+
628+
if (DisplayAssemblySource(debugger, address)) {
629+
auto create_reference = [this](lldb::addr_t addr) {
630+
return CreateSourceReference(addr);
631+
};
632+
return CreateAssemblySource(target, address, create_reference);
633+
}
634+
635+
lldb::SBLineEntry line_entry = GetLineEntryForAddress(target, address);
636+
if (!line_entry.IsValid())
637+
return std::nullopt;
638+
639+
return CreateSource(line_entry.GetFileSpec());
640+
}
641+
624642
bool DAP::RunLLDBCommands(llvm::StringRef prefix,
625643
llvm::ArrayRef<std::string> commands) {
626644
bool required_command_failed = false;

lldb/tools/lldb-dap/DAP.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,6 @@ struct DAP {
105105
/// Map step in target id to list of function targets that user can choose.
106106
llvm::DenseMap<lldb::addr_t, std::string> step_in_targets;
107107

108-
/// list of addresses mapped by sourceReference(index - 1)
109-
std::vector<lldb::addr_t> source_references;
110-
111108
/// A copy of the last LaunchRequest so we can reuse its arguments if we get a
112109
/// RestartRequest. Restarting an AttachRequest is not supported.
113110
std::optional<protocol::LaunchRequestArguments> last_launch_request;
@@ -257,6 +254,17 @@ struct DAP {
257254
ReplMode DetectReplMode(lldb::SBFrame frame, std::string &expression,
258255
bool partial_expression);
259256

257+
/// Create a "Source" JSON object as described in the debug adapter
258+
/// definition.
259+
///
260+
/// \param[in] address
261+
/// The address to use when populating out the "Source" object.
262+
///
263+
/// \return
264+
/// An optional "Source" JSON object that follows the formal JSON
265+
/// definition outlined by Microsoft.
266+
std::optional<protocol::Source> ResolveSource(lldb::SBAddress address);
267+
260268
/// \return
261269
/// \b false if a fatal error was found while executing these commands,
262270
/// according to the rules of \a LLDBUtils::RunLLDBCommands.
@@ -411,6 +419,10 @@ struct DAP {
411419
std::thread progress_event_thread;
412420
/// @}
413421

422+
/// list of addresses mapped by sourceReference(index - 1)
423+
std::vector<lldb::addr_t> m_source_references;
424+
std::mutex m_source_references_mutex;
425+
414426
/// Queue for all incoming messages.
415427
std::deque<protocol::Message> m_queue;
416428
std::mutex m_queue_mutex;

lldb/tools/lldb-dap/Handler/DisassembleRequestHandler.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,7 @@ static DisassembledInstruction ConvertSBInstructionToDisassembledInstruction(
139139
si << " ; " << c;
140140
}
141141

142-
std::optional<protocol::Source> source =
143-
CreateSource(addr, target, [&dap](lldb::addr_t addr) {
144-
return dap.CreateSourceReference(addr);
145-
});
142+
std::optional<protocol::Source> source = dap.ResolveSource(addr);
146143
lldb::SBLineEntry line_entry = GetLineEntryForAddress(target, addr);
147144

148145
// If the line number is 0 then the entry represents a compiler generated

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,8 @@ llvm::json::Value CreateStackFrame(DAP &dap, lldb::SBFrame &frame,
574574

575575
auto target = frame.GetThread().GetProcess().GetTarget();
576576
std::optional<protocol::Source> source =
577-
CreateSource(frame.GetPCAddress(), target, [&dap](lldb::addr_t addr) {
578-
return dap.CreateSourceReference(addr);
579-
});
577+
dap.ResolveSource(frame.GetPCAddress());
578+
580579
if (source && !IsAssemblySource(*source)) {
581580
// This is a normal source with a valid line entry.
582581
auto line_entry = frame.GetLineEntry();

lldb/tools/lldb-dap/ProtocolUtils.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ static bool ShouldDisplayAssemblySource(
4646
return false;
4747
}
4848

49-
static std::optional<protocol::Source> CreateAssemblySource(
49+
std::optional<protocol::Source> CreateAssemblySource(
5050
const lldb::SBTarget &target, lldb::SBAddress address,
51-
llvm::function_ref<uint32_t(lldb::addr_t)> create_reference) {
51+
llvm::function_ref<int32_t(lldb::addr_t)> create_reference) {
5252

5353
lldb::SBSymbol symbol = address.GetSymbol();
5454
lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
@@ -101,22 +101,6 @@ std::optional<protocol::Source> CreateSource(const lldb::SBFileSpec &file) {
101101
return source;
102102
}
103103

104-
std::optional<protocol::Source>
105-
CreateSource(lldb::SBAddress address, lldb::SBTarget &target,
106-
llvm::function_ref<int32_t(lldb::addr_t)> create_reference) {
107-
lldb::SBDebugger debugger = target.GetDebugger();
108-
lldb::StopDisassemblyType stop_disassembly_display =
109-
GetStopDisassemblyDisplay(debugger);
110-
if (ShouldDisplayAssemblySource(address, stop_disassembly_display))
111-
return CreateAssemblySource(target, address, create_reference);
112-
113-
lldb::SBLineEntry line_entry = GetLineEntryForAddress(target, address);
114-
if (!line_entry.IsValid())
115-
return std::nullopt;
116-
117-
return CreateSource(line_entry.GetFileSpec());
118-
}
119-
120104
bool IsAssemblySource(const protocol::Source &source) {
121105
// According to the specification, a source must have either `path` or
122106
// `sourceReference` specified. We use `path` for sources with known source
@@ -125,6 +109,13 @@ bool IsAssemblySource(const protocol::Source &source) {
125109
LLDB_DAP_INVALID_SRC_REF;
126110
}
127111

112+
bool DisplayAssemblySource(lldb::SBDebugger &debugger,
113+
lldb::SBAddress address) {
114+
const lldb::StopDisassemblyType stop_disassembly_display =
115+
GetStopDisassemblyDisplay(debugger);
116+
return ShouldDisplayAssemblySource(address, stop_disassembly_display);
117+
}
118+
128119
std::string GetLoadAddressString(const lldb::addr_t addr) {
129120
return "0x" + llvm::utohexstr(addr, false, 16);
130121
}

lldb/tools/lldb-dap/ProtocolUtils.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,27 @@ std::optional<protocol::Source> CreateSource(const lldb::SBFileSpec &file);
3232

3333
/// Create a "Source" JSON object as described in the debug adapter definition.
3434
///
35-
/// \param[in] address
36-
/// The address to use when populating out the "Source" object.
37-
///
3835
/// \param[in] target
3936
/// The target that has the address.
4037
///
38+
/// \param[in] address
39+
/// The address to use when populating out the "Source" object.
40+
///
4141
/// \param[in] create_reference
4242
/// function used to create a source_reference
4343
///
4444
/// \return
4545
/// An optional "Source" JSON object that follows the formal JSON
4646
/// definition outlined by Microsoft.
47-
std::optional<protocol::Source>
48-
CreateSource(lldb::SBAddress address, lldb::SBTarget &target,
49-
llvm::function_ref<int32_t(lldb::addr_t)> create_reference);
47+
std::optional<protocol::Source> CreateAssemblySource(
48+
const lldb::SBTarget &target, lldb::SBAddress address,
49+
llvm::function_ref<int32_t(lldb::addr_t)> create_reference);
5050

5151
/// Checks if the given source is for assembly code.
5252
bool IsAssemblySource(const protocol::Source &source);
5353

54+
bool DisplayAssemblySource(lldb::SBDebugger &debugger, lldb::SBAddress address);
55+
5456
/// Get the address as a 16-digit hex string, e.g. "0x0000000000012345"
5557
std::string GetLoadAddressString(const lldb::addr_t addr);
5658

0 commit comments

Comments
 (0)