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"
26015d818bSTodd 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 {
49015d818bSTodd Fiala     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
50015d818bSTodd Fiala     if (log)
51015d818bSTodd Fiala     {
52015d818bSTodd Fiala         const char *arch_name;
53015d818bSTodd Fiala         if (arch && arch->GetArchitectureName ())
54015d818bSTodd Fiala             arch_name = arch->GetArchitectureName ();
55015d818bSTodd Fiala         else
56015d818bSTodd Fiala             arch_name = "<null>";
57015d818bSTodd Fiala 
58015d818bSTodd Fiala         const char *triple_cstr = arch ? arch->GetTriple ().getTriple ().c_str() : "<null>";
59015d818bSTodd Fiala 
60015d818bSTodd Fiala         log->Printf ("PlatformLinux::%s(force=%s, arch={%s,%s})", __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
61015d818bSTodd Fiala     }
62015d818bSTodd 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_
77015d818bSTodd 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)
96015d818bSTodd 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     }
106015d818bSTodd Fiala 
107b3a40ba8SGreg Clayton     if (create)
108015d818bSTodd Fiala     {
109015d818bSTodd Fiala         if (log)
110015d818bSTodd Fiala             log->Printf ("PlatformLinux::%s() creating remote-linux platform", __FUNCTION__);
111a4756939STodd Fiala         return new PlatformLinux(false);
112015d818bSTodd Fiala     }
113015d818bSTodd Fiala 
114015d818bSTodd Fiala     if (log)
115015d818bSTodd Fiala         log->Printf ("PlatformLinux::%s() aborting creation of remote-linux platform", __FUNCTION__);
116015d818bSTodd 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             {
304*39945dccSGreg Clayton                 if (exe_file.Readable())
305*39945dccSGreg Clayton                 {
306b5ad4ec7SGreg Clayton                     error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
307b5ad4ec7SGreg Clayton                                                     exe_file.GetPath().c_str(),
30857abc5d6SGreg Clayton                                                     GetPluginName().GetCString(),
309e996fd30SGreg Clayton                                                     arch_names.GetString().c_str());
310e996fd30SGreg Clayton                 }
311*39945dccSGreg Clayton                 else
312*39945dccSGreg Clayton                 {
313*39945dccSGreg Clayton                     error.SetErrorStringWithFormat("'%s' is not readable", exe_file.GetPath().c_str());
314*39945dccSGreg Clayton                 }
315*39945dccSGreg Clayton             }
316e996fd30SGreg Clayton         }
317e996fd30SGreg Clayton     }
318e996fd30SGreg Clayton 
319e996fd30SGreg Clayton     return error;
320e996fd30SGreg Clayton }
321e996fd30SGreg Clayton 
322e996fd30SGreg Clayton Error
323fc995725SSteve Pucci PlatformLinux::GetFileWithUUID (const FileSpec &platform_file,
32428041352SGreg Clayton                                 const UUID *uuid_ptr, FileSpec &local_file)
325e996fd30SGreg Clayton {
32628041352SGreg Clayton     if (IsRemote())
32728041352SGreg Clayton     {
32828041352SGreg Clayton         if (m_remote_platform_sp)
329fc995725SSteve Pucci             return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file);
33028041352SGreg Clayton     }
33128041352SGreg Clayton 
332e996fd30SGreg Clayton     // Default to the local case
333e996fd30SGreg Clayton     local_file = platform_file;
334e996fd30SGreg Clayton     return Error();
335e996fd30SGreg Clayton }
336e996fd30SGreg Clayton 
337e996fd30SGreg Clayton 
338e996fd30SGreg Clayton //------------------------------------------------------------------
339e996fd30SGreg Clayton /// Default Constructor
340e996fd30SGreg Clayton //------------------------------------------------------------------
34128041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) :
342015d818bSTodd Fiala     PlatformPOSIX(is_host)  // This is the local host platform
343e996fd30SGreg Clayton {
344e996fd30SGreg Clayton }
345e996fd30SGreg Clayton 
346e996fd30SGreg Clayton //------------------------------------------------------------------
347e996fd30SGreg Clayton /// Destructor.
348e996fd30SGreg Clayton ///
349e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be
350e996fd30SGreg Clayton /// inherited from by the plug-in instance.
351e996fd30SGreg Clayton //------------------------------------------------------------------
352e996fd30SGreg Clayton PlatformLinux::~PlatformLinux()
353e996fd30SGreg Clayton {
354e996fd30SGreg Clayton }
355e996fd30SGreg Clayton 
356e996fd30SGreg Clayton bool
35713e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
358e996fd30SGreg Clayton {
35928041352SGreg Clayton     bool success = false;
36028041352SGreg Clayton     if (IsHost())
36128041352SGreg Clayton     {
36228041352SGreg Clayton         success = Platform::GetProcessInfo (pid, process_info);
36328041352SGreg Clayton     }
36428041352SGreg Clayton     else
36528041352SGreg Clayton     {
36628041352SGreg Clayton         if (m_remote_platform_sp)
36728041352SGreg Clayton             success = m_remote_platform_sp->GetProcessInfo (pid, process_info);
36828041352SGreg Clayton     }
36928041352SGreg Clayton     return success;
370e996fd30SGreg Clayton }
371e996fd30SGreg Clayton 
372e996fd30SGreg Clayton bool
373e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
374e996fd30SGreg Clayton {
375e996fd30SGreg Clayton     if (idx == 0)
376e996fd30SGreg Clayton     {
377e996fd30SGreg Clayton         arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
378e996fd30SGreg Clayton         return arch.IsValid();
379e996fd30SGreg Clayton     }
380542e4075SGreg Clayton     else if (idx == 1)
381542e4075SGreg Clayton     {
382542e4075SGreg Clayton         // If the default host architecture is 64-bit, look for a 32-bit variant
383542e4075SGreg Clayton         ArchSpec hostArch
384542e4075SGreg Clayton                       = Host::GetArchitecture(Host::eSystemDefaultArchitecture);
385542e4075SGreg Clayton         if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit())
386542e4075SGreg Clayton         {
387542e4075SGreg Clayton             arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
388542e4075SGreg Clayton             return arch.IsValid();
389542e4075SGreg Clayton         }
390542e4075SGreg Clayton     }
391e996fd30SGreg Clayton     return false;
392e996fd30SGreg Clayton }
393ecc11474SStephen Wilson 
394ecc11474SStephen Wilson void
395ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm)
396ecc11474SStephen Wilson {
3973be69dacSDaniel Malea     Platform::GetStatus(strm);
398ecc11474SStephen Wilson 
399b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX
400b2f1fb29SVirgile Bello     struct utsname un;
401b2f1fb29SVirgile Bello 
4023be69dacSDaniel Malea     if (uname(&un))
4033be69dacSDaniel Malea         return;
4043be69dacSDaniel Malea 
4053be69dacSDaniel Malea     strm.Printf ("    Kernel: %s\n", un.sysname);
4063be69dacSDaniel Malea     strm.Printf ("   Release: %s\n", un.release);
4073be69dacSDaniel Malea     strm.Printf ("   Version: %s\n", un.version);
408b2f1fb29SVirgile Bello #endif
409ecc11474SStephen Wilson }
410ecc11474SStephen Wilson 
411ecc11474SStephen Wilson size_t
412ecc11474SStephen Wilson PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target,
413ecc11474SStephen Wilson                                                 BreakpointSite *bp_site)
414ecc11474SStephen Wilson {
415ecc11474SStephen Wilson     ArchSpec arch = target.GetArchitecture();
41628041352SGreg Clayton     const uint8_t *trap_opcode = NULL;
41728041352SGreg Clayton     size_t trap_opcode_size = 0;
418ecc11474SStephen Wilson 
419906e9acfSGreg Clayton     switch (arch.GetMachine())
420ecc11474SStephen Wilson     {
421ecc11474SStephen Wilson     default:
422ecc11474SStephen Wilson         assert(false && "CPU type not supported!");
423ecc11474SStephen Wilson         break;
424ecc11474SStephen Wilson 
425906e9acfSGreg Clayton     case llvm::Triple::x86:
426906e9acfSGreg Clayton     case llvm::Triple::x86_64:
42728041352SGreg Clayton         {
42828041352SGreg Clayton             static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
42928041352SGreg Clayton             trap_opcode = g_i386_breakpoint_opcode;
43028041352SGreg Clayton             trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
43128041352SGreg Clayton         }
432ecc11474SStephen Wilson         break;
433906e9acfSGreg Clayton     case llvm::Triple::hexagon:
4348006d319SDeepak Panickal         {
4358006d319SDeepak Panickal             static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 };
4368006d319SDeepak Panickal             trap_opcode = g_hex_opcode;
4378006d319SDeepak Panickal             trap_opcode_size = sizeof(g_hex_opcode);
4388006d319SDeepak Panickal         }
4398006d319SDeepak Panickal         break;
440ecc11474SStephen Wilson     }
441ecc11474SStephen Wilson 
44228041352SGreg Clayton     if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
44328041352SGreg Clayton         return trap_opcode_size;
44428041352SGreg Clayton     return 0;
44528041352SGreg Clayton }
44628041352SGreg Clayton 
44728041352SGreg Clayton Error
44828041352SGreg Clayton PlatformLinux::LaunchProcess (ProcessLaunchInfo &launch_info)
44928041352SGreg Clayton {
450015d818bSTodd Fiala     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
45128041352SGreg Clayton     Error error;
45228041352SGreg Clayton 
45328041352SGreg Clayton     if (IsHost())
45428041352SGreg Clayton     {
455015d818bSTodd Fiala         if (log)
456015d818bSTodd Fiala             log->Printf ("PlatformLinux::%s() launching process as host", __FUNCTION__);
457015d818bSTodd Fiala 
45828041352SGreg Clayton         if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
45928041352SGreg Clayton         {
46028041352SGreg Clayton             const bool is_localhost = true;
461d1cf11a7SGreg Clayton             const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
462d1cf11a7SGreg Clayton             const bool first_arg_is_full_shell_command = false;
463d3990793SJim Ingham             uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info);
464d1cf11a7SGreg Clayton             if (!launch_info.ConvertArgumentsForLaunchingInShell (error,
465d1cf11a7SGreg Clayton                                                                   is_localhost,
466d1cf11a7SGreg Clayton                                                                   will_debug,
467df0ae22fSJim Ingham                                                                   first_arg_is_full_shell_command,
468df0ae22fSJim Ingham                                                                   num_resumes))
46928041352SGreg Clayton                 return error;
47028041352SGreg Clayton         }
47128041352SGreg Clayton         error = Platform::LaunchProcess (launch_info);
47228041352SGreg Clayton     }
47328041352SGreg Clayton     else
47428041352SGreg Clayton     {
475015d818bSTodd Fiala         if (m_remote_platform_sp)
476015d818bSTodd Fiala         {
477015d818bSTodd Fiala             if (log)
478015d818bSTodd Fiala                 log->Printf ("PlatformLinux::%s() attempting to launch remote process", __FUNCTION__);
479015d818bSTodd Fiala             error = m_remote_platform_sp->LaunchProcess (launch_info);
480015d818bSTodd Fiala         }
481015d818bSTodd Fiala         else
482015d818bSTodd Fiala         {
483015d818bSTodd Fiala             if (log)
484015d818bSTodd Fiala                 log->Printf ("PlatformLinux::%s() attempted to launch process but is not the host and no remote platform set", __FUNCTION__);
48528041352SGreg Clayton             error.SetErrorString ("the platform is not currently connected");
48628041352SGreg Clayton         }
487015d818bSTodd Fiala     }
48828041352SGreg Clayton     return error;
489ecc11474SStephen Wilson }
49013e8e1c3SJohnny Chen 
491015d818bSTodd Fiala // Linux processes can not be launched by spawning and attaching.
492015d818bSTodd Fiala bool
493015d818bSTodd Fiala PlatformLinux::CanDebugProcess ()
494015d818bSTodd Fiala {
495015d818bSTodd Fiala     // If we're the host, launch via normal host setup.
496015d818bSTodd Fiala     if (IsHost ())
497015d818bSTodd Fiala         return false;
498015d818bSTodd Fiala 
499015d818bSTodd Fiala     // If we're connected, we can debug.
500015d818bSTodd Fiala     return IsConnected ();
501015d818bSTodd Fiala }
502015d818bSTodd Fiala 
50313e8e1c3SJohnny Chen lldb::ProcessSP
504fb2b629dSPeter Collingbourne PlatformLinux::Attach(ProcessAttachInfo &attach_info,
50513e8e1c3SJohnny Chen                       Debugger &debugger,
50613e8e1c3SJohnny Chen                       Target *target,
50713e8e1c3SJohnny Chen                       Listener &listener,
50813e8e1c3SJohnny Chen                       Error &error)
50913e8e1c3SJohnny Chen {
51028041352SGreg Clayton     lldb::ProcessSP process_sp;
51128041352SGreg Clayton     if (IsHost())
51228041352SGreg Clayton     {
51328041352SGreg Clayton         if (target == NULL)
51428041352SGreg Clayton         {
51528041352SGreg Clayton             TargetSP new_target_sp;
51628041352SGreg Clayton             ArchSpec emptyArchSpec;
51728041352SGreg Clayton 
51828041352SGreg Clayton             error = debugger.GetTargetList().CreateTarget (debugger,
519a0ca6601SGreg Clayton                                                            NULL,
52028041352SGreg Clayton                                                            emptyArchSpec,
52128041352SGreg Clayton                                                            false,
52228041352SGreg Clayton                                                            m_remote_platform_sp,
52328041352SGreg Clayton                                                            new_target_sp);
52428041352SGreg Clayton             target = new_target_sp.get();
52528041352SGreg Clayton         }
52628041352SGreg Clayton         else
52728041352SGreg Clayton             error.Clear();
52828041352SGreg Clayton 
52928041352SGreg Clayton         if (target && error.Success())
53028041352SGreg Clayton         {
53128041352SGreg Clayton             debugger.GetTargetList().SetSelectedTarget(target);
53228041352SGreg Clayton 
5330c90ef47SGreg Clayton             process_sp = target->CreateProcess (listener,
5340c90ef47SGreg Clayton                                                 attach_info.GetProcessPluginName(),
5350c90ef47SGreg Clayton                                                 NULL);
53628041352SGreg Clayton 
53728041352SGreg Clayton             if (process_sp)
53828041352SGreg Clayton                 error = process_sp->Attach (attach_info);
53928041352SGreg Clayton         }
54028041352SGreg Clayton     }
54128041352SGreg Clayton     else
54228041352SGreg Clayton     {
54328041352SGreg Clayton         if (m_remote_platform_sp)
54428041352SGreg Clayton             process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, listener, error);
54528041352SGreg Clayton         else
54628041352SGreg Clayton             error.SetErrorString ("the platform is not currently connected");
54728041352SGreg Clayton     }
54828041352SGreg Clayton     return process_sp;
54913e8e1c3SJohnny Chen }
5502094dbf4SJason Molenda 
5512094dbf4SJason Molenda void
5522094dbf4SJason Molenda PlatformLinux::CalculateTrapHandlerSymbolNames ()
5532094dbf4SJason Molenda {
5542094dbf4SJason Molenda     m_trap_handlers.push_back (ConstString ("_sigtramp"));
5552094dbf4SJason Molenda }
556af245d11STodd Fiala 
557af245d11STodd Fiala Error
558af245d11STodd Fiala PlatformLinux::LaunchNativeProcess (
559af245d11STodd Fiala     ProcessLaunchInfo &launch_info,
560af245d11STodd Fiala     lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
561af245d11STodd Fiala     NativeProcessProtocolSP &process_sp)
562af245d11STodd Fiala {
563af245d11STodd Fiala #if !defined(__linux__)
564af245d11STodd Fiala     return Error("only implemented on Linux hosts");
565af245d11STodd Fiala #else
566af245d11STodd Fiala     if (!IsHost ())
567af245d11STodd Fiala         return Error("PlatformLinux::%s (): cannot launch a debug process when not the host", __FUNCTION__);
568af245d11STodd Fiala 
569af245d11STodd Fiala     // Retrieve the exe module.
570af245d11STodd Fiala     lldb::ModuleSP exe_module_sp;
571af245d11STodd Fiala 
572af245d11STodd Fiala     Error error = ResolveExecutable (
573af245d11STodd Fiala         launch_info.GetExecutableFile (),
574af245d11STodd Fiala         launch_info.GetArchitecture (),
575af245d11STodd Fiala         exe_module_sp,
576af245d11STodd Fiala         NULL);
577af245d11STodd Fiala 
578af245d11STodd Fiala     if (!error.Success ())
579af245d11STodd Fiala         return error;
580af245d11STodd Fiala 
581af245d11STodd Fiala     if (!exe_module_sp)
582af245d11STodd Fiala         return Error("exe_module_sp could not be resolved for %s", launch_info.GetExecutableFile ().GetPath ().c_str ());
583af245d11STodd Fiala 
584af245d11STodd Fiala     // Launch it for debugging
585af245d11STodd Fiala     error = NativeProcessLinux::LaunchProcess (
586af245d11STodd Fiala         exe_module_sp.get (),
587af245d11STodd Fiala         launch_info,
588af245d11STodd Fiala         native_delegate,
589af245d11STodd Fiala         process_sp);
590af245d11STodd Fiala 
591af245d11STodd Fiala     return error;
592af245d11STodd Fiala #endif
593af245d11STodd Fiala }
594af245d11STodd Fiala 
595af245d11STodd Fiala Error
596af245d11STodd Fiala PlatformLinux::AttachNativeProcess (lldb::pid_t pid,
597af245d11STodd Fiala                                     lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
598af245d11STodd Fiala                                     NativeProcessProtocolSP &process_sp)
599af245d11STodd Fiala {
600af245d11STodd Fiala #if !defined(__linux__)
601af245d11STodd Fiala     return Error("only implemented on Linux hosts");
602af245d11STodd Fiala #else
603af245d11STodd Fiala     if (!IsHost ())
604af245d11STodd Fiala         return Error("PlatformLinux::%s (): cannot attach to a debug process when not the host", __FUNCTION__);
605af245d11STodd Fiala 
606af245d11STodd Fiala     // Launch it for debugging
607af245d11STodd Fiala     return NativeProcessLinux::AttachToProcess (pid, native_delegate, process_sp);
608af245d11STodd Fiala #endif
609af245d11STodd Fiala }
610