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
24e996fd30SGreg Clayton #include "lldb/Core/Error.h"
2528041352SGreg Clayton #include "lldb/Core/Debugger.h"
26*015d818bSTodd Fiala #include "lldb/Core/Log.h"
27e996fd30SGreg Clayton #include "lldb/Core/Module.h"
28e996fd30SGreg Clayton #include "lldb/Core/ModuleList.h"
291f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h"
30ecc11474SStephen Wilson #include "lldb/Core/PluginManager.h"
31e996fd30SGreg Clayton #include "lldb/Core/StreamString.h"
32e996fd30SGreg Clayton #include "lldb/Host/FileSpec.h"
33e996fd30SGreg Clayton #include "lldb/Host/Host.h"
34ecc11474SStephen Wilson #include "lldb/Target/Target.h"
35e996fd30SGreg Clayton #include "lldb/Target/Process.h"
36e996fd30SGreg Clayton 
37af245d11STodd Fiala #if defined(__linux__)
38af245d11STodd Fiala #include "../../Process/Linux/NativeProcessLinux.h"
39af245d11STodd Fiala #endif
40af245d11STodd Fiala 
41e996fd30SGreg Clayton using namespace lldb;
42e996fd30SGreg Clayton using namespace lldb_private;
43e996fd30SGreg Clayton 
4428041352SGreg Clayton static uint32_t g_initialize_count = 0;
4528041352SGreg Clayton 
46ecc11474SStephen Wilson Platform *
47b3a40ba8SGreg Clayton PlatformLinux::CreateInstance (bool force, const ArchSpec *arch)
48ecc11474SStephen Wilson {
49*015d818bSTodd Fiala     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
50*015d818bSTodd Fiala     if (log)
51*015d818bSTodd Fiala     {
52*015d818bSTodd Fiala         const char *arch_name;
53*015d818bSTodd Fiala         if (arch && arch->GetArchitectureName ())
54*015d818bSTodd Fiala             arch_name = arch->GetArchitectureName ();
55*015d818bSTodd Fiala         else
56*015d818bSTodd Fiala             arch_name = "<null>";
57*015d818bSTodd Fiala 
58*015d818bSTodd Fiala         const char *triple_cstr = arch ? arch->GetTriple ().getTriple ().c_str() : "<null>";
59*015d818bSTodd Fiala 
60*015d818bSTodd Fiala         log->Printf ("PlatformLinux::%s(force=%s, arch={%s,%s})", __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
61*015d818bSTodd Fiala     }
62*015d818bSTodd Fiala 
63b3a40ba8SGreg Clayton     bool create = force;
64b3a40ba8SGreg Clayton     if (create == false && arch && arch->IsValid())
65b3a40ba8SGreg Clayton     {
66b3a40ba8SGreg Clayton         const llvm::Triple &triple = arch->GetTriple();
6770512317SGreg Clayton         switch (triple.getVendor())
6870512317SGreg Clayton         {
6970512317SGreg Clayton             case llvm::Triple::PC:
70b3a40ba8SGreg Clayton                 create = true;
7170512317SGreg Clayton                 break;
7270512317SGreg Clayton 
73dbc6c0bbSGreg Clayton #if defined(__linux__)
74dbc6c0bbSGreg Clayton             // Only accept "unknown" for the vendor if the host is linux and
756a7f3338SBruce Mitchener             // it "unknown" wasn't specified (it was just returned because it
76dbc6c0bbSGreg Clayton             // was NOT specified_
77*015d818bSTodd Fiala             case llvm::Triple::VendorType::UnknownVendor:
7870512317SGreg Clayton                 create = !arch->TripleVendorWasSpecified();
7970512317SGreg Clayton                 break;
80dbc6c0bbSGreg Clayton #endif
8170512317SGreg Clayton             default:
8270512317SGreg Clayton                 break;
8370512317SGreg Clayton         }
8470512317SGreg Clayton 
8570512317SGreg Clayton         if (create)
8670512317SGreg Clayton         {
8770512317SGreg Clayton             switch (triple.getOS())
8870512317SGreg Clayton             {
8970512317SGreg Clayton                 case llvm::Triple::Linux:
9070512317SGreg Clayton                     break;
9170512317SGreg Clayton 
92dbc6c0bbSGreg Clayton #if defined(__linux__)
93dbc6c0bbSGreg Clayton                 // Only accept "unknown" for the OS if the host is linux and
946a7f3338SBruce Mitchener                 // it "unknown" wasn't specified (it was just returned because it
95dbc6c0bbSGreg Clayton                 // was NOT specified)
96*015d818bSTodd Fiala                 case llvm::Triple::OSType::UnknownOS:
9770512317SGreg Clayton                     create = !arch->TripleOSWasSpecified();
9870512317SGreg Clayton                     break;
99dbc6c0bbSGreg Clayton #endif
10070512317SGreg Clayton                 default:
10170512317SGreg Clayton                     create = false;
10270512317SGreg Clayton                     break;
10370512317SGreg Clayton             }
10470512317SGreg Clayton         }
105b3a40ba8SGreg Clayton     }
106*015d818bSTodd Fiala 
107b3a40ba8SGreg Clayton     if (create)
108*015d818bSTodd Fiala     {
109*015d818bSTodd Fiala         if (log)
110*015d818bSTodd Fiala             log->Printf ("PlatformLinux::%s() creating remote-linux platform", __FUNCTION__);
111a4756939STodd Fiala         return new PlatformLinux(false);
112*015d818bSTodd Fiala     }
113*015d818bSTodd Fiala 
114*015d818bSTodd Fiala     if (log)
115*015d818bSTodd Fiala         log->Printf ("PlatformLinux::%s() aborting creation of remote-linux platform", __FUNCTION__);
116*015d818bSTodd Fiala 
117b3a40ba8SGreg Clayton     return NULL;
118ecc11474SStephen Wilson }
119ecc11474SStephen Wilson 
120ecc11474SStephen Wilson 
12157abc5d6SGreg Clayton lldb_private::ConstString
12257abc5d6SGreg Clayton PlatformLinux::GetPluginNameStatic (bool is_host)
123ecc11474SStephen Wilson {
12428041352SGreg Clayton     if (is_host)
12557abc5d6SGreg Clayton     {
12657abc5d6SGreg Clayton         static ConstString g_host_name(Platform::GetHostPlatformName ());
12757abc5d6SGreg Clayton         return g_host_name;
12857abc5d6SGreg Clayton     }
12928041352SGreg Clayton     else
13057abc5d6SGreg Clayton     {
13157abc5d6SGreg Clayton         static ConstString g_remote_name("remote-linux");
13257abc5d6SGreg Clayton         return g_remote_name;
13357abc5d6SGreg Clayton     }
13428041352SGreg Clayton }
13528041352SGreg Clayton 
13628041352SGreg Clayton const char *
13728041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic (bool is_host)
13828041352SGreg Clayton {
13928041352SGreg Clayton     if (is_host)
14028041352SGreg Clayton         return "Local Linux user platform plug-in.";
14128041352SGreg Clayton     else
14228041352SGreg Clayton         return "Remote Linux user platform plug-in.";
143ecc11474SStephen Wilson }
144ecc11474SStephen Wilson 
14557abc5d6SGreg Clayton lldb_private::ConstString
14657abc5d6SGreg Clayton PlatformLinux::GetPluginName()
14757abc5d6SGreg Clayton {
14857abc5d6SGreg Clayton     return GetPluginNameStatic(IsHost());
14957abc5d6SGreg Clayton }
15057abc5d6SGreg Clayton 
151e996fd30SGreg Clayton void
152e996fd30SGreg Clayton PlatformLinux::Initialize ()
153e996fd30SGreg Clayton {
15428041352SGreg Clayton     if (g_initialize_count++ == 0)
155ecc11474SStephen Wilson     {
15628041352SGreg Clayton #if defined(__linux__)
15728041352SGreg Clayton         PlatformSP default_platform_sp (new PlatformLinux(true));
15828041352SGreg Clayton         default_platform_sp->SetSystemArchitecture (Host::GetArchitecture());
159e996fd30SGreg Clayton         Platform::SetDefaultPlatform (default_platform_sp);
16028041352SGreg Clayton #endif
16157abc5d6SGreg Clayton         PluginManager::RegisterPlugin(PlatformLinux::GetPluginNameStatic(false),
16228041352SGreg Clayton                                       PlatformLinux::GetPluginDescriptionStatic(false),
16328041352SGreg Clayton                                       PlatformLinux::CreateInstance);
164ecc11474SStephen Wilson     }
165e996fd30SGreg Clayton }
166e996fd30SGreg Clayton 
167e996fd30SGreg Clayton void
168e996fd30SGreg Clayton PlatformLinux::Terminate ()
169e996fd30SGreg Clayton {
17028041352SGreg Clayton     if (g_initialize_count > 0)
17128041352SGreg Clayton     {
17228041352SGreg Clayton         if (--g_initialize_count == 0)
17328041352SGreg Clayton         {
17428041352SGreg Clayton             PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance);
175e996fd30SGreg Clayton         }
17628041352SGreg Clayton     }
17728041352SGreg Clayton }
178e996fd30SGreg Clayton 
179e996fd30SGreg Clayton Error
180e996fd30SGreg Clayton PlatformLinux::ResolveExecutable (const FileSpec &exe_file,
181e996fd30SGreg Clayton                                   const ArchSpec &exe_arch,
182ea5e0cc3SGreg Clayton                                   lldb::ModuleSP &exe_module_sp,
183ea5e0cc3SGreg Clayton                                   const FileSpecList *module_search_paths_ptr)
184e996fd30SGreg Clayton {
185e996fd30SGreg Clayton     Error error;
186e996fd30SGreg Clayton     // Nothing special to do here, just use the actual file and architecture
187e996fd30SGreg Clayton 
18828041352SGreg Clayton     char exe_path[PATH_MAX];
189e996fd30SGreg Clayton     FileSpec resolved_exe_file (exe_file);
190e996fd30SGreg Clayton 
19128041352SGreg Clayton     if (IsHost())
19228041352SGreg Clayton     {
19328041352SGreg Clayton         // If we have "ls" as the exe_file, resolve the executable location based on
194e996fd30SGreg Clayton         // the current path variables
195e996fd30SGreg Clayton         if (!resolved_exe_file.Exists())
19628041352SGreg Clayton         {
19728041352SGreg Clayton             exe_file.GetPath(exe_path, sizeof(exe_path));
19828041352SGreg Clayton             resolved_exe_file.SetFile(exe_path, true);
19928041352SGreg Clayton         }
20028041352SGreg Clayton 
20128041352SGreg Clayton         if (!resolved_exe_file.Exists())
202e996fd30SGreg Clayton             resolved_exe_file.ResolveExecutableLocation ();
203e996fd30SGreg Clayton 
20428041352SGreg Clayton         if (resolved_exe_file.Exists())
20528041352SGreg Clayton             error.Clear();
20628041352SGreg Clayton         else
20728041352SGreg Clayton         {
20828041352SGreg Clayton             exe_file.GetPath(exe_path, sizeof(exe_path));
20928041352SGreg Clayton             error.SetErrorStringWithFormat("unable to find executable for '%s'", exe_path);
21028041352SGreg Clayton         }
21128041352SGreg Clayton     }
21228041352SGreg Clayton     else
21328041352SGreg Clayton     {
21428041352SGreg Clayton         if (m_remote_platform_sp)
21528041352SGreg Clayton         {
21628041352SGreg Clayton             error = m_remote_platform_sp->ResolveExecutable (exe_file,
21728041352SGreg Clayton                                                              exe_arch,
2180c90ef47SGreg Clayton                                                              exe_module_sp,
2190c90ef47SGreg Clayton                                                              NULL);
22028041352SGreg Clayton         }
22128041352SGreg Clayton         else
22228041352SGreg Clayton         {
22328041352SGreg Clayton             // We may connect to a process and use the provided executable (Don't use local $PATH).
224e996fd30SGreg Clayton 
225e996fd30SGreg Clayton             if (resolved_exe_file.Exists())
22628041352SGreg Clayton                 error.Clear();
22728041352SGreg Clayton             else
22828041352SGreg Clayton                 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path);
22928041352SGreg Clayton         }
23028041352SGreg Clayton     }
23128041352SGreg Clayton 
23228041352SGreg Clayton     if (error.Success())
233e996fd30SGreg Clayton     {
234ea5e0cc3SGreg Clayton         ModuleSpec module_spec (resolved_exe_file, exe_arch);
235e996fd30SGreg Clayton         if (exe_arch.IsValid())
236e996fd30SGreg Clayton         {
237ea5e0cc3SGreg Clayton             error = ModuleList::GetSharedModule (module_spec,
238e996fd30SGreg Clayton                                                  exe_module_sp,
239e996fd30SGreg Clayton                                                  NULL,
2400c90ef47SGreg Clayton                                                  NULL,
241e996fd30SGreg Clayton                                                  NULL);
2429f0013d8SMichael Sartain             if (error.Fail())
2439f0013d8SMichael Sartain             {
2449f0013d8SMichael Sartain                 // If we failed, it may be because the vendor and os aren't known. If that is the
2459f0013d8SMichael Sartain                 // case, try setting them to the host architecture and give it another try.
2469f0013d8SMichael Sartain                 llvm::Triple &module_triple = module_spec.GetArchitecture().GetTriple();
2479f0013d8SMichael Sartain                 bool is_vendor_specified = (module_triple.getVendor() != llvm::Triple::UnknownVendor);
2489f0013d8SMichael Sartain                 bool is_os_specified = (module_triple.getOS() != llvm::Triple::UnknownOS);
2499f0013d8SMichael Sartain                 if (!is_vendor_specified || !is_os_specified)
2509f0013d8SMichael Sartain                 {
2519f0013d8SMichael Sartain                     const llvm::Triple &host_triple = Host::GetArchitecture (Host::eSystemDefaultArchitecture).GetTriple();
2529f0013d8SMichael Sartain 
2539f0013d8SMichael Sartain                     if (!is_vendor_specified)
2549f0013d8SMichael Sartain                         module_triple.setVendorName (host_triple.getVendorName());
2559f0013d8SMichael Sartain                     if (!is_os_specified)
2569f0013d8SMichael Sartain                         module_triple.setOSName (host_triple.getOSName());
2579f0013d8SMichael Sartain 
2589f0013d8SMichael Sartain                     error = ModuleList::GetSharedModule (module_spec,
2599f0013d8SMichael Sartain                                                          exe_module_sp,
2609f0013d8SMichael Sartain                                                          NULL,
2619f0013d8SMichael Sartain                                                          NULL,
2629f0013d8SMichael Sartain                                                          NULL);
2639f0013d8SMichael Sartain                 }
2649f0013d8SMichael Sartain             }
265e996fd30SGreg Clayton 
266e635db49SSean Callanan             // TODO find out why exe_module_sp might be NULL
267e635db49SSean Callanan             if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL)
268e996fd30SGreg Clayton             {
269e996fd30SGreg Clayton                 exe_module_sp.reset();
270b5ad4ec7SGreg Clayton                 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s",
271b5ad4ec7SGreg Clayton                                                 exe_file.GetPath().c_str(),
272e996fd30SGreg Clayton                                                 exe_arch.GetArchitectureName());
273e996fd30SGreg Clayton             }
274e996fd30SGreg Clayton         }
275e996fd30SGreg Clayton         else
276e996fd30SGreg Clayton         {
277e996fd30SGreg Clayton             // No valid architecture was specified, ask the platform for
278e996fd30SGreg Clayton             // the architectures that we should be using (in the correct order)
279e996fd30SGreg Clayton             // and see if we can find a match that way
280e996fd30SGreg Clayton             StreamString arch_names;
281ea5e0cc3SGreg Clayton             for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, module_spec.GetArchitecture()); ++idx)
282e996fd30SGreg Clayton             {
283ea5e0cc3SGreg Clayton                 error = ModuleList::GetSharedModule (module_spec,
284e996fd30SGreg Clayton                                                      exe_module_sp,
285e996fd30SGreg Clayton                                                      NULL,
2860c90ef47SGreg Clayton                                                      NULL,
287e996fd30SGreg Clayton                                                      NULL);
288e996fd30SGreg Clayton                 // Did we find an executable using one of the
289e996fd30SGreg Clayton                 if (error.Success())
290e996fd30SGreg Clayton                 {
291e996fd30SGreg Clayton                     if (exe_module_sp && exe_module_sp->GetObjectFile())
292e996fd30SGreg Clayton                         break;
293e996fd30SGreg Clayton                     else
294e996fd30SGreg Clayton                         error.SetErrorToGenericError();
295e996fd30SGreg Clayton                 }
296e996fd30SGreg Clayton 
297e996fd30SGreg Clayton                 if (idx > 0)
298e996fd30SGreg Clayton                     arch_names.PutCString (", ");
299ea5e0cc3SGreg Clayton                 arch_names.PutCString (module_spec.GetArchitecture().GetArchitectureName());
300e996fd30SGreg Clayton             }
301e996fd30SGreg Clayton 
302e996fd30SGreg Clayton             if (error.Fail() || !exe_module_sp)
303e996fd30SGreg Clayton             {
304b5ad4ec7SGreg Clayton                 error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
305b5ad4ec7SGreg Clayton                                                 exe_file.GetPath().c_str(),
30657abc5d6SGreg Clayton                                                 GetPluginName().GetCString(),
307e996fd30SGreg Clayton                                                 arch_names.GetString().c_str());
308e996fd30SGreg Clayton             }
309e996fd30SGreg Clayton         }
310e996fd30SGreg Clayton     }
311e996fd30SGreg Clayton 
312e996fd30SGreg Clayton     return error;
313e996fd30SGreg Clayton }
314e996fd30SGreg Clayton 
315e996fd30SGreg Clayton Error
316fc995725SSteve Pucci PlatformLinux::GetFileWithUUID (const FileSpec &platform_file,
31728041352SGreg Clayton                                 const UUID *uuid_ptr, FileSpec &local_file)
318e996fd30SGreg Clayton {
31928041352SGreg Clayton     if (IsRemote())
32028041352SGreg Clayton     {
32128041352SGreg Clayton         if (m_remote_platform_sp)
322fc995725SSteve Pucci             return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file);
32328041352SGreg Clayton     }
32428041352SGreg Clayton 
325e996fd30SGreg Clayton     // Default to the local case
326e996fd30SGreg Clayton     local_file = platform_file;
327e996fd30SGreg Clayton     return Error();
328e996fd30SGreg Clayton }
329e996fd30SGreg Clayton 
330e996fd30SGreg Clayton 
331e996fd30SGreg Clayton //------------------------------------------------------------------
332e996fd30SGreg Clayton /// Default Constructor
333e996fd30SGreg Clayton //------------------------------------------------------------------
33428041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) :
335*015d818bSTodd Fiala     PlatformPOSIX(is_host)  // This is the local host platform
336e996fd30SGreg Clayton {
337e996fd30SGreg Clayton }
338e996fd30SGreg Clayton 
339e996fd30SGreg Clayton //------------------------------------------------------------------
340e996fd30SGreg Clayton /// Destructor.
341e996fd30SGreg Clayton ///
342e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be
343e996fd30SGreg Clayton /// inherited from by the plug-in instance.
344e996fd30SGreg Clayton //------------------------------------------------------------------
345e996fd30SGreg Clayton PlatformLinux::~PlatformLinux()
346e996fd30SGreg Clayton {
347e996fd30SGreg Clayton }
348e996fd30SGreg Clayton 
349e996fd30SGreg Clayton bool
35013e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
351e996fd30SGreg Clayton {
35228041352SGreg Clayton     bool success = false;
35328041352SGreg Clayton     if (IsHost())
35428041352SGreg Clayton     {
35528041352SGreg Clayton         success = Platform::GetProcessInfo (pid, process_info);
35628041352SGreg Clayton     }
35728041352SGreg Clayton     else
35828041352SGreg Clayton     {
35928041352SGreg Clayton         if (m_remote_platform_sp)
36028041352SGreg Clayton             success = m_remote_platform_sp->GetProcessInfo (pid, process_info);
36128041352SGreg Clayton     }
36228041352SGreg Clayton     return success;
363e996fd30SGreg Clayton }
364e996fd30SGreg Clayton 
365e996fd30SGreg Clayton bool
366e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
367e996fd30SGreg Clayton {
368e996fd30SGreg Clayton     if (idx == 0)
369e996fd30SGreg Clayton     {
370e996fd30SGreg Clayton         arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
371e996fd30SGreg Clayton         return arch.IsValid();
372e996fd30SGreg Clayton     }
373542e4075SGreg Clayton     else if (idx == 1)
374542e4075SGreg Clayton     {
375542e4075SGreg Clayton         // If the default host architecture is 64-bit, look for a 32-bit variant
376542e4075SGreg Clayton         ArchSpec hostArch
377542e4075SGreg Clayton                       = Host::GetArchitecture(Host::eSystemDefaultArchitecture);
378542e4075SGreg Clayton         if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit())
379542e4075SGreg Clayton         {
380542e4075SGreg Clayton             arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
381542e4075SGreg Clayton             return arch.IsValid();
382542e4075SGreg Clayton         }
383542e4075SGreg Clayton     }
384e996fd30SGreg Clayton     return false;
385e996fd30SGreg Clayton }
386ecc11474SStephen Wilson 
387ecc11474SStephen Wilson void
388ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm)
389ecc11474SStephen Wilson {
3903be69dacSDaniel Malea     Platform::GetStatus(strm);
391ecc11474SStephen Wilson 
392b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX
393b2f1fb29SVirgile Bello     struct utsname un;
394b2f1fb29SVirgile Bello 
3953be69dacSDaniel Malea     if (uname(&un))
3963be69dacSDaniel Malea         return;
3973be69dacSDaniel Malea 
3983be69dacSDaniel Malea     strm.Printf ("    Kernel: %s\n", un.sysname);
3993be69dacSDaniel Malea     strm.Printf ("   Release: %s\n", un.release);
4003be69dacSDaniel Malea     strm.Printf ("   Version: %s\n", un.version);
401b2f1fb29SVirgile Bello #endif
402ecc11474SStephen Wilson }
403ecc11474SStephen Wilson 
404ecc11474SStephen Wilson size_t
405ecc11474SStephen Wilson PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target,
406ecc11474SStephen Wilson                                                 BreakpointSite *bp_site)
407ecc11474SStephen Wilson {
408ecc11474SStephen Wilson     ArchSpec arch = target.GetArchitecture();
40928041352SGreg Clayton     const uint8_t *trap_opcode = NULL;
41028041352SGreg Clayton     size_t trap_opcode_size = 0;
411ecc11474SStephen Wilson 
412906e9acfSGreg Clayton     switch (arch.GetMachine())
413ecc11474SStephen Wilson     {
414ecc11474SStephen Wilson     default:
415ecc11474SStephen Wilson         assert(false && "CPU type not supported!");
416ecc11474SStephen Wilson         break;
417ecc11474SStephen Wilson 
418906e9acfSGreg Clayton     case llvm::Triple::x86:
419906e9acfSGreg Clayton     case llvm::Triple::x86_64:
42028041352SGreg Clayton         {
42128041352SGreg Clayton             static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
42228041352SGreg Clayton             trap_opcode = g_i386_breakpoint_opcode;
42328041352SGreg Clayton             trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
42428041352SGreg Clayton         }
425ecc11474SStephen Wilson         break;
426906e9acfSGreg Clayton     case llvm::Triple::hexagon:
4278006d319SDeepak Panickal         {
4288006d319SDeepak Panickal             static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 };
4298006d319SDeepak Panickal             trap_opcode = g_hex_opcode;
4308006d319SDeepak Panickal             trap_opcode_size = sizeof(g_hex_opcode);
4318006d319SDeepak Panickal         }
4328006d319SDeepak Panickal         break;
433ecc11474SStephen Wilson     }
434ecc11474SStephen Wilson 
43528041352SGreg Clayton     if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
43628041352SGreg Clayton         return trap_opcode_size;
43728041352SGreg Clayton     return 0;
43828041352SGreg Clayton }
43928041352SGreg Clayton 
44028041352SGreg Clayton Error
44128041352SGreg Clayton PlatformLinux::LaunchProcess (ProcessLaunchInfo &launch_info)
44228041352SGreg Clayton {
443*015d818bSTodd Fiala     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
44428041352SGreg Clayton     Error error;
44528041352SGreg Clayton 
44628041352SGreg Clayton     if (IsHost())
44728041352SGreg Clayton     {
448*015d818bSTodd Fiala         if (log)
449*015d818bSTodd Fiala             log->Printf ("PlatformLinux::%s() launching process as host", __FUNCTION__);
450*015d818bSTodd Fiala 
45128041352SGreg Clayton         if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
45228041352SGreg Clayton         {
45328041352SGreg Clayton             const bool is_localhost = true;
454d1cf11a7SGreg Clayton             const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
455d1cf11a7SGreg Clayton             const bool first_arg_is_full_shell_command = false;
456d3990793SJim Ingham             uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info);
457d1cf11a7SGreg Clayton             if (!launch_info.ConvertArgumentsForLaunchingInShell (error,
458d1cf11a7SGreg Clayton                                                                   is_localhost,
459d1cf11a7SGreg Clayton                                                                   will_debug,
460df0ae22fSJim Ingham                                                                   first_arg_is_full_shell_command,
461df0ae22fSJim Ingham                                                                   num_resumes))
46228041352SGreg Clayton                 return error;
46328041352SGreg Clayton         }
46428041352SGreg Clayton         error = Platform::LaunchProcess (launch_info);
46528041352SGreg Clayton     }
46628041352SGreg Clayton     else
46728041352SGreg Clayton     {
468*015d818bSTodd Fiala         if (m_remote_platform_sp)
469*015d818bSTodd Fiala         {
470*015d818bSTodd Fiala             if (log)
471*015d818bSTodd Fiala                 log->Printf ("PlatformLinux::%s() attempting to launch remote process", __FUNCTION__);
472*015d818bSTodd Fiala             error = m_remote_platform_sp->LaunchProcess (launch_info);
473*015d818bSTodd Fiala         }
474*015d818bSTodd Fiala         else
475*015d818bSTodd Fiala         {
476*015d818bSTodd Fiala             if (log)
477*015d818bSTodd Fiala                 log->Printf ("PlatformLinux::%s() attempted to launch process but is not the host and no remote platform set", __FUNCTION__);
47828041352SGreg Clayton             error.SetErrorString ("the platform is not currently connected");
47928041352SGreg Clayton         }
480*015d818bSTodd Fiala     }
48128041352SGreg Clayton     return error;
482ecc11474SStephen Wilson }
48313e8e1c3SJohnny Chen 
484*015d818bSTodd Fiala // Linux processes can not be launched by spawning and attaching.
485*015d818bSTodd Fiala bool
486*015d818bSTodd Fiala PlatformLinux::CanDebugProcess ()
487*015d818bSTodd Fiala {
488*015d818bSTodd Fiala     // If we're the host, launch via normal host setup.
489*015d818bSTodd Fiala     if (IsHost ())
490*015d818bSTodd Fiala         return false;
491*015d818bSTodd Fiala 
492*015d818bSTodd Fiala     // If we're connected, we can debug.
493*015d818bSTodd Fiala     return IsConnected ();
494*015d818bSTodd Fiala }
495*015d818bSTodd Fiala 
49613e8e1c3SJohnny Chen lldb::ProcessSP
497fb2b629dSPeter Collingbourne PlatformLinux::Attach(ProcessAttachInfo &attach_info,
49813e8e1c3SJohnny Chen                       Debugger &debugger,
49913e8e1c3SJohnny Chen                       Target *target,
50013e8e1c3SJohnny Chen                       Listener &listener,
50113e8e1c3SJohnny Chen                       Error &error)
50213e8e1c3SJohnny Chen {
50328041352SGreg Clayton     lldb::ProcessSP process_sp;
50428041352SGreg Clayton     if (IsHost())
50528041352SGreg Clayton     {
50628041352SGreg Clayton         if (target == NULL)
50728041352SGreg Clayton         {
50828041352SGreg Clayton             TargetSP new_target_sp;
50928041352SGreg Clayton             ArchSpec emptyArchSpec;
51028041352SGreg Clayton 
51128041352SGreg Clayton             error = debugger.GetTargetList().CreateTarget (debugger,
512a0ca6601SGreg Clayton                                                            NULL,
51328041352SGreg Clayton                                                            emptyArchSpec,
51428041352SGreg Clayton                                                            false,
51528041352SGreg Clayton                                                            m_remote_platform_sp,
51628041352SGreg Clayton                                                            new_target_sp);
51728041352SGreg Clayton             target = new_target_sp.get();
51828041352SGreg Clayton         }
51928041352SGreg Clayton         else
52028041352SGreg Clayton             error.Clear();
52128041352SGreg Clayton 
52228041352SGreg Clayton         if (target && error.Success())
52328041352SGreg Clayton         {
52428041352SGreg Clayton             debugger.GetTargetList().SetSelectedTarget(target);
52528041352SGreg Clayton 
5260c90ef47SGreg Clayton             process_sp = target->CreateProcess (listener,
5270c90ef47SGreg Clayton                                                 attach_info.GetProcessPluginName(),
5280c90ef47SGreg Clayton                                                 NULL);
52928041352SGreg Clayton 
53028041352SGreg Clayton             if (process_sp)
53128041352SGreg Clayton                 error = process_sp->Attach (attach_info);
53228041352SGreg Clayton         }
53328041352SGreg Clayton     }
53428041352SGreg Clayton     else
53528041352SGreg Clayton     {
53628041352SGreg Clayton         if (m_remote_platform_sp)
53728041352SGreg Clayton             process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, listener, error);
53828041352SGreg Clayton         else
53928041352SGreg Clayton             error.SetErrorString ("the platform is not currently connected");
54028041352SGreg Clayton     }
54128041352SGreg Clayton     return process_sp;
54213e8e1c3SJohnny Chen }
5432094dbf4SJason Molenda 
5442094dbf4SJason Molenda void
5452094dbf4SJason Molenda PlatformLinux::CalculateTrapHandlerSymbolNames ()
5462094dbf4SJason Molenda {
5472094dbf4SJason Molenda     m_trap_handlers.push_back (ConstString ("_sigtramp"));
5482094dbf4SJason Molenda }
549af245d11STodd Fiala 
550af245d11STodd Fiala Error
551af245d11STodd Fiala PlatformLinux::LaunchNativeProcess (
552af245d11STodd Fiala     ProcessLaunchInfo &launch_info,
553af245d11STodd Fiala     lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
554af245d11STodd Fiala     NativeProcessProtocolSP &process_sp)
555af245d11STodd Fiala {
556af245d11STodd Fiala #if !defined(__linux__)
557af245d11STodd Fiala     return Error("only implemented on Linux hosts");
558af245d11STodd Fiala #else
559af245d11STodd Fiala     if (!IsHost ())
560af245d11STodd Fiala         return Error("PlatformLinux::%s (): cannot launch a debug process when not the host", __FUNCTION__);
561af245d11STodd Fiala 
562af245d11STodd Fiala     // Retrieve the exe module.
563af245d11STodd Fiala     lldb::ModuleSP exe_module_sp;
564af245d11STodd Fiala 
565af245d11STodd Fiala     Error error = ResolveExecutable (
566af245d11STodd Fiala         launch_info.GetExecutableFile (),
567af245d11STodd Fiala         launch_info.GetArchitecture (),
568af245d11STodd Fiala         exe_module_sp,
569af245d11STodd Fiala         NULL);
570af245d11STodd Fiala 
571af245d11STodd Fiala     if (!error.Success ())
572af245d11STodd Fiala         return error;
573af245d11STodd Fiala 
574af245d11STodd Fiala     if (!exe_module_sp)
575af245d11STodd Fiala         return Error("exe_module_sp could not be resolved for %s", launch_info.GetExecutableFile ().GetPath ().c_str ());
576af245d11STodd Fiala 
577af245d11STodd Fiala     // Launch it for debugging
578af245d11STodd Fiala     error = NativeProcessLinux::LaunchProcess (
579af245d11STodd Fiala         exe_module_sp.get (),
580af245d11STodd Fiala         launch_info,
581af245d11STodd Fiala         native_delegate,
582af245d11STodd Fiala         process_sp);
583af245d11STodd Fiala 
584af245d11STodd Fiala     return error;
585af245d11STodd Fiala #endif
586af245d11STodd Fiala }
587af245d11STodd Fiala 
588af245d11STodd Fiala Error
589af245d11STodd Fiala PlatformLinux::AttachNativeProcess (lldb::pid_t pid,
590af245d11STodd Fiala                                     lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
591af245d11STodd Fiala                                     NativeProcessProtocolSP &process_sp)
592af245d11STodd Fiala {
593af245d11STodd Fiala #if !defined(__linux__)
594af245d11STodd Fiala     return Error("only implemented on Linux hosts");
595af245d11STodd Fiala #else
596af245d11STodd Fiala     if (!IsHost ())
597af245d11STodd Fiala         return Error("PlatformLinux::%s (): cannot attach to a debug process when not the host", __FUNCTION__);
598af245d11STodd Fiala 
599af245d11STodd Fiala     // Launch it for debugging
600af245d11STodd Fiala     return NativeProcessLinux::AttachToProcess (pid, native_delegate, process_sp);
601af245d11STodd Fiala #endif
602af245d11STodd Fiala }
603