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"
20*28041352SGreg 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 
33*28041352SGreg Clayton static uint32_t g_initialize_count = 0;
34*28041352SGreg Clayton 
35ecc11474SStephen Wilson Platform *
36ecc11474SStephen Wilson PlatformLinux::CreateInstance ()
37ecc11474SStephen Wilson {
38*28041352SGreg 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 *
48*28041352SGreg Clayton PlatformLinux::GetShortPluginNameStatic (bool is_host)
49ecc11474SStephen Wilson {
50*28041352SGreg Clayton     if (is_host)
51*28041352SGreg Clayton         return Platform::GetHostPlatformName ();
52*28041352SGreg Clayton     else
53*28041352SGreg Clayton         return "remote-linux";
54*28041352SGreg Clayton }
55*28041352SGreg Clayton 
56*28041352SGreg Clayton const char *
57*28041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic (bool is_host)
58*28041352SGreg Clayton {
59*28041352SGreg Clayton     if (is_host)
60*28041352SGreg Clayton         return "Local Linux user platform plug-in.";
61*28041352SGreg Clayton     else
62*28041352SGreg Clayton         return "Remote Linux user platform plug-in.";
63ecc11474SStephen Wilson }
64ecc11474SStephen Wilson 
65e996fd30SGreg Clayton void
66e996fd30SGreg Clayton PlatformLinux::Initialize ()
67e996fd30SGreg Clayton {
68*28041352SGreg Clayton     if (g_initialize_count++ == 0)
69ecc11474SStephen Wilson     {
70*28041352SGreg Clayton #if defined(__linux__)
71*28041352SGreg Clayton         PlatformSP default_platform_sp (new PlatformLinux(true));
72*28041352SGreg Clayton         default_platform_sp->SetSystemArchitecture (Host::GetArchitecture());
73e996fd30SGreg Clayton         Platform::SetDefaultPlatform (default_platform_sp);
74*28041352SGreg Clayton #endif
75*28041352SGreg Clayton         PluginManager::RegisterPlugin(PlatformLinux::GetShortPluginNameStatic(false),
76*28041352SGreg Clayton                                       PlatformLinux::GetPluginDescriptionStatic(false),
77*28041352SGreg Clayton                                       PlatformLinux::CreateInstance);
78ecc11474SStephen Wilson     }
79e996fd30SGreg Clayton }
80e996fd30SGreg Clayton 
81e996fd30SGreg Clayton void
82e996fd30SGreg Clayton PlatformLinux::Terminate ()
83e996fd30SGreg Clayton {
84*28041352SGreg Clayton     if (g_initialize_count > 0)
85*28041352SGreg Clayton     {
86*28041352SGreg Clayton         if (--g_initialize_count == 0)
87*28041352SGreg Clayton         {
88*28041352SGreg Clayton             PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance);
89e996fd30SGreg Clayton         }
90*28041352SGreg Clayton     }
91*28041352SGreg 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 
101*28041352SGreg Clayton     char exe_path[PATH_MAX];
102e996fd30SGreg Clayton     FileSpec resolved_exe_file (exe_file);
103e996fd30SGreg Clayton 
104*28041352SGreg Clayton     if (IsHost())
105*28041352SGreg Clayton     {
106*28041352SGreg 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())
109*28041352SGreg Clayton         {
110*28041352SGreg Clayton             exe_file.GetPath(exe_path, sizeof(exe_path));
111*28041352SGreg Clayton             resolved_exe_file.SetFile(exe_path, true);
112*28041352SGreg Clayton         }
113*28041352SGreg Clayton 
114*28041352SGreg Clayton         if (!resolved_exe_file.Exists())
115e996fd30SGreg Clayton             resolved_exe_file.ResolveExecutableLocation ();
116e996fd30SGreg Clayton 
117*28041352SGreg Clayton         if (resolved_exe_file.Exists())
118*28041352SGreg Clayton             error.Clear();
119*28041352SGreg Clayton         else
120*28041352SGreg Clayton         {
121*28041352SGreg Clayton             exe_file.GetPath(exe_path, sizeof(exe_path));
122*28041352SGreg Clayton             error.SetErrorStringWithFormat("unable to find executable for '%s'", exe_path);
123*28041352SGreg Clayton         }
124*28041352SGreg Clayton     }
125*28041352SGreg Clayton     else
126*28041352SGreg Clayton     {
127*28041352SGreg Clayton         if (m_remote_platform_sp)
128*28041352SGreg Clayton         {
129*28041352SGreg Clayton             error = m_remote_platform_sp->ResolveExecutable (exe_file,
130*28041352SGreg Clayton                                                              exe_arch,
131*28041352SGreg Clayton                                                              exe_module_sp);
132*28041352SGreg Clayton         }
133*28041352SGreg Clayton         else
134*28041352SGreg Clayton         {
135*28041352SGreg Clayton             // We may connect to a process and use the provided executable (Don't use local $PATH).
136e996fd30SGreg Clayton 
137e996fd30SGreg Clayton             if (resolved_exe_file.Exists())
138*28041352SGreg Clayton                 error.Clear();
139*28041352SGreg Clayton             else
140*28041352SGreg Clayton                 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path);
141*28041352SGreg Clayton         }
142*28041352SGreg Clayton     }
143*28041352SGreg Clayton 
144*28041352SGreg Clayton     if (error.Success())
145e996fd30SGreg Clayton     {
146e996fd30SGreg Clayton         if (exe_arch.IsValid())
147e996fd30SGreg Clayton         {
148e996fd30SGreg Clayton             error = ModuleList::GetSharedModule (resolved_exe_file,
149e996fd30SGreg Clayton                                                  exe_arch,
150e996fd30SGreg Clayton                                                  NULL,
151e996fd30SGreg Clayton                                                  NULL,
152e996fd30SGreg Clayton                                                  0,
153e996fd30SGreg Clayton                                                  exe_module_sp,
154e996fd30SGreg 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;
173e996fd30SGreg Clayton             ArchSpec platform_arch;
174e996fd30SGreg Clayton             for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, platform_arch); ++idx)
175e996fd30SGreg Clayton             {
176e996fd30SGreg Clayton                 error = ModuleList::GetSharedModule (resolved_exe_file,
177e996fd30SGreg Clayton                                                      platform_arch,
178e996fd30SGreg Clayton                                                      NULL,
179e996fd30SGreg Clayton                                                      NULL,
180e996fd30SGreg Clayton                                                      0,
181e996fd30SGreg Clayton                                                      exe_module_sp,
182e996fd30SGreg Clayton                                                      NULL,
183e996fd30SGreg Clayton                                                      NULL);
184e996fd30SGreg Clayton                 // Did we find an executable using one of the
185e996fd30SGreg Clayton                 if (error.Success())
186e996fd30SGreg Clayton                 {
187e996fd30SGreg Clayton                     if (exe_module_sp && exe_module_sp->GetObjectFile())
188e996fd30SGreg Clayton                         break;
189e996fd30SGreg Clayton                     else
190e996fd30SGreg Clayton                         error.SetErrorToGenericError();
191e996fd30SGreg Clayton                 }
192e996fd30SGreg Clayton 
193e996fd30SGreg Clayton                 if (idx > 0)
194e996fd30SGreg Clayton                     arch_names.PutCString (", ");
195e996fd30SGreg Clayton                 arch_names.PutCString (platform_arch.GetArchitectureName());
196e996fd30SGreg Clayton             }
197e996fd30SGreg Clayton 
198e996fd30SGreg Clayton             if (error.Fail() || !exe_module_sp)
199e996fd30SGreg Clayton             {
200e996fd30SGreg Clayton                 error.SetErrorStringWithFormat ("'%s%s%s' doesn't contain any '%s' platform architectures: %s",
201e996fd30SGreg Clayton                                                 exe_file.GetDirectory().AsCString(""),
202e996fd30SGreg Clayton                                                 exe_file.GetDirectory() ? "/" : "",
203e996fd30SGreg Clayton                                                 exe_file.GetFilename().AsCString(""),
204e996fd30SGreg Clayton                                                 GetShortPluginName(),
205e996fd30SGreg Clayton                                                 arch_names.GetString().c_str());
206e996fd30SGreg Clayton             }
207e996fd30SGreg Clayton         }
208e996fd30SGreg Clayton     }
209e996fd30SGreg Clayton 
210e996fd30SGreg Clayton     return error;
211e996fd30SGreg Clayton }
212e996fd30SGreg Clayton 
213e996fd30SGreg Clayton Error
21478decfd0SStephen Wilson PlatformLinux::GetFile (const FileSpec &platform_file,
215*28041352SGreg Clayton                         const UUID *uuid_ptr, FileSpec &local_file)
216e996fd30SGreg Clayton {
217*28041352SGreg Clayton     if (IsRemote())
218*28041352SGreg Clayton     {
219*28041352SGreg Clayton         if (m_remote_platform_sp)
220*28041352SGreg Clayton             return m_remote_platform_sp->GetFile (platform_file, uuid_ptr, local_file);
221*28041352SGreg Clayton     }
222*28041352SGreg Clayton 
223e996fd30SGreg Clayton     // Default to the local case
224e996fd30SGreg Clayton     local_file = platform_file;
225e996fd30SGreg Clayton     return Error();
226e996fd30SGreg Clayton }
227e996fd30SGreg Clayton 
228e996fd30SGreg Clayton 
229e996fd30SGreg Clayton //------------------------------------------------------------------
230e996fd30SGreg Clayton /// Default Constructor
231e996fd30SGreg Clayton //------------------------------------------------------------------
232*28041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) :
233*28041352SGreg Clayton     Platform(is_host),  // This is the local host platform
234*28041352SGreg Clayton     m_remote_platform_sp ()
235e996fd30SGreg Clayton {
236e996fd30SGreg Clayton }
237e996fd30SGreg Clayton 
238e996fd30SGreg Clayton //------------------------------------------------------------------
239e996fd30SGreg Clayton /// Destructor.
240e996fd30SGreg Clayton ///
241e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be
242e996fd30SGreg Clayton /// inherited from by the plug-in instance.
243e996fd30SGreg Clayton //------------------------------------------------------------------
244e996fd30SGreg Clayton PlatformLinux::~PlatformLinux()
245e996fd30SGreg Clayton {
246e996fd30SGreg Clayton }
247e996fd30SGreg Clayton 
248e996fd30SGreg Clayton bool
24913e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
250e996fd30SGreg Clayton {
251*28041352SGreg Clayton     bool success = false;
252*28041352SGreg Clayton     if (IsHost())
253*28041352SGreg Clayton     {
254*28041352SGreg Clayton         success = Platform::GetProcessInfo (pid, process_info);
255*28041352SGreg Clayton     }
256*28041352SGreg Clayton     else
257*28041352SGreg Clayton     {
258*28041352SGreg Clayton         if (m_remote_platform_sp)
259*28041352SGreg Clayton             success = m_remote_platform_sp->GetProcessInfo (pid, process_info);
260*28041352SGreg Clayton     }
261*28041352SGreg Clayton     return success;
262e996fd30SGreg Clayton }
263e996fd30SGreg Clayton 
264e996fd30SGreg Clayton bool
265e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
266e996fd30SGreg Clayton {
267e996fd30SGreg Clayton     if (idx == 0)
268e996fd30SGreg Clayton     {
269e996fd30SGreg Clayton         arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
270e996fd30SGreg Clayton         return arch.IsValid();
271e996fd30SGreg Clayton     }
272e996fd30SGreg Clayton     return false;
273e996fd30SGreg Clayton }
274ecc11474SStephen Wilson 
275ecc11474SStephen Wilson void
276ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm)
277ecc11474SStephen Wilson {
278ecc11474SStephen Wilson     struct utsname un;
279ecc11474SStephen Wilson 
280ecc11474SStephen Wilson     if (uname(&un)) {
281ecc11474SStephen Wilson         strm << "Linux";
282ecc11474SStephen Wilson         return;
283ecc11474SStephen Wilson     }
284ecc11474SStephen Wilson 
285ecc11474SStephen Wilson     strm << un.sysname << ' ' << un.release << ' ' << un.version << '\n';
286ecc11474SStephen Wilson }
287ecc11474SStephen Wilson 
288ecc11474SStephen Wilson size_t
289ecc11474SStephen Wilson PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target,
290ecc11474SStephen Wilson                                                 BreakpointSite *bp_site)
291ecc11474SStephen Wilson {
292ecc11474SStephen Wilson     ArchSpec arch = target.GetArchitecture();
293*28041352SGreg Clayton     const uint8_t *trap_opcode = NULL;
294*28041352SGreg Clayton     size_t trap_opcode_size = 0;
295ecc11474SStephen Wilson 
296ecc11474SStephen Wilson     switch (arch.GetCore())
297ecc11474SStephen Wilson     {
298ecc11474SStephen Wilson     default:
299ecc11474SStephen Wilson         assert(false && "CPU type not supported!");
300ecc11474SStephen Wilson         break;
301ecc11474SStephen Wilson 
302ecc11474SStephen Wilson     case ArchSpec::eCore_x86_32_i386:
303ecc11474SStephen Wilson     case ArchSpec::eCore_x86_64_x86_64:
304*28041352SGreg Clayton         {
305*28041352SGreg Clayton             static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
306*28041352SGreg Clayton             trap_opcode = g_i386_breakpoint_opcode;
307*28041352SGreg Clayton             trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
308*28041352SGreg Clayton         }
309ecc11474SStephen Wilson         break;
310ecc11474SStephen Wilson     }
311ecc11474SStephen Wilson 
312*28041352SGreg Clayton     if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
313*28041352SGreg Clayton         return trap_opcode_size;
314*28041352SGreg Clayton     return 0;
315*28041352SGreg Clayton }
316*28041352SGreg Clayton 
317*28041352SGreg Clayton Error
318*28041352SGreg Clayton PlatformLinux::LaunchProcess (ProcessLaunchInfo &launch_info)
319*28041352SGreg Clayton {
320*28041352SGreg Clayton     Error error;
321*28041352SGreg Clayton 
322*28041352SGreg Clayton     if (IsHost())
323*28041352SGreg Clayton     {
324*28041352SGreg Clayton         if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
325*28041352SGreg Clayton         {
326*28041352SGreg Clayton             const bool is_localhost = true;
327*28041352SGreg Clayton             if (!launch_info.ConvertArgumentsForLaunchingInShell (error, is_localhost))
328*28041352SGreg Clayton                 return error;
329*28041352SGreg Clayton         }
330*28041352SGreg Clayton         error = Platform::LaunchProcess (launch_info);
331*28041352SGreg Clayton     }
332*28041352SGreg Clayton     else
333*28041352SGreg Clayton     {
334*28041352SGreg Clayton         error.SetErrorString ("the platform is not currently connected");
335*28041352SGreg Clayton     }
336*28041352SGreg Clayton     return error;
337ecc11474SStephen Wilson }
33813e8e1c3SJohnny Chen 
33913e8e1c3SJohnny Chen lldb::ProcessSP
340fb2b629dSPeter Collingbourne PlatformLinux::Attach(ProcessAttachInfo &attach_info,
34113e8e1c3SJohnny Chen                       Debugger &debugger,
34213e8e1c3SJohnny Chen                       Target *target,
34313e8e1c3SJohnny Chen                       Listener &listener,
34413e8e1c3SJohnny Chen                       Error &error)
34513e8e1c3SJohnny Chen {
346*28041352SGreg Clayton     lldb::ProcessSP process_sp;
347*28041352SGreg Clayton     if (IsHost())
348*28041352SGreg Clayton     {
349*28041352SGreg Clayton         if (target == NULL)
350*28041352SGreg Clayton         {
351*28041352SGreg Clayton             TargetSP new_target_sp;
352*28041352SGreg Clayton             FileSpec emptyFileSpec;
353*28041352SGreg Clayton             ArchSpec emptyArchSpec;
354*28041352SGreg Clayton 
355*28041352SGreg Clayton             error = debugger.GetTargetList().CreateTarget (debugger,
356*28041352SGreg Clayton                                                            emptyFileSpec,
357*28041352SGreg Clayton                                                            emptyArchSpec,
358*28041352SGreg Clayton                                                            false,
359*28041352SGreg Clayton                                                            m_remote_platform_sp,
360*28041352SGreg Clayton                                                            new_target_sp);
361*28041352SGreg Clayton             target = new_target_sp.get();
362*28041352SGreg Clayton         }
363*28041352SGreg Clayton         else
364*28041352SGreg Clayton             error.Clear();
365*28041352SGreg Clayton 
366*28041352SGreg Clayton         if (target && error.Success())
367*28041352SGreg Clayton         {
368*28041352SGreg Clayton             debugger.GetTargetList().SetSelectedTarget(target);
369*28041352SGreg Clayton 
370*28041352SGreg Clayton             process_sp = target->CreateProcess (listener, attach_info.GetProcessPluginName());
371*28041352SGreg Clayton 
372*28041352SGreg Clayton             if (process_sp)
373*28041352SGreg Clayton                 error = process_sp->Attach (attach_info);
374*28041352SGreg Clayton         }
375*28041352SGreg Clayton     }
376*28041352SGreg Clayton     else
377*28041352SGreg Clayton     {
378*28041352SGreg Clayton         if (m_remote_platform_sp)
379*28041352SGreg Clayton             process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, listener, error);
380*28041352SGreg Clayton         else
381*28041352SGreg Clayton             error.SetErrorString ("the platform is not currently connected");
382*28041352SGreg Clayton     }
383*28041352SGreg Clayton     return process_sp;
38413e8e1c3SJohnny Chen }
385