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 
10e996fd30SGreg Clayton #include "PlatformLinux.h"
11b2f1fb29SVirgile Bello #include "lldb/Host/Config.h"
12e996fd30SGreg Clayton 
13e996fd30SGreg Clayton // C Includes
14ecc11474SStephen Wilson #include <stdio.h>
15b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX
16ecc11474SStephen Wilson #include <sys/utsname.h>
17b2f1fb29SVirgile Bello #endif
18ecc11474SStephen Wilson 
19e996fd30SGreg Clayton // C++ Includes
20e996fd30SGreg Clayton // Other libraries and framework includes
21e996fd30SGreg Clayton // Project includes
222586e94bSJason Molenda #include "lldb/Breakpoint/BreakpointLocation.h"
2328041352SGreg Clayton #include "lldb/Core/Debugger.h"
24348fb385STodd Fiala #include "lldb/Core/Error.h"
25015d818bSTodd Fiala #include "lldb/Core/Log.h"
26e996fd30SGreg Clayton #include "lldb/Core/Module.h"
27e996fd30SGreg Clayton #include "lldb/Core/ModuleList.h"
281f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h"
29ecc11474SStephen Wilson #include "lldb/Core/PluginManager.h"
30348fb385STodd Fiala #include "lldb/Core/State.h"
31e996fd30SGreg Clayton #include "lldb/Core/StreamString.h"
32e996fd30SGreg Clayton #include "lldb/Host/FileSpec.h"
3313b18261SZachary Turner #include "lldb/Host/HostInfo.h"
34348fb385STodd Fiala #include "lldb/Interpreter/OptionValueProperties.h"
35348fb385STodd Fiala #include "lldb/Interpreter/Property.h"
36ecc11474SStephen Wilson #include "lldb/Target/Target.h"
37e996fd30SGreg Clayton #include "lldb/Target/Process.h"
38e996fd30SGreg Clayton 
39a868c13cSBruce Mitchener // Define these constants from Linux mman.h for use when targeting
4096ad3de5SRobert Flack // remote linux systems even when host has different values.
4196ad3de5SRobert Flack #define MAP_PRIVATE 2
4296ad3de5SRobert Flack #define MAP_ANON 0x20
4396ad3de5SRobert Flack 
44e996fd30SGreg Clayton using namespace lldb;
45e996fd30SGreg Clayton using namespace lldb_private;
46db264a6dSTamas Berghammer using namespace lldb_private::platform_linux;
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 {
56db264a6dSTamas Berghammer     class PlatformLinuxProperties : public Properties
57db264a6dSTamas Berghammer     {
58db264a6dSTamas Berghammer     public:
59db264a6dSTamas Berghammer         static ConstString&
60db264a6dSTamas Berghammer         GetSettingName ();
61db264a6dSTamas Berghammer 
62db264a6dSTamas Berghammer         PlatformLinuxProperties();
63db264a6dSTamas Berghammer 
64db264a6dSTamas Berghammer         virtual
65db264a6dSTamas Berghammer         ~PlatformLinuxProperties() = default;
66db264a6dSTamas Berghammer 
67db264a6dSTamas Berghammer     private:
68db264a6dSTamas Berghammer         static const PropertyDefinition*
69db264a6dSTamas Berghammer         GetStaticPropertyDefinitions();
70db264a6dSTamas Berghammer     };
71db264a6dSTamas Berghammer 
72db264a6dSTamas Berghammer     typedef std::shared_ptr<PlatformLinuxProperties> PlatformLinuxPropertiesSP;
73db264a6dSTamas Berghammer 
74db264a6dSTamas Berghammer } // anonymous namespace
75db264a6dSTamas Berghammer 
76db264a6dSTamas Berghammer PlatformLinuxProperties::PlatformLinuxProperties() :
77db264a6dSTamas Berghammer     Properties ()
78db264a6dSTamas Berghammer {
79db264a6dSTamas Berghammer     m_collection_sp.reset (new OptionValueProperties(GetSettingName ()));
80db264a6dSTamas Berghammer     m_collection_sp->Initialize (GetStaticPropertyDefinitions ());
81db264a6dSTamas Berghammer }
82db264a6dSTamas Berghammer 
83db264a6dSTamas Berghammer ConstString&
84db264a6dSTamas Berghammer PlatformLinuxProperties::GetSettingName ()
85db264a6dSTamas Berghammer {
86db264a6dSTamas Berghammer     static ConstString g_setting_name("linux");
87db264a6dSTamas Berghammer     return g_setting_name;
88db264a6dSTamas Berghammer }
89db264a6dSTamas Berghammer 
90348fb385STodd Fiala const PropertyDefinition*
91db264a6dSTamas Berghammer PlatformLinuxProperties::GetStaticPropertyDefinitions()
92348fb385STodd Fiala {
93281961a8STodd Fiala     static PropertyDefinition
94281961a8STodd Fiala     g_properties[] =
95281961a8STodd Fiala     {
96281961a8STodd Fiala         {  NULL        , OptionValue::eTypeInvalid, false, 0  , NULL, NULL, NULL  }
97281961a8STodd Fiala     };
98281961a8STodd Fiala 
99348fb385STodd Fiala     return g_properties;
100348fb385STodd Fiala }
101281961a8STodd Fiala 
102281961a8STodd Fiala static const PlatformLinuxPropertiesSP &
103281961a8STodd Fiala GetGlobalProperties()
104281961a8STodd Fiala {
105281961a8STodd Fiala     static PlatformLinuxPropertiesSP g_settings_sp;
106281961a8STodd Fiala     if (!g_settings_sp)
107281961a8STodd Fiala         g_settings_sp.reset (new PlatformLinuxProperties ());
108281961a8STodd Fiala     return g_settings_sp;
109281961a8STodd Fiala }
110281961a8STodd Fiala 
111281961a8STodd Fiala void
112db264a6dSTamas Berghammer PlatformLinux::DebuggerInitialize (Debugger &debugger)
113281961a8STodd Fiala {
114281961a8STodd Fiala     if (!PluginManager::GetSettingForPlatformPlugin (debugger, PlatformLinuxProperties::GetSettingName()))
115281961a8STodd Fiala     {
116281961a8STodd Fiala         const bool is_global_setting = true;
117281961a8STodd Fiala         PluginManager::CreateSettingForPlatformPlugin (debugger,
118281961a8STodd Fiala                                                        GetGlobalProperties()->GetValueProperties(),
119281961a8STodd Fiala                                                        ConstString ("Properties for the PlatformLinux plug-in."),
120281961a8STodd Fiala                                                        is_global_setting);
121281961a8STodd Fiala     }
122281961a8STodd Fiala }
123281961a8STodd Fiala 
124281961a8STodd Fiala 
125281961a8STodd Fiala //------------------------------------------------------------------
126281961a8STodd Fiala 
127615eb7e6SGreg Clayton PlatformSP
128b3a40ba8SGreg Clayton PlatformLinux::CreateInstance (bool force, const ArchSpec *arch)
129ecc11474SStephen Wilson {
130db264a6dSTamas Berghammer     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
131015d818bSTodd Fiala     if (log)
132015d818bSTodd Fiala     {
133015d818bSTodd Fiala         const char *arch_name;
134015d818bSTodd Fiala         if (arch && arch->GetArchitectureName ())
135015d818bSTodd Fiala             arch_name = arch->GetArchitectureName ();
136015d818bSTodd Fiala         else
137015d818bSTodd Fiala             arch_name = "<null>";
138015d818bSTodd Fiala 
139015d818bSTodd Fiala         const char *triple_cstr = arch ? arch->GetTriple ().getTriple ().c_str() : "<null>";
140015d818bSTodd Fiala 
141015d818bSTodd Fiala         log->Printf ("PlatformLinux::%s(force=%s, arch={%s,%s})", __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
142015d818bSTodd Fiala     }
143015d818bSTodd Fiala 
144b3a40ba8SGreg Clayton     bool create = force;
145b3a40ba8SGreg Clayton     if (create == false && arch && arch->IsValid())
146b3a40ba8SGreg Clayton     {
147b3a40ba8SGreg Clayton         const llvm::Triple &triple = arch->GetTriple();
14870512317SGreg Clayton         switch (triple.getOS())
14970512317SGreg Clayton         {
15070512317SGreg Clayton             case llvm::Triple::Linux:
151b1da257aSTed Woodward                 create = true;
15270512317SGreg Clayton                 break;
15370512317SGreg Clayton 
154dbc6c0bbSGreg Clayton #if defined(__linux__)
155dbc6c0bbSGreg Clayton             // Only accept "unknown" for the OS if the host is linux and
1566a7f3338SBruce Mitchener             // it "unknown" wasn't specified (it was just returned because it
157dbc6c0bbSGreg Clayton             // was NOT specified)
158015d818bSTodd Fiala             case llvm::Triple::OSType::UnknownOS:
15970512317SGreg Clayton                 create = !arch->TripleOSWasSpecified();
16070512317SGreg Clayton                 break;
161dbc6c0bbSGreg Clayton #endif
16270512317SGreg Clayton             default:
16370512317SGreg Clayton                 break;
16470512317SGreg Clayton         }
16570512317SGreg Clayton     }
166015d818bSTodd Fiala 
167b3a40ba8SGreg Clayton     if (create)
168015d818bSTodd Fiala     {
169015d818bSTodd Fiala         if (log)
170015d818bSTodd Fiala             log->Printf ("PlatformLinux::%s() creating remote-linux platform", __FUNCTION__);
171615eb7e6SGreg Clayton         return PlatformSP(new PlatformLinux(false));
172015d818bSTodd Fiala     }
173015d818bSTodd Fiala 
174015d818bSTodd Fiala     if (log)
175015d818bSTodd Fiala         log->Printf ("PlatformLinux::%s() aborting creation of remote-linux platform", __FUNCTION__);
176015d818bSTodd Fiala 
177615eb7e6SGreg Clayton     return PlatformSP();
178ecc11474SStephen Wilson }
179ecc11474SStephen Wilson 
180ecc11474SStephen Wilson 
181db264a6dSTamas Berghammer ConstString
18257abc5d6SGreg Clayton PlatformLinux::GetPluginNameStatic (bool is_host)
183ecc11474SStephen Wilson {
18428041352SGreg Clayton     if (is_host)
18557abc5d6SGreg Clayton     {
18657abc5d6SGreg Clayton         static ConstString g_host_name(Platform::GetHostPlatformName ());
18757abc5d6SGreg Clayton         return g_host_name;
18857abc5d6SGreg Clayton     }
18928041352SGreg Clayton     else
19057abc5d6SGreg Clayton     {
19157abc5d6SGreg Clayton         static ConstString g_remote_name("remote-linux");
19257abc5d6SGreg Clayton         return g_remote_name;
19357abc5d6SGreg Clayton     }
19428041352SGreg Clayton }
19528041352SGreg Clayton 
19628041352SGreg Clayton const char *
19728041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic (bool is_host)
19828041352SGreg Clayton {
19928041352SGreg Clayton     if (is_host)
20028041352SGreg Clayton         return "Local Linux user platform plug-in.";
20128041352SGreg Clayton     else
20228041352SGreg Clayton         return "Remote Linux user platform plug-in.";
203ecc11474SStephen Wilson }
204ecc11474SStephen Wilson 
205db264a6dSTamas Berghammer ConstString
20657abc5d6SGreg Clayton PlatformLinux::GetPluginName()
20757abc5d6SGreg Clayton {
20857abc5d6SGreg Clayton     return GetPluginNameStatic(IsHost());
20957abc5d6SGreg Clayton }
21057abc5d6SGreg Clayton 
211e996fd30SGreg Clayton void
212e996fd30SGreg Clayton PlatformLinux::Initialize ()
213e996fd30SGreg Clayton {
2143c4f89d7STamas Berghammer     PlatformPOSIX::Initialize ();
2153c4f89d7STamas Berghammer 
21628041352SGreg Clayton     if (g_initialize_count++ == 0)
217ecc11474SStephen Wilson     {
2181c6a1ea9STamas Berghammer #if defined(__linux__) && !defined(__ANDROID__)
21928041352SGreg Clayton         PlatformSP default_platform_sp (new PlatformLinux(true));
22013b18261SZachary Turner         default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
221615eb7e6SGreg Clayton         Platform::SetHostPlatform (default_platform_sp);
22228041352SGreg Clayton #endif
22357abc5d6SGreg Clayton         PluginManager::RegisterPlugin(PlatformLinux::GetPluginNameStatic(false),
22428041352SGreg Clayton                                       PlatformLinux::GetPluginDescriptionStatic(false),
225281961a8STodd Fiala                                       PlatformLinux::CreateInstance,
226281961a8STodd Fiala                                       PlatformLinux::DebuggerInitialize);
227ecc11474SStephen Wilson     }
228e996fd30SGreg Clayton }
229e996fd30SGreg Clayton 
230e996fd30SGreg Clayton void
231e996fd30SGreg Clayton PlatformLinux::Terminate ()
232e996fd30SGreg Clayton {
23328041352SGreg Clayton     if (g_initialize_count > 0)
23428041352SGreg Clayton     {
23528041352SGreg Clayton         if (--g_initialize_count == 0)
23628041352SGreg Clayton         {
23728041352SGreg Clayton             PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance);
238e996fd30SGreg Clayton         }
23928041352SGreg Clayton     }
2403c4f89d7STamas Berghammer 
2413c4f89d7STamas Berghammer     PlatformPOSIX::Terminate ();
24228041352SGreg Clayton }
243e996fd30SGreg Clayton 
244e996fd30SGreg Clayton Error
2458012cadbSGreg Clayton PlatformLinux::ResolveExecutable (const ModuleSpec &ms,
246ea5e0cc3SGreg Clayton                                   lldb::ModuleSP &exe_module_sp,
247ea5e0cc3SGreg Clayton                                   const FileSpecList *module_search_paths_ptr)
248e996fd30SGreg Clayton {
249e996fd30SGreg Clayton     Error error;
250e996fd30SGreg Clayton     // Nothing special to do here, just use the actual file and architecture
251e996fd30SGreg Clayton 
25228041352SGreg Clayton     char exe_path[PATH_MAX];
2538012cadbSGreg Clayton     ModuleSpec resolved_module_spec (ms);
254e996fd30SGreg Clayton 
25528041352SGreg Clayton     if (IsHost())
25628041352SGreg Clayton     {
25728041352SGreg Clayton         // If we have "ls" as the exe_file, resolve the executable location based on
258e996fd30SGreg Clayton         // the current path variables
2598012cadbSGreg Clayton         if (!resolved_module_spec.GetFileSpec().Exists())
26028041352SGreg Clayton         {
2618012cadbSGreg Clayton             resolved_module_spec.GetFileSpec().GetPath(exe_path, sizeof(exe_path));
2628012cadbSGreg Clayton             resolved_module_spec.GetFileSpec().SetFile(exe_path, true);
26328041352SGreg Clayton         }
26428041352SGreg Clayton 
2658012cadbSGreg Clayton         if (!resolved_module_spec.GetFileSpec().Exists())
2668012cadbSGreg Clayton             resolved_module_spec.GetFileSpec().ResolveExecutableLocation ();
267e996fd30SGreg Clayton 
2688012cadbSGreg Clayton         if (resolved_module_spec.GetFileSpec().Exists())
26928041352SGreg Clayton             error.Clear();
27028041352SGreg Clayton         else
27128041352SGreg Clayton         {
2728012cadbSGreg Clayton             error.SetErrorStringWithFormat("unable to find executable for '%s'", resolved_module_spec.GetFileSpec().GetPath().c_str());
27328041352SGreg Clayton         }
27428041352SGreg Clayton     }
27528041352SGreg Clayton     else
27628041352SGreg Clayton     {
27728041352SGreg Clayton         if (m_remote_platform_sp)
27828041352SGreg Clayton         {
279*a3f245ffSOleksiy Vyalov             error = GetCachedExecutable (resolved_module_spec, exe_module_sp, module_search_paths_ptr, *m_remote_platform_sp);
28028041352SGreg Clayton         }
28128041352SGreg Clayton         else
28228041352SGreg Clayton         {
28328041352SGreg Clayton             // We may connect to a process and use the provided executable (Don't use local $PATH).
284e996fd30SGreg Clayton 
2858012cadbSGreg Clayton             if (resolved_module_spec.GetFileSpec().Exists())
28628041352SGreg Clayton                 error.Clear();
28728041352SGreg Clayton             else
28828041352SGreg Clayton                 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path);
28928041352SGreg Clayton         }
29028041352SGreg Clayton     }
29128041352SGreg Clayton 
29228041352SGreg Clayton     if (error.Success())
293e996fd30SGreg Clayton     {
2948012cadbSGreg Clayton         if (resolved_module_spec.GetArchitecture().IsValid())
295e996fd30SGreg Clayton         {
2968012cadbSGreg Clayton             error = ModuleList::GetSharedModule (resolved_module_spec,
297e996fd30SGreg Clayton                                                  exe_module_sp,
298e996fd30SGreg Clayton                                                  NULL,
2990c90ef47SGreg Clayton                                                  NULL,
300e996fd30SGreg Clayton                                                  NULL);
3019f0013d8SMichael Sartain             if (error.Fail())
3029f0013d8SMichael Sartain             {
3039f0013d8SMichael Sartain                 // If we failed, it may be because the vendor and os aren't known. If that is the
3049f0013d8SMichael Sartain                 // case, try setting them to the host architecture and give it another try.
3058012cadbSGreg Clayton                 llvm::Triple &module_triple = resolved_module_spec.GetArchitecture().GetTriple();
3069f0013d8SMichael Sartain                 bool is_vendor_specified = (module_triple.getVendor() != llvm::Triple::UnknownVendor);
3079f0013d8SMichael Sartain                 bool is_os_specified = (module_triple.getOS() != llvm::Triple::UnknownOS);
3089f0013d8SMichael Sartain                 if (!is_vendor_specified || !is_os_specified)
3099f0013d8SMichael Sartain                 {
31013b18261SZachary Turner                     const llvm::Triple &host_triple = HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple();
3119f0013d8SMichael Sartain 
3129f0013d8SMichael Sartain                     if (!is_vendor_specified)
3139f0013d8SMichael Sartain                         module_triple.setVendorName (host_triple.getVendorName());
3149f0013d8SMichael Sartain                     if (!is_os_specified)
3159f0013d8SMichael Sartain                         module_triple.setOSName (host_triple.getOSName());
3169f0013d8SMichael Sartain 
3178012cadbSGreg Clayton                     error = ModuleList::GetSharedModule (resolved_module_spec,
3189f0013d8SMichael Sartain                                                          exe_module_sp,
3199f0013d8SMichael Sartain                                                          NULL,
3209f0013d8SMichael Sartain                                                          NULL,
3219f0013d8SMichael Sartain                                                          NULL);
3229f0013d8SMichael Sartain                 }
3239f0013d8SMichael Sartain             }
324e996fd30SGreg Clayton 
325e635db49SSean Callanan             // TODO find out why exe_module_sp might be NULL
326e635db49SSean Callanan             if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL)
327e996fd30SGreg Clayton             {
328e996fd30SGreg Clayton                 exe_module_sp.reset();
329b5ad4ec7SGreg Clayton                 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s",
3308012cadbSGreg Clayton                                                 resolved_module_spec.GetFileSpec().GetPath().c_str(),
3318012cadbSGreg Clayton                                                 resolved_module_spec.GetArchitecture().GetArchitectureName());
332e996fd30SGreg Clayton             }
333e996fd30SGreg Clayton         }
334e996fd30SGreg Clayton         else
335e996fd30SGreg Clayton         {
336e996fd30SGreg Clayton             // No valid architecture was specified, ask the platform for
337e996fd30SGreg Clayton             // the architectures that we should be using (in the correct order)
338e996fd30SGreg Clayton             // and see if we can find a match that way
339e996fd30SGreg Clayton             StreamString arch_names;
3408012cadbSGreg Clayton             for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, resolved_module_spec.GetArchitecture()); ++idx)
341e996fd30SGreg Clayton             {
3428012cadbSGreg Clayton                 error = ModuleList::GetSharedModule (resolved_module_spec,
343e996fd30SGreg Clayton                                                      exe_module_sp,
344e996fd30SGreg Clayton                                                      NULL,
3450c90ef47SGreg Clayton                                                      NULL,
346e996fd30SGreg Clayton                                                      NULL);
347e996fd30SGreg Clayton                 // Did we find an executable using one of the
348e996fd30SGreg Clayton                 if (error.Success())
349e996fd30SGreg Clayton                 {
350e996fd30SGreg Clayton                     if (exe_module_sp && exe_module_sp->GetObjectFile())
351e996fd30SGreg Clayton                         break;
352e996fd30SGreg Clayton                     else
353e996fd30SGreg Clayton                         error.SetErrorToGenericError();
354e996fd30SGreg Clayton                 }
355e996fd30SGreg Clayton 
356e996fd30SGreg Clayton                 if (idx > 0)
357e996fd30SGreg Clayton                     arch_names.PutCString (", ");
3588012cadbSGreg Clayton                 arch_names.PutCString (resolved_module_spec.GetArchitecture().GetArchitectureName());
359e996fd30SGreg Clayton             }
360e996fd30SGreg Clayton 
361e996fd30SGreg Clayton             if (error.Fail() || !exe_module_sp)
362e996fd30SGreg Clayton             {
3638012cadbSGreg Clayton                 if (resolved_module_spec.GetFileSpec().Readable())
36439945dccSGreg Clayton                 {
365b5ad4ec7SGreg Clayton                     error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
3668012cadbSGreg Clayton                                                     resolved_module_spec.GetFileSpec().GetPath().c_str(),
36757abc5d6SGreg Clayton                                                     GetPluginName().GetCString(),
368e996fd30SGreg Clayton                                                     arch_names.GetString().c_str());
369e996fd30SGreg Clayton                 }
37039945dccSGreg Clayton                 else
37139945dccSGreg Clayton                 {
3728012cadbSGreg Clayton                     error.SetErrorStringWithFormat("'%s' is not readable", resolved_module_spec.GetFileSpec().GetPath().c_str());
37339945dccSGreg Clayton                 }
37439945dccSGreg Clayton             }
375e996fd30SGreg Clayton         }
376e996fd30SGreg Clayton     }
377e996fd30SGreg Clayton 
378e996fd30SGreg Clayton     return error;
379e996fd30SGreg Clayton }
380e996fd30SGreg Clayton 
381e996fd30SGreg Clayton Error
382fc995725SSteve Pucci PlatformLinux::GetFileWithUUID (const FileSpec &platform_file,
38328041352SGreg Clayton                                 const UUID *uuid_ptr, FileSpec &local_file)
384e996fd30SGreg Clayton {
38528041352SGreg Clayton     if (IsRemote())
38628041352SGreg Clayton     {
38728041352SGreg Clayton         if (m_remote_platform_sp)
388fc995725SSteve Pucci             return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file);
38928041352SGreg Clayton     }
39028041352SGreg Clayton 
391e996fd30SGreg Clayton     // Default to the local case
392e996fd30SGreg Clayton     local_file = platform_file;
393e996fd30SGreg Clayton     return Error();
394e996fd30SGreg Clayton }
395e996fd30SGreg Clayton 
396e996fd30SGreg Clayton 
397e996fd30SGreg Clayton //------------------------------------------------------------------
398e996fd30SGreg Clayton /// Default Constructor
399e996fd30SGreg Clayton //------------------------------------------------------------------
40028041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) :
401015d818bSTodd Fiala     PlatformPOSIX(is_host)  // This is the local host platform
402e996fd30SGreg Clayton {
403e996fd30SGreg Clayton }
404e996fd30SGreg Clayton 
405e996fd30SGreg Clayton //------------------------------------------------------------------
406e996fd30SGreg Clayton /// Destructor.
407e996fd30SGreg Clayton ///
408e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be
409e996fd30SGreg Clayton /// inherited from by the plug-in instance.
410e996fd30SGreg Clayton //------------------------------------------------------------------
411e996fd30SGreg Clayton PlatformLinux::~PlatformLinux()
412e996fd30SGreg Clayton {
413e996fd30SGreg Clayton }
414e996fd30SGreg Clayton 
415e996fd30SGreg Clayton bool
41613e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
417e996fd30SGreg Clayton {
41828041352SGreg Clayton     bool success = false;
41928041352SGreg Clayton     if (IsHost())
42028041352SGreg Clayton     {
42128041352SGreg Clayton         success = Platform::GetProcessInfo (pid, process_info);
42228041352SGreg Clayton     }
42328041352SGreg Clayton     else
42428041352SGreg Clayton     {
42528041352SGreg Clayton         if (m_remote_platform_sp)
42628041352SGreg Clayton             success = m_remote_platform_sp->GetProcessInfo (pid, process_info);
42728041352SGreg Clayton     }
42828041352SGreg Clayton     return success;
429e996fd30SGreg Clayton }
430e996fd30SGreg Clayton 
4318e6ec453SStephane Sezer uint32_t
4328e6ec453SStephane Sezer PlatformLinux::FindProcesses (const ProcessInstanceInfoMatch &match_info,
4338e6ec453SStephane Sezer                               ProcessInstanceInfoList &process_infos)
4348e6ec453SStephane Sezer {
4358e6ec453SStephane Sezer     uint32_t match_count = 0;
4368e6ec453SStephane Sezer     if (IsHost())
4378e6ec453SStephane Sezer     {
4388e6ec453SStephane Sezer         // Let the base class figure out the host details
4398e6ec453SStephane Sezer         match_count = Platform::FindProcesses (match_info, process_infos);
4408e6ec453SStephane Sezer     }
4418e6ec453SStephane Sezer     else
4428e6ec453SStephane Sezer     {
4438e6ec453SStephane Sezer         // If we are remote, we can only return results if we are connected
4448e6ec453SStephane Sezer         if (m_remote_platform_sp)
4458e6ec453SStephane Sezer             match_count = m_remote_platform_sp->FindProcesses (match_info, process_infos);
4468e6ec453SStephane Sezer     }
4478e6ec453SStephane Sezer     return match_count;
4488e6ec453SStephane Sezer }
4498e6ec453SStephane Sezer 
450e996fd30SGreg Clayton bool
451e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
452e996fd30SGreg Clayton {
453e49b8e06SRobert Flack     if (IsHost())
454e49b8e06SRobert Flack     {
455e49b8e06SRobert Flack         ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
456e49b8e06SRobert Flack         if (hostArch.GetTriple().isOSLinux())
457e49b8e06SRobert Flack         {
458e49b8e06SRobert Flack             if (idx == 0)
459e49b8e06SRobert Flack             {
460e49b8e06SRobert Flack                 arch = hostArch;
461e49b8e06SRobert Flack                 return arch.IsValid();
462e49b8e06SRobert Flack             }
463e49b8e06SRobert Flack             else if (idx == 1)
464e49b8e06SRobert Flack             {
465e49b8e06SRobert Flack                 // If the default host architecture is 64-bit, look for a 32-bit variant
466e49b8e06SRobert Flack                 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit())
467e49b8e06SRobert Flack                 {
468e49b8e06SRobert Flack                     arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
469e49b8e06SRobert Flack                     return arch.IsValid();
470e49b8e06SRobert Flack                 }
471e49b8e06SRobert Flack             }
472e49b8e06SRobert Flack         }
473e49b8e06SRobert Flack     }
474e49b8e06SRobert Flack     else
475e49b8e06SRobert Flack     {
476e49b8e06SRobert Flack         if (m_remote_platform_sp)
477e49b8e06SRobert Flack             return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
478bb1e283cSTed Woodward 
479bb1e283cSTed Woodward         llvm::Triple triple;
480bb1e283cSTed Woodward         // Set the OS to linux
481bb1e283cSTed Woodward         triple.setOS(llvm::Triple::Linux);
482bb1e283cSTed Woodward         // Set the architecture
483bb1e283cSTed Woodward         switch (idx)
484bb1e283cSTed Woodward         {
485bb1e283cSTed Woodward             case 0: triple.setArchName("x86_64"); break;
486bb1e283cSTed Woodward             case 1: triple.setArchName("i386"); break;
487bb1e283cSTed Woodward             case 2: triple.setArchName("arm"); break;
488bb1e283cSTed Woodward             case 3: triple.setArchName("aarch64"); break;
489bb1e283cSTed Woodward             case 4: triple.setArchName("mips64"); break;
490bb1e283cSTed Woodward             case 5: triple.setArchName("hexagon"); break;
491bb1e283cSTed Woodward             case 6: triple.setArchName("mips"); break;
492ce815e45SSagar Thakur             case 7: triple.setArchName("mips64el"); break;
493ce815e45SSagar Thakur             case 8: triple.setArchName("mipsel"); break;
494bb1e283cSTed Woodward             default: return false;
495bb1e283cSTed Woodward         }
496bb1e283cSTed Woodward         // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the vendor by
497bb1e283cSTed Woodward         // calling triple.SetVendorName("unknown") so that it is a "unspecified unknown".
498bb1e283cSTed Woodward         // This means when someone calls triple.GetVendorName() it will return an empty string
499bb1e283cSTed Woodward         // which indicates that the vendor can be set when two architectures are merged
500bb1e283cSTed Woodward 
501bb1e283cSTed Woodward         // Now set the triple into "arch" and return true
502bb1e283cSTed Woodward         arch.SetTriple(triple);
503bb1e283cSTed Woodward         return true;
504e49b8e06SRobert Flack     }
505e996fd30SGreg Clayton     return false;
506e996fd30SGreg Clayton }
507ecc11474SStephen Wilson 
508ecc11474SStephen Wilson void
509ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm)
510ecc11474SStephen Wilson {
5113be69dacSDaniel Malea     Platform::GetStatus(strm);
512ecc11474SStephen Wilson 
513b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX
514b743a803SStephane Sezer     // Display local kernel information only when we are running in host mode.
515b743a803SStephane Sezer     // Otherwise, we would end up printing non-Linux information (when running
516b743a803SStephane Sezer     // on Mac OS for example).
517b743a803SStephane Sezer     if (IsHost())
518b743a803SStephane Sezer     {
519b2f1fb29SVirgile Bello         struct utsname un;
520b2f1fb29SVirgile Bello 
5213be69dacSDaniel Malea         if (uname(&un))
5223be69dacSDaniel Malea             return;
5233be69dacSDaniel Malea 
5243be69dacSDaniel Malea         strm.Printf ("    Kernel: %s\n", un.sysname);
5253be69dacSDaniel Malea         strm.Printf ("   Release: %s\n", un.release);
5263be69dacSDaniel Malea         strm.Printf ("   Version: %s\n", un.version);
527b743a803SStephane Sezer     }
528b2f1fb29SVirgile Bello #endif
529ecc11474SStephen Wilson }
530ecc11474SStephen Wilson 
531ecc11474SStephen Wilson size_t
532ecc11474SStephen Wilson PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target,
533ecc11474SStephen Wilson                                                 BreakpointSite *bp_site)
534ecc11474SStephen Wilson {
535ecc11474SStephen Wilson     ArchSpec arch = target.GetArchitecture();
53628041352SGreg Clayton     const uint8_t *trap_opcode = NULL;
53728041352SGreg Clayton     size_t trap_opcode_size = 0;
538ecc11474SStephen Wilson 
539906e9acfSGreg Clayton     switch (arch.GetMachine())
540ecc11474SStephen Wilson     {
541ecc11474SStephen Wilson     default:
542ecc11474SStephen Wilson         assert(false && "CPU type not supported!");
543ecc11474SStephen Wilson         break;
544ecc11474SStephen Wilson 
5452afc5966STodd Fiala     case llvm::Triple::aarch64:
5462afc5966STodd Fiala         {
5472afc5966STodd Fiala             static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 };
5482afc5966STodd Fiala             trap_opcode = g_aarch64_opcode;
5492afc5966STodd Fiala             trap_opcode_size = sizeof(g_aarch64_opcode);
5502afc5966STodd Fiala         }
5512afc5966STodd Fiala         break;
552906e9acfSGreg Clayton     case llvm::Triple::x86:
553906e9acfSGreg Clayton     case llvm::Triple::x86_64:
55428041352SGreg Clayton         {
55528041352SGreg Clayton             static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
55628041352SGreg Clayton             trap_opcode = g_i386_breakpoint_opcode;
55728041352SGreg Clayton             trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
55828041352SGreg Clayton         }
559ecc11474SStephen Wilson         break;
560906e9acfSGreg Clayton     case llvm::Triple::hexagon:
5618006d319SDeepak Panickal         {
5628006d319SDeepak Panickal             static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 };
5638006d319SDeepak Panickal             trap_opcode = g_hex_opcode;
5648006d319SDeepak Panickal             trap_opcode_size = sizeof(g_hex_opcode);
5658006d319SDeepak Panickal         }
5668006d319SDeepak Panickal         break;
5672586e94bSJason Molenda     case llvm::Triple::arm:
5682586e94bSJason Molenda         {
5692586e94bSJason Molenda             // The ARM reference recommends the use of 0xe7fddefe and 0xdefe
5702586e94bSJason Molenda             // but the linux kernel does otherwise.
5712586e94bSJason Molenda             static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 };
5722586e94bSJason Molenda             static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde };
5732586e94bSJason Molenda 
5742586e94bSJason Molenda             lldb::BreakpointLocationSP bp_loc_sp (bp_site->GetOwnerAtIndex (0));
5752586e94bSJason Molenda             AddressClass addr_class = eAddressClassUnknown;
5762586e94bSJason Molenda 
5772586e94bSJason Molenda             if (bp_loc_sp)
5782586e94bSJason Molenda                 addr_class = bp_loc_sp->GetAddress ().GetAddressClass ();
5792586e94bSJason Molenda 
5802586e94bSJason Molenda             if (addr_class == eAddressClassCodeAlternateISA
58163c8be95STamas Berghammer                 || (addr_class == eAddressClassUnknown && (bp_site->GetLoadAddress() & 1)))
5822586e94bSJason Molenda             {
5832586e94bSJason Molenda                 trap_opcode = g_thumb_breakpoint_opcode;
5842586e94bSJason Molenda                 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
5852586e94bSJason Molenda             }
5862586e94bSJason Molenda             else
5872586e94bSJason Molenda             {
5882586e94bSJason Molenda                 trap_opcode = g_arm_breakpoint_opcode;
5892586e94bSJason Molenda                 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
5902586e94bSJason Molenda             }
5912586e94bSJason Molenda         }
5922586e94bSJason Molenda         break;
593794a4d5aSBhushan D. Attarde     case llvm::Triple::mips:
594c9335a3fSMohit K. Bhakkad     case llvm::Triple::mips64:
595c9335a3fSMohit K. Bhakkad         {
596c9335a3fSMohit K. Bhakkad             static const uint8_t g_hex_opcode[] = { 0x00, 0x00, 0x00, 0x0d };
597c9335a3fSMohit K. Bhakkad             trap_opcode = g_hex_opcode;
598c9335a3fSMohit K. Bhakkad             trap_opcode_size = sizeof(g_hex_opcode);
599c9335a3fSMohit K. Bhakkad         }
600c9335a3fSMohit K. Bhakkad         break;
601794a4d5aSBhushan D. Attarde     case llvm::Triple::mipsel:
6022c2acf96SMohit K. Bhakkad     case llvm::Triple::mips64el:
6032c2acf96SMohit K. Bhakkad         {
6042c2acf96SMohit K. Bhakkad             static const uint8_t g_hex_opcode[] = { 0x0d, 0x00, 0x00, 0x00 };
6052c2acf96SMohit K. Bhakkad             trap_opcode = g_hex_opcode;
6062c2acf96SMohit K. Bhakkad             trap_opcode_size = sizeof(g_hex_opcode);
6072c2acf96SMohit K. Bhakkad         }
6082c2acf96SMohit K. Bhakkad         break;
609ecc11474SStephen Wilson     }
610ecc11474SStephen Wilson 
61128041352SGreg Clayton     if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
61228041352SGreg Clayton         return trap_opcode_size;
61328041352SGreg Clayton     return 0;
61428041352SGreg Clayton }
61528041352SGreg Clayton 
616348fb385STodd Fiala int32_t
617348fb385STodd Fiala PlatformLinux::GetResumeCountForLaunchInfo (ProcessLaunchInfo &launch_info)
61828041352SGreg Clayton {
619348fb385STodd Fiala     int32_t resume_count = 0;
62028041352SGreg Clayton 
621348fb385STodd Fiala     // Always resume past the initial stop when we use eLaunchFlagDebug
622348fb385STodd Fiala     if (launch_info.GetFlags ().Test (eLaunchFlagDebug))
62328041352SGreg Clayton     {
624348fb385STodd Fiala         // Resume past the stop for the final exec into the true inferior.
625348fb385STodd Fiala         ++resume_count;
626348fb385STodd Fiala     }
627015d818bSTodd Fiala 
628348fb385STodd Fiala     // If we're not launching a shell, we're done.
62910687b0eSZachary Turner     const FileSpec &shell = launch_info.GetShell();
63010687b0eSZachary Turner     if (!shell)
631348fb385STodd Fiala         return resume_count;
632348fb385STodd Fiala 
63310687b0eSZachary Turner     std::string shell_string = shell.GetPath();
634348fb385STodd Fiala     // We're in a shell, so for sure we have to resume past the shell exec.
635348fb385STodd Fiala     ++resume_count;
636348fb385STodd Fiala 
637348fb385STodd Fiala     // Figure out what shell we're planning on using.
63810687b0eSZachary Turner     const char *shell_name = strrchr (shell_string.c_str(), '/');
639348fb385STodd Fiala     if (shell_name == NULL)
64010687b0eSZachary Turner         shell_name = shell_string.c_str();
64128041352SGreg Clayton     else
642348fb385STodd Fiala         shell_name++;
643348fb385STodd Fiala 
644348fb385STodd Fiala     if (strcmp (shell_name, "csh") == 0
645348fb385STodd Fiala              || strcmp (shell_name, "tcsh") == 0
646348fb385STodd Fiala              || strcmp (shell_name, "zsh") == 0
647348fb385STodd Fiala              || strcmp (shell_name, "sh") == 0)
64828041352SGreg Clayton     {
649348fb385STodd Fiala         // These shells seem to re-exec themselves.  Add another resume.
650348fb385STodd Fiala         ++resume_count;
651ecc11474SStephen Wilson     }
65213e8e1c3SJohnny Chen 
653348fb385STodd Fiala     return resume_count;
654348fb385STodd Fiala }
655348fb385STodd Fiala 
656348fb385STodd Fiala bool
657015d818bSTodd Fiala PlatformLinux::CanDebugProcess ()
658015d818bSTodd Fiala {
659015d818bSTodd Fiala     if (IsHost ())
660348fb385STodd Fiala     {
661b36f9178SPavel Labath         return true;
662348fb385STodd Fiala     }
663348fb385STodd Fiala     else
664348fb385STodd Fiala     {
665015d818bSTodd Fiala         // If we're connected, we can debug.
666015d818bSTodd Fiala         return IsConnected ();
667015d818bSTodd Fiala     }
668348fb385STodd Fiala }
669015d818bSTodd Fiala 
670348fb385STodd Fiala // For local debugging, Linux will override the debug logic to use llgs-launch rather than
671348fb385STodd Fiala // lldb-launch, llgs-attach.  This differs from current lldb-launch, debugserver-attach
672348fb385STodd Fiala // approach on MacOSX.
67313e8e1c3SJohnny Chen lldb::ProcessSP
674348fb385STodd Fiala PlatformLinux::DebugProcess (ProcessLaunchInfo &launch_info,
67513e8e1c3SJohnny Chen                              Debugger &debugger,
676348fb385STodd Fiala                              Target *target,       // Can be NULL, if NULL create a new target, else use existing one
67713e8e1c3SJohnny Chen                              Error &error)
67813e8e1c3SJohnny Chen {
679db264a6dSTamas Berghammer     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
680348fb385STodd Fiala     if (log)
681348fb385STodd Fiala         log->Printf ("PlatformLinux::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target));
68228041352SGreg Clayton 
683348fb385STodd Fiala     // If we're a remote host, use standard behavior from parent class.
684348fb385STodd Fiala     if (!IsHost ())
6858012cadbSGreg Clayton         return PlatformPOSIX::DebugProcess (launch_info, debugger, target, error);
686348fb385STodd Fiala 
687348fb385STodd Fiala     //
688348fb385STodd Fiala     // For local debugging, we'll insist on having ProcessGDBRemote create the process.
689348fb385STodd Fiala     //
690348fb385STodd Fiala 
691348fb385STodd Fiala     ProcessSP process_sp;
692348fb385STodd Fiala 
693348fb385STodd Fiala     // Make sure we stop at the entry point
694348fb385STodd Fiala     launch_info.GetFlags ().Set (eLaunchFlagDebug);
695348fb385STodd Fiala 
696348fb385STodd Fiala     // We always launch the process we are going to debug in a separate process
697348fb385STodd Fiala     // group, since then we can handle ^C interrupts ourselves w/o having to worry
698348fb385STodd Fiala     // about the target getting them as well.
699348fb385STodd Fiala     launch_info.SetLaunchInSeparateProcessGroup(true);
700348fb385STodd Fiala 
701348fb385STodd Fiala     // Ensure we have a target.
702348fb385STodd Fiala     if (target == nullptr)
703348fb385STodd Fiala     {
704348fb385STodd Fiala         if (log)
705348fb385STodd Fiala             log->Printf ("PlatformLinux::%s creating new target", __FUNCTION__);
706348fb385STodd Fiala 
707348fb385STodd Fiala         TargetSP new_target_sp;
70828041352SGreg Clayton         error = debugger.GetTargetList().CreateTarget (debugger,
709348fb385STodd Fiala                                                        nullptr,
710348fb385STodd Fiala                                                        nullptr,
71128041352SGreg Clayton                                                        false,
712348fb385STodd Fiala                                                        nullptr,
71328041352SGreg Clayton                                                        new_target_sp);
714348fb385STodd Fiala         if (error.Fail ())
715348fb385STodd Fiala         {
716348fb385STodd Fiala             if (log)
717348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s failed to create new target: %s", __FUNCTION__, error.AsCString ());
718348fb385STodd Fiala             return process_sp;
719348fb385STodd Fiala         }
720348fb385STodd Fiala 
72128041352SGreg Clayton         target = new_target_sp.get();
722348fb385STodd Fiala         if (!target)
723348fb385STodd Fiala         {
724348fb385STodd Fiala             error.SetErrorString ("CreateTarget() returned nullptr");
725348fb385STodd Fiala             if (log)
726348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ());
727348fb385STodd Fiala             return process_sp;
728348fb385STodd Fiala         }
72928041352SGreg Clayton     }
73028041352SGreg Clayton     else
73128041352SGreg Clayton     {
732348fb385STodd Fiala         if (log)
733348fb385STodd Fiala             log->Printf ("PlatformLinux::%s using provided target", __FUNCTION__);
734348fb385STodd Fiala     }
735348fb385STodd Fiala 
736348fb385STodd Fiala     // Mark target as currently selected target.
73728041352SGreg Clayton     debugger.GetTargetList().SetSelectedTarget(target);
73828041352SGreg Clayton 
739348fb385STodd Fiala     // Now create the gdb-remote process.
740348fb385STodd Fiala     if (log)
741348fb385STodd Fiala         log->Printf ("PlatformLinux::%s having target create process with gdb-remote plugin", __FUNCTION__);
7428012cadbSGreg Clayton     process_sp = target->CreateProcess (launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr);
74328041352SGreg Clayton 
744348fb385STodd Fiala     if (!process_sp)
745348fb385STodd Fiala     {
746348fb385STodd Fiala         error.SetErrorString ("CreateProcess() failed for gdb-remote process");
747348fb385STodd Fiala         if (log)
748348fb385STodd Fiala             log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ());
749348fb385STodd Fiala         return process_sp;
750348fb385STodd Fiala     }
751348fb385STodd Fiala     else
752348fb385STodd Fiala     {
753348fb385STodd Fiala         if (log)
754348fb385STodd Fiala             log->Printf ("PlatformLinux::%s successfully created process", __FUNCTION__);
755348fb385STodd Fiala     }
756348fb385STodd Fiala 
757348fb385STodd Fiala     // Adjust launch for a hijacker.
758348fb385STodd Fiala     ListenerSP listener_sp;
759348fb385STodd Fiala     if (!launch_info.GetHijackListener ())
760348fb385STodd Fiala     {
761348fb385STodd Fiala         if (log)
762348fb385STodd Fiala             log->Printf ("PlatformLinux::%s setting up hijacker", __FUNCTION__);
763348fb385STodd Fiala 
764348fb385STodd Fiala         listener_sp.reset (new Listener("lldb.PlatformLinux.DebugProcess.hijack"));
765348fb385STodd Fiala         launch_info.SetHijackListener (listener_sp);
766348fb385STodd Fiala         process_sp->HijackProcessEvents (listener_sp.get ());
767348fb385STodd Fiala     }
768348fb385STodd Fiala 
769348fb385STodd Fiala     // Log file actions.
770348fb385STodd Fiala     if (log)
771348fb385STodd Fiala     {
772348fb385STodd Fiala         log->Printf ("PlatformLinux::%s launching process with the following file actions:", __FUNCTION__);
773348fb385STodd Fiala 
774348fb385STodd Fiala         StreamString stream;
775348fb385STodd Fiala         size_t i = 0;
776348fb385STodd Fiala         const FileAction *file_action;
777348fb385STodd Fiala         while ((file_action = launch_info.GetFileActionAtIndex (i++)) != nullptr)
778348fb385STodd Fiala         {
779348fb385STodd Fiala             file_action->Dump (stream);
780348fb385STodd Fiala             log->PutCString (stream.GetString().c_str ());
781348fb385STodd Fiala             stream.Clear();
782348fb385STodd Fiala         }
783348fb385STodd Fiala     }
784348fb385STodd Fiala 
785348fb385STodd Fiala     // Do the launch.
786348fb385STodd Fiala     error = process_sp->Launch(launch_info);
787348fb385STodd Fiala     if (error.Success ())
788348fb385STodd Fiala     {
789348fb385STodd Fiala         // Handle the hijacking of process events.
790348fb385STodd Fiala         if (listener_sp)
791348fb385STodd Fiala         {
792348fb385STodd Fiala             const StateType state = process_sp->WaitForProcessToStop (NULL, NULL, false, listener_sp.get());
793348fb385STodd Fiala 
794348fb385STodd Fiala             if (state == eStateStopped)
795348fb385STodd Fiala             {
796348fb385STodd Fiala                 if (log)
797348fb385STodd Fiala                     log->Printf ("PlatformLinux::%s pid %" PRIu64 " state %s\n",
798348fb385STodd Fiala                                  __FUNCTION__, process_sp->GetID (), StateAsCString (state));
799348fb385STodd Fiala             }
800348fb385STodd Fiala             else
801348fb385STodd Fiala             {
802348fb385STodd Fiala                 if (log)
803348fb385STodd Fiala                     log->Printf ("PlatformLinux::%s pid %" PRIu64 " state is not stopped - %s\n",
804348fb385STodd Fiala                                  __FUNCTION__, process_sp->GetID (), StateAsCString (state));
805348fb385STodd Fiala             }
806348fb385STodd Fiala         }
807348fb385STodd Fiala 
808348fb385STodd Fiala         // Hook up process PTY if we have one (which we should for local debugging with llgs).
809348fb385STodd Fiala         int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
810348fb385STodd Fiala         if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
811348fb385STodd Fiala         {
812348fb385STodd Fiala             process_sp->SetSTDIOFileDescriptor(pty_fd);
813348fb385STodd Fiala             if (log)
814348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s pid %" PRIu64 " hooked up STDIO pty to process", __FUNCTION__, process_sp->GetID ());
815348fb385STodd Fiala         }
816348fb385STodd Fiala         else
817348fb385STodd Fiala         {
818348fb385STodd Fiala             if (log)
819348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s pid %" PRIu64 " not using process STDIO pty", __FUNCTION__, process_sp->GetID ());
82028041352SGreg Clayton         }
82128041352SGreg Clayton     }
82228041352SGreg Clayton     else
82328041352SGreg Clayton     {
824348fb385STodd Fiala         if (log)
825348fb385STodd Fiala             log->Printf ("PlatformLinux::%s process launch failed: %s", __FUNCTION__, error.AsCString ());
826348fb385STodd Fiala         // FIXME figure out appropriate cleanup here.  Do we delete the target? Do we delete the process?  Does our caller do that?
82728041352SGreg Clayton     }
828348fb385STodd Fiala 
82928041352SGreg Clayton     return process_sp;
83013e8e1c3SJohnny Chen }
8312094dbf4SJason Molenda 
8322094dbf4SJason Molenda void
8332094dbf4SJason Molenda PlatformLinux::CalculateTrapHandlerSymbolNames ()
8342094dbf4SJason Molenda {
8352094dbf4SJason Molenda     m_trap_handlers.push_back (ConstString ("_sigtramp"));
8362094dbf4SJason Molenda }
837af245d11STodd Fiala 
83896ad3de5SRobert Flack uint64_t
839e0d8c422SMohit K. Bhakkad PlatformLinux::ConvertMmapFlagsToPlatform(const ArchSpec &arch, unsigned flags)
84096ad3de5SRobert Flack {
84196ad3de5SRobert Flack     uint64_t flags_platform = 0;
842e0d8c422SMohit K. Bhakkad     uint64_t map_anon = MAP_ANON;
843e0d8c422SMohit K. Bhakkad 
844e0d8c422SMohit K. Bhakkad     // To get correct flags for MIPS Architecture
845e0d8c422SMohit K. Bhakkad     if (arch.GetTriple ().getArch () == llvm::Triple::mips64
846e0d8c422SMohit K. Bhakkad        || arch.GetTriple ().getArch () == llvm::Triple::mips64el
847e0d8c422SMohit K. Bhakkad        || arch.GetTriple ().getArch () == llvm::Triple::mips
848e0d8c422SMohit K. Bhakkad        || arch.GetTriple ().getArch () == llvm::Triple::mipsel)
849e0d8c422SMohit K. Bhakkad            map_anon = 0x800;
850e0d8c422SMohit K. Bhakkad 
85196ad3de5SRobert Flack     if (flags & eMmapFlagsPrivate)
85296ad3de5SRobert Flack         flags_platform |= MAP_PRIVATE;
85396ad3de5SRobert Flack     if (flags & eMmapFlagsAnon)
854e0d8c422SMohit K. Bhakkad         flags_platform |= map_anon;
85596ad3de5SRobert Flack     return flags_platform;
85696ad3de5SRobert Flack }
857