1e996fd30SGreg Clayton //===-- PlatformLinux.cpp ---------------------------------------*- C++ -*-===//
2e996fd30SGreg Clayton //
3e996fd30SGreg Clayton //                     The LLVM Compiler Infrastructure
4e996fd30SGreg Clayton //
5e996fd30SGreg Clayton // This file is distributed under the University of Illinois Open Source
6e996fd30SGreg Clayton // License. See LICENSE.TXT for details.
7e996fd30SGreg Clayton //
8e996fd30SGreg Clayton //===----------------------------------------------------------------------===//
9e996fd30SGreg Clayton 
1093a64300SDaniel Malea #include "lldb/lldb-python.h"
1193a64300SDaniel Malea 
12e996fd30SGreg Clayton #include "PlatformLinux.h"
13b2f1fb29SVirgile Bello #include "lldb/Host/Config.h"
14e996fd30SGreg Clayton 
15e996fd30SGreg Clayton // C Includes
16ecc11474SStephen Wilson #include <stdio.h>
17b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX
18ecc11474SStephen Wilson #include <sys/utsname.h>
19b2f1fb29SVirgile Bello #endif
20ecc11474SStephen Wilson 
21e996fd30SGreg Clayton // C++ Includes
22e996fd30SGreg Clayton // Other libraries and framework includes
23e996fd30SGreg Clayton // Project includes
242586e94bSJason Molenda #include "lldb/Breakpoint/BreakpointLocation.h"
2528041352SGreg Clayton #include "lldb/Core/Debugger.h"
26348fb385STodd Fiala #include "lldb/Core/Error.h"
27015d818bSTodd Fiala #include "lldb/Core/Log.h"
28e996fd30SGreg Clayton #include "lldb/Core/Module.h"
29e996fd30SGreg Clayton #include "lldb/Core/ModuleList.h"
301f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h"
31ecc11474SStephen Wilson #include "lldb/Core/PluginManager.h"
32348fb385STodd Fiala #include "lldb/Core/State.h"
33e996fd30SGreg Clayton #include "lldb/Core/StreamString.h"
34e996fd30SGreg Clayton #include "lldb/Host/FileSpec.h"
3513b18261SZachary Turner #include "lldb/Host/HostInfo.h"
36348fb385STodd Fiala #include "lldb/Interpreter/OptionValueProperties.h"
37348fb385STodd Fiala #include "lldb/Interpreter/Property.h"
38ecc11474SStephen Wilson #include "lldb/Target/Target.h"
39e996fd30SGreg Clayton #include "lldb/Target/Process.h"
40e996fd30SGreg Clayton 
41af245d11STodd Fiala #if defined(__linux__)
42af245d11STodd Fiala #include "../../Process/Linux/NativeProcessLinux.h"
43af245d11STodd Fiala #endif
44af245d11STodd Fiala 
45e996fd30SGreg Clayton using namespace lldb;
46e996fd30SGreg Clayton using namespace lldb_private;
47db264a6dSTamas Berghammer using namespace lldb_private::platform_linux;
48e996fd30SGreg Clayton 
4928041352SGreg Clayton static uint32_t g_initialize_count = 0;
5028041352SGreg Clayton 
51281961a8STodd Fiala //------------------------------------------------------------------
52281961a8STodd Fiala /// Code to handle the PlatformLinux settings
53281961a8STodd Fiala //------------------------------------------------------------------
54281961a8STodd Fiala 
55348fb385STodd Fiala namespace
56348fb385STodd Fiala {
57348fb385STodd Fiala     enum
58348fb385STodd Fiala     {
59348fb385STodd Fiala         ePropertyUseLlgsForLocal = 0,
60348fb385STodd Fiala     };
61348fb385STodd Fiala 
62db264a6dSTamas Berghammer     class PlatformLinuxProperties : public Properties
63db264a6dSTamas Berghammer     {
64db264a6dSTamas Berghammer     public:
65db264a6dSTamas Berghammer         static ConstString&
66db264a6dSTamas Berghammer         GetSettingName ();
67db264a6dSTamas Berghammer 
68db264a6dSTamas Berghammer         PlatformLinuxProperties();
69db264a6dSTamas Berghammer 
70db264a6dSTamas Berghammer         virtual
71db264a6dSTamas Berghammer         ~PlatformLinuxProperties() = default;
72db264a6dSTamas Berghammer 
73db264a6dSTamas Berghammer         bool
74db264a6dSTamas Berghammer         GetUseLlgsForLocal() const;
75db264a6dSTamas Berghammer 
76db264a6dSTamas Berghammer     private:
77db264a6dSTamas Berghammer         static const PropertyDefinition*
78db264a6dSTamas Berghammer         GetStaticPropertyDefinitions();
79db264a6dSTamas Berghammer     };
80db264a6dSTamas Berghammer 
81db264a6dSTamas Berghammer     typedef std::shared_ptr<PlatformLinuxProperties> PlatformLinuxPropertiesSP;
82db264a6dSTamas Berghammer 
83db264a6dSTamas Berghammer } // anonymous namespace
84db264a6dSTamas Berghammer 
85db264a6dSTamas Berghammer PlatformLinuxProperties::PlatformLinuxProperties() :
86db264a6dSTamas Berghammer     Properties ()
87db264a6dSTamas Berghammer {
88db264a6dSTamas Berghammer     m_collection_sp.reset (new OptionValueProperties(GetSettingName ()));
89db264a6dSTamas Berghammer     m_collection_sp->Initialize (GetStaticPropertyDefinitions ());
90db264a6dSTamas Berghammer }
91db264a6dSTamas Berghammer 
92db264a6dSTamas Berghammer ConstString&
93db264a6dSTamas Berghammer PlatformLinuxProperties::GetSettingName ()
94db264a6dSTamas Berghammer {
95db264a6dSTamas Berghammer     static ConstString g_setting_name("linux");
96db264a6dSTamas Berghammer     return g_setting_name;
97db264a6dSTamas Berghammer }
98db264a6dSTamas Berghammer 
99db264a6dSTamas Berghammer bool
100db264a6dSTamas Berghammer PlatformLinuxProperties::GetUseLlgsForLocal() const
101db264a6dSTamas Berghammer {
102db264a6dSTamas Berghammer     const uint32_t idx = ePropertyUseLlgsForLocal;
103db264a6dSTamas Berghammer     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, GetStaticPropertyDefinitions()[idx].default_uint_value != 0);
104db264a6dSTamas Berghammer }
105db264a6dSTamas Berghammer 
106348fb385STodd Fiala const PropertyDefinition*
107db264a6dSTamas Berghammer PlatformLinuxProperties::GetStaticPropertyDefinitions()
108348fb385STodd Fiala {
109281961a8STodd Fiala     static PropertyDefinition
110281961a8STodd Fiala     g_properties[] =
111281961a8STodd Fiala     {
112bc477ddeSVince Harron         { "use-llgs-for-local" , OptionValue::eTypeBoolean, true, true, NULL, NULL, "Control whether the platform uses llgs for local debug sessions." },
113281961a8STodd Fiala         {  NULL        , OptionValue::eTypeInvalid, false, 0  , NULL, NULL, NULL  }
114281961a8STodd Fiala     };
115281961a8STodd Fiala 
116bc477ddeSVince Harron     // Allow environment variable to disable llgs-local.
117bc477ddeSVince Harron     if (getenv("PLATFORM_LINUX_DISABLE_LLGS_LOCAL"))
118bc477ddeSVince Harron         g_properties[ePropertyUseLlgsForLocal].default_uint_value = false;
119281961a8STodd Fiala 
120348fb385STodd Fiala     return g_properties;
121348fb385STodd Fiala }
122281961a8STodd Fiala 
123281961a8STodd Fiala static const PlatformLinuxPropertiesSP &
124281961a8STodd Fiala GetGlobalProperties()
125281961a8STodd Fiala {
126281961a8STodd Fiala     static PlatformLinuxPropertiesSP g_settings_sp;
127281961a8STodd Fiala     if (!g_settings_sp)
128281961a8STodd Fiala         g_settings_sp.reset (new PlatformLinuxProperties ());
129281961a8STodd Fiala     return g_settings_sp;
130281961a8STodd Fiala }
131281961a8STodd Fiala 
132281961a8STodd Fiala void
133db264a6dSTamas Berghammer PlatformLinux::DebuggerInitialize (Debugger &debugger)
134281961a8STodd Fiala {
135281961a8STodd Fiala     if (!PluginManager::GetSettingForPlatformPlugin (debugger, PlatformLinuxProperties::GetSettingName()))
136281961a8STodd Fiala     {
137281961a8STodd Fiala         const bool is_global_setting = true;
138281961a8STodd Fiala         PluginManager::CreateSettingForPlatformPlugin (debugger,
139281961a8STodd Fiala                                                        GetGlobalProperties()->GetValueProperties(),
140281961a8STodd Fiala                                                        ConstString ("Properties for the PlatformLinux plug-in."),
141281961a8STodd Fiala                                                        is_global_setting);
142281961a8STodd Fiala     }
143281961a8STodd Fiala }
144281961a8STodd Fiala 
145281961a8STodd Fiala 
146281961a8STodd Fiala //------------------------------------------------------------------
147281961a8STodd Fiala 
148615eb7e6SGreg Clayton PlatformSP
149b3a40ba8SGreg Clayton PlatformLinux::CreateInstance (bool force, const ArchSpec *arch)
150ecc11474SStephen Wilson {
151db264a6dSTamas Berghammer     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
152015d818bSTodd Fiala     if (log)
153015d818bSTodd Fiala     {
154015d818bSTodd Fiala         const char *arch_name;
155015d818bSTodd Fiala         if (arch && arch->GetArchitectureName ())
156015d818bSTodd Fiala             arch_name = arch->GetArchitectureName ();
157015d818bSTodd Fiala         else
158015d818bSTodd Fiala             arch_name = "<null>";
159015d818bSTodd Fiala 
160015d818bSTodd Fiala         const char *triple_cstr = arch ? arch->GetTriple ().getTriple ().c_str() : "<null>";
161015d818bSTodd Fiala 
162015d818bSTodd Fiala         log->Printf ("PlatformLinux::%s(force=%s, arch={%s,%s})", __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
163015d818bSTodd Fiala     }
164015d818bSTodd Fiala 
165b3a40ba8SGreg Clayton     bool create = force;
166b3a40ba8SGreg Clayton     if (create == false && arch && arch->IsValid())
167b3a40ba8SGreg Clayton     {
168b3a40ba8SGreg Clayton         const llvm::Triple &triple = arch->GetTriple();
16970512317SGreg Clayton         switch (triple.getVendor())
17070512317SGreg Clayton         {
17170512317SGreg Clayton             case llvm::Triple::PC:
172b3a40ba8SGreg Clayton                 create = true;
17370512317SGreg Clayton                 break;
17470512317SGreg Clayton 
175dbc6c0bbSGreg Clayton #if defined(__linux__)
176dbc6c0bbSGreg Clayton             // Only accept "unknown" for the vendor if the host is linux and
1776a7f3338SBruce Mitchener             // it "unknown" wasn't specified (it was just returned because it
178dbc6c0bbSGreg Clayton             // was NOT specified_
179015d818bSTodd Fiala             case llvm::Triple::VendorType::UnknownVendor:
18070512317SGreg Clayton                 create = !arch->TripleVendorWasSpecified();
18170512317SGreg Clayton                 break;
182dbc6c0bbSGreg Clayton #endif
18370512317SGreg Clayton             default:
18470512317SGreg Clayton                 break;
18570512317SGreg Clayton         }
18670512317SGreg Clayton 
18770512317SGreg Clayton         if (create)
18870512317SGreg Clayton         {
18970512317SGreg Clayton             switch (triple.getOS())
19070512317SGreg Clayton             {
19170512317SGreg Clayton                 case llvm::Triple::Linux:
19270512317SGreg Clayton                     break;
19370512317SGreg Clayton 
194dbc6c0bbSGreg Clayton #if defined(__linux__)
195dbc6c0bbSGreg Clayton                 // Only accept "unknown" for the OS if the host is linux and
1966a7f3338SBruce Mitchener                 // it "unknown" wasn't specified (it was just returned because it
197dbc6c0bbSGreg Clayton                 // was NOT specified)
198015d818bSTodd Fiala                 case llvm::Triple::OSType::UnknownOS:
19970512317SGreg Clayton                     create = !arch->TripleOSWasSpecified();
20070512317SGreg Clayton                     break;
201dbc6c0bbSGreg Clayton #endif
20270512317SGreg Clayton                 default:
20370512317SGreg Clayton                     create = false;
20470512317SGreg Clayton                     break;
20570512317SGreg Clayton             }
20670512317SGreg Clayton         }
207b3a40ba8SGreg Clayton     }
208015d818bSTodd Fiala 
209b3a40ba8SGreg Clayton     if (create)
210015d818bSTodd Fiala     {
211015d818bSTodd Fiala         if (log)
212015d818bSTodd Fiala             log->Printf ("PlatformLinux::%s() creating remote-linux platform", __FUNCTION__);
213615eb7e6SGreg Clayton         return PlatformSP(new PlatformLinux(false));
214015d818bSTodd Fiala     }
215015d818bSTodd Fiala 
216015d818bSTodd Fiala     if (log)
217015d818bSTodd Fiala         log->Printf ("PlatformLinux::%s() aborting creation of remote-linux platform", __FUNCTION__);
218015d818bSTodd Fiala 
219615eb7e6SGreg Clayton     return PlatformSP();
220ecc11474SStephen Wilson }
221ecc11474SStephen Wilson 
222ecc11474SStephen Wilson 
223db264a6dSTamas Berghammer ConstString
22457abc5d6SGreg Clayton PlatformLinux::GetPluginNameStatic (bool is_host)
225ecc11474SStephen Wilson {
22628041352SGreg Clayton     if (is_host)
22757abc5d6SGreg Clayton     {
22857abc5d6SGreg Clayton         static ConstString g_host_name(Platform::GetHostPlatformName ());
22957abc5d6SGreg Clayton         return g_host_name;
23057abc5d6SGreg Clayton     }
23128041352SGreg Clayton     else
23257abc5d6SGreg Clayton     {
23357abc5d6SGreg Clayton         static ConstString g_remote_name("remote-linux");
23457abc5d6SGreg Clayton         return g_remote_name;
23557abc5d6SGreg Clayton     }
23628041352SGreg Clayton }
23728041352SGreg Clayton 
23828041352SGreg Clayton const char *
23928041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic (bool is_host)
24028041352SGreg Clayton {
24128041352SGreg Clayton     if (is_host)
24228041352SGreg Clayton         return "Local Linux user platform plug-in.";
24328041352SGreg Clayton     else
24428041352SGreg Clayton         return "Remote Linux user platform plug-in.";
245ecc11474SStephen Wilson }
246ecc11474SStephen Wilson 
247db264a6dSTamas Berghammer ConstString
24857abc5d6SGreg Clayton PlatformLinux::GetPluginName()
24957abc5d6SGreg Clayton {
25057abc5d6SGreg Clayton     return GetPluginNameStatic(IsHost());
25157abc5d6SGreg Clayton }
25257abc5d6SGreg Clayton 
253e996fd30SGreg Clayton void
254e996fd30SGreg Clayton PlatformLinux::Initialize ()
255e996fd30SGreg Clayton {
2563c4f89d7STamas Berghammer     PlatformPOSIX::Initialize ();
2573c4f89d7STamas Berghammer 
25828041352SGreg Clayton     if (g_initialize_count++ == 0)
259ecc11474SStephen Wilson     {
2601c6a1ea9STamas Berghammer #if defined(__linux__) && !defined(__ANDROID__)
26128041352SGreg Clayton         PlatformSP default_platform_sp (new PlatformLinux(true));
26213b18261SZachary Turner         default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
263615eb7e6SGreg Clayton         Platform::SetHostPlatform (default_platform_sp);
26428041352SGreg Clayton #endif
26557abc5d6SGreg Clayton         PluginManager::RegisterPlugin(PlatformLinux::GetPluginNameStatic(false),
26628041352SGreg Clayton                                       PlatformLinux::GetPluginDescriptionStatic(false),
267281961a8STodd Fiala                                       PlatformLinux::CreateInstance,
268281961a8STodd Fiala                                       PlatformLinux::DebuggerInitialize);
269ecc11474SStephen Wilson     }
270e996fd30SGreg Clayton }
271e996fd30SGreg Clayton 
272e996fd30SGreg Clayton void
273e996fd30SGreg Clayton PlatformLinux::Terminate ()
274e996fd30SGreg Clayton {
27528041352SGreg Clayton     if (g_initialize_count > 0)
27628041352SGreg Clayton     {
27728041352SGreg Clayton         if (--g_initialize_count == 0)
27828041352SGreg Clayton         {
27928041352SGreg Clayton             PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance);
280e996fd30SGreg Clayton         }
28128041352SGreg Clayton     }
2823c4f89d7STamas Berghammer 
2833c4f89d7STamas Berghammer     PlatformPOSIX::Terminate ();
28428041352SGreg Clayton }
285e996fd30SGreg Clayton 
286e996fd30SGreg Clayton Error
2878012cadbSGreg Clayton PlatformLinux::ResolveExecutable (const ModuleSpec &ms,
288ea5e0cc3SGreg Clayton                                   lldb::ModuleSP &exe_module_sp,
289ea5e0cc3SGreg Clayton                                   const FileSpecList *module_search_paths_ptr)
290e996fd30SGreg Clayton {
291e996fd30SGreg Clayton     Error error;
292e996fd30SGreg Clayton     // Nothing special to do here, just use the actual file and architecture
293e996fd30SGreg Clayton 
29428041352SGreg Clayton     char exe_path[PATH_MAX];
2958012cadbSGreg Clayton     ModuleSpec resolved_module_spec (ms);
296e996fd30SGreg Clayton 
29728041352SGreg Clayton     if (IsHost())
29828041352SGreg Clayton     {
29928041352SGreg Clayton         // If we have "ls" as the exe_file, resolve the executable location based on
300e996fd30SGreg Clayton         // the current path variables
3018012cadbSGreg Clayton         if (!resolved_module_spec.GetFileSpec().Exists())
30228041352SGreg Clayton         {
3038012cadbSGreg Clayton             resolved_module_spec.GetFileSpec().GetPath(exe_path, sizeof(exe_path));
3048012cadbSGreg Clayton             resolved_module_spec.GetFileSpec().SetFile(exe_path, true);
30528041352SGreg Clayton         }
30628041352SGreg Clayton 
3078012cadbSGreg Clayton         if (!resolved_module_spec.GetFileSpec().Exists())
3088012cadbSGreg Clayton             resolved_module_spec.GetFileSpec().ResolveExecutableLocation ();
309e996fd30SGreg Clayton 
3108012cadbSGreg Clayton         if (resolved_module_spec.GetFileSpec().Exists())
31128041352SGreg Clayton             error.Clear();
31228041352SGreg Clayton         else
31328041352SGreg Clayton         {
3148012cadbSGreg Clayton             error.SetErrorStringWithFormat("unable to find executable for '%s'", resolved_module_spec.GetFileSpec().GetPath().c_str());
31528041352SGreg Clayton         }
31628041352SGreg Clayton     }
31728041352SGreg Clayton     else
31828041352SGreg Clayton     {
31928041352SGreg Clayton         if (m_remote_platform_sp)
32028041352SGreg Clayton         {
3216474721cSOleksiy Vyalov             error = GetCachedExecutable (resolved_module_spec, exe_module_sp, nullptr, *m_remote_platform_sp);
32228041352SGreg Clayton         }
32328041352SGreg Clayton         else
32428041352SGreg Clayton         {
32528041352SGreg Clayton             // We may connect to a process and use the provided executable (Don't use local $PATH).
326e996fd30SGreg Clayton 
3278012cadbSGreg Clayton             if (resolved_module_spec.GetFileSpec().Exists())
32828041352SGreg Clayton                 error.Clear();
32928041352SGreg Clayton             else
33028041352SGreg Clayton                 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path);
33128041352SGreg Clayton         }
33228041352SGreg Clayton     }
33328041352SGreg Clayton 
33428041352SGreg Clayton     if (error.Success())
335e996fd30SGreg Clayton     {
3368012cadbSGreg Clayton         if (resolved_module_spec.GetArchitecture().IsValid())
337e996fd30SGreg Clayton         {
3388012cadbSGreg Clayton             error = ModuleList::GetSharedModule (resolved_module_spec,
339e996fd30SGreg Clayton                                                  exe_module_sp,
340e996fd30SGreg Clayton                                                  NULL,
3410c90ef47SGreg Clayton                                                  NULL,
342e996fd30SGreg Clayton                                                  NULL);
3439f0013d8SMichael Sartain             if (error.Fail())
3449f0013d8SMichael Sartain             {
3459f0013d8SMichael Sartain                 // If we failed, it may be because the vendor and os aren't known. If that is the
3469f0013d8SMichael Sartain                 // case, try setting them to the host architecture and give it another try.
3478012cadbSGreg Clayton                 llvm::Triple &module_triple = resolved_module_spec.GetArchitecture().GetTriple();
3489f0013d8SMichael Sartain                 bool is_vendor_specified = (module_triple.getVendor() != llvm::Triple::UnknownVendor);
3499f0013d8SMichael Sartain                 bool is_os_specified = (module_triple.getOS() != llvm::Triple::UnknownOS);
3509f0013d8SMichael Sartain                 if (!is_vendor_specified || !is_os_specified)
3519f0013d8SMichael Sartain                 {
35213b18261SZachary Turner                     const llvm::Triple &host_triple = HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple();
3539f0013d8SMichael Sartain 
3549f0013d8SMichael Sartain                     if (!is_vendor_specified)
3559f0013d8SMichael Sartain                         module_triple.setVendorName (host_triple.getVendorName());
3569f0013d8SMichael Sartain                     if (!is_os_specified)
3579f0013d8SMichael Sartain                         module_triple.setOSName (host_triple.getOSName());
3589f0013d8SMichael Sartain 
3598012cadbSGreg Clayton                     error = ModuleList::GetSharedModule (resolved_module_spec,
3609f0013d8SMichael Sartain                                                          exe_module_sp,
3619f0013d8SMichael Sartain                                                          NULL,
3629f0013d8SMichael Sartain                                                          NULL,
3639f0013d8SMichael Sartain                                                          NULL);
3649f0013d8SMichael Sartain                 }
3659f0013d8SMichael Sartain             }
366e996fd30SGreg Clayton 
367e635db49SSean Callanan             // TODO find out why exe_module_sp might be NULL
368e635db49SSean Callanan             if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL)
369e996fd30SGreg Clayton             {
370e996fd30SGreg Clayton                 exe_module_sp.reset();
371b5ad4ec7SGreg Clayton                 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s",
3728012cadbSGreg Clayton                                                 resolved_module_spec.GetFileSpec().GetPath().c_str(),
3738012cadbSGreg Clayton                                                 resolved_module_spec.GetArchitecture().GetArchitectureName());
374e996fd30SGreg Clayton             }
375e996fd30SGreg Clayton         }
376e996fd30SGreg Clayton         else
377e996fd30SGreg Clayton         {
378e996fd30SGreg Clayton             // No valid architecture was specified, ask the platform for
379e996fd30SGreg Clayton             // the architectures that we should be using (in the correct order)
380e996fd30SGreg Clayton             // and see if we can find a match that way
381e996fd30SGreg Clayton             StreamString arch_names;
3828012cadbSGreg Clayton             for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, resolved_module_spec.GetArchitecture()); ++idx)
383e996fd30SGreg Clayton             {
3848012cadbSGreg Clayton                 error = ModuleList::GetSharedModule (resolved_module_spec,
385e996fd30SGreg Clayton                                                      exe_module_sp,
386e996fd30SGreg Clayton                                                      NULL,
3870c90ef47SGreg Clayton                                                      NULL,
388e996fd30SGreg Clayton                                                      NULL);
389e996fd30SGreg Clayton                 // Did we find an executable using one of the
390e996fd30SGreg Clayton                 if (error.Success())
391e996fd30SGreg Clayton                 {
392e996fd30SGreg Clayton                     if (exe_module_sp && exe_module_sp->GetObjectFile())
393e996fd30SGreg Clayton                         break;
394e996fd30SGreg Clayton                     else
395e996fd30SGreg Clayton                         error.SetErrorToGenericError();
396e996fd30SGreg Clayton                 }
397e996fd30SGreg Clayton 
398e996fd30SGreg Clayton                 if (idx > 0)
399e996fd30SGreg Clayton                     arch_names.PutCString (", ");
4008012cadbSGreg Clayton                 arch_names.PutCString (resolved_module_spec.GetArchitecture().GetArchitectureName());
401e996fd30SGreg Clayton             }
402e996fd30SGreg Clayton 
403e996fd30SGreg Clayton             if (error.Fail() || !exe_module_sp)
404e996fd30SGreg Clayton             {
4058012cadbSGreg Clayton                 if (resolved_module_spec.GetFileSpec().Readable())
40639945dccSGreg Clayton                 {
407b5ad4ec7SGreg Clayton                     error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
4088012cadbSGreg Clayton                                                     resolved_module_spec.GetFileSpec().GetPath().c_str(),
40957abc5d6SGreg Clayton                                                     GetPluginName().GetCString(),
410e996fd30SGreg Clayton                                                     arch_names.GetString().c_str());
411e996fd30SGreg Clayton                 }
41239945dccSGreg Clayton                 else
41339945dccSGreg Clayton                 {
4148012cadbSGreg Clayton                     error.SetErrorStringWithFormat("'%s' is not readable", resolved_module_spec.GetFileSpec().GetPath().c_str());
41539945dccSGreg Clayton                 }
41639945dccSGreg Clayton             }
417e996fd30SGreg Clayton         }
418e996fd30SGreg Clayton     }
419e996fd30SGreg Clayton 
420e996fd30SGreg Clayton     return error;
421e996fd30SGreg Clayton }
422e996fd30SGreg Clayton 
423e996fd30SGreg Clayton Error
424fc995725SSteve Pucci PlatformLinux::GetFileWithUUID (const FileSpec &platform_file,
42528041352SGreg Clayton                                 const UUID *uuid_ptr, FileSpec &local_file)
426e996fd30SGreg Clayton {
42728041352SGreg Clayton     if (IsRemote())
42828041352SGreg Clayton     {
42928041352SGreg Clayton         if (m_remote_platform_sp)
430fc995725SSteve Pucci             return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file);
43128041352SGreg Clayton     }
43228041352SGreg Clayton 
433e996fd30SGreg Clayton     // Default to the local case
434e996fd30SGreg Clayton     local_file = platform_file;
435e996fd30SGreg Clayton     return Error();
436e996fd30SGreg Clayton }
437e996fd30SGreg Clayton 
438e996fd30SGreg Clayton 
439e996fd30SGreg Clayton //------------------------------------------------------------------
440e996fd30SGreg Clayton /// Default Constructor
441e996fd30SGreg Clayton //------------------------------------------------------------------
44228041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) :
443015d818bSTodd Fiala     PlatformPOSIX(is_host)  // This is the local host platform
444e996fd30SGreg Clayton {
445e996fd30SGreg Clayton }
446e996fd30SGreg Clayton 
447e996fd30SGreg Clayton //------------------------------------------------------------------
448e996fd30SGreg Clayton /// Destructor.
449e996fd30SGreg Clayton ///
450e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be
451e996fd30SGreg Clayton /// inherited from by the plug-in instance.
452e996fd30SGreg Clayton //------------------------------------------------------------------
453e996fd30SGreg Clayton PlatformLinux::~PlatformLinux()
454e996fd30SGreg Clayton {
455e996fd30SGreg Clayton }
456e996fd30SGreg Clayton 
457e996fd30SGreg Clayton bool
45813e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
459e996fd30SGreg Clayton {
46028041352SGreg Clayton     bool success = false;
46128041352SGreg Clayton     if (IsHost())
46228041352SGreg Clayton     {
46328041352SGreg Clayton         success = Platform::GetProcessInfo (pid, process_info);
46428041352SGreg Clayton     }
46528041352SGreg Clayton     else
46628041352SGreg Clayton     {
46728041352SGreg Clayton         if (m_remote_platform_sp)
46828041352SGreg Clayton             success = m_remote_platform_sp->GetProcessInfo (pid, process_info);
46928041352SGreg Clayton     }
47028041352SGreg Clayton     return success;
471e996fd30SGreg Clayton }
472e996fd30SGreg Clayton 
4738e6ec453SStephane Sezer uint32_t
4748e6ec453SStephane Sezer PlatformLinux::FindProcesses (const ProcessInstanceInfoMatch &match_info,
4758e6ec453SStephane Sezer                               ProcessInstanceInfoList &process_infos)
4768e6ec453SStephane Sezer {
4778e6ec453SStephane Sezer     uint32_t match_count = 0;
4788e6ec453SStephane Sezer     if (IsHost())
4798e6ec453SStephane Sezer     {
4808e6ec453SStephane Sezer         // Let the base class figure out the host details
4818e6ec453SStephane Sezer         match_count = Platform::FindProcesses (match_info, process_infos);
4828e6ec453SStephane Sezer     }
4838e6ec453SStephane Sezer     else
4848e6ec453SStephane Sezer     {
4858e6ec453SStephane Sezer         // If we are remote, we can only return results if we are connected
4868e6ec453SStephane Sezer         if (m_remote_platform_sp)
4878e6ec453SStephane Sezer             match_count = m_remote_platform_sp->FindProcesses (match_info, process_infos);
4888e6ec453SStephane Sezer     }
4898e6ec453SStephane Sezer     return match_count;
4908e6ec453SStephane Sezer }
4918e6ec453SStephane Sezer 
492e996fd30SGreg Clayton bool
493e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
494e996fd30SGreg Clayton {
495e996fd30SGreg Clayton     if (idx == 0)
496e996fd30SGreg Clayton     {
49713b18261SZachary Turner         arch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
498e996fd30SGreg Clayton         return arch.IsValid();
499e996fd30SGreg Clayton     }
500542e4075SGreg Clayton     else if (idx == 1)
501542e4075SGreg Clayton     {
502542e4075SGreg Clayton         // If the default host architecture is 64-bit, look for a 32-bit variant
50313b18261SZachary Turner         ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
504542e4075SGreg Clayton         if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit())
505542e4075SGreg Clayton         {
50613b18261SZachary Turner             arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
507542e4075SGreg Clayton             return arch.IsValid();
508542e4075SGreg Clayton         }
509542e4075SGreg Clayton     }
510e996fd30SGreg Clayton     return false;
511e996fd30SGreg Clayton }
512ecc11474SStephen Wilson 
513ecc11474SStephen Wilson void
514ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm)
515ecc11474SStephen Wilson {
5163be69dacSDaniel Malea     Platform::GetStatus(strm);
517ecc11474SStephen Wilson 
518b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX
519b743a803SStephane Sezer     // Display local kernel information only when we are running in host mode.
520b743a803SStephane Sezer     // Otherwise, we would end up printing non-Linux information (when running
521b743a803SStephane Sezer     // on Mac OS for example).
522b743a803SStephane Sezer     if (IsHost())
523b743a803SStephane Sezer     {
524b2f1fb29SVirgile Bello         struct utsname un;
525b2f1fb29SVirgile Bello 
5263be69dacSDaniel Malea         if (uname(&un))
5273be69dacSDaniel Malea             return;
5283be69dacSDaniel Malea 
5293be69dacSDaniel Malea         strm.Printf ("    Kernel: %s\n", un.sysname);
5303be69dacSDaniel Malea         strm.Printf ("   Release: %s\n", un.release);
5313be69dacSDaniel Malea         strm.Printf ("   Version: %s\n", un.version);
532b743a803SStephane Sezer     }
533b2f1fb29SVirgile Bello #endif
534ecc11474SStephen Wilson }
535ecc11474SStephen Wilson 
536ecc11474SStephen Wilson size_t
537ecc11474SStephen Wilson PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target,
538ecc11474SStephen Wilson                                                 BreakpointSite *bp_site)
539ecc11474SStephen Wilson {
540ecc11474SStephen Wilson     ArchSpec arch = target.GetArchitecture();
54128041352SGreg Clayton     const uint8_t *trap_opcode = NULL;
54228041352SGreg Clayton     size_t trap_opcode_size = 0;
543ecc11474SStephen Wilson 
544906e9acfSGreg Clayton     switch (arch.GetMachine())
545ecc11474SStephen Wilson     {
546ecc11474SStephen Wilson     default:
547ecc11474SStephen Wilson         assert(false && "CPU type not supported!");
548ecc11474SStephen Wilson         break;
549ecc11474SStephen Wilson 
5502afc5966STodd Fiala     case llvm::Triple::aarch64:
5512afc5966STodd Fiala         {
5522afc5966STodd Fiala             static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 };
5532afc5966STodd Fiala             trap_opcode = g_aarch64_opcode;
5542afc5966STodd Fiala             trap_opcode_size = sizeof(g_aarch64_opcode);
5552afc5966STodd Fiala         }
5562afc5966STodd Fiala         break;
557906e9acfSGreg Clayton     case llvm::Triple::x86:
558906e9acfSGreg Clayton     case llvm::Triple::x86_64:
55928041352SGreg Clayton         {
56028041352SGreg Clayton             static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
56128041352SGreg Clayton             trap_opcode = g_i386_breakpoint_opcode;
56228041352SGreg Clayton             trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
56328041352SGreg Clayton         }
564ecc11474SStephen Wilson         break;
565906e9acfSGreg Clayton     case llvm::Triple::hexagon:
5668006d319SDeepak Panickal         {
5678006d319SDeepak Panickal             static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 };
5688006d319SDeepak Panickal             trap_opcode = g_hex_opcode;
5698006d319SDeepak Panickal             trap_opcode_size = sizeof(g_hex_opcode);
5708006d319SDeepak Panickal         }
5718006d319SDeepak Panickal         break;
5722586e94bSJason Molenda     case llvm::Triple::arm:
5732586e94bSJason Molenda         {
5742586e94bSJason Molenda             // The ARM reference recommends the use of 0xe7fddefe and 0xdefe
5752586e94bSJason Molenda             // but the linux kernel does otherwise.
5762586e94bSJason Molenda             static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 };
5772586e94bSJason Molenda             static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde };
5782586e94bSJason Molenda 
5792586e94bSJason Molenda             lldb::BreakpointLocationSP bp_loc_sp (bp_site->GetOwnerAtIndex (0));
5802586e94bSJason Molenda             AddressClass addr_class = eAddressClassUnknown;
5812586e94bSJason Molenda 
5822586e94bSJason Molenda             if (bp_loc_sp)
5832586e94bSJason Molenda                 addr_class = bp_loc_sp->GetAddress ().GetAddressClass ();
5842586e94bSJason Molenda 
5852586e94bSJason Molenda             if (addr_class == eAddressClassCodeAlternateISA
5862586e94bSJason Molenda                 || (addr_class == eAddressClassUnknown
5872586e94bSJason Molenda                     && bp_loc_sp->GetAddress().GetOffset() & 1))
5882586e94bSJason Molenda             {
5892586e94bSJason Molenda                 trap_opcode = g_thumb_breakpoint_opcode;
5902586e94bSJason Molenda                 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
5912586e94bSJason Molenda             }
5922586e94bSJason Molenda             else
5932586e94bSJason Molenda             {
5942586e94bSJason Molenda                 trap_opcode = g_arm_breakpoint_opcode;
5952586e94bSJason Molenda                 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
5962586e94bSJason Molenda             }
5972586e94bSJason Molenda         }
5982586e94bSJason Molenda         break;
599c9335a3fSMohit K. Bhakkad     case llvm::Triple::mips64:
600c9335a3fSMohit K. Bhakkad         {
601c9335a3fSMohit K. Bhakkad             static const uint8_t g_hex_opcode[] = { 0x00, 0x00, 0x00, 0x0d };
602c9335a3fSMohit K. Bhakkad             trap_opcode = g_hex_opcode;
603c9335a3fSMohit K. Bhakkad             trap_opcode_size = sizeof(g_hex_opcode);
604c9335a3fSMohit K. Bhakkad         }
605c9335a3fSMohit K. Bhakkad         break;
606*2c2acf96SMohit K. Bhakkad     case llvm::Triple::mips64el:
607*2c2acf96SMohit K. Bhakkad         {
608*2c2acf96SMohit K. Bhakkad             static const uint8_t g_hex_opcode[] = { 0x0d, 0x00, 0x00, 0x00 };
609*2c2acf96SMohit K. Bhakkad             trap_opcode = g_hex_opcode;
610*2c2acf96SMohit K. Bhakkad             trap_opcode_size = sizeof(g_hex_opcode);
611*2c2acf96SMohit K. Bhakkad         }
612*2c2acf96SMohit K. Bhakkad         break;
613ecc11474SStephen Wilson     }
614ecc11474SStephen Wilson 
61528041352SGreg Clayton     if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
61628041352SGreg Clayton         return trap_opcode_size;
61728041352SGreg Clayton     return 0;
61828041352SGreg Clayton }
61928041352SGreg Clayton 
620348fb385STodd Fiala int32_t
621348fb385STodd Fiala PlatformLinux::GetResumeCountForLaunchInfo (ProcessLaunchInfo &launch_info)
62228041352SGreg Clayton {
623348fb385STodd Fiala     int32_t resume_count = 0;
62428041352SGreg Clayton 
625348fb385STodd Fiala     // Always resume past the initial stop when we use eLaunchFlagDebug
626348fb385STodd Fiala     if (launch_info.GetFlags ().Test (eLaunchFlagDebug))
62728041352SGreg Clayton     {
628348fb385STodd Fiala         // Resume past the stop for the final exec into the true inferior.
629348fb385STodd Fiala         ++resume_count;
630348fb385STodd Fiala     }
631015d818bSTodd Fiala 
632348fb385STodd Fiala     // If we're not launching a shell, we're done.
63310687b0eSZachary Turner     const FileSpec &shell = launch_info.GetShell();
63410687b0eSZachary Turner     if (!shell)
635348fb385STodd Fiala         return resume_count;
636348fb385STodd Fiala 
63710687b0eSZachary Turner     std::string shell_string = shell.GetPath();
638348fb385STodd Fiala     // We're in a shell, so for sure we have to resume past the shell exec.
639348fb385STodd Fiala     ++resume_count;
640348fb385STodd Fiala 
641348fb385STodd Fiala     // Figure out what shell we're planning on using.
64210687b0eSZachary Turner     const char *shell_name = strrchr (shell_string.c_str(), '/');
643348fb385STodd Fiala     if (shell_name == NULL)
64410687b0eSZachary Turner         shell_name = shell_string.c_str();
64528041352SGreg Clayton     else
646348fb385STodd Fiala         shell_name++;
647348fb385STodd Fiala 
648348fb385STodd Fiala     if (strcmp (shell_name, "csh") == 0
649348fb385STodd Fiala              || strcmp (shell_name, "tcsh") == 0
650348fb385STodd Fiala              || strcmp (shell_name, "zsh") == 0
651348fb385STodd Fiala              || strcmp (shell_name, "sh") == 0)
65228041352SGreg Clayton     {
653348fb385STodd Fiala         // These shells seem to re-exec themselves.  Add another resume.
654348fb385STodd Fiala         ++resume_count;
655ecc11474SStephen Wilson     }
65613e8e1c3SJohnny Chen 
657348fb385STodd Fiala     return resume_count;
658348fb385STodd Fiala }
659348fb385STodd Fiala 
660348fb385STodd Fiala bool
661348fb385STodd Fiala PlatformLinux::UseLlgsForLocalDebugging ()
662348fb385STodd Fiala {
663348fb385STodd Fiala     PlatformLinuxPropertiesSP properties_sp = GetGlobalProperties ();
664348fb385STodd Fiala     assert (properties_sp && "global properties shared pointer is null");
665348fb385STodd Fiala     return properties_sp ? properties_sp->GetUseLlgsForLocal () : false;
666348fb385STodd Fiala }
667348fb385STodd Fiala 
668015d818bSTodd Fiala bool
669015d818bSTodd Fiala PlatformLinux::CanDebugProcess ()
670015d818bSTodd Fiala {
671015d818bSTodd Fiala     if (IsHost ())
672348fb385STodd Fiala     {
673348fb385STodd Fiala         // The platform only does local debugging (i.e. uses llgs) when the setting indicates we do that.
674348fb385STodd Fiala         // Otherwise, we'll use ProcessLinux/ProcessPOSIX to handle with ProcessMonitor.
675348fb385STodd Fiala         return UseLlgsForLocalDebugging ();
676348fb385STodd Fiala     }
677348fb385STodd Fiala     else
678348fb385STodd Fiala     {
679015d818bSTodd Fiala         // If we're connected, we can debug.
680015d818bSTodd Fiala         return IsConnected ();
681015d818bSTodd Fiala     }
682348fb385STodd Fiala }
683015d818bSTodd Fiala 
684348fb385STodd Fiala // For local debugging, Linux will override the debug logic to use llgs-launch rather than
685348fb385STodd Fiala // lldb-launch, llgs-attach.  This differs from current lldb-launch, debugserver-attach
686348fb385STodd Fiala // approach on MacOSX.
68713e8e1c3SJohnny Chen lldb::ProcessSP
688348fb385STodd Fiala PlatformLinux::DebugProcess (ProcessLaunchInfo &launch_info,
68913e8e1c3SJohnny Chen                              Debugger &debugger,
690348fb385STodd Fiala                              Target *target,       // Can be NULL, if NULL create a new target, else use existing one
69113e8e1c3SJohnny Chen                              Error &error)
69213e8e1c3SJohnny Chen {
693db264a6dSTamas Berghammer     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
694348fb385STodd Fiala     if (log)
695348fb385STodd Fiala         log->Printf ("PlatformLinux::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target));
69628041352SGreg Clayton 
697348fb385STodd Fiala     // If we're a remote host, use standard behavior from parent class.
698348fb385STodd Fiala     if (!IsHost ())
6998012cadbSGreg Clayton         return PlatformPOSIX::DebugProcess (launch_info, debugger, target, error);
700348fb385STodd Fiala 
701348fb385STodd Fiala     //
702348fb385STodd Fiala     // For local debugging, we'll insist on having ProcessGDBRemote create the process.
703348fb385STodd Fiala     //
704348fb385STodd Fiala 
705348fb385STodd Fiala     ProcessSP process_sp;
706348fb385STodd Fiala 
707348fb385STodd Fiala     // Ensure we're using llgs for local debugging.
708348fb385STodd Fiala     if (!UseLlgsForLocalDebugging ())
709348fb385STodd Fiala     {
710348fb385STodd Fiala         assert (false && "we're trying to debug a local process but platform.plugin.linux.use-llgs-for-local is false, should never get here");
711348fb385STodd Fiala         error.SetErrorString ("attempted to start gdb-remote-based debugging for local process but platform.plugin.linux.use-llgs-for-local is false");
712348fb385STodd Fiala         return process_sp;
713348fb385STodd Fiala     }
714348fb385STodd Fiala 
715348fb385STodd Fiala     // Make sure we stop at the entry point
716348fb385STodd Fiala     launch_info.GetFlags ().Set (eLaunchFlagDebug);
717348fb385STodd Fiala 
718348fb385STodd Fiala     // We always launch the process we are going to debug in a separate process
719348fb385STodd Fiala     // group, since then we can handle ^C interrupts ourselves w/o having to worry
720348fb385STodd Fiala     // about the target getting them as well.
721348fb385STodd Fiala     launch_info.SetLaunchInSeparateProcessGroup(true);
722348fb385STodd Fiala 
723348fb385STodd Fiala     // Ensure we have a target.
724348fb385STodd Fiala     if (target == nullptr)
725348fb385STodd Fiala     {
726348fb385STodd Fiala         if (log)
727348fb385STodd Fiala             log->Printf ("PlatformLinux::%s creating new target", __FUNCTION__);
728348fb385STodd Fiala 
729348fb385STodd Fiala         TargetSP new_target_sp;
73028041352SGreg Clayton         error = debugger.GetTargetList().CreateTarget (debugger,
731348fb385STodd Fiala                                                        nullptr,
732348fb385STodd Fiala                                                        nullptr,
73328041352SGreg Clayton                                                        false,
734348fb385STodd Fiala                                                        nullptr,
73528041352SGreg Clayton                                                        new_target_sp);
736348fb385STodd Fiala         if (error.Fail ())
737348fb385STodd Fiala         {
738348fb385STodd Fiala             if (log)
739348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s failed to create new target: %s", __FUNCTION__, error.AsCString ());
740348fb385STodd Fiala             return process_sp;
741348fb385STodd Fiala         }
742348fb385STodd Fiala 
74328041352SGreg Clayton         target = new_target_sp.get();
744348fb385STodd Fiala         if (!target)
745348fb385STodd Fiala         {
746348fb385STodd Fiala             error.SetErrorString ("CreateTarget() returned nullptr");
747348fb385STodd Fiala             if (log)
748348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ());
749348fb385STodd Fiala             return process_sp;
750348fb385STodd Fiala         }
75128041352SGreg Clayton     }
75228041352SGreg Clayton     else
75328041352SGreg Clayton     {
754348fb385STodd Fiala         if (log)
755348fb385STodd Fiala             log->Printf ("PlatformLinux::%s using provided target", __FUNCTION__);
756348fb385STodd Fiala     }
757348fb385STodd Fiala 
758348fb385STodd Fiala     // Mark target as currently selected target.
75928041352SGreg Clayton     debugger.GetTargetList().SetSelectedTarget(target);
76028041352SGreg Clayton 
761348fb385STodd Fiala     // Now create the gdb-remote process.
762348fb385STodd Fiala     if (log)
763348fb385STodd Fiala         log->Printf ("PlatformLinux::%s having target create process with gdb-remote plugin", __FUNCTION__);
7648012cadbSGreg Clayton     process_sp = target->CreateProcess (launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr);
76528041352SGreg Clayton 
766348fb385STodd Fiala     if (!process_sp)
767348fb385STodd Fiala     {
768348fb385STodd Fiala         error.SetErrorString ("CreateProcess() failed for gdb-remote process");
769348fb385STodd Fiala         if (log)
770348fb385STodd Fiala             log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ());
771348fb385STodd Fiala         return process_sp;
772348fb385STodd Fiala     }
773348fb385STodd Fiala     else
774348fb385STodd Fiala     {
775348fb385STodd Fiala         if (log)
776348fb385STodd Fiala             log->Printf ("PlatformLinux::%s successfully created process", __FUNCTION__);
777348fb385STodd Fiala     }
778348fb385STodd Fiala 
779348fb385STodd Fiala     // Set the unix signals properly.
780348fb385STodd Fiala     process_sp->SetUnixSignals (Host::GetUnixSignals ());
781348fb385STodd Fiala 
782348fb385STodd Fiala     // Adjust launch for a hijacker.
783348fb385STodd Fiala     ListenerSP listener_sp;
784348fb385STodd Fiala     if (!launch_info.GetHijackListener ())
785348fb385STodd Fiala     {
786348fb385STodd Fiala         if (log)
787348fb385STodd Fiala             log->Printf ("PlatformLinux::%s setting up hijacker", __FUNCTION__);
788348fb385STodd Fiala 
789348fb385STodd Fiala         listener_sp.reset (new Listener("lldb.PlatformLinux.DebugProcess.hijack"));
790348fb385STodd Fiala         launch_info.SetHijackListener (listener_sp);
791348fb385STodd Fiala         process_sp->HijackProcessEvents (listener_sp.get ());
792348fb385STodd Fiala     }
793348fb385STodd Fiala 
794348fb385STodd Fiala     // Log file actions.
795348fb385STodd Fiala     if (log)
796348fb385STodd Fiala     {
797348fb385STodd Fiala         log->Printf ("PlatformLinux::%s launching process with the following file actions:", __FUNCTION__);
798348fb385STodd Fiala 
799348fb385STodd Fiala         StreamString stream;
800348fb385STodd Fiala         size_t i = 0;
801348fb385STodd Fiala         const FileAction *file_action;
802348fb385STodd Fiala         while ((file_action = launch_info.GetFileActionAtIndex (i++)) != nullptr)
803348fb385STodd Fiala         {
804348fb385STodd Fiala             file_action->Dump (stream);
805348fb385STodd Fiala             log->PutCString (stream.GetString().c_str ());
806348fb385STodd Fiala             stream.Clear();
807348fb385STodd Fiala         }
808348fb385STodd Fiala     }
809348fb385STodd Fiala 
810348fb385STodd Fiala     // Do the launch.
811348fb385STodd Fiala     error = process_sp->Launch(launch_info);
812348fb385STodd Fiala     if (error.Success ())
813348fb385STodd Fiala     {
814348fb385STodd Fiala         // Handle the hijacking of process events.
815348fb385STodd Fiala         if (listener_sp)
816348fb385STodd Fiala         {
817348fb385STodd Fiala             const StateType state = process_sp->WaitForProcessToStop (NULL, NULL, false, listener_sp.get());
818348fb385STodd Fiala 
819348fb385STodd Fiala             if (state == eStateStopped)
820348fb385STodd Fiala             {
821348fb385STodd Fiala                 if (log)
822348fb385STodd Fiala                     log->Printf ("PlatformLinux::%s pid %" PRIu64 " state %s\n",
823348fb385STodd Fiala                                  __FUNCTION__, process_sp->GetID (), StateAsCString (state));
824348fb385STodd Fiala             }
825348fb385STodd Fiala             else
826348fb385STodd Fiala             {
827348fb385STodd Fiala                 if (log)
828348fb385STodd Fiala                     log->Printf ("PlatformLinux::%s pid %" PRIu64 " state is not stopped - %s\n",
829348fb385STodd Fiala                                  __FUNCTION__, process_sp->GetID (), StateAsCString (state));
830348fb385STodd Fiala             }
831348fb385STodd Fiala         }
832348fb385STodd Fiala 
833348fb385STodd Fiala         // Hook up process PTY if we have one (which we should for local debugging with llgs).
834348fb385STodd Fiala         int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
835348fb385STodd Fiala         if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
836348fb385STodd Fiala         {
837348fb385STodd Fiala             process_sp->SetSTDIOFileDescriptor(pty_fd);
838348fb385STodd Fiala             if (log)
839348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s pid %" PRIu64 " hooked up STDIO pty to process", __FUNCTION__, process_sp->GetID ());
840348fb385STodd Fiala         }
841348fb385STodd Fiala         else
842348fb385STodd Fiala         {
843348fb385STodd Fiala             if (log)
844348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s pid %" PRIu64 " not using process STDIO pty", __FUNCTION__, process_sp->GetID ());
84528041352SGreg Clayton         }
84628041352SGreg Clayton     }
84728041352SGreg Clayton     else
84828041352SGreg Clayton     {
849348fb385STodd Fiala         if (log)
850348fb385STodd Fiala             log->Printf ("PlatformLinux::%s process launch failed: %s", __FUNCTION__, error.AsCString ());
851348fb385STodd Fiala         // FIXME figure out appropriate cleanup here.  Do we delete the target? Do we delete the process?  Does our caller do that?
85228041352SGreg Clayton     }
853348fb385STodd Fiala 
85428041352SGreg Clayton     return process_sp;
85513e8e1c3SJohnny Chen }
8562094dbf4SJason Molenda 
8572094dbf4SJason Molenda void
8582094dbf4SJason Molenda PlatformLinux::CalculateTrapHandlerSymbolNames ()
8592094dbf4SJason Molenda {
8602094dbf4SJason Molenda     m_trap_handlers.push_back (ConstString ("_sigtramp"));
8612094dbf4SJason Molenda }
862af245d11STodd Fiala 
863af245d11STodd Fiala Error
864db264a6dSTamas Berghammer PlatformLinux::LaunchNativeProcess (ProcessLaunchInfo &launch_info,
865db264a6dSTamas Berghammer                                     NativeProcessProtocol::NativeDelegate &native_delegate,
866af245d11STodd Fiala                                     NativeProcessProtocolSP &process_sp)
867af245d11STodd Fiala {
868c6ec76e3STamas Berghammer #if !defined(__linux__)
869c6ec76e3STamas Berghammer     return Error("Only implemented on Linux hosts");
870c6ec76e3STamas Berghammer #else
871af245d11STodd Fiala     if (!IsHost ())
872af245d11STodd Fiala         return Error("PlatformLinux::%s (): cannot launch a debug process when not the host", __FUNCTION__);
873af245d11STodd Fiala 
874af245d11STodd Fiala     // Retrieve the exe module.
875af245d11STodd Fiala     lldb::ModuleSP exe_module_sp;
8766edef204SOleksiy Vyalov     ModuleSpec exe_module_spec(launch_info.GetExecutableFile(), launch_info.GetArchitecture());
877af245d11STodd Fiala 
878af245d11STodd Fiala     Error error = ResolveExecutable (
8796edef204SOleksiy Vyalov         exe_module_spec,
880af245d11STodd Fiala         exe_module_sp,
881af245d11STodd Fiala         NULL);
882af245d11STodd Fiala 
883af245d11STodd Fiala     if (!error.Success ())
884af245d11STodd Fiala         return error;
885af245d11STodd Fiala 
886af245d11STodd Fiala     if (!exe_module_sp)
887af245d11STodd Fiala         return Error("exe_module_sp could not be resolved for %s", launch_info.GetExecutableFile ().GetPath ().c_str ());
888af245d11STodd Fiala 
889af245d11STodd Fiala     // Launch it for debugging
89047b11c61STamas Berghammer     error = process_linux::NativeProcessLinux::LaunchProcess (
891af245d11STodd Fiala         exe_module_sp.get (),
892af245d11STodd Fiala         launch_info,
893af245d11STodd Fiala         native_delegate,
894af245d11STodd Fiala         process_sp);
895af245d11STodd Fiala 
896af245d11STodd Fiala     return error;
897c6ec76e3STamas Berghammer #endif
898af245d11STodd Fiala }
899af245d11STodd Fiala 
900af245d11STodd Fiala Error
901af245d11STodd Fiala PlatformLinux::AttachNativeProcess (lldb::pid_t pid,
902db264a6dSTamas Berghammer                                     NativeProcessProtocol::NativeDelegate &native_delegate,
903af245d11STodd Fiala                                     NativeProcessProtocolSP &process_sp)
904af245d11STodd Fiala {
905c6ec76e3STamas Berghammer #if !defined(__linux__)
906c6ec76e3STamas Berghammer     return Error("Only implemented on Linux hosts");
907c6ec76e3STamas Berghammer #else
908af245d11STodd Fiala     if (!IsHost ())
909af245d11STodd Fiala         return Error("PlatformLinux::%s (): cannot attach to a debug process when not the host", __FUNCTION__);
910af245d11STodd Fiala 
911af245d11STodd Fiala     // Launch it for debugging
91247b11c61STamas Berghammer     return process_linux::NativeProcessLinux::AttachToProcess (pid, native_delegate, process_sp);
913c6ec76e3STamas Berghammer #endif
914af245d11STodd Fiala }
915