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 
39af245d11STodd Fiala #if defined(__linux__)
40af245d11STodd Fiala #include "../../Process/Linux/NativeProcessLinux.h"
41af245d11STodd Fiala #endif
42af245d11STodd Fiala 
4396ad3de5SRobert Flack // Define these constants from Linux mman.h for use when targetting
4496ad3de5SRobert Flack // remote linux systems even when host has different values.
4596ad3de5SRobert Flack #define MAP_PRIVATE 2
4696ad3de5SRobert Flack #define MAP_ANON 0x20
4796ad3de5SRobert Flack 
48e996fd30SGreg Clayton using namespace lldb;
49e996fd30SGreg Clayton using namespace lldb_private;
50db264a6dSTamas Berghammer using namespace lldb_private::platform_linux;
51e996fd30SGreg Clayton 
5228041352SGreg Clayton static uint32_t g_initialize_count = 0;
5328041352SGreg Clayton 
54281961a8STodd Fiala //------------------------------------------------------------------
55281961a8STodd Fiala /// Code to handle the PlatformLinux settings
56281961a8STodd Fiala //------------------------------------------------------------------
57281961a8STodd Fiala 
58348fb385STodd Fiala namespace
59348fb385STodd Fiala {
60db264a6dSTamas Berghammer     class PlatformLinuxProperties : public Properties
61db264a6dSTamas Berghammer     {
62db264a6dSTamas Berghammer     public:
63db264a6dSTamas Berghammer         static ConstString&
64db264a6dSTamas Berghammer         GetSettingName ();
65db264a6dSTamas Berghammer 
66db264a6dSTamas Berghammer         PlatformLinuxProperties();
67db264a6dSTamas Berghammer 
68db264a6dSTamas Berghammer         virtual
69db264a6dSTamas Berghammer         ~PlatformLinuxProperties() = default;
70db264a6dSTamas Berghammer 
71db264a6dSTamas Berghammer     private:
72db264a6dSTamas Berghammer         static const PropertyDefinition*
73db264a6dSTamas Berghammer         GetStaticPropertyDefinitions();
74db264a6dSTamas Berghammer     };
75db264a6dSTamas Berghammer 
76db264a6dSTamas Berghammer     typedef std::shared_ptr<PlatformLinuxProperties> PlatformLinuxPropertiesSP;
77db264a6dSTamas Berghammer 
78db264a6dSTamas Berghammer } // anonymous namespace
79db264a6dSTamas Berghammer 
80db264a6dSTamas Berghammer PlatformLinuxProperties::PlatformLinuxProperties() :
81db264a6dSTamas Berghammer     Properties ()
82db264a6dSTamas Berghammer {
83db264a6dSTamas Berghammer     m_collection_sp.reset (new OptionValueProperties(GetSettingName ()));
84db264a6dSTamas Berghammer     m_collection_sp->Initialize (GetStaticPropertyDefinitions ());
85db264a6dSTamas Berghammer }
86db264a6dSTamas Berghammer 
87db264a6dSTamas Berghammer ConstString&
88db264a6dSTamas Berghammer PlatformLinuxProperties::GetSettingName ()
89db264a6dSTamas Berghammer {
90db264a6dSTamas Berghammer     static ConstString g_setting_name("linux");
91db264a6dSTamas Berghammer     return g_setting_name;
92db264a6dSTamas Berghammer }
93db264a6dSTamas Berghammer 
94348fb385STodd Fiala const PropertyDefinition*
95db264a6dSTamas Berghammer PlatformLinuxProperties::GetStaticPropertyDefinitions()
96348fb385STodd Fiala {
97281961a8STodd Fiala     static PropertyDefinition
98281961a8STodd Fiala     g_properties[] =
99281961a8STodd Fiala     {
100281961a8STodd Fiala         {  NULL        , OptionValue::eTypeInvalid, false, 0  , NULL, NULL, NULL  }
101281961a8STodd Fiala     };
102281961a8STodd Fiala 
103348fb385STodd Fiala     return g_properties;
104348fb385STodd Fiala }
105281961a8STodd Fiala 
106281961a8STodd Fiala static const PlatformLinuxPropertiesSP &
107281961a8STodd Fiala GetGlobalProperties()
108281961a8STodd Fiala {
109281961a8STodd Fiala     static PlatformLinuxPropertiesSP g_settings_sp;
110281961a8STodd Fiala     if (!g_settings_sp)
111281961a8STodd Fiala         g_settings_sp.reset (new PlatformLinuxProperties ());
112281961a8STodd Fiala     return g_settings_sp;
113281961a8STodd Fiala }
114281961a8STodd Fiala 
115281961a8STodd Fiala void
116db264a6dSTamas Berghammer PlatformLinux::DebuggerInitialize (Debugger &debugger)
117281961a8STodd Fiala {
118281961a8STodd Fiala     if (!PluginManager::GetSettingForPlatformPlugin (debugger, PlatformLinuxProperties::GetSettingName()))
119281961a8STodd Fiala     {
120281961a8STodd Fiala         const bool is_global_setting = true;
121281961a8STodd Fiala         PluginManager::CreateSettingForPlatformPlugin (debugger,
122281961a8STodd Fiala                                                        GetGlobalProperties()->GetValueProperties(),
123281961a8STodd Fiala                                                        ConstString ("Properties for the PlatformLinux plug-in."),
124281961a8STodd Fiala                                                        is_global_setting);
125281961a8STodd Fiala     }
126281961a8STodd Fiala }
127281961a8STodd Fiala 
128281961a8STodd Fiala 
129281961a8STodd Fiala //------------------------------------------------------------------
130281961a8STodd Fiala 
131615eb7e6SGreg Clayton PlatformSP
132b3a40ba8SGreg Clayton PlatformLinux::CreateInstance (bool force, const ArchSpec *arch)
133ecc11474SStephen Wilson {
134db264a6dSTamas Berghammer     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
135015d818bSTodd Fiala     if (log)
136015d818bSTodd Fiala     {
137015d818bSTodd Fiala         const char *arch_name;
138015d818bSTodd Fiala         if (arch && arch->GetArchitectureName ())
139015d818bSTodd Fiala             arch_name = arch->GetArchitectureName ();
140015d818bSTodd Fiala         else
141015d818bSTodd Fiala             arch_name = "<null>";
142015d818bSTodd Fiala 
143015d818bSTodd Fiala         const char *triple_cstr = arch ? arch->GetTriple ().getTriple ().c_str() : "<null>";
144015d818bSTodd Fiala 
145015d818bSTodd Fiala         log->Printf ("PlatformLinux::%s(force=%s, arch={%s,%s})", __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
146015d818bSTodd Fiala     }
147015d818bSTodd Fiala 
148b3a40ba8SGreg Clayton     bool create = force;
149b3a40ba8SGreg Clayton     if (create == false && arch && arch->IsValid())
150b3a40ba8SGreg Clayton     {
151b3a40ba8SGreg Clayton         const llvm::Triple &triple = arch->GetTriple();
15270512317SGreg Clayton         switch (triple.getOS())
15370512317SGreg Clayton         {
15470512317SGreg Clayton             case llvm::Triple::Linux:
155b1da257aSTed Woodward                 create = true;
15670512317SGreg Clayton                 break;
15770512317SGreg Clayton 
158dbc6c0bbSGreg Clayton #if defined(__linux__)
159dbc6c0bbSGreg Clayton             // Only accept "unknown" for the OS if the host is linux and
1606a7f3338SBruce Mitchener             // it "unknown" wasn't specified (it was just returned because it
161dbc6c0bbSGreg Clayton             // was NOT specified)
162015d818bSTodd Fiala             case llvm::Triple::OSType::UnknownOS:
16370512317SGreg Clayton                 create = !arch->TripleOSWasSpecified();
16470512317SGreg Clayton                 break;
165dbc6c0bbSGreg Clayton #endif
16670512317SGreg Clayton             default:
16770512317SGreg Clayton                 break;
16870512317SGreg Clayton         }
16970512317SGreg Clayton     }
170015d818bSTodd Fiala 
171b3a40ba8SGreg Clayton     if (create)
172015d818bSTodd Fiala     {
173015d818bSTodd Fiala         if (log)
174015d818bSTodd Fiala             log->Printf ("PlatformLinux::%s() creating remote-linux platform", __FUNCTION__);
175615eb7e6SGreg Clayton         return PlatformSP(new PlatformLinux(false));
176015d818bSTodd Fiala     }
177015d818bSTodd Fiala 
178015d818bSTodd Fiala     if (log)
179015d818bSTodd Fiala         log->Printf ("PlatformLinux::%s() aborting creation of remote-linux platform", __FUNCTION__);
180015d818bSTodd Fiala 
181615eb7e6SGreg Clayton     return PlatformSP();
182ecc11474SStephen Wilson }
183ecc11474SStephen Wilson 
184ecc11474SStephen Wilson 
185db264a6dSTamas Berghammer ConstString
18657abc5d6SGreg Clayton PlatformLinux::GetPluginNameStatic (bool is_host)
187ecc11474SStephen Wilson {
18828041352SGreg Clayton     if (is_host)
18957abc5d6SGreg Clayton     {
19057abc5d6SGreg Clayton         static ConstString g_host_name(Platform::GetHostPlatformName ());
19157abc5d6SGreg Clayton         return g_host_name;
19257abc5d6SGreg Clayton     }
19328041352SGreg Clayton     else
19457abc5d6SGreg Clayton     {
19557abc5d6SGreg Clayton         static ConstString g_remote_name("remote-linux");
19657abc5d6SGreg Clayton         return g_remote_name;
19757abc5d6SGreg Clayton     }
19828041352SGreg Clayton }
19928041352SGreg Clayton 
20028041352SGreg Clayton const char *
20128041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic (bool is_host)
20228041352SGreg Clayton {
20328041352SGreg Clayton     if (is_host)
20428041352SGreg Clayton         return "Local Linux user platform plug-in.";
20528041352SGreg Clayton     else
20628041352SGreg Clayton         return "Remote Linux user platform plug-in.";
207ecc11474SStephen Wilson }
208ecc11474SStephen Wilson 
209db264a6dSTamas Berghammer ConstString
21057abc5d6SGreg Clayton PlatformLinux::GetPluginName()
21157abc5d6SGreg Clayton {
21257abc5d6SGreg Clayton     return GetPluginNameStatic(IsHost());
21357abc5d6SGreg Clayton }
21457abc5d6SGreg Clayton 
215e996fd30SGreg Clayton void
216e996fd30SGreg Clayton PlatformLinux::Initialize ()
217e996fd30SGreg Clayton {
2183c4f89d7STamas Berghammer     PlatformPOSIX::Initialize ();
2193c4f89d7STamas Berghammer 
22028041352SGreg Clayton     if (g_initialize_count++ == 0)
221ecc11474SStephen Wilson     {
2221c6a1ea9STamas Berghammer #if defined(__linux__) && !defined(__ANDROID__)
22328041352SGreg Clayton         PlatformSP default_platform_sp (new PlatformLinux(true));
22413b18261SZachary Turner         default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
225615eb7e6SGreg Clayton         Platform::SetHostPlatform (default_platform_sp);
22628041352SGreg Clayton #endif
22757abc5d6SGreg Clayton         PluginManager::RegisterPlugin(PlatformLinux::GetPluginNameStatic(false),
22828041352SGreg Clayton                                       PlatformLinux::GetPluginDescriptionStatic(false),
229281961a8STodd Fiala                                       PlatformLinux::CreateInstance,
230281961a8STodd Fiala                                       PlatformLinux::DebuggerInitialize);
231ecc11474SStephen Wilson     }
232e996fd30SGreg Clayton }
233e996fd30SGreg Clayton 
234e996fd30SGreg Clayton void
235e996fd30SGreg Clayton PlatformLinux::Terminate ()
236e996fd30SGreg Clayton {
23728041352SGreg Clayton     if (g_initialize_count > 0)
23828041352SGreg Clayton     {
23928041352SGreg Clayton         if (--g_initialize_count == 0)
24028041352SGreg Clayton         {
24128041352SGreg Clayton             PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance);
242e996fd30SGreg Clayton         }
24328041352SGreg Clayton     }
2443c4f89d7STamas Berghammer 
2453c4f89d7STamas Berghammer     PlatformPOSIX::Terminate ();
24628041352SGreg Clayton }
247e996fd30SGreg Clayton 
248e996fd30SGreg Clayton Error
2498012cadbSGreg Clayton PlatformLinux::ResolveExecutable (const ModuleSpec &ms,
250ea5e0cc3SGreg Clayton                                   lldb::ModuleSP &exe_module_sp,
251ea5e0cc3SGreg Clayton                                   const FileSpecList *module_search_paths_ptr)
252e996fd30SGreg Clayton {
253e996fd30SGreg Clayton     Error error;
254e996fd30SGreg Clayton     // Nothing special to do here, just use the actual file and architecture
255e996fd30SGreg Clayton 
25628041352SGreg Clayton     char exe_path[PATH_MAX];
2578012cadbSGreg Clayton     ModuleSpec resolved_module_spec (ms);
258e996fd30SGreg Clayton 
25928041352SGreg Clayton     if (IsHost())
26028041352SGreg Clayton     {
26128041352SGreg Clayton         // If we have "ls" as the exe_file, resolve the executable location based on
262e996fd30SGreg Clayton         // the current path variables
2638012cadbSGreg Clayton         if (!resolved_module_spec.GetFileSpec().Exists())
26428041352SGreg Clayton         {
2658012cadbSGreg Clayton             resolved_module_spec.GetFileSpec().GetPath(exe_path, sizeof(exe_path));
2668012cadbSGreg Clayton             resolved_module_spec.GetFileSpec().SetFile(exe_path, true);
26728041352SGreg Clayton         }
26828041352SGreg Clayton 
2698012cadbSGreg Clayton         if (!resolved_module_spec.GetFileSpec().Exists())
2708012cadbSGreg Clayton             resolved_module_spec.GetFileSpec().ResolveExecutableLocation ();
271e996fd30SGreg Clayton 
2728012cadbSGreg Clayton         if (resolved_module_spec.GetFileSpec().Exists())
27328041352SGreg Clayton             error.Clear();
27428041352SGreg Clayton         else
27528041352SGreg Clayton         {
2768012cadbSGreg Clayton             error.SetErrorStringWithFormat("unable to find executable for '%s'", resolved_module_spec.GetFileSpec().GetPath().c_str());
27728041352SGreg Clayton         }
27828041352SGreg Clayton     }
27928041352SGreg Clayton     else
28028041352SGreg Clayton     {
28128041352SGreg Clayton         if (m_remote_platform_sp)
28228041352SGreg Clayton         {
2836474721cSOleksiy Vyalov             error = GetCachedExecutable (resolved_module_spec, exe_module_sp, nullptr, *m_remote_platform_sp);
28428041352SGreg Clayton         }
28528041352SGreg Clayton         else
28628041352SGreg Clayton         {
28728041352SGreg Clayton             // We may connect to a process and use the provided executable (Don't use local $PATH).
288e996fd30SGreg Clayton 
2898012cadbSGreg Clayton             if (resolved_module_spec.GetFileSpec().Exists())
29028041352SGreg Clayton                 error.Clear();
29128041352SGreg Clayton             else
29228041352SGreg Clayton                 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path);
29328041352SGreg Clayton         }
29428041352SGreg Clayton     }
29528041352SGreg Clayton 
29628041352SGreg Clayton     if (error.Success())
297e996fd30SGreg Clayton     {
2988012cadbSGreg Clayton         if (resolved_module_spec.GetArchitecture().IsValid())
299e996fd30SGreg Clayton         {
3008012cadbSGreg Clayton             error = ModuleList::GetSharedModule (resolved_module_spec,
301e996fd30SGreg Clayton                                                  exe_module_sp,
302e996fd30SGreg Clayton                                                  NULL,
3030c90ef47SGreg Clayton                                                  NULL,
304e996fd30SGreg Clayton                                                  NULL);
3059f0013d8SMichael Sartain             if (error.Fail())
3069f0013d8SMichael Sartain             {
3079f0013d8SMichael Sartain                 // If we failed, it may be because the vendor and os aren't known. If that is the
3089f0013d8SMichael Sartain                 // case, try setting them to the host architecture and give it another try.
3098012cadbSGreg Clayton                 llvm::Triple &module_triple = resolved_module_spec.GetArchitecture().GetTriple();
3109f0013d8SMichael Sartain                 bool is_vendor_specified = (module_triple.getVendor() != llvm::Triple::UnknownVendor);
3119f0013d8SMichael Sartain                 bool is_os_specified = (module_triple.getOS() != llvm::Triple::UnknownOS);
3129f0013d8SMichael Sartain                 if (!is_vendor_specified || !is_os_specified)
3139f0013d8SMichael Sartain                 {
31413b18261SZachary Turner                     const llvm::Triple &host_triple = HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple();
3159f0013d8SMichael Sartain 
3169f0013d8SMichael Sartain                     if (!is_vendor_specified)
3179f0013d8SMichael Sartain                         module_triple.setVendorName (host_triple.getVendorName());
3189f0013d8SMichael Sartain                     if (!is_os_specified)
3199f0013d8SMichael Sartain                         module_triple.setOSName (host_triple.getOSName());
3209f0013d8SMichael Sartain 
3218012cadbSGreg Clayton                     error = ModuleList::GetSharedModule (resolved_module_spec,
3229f0013d8SMichael Sartain                                                          exe_module_sp,
3239f0013d8SMichael Sartain                                                          NULL,
3249f0013d8SMichael Sartain                                                          NULL,
3259f0013d8SMichael Sartain                                                          NULL);
3269f0013d8SMichael Sartain                 }
3279f0013d8SMichael Sartain             }
328e996fd30SGreg Clayton 
329e635db49SSean Callanan             // TODO find out why exe_module_sp might be NULL
330e635db49SSean Callanan             if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL)
331e996fd30SGreg Clayton             {
332e996fd30SGreg Clayton                 exe_module_sp.reset();
333b5ad4ec7SGreg Clayton                 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s",
3348012cadbSGreg Clayton                                                 resolved_module_spec.GetFileSpec().GetPath().c_str(),
3358012cadbSGreg Clayton                                                 resolved_module_spec.GetArchitecture().GetArchitectureName());
336e996fd30SGreg Clayton             }
337e996fd30SGreg Clayton         }
338e996fd30SGreg Clayton         else
339e996fd30SGreg Clayton         {
340e996fd30SGreg Clayton             // No valid architecture was specified, ask the platform for
341e996fd30SGreg Clayton             // the architectures that we should be using (in the correct order)
342e996fd30SGreg Clayton             // and see if we can find a match that way
343e996fd30SGreg Clayton             StreamString arch_names;
3448012cadbSGreg Clayton             for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, resolved_module_spec.GetArchitecture()); ++idx)
345e996fd30SGreg Clayton             {
3468012cadbSGreg Clayton                 error = ModuleList::GetSharedModule (resolved_module_spec,
347e996fd30SGreg Clayton                                                      exe_module_sp,
348e996fd30SGreg Clayton                                                      NULL,
3490c90ef47SGreg Clayton                                                      NULL,
350e996fd30SGreg Clayton                                                      NULL);
351e996fd30SGreg Clayton                 // Did we find an executable using one of the
352e996fd30SGreg Clayton                 if (error.Success())
353e996fd30SGreg Clayton                 {
354e996fd30SGreg Clayton                     if (exe_module_sp && exe_module_sp->GetObjectFile())
355e996fd30SGreg Clayton                         break;
356e996fd30SGreg Clayton                     else
357e996fd30SGreg Clayton                         error.SetErrorToGenericError();
358e996fd30SGreg Clayton                 }
359e996fd30SGreg Clayton 
360e996fd30SGreg Clayton                 if (idx > 0)
361e996fd30SGreg Clayton                     arch_names.PutCString (", ");
3628012cadbSGreg Clayton                 arch_names.PutCString (resolved_module_spec.GetArchitecture().GetArchitectureName());
363e996fd30SGreg Clayton             }
364e996fd30SGreg Clayton 
365e996fd30SGreg Clayton             if (error.Fail() || !exe_module_sp)
366e996fd30SGreg Clayton             {
3678012cadbSGreg Clayton                 if (resolved_module_spec.GetFileSpec().Readable())
36839945dccSGreg Clayton                 {
369b5ad4ec7SGreg Clayton                     error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
3708012cadbSGreg Clayton                                                     resolved_module_spec.GetFileSpec().GetPath().c_str(),
37157abc5d6SGreg Clayton                                                     GetPluginName().GetCString(),
372e996fd30SGreg Clayton                                                     arch_names.GetString().c_str());
373e996fd30SGreg Clayton                 }
37439945dccSGreg Clayton                 else
37539945dccSGreg Clayton                 {
3768012cadbSGreg Clayton                     error.SetErrorStringWithFormat("'%s' is not readable", resolved_module_spec.GetFileSpec().GetPath().c_str());
37739945dccSGreg Clayton                 }
37839945dccSGreg Clayton             }
379e996fd30SGreg Clayton         }
380e996fd30SGreg Clayton     }
381e996fd30SGreg Clayton 
382e996fd30SGreg Clayton     return error;
383e996fd30SGreg Clayton }
384e996fd30SGreg Clayton 
385e996fd30SGreg Clayton Error
386fc995725SSteve Pucci PlatformLinux::GetFileWithUUID (const FileSpec &platform_file,
38728041352SGreg Clayton                                 const UUID *uuid_ptr, FileSpec &local_file)
388e996fd30SGreg Clayton {
38928041352SGreg Clayton     if (IsRemote())
39028041352SGreg Clayton     {
39128041352SGreg Clayton         if (m_remote_platform_sp)
392fc995725SSteve Pucci             return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file);
39328041352SGreg Clayton     }
39428041352SGreg Clayton 
395e996fd30SGreg Clayton     // Default to the local case
396e996fd30SGreg Clayton     local_file = platform_file;
397e996fd30SGreg Clayton     return Error();
398e996fd30SGreg Clayton }
399e996fd30SGreg Clayton 
400e996fd30SGreg Clayton 
401e996fd30SGreg Clayton //------------------------------------------------------------------
402e996fd30SGreg Clayton /// Default Constructor
403e996fd30SGreg Clayton //------------------------------------------------------------------
40428041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) :
405015d818bSTodd Fiala     PlatformPOSIX(is_host)  // This is the local host platform
406e996fd30SGreg Clayton {
407e996fd30SGreg Clayton }
408e996fd30SGreg Clayton 
409e996fd30SGreg Clayton //------------------------------------------------------------------
410e996fd30SGreg Clayton /// Destructor.
411e996fd30SGreg Clayton ///
412e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be
413e996fd30SGreg Clayton /// inherited from by the plug-in instance.
414e996fd30SGreg Clayton //------------------------------------------------------------------
415e996fd30SGreg Clayton PlatformLinux::~PlatformLinux()
416e996fd30SGreg Clayton {
417e996fd30SGreg Clayton }
418e996fd30SGreg Clayton 
419e996fd30SGreg Clayton bool
42013e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
421e996fd30SGreg Clayton {
42228041352SGreg Clayton     bool success = false;
42328041352SGreg Clayton     if (IsHost())
42428041352SGreg Clayton     {
42528041352SGreg Clayton         success = Platform::GetProcessInfo (pid, process_info);
42628041352SGreg Clayton     }
42728041352SGreg Clayton     else
42828041352SGreg Clayton     {
42928041352SGreg Clayton         if (m_remote_platform_sp)
43028041352SGreg Clayton             success = m_remote_platform_sp->GetProcessInfo (pid, process_info);
43128041352SGreg Clayton     }
43228041352SGreg Clayton     return success;
433e996fd30SGreg Clayton }
434e996fd30SGreg Clayton 
4358e6ec453SStephane Sezer uint32_t
4368e6ec453SStephane Sezer PlatformLinux::FindProcesses (const ProcessInstanceInfoMatch &match_info,
4378e6ec453SStephane Sezer                               ProcessInstanceInfoList &process_infos)
4388e6ec453SStephane Sezer {
4398e6ec453SStephane Sezer     uint32_t match_count = 0;
4408e6ec453SStephane Sezer     if (IsHost())
4418e6ec453SStephane Sezer     {
4428e6ec453SStephane Sezer         // Let the base class figure out the host details
4438e6ec453SStephane Sezer         match_count = Platform::FindProcesses (match_info, process_infos);
4448e6ec453SStephane Sezer     }
4458e6ec453SStephane Sezer     else
4468e6ec453SStephane Sezer     {
4478e6ec453SStephane Sezer         // If we are remote, we can only return results if we are connected
4488e6ec453SStephane Sezer         if (m_remote_platform_sp)
4498e6ec453SStephane Sezer             match_count = m_remote_platform_sp->FindProcesses (match_info, process_infos);
4508e6ec453SStephane Sezer     }
4518e6ec453SStephane Sezer     return match_count;
4528e6ec453SStephane Sezer }
4538e6ec453SStephane Sezer 
454e996fd30SGreg Clayton bool
455e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
456e996fd30SGreg Clayton {
457e49b8e06SRobert Flack     if (IsHost())
458e49b8e06SRobert Flack     {
459e49b8e06SRobert Flack         ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
460e49b8e06SRobert Flack         if (hostArch.GetTriple().isOSLinux())
461e49b8e06SRobert Flack         {
462e49b8e06SRobert Flack             if (idx == 0)
463e49b8e06SRobert Flack             {
464e49b8e06SRobert Flack                 arch = hostArch;
465e49b8e06SRobert Flack                 return arch.IsValid();
466e49b8e06SRobert Flack             }
467e49b8e06SRobert Flack             else if (idx == 1)
468e49b8e06SRobert Flack             {
469e49b8e06SRobert Flack                 // If the default host architecture is 64-bit, look for a 32-bit variant
470e49b8e06SRobert Flack                 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit())
471e49b8e06SRobert Flack                 {
472e49b8e06SRobert Flack                     arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
473e49b8e06SRobert Flack                     return arch.IsValid();
474e49b8e06SRobert Flack                 }
475e49b8e06SRobert Flack             }
476e49b8e06SRobert Flack         }
477e49b8e06SRobert Flack     }
478e49b8e06SRobert Flack     else
479e49b8e06SRobert Flack     {
480e49b8e06SRobert Flack         if (m_remote_platform_sp)
481e49b8e06SRobert Flack             return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
482bb1e283cSTed Woodward 
483bb1e283cSTed Woodward         llvm::Triple triple;
484bb1e283cSTed Woodward         // Set the OS to linux
485bb1e283cSTed Woodward         triple.setOS(llvm::Triple::Linux);
486bb1e283cSTed Woodward         // Set the architecture
487bb1e283cSTed Woodward         switch (idx)
488bb1e283cSTed Woodward         {
489bb1e283cSTed Woodward             case 0: triple.setArchName("x86_64"); break;
490bb1e283cSTed Woodward             case 1: triple.setArchName("i386"); break;
491bb1e283cSTed Woodward             case 2: triple.setArchName("arm"); break;
492bb1e283cSTed Woodward             case 3: triple.setArchName("aarch64"); break;
493bb1e283cSTed Woodward             case 4: triple.setArchName("mips64"); break;
494bb1e283cSTed Woodward             case 5: triple.setArchName("hexagon"); break;
495bb1e283cSTed Woodward             case 6: triple.setArchName("mips"); break;
496ce815e45SSagar Thakur             case 7: triple.setArchName("mips64el"); break;
497ce815e45SSagar Thakur             case 8: triple.setArchName("mipsel"); break;
498bb1e283cSTed Woodward             default: return false;
499bb1e283cSTed Woodward         }
500bb1e283cSTed Woodward         // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the vendor by
501bb1e283cSTed Woodward         // calling triple.SetVendorName("unknown") so that it is a "unspecified unknown".
502bb1e283cSTed Woodward         // This means when someone calls triple.GetVendorName() it will return an empty string
503bb1e283cSTed Woodward         // which indicates that the vendor can be set when two architectures are merged
504bb1e283cSTed Woodward 
505bb1e283cSTed Woodward         // Now set the triple into "arch" and return true
506bb1e283cSTed Woodward         arch.SetTriple(triple);
507bb1e283cSTed Woodward         return true;
508e49b8e06SRobert Flack     }
509e996fd30SGreg Clayton     return false;
510e996fd30SGreg Clayton }
511ecc11474SStephen Wilson 
512ecc11474SStephen Wilson void
513ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm)
514ecc11474SStephen Wilson {
5153be69dacSDaniel Malea     Platform::GetStatus(strm);
516ecc11474SStephen Wilson 
517b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX
518b743a803SStephane Sezer     // Display local kernel information only when we are running in host mode.
519b743a803SStephane Sezer     // Otherwise, we would end up printing non-Linux information (when running
520b743a803SStephane Sezer     // on Mac OS for example).
521b743a803SStephane Sezer     if (IsHost())
522b743a803SStephane Sezer     {
523b2f1fb29SVirgile Bello         struct utsname un;
524b2f1fb29SVirgile Bello 
5253be69dacSDaniel Malea         if (uname(&un))
5263be69dacSDaniel Malea             return;
5273be69dacSDaniel Malea 
5283be69dacSDaniel Malea         strm.Printf ("    Kernel: %s\n", un.sysname);
5293be69dacSDaniel Malea         strm.Printf ("   Release: %s\n", un.release);
5303be69dacSDaniel Malea         strm.Printf ("   Version: %s\n", un.version);
531b743a803SStephane Sezer     }
532b2f1fb29SVirgile Bello #endif
533ecc11474SStephen Wilson }
534ecc11474SStephen Wilson 
535ecc11474SStephen Wilson size_t
536ecc11474SStephen Wilson PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target,
537ecc11474SStephen Wilson                                                 BreakpointSite *bp_site)
538ecc11474SStephen Wilson {
539ecc11474SStephen Wilson     ArchSpec arch = target.GetArchitecture();
54028041352SGreg Clayton     const uint8_t *trap_opcode = NULL;
54128041352SGreg Clayton     size_t trap_opcode_size = 0;
542ecc11474SStephen Wilson 
543906e9acfSGreg Clayton     switch (arch.GetMachine())
544ecc11474SStephen Wilson     {
545ecc11474SStephen Wilson     default:
546ecc11474SStephen Wilson         assert(false && "CPU type not supported!");
547ecc11474SStephen Wilson         break;
548ecc11474SStephen Wilson 
5492afc5966STodd Fiala     case llvm::Triple::aarch64:
5502afc5966STodd Fiala         {
5512afc5966STodd Fiala             static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 };
5522afc5966STodd Fiala             trap_opcode = g_aarch64_opcode;
5532afc5966STodd Fiala             trap_opcode_size = sizeof(g_aarch64_opcode);
5542afc5966STodd Fiala         }
5552afc5966STodd Fiala         break;
556906e9acfSGreg Clayton     case llvm::Triple::x86:
557906e9acfSGreg Clayton     case llvm::Triple::x86_64:
55828041352SGreg Clayton         {
55928041352SGreg Clayton             static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
56028041352SGreg Clayton             trap_opcode = g_i386_breakpoint_opcode;
56128041352SGreg Clayton             trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
56228041352SGreg Clayton         }
563ecc11474SStephen Wilson         break;
564906e9acfSGreg Clayton     case llvm::Triple::hexagon:
5658006d319SDeepak Panickal         {
5668006d319SDeepak Panickal             static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 };
5678006d319SDeepak Panickal             trap_opcode = g_hex_opcode;
5688006d319SDeepak Panickal             trap_opcode_size = sizeof(g_hex_opcode);
5698006d319SDeepak Panickal         }
5708006d319SDeepak Panickal         break;
5712586e94bSJason Molenda     case llvm::Triple::arm:
5722586e94bSJason Molenda         {
5732586e94bSJason Molenda             // The ARM reference recommends the use of 0xe7fddefe and 0xdefe
5742586e94bSJason Molenda             // but the linux kernel does otherwise.
5752586e94bSJason Molenda             static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 };
5762586e94bSJason Molenda             static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde };
5772586e94bSJason Molenda 
5782586e94bSJason Molenda             lldb::BreakpointLocationSP bp_loc_sp (bp_site->GetOwnerAtIndex (0));
5792586e94bSJason Molenda             AddressClass addr_class = eAddressClassUnknown;
5802586e94bSJason Molenda 
5812586e94bSJason Molenda             if (bp_loc_sp)
5822586e94bSJason Molenda                 addr_class = bp_loc_sp->GetAddress ().GetAddressClass ();
5832586e94bSJason Molenda 
5842586e94bSJason Molenda             if (addr_class == eAddressClassCodeAlternateISA
58563c8be95STamas Berghammer                 || (addr_class == eAddressClassUnknown && (bp_site->GetLoadAddress() & 1)))
5862586e94bSJason Molenda             {
5872586e94bSJason Molenda                 trap_opcode = g_thumb_breakpoint_opcode;
5882586e94bSJason Molenda                 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
5892586e94bSJason Molenda             }
5902586e94bSJason Molenda             else
5912586e94bSJason Molenda             {
5922586e94bSJason Molenda                 trap_opcode = g_arm_breakpoint_opcode;
5932586e94bSJason Molenda                 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
5942586e94bSJason Molenda             }
5952586e94bSJason Molenda         }
5962586e94bSJason Molenda         break;
597794a4d5aSBhushan D. Attarde     case llvm::Triple::mips:
598c9335a3fSMohit K. Bhakkad     case llvm::Triple::mips64:
599c9335a3fSMohit K. Bhakkad         {
600c9335a3fSMohit K. Bhakkad             static const uint8_t g_hex_opcode[] = { 0x00, 0x00, 0x00, 0x0d };
601c9335a3fSMohit K. Bhakkad             trap_opcode = g_hex_opcode;
602c9335a3fSMohit K. Bhakkad             trap_opcode_size = sizeof(g_hex_opcode);
603c9335a3fSMohit K. Bhakkad         }
604c9335a3fSMohit K. Bhakkad         break;
605794a4d5aSBhushan D. Attarde     case llvm::Triple::mipsel:
6062c2acf96SMohit K. Bhakkad     case llvm::Triple::mips64el:
6072c2acf96SMohit K. Bhakkad         {
6082c2acf96SMohit K. Bhakkad             static const uint8_t g_hex_opcode[] = { 0x0d, 0x00, 0x00, 0x00 };
6092c2acf96SMohit K. Bhakkad             trap_opcode = g_hex_opcode;
6102c2acf96SMohit K. Bhakkad             trap_opcode_size = sizeof(g_hex_opcode);
6112c2acf96SMohit K. Bhakkad         }
6122c2acf96SMohit K. Bhakkad         break;
613ecc11474SStephen Wilson     }
614ecc11474SStephen Wilson 
61528041352SGreg Clayton     if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
61628041352SGreg Clayton         return trap_opcode_size;
61728041352SGreg Clayton     return 0;
61828041352SGreg Clayton }
61928041352SGreg Clayton 
620348fb385STodd Fiala int32_t
621348fb385STodd Fiala PlatformLinux::GetResumeCountForLaunchInfo (ProcessLaunchInfo &launch_info)
62228041352SGreg Clayton {
623348fb385STodd Fiala     int32_t resume_count = 0;
62428041352SGreg Clayton 
625348fb385STodd Fiala     // Always resume past the initial stop when we use eLaunchFlagDebug
626348fb385STodd Fiala     if (launch_info.GetFlags ().Test (eLaunchFlagDebug))
62728041352SGreg Clayton     {
628348fb385STodd Fiala         // Resume past the stop for the final exec into the true inferior.
629348fb385STodd Fiala         ++resume_count;
630348fb385STodd Fiala     }
631015d818bSTodd Fiala 
632348fb385STodd Fiala     // If we're not launching a shell, we're done.
63310687b0eSZachary Turner     const FileSpec &shell = launch_info.GetShell();
63410687b0eSZachary Turner     if (!shell)
635348fb385STodd Fiala         return resume_count;
636348fb385STodd Fiala 
63710687b0eSZachary Turner     std::string shell_string = shell.GetPath();
638348fb385STodd Fiala     // We're in a shell, so for sure we have to resume past the shell exec.
639348fb385STodd Fiala     ++resume_count;
640348fb385STodd Fiala 
641348fb385STodd Fiala     // Figure out what shell we're planning on using.
64210687b0eSZachary Turner     const char *shell_name = strrchr (shell_string.c_str(), '/');
643348fb385STodd Fiala     if (shell_name == NULL)
64410687b0eSZachary Turner         shell_name = shell_string.c_str();
64528041352SGreg Clayton     else
646348fb385STodd Fiala         shell_name++;
647348fb385STodd Fiala 
648348fb385STodd Fiala     if (strcmp (shell_name, "csh") == 0
649348fb385STodd Fiala              || strcmp (shell_name, "tcsh") == 0
650348fb385STodd Fiala              || strcmp (shell_name, "zsh") == 0
651348fb385STodd Fiala              || strcmp (shell_name, "sh") == 0)
65228041352SGreg Clayton     {
653348fb385STodd Fiala         // These shells seem to re-exec themselves.  Add another resume.
654348fb385STodd Fiala         ++resume_count;
655ecc11474SStephen Wilson     }
65613e8e1c3SJohnny Chen 
657348fb385STodd Fiala     return resume_count;
658348fb385STodd Fiala }
659348fb385STodd Fiala 
660348fb385STodd Fiala bool
661015d818bSTodd Fiala PlatformLinux::CanDebugProcess ()
662015d818bSTodd Fiala {
663015d818bSTodd Fiala     if (IsHost ())
664348fb385STodd Fiala     {
665*b36f9178SPavel Labath         return true;
666348fb385STodd Fiala     }
667348fb385STodd Fiala     else
668348fb385STodd Fiala     {
669015d818bSTodd Fiala         // If we're connected, we can debug.
670015d818bSTodd Fiala         return IsConnected ();
671015d818bSTodd Fiala     }
672348fb385STodd Fiala }
673015d818bSTodd Fiala 
674348fb385STodd Fiala // For local debugging, Linux will override the debug logic to use llgs-launch rather than
675348fb385STodd Fiala // lldb-launch, llgs-attach.  This differs from current lldb-launch, debugserver-attach
676348fb385STodd Fiala // approach on MacOSX.
67713e8e1c3SJohnny Chen lldb::ProcessSP
678348fb385STodd Fiala PlatformLinux::DebugProcess (ProcessLaunchInfo &launch_info,
67913e8e1c3SJohnny Chen                              Debugger &debugger,
680348fb385STodd Fiala                              Target *target,       // Can be NULL, if NULL create a new target, else use existing one
68113e8e1c3SJohnny Chen                              Error &error)
68213e8e1c3SJohnny Chen {
683db264a6dSTamas Berghammer     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
684348fb385STodd Fiala     if (log)
685348fb385STodd Fiala         log->Printf ("PlatformLinux::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target));
68628041352SGreg Clayton 
687348fb385STodd Fiala     // If we're a remote host, use standard behavior from parent class.
688348fb385STodd Fiala     if (!IsHost ())
6898012cadbSGreg Clayton         return PlatformPOSIX::DebugProcess (launch_info, debugger, target, error);
690348fb385STodd Fiala 
691348fb385STodd Fiala     //
692348fb385STodd Fiala     // For local debugging, we'll insist on having ProcessGDBRemote create the process.
693348fb385STodd Fiala     //
694348fb385STodd Fiala 
695348fb385STodd Fiala     ProcessSP process_sp;
696348fb385STodd Fiala 
697348fb385STodd Fiala     // Make sure we stop at the entry point
698348fb385STodd Fiala     launch_info.GetFlags ().Set (eLaunchFlagDebug);
699348fb385STodd Fiala 
700348fb385STodd Fiala     // We always launch the process we are going to debug in a separate process
701348fb385STodd Fiala     // group, since then we can handle ^C interrupts ourselves w/o having to worry
702348fb385STodd Fiala     // about the target getting them as well.
703348fb385STodd Fiala     launch_info.SetLaunchInSeparateProcessGroup(true);
704348fb385STodd Fiala 
705348fb385STodd Fiala     // Ensure we have a target.
706348fb385STodd Fiala     if (target == nullptr)
707348fb385STodd Fiala     {
708348fb385STodd Fiala         if (log)
709348fb385STodd Fiala             log->Printf ("PlatformLinux::%s creating new target", __FUNCTION__);
710348fb385STodd Fiala 
711348fb385STodd Fiala         TargetSP new_target_sp;
71228041352SGreg Clayton         error = debugger.GetTargetList().CreateTarget (debugger,
713348fb385STodd Fiala                                                        nullptr,
714348fb385STodd Fiala                                                        nullptr,
71528041352SGreg Clayton                                                        false,
716348fb385STodd Fiala                                                        nullptr,
71728041352SGreg Clayton                                                        new_target_sp);
718348fb385STodd Fiala         if (error.Fail ())
719348fb385STodd Fiala         {
720348fb385STodd Fiala             if (log)
721348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s failed to create new target: %s", __FUNCTION__, error.AsCString ());
722348fb385STodd Fiala             return process_sp;
723348fb385STodd Fiala         }
724348fb385STodd Fiala 
72528041352SGreg Clayton         target = new_target_sp.get();
726348fb385STodd Fiala         if (!target)
727348fb385STodd Fiala         {
728348fb385STodd Fiala             error.SetErrorString ("CreateTarget() returned nullptr");
729348fb385STodd Fiala             if (log)
730348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ());
731348fb385STodd Fiala             return process_sp;
732348fb385STodd Fiala         }
73328041352SGreg Clayton     }
73428041352SGreg Clayton     else
73528041352SGreg Clayton     {
736348fb385STodd Fiala         if (log)
737348fb385STodd Fiala             log->Printf ("PlatformLinux::%s using provided target", __FUNCTION__);
738348fb385STodd Fiala     }
739348fb385STodd Fiala 
740348fb385STodd Fiala     // Mark target as currently selected target.
74128041352SGreg Clayton     debugger.GetTargetList().SetSelectedTarget(target);
74228041352SGreg Clayton 
743348fb385STodd Fiala     // Now create the gdb-remote process.
744348fb385STodd Fiala     if (log)
745348fb385STodd Fiala         log->Printf ("PlatformLinux::%s having target create process with gdb-remote plugin", __FUNCTION__);
7468012cadbSGreg Clayton     process_sp = target->CreateProcess (launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr);
74728041352SGreg Clayton 
748348fb385STodd Fiala     if (!process_sp)
749348fb385STodd Fiala     {
750348fb385STodd Fiala         error.SetErrorString ("CreateProcess() failed for gdb-remote process");
751348fb385STodd Fiala         if (log)
752348fb385STodd Fiala             log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ());
753348fb385STodd Fiala         return process_sp;
754348fb385STodd Fiala     }
755348fb385STodd Fiala     else
756348fb385STodd Fiala     {
757348fb385STodd Fiala         if (log)
758348fb385STodd Fiala             log->Printf ("PlatformLinux::%s successfully created process", __FUNCTION__);
759348fb385STodd Fiala     }
760348fb385STodd Fiala 
761348fb385STodd Fiala     // Set the unix signals properly.
762348fb385STodd Fiala     process_sp->SetUnixSignals (Host::GetUnixSignals ());
763348fb385STodd Fiala 
764348fb385STodd Fiala     // Adjust launch for a hijacker.
765348fb385STodd Fiala     ListenerSP listener_sp;
766348fb385STodd Fiala     if (!launch_info.GetHijackListener ())
767348fb385STodd Fiala     {
768348fb385STodd Fiala         if (log)
769348fb385STodd Fiala             log->Printf ("PlatformLinux::%s setting up hijacker", __FUNCTION__);
770348fb385STodd Fiala 
771348fb385STodd Fiala         listener_sp.reset (new Listener("lldb.PlatformLinux.DebugProcess.hijack"));
772348fb385STodd Fiala         launch_info.SetHijackListener (listener_sp);
773348fb385STodd Fiala         process_sp->HijackProcessEvents (listener_sp.get ());
774348fb385STodd Fiala     }
775348fb385STodd Fiala 
776348fb385STodd Fiala     // Log file actions.
777348fb385STodd Fiala     if (log)
778348fb385STodd Fiala     {
779348fb385STodd Fiala         log->Printf ("PlatformLinux::%s launching process with the following file actions:", __FUNCTION__);
780348fb385STodd Fiala 
781348fb385STodd Fiala         StreamString stream;
782348fb385STodd Fiala         size_t i = 0;
783348fb385STodd Fiala         const FileAction *file_action;
784348fb385STodd Fiala         while ((file_action = launch_info.GetFileActionAtIndex (i++)) != nullptr)
785348fb385STodd Fiala         {
786348fb385STodd Fiala             file_action->Dump (stream);
787348fb385STodd Fiala             log->PutCString (stream.GetString().c_str ());
788348fb385STodd Fiala             stream.Clear();
789348fb385STodd Fiala         }
790348fb385STodd Fiala     }
791348fb385STodd Fiala 
792348fb385STodd Fiala     // Do the launch.
793348fb385STodd Fiala     error = process_sp->Launch(launch_info);
794348fb385STodd Fiala     if (error.Success ())
795348fb385STodd Fiala     {
796348fb385STodd Fiala         // Handle the hijacking of process events.
797348fb385STodd Fiala         if (listener_sp)
798348fb385STodd Fiala         {
799348fb385STodd Fiala             const StateType state = process_sp->WaitForProcessToStop (NULL, NULL, false, listener_sp.get());
800348fb385STodd Fiala 
801348fb385STodd Fiala             if (state == eStateStopped)
802348fb385STodd Fiala             {
803348fb385STodd Fiala                 if (log)
804348fb385STodd Fiala                     log->Printf ("PlatformLinux::%s pid %" PRIu64 " state %s\n",
805348fb385STodd Fiala                                  __FUNCTION__, process_sp->GetID (), StateAsCString (state));
806348fb385STodd Fiala             }
807348fb385STodd Fiala             else
808348fb385STodd Fiala             {
809348fb385STodd Fiala                 if (log)
810348fb385STodd Fiala                     log->Printf ("PlatformLinux::%s pid %" PRIu64 " state is not stopped - %s\n",
811348fb385STodd Fiala                                  __FUNCTION__, process_sp->GetID (), StateAsCString (state));
812348fb385STodd Fiala             }
813348fb385STodd Fiala         }
814348fb385STodd Fiala 
815348fb385STodd Fiala         // Hook up process PTY if we have one (which we should for local debugging with llgs).
816348fb385STodd Fiala         int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
817348fb385STodd Fiala         if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
818348fb385STodd Fiala         {
819348fb385STodd Fiala             process_sp->SetSTDIOFileDescriptor(pty_fd);
820348fb385STodd Fiala             if (log)
821348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s pid %" PRIu64 " hooked up STDIO pty to process", __FUNCTION__, process_sp->GetID ());
822348fb385STodd Fiala         }
823348fb385STodd Fiala         else
824348fb385STodd Fiala         {
825348fb385STodd Fiala             if (log)
826348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s pid %" PRIu64 " not using process STDIO pty", __FUNCTION__, process_sp->GetID ());
82728041352SGreg Clayton         }
82828041352SGreg Clayton     }
82928041352SGreg Clayton     else
83028041352SGreg Clayton     {
831348fb385STodd Fiala         if (log)
832348fb385STodd Fiala             log->Printf ("PlatformLinux::%s process launch failed: %s", __FUNCTION__, error.AsCString ());
833348fb385STodd Fiala         // FIXME figure out appropriate cleanup here.  Do we delete the target? Do we delete the process?  Does our caller do that?
83428041352SGreg Clayton     }
835348fb385STodd Fiala 
83628041352SGreg Clayton     return process_sp;
83713e8e1c3SJohnny Chen }
8382094dbf4SJason Molenda 
8392094dbf4SJason Molenda void
8402094dbf4SJason Molenda PlatformLinux::CalculateTrapHandlerSymbolNames ()
8412094dbf4SJason Molenda {
8422094dbf4SJason Molenda     m_trap_handlers.push_back (ConstString ("_sigtramp"));
8432094dbf4SJason Molenda }
844af245d11STodd Fiala 
845af245d11STodd Fiala Error
846db264a6dSTamas Berghammer PlatformLinux::LaunchNativeProcess (ProcessLaunchInfo &launch_info,
847db264a6dSTamas Berghammer                                     NativeProcessProtocol::NativeDelegate &native_delegate,
848af245d11STodd Fiala                                     NativeProcessProtocolSP &process_sp)
849af245d11STodd Fiala {
850c6ec76e3STamas Berghammer #if !defined(__linux__)
851c6ec76e3STamas Berghammer     return Error("Only implemented on Linux hosts");
852c6ec76e3STamas Berghammer #else
853af245d11STodd Fiala     if (!IsHost ())
854af245d11STodd Fiala         return Error("PlatformLinux::%s (): cannot launch a debug process when not the host", __FUNCTION__);
855af245d11STodd Fiala 
856af245d11STodd Fiala     // Retrieve the exe module.
857af245d11STodd Fiala     lldb::ModuleSP exe_module_sp;
8586edef204SOleksiy Vyalov     ModuleSpec exe_module_spec(launch_info.GetExecutableFile(), launch_info.GetArchitecture());
859af245d11STodd Fiala 
860af245d11STodd Fiala     Error error = ResolveExecutable (
8616edef204SOleksiy Vyalov         exe_module_spec,
862af245d11STodd Fiala         exe_module_sp,
863af245d11STodd Fiala         NULL);
864af245d11STodd Fiala 
865af245d11STodd Fiala     if (!error.Success ())
866af245d11STodd Fiala         return error;
867af245d11STodd Fiala 
868af245d11STodd Fiala     if (!exe_module_sp)
869af245d11STodd Fiala         return Error("exe_module_sp could not be resolved for %s", launch_info.GetExecutableFile ().GetPath ().c_str ());
870af245d11STodd Fiala 
871af245d11STodd Fiala     // Launch it for debugging
87247b11c61STamas Berghammer     error = process_linux::NativeProcessLinux::LaunchProcess (
873af245d11STodd Fiala         exe_module_sp.get (),
874af245d11STodd Fiala         launch_info,
875af245d11STodd Fiala         native_delegate,
876af245d11STodd Fiala         process_sp);
877af245d11STodd Fiala 
878af245d11STodd Fiala     return error;
879c6ec76e3STamas Berghammer #endif
880af245d11STodd Fiala }
881af245d11STodd Fiala 
882af245d11STodd Fiala Error
883af245d11STodd Fiala PlatformLinux::AttachNativeProcess (lldb::pid_t pid,
884db264a6dSTamas Berghammer                                     NativeProcessProtocol::NativeDelegate &native_delegate,
885af245d11STodd Fiala                                     NativeProcessProtocolSP &process_sp)
886af245d11STodd Fiala {
887c6ec76e3STamas Berghammer #if !defined(__linux__)
888c6ec76e3STamas Berghammer     return Error("Only implemented on Linux hosts");
889c6ec76e3STamas Berghammer #else
890af245d11STodd Fiala     if (!IsHost ())
891af245d11STodd Fiala         return Error("PlatformLinux::%s (): cannot attach to a debug process when not the host", __FUNCTION__);
892af245d11STodd Fiala 
893af245d11STodd Fiala     // Launch it for debugging
89447b11c61STamas Berghammer     return process_linux::NativeProcessLinux::AttachToProcess (pid, native_delegate, process_sp);
895c6ec76e3STamas Berghammer #endif
896af245d11STodd Fiala }
89796ad3de5SRobert Flack 
89896ad3de5SRobert Flack uint64_t
89996ad3de5SRobert Flack PlatformLinux::ConvertMmapFlagsToPlatform(unsigned flags)
90096ad3de5SRobert Flack {
90196ad3de5SRobert Flack     uint64_t flags_platform = 0;
90296ad3de5SRobert Flack     if (flags & eMmapFlagsPrivate)
90396ad3de5SRobert Flack         flags_platform |= MAP_PRIVATE;
90496ad3de5SRobert Flack     if (flags & eMmapFlagsAnon)
90596ad3de5SRobert Flack         flags_platform |= MAP_ANON;
90696ad3de5SRobert Flack     return flags_platform;
90796ad3de5SRobert Flack }
908