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" 231f746071SGreg 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 195*e635db49SSean Callanan // TODO find out why exe_module_sp might be NULL 196*e635db49SSean Callanan if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL) 197e996fd30SGreg Clayton { 198e996fd30SGreg Clayton exe_module_sp.reset(); 199e996fd30SGreg Clayton error.SetErrorStringWithFormat ("'%s%s%s' doesn't contain the architecture %s", 200e996fd30SGreg Clayton exe_file.GetDirectory().AsCString(""), 201e996fd30SGreg Clayton exe_file.GetDirectory() ? "/" : "", 202e996fd30SGreg Clayton exe_file.GetFilename().AsCString(""), 203e996fd30SGreg Clayton exe_arch.GetArchitectureName()); 204e996fd30SGreg Clayton } 205e996fd30SGreg Clayton } 206e996fd30SGreg Clayton else 207e996fd30SGreg Clayton { 208e996fd30SGreg Clayton // No valid architecture was specified, ask the platform for 209e996fd30SGreg Clayton // the architectures that we should be using (in the correct order) 210e996fd30SGreg Clayton // and see if we can find a match that way 211e996fd30SGreg Clayton StreamString arch_names; 212ea5e0cc3SGreg Clayton for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, module_spec.GetArchitecture()); ++idx) 213e996fd30SGreg Clayton { 214ea5e0cc3SGreg Clayton error = ModuleList::GetSharedModule (module_spec, 215e996fd30SGreg Clayton exe_module_sp, 216e996fd30SGreg Clayton NULL, 2170c90ef47SGreg Clayton NULL, 218e996fd30SGreg Clayton NULL); 219e996fd30SGreg Clayton // Did we find an executable using one of the 220e996fd30SGreg Clayton if (error.Success()) 221e996fd30SGreg Clayton { 222e996fd30SGreg Clayton if (exe_module_sp && exe_module_sp->GetObjectFile()) 223e996fd30SGreg Clayton break; 224e996fd30SGreg Clayton else 225e996fd30SGreg Clayton error.SetErrorToGenericError(); 226e996fd30SGreg Clayton } 227e996fd30SGreg Clayton 228e996fd30SGreg Clayton if (idx > 0) 229e996fd30SGreg Clayton arch_names.PutCString (", "); 230ea5e0cc3SGreg Clayton arch_names.PutCString (module_spec.GetArchitecture().GetArchitectureName()); 231e996fd30SGreg Clayton } 232e996fd30SGreg Clayton 233e996fd30SGreg Clayton if (error.Fail() || !exe_module_sp) 234e996fd30SGreg Clayton { 235e996fd30SGreg Clayton error.SetErrorStringWithFormat ("'%s%s%s' doesn't contain any '%s' platform architectures: %s", 236e996fd30SGreg Clayton exe_file.GetDirectory().AsCString(""), 237e996fd30SGreg Clayton exe_file.GetDirectory() ? "/" : "", 238e996fd30SGreg Clayton exe_file.GetFilename().AsCString(""), 239e996fd30SGreg Clayton GetShortPluginName(), 240e996fd30SGreg Clayton arch_names.GetString().c_str()); 241e996fd30SGreg Clayton } 242e996fd30SGreg Clayton } 243e996fd30SGreg Clayton } 244e996fd30SGreg Clayton 245e996fd30SGreg Clayton return error; 246e996fd30SGreg Clayton } 247e996fd30SGreg Clayton 248e996fd30SGreg Clayton Error 24978decfd0SStephen Wilson PlatformLinux::GetFile (const FileSpec &platform_file, 25028041352SGreg Clayton const UUID *uuid_ptr, FileSpec &local_file) 251e996fd30SGreg Clayton { 25228041352SGreg Clayton if (IsRemote()) 25328041352SGreg Clayton { 25428041352SGreg Clayton if (m_remote_platform_sp) 25528041352SGreg Clayton return m_remote_platform_sp->GetFile (platform_file, uuid_ptr, local_file); 25628041352SGreg Clayton } 25728041352SGreg Clayton 258e996fd30SGreg Clayton // Default to the local case 259e996fd30SGreg Clayton local_file = platform_file; 260e996fd30SGreg Clayton return Error(); 261e996fd30SGreg Clayton } 262e996fd30SGreg Clayton 263e996fd30SGreg Clayton 264e996fd30SGreg Clayton //------------------------------------------------------------------ 265e996fd30SGreg Clayton /// Default Constructor 266e996fd30SGreg Clayton //------------------------------------------------------------------ 26728041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) : 26828041352SGreg Clayton Platform(is_host), // This is the local host platform 26928041352SGreg Clayton m_remote_platform_sp () 270e996fd30SGreg Clayton { 271e996fd30SGreg Clayton } 272e996fd30SGreg Clayton 273e996fd30SGreg Clayton //------------------------------------------------------------------ 274e996fd30SGreg Clayton /// Destructor. 275e996fd30SGreg Clayton /// 276e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be 277e996fd30SGreg Clayton /// inherited from by the plug-in instance. 278e996fd30SGreg Clayton //------------------------------------------------------------------ 279e996fd30SGreg Clayton PlatformLinux::~PlatformLinux() 280e996fd30SGreg Clayton { 281e996fd30SGreg Clayton } 282e996fd30SGreg Clayton 283e996fd30SGreg Clayton bool 28413e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) 285e996fd30SGreg Clayton { 28628041352SGreg Clayton bool success = false; 28728041352SGreg Clayton if (IsHost()) 28828041352SGreg Clayton { 28928041352SGreg Clayton success = Platform::GetProcessInfo (pid, process_info); 29028041352SGreg Clayton } 29128041352SGreg Clayton else 29228041352SGreg Clayton { 29328041352SGreg Clayton if (m_remote_platform_sp) 29428041352SGreg Clayton success = m_remote_platform_sp->GetProcessInfo (pid, process_info); 29528041352SGreg Clayton } 29628041352SGreg Clayton return success; 297e996fd30SGreg Clayton } 298e996fd30SGreg Clayton 299e996fd30SGreg Clayton bool 300e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch) 301e996fd30SGreg Clayton { 302e996fd30SGreg Clayton if (idx == 0) 303e996fd30SGreg Clayton { 304e996fd30SGreg Clayton arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture); 305e996fd30SGreg Clayton return arch.IsValid(); 306e996fd30SGreg Clayton } 307e996fd30SGreg Clayton return false; 308e996fd30SGreg Clayton } 309ecc11474SStephen Wilson 310ecc11474SStephen Wilson void 311ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm) 312ecc11474SStephen Wilson { 313ecc11474SStephen Wilson struct utsname un; 314ecc11474SStephen Wilson 315ecc11474SStephen Wilson if (uname(&un)) { 316ecc11474SStephen Wilson strm << "Linux"; 317ecc11474SStephen Wilson return; 318ecc11474SStephen Wilson } 319ecc11474SStephen Wilson 320ecc11474SStephen Wilson strm << un.sysname << ' ' << un.release << ' ' << un.version << '\n'; 321ecc11474SStephen Wilson } 322ecc11474SStephen Wilson 323ecc11474SStephen Wilson size_t 324ecc11474SStephen Wilson PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target, 325ecc11474SStephen Wilson BreakpointSite *bp_site) 326ecc11474SStephen Wilson { 327ecc11474SStephen Wilson ArchSpec arch = target.GetArchitecture(); 32828041352SGreg Clayton const uint8_t *trap_opcode = NULL; 32928041352SGreg Clayton size_t trap_opcode_size = 0; 330ecc11474SStephen Wilson 331ecc11474SStephen Wilson switch (arch.GetCore()) 332ecc11474SStephen Wilson { 333ecc11474SStephen Wilson default: 334ecc11474SStephen Wilson assert(false && "CPU type not supported!"); 335ecc11474SStephen Wilson break; 336ecc11474SStephen Wilson 337ecc11474SStephen Wilson case ArchSpec::eCore_x86_32_i386: 338ecc11474SStephen Wilson case ArchSpec::eCore_x86_64_x86_64: 33928041352SGreg Clayton { 34028041352SGreg Clayton static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC }; 34128041352SGreg Clayton trap_opcode = g_i386_breakpoint_opcode; 34228041352SGreg Clayton trap_opcode_size = sizeof(g_i386_breakpoint_opcode); 34328041352SGreg Clayton } 344ecc11474SStephen Wilson break; 345ecc11474SStephen Wilson } 346ecc11474SStephen Wilson 34728041352SGreg Clayton if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size)) 34828041352SGreg Clayton return trap_opcode_size; 34928041352SGreg Clayton return 0; 35028041352SGreg Clayton } 35128041352SGreg Clayton 35228041352SGreg Clayton Error 35328041352SGreg Clayton PlatformLinux::LaunchProcess (ProcessLaunchInfo &launch_info) 35428041352SGreg Clayton { 35528041352SGreg Clayton Error error; 35628041352SGreg Clayton 35728041352SGreg Clayton if (IsHost()) 35828041352SGreg Clayton { 35928041352SGreg Clayton if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell)) 36028041352SGreg Clayton { 36128041352SGreg Clayton const bool is_localhost = true; 362d1cf11a7SGreg Clayton const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug); 363d1cf11a7SGreg Clayton const bool first_arg_is_full_shell_command = false; 364d1cf11a7SGreg Clayton if (!launch_info.ConvertArgumentsForLaunchingInShell (error, 365d1cf11a7SGreg Clayton is_localhost, 366d1cf11a7SGreg Clayton will_debug, 367d1cf11a7SGreg Clayton first_arg_is_full_shell_command)) 36828041352SGreg Clayton return error; 36928041352SGreg Clayton } 37028041352SGreg Clayton error = Platform::LaunchProcess (launch_info); 37128041352SGreg Clayton } 37228041352SGreg Clayton else 37328041352SGreg Clayton { 37428041352SGreg Clayton error.SetErrorString ("the platform is not currently connected"); 37528041352SGreg Clayton } 37628041352SGreg Clayton return error; 377ecc11474SStephen Wilson } 37813e8e1c3SJohnny Chen 37913e8e1c3SJohnny Chen lldb::ProcessSP 380fb2b629dSPeter Collingbourne PlatformLinux::Attach(ProcessAttachInfo &attach_info, 38113e8e1c3SJohnny Chen Debugger &debugger, 38213e8e1c3SJohnny Chen Target *target, 38313e8e1c3SJohnny Chen Listener &listener, 38413e8e1c3SJohnny Chen Error &error) 38513e8e1c3SJohnny Chen { 38628041352SGreg Clayton lldb::ProcessSP process_sp; 38728041352SGreg Clayton if (IsHost()) 38828041352SGreg Clayton { 38928041352SGreg Clayton if (target == NULL) 39028041352SGreg Clayton { 39128041352SGreg Clayton TargetSP new_target_sp; 39228041352SGreg Clayton FileSpec emptyFileSpec; 39328041352SGreg Clayton ArchSpec emptyArchSpec; 39428041352SGreg Clayton 39528041352SGreg Clayton error = debugger.GetTargetList().CreateTarget (debugger, 39628041352SGreg Clayton emptyFileSpec, 39728041352SGreg Clayton emptyArchSpec, 39828041352SGreg Clayton false, 39928041352SGreg Clayton m_remote_platform_sp, 40028041352SGreg Clayton new_target_sp); 40128041352SGreg Clayton target = new_target_sp.get(); 40228041352SGreg Clayton } 40328041352SGreg Clayton else 40428041352SGreg Clayton error.Clear(); 40528041352SGreg Clayton 40628041352SGreg Clayton if (target && error.Success()) 40728041352SGreg Clayton { 40828041352SGreg Clayton debugger.GetTargetList().SetSelectedTarget(target); 40928041352SGreg Clayton 4100c90ef47SGreg Clayton process_sp = target->CreateProcess (listener, 4110c90ef47SGreg Clayton attach_info.GetProcessPluginName(), 4120c90ef47SGreg Clayton NULL); 41328041352SGreg Clayton 41428041352SGreg Clayton if (process_sp) 41528041352SGreg Clayton error = process_sp->Attach (attach_info); 41628041352SGreg Clayton } 41728041352SGreg Clayton } 41828041352SGreg Clayton else 41928041352SGreg Clayton { 42028041352SGreg Clayton if (m_remote_platform_sp) 42128041352SGreg Clayton process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, listener, error); 42228041352SGreg Clayton else 42328041352SGreg Clayton error.SetErrorString ("the platform is not currently connected"); 42428041352SGreg Clayton } 42528041352SGreg Clayton return process_sp; 42613e8e1c3SJohnny Chen } 427