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>
1796ad3de5SRobert Flack #include <vector>
18b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX
19ecc11474SStephen Wilson #include <sys/utsname.h>
20b2f1fb29SVirgile Bello #endif
21ecc11474SStephen Wilson 
22e996fd30SGreg Clayton // C++ Includes
23e996fd30SGreg Clayton // Other libraries and framework includes
24e996fd30SGreg Clayton // Project includes
252586e94bSJason Molenda #include "lldb/Breakpoint/BreakpointLocation.h"
2628041352SGreg Clayton #include "lldb/Core/Debugger.h"
27348fb385STodd Fiala #include "lldb/Core/Error.h"
28015d818bSTodd Fiala #include "lldb/Core/Log.h"
29e996fd30SGreg Clayton #include "lldb/Core/Module.h"
30e996fd30SGreg Clayton #include "lldb/Core/ModuleList.h"
311f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h"
32ecc11474SStephen Wilson #include "lldb/Core/PluginManager.h"
33348fb385STodd Fiala #include "lldb/Core/State.h"
34e996fd30SGreg Clayton #include "lldb/Core/StreamString.h"
35e996fd30SGreg Clayton #include "lldb/Host/FileSpec.h"
3613b18261SZachary Turner #include "lldb/Host/HostInfo.h"
37348fb385STodd Fiala #include "lldb/Interpreter/OptionValueProperties.h"
38348fb385STodd Fiala #include "lldb/Interpreter/Property.h"
39ecc11474SStephen Wilson #include "lldb/Target/Target.h"
40e996fd30SGreg Clayton #include "lldb/Target/Process.h"
41e996fd30SGreg Clayton 
42af245d11STodd Fiala #if defined(__linux__)
43af245d11STodd Fiala #include "../../Process/Linux/NativeProcessLinux.h"
44af245d11STodd Fiala #endif
45af245d11STodd Fiala 
4696ad3de5SRobert Flack // Define these constants from Linux mman.h for use when targetting
4796ad3de5SRobert Flack // remote linux systems even when host has different values.
4896ad3de5SRobert Flack #define MAP_PRIVATE 2
4996ad3de5SRobert Flack #define MAP_ANON 0x20
5096ad3de5SRobert Flack 
51e996fd30SGreg Clayton using namespace lldb;
52e996fd30SGreg Clayton using namespace lldb_private;
53db264a6dSTamas Berghammer using namespace lldb_private::platform_linux;
54e996fd30SGreg Clayton 
5528041352SGreg Clayton static uint32_t g_initialize_count = 0;
5628041352SGreg Clayton 
57281961a8STodd Fiala //------------------------------------------------------------------
58281961a8STodd Fiala /// Code to handle the PlatformLinux settings
59281961a8STodd Fiala //------------------------------------------------------------------
60281961a8STodd Fiala 
61348fb385STodd Fiala namespace
62348fb385STodd Fiala {
63348fb385STodd Fiala     enum
64348fb385STodd Fiala     {
65348fb385STodd Fiala         ePropertyUseLlgsForLocal = 0,
66348fb385STodd Fiala     };
67348fb385STodd Fiala 
68db264a6dSTamas Berghammer     class PlatformLinuxProperties : public Properties
69db264a6dSTamas Berghammer     {
70db264a6dSTamas Berghammer     public:
71db264a6dSTamas Berghammer         static ConstString&
72db264a6dSTamas Berghammer         GetSettingName ();
73db264a6dSTamas Berghammer 
74db264a6dSTamas Berghammer         PlatformLinuxProperties();
75db264a6dSTamas Berghammer 
76db264a6dSTamas Berghammer         virtual
77db264a6dSTamas Berghammer         ~PlatformLinuxProperties() = default;
78db264a6dSTamas Berghammer 
79db264a6dSTamas Berghammer         bool
80db264a6dSTamas Berghammer         GetUseLlgsForLocal() const;
81db264a6dSTamas Berghammer 
82db264a6dSTamas Berghammer     private:
83db264a6dSTamas Berghammer         static const PropertyDefinition*
84db264a6dSTamas Berghammer         GetStaticPropertyDefinitions();
85db264a6dSTamas Berghammer     };
86db264a6dSTamas Berghammer 
87db264a6dSTamas Berghammer     typedef std::shared_ptr<PlatformLinuxProperties> PlatformLinuxPropertiesSP;
88db264a6dSTamas Berghammer 
89db264a6dSTamas Berghammer } // anonymous namespace
90db264a6dSTamas Berghammer 
91db264a6dSTamas Berghammer PlatformLinuxProperties::PlatformLinuxProperties() :
92db264a6dSTamas Berghammer     Properties ()
93db264a6dSTamas Berghammer {
94db264a6dSTamas Berghammer     m_collection_sp.reset (new OptionValueProperties(GetSettingName ()));
95db264a6dSTamas Berghammer     m_collection_sp->Initialize (GetStaticPropertyDefinitions ());
96db264a6dSTamas Berghammer }
97db264a6dSTamas Berghammer 
98db264a6dSTamas Berghammer ConstString&
99db264a6dSTamas Berghammer PlatformLinuxProperties::GetSettingName ()
100db264a6dSTamas Berghammer {
101db264a6dSTamas Berghammer     static ConstString g_setting_name("linux");
102db264a6dSTamas Berghammer     return g_setting_name;
103db264a6dSTamas Berghammer }
104db264a6dSTamas Berghammer 
105db264a6dSTamas Berghammer bool
106db264a6dSTamas Berghammer PlatformLinuxProperties::GetUseLlgsForLocal() const
107db264a6dSTamas Berghammer {
108db264a6dSTamas Berghammer     const uint32_t idx = ePropertyUseLlgsForLocal;
109db264a6dSTamas Berghammer     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, GetStaticPropertyDefinitions()[idx].default_uint_value != 0);
110db264a6dSTamas Berghammer }
111db264a6dSTamas Berghammer 
112348fb385STodd Fiala const PropertyDefinition*
113db264a6dSTamas Berghammer PlatformLinuxProperties::GetStaticPropertyDefinitions()
114348fb385STodd Fiala {
115281961a8STodd Fiala     static PropertyDefinition
116281961a8STodd Fiala     g_properties[] =
117281961a8STodd Fiala     {
118bc477ddeSVince Harron         { "use-llgs-for-local" , OptionValue::eTypeBoolean, true, true, NULL, NULL, "Control whether the platform uses llgs for local debug sessions." },
119281961a8STodd Fiala         {  NULL        , OptionValue::eTypeInvalid, false, 0  , NULL, NULL, NULL  }
120281961a8STodd Fiala     };
121281961a8STodd Fiala 
122bc477ddeSVince Harron     // Allow environment variable to disable llgs-local.
123bc477ddeSVince Harron     if (getenv("PLATFORM_LINUX_DISABLE_LLGS_LOCAL"))
124bc477ddeSVince Harron         g_properties[ePropertyUseLlgsForLocal].default_uint_value = false;
125281961a8STodd Fiala 
126348fb385STodd Fiala     return g_properties;
127348fb385STodd Fiala }
128281961a8STodd Fiala 
129281961a8STodd Fiala static const PlatformLinuxPropertiesSP &
130281961a8STodd Fiala GetGlobalProperties()
131281961a8STodd Fiala {
132281961a8STodd Fiala     static PlatformLinuxPropertiesSP g_settings_sp;
133281961a8STodd Fiala     if (!g_settings_sp)
134281961a8STodd Fiala         g_settings_sp.reset (new PlatformLinuxProperties ());
135281961a8STodd Fiala     return g_settings_sp;
136281961a8STodd Fiala }
137281961a8STodd Fiala 
138281961a8STodd Fiala void
139db264a6dSTamas Berghammer PlatformLinux::DebuggerInitialize (Debugger &debugger)
140281961a8STodd Fiala {
141281961a8STodd Fiala     if (!PluginManager::GetSettingForPlatformPlugin (debugger, PlatformLinuxProperties::GetSettingName()))
142281961a8STodd Fiala     {
143281961a8STodd Fiala         const bool is_global_setting = true;
144281961a8STodd Fiala         PluginManager::CreateSettingForPlatformPlugin (debugger,
145281961a8STodd Fiala                                                        GetGlobalProperties()->GetValueProperties(),
146281961a8STodd Fiala                                                        ConstString ("Properties for the PlatformLinux plug-in."),
147281961a8STodd Fiala                                                        is_global_setting);
148281961a8STodd Fiala     }
149281961a8STodd Fiala }
150281961a8STodd Fiala 
151281961a8STodd Fiala 
152281961a8STodd Fiala //------------------------------------------------------------------
153281961a8STodd Fiala 
154615eb7e6SGreg Clayton PlatformSP
155b3a40ba8SGreg Clayton PlatformLinux::CreateInstance (bool force, const ArchSpec *arch)
156ecc11474SStephen Wilson {
157db264a6dSTamas Berghammer     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
158015d818bSTodd Fiala     if (log)
159015d818bSTodd Fiala     {
160015d818bSTodd Fiala         const char *arch_name;
161015d818bSTodd Fiala         if (arch && arch->GetArchitectureName ())
162015d818bSTodd Fiala             arch_name = arch->GetArchitectureName ();
163015d818bSTodd Fiala         else
164015d818bSTodd Fiala             arch_name = "<null>";
165015d818bSTodd Fiala 
166015d818bSTodd Fiala         const char *triple_cstr = arch ? arch->GetTriple ().getTriple ().c_str() : "<null>";
167015d818bSTodd Fiala 
168015d818bSTodd Fiala         log->Printf ("PlatformLinux::%s(force=%s, arch={%s,%s})", __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
169015d818bSTodd Fiala     }
170015d818bSTodd Fiala 
171b3a40ba8SGreg Clayton     bool create = force;
172b3a40ba8SGreg Clayton     if (create == false && arch && arch->IsValid())
173b3a40ba8SGreg Clayton     {
174b3a40ba8SGreg Clayton         const llvm::Triple &triple = arch->GetTriple();
17570512317SGreg Clayton         switch (triple.getOS())
17670512317SGreg Clayton         {
17770512317SGreg Clayton             case llvm::Triple::Linux:
178*b1da257aSTed Woodward                 create = true;
17970512317SGreg Clayton                 break;
18070512317SGreg Clayton 
181dbc6c0bbSGreg Clayton #if defined(__linux__)
182dbc6c0bbSGreg Clayton             // Only accept "unknown" for the OS if the host is linux and
1836a7f3338SBruce Mitchener             // it "unknown" wasn't specified (it was just returned because it
184dbc6c0bbSGreg Clayton             // was NOT specified)
185015d818bSTodd Fiala             case llvm::Triple::OSType::UnknownOS:
18670512317SGreg Clayton                 create = !arch->TripleOSWasSpecified();
18770512317SGreg Clayton                 break;
188dbc6c0bbSGreg Clayton #endif
18970512317SGreg Clayton             default:
19070512317SGreg Clayton                 break;
19170512317SGreg Clayton         }
19270512317SGreg Clayton     }
193015d818bSTodd Fiala 
194b3a40ba8SGreg Clayton     if (create)
195015d818bSTodd Fiala     {
196015d818bSTodd Fiala         if (log)
197015d818bSTodd Fiala             log->Printf ("PlatformLinux::%s() creating remote-linux platform", __FUNCTION__);
198615eb7e6SGreg Clayton         return PlatformSP(new PlatformLinux(false));
199015d818bSTodd Fiala     }
200015d818bSTodd Fiala 
201015d818bSTodd Fiala     if (log)
202015d818bSTodd Fiala         log->Printf ("PlatformLinux::%s() aborting creation of remote-linux platform", __FUNCTION__);
203015d818bSTodd Fiala 
204615eb7e6SGreg Clayton     return PlatformSP();
205ecc11474SStephen Wilson }
206ecc11474SStephen Wilson 
207ecc11474SStephen Wilson 
208db264a6dSTamas Berghammer ConstString
20957abc5d6SGreg Clayton PlatformLinux::GetPluginNameStatic (bool is_host)
210ecc11474SStephen Wilson {
21128041352SGreg Clayton     if (is_host)
21257abc5d6SGreg Clayton     {
21357abc5d6SGreg Clayton         static ConstString g_host_name(Platform::GetHostPlatformName ());
21457abc5d6SGreg Clayton         return g_host_name;
21557abc5d6SGreg Clayton     }
21628041352SGreg Clayton     else
21757abc5d6SGreg Clayton     {
21857abc5d6SGreg Clayton         static ConstString g_remote_name("remote-linux");
21957abc5d6SGreg Clayton         return g_remote_name;
22057abc5d6SGreg Clayton     }
22128041352SGreg Clayton }
22228041352SGreg Clayton 
22328041352SGreg Clayton const char *
22428041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic (bool is_host)
22528041352SGreg Clayton {
22628041352SGreg Clayton     if (is_host)
22728041352SGreg Clayton         return "Local Linux user platform plug-in.";
22828041352SGreg Clayton     else
22928041352SGreg Clayton         return "Remote Linux user platform plug-in.";
230ecc11474SStephen Wilson }
231ecc11474SStephen Wilson 
232db264a6dSTamas Berghammer ConstString
23357abc5d6SGreg Clayton PlatformLinux::GetPluginName()
23457abc5d6SGreg Clayton {
23557abc5d6SGreg Clayton     return GetPluginNameStatic(IsHost());
23657abc5d6SGreg Clayton }
23757abc5d6SGreg Clayton 
238e996fd30SGreg Clayton void
239e996fd30SGreg Clayton PlatformLinux::Initialize ()
240e996fd30SGreg Clayton {
2413c4f89d7STamas Berghammer     PlatformPOSIX::Initialize ();
2423c4f89d7STamas Berghammer 
24328041352SGreg Clayton     if (g_initialize_count++ == 0)
244ecc11474SStephen Wilson     {
2451c6a1ea9STamas Berghammer #if defined(__linux__) && !defined(__ANDROID__)
24628041352SGreg Clayton         PlatformSP default_platform_sp (new PlatformLinux(true));
24713b18261SZachary Turner         default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
248615eb7e6SGreg Clayton         Platform::SetHostPlatform (default_platform_sp);
24928041352SGreg Clayton #endif
25057abc5d6SGreg Clayton         PluginManager::RegisterPlugin(PlatformLinux::GetPluginNameStatic(false),
25128041352SGreg Clayton                                       PlatformLinux::GetPluginDescriptionStatic(false),
252281961a8STodd Fiala                                       PlatformLinux::CreateInstance,
253281961a8STodd Fiala                                       PlatformLinux::DebuggerInitialize);
254ecc11474SStephen Wilson     }
255e996fd30SGreg Clayton }
256e996fd30SGreg Clayton 
257e996fd30SGreg Clayton void
258e996fd30SGreg Clayton PlatformLinux::Terminate ()
259e996fd30SGreg Clayton {
26028041352SGreg Clayton     if (g_initialize_count > 0)
26128041352SGreg Clayton     {
26228041352SGreg Clayton         if (--g_initialize_count == 0)
26328041352SGreg Clayton         {
26428041352SGreg Clayton             PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance);
265e996fd30SGreg Clayton         }
26628041352SGreg Clayton     }
2673c4f89d7STamas Berghammer 
2683c4f89d7STamas Berghammer     PlatformPOSIX::Terminate ();
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         {
3066474721cSOleksiy Vyalov             error = GetCachedExecutable (resolved_module_spec, exe_module_sp, nullptr, *m_remote_platform_sp);
30728041352SGreg Clayton         }
30828041352SGreg Clayton         else
30928041352SGreg Clayton         {
31028041352SGreg Clayton             // We may connect to a process and use the provided executable (Don't use local $PATH).
311e996fd30SGreg Clayton 
3128012cadbSGreg Clayton             if (resolved_module_spec.GetFileSpec().Exists())
31328041352SGreg Clayton                 error.Clear();
31428041352SGreg Clayton             else
31528041352SGreg Clayton                 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path);
31628041352SGreg Clayton         }
31728041352SGreg Clayton     }
31828041352SGreg Clayton 
31928041352SGreg Clayton     if (error.Success())
320e996fd30SGreg Clayton     {
3218012cadbSGreg Clayton         if (resolved_module_spec.GetArchitecture().IsValid())
322e996fd30SGreg Clayton         {
3238012cadbSGreg Clayton             error = ModuleList::GetSharedModule (resolved_module_spec,
324e996fd30SGreg Clayton                                                  exe_module_sp,
325e996fd30SGreg Clayton                                                  NULL,
3260c90ef47SGreg Clayton                                                  NULL,
327e996fd30SGreg Clayton                                                  NULL);
3289f0013d8SMichael Sartain             if (error.Fail())
3299f0013d8SMichael Sartain             {
3309f0013d8SMichael Sartain                 // If we failed, it may be because the vendor and os aren't known. If that is the
3319f0013d8SMichael Sartain                 // case, try setting them to the host architecture and give it another try.
3328012cadbSGreg Clayton                 llvm::Triple &module_triple = resolved_module_spec.GetArchitecture().GetTriple();
3339f0013d8SMichael Sartain                 bool is_vendor_specified = (module_triple.getVendor() != llvm::Triple::UnknownVendor);
3349f0013d8SMichael Sartain                 bool is_os_specified = (module_triple.getOS() != llvm::Triple::UnknownOS);
3359f0013d8SMichael Sartain                 if (!is_vendor_specified || !is_os_specified)
3369f0013d8SMichael Sartain                 {
33713b18261SZachary Turner                     const llvm::Triple &host_triple = HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple();
3389f0013d8SMichael Sartain 
3399f0013d8SMichael Sartain                     if (!is_vendor_specified)
3409f0013d8SMichael Sartain                         module_triple.setVendorName (host_triple.getVendorName());
3419f0013d8SMichael Sartain                     if (!is_os_specified)
3429f0013d8SMichael Sartain                         module_triple.setOSName (host_triple.getOSName());
3439f0013d8SMichael Sartain 
3448012cadbSGreg Clayton                     error = ModuleList::GetSharedModule (resolved_module_spec,
3459f0013d8SMichael Sartain                                                          exe_module_sp,
3469f0013d8SMichael Sartain                                                          NULL,
3479f0013d8SMichael Sartain                                                          NULL,
3489f0013d8SMichael Sartain                                                          NULL);
3499f0013d8SMichael Sartain                 }
3509f0013d8SMichael Sartain             }
351e996fd30SGreg Clayton 
352e635db49SSean Callanan             // TODO find out why exe_module_sp might be NULL
353e635db49SSean Callanan             if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL)
354e996fd30SGreg Clayton             {
355e996fd30SGreg Clayton                 exe_module_sp.reset();
356b5ad4ec7SGreg Clayton                 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s",
3578012cadbSGreg Clayton                                                 resolved_module_spec.GetFileSpec().GetPath().c_str(),
3588012cadbSGreg Clayton                                                 resolved_module_spec.GetArchitecture().GetArchitectureName());
359e996fd30SGreg Clayton             }
360e996fd30SGreg Clayton         }
361e996fd30SGreg Clayton         else
362e996fd30SGreg Clayton         {
363e996fd30SGreg Clayton             // No valid architecture was specified, ask the platform for
364e996fd30SGreg Clayton             // the architectures that we should be using (in the correct order)
365e996fd30SGreg Clayton             // and see if we can find a match that way
366e996fd30SGreg Clayton             StreamString arch_names;
3678012cadbSGreg Clayton             for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, resolved_module_spec.GetArchitecture()); ++idx)
368e996fd30SGreg Clayton             {
3698012cadbSGreg Clayton                 error = ModuleList::GetSharedModule (resolved_module_spec,
370e996fd30SGreg Clayton                                                      exe_module_sp,
371e996fd30SGreg Clayton                                                      NULL,
3720c90ef47SGreg Clayton                                                      NULL,
373e996fd30SGreg Clayton                                                      NULL);
374e996fd30SGreg Clayton                 // Did we find an executable using one of the
375e996fd30SGreg Clayton                 if (error.Success())
376e996fd30SGreg Clayton                 {
377e996fd30SGreg Clayton                     if (exe_module_sp && exe_module_sp->GetObjectFile())
378e996fd30SGreg Clayton                         break;
379e996fd30SGreg Clayton                     else
380e996fd30SGreg Clayton                         error.SetErrorToGenericError();
381e996fd30SGreg Clayton                 }
382e996fd30SGreg Clayton 
383e996fd30SGreg Clayton                 if (idx > 0)
384e996fd30SGreg Clayton                     arch_names.PutCString (", ");
3858012cadbSGreg Clayton                 arch_names.PutCString (resolved_module_spec.GetArchitecture().GetArchitectureName());
386e996fd30SGreg Clayton             }
387e996fd30SGreg Clayton 
388e996fd30SGreg Clayton             if (error.Fail() || !exe_module_sp)
389e996fd30SGreg Clayton             {
3908012cadbSGreg Clayton                 if (resolved_module_spec.GetFileSpec().Readable())
39139945dccSGreg Clayton                 {
392b5ad4ec7SGreg Clayton                     error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
3938012cadbSGreg Clayton                                                     resolved_module_spec.GetFileSpec().GetPath().c_str(),
39457abc5d6SGreg Clayton                                                     GetPluginName().GetCString(),
395e996fd30SGreg Clayton                                                     arch_names.GetString().c_str());
396e996fd30SGreg Clayton                 }
39739945dccSGreg Clayton                 else
39839945dccSGreg Clayton                 {
3998012cadbSGreg Clayton                     error.SetErrorStringWithFormat("'%s' is not readable", resolved_module_spec.GetFileSpec().GetPath().c_str());
40039945dccSGreg Clayton                 }
40139945dccSGreg Clayton             }
402e996fd30SGreg Clayton         }
403e996fd30SGreg Clayton     }
404e996fd30SGreg Clayton 
405e996fd30SGreg Clayton     return error;
406e996fd30SGreg Clayton }
407e996fd30SGreg Clayton 
408e996fd30SGreg Clayton Error
409fc995725SSteve Pucci PlatformLinux::GetFileWithUUID (const FileSpec &platform_file,
41028041352SGreg Clayton                                 const UUID *uuid_ptr, FileSpec &local_file)
411e996fd30SGreg Clayton {
41228041352SGreg Clayton     if (IsRemote())
41328041352SGreg Clayton     {
41428041352SGreg Clayton         if (m_remote_platform_sp)
415fc995725SSteve Pucci             return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file);
41628041352SGreg Clayton     }
41728041352SGreg Clayton 
418e996fd30SGreg Clayton     // Default to the local case
419e996fd30SGreg Clayton     local_file = platform_file;
420e996fd30SGreg Clayton     return Error();
421e996fd30SGreg Clayton }
422e996fd30SGreg Clayton 
423e996fd30SGreg Clayton 
424e996fd30SGreg Clayton //------------------------------------------------------------------
425e996fd30SGreg Clayton /// Default Constructor
426e996fd30SGreg Clayton //------------------------------------------------------------------
42728041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) :
428015d818bSTodd Fiala     PlatformPOSIX(is_host)  // This is the local host platform
429e996fd30SGreg Clayton {
430e996fd30SGreg Clayton }
431e996fd30SGreg Clayton 
432e996fd30SGreg Clayton //------------------------------------------------------------------
433e996fd30SGreg Clayton /// Destructor.
434e996fd30SGreg Clayton ///
435e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be
436e996fd30SGreg Clayton /// inherited from by the plug-in instance.
437e996fd30SGreg Clayton //------------------------------------------------------------------
438e996fd30SGreg Clayton PlatformLinux::~PlatformLinux()
439e996fd30SGreg Clayton {
440e996fd30SGreg Clayton }
441e996fd30SGreg Clayton 
442e996fd30SGreg Clayton bool
44313e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
444e996fd30SGreg Clayton {
44528041352SGreg Clayton     bool success = false;
44628041352SGreg Clayton     if (IsHost())
44728041352SGreg Clayton     {
44828041352SGreg Clayton         success = Platform::GetProcessInfo (pid, process_info);
44928041352SGreg Clayton     }
45028041352SGreg Clayton     else
45128041352SGreg Clayton     {
45228041352SGreg Clayton         if (m_remote_platform_sp)
45328041352SGreg Clayton             success = m_remote_platform_sp->GetProcessInfo (pid, process_info);
45428041352SGreg Clayton     }
45528041352SGreg Clayton     return success;
456e996fd30SGreg Clayton }
457e996fd30SGreg Clayton 
4588e6ec453SStephane Sezer uint32_t
4598e6ec453SStephane Sezer PlatformLinux::FindProcesses (const ProcessInstanceInfoMatch &match_info,
4608e6ec453SStephane Sezer                               ProcessInstanceInfoList &process_infos)
4618e6ec453SStephane Sezer {
4628e6ec453SStephane Sezer     uint32_t match_count = 0;
4638e6ec453SStephane Sezer     if (IsHost())
4648e6ec453SStephane Sezer     {
4658e6ec453SStephane Sezer         // Let the base class figure out the host details
4668e6ec453SStephane Sezer         match_count = Platform::FindProcesses (match_info, process_infos);
4678e6ec453SStephane Sezer     }
4688e6ec453SStephane Sezer     else
4698e6ec453SStephane Sezer     {
4708e6ec453SStephane Sezer         // If we are remote, we can only return results if we are connected
4718e6ec453SStephane Sezer         if (m_remote_platform_sp)
4728e6ec453SStephane Sezer             match_count = m_remote_platform_sp->FindProcesses (match_info, process_infos);
4738e6ec453SStephane Sezer     }
4748e6ec453SStephane Sezer     return match_count;
4758e6ec453SStephane Sezer }
4768e6ec453SStephane Sezer 
477e996fd30SGreg Clayton bool
478e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
479e996fd30SGreg Clayton {
48096ad3de5SRobert Flack     static std::vector<ArchSpec> architectures = {
48196ad3de5SRobert Flack         ArchSpec("x86_64-unknown-linux-gnu"),
48296ad3de5SRobert Flack         ArchSpec("i386-unknown-linux-gnu"),
48396ad3de5SRobert Flack     };
48496ad3de5SRobert Flack     if (idx >= architectures.size())
485e996fd30SGreg Clayton         return false;
48696ad3de5SRobert Flack     arch = architectures[idx];
48796ad3de5SRobert Flack     return true;
488e996fd30SGreg Clayton }
489ecc11474SStephen Wilson 
490ecc11474SStephen Wilson void
491ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm)
492ecc11474SStephen Wilson {
4933be69dacSDaniel Malea     Platform::GetStatus(strm);
494ecc11474SStephen Wilson 
495b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX
496b743a803SStephane Sezer     // Display local kernel information only when we are running in host mode.
497b743a803SStephane Sezer     // Otherwise, we would end up printing non-Linux information (when running
498b743a803SStephane Sezer     // on Mac OS for example).
499b743a803SStephane Sezer     if (IsHost())
500b743a803SStephane Sezer     {
501b2f1fb29SVirgile Bello         struct utsname un;
502b2f1fb29SVirgile Bello 
5033be69dacSDaniel Malea         if (uname(&un))
5043be69dacSDaniel Malea             return;
5053be69dacSDaniel Malea 
5063be69dacSDaniel Malea         strm.Printf ("    Kernel: %s\n", un.sysname);
5073be69dacSDaniel Malea         strm.Printf ("   Release: %s\n", un.release);
5083be69dacSDaniel Malea         strm.Printf ("   Version: %s\n", un.version);
509b743a803SStephane Sezer     }
510b2f1fb29SVirgile Bello #endif
511ecc11474SStephen Wilson }
512ecc11474SStephen Wilson 
513ecc11474SStephen Wilson size_t
514ecc11474SStephen Wilson PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target,
515ecc11474SStephen Wilson                                                 BreakpointSite *bp_site)
516ecc11474SStephen Wilson {
517ecc11474SStephen Wilson     ArchSpec arch = target.GetArchitecture();
51828041352SGreg Clayton     const uint8_t *trap_opcode = NULL;
51928041352SGreg Clayton     size_t trap_opcode_size = 0;
520ecc11474SStephen Wilson 
521906e9acfSGreg Clayton     switch (arch.GetMachine())
522ecc11474SStephen Wilson     {
523ecc11474SStephen Wilson     default:
524ecc11474SStephen Wilson         assert(false && "CPU type not supported!");
525ecc11474SStephen Wilson         break;
526ecc11474SStephen Wilson 
5272afc5966STodd Fiala     case llvm::Triple::aarch64:
5282afc5966STodd Fiala         {
5292afc5966STodd Fiala             static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 };
5302afc5966STodd Fiala             trap_opcode = g_aarch64_opcode;
5312afc5966STodd Fiala             trap_opcode_size = sizeof(g_aarch64_opcode);
5322afc5966STodd Fiala         }
5332afc5966STodd Fiala         break;
534906e9acfSGreg Clayton     case llvm::Triple::x86:
535906e9acfSGreg Clayton     case llvm::Triple::x86_64:
53628041352SGreg Clayton         {
53728041352SGreg Clayton             static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
53828041352SGreg Clayton             trap_opcode = g_i386_breakpoint_opcode;
53928041352SGreg Clayton             trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
54028041352SGreg Clayton         }
541ecc11474SStephen Wilson         break;
542906e9acfSGreg Clayton     case llvm::Triple::hexagon:
5438006d319SDeepak Panickal         {
5448006d319SDeepak Panickal             static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 };
5458006d319SDeepak Panickal             trap_opcode = g_hex_opcode;
5468006d319SDeepak Panickal             trap_opcode_size = sizeof(g_hex_opcode);
5478006d319SDeepak Panickal         }
5488006d319SDeepak Panickal         break;
5492586e94bSJason Molenda     case llvm::Triple::arm:
5502586e94bSJason Molenda         {
5512586e94bSJason Molenda             // The ARM reference recommends the use of 0xe7fddefe and 0xdefe
5522586e94bSJason Molenda             // but the linux kernel does otherwise.
5532586e94bSJason Molenda             static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 };
5542586e94bSJason Molenda             static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde };
5552586e94bSJason Molenda 
5562586e94bSJason Molenda             lldb::BreakpointLocationSP bp_loc_sp (bp_site->GetOwnerAtIndex (0));
5572586e94bSJason Molenda             AddressClass addr_class = eAddressClassUnknown;
5582586e94bSJason Molenda 
5592586e94bSJason Molenda             if (bp_loc_sp)
5602586e94bSJason Molenda                 addr_class = bp_loc_sp->GetAddress ().GetAddressClass ();
5612586e94bSJason Molenda 
5622586e94bSJason Molenda             if (addr_class == eAddressClassCodeAlternateISA
56363c8be95STamas Berghammer                 || (addr_class == eAddressClassUnknown && (bp_site->GetLoadAddress() & 1)))
5642586e94bSJason Molenda             {
5652586e94bSJason Molenda                 trap_opcode = g_thumb_breakpoint_opcode;
5662586e94bSJason Molenda                 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
5672586e94bSJason Molenda             }
5682586e94bSJason Molenda             else
5692586e94bSJason Molenda             {
5702586e94bSJason Molenda                 trap_opcode = g_arm_breakpoint_opcode;
5712586e94bSJason Molenda                 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
5722586e94bSJason Molenda             }
5732586e94bSJason Molenda         }
5742586e94bSJason Molenda         break;
575c9335a3fSMohit K. Bhakkad     case llvm::Triple::mips64:
576c9335a3fSMohit K. Bhakkad         {
577c9335a3fSMohit K. Bhakkad             static const uint8_t g_hex_opcode[] = { 0x00, 0x00, 0x00, 0x0d };
578c9335a3fSMohit K. Bhakkad             trap_opcode = g_hex_opcode;
579c9335a3fSMohit K. Bhakkad             trap_opcode_size = sizeof(g_hex_opcode);
580c9335a3fSMohit K. Bhakkad         }
581c9335a3fSMohit K. Bhakkad         break;
5822c2acf96SMohit K. Bhakkad     case llvm::Triple::mips64el:
5832c2acf96SMohit K. Bhakkad         {
5842c2acf96SMohit K. Bhakkad             static const uint8_t g_hex_opcode[] = { 0x0d, 0x00, 0x00, 0x00 };
5852c2acf96SMohit K. Bhakkad             trap_opcode = g_hex_opcode;
5862c2acf96SMohit K. Bhakkad             trap_opcode_size = sizeof(g_hex_opcode);
5872c2acf96SMohit K. Bhakkad         }
5882c2acf96SMohit K. Bhakkad         break;
589ecc11474SStephen Wilson     }
590ecc11474SStephen Wilson 
59128041352SGreg Clayton     if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
59228041352SGreg Clayton         return trap_opcode_size;
59328041352SGreg Clayton     return 0;
59428041352SGreg Clayton }
59528041352SGreg Clayton 
596348fb385STodd Fiala int32_t
597348fb385STodd Fiala PlatformLinux::GetResumeCountForLaunchInfo (ProcessLaunchInfo &launch_info)
59828041352SGreg Clayton {
599348fb385STodd Fiala     int32_t resume_count = 0;
60028041352SGreg Clayton 
601348fb385STodd Fiala     // Always resume past the initial stop when we use eLaunchFlagDebug
602348fb385STodd Fiala     if (launch_info.GetFlags ().Test (eLaunchFlagDebug))
60328041352SGreg Clayton     {
604348fb385STodd Fiala         // Resume past the stop for the final exec into the true inferior.
605348fb385STodd Fiala         ++resume_count;
606348fb385STodd Fiala     }
607015d818bSTodd Fiala 
608348fb385STodd Fiala     // If we're not launching a shell, we're done.
60910687b0eSZachary Turner     const FileSpec &shell = launch_info.GetShell();
61010687b0eSZachary Turner     if (!shell)
611348fb385STodd Fiala         return resume_count;
612348fb385STodd Fiala 
61310687b0eSZachary Turner     std::string shell_string = shell.GetPath();
614348fb385STodd Fiala     // We're in a shell, so for sure we have to resume past the shell exec.
615348fb385STodd Fiala     ++resume_count;
616348fb385STodd Fiala 
617348fb385STodd Fiala     // Figure out what shell we're planning on using.
61810687b0eSZachary Turner     const char *shell_name = strrchr (shell_string.c_str(), '/');
619348fb385STodd Fiala     if (shell_name == NULL)
62010687b0eSZachary Turner         shell_name = shell_string.c_str();
62128041352SGreg Clayton     else
622348fb385STodd Fiala         shell_name++;
623348fb385STodd Fiala 
624348fb385STodd Fiala     if (strcmp (shell_name, "csh") == 0
625348fb385STodd Fiala              || strcmp (shell_name, "tcsh") == 0
626348fb385STodd Fiala              || strcmp (shell_name, "zsh") == 0
627348fb385STodd Fiala              || strcmp (shell_name, "sh") == 0)
62828041352SGreg Clayton     {
629348fb385STodd Fiala         // These shells seem to re-exec themselves.  Add another resume.
630348fb385STodd Fiala         ++resume_count;
631ecc11474SStephen Wilson     }
63213e8e1c3SJohnny Chen 
633348fb385STodd Fiala     return resume_count;
634348fb385STodd Fiala }
635348fb385STodd Fiala 
636348fb385STodd Fiala bool
637348fb385STodd Fiala PlatformLinux::UseLlgsForLocalDebugging ()
638348fb385STodd Fiala {
639348fb385STodd Fiala     PlatformLinuxPropertiesSP properties_sp = GetGlobalProperties ();
640348fb385STodd Fiala     assert (properties_sp && "global properties shared pointer is null");
641348fb385STodd Fiala     return properties_sp ? properties_sp->GetUseLlgsForLocal () : false;
642348fb385STodd Fiala }
643348fb385STodd Fiala 
644015d818bSTodd Fiala bool
645015d818bSTodd Fiala PlatformLinux::CanDebugProcess ()
646015d818bSTodd Fiala {
647015d818bSTodd Fiala     if (IsHost ())
648348fb385STodd Fiala     {
649348fb385STodd Fiala         // The platform only does local debugging (i.e. uses llgs) when the setting indicates we do that.
650348fb385STodd Fiala         // Otherwise, we'll use ProcessLinux/ProcessPOSIX to handle with ProcessMonitor.
651348fb385STodd Fiala         return UseLlgsForLocalDebugging ();
652348fb385STodd Fiala     }
653348fb385STodd Fiala     else
654348fb385STodd Fiala     {
655015d818bSTodd Fiala         // If we're connected, we can debug.
656015d818bSTodd Fiala         return IsConnected ();
657015d818bSTodd Fiala     }
658348fb385STodd Fiala }
659015d818bSTodd Fiala 
660348fb385STodd Fiala // For local debugging, Linux will override the debug logic to use llgs-launch rather than
661348fb385STodd Fiala // lldb-launch, llgs-attach.  This differs from current lldb-launch, debugserver-attach
662348fb385STodd Fiala // approach on MacOSX.
66313e8e1c3SJohnny Chen lldb::ProcessSP
664348fb385STodd Fiala PlatformLinux::DebugProcess (ProcessLaunchInfo &launch_info,
66513e8e1c3SJohnny Chen                              Debugger &debugger,
666348fb385STodd Fiala                              Target *target,       // Can be NULL, if NULL create a new target, else use existing one
66713e8e1c3SJohnny Chen                              Error &error)
66813e8e1c3SJohnny Chen {
669db264a6dSTamas Berghammer     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
670348fb385STodd Fiala     if (log)
671348fb385STodd Fiala         log->Printf ("PlatformLinux::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target));
67228041352SGreg Clayton 
673348fb385STodd Fiala     // If we're a remote host, use standard behavior from parent class.
674348fb385STodd Fiala     if (!IsHost ())
6758012cadbSGreg Clayton         return PlatformPOSIX::DebugProcess (launch_info, debugger, target, error);
676348fb385STodd Fiala 
677348fb385STodd Fiala     //
678348fb385STodd Fiala     // For local debugging, we'll insist on having ProcessGDBRemote create the process.
679348fb385STodd Fiala     //
680348fb385STodd Fiala 
681348fb385STodd Fiala     ProcessSP process_sp;
682348fb385STodd Fiala 
683348fb385STodd Fiala     // Ensure we're using llgs for local debugging.
684348fb385STodd Fiala     if (!UseLlgsForLocalDebugging ())
685348fb385STodd Fiala     {
686348fb385STodd 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");
687348fb385STodd Fiala         error.SetErrorString ("attempted to start gdb-remote-based debugging for local process but platform.plugin.linux.use-llgs-for-local is false");
688348fb385STodd Fiala         return process_sp;
689348fb385STodd Fiala     }
690348fb385STodd Fiala 
691348fb385STodd Fiala     // Make sure we stop at the entry point
692348fb385STodd Fiala     launch_info.GetFlags ().Set (eLaunchFlagDebug);
693348fb385STodd Fiala 
694348fb385STodd Fiala     // We always launch the process we are going to debug in a separate process
695348fb385STodd Fiala     // group, since then we can handle ^C interrupts ourselves w/o having to worry
696348fb385STodd Fiala     // about the target getting them as well.
697348fb385STodd Fiala     launch_info.SetLaunchInSeparateProcessGroup(true);
698348fb385STodd Fiala 
699348fb385STodd Fiala     // Ensure we have a target.
700348fb385STodd Fiala     if (target == nullptr)
701348fb385STodd Fiala     {
702348fb385STodd Fiala         if (log)
703348fb385STodd Fiala             log->Printf ("PlatformLinux::%s creating new target", __FUNCTION__);
704348fb385STodd Fiala 
705348fb385STodd Fiala         TargetSP new_target_sp;
70628041352SGreg Clayton         error = debugger.GetTargetList().CreateTarget (debugger,
707348fb385STodd Fiala                                                        nullptr,
708348fb385STodd Fiala                                                        nullptr,
70928041352SGreg Clayton                                                        false,
710348fb385STodd Fiala                                                        nullptr,
71128041352SGreg Clayton                                                        new_target_sp);
712348fb385STodd Fiala         if (error.Fail ())
713348fb385STodd Fiala         {
714348fb385STodd Fiala             if (log)
715348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s failed to create new target: %s", __FUNCTION__, error.AsCString ());
716348fb385STodd Fiala             return process_sp;
717348fb385STodd Fiala         }
718348fb385STodd Fiala 
71928041352SGreg Clayton         target = new_target_sp.get();
720348fb385STodd Fiala         if (!target)
721348fb385STodd Fiala         {
722348fb385STodd Fiala             error.SetErrorString ("CreateTarget() returned nullptr");
723348fb385STodd Fiala             if (log)
724348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ());
725348fb385STodd Fiala             return process_sp;
726348fb385STodd Fiala         }
72728041352SGreg Clayton     }
72828041352SGreg Clayton     else
72928041352SGreg Clayton     {
730348fb385STodd Fiala         if (log)
731348fb385STodd Fiala             log->Printf ("PlatformLinux::%s using provided target", __FUNCTION__);
732348fb385STodd Fiala     }
733348fb385STodd Fiala 
734348fb385STodd Fiala     // Mark target as currently selected target.
73528041352SGreg Clayton     debugger.GetTargetList().SetSelectedTarget(target);
73628041352SGreg Clayton 
737348fb385STodd Fiala     // Now create the gdb-remote process.
738348fb385STodd Fiala     if (log)
739348fb385STodd Fiala         log->Printf ("PlatformLinux::%s having target create process with gdb-remote plugin", __FUNCTION__);
7408012cadbSGreg Clayton     process_sp = target->CreateProcess (launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr);
74128041352SGreg Clayton 
742348fb385STodd Fiala     if (!process_sp)
743348fb385STodd Fiala     {
744348fb385STodd Fiala         error.SetErrorString ("CreateProcess() failed for gdb-remote process");
745348fb385STodd Fiala         if (log)
746348fb385STodd Fiala             log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ());
747348fb385STodd Fiala         return process_sp;
748348fb385STodd Fiala     }
749348fb385STodd Fiala     else
750348fb385STodd Fiala     {
751348fb385STodd Fiala         if (log)
752348fb385STodd Fiala             log->Printf ("PlatformLinux::%s successfully created process", __FUNCTION__);
753348fb385STodd Fiala     }
754348fb385STodd Fiala 
755348fb385STodd Fiala     // Set the unix signals properly.
756348fb385STodd Fiala     process_sp->SetUnixSignals (Host::GetUnixSignals ());
757348fb385STodd Fiala 
758348fb385STodd Fiala     // Adjust launch for a hijacker.
759348fb385STodd Fiala     ListenerSP listener_sp;
760348fb385STodd Fiala     if (!launch_info.GetHijackListener ())
761348fb385STodd Fiala     {
762348fb385STodd Fiala         if (log)
763348fb385STodd Fiala             log->Printf ("PlatformLinux::%s setting up hijacker", __FUNCTION__);
764348fb385STodd Fiala 
765348fb385STodd Fiala         listener_sp.reset (new Listener("lldb.PlatformLinux.DebugProcess.hijack"));
766348fb385STodd Fiala         launch_info.SetHijackListener (listener_sp);
767348fb385STodd Fiala         process_sp->HijackProcessEvents (listener_sp.get ());
768348fb385STodd Fiala     }
769348fb385STodd Fiala 
770348fb385STodd Fiala     // Log file actions.
771348fb385STodd Fiala     if (log)
772348fb385STodd Fiala     {
773348fb385STodd Fiala         log->Printf ("PlatformLinux::%s launching process with the following file actions:", __FUNCTION__);
774348fb385STodd Fiala 
775348fb385STodd Fiala         StreamString stream;
776348fb385STodd Fiala         size_t i = 0;
777348fb385STodd Fiala         const FileAction *file_action;
778348fb385STodd Fiala         while ((file_action = launch_info.GetFileActionAtIndex (i++)) != nullptr)
779348fb385STodd Fiala         {
780348fb385STodd Fiala             file_action->Dump (stream);
781348fb385STodd Fiala             log->PutCString (stream.GetString().c_str ());
782348fb385STodd Fiala             stream.Clear();
783348fb385STodd Fiala         }
784348fb385STodd Fiala     }
785348fb385STodd Fiala 
786348fb385STodd Fiala     // Do the launch.
787348fb385STodd Fiala     error = process_sp->Launch(launch_info);
788348fb385STodd Fiala     if (error.Success ())
789348fb385STodd Fiala     {
790348fb385STodd Fiala         // Handle the hijacking of process events.
791348fb385STodd Fiala         if (listener_sp)
792348fb385STodd Fiala         {
793348fb385STodd Fiala             const StateType state = process_sp->WaitForProcessToStop (NULL, NULL, false, listener_sp.get());
794348fb385STodd Fiala 
795348fb385STodd Fiala             if (state == eStateStopped)
796348fb385STodd Fiala             {
797348fb385STodd Fiala                 if (log)
798348fb385STodd Fiala                     log->Printf ("PlatformLinux::%s pid %" PRIu64 " state %s\n",
799348fb385STodd Fiala                                  __FUNCTION__, process_sp->GetID (), StateAsCString (state));
800348fb385STodd Fiala             }
801348fb385STodd Fiala             else
802348fb385STodd Fiala             {
803348fb385STodd Fiala                 if (log)
804348fb385STodd Fiala                     log->Printf ("PlatformLinux::%s pid %" PRIu64 " state is not stopped - %s\n",
805348fb385STodd Fiala                                  __FUNCTION__, process_sp->GetID (), StateAsCString (state));
806348fb385STodd Fiala             }
807348fb385STodd Fiala         }
808348fb385STodd Fiala 
809348fb385STodd Fiala         // Hook up process PTY if we have one (which we should for local debugging with llgs).
810348fb385STodd Fiala         int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
811348fb385STodd Fiala         if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
812348fb385STodd Fiala         {
813348fb385STodd Fiala             process_sp->SetSTDIOFileDescriptor(pty_fd);
814348fb385STodd Fiala             if (log)
815348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s pid %" PRIu64 " hooked up STDIO pty to process", __FUNCTION__, process_sp->GetID ());
816348fb385STodd Fiala         }
817348fb385STodd Fiala         else
818348fb385STodd Fiala         {
819348fb385STodd Fiala             if (log)
820348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s pid %" PRIu64 " not using process STDIO pty", __FUNCTION__, process_sp->GetID ());
82128041352SGreg Clayton         }
82228041352SGreg Clayton     }
82328041352SGreg Clayton     else
82428041352SGreg Clayton     {
825348fb385STodd Fiala         if (log)
826348fb385STodd Fiala             log->Printf ("PlatformLinux::%s process launch failed: %s", __FUNCTION__, error.AsCString ());
827348fb385STodd Fiala         // FIXME figure out appropriate cleanup here.  Do we delete the target? Do we delete the process?  Does our caller do that?
82828041352SGreg Clayton     }
829348fb385STodd Fiala 
83028041352SGreg Clayton     return process_sp;
83113e8e1c3SJohnny Chen }
8322094dbf4SJason Molenda 
8332094dbf4SJason Molenda void
8342094dbf4SJason Molenda PlatformLinux::CalculateTrapHandlerSymbolNames ()
8352094dbf4SJason Molenda {
8362094dbf4SJason Molenda     m_trap_handlers.push_back (ConstString ("_sigtramp"));
8372094dbf4SJason Molenda }
838af245d11STodd Fiala 
839af245d11STodd Fiala Error
840db264a6dSTamas Berghammer PlatformLinux::LaunchNativeProcess (ProcessLaunchInfo &launch_info,
841db264a6dSTamas Berghammer                                     NativeProcessProtocol::NativeDelegate &native_delegate,
842af245d11STodd Fiala                                     NativeProcessProtocolSP &process_sp)
843af245d11STodd Fiala {
844c6ec76e3STamas Berghammer #if !defined(__linux__)
845c6ec76e3STamas Berghammer     return Error("Only implemented on Linux hosts");
846c6ec76e3STamas Berghammer #else
847af245d11STodd Fiala     if (!IsHost ())
848af245d11STodd Fiala         return Error("PlatformLinux::%s (): cannot launch a debug process when not the host", __FUNCTION__);
849af245d11STodd Fiala 
850af245d11STodd Fiala     // Retrieve the exe module.
851af245d11STodd Fiala     lldb::ModuleSP exe_module_sp;
8526edef204SOleksiy Vyalov     ModuleSpec exe_module_spec(launch_info.GetExecutableFile(), launch_info.GetArchitecture());
853af245d11STodd Fiala 
854af245d11STodd Fiala     Error error = ResolveExecutable (
8556edef204SOleksiy Vyalov         exe_module_spec,
856af245d11STodd Fiala         exe_module_sp,
857af245d11STodd Fiala         NULL);
858af245d11STodd Fiala 
859af245d11STodd Fiala     if (!error.Success ())
860af245d11STodd Fiala         return error;
861af245d11STodd Fiala 
862af245d11STodd Fiala     if (!exe_module_sp)
863af245d11STodd Fiala         return Error("exe_module_sp could not be resolved for %s", launch_info.GetExecutableFile ().GetPath ().c_str ());
864af245d11STodd Fiala 
865af245d11STodd Fiala     // Launch it for debugging
86647b11c61STamas Berghammer     error = process_linux::NativeProcessLinux::LaunchProcess (
867af245d11STodd Fiala         exe_module_sp.get (),
868af245d11STodd Fiala         launch_info,
869af245d11STodd Fiala         native_delegate,
870af245d11STodd Fiala         process_sp);
871af245d11STodd Fiala 
872af245d11STodd Fiala     return error;
873c6ec76e3STamas Berghammer #endif
874af245d11STodd Fiala }
875af245d11STodd Fiala 
876af245d11STodd Fiala Error
877af245d11STodd Fiala PlatformLinux::AttachNativeProcess (lldb::pid_t pid,
878db264a6dSTamas Berghammer                                     NativeProcessProtocol::NativeDelegate &native_delegate,
879af245d11STodd Fiala                                     NativeProcessProtocolSP &process_sp)
880af245d11STodd Fiala {
881c6ec76e3STamas Berghammer #if !defined(__linux__)
882c6ec76e3STamas Berghammer     return Error("Only implemented on Linux hosts");
883c6ec76e3STamas Berghammer #else
884af245d11STodd Fiala     if (!IsHost ())
885af245d11STodd Fiala         return Error("PlatformLinux::%s (): cannot attach to a debug process when not the host", __FUNCTION__);
886af245d11STodd Fiala 
887af245d11STodd Fiala     // Launch it for debugging
88847b11c61STamas Berghammer     return process_linux::NativeProcessLinux::AttachToProcess (pid, native_delegate, process_sp);
889c6ec76e3STamas Berghammer #endif
890af245d11STodd Fiala }
89196ad3de5SRobert Flack 
89296ad3de5SRobert Flack uint64_t
89396ad3de5SRobert Flack PlatformLinux::ConvertMmapFlagsToPlatform(unsigned flags)
89496ad3de5SRobert Flack {
89596ad3de5SRobert Flack     uint64_t flags_platform = 0;
89696ad3de5SRobert Flack     if (flags & eMmapFlagsPrivate)
89796ad3de5SRobert Flack         flags_platform |= MAP_PRIVATE;
89896ad3de5SRobert Flack     if (flags & eMmapFlagsAnon)
89996ad3de5SRobert Flack         flags_platform |= MAP_ANON;
90096ad3de5SRobert Flack     return flags_platform;
90196ad3de5SRobert Flack }
902