Skip to content

Commit 48e34e7

Browse files
committed
Reworked attach and launch to have the same flow for collecting the initial_thread_list and simplifying the number of if branches in the attach request handler.
1 parent 063192b commit 48e34e7

File tree

3 files changed

+42
-46
lines changed

3 files changed

+42
-46
lines changed

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

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -78,34 +78,7 @@ Error AttachRequestHandler::Run(const AttachRequestArguments &args) const {
7878
// Perform the launch in synchronous mode so that we don't have to worry
7979
// about process state changes during the launch.
8080
ScopeSyncMode scope_sync_mode(dap.debugger);
81-
if (args.attachCommands.empty()) {
82-
// No "attachCommands", just attach normally.
83-
if (args.coreFile.empty()) {
84-
if (args.gdbRemotePort != LLDB_DAP_INVALID_PORT) {
85-
// If port is specified and pid is not.
86-
lldb::SBListener listener = dap.debugger.GetListener();
87-
88-
// If the user hasn't provided the hostname property, default
89-
// localhost being used.
90-
std::string connect_url =
91-
llvm::formatv("connect://{0}:", args.gdbRemoteHostname);
92-
connect_url += std::to_string(args.gdbRemotePort);
93-
dap.target.ConnectRemote(listener, connect_url.c_str(), "gdb-remote",
94-
error);
95-
} else {
96-
// Attach by pid or process name.
97-
lldb::SBAttachInfo attach_info;
98-
if (args.pid != LLDB_INVALID_PROCESS_ID)
99-
attach_info.SetProcessID(args.pid);
100-
else if (!dap.configuration.program.empty())
101-
attach_info.SetExecutable(dap.configuration.program.data());
102-
attach_info.SetWaitForLaunch(args.waitFor, false /*async*/);
103-
dap.target.Attach(attach_info, error);
104-
}
105-
} else {
106-
dap.target.LoadCore(args.coreFile.data(), error);
107-
}
108-
} else {
81+
if (!args.attachCommands.empty()) {
10982
// We have "attachCommands" that are a set of commands that are expected
11083
// to execute the commands after which a process should be created. If
11184
// there is no valid process after running these commands, we have failed.
@@ -115,6 +88,28 @@ Error AttachRequestHandler::Run(const AttachRequestArguments &args) const {
11588
// The custom commands might have created a new target so we should use
11689
// the selected target after these commands are run.
11790
dap.target = dap.debugger.GetSelectedTarget();
91+
} else if (!args.coreFile.empty()) {
92+
dap.target.LoadCore(args.coreFile.data(), error);
93+
} else if (args.gdbRemotePort != LLDB_DAP_INVALID_PORT) {
94+
// If port is specified and pid is not.
95+
lldb::SBListener listener = dap.debugger.GetListener();
96+
97+
// If the user hasn't provided the hostname property, default
98+
// localhost being used.
99+
std::string connect_url =
100+
llvm::formatv("connect://{0}:", args.gdbRemoteHostname);
101+
connect_url += std::to_string(args.gdbRemotePort);
102+
dap.target.ConnectRemote(listener, connect_url.c_str(), "gdb-remote",
103+
error);
104+
} else {
105+
// Attach by pid or process name.
106+
lldb::SBAttachInfo attach_info;
107+
if (args.pid != LLDB_INVALID_PROCESS_ID)
108+
attach_info.SetProcessID(args.pid);
109+
else if (!dap.configuration.program.empty())
110+
attach_info.SetExecutable(dap.configuration.program.data());
111+
attach_info.SetWaitForLaunch(args.waitFor, false /*async*/);
112+
dap.target.Attach(attach_info, error);
118113
}
119114
}
120115

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,25 @@ Error LaunchRequestHandler::Run(const LaunchRequestArguments &arguments) const {
6666
}
6767

6868
void LaunchRequestHandler::PostRun() const {
69-
if (dap.target.GetProcess().IsValid()) {
70-
// Attach happens when launching with runInTerminal.
71-
SendProcessEvent(dap, dap.is_attach ? Attach : Launch);
72-
73-
if (dap.stop_at_entry)
74-
SendThreadStoppedEvent(dap);
75-
else
76-
dap.target.GetProcess().Continue();
77-
}
69+
if (!dap.target.GetProcess().IsValid())
70+
return;
71+
72+
// Clients can request a baseline of currently existing threads after
73+
// we acknowledge the configurationDone request.
74+
// Client requests the baseline of currently existing threads after
75+
// a successful or attach by sending a 'threads' request
76+
// right after receiving the configurationDone response.
77+
// Obtain the list of threads before we resume the process
78+
dap.initial_thread_list =
79+
GetThreads(dap.target.GetProcess(), dap.thread_format);
80+
81+
// Attach happens when launching with runInTerminal.
82+
SendProcessEvent(dap, dap.is_attach ? Attach : Launch);
83+
84+
if (dap.stop_at_entry)
85+
SendThreadStoppedEvent(dap);
86+
else
87+
dap.target.GetProcess().Continue();
7888
}
7989

8090
} // namespace lldb_dap

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,6 @@ llvm::Error BaseRequestHandler::LaunchProcess(
220220
if (error.Fail())
221221
return ToError(error);
222222

223-
// Clients can request a baseline of currently existing threads after
224-
// we acknowledge the configurationDone request.
225-
// Client requests the baseline of currently existing threads after
226-
// a successful or attach by sending a 'threads' request
227-
// right after receiving the configurationDone response.
228-
// Obtain the list of threads before we resume the process
229-
dap.initial_thread_list =
230-
GetThreads(dap.target.GetProcess(), dap.thread_format);
231-
232223
return llvm::Error::success();
233224
}
234225

0 commit comments

Comments
 (0)