Skip to content

Commit 5db2a6e

Browse files
committed
use reference, move ProtocolUtils.h
1 parent cd2f834 commit 5db2a6e

File tree

10 files changed

+128
-20
lines changed

10 files changed

+128
-20
lines changed

lldb/tools/lldb-dap/Breakpoint.cpp

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

99
#include "Breakpoint.h"
1010
#include "DAP.h"
11-
#include "Protocol/ProtocolUtils.h"
11+
#include "ProtocolUtils.h"
1212
#include "lldb/API/SBAddress.h"
1313
#include "lldb/API/SBBreakpointLocation.h"
1414
#include "lldb/API/SBLineEntry.h"
@@ -64,7 +64,7 @@ protocol::Breakpoint Breakpoint::ToProtocolBreakpoint() {
6464
"0x" + llvm::utohexstr(bp_addr.GetLoadAddress(m_bp.GetTarget()));
6565
breakpoint.instructionReference = formatted_addr;
6666

67-
auto source = protocol::CreateSource(bp_addr, m_dap.target);
67+
auto source = CreateSource(bp_addr, m_dap.target);
6868
if (!IsAssemblySource(source)) {
6969
auto line_entry = bp_addr.GetLineEntry();
7070
const auto line = line_entry.GetLine();

lldb/tools/lldb-dap/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ add_lldb_library(lldbDAP
2121
LLDBUtils.cpp
2222
OutputRedirector.cpp
2323
ProgressEvent.cpp
24+
ProtocolUtils.cpp
2425
RunInTerminal.cpp
2526
SourceBreakpoint.cpp
2627
Transport.cpp
@@ -68,7 +69,6 @@ add_lldb_library(lldbDAP
6869
Protocol/ProtocolBase.cpp
6970
Protocol/ProtocolTypes.cpp
7071
Protocol/ProtocolRequests.cpp
71-
Protocol/ProtocolUtils.cpp
7272

7373
LINK_LIBS
7474
liblldb

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "LLDBUtils.h"
1313
#include "Protocol/ProtocolRequests.h"
1414
#include "Protocol/ProtocolTypes.h"
15-
#include "Protocol/ProtocolUtils.h"
15+
#include "ProtocolUtils.h"
1616
#include "RequestHandler.h"
1717
#include "lldb/API/SBAddress.h"
1818
#include "lldb/API/SBInstruction.h"

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "EventHelper.h"
1111
#include "JSONUtils.h"
1212
#include "LLDBUtils.h"
13-
#include "Protocol/ProtocolUtils.h"
13+
#include "ProtocolUtils.h"
1414
#include "RequestHandler.h"
1515
#include "lldb/API/SBAddress.h"
1616
#include "lldb/API/SBDeclaration.h"
@@ -137,8 +137,7 @@ void LocationsRequestHandler::operator()(
137137
return;
138138
}
139139

140-
body.try_emplace("source",
141-
protocol::CreateSource(line_entry.GetFileSpec()));
140+
body.try_emplace("source", CreateSource(line_entry.GetFileSpec()));
142141
if (int line = line_entry.GetLine())
143142
body.try_emplace("line", line);
144143
if (int column = line_entry.GetColumn())
@@ -153,7 +152,7 @@ void LocationsRequestHandler::operator()(
153152
return;
154153
}
155154

156-
body.try_emplace("source", protocol::CreateSource(decl.GetFileSpec()));
155+
body.try_emplace("source", CreateSource(decl.GetFileSpec()));
157156
if (int line = decl.GetLine())
158157
body.try_emplace("line", line);
159158
if (int column = decl.GetColumn())

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "DAP.h"
1111
#include "ExceptionBreakpoint.h"
1212
#include "LLDBUtils.h"
13-
#include "Protocol/ProtocolUtils.h"
13+
#include "ProtocolUtils.h"
1414
#include "lldb/API/SBAddress.h"
1515
#include "lldb/API/SBCompileUnit.h"
1616
#include "lldb/API/SBDeclaration.h"
@@ -572,7 +572,7 @@ llvm::json::Value CreateStackFrame(lldb::SBFrame &frame,
572572
if (frame_name.empty()) {
573573
// If the function name is unavailable, display the pc address as a 16-digit
574574
// hex string, e.g. "0x0000000000012345"
575-
frame_name = protocol::GetLoadAddressString(frame.GetPC());
575+
frame_name = GetLoadAddressString(frame.GetPC());
576576
}
577577

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

584584
auto target = frame.GetThread().GetProcess().GetTarget();
585-
auto source = protocol::CreateSource(frame.GetPCAddress(), target);
585+
auto source = CreateSource(frame.GetPCAddress(), target);
586586
if (!IsAssemblySource(source)) {
587587
// This is a normal source with a valid line entry.
588588
auto line_entry = frame.GetLineEntry();

lldb/tools/lldb-dap/LLDBUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ std::string GetSBFileSpecPath(const lldb::SBFileSpec &file_spec) {
253253
}
254254

255255
lldb::SBLineEntry GetLineEntryForAddress(lldb::SBTarget &target,
256-
lldb::SBAddress &address) {
256+
const lldb::SBAddress &address) {
257257
lldb::SBSymbolContext sc = target.ResolveSymbolContextForAddress(
258258
address, lldb::eSymbolContextLineEntry);
259259
return sc.GetLineEntry();

lldb/tools/lldb-dap/LLDBUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ std::string GetSBFileSpecPath(const lldb::SBFileSpec &file_spec);
183183
/// \return
184184
/// The line entry for the given address.
185185
lldb::SBLineEntry GetLineEntryForAddress(lldb::SBTarget &target,
186-
lldb::SBAddress &address);
186+
const lldb::SBAddress &address);
187187

188188
/// Helper for sending telemetry to lldb server, if client-telemetry is enabled.
189189
class TelemetryDispatcher {

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "Protocol/ProtocolUtils.h"
9+
#include "ProtocolUtils.h"
1010
#include "LLDBUtils.h"
1111

1212
#include "lldb/API/SBDebugger.h"
@@ -106,10 +106,7 @@ bool IsAssemblySource(const protocol::Source &source) {
106106
}
107107

108108
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;
109+
return "0x" + llvm::utohexstr(addr);
113110
}
114111

115112
} // namespace lldb_dap::protocol

lldb/tools/lldb-dap/ProtocolUtils.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//===-- ProtocolUtils.cpp -------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "ProtocolUtils.h"
10+
#include "LLDBUtils.h"
11+
12+
#include "lldb/API/SBDebugger.h"
13+
#include "lldb/API/SBTarget.h"
14+
15+
namespace lldb_dap {
16+
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+
101+
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.
105+
return source.sourceReference.value_or(0) != 0;
106+
}
107+
108+
std::string GetLoadAddressString(const lldb::addr_t addr) {
109+
return "0x" + llvm::utohexstr(addr);
110+
}
111+
112+
} // namespace lldb_dap

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include "lldb/API/SBAddress.h"
1919

20-
namespace lldb_dap::protocol {
20+
namespace lldb_dap {
2121

2222
/// Create a "Source" JSON object as described in the debug adapter definition.
2323
///
@@ -48,6 +48,6 @@ bool IsAssemblySource(const protocol::Source &source);
4848
/// Get the address as a 16-digit hex string, e.g. "0x0000000000012345"
4949
std::string GetLoadAddressString(const lldb::addr_t addr);
5050

51-
} // namespace lldb_dap::protocol
51+
} // namespace lldb_dap
5252

5353
#endif

0 commit comments

Comments
 (0)