1e996fd30SGreg Clayton //===-- PlatformLinux.cpp ---------------------------------------*- C++ -*-===//
2e996fd30SGreg Clayton //
3e996fd30SGreg Clayton //                     The LLVM Compiler Infrastructure
4e996fd30SGreg Clayton //
5e996fd30SGreg Clayton // This file is distributed under the University of Illinois Open Source
6e996fd30SGreg Clayton // License. See LICENSE.TXT for details.
7e996fd30SGreg Clayton //
8e996fd30SGreg Clayton //===----------------------------------------------------------------------===//
9e996fd30SGreg Clayton 
1093a64300SDaniel Malea #include "lldb/lldb-python.h"
1193a64300SDaniel Malea 
12e996fd30SGreg Clayton #include "PlatformLinux.h"
13b2f1fb29SVirgile Bello #include "lldb/Host/Config.h"
14e996fd30SGreg Clayton 
15e996fd30SGreg Clayton // C Includes
16ecc11474SStephen Wilson #include <stdio.h>
17b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX
18ecc11474SStephen Wilson #include <sys/utsname.h>
19b2f1fb29SVirgile Bello #endif
20ecc11474SStephen Wilson 
21e996fd30SGreg Clayton // C++ Includes
22e996fd30SGreg Clayton // Other libraries and framework includes
23e996fd30SGreg Clayton // Project includes
242586e94bSJason Molenda #include "lldb/Breakpoint/BreakpointLocation.h"
2528041352SGreg Clayton #include "lldb/Core/Debugger.h"
26348fb385STodd Fiala #include "lldb/Core/Error.h"
27015d818bSTodd Fiala #include "lldb/Core/Log.h"
28e996fd30SGreg Clayton #include "lldb/Core/Module.h"
29e996fd30SGreg Clayton #include "lldb/Core/ModuleList.h"
301f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h"
31ecc11474SStephen Wilson #include "lldb/Core/PluginManager.h"
32348fb385STodd Fiala #include "lldb/Core/State.h"
33e996fd30SGreg Clayton #include "lldb/Core/StreamString.h"
34e996fd30SGreg Clayton #include "lldb/Host/FileSpec.h"
3513b18261SZachary Turner #include "lldb/Host/HostInfo.h"
36348fb385STodd Fiala #include "lldb/Interpreter/OptionValueProperties.h"
37348fb385STodd Fiala #include "lldb/Interpreter/Property.h"
38ecc11474SStephen Wilson #include "lldb/Target/Target.h"
39e996fd30SGreg Clayton #include "lldb/Target/Process.h"
40e996fd30SGreg Clayton 
41af245d11STodd Fiala #if defined(__linux__)
42af245d11STodd Fiala #include "../../Process/Linux/NativeProcessLinux.h"
43af245d11STodd Fiala #endif
44af245d11STodd Fiala 
4596ad3de5SRobert Flack // Define these constants from Linux mman.h for use when targetting
4696ad3de5SRobert Flack // remote linux systems even when host has different values.
4796ad3de5SRobert Flack #define MAP_PRIVATE 2
4896ad3de5SRobert Flack #define MAP_ANON 0x20
4996ad3de5SRobert Flack 
50e996fd30SGreg Clayton using namespace lldb;
51e996fd30SGreg Clayton using namespace lldb_private;
52db264a6dSTamas Berghammer using namespace lldb_private::platform_linux;
53e996fd30SGreg Clayton 
5428041352SGreg Clayton static uint32_t g_initialize_count = 0;
5528041352SGreg Clayton 
56281961a8STodd Fiala //------------------------------------------------------------------
57281961a8STodd Fiala /// Code to handle the PlatformLinux settings
58281961a8STodd Fiala //------------------------------------------------------------------
59281961a8STodd Fiala 
60348fb385STodd Fiala namespace
61348fb385STodd Fiala {
62348fb385STodd Fiala     enum
63348fb385STodd Fiala     {
64348fb385STodd Fiala         ePropertyUseLlgsForLocal = 0,
65348fb385STodd Fiala     };
66348fb385STodd Fiala 
67db264a6dSTamas Berghammer     class PlatformLinuxProperties : public Properties
68db264a6dSTamas Berghammer     {
69db264a6dSTamas Berghammer     public:
70db264a6dSTamas Berghammer         static ConstString&
71db264a6dSTamas Berghammer         GetSettingName ();
72db264a6dSTamas Berghammer 
73db264a6dSTamas Berghammer         PlatformLinuxProperties();
74db264a6dSTamas Berghammer 
75db264a6dSTamas Berghammer         virtual
76db264a6dSTamas Berghammer         ~PlatformLinuxProperties() = default;
77db264a6dSTamas Berghammer 
78db264a6dSTamas Berghammer         bool
79db264a6dSTamas Berghammer         GetUseLlgsForLocal() const;
80db264a6dSTamas Berghammer 
81db264a6dSTamas Berghammer     private:
82db264a6dSTamas Berghammer         static const PropertyDefinition*
83db264a6dSTamas Berghammer         GetStaticPropertyDefinitions();
84db264a6dSTamas Berghammer     };
85db264a6dSTamas Berghammer 
86db264a6dSTamas Berghammer     typedef std::shared_ptr<PlatformLinuxProperties> PlatformLinuxPropertiesSP;
87db264a6dSTamas Berghammer 
88db264a6dSTamas Berghammer } // anonymous namespace
89db264a6dSTamas Berghammer 
90db264a6dSTamas Berghammer PlatformLinuxProperties::PlatformLinuxProperties() :
91db264a6dSTamas Berghammer     Properties ()
92db264a6dSTamas Berghammer {
93db264a6dSTamas Berghammer     m_collection_sp.reset (new OptionValueProperties(GetSettingName ()));
94db264a6dSTamas Berghammer     m_collection_sp->Initialize (GetStaticPropertyDefinitions ());
95db264a6dSTamas Berghammer }
96db264a6dSTamas Berghammer 
97db264a6dSTamas Berghammer ConstString&
98db264a6dSTamas Berghammer PlatformLinuxProperties::GetSettingName ()
99db264a6dSTamas Berghammer {
100db264a6dSTamas Berghammer     static ConstString g_setting_name("linux");
101db264a6dSTamas Berghammer     return g_setting_name;
102db264a6dSTamas Berghammer }
103db264a6dSTamas Berghammer 
104db264a6dSTamas Berghammer bool
105db264a6dSTamas Berghammer PlatformLinuxProperties::GetUseLlgsForLocal() const
106db264a6dSTamas Berghammer {
107db264a6dSTamas Berghammer     const uint32_t idx = ePropertyUseLlgsForLocal;
108db264a6dSTamas Berghammer     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, GetStaticPropertyDefinitions()[idx].default_uint_value != 0);
109db264a6dSTamas Berghammer }
110db264a6dSTamas Berghammer 
111348fb385STodd Fiala const PropertyDefinition*
112db264a6dSTamas Berghammer PlatformLinuxProperties::GetStaticPropertyDefinitions()
113348fb385STodd Fiala {
114281961a8STodd Fiala     static PropertyDefinition
115281961a8STodd Fiala     g_properties[] =
116281961a8STodd Fiala     {
117bc477ddeSVince Harron         { "use-llgs-for-local" , OptionValue::eTypeBoolean, true, true, NULL, NULL, "Control whether the platform uses llgs for local debug sessions." },
118281961a8STodd Fiala         {  NULL        , OptionValue::eTypeInvalid, false, 0  , NULL, NULL, NULL  }
119281961a8STodd Fiala     };
120281961a8STodd Fiala 
121bc477ddeSVince Harron     // Allow environment variable to disable llgs-local.
122bc477ddeSVince Harron     if (getenv("PLATFORM_LINUX_DISABLE_LLGS_LOCAL"))
123bc477ddeSVince Harron         g_properties[ePropertyUseLlgsForLocal].default_uint_value = false;
124281961a8STodd Fiala 
125348fb385STodd Fiala     return g_properties;
126348fb385STodd Fiala }
127281961a8STodd Fiala 
128281961a8STodd Fiala static const PlatformLinuxPropertiesSP &
129281961a8STodd Fiala GetGlobalProperties()
130281961a8STodd Fiala {
131281961a8STodd Fiala     static PlatformLinuxPropertiesSP g_settings_sp;
132281961a8STodd Fiala     if (!g_settings_sp)
133281961a8STodd Fiala         g_settings_sp.reset (new PlatformLinuxProperties ());
134281961a8STodd Fiala     return g_settings_sp;
135281961a8STodd Fiala }
136281961a8STodd Fiala 
137281961a8STodd Fiala void
138db264a6dSTamas Berghammer PlatformLinux::DebuggerInitialize (Debugger &debugger)
139281961a8STodd Fiala {
140281961a8STodd Fiala     if (!PluginManager::GetSettingForPlatformPlugin (debugger, PlatformLinuxProperties::GetSettingName()))
141281961a8STodd Fiala     {
142281961a8STodd Fiala         const bool is_global_setting = true;
143281961a8STodd Fiala         PluginManager::CreateSettingForPlatformPlugin (debugger,
144281961a8STodd Fiala                                                        GetGlobalProperties()->GetValueProperties(),
145281961a8STodd Fiala                                                        ConstString ("Properties for the PlatformLinux plug-in."),
146281961a8STodd Fiala                                                        is_global_setting);
147281961a8STodd Fiala     }
148281961a8STodd Fiala }
149281961a8STodd Fiala 
150281961a8STodd Fiala 
151281961a8STodd Fiala //------------------------------------------------------------------
152281961a8STodd Fiala 
153615eb7e6SGreg Clayton PlatformSP
154b3a40ba8SGreg Clayton PlatformLinux::CreateInstance (bool force, const ArchSpec *arch)
155ecc11474SStephen Wilson {
156db264a6dSTamas Berghammer     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
157015d818bSTodd Fiala     if (log)
158015d818bSTodd Fiala     {
159015d818bSTodd Fiala         const char *arch_name;
160015d818bSTodd Fiala         if (arch && arch->GetArchitectureName ())
161015d818bSTodd Fiala             arch_name = arch->GetArchitectureName ();
162015d818bSTodd Fiala         else
163015d818bSTodd Fiala             arch_name = "<null>";
164015d818bSTodd Fiala 
165015d818bSTodd Fiala         const char *triple_cstr = arch ? arch->GetTriple ().getTriple ().c_str() : "<null>";
166015d818bSTodd Fiala 
167015d818bSTodd Fiala         log->Printf ("PlatformLinux::%s(force=%s, arch={%s,%s})", __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
168015d818bSTodd Fiala     }
169015d818bSTodd Fiala 
170b3a40ba8SGreg Clayton     bool create = force;
171b3a40ba8SGreg Clayton     if (create == false && arch && arch->IsValid())
172b3a40ba8SGreg Clayton     {
173b3a40ba8SGreg Clayton         const llvm::Triple &triple = arch->GetTriple();
17470512317SGreg Clayton         switch (triple.getOS())
17570512317SGreg Clayton         {
17670512317SGreg Clayton             case llvm::Triple::Linux:
177b1da257aSTed Woodward                 create = true;
17870512317SGreg Clayton                 break;
17970512317SGreg Clayton 
180dbc6c0bbSGreg Clayton #if defined(__linux__)
181dbc6c0bbSGreg Clayton             // Only accept "unknown" for the OS if the host is linux and
1826a7f3338SBruce Mitchener             // it "unknown" wasn't specified (it was just returned because it
183dbc6c0bbSGreg Clayton             // was NOT specified)
184015d818bSTodd Fiala             case llvm::Triple::OSType::UnknownOS:
18570512317SGreg Clayton                 create = !arch->TripleOSWasSpecified();
18670512317SGreg Clayton                 break;
187dbc6c0bbSGreg Clayton #endif
18870512317SGreg Clayton             default:
18970512317SGreg Clayton                 break;
19070512317SGreg Clayton         }
19170512317SGreg Clayton     }
192015d818bSTodd Fiala 
193b3a40ba8SGreg Clayton     if (create)
194015d818bSTodd Fiala     {
195015d818bSTodd Fiala         if (log)
196015d818bSTodd Fiala             log->Printf ("PlatformLinux::%s() creating remote-linux platform", __FUNCTION__);
197615eb7e6SGreg Clayton         return PlatformSP(new PlatformLinux(false));
198015d818bSTodd Fiala     }
199015d818bSTodd Fiala 
200015d818bSTodd Fiala     if (log)
201015d818bSTodd Fiala         log->Printf ("PlatformLinux::%s() aborting creation of remote-linux platform", __FUNCTION__);
202015d818bSTodd Fiala 
203615eb7e6SGreg Clayton     return PlatformSP();
204ecc11474SStephen Wilson }
205ecc11474SStephen Wilson 
206ecc11474SStephen Wilson 
207db264a6dSTamas Berghammer ConstString
20857abc5d6SGreg Clayton PlatformLinux::GetPluginNameStatic (bool is_host)
209ecc11474SStephen Wilson {
21028041352SGreg Clayton     if (is_host)
21157abc5d6SGreg Clayton     {
21257abc5d6SGreg Clayton         static ConstString g_host_name(Platform::GetHostPlatformName ());
21357abc5d6SGreg Clayton         return g_host_name;
21457abc5d6SGreg Clayton     }
21528041352SGreg Clayton     else
21657abc5d6SGreg Clayton     {
21757abc5d6SGreg Clayton         static ConstString g_remote_name("remote-linux");
21857abc5d6SGreg Clayton         return g_remote_name;
21957abc5d6SGreg Clayton     }
22028041352SGreg Clayton }
22128041352SGreg Clayton 
22228041352SGreg Clayton const char *
22328041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic (bool is_host)
22428041352SGreg Clayton {
22528041352SGreg Clayton     if (is_host)
22628041352SGreg Clayton         return "Local Linux user platform plug-in.";
22728041352SGreg Clayton     else
22828041352SGreg Clayton         return "Remote Linux user platform plug-in.";
229ecc11474SStephen Wilson }
230ecc11474SStephen Wilson 
231db264a6dSTamas Berghammer ConstString
23257abc5d6SGreg Clayton PlatformLinux::GetPluginName()
23357abc5d6SGreg Clayton {
23457abc5d6SGreg Clayton     return GetPluginNameStatic(IsHost());
23557abc5d6SGreg Clayton }
23657abc5d6SGreg Clayton 
237e996fd30SGreg Clayton void
238e996fd30SGreg Clayton PlatformLinux::Initialize ()
239e996fd30SGreg Clayton {
2403c4f89d7STamas Berghammer     PlatformPOSIX::Initialize ();
2413c4f89d7STamas Berghammer 
24228041352SGreg Clayton     if (g_initialize_count++ == 0)
243ecc11474SStephen Wilson     {
2441c6a1ea9STamas Berghammer #if defined(__linux__) && !defined(__ANDROID__)
24528041352SGreg Clayton         PlatformSP default_platform_sp (new PlatformLinux(true));
24613b18261SZachary Turner         default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
247615eb7e6SGreg Clayton         Platform::SetHostPlatform (default_platform_sp);
24828041352SGreg Clayton #endif
24957abc5d6SGreg Clayton         PluginManager::RegisterPlugin(PlatformLinux::GetPluginNameStatic(false),
25028041352SGreg Clayton                                       PlatformLinux::GetPluginDescriptionStatic(false),
251281961a8STodd Fiala                                       PlatformLinux::CreateInstance,
252281961a8STodd Fiala                                       PlatformLinux::DebuggerInitialize);
253ecc11474SStephen Wilson     }
254e996fd30SGreg Clayton }
255e996fd30SGreg Clayton 
256e996fd30SGreg Clayton void
257e996fd30SGreg Clayton PlatformLinux::Terminate ()
258e996fd30SGreg Clayton {
25928041352SGreg Clayton     if (g_initialize_count > 0)
26028041352SGreg Clayton     {
26128041352SGreg Clayton         if (--g_initialize_count == 0)
26228041352SGreg Clayton         {
26328041352SGreg Clayton             PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance);
264e996fd30SGreg Clayton         }
26528041352SGreg Clayton     }
2663c4f89d7STamas Berghammer 
2673c4f89d7STamas Berghammer     PlatformPOSIX::Terminate ();
26828041352SGreg Clayton }
269e996fd30SGreg Clayton 
270e996fd30SGreg Clayton Error
2718012cadbSGreg Clayton PlatformLinux::ResolveExecutable (const ModuleSpec &ms,
272ea5e0cc3SGreg Clayton                                   lldb::ModuleSP &exe_module_sp,
273ea5e0cc3SGreg Clayton                                   const FileSpecList *module_search_paths_ptr)
274e996fd30SGreg Clayton {
275e996fd30SGreg Clayton     Error error;
276e996fd30SGreg Clayton     // Nothing special to do here, just use the actual file and architecture
277e996fd30SGreg Clayton 
27828041352SGreg Clayton     char exe_path[PATH_MAX];
2798012cadbSGreg Clayton     ModuleSpec resolved_module_spec (ms);
280e996fd30SGreg Clayton 
28128041352SGreg Clayton     if (IsHost())
28228041352SGreg Clayton     {
28328041352SGreg Clayton         // If we have "ls" as the exe_file, resolve the executable location based on
284e996fd30SGreg Clayton         // the current path variables
2858012cadbSGreg Clayton         if (!resolved_module_spec.GetFileSpec().Exists())
28628041352SGreg Clayton         {
2878012cadbSGreg Clayton             resolved_module_spec.GetFileSpec().GetPath(exe_path, sizeof(exe_path));
2888012cadbSGreg Clayton             resolved_module_spec.GetFileSpec().SetFile(exe_path, true);
28928041352SGreg Clayton         }
29028041352SGreg Clayton 
2918012cadbSGreg Clayton         if (!resolved_module_spec.GetFileSpec().Exists())
2928012cadbSGreg Clayton             resolved_module_spec.GetFileSpec().ResolveExecutableLocation ();
293e996fd30SGreg Clayton 
2948012cadbSGreg Clayton         if (resolved_module_spec.GetFileSpec().Exists())
29528041352SGreg Clayton             error.Clear();
29628041352SGreg Clayton         else
29728041352SGreg Clayton         {
2988012cadbSGreg Clayton             error.SetErrorStringWithFormat("unable to find executable for '%s'", resolved_module_spec.GetFileSpec().GetPath().c_str());
29928041352SGreg Clayton         }
30028041352SGreg Clayton     }
30128041352SGreg Clayton     else
30228041352SGreg Clayton     {
30328041352SGreg Clayton         if (m_remote_platform_sp)
30428041352SGreg Clayton         {
3056474721cSOleksiy Vyalov             error = GetCachedExecutable (resolved_module_spec, exe_module_sp, nullptr, *m_remote_platform_sp);
30628041352SGreg Clayton         }
30728041352SGreg Clayton         else
30828041352SGreg Clayton         {
30928041352SGreg Clayton             // We may connect to a process and use the provided executable (Don't use local $PATH).
310e996fd30SGreg Clayton 
3118012cadbSGreg Clayton             if (resolved_module_spec.GetFileSpec().Exists())
31228041352SGreg Clayton                 error.Clear();
31328041352SGreg Clayton             else
31428041352SGreg Clayton                 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path);
31528041352SGreg Clayton         }
31628041352SGreg Clayton     }
31728041352SGreg Clayton 
31828041352SGreg Clayton     if (error.Success())
319e996fd30SGreg Clayton     {
3208012cadbSGreg Clayton         if (resolved_module_spec.GetArchitecture().IsValid())
321e996fd30SGreg Clayton         {
3228012cadbSGreg Clayton             error = ModuleList::GetSharedModule (resolved_module_spec,
323e996fd30SGreg Clayton                                                  exe_module_sp,
324e996fd30SGreg Clayton                                                  NULL,
3250c90ef47SGreg Clayton                                                  NULL,
326e996fd30SGreg Clayton                                                  NULL);
3279f0013d8SMichael Sartain             if (error.Fail())
3289f0013d8SMichael Sartain             {
3299f0013d8SMichael Sartain                 // If we failed, it may be because the vendor and os aren't known. If that is the
3309f0013d8SMichael Sartain                 // case, try setting them to the host architecture and give it another try.
3318012cadbSGreg Clayton                 llvm::Triple &module_triple = resolved_module_spec.GetArchitecture().GetTriple();
3329f0013d8SMichael Sartain                 bool is_vendor_specified = (module_triple.getVendor() != llvm::Triple::UnknownVendor);
3339f0013d8SMichael Sartain                 bool is_os_specified = (module_triple.getOS() != llvm::Triple::UnknownOS);
3349f0013d8SMichael Sartain                 if (!is_vendor_specified || !is_os_specified)
3359f0013d8SMichael Sartain                 {
33613b18261SZachary Turner                     const llvm::Triple &host_triple = HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple();
3379f0013d8SMichael Sartain 
3389f0013d8SMichael Sartain                     if (!is_vendor_specified)
3399f0013d8SMichael Sartain                         module_triple.setVendorName (host_triple.getVendorName());
3409f0013d8SMichael Sartain                     if (!is_os_specified)
3419f0013d8SMichael Sartain                         module_triple.setOSName (host_triple.getOSName());
3429f0013d8SMichael Sartain 
3438012cadbSGreg Clayton                     error = ModuleList::GetSharedModule (resolved_module_spec,
3449f0013d8SMichael Sartain                                                          exe_module_sp,
3459f0013d8SMichael Sartain                                                          NULL,
3469f0013d8SMichael Sartain                                                          NULL,
3479f0013d8SMichael Sartain                                                          NULL);
3489f0013d8SMichael Sartain                 }
3499f0013d8SMichael Sartain             }
350e996fd30SGreg Clayton 
351e635db49SSean Callanan             // TODO find out why exe_module_sp might be NULL
352e635db49SSean Callanan             if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL)
353e996fd30SGreg Clayton             {
354e996fd30SGreg Clayton                 exe_module_sp.reset();
355b5ad4ec7SGreg Clayton                 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s",
3568012cadbSGreg Clayton                                                 resolved_module_spec.GetFileSpec().GetPath().c_str(),
3578012cadbSGreg Clayton                                                 resolved_module_spec.GetArchitecture().GetArchitectureName());
358e996fd30SGreg Clayton             }
359e996fd30SGreg Clayton         }
360e996fd30SGreg Clayton         else
361e996fd30SGreg Clayton         {
362e996fd30SGreg Clayton             // No valid architecture was specified, ask the platform for
363e996fd30SGreg Clayton             // the architectures that we should be using (in the correct order)
364e996fd30SGreg Clayton             // and see if we can find a match that way
365e996fd30SGreg Clayton             StreamString arch_names;
3668012cadbSGreg Clayton             for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, resolved_module_spec.GetArchitecture()); ++idx)
367e996fd30SGreg Clayton             {
3688012cadbSGreg Clayton                 error = ModuleList::GetSharedModule (resolved_module_spec,
369e996fd30SGreg Clayton                                                      exe_module_sp,
370e996fd30SGreg Clayton                                                      NULL,
3710c90ef47SGreg Clayton                                                      NULL,
372e996fd30SGreg Clayton                                                      NULL);
373e996fd30SGreg Clayton                 // Did we find an executable using one of the
374e996fd30SGreg Clayton                 if (error.Success())
375e996fd30SGreg Clayton                 {
376e996fd30SGreg Clayton                     if (exe_module_sp && exe_module_sp->GetObjectFile())
377e996fd30SGreg Clayton                         break;
378e996fd30SGreg Clayton                     else
379e996fd30SGreg Clayton                         error.SetErrorToGenericError();
380e996fd30SGreg Clayton                 }
381e996fd30SGreg Clayton 
382e996fd30SGreg Clayton                 if (idx > 0)
383e996fd30SGreg Clayton                     arch_names.PutCString (", ");
3848012cadbSGreg Clayton                 arch_names.PutCString (resolved_module_spec.GetArchitecture().GetArchitectureName());
385e996fd30SGreg Clayton             }
386e996fd30SGreg Clayton 
387e996fd30SGreg Clayton             if (error.Fail() || !exe_module_sp)
388e996fd30SGreg Clayton             {
3898012cadbSGreg Clayton                 if (resolved_module_spec.GetFileSpec().Readable())
39039945dccSGreg Clayton                 {
391b5ad4ec7SGreg Clayton                     error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
3928012cadbSGreg Clayton                                                     resolved_module_spec.GetFileSpec().GetPath().c_str(),
39357abc5d6SGreg Clayton                                                     GetPluginName().GetCString(),
394e996fd30SGreg Clayton                                                     arch_names.GetString().c_str());
395e996fd30SGreg Clayton                 }
39639945dccSGreg Clayton                 else
39739945dccSGreg Clayton                 {
3988012cadbSGreg Clayton                     error.SetErrorStringWithFormat("'%s' is not readable", resolved_module_spec.GetFileSpec().GetPath().c_str());
39939945dccSGreg Clayton                 }
40039945dccSGreg Clayton             }
401e996fd30SGreg Clayton         }
402e996fd30SGreg Clayton     }
403e996fd30SGreg Clayton 
404e996fd30SGreg Clayton     return error;
405e996fd30SGreg Clayton }
406e996fd30SGreg Clayton 
407e996fd30SGreg Clayton Error
408fc995725SSteve Pucci PlatformLinux::GetFileWithUUID (const FileSpec &platform_file,
40928041352SGreg Clayton                                 const UUID *uuid_ptr, FileSpec &local_file)
410e996fd30SGreg Clayton {
41128041352SGreg Clayton     if (IsRemote())
41228041352SGreg Clayton     {
41328041352SGreg Clayton         if (m_remote_platform_sp)
414fc995725SSteve Pucci             return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file);
41528041352SGreg Clayton     }
41628041352SGreg Clayton 
417e996fd30SGreg Clayton     // Default to the local case
418e996fd30SGreg Clayton     local_file = platform_file;
419e996fd30SGreg Clayton     return Error();
420e996fd30SGreg Clayton }
421e996fd30SGreg Clayton 
422e996fd30SGreg Clayton 
423e996fd30SGreg Clayton //------------------------------------------------------------------
424e996fd30SGreg Clayton /// Default Constructor
425e996fd30SGreg Clayton //------------------------------------------------------------------
42628041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) :
427015d818bSTodd Fiala     PlatformPOSIX(is_host)  // This is the local host platform
428e996fd30SGreg Clayton {
429e996fd30SGreg Clayton }
430e996fd30SGreg Clayton 
431e996fd30SGreg Clayton //------------------------------------------------------------------
432e996fd30SGreg Clayton /// Destructor.
433e996fd30SGreg Clayton ///
434e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be
435e996fd30SGreg Clayton /// inherited from by the plug-in instance.
436e996fd30SGreg Clayton //------------------------------------------------------------------
437e996fd30SGreg Clayton PlatformLinux::~PlatformLinux()
438e996fd30SGreg Clayton {
439e996fd30SGreg Clayton }
440e996fd30SGreg Clayton 
441e996fd30SGreg Clayton bool
44213e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
443e996fd30SGreg Clayton {
44428041352SGreg Clayton     bool success = false;
44528041352SGreg Clayton     if (IsHost())
44628041352SGreg Clayton     {
44728041352SGreg Clayton         success = Platform::GetProcessInfo (pid, process_info);
44828041352SGreg Clayton     }
44928041352SGreg Clayton     else
45028041352SGreg Clayton     {
45128041352SGreg Clayton         if (m_remote_platform_sp)
45228041352SGreg Clayton             success = m_remote_platform_sp->GetProcessInfo (pid, process_info);
45328041352SGreg Clayton     }
45428041352SGreg Clayton     return success;
455e996fd30SGreg Clayton }
456e996fd30SGreg Clayton 
4578e6ec453SStephane Sezer uint32_t
4588e6ec453SStephane Sezer PlatformLinux::FindProcesses (const ProcessInstanceInfoMatch &match_info,
4598e6ec453SStephane Sezer                               ProcessInstanceInfoList &process_infos)
4608e6ec453SStephane Sezer {
4618e6ec453SStephane Sezer     uint32_t match_count = 0;
4628e6ec453SStephane Sezer     if (IsHost())
4638e6ec453SStephane Sezer     {
4648e6ec453SStephane Sezer         // Let the base class figure out the host details
4658e6ec453SStephane Sezer         match_count = Platform::FindProcesses (match_info, process_infos);
4668e6ec453SStephane Sezer     }
4678e6ec453SStephane Sezer     else
4688e6ec453SStephane Sezer     {
4698e6ec453SStephane Sezer         // If we are remote, we can only return results if we are connected
4708e6ec453SStephane Sezer         if (m_remote_platform_sp)
4718e6ec453SStephane Sezer             match_count = m_remote_platform_sp->FindProcesses (match_info, process_infos);
4728e6ec453SStephane Sezer     }
4738e6ec453SStephane Sezer     return match_count;
4748e6ec453SStephane Sezer }
4758e6ec453SStephane Sezer 
476e996fd30SGreg Clayton bool
477e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
478e996fd30SGreg Clayton {
479e49b8e06SRobert Flack     if (IsHost())
480e49b8e06SRobert Flack     {
481e49b8e06SRobert Flack         ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
482e49b8e06SRobert Flack         if (hostArch.GetTriple().isOSLinux())
483e49b8e06SRobert Flack         {
484e49b8e06SRobert Flack             if (idx == 0)
485e49b8e06SRobert Flack             {
486e49b8e06SRobert Flack                 arch = hostArch;
487e49b8e06SRobert Flack                 return arch.IsValid();
488e49b8e06SRobert Flack             }
489e49b8e06SRobert Flack             else if (idx == 1)
490e49b8e06SRobert Flack             {
491e49b8e06SRobert Flack                 // If the default host architecture is 64-bit, look for a 32-bit variant
492e49b8e06SRobert Flack                 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit())
493e49b8e06SRobert Flack                 {
494e49b8e06SRobert Flack                     arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
495e49b8e06SRobert Flack                     return arch.IsValid();
496e49b8e06SRobert Flack                 }
497e49b8e06SRobert Flack             }
498e49b8e06SRobert Flack         }
499e49b8e06SRobert Flack     }
500e49b8e06SRobert Flack     else
501e49b8e06SRobert Flack     {
502e49b8e06SRobert Flack         if (m_remote_platform_sp)
503e49b8e06SRobert Flack             return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
504*bb1e283cSTed Woodward 
505*bb1e283cSTed Woodward         llvm::Triple triple;
506*bb1e283cSTed Woodward         // Set the OS to linux
507*bb1e283cSTed Woodward         triple.setOS(llvm::Triple::Linux);
508*bb1e283cSTed Woodward         // Set the architecture
509*bb1e283cSTed Woodward         switch (idx)
510*bb1e283cSTed Woodward         {
511*bb1e283cSTed Woodward             case 0: triple.setArchName("x86_64"); break;
512*bb1e283cSTed Woodward             case 1: triple.setArchName("i386"); break;
513*bb1e283cSTed Woodward             case 2: triple.setArchName("arm"); break;
514*bb1e283cSTed Woodward             case 3: triple.setArchName("aarch64"); break;
515*bb1e283cSTed Woodward             case 4: triple.setArchName("mips64"); break;
516*bb1e283cSTed Woodward             case 5: triple.setArchName("hexagon"); break;
517*bb1e283cSTed Woodward             case 6: triple.setArchName("mips"); break;
518*bb1e283cSTed Woodward             default: return false;
519*bb1e283cSTed Woodward         }
520*bb1e283cSTed Woodward         // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the vendor by
521*bb1e283cSTed Woodward         // calling triple.SetVendorName("unknown") so that it is a "unspecified unknown".
522*bb1e283cSTed Woodward         // This means when someone calls triple.GetVendorName() it will return an empty string
523*bb1e283cSTed Woodward         // which indicates that the vendor can be set when two architectures are merged
524*bb1e283cSTed Woodward 
525*bb1e283cSTed Woodward         // Now set the triple into "arch" and return true
526*bb1e283cSTed Woodward         arch.SetTriple(triple);
527*bb1e283cSTed Woodward         return true;
528e49b8e06SRobert Flack     }
529e996fd30SGreg Clayton     return false;
530e996fd30SGreg Clayton }
531ecc11474SStephen Wilson 
532ecc11474SStephen Wilson void
533ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm)
534ecc11474SStephen Wilson {
5353be69dacSDaniel Malea     Platform::GetStatus(strm);
536ecc11474SStephen Wilson 
537b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX
538b743a803SStephane Sezer     // Display local kernel information only when we are running in host mode.
539b743a803SStephane Sezer     // Otherwise, we would end up printing non-Linux information (when running
540b743a803SStephane Sezer     // on Mac OS for example).
541b743a803SStephane Sezer     if (IsHost())
542b743a803SStephane Sezer     {
543b2f1fb29SVirgile Bello         struct utsname un;
544b2f1fb29SVirgile Bello 
5453be69dacSDaniel Malea         if (uname(&un))
5463be69dacSDaniel Malea             return;
5473be69dacSDaniel Malea 
5483be69dacSDaniel Malea         strm.Printf ("    Kernel: %s\n", un.sysname);
5493be69dacSDaniel Malea         strm.Printf ("   Release: %s\n", un.release);
5503be69dacSDaniel Malea         strm.Printf ("   Version: %s\n", un.version);
551b743a803SStephane Sezer     }
552b2f1fb29SVirgile Bello #endif
553ecc11474SStephen Wilson }
554ecc11474SStephen Wilson 
555ecc11474SStephen Wilson size_t
556ecc11474SStephen Wilson PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target,
557ecc11474SStephen Wilson                                                 BreakpointSite *bp_site)
558ecc11474SStephen Wilson {
559ecc11474SStephen Wilson     ArchSpec arch = target.GetArchitecture();
56028041352SGreg Clayton     const uint8_t *trap_opcode = NULL;
56128041352SGreg Clayton     size_t trap_opcode_size = 0;
562ecc11474SStephen Wilson 
563906e9acfSGreg Clayton     switch (arch.GetMachine())
564ecc11474SStephen Wilson     {
565ecc11474SStephen Wilson     default:
566ecc11474SStephen Wilson         assert(false && "CPU type not supported!");
567ecc11474SStephen Wilson         break;
568ecc11474SStephen Wilson 
5692afc5966STodd Fiala     case llvm::Triple::aarch64:
5702afc5966STodd Fiala         {
5712afc5966STodd Fiala             static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 };
5722afc5966STodd Fiala             trap_opcode = g_aarch64_opcode;
5732afc5966STodd Fiala             trap_opcode_size = sizeof(g_aarch64_opcode);
5742afc5966STodd Fiala         }
5752afc5966STodd Fiala         break;
576906e9acfSGreg Clayton     case llvm::Triple::x86:
577906e9acfSGreg Clayton     case llvm::Triple::x86_64:
57828041352SGreg Clayton         {
57928041352SGreg Clayton             static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
58028041352SGreg Clayton             trap_opcode = g_i386_breakpoint_opcode;
58128041352SGreg Clayton             trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
58228041352SGreg Clayton         }
583ecc11474SStephen Wilson         break;
584906e9acfSGreg Clayton     case llvm::Triple::hexagon:
5858006d319SDeepak Panickal         {
5868006d319SDeepak Panickal             static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 };
5878006d319SDeepak Panickal             trap_opcode = g_hex_opcode;
5888006d319SDeepak Panickal             trap_opcode_size = sizeof(g_hex_opcode);
5898006d319SDeepak Panickal         }
5908006d319SDeepak Panickal         break;
5912586e94bSJason Molenda     case llvm::Triple::arm:
5922586e94bSJason Molenda         {
5932586e94bSJason Molenda             // The ARM reference recommends the use of 0xe7fddefe and 0xdefe
5942586e94bSJason Molenda             // but the linux kernel does otherwise.
5952586e94bSJason Molenda             static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 };
5962586e94bSJason Molenda             static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde };
5972586e94bSJason Molenda 
5982586e94bSJason Molenda             lldb::BreakpointLocationSP bp_loc_sp (bp_site->GetOwnerAtIndex (0));
5992586e94bSJason Molenda             AddressClass addr_class = eAddressClassUnknown;
6002586e94bSJason Molenda 
6012586e94bSJason Molenda             if (bp_loc_sp)
6022586e94bSJason Molenda                 addr_class = bp_loc_sp->GetAddress ().GetAddressClass ();
6032586e94bSJason Molenda 
6042586e94bSJason Molenda             if (addr_class == eAddressClassCodeAlternateISA
60563c8be95STamas Berghammer                 || (addr_class == eAddressClassUnknown && (bp_site->GetLoadAddress() & 1)))
6062586e94bSJason Molenda             {
6072586e94bSJason Molenda                 trap_opcode = g_thumb_breakpoint_opcode;
6082586e94bSJason Molenda                 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
6092586e94bSJason Molenda             }
6102586e94bSJason Molenda             else
6112586e94bSJason Molenda             {
6122586e94bSJason Molenda                 trap_opcode = g_arm_breakpoint_opcode;
6132586e94bSJason Molenda                 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
6142586e94bSJason Molenda             }
6152586e94bSJason Molenda         }
6162586e94bSJason Molenda         break;
617c9335a3fSMohit K. Bhakkad     case llvm::Triple::mips64:
618c9335a3fSMohit K. Bhakkad         {
619c9335a3fSMohit K. Bhakkad             static const uint8_t g_hex_opcode[] = { 0x00, 0x00, 0x00, 0x0d };
620c9335a3fSMohit K. Bhakkad             trap_opcode = g_hex_opcode;
621c9335a3fSMohit K. Bhakkad             trap_opcode_size = sizeof(g_hex_opcode);
622c9335a3fSMohit K. Bhakkad         }
623c9335a3fSMohit K. Bhakkad         break;
6242c2acf96SMohit K. Bhakkad     case llvm::Triple::mips64el:
6252c2acf96SMohit K. Bhakkad         {
6262c2acf96SMohit K. Bhakkad             static const uint8_t g_hex_opcode[] = { 0x0d, 0x00, 0x00, 0x00 };
6272c2acf96SMohit K. Bhakkad             trap_opcode = g_hex_opcode;
6282c2acf96SMohit K. Bhakkad             trap_opcode_size = sizeof(g_hex_opcode);
6292c2acf96SMohit K. Bhakkad         }
6302c2acf96SMohit K. Bhakkad         break;
631ecc11474SStephen Wilson     }
632ecc11474SStephen Wilson 
63328041352SGreg Clayton     if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
63428041352SGreg Clayton         return trap_opcode_size;
63528041352SGreg Clayton     return 0;
63628041352SGreg Clayton }
63728041352SGreg Clayton 
638348fb385STodd Fiala int32_t
639348fb385STodd Fiala PlatformLinux::GetResumeCountForLaunchInfo (ProcessLaunchInfo &launch_info)
64028041352SGreg Clayton {
641348fb385STodd Fiala     int32_t resume_count = 0;
64228041352SGreg Clayton 
643348fb385STodd Fiala     // Always resume past the initial stop when we use eLaunchFlagDebug
644348fb385STodd Fiala     if (launch_info.GetFlags ().Test (eLaunchFlagDebug))
64528041352SGreg Clayton     {
646348fb385STodd Fiala         // Resume past the stop for the final exec into the true inferior.
647348fb385STodd Fiala         ++resume_count;
648348fb385STodd Fiala     }
649015d818bSTodd Fiala 
650348fb385STodd Fiala     // If we're not launching a shell, we're done.
65110687b0eSZachary Turner     const FileSpec &shell = launch_info.GetShell();
65210687b0eSZachary Turner     if (!shell)
653348fb385STodd Fiala         return resume_count;
654348fb385STodd Fiala 
65510687b0eSZachary Turner     std::string shell_string = shell.GetPath();
656348fb385STodd Fiala     // We're in a shell, so for sure we have to resume past the shell exec.
657348fb385STodd Fiala     ++resume_count;
658348fb385STodd Fiala 
659348fb385STodd Fiala     // Figure out what shell we're planning on using.
66010687b0eSZachary Turner     const char *shell_name = strrchr (shell_string.c_str(), '/');
661348fb385STodd Fiala     if (shell_name == NULL)
66210687b0eSZachary Turner         shell_name = shell_string.c_str();
66328041352SGreg Clayton     else
664348fb385STodd Fiala         shell_name++;
665348fb385STodd Fiala 
666348fb385STodd Fiala     if (strcmp (shell_name, "csh") == 0
667348fb385STodd Fiala              || strcmp (shell_name, "tcsh") == 0
668348fb385STodd Fiala              || strcmp (shell_name, "zsh") == 0
669348fb385STodd Fiala              || strcmp (shell_name, "sh") == 0)
67028041352SGreg Clayton     {
671348fb385STodd Fiala         // These shells seem to re-exec themselves.  Add another resume.
672348fb385STodd Fiala         ++resume_count;
673ecc11474SStephen Wilson     }
67413e8e1c3SJohnny Chen 
675348fb385STodd Fiala     return resume_count;
676348fb385STodd Fiala }
677348fb385STodd Fiala 
678348fb385STodd Fiala bool
679348fb385STodd Fiala PlatformLinux::UseLlgsForLocalDebugging ()
680348fb385STodd Fiala {
681348fb385STodd Fiala     PlatformLinuxPropertiesSP properties_sp = GetGlobalProperties ();
682348fb385STodd Fiala     assert (properties_sp && "global properties shared pointer is null");
683348fb385STodd Fiala     return properties_sp ? properties_sp->GetUseLlgsForLocal () : false;
684348fb385STodd Fiala }
685348fb385STodd Fiala 
686015d818bSTodd Fiala bool
687015d818bSTodd Fiala PlatformLinux::CanDebugProcess ()
688015d818bSTodd Fiala {
689015d818bSTodd Fiala     if (IsHost ())
690348fb385STodd Fiala     {
691348fb385STodd Fiala         // The platform only does local debugging (i.e. uses llgs) when the setting indicates we do that.
692348fb385STodd Fiala         // Otherwise, we'll use ProcessLinux/ProcessPOSIX to handle with ProcessMonitor.
693348fb385STodd Fiala         return UseLlgsForLocalDebugging ();
694348fb385STodd Fiala     }
695348fb385STodd Fiala     else
696348fb385STodd Fiala     {
697015d818bSTodd Fiala         // If we're connected, we can debug.
698015d818bSTodd Fiala         return IsConnected ();
699015d818bSTodd Fiala     }
700348fb385STodd Fiala }
701015d818bSTodd Fiala 
702348fb385STodd Fiala // For local debugging, Linux will override the debug logic to use llgs-launch rather than
703348fb385STodd Fiala // lldb-launch, llgs-attach.  This differs from current lldb-launch, debugserver-attach
704348fb385STodd Fiala // approach on MacOSX.
70513e8e1c3SJohnny Chen lldb::ProcessSP
706348fb385STodd Fiala PlatformLinux::DebugProcess (ProcessLaunchInfo &launch_info,
70713e8e1c3SJohnny Chen                              Debugger &debugger,
708348fb385STodd Fiala                              Target *target,       // Can be NULL, if NULL create a new target, else use existing one
70913e8e1c3SJohnny Chen                              Error &error)
71013e8e1c3SJohnny Chen {
711db264a6dSTamas Berghammer     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
712348fb385STodd Fiala     if (log)
713348fb385STodd Fiala         log->Printf ("PlatformLinux::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target));
71428041352SGreg Clayton 
715348fb385STodd Fiala     // If we're a remote host, use standard behavior from parent class.
716348fb385STodd Fiala     if (!IsHost ())
7178012cadbSGreg Clayton         return PlatformPOSIX::DebugProcess (launch_info, debugger, target, error);
718348fb385STodd Fiala 
719348fb385STodd Fiala     //
720348fb385STodd Fiala     // For local debugging, we'll insist on having ProcessGDBRemote create the process.
721348fb385STodd Fiala     //
722348fb385STodd Fiala 
723348fb385STodd Fiala     ProcessSP process_sp;
724348fb385STodd Fiala 
725348fb385STodd Fiala     // Ensure we're using llgs for local debugging.
726348fb385STodd Fiala     if (!UseLlgsForLocalDebugging ())
727348fb385STodd Fiala     {
728348fb385STodd Fiala         assert (false && "we're trying to debug a local process but platform.plugin.linux.use-llgs-for-local is false, should never get here");
729348fb385STodd Fiala         error.SetErrorString ("attempted to start gdb-remote-based debugging for local process but platform.plugin.linux.use-llgs-for-local is false");
730348fb385STodd Fiala         return process_sp;
731348fb385STodd Fiala     }
732348fb385STodd Fiala 
733348fb385STodd Fiala     // Make sure we stop at the entry point
734348fb385STodd Fiala     launch_info.GetFlags ().Set (eLaunchFlagDebug);
735348fb385STodd Fiala 
736348fb385STodd Fiala     // We always launch the process we are going to debug in a separate process
737348fb385STodd Fiala     // group, since then we can handle ^C interrupts ourselves w/o having to worry
738348fb385STodd Fiala     // about the target getting them as well.
739348fb385STodd Fiala     launch_info.SetLaunchInSeparateProcessGroup(true);
740348fb385STodd Fiala 
741348fb385STodd Fiala     // Ensure we have a target.
742348fb385STodd Fiala     if (target == nullptr)
743348fb385STodd Fiala     {
744348fb385STodd Fiala         if (log)
745348fb385STodd Fiala             log->Printf ("PlatformLinux::%s creating new target", __FUNCTION__);
746348fb385STodd Fiala 
747348fb385STodd Fiala         TargetSP new_target_sp;
74828041352SGreg Clayton         error = debugger.GetTargetList().CreateTarget (debugger,
749348fb385STodd Fiala                                                        nullptr,
750348fb385STodd Fiala                                                        nullptr,
75128041352SGreg Clayton                                                        false,
752348fb385STodd Fiala                                                        nullptr,
75328041352SGreg Clayton                                                        new_target_sp);
754348fb385STodd Fiala         if (error.Fail ())
755348fb385STodd Fiala         {
756348fb385STodd Fiala             if (log)
757348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s failed to create new target: %s", __FUNCTION__, error.AsCString ());
758348fb385STodd Fiala             return process_sp;
759348fb385STodd Fiala         }
760348fb385STodd Fiala 
76128041352SGreg Clayton         target = new_target_sp.get();
762348fb385STodd Fiala         if (!target)
763348fb385STodd Fiala         {
764348fb385STodd Fiala             error.SetErrorString ("CreateTarget() returned nullptr");
765348fb385STodd Fiala             if (log)
766348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ());
767348fb385STodd Fiala             return process_sp;
768348fb385STodd Fiala         }
76928041352SGreg Clayton     }
77028041352SGreg Clayton     else
77128041352SGreg Clayton     {
772348fb385STodd Fiala         if (log)
773348fb385STodd Fiala             log->Printf ("PlatformLinux::%s using provided target", __FUNCTION__);
774348fb385STodd Fiala     }
775348fb385STodd Fiala 
776348fb385STodd Fiala     // Mark target as currently selected target.
77728041352SGreg Clayton     debugger.GetTargetList().SetSelectedTarget(target);
77828041352SGreg Clayton 
779348fb385STodd Fiala     // Now create the gdb-remote process.
780348fb385STodd Fiala     if (log)
781348fb385STodd Fiala         log->Printf ("PlatformLinux::%s having target create process with gdb-remote plugin", __FUNCTION__);
7828012cadbSGreg Clayton     process_sp = target->CreateProcess (launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr);
78328041352SGreg Clayton 
784348fb385STodd Fiala     if (!process_sp)
785348fb385STodd Fiala     {
786348fb385STodd Fiala         error.SetErrorString ("CreateProcess() failed for gdb-remote process");
787348fb385STodd Fiala         if (log)
788348fb385STodd Fiala             log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ());
789348fb385STodd Fiala         return process_sp;
790348fb385STodd Fiala     }
791348fb385STodd Fiala     else
792348fb385STodd Fiala     {
793348fb385STodd Fiala         if (log)
794348fb385STodd Fiala             log->Printf ("PlatformLinux::%s successfully created process", __FUNCTION__);
795348fb385STodd Fiala     }
796348fb385STodd Fiala 
797348fb385STodd Fiala     // Set the unix signals properly.
798348fb385STodd Fiala     process_sp->SetUnixSignals (Host::GetUnixSignals ());
799348fb385STodd Fiala 
800348fb385STodd Fiala     // Adjust launch for a hijacker.
801348fb385STodd Fiala     ListenerSP listener_sp;
802348fb385STodd Fiala     if (!launch_info.GetHijackListener ())
803348fb385STodd Fiala     {
804348fb385STodd Fiala         if (log)
805348fb385STodd Fiala             log->Printf ("PlatformLinux::%s setting up hijacker", __FUNCTION__);
806348fb385STodd Fiala 
807348fb385STodd Fiala         listener_sp.reset (new Listener("lldb.PlatformLinux.DebugProcess.hijack"));
808348fb385STodd Fiala         launch_info.SetHijackListener (listener_sp);
809348fb385STodd Fiala         process_sp->HijackProcessEvents (listener_sp.get ());
810348fb385STodd Fiala     }
811348fb385STodd Fiala 
812348fb385STodd Fiala     // Log file actions.
813348fb385STodd Fiala     if (log)
814348fb385STodd Fiala     {
815348fb385STodd Fiala         log->Printf ("PlatformLinux::%s launching process with the following file actions:", __FUNCTION__);
816348fb385STodd Fiala 
817348fb385STodd Fiala         StreamString stream;
818348fb385STodd Fiala         size_t i = 0;
819348fb385STodd Fiala         const FileAction *file_action;
820348fb385STodd Fiala         while ((file_action = launch_info.GetFileActionAtIndex (i++)) != nullptr)
821348fb385STodd Fiala         {
822348fb385STodd Fiala             file_action->Dump (stream);
823348fb385STodd Fiala             log->PutCString (stream.GetString().c_str ());
824348fb385STodd Fiala             stream.Clear();
825348fb385STodd Fiala         }
826348fb385STodd Fiala     }
827348fb385STodd Fiala 
828348fb385STodd Fiala     // Do the launch.
829348fb385STodd Fiala     error = process_sp->Launch(launch_info);
830348fb385STodd Fiala     if (error.Success ())
831348fb385STodd Fiala     {
832348fb385STodd Fiala         // Handle the hijacking of process events.
833348fb385STodd Fiala         if (listener_sp)
834348fb385STodd Fiala         {
835348fb385STodd Fiala             const StateType state = process_sp->WaitForProcessToStop (NULL, NULL, false, listener_sp.get());
836348fb385STodd Fiala 
837348fb385STodd Fiala             if (state == eStateStopped)
838348fb385STodd Fiala             {
839348fb385STodd Fiala                 if (log)
840348fb385STodd Fiala                     log->Printf ("PlatformLinux::%s pid %" PRIu64 " state %s\n",
841348fb385STodd Fiala                                  __FUNCTION__, process_sp->GetID (), StateAsCString (state));
842348fb385STodd Fiala             }
843348fb385STodd Fiala             else
844348fb385STodd Fiala             {
845348fb385STodd Fiala                 if (log)
846348fb385STodd Fiala                     log->Printf ("PlatformLinux::%s pid %" PRIu64 " state is not stopped - %s\n",
847348fb385STodd Fiala                                  __FUNCTION__, process_sp->GetID (), StateAsCString (state));
848348fb385STodd Fiala             }
849348fb385STodd Fiala         }
850348fb385STodd Fiala 
851348fb385STodd Fiala         // Hook up process PTY if we have one (which we should for local debugging with llgs).
852348fb385STodd Fiala         int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
853348fb385STodd Fiala         if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
854348fb385STodd Fiala         {
855348fb385STodd Fiala             process_sp->SetSTDIOFileDescriptor(pty_fd);
856348fb385STodd Fiala             if (log)
857348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s pid %" PRIu64 " hooked up STDIO pty to process", __FUNCTION__, process_sp->GetID ());
858348fb385STodd Fiala         }
859348fb385STodd Fiala         else
860348fb385STodd Fiala         {
861348fb385STodd Fiala             if (log)
862348fb385STodd Fiala                 log->Printf ("PlatformLinux::%s pid %" PRIu64 " not using process STDIO pty", __FUNCTION__, process_sp->GetID ());
86328041352SGreg Clayton         }
86428041352SGreg Clayton     }
86528041352SGreg Clayton     else
86628041352SGreg Clayton     {
867348fb385STodd Fiala         if (log)
868348fb385STodd Fiala             log->Printf ("PlatformLinux::%s process launch failed: %s", __FUNCTION__, error.AsCString ());
869348fb385STodd Fiala         // FIXME figure out appropriate cleanup here.  Do we delete the target? Do we delete the process?  Does our caller do that?
87028041352SGreg Clayton     }
871348fb385STodd Fiala 
87228041352SGreg Clayton     return process_sp;
87313e8e1c3SJohnny Chen }
8742094dbf4SJason Molenda 
8752094dbf4SJason Molenda void
8762094dbf4SJason Molenda PlatformLinux::CalculateTrapHandlerSymbolNames ()
8772094dbf4SJason Molenda {
8782094dbf4SJason Molenda     m_trap_handlers.push_back (ConstString ("_sigtramp"));
8792094dbf4SJason Molenda }
880af245d11STodd Fiala 
881af245d11STodd Fiala Error
882db264a6dSTamas Berghammer PlatformLinux::LaunchNativeProcess (ProcessLaunchInfo &launch_info,
883db264a6dSTamas Berghammer                                     NativeProcessProtocol::NativeDelegate &native_delegate,
884af245d11STodd Fiala                                     NativeProcessProtocolSP &process_sp)
885af245d11STodd Fiala {
886c6ec76e3STamas Berghammer #if !defined(__linux__)
887c6ec76e3STamas Berghammer     return Error("Only implemented on Linux hosts");
888c6ec76e3STamas Berghammer #else
889af245d11STodd Fiala     if (!IsHost ())
890af245d11STodd Fiala         return Error("PlatformLinux::%s (): cannot launch a debug process when not the host", __FUNCTION__);
891af245d11STodd Fiala 
892af245d11STodd Fiala     // Retrieve the exe module.
893af245d11STodd Fiala     lldb::ModuleSP exe_module_sp;
8946edef204SOleksiy Vyalov     ModuleSpec exe_module_spec(launch_info.GetExecutableFile(), launch_info.GetArchitecture());
895af245d11STodd Fiala 
896af245d11STodd Fiala     Error error = ResolveExecutable (
8976edef204SOleksiy Vyalov         exe_module_spec,
898af245d11STodd Fiala         exe_module_sp,
899af245d11STodd Fiala         NULL);
900af245d11STodd Fiala 
901af245d11STodd Fiala     if (!error.Success ())
902af245d11STodd Fiala         return error;
903af245d11STodd Fiala 
904af245d11STodd Fiala     if (!exe_module_sp)
905af245d11STodd Fiala         return Error("exe_module_sp could not be resolved for %s", launch_info.GetExecutableFile ().GetPath ().c_str ());
906af245d11STodd Fiala 
907af245d11STodd Fiala     // Launch it for debugging
90847b11c61STamas Berghammer     error = process_linux::NativeProcessLinux::LaunchProcess (
909af245d11STodd Fiala         exe_module_sp.get (),
910af245d11STodd Fiala         launch_info,
911af245d11STodd Fiala         native_delegate,
912af245d11STodd Fiala         process_sp);
913af245d11STodd Fiala 
914af245d11STodd Fiala     return error;
915c6ec76e3STamas Berghammer #endif
916af245d11STodd Fiala }
917af245d11STodd Fiala 
918af245d11STodd Fiala Error
919af245d11STodd Fiala PlatformLinux::AttachNativeProcess (lldb::pid_t pid,
920db264a6dSTamas Berghammer                                     NativeProcessProtocol::NativeDelegate &native_delegate,
921af245d11STodd Fiala                                     NativeProcessProtocolSP &process_sp)
922af245d11STodd Fiala {
923c6ec76e3STamas Berghammer #if !defined(__linux__)
924c6ec76e3STamas Berghammer     return Error("Only implemented on Linux hosts");
925c6ec76e3STamas Berghammer #else
926af245d11STodd Fiala     if (!IsHost ())
927af245d11STodd Fiala         return Error("PlatformLinux::%s (): cannot attach to a debug process when not the host", __FUNCTION__);
928af245d11STodd Fiala 
929af245d11STodd Fiala     // Launch it for debugging
93047b11c61STamas Berghammer     return process_linux::NativeProcessLinux::AttachToProcess (pid, native_delegate, process_sp);
931c6ec76e3STamas Berghammer #endif
932af245d11STodd Fiala }
93396ad3de5SRobert Flack 
93496ad3de5SRobert Flack uint64_t
93596ad3de5SRobert Flack PlatformLinux::ConvertMmapFlagsToPlatform(unsigned flags)
93696ad3de5SRobert Flack {
93796ad3de5SRobert Flack     uint64_t flags_platform = 0;
93896ad3de5SRobert Flack     if (flags & eMmapFlagsPrivate)
93996ad3de5SRobert Flack         flags_platform |= MAP_PRIVATE;
94096ad3de5SRobert Flack     if (flags & eMmapFlagsAnon)
94196ad3de5SRobert Flack         flags_platform |= MAP_ANON;
94296ad3de5SRobert Flack     return flags_platform;
94396ad3de5SRobert Flack }
944