1 //===-- PlatformLinux.cpp ---------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/lldb-python.h"
11 
12 #include "PlatformLinux.h"
13 #include "lldb/Host/Config.h"
14 
15 // C Includes
16 #include <stdio.h>
17 #ifndef LLDB_DISABLE_POSIX
18 #include <sys/utsname.h>
19 #endif
20 
21 // C++ Includes
22 // Other libraries and framework includes
23 // Project includes
24 #include "lldb/Breakpoint/BreakpointLocation.h"
25 #include "lldb/Core/Debugger.h"
26 #include "lldb/Core/Error.h"
27 #include "lldb/Core/Log.h"
28 #include "lldb/Core/Module.h"
29 #include "lldb/Core/ModuleList.h"
30 #include "lldb/Core/ModuleSpec.h"
31 #include "lldb/Core/PluginManager.h"
32 #include "lldb/Core/State.h"
33 #include "lldb/Core/StreamString.h"
34 #include "lldb/Host/FileSpec.h"
35 #include "lldb/Host/HostInfo.h"
36 #include "lldb/Interpreter/OptionValueProperties.h"
37 #include "lldb/Interpreter/Property.h"
38 #include "lldb/Target/Target.h"
39 #include "lldb/Target/Process.h"
40 
41 #if defined(__linux__)
42 #include "../../Process/Linux/NativeProcessLinux.h"
43 #endif
44 
45 using namespace lldb;
46 using namespace lldb_private;
47 
48 static uint32_t g_initialize_count = 0;
49 
50 //------------------------------------------------------------------
51 /// Code to handle the PlatformLinux settings
52 //------------------------------------------------------------------
53 
54 namespace
55 {
56     enum
57     {
58         ePropertyUseLlgsForLocal = 0,
59     };
60 
61     const PropertyDefinition*
62     GetStaticPropertyDefinitions ()
63     {
64         static PropertyDefinition
65         g_properties[] =
66         {
67             { "use-llgs-for-local" , OptionValue::eTypeBoolean, true, false, NULL, NULL, "Control whether the platform uses llgs for local debug sessions." },
68             {  NULL        , OptionValue::eTypeInvalid, false, 0  , NULL, NULL, NULL  }
69         };
70 
71         // Allow environment variable to force using llgs-local.
72         if (getenv("PLATFORM_LINUX_FORCE_LLGS_LOCAL"))
73             g_properties[ePropertyUseLlgsForLocal].default_uint_value = true;
74 
75         return g_properties;
76     }
77 }
78 
79 class PlatformLinuxProperties : public Properties
80 {
81 public:
82 
83     static ConstString &
84     GetSettingName ()
85     {
86         static ConstString g_setting_name("linux");
87         return g_setting_name;
88     }
89 
90     PlatformLinuxProperties() :
91     Properties ()
92     {
93         m_collection_sp.reset (new OptionValueProperties(GetSettingName ()));
94         m_collection_sp->Initialize (GetStaticPropertyDefinitions ());
95     }
96 
97     virtual
98     ~PlatformLinuxProperties()
99     {
100     }
101 
102     bool
103     GetUseLlgsForLocal() const
104     {
105         const uint32_t idx = ePropertyUseLlgsForLocal;
106         return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, GetStaticPropertyDefinitions()[idx].default_uint_value != 0);
107     }
108 };
109 
110 typedef std::shared_ptr<PlatformLinuxProperties> PlatformLinuxPropertiesSP;
111 
112 static const PlatformLinuxPropertiesSP &
113 GetGlobalProperties()
114 {
115     static PlatformLinuxPropertiesSP g_settings_sp;
116     if (!g_settings_sp)
117         g_settings_sp.reset (new PlatformLinuxProperties ());
118     return g_settings_sp;
119 }
120 
121 void
122 PlatformLinux::DebuggerInitialize (lldb_private::Debugger &debugger)
123 {
124     if (!PluginManager::GetSettingForPlatformPlugin (debugger, PlatformLinuxProperties::GetSettingName()))
125     {
126         const bool is_global_setting = true;
127         PluginManager::CreateSettingForPlatformPlugin (debugger,
128                                                        GetGlobalProperties()->GetValueProperties(),
129                                                        ConstString ("Properties for the PlatformLinux plug-in."),
130                                                        is_global_setting);
131     }
132 }
133 
134 
135 //------------------------------------------------------------------
136 
137 PlatformSP
138 PlatformLinux::CreateInstance (bool force, const ArchSpec *arch)
139 {
140     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
141     if (log)
142     {
143         const char *arch_name;
144         if (arch && arch->GetArchitectureName ())
145             arch_name = arch->GetArchitectureName ();
146         else
147             arch_name = "<null>";
148 
149         const char *triple_cstr = arch ? arch->GetTriple ().getTriple ().c_str() : "<null>";
150 
151         log->Printf ("PlatformLinux::%s(force=%s, arch={%s,%s})", __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
152     }
153 
154     bool create = force;
155     if (create == false && arch && arch->IsValid())
156     {
157         const llvm::Triple &triple = arch->GetTriple();
158         switch (triple.getVendor())
159         {
160             case llvm::Triple::PC:
161                 create = true;
162                 break;
163 
164 #if defined(__linux__)
165             // Only accept "unknown" for the vendor if the host is linux and
166             // it "unknown" wasn't specified (it was just returned because it
167             // was NOT specified_
168             case llvm::Triple::VendorType::UnknownVendor:
169                 create = !arch->TripleVendorWasSpecified();
170                 break;
171 #endif
172             default:
173                 break;
174         }
175 
176         if (create)
177         {
178             switch (triple.getOS())
179             {
180                 case llvm::Triple::Linux:
181                     break;
182 
183 #if defined(__linux__)
184                 // Only accept "unknown" for the OS if the host is linux and
185                 // it "unknown" wasn't specified (it was just returned because it
186                 // was NOT specified)
187                 case llvm::Triple::OSType::UnknownOS:
188                     create = !arch->TripleOSWasSpecified();
189                     break;
190 #endif
191                 default:
192                     create = false;
193                     break;
194             }
195         }
196     }
197 
198     if (create)
199     {
200         if (log)
201             log->Printf ("PlatformLinux::%s() creating remote-linux platform", __FUNCTION__);
202         return PlatformSP(new PlatformLinux(false));
203     }
204 
205     if (log)
206         log->Printf ("PlatformLinux::%s() aborting creation of remote-linux platform", __FUNCTION__);
207 
208     return PlatformSP();
209 }
210 
211 
212 lldb_private::ConstString
213 PlatformLinux::GetPluginNameStatic (bool is_host)
214 {
215     if (is_host)
216     {
217         static ConstString g_host_name(Platform::GetHostPlatformName ());
218         return g_host_name;
219     }
220     else
221     {
222         static ConstString g_remote_name("remote-linux");
223         return g_remote_name;
224     }
225 }
226 
227 const char *
228 PlatformLinux::GetPluginDescriptionStatic (bool is_host)
229 {
230     if (is_host)
231         return "Local Linux user platform plug-in.";
232     else
233         return "Remote Linux user platform plug-in.";
234 }
235 
236 lldb_private::ConstString
237 PlatformLinux::GetPluginName()
238 {
239     return GetPluginNameStatic(IsHost());
240 }
241 
242 void
243 PlatformLinux::Initialize ()
244 {
245     if (g_initialize_count++ == 0)
246     {
247 #if defined(__linux__)
248         PlatformSP default_platform_sp (new PlatformLinux(true));
249         default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
250         Platform::SetHostPlatform (default_platform_sp);
251 #endif
252         PluginManager::RegisterPlugin(PlatformLinux::GetPluginNameStatic(false),
253                                       PlatformLinux::GetPluginDescriptionStatic(false),
254                                       PlatformLinux::CreateInstance,
255                                       PlatformLinux::DebuggerInitialize);
256     }
257 }
258 
259 void
260 PlatformLinux::Terminate ()
261 {
262     if (g_initialize_count > 0)
263     {
264         if (--g_initialize_count == 0)
265         {
266             PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance);
267         }
268     }
269 }
270 
271 Error
272 PlatformLinux::ResolveExecutable (const FileSpec &exe_file,
273                                   const ArchSpec &exe_arch,
274                                   lldb::ModuleSP &exe_module_sp,
275                                   const FileSpecList *module_search_paths_ptr)
276 {
277     Error error;
278     // Nothing special to do here, just use the actual file and architecture
279 
280     char exe_path[PATH_MAX];
281     FileSpec resolved_exe_file (exe_file);
282 
283     if (IsHost())
284     {
285         // If we have "ls" as the exe_file, resolve the executable location based on
286         // the current path variables
287         if (!resolved_exe_file.Exists())
288         {
289             exe_file.GetPath(exe_path, sizeof(exe_path));
290             resolved_exe_file.SetFile(exe_path, true);
291         }
292 
293         if (!resolved_exe_file.Exists())
294             resolved_exe_file.ResolveExecutableLocation ();
295 
296         if (resolved_exe_file.Exists())
297             error.Clear();
298         else
299         {
300             exe_file.GetPath(exe_path, sizeof(exe_path));
301             error.SetErrorStringWithFormat("unable to find executable for '%s'", exe_path);
302         }
303     }
304     else
305     {
306         if (m_remote_platform_sp)
307         {
308             error = m_remote_platform_sp->ResolveExecutable (exe_file,
309                                                              exe_arch,
310                                                              exe_module_sp,
311                                                              NULL);
312         }
313         else
314         {
315             // We may connect to a process and use the provided executable (Don't use local $PATH).
316 
317             if (resolved_exe_file.Exists())
318                 error.Clear();
319             else
320                 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path);
321         }
322     }
323 
324     if (error.Success())
325     {
326         ModuleSpec module_spec (resolved_exe_file, exe_arch);
327         if (exe_arch.IsValid())
328         {
329             error = ModuleList::GetSharedModule (module_spec,
330                                                  exe_module_sp,
331                                                  NULL,
332                                                  NULL,
333                                                  NULL);
334             if (error.Fail())
335             {
336                 // If we failed, it may be because the vendor and os aren't known. If that is the
337                 // case, try setting them to the host architecture and give it another try.
338                 llvm::Triple &module_triple = module_spec.GetArchitecture().GetTriple();
339                 bool is_vendor_specified = (module_triple.getVendor() != llvm::Triple::UnknownVendor);
340                 bool is_os_specified = (module_triple.getOS() != llvm::Triple::UnknownOS);
341                 if (!is_vendor_specified || !is_os_specified)
342                 {
343                     const llvm::Triple &host_triple = HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple();
344 
345                     if (!is_vendor_specified)
346                         module_triple.setVendorName (host_triple.getVendorName());
347                     if (!is_os_specified)
348                         module_triple.setOSName (host_triple.getOSName());
349 
350                     error = ModuleList::GetSharedModule (module_spec,
351                                                          exe_module_sp,
352                                                          NULL,
353                                                          NULL,
354                                                          NULL);
355                 }
356             }
357 
358             // TODO find out why exe_module_sp might be NULL
359             if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL)
360             {
361                 exe_module_sp.reset();
362                 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s",
363                                                 exe_file.GetPath().c_str(),
364                                                 exe_arch.GetArchitectureName());
365             }
366         }
367         else
368         {
369             // No valid architecture was specified, ask the platform for
370             // the architectures that we should be using (in the correct order)
371             // and see if we can find a match that way
372             StreamString arch_names;
373             for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, module_spec.GetArchitecture()); ++idx)
374             {
375                 error = ModuleList::GetSharedModule (module_spec,
376                                                      exe_module_sp,
377                                                      NULL,
378                                                      NULL,
379                                                      NULL);
380                 // Did we find an executable using one of the
381                 if (error.Success())
382                 {
383                     if (exe_module_sp && exe_module_sp->GetObjectFile())
384                         break;
385                     else
386                         error.SetErrorToGenericError();
387                 }
388 
389                 if (idx > 0)
390                     arch_names.PutCString (", ");
391                 arch_names.PutCString (module_spec.GetArchitecture().GetArchitectureName());
392             }
393 
394             if (error.Fail() || !exe_module_sp)
395             {
396                 if (exe_file.Readable())
397                 {
398                     error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
399                                                     exe_file.GetPath().c_str(),
400                                                     GetPluginName().GetCString(),
401                                                     arch_names.GetString().c_str());
402                 }
403                 else
404                 {
405                     error.SetErrorStringWithFormat("'%s' is not readable", exe_file.GetPath().c_str());
406                 }
407             }
408         }
409     }
410 
411     return error;
412 }
413 
414 Error
415 PlatformLinux::GetFileWithUUID (const FileSpec &platform_file,
416                                 const UUID *uuid_ptr, FileSpec &local_file)
417 {
418     if (IsRemote())
419     {
420         if (m_remote_platform_sp)
421             return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file);
422     }
423 
424     // Default to the local case
425     local_file = platform_file;
426     return Error();
427 }
428 
429 
430 //------------------------------------------------------------------
431 /// Default Constructor
432 //------------------------------------------------------------------
433 PlatformLinux::PlatformLinux (bool is_host) :
434     PlatformPOSIX(is_host)  // This is the local host platform
435 {
436 }
437 
438 //------------------------------------------------------------------
439 /// Destructor.
440 ///
441 /// The destructor is virtual since this class is designed to be
442 /// inherited from by the plug-in instance.
443 //------------------------------------------------------------------
444 PlatformLinux::~PlatformLinux()
445 {
446 }
447 
448 bool
449 PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
450 {
451     bool success = false;
452     if (IsHost())
453     {
454         success = Platform::GetProcessInfo (pid, process_info);
455     }
456     else
457     {
458         if (m_remote_platform_sp)
459             success = m_remote_platform_sp->GetProcessInfo (pid, process_info);
460     }
461     return success;
462 }
463 
464 bool
465 PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
466 {
467     if (idx == 0)
468     {
469         arch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
470         return arch.IsValid();
471     }
472     else if (idx == 1)
473     {
474         // If the default host architecture is 64-bit, look for a 32-bit variant
475         ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
476         if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit())
477         {
478             arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
479             return arch.IsValid();
480         }
481     }
482     return false;
483 }
484 
485 void
486 PlatformLinux::GetStatus (Stream &strm)
487 {
488     Platform::GetStatus(strm);
489 
490 #ifndef LLDB_DISABLE_POSIX
491     struct utsname un;
492 
493     if (uname(&un))
494         return;
495 
496     strm.Printf ("    Kernel: %s\n", un.sysname);
497     strm.Printf ("   Release: %s\n", un.release);
498     strm.Printf ("   Version: %s\n", un.version);
499 #endif
500 }
501 
502 size_t
503 PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target,
504                                                 BreakpointSite *bp_site)
505 {
506     ArchSpec arch = target.GetArchitecture();
507     const uint8_t *trap_opcode = NULL;
508     size_t trap_opcode_size = 0;
509 
510     switch (arch.GetMachine())
511     {
512     default:
513         assert(false && "CPU type not supported!");
514         break;
515 
516     case llvm::Triple::aarch64:
517         {
518             static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 };
519             trap_opcode = g_aarch64_opcode;
520             trap_opcode_size = sizeof(g_aarch64_opcode);
521         }
522         break;
523     case llvm::Triple::x86:
524     case llvm::Triple::x86_64:
525         {
526             static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
527             trap_opcode = g_i386_breakpoint_opcode;
528             trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
529         }
530         break;
531     case llvm::Triple::hexagon:
532         {
533             static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 };
534             trap_opcode = g_hex_opcode;
535             trap_opcode_size = sizeof(g_hex_opcode);
536         }
537         break;
538     case llvm::Triple::arm:
539         {
540             // The ARM reference recommends the use of 0xe7fddefe and 0xdefe
541             // but the linux kernel does otherwise.
542             static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 };
543             static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde };
544 
545             lldb::BreakpointLocationSP bp_loc_sp (bp_site->GetOwnerAtIndex (0));
546             AddressClass addr_class = eAddressClassUnknown;
547 
548             if (bp_loc_sp)
549                 addr_class = bp_loc_sp->GetAddress ().GetAddressClass ();
550 
551             if (addr_class == eAddressClassCodeAlternateISA
552                 || (addr_class == eAddressClassUnknown
553                     && bp_loc_sp->GetAddress().GetOffset() & 1))
554             {
555                 trap_opcode = g_thumb_breakpoint_opcode;
556                 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
557             }
558             else
559             {
560                 trap_opcode = g_arm_breakpoint_opcode;
561                 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
562             }
563         }
564         break;
565     }
566 
567     if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
568         return trap_opcode_size;
569     return 0;
570 }
571 
572 int32_t
573 PlatformLinux::GetResumeCountForLaunchInfo (ProcessLaunchInfo &launch_info)
574 {
575     int32_t resume_count = 0;
576 
577     // Always resume past the initial stop when we use eLaunchFlagDebug
578     if (launch_info.GetFlags ().Test (eLaunchFlagDebug))
579     {
580         // Resume past the stop for the final exec into the true inferior.
581         ++resume_count;
582     }
583 
584     // If we're not launching a shell, we're done.
585     const FileSpec &shell = launch_info.GetShell();
586     if (!shell)
587         return resume_count;
588 
589     std::string shell_string = shell.GetPath();
590     // We're in a shell, so for sure we have to resume past the shell exec.
591     ++resume_count;
592 
593     // Figure out what shell we're planning on using.
594     const char *shell_name = strrchr (shell_string.c_str(), '/');
595     if (shell_name == NULL)
596         shell_name = shell_string.c_str();
597     else
598         shell_name++;
599 
600     if (strcmp (shell_name, "csh") == 0
601              || strcmp (shell_name, "tcsh") == 0
602              || strcmp (shell_name, "zsh") == 0
603              || strcmp (shell_name, "sh") == 0)
604     {
605         // These shells seem to re-exec themselves.  Add another resume.
606         ++resume_count;
607     }
608 
609     return resume_count;
610 }
611 
612 bool
613 PlatformLinux::UseLlgsForLocalDebugging ()
614 {
615     PlatformLinuxPropertiesSP properties_sp = GetGlobalProperties ();
616     assert (properties_sp && "global properties shared pointer is null");
617     return properties_sp ? properties_sp->GetUseLlgsForLocal () : false;
618 }
619 
620 bool
621 PlatformLinux::CanDebugProcess ()
622 {
623     if (IsHost ())
624     {
625         // The platform only does local debugging (i.e. uses llgs) when the setting indicates we do that.
626         // Otherwise, we'll use ProcessLinux/ProcessPOSIX to handle with ProcessMonitor.
627         return UseLlgsForLocalDebugging ();
628     }
629     else
630     {
631         // If we're connected, we can debug.
632         return IsConnected ();
633     }
634 }
635 
636 // For local debugging, Linux will override the debug logic to use llgs-launch rather than
637 // lldb-launch, llgs-attach.  This differs from current lldb-launch, debugserver-attach
638 // approach on MacOSX.
639 lldb::ProcessSP
640 PlatformLinux::DebugProcess (ProcessLaunchInfo &launch_info,
641                         Debugger &debugger,
642                         Target *target,       // Can be NULL, if NULL create a new target, else use existing one
643                         Listener &listener,
644                         Error &error)
645 {
646     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
647     if (log)
648         log->Printf ("PlatformLinux::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target));
649 
650     // If we're a remote host, use standard behavior from parent class.
651     if (!IsHost ())
652         return PlatformPOSIX::DebugProcess (launch_info, debugger, target, listener, error);
653 
654     //
655     // For local debugging, we'll insist on having ProcessGDBRemote create the process.
656     //
657 
658     ProcessSP process_sp;
659 
660     // Ensure we're using llgs for local debugging.
661     if (!UseLlgsForLocalDebugging ())
662     {
663         assert (false && "we're trying to debug a local process but platform.plugin.linux.use-llgs-for-local is false, should never get here");
664         error.SetErrorString ("attempted to start gdb-remote-based debugging for local process but platform.plugin.linux.use-llgs-for-local is false");
665         return process_sp;
666     }
667 
668     // Make sure we stop at the entry point
669     launch_info.GetFlags ().Set (eLaunchFlagDebug);
670 
671     // We always launch the process we are going to debug in a separate process
672     // group, since then we can handle ^C interrupts ourselves w/o having to worry
673     // about the target getting them as well.
674     launch_info.SetLaunchInSeparateProcessGroup(true);
675 
676     // Ensure we have a target.
677     if (target == nullptr)
678     {
679         if (log)
680             log->Printf ("PlatformLinux::%s creating new target", __FUNCTION__);
681 
682         TargetSP new_target_sp;
683         error = debugger.GetTargetList().CreateTarget (debugger,
684                                                        nullptr,
685                                                        nullptr,
686                                                        false,
687                                                        nullptr,
688                                                        new_target_sp);
689         if (error.Fail ())
690         {
691             if (log)
692                 log->Printf ("PlatformLinux::%s failed to create new target: %s", __FUNCTION__, error.AsCString ());
693             return process_sp;
694         }
695 
696         target = new_target_sp.get();
697         if (!target)
698         {
699             error.SetErrorString ("CreateTarget() returned nullptr");
700             if (log)
701                 log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ());
702             return process_sp;
703         }
704     }
705     else
706     {
707         if (log)
708             log->Printf ("PlatformLinux::%s using provided target", __FUNCTION__);
709     }
710 
711     // Mark target as currently selected target.
712     debugger.GetTargetList().SetSelectedTarget(target);
713 
714     // Now create the gdb-remote process.
715     if (log)
716         log->Printf ("PlatformLinux::%s having target create process with gdb-remote plugin", __FUNCTION__);
717     process_sp = target->CreateProcess (listener, "gdb-remote", nullptr);
718 
719     if (!process_sp)
720     {
721         error.SetErrorString ("CreateProcess() failed for gdb-remote process");
722         if (log)
723             log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ());
724         return process_sp;
725     }
726     else
727     {
728         if (log)
729             log->Printf ("PlatformLinux::%s successfully created process", __FUNCTION__);
730     }
731 
732     // Set the unix signals properly.
733     process_sp->SetUnixSignals (Host::GetUnixSignals ());
734 
735     // Adjust launch for a hijacker.
736     ListenerSP listener_sp;
737     if (!launch_info.GetHijackListener ())
738     {
739         if (log)
740             log->Printf ("PlatformLinux::%s setting up hijacker", __FUNCTION__);
741 
742         listener_sp.reset (new Listener("lldb.PlatformLinux.DebugProcess.hijack"));
743         launch_info.SetHijackListener (listener_sp);
744         process_sp->HijackProcessEvents (listener_sp.get ());
745     }
746 
747     // Log file actions.
748     if (log)
749     {
750         log->Printf ("PlatformLinux::%s launching process with the following file actions:", __FUNCTION__);
751 
752         StreamString stream;
753         size_t i = 0;
754         const FileAction *file_action;
755         while ((file_action = launch_info.GetFileActionAtIndex (i++)) != nullptr)
756         {
757             file_action->Dump (stream);
758             log->PutCString (stream.GetString().c_str ());
759             stream.Clear();
760         }
761     }
762 
763     // Do the launch.
764     error = process_sp->Launch(launch_info);
765     if (error.Success ())
766     {
767         // Handle the hijacking of process events.
768         if (listener_sp)
769         {
770             const StateType state = process_sp->WaitForProcessToStop (NULL, NULL, false, listener_sp.get());
771             process_sp->RestoreProcessEvents();
772 
773             if (state == eStateStopped)
774             {
775                 if (log)
776                     log->Printf ("PlatformLinux::%s pid %" PRIu64 " state %s\n",
777                                  __FUNCTION__, process_sp->GetID (), StateAsCString (state));
778             }
779             else
780             {
781                 if (log)
782                     log->Printf ("PlatformLinux::%s pid %" PRIu64 " state is not stopped - %s\n",
783                                  __FUNCTION__, process_sp->GetID (), StateAsCString (state));
784             }
785         }
786 
787         // Hook up process PTY if we have one (which we should for local debugging with llgs).
788         int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
789         if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
790         {
791             process_sp->SetSTDIOFileDescriptor(pty_fd);
792             if (log)
793                 log->Printf ("PlatformLinux::%s pid %" PRIu64 " hooked up STDIO pty to process", __FUNCTION__, process_sp->GetID ());
794         }
795         else
796         {
797             if (log)
798                 log->Printf ("PlatformLinux::%s pid %" PRIu64 " not using process STDIO pty", __FUNCTION__, process_sp->GetID ());
799         }
800     }
801     else
802     {
803         if (log)
804             log->Printf ("PlatformLinux::%s process launch failed: %s", __FUNCTION__, error.AsCString ());
805         // FIXME figure out appropriate cleanup here.  Do we delete the target? Do we delete the process?  Does our caller do that?
806     }
807 
808     return process_sp;
809 }
810 
811 void
812 PlatformLinux::CalculateTrapHandlerSymbolNames ()
813 {
814     m_trap_handlers.push_back (ConstString ("_sigtramp"));
815 }
816 
817 Error
818 PlatformLinux::LaunchNativeProcess (
819     ProcessLaunchInfo &launch_info,
820     lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
821     NativeProcessProtocolSP &process_sp)
822 {
823 #if !defined(__linux__)
824     return Error("only implemented on Linux hosts");
825 #else
826     if (!IsHost ())
827         return Error("PlatformLinux::%s (): cannot launch a debug process when not the host", __FUNCTION__);
828 
829     // Retrieve the exe module.
830     lldb::ModuleSP exe_module_sp;
831 
832     Error error = ResolveExecutable (
833         launch_info.GetExecutableFile (),
834         launch_info.GetArchitecture (),
835         exe_module_sp,
836         NULL);
837 
838     if (!error.Success ())
839         return error;
840 
841     if (!exe_module_sp)
842         return Error("exe_module_sp could not be resolved for %s", launch_info.GetExecutableFile ().GetPath ().c_str ());
843 
844     // Launch it for debugging
845     error = NativeProcessLinux::LaunchProcess (
846         exe_module_sp.get (),
847         launch_info,
848         native_delegate,
849         process_sp);
850 
851     return error;
852 #endif
853 }
854 
855 Error
856 PlatformLinux::AttachNativeProcess (lldb::pid_t pid,
857                                     lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
858                                     NativeProcessProtocolSP &process_sp)
859 {
860 #if !defined(__linux__)
861     return Error("only implemented on Linux hosts");
862 #else
863     if (!IsHost ())
864         return Error("PlatformLinux::%s (): cannot attach to a debug process when not the host", __FUNCTION__);
865 
866     // Launch it for debugging
867     return NativeProcessLinux::AttachToProcess (pid, native_delegate, process_sp);
868 #endif
869 }
870