Skip to content

Commit a825eaa

Browse files
committed
[lldb] [Platform] Move common ::DebugProcess() to PlatformPOSIX
Move common ::DebugProcess() implementation shared by Linux and NetBSD (and to be shared by FreeBSD shortly) into PlatformPOSIX, and move the old base implementation used only by Darwin to PlatformDarwin. Differential Revision: https://reviews.llvm.org/D88852
1 parent 21100f8 commit a825eaa

File tree

7 files changed

+134
-248
lines changed

7 files changed

+134
-248
lines changed

lldb/source/Plugins/Platform/Linux/PlatformLinux.cpp

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -262,121 +262,6 @@ bool PlatformLinux::CanDebugProcess() {
262262
}
263263
}
264264

265-
// For local debugging, Linux will override the debug logic to use llgs-launch
266-
// rather than lldb-launch, llgs-attach. This differs from current lldb-
267-
// launch, debugserver-attach approach on MacOSX.
268-
lldb::ProcessSP
269-
PlatformLinux::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
270-
Target *target, // Can be NULL, if NULL create a new
271-
// target, else use existing one
272-
Status &error) {
273-
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
274-
LLDB_LOG(log, "target {0}", target);
275-
276-
// If we're a remote host, use standard behavior from parent class.
277-
if (!IsHost())
278-
return PlatformPOSIX::DebugProcess(launch_info, debugger, target, error);
279-
280-
//
281-
// For local debugging, we'll insist on having ProcessGDBRemote create the
282-
// process.
283-
//
284-
285-
ProcessSP process_sp;
286-
287-
// Make sure we stop at the entry point
288-
launch_info.GetFlags().Set(eLaunchFlagDebug);
289-
290-
// We always launch the process we are going to debug in a separate process
291-
// group, since then we can handle ^C interrupts ourselves w/o having to
292-
// worry about the target getting them as well.
293-
launch_info.SetLaunchInSeparateProcessGroup(true);
294-
295-
// Ensure we have a target.
296-
if (target == nullptr) {
297-
LLDB_LOG(log, "creating new target");
298-
TargetSP new_target_sp;
299-
error = debugger.GetTargetList().CreateTarget(
300-
debugger, "", "", eLoadDependentsNo, nullptr, new_target_sp);
301-
if (error.Fail()) {
302-
LLDB_LOG(log, "failed to create new target: {0}", error);
303-
return process_sp;
304-
}
305-
306-
target = new_target_sp.get();
307-
if (!target) {
308-
error.SetErrorString("CreateTarget() returned nullptr");
309-
LLDB_LOG(log, "error: {0}", error);
310-
return process_sp;
311-
}
312-
}
313-
314-
// Mark target as currently selected target.
315-
debugger.GetTargetList().SetSelectedTarget(target);
316-
317-
// Now create the gdb-remote process.
318-
LLDB_LOG(log, "having target create process with gdb-remote plugin");
319-
process_sp =
320-
target->CreateProcess(launch_info.GetListener(), "gdb-remote", nullptr);
321-
322-
if (!process_sp) {
323-
error.SetErrorString("CreateProcess() failed for gdb-remote process");
324-
LLDB_LOG(log, "error: {0}", error);
325-
return process_sp;
326-
}
327-
328-
LLDB_LOG(log, "successfully created process");
329-
// Adjust launch for a hijacker.
330-
ListenerSP listener_sp;
331-
if (!launch_info.GetHijackListener()) {
332-
LLDB_LOG(log, "setting up hijacker");
333-
listener_sp =
334-
Listener::MakeListener("lldb.PlatformLinux.DebugProcess.hijack");
335-
launch_info.SetHijackListener(listener_sp);
336-
process_sp->HijackProcessEvents(listener_sp);
337-
}
338-
339-
// Log file actions.
340-
if (log) {
341-
LLDB_LOG(log, "launching process with the following file actions:");
342-
StreamString stream;
343-
size_t i = 0;
344-
const FileAction *file_action;
345-
while ((file_action = launch_info.GetFileActionAtIndex(i++)) != nullptr) {
346-
file_action->Dump(stream);
347-
LLDB_LOG(log, "{0}", stream.GetData());
348-
stream.Clear();
349-
}
350-
}
351-
352-
// Do the launch.
353-
error = process_sp->Launch(launch_info);
354-
if (error.Success()) {
355-
// Handle the hijacking of process events.
356-
if (listener_sp) {
357-
const StateType state = process_sp->WaitForProcessToStop(
358-
llvm::None, nullptr, false, listener_sp);
359-
360-
LLDB_LOG(log, "pid {0} state {0}", process_sp->GetID(), state);
361-
}
362-
363-
// Hook up process PTY if we have one (which we should for local debugging
364-
// with llgs).
365-
int pty_fd = launch_info.GetPTY().ReleasePrimaryFileDescriptor();
366-
if (pty_fd != PseudoTerminal::invalid_fd) {
367-
process_sp->SetSTDIOFileDescriptor(pty_fd);
368-
LLDB_LOG(log, "hooked up STDIO pty to process");
369-
} else
370-
LLDB_LOG(log, "not using process STDIO pty");
371-
} else {
372-
LLDB_LOG(log, "{0}", error);
373-
// FIXME figure out appropriate cleanup here. Do we delete the target? Do
374-
// we delete the process? Does our caller do that?
375-
}
376-
377-
return process_sp;
378-
}
379-
380265
void PlatformLinux::CalculateTrapHandlerSymbolNames() {
381266
m_trap_handlers.push_back(ConstString("_sigtramp"));
382267
m_trap_handlers.push_back(ConstString("__kernel_rt_sigreturn"));

lldb/source/Plugins/Platform/Linux/PlatformLinux.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ class PlatformLinux : public PlatformPOSIX {
4848

4949
bool CanDebugProcess() override;
5050

51-
lldb::ProcessSP DebugProcess(ProcessLaunchInfo &launch_info,
52-
Debugger &debugger, Target *target,
53-
Status &error) override;
54-
5551
void CalculateTrapHandlerSymbolNames() override;
5652

5753
MmapArgList GetMmapArgumentList(const ArchSpec &arch, lldb::addr_t addr,

lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,33 @@ PlatformDarwin::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
12261226
return 1;
12271227
}
12281228

1229+
lldb::ProcessSP
1230+
PlatformDarwin::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
1231+
Target *target, // Can be NULL, if NULL create
1232+
// a new target, else use existing
1233+
// one
1234+
Status &error) {
1235+
ProcessSP process_sp;
1236+
1237+
if (IsHost()) {
1238+
// We are going to hand this process off to debugserver which will be in
1239+
// charge of setting the exit status. However, we still need to reap it
1240+
// from lldb. So, make sure we use a exit callback which does not set exit
1241+
// status.
1242+
const bool monitor_signals = false;
1243+
launch_info.SetMonitorProcessCallback(
1244+
&ProcessLaunchInfo::NoOpMonitorCallback, monitor_signals);
1245+
process_sp = Platform::DebugProcess(launch_info, debugger, target, error);
1246+
} else {
1247+
if (m_remote_platform_sp)
1248+
process_sp = m_remote_platform_sp->DebugProcess(launch_info, debugger,
1249+
target, error);
1250+
else
1251+
error.SetErrorString("the platform is not currently connected");
1252+
}
1253+
return process_sp;
1254+
}
1255+
12291256
void PlatformDarwin::CalculateTrapHandlerSymbolNames() {
12301257
m_trap_handlers.push_back(ConstString("_sigtramp"));
12311258
}

lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "Plugins/Platform/POSIX/PlatformPOSIX.h"
1313
#include "lldb/Host/FileSystem.h"
14+
#include "lldb/Host/ProcessLaunchInfo.h"
1415
#include "lldb/Utility/ConstString.h"
1516
#include "lldb/Utility/FileSpec.h"
1617
#include "lldb/Utility/StructuredData.h"
@@ -68,6 +69,11 @@ class PlatformDarwin : public PlatformPOSIX {
6869
int32_t GetResumeCountForLaunchInfo(
6970
lldb_private::ProcessLaunchInfo &launch_info) override;
7071

72+
lldb::ProcessSP DebugProcess(lldb_private::ProcessLaunchInfo &launch_info,
73+
lldb_private::Debugger &debugger,
74+
lldb_private::Target *target,
75+
lldb_private::Status &error) override;
76+
7177
void CalculateTrapHandlerSymbolNames() override;
7278

7379
llvm::VersionTuple

lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -231,121 +231,6 @@ bool PlatformNetBSD::CanDebugProcess() {
231231
}
232232
}
233233

234-
// For local debugging, NetBSD will override the debug logic to use llgs-launch
235-
// rather than lldb-launch, llgs-attach. This differs from current lldb-
236-
// launch, debugserver-attach approach on MacOSX.
237-
lldb::ProcessSP
238-
PlatformNetBSD::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
239-
Target *target, // Can be NULL, if NULL create a new
240-
// target, else use existing one
241-
Status &error) {
242-
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
243-
LLDB_LOG(log, "target {0}", target);
244-
245-
// If we're a remote host, use standard behavior from parent class.
246-
if (!IsHost())
247-
return PlatformPOSIX::DebugProcess(launch_info, debugger, target, error);
248-
249-
//
250-
// For local debugging, we'll insist on having ProcessGDBRemote create the
251-
// process.
252-
//
253-
254-
ProcessSP process_sp;
255-
256-
// Make sure we stop at the entry point
257-
launch_info.GetFlags().Set(eLaunchFlagDebug);
258-
259-
// We always launch the process we are going to debug in a separate process
260-
// group, since then we can handle ^C interrupts ourselves w/o having to
261-
// worry about the target getting them as well.
262-
launch_info.SetLaunchInSeparateProcessGroup(true);
263-
264-
// Ensure we have a target.
265-
if (target == nullptr) {
266-
LLDB_LOG(log, "creating new target");
267-
TargetSP new_target_sp;
268-
error = debugger.GetTargetList().CreateTarget(
269-
debugger, "", "", eLoadDependentsNo, nullptr, new_target_sp);
270-
if (error.Fail()) {
271-
LLDB_LOG(log, "failed to create new target: {0}", error);
272-
return process_sp;
273-
}
274-
275-
target = new_target_sp.get();
276-
if (!target) {
277-
error.SetErrorString("CreateTarget() returned nullptr");
278-
LLDB_LOG(log, "error: {0}", error);
279-
return process_sp;
280-
}
281-
}
282-
283-
// Mark target as currently selected target.
284-
debugger.GetTargetList().SetSelectedTarget(target);
285-
286-
// Now create the gdb-remote process.
287-
LLDB_LOG(log, "having target create process with gdb-remote plugin");
288-
process_sp =
289-
target->CreateProcess(launch_info.GetListener(), "gdb-remote", nullptr);
290-
291-
if (!process_sp) {
292-
error.SetErrorString("CreateProcess() failed for gdb-remote process");
293-
LLDB_LOG(log, "error: {0}", error);
294-
return process_sp;
295-
}
296-
297-
LLDB_LOG(log, "successfully created process");
298-
// Adjust launch for a hijacker.
299-
ListenerSP listener_sp;
300-
if (!launch_info.GetHijackListener()) {
301-
LLDB_LOG(log, "setting up hijacker");
302-
listener_sp =
303-
Listener::MakeListener("lldb.PlatformNetBSD.DebugProcess.hijack");
304-
launch_info.SetHijackListener(listener_sp);
305-
process_sp->HijackProcessEvents(listener_sp);
306-
}
307-
308-
// Log file actions.
309-
if (log) {
310-
LLDB_LOG(log, "launching process with the following file actions:");
311-
StreamString stream;
312-
size_t i = 0;
313-
const FileAction *file_action;
314-
while ((file_action = launch_info.GetFileActionAtIndex(i++)) != nullptr) {
315-
file_action->Dump(stream);
316-
LLDB_LOG(log, "{0}", stream.GetData());
317-
stream.Clear();
318-
}
319-
}
320-
321-
// Do the launch.
322-
error = process_sp->Launch(launch_info);
323-
if (error.Success()) {
324-
// Handle the hijacking of process events.
325-
if (listener_sp) {
326-
const StateType state = process_sp->WaitForProcessToStop(
327-
llvm::None, nullptr, false, listener_sp);
328-
329-
LLDB_LOG(log, "pid {0} state {0}", process_sp->GetID(), state);
330-
}
331-
332-
// Hook up process PTY if we have one (which we should for local debugging
333-
// with llgs).
334-
int pty_fd = launch_info.GetPTY().ReleasePrimaryFileDescriptor();
335-
if (pty_fd != PseudoTerminal::invalid_fd) {
336-
process_sp->SetSTDIOFileDescriptor(pty_fd);
337-
LLDB_LOG(log, "hooked up STDIO pty to process");
338-
} else
339-
LLDB_LOG(log, "not using process STDIO pty");
340-
} else {
341-
LLDB_LOG(log, "{0}", error);
342-
// FIXME figure out appropriate cleanup here. Do we delete the target? Do
343-
// we delete the process? Does our caller do that?
344-
}
345-
346-
return process_sp;
347-
}
348-
349234
void PlatformNetBSD::CalculateTrapHandlerSymbolNames() {
350235
m_trap_handlers.push_back(ConstString("_sigtramp"));
351236
}

lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ class PlatformNetBSD : public PlatformPOSIX {
4848

4949
bool CanDebugProcess() override;
5050

51-
lldb::ProcessSP DebugProcess(ProcessLaunchInfo &launch_info,
52-
Debugger &debugger, Target *target,
53-
Status &error) override;
54-
5551
void CalculateTrapHandlerSymbolNames() override;
5652

5753
MmapArgList GetMmapArgumentList(const ArchSpec &arch, lldb::addr_t addr,

0 commit comments

Comments
 (0)