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 *
36ecc11474SStephen Wilson PlatformLinux::CreateInstance ()
37ecc11474SStephen Wilson {
3828041352SGreg Clayton     return new PlatformLinux(true);
39ecc11474SStephen Wilson }
40ecc11474SStephen Wilson 
41ecc11474SStephen Wilson const char *
42ecc11474SStephen Wilson PlatformLinux::GetPluginNameStatic()
43ecc11474SStephen Wilson {
44ecc11474SStephen Wilson     return "plugin.platform.linux";
45ecc11474SStephen Wilson }
46ecc11474SStephen Wilson 
47ecc11474SStephen Wilson const char *
4828041352SGreg Clayton PlatformLinux::GetShortPluginNameStatic (bool is_host)
49ecc11474SStephen Wilson {
5028041352SGreg Clayton     if (is_host)
5128041352SGreg Clayton         return Platform::GetHostPlatformName ();
5228041352SGreg Clayton     else
5328041352SGreg Clayton         return "remote-linux";
5428041352SGreg Clayton }
5528041352SGreg Clayton 
5628041352SGreg Clayton const char *
5728041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic (bool is_host)
5828041352SGreg Clayton {
5928041352SGreg Clayton     if (is_host)
6028041352SGreg Clayton         return "Local Linux user platform plug-in.";
6128041352SGreg Clayton     else
6228041352SGreg Clayton         return "Remote Linux user platform plug-in.";
63ecc11474SStephen Wilson }
64ecc11474SStephen Wilson 
65e996fd30SGreg Clayton void
66e996fd30SGreg Clayton PlatformLinux::Initialize ()
67e996fd30SGreg Clayton {
6828041352SGreg Clayton     if (g_initialize_count++ == 0)
69ecc11474SStephen Wilson     {
7028041352SGreg Clayton #if defined(__linux__)
7128041352SGreg Clayton         PlatformSP default_platform_sp (new PlatformLinux(true));
7228041352SGreg Clayton         default_platform_sp->SetSystemArchitecture (Host::GetArchitecture());
73e996fd30SGreg Clayton         Platform::SetDefaultPlatform (default_platform_sp);
7428041352SGreg Clayton #endif
7528041352SGreg Clayton         PluginManager::RegisterPlugin(PlatformLinux::GetShortPluginNameStatic(false),
7628041352SGreg Clayton                                       PlatformLinux::GetPluginDescriptionStatic(false),
7728041352SGreg Clayton                                       PlatformLinux::CreateInstance);
78ecc11474SStephen Wilson     }
79e996fd30SGreg Clayton }
80e996fd30SGreg Clayton 
81e996fd30SGreg Clayton void
82e996fd30SGreg Clayton PlatformLinux::Terminate ()
83e996fd30SGreg Clayton {
8428041352SGreg Clayton     if (g_initialize_count > 0)
8528041352SGreg Clayton     {
8628041352SGreg Clayton         if (--g_initialize_count == 0)
8728041352SGreg Clayton         {
8828041352SGreg Clayton             PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance);
89e996fd30SGreg Clayton         }
9028041352SGreg Clayton     }
9128041352SGreg Clayton }
92e996fd30SGreg Clayton 
93e996fd30SGreg Clayton Error
94e996fd30SGreg Clayton PlatformLinux::ResolveExecutable (const FileSpec &exe_file,
95e996fd30SGreg Clayton                                   const ArchSpec &exe_arch,
96*ea5e0cc3SGreg Clayton                                   lldb::ModuleSP &exe_module_sp,
97*ea5e0cc3SGreg Clayton                                   const FileSpecList *module_search_paths_ptr)
98e996fd30SGreg Clayton {
99e996fd30SGreg Clayton     Error error;
100e996fd30SGreg Clayton     // Nothing special to do here, just use the actual file and architecture
101e996fd30SGreg Clayton 
10228041352SGreg Clayton     char exe_path[PATH_MAX];
103e996fd30SGreg Clayton     FileSpec resolved_exe_file (exe_file);
104e996fd30SGreg Clayton 
10528041352SGreg Clayton     if (IsHost())
10628041352SGreg Clayton     {
10728041352SGreg Clayton         // If we have "ls" as the exe_file, resolve the executable location based on
108e996fd30SGreg Clayton         // the current path variables
109e996fd30SGreg Clayton         if (!resolved_exe_file.Exists())
11028041352SGreg Clayton         {
11128041352SGreg Clayton             exe_file.GetPath(exe_path, sizeof(exe_path));
11228041352SGreg Clayton             resolved_exe_file.SetFile(exe_path, true);
11328041352SGreg Clayton         }
11428041352SGreg Clayton 
11528041352SGreg Clayton         if (!resolved_exe_file.Exists())
116e996fd30SGreg Clayton             resolved_exe_file.ResolveExecutableLocation ();
117e996fd30SGreg Clayton 
11828041352SGreg Clayton         if (resolved_exe_file.Exists())
11928041352SGreg Clayton             error.Clear();
12028041352SGreg Clayton         else
12128041352SGreg Clayton         {
12228041352SGreg Clayton             exe_file.GetPath(exe_path, sizeof(exe_path));
12328041352SGreg Clayton             error.SetErrorStringWithFormat("unable to find executable for '%s'", exe_path);
12428041352SGreg Clayton         }
12528041352SGreg Clayton     }
12628041352SGreg Clayton     else
12728041352SGreg Clayton     {
12828041352SGreg Clayton         if (m_remote_platform_sp)
12928041352SGreg Clayton         {
13028041352SGreg Clayton             error = m_remote_platform_sp->ResolveExecutable (exe_file,
13128041352SGreg Clayton                                                              exe_arch,
1320c90ef47SGreg Clayton                                                              exe_module_sp,
1330c90ef47SGreg Clayton                                                              NULL);
13428041352SGreg Clayton         }
13528041352SGreg Clayton         else
13628041352SGreg Clayton         {
13728041352SGreg Clayton             // We may connect to a process and use the provided executable (Don't use local $PATH).
138e996fd30SGreg Clayton 
139e996fd30SGreg Clayton             if (resolved_exe_file.Exists())
14028041352SGreg Clayton                 error.Clear();
14128041352SGreg Clayton             else
14228041352SGreg Clayton                 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path);
14328041352SGreg Clayton         }
14428041352SGreg Clayton     }
14528041352SGreg Clayton 
14628041352SGreg Clayton     if (error.Success())
147e996fd30SGreg Clayton     {
148*ea5e0cc3SGreg Clayton         ModuleSpec module_spec (resolved_exe_file, exe_arch);
149e996fd30SGreg Clayton         if (exe_arch.IsValid())
150e996fd30SGreg Clayton         {
151*ea5e0cc3SGreg Clayton             error = ModuleList::GetSharedModule (module_spec,
152e996fd30SGreg Clayton                                                  exe_module_sp,
153e996fd30SGreg Clayton                                                  NULL,
1540c90ef47SGreg Clayton                                                  NULL,
155e996fd30SGreg Clayton                                                  NULL);
156e996fd30SGreg Clayton 
157e996fd30SGreg Clayton             if (exe_module_sp->GetObjectFile() == NULL)
158e996fd30SGreg Clayton             {
159e996fd30SGreg Clayton                 exe_module_sp.reset();
160e996fd30SGreg Clayton                 error.SetErrorStringWithFormat ("'%s%s%s' doesn't contain the architecture %s",
161e996fd30SGreg Clayton                                                 exe_file.GetDirectory().AsCString(""),
162e996fd30SGreg Clayton                                                 exe_file.GetDirectory() ? "/" : "",
163e996fd30SGreg Clayton                                                 exe_file.GetFilename().AsCString(""),
164e996fd30SGreg Clayton                                                 exe_arch.GetArchitectureName());
165e996fd30SGreg Clayton             }
166e996fd30SGreg Clayton         }
167e996fd30SGreg Clayton         else
168e996fd30SGreg Clayton         {
169e996fd30SGreg Clayton             // No valid architecture was specified, ask the platform for
170e996fd30SGreg Clayton             // the architectures that we should be using (in the correct order)
171e996fd30SGreg Clayton             // and see if we can find a match that way
172e996fd30SGreg Clayton             StreamString arch_names;
173*ea5e0cc3SGreg Clayton             for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, module_spec.GetArchitecture()); ++idx)
174e996fd30SGreg Clayton             {
175*ea5e0cc3SGreg Clayton                 error = ModuleList::GetSharedModule (module_spec,
176e996fd30SGreg Clayton                                                      exe_module_sp,
177e996fd30SGreg Clayton                                                      NULL,
1780c90ef47SGreg Clayton                                                      NULL,
179e996fd30SGreg Clayton                                                      NULL);
180e996fd30SGreg Clayton                 // Did we find an executable using one of the
181e996fd30SGreg Clayton                 if (error.Success())
182e996fd30SGreg Clayton                 {
183e996fd30SGreg Clayton                     if (exe_module_sp && exe_module_sp->GetObjectFile())
184e996fd30SGreg Clayton                         break;
185e996fd30SGreg Clayton                     else
186e996fd30SGreg Clayton                         error.SetErrorToGenericError();
187e996fd30SGreg Clayton                 }
188e996fd30SGreg Clayton 
189e996fd30SGreg Clayton                 if (idx > 0)
190e996fd30SGreg Clayton                     arch_names.PutCString (", ");
191*ea5e0cc3SGreg Clayton                 arch_names.PutCString (module_spec.GetArchitecture().GetArchitectureName());
192e996fd30SGreg Clayton             }
193e996fd30SGreg Clayton 
194e996fd30SGreg Clayton             if (error.Fail() || !exe_module_sp)
195e996fd30SGreg Clayton             {
196e996fd30SGreg Clayton                 error.SetErrorStringWithFormat ("'%s%s%s' doesn't contain any '%s' platform architectures: %s",
197e996fd30SGreg Clayton                                                 exe_file.GetDirectory().AsCString(""),
198e996fd30SGreg Clayton                                                 exe_file.GetDirectory() ? "/" : "",
199e996fd30SGreg Clayton                                                 exe_file.GetFilename().AsCString(""),
200e996fd30SGreg Clayton                                                 GetShortPluginName(),
201e996fd30SGreg Clayton                                                 arch_names.GetString().c_str());
202e996fd30SGreg Clayton             }
203e996fd30SGreg Clayton         }
204e996fd30SGreg Clayton     }
205e996fd30SGreg Clayton 
206e996fd30SGreg Clayton     return error;
207e996fd30SGreg Clayton }
208e996fd30SGreg Clayton 
209e996fd30SGreg Clayton Error
21078decfd0SStephen Wilson PlatformLinux::GetFile (const FileSpec &platform_file,
21128041352SGreg Clayton                         const UUID *uuid_ptr, FileSpec &local_file)
212e996fd30SGreg Clayton {
21328041352SGreg Clayton     if (IsRemote())
21428041352SGreg Clayton     {
21528041352SGreg Clayton         if (m_remote_platform_sp)
21628041352SGreg Clayton             return m_remote_platform_sp->GetFile (platform_file, uuid_ptr, local_file);
21728041352SGreg Clayton     }
21828041352SGreg Clayton 
219e996fd30SGreg Clayton     // Default to the local case
220e996fd30SGreg Clayton     local_file = platform_file;
221e996fd30SGreg Clayton     return Error();
222e996fd30SGreg Clayton }
223e996fd30SGreg Clayton 
224e996fd30SGreg Clayton 
225e996fd30SGreg Clayton //------------------------------------------------------------------
226e996fd30SGreg Clayton /// Default Constructor
227e996fd30SGreg Clayton //------------------------------------------------------------------
22828041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) :
22928041352SGreg Clayton     Platform(is_host),  // This is the local host platform
23028041352SGreg Clayton     m_remote_platform_sp ()
231e996fd30SGreg Clayton {
232e996fd30SGreg Clayton }
233e996fd30SGreg Clayton 
234e996fd30SGreg Clayton //------------------------------------------------------------------
235e996fd30SGreg Clayton /// Destructor.
236e996fd30SGreg Clayton ///
237e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be
238e996fd30SGreg Clayton /// inherited from by the plug-in instance.
239e996fd30SGreg Clayton //------------------------------------------------------------------
240e996fd30SGreg Clayton PlatformLinux::~PlatformLinux()
241e996fd30SGreg Clayton {
242e996fd30SGreg Clayton }
243e996fd30SGreg Clayton 
244e996fd30SGreg Clayton bool
24513e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
246e996fd30SGreg Clayton {
24728041352SGreg Clayton     bool success = false;
24828041352SGreg Clayton     if (IsHost())
24928041352SGreg Clayton     {
25028041352SGreg Clayton         success = Platform::GetProcessInfo (pid, process_info);
25128041352SGreg Clayton     }
25228041352SGreg Clayton     else
25328041352SGreg Clayton     {
25428041352SGreg Clayton         if (m_remote_platform_sp)
25528041352SGreg Clayton             success = m_remote_platform_sp->GetProcessInfo (pid, process_info);
25628041352SGreg Clayton     }
25728041352SGreg Clayton     return success;
258e996fd30SGreg Clayton }
259e996fd30SGreg Clayton 
260e996fd30SGreg Clayton bool
261e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
262e996fd30SGreg Clayton {
263e996fd30SGreg Clayton     if (idx == 0)
264e996fd30SGreg Clayton     {
265e996fd30SGreg Clayton         arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
266e996fd30SGreg Clayton         return arch.IsValid();
267e996fd30SGreg Clayton     }
268e996fd30SGreg Clayton     return false;
269e996fd30SGreg Clayton }
270ecc11474SStephen Wilson 
271ecc11474SStephen Wilson void
272ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm)
273ecc11474SStephen Wilson {
274ecc11474SStephen Wilson     struct utsname un;
275ecc11474SStephen Wilson 
276ecc11474SStephen Wilson     if (uname(&un)) {
277ecc11474SStephen Wilson         strm << "Linux";
278ecc11474SStephen Wilson         return;
279ecc11474SStephen Wilson     }
280ecc11474SStephen Wilson 
281ecc11474SStephen Wilson     strm << un.sysname << ' ' << un.release << ' ' << un.version << '\n';
282ecc11474SStephen Wilson }
283ecc11474SStephen Wilson 
284ecc11474SStephen Wilson size_t
285ecc11474SStephen Wilson PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target,
286ecc11474SStephen Wilson                                                 BreakpointSite *bp_site)
287ecc11474SStephen Wilson {
288ecc11474SStephen Wilson     ArchSpec arch = target.GetArchitecture();
28928041352SGreg Clayton     const uint8_t *trap_opcode = NULL;
29028041352SGreg Clayton     size_t trap_opcode_size = 0;
291ecc11474SStephen Wilson 
292ecc11474SStephen Wilson     switch (arch.GetCore())
293ecc11474SStephen Wilson     {
294ecc11474SStephen Wilson     default:
295ecc11474SStephen Wilson         assert(false && "CPU type not supported!");
296ecc11474SStephen Wilson         break;
297ecc11474SStephen Wilson 
298ecc11474SStephen Wilson     case ArchSpec::eCore_x86_32_i386:
299ecc11474SStephen Wilson     case ArchSpec::eCore_x86_64_x86_64:
30028041352SGreg Clayton         {
30128041352SGreg Clayton             static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
30228041352SGreg Clayton             trap_opcode = g_i386_breakpoint_opcode;
30328041352SGreg Clayton             trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
30428041352SGreg Clayton         }
305ecc11474SStephen Wilson         break;
306ecc11474SStephen Wilson     }
307ecc11474SStephen Wilson 
30828041352SGreg Clayton     if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
30928041352SGreg Clayton         return trap_opcode_size;
31028041352SGreg Clayton     return 0;
31128041352SGreg Clayton }
31228041352SGreg Clayton 
31328041352SGreg Clayton Error
31428041352SGreg Clayton PlatformLinux::LaunchProcess (ProcessLaunchInfo &launch_info)
31528041352SGreg Clayton {
31628041352SGreg Clayton     Error error;
31728041352SGreg Clayton 
31828041352SGreg Clayton     if (IsHost())
31928041352SGreg Clayton     {
32028041352SGreg Clayton         if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
32128041352SGreg Clayton         {
32228041352SGreg Clayton             const bool is_localhost = true;
32328041352SGreg Clayton             if (!launch_info.ConvertArgumentsForLaunchingInShell (error, is_localhost))
32428041352SGreg Clayton                 return error;
32528041352SGreg Clayton         }
32628041352SGreg Clayton         error = Platform::LaunchProcess (launch_info);
32728041352SGreg Clayton     }
32828041352SGreg Clayton     else
32928041352SGreg Clayton     {
33028041352SGreg Clayton         error.SetErrorString ("the platform is not currently connected");
33128041352SGreg Clayton     }
33228041352SGreg Clayton     return error;
333ecc11474SStephen Wilson }
33413e8e1c3SJohnny Chen 
33513e8e1c3SJohnny Chen lldb::ProcessSP
336fb2b629dSPeter Collingbourne PlatformLinux::Attach(ProcessAttachInfo &attach_info,
33713e8e1c3SJohnny Chen                       Debugger &debugger,
33813e8e1c3SJohnny Chen                       Target *target,
33913e8e1c3SJohnny Chen                       Listener &listener,
34013e8e1c3SJohnny Chen                       Error &error)
34113e8e1c3SJohnny Chen {
34228041352SGreg Clayton     lldb::ProcessSP process_sp;
34328041352SGreg Clayton     if (IsHost())
34428041352SGreg Clayton     {
34528041352SGreg Clayton         if (target == NULL)
34628041352SGreg Clayton         {
34728041352SGreg Clayton             TargetSP new_target_sp;
34828041352SGreg Clayton             FileSpec emptyFileSpec;
34928041352SGreg Clayton             ArchSpec emptyArchSpec;
35028041352SGreg Clayton 
35128041352SGreg Clayton             error = debugger.GetTargetList().CreateTarget (debugger,
35228041352SGreg Clayton                                                            emptyFileSpec,
35328041352SGreg Clayton                                                            emptyArchSpec,
35428041352SGreg Clayton                                                            false,
35528041352SGreg Clayton                                                            m_remote_platform_sp,
35628041352SGreg Clayton                                                            new_target_sp);
35728041352SGreg Clayton             target = new_target_sp.get();
35828041352SGreg Clayton         }
35928041352SGreg Clayton         else
36028041352SGreg Clayton             error.Clear();
36128041352SGreg Clayton 
36228041352SGreg Clayton         if (target && error.Success())
36328041352SGreg Clayton         {
36428041352SGreg Clayton             debugger.GetTargetList().SetSelectedTarget(target);
36528041352SGreg Clayton 
3660c90ef47SGreg Clayton             process_sp = target->CreateProcess (listener,
3670c90ef47SGreg Clayton                                                 attach_info.GetProcessPluginName(),
3680c90ef47SGreg Clayton                                                 NULL);
36928041352SGreg Clayton 
37028041352SGreg Clayton             if (process_sp)
37128041352SGreg Clayton                 error = process_sp->Attach (attach_info);
37228041352SGreg Clayton         }
37328041352SGreg Clayton     }
37428041352SGreg Clayton     else
37528041352SGreg Clayton     {
37628041352SGreg Clayton         if (m_remote_platform_sp)
37728041352SGreg Clayton             process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, listener, error);
37828041352SGreg Clayton         else
37928041352SGreg Clayton             error.SetErrorString ("the platform is not currently connected");
38028041352SGreg Clayton     }
38128041352SGreg Clayton     return process_sp;
38213e8e1c3SJohnny Chen }
383