1e996fd30SGreg Clayton //===-- PlatformLinux.cpp ---------------------------------------*- C++ -*-===//
2e996fd30SGreg Clayton //
3e996fd30SGreg Clayton //                     The LLVM Compiler Infrastructure
4e996fd30SGreg Clayton //
5e996fd30SGreg Clayton // This file is distributed under the University of Illinois Open Source
6e996fd30SGreg Clayton // License. See LICENSE.TXT for details.
7e996fd30SGreg Clayton //
8e996fd30SGreg Clayton //===----------------------------------------------------------------------===//
9e996fd30SGreg Clayton 
10e996fd30SGreg Clayton #include "PlatformLinux.h"
11e996fd30SGreg Clayton 
12e996fd30SGreg Clayton // C Includes
13ecc11474SStephen Wilson #include <stdio.h>
14ecc11474SStephen Wilson #include <sys/utsname.h>
15ecc11474SStephen Wilson 
16e996fd30SGreg Clayton // C++ Includes
17e996fd30SGreg Clayton // Other libraries and framework includes
18e996fd30SGreg Clayton // Project includes
19e996fd30SGreg Clayton #include "lldb/Core/Error.h"
2028041352SGreg Clayton #include "lldb/Core/Debugger.h"
21e996fd30SGreg Clayton #include "lldb/Core/Module.h"
22e996fd30SGreg Clayton #include "lldb/Core/ModuleList.h"
23ecc11474SStephen Wilson #include "lldb/Core/PluginManager.h"
24e996fd30SGreg Clayton #include "lldb/Core/StreamString.h"
25e996fd30SGreg Clayton #include "lldb/Host/FileSpec.h"
26e996fd30SGreg Clayton #include "lldb/Host/Host.h"
27ecc11474SStephen Wilson #include "lldb/Target/Target.h"
28e996fd30SGreg Clayton #include "lldb/Target/Process.h"
29e996fd30SGreg Clayton 
30e996fd30SGreg Clayton using namespace lldb;
31e996fd30SGreg Clayton using namespace lldb_private;
32e996fd30SGreg Clayton 
3328041352SGreg Clayton static uint32_t g_initialize_count = 0;
3428041352SGreg Clayton 
35ecc11474SStephen Wilson Platform *
36b3a40ba8SGreg Clayton PlatformLinux::CreateInstance (bool force, const ArchSpec *arch)
37ecc11474SStephen Wilson {
38b3a40ba8SGreg Clayton     bool create = force;
39b3a40ba8SGreg Clayton     if (create == false && arch && arch->IsValid())
40b3a40ba8SGreg Clayton     {
41b3a40ba8SGreg Clayton         const llvm::Triple &triple = arch->GetTriple();
42b3a40ba8SGreg Clayton         const llvm::Triple::OSType os = triple.getOS();
43b3a40ba8SGreg Clayton         if (os == llvm::Triple::Linux)
44b3a40ba8SGreg Clayton             create = true;
45b3a40ba8SGreg Clayton     }
46b3a40ba8SGreg Clayton     if (create)
4728041352SGreg Clayton         return new PlatformLinux(true);
48b3a40ba8SGreg Clayton     return NULL;
49ecc11474SStephen Wilson }
50ecc11474SStephen Wilson 
51ecc11474SStephen Wilson const char *
52ecc11474SStephen Wilson PlatformLinux::GetPluginNameStatic()
53ecc11474SStephen Wilson {
54ecc11474SStephen Wilson     return "plugin.platform.linux";
55ecc11474SStephen Wilson }
56ecc11474SStephen Wilson 
57ecc11474SStephen Wilson const char *
5828041352SGreg Clayton PlatformLinux::GetShortPluginNameStatic (bool is_host)
59ecc11474SStephen Wilson {
6028041352SGreg Clayton     if (is_host)
6128041352SGreg Clayton         return Platform::GetHostPlatformName ();
6228041352SGreg Clayton     else
6328041352SGreg Clayton         return "remote-linux";
6428041352SGreg Clayton }
6528041352SGreg Clayton 
6628041352SGreg Clayton const char *
6728041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic (bool is_host)
6828041352SGreg Clayton {
6928041352SGreg Clayton     if (is_host)
7028041352SGreg Clayton         return "Local Linux user platform plug-in.";
7128041352SGreg Clayton     else
7228041352SGreg Clayton         return "Remote Linux user platform plug-in.";
73ecc11474SStephen Wilson }
74ecc11474SStephen Wilson 
75e996fd30SGreg Clayton void
76e996fd30SGreg Clayton PlatformLinux::Initialize ()
77e996fd30SGreg Clayton {
7828041352SGreg Clayton     if (g_initialize_count++ == 0)
79ecc11474SStephen Wilson     {
8028041352SGreg Clayton #if defined(__linux__)
8128041352SGreg Clayton         PlatformSP default_platform_sp (new PlatformLinux(true));
8228041352SGreg Clayton         default_platform_sp->SetSystemArchitecture (Host::GetArchitecture());
83e996fd30SGreg Clayton         Platform::SetDefaultPlatform (default_platform_sp);
8428041352SGreg Clayton #endif
8528041352SGreg Clayton         PluginManager::RegisterPlugin(PlatformLinux::GetShortPluginNameStatic(false),
8628041352SGreg Clayton                                       PlatformLinux::GetPluginDescriptionStatic(false),
8728041352SGreg Clayton                                       PlatformLinux::CreateInstance);
88ecc11474SStephen Wilson     }
89e996fd30SGreg Clayton }
90e996fd30SGreg Clayton 
91e996fd30SGreg Clayton void
92e996fd30SGreg Clayton PlatformLinux::Terminate ()
93e996fd30SGreg Clayton {
9428041352SGreg Clayton     if (g_initialize_count > 0)
9528041352SGreg Clayton     {
9628041352SGreg Clayton         if (--g_initialize_count == 0)
9728041352SGreg Clayton         {
9828041352SGreg Clayton             PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance);
99e996fd30SGreg Clayton         }
10028041352SGreg Clayton     }
10128041352SGreg Clayton }
102e996fd30SGreg Clayton 
103e996fd30SGreg Clayton Error
104e996fd30SGreg Clayton PlatformLinux::ResolveExecutable (const FileSpec &exe_file,
105e996fd30SGreg Clayton                                   const ArchSpec &exe_arch,
106ea5e0cc3SGreg Clayton                                   lldb::ModuleSP &exe_module_sp,
107ea5e0cc3SGreg Clayton                                   const FileSpecList *module_search_paths_ptr)
108e996fd30SGreg Clayton {
109e996fd30SGreg Clayton     Error error;
110e996fd30SGreg Clayton     // Nothing special to do here, just use the actual file and architecture
111e996fd30SGreg Clayton 
11228041352SGreg Clayton     char exe_path[PATH_MAX];
113e996fd30SGreg Clayton     FileSpec resolved_exe_file (exe_file);
114e996fd30SGreg Clayton 
11528041352SGreg Clayton     if (IsHost())
11628041352SGreg Clayton     {
11728041352SGreg Clayton         // If we have "ls" as the exe_file, resolve the executable location based on
118e996fd30SGreg Clayton         // the current path variables
119e996fd30SGreg Clayton         if (!resolved_exe_file.Exists())
12028041352SGreg Clayton         {
12128041352SGreg Clayton             exe_file.GetPath(exe_path, sizeof(exe_path));
12228041352SGreg Clayton             resolved_exe_file.SetFile(exe_path, true);
12328041352SGreg Clayton         }
12428041352SGreg Clayton 
12528041352SGreg Clayton         if (!resolved_exe_file.Exists())
126e996fd30SGreg Clayton             resolved_exe_file.ResolveExecutableLocation ();
127e996fd30SGreg Clayton 
12828041352SGreg Clayton         if (resolved_exe_file.Exists())
12928041352SGreg Clayton             error.Clear();
13028041352SGreg Clayton         else
13128041352SGreg Clayton         {
13228041352SGreg Clayton             exe_file.GetPath(exe_path, sizeof(exe_path));
13328041352SGreg Clayton             error.SetErrorStringWithFormat("unable to find executable for '%s'", exe_path);
13428041352SGreg Clayton         }
13528041352SGreg Clayton     }
13628041352SGreg Clayton     else
13728041352SGreg Clayton     {
13828041352SGreg Clayton         if (m_remote_platform_sp)
13928041352SGreg Clayton         {
14028041352SGreg Clayton             error = m_remote_platform_sp->ResolveExecutable (exe_file,
14128041352SGreg Clayton                                                              exe_arch,
1420c90ef47SGreg Clayton                                                              exe_module_sp,
1430c90ef47SGreg Clayton                                                              NULL);
14428041352SGreg Clayton         }
14528041352SGreg Clayton         else
14628041352SGreg Clayton         {
14728041352SGreg Clayton             // We may connect to a process and use the provided executable (Don't use local $PATH).
148e996fd30SGreg Clayton 
149e996fd30SGreg Clayton             if (resolved_exe_file.Exists())
15028041352SGreg Clayton                 error.Clear();
15128041352SGreg Clayton             else
15228041352SGreg Clayton                 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path);
15328041352SGreg Clayton         }
15428041352SGreg Clayton     }
15528041352SGreg Clayton 
15628041352SGreg Clayton     if (error.Success())
157e996fd30SGreg Clayton     {
158ea5e0cc3SGreg Clayton         ModuleSpec module_spec (resolved_exe_file, exe_arch);
159e996fd30SGreg Clayton         if (exe_arch.IsValid())
160e996fd30SGreg Clayton         {
161ea5e0cc3SGreg Clayton             error = ModuleList::GetSharedModule (module_spec,
162e996fd30SGreg Clayton                                                  exe_module_sp,
163e996fd30SGreg Clayton                                                  NULL,
1640c90ef47SGreg Clayton                                                  NULL,
165e996fd30SGreg Clayton                                                  NULL);
166e996fd30SGreg Clayton 
167e996fd30SGreg Clayton             if (exe_module_sp->GetObjectFile() == NULL)
168e996fd30SGreg Clayton             {
169e996fd30SGreg Clayton                 exe_module_sp.reset();
170e996fd30SGreg Clayton                 error.SetErrorStringWithFormat ("'%s%s%s' doesn't contain the architecture %s",
171e996fd30SGreg Clayton                                                 exe_file.GetDirectory().AsCString(""),
172e996fd30SGreg Clayton                                                 exe_file.GetDirectory() ? "/" : "",
173e996fd30SGreg Clayton                                                 exe_file.GetFilename().AsCString(""),
174e996fd30SGreg Clayton                                                 exe_arch.GetArchitectureName());
175e996fd30SGreg Clayton             }
176e996fd30SGreg Clayton         }
177e996fd30SGreg Clayton         else
178e996fd30SGreg Clayton         {
179e996fd30SGreg Clayton             // No valid architecture was specified, ask the platform for
180e996fd30SGreg Clayton             // the architectures that we should be using (in the correct order)
181e996fd30SGreg Clayton             // and see if we can find a match that way
182e996fd30SGreg Clayton             StreamString arch_names;
183ea5e0cc3SGreg Clayton             for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, module_spec.GetArchitecture()); ++idx)
184e996fd30SGreg Clayton             {
185ea5e0cc3SGreg Clayton                 error = ModuleList::GetSharedModule (module_spec,
186e996fd30SGreg Clayton                                                      exe_module_sp,
187e996fd30SGreg Clayton                                                      NULL,
1880c90ef47SGreg Clayton                                                      NULL,
189e996fd30SGreg Clayton                                                      NULL);
190e996fd30SGreg Clayton                 // Did we find an executable using one of the
191e996fd30SGreg Clayton                 if (error.Success())
192e996fd30SGreg Clayton                 {
193e996fd30SGreg Clayton                     if (exe_module_sp && exe_module_sp->GetObjectFile())
194e996fd30SGreg Clayton                         break;
195e996fd30SGreg Clayton                     else
196e996fd30SGreg Clayton                         error.SetErrorToGenericError();
197e996fd30SGreg Clayton                 }
198e996fd30SGreg Clayton 
199e996fd30SGreg Clayton                 if (idx > 0)
200e996fd30SGreg Clayton                     arch_names.PutCString (", ");
201ea5e0cc3SGreg Clayton                 arch_names.PutCString (module_spec.GetArchitecture().GetArchitectureName());
202e996fd30SGreg Clayton             }
203e996fd30SGreg Clayton 
204e996fd30SGreg Clayton             if (error.Fail() || !exe_module_sp)
205e996fd30SGreg Clayton             {
206e996fd30SGreg Clayton                 error.SetErrorStringWithFormat ("'%s%s%s' doesn't contain any '%s' platform architectures: %s",
207e996fd30SGreg Clayton                                                 exe_file.GetDirectory().AsCString(""),
208e996fd30SGreg Clayton                                                 exe_file.GetDirectory() ? "/" : "",
209e996fd30SGreg Clayton                                                 exe_file.GetFilename().AsCString(""),
210e996fd30SGreg Clayton                                                 GetShortPluginName(),
211e996fd30SGreg Clayton                                                 arch_names.GetString().c_str());
212e996fd30SGreg Clayton             }
213e996fd30SGreg Clayton         }
214e996fd30SGreg Clayton     }
215e996fd30SGreg Clayton 
216e996fd30SGreg Clayton     return error;
217e996fd30SGreg Clayton }
218e996fd30SGreg Clayton 
219e996fd30SGreg Clayton Error
22078decfd0SStephen Wilson PlatformLinux::GetFile (const FileSpec &platform_file,
22128041352SGreg Clayton                         const UUID *uuid_ptr, FileSpec &local_file)
222e996fd30SGreg Clayton {
22328041352SGreg Clayton     if (IsRemote())
22428041352SGreg Clayton     {
22528041352SGreg Clayton         if (m_remote_platform_sp)
22628041352SGreg Clayton             return m_remote_platform_sp->GetFile (platform_file, uuid_ptr, local_file);
22728041352SGreg Clayton     }
22828041352SGreg Clayton 
229e996fd30SGreg Clayton     // Default to the local case
230e996fd30SGreg Clayton     local_file = platform_file;
231e996fd30SGreg Clayton     return Error();
232e996fd30SGreg Clayton }
233e996fd30SGreg Clayton 
234e996fd30SGreg Clayton 
235e996fd30SGreg Clayton //------------------------------------------------------------------
236e996fd30SGreg Clayton /// Default Constructor
237e996fd30SGreg Clayton //------------------------------------------------------------------
23828041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) :
23928041352SGreg Clayton     Platform(is_host),  // This is the local host platform
24028041352SGreg Clayton     m_remote_platform_sp ()
241e996fd30SGreg Clayton {
242e996fd30SGreg Clayton }
243e996fd30SGreg Clayton 
244e996fd30SGreg Clayton //------------------------------------------------------------------
245e996fd30SGreg Clayton /// Destructor.
246e996fd30SGreg Clayton ///
247e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be
248e996fd30SGreg Clayton /// inherited from by the plug-in instance.
249e996fd30SGreg Clayton //------------------------------------------------------------------
250e996fd30SGreg Clayton PlatformLinux::~PlatformLinux()
251e996fd30SGreg Clayton {
252e996fd30SGreg Clayton }
253e996fd30SGreg Clayton 
254e996fd30SGreg Clayton bool
25513e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
256e996fd30SGreg Clayton {
25728041352SGreg Clayton     bool success = false;
25828041352SGreg Clayton     if (IsHost())
25928041352SGreg Clayton     {
26028041352SGreg Clayton         success = Platform::GetProcessInfo (pid, process_info);
26128041352SGreg Clayton     }
26228041352SGreg Clayton     else
26328041352SGreg Clayton     {
26428041352SGreg Clayton         if (m_remote_platform_sp)
26528041352SGreg Clayton             success = m_remote_platform_sp->GetProcessInfo (pid, process_info);
26628041352SGreg Clayton     }
26728041352SGreg Clayton     return success;
268e996fd30SGreg Clayton }
269e996fd30SGreg Clayton 
270e996fd30SGreg Clayton bool
271e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
272e996fd30SGreg Clayton {
273e996fd30SGreg Clayton     if (idx == 0)
274e996fd30SGreg Clayton     {
275e996fd30SGreg Clayton         arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
276e996fd30SGreg Clayton         return arch.IsValid();
277e996fd30SGreg Clayton     }
278e996fd30SGreg Clayton     return false;
279e996fd30SGreg Clayton }
280ecc11474SStephen Wilson 
281ecc11474SStephen Wilson void
282ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm)
283ecc11474SStephen Wilson {
284ecc11474SStephen Wilson     struct utsname un;
285ecc11474SStephen Wilson 
286ecc11474SStephen Wilson     if (uname(&un)) {
287ecc11474SStephen Wilson         strm << "Linux";
288ecc11474SStephen Wilson         return;
289ecc11474SStephen Wilson     }
290ecc11474SStephen Wilson 
291ecc11474SStephen Wilson     strm << un.sysname << ' ' << un.release << ' ' << un.version << '\n';
292ecc11474SStephen Wilson }
293ecc11474SStephen Wilson 
294ecc11474SStephen Wilson size_t
295ecc11474SStephen Wilson PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target,
296ecc11474SStephen Wilson                                                 BreakpointSite *bp_site)
297ecc11474SStephen Wilson {
298ecc11474SStephen Wilson     ArchSpec arch = target.GetArchitecture();
29928041352SGreg Clayton     const uint8_t *trap_opcode = NULL;
30028041352SGreg Clayton     size_t trap_opcode_size = 0;
301ecc11474SStephen Wilson 
302ecc11474SStephen Wilson     switch (arch.GetCore())
303ecc11474SStephen Wilson     {
304ecc11474SStephen Wilson     default:
305ecc11474SStephen Wilson         assert(false && "CPU type not supported!");
306ecc11474SStephen Wilson         break;
307ecc11474SStephen Wilson 
308ecc11474SStephen Wilson     case ArchSpec::eCore_x86_32_i386:
309ecc11474SStephen Wilson     case ArchSpec::eCore_x86_64_x86_64:
31028041352SGreg Clayton         {
31128041352SGreg Clayton             static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
31228041352SGreg Clayton             trap_opcode = g_i386_breakpoint_opcode;
31328041352SGreg Clayton             trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
31428041352SGreg Clayton         }
315ecc11474SStephen Wilson         break;
316ecc11474SStephen Wilson     }
317ecc11474SStephen Wilson 
31828041352SGreg Clayton     if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
31928041352SGreg Clayton         return trap_opcode_size;
32028041352SGreg Clayton     return 0;
32128041352SGreg Clayton }
32228041352SGreg Clayton 
32328041352SGreg Clayton Error
32428041352SGreg Clayton PlatformLinux::LaunchProcess (ProcessLaunchInfo &launch_info)
32528041352SGreg Clayton {
32628041352SGreg Clayton     Error error;
32728041352SGreg Clayton 
32828041352SGreg Clayton     if (IsHost())
32928041352SGreg Clayton     {
33028041352SGreg Clayton         if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
33128041352SGreg Clayton         {
33228041352SGreg Clayton             const bool is_localhost = true;
333*d1cf11a7SGreg Clayton             const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
334*d1cf11a7SGreg Clayton             const bool first_arg_is_full_shell_command = false;
335*d1cf11a7SGreg Clayton             if (!launch_info.ConvertArgumentsForLaunchingInShell (error,
336*d1cf11a7SGreg Clayton                                                                   is_localhost,
337*d1cf11a7SGreg Clayton                                                                   will_debug,
338*d1cf11a7SGreg Clayton                                                                   first_arg_is_full_shell_command))
33928041352SGreg Clayton                 return error;
34028041352SGreg Clayton         }
34128041352SGreg Clayton         error = Platform::LaunchProcess (launch_info);
34228041352SGreg Clayton     }
34328041352SGreg Clayton     else
34428041352SGreg Clayton     {
34528041352SGreg Clayton         error.SetErrorString ("the platform is not currently connected");
34628041352SGreg Clayton     }
34728041352SGreg Clayton     return error;
348ecc11474SStephen Wilson }
34913e8e1c3SJohnny Chen 
35013e8e1c3SJohnny Chen lldb::ProcessSP
351fb2b629dSPeter Collingbourne PlatformLinux::Attach(ProcessAttachInfo &attach_info,
35213e8e1c3SJohnny Chen                       Debugger &debugger,
35313e8e1c3SJohnny Chen                       Target *target,
35413e8e1c3SJohnny Chen                       Listener &listener,
35513e8e1c3SJohnny Chen                       Error &error)
35613e8e1c3SJohnny Chen {
35728041352SGreg Clayton     lldb::ProcessSP process_sp;
35828041352SGreg Clayton     if (IsHost())
35928041352SGreg Clayton     {
36028041352SGreg Clayton         if (target == NULL)
36128041352SGreg Clayton         {
36228041352SGreg Clayton             TargetSP new_target_sp;
36328041352SGreg Clayton             FileSpec emptyFileSpec;
36428041352SGreg Clayton             ArchSpec emptyArchSpec;
36528041352SGreg Clayton 
36628041352SGreg Clayton             error = debugger.GetTargetList().CreateTarget (debugger,
36728041352SGreg Clayton                                                            emptyFileSpec,
36828041352SGreg Clayton                                                            emptyArchSpec,
36928041352SGreg Clayton                                                            false,
37028041352SGreg Clayton                                                            m_remote_platform_sp,
37128041352SGreg Clayton                                                            new_target_sp);
37228041352SGreg Clayton             target = new_target_sp.get();
37328041352SGreg Clayton         }
37428041352SGreg Clayton         else
37528041352SGreg Clayton             error.Clear();
37628041352SGreg Clayton 
37728041352SGreg Clayton         if (target && error.Success())
37828041352SGreg Clayton         {
37928041352SGreg Clayton             debugger.GetTargetList().SetSelectedTarget(target);
38028041352SGreg Clayton 
3810c90ef47SGreg Clayton             process_sp = target->CreateProcess (listener,
3820c90ef47SGreg Clayton                                                 attach_info.GetProcessPluginName(),
3830c90ef47SGreg Clayton                                                 NULL);
38428041352SGreg Clayton 
38528041352SGreg Clayton             if (process_sp)
38628041352SGreg Clayton                 error = process_sp->Attach (attach_info);
38728041352SGreg Clayton         }
38828041352SGreg Clayton     }
38928041352SGreg Clayton     else
39028041352SGreg Clayton     {
39128041352SGreg Clayton         if (m_remote_platform_sp)
39228041352SGreg Clayton             process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, listener, error);
39328041352SGreg Clayton         else
39428041352SGreg Clayton             error.SetErrorString ("the platform is not currently connected");
39528041352SGreg Clayton     }
39628041352SGreg Clayton     return process_sp;
39713e8e1c3SJohnny Chen }
398