Skip to content

Commit cd2f834

Browse files
committed
move CreaetSource function to ProtocolUtils instead of JSONUtils
1 parent 1d14ddf commit cd2f834

File tree

7 files changed

+137
-129
lines changed

7 files changed

+137
-129
lines changed

lldb/tools/lldb-dap/Breakpoint.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "Breakpoint.h"
1010
#include "DAP.h"
11-
#include "JSONUtils.h"
1211
#include "Protocol/ProtocolUtils.h"
1312
#include "lldb/API/SBAddress.h"
1413
#include "lldb/API/SBBreakpointLocation.h"
@@ -65,7 +64,7 @@ protocol::Breakpoint Breakpoint::ToProtocolBreakpoint() {
6564
"0x" + llvm::utohexstr(bp_addr.GetLoadAddress(m_bp.GetTarget()));
6665
breakpoint.instructionReference = formatted_addr;
6766

68-
auto source = CreateSource(bp_addr, m_dap.target);
67+
auto source = protocol::CreateSource(bp_addr, m_dap.target);
6968
if (!IsAssemblySource(source)) {
7069
auto line_entry = bp_addr.GetLineEntry();
7170
const auto line = line_entry.GetLine();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "RequestHandler.h"
1717
#include "lldb/API/SBAddress.h"
1818
#include "lldb/API/SBInstruction.h"
19+
#include "lldb/API/SBLineEntry.h"
1920
#include "lldb/API/SBTarget.h"
2021
#include "lldb/lldb-types.h"
2122
#include "llvm/ADT/StringExtras.h"
@@ -141,8 +142,8 @@ static DisassembledInstruction ConvertSBInstructionToDisassembledInstruction(
141142

142143
disassembled_inst.instruction = std::move(instruction);
143144

144-
auto source = CreateSource(addr, target);
145-
auto line_entry = GetLineEntryForAddress(target, addr);
145+
protocol::Source source = CreateSource(addr, target);
146+
lldb::SBLineEntry line_entry = GetLineEntryForAddress(target, addr);
146147

147148
// If the line number is 0 then the entry represents a compiler generated
148149
// location.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "EventHelper.h"
1111
#include "JSONUtils.h"
1212
#include "LLDBUtils.h"
13+
#include "Protocol/ProtocolUtils.h"
1314
#include "RequestHandler.h"
1415
#include "lldb/API/SBAddress.h"
1516
#include "lldb/API/SBDeclaration.h"
@@ -136,7 +137,8 @@ void LocationsRequestHandler::operator()(
136137
return;
137138
}
138139

139-
body.try_emplace("source", CreateSource(line_entry.GetFileSpec()));
140+
body.try_emplace("source",
141+
protocol::CreateSource(line_entry.GetFileSpec()));
140142
if (int line = line_entry.GetLine())
141143
body.try_emplace("line", line);
142144
if (int column = line_entry.GetColumn())
@@ -151,7 +153,7 @@ void LocationsRequestHandler::operator()(
151153
return;
152154
}
153155

154-
body.try_emplace("source", CreateSource(decl.GetFileSpec()));
156+
body.try_emplace("source", protocol::CreateSource(decl.GetFileSpec()));
155157
if (int line = decl.GetLine())
156158
body.try_emplace("line", line);
157159
if (int column = decl.GetColumn())

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 2 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "Protocol/ProtocolUtils.h"
1414
#include "lldb/API/SBAddress.h"
1515
#include "lldb/API/SBCompileUnit.h"
16-
#include "lldb/API/SBDebugger.h"
1716
#include "lldb/API/SBDeclaration.h"
1817
#include "lldb/API/SBEnvironment.h"
1918
#include "lldb/API/SBError.h"
@@ -492,99 +491,6 @@ CreateExceptionBreakpointFilter(const ExceptionBreakpoint &bp) {
492491
return filter;
493492
}
494493

495-
static std::string GetLoadAddressString(const lldb::addr_t addr) {
496-
std::string result;
497-
llvm::raw_string_ostream os(result);
498-
os << llvm::format_hex(addr, 18);
499-
return result;
500-
}
501-
502-
static bool ShouldDisplayAssemblySource(
503-
lldb::SBAddress address,
504-
lldb::StopDisassemblyType stop_disassembly_display) {
505-
if (stop_disassembly_display == lldb::eStopDisassemblyTypeNever)
506-
return false;
507-
508-
if (stop_disassembly_display == lldb::eStopDisassemblyTypeAlways)
509-
return true;
510-
511-
// A line entry of 0 indicates the line is compiler generated i.e. no source
512-
// file is associated with the frame.
513-
auto line_entry = address.GetLineEntry();
514-
auto file_spec = line_entry.GetFileSpec();
515-
if (!file_spec.IsValid() || line_entry.GetLine() == 0 ||
516-
line_entry.GetLine() == LLDB_INVALID_LINE_NUMBER)
517-
return true;
518-
519-
if (stop_disassembly_display == lldb::eStopDisassemblyTypeNoSource &&
520-
!file_spec.Exists()) {
521-
return true;
522-
}
523-
524-
return false;
525-
}
526-
527-
static protocol::Source CreateAssemblySource(const lldb::SBTarget &target,
528-
lldb::SBAddress address) {
529-
protocol::Source source;
530-
531-
auto symbol = address.GetSymbol();
532-
std::string name;
533-
if (symbol.IsValid()) {
534-
source.sourceReference = symbol.GetStartAddress().GetLoadAddress(target);
535-
name = symbol.GetName();
536-
} else {
537-
const auto load_addr = address.GetLoadAddress(target);
538-
source.sourceReference = load_addr;
539-
name = GetLoadAddressString(load_addr);
540-
}
541-
542-
lldb::SBModule module = address.GetModule();
543-
if (module.IsValid()) {
544-
lldb::SBFileSpec file_spec = module.GetFileSpec();
545-
if (file_spec.IsValid()) {
546-
std::string path = GetSBFileSpecPath(file_spec);
547-
if (!path.empty())
548-
source.path = path + '`' + name;
549-
}
550-
}
551-
552-
source.name = std::move(name);
553-
554-
// Mark the source as deemphasized since users will only be able to view
555-
// assembly for these frames.
556-
source.presentationHint =
557-
protocol::Source::PresentationHint::eSourcePresentationHintDeemphasize;
558-
559-
return source;
560-
}
561-
562-
protocol::Source CreateSource(const lldb::SBFileSpec &file) {
563-
protocol::Source source;
564-
if (file.IsValid()) {
565-
const char *name = file.GetFilename();
566-
if (name)
567-
source.name = name;
568-
char path[PATH_MAX] = "";
569-
if (file.GetPath(path, sizeof(path)) &&
570-
lldb::SBFileSpec::ResolvePath(path, path, PATH_MAX))
571-
source.path = path;
572-
}
573-
return source;
574-
}
575-
576-
protocol::Source CreateSource(lldb::SBAddress address, lldb::SBTarget &target) {
577-
lldb::SBDebugger debugger = target.GetDebugger();
578-
lldb::StopDisassemblyType stop_disassembly_display =
579-
GetStopDisassemblyDisplay(debugger);
580-
if (!ShouldDisplayAssemblySource(address, stop_disassembly_display)) {
581-
lldb::SBLineEntry line_entry = GetLineEntryForAddress(target, address);
582-
return CreateSource(line_entry.GetFileSpec());
583-
}
584-
585-
return CreateAssemblySource(target, address);
586-
}
587-
588494
// "StackFrame": {
589495
// "type": "object",
590496
// "description": "A Stackframe contains the source location.",
@@ -666,7 +572,7 @@ llvm::json::Value CreateStackFrame(lldb::SBFrame &frame,
666572
if (frame_name.empty()) {
667573
// If the function name is unavailable, display the pc address as a 16-digit
668574
// hex string, e.g. "0x0000000000012345"
669-
frame_name = GetLoadAddressString(frame.GetPC());
575+
frame_name = protocol::GetLoadAddressString(frame.GetPC());
670576
}
671577

672578
// We only include `[opt]` if a custom frame format is not specified.
@@ -676,7 +582,7 @@ llvm::json::Value CreateStackFrame(lldb::SBFrame &frame,
676582
EmplaceSafeString(object, "name", frame_name);
677583

678584
auto target = frame.GetThread().GetProcess().GetTarget();
679-
auto source = CreateSource(frame.GetPCAddress(), target);
585+
auto source = protocol::CreateSource(frame.GetPCAddress(), target);
680586
if (!IsAssemblySource(source)) {
681587
// This is a normal source with a valid line entry.
682588
auto line_entry = frame.GetLineEntry();

lldb/tools/lldb-dap/JSONUtils.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@
1111

1212
#include "DAPForward.h"
1313
#include "Protocol/ProtocolTypes.h"
14-
#include "lldb/API/SBAddress.h"
1514
#include "lldb/API/SBCompileUnit.h"
16-
#include "lldb/API/SBDebugger.h"
17-
#include "lldb/API/SBFileSpec.h"
1815
#include "lldb/API/SBFormat.h"
19-
#include "lldb/API/SBLineEntry.h"
2016
#include "lldb/API/SBType.h"
2117
#include "lldb/API/SBValue.h"
2218
#include "lldb/lldb-types.h"
@@ -240,29 +236,6 @@ llvm::json::Object CreateEventObject(const llvm::StringRef event_name);
240236
protocol::ExceptionBreakpointsFilter
241237
CreateExceptionBreakpointFilter(const ExceptionBreakpoint &bp);
242238

243-
/// Create a "Source" JSON object as described in the debug adapter definition.
244-
///
245-
/// \param[in] file
246-
/// The SBFileSpec to use when populating out the "Source" object
247-
///
248-
/// \return
249-
/// A "Source" JSON object that follows the formal JSON
250-
/// definition outlined by Microsoft.
251-
protocol::Source CreateSource(const lldb::SBFileSpec &file);
252-
253-
/// Create a "Source" JSON object as described in the debug adapter definition.
254-
///
255-
/// \param[in] address
256-
/// The address to use when populating out the "Source" object.
257-
///
258-
/// \param[in] target
259-
/// The target that has the address.
260-
///
261-
/// \return
262-
/// A "Source" JSON object that follows the formal JSON
263-
/// definition outlined by Microsoft.
264-
protocol::Source CreateSource(lldb::SBAddress address, lldb::SBTarget &target);
265-
266239
/// Create a "StackFrame" object for a LLDB frame object.
267240
///
268241
/// This function will fill in the following keys in the returned

lldb/tools/lldb-dap/Protocol/ProtocolUtils.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,109 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Protocol/ProtocolUtils.h"
10+
#include "LLDBUtils.h"
11+
12+
#include "lldb/API/SBDebugger.h"
13+
#include "lldb/API/SBTarget.h"
1014

1115
namespace lldb_dap::protocol {
1216

17+
static bool ShouldDisplayAssemblySource(
18+
lldb::SBAddress address,
19+
lldb::StopDisassemblyType stop_disassembly_display) {
20+
if (stop_disassembly_display == lldb::eStopDisassemblyTypeNever)
21+
return false;
22+
23+
if (stop_disassembly_display == lldb::eStopDisassemblyTypeAlways)
24+
return true;
25+
26+
// A line entry of 0 indicates the line is compiler generated i.e. no source
27+
// file is associated with the frame.
28+
auto line_entry = address.GetLineEntry();
29+
auto file_spec = line_entry.GetFileSpec();
30+
if (!file_spec.IsValid() || line_entry.GetLine() == 0 ||
31+
line_entry.GetLine() == LLDB_INVALID_LINE_NUMBER)
32+
return true;
33+
34+
if (stop_disassembly_display == lldb::eStopDisassemblyTypeNoSource &&
35+
!file_spec.Exists()) {
36+
return true;
37+
}
38+
39+
return false;
40+
}
41+
42+
static protocol::Source CreateAssemblySource(const lldb::SBTarget &target,
43+
lldb::SBAddress address) {
44+
protocol::Source source;
45+
46+
auto symbol = address.GetSymbol();
47+
std::string name;
48+
if (symbol.IsValid()) {
49+
source.sourceReference = symbol.GetStartAddress().GetLoadAddress(target);
50+
name = symbol.GetName();
51+
} else {
52+
const auto load_addr = address.GetLoadAddress(target);
53+
source.sourceReference = load_addr;
54+
name = GetLoadAddressString(load_addr);
55+
}
56+
57+
lldb::SBModule module = address.GetModule();
58+
if (module.IsValid()) {
59+
lldb::SBFileSpec file_spec = module.GetFileSpec();
60+
if (file_spec.IsValid()) {
61+
std::string path = GetSBFileSpecPath(file_spec);
62+
if (!path.empty())
63+
source.path = path + '`' + name;
64+
}
65+
}
66+
67+
source.name = std::move(name);
68+
69+
// Mark the source as deemphasized since users will only be able to view
70+
// assembly for these frames.
71+
source.presentationHint =
72+
protocol::Source::PresentationHint::eSourcePresentationHintDeemphasize;
73+
74+
return source;
75+
}
76+
77+
protocol::Source CreateSource(const lldb::SBFileSpec &file) {
78+
protocol::Source source;
79+
if (file.IsValid()) {
80+
if (const char *name = file.GetFilename())
81+
source.name = name;
82+
char path[PATH_MAX] = "";
83+
if (file.GetPath(path, sizeof(path)) &&
84+
lldb::SBFileSpec::ResolvePath(path, path, PATH_MAX))
85+
source.path = path;
86+
}
87+
return source;
88+
}
89+
90+
protocol::Source CreateSource(lldb::SBAddress address, lldb::SBTarget &target) {
91+
lldb::SBDebugger debugger = target.GetDebugger();
92+
lldb::StopDisassemblyType stop_disassembly_display =
93+
GetStopDisassemblyDisplay(debugger);
94+
if (ShouldDisplayAssemblySource(address, stop_disassembly_display))
95+
return CreateAssemblySource(target, address);
96+
97+
lldb::SBLineEntry line_entry = GetLineEntryForAddress(target, address);
98+
return CreateSource(line_entry.GetFileSpec());
99+
}
100+
13101
bool IsAssemblySource(const protocol::Source &source) {
102+
// According to the specification, a source must have either `path` or
103+
// `sourceReference` specified. We use `path` for sources with known source
104+
// code, and `sourceReferences` when falling back to assembly.
14105
return source.sourceReference.value_or(0) != 0;
15106
}
16107

108+
std::string GetLoadAddressString(const lldb::addr_t addr) {
109+
std::string result;
110+
llvm::raw_string_ostream os(result);
111+
os << llvm::format_hex(addr, 18);
112+
return result;
113+
}
114+
17115
} // namespace lldb_dap::protocol

lldb/tools/lldb-dap/Protocol/ProtocolUtils.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,39 @@
1515

1616
#include "Protocol/ProtocolTypes.h"
1717

18+
#include "lldb/API/SBAddress.h"
19+
1820
namespace lldb_dap::protocol {
1921

22+
/// Create a "Source" JSON object as described in the debug adapter definition.
23+
///
24+
/// \param[in] file
25+
/// The SBFileSpec to use when populating out the "Source" object
26+
///
27+
/// \return
28+
/// A "Source" JSON object that follows the formal JSON
29+
/// definition outlined by Microsoft.
30+
protocol::Source CreateSource(const lldb::SBFileSpec &file);
31+
32+
/// Create a "Source" JSON object as described in the debug adapter definition.
33+
///
34+
/// \param[in] address
35+
/// The address to use when populating out the "Source" object.
36+
///
37+
/// \param[in] target
38+
/// The target that has the address.
39+
///
40+
/// \return
41+
/// A "Source" JSON object that follows the formal JSON
42+
/// definition outlined by Microsoft.
43+
protocol::Source CreateSource(lldb::SBAddress address, lldb::SBTarget &target);
44+
45+
/// Checks if the given source is for assembly code.
2046
bool IsAssemblySource(const protocol::Source &source);
2147

48+
/// Get the address as a 16-digit hex string, e.g. "0x0000000000012345"
49+
std::string GetLoadAddressString(const lldb::addr_t addr);
50+
2251
} // namespace lldb_dap::protocol
2352

2453
#endif

0 commit comments

Comments
 (0)