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,
96e996fd30SGreg Clayton                                   lldb::ModuleSP &exe_module_sp)
97e996fd30SGreg Clayton {
98e996fd30SGreg Clayton     Error error;
99e996fd30SGreg Clayton     // Nothing special to do here, just use the actual file and architecture
100e996fd30SGreg Clayton 
10128041352SGreg Clayton     char exe_path[PATH_MAX];
102e996fd30SGreg Clayton     FileSpec resolved_exe_file (exe_file);
103e996fd30SGreg Clayton 
10428041352SGreg Clayton     if (IsHost())
10528041352SGreg Clayton     {
10628041352SGreg Clayton         // If we have "ls" as the exe_file, resolve the executable location based on
107e996fd30SGreg Clayton         // the current path variables
108e996fd30SGreg Clayton         if (!resolved_exe_file.Exists())
10928041352SGreg Clayton         {
11028041352SGreg Clayton             exe_file.GetPath(exe_path, sizeof(exe_path));
11128041352SGreg Clayton             resolved_exe_file.SetFile(exe_path, true);
11228041352SGreg Clayton         }
11328041352SGreg Clayton 
11428041352SGreg Clayton         if (!resolved_exe_file.Exists())
115e996fd30SGreg Clayton             resolved_exe_file.ResolveExecutableLocation ();
116e996fd30SGreg Clayton 
11728041352SGreg Clayton         if (resolved_exe_file.Exists())
11828041352SGreg Clayton             error.Clear();
11928041352SGreg Clayton         else
12028041352SGreg Clayton         {
12128041352SGreg Clayton             exe_file.GetPath(exe_path, sizeof(exe_path));
12228041352SGreg Clayton             error.SetErrorStringWithFormat("unable to find executable for '%s'", exe_path);
12328041352SGreg Clayton         }
12428041352SGreg Clayton     }
12528041352SGreg Clayton     else
12628041352SGreg Clayton     {
12728041352SGreg Clayton         if (m_remote_platform_sp)
12828041352SGreg Clayton         {
12928041352SGreg Clayton             error = m_remote_platform_sp->ResolveExecutable (exe_file,
13028041352SGreg Clayton                                                              exe_arch,
131*0c90ef47SGreg Clayton                                                              exe_module_sp,
132*0c90ef47SGreg Clayton                                                              NULL);
13328041352SGreg Clayton         }
13428041352SGreg Clayton         else
13528041352SGreg Clayton         {
13628041352SGreg Clayton             // We may connect to a process and use the provided executable (Don't use local $PATH).
137e996fd30SGreg Clayton 
138e996fd30SGreg Clayton             if (resolved_exe_file.Exists())
13928041352SGreg Clayton                 error.Clear();
14028041352SGreg Clayton             else
14128041352SGreg Clayton                 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path);
14228041352SGreg Clayton         }
14328041352SGreg Clayton     }
14428041352SGreg Clayton 
14528041352SGreg Clayton     if (error.Success())
146e996fd30SGreg Clayton     {
147e996fd30SGreg Clayton         if (exe_arch.IsValid())
148e996fd30SGreg Clayton         {
149e996fd30SGreg Clayton             error = ModuleList::GetSharedModule (resolved_exe_file,
150e996fd30SGreg Clayton                                                  exe_arch,
151e996fd30SGreg Clayton                                                  NULL,
152e996fd30SGreg Clayton                                                  NULL,
153e996fd30SGreg Clayton                                                  0,
154e996fd30SGreg Clayton                                                  exe_module_sp,
155e996fd30SGreg Clayton                                                  NULL,
156*0c90ef47SGreg Clayton                                                  NULL,
157e996fd30SGreg Clayton                                                  NULL);
158e996fd30SGreg Clayton 
159e996fd30SGreg Clayton             if (exe_module_sp->GetObjectFile() == NULL)
160e996fd30SGreg Clayton             {
161e996fd30SGreg Clayton                 exe_module_sp.reset();
162e996fd30SGreg Clayton                 error.SetErrorStringWithFormat ("'%s%s%s' doesn't contain the architecture %s",
163e996fd30SGreg Clayton                                                 exe_file.GetDirectory().AsCString(""),
164e996fd30SGreg Clayton                                                 exe_file.GetDirectory() ? "/" : "",
165e996fd30SGreg Clayton                                                 exe_file.GetFilename().AsCString(""),
166e996fd30SGreg Clayton                                                 exe_arch.GetArchitectureName());
167e996fd30SGreg Clayton             }
168e996fd30SGreg Clayton         }
169e996fd30SGreg Clayton         else
170e996fd30SGreg Clayton         {
171e996fd30SGreg Clayton             // No valid architecture was specified, ask the platform for
172e996fd30SGreg Clayton             // the architectures that we should be using (in the correct order)
173e996fd30SGreg Clayton             // and see if we can find a match that way
174e996fd30SGreg Clayton             StreamString arch_names;
175e996fd30SGreg Clayton             ArchSpec platform_arch;
176e996fd30SGreg Clayton             for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, platform_arch); ++idx)
177e996fd30SGreg Clayton             {
178e996fd30SGreg Clayton                 error = ModuleList::GetSharedModule (resolved_exe_file,
179e996fd30SGreg Clayton                                                      platform_arch,
180e996fd30SGreg Clayton                                                      NULL,
181e996fd30SGreg Clayton                                                      NULL,
182e996fd30SGreg Clayton                                                      0,
183e996fd30SGreg Clayton                                                      exe_module_sp,
184e996fd30SGreg Clayton                                                      NULL,
185*0c90ef47SGreg Clayton                                                      NULL,
186e996fd30SGreg Clayton                                                      NULL);
187e996fd30SGreg Clayton                 // Did we find an executable using one of the
188e996fd30SGreg Clayton                 if (error.Success())
189e996fd30SGreg Clayton                 {
190e996fd30SGreg Clayton                     if (exe_module_sp && exe_module_sp->GetObjectFile())
191e996fd30SGreg Clayton                         break;
192e996fd30SGreg Clayton                     else
193e996fd30SGreg Clayton                         error.SetErrorToGenericError();
194e996fd30SGreg Clayton                 }
195e996fd30SGreg Clayton 
196e996fd30SGreg Clayton                 if (idx > 0)
197e996fd30SGreg Clayton                     arch_names.PutCString (", ");
198e996fd30SGreg Clayton                 arch_names.PutCString (platform_arch.GetArchitectureName());
199e996fd30SGreg Clayton             }
200e996fd30SGreg Clayton 
201e996fd30SGreg Clayton             if (error.Fail() || !exe_module_sp)
202e996fd30SGreg Clayton             {
203e996fd30SGreg Clayton                 error.SetErrorStringWithFormat ("'%s%s%s' doesn't contain any '%s' platform architectures: %s",
204e996fd30SGreg Clayton                                                 exe_file.GetDirectory().AsCString(""),
205e996fd30SGreg Clayton                                                 exe_file.GetDirectory() ? "/" : "",
206e996fd30SGreg Clayton                                                 exe_file.GetFilename().AsCString(""),
207e996fd30SGreg Clayton                                                 GetShortPluginName(),
208e996fd30SGreg Clayton                                                 arch_names.GetString().c_str());
209e996fd30SGreg Clayton             }
210e996fd30SGreg Clayton         }
211e996fd30SGreg Clayton     }
212e996fd30SGreg Clayton 
213e996fd30SGreg Clayton     return error;
214e996fd30SGreg Clayton }
215e996fd30SGreg Clayton 
216e996fd30SGreg Clayton Error
21778decfd0SStephen Wilson PlatformLinux::GetFile (const FileSpec &platform_file,
21828041352SGreg Clayton                         const UUID *uuid_ptr, FileSpec &local_file)
219e996fd30SGreg Clayton {
22028041352SGreg Clayton     if (IsRemote())
22128041352SGreg Clayton     {
22228041352SGreg Clayton         if (m_remote_platform_sp)
22328041352SGreg Clayton             return m_remote_platform_sp->GetFile (platform_file, uuid_ptr, local_file);
22428041352SGreg Clayton     }
22528041352SGreg Clayton 
226e996fd30SGreg Clayton     // Default to the local case
227e996fd30SGreg Clayton     local_file = platform_file;
228e996fd30SGreg Clayton     return Error();
229e996fd30SGreg Clayton }
230e996fd30SGreg Clayton 
231e996fd30SGreg Clayton 
232e996fd30SGreg Clayton //------------------------------------------------------------------
233e996fd30SGreg Clayton /// Default Constructor
234e996fd30SGreg Clayton //------------------------------------------------------------------
23528041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) :
23628041352SGreg Clayton     Platform(is_host),  // This is the local host platform
23728041352SGreg Clayton     m_remote_platform_sp ()
238e996fd30SGreg Clayton {
239e996fd30SGreg Clayton }
240e996fd30SGreg Clayton 
241e996fd30SGreg Clayton //------------------------------------------------------------------
242e996fd30SGreg Clayton /// Destructor.
243e996fd30SGreg Clayton ///
244e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be
245e996fd30SGreg Clayton /// inherited from by the plug-in instance.
246e996fd30SGreg Clayton //------------------------------------------------------------------
247e996fd30SGreg Clayton PlatformLinux::~PlatformLinux()
248e996fd30SGreg Clayton {
249e996fd30SGreg Clayton }
250e996fd30SGreg Clayton 
251e996fd30SGreg Clayton bool
25213e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
253e996fd30SGreg Clayton {
25428041352SGreg Clayton     bool success = false;
25528041352SGreg Clayton     if (IsHost())
25628041352SGreg Clayton     {
25728041352SGreg Clayton         success = Platform::GetProcessInfo (pid, process_info);
25828041352SGreg Clayton     }
25928041352SGreg Clayton     else
26028041352SGreg Clayton     {
26128041352SGreg Clayton         if (m_remote_platform_sp)
26228041352SGreg Clayton             success = m_remote_platform_sp->GetProcessInfo (pid, process_info);
26328041352SGreg Clayton     }
26428041352SGreg Clayton     return success;
265e996fd30SGreg Clayton }
266e996fd30SGreg Clayton 
267e996fd30SGreg Clayton bool
268e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
269e996fd30SGreg Clayton {
270e996fd30SGreg Clayton     if (idx == 0)
271e996fd30SGreg Clayton     {
272e996fd30SGreg Clayton         arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
273e996fd30SGreg Clayton         return arch.IsValid();
274e996fd30SGreg Clayton     }
275e996fd30SGreg Clayton     return false;
276e996fd30SGreg Clayton }
277ecc11474SStephen Wilson 
278ecc11474SStephen Wilson void
279ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm)
280ecc11474SStephen Wilson {
281ecc11474SStephen Wilson     struct utsname un;
282ecc11474SStephen Wilson 
283ecc11474SStephen Wilson     if (uname(&un)) {
284ecc11474SStephen Wilson         strm << "Linux";
285ecc11474SStephen Wilson         return;
286ecc11474SStephen Wilson     }
287ecc11474SStephen Wilson 
288ecc11474SStephen Wilson     strm << un.sysname << ' ' << un.release << ' ' << un.version << '\n';
289ecc11474SStephen Wilson }
290ecc11474SStephen Wilson 
291ecc11474SStephen Wilson size_t
292ecc11474SStephen Wilson PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target,
293ecc11474SStephen Wilson                                                 BreakpointSite *bp_site)
294ecc11474SStephen Wilson {
295ecc11474SStephen Wilson     ArchSpec arch = target.GetArchitecture();
29628041352SGreg Clayton     const uint8_t *trap_opcode = NULL;
29728041352SGreg Clayton     size_t trap_opcode_size = 0;
298ecc11474SStephen Wilson 
299ecc11474SStephen Wilson     switch (arch.GetCore())
300ecc11474SStephen Wilson     {
301ecc11474SStephen Wilson     default:
302ecc11474SStephen Wilson         assert(false && "CPU type not supported!");
303ecc11474SStephen Wilson         break;
304ecc11474SStephen Wilson 
305ecc11474SStephen Wilson     case ArchSpec::eCore_x86_32_i386:
306ecc11474SStephen Wilson     case ArchSpec::eCore_x86_64_x86_64:
30728041352SGreg Clayton         {
30828041352SGreg Clayton             static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
30928041352SGreg Clayton             trap_opcode = g_i386_breakpoint_opcode;
31028041352SGreg Clayton             trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
31128041352SGreg Clayton         }
312ecc11474SStephen Wilson         break;
313ecc11474SStephen Wilson     }
314ecc11474SStephen Wilson 
31528041352SGreg Clayton     if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
31628041352SGreg Clayton         return trap_opcode_size;
31728041352SGreg Clayton     return 0;
31828041352SGreg Clayton }
31928041352SGreg Clayton 
32028041352SGreg Clayton Error
32128041352SGreg Clayton PlatformLinux::LaunchProcess (ProcessLaunchInfo &launch_info)
32228041352SGreg Clayton {
32328041352SGreg Clayton     Error error;
32428041352SGreg Clayton 
32528041352SGreg Clayton     if (IsHost())
32628041352SGreg Clayton     {
32728041352SGreg Clayton         if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
32828041352SGreg Clayton         {
32928041352SGreg Clayton             const bool is_localhost = true;
33028041352SGreg Clayton             if (!launch_info.ConvertArgumentsForLaunchingInShell (error, is_localhost))
33128041352SGreg Clayton                 return error;
33228041352SGreg Clayton         }
33328041352SGreg Clayton         error = Platform::LaunchProcess (launch_info);
33428041352SGreg Clayton     }
33528041352SGreg Clayton     else
33628041352SGreg Clayton     {
33728041352SGreg Clayton         error.SetErrorString ("the platform is not currently connected");
33828041352SGreg Clayton     }
33928041352SGreg Clayton     return error;
340ecc11474SStephen Wilson }
34113e8e1c3SJohnny Chen 
34213e8e1c3SJohnny Chen lldb::ProcessSP
343fb2b629dSPeter Collingbourne PlatformLinux::Attach(ProcessAttachInfo &attach_info,
34413e8e1c3SJohnny Chen                       Debugger &debugger,
34513e8e1c3SJohnny Chen                       Target *target,
34613e8e1c3SJohnny Chen                       Listener &listener,
34713e8e1c3SJohnny Chen                       Error &error)
34813e8e1c3SJohnny Chen {
34928041352SGreg Clayton     lldb::ProcessSP process_sp;
35028041352SGreg Clayton     if (IsHost())
35128041352SGreg Clayton     {
35228041352SGreg Clayton         if (target == NULL)
35328041352SGreg Clayton         {
35428041352SGreg Clayton             TargetSP new_target_sp;
35528041352SGreg Clayton             FileSpec emptyFileSpec;
35628041352SGreg Clayton             ArchSpec emptyArchSpec;
35728041352SGreg Clayton 
35828041352SGreg Clayton             error = debugger.GetTargetList().CreateTarget (debugger,
35928041352SGreg Clayton                                                            emptyFileSpec,
36028041352SGreg Clayton                                                            emptyArchSpec,
36128041352SGreg Clayton                                                            false,
36228041352SGreg Clayton                                                            m_remote_platform_sp,
36328041352SGreg Clayton                                                            new_target_sp);
36428041352SGreg Clayton             target = new_target_sp.get();
36528041352SGreg Clayton         }
36628041352SGreg Clayton         else
36728041352SGreg Clayton             error.Clear();
36828041352SGreg Clayton 
36928041352SGreg Clayton         if (target && error.Success())
37028041352SGreg Clayton         {
37128041352SGreg Clayton             debugger.GetTargetList().SetSelectedTarget(target);
37228041352SGreg Clayton 
373*0c90ef47SGreg Clayton             process_sp = target->CreateProcess (listener,
374*0c90ef47SGreg Clayton                                                 attach_info.GetProcessPluginName(),
375*0c90ef47SGreg Clayton                                                 NULL);
37628041352SGreg Clayton 
37728041352SGreg Clayton             if (process_sp)
37828041352SGreg Clayton                 error = process_sp->Attach (attach_info);
37928041352SGreg Clayton         }
38028041352SGreg Clayton     }
38128041352SGreg Clayton     else
38228041352SGreg Clayton     {
38328041352SGreg Clayton         if (m_remote_platform_sp)
38428041352SGreg Clayton             process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, listener, error);
38528041352SGreg Clayton         else
38628041352SGreg Clayton             error.SetErrorString ("the platform is not currently connected");
38728041352SGreg Clayton     }
38828041352SGreg Clayton     return process_sp;
38913e8e1c3SJohnny Chen }
390