Skip to content

Commit 03a1099

Browse files
committed
[lldb-dap] add review changes.
1 parent 8b79cd7 commit 03a1099

File tree

7 files changed

+62
-45
lines changed

7 files changed

+62
-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: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "Protocol/ProtocolBase.h"
1818
#include "Protocol/ProtocolRequests.h"
1919
#include "Protocol/ProtocolTypes.h"
20+
#include "ProtocolUtils.h"
2021
#include "Transport.h"
2122
#include "lldb/API/SBBreakpoint.h"
2223
#include "lldb/API/SBCommandInterpreter.h"
@@ -511,22 +512,24 @@ DAP::SendFormattedOutput(OutputType o, const char *format, ...) {
511512
}
512513

513514
int32_t DAP::CreateSourceReference(lldb::addr_t address) {
514-
auto iter = llvm::find(source_references, address);
515-
if (iter != source_references.end())
516-
return std::distance(source_references.begin(), iter) + 1;
515+
std::lock_guard<std::mutex> guard(m_source_references_mutex);
516+
auto iter = llvm::find(m_source_references, address);
517+
if (iter != m_source_references.end())
518+
return std::distance(m_source_references.begin(), iter) + 1;
517519

518-
source_references.emplace_back(address);
519-
return static_cast<int32_t>(source_references.size());
520+
m_source_references.emplace_back(address);
521+
return static_cast<int32_t>(m_source_references.size());
520522
}
521523

522524
std::optional<lldb::addr_t> DAP::GetSourceReferenceAddress(int32_t reference) {
525+
std::lock_guard<std::mutex> guard(m_source_references_mutex);
523526
if (reference <= LLDB_DAP_INVALID_SRC_REF)
524527
return std::nullopt;
525528

526-
if (static_cast<size_t>(reference) > source_references.size())
529+
if (static_cast<size_t>(reference) > m_source_references.size())
527530
return std::nullopt;
528531

529-
return source_references[reference - 1];
532+
return m_source_references[reference - 1];
530533
}
531534

532535
ExceptionBreakpoint *DAP::GetExceptionBPFromStopReason(lldb::SBThread &thread) {
@@ -634,6 +637,22 @@ ReplMode DAP::DetectReplMode(lldb::SBFrame frame, std::string &expression,
634637
llvm_unreachable("enum cases exhausted.");
635638
}
636639

640+
std::optional<protocol::Source> DAP::ResolveSource(lldb::SBAddress address) {
641+
642+
if (DisplayAssemblySource(debugger, address)) {
643+
auto create_reference = [this](lldb::addr_t addr) {
644+
return CreateSourceReference(addr);
645+
};
646+
return CreateAssemblySource(target, address, create_reference);
647+
}
648+
649+
lldb::SBLineEntry line_entry = GetLineEntryForAddress(target, address);
650+
if (!line_entry.IsValid())
651+
return std::nullopt;
652+
653+
return CreateSource(line_entry.GetFileSpec());
654+
}
655+
637656
bool DAP::RunLLDBCommands(llvm::StringRef prefix,
638657
llvm::ArrayRef<std::string> commands) {
639658
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
@@ -583,9 +583,8 @@ llvm::json::Value CreateStackFrame(DAP &dap, lldb::SBFrame &frame,
583583

584584
auto target = frame.GetThread().GetProcess().GetTarget();
585585
std::optional<protocol::Source> source =
586-
CreateSource(frame.GetPCAddress(), target, [&dap](lldb::addr_t addr) {
587-
return dap.CreateSourceReference(addr);
588-
});
586+
dap.ResolveSource(frame.GetPCAddress());
587+
589588
if (source && !IsAssemblySource(*source)) {
590589
// This is a normal source with a valid line entry.
591590
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
@@ -31,25 +31,27 @@ std::optional<protocol::Source> CreateSource(const lldb::SBFileSpec &file);
3131

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

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

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

0 commit comments

Comments
 (0)