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;
47e996fd30SGreg Clayton 
4828041352SGreg Clayton static uint32_t g_initialize_count = 0;
4928041352SGreg Clayton 
50281961a8STodd Fiala //------------------------------------------------------------------
51281961a8STodd Fiala /// Code to handle the PlatformLinux settings
52281961a8STodd Fiala //------------------------------------------------------------------
53281961a8STodd Fiala 
54348fb385STodd Fiala namespace
55348fb385STodd Fiala {
56348fb385STodd Fiala     enum
57348fb385STodd Fiala     {
58348fb385STodd Fiala         ePropertyUseLlgsForLocal = 0,
59348fb385STodd Fiala     };
60348fb385STodd Fiala 
61348fb385STodd Fiala     const PropertyDefinition*
62348fb385STodd Fiala     GetStaticPropertyDefinitions ()
63348fb385STodd Fiala     {
64281961a8STodd Fiala         static PropertyDefinition
65281961a8STodd Fiala         g_properties[] =
66281961a8STodd Fiala         {
67281961a8STodd Fiala             { "use-llgs-for-local" , OptionValue::eTypeBoolean, true, false, NULL, NULL, "Control whether the platform uses llgs for local debug sessions." },
68281961a8STodd Fiala             {  NULL        , OptionValue::eTypeInvalid, false, 0  , NULL, NULL, NULL  }
69281961a8STodd Fiala         };
70281961a8STodd Fiala 
71348fb385STodd Fiala         // Allow environment variable to force using llgs-local.
72348fb385STodd Fiala         if (getenv("PLATFORM_LINUX_FORCE_LLGS_LOCAL"))
73348fb385STodd Fiala             g_properties[ePropertyUseLlgsForLocal].default_uint_value = true;
74281961a8STodd Fiala 
75348fb385STodd Fiala         return g_properties;
76348fb385STodd Fiala     }
77348fb385STodd Fiala }
78281961a8STodd Fiala 
79281961a8STodd Fiala class PlatformLinuxProperties : public Properties
80281961a8STodd Fiala {
81281961a8STodd Fiala public:
82281961a8STodd Fiala 
83281961a8STodd Fiala     static ConstString &
84281961a8STodd Fiala     GetSettingName ()
85281961a8STodd Fiala     {
86281961a8STodd Fiala         static ConstString g_setting_name("linux");
87281961a8STodd Fiala         return g_setting_name;
88281961a8STodd Fiala     }
89281961a8STodd Fiala 
90281961a8STodd Fiala     PlatformLinuxProperties() :
91281961a8STodd Fiala     Properties ()
92281961a8STodd Fiala     {
93281961a8STodd Fiala         m_collection_sp.reset (new OptionValueProperties(GetSettingName ()));
94348fb385STodd Fiala         m_collection_sp->Initialize (GetStaticPropertyDefinitions ());
95281961a8STodd Fiala     }
96281961a8STodd Fiala 
97281961a8STodd Fiala     virtual
98281961a8STodd Fiala     ~PlatformLinuxProperties()
99281961a8STodd Fiala     {
100281961a8STodd Fiala     }
101281961a8STodd Fiala 
102281961a8STodd Fiala     bool
103281961a8STodd Fiala     GetUseLlgsForLocal() const
104281961a8STodd Fiala     {
105281961a8STodd Fiala         const uint32_t idx = ePropertyUseLlgsForLocal;
106348fb385STodd Fiala         return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, GetStaticPropertyDefinitions()[idx].default_uint_value != 0);
107281961a8STodd Fiala     }
108281961a8STodd Fiala };
109281961a8STodd Fiala 
110281961a8STodd Fiala typedef std::shared_ptr<PlatformLinuxProperties> PlatformLinuxPropertiesSP;
111281961a8STodd Fiala 
112281961a8STodd Fiala static const PlatformLinuxPropertiesSP &
113281961a8STodd Fiala GetGlobalProperties()
114281961a8STodd Fiala {
115281961a8STodd Fiala     static PlatformLinuxPropertiesSP g_settings_sp;
116281961a8STodd Fiala     if (!g_settings_sp)
117281961a8STodd Fiala         g_settings_sp.reset (new PlatformLinuxProperties ());
118281961a8STodd Fiala     return g_settings_sp;
119281961a8STodd Fiala }
120281961a8STodd Fiala 
121281961a8STodd Fiala void
122281961a8STodd Fiala PlatformLinux::DebuggerInitialize (lldb_private::Debugger &debugger)
123281961a8STodd Fiala {
124281961a8STodd Fiala     if (!PluginManager::GetSettingForPlatformPlugin (debugger, PlatformLinuxProperties::GetSettingName()))
125281961a8STodd Fiala     {
126281961a8STodd Fiala         const bool is_global_setting = true;
127281961a8STodd Fiala         PluginManager::CreateSettingForPlatformPlugin (debugger,
128281961a8STodd Fiala                                                        GetGlobalProperties()->GetValueProperties(),
129281961a8STodd Fiala                                                        ConstString ("Properties for the PlatformLinux plug-in."),
130281961a8STodd Fiala                                                        is_global_setting);
131281961a8STodd Fiala     }
132281961a8STodd Fiala }
133281961a8STodd Fiala 
134281961a8STodd Fiala 
135281961a8STodd Fiala //------------------------------------------------------------------
136281961a8STodd Fiala 
137615eb7e6SGreg Clayton PlatformSP
138b3a40ba8SGreg Clayton PlatformLinux::CreateInstance (bool force, const ArchSpec *arch)
139ecc11474SStephen Wilson {
140015d818bSTodd Fiala     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
141015d818bSTodd Fiala     if (log)
142015d818bSTodd Fiala     {
143015d818bSTodd Fiala         const char *arch_name;
144015d818bSTodd Fiala         if (arch && arch->GetArchitectureName ())
145015d818bSTodd Fiala             arch_name = arch->GetArchitectureName ();
146015d818bSTodd Fiala         else
147015d818bSTodd Fiala             arch_name = "<null>";
148015d818bSTodd Fiala 
149015d818bSTodd Fiala         const char *triple_cstr = arch ? arch->GetTriple ().getTriple ().c_str() : "<null>";
150015d818bSTodd Fiala 
151015d818bSTodd Fiala         log->Printf ("PlatformLinux::%s(force=%s, arch={%s,%s})", __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
152015d818bSTodd Fiala     }
153015d818bSTodd Fiala 
154b3a40ba8SGreg Clayton     bool create = force;
155b3a40ba8SGreg Clayton     if (create == false && arch && arch->IsValid())
156b3a40ba8SGreg Clayton     {
157b3a40ba8SGreg Clayton         const llvm::Triple &triple = arch->GetTriple();
15870512317SGreg Clayton         switch (triple.getVendor())
15970512317SGreg Clayton         {
16070512317SGreg Clayton             case llvm::Triple::PC:
161b3a40ba8SGreg Clayton                 create = true;
16270512317SGreg Clayton                 break;
16370512317SGreg Clayton 
164dbc6c0bbSGreg Clayton #if defined(__linux__)
165dbc6c0bbSGreg Clayton             // Only accept "unknown" for the vendor if the host is linux and
1666a7f3338SBruce Mitchener             // it "unknown" wasn't specified (it was just returned because it
167dbc6c0bbSGreg Clayton             // was NOT specified_
168015d818bSTodd Fiala             case llvm::Triple::VendorType::UnknownVendor:
16970512317SGreg Clayton                 create = !arch->TripleVendorWasSpecified();
17070512317SGreg Clayton                 break;
171dbc6c0bbSGreg Clayton #endif
17270512317SGreg Clayton             default:
17370512317SGreg Clayton                 break;
17470512317SGreg Clayton         }
17570512317SGreg Clayton 
17670512317SGreg Clayton         if (create)
17770512317SGreg Clayton         {
17870512317SGreg Clayton             switch (triple.getOS())
17970512317SGreg Clayton             {
18070512317SGreg Clayton                 case llvm::Triple::Linux:
18170512317SGreg Clayton                     break;
18270512317SGreg Clayton 
183dbc6c0bbSGreg Clayton #if defined(__linux__)
184dbc6c0bbSGreg Clayton                 // Only accept "unknown" for the OS if the host is linux and
1856a7f3338SBruce Mitchener                 // it "unknown" wasn't specified (it was just returned because it
186dbc6c0bbSGreg Clayton                 // was NOT specified)
187015d818bSTodd Fiala                 case llvm::Triple::OSType::UnknownOS:
18870512317SGreg Clayton                     create = !arch->TripleOSWasSpecified();
18970512317SGreg Clayton                     break;
190dbc6c0bbSGreg Clayton #endif
19170512317SGreg Clayton                 default:
19270512317SGreg Clayton                     create = false;
19370512317SGreg Clayton                     break;
19470512317SGreg Clayton             }
19570512317SGreg Clayton         }
196b3a40ba8SGreg Clayton     }
197015d818bSTodd Fiala 
198b3a40ba8SGreg Clayton     if (create)
199015d818bSTodd Fiala     {
200015d818bSTodd Fiala         if (log)
201015d818bSTodd Fiala             log->Printf ("PlatformLinux::%s() creating remote-linux platform", __FUNCTION__);
202615eb7e6SGreg Clayton         return PlatformSP(new PlatformLinux(false));
203015d818bSTodd Fiala     }
204015d818bSTodd Fiala 
205015d818bSTodd Fiala     if (log)
206015d818bSTodd Fiala         log->Printf ("PlatformLinux::%s() aborting creation of remote-linux platform", __FUNCTION__);
207015d818bSTodd Fiala 
208615eb7e6SGreg Clayton     return PlatformSP();
209ecc11474SStephen Wilson }
210ecc11474SStephen Wilson 
211ecc11474SStephen Wilson 
21257abc5d6SGreg Clayton lldb_private::ConstString
21357abc5d6SGreg Clayton PlatformLinux::GetPluginNameStatic (bool is_host)
214ecc11474SStephen Wilson {
21528041352SGreg Clayton     if (is_host)
21657abc5d6SGreg Clayton     {
21757abc5d6SGreg Clayton         static ConstString g_host_name(Platform::GetHostPlatformName ());
21857abc5d6SGreg Clayton         return g_host_name;
21957abc5d6SGreg Clayton     }
22028041352SGreg Clayton     else
22157abc5d6SGreg Clayton     {
22257abc5d6SGreg Clayton         static ConstString g_remote_name("remote-linux");
22357abc5d6SGreg Clayton         return g_remote_name;
22457abc5d6SGreg Clayton     }
22528041352SGreg Clayton }
22628041352SGreg Clayton 
22728041352SGreg Clayton const char *
22828041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic (bool is_host)
22928041352SGreg Clayton {
23028041352SGreg Clayton     if (is_host)
23128041352SGreg Clayton         return "Local Linux user platform plug-in.";
23228041352SGreg Clayton     else
23328041352SGreg Clayton         return "Remote Linux user platform plug-in.";
234ecc11474SStephen Wilson }
235ecc11474SStephen Wilson 
23657abc5d6SGreg Clayton lldb_private::ConstString
23757abc5d6SGreg Clayton PlatformLinux::GetPluginName()
23857abc5d6SGreg Clayton {
23957abc5d6SGreg Clayton     return GetPluginNameStatic(IsHost());
24057abc5d6SGreg Clayton }
24157abc5d6SGreg Clayton 
242e996fd30SGreg Clayton void
243e996fd30SGreg Clayton PlatformLinux::Initialize ()
244e996fd30SGreg Clayton {
24528041352SGreg Clayton     if (g_initialize_count++ == 0)
246ecc11474SStephen Wilson     {
24728041352SGreg Clayton #if defined(__linux__)
24828041352SGreg Clayton         PlatformSP default_platform_sp (new PlatformLinux(true));
24913b18261SZachary Turner         default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
250615eb7e6SGreg Clayton         Platform::SetHostPlatform (default_platform_sp);
25128041352SGreg Clayton #endif
25257abc5d6SGreg Clayton         PluginManager::RegisterPlugin(PlatformLinux::GetPluginNameStatic(false),
25328041352SGreg Clayton                                       PlatformLinux::GetPluginDescriptionStatic(false),
254281961a8STodd Fiala                                       PlatformLinux::CreateInstance,
255281961a8STodd Fiala                                       PlatformLinux::DebuggerInitialize);
256ecc11474SStephen Wilson     }
257e996fd30SGreg Clayton }
258e996fd30SGreg Clayton 
259e996fd30SGreg Clayton void
260e996fd30SGreg Clayton PlatformLinux::Terminate ()
261e996fd30SGreg Clayton {
26228041352SGreg Clayton     if (g_initialize_count > 0)
26328041352SGreg Clayton     {
26428041352SGreg Clayton         if (--g_initialize_count == 0)
26528041352SGreg Clayton         {
26628041352SGreg Clayton             PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance);
267e996fd30SGreg Clayton         }
26828041352SGreg Clayton     }
26928041352SGreg Clayton }
270e996fd30SGreg Clayton 
271e996fd30SGreg Clayton Error
2728012cadbSGreg Clayton PlatformLinux::ResolveExecutable (const ModuleSpec &ms,
273ea5e0cc3SGreg Clayton                                   lldb::ModuleSP &exe_module_sp,
274ea5e0cc3SGreg Clayton                                   const FileSpecList *module_search_paths_ptr)
275e996fd30SGreg Clayton {
276e996fd30SGreg Clayton     Error error;
277e996fd30SGreg Clayton     // Nothing special to do here, just use the actual file and architecture
278e996fd30SGreg Clayton 
27928041352SGreg Clayton     char exe_path[PATH_MAX];
2808012cadbSGreg Clayton     ModuleSpec resolved_module_spec (ms);
281e996fd30SGreg Clayton 
28228041352SGreg Clayton     if (IsHost())
28328041352SGreg Clayton     {
28428041352SGreg Clayton         // If we have "ls" as the exe_file, resolve the executable location based on
285e996fd30SGreg Clayton         // the current path variables
2868012cadbSGreg Clayton         if (!resolved_module_spec.GetFileSpec().Exists())
28728041352SGreg Clayton         {
2888012cadbSGreg Clayton             resolved_module_spec.GetFileSpec().GetPath(exe_path, sizeof(exe_path));
2898012cadbSGreg Clayton             resolved_module_spec.GetFileSpec().SetFile(exe_path, true);
29028041352SGreg Clayton         }
29128041352SGreg Clayton 
2928012cadbSGreg Clayton         if (!resolved_module_spec.GetFileSpec().Exists())
2938012cadbSGreg Clayton             resolved_module_spec.GetFileSpec().ResolveExecutableLocation ();
294e996fd30SGreg Clayton 
2958012cadbSGreg Clayton         if (resolved_module_spec.GetFileSpec().Exists())
29628041352SGreg Clayton             error.Clear();
29728041352SGreg Clayton         else
29828041352SGreg Clayton         {
2998012cadbSGreg Clayton             error.SetErrorStringWithFormat("unable to find executable for '%s'", resolved_module_spec.GetFileSpec().GetPath().c_str());
30028041352SGreg Clayton         }
30128041352SGreg Clayton     }
30228041352SGreg Clayton     else
30328041352SGreg Clayton     {
30428041352SGreg Clayton         if (m_remote_platform_sp)
30528041352SGreg Clayton         {
3068012cadbSGreg Clayton             error = m_remote_platform_sp->ResolveExecutable (ms,
3070c90ef47SGreg Clayton                                                              exe_module_sp,
3080c90ef47SGreg Clayton                                                              NULL);
30928041352SGreg Clayton         }
31028041352SGreg Clayton         else
31128041352SGreg Clayton         {
31228041352SGreg Clayton             // We may connect to a process and use the provided executable (Don't use local $PATH).
313e996fd30SGreg Clayton 
3148012cadbSGreg Clayton             if (resolved_module_spec.GetFileSpec().Exists())
31528041352SGreg Clayton                 error.Clear();
31628041352SGreg Clayton             else
31728041352SGreg Clayton                 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path);
31828041352SGreg Clayton         }
31928041352SGreg Clayton     }
32028041352SGreg Clayton 
32128041352SGreg Clayton     if (error.Success())
322e996fd30SGreg Clayton     {
3238012cadbSGreg Clayton         if (resolved_module_spec.GetArchitecture().IsValid())
324e996fd30SGreg Clayton         {
3258012cadbSGreg Clayton             error = ModuleList::GetSharedModule (resolved_module_spec,
326e996fd30SGreg Clayton                                                  exe_module_sp,
327e996fd30SGreg Clayton                                                  NULL,
3280c90ef47SGreg Clayton                                                  NULL,
329e996fd30SGreg Clayton                                                  NULL);
3309f0013d8SMichael Sartain             if (error.Fail())
3319f0013d8SMichael Sartain             {
3329f0013d8SMichael Sartain                 // If we failed, it may be because the vendor and os aren't known. If that is the
3339f0013d8SMichael Sartain                 // case, try setting them to the host architecture and give it another try.
3348012cadbSGreg Clayton                 llvm::Triple &module_triple = resolved_module_spec.GetArchitecture().GetTriple();
3359f0013d8SMichael Sartain                 bool is_vendor_specified = (module_triple.getVendor() != llvm::Triple::UnknownVendor);
3369f0013d8SMichael Sartain                 bool is_os_specified = (module_triple.getOS() != llvm::Triple::UnknownOS);
3379f0013d8SMichael Sartain                 if (!is_vendor_specified || !is_os_specified)
3389f0013d8SMichael Sartain                 {
33913b18261SZachary Turner                     const llvm::Triple &host_triple = HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple();
3409f0013d8SMichael Sartain 
3419f0013d8SMichael Sartain                     if (!is_vendor_specified)
3429f0013d8SMichael Sartain                         module_triple.setVendorName (host_triple.getVendorName());
3439f0013d8SMichael Sartain                     if (!is_os_specified)
3449f0013d8SMichael Sartain                         module_triple.setOSName (host_triple.getOSName());
3459f0013d8SMichael Sartain 
3468012cadbSGreg Clayton                     error = ModuleList::GetSharedModule (resolved_module_spec,
3479f0013d8SMichael Sartain                                                          exe_module_sp,
3489f0013d8SMichael Sartain                                                          NULL,
3499f0013d8SMichael Sartain                                                          NULL,
3509f0013d8SMichael Sartain                                                          NULL);
3519f0013d8SMichael Sartain                 }
3529f0013d8SMichael Sartain             }
353e996fd30SGreg Clayton 
354e635db49SSean Callanan             // TODO find out why exe_module_sp might be NULL
355e635db49SSean Callanan             if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL)
356e996fd30SGreg Clayton             {
357e996fd30SGreg Clayton                 exe_module_sp.reset();
358b5ad4ec7SGreg Clayton                 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s",
3598012cadbSGreg Clayton                                                 resolved_module_spec.GetFileSpec().GetPath().c_str(),
3608012cadbSGreg Clayton                                                 resolved_module_spec.GetArchitecture().GetArchitectureName());
361e996fd30SGreg Clayton             }
362e996fd30SGreg Clayton         }
363e996fd30SGreg Clayton         else
364e996fd30SGreg Clayton         {
365e996fd30SGreg Clayton             // No valid architecture was specified, ask the platform for
366e996fd30SGreg Clayton             // the architectures that we should be using (in the correct order)
367e996fd30SGreg Clayton             // and see if we can find a match that way
368e996fd30SGreg Clayton             StreamString arch_names;
3698012cadbSGreg Clayton             for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, resolved_module_spec.GetArchitecture()); ++idx)
370e996fd30SGreg Clayton             {
3718012cadbSGreg Clayton                 error = ModuleList::GetSharedModule (resolved_module_spec,
372e996fd30SGreg Clayton                                                      exe_module_sp,
373e996fd30SGreg Clayton                                                      NULL,
3740c90ef47SGreg Clayton                                                      NULL,
375e996fd30SGreg Clayton                                                      NULL);
376e996fd30SGreg Clayton                 // Did we find an executable using one of the
377e996fd30SGreg Clayton                 if (error.Success())
378e996fd30SGreg Clayton                 {
379e996fd30SGreg Clayton                     if (exe_module_sp && exe_module_sp->GetObjectFile())
380e996fd30SGreg Clayton                         break;
381e996fd30SGreg Clayton                     else
382e996fd30SGreg Clayton                         error.SetErrorToGenericError();
383e996fd30SGreg Clayton                 }
384e996fd30SGreg Clayton 
385e996fd30SGreg Clayton                 if (idx > 0)
386e996fd30SGreg Clayton                     arch_names.PutCString (", ");
3878012cadbSGreg Clayton                 arch_names.PutCString (resolved_module_spec.GetArchitecture().GetArchitectureName());
388e996fd30SGreg Clayton             }
389e996fd30SGreg Clayton 
390e996fd30SGreg Clayton             if (error.Fail() || !exe_module_sp)
391e996fd30SGreg Clayton             {
3928012cadbSGreg Clayton                 if (resolved_module_spec.GetFileSpec().Readable())
39339945dccSGreg Clayton                 {
394b5ad4ec7SGreg Clayton                     error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
3958012cadbSGreg Clayton                                                     resolved_module_spec.GetFileSpec().GetPath().c_str(),
39657abc5d6SGreg Clayton                                                     GetPluginName().GetCString(),
397e996fd30SGreg Clayton                                                     arch_names.GetString().c_str());
398e996fd30SGreg Clayton                 }
39939945dccSGreg Clayton                 else
40039945dccSGreg Clayton                 {
4018012cadbSGreg Clayton                     error.SetErrorStringWithFormat("'%s' is not readable", resolved_module_spec.GetFileSpec().GetPath().c_str());
40239945dccSGreg Clayton                 }
40339945dccSGreg Clayton             }
404e996fd30SGreg Clayton         }
405e996fd30SGreg Clayton     }
406e996fd30SGreg Clayton 
407e996fd30SGreg Clayton     return error;
408e996fd30SGreg Clayton }
409e996fd30SGreg Clayton 
410e996fd30SGreg Clayton Error
411fc995725SSteve Pucci PlatformLinux::GetFileWithUUID (const FileSpec &platform_file,
41228041352SGreg Clayton                                 const UUID *uuid_ptr, FileSpec &local_file)
413e996fd30SGreg Clayton {
41428041352SGreg Clayton     if (IsRemote())
41528041352SGreg Clayton     {
41628041352SGreg Clayton         if (m_remote_platform_sp)
417fc995725SSteve Pucci             return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file);
41828041352SGreg Clayton     }
41928041352SGreg Clayton 
420e996fd30SGreg Clayton     // Default to the local case
421e996fd30SGreg Clayton     local_file = platform_file;
422e996fd30SGreg Clayton     return Error();
423e996fd30SGreg Clayton }
424e996fd30SGreg Clayton 
425e996fd30SGreg Clayton 
426e996fd30SGreg Clayton //------------------------------------------------------------------
427e996fd30SGreg Clayton /// Default Constructor
428e996fd30SGreg Clayton //------------------------------------------------------------------
42928041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) :
430015d818bSTodd Fiala     PlatformPOSIX(is_host)  // This is the local host platform
431e996fd30SGreg Clayton {
432e996fd30SGreg Clayton }
433e996fd30SGreg Clayton 
434e996fd30SGreg Clayton //------------------------------------------------------------------
435e996fd30SGreg Clayton /// Destructor.
436e996fd30SGreg Clayton ///
437e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be
438e996fd30SGreg Clayton /// inherited from by the plug-in instance.
439e996fd30SGreg Clayton //------------------------------------------------------------------
440e996fd30SGreg Clayton PlatformLinux::~PlatformLinux()
441e996fd30SGreg Clayton {
442e996fd30SGreg Clayton }
443e996fd30SGreg Clayton 
444e996fd30SGreg Clayton bool
44513e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
446e996fd30SGreg Clayton {
44728041352SGreg Clayton     bool success = false;
44828041352SGreg Clayton     if (IsHost())
44928041352SGreg Clayton     {
45028041352SGreg Clayton         success = Platform::GetProcessInfo (pid, process_info);
45128041352SGreg Clayton     }
45228041352SGreg Clayton     else
45328041352SGreg Clayton     {
45428041352SGreg Clayton         if (m_remote_platform_sp)
45528041352SGreg Clayton             success = m_remote_platform_sp->GetProcessInfo (pid, process_info);
45628041352SGreg Clayton     }
45728041352SGreg Clayton     return success;
458e996fd30SGreg Clayton }
459e996fd30SGreg Clayton 
460*8e6ec453SStephane Sezer uint32_t
461*8e6ec453SStephane Sezer PlatformLinux::FindProcesses (const ProcessInstanceInfoMatch &match_info,
462*8e6ec453SStephane Sezer                               ProcessInstanceInfoList &process_infos)
463*8e6ec453SStephane Sezer {
464*8e6ec453SStephane Sezer     uint32_t match_count = 0;
465*8e6ec453SStephane Sezer     if (IsHost())
466*8e6ec453SStephane Sezer     {
467*8e6ec453SStephane Sezer         // Let the base class figure out the host details
468*8e6ec453SStephane Sezer         match_count = Platform::FindProcesses (match_info, process_infos);
469*8e6ec453SStephane Sezer     }
470*8e6ec453SStephane Sezer     else
471*8e6ec453SStephane Sezer     {
472*8e6ec453SStephane Sezer         // If we are remote, we can only return results if we are connected
473*8e6ec453SStephane Sezer         if (m_remote_platform_sp)
474*8e6ec453SStephane Sezer             match_count = m_remote_platform_sp->FindProcesses (match_info, process_infos);
475*8e6ec453SStephane Sezer     }
476*8e6ec453SStephane Sezer     return match_count;
477*8e6ec453SStephane Sezer }
478*8e6ec453SStephane Sezer 
479e996fd30SGreg Clayton bool
480e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
481e996fd30SGreg Clayton {
482e996fd30SGreg Clayton     if (idx == 0)
483e996fd30SGreg Clayton     {
48413b18261SZachary Turner         arch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
485e996fd30SGreg Clayton         return arch.IsValid();
486e996fd30SGreg Clayton     }
487542e4075SGreg Clayton     else if (idx == 1)
488542e4075SGreg Clayton     {
489542e4075SGreg Clayton         // If the default host architecture is 64-bit, look for a 32-bit variant
49013b18261SZachary Turner         ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
491542e4075SGreg Clayton         if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit())
492542e4075SGreg Clayton         {
49313b18261SZachary Turner             arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
494542e4075SGreg Clayton             return arch.IsValid();
495542e4075SGreg Clayton         }
496542e4075SGreg Clayton     }
497e996fd30SGreg Clayton     return false;
498e996fd30SGreg Clayton }
499ecc11474SStephen Wilson 
500ecc11474SStephen Wilson void
501ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm)
502ecc11474SStephen Wilson {
5033be69dacSDaniel Malea     Platform::GetStatus(strm);
504ecc11474SStephen Wilson 
505b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX
506b2f1fb29SVirgile Bello     struct utsname un;
507b2f1fb29SVirgile Bello 
5083be69dacSDaniel Malea     if (uname(&un))
5093be69dacSDaniel Malea         return;
5103be69dacSDaniel Malea 
5113be69dacSDaniel Malea     strm.Printf ("    Kernel: %s\n", un.sysname);
5123be69dacSDaniel Malea     strm.Printf ("   Release: %s\n", un.release);
5133be69dacSDaniel Malea     strm.Printf ("   Version: %s\n", un.version);
514b2f1fb29SVirgile Bello #endif
515ecc11474SStephen Wilson }
516ecc11474SStephen Wilson 
517ecc11474SStephen Wilson size_t
518ecc11474SStephen Wilson PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target,
519ecc11474SStephen Wilson                                                 BreakpointSite *bp_site)
520ecc11474SStephen Wilson {
521ecc11474SStephen Wilson     ArchSpec arch = target.GetArchitecture();
52228041352SGreg Clayton     const uint8_t *trap_opcode = NULL;
52328041352SGreg Clayton     size_t trap_opcode_size = 0;
524ecc11474SStephen Wilson 
525906e9acfSGreg Clayton     switch (arch.GetMachine())
526ecc11474SStephen Wilson     {
527ecc11474SStephen Wilson     default:
528ecc11474SStephen Wilson         assert(false && "CPU type not supported!");
529ecc11474SStephen Wilson         break;
530ecc11474SStephen Wilson 
5312afc5966STodd Fiala     case llvm::Triple::aarch64:
5322afc5966STodd Fiala         {
5332afc5966STodd Fiala             static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 };
5342afc5966STodd Fiala             trap_opcode = g_aarch64_opcode;
5352afc5966STodd Fiala             trap_opcode_size = sizeof(g_aarch64_opcode);
5362afc5966STodd Fiala         }
5372afc5966STodd Fiala         break;
538906e9acfSGreg Clayton     case llvm::Triple::x86:
539906e9acfSGreg Clayton     case llvm::Triple::x86_64:
54028041352SGreg Clayton         {
54128041352SGreg Clayton             static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
54228041352SGreg Clayton             trap_opcode = g_i386_breakpoint_opcode;
54328041352SGreg Clayton             trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
54428041352SGreg Clayton         }
545ecc11474SStephen Wilson         break;
546906e9acfSGreg Clayton     case llvm::Triple::hexagon:
5478006d319SDeepak Panickal         {
5488006d319SDeepak Panickal             static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 };
5498006d319SDeepak Panickal             trap_opcode = g_hex_opcode;
5508006d319SDeepak Panickal             trap_opcode_size = sizeof(g_hex_opcode);
5518006d319SDeepak Panickal         }
5528006d319SDeepak Panickal         break;
5532586e94bSJason Molenda     case llvm::Triple::arm:
5542586e94bSJason Molenda         {
5552586e94bSJason Molenda             // The ARM reference recommends the use of 0xe7fddefe and 0xdefe
5562586e94bSJason Molenda             // but the linux kernel does otherwise.
5572586e94bSJason Molenda             static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 };
5582586e94bSJason Molenda             static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde };
5592586e94bSJason Molenda 
5602586e94bSJason Molenda             lldb::BreakpointLocationSP bp_loc_sp (bp_site->GetOwnerAtIndex (0));
5612586e94bSJason Molenda             AddressClass addr_class = eAddressClassUnknown;
5622586e94bSJason Molenda 
5632586e94bSJason Molenda             if (bp_loc_sp)
5642586e94bSJason Molenda                 addr_class = bp_loc_sp->GetAddress ().GetAddressClass ();
5652586e94bSJason Molenda 
5662586e94bSJason Molenda             if (addr_class == eAddressClassCodeAlternateISA
5672586e94bSJason Molenda                 || (addr_class == eAddressClassUnknown
5682586e94bSJason Molenda                     && bp_loc_sp->GetAddress().GetOffset() & 1))
5692586e94bSJason Molenda             {
5702586e94bSJason Molenda                 trap_opcode = g_thumb_breakpoint_opcode;
5712586e94bSJason Molenda                 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
5722586e94bSJason Molenda             }
5732586e94bSJason Molenda             else
5742586e94bSJason Molenda             {
5752586e94bSJason Molenda                 trap_opcode = g_arm_breakpoint_opcode;
5762586e94bSJason Molenda                 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
5772586e94bSJason Molenda             }
5782586e94bSJason Molenda         }
5792586e94bSJason Molenda         break;
580ecc11474SStephen Wilson     }
581ecc11474SStephen Wilson 
58228041352SGreg Clayton     if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
58328041352SGreg Clayton         return trap_opcode_size;
58428041352SGreg Clayton     return 0;
58528041352SGreg Clayton }
58628041352SGreg Clayton 
587348fb385STodd Fiala int32_t
588348fb385STodd Fiala PlatformLinux::GetResumeCountForLaunchInfo (ProcessLaunchInfo &launch_info)
58928041352SGreg Clayton {
590348fb385STodd Fiala     int32_t resume_count = 0;
59128041352SGreg Clayton 
592348fb385STodd Fiala     // Always resume past the initial stop when we use eLaunchFlagDebug
593348fb385STodd Fiala     if (launch_info.GetFlags ().Test (eLaunchFlagDebug))
59428041352SGreg Clayton     {
595348fb385STodd Fiala         // Resume past the stop for the final exec into the true inferior.
596348fb385STodd Fiala         ++resume_count;
597348fb385STodd Fiala     }
598015d818bSTodd Fiala 
599348fb385STodd Fiala     // If we're not launching a shell, we're done.
60010687b0eSZachary Turner     const FileSpec &shell = launch_info.GetShell();
60110687b0eSZachary Turner     if (!shell)
602348fb385STodd Fiala         return resume_count;
603348fb385STodd Fiala 
60410687b0eSZachary Turner     std::string shell_string = shell.GetPath();
605348fb385STodd Fiala     // We're in a shell, so for sure we have to resume past the shell exec.
606348fb385STodd Fiala     ++resume_count;
607348fb385STodd Fiala 
608348fb385STodd Fiala     // Figure out what shell we're planning on using.
60910687b0eSZachary Turner     const char *shell_name = strrchr (shell_string.c_str(), '/');
610348fb385STodd Fiala     if (shell_name == NULL)
61110687b0eSZachary Turner         shell_name = shell_string.c_str();
61228041352SGreg Clayton     else
613348fb385STodd Fiala         shell_name++;
614348fb385STodd Fiala 
615348fb385STodd Fiala     if (strcmp (shell_name, "csh") == 0
616348fb385STodd Fiala              || strcmp (shell_name, "tcsh") == 0
617348fb385STodd Fiala              || strcmp (shell_name, "zsh") == 0
618348fb385STodd Fiala              || strcmp (shell_name, "sh") == 0)
61928041352SGreg Clayton     {
620348fb385STodd Fiala         // These shells seem to re-exec themselves.  Add another resume.
621348fb385STodd Fiala         ++resume_count;
622ecc11474SStephen Wilson     }
62313e8e1c3SJohnny Chen 
624348fb385STodd Fiala     return resume_count;
625348fb385STodd Fiala }
626348fb385STodd Fiala 
627348fb385STodd Fiala bool
628348fb385STodd Fiala PlatformLinux::UseLlgsForLocalDebugging ()
629348fb385STodd Fiala {
630348fb385STodd Fiala     PlatformLinuxPropertiesSP properties_sp = GetGlobalProperties ();
631348fb385STodd Fiala     assert (properties_sp && "global properties shared pointer is null");
632348fb385STodd Fiala     return properties_sp ? properties_sp->GetUseLlgsForLocal () : false;
633348fb385STodd Fiala }
634348fb385STodd Fiala 
635015d818bSTodd Fiala bool
636015d818bSTodd Fiala PlatformLinux::CanDebugProcess ()
637015d818bSTodd Fiala {
638015d818bSTodd Fiala     if (IsHost ())
639348fb385STodd Fiala     {
640348fb385STodd Fiala         // The platform only does local debugging (i.e. uses llgs) when the setting indicates we do that.
641348fb385STodd Fiala         // Otherwise, we'll use ProcessLinux/ProcessPOSIX to handle with ProcessMonitor.
642348fb385STodd Fiala         return UseLlgsForLocalDebugging ();
643348fb385STodd Fiala     }
644348fb385STodd Fiala     else
645348fb385STodd Fiala     {
646015d818bSTodd Fiala         // If we're connected, we can debug.
647015d818bSTodd Fiala         return IsConnected ();
648015d818bSTodd Fiala     }
649348fb385STodd Fiala }
650015d818bSTodd Fiala 
651348fb385STodd Fiala // For local debugging, Linux will override the debug logic to use llgs-launch rather than
652348fb385STodd Fiala // lldb-launch, llgs-attach.  This differs from current lldb-launch, debugserver-attach
653348fb385STodd Fiala // approach on MacOSX.
65413e8e1c3SJohnny Chen lldb::ProcessSP
655348fb385STodd Fiala PlatformLinux::DebugProcess (ProcessLaunchInfo &launch_info,
65613e8e1c3SJohnny Chen                              Debugger &debugger,
657348fb385STodd Fiala                              Target *target,       // Can be NULL, if NULL create a new target, else use existing one
65813e8e1c3SJohnny Chen                              Error &error)
65913e8e1c3SJohnny Chen {
660348fb385STodd Fiala     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
661348fb385STodd Fiala     if (log)
662348fb385STodd Fiala         log->Printf ("PlatformLinux::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target));
66328041352SGreg Clayton 
664348fb385STodd Fiala     // If we're a remote host, use standard behavior from parent class.
665348fb385STodd Fiala     if (!IsHost ())
6668012cadbSGreg Clayton         return PlatformPOSIX::DebugProcess (launch_info, debugger, target, error);
667348fb385STodd Fiala 
668348fb385STodd Fiala     //
669348fb385STodd Fiala     // For local debugging, we'll insist on having ProcessGDBRemote create the process.
670348fb385STodd Fiala     //
671348fb385STodd Fiala 
672348fb385STodd Fiala     ProcessSP process_sp;
673348fb385STodd Fiala 
674348fb385STodd Fiala     // Ensure we're using llgs for local debugging.
675348fb385STodd Fiala     if (!UseLlgsForLocalDebugging ())
676348fb385STodd Fiala     {
677348fb385STodd 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");
678348fb385STodd Fiala         error.SetErrorString ("attempted to start gdb-remote-based debugging for local process but platform.plugin.linux.use-llgs-for-local is false");
679348fb385STodd Fiala         return process_sp;
680348fb385STodd Fiala     }
681348fb385STodd Fiala 
682348fb385STodd Fiala     // Make sure we stop at the entry point
683348fb385STodd Fiala     launch_info.GetFlags ().Set (eLaunchFlagDebug);
684348fb385STodd Fiala 
685348fb385STodd Fiala     // We always launch the process we are going to debug in a separate process
686348fb385STodd Fiala     // group, since then we can handle ^C interrupts ourselves w/o having to worry
687348fb385STodd Fiala     // about the target getting them as well.
688348fb385STodd Fiala     launch_info.SetLaunchInSeparateProcessGroup(true);
689348fb385STodd Fiala 
690348fb385STodd Fiala     // Ensure we have a target.
691348fb385STodd Fiala     if (target == nullptr)
692348fb385STodd Fiala     {
693348fb385STodd Fiala         if (log)
694348fb385STodd Fiala             log->Printf ("PlatformLinux::%s creating new target", __FUNCTION__);
695348fb385STodd Fiala 
696348fb385STodd Fiala         TargetSP new_target_sp;
69728041352SGreg Clayton         error = debugger.GetTargetList().CreateTarget (debugger,
698348fb385STodd Fiala                                                        nullptr,
699348fb385STodd Fiala                                                        nullptr,
70028041352SGreg Clayton                                                        false,
701348fb385STodd Fiala                                                        nullptr,
70228041352SGreg Clayton                                                        new_target_sp);
703348fb385STodd Fiala         if (error.Fail ())
704348fb385STodd Fiala         {
705348fb385STodd Fiala             if (log)
706348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s failed to create new target: %s", __FUNCTION__, error.AsCString ());
707348fb385STodd Fiala             return process_sp;
708348fb385STodd Fiala         }
709348fb385STodd Fiala 
71028041352SGreg Clayton         target = new_target_sp.get();
711348fb385STodd Fiala         if (!target)
712348fb385STodd Fiala         {
713348fb385STodd Fiala             error.SetErrorString ("CreateTarget() returned nullptr");
714348fb385STodd Fiala             if (log)
715348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ());
716348fb385STodd Fiala             return process_sp;
717348fb385STodd Fiala         }
71828041352SGreg Clayton     }
71928041352SGreg Clayton     else
72028041352SGreg Clayton     {
721348fb385STodd Fiala         if (log)
722348fb385STodd Fiala             log->Printf ("PlatformLinux::%s using provided target", __FUNCTION__);
723348fb385STodd Fiala     }
724348fb385STodd Fiala 
725348fb385STodd Fiala     // Mark target as currently selected target.
72628041352SGreg Clayton     debugger.GetTargetList().SetSelectedTarget(target);
72728041352SGreg Clayton 
728348fb385STodd Fiala     // Now create the gdb-remote process.
729348fb385STodd Fiala     if (log)
730348fb385STodd Fiala         log->Printf ("PlatformLinux::%s having target create process with gdb-remote plugin", __FUNCTION__);
7318012cadbSGreg Clayton     process_sp = target->CreateProcess (launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr);
73228041352SGreg Clayton 
733348fb385STodd Fiala     if (!process_sp)
734348fb385STodd Fiala     {
735348fb385STodd Fiala         error.SetErrorString ("CreateProcess() failed for gdb-remote process");
736348fb385STodd Fiala         if (log)
737348fb385STodd Fiala             log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ());
738348fb385STodd Fiala         return process_sp;
739348fb385STodd Fiala     }
740348fb385STodd Fiala     else
741348fb385STodd Fiala     {
742348fb385STodd Fiala         if (log)
743348fb385STodd Fiala             log->Printf ("PlatformLinux::%s successfully created process", __FUNCTION__);
744348fb385STodd Fiala     }
745348fb385STodd Fiala 
746348fb385STodd Fiala     // Set the unix signals properly.
747348fb385STodd Fiala     process_sp->SetUnixSignals (Host::GetUnixSignals ());
748348fb385STodd Fiala 
749348fb385STodd Fiala     // Adjust launch for a hijacker.
750348fb385STodd Fiala     ListenerSP listener_sp;
751348fb385STodd Fiala     if (!launch_info.GetHijackListener ())
752348fb385STodd Fiala     {
753348fb385STodd Fiala         if (log)
754348fb385STodd Fiala             log->Printf ("PlatformLinux::%s setting up hijacker", __FUNCTION__);
755348fb385STodd Fiala 
756348fb385STodd Fiala         listener_sp.reset (new Listener("lldb.PlatformLinux.DebugProcess.hijack"));
757348fb385STodd Fiala         launch_info.SetHijackListener (listener_sp);
758348fb385STodd Fiala         process_sp->HijackProcessEvents (listener_sp.get ());
759348fb385STodd Fiala     }
760348fb385STodd Fiala 
761348fb385STodd Fiala     // Log file actions.
762348fb385STodd Fiala     if (log)
763348fb385STodd Fiala     {
764348fb385STodd Fiala         log->Printf ("PlatformLinux::%s launching process with the following file actions:", __FUNCTION__);
765348fb385STodd Fiala 
766348fb385STodd Fiala         StreamString stream;
767348fb385STodd Fiala         size_t i = 0;
768348fb385STodd Fiala         const FileAction *file_action;
769348fb385STodd Fiala         while ((file_action = launch_info.GetFileActionAtIndex (i++)) != nullptr)
770348fb385STodd Fiala         {
771348fb385STodd Fiala             file_action->Dump (stream);
772348fb385STodd Fiala             log->PutCString (stream.GetString().c_str ());
773348fb385STodd Fiala             stream.Clear();
774348fb385STodd Fiala         }
775348fb385STodd Fiala     }
776348fb385STodd Fiala 
777348fb385STodd Fiala     // Do the launch.
778348fb385STodd Fiala     error = process_sp->Launch(launch_info);
779348fb385STodd Fiala     if (error.Success ())
780348fb385STodd Fiala     {
781348fb385STodd Fiala         // Handle the hijacking of process events.
782348fb385STodd Fiala         if (listener_sp)
783348fb385STodd Fiala         {
784348fb385STodd Fiala             const StateType state = process_sp->WaitForProcessToStop (NULL, NULL, false, listener_sp.get());
785348fb385STodd Fiala             process_sp->RestoreProcessEvents();
786348fb385STodd Fiala 
787348fb385STodd Fiala             if (state == eStateStopped)
788348fb385STodd Fiala             {
789348fb385STodd Fiala                 if (log)
790348fb385STodd Fiala                     log->Printf ("PlatformLinux::%s pid %" PRIu64 " state %s\n",
791348fb385STodd Fiala                                  __FUNCTION__, process_sp->GetID (), StateAsCString (state));
792348fb385STodd Fiala             }
793348fb385STodd Fiala             else
794348fb385STodd Fiala             {
795348fb385STodd Fiala                 if (log)
796348fb385STodd Fiala                     log->Printf ("PlatformLinux::%s pid %" PRIu64 " state is not stopped - %s\n",
797348fb385STodd Fiala                                  __FUNCTION__, process_sp->GetID (), StateAsCString (state));
798348fb385STodd Fiala             }
799348fb385STodd Fiala         }
800348fb385STodd Fiala 
801348fb385STodd Fiala         // Hook up process PTY if we have one (which we should for local debugging with llgs).
802348fb385STodd Fiala         int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
803348fb385STodd Fiala         if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
804348fb385STodd Fiala         {
805348fb385STodd Fiala             process_sp->SetSTDIOFileDescriptor(pty_fd);
806348fb385STodd Fiala             if (log)
807348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s pid %" PRIu64 " hooked up STDIO pty to process", __FUNCTION__, process_sp->GetID ());
808348fb385STodd Fiala         }
809348fb385STodd Fiala         else
810348fb385STodd Fiala         {
811348fb385STodd Fiala             if (log)
812348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s pid %" PRIu64 " not using process STDIO pty", __FUNCTION__, process_sp->GetID ());
81328041352SGreg Clayton         }
81428041352SGreg Clayton     }
81528041352SGreg Clayton     else
81628041352SGreg Clayton     {
817348fb385STodd Fiala         if (log)
818348fb385STodd Fiala             log->Printf ("PlatformLinux::%s process launch failed: %s", __FUNCTION__, error.AsCString ());
819348fb385STodd Fiala         // FIXME figure out appropriate cleanup here.  Do we delete the target? Do we delete the process?  Does our caller do that?
82028041352SGreg Clayton     }
821348fb385STodd Fiala 
82228041352SGreg Clayton     return process_sp;
82313e8e1c3SJohnny Chen }
8242094dbf4SJason Molenda 
8252094dbf4SJason Molenda void
8262094dbf4SJason Molenda PlatformLinux::CalculateTrapHandlerSymbolNames ()
8272094dbf4SJason Molenda {
8282094dbf4SJason Molenda     m_trap_handlers.push_back (ConstString ("_sigtramp"));
8292094dbf4SJason Molenda }
830af245d11STodd Fiala 
831af245d11STodd Fiala Error
832af245d11STodd Fiala PlatformLinux::LaunchNativeProcess (
833af245d11STodd Fiala     ProcessLaunchInfo &launch_info,
834af245d11STodd Fiala     lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
835af245d11STodd Fiala     NativeProcessProtocolSP &process_sp)
836af245d11STodd Fiala {
8378da0bf3bSShawn Best #if !defined(__linux__) || defined(__ANDROID_NDK__)
838af245d11STodd Fiala     return Error("only implemented on Linux hosts");
839af245d11STodd Fiala #else
840af245d11STodd Fiala     if (!IsHost ())
841af245d11STodd Fiala         return Error("PlatformLinux::%s (): cannot launch a debug process when not the host", __FUNCTION__);
842af245d11STodd Fiala 
843af245d11STodd Fiala     // Retrieve the exe module.
844af245d11STodd Fiala     lldb::ModuleSP exe_module_sp;
8456edef204SOleksiy Vyalov     ModuleSpec exe_module_spec(launch_info.GetExecutableFile(), launch_info.GetArchitecture());
846af245d11STodd Fiala 
847af245d11STodd Fiala     Error error = ResolveExecutable (
8486edef204SOleksiy Vyalov         exe_module_spec,
849af245d11STodd Fiala         exe_module_sp,
850af245d11STodd Fiala         NULL);
851af245d11STodd Fiala 
852af245d11STodd Fiala     if (!error.Success ())
853af245d11STodd Fiala         return error;
854af245d11STodd Fiala 
855af245d11STodd Fiala     if (!exe_module_sp)
856af245d11STodd Fiala         return Error("exe_module_sp could not be resolved for %s", launch_info.GetExecutableFile ().GetPath ().c_str ());
857af245d11STodd Fiala 
858af245d11STodd Fiala     // Launch it for debugging
859af245d11STodd Fiala     error = NativeProcessLinux::LaunchProcess (
860af245d11STodd Fiala         exe_module_sp.get (),
861af245d11STodd Fiala         launch_info,
862af245d11STodd Fiala         native_delegate,
863af245d11STodd Fiala         process_sp);
864af245d11STodd Fiala 
865af245d11STodd Fiala     return error;
866af245d11STodd Fiala #endif
867af245d11STodd Fiala }
868af245d11STodd Fiala 
869af245d11STodd Fiala Error
870af245d11STodd Fiala PlatformLinux::AttachNativeProcess (lldb::pid_t pid,
871af245d11STodd Fiala                                     lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
872af245d11STodd Fiala                                     NativeProcessProtocolSP &process_sp)
873af245d11STodd Fiala {
8748da0bf3bSShawn Best #if !defined(__linux__) || defined(__ANDROID_NDK__)
875af245d11STodd Fiala     return Error("only implemented on Linux hosts");
876af245d11STodd Fiala #else
877af245d11STodd Fiala     if (!IsHost ())
878af245d11STodd Fiala         return Error("PlatformLinux::%s (): cannot attach to a debug process when not the host", __FUNCTION__);
879af245d11STodd Fiala 
880af245d11STodd Fiala     // Launch it for debugging
881af245d11STodd Fiala     return NativeProcessLinux::AttachToProcess (pid, native_delegate, process_sp);
882af245d11STodd Fiala #endif
883af245d11STodd Fiala }
884