Skip to content

Commit bc6cd82

Browse files
authored
[lldb-dap] Creating a common configuration structure for launch and attach requests. (#133960)
This moves all the common settings of the launch and attach operations into the `lldb_dap::protocol::Configuration`. These common settings can be in both `launch` and `attach` requests and allows us to isolate the DAP configuration operations into a single common location. This is split out from #133624.
1 parent 73e8d67 commit bc6cd82

15 files changed

+156
-91
lines changed

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,20 @@ const char DEV_NULL[] = "/dev/null";
6969

7070
namespace lldb_dap {
7171

72-
DAP::DAP(llvm::StringRef path, Log *log, const ReplMode default_repl_mode,
72+
llvm::StringRef DAP::debug_adapter_path = "";
73+
74+
DAP::DAP(Log *log, const ReplMode default_repl_mode,
7375
std::vector<std::string> pre_init_commands, Transport &transport)
74-
: debug_adapter_path(path), log(log), transport(transport),
75-
broadcaster("lldb-dap"), exception_breakpoints(),
76-
pre_init_commands(std::move(pre_init_commands)),
77-
focus_tid(LLDB_INVALID_THREAD_ID), stop_at_entry(false), is_attach(false),
78-
enable_auto_variable_summaries(false),
79-
enable_synthetic_child_debugging(false),
80-
display_extended_backtrace(false),
76+
: log(log), transport(transport), broadcaster("lldb-dap"),
77+
exception_breakpoints(), focus_tid(LLDB_INVALID_THREAD_ID),
78+
stop_at_entry(false), is_attach(false),
8179
restarting_process_id(LLDB_INVALID_PROCESS_ID),
8280
configuration_done_sent(false), waiting_for_run_in_terminal(false),
8381
progress_event_reporter(
8482
[&](const ProgressEvent &event) { SendJSON(event.ToJSON()); }),
85-
reverse_request_seq(0), repl_mode(default_repl_mode) {}
83+
reverse_request_seq(0), repl_mode(default_repl_mode) {
84+
configuration.preInitCommands = std::move(pre_init_commands);
85+
}
8686

8787
DAP::~DAP() = default;
8888

@@ -505,8 +505,9 @@ ReplMode DAP::DetectReplMode(lldb::SBFrame frame, std::string &expression,
505505
bool partial_expression) {
506506
// Check for the escape hatch prefix.
507507
if (!expression.empty() &&
508-
llvm::StringRef(expression).starts_with(command_escape_prefix)) {
509-
expression = expression.substr(command_escape_prefix.size());
508+
llvm::StringRef(expression)
509+
.starts_with(configuration.commandEscapePrefix)) {
510+
expression = expression.substr(configuration.commandEscapePrefix.size());
510511
return ReplMode::Command;
511512
}
512513

@@ -546,7 +547,7 @@ ReplMode DAP::DetectReplMode(lldb::SBFrame frame, std::string &expression,
546547
<< "Warning: Expression '" << term
547548
<< "' is both an LLDB command and variable. It will be evaluated as "
548549
"a variable. To evaluate the expression as an LLDB command, use '"
549-
<< command_escape_prefix << "' as a prefix.\n";
550+
<< configuration.commandEscapePrefix << "' as a prefix.\n";
550551
}
551552

552553
// Variables take preference to commands in auto, since commands can always
@@ -593,36 +594,38 @@ DAP::RunLaunchCommands(llvm::ArrayRef<std::string> launch_commands) {
593594
}
594595

595596
llvm::Error DAP::RunInitCommands() {
596-
if (!RunLLDBCommands("Running initCommands:", init_commands))
597+
if (!RunLLDBCommands("Running initCommands:", configuration.initCommands))
597598
return createRunLLDBCommandsErrorMessage("initCommands");
598599
return llvm::Error::success();
599600
}
600601

601602
llvm::Error DAP::RunPreInitCommands() {
602-
if (!RunLLDBCommands("Running preInitCommands:", pre_init_commands))
603+
if (!RunLLDBCommands("Running preInitCommands:",
604+
configuration.preInitCommands))
603605
return createRunLLDBCommandsErrorMessage("preInitCommands");
604606
return llvm::Error::success();
605607
}
606608

607609
llvm::Error DAP::RunPreRunCommands() {
608-
if (!RunLLDBCommands("Running preRunCommands:", pre_run_commands))
610+
if (!RunLLDBCommands("Running preRunCommands:", configuration.preRunCommands))
609611
return createRunLLDBCommandsErrorMessage("preRunCommands");
610612
return llvm::Error::success();
611613
}
612614

613615
void DAP::RunPostRunCommands() {
614-
RunLLDBCommands("Running postRunCommands:", post_run_commands);
616+
RunLLDBCommands("Running postRunCommands:", configuration.postRunCommands);
615617
}
616618
void DAP::RunStopCommands() {
617-
RunLLDBCommands("Running stopCommands:", stop_commands);
619+
RunLLDBCommands("Running stopCommands:", configuration.stopCommands);
618620
}
619621

620622
void DAP::RunExitCommands() {
621-
RunLLDBCommands("Running exitCommands:", exit_commands);
623+
RunLLDBCommands("Running exitCommands:", configuration.exitCommands);
622624
}
623625

624626
void DAP::RunTerminateCommands() {
625-
RunLLDBCommands("Running terminateCommands:", terminate_commands);
627+
RunLLDBCommands("Running terminateCommands:",
628+
configuration.terminateCommands);
626629
}
627630

628631
lldb::SBTarget

lldb/tools/lldb-dap/DAP.h

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "lldb/API/SBThread.h"
3333
#include "lldb/API/SBValue.h"
3434
#include "lldb/API/SBValueList.h"
35-
#include "lldb/lldb-forward.h"
3635
#include "lldb/lldb-types.h"
3736
#include "llvm/ADT/DenseMap.h"
3837
#include "llvm/ADT/DenseSet.h"
@@ -149,12 +148,16 @@ struct SendEventRequestHandler : public lldb::SBCommandPluginInterface {
149148
};
150149

151150
struct DAP {
152-
llvm::StringRef debug_adapter_path;
151+
/// Path to the lldb-dap binary itself.
152+
static llvm::StringRef debug_adapter_path;
153+
153154
Log *log;
154155
Transport &transport;
155156
lldb::SBFile in;
156157
OutputRedirector out;
157158
OutputRedirector err;
159+
/// Configuration specified by the launch or attach commands.
160+
protocol::Configuration configuration;
158161
lldb::SBDebugger debugger;
159162
lldb::SBTarget target;
160163
Variables variables;
@@ -166,13 +169,6 @@ struct DAP {
166169
InstructionBreakpointMap instruction_breakpoints;
167170
std::optional<std::vector<ExceptionBreakpoint>> exception_breakpoints;
168171
llvm::once_flag init_exception_breakpoints_flag;
169-
std::vector<std::string> pre_init_commands;
170-
std::vector<std::string> init_commands;
171-
std::vector<std::string> pre_run_commands;
172-
std::vector<std::string> post_run_commands;
173-
std::vector<std::string> exit_commands;
174-
std::vector<std::string> stop_commands;
175-
std::vector<std::string> terminate_commands;
176172
// Map step in target id to list of function targets that user can choose.
177173
llvm::DenseMap<lldb::addr_t, std::string> step_in_targets;
178174
// A copy of the last LaunchRequest or AttachRequest so we can reuse its
@@ -183,9 +179,6 @@ struct DAP {
183179
llvm::once_flag terminated_event_flag;
184180
bool stop_at_entry;
185181
bool is_attach;
186-
bool enable_auto_variable_summaries;
187-
bool enable_synthetic_child_debugging;
188-
bool display_extended_backtrace;
189182
// The process event thread normally responds to process exited events by
190183
// shutting down the entire adapter. When we're restarting, we keep the id of
191184
// the old process here so we can detect this case and keep running.
@@ -202,7 +195,7 @@ struct DAP {
202195
llvm::SmallDenseMap<int64_t, std::unique_ptr<ResponseHandler>>
203196
inflight_reverse_requests;
204197
ReplMode repl_mode;
205-
std::string command_escape_prefix = "`";
198+
206199
lldb::SBFormat frame_format;
207200
lldb::SBFormat thread_format;
208201
// This is used to allow request_evaluate to handle empty expressions
@@ -216,8 +209,6 @@ struct DAP {
216209

217210
/// Creates a new DAP sessions.
218211
///
219-
/// \param[in] path
220-
/// Path to the lldb-dap binary.
221212
/// \param[in] log
222213
/// Log stream, if configured.
223214
/// \param[in] default_repl_mode
@@ -226,7 +217,7 @@ struct DAP {
226217
/// LLDB commands to execute as soon as the debugger instance is allocaed.
227218
/// \param[in] transport
228219
/// Transport for this debug session.
229-
DAP(llvm::StringRef path, Log *log, const ReplMode default_repl_mode,
220+
DAP(Log *log, const ReplMode default_repl_mode,
230221
std::vector<std::string> pre_init_commands, Transport &transport);
231222

232223
~DAP();

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,29 @@ void AttachRequestHandler::operator()(const llvm::json::Object &request) const {
6363
attach_info.SetProcessID(pid);
6464
const auto wait_for = GetBoolean(arguments, "waitFor").value_or(false);
6565
attach_info.SetWaitForLaunch(wait_for, false /*async*/);
66-
dap.init_commands = GetStrings(arguments, "initCommands");
67-
dap.pre_run_commands = GetStrings(arguments, "preRunCommands");
68-
dap.stop_commands = GetStrings(arguments, "stopCommands");
69-
dap.exit_commands = GetStrings(arguments, "exitCommands");
70-
dap.terminate_commands = GetStrings(arguments, "terminateCommands");
66+
dap.configuration.initCommands = GetStrings(arguments, "initCommands");
67+
dap.configuration.preRunCommands = GetStrings(arguments, "preRunCommands");
68+
dap.configuration.stopCommands = GetStrings(arguments, "stopCommands");
69+
dap.configuration.exitCommands = GetStrings(arguments, "exitCommands");
70+
dap.configuration.terminateCommands =
71+
GetStrings(arguments, "terminateCommands");
7172
auto attachCommands = GetStrings(arguments, "attachCommands");
7273
llvm::StringRef core_file = GetString(arguments, "coreFile").value_or("");
7374
const uint64_t timeout_seconds =
7475
GetInteger<uint64_t>(arguments, "timeout").value_or(30);
7576
dap.stop_at_entry = core_file.empty()
7677
? GetBoolean(arguments, "stopOnEntry").value_or(false)
7778
: true;
78-
dap.post_run_commands = GetStrings(arguments, "postRunCommands");
79+
dap.configuration.postRunCommands = GetStrings(arguments, "postRunCommands");
7980
const llvm::StringRef debuggerRoot =
8081
GetString(arguments, "debuggerRoot").value_or("");
81-
dap.enable_auto_variable_summaries =
82+
dap.configuration.enableAutoVariableSummaries =
8283
GetBoolean(arguments, "enableAutoVariableSummaries").value_or(false);
83-
dap.enable_synthetic_child_debugging =
84+
dap.configuration.enableSyntheticChildDebugging =
8485
GetBoolean(arguments, "enableSyntheticChildDebugging").value_or(false);
85-
dap.display_extended_backtrace =
86+
dap.configuration.displayExtendedBacktrace =
8687
GetBoolean(arguments, "displayExtendedBacktrace").value_or(false);
87-
dap.command_escape_prefix =
88+
dap.configuration.commandEscapePrefix =
8889
GetString(arguments, "commandEscapePrefix").value_or("`");
8990
dap.SetFrameFormat(GetString(arguments, "customFrameFormat").value_or(""));
9091
dap.SetThreadFormat(GetString(arguments, "customThreadFormat").value_or(""));

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,20 @@ void CompletionsRequestHandler::operator()(
157157
llvm::json::Array targets;
158158

159159
bool had_escape_prefix =
160-
llvm::StringRef(text).starts_with(dap.command_escape_prefix);
160+
llvm::StringRef(text).starts_with(dap.configuration.commandEscapePrefix);
161161
ReplMode completion_mode = dap.DetectReplMode(frame, text, true);
162162

163163
// Handle the offset change introduced by stripping out the
164164
// `command_escape_prefix`.
165165
if (had_escape_prefix) {
166-
if (offset < static_cast<int64_t>(dap.command_escape_prefix.size())) {
166+
if (offset <
167+
static_cast<int64_t>(dap.configuration.commandEscapePrefix.size())) {
167168
body.try_emplace("targets", std::move(targets));
168169
response.try_emplace("body", std::move(body));
169170
dap.SendJSON(llvm::json::Value(std::move(response)));
170171
return;
171172
}
172-
offset -= dap.command_escape_prefix.size();
173+
offset -= dap.configuration.commandEscapePrefix.size();
173174
}
174175

175176
// While the user is typing then we likely have an incomplete input and cannot

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ void EvaluateRequestHandler::operator()(
205205
else
206206
EmplaceSafeString(response, "message", "evaluate failed");
207207
} else {
208-
VariableDescription desc(value, dap.enable_auto_variable_summaries);
208+
VariableDescription desc(value,
209+
dap.configuration.enableAutoVariableSummaries);
209210
EmplaceSafeString(body, "result", desc.GetResult(context));
210211
EmplaceSafeString(body, "type", desc.display_type_name);
211212
int64_t var_ref = 0;

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,23 @@ void LaunchRequestHandler::operator()(const llvm::json::Object &request) const {
5454
llvm::json::Object response;
5555
FillResponse(request, response);
5656
const auto *arguments = request.getObject("arguments");
57-
dap.init_commands = GetStrings(arguments, "initCommands");
58-
dap.pre_run_commands = GetStrings(arguments, "preRunCommands");
59-
dap.stop_commands = GetStrings(arguments, "stopCommands");
60-
dap.exit_commands = GetStrings(arguments, "exitCommands");
61-
dap.terminate_commands = GetStrings(arguments, "terminateCommands");
62-
dap.post_run_commands = GetStrings(arguments, "postRunCommands");
57+
dap.configuration.initCommands = GetStrings(arguments, "initCommands");
58+
dap.configuration.preRunCommands = GetStrings(arguments, "preRunCommands");
59+
dap.configuration.stopCommands = GetStrings(arguments, "stopCommands");
60+
dap.configuration.exitCommands = GetStrings(arguments, "exitCommands");
61+
dap.configuration.terminateCommands =
62+
GetStrings(arguments, "terminateCommands");
63+
dap.configuration.postRunCommands = GetStrings(arguments, "postRunCommands");
6364
dap.stop_at_entry = GetBoolean(arguments, "stopOnEntry").value_or(false);
6465
const llvm::StringRef debuggerRoot =
6566
GetString(arguments, "debuggerRoot").value_or("");
66-
dap.enable_auto_variable_summaries =
67+
dap.configuration.enableAutoVariableSummaries =
6768
GetBoolean(arguments, "enableAutoVariableSummaries").value_or(false);
68-
dap.enable_synthetic_child_debugging =
69+
dap.configuration.enableSyntheticChildDebugging =
6970
GetBoolean(arguments, "enableSyntheticChildDebugging").value_or(false);
70-
dap.display_extended_backtrace =
71+
dap.configuration.displayExtendedBacktrace =
7172
GetBoolean(arguments, "displayExtendedBacktrace").value_or(false);
72-
dap.command_escape_prefix =
73+
dap.configuration.commandEscapePrefix =
7374
GetString(arguments, "commandEscapePrefix").value_or("`");
7475
dap.SetFrameFormat(GetString(arguments, "customFrameFormat").value_or(""));
7576
dap.SetThreadFormat(GetString(arguments, "customThreadFormat").value_or(""));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static llvm::Error RunInTerminal(DAP &dap,
119119
debugger_pid = getpid();
120120
#endif
121121
llvm::json::Object reverse_request = CreateRunInTerminalReverseRequest(
122-
launch_request, dap.debug_adapter_path, comm_file.m_path, debugger_pid);
122+
launch_request, comm_file.m_path, debugger_pid);
123123
dap.SendReverseRequest<LogFailureResponseHandler>("runInTerminal",
124124
std::move(reverse_request));
125125

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ void SetVariableRequestHandler::operator()(
145145
lldb::SBError error;
146146
bool success = variable.SetValueFromCString(value.data(), error);
147147
if (success) {
148-
VariableDescription desc(variable, dap.enable_auto_variable_summaries);
148+
VariableDescription desc(variable,
149+
dap.configuration.enableAutoVariableSummaries);
149150
EmplaceSafeString(body, "value", desc.display_value);
150151
EmplaceSafeString(body, "type", desc.display_type_name);
151152

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static bool FillStackFrames(DAP &dap, lldb::SBThread &thread,
7070
stack_frames.emplace_back(CreateStackFrame(frame, dap.frame_format));
7171
}
7272

73-
if (dap.display_extended_backtrace && reached_end_of_stack) {
73+
if (dap.configuration.displayExtendedBacktrace && reached_end_of_stack) {
7474
// Check for any extended backtraces.
7575
for (uint32_t bt = 0;
7676
bt < thread.GetProcess().GetNumExtendedBacktraceTypes(); bt++) {

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ void VariablesRequestHandler::operator()(
180180
return_var_ref = dap.variables.InsertVariable(stop_return_value,
181181
/*is_permanent=*/false);
182182
}
183-
variables.emplace_back(
184-
CreateVariable(renamed_return_value, return_var_ref, hex,
185-
dap.enable_auto_variable_summaries,
186-
dap.enable_synthetic_child_debugging, false));
183+
variables.emplace_back(CreateVariable(
184+
renamed_return_value, return_var_ref, hex,
185+
dap.configuration.enableAutoVariableSummaries,
186+
dap.configuration.enableSyntheticChildDebugging, false));
187187
}
188188
}
189189

@@ -197,8 +197,8 @@ void VariablesRequestHandler::operator()(
197197
int64_t var_ref =
198198
dap.variables.InsertVariable(variable, /*is_permanent=*/false);
199199
variables.emplace_back(CreateVariable(
200-
variable, var_ref, hex, dap.enable_auto_variable_summaries,
201-
dap.enable_synthetic_child_debugging,
200+
variable, var_ref, hex, dap.configuration.enableAutoVariableSummaries,
201+
dap.configuration.enableSyntheticChildDebugging,
202202
variable_name_counts[GetNonNullVariableName(variable)] > 1));
203203
}
204204
} else {
@@ -214,8 +214,8 @@ void VariablesRequestHandler::operator()(
214214
dap.variables.IsPermanentVariableReference(variablesReference);
215215
int64_t var_ref = dap.variables.InsertVariable(child, is_permanent);
216216
variables.emplace_back(CreateVariable(
217-
child, var_ref, hex, dap.enable_auto_variable_summaries,
218-
dap.enable_synthetic_child_debugging,
217+
child, var_ref, hex, dap.configuration.enableAutoVariableSummaries,
218+
dap.configuration.enableSyntheticChildDebugging,
219219
/*is_name_duplicated=*/false, custom_name));
220220
};
221221
const int64_t num_children = variable.GetNumChildren();
@@ -228,8 +228,8 @@ void VariablesRequestHandler::operator()(
228228
// "[raw]" child that can be used to inspect the raw version of a
229229
// synthetic member. That eliminates the need for the user to go to the
230230
// debug console and type `frame var <variable> to get these values.
231-
if (dap.enable_synthetic_child_debugging && variable.IsSynthetic() &&
232-
i == num_children)
231+
if (dap.configuration.enableSyntheticChildDebugging &&
232+
variable.IsSynthetic() && i == num_children)
233233
addChild(variable.GetNonSyntheticValue(), "[raw]");
234234
}
235235
}

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,7 +1400,6 @@ llvm::json::Value CreateCompileUnit(lldb::SBCompileUnit &unit) {
14001400
/// https://microsoft.github.io/debug-adapter-protocol/specification#Reverse_Requests_RunInTerminal
14011401
llvm::json::Object
14021402
CreateRunInTerminalReverseRequest(const llvm::json::Object &launch_request,
1403-
llvm::StringRef debug_adapter_path,
14041403
llvm::StringRef comm_file,
14051404
lldb::pid_t debugger_pid) {
14061405
llvm::json::Object run_in_terminal_args;
@@ -1410,7 +1409,7 @@ CreateRunInTerminalReverseRequest(const llvm::json::Object &launch_request,
14101409

14111410
const auto *launch_request_arguments = launch_request.getObject("arguments");
14121411
// The program path must be the first entry in the "args" field
1413-
std::vector<std::string> args = {debug_adapter_path.str(), "--comm-file",
1412+
std::vector<std::string> args = {DAP::debug_adapter_path.str(), "--comm-file",
14141413
comm_file.str()};
14151414
if (debugger_pid != LLDB_INVALID_PROCESS_ID) {
14161415
args.push_back("--debugger-pid");

lldb/tools/lldb-dap/JSONUtils.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,6 @@ llvm::json::Value CreateCompileUnit(lldb::SBCompileUnit &unit);
565565
/// The original launch_request object whose fields are used to construct
566566
/// the reverse request object.
567567
///
568-
/// \param[in] debug_adapter_path
569-
/// Path to the current debug adapter. It will be used to delegate the
570-
/// launch of the target.
571-
///
572568
/// \param[in] comm_file
573569
/// The fifo file used to communicate the with the target launcher.
574570
///
@@ -582,7 +578,6 @@ llvm::json::Value CreateCompileUnit(lldb::SBCompileUnit &unit);
582578
/// Microsoft.
583579
llvm::json::Object
584580
CreateRunInTerminalReverseRequest(const llvm::json::Object &launch_request,
585-
llvm::StringRef debug_adapter_path,
586581
llvm::StringRef comm_file,
587582
lldb::pid_t debugger_pid);
588583

0 commit comments

Comments
 (0)