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" 23*1f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h" 24ecc11474SStephen Wilson #include "lldb/Core/PluginManager.h" 25e996fd30SGreg Clayton #include "lldb/Core/StreamString.h" 26e996fd30SGreg Clayton #include "lldb/Host/FileSpec.h" 27e996fd30SGreg Clayton #include "lldb/Host/Host.h" 28ecc11474SStephen Wilson #include "lldb/Target/Target.h" 29e996fd30SGreg Clayton #include "lldb/Target/Process.h" 30e996fd30SGreg Clayton 31e996fd30SGreg Clayton using namespace lldb; 32e996fd30SGreg Clayton using namespace lldb_private; 33e996fd30SGreg Clayton 3428041352SGreg Clayton static uint32_t g_initialize_count = 0; 3528041352SGreg Clayton 36ecc11474SStephen Wilson Platform * 37b3a40ba8SGreg Clayton PlatformLinux::CreateInstance (bool force, const ArchSpec *arch) 38ecc11474SStephen Wilson { 39b3a40ba8SGreg Clayton bool create = force; 40b3a40ba8SGreg Clayton if (create == false && arch && arch->IsValid()) 41b3a40ba8SGreg Clayton { 42b3a40ba8SGreg Clayton const llvm::Triple &triple = arch->GetTriple(); 4370512317SGreg Clayton switch (triple.getVendor()) 4470512317SGreg Clayton { 4570512317SGreg Clayton case llvm::Triple::PC: 46b3a40ba8SGreg Clayton create = true; 4770512317SGreg Clayton break; 4870512317SGreg Clayton 4970512317SGreg Clayton case llvm::Triple::UnknownArch: 5070512317SGreg Clayton create = !arch->TripleVendorWasSpecified(); 5170512317SGreg Clayton break; 5270512317SGreg Clayton 5370512317SGreg Clayton default: 5470512317SGreg Clayton break; 5570512317SGreg Clayton } 5670512317SGreg Clayton 5770512317SGreg Clayton if (create) 5870512317SGreg Clayton { 5970512317SGreg Clayton switch (triple.getOS()) 6070512317SGreg Clayton { 6170512317SGreg Clayton case llvm::Triple::Linux: 6270512317SGreg Clayton break; 6370512317SGreg Clayton 6470512317SGreg Clayton case llvm::Triple::UnknownOS: 6570512317SGreg Clayton create = !arch->TripleOSWasSpecified(); 6670512317SGreg Clayton break; 6770512317SGreg Clayton 6870512317SGreg Clayton default: 6970512317SGreg Clayton create = false; 7070512317SGreg Clayton break; 7170512317SGreg Clayton } 7270512317SGreg Clayton } 73b3a40ba8SGreg Clayton } 74b3a40ba8SGreg Clayton if (create) 7528041352SGreg Clayton return new PlatformLinux(true); 76b3a40ba8SGreg Clayton return NULL; 77ecc11474SStephen Wilson } 78ecc11474SStephen Wilson 79ecc11474SStephen Wilson const char * 80ecc11474SStephen Wilson PlatformLinux::GetPluginNameStatic() 81ecc11474SStephen Wilson { 82ecc11474SStephen Wilson return "plugin.platform.linux"; 83ecc11474SStephen Wilson } 84ecc11474SStephen Wilson 85ecc11474SStephen Wilson const char * 8628041352SGreg Clayton PlatformLinux::GetShortPluginNameStatic (bool is_host) 87ecc11474SStephen Wilson { 8828041352SGreg Clayton if (is_host) 8928041352SGreg Clayton return Platform::GetHostPlatformName (); 9028041352SGreg Clayton else 9128041352SGreg Clayton return "remote-linux"; 9228041352SGreg Clayton } 9328041352SGreg Clayton 9428041352SGreg Clayton const char * 9528041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic (bool is_host) 9628041352SGreg Clayton { 9728041352SGreg Clayton if (is_host) 9828041352SGreg Clayton return "Local Linux user platform plug-in."; 9928041352SGreg Clayton else 10028041352SGreg Clayton return "Remote Linux user platform plug-in."; 101ecc11474SStephen Wilson } 102ecc11474SStephen Wilson 103e996fd30SGreg Clayton void 104e996fd30SGreg Clayton PlatformLinux::Initialize () 105e996fd30SGreg Clayton { 10628041352SGreg Clayton if (g_initialize_count++ == 0) 107ecc11474SStephen Wilson { 10828041352SGreg Clayton #if defined(__linux__) 10928041352SGreg Clayton PlatformSP default_platform_sp (new PlatformLinux(true)); 11028041352SGreg Clayton default_platform_sp->SetSystemArchitecture (Host::GetArchitecture()); 111e996fd30SGreg Clayton Platform::SetDefaultPlatform (default_platform_sp); 11228041352SGreg Clayton #endif 11328041352SGreg Clayton PluginManager::RegisterPlugin(PlatformLinux::GetShortPluginNameStatic(false), 11428041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic(false), 11528041352SGreg Clayton PlatformLinux::CreateInstance); 116ecc11474SStephen Wilson } 117e996fd30SGreg Clayton } 118e996fd30SGreg Clayton 119e996fd30SGreg Clayton void 120e996fd30SGreg Clayton PlatformLinux::Terminate () 121e996fd30SGreg Clayton { 12228041352SGreg Clayton if (g_initialize_count > 0) 12328041352SGreg Clayton { 12428041352SGreg Clayton if (--g_initialize_count == 0) 12528041352SGreg Clayton { 12628041352SGreg Clayton PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance); 127e996fd30SGreg Clayton } 12828041352SGreg Clayton } 12928041352SGreg Clayton } 130e996fd30SGreg Clayton 131e996fd30SGreg Clayton Error 132e996fd30SGreg Clayton PlatformLinux::ResolveExecutable (const FileSpec &exe_file, 133e996fd30SGreg Clayton const ArchSpec &exe_arch, 134ea5e0cc3SGreg Clayton lldb::ModuleSP &exe_module_sp, 135ea5e0cc3SGreg Clayton const FileSpecList *module_search_paths_ptr) 136e996fd30SGreg Clayton { 137e996fd30SGreg Clayton Error error; 138e996fd30SGreg Clayton // Nothing special to do here, just use the actual file and architecture 139e996fd30SGreg Clayton 14028041352SGreg Clayton char exe_path[PATH_MAX]; 141e996fd30SGreg Clayton FileSpec resolved_exe_file (exe_file); 142e996fd30SGreg Clayton 14328041352SGreg Clayton if (IsHost()) 14428041352SGreg Clayton { 14528041352SGreg Clayton // If we have "ls" as the exe_file, resolve the executable location based on 146e996fd30SGreg Clayton // the current path variables 147e996fd30SGreg Clayton if (!resolved_exe_file.Exists()) 14828041352SGreg Clayton { 14928041352SGreg Clayton exe_file.GetPath(exe_path, sizeof(exe_path)); 15028041352SGreg Clayton resolved_exe_file.SetFile(exe_path, true); 15128041352SGreg Clayton } 15228041352SGreg Clayton 15328041352SGreg Clayton if (!resolved_exe_file.Exists()) 154e996fd30SGreg Clayton resolved_exe_file.ResolveExecutableLocation (); 155e996fd30SGreg Clayton 15628041352SGreg Clayton if (resolved_exe_file.Exists()) 15728041352SGreg Clayton error.Clear(); 15828041352SGreg Clayton else 15928041352SGreg Clayton { 16028041352SGreg Clayton exe_file.GetPath(exe_path, sizeof(exe_path)); 16128041352SGreg Clayton error.SetErrorStringWithFormat("unable to find executable for '%s'", exe_path); 16228041352SGreg Clayton } 16328041352SGreg Clayton } 16428041352SGreg Clayton else 16528041352SGreg Clayton { 16628041352SGreg Clayton if (m_remote_platform_sp) 16728041352SGreg Clayton { 16828041352SGreg Clayton error = m_remote_platform_sp->ResolveExecutable (exe_file, 16928041352SGreg Clayton exe_arch, 1700c90ef47SGreg Clayton exe_module_sp, 1710c90ef47SGreg Clayton NULL); 17228041352SGreg Clayton } 17328041352SGreg Clayton else 17428041352SGreg Clayton { 17528041352SGreg Clayton // We may connect to a process and use the provided executable (Don't use local $PATH). 176e996fd30SGreg Clayton 177e996fd30SGreg Clayton if (resolved_exe_file.Exists()) 17828041352SGreg Clayton error.Clear(); 17928041352SGreg Clayton else 18028041352SGreg Clayton error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path); 18128041352SGreg Clayton } 18228041352SGreg Clayton } 18328041352SGreg Clayton 18428041352SGreg Clayton if (error.Success()) 185e996fd30SGreg Clayton { 186ea5e0cc3SGreg Clayton ModuleSpec module_spec (resolved_exe_file, exe_arch); 187e996fd30SGreg Clayton if (exe_arch.IsValid()) 188e996fd30SGreg Clayton { 189ea5e0cc3SGreg Clayton error = ModuleList::GetSharedModule (module_spec, 190e996fd30SGreg Clayton exe_module_sp, 191e996fd30SGreg Clayton NULL, 1920c90ef47SGreg Clayton NULL, 193e996fd30SGreg Clayton NULL); 194e996fd30SGreg Clayton 195e996fd30SGreg Clayton if (exe_module_sp->GetObjectFile() == NULL) 196e996fd30SGreg Clayton { 197e996fd30SGreg Clayton exe_module_sp.reset(); 198e996fd30SGreg Clayton error.SetErrorStringWithFormat ("'%s%s%s' doesn't contain the architecture %s", 199e996fd30SGreg Clayton exe_file.GetDirectory().AsCString(""), 200e996fd30SGreg Clayton exe_file.GetDirectory() ? "/" : "", 201e996fd30SGreg Clayton exe_file.GetFilename().AsCString(""), 202e996fd30SGreg Clayton exe_arch.GetArchitectureName()); 203e996fd30SGreg Clayton } 204e996fd30SGreg Clayton } 205e996fd30SGreg Clayton else 206e996fd30SGreg Clayton { 207e996fd30SGreg Clayton // No valid architecture was specified, ask the platform for 208e996fd30SGreg Clayton // the architectures that we should be using (in the correct order) 209e996fd30SGreg Clayton // and see if we can find a match that way 210e996fd30SGreg Clayton StreamString arch_names; 211ea5e0cc3SGreg Clayton for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, module_spec.GetArchitecture()); ++idx) 212e996fd30SGreg Clayton { 213ea5e0cc3SGreg Clayton error = ModuleList::GetSharedModule (module_spec, 214e996fd30SGreg Clayton exe_module_sp, 215e996fd30SGreg Clayton NULL, 2160c90ef47SGreg Clayton NULL, 217e996fd30SGreg Clayton NULL); 218e996fd30SGreg Clayton // Did we find an executable using one of the 219e996fd30SGreg Clayton if (error.Success()) 220e996fd30SGreg Clayton { 221e996fd30SGreg Clayton if (exe_module_sp && exe_module_sp->GetObjectFile()) 222e996fd30SGreg Clayton break; 223e996fd30SGreg Clayton else 224e996fd30SGreg Clayton error.SetErrorToGenericError(); 225e996fd30SGreg Clayton } 226e996fd30SGreg Clayton 227e996fd30SGreg Clayton if (idx > 0) 228e996fd30SGreg Clayton arch_names.PutCString (", "); 229ea5e0cc3SGreg Clayton arch_names.PutCString (module_spec.GetArchitecture().GetArchitectureName()); 230e996fd30SGreg Clayton } 231e996fd30SGreg Clayton 232e996fd30SGreg Clayton if (error.Fail() || !exe_module_sp) 233e996fd30SGreg Clayton { 234e996fd30SGreg Clayton error.SetErrorStringWithFormat ("'%s%s%s' doesn't contain any '%s' platform architectures: %s", 235e996fd30SGreg Clayton exe_file.GetDirectory().AsCString(""), 236e996fd30SGreg Clayton exe_file.GetDirectory() ? "/" : "", 237e996fd30SGreg Clayton exe_file.GetFilename().AsCString(""), 238e996fd30SGreg Clayton GetShortPluginName(), 239e996fd30SGreg Clayton arch_names.GetString().c_str()); 240e996fd30SGreg Clayton } 241e996fd30SGreg Clayton } 242e996fd30SGreg Clayton } 243e996fd30SGreg Clayton 244e996fd30SGreg Clayton return error; 245e996fd30SGreg Clayton } 246e996fd30SGreg Clayton 247e996fd30SGreg Clayton Error 24878decfd0SStephen Wilson PlatformLinux::GetFile (const FileSpec &platform_file, 24928041352SGreg Clayton const UUID *uuid_ptr, FileSpec &local_file) 250e996fd30SGreg Clayton { 25128041352SGreg Clayton if (IsRemote()) 25228041352SGreg Clayton { 25328041352SGreg Clayton if (m_remote_platform_sp) 25428041352SGreg Clayton return m_remote_platform_sp->GetFile (platform_file, uuid_ptr, local_file); 25528041352SGreg Clayton } 25628041352SGreg Clayton 257e996fd30SGreg Clayton // Default to the local case 258e996fd30SGreg Clayton local_file = platform_file; 259e996fd30SGreg Clayton return Error(); 260e996fd30SGreg Clayton } 261e996fd30SGreg Clayton 262e996fd30SGreg Clayton 263e996fd30SGreg Clayton //------------------------------------------------------------------ 264e996fd30SGreg Clayton /// Default Constructor 265e996fd30SGreg Clayton //------------------------------------------------------------------ 26628041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) : 26728041352SGreg Clayton Platform(is_host), // This is the local host platform 26828041352SGreg Clayton m_remote_platform_sp () 269e996fd30SGreg Clayton { 270e996fd30SGreg Clayton } 271e996fd30SGreg Clayton 272e996fd30SGreg Clayton //------------------------------------------------------------------ 273e996fd30SGreg Clayton /// Destructor. 274e996fd30SGreg Clayton /// 275e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be 276e996fd30SGreg Clayton /// inherited from by the plug-in instance. 277e996fd30SGreg Clayton //------------------------------------------------------------------ 278e996fd30SGreg Clayton PlatformLinux::~PlatformLinux() 279e996fd30SGreg Clayton { 280e996fd30SGreg Clayton } 281e996fd30SGreg Clayton 282e996fd30SGreg Clayton bool 28313e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) 284e996fd30SGreg Clayton { 28528041352SGreg Clayton bool success = false; 28628041352SGreg Clayton if (IsHost()) 28728041352SGreg Clayton { 28828041352SGreg Clayton success = Platform::GetProcessInfo (pid, process_info); 28928041352SGreg Clayton } 29028041352SGreg Clayton else 29128041352SGreg Clayton { 29228041352SGreg Clayton if (m_remote_platform_sp) 29328041352SGreg Clayton success = m_remote_platform_sp->GetProcessInfo (pid, process_info); 29428041352SGreg Clayton } 29528041352SGreg Clayton return success; 296e996fd30SGreg Clayton } 297e996fd30SGreg Clayton 298e996fd30SGreg Clayton bool 299e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch) 300e996fd30SGreg Clayton { 301e996fd30SGreg Clayton if (idx == 0) 302e996fd30SGreg Clayton { 303e996fd30SGreg Clayton arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture); 304e996fd30SGreg Clayton return arch.IsValid(); 305e996fd30SGreg Clayton } 306e996fd30SGreg Clayton return false; 307e996fd30SGreg Clayton } 308ecc11474SStephen Wilson 309ecc11474SStephen Wilson void 310ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm) 311ecc11474SStephen Wilson { 312ecc11474SStephen Wilson struct utsname un; 313ecc11474SStephen Wilson 314ecc11474SStephen Wilson if (uname(&un)) { 315ecc11474SStephen Wilson strm << "Linux"; 316ecc11474SStephen Wilson return; 317ecc11474SStephen Wilson } 318ecc11474SStephen Wilson 319ecc11474SStephen Wilson strm << un.sysname << ' ' << un.release << ' ' << un.version << '\n'; 320ecc11474SStephen Wilson } 321ecc11474SStephen Wilson 322ecc11474SStephen Wilson size_t 323ecc11474SStephen Wilson PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target, 324ecc11474SStephen Wilson BreakpointSite *bp_site) 325ecc11474SStephen Wilson { 326ecc11474SStephen Wilson ArchSpec arch = target.GetArchitecture(); 32728041352SGreg Clayton const uint8_t *trap_opcode = NULL; 32828041352SGreg Clayton size_t trap_opcode_size = 0; 329ecc11474SStephen Wilson 330ecc11474SStephen Wilson switch (arch.GetCore()) 331ecc11474SStephen Wilson { 332ecc11474SStephen Wilson default: 333ecc11474SStephen Wilson assert(false && "CPU type not supported!"); 334ecc11474SStephen Wilson break; 335ecc11474SStephen Wilson 336ecc11474SStephen Wilson case ArchSpec::eCore_x86_32_i386: 337ecc11474SStephen Wilson case ArchSpec::eCore_x86_64_x86_64: 33828041352SGreg Clayton { 33928041352SGreg Clayton static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC }; 34028041352SGreg Clayton trap_opcode = g_i386_breakpoint_opcode; 34128041352SGreg Clayton trap_opcode_size = sizeof(g_i386_breakpoint_opcode); 34228041352SGreg Clayton } 343ecc11474SStephen Wilson break; 344ecc11474SStephen Wilson } 345ecc11474SStephen Wilson 34628041352SGreg Clayton if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size)) 34728041352SGreg Clayton return trap_opcode_size; 34828041352SGreg Clayton return 0; 34928041352SGreg Clayton } 35028041352SGreg Clayton 35128041352SGreg Clayton Error 35228041352SGreg Clayton PlatformLinux::LaunchProcess (ProcessLaunchInfo &launch_info) 35328041352SGreg Clayton { 35428041352SGreg Clayton Error error; 35528041352SGreg Clayton 35628041352SGreg Clayton if (IsHost()) 35728041352SGreg Clayton { 35828041352SGreg Clayton if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell)) 35928041352SGreg Clayton { 36028041352SGreg Clayton const bool is_localhost = true; 361d1cf11a7SGreg Clayton const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug); 362d1cf11a7SGreg Clayton const bool first_arg_is_full_shell_command = false; 363d1cf11a7SGreg Clayton if (!launch_info.ConvertArgumentsForLaunchingInShell (error, 364d1cf11a7SGreg Clayton is_localhost, 365d1cf11a7SGreg Clayton will_debug, 366d1cf11a7SGreg Clayton first_arg_is_full_shell_command)) 36728041352SGreg Clayton return error; 36828041352SGreg Clayton } 36928041352SGreg Clayton error = Platform::LaunchProcess (launch_info); 37028041352SGreg Clayton } 37128041352SGreg Clayton else 37228041352SGreg Clayton { 37328041352SGreg Clayton error.SetErrorString ("the platform is not currently connected"); 37428041352SGreg Clayton } 37528041352SGreg Clayton return error; 376ecc11474SStephen Wilson } 37713e8e1c3SJohnny Chen 37813e8e1c3SJohnny Chen lldb::ProcessSP 379fb2b629dSPeter Collingbourne PlatformLinux::Attach(ProcessAttachInfo &attach_info, 38013e8e1c3SJohnny Chen Debugger &debugger, 38113e8e1c3SJohnny Chen Target *target, 38213e8e1c3SJohnny Chen Listener &listener, 38313e8e1c3SJohnny Chen Error &error) 38413e8e1c3SJohnny Chen { 38528041352SGreg Clayton lldb::ProcessSP process_sp; 38628041352SGreg Clayton if (IsHost()) 38728041352SGreg Clayton { 38828041352SGreg Clayton if (target == NULL) 38928041352SGreg Clayton { 39028041352SGreg Clayton TargetSP new_target_sp; 39128041352SGreg Clayton FileSpec emptyFileSpec; 39228041352SGreg Clayton ArchSpec emptyArchSpec; 39328041352SGreg Clayton 39428041352SGreg Clayton error = debugger.GetTargetList().CreateTarget (debugger, 39528041352SGreg Clayton emptyFileSpec, 39628041352SGreg Clayton emptyArchSpec, 39728041352SGreg Clayton false, 39828041352SGreg Clayton m_remote_platform_sp, 39928041352SGreg Clayton new_target_sp); 40028041352SGreg Clayton target = new_target_sp.get(); 40128041352SGreg Clayton } 40228041352SGreg Clayton else 40328041352SGreg Clayton error.Clear(); 40428041352SGreg Clayton 40528041352SGreg Clayton if (target && error.Success()) 40628041352SGreg Clayton { 40728041352SGreg Clayton debugger.GetTargetList().SetSelectedTarget(target); 40828041352SGreg Clayton 4090c90ef47SGreg Clayton process_sp = target->CreateProcess (listener, 4100c90ef47SGreg Clayton attach_info.GetProcessPluginName(), 4110c90ef47SGreg Clayton NULL); 41228041352SGreg Clayton 41328041352SGreg Clayton if (process_sp) 41428041352SGreg Clayton error = process_sp->Attach (attach_info); 41528041352SGreg Clayton } 41628041352SGreg Clayton } 41728041352SGreg Clayton else 41828041352SGreg Clayton { 41928041352SGreg Clayton if (m_remote_platform_sp) 42028041352SGreg Clayton process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, listener, error); 42128041352SGreg Clayton else 42228041352SGreg Clayton error.SetErrorString ("the platform is not currently connected"); 42328041352SGreg Clayton } 42428041352SGreg Clayton return process_sp; 42513e8e1c3SJohnny Chen } 426