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"
13e996fd30SGreg Clayton 
14e996fd30SGreg Clayton // C Includes
15ecc11474SStephen Wilson #include <stdio.h>
16ecc11474SStephen Wilson #include <sys/utsname.h>
17ecc11474SStephen Wilson 
18e996fd30SGreg Clayton // C++ Includes
19e996fd30SGreg Clayton // Other libraries and framework includes
20e996fd30SGreg Clayton // Project includes
21e996fd30SGreg Clayton #include "lldb/Core/Error.h"
2228041352SGreg Clayton #include "lldb/Core/Debugger.h"
23e996fd30SGreg Clayton #include "lldb/Core/Module.h"
24e996fd30SGreg Clayton #include "lldb/Core/ModuleList.h"
251f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h"
26ecc11474SStephen Wilson #include "lldb/Core/PluginManager.h"
27e996fd30SGreg Clayton #include "lldb/Core/StreamString.h"
28e996fd30SGreg Clayton #include "lldb/Host/FileSpec.h"
29e996fd30SGreg Clayton #include "lldb/Host/Host.h"
30ecc11474SStephen Wilson #include "lldb/Target/Target.h"
31e996fd30SGreg Clayton #include "lldb/Target/Process.h"
32e996fd30SGreg Clayton 
33e996fd30SGreg Clayton using namespace lldb;
34e996fd30SGreg Clayton using namespace lldb_private;
35e996fd30SGreg Clayton 
3628041352SGreg Clayton static uint32_t g_initialize_count = 0;
3728041352SGreg Clayton 
38ecc11474SStephen Wilson Platform *
39b3a40ba8SGreg Clayton PlatformLinux::CreateInstance (bool force, const ArchSpec *arch)
40ecc11474SStephen Wilson {
41b3a40ba8SGreg Clayton     bool create = force;
42b3a40ba8SGreg Clayton     if (create == false && arch && arch->IsValid())
43b3a40ba8SGreg Clayton     {
44b3a40ba8SGreg Clayton         const llvm::Triple &triple = arch->GetTriple();
4570512317SGreg Clayton         switch (triple.getVendor())
4670512317SGreg Clayton         {
4770512317SGreg Clayton             case llvm::Triple::PC:
48b3a40ba8SGreg Clayton                 create = true;
4970512317SGreg Clayton                 break;
5070512317SGreg Clayton 
51dbc6c0bbSGreg Clayton #if defined(__linux__)
52dbc6c0bbSGreg Clayton             // Only accept "unknown" for the vendor if the host is linux and
53dbc6c0bbSGreg Clayton             // it "unknown" wasn't specified (it was just returned becasue it
54dbc6c0bbSGreg Clayton             // was NOT specified_
5570512317SGreg Clayton             case llvm::Triple::UnknownArch:
5670512317SGreg Clayton                 create = !arch->TripleVendorWasSpecified();
5770512317SGreg Clayton                 break;
58dbc6c0bbSGreg Clayton #endif
5970512317SGreg Clayton             default:
6070512317SGreg Clayton                 break;
6170512317SGreg Clayton         }
6270512317SGreg Clayton 
6370512317SGreg Clayton         if (create)
6470512317SGreg Clayton         {
6570512317SGreg Clayton             switch (triple.getOS())
6670512317SGreg Clayton             {
6770512317SGreg Clayton                 case llvm::Triple::Linux:
6870512317SGreg Clayton                     break;
6970512317SGreg Clayton 
70dbc6c0bbSGreg Clayton #if defined(__linux__)
71dbc6c0bbSGreg Clayton                 // Only accept "unknown" for the OS if the host is linux and
72dbc6c0bbSGreg Clayton                 // it "unknown" wasn't specified (it was just returned becasue it
73dbc6c0bbSGreg Clayton                 // was NOT specified)
7470512317SGreg Clayton                 case llvm::Triple::UnknownOS:
7570512317SGreg Clayton                     create = !arch->TripleOSWasSpecified();
7670512317SGreg Clayton                     break;
77dbc6c0bbSGreg Clayton #endif
7870512317SGreg Clayton                 default:
7970512317SGreg Clayton                     create = false;
8070512317SGreg Clayton                     break;
8170512317SGreg Clayton             }
8270512317SGreg Clayton         }
83b3a40ba8SGreg Clayton     }
84b3a40ba8SGreg Clayton     if (create)
8528041352SGreg Clayton         return new PlatformLinux(true);
86b3a40ba8SGreg Clayton     return NULL;
87ecc11474SStephen Wilson }
88ecc11474SStephen Wilson 
89ecc11474SStephen Wilson 
9057abc5d6SGreg Clayton lldb_private::ConstString
9157abc5d6SGreg Clayton PlatformLinux::GetPluginNameStatic (bool is_host)
92ecc11474SStephen Wilson {
9328041352SGreg Clayton     if (is_host)
9457abc5d6SGreg Clayton     {
9557abc5d6SGreg Clayton         static ConstString g_host_name(Platform::GetHostPlatformName ());
9657abc5d6SGreg Clayton         return g_host_name;
9757abc5d6SGreg Clayton     }
9828041352SGreg Clayton     else
9957abc5d6SGreg Clayton     {
10057abc5d6SGreg Clayton         static ConstString g_remote_name("remote-linux");
10157abc5d6SGreg Clayton         return g_remote_name;
10257abc5d6SGreg Clayton     }
10328041352SGreg Clayton }
10428041352SGreg Clayton 
10528041352SGreg Clayton const char *
10628041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic (bool is_host)
10728041352SGreg Clayton {
10828041352SGreg Clayton     if (is_host)
10928041352SGreg Clayton         return "Local Linux user platform plug-in.";
11028041352SGreg Clayton     else
11128041352SGreg Clayton         return "Remote Linux user platform plug-in.";
112ecc11474SStephen Wilson }
113ecc11474SStephen Wilson 
11457abc5d6SGreg Clayton lldb_private::ConstString
11557abc5d6SGreg Clayton PlatformLinux::GetPluginName()
11657abc5d6SGreg Clayton {
11757abc5d6SGreg Clayton     return GetPluginNameStatic(IsHost());
11857abc5d6SGreg Clayton }
11957abc5d6SGreg Clayton 
120e996fd30SGreg Clayton void
121e996fd30SGreg Clayton PlatformLinux::Initialize ()
122e996fd30SGreg Clayton {
12328041352SGreg Clayton     if (g_initialize_count++ == 0)
124ecc11474SStephen Wilson     {
12528041352SGreg Clayton #if defined(__linux__)
12628041352SGreg Clayton         PlatformSP default_platform_sp (new PlatformLinux(true));
12728041352SGreg Clayton         default_platform_sp->SetSystemArchitecture (Host::GetArchitecture());
128e996fd30SGreg Clayton         Platform::SetDefaultPlatform (default_platform_sp);
12928041352SGreg Clayton #endif
13057abc5d6SGreg Clayton         PluginManager::RegisterPlugin(PlatformLinux::GetPluginNameStatic(false),
13128041352SGreg Clayton                                       PlatformLinux::GetPluginDescriptionStatic(false),
13228041352SGreg Clayton                                       PlatformLinux::CreateInstance);
133ecc11474SStephen Wilson     }
134e996fd30SGreg Clayton }
135e996fd30SGreg Clayton 
136e996fd30SGreg Clayton void
137e996fd30SGreg Clayton PlatformLinux::Terminate ()
138e996fd30SGreg Clayton {
13928041352SGreg Clayton     if (g_initialize_count > 0)
14028041352SGreg Clayton     {
14128041352SGreg Clayton         if (--g_initialize_count == 0)
14228041352SGreg Clayton         {
14328041352SGreg Clayton             PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance);
144e996fd30SGreg Clayton         }
14528041352SGreg Clayton     }
14628041352SGreg Clayton }
147e996fd30SGreg Clayton 
148e996fd30SGreg Clayton Error
149e996fd30SGreg Clayton PlatformLinux::ResolveExecutable (const FileSpec &exe_file,
150e996fd30SGreg Clayton                                   const ArchSpec &exe_arch,
151ea5e0cc3SGreg Clayton                                   lldb::ModuleSP &exe_module_sp,
152ea5e0cc3SGreg Clayton                                   const FileSpecList *module_search_paths_ptr)
153e996fd30SGreg Clayton {
154e996fd30SGreg Clayton     Error error;
155e996fd30SGreg Clayton     // Nothing special to do here, just use the actual file and architecture
156e996fd30SGreg Clayton 
15728041352SGreg Clayton     char exe_path[PATH_MAX];
158e996fd30SGreg Clayton     FileSpec resolved_exe_file (exe_file);
159e996fd30SGreg Clayton 
16028041352SGreg Clayton     if (IsHost())
16128041352SGreg Clayton     {
16228041352SGreg Clayton         // If we have "ls" as the exe_file, resolve the executable location based on
163e996fd30SGreg Clayton         // the current path variables
164e996fd30SGreg Clayton         if (!resolved_exe_file.Exists())
16528041352SGreg Clayton         {
16628041352SGreg Clayton             exe_file.GetPath(exe_path, sizeof(exe_path));
16728041352SGreg Clayton             resolved_exe_file.SetFile(exe_path, true);
16828041352SGreg Clayton         }
16928041352SGreg Clayton 
17028041352SGreg Clayton         if (!resolved_exe_file.Exists())
171e996fd30SGreg Clayton             resolved_exe_file.ResolveExecutableLocation ();
172e996fd30SGreg Clayton 
17328041352SGreg Clayton         if (resolved_exe_file.Exists())
17428041352SGreg Clayton             error.Clear();
17528041352SGreg Clayton         else
17628041352SGreg Clayton         {
17728041352SGreg Clayton             exe_file.GetPath(exe_path, sizeof(exe_path));
17828041352SGreg Clayton             error.SetErrorStringWithFormat("unable to find executable for '%s'", exe_path);
17928041352SGreg Clayton         }
18028041352SGreg Clayton     }
18128041352SGreg Clayton     else
18228041352SGreg Clayton     {
18328041352SGreg Clayton         if (m_remote_platform_sp)
18428041352SGreg Clayton         {
18528041352SGreg Clayton             error = m_remote_platform_sp->ResolveExecutable (exe_file,
18628041352SGreg Clayton                                                              exe_arch,
1870c90ef47SGreg Clayton                                                              exe_module_sp,
1880c90ef47SGreg Clayton                                                              NULL);
18928041352SGreg Clayton         }
19028041352SGreg Clayton         else
19128041352SGreg Clayton         {
19228041352SGreg Clayton             // We may connect to a process and use the provided executable (Don't use local $PATH).
193e996fd30SGreg Clayton 
194e996fd30SGreg Clayton             if (resolved_exe_file.Exists())
19528041352SGreg Clayton                 error.Clear();
19628041352SGreg Clayton             else
19728041352SGreg Clayton                 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path);
19828041352SGreg Clayton         }
19928041352SGreg Clayton     }
20028041352SGreg Clayton 
20128041352SGreg Clayton     if (error.Success())
202e996fd30SGreg Clayton     {
203ea5e0cc3SGreg Clayton         ModuleSpec module_spec (resolved_exe_file, exe_arch);
204e996fd30SGreg Clayton         if (exe_arch.IsValid())
205e996fd30SGreg Clayton         {
206ea5e0cc3SGreg Clayton             error = ModuleList::GetSharedModule (module_spec,
207e996fd30SGreg Clayton                                                  exe_module_sp,
208e996fd30SGreg Clayton                                                  NULL,
2090c90ef47SGreg Clayton                                                  NULL,
210e996fd30SGreg Clayton                                                  NULL);
211*9f0013d8SMichael Sartain             if (error.Fail())
212*9f0013d8SMichael Sartain             {
213*9f0013d8SMichael Sartain                 // If we failed, it may be because the vendor and os aren't known. If that is the
214*9f0013d8SMichael Sartain                 // case, try setting them to the host architecture and give it another try.
215*9f0013d8SMichael Sartain                 llvm::Triple &module_triple = module_spec.GetArchitecture().GetTriple();
216*9f0013d8SMichael Sartain                 bool is_vendor_specified = (module_triple.getVendor() != llvm::Triple::UnknownVendor);
217*9f0013d8SMichael Sartain                 bool is_os_specified = (module_triple.getOS() != llvm::Triple::UnknownOS);
218*9f0013d8SMichael Sartain                 if (!is_vendor_specified || !is_os_specified)
219*9f0013d8SMichael Sartain                 {
220*9f0013d8SMichael Sartain                     const llvm::Triple &host_triple = Host::GetArchitecture (Host::eSystemDefaultArchitecture).GetTriple();
221*9f0013d8SMichael Sartain 
222*9f0013d8SMichael Sartain                     if (!is_vendor_specified)
223*9f0013d8SMichael Sartain                         module_triple.setVendorName (host_triple.getVendorName());
224*9f0013d8SMichael Sartain                     if (!is_os_specified)
225*9f0013d8SMichael Sartain                         module_triple.setOSName (host_triple.getOSName());
226*9f0013d8SMichael Sartain 
227*9f0013d8SMichael Sartain                     error = ModuleList::GetSharedModule (module_spec,
228*9f0013d8SMichael Sartain                                                          exe_module_sp,
229*9f0013d8SMichael Sartain                                                          NULL,
230*9f0013d8SMichael Sartain                                                          NULL,
231*9f0013d8SMichael Sartain                                                          NULL);
232*9f0013d8SMichael Sartain                 }
233*9f0013d8SMichael Sartain             }
234e996fd30SGreg Clayton 
235e635db49SSean Callanan             // TODO find out why exe_module_sp might be NULL
236e635db49SSean Callanan             if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL)
237e996fd30SGreg Clayton             {
238e996fd30SGreg Clayton                 exe_module_sp.reset();
239b5ad4ec7SGreg Clayton                 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s",
240b5ad4ec7SGreg Clayton                                                 exe_file.GetPath().c_str(),
241e996fd30SGreg Clayton                                                 exe_arch.GetArchitectureName());
242e996fd30SGreg Clayton             }
243e996fd30SGreg Clayton         }
244e996fd30SGreg Clayton         else
245e996fd30SGreg Clayton         {
246e996fd30SGreg Clayton             // No valid architecture was specified, ask the platform for
247e996fd30SGreg Clayton             // the architectures that we should be using (in the correct order)
248e996fd30SGreg Clayton             // and see if we can find a match that way
249e996fd30SGreg Clayton             StreamString arch_names;
250ea5e0cc3SGreg Clayton             for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, module_spec.GetArchitecture()); ++idx)
251e996fd30SGreg Clayton             {
252ea5e0cc3SGreg Clayton                 error = ModuleList::GetSharedModule (module_spec,
253e996fd30SGreg Clayton                                                      exe_module_sp,
254e996fd30SGreg Clayton                                                      NULL,
2550c90ef47SGreg Clayton                                                      NULL,
256e996fd30SGreg Clayton                                                      NULL);
257e996fd30SGreg Clayton                 // Did we find an executable using one of the
258e996fd30SGreg Clayton                 if (error.Success())
259e996fd30SGreg Clayton                 {
260e996fd30SGreg Clayton                     if (exe_module_sp && exe_module_sp->GetObjectFile())
261e996fd30SGreg Clayton                         break;
262e996fd30SGreg Clayton                     else
263e996fd30SGreg Clayton                         error.SetErrorToGenericError();
264e996fd30SGreg Clayton                 }
265e996fd30SGreg Clayton 
266e996fd30SGreg Clayton                 if (idx > 0)
267e996fd30SGreg Clayton                     arch_names.PutCString (", ");
268ea5e0cc3SGreg Clayton                 arch_names.PutCString (module_spec.GetArchitecture().GetArchitectureName());
269e996fd30SGreg Clayton             }
270e996fd30SGreg Clayton 
271e996fd30SGreg Clayton             if (error.Fail() || !exe_module_sp)
272e996fd30SGreg Clayton             {
273b5ad4ec7SGreg Clayton                 error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
274b5ad4ec7SGreg Clayton                                                 exe_file.GetPath().c_str(),
27557abc5d6SGreg Clayton                                                 GetPluginName().GetCString(),
276e996fd30SGreg Clayton                                                 arch_names.GetString().c_str());
277e996fd30SGreg Clayton             }
278e996fd30SGreg Clayton         }
279e996fd30SGreg Clayton     }
280e996fd30SGreg Clayton 
281e996fd30SGreg Clayton     return error;
282e996fd30SGreg Clayton }
283e996fd30SGreg Clayton 
284e996fd30SGreg Clayton Error
28578decfd0SStephen Wilson PlatformLinux::GetFile (const FileSpec &platform_file,
28628041352SGreg Clayton                         const UUID *uuid_ptr, FileSpec &local_file)
287e996fd30SGreg Clayton {
28828041352SGreg Clayton     if (IsRemote())
28928041352SGreg Clayton     {
29028041352SGreg Clayton         if (m_remote_platform_sp)
29128041352SGreg Clayton             return m_remote_platform_sp->GetFile (platform_file, uuid_ptr, local_file);
29228041352SGreg Clayton     }
29328041352SGreg Clayton 
294e996fd30SGreg Clayton     // Default to the local case
295e996fd30SGreg Clayton     local_file = platform_file;
296e996fd30SGreg Clayton     return Error();
297e996fd30SGreg Clayton }
298e996fd30SGreg Clayton 
299e996fd30SGreg Clayton 
300e996fd30SGreg Clayton //------------------------------------------------------------------
301e996fd30SGreg Clayton /// Default Constructor
302e996fd30SGreg Clayton //------------------------------------------------------------------
30328041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) :
30428041352SGreg Clayton     Platform(is_host),  // This is the local host platform
30528041352SGreg Clayton     m_remote_platform_sp ()
306e996fd30SGreg Clayton {
307e996fd30SGreg Clayton }
308e996fd30SGreg Clayton 
309e996fd30SGreg Clayton //------------------------------------------------------------------
310e996fd30SGreg Clayton /// Destructor.
311e996fd30SGreg Clayton ///
312e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be
313e996fd30SGreg Clayton /// inherited from by the plug-in instance.
314e996fd30SGreg Clayton //------------------------------------------------------------------
315e996fd30SGreg Clayton PlatformLinux::~PlatformLinux()
316e996fd30SGreg Clayton {
317e996fd30SGreg Clayton }
318e996fd30SGreg Clayton 
319e996fd30SGreg Clayton bool
32013e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
321e996fd30SGreg Clayton {
32228041352SGreg Clayton     bool success = false;
32328041352SGreg Clayton     if (IsHost())
32428041352SGreg Clayton     {
32528041352SGreg Clayton         success = Platform::GetProcessInfo (pid, process_info);
32628041352SGreg Clayton     }
32728041352SGreg Clayton     else
32828041352SGreg Clayton     {
32928041352SGreg Clayton         if (m_remote_platform_sp)
33028041352SGreg Clayton             success = m_remote_platform_sp->GetProcessInfo (pid, process_info);
33128041352SGreg Clayton     }
33228041352SGreg Clayton     return success;
333e996fd30SGreg Clayton }
334e996fd30SGreg Clayton 
335e996fd30SGreg Clayton bool
336e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
337e996fd30SGreg Clayton {
338e996fd30SGreg Clayton     if (idx == 0)
339e996fd30SGreg Clayton     {
340e996fd30SGreg Clayton         arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
341e996fd30SGreg Clayton         return arch.IsValid();
342e996fd30SGreg Clayton     }
343542e4075SGreg Clayton     else if (idx == 1)
344542e4075SGreg Clayton     {
345542e4075SGreg Clayton         // If the default host architecture is 64-bit, look for a 32-bit variant
346542e4075SGreg Clayton         ArchSpec hostArch
347542e4075SGreg Clayton                       = Host::GetArchitecture(Host::eSystemDefaultArchitecture);
348542e4075SGreg Clayton         if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit())
349542e4075SGreg Clayton         {
350542e4075SGreg Clayton             arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
351542e4075SGreg Clayton             return arch.IsValid();
352542e4075SGreg Clayton         }
353542e4075SGreg Clayton     }
354e996fd30SGreg Clayton     return false;
355e996fd30SGreg Clayton }
356ecc11474SStephen Wilson 
357ecc11474SStephen Wilson void
358ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm)
359ecc11474SStephen Wilson {
360ecc11474SStephen Wilson     struct utsname un;
361ecc11474SStephen Wilson 
3623be69dacSDaniel Malea     Platform::GetStatus(strm);
363ecc11474SStephen Wilson 
3643be69dacSDaniel Malea     if (uname(&un))
3653be69dacSDaniel Malea         return;
3663be69dacSDaniel Malea 
3673be69dacSDaniel Malea     strm.Printf ("    Kernel: %s\n", un.sysname);
3683be69dacSDaniel Malea     strm.Printf ("   Release: %s\n", un.release);
3693be69dacSDaniel Malea     strm.Printf ("   Version: %s\n", un.version);
370ecc11474SStephen Wilson }
371ecc11474SStephen Wilson 
372ecc11474SStephen Wilson size_t
373ecc11474SStephen Wilson PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target,
374ecc11474SStephen Wilson                                                 BreakpointSite *bp_site)
375ecc11474SStephen Wilson {
376ecc11474SStephen Wilson     ArchSpec arch = target.GetArchitecture();
37728041352SGreg Clayton     const uint8_t *trap_opcode = NULL;
37828041352SGreg Clayton     size_t trap_opcode_size = 0;
379ecc11474SStephen Wilson 
380ecc11474SStephen Wilson     switch (arch.GetCore())
381ecc11474SStephen Wilson     {
382ecc11474SStephen Wilson     default:
383ecc11474SStephen Wilson         assert(false && "CPU type not supported!");
384ecc11474SStephen Wilson         break;
385ecc11474SStephen Wilson 
386ecc11474SStephen Wilson     case ArchSpec::eCore_x86_32_i386:
387ecc11474SStephen Wilson     case ArchSpec::eCore_x86_64_x86_64:
38828041352SGreg Clayton         {
38928041352SGreg Clayton             static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
39028041352SGreg Clayton             trap_opcode = g_i386_breakpoint_opcode;
39128041352SGreg Clayton             trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
39228041352SGreg Clayton         }
393ecc11474SStephen Wilson         break;
394ecc11474SStephen Wilson     }
395ecc11474SStephen Wilson 
39628041352SGreg Clayton     if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
39728041352SGreg Clayton         return trap_opcode_size;
39828041352SGreg Clayton     return 0;
39928041352SGreg Clayton }
40028041352SGreg Clayton 
40128041352SGreg Clayton Error
40228041352SGreg Clayton PlatformLinux::LaunchProcess (ProcessLaunchInfo &launch_info)
40328041352SGreg Clayton {
40428041352SGreg Clayton     Error error;
40528041352SGreg Clayton 
40628041352SGreg Clayton     if (IsHost())
40728041352SGreg Clayton     {
40828041352SGreg Clayton         if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
40928041352SGreg Clayton         {
41028041352SGreg Clayton             const bool is_localhost = true;
411d1cf11a7SGreg Clayton             const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
412d1cf11a7SGreg Clayton             const bool first_arg_is_full_shell_command = false;
413d1cf11a7SGreg Clayton             if (!launch_info.ConvertArgumentsForLaunchingInShell (error,
414d1cf11a7SGreg Clayton                                                                   is_localhost,
415d1cf11a7SGreg Clayton                                                                   will_debug,
416d1cf11a7SGreg Clayton                                                                   first_arg_is_full_shell_command))
41728041352SGreg Clayton                 return error;
41828041352SGreg Clayton         }
41928041352SGreg Clayton         error = Platform::LaunchProcess (launch_info);
42028041352SGreg Clayton     }
42128041352SGreg Clayton     else
42228041352SGreg Clayton     {
42328041352SGreg Clayton         error.SetErrorString ("the platform is not currently connected");
42428041352SGreg Clayton     }
42528041352SGreg Clayton     return error;
426ecc11474SStephen Wilson }
42713e8e1c3SJohnny Chen 
42813e8e1c3SJohnny Chen lldb::ProcessSP
429fb2b629dSPeter Collingbourne PlatformLinux::Attach(ProcessAttachInfo &attach_info,
43013e8e1c3SJohnny Chen                       Debugger &debugger,
43113e8e1c3SJohnny Chen                       Target *target,
43213e8e1c3SJohnny Chen                       Listener &listener,
43313e8e1c3SJohnny Chen                       Error &error)
43413e8e1c3SJohnny Chen {
43528041352SGreg Clayton     lldb::ProcessSP process_sp;
43628041352SGreg Clayton     if (IsHost())
43728041352SGreg Clayton     {
43828041352SGreg Clayton         if (target == NULL)
43928041352SGreg Clayton         {
44028041352SGreg Clayton             TargetSP new_target_sp;
44128041352SGreg Clayton             ArchSpec emptyArchSpec;
44228041352SGreg Clayton 
44328041352SGreg Clayton             error = debugger.GetTargetList().CreateTarget (debugger,
444a0ca6601SGreg Clayton                                                            NULL,
44528041352SGreg Clayton                                                            emptyArchSpec,
44628041352SGreg Clayton                                                            false,
44728041352SGreg Clayton                                                            m_remote_platform_sp,
44828041352SGreg Clayton                                                            new_target_sp);
44928041352SGreg Clayton             target = new_target_sp.get();
45028041352SGreg Clayton         }
45128041352SGreg Clayton         else
45228041352SGreg Clayton             error.Clear();
45328041352SGreg Clayton 
45428041352SGreg Clayton         if (target && error.Success())
45528041352SGreg Clayton         {
45628041352SGreg Clayton             debugger.GetTargetList().SetSelectedTarget(target);
45728041352SGreg Clayton 
4580c90ef47SGreg Clayton             process_sp = target->CreateProcess (listener,
4590c90ef47SGreg Clayton                                                 attach_info.GetProcessPluginName(),
4600c90ef47SGreg Clayton                                                 NULL);
46128041352SGreg Clayton 
46228041352SGreg Clayton             if (process_sp)
46328041352SGreg Clayton                 error = process_sp->Attach (attach_info);
46428041352SGreg Clayton         }
46528041352SGreg Clayton     }
46628041352SGreg Clayton     else
46728041352SGreg Clayton     {
46828041352SGreg Clayton         if (m_remote_platform_sp)
46928041352SGreg Clayton             process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, listener, error);
47028041352SGreg Clayton         else
47128041352SGreg Clayton             error.SetErrorString ("the platform is not currently connected");
47228041352SGreg Clayton     }
47328041352SGreg Clayton     return process_sp;
47413e8e1c3SJohnny Chen }
475