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 1093a64300SDaniel Malea #include "lldb/lldb-python.h" 1193a64300SDaniel Malea 12e996fd30SGreg Clayton #include "PlatformLinux.h" 13b2f1fb29SVirgile Bello #include "lldb/Host/Config.h" 14e996fd30SGreg Clayton 15e996fd30SGreg Clayton // C Includes 16ecc11474SStephen Wilson #include <stdio.h> 17b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX 18ecc11474SStephen Wilson #include <sys/utsname.h> 19b2f1fb29SVirgile Bello #endif 20ecc11474SStephen Wilson 21e996fd30SGreg Clayton // C++ Includes 22e996fd30SGreg Clayton // Other libraries and framework includes 23e996fd30SGreg Clayton // Project includes 24e996fd30SGreg Clayton #include "lldb/Core/Error.h" 2528041352SGreg Clayton #include "lldb/Core/Debugger.h" 26015d818bSTodd Fiala #include "lldb/Core/Log.h" 27e996fd30SGreg Clayton #include "lldb/Core/Module.h" 28e996fd30SGreg Clayton #include "lldb/Core/ModuleList.h" 291f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h" 30ecc11474SStephen Wilson #include "lldb/Core/PluginManager.h" 31e996fd30SGreg Clayton #include "lldb/Core/StreamString.h" 32e996fd30SGreg Clayton #include "lldb/Host/FileSpec.h" 3313b18261SZachary Turner #include "lldb/Host/HostInfo.h" 34ecc11474SStephen Wilson #include "lldb/Target/Target.h" 35e996fd30SGreg Clayton #include "lldb/Target/Process.h" 36e996fd30SGreg Clayton 37af245d11STodd Fiala #if defined(__linux__) 38af245d11STodd Fiala #include "../../Process/Linux/NativeProcessLinux.h" 39af245d11STodd Fiala #endif 40af245d11STodd Fiala 41e996fd30SGreg Clayton using namespace lldb; 42e996fd30SGreg Clayton using namespace lldb_private; 43e996fd30SGreg Clayton 4428041352SGreg Clayton static uint32_t g_initialize_count = 0; 4528041352SGreg Clayton 46ecc11474SStephen Wilson Platform * 47b3a40ba8SGreg Clayton PlatformLinux::CreateInstance (bool force, const ArchSpec *arch) 48ecc11474SStephen Wilson { 49015d818bSTodd Fiala Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); 50015d818bSTodd Fiala if (log) 51015d818bSTodd Fiala { 52015d818bSTodd Fiala const char *arch_name; 53015d818bSTodd Fiala if (arch && arch->GetArchitectureName ()) 54015d818bSTodd Fiala arch_name = arch->GetArchitectureName (); 55015d818bSTodd Fiala else 56015d818bSTodd Fiala arch_name = "<null>"; 57015d818bSTodd Fiala 58015d818bSTodd Fiala const char *triple_cstr = arch ? arch->GetTriple ().getTriple ().c_str() : "<null>"; 59015d818bSTodd Fiala 60015d818bSTodd Fiala log->Printf ("PlatformLinux::%s(force=%s, arch={%s,%s})", __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr); 61015d818bSTodd Fiala } 62015d818bSTodd Fiala 63b3a40ba8SGreg Clayton bool create = force; 64b3a40ba8SGreg Clayton if (create == false && arch && arch->IsValid()) 65b3a40ba8SGreg Clayton { 66b3a40ba8SGreg Clayton const llvm::Triple &triple = arch->GetTriple(); 6770512317SGreg Clayton switch (triple.getVendor()) 6870512317SGreg Clayton { 6970512317SGreg Clayton case llvm::Triple::PC: 70b3a40ba8SGreg Clayton create = true; 7170512317SGreg Clayton break; 7270512317SGreg Clayton 73dbc6c0bbSGreg Clayton #if defined(__linux__) 74dbc6c0bbSGreg Clayton // Only accept "unknown" for the vendor if the host is linux and 756a7f3338SBruce Mitchener // it "unknown" wasn't specified (it was just returned because it 76dbc6c0bbSGreg Clayton // was NOT specified_ 77015d818bSTodd Fiala case llvm::Triple::VendorType::UnknownVendor: 7870512317SGreg Clayton create = !arch->TripleVendorWasSpecified(); 7970512317SGreg Clayton break; 80dbc6c0bbSGreg Clayton #endif 8170512317SGreg Clayton default: 8270512317SGreg Clayton break; 8370512317SGreg Clayton } 8470512317SGreg Clayton 8570512317SGreg Clayton if (create) 8670512317SGreg Clayton { 8770512317SGreg Clayton switch (triple.getOS()) 8870512317SGreg Clayton { 8970512317SGreg Clayton case llvm::Triple::Linux: 9070512317SGreg Clayton break; 9170512317SGreg Clayton 92dbc6c0bbSGreg Clayton #if defined(__linux__) 93dbc6c0bbSGreg Clayton // Only accept "unknown" for the OS if the host is linux and 946a7f3338SBruce Mitchener // it "unknown" wasn't specified (it was just returned because it 95dbc6c0bbSGreg Clayton // was NOT specified) 96015d818bSTodd Fiala case llvm::Triple::OSType::UnknownOS: 9770512317SGreg Clayton create = !arch->TripleOSWasSpecified(); 9870512317SGreg Clayton break; 99dbc6c0bbSGreg Clayton #endif 10070512317SGreg Clayton default: 10170512317SGreg Clayton create = false; 10270512317SGreg Clayton break; 10370512317SGreg Clayton } 10470512317SGreg Clayton } 105b3a40ba8SGreg Clayton } 106015d818bSTodd Fiala 107b3a40ba8SGreg Clayton if (create) 108015d818bSTodd Fiala { 109015d818bSTodd Fiala if (log) 110015d818bSTodd Fiala log->Printf ("PlatformLinux::%s() creating remote-linux platform", __FUNCTION__); 111a4756939STodd Fiala return new PlatformLinux(false); 112015d818bSTodd Fiala } 113015d818bSTodd Fiala 114015d818bSTodd Fiala if (log) 115015d818bSTodd Fiala log->Printf ("PlatformLinux::%s() aborting creation of remote-linux platform", __FUNCTION__); 116015d818bSTodd Fiala 117b3a40ba8SGreg Clayton return NULL; 118ecc11474SStephen Wilson } 119ecc11474SStephen Wilson 120ecc11474SStephen Wilson 12157abc5d6SGreg Clayton lldb_private::ConstString 12257abc5d6SGreg Clayton PlatformLinux::GetPluginNameStatic (bool is_host) 123ecc11474SStephen Wilson { 12428041352SGreg Clayton if (is_host) 12557abc5d6SGreg Clayton { 12657abc5d6SGreg Clayton static ConstString g_host_name(Platform::GetHostPlatformName ()); 12757abc5d6SGreg Clayton return g_host_name; 12857abc5d6SGreg Clayton } 12928041352SGreg Clayton else 13057abc5d6SGreg Clayton { 13157abc5d6SGreg Clayton static ConstString g_remote_name("remote-linux"); 13257abc5d6SGreg Clayton return g_remote_name; 13357abc5d6SGreg Clayton } 13428041352SGreg Clayton } 13528041352SGreg Clayton 13628041352SGreg Clayton const char * 13728041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic (bool is_host) 13828041352SGreg Clayton { 13928041352SGreg Clayton if (is_host) 14028041352SGreg Clayton return "Local Linux user platform plug-in."; 14128041352SGreg Clayton else 14228041352SGreg Clayton return "Remote Linux user platform plug-in."; 143ecc11474SStephen Wilson } 144ecc11474SStephen Wilson 14557abc5d6SGreg Clayton lldb_private::ConstString 14657abc5d6SGreg Clayton PlatformLinux::GetPluginName() 14757abc5d6SGreg Clayton { 14857abc5d6SGreg Clayton return GetPluginNameStatic(IsHost()); 14957abc5d6SGreg Clayton } 15057abc5d6SGreg Clayton 151e996fd30SGreg Clayton void 152e996fd30SGreg Clayton PlatformLinux::Initialize () 153e996fd30SGreg Clayton { 15428041352SGreg Clayton if (g_initialize_count++ == 0) 155ecc11474SStephen Wilson { 15628041352SGreg Clayton #if defined(__linux__) 15728041352SGreg Clayton PlatformSP default_platform_sp (new PlatformLinux(true)); 15813b18261SZachary Turner default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 159e996fd30SGreg Clayton Platform::SetDefaultPlatform (default_platform_sp); 16028041352SGreg Clayton #endif 16157abc5d6SGreg Clayton PluginManager::RegisterPlugin(PlatformLinux::GetPluginNameStatic(false), 16228041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic(false), 16328041352SGreg Clayton PlatformLinux::CreateInstance); 164ecc11474SStephen Wilson } 165e996fd30SGreg Clayton } 166e996fd30SGreg Clayton 167e996fd30SGreg Clayton void 168e996fd30SGreg Clayton PlatformLinux::Terminate () 169e996fd30SGreg Clayton { 17028041352SGreg Clayton if (g_initialize_count > 0) 17128041352SGreg Clayton { 17228041352SGreg Clayton if (--g_initialize_count == 0) 17328041352SGreg Clayton { 17428041352SGreg Clayton PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance); 175e996fd30SGreg Clayton } 17628041352SGreg Clayton } 17728041352SGreg Clayton } 178e996fd30SGreg Clayton 179e996fd30SGreg Clayton Error 180e996fd30SGreg Clayton PlatformLinux::ResolveExecutable (const FileSpec &exe_file, 181e996fd30SGreg Clayton const ArchSpec &exe_arch, 182ea5e0cc3SGreg Clayton lldb::ModuleSP &exe_module_sp, 183ea5e0cc3SGreg Clayton const FileSpecList *module_search_paths_ptr) 184e996fd30SGreg Clayton { 185e996fd30SGreg Clayton Error error; 186e996fd30SGreg Clayton // Nothing special to do here, just use the actual file and architecture 187e996fd30SGreg Clayton 18828041352SGreg Clayton char exe_path[PATH_MAX]; 189e996fd30SGreg Clayton FileSpec resolved_exe_file (exe_file); 190e996fd30SGreg Clayton 19128041352SGreg Clayton if (IsHost()) 19228041352SGreg Clayton { 19328041352SGreg Clayton // If we have "ls" as the exe_file, resolve the executable location based on 194e996fd30SGreg Clayton // the current path variables 195e996fd30SGreg Clayton if (!resolved_exe_file.Exists()) 19628041352SGreg Clayton { 19728041352SGreg Clayton exe_file.GetPath(exe_path, sizeof(exe_path)); 19828041352SGreg Clayton resolved_exe_file.SetFile(exe_path, true); 19928041352SGreg Clayton } 20028041352SGreg Clayton 20128041352SGreg Clayton if (!resolved_exe_file.Exists()) 202e996fd30SGreg Clayton resolved_exe_file.ResolveExecutableLocation (); 203e996fd30SGreg Clayton 20428041352SGreg Clayton if (resolved_exe_file.Exists()) 20528041352SGreg Clayton error.Clear(); 20628041352SGreg Clayton else 20728041352SGreg Clayton { 20828041352SGreg Clayton exe_file.GetPath(exe_path, sizeof(exe_path)); 20928041352SGreg Clayton error.SetErrorStringWithFormat("unable to find executable for '%s'", exe_path); 21028041352SGreg Clayton } 21128041352SGreg Clayton } 21228041352SGreg Clayton else 21328041352SGreg Clayton { 21428041352SGreg Clayton if (m_remote_platform_sp) 21528041352SGreg Clayton { 21628041352SGreg Clayton error = m_remote_platform_sp->ResolveExecutable (exe_file, 21728041352SGreg Clayton exe_arch, 2180c90ef47SGreg Clayton exe_module_sp, 2190c90ef47SGreg Clayton NULL); 22028041352SGreg Clayton } 22128041352SGreg Clayton else 22228041352SGreg Clayton { 22328041352SGreg Clayton // We may connect to a process and use the provided executable (Don't use local $PATH). 224e996fd30SGreg Clayton 225e996fd30SGreg Clayton if (resolved_exe_file.Exists()) 22628041352SGreg Clayton error.Clear(); 22728041352SGreg Clayton else 22828041352SGreg Clayton error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path); 22928041352SGreg Clayton } 23028041352SGreg Clayton } 23128041352SGreg Clayton 23228041352SGreg Clayton if (error.Success()) 233e996fd30SGreg Clayton { 234ea5e0cc3SGreg Clayton ModuleSpec module_spec (resolved_exe_file, exe_arch); 235e996fd30SGreg Clayton if (exe_arch.IsValid()) 236e996fd30SGreg Clayton { 237ea5e0cc3SGreg Clayton error = ModuleList::GetSharedModule (module_spec, 238e996fd30SGreg Clayton exe_module_sp, 239e996fd30SGreg Clayton NULL, 2400c90ef47SGreg Clayton NULL, 241e996fd30SGreg Clayton NULL); 2429f0013d8SMichael Sartain if (error.Fail()) 2439f0013d8SMichael Sartain { 2449f0013d8SMichael Sartain // If we failed, it may be because the vendor and os aren't known. If that is the 2459f0013d8SMichael Sartain // case, try setting them to the host architecture and give it another try. 2469f0013d8SMichael Sartain llvm::Triple &module_triple = module_spec.GetArchitecture().GetTriple(); 2479f0013d8SMichael Sartain bool is_vendor_specified = (module_triple.getVendor() != llvm::Triple::UnknownVendor); 2489f0013d8SMichael Sartain bool is_os_specified = (module_triple.getOS() != llvm::Triple::UnknownOS); 2499f0013d8SMichael Sartain if (!is_vendor_specified || !is_os_specified) 2509f0013d8SMichael Sartain { 25113b18261SZachary Turner const llvm::Triple &host_triple = HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple(); 2529f0013d8SMichael Sartain 2539f0013d8SMichael Sartain if (!is_vendor_specified) 2549f0013d8SMichael Sartain module_triple.setVendorName (host_triple.getVendorName()); 2559f0013d8SMichael Sartain if (!is_os_specified) 2569f0013d8SMichael Sartain module_triple.setOSName (host_triple.getOSName()); 2579f0013d8SMichael Sartain 2589f0013d8SMichael Sartain error = ModuleList::GetSharedModule (module_spec, 2599f0013d8SMichael Sartain exe_module_sp, 2609f0013d8SMichael Sartain NULL, 2619f0013d8SMichael Sartain NULL, 2629f0013d8SMichael Sartain NULL); 2639f0013d8SMichael Sartain } 2649f0013d8SMichael Sartain } 265e996fd30SGreg Clayton 266e635db49SSean Callanan // TODO find out why exe_module_sp might be NULL 267e635db49SSean Callanan if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL) 268e996fd30SGreg Clayton { 269e996fd30SGreg Clayton exe_module_sp.reset(); 270b5ad4ec7SGreg Clayton error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s", 271b5ad4ec7SGreg Clayton exe_file.GetPath().c_str(), 272e996fd30SGreg Clayton exe_arch.GetArchitectureName()); 273e996fd30SGreg Clayton } 274e996fd30SGreg Clayton } 275e996fd30SGreg Clayton else 276e996fd30SGreg Clayton { 277e996fd30SGreg Clayton // No valid architecture was specified, ask the platform for 278e996fd30SGreg Clayton // the architectures that we should be using (in the correct order) 279e996fd30SGreg Clayton // and see if we can find a match that way 280e996fd30SGreg Clayton StreamString arch_names; 281ea5e0cc3SGreg Clayton for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, module_spec.GetArchitecture()); ++idx) 282e996fd30SGreg Clayton { 283ea5e0cc3SGreg Clayton error = ModuleList::GetSharedModule (module_spec, 284e996fd30SGreg Clayton exe_module_sp, 285e996fd30SGreg Clayton NULL, 2860c90ef47SGreg Clayton NULL, 287e996fd30SGreg Clayton NULL); 288e996fd30SGreg Clayton // Did we find an executable using one of the 289e996fd30SGreg Clayton if (error.Success()) 290e996fd30SGreg Clayton { 291e996fd30SGreg Clayton if (exe_module_sp && exe_module_sp->GetObjectFile()) 292e996fd30SGreg Clayton break; 293e996fd30SGreg Clayton else 294e996fd30SGreg Clayton error.SetErrorToGenericError(); 295e996fd30SGreg Clayton } 296e996fd30SGreg Clayton 297e996fd30SGreg Clayton if (idx > 0) 298e996fd30SGreg Clayton arch_names.PutCString (", "); 299ea5e0cc3SGreg Clayton arch_names.PutCString (module_spec.GetArchitecture().GetArchitectureName()); 300e996fd30SGreg Clayton } 301e996fd30SGreg Clayton 302e996fd30SGreg Clayton if (error.Fail() || !exe_module_sp) 303e996fd30SGreg Clayton { 30439945dccSGreg Clayton if (exe_file.Readable()) 30539945dccSGreg Clayton { 306b5ad4ec7SGreg Clayton error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s", 307b5ad4ec7SGreg Clayton exe_file.GetPath().c_str(), 30857abc5d6SGreg Clayton GetPluginName().GetCString(), 309e996fd30SGreg Clayton arch_names.GetString().c_str()); 310e996fd30SGreg Clayton } 31139945dccSGreg Clayton else 31239945dccSGreg Clayton { 31339945dccSGreg Clayton error.SetErrorStringWithFormat("'%s' is not readable", exe_file.GetPath().c_str()); 31439945dccSGreg Clayton } 31539945dccSGreg Clayton } 316e996fd30SGreg Clayton } 317e996fd30SGreg Clayton } 318e996fd30SGreg Clayton 319e996fd30SGreg Clayton return error; 320e996fd30SGreg Clayton } 321e996fd30SGreg Clayton 322e996fd30SGreg Clayton Error 323fc995725SSteve Pucci PlatformLinux::GetFileWithUUID (const FileSpec &platform_file, 32428041352SGreg Clayton const UUID *uuid_ptr, FileSpec &local_file) 325e996fd30SGreg Clayton { 32628041352SGreg Clayton if (IsRemote()) 32728041352SGreg Clayton { 32828041352SGreg Clayton if (m_remote_platform_sp) 329fc995725SSteve Pucci return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file); 33028041352SGreg Clayton } 33128041352SGreg Clayton 332e996fd30SGreg Clayton // Default to the local case 333e996fd30SGreg Clayton local_file = platform_file; 334e996fd30SGreg Clayton return Error(); 335e996fd30SGreg Clayton } 336e996fd30SGreg Clayton 337e996fd30SGreg Clayton 338e996fd30SGreg Clayton //------------------------------------------------------------------ 339e996fd30SGreg Clayton /// Default Constructor 340e996fd30SGreg Clayton //------------------------------------------------------------------ 34128041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) : 342015d818bSTodd Fiala PlatformPOSIX(is_host) // This is the local host platform 343e996fd30SGreg Clayton { 344e996fd30SGreg Clayton } 345e996fd30SGreg Clayton 346e996fd30SGreg Clayton //------------------------------------------------------------------ 347e996fd30SGreg Clayton /// Destructor. 348e996fd30SGreg Clayton /// 349e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be 350e996fd30SGreg Clayton /// inherited from by the plug-in instance. 351e996fd30SGreg Clayton //------------------------------------------------------------------ 352e996fd30SGreg Clayton PlatformLinux::~PlatformLinux() 353e996fd30SGreg Clayton { 354e996fd30SGreg Clayton } 355e996fd30SGreg Clayton 356e996fd30SGreg Clayton bool 35713e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) 358e996fd30SGreg Clayton { 35928041352SGreg Clayton bool success = false; 36028041352SGreg Clayton if (IsHost()) 36128041352SGreg Clayton { 36228041352SGreg Clayton success = Platform::GetProcessInfo (pid, process_info); 36328041352SGreg Clayton } 36428041352SGreg Clayton else 36528041352SGreg Clayton { 36628041352SGreg Clayton if (m_remote_platform_sp) 36728041352SGreg Clayton success = m_remote_platform_sp->GetProcessInfo (pid, process_info); 36828041352SGreg Clayton } 36928041352SGreg Clayton return success; 370e996fd30SGreg Clayton } 371e996fd30SGreg Clayton 372e996fd30SGreg Clayton bool 373e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch) 374e996fd30SGreg Clayton { 375e996fd30SGreg Clayton if (idx == 0) 376e996fd30SGreg Clayton { 37713b18261SZachary Turner arch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 378e996fd30SGreg Clayton return arch.IsValid(); 379e996fd30SGreg Clayton } 380542e4075SGreg Clayton else if (idx == 1) 381542e4075SGreg Clayton { 382542e4075SGreg Clayton // If the default host architecture is 64-bit, look for a 32-bit variant 38313b18261SZachary Turner ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 384542e4075SGreg Clayton if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) 385542e4075SGreg Clayton { 38613b18261SZachary Turner arch = HostInfo::GetArchitecture(HostInfo::eArchKind32); 387542e4075SGreg Clayton return arch.IsValid(); 388542e4075SGreg Clayton } 389542e4075SGreg Clayton } 390e996fd30SGreg Clayton return false; 391e996fd30SGreg Clayton } 392ecc11474SStephen Wilson 393ecc11474SStephen Wilson void 394ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm) 395ecc11474SStephen Wilson { 3963be69dacSDaniel Malea Platform::GetStatus(strm); 397ecc11474SStephen Wilson 398b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX 399b2f1fb29SVirgile Bello struct utsname un; 400b2f1fb29SVirgile Bello 4013be69dacSDaniel Malea if (uname(&un)) 4023be69dacSDaniel Malea return; 4033be69dacSDaniel Malea 4043be69dacSDaniel Malea strm.Printf (" Kernel: %s\n", un.sysname); 4053be69dacSDaniel Malea strm.Printf (" Release: %s\n", un.release); 4063be69dacSDaniel Malea strm.Printf (" Version: %s\n", un.version); 407b2f1fb29SVirgile Bello #endif 408ecc11474SStephen Wilson } 409ecc11474SStephen Wilson 410ecc11474SStephen Wilson size_t 411ecc11474SStephen Wilson PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target, 412ecc11474SStephen Wilson BreakpointSite *bp_site) 413ecc11474SStephen Wilson { 414ecc11474SStephen Wilson ArchSpec arch = target.GetArchitecture(); 41528041352SGreg Clayton const uint8_t *trap_opcode = NULL; 41628041352SGreg Clayton size_t trap_opcode_size = 0; 417ecc11474SStephen Wilson 418906e9acfSGreg Clayton switch (arch.GetMachine()) 419ecc11474SStephen Wilson { 420ecc11474SStephen Wilson default: 421ecc11474SStephen Wilson assert(false && "CPU type not supported!"); 422ecc11474SStephen Wilson break; 423ecc11474SStephen Wilson 424*2afc5966STodd Fiala case llvm::Triple::aarch64: 425*2afc5966STodd Fiala { 426*2afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; 427*2afc5966STodd Fiala trap_opcode = g_aarch64_opcode; 428*2afc5966STodd Fiala trap_opcode_size = sizeof(g_aarch64_opcode); 429*2afc5966STodd Fiala } 430*2afc5966STodd Fiala break; 431906e9acfSGreg Clayton case llvm::Triple::x86: 432906e9acfSGreg Clayton case llvm::Triple::x86_64: 43328041352SGreg Clayton { 43428041352SGreg Clayton static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC }; 43528041352SGreg Clayton trap_opcode = g_i386_breakpoint_opcode; 43628041352SGreg Clayton trap_opcode_size = sizeof(g_i386_breakpoint_opcode); 43728041352SGreg Clayton } 438ecc11474SStephen Wilson break; 439906e9acfSGreg Clayton case llvm::Triple::hexagon: 4408006d319SDeepak Panickal { 4418006d319SDeepak Panickal static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 }; 4428006d319SDeepak Panickal trap_opcode = g_hex_opcode; 4438006d319SDeepak Panickal trap_opcode_size = sizeof(g_hex_opcode); 4448006d319SDeepak Panickal } 4458006d319SDeepak Panickal break; 446ecc11474SStephen Wilson } 447ecc11474SStephen Wilson 44828041352SGreg Clayton if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size)) 44928041352SGreg Clayton return trap_opcode_size; 45028041352SGreg Clayton return 0; 45128041352SGreg Clayton } 45228041352SGreg Clayton 45328041352SGreg Clayton Error 45428041352SGreg Clayton PlatformLinux::LaunchProcess (ProcessLaunchInfo &launch_info) 45528041352SGreg Clayton { 456015d818bSTodd Fiala Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); 45728041352SGreg Clayton Error error; 45828041352SGreg Clayton 45928041352SGreg Clayton if (IsHost()) 46028041352SGreg Clayton { 461015d818bSTodd Fiala if (log) 462015d818bSTodd Fiala log->Printf ("PlatformLinux::%s() launching process as host", __FUNCTION__); 463015d818bSTodd Fiala 46428041352SGreg Clayton if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell)) 46528041352SGreg Clayton { 46628041352SGreg Clayton const bool is_localhost = true; 467d1cf11a7SGreg Clayton const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug); 468d1cf11a7SGreg Clayton const bool first_arg_is_full_shell_command = false; 469d3990793SJim Ingham uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info); 470d1cf11a7SGreg Clayton if (!launch_info.ConvertArgumentsForLaunchingInShell (error, 471d1cf11a7SGreg Clayton is_localhost, 472d1cf11a7SGreg Clayton will_debug, 473df0ae22fSJim Ingham first_arg_is_full_shell_command, 474df0ae22fSJim Ingham num_resumes)) 47528041352SGreg Clayton return error; 47628041352SGreg Clayton } 47728041352SGreg Clayton error = Platform::LaunchProcess (launch_info); 47828041352SGreg Clayton } 47928041352SGreg Clayton else 48028041352SGreg Clayton { 481015d818bSTodd Fiala if (m_remote_platform_sp) 482015d818bSTodd Fiala { 483015d818bSTodd Fiala if (log) 484015d818bSTodd Fiala log->Printf ("PlatformLinux::%s() attempting to launch remote process", __FUNCTION__); 485015d818bSTodd Fiala error = m_remote_platform_sp->LaunchProcess (launch_info); 486015d818bSTodd Fiala } 487015d818bSTodd Fiala else 488015d818bSTodd Fiala { 489015d818bSTodd Fiala if (log) 490015d818bSTodd Fiala log->Printf ("PlatformLinux::%s() attempted to launch process but is not the host and no remote platform set", __FUNCTION__); 49128041352SGreg Clayton error.SetErrorString ("the platform is not currently connected"); 49228041352SGreg Clayton } 493015d818bSTodd Fiala } 49428041352SGreg Clayton return error; 495ecc11474SStephen Wilson } 49613e8e1c3SJohnny Chen 497015d818bSTodd Fiala // Linux processes can not be launched by spawning and attaching. 498015d818bSTodd Fiala bool 499015d818bSTodd Fiala PlatformLinux::CanDebugProcess () 500015d818bSTodd Fiala { 501015d818bSTodd Fiala // If we're the host, launch via normal host setup. 502015d818bSTodd Fiala if (IsHost ()) 503015d818bSTodd Fiala return false; 504015d818bSTodd Fiala 505015d818bSTodd Fiala // If we're connected, we can debug. 506015d818bSTodd Fiala return IsConnected (); 507015d818bSTodd Fiala } 508015d818bSTodd Fiala 50913e8e1c3SJohnny Chen lldb::ProcessSP 510fb2b629dSPeter Collingbourne PlatformLinux::Attach(ProcessAttachInfo &attach_info, 51113e8e1c3SJohnny Chen Debugger &debugger, 51213e8e1c3SJohnny Chen Target *target, 51313e8e1c3SJohnny Chen Listener &listener, 51413e8e1c3SJohnny Chen Error &error) 51513e8e1c3SJohnny Chen { 51628041352SGreg Clayton lldb::ProcessSP process_sp; 51728041352SGreg Clayton if (IsHost()) 51828041352SGreg Clayton { 51928041352SGreg Clayton if (target == NULL) 52028041352SGreg Clayton { 52128041352SGreg Clayton TargetSP new_target_sp; 52228041352SGreg Clayton ArchSpec emptyArchSpec; 52328041352SGreg Clayton 52428041352SGreg Clayton error = debugger.GetTargetList().CreateTarget (debugger, 525a0ca6601SGreg Clayton NULL, 52628041352SGreg Clayton emptyArchSpec, 52728041352SGreg Clayton false, 52828041352SGreg Clayton m_remote_platform_sp, 52928041352SGreg Clayton new_target_sp); 53028041352SGreg Clayton target = new_target_sp.get(); 53128041352SGreg Clayton } 53228041352SGreg Clayton else 53328041352SGreg Clayton error.Clear(); 53428041352SGreg Clayton 53528041352SGreg Clayton if (target && error.Success()) 53628041352SGreg Clayton { 53728041352SGreg Clayton debugger.GetTargetList().SetSelectedTarget(target); 53828041352SGreg Clayton 5390c90ef47SGreg Clayton process_sp = target->CreateProcess (listener, 5400c90ef47SGreg Clayton attach_info.GetProcessPluginName(), 5410c90ef47SGreg Clayton NULL); 54228041352SGreg Clayton 54328041352SGreg Clayton if (process_sp) 54428041352SGreg Clayton error = process_sp->Attach (attach_info); 54528041352SGreg Clayton } 54628041352SGreg Clayton } 54728041352SGreg Clayton else 54828041352SGreg Clayton { 54928041352SGreg Clayton if (m_remote_platform_sp) 55028041352SGreg Clayton process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, listener, error); 55128041352SGreg Clayton else 55228041352SGreg Clayton error.SetErrorString ("the platform is not currently connected"); 55328041352SGreg Clayton } 55428041352SGreg Clayton return process_sp; 55513e8e1c3SJohnny Chen } 5562094dbf4SJason Molenda 5572094dbf4SJason Molenda void 5582094dbf4SJason Molenda PlatformLinux::CalculateTrapHandlerSymbolNames () 5592094dbf4SJason Molenda { 5602094dbf4SJason Molenda m_trap_handlers.push_back (ConstString ("_sigtramp")); 5612094dbf4SJason Molenda } 562af245d11STodd Fiala 563af245d11STodd Fiala Error 564af245d11STodd Fiala PlatformLinux::LaunchNativeProcess ( 565af245d11STodd Fiala ProcessLaunchInfo &launch_info, 566af245d11STodd Fiala lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, 567af245d11STodd Fiala NativeProcessProtocolSP &process_sp) 568af245d11STodd Fiala { 569af245d11STodd Fiala #if !defined(__linux__) 570af245d11STodd Fiala return Error("only implemented on Linux hosts"); 571af245d11STodd Fiala #else 572af245d11STodd Fiala if (!IsHost ()) 573af245d11STodd Fiala return Error("PlatformLinux::%s (): cannot launch a debug process when not the host", __FUNCTION__); 574af245d11STodd Fiala 575af245d11STodd Fiala // Retrieve the exe module. 576af245d11STodd Fiala lldb::ModuleSP exe_module_sp; 577af245d11STodd Fiala 578af245d11STodd Fiala Error error = ResolveExecutable ( 579af245d11STodd Fiala launch_info.GetExecutableFile (), 580af245d11STodd Fiala launch_info.GetArchitecture (), 581af245d11STodd Fiala exe_module_sp, 582af245d11STodd Fiala NULL); 583af245d11STodd Fiala 584af245d11STodd Fiala if (!error.Success ()) 585af245d11STodd Fiala return error; 586af245d11STodd Fiala 587af245d11STodd Fiala if (!exe_module_sp) 588af245d11STodd Fiala return Error("exe_module_sp could not be resolved for %s", launch_info.GetExecutableFile ().GetPath ().c_str ()); 589af245d11STodd Fiala 590af245d11STodd Fiala // Launch it for debugging 591af245d11STodd Fiala error = NativeProcessLinux::LaunchProcess ( 592af245d11STodd Fiala exe_module_sp.get (), 593af245d11STodd Fiala launch_info, 594af245d11STodd Fiala native_delegate, 595af245d11STodd Fiala process_sp); 596af245d11STodd Fiala 597af245d11STodd Fiala return error; 598af245d11STodd Fiala #endif 599af245d11STodd Fiala } 600af245d11STodd Fiala 601af245d11STodd Fiala Error 602af245d11STodd Fiala PlatformLinux::AttachNativeProcess (lldb::pid_t pid, 603af245d11STodd Fiala lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, 604af245d11STodd Fiala NativeProcessProtocolSP &process_sp) 605af245d11STodd Fiala { 606af245d11STodd Fiala #if !defined(__linux__) 607af245d11STodd Fiala return Error("only implemented on Linux hosts"); 608af245d11STodd Fiala #else 609af245d11STodd Fiala if (!IsHost ()) 610af245d11STodd Fiala return Error("PlatformLinux::%s (): cannot attach to a debug process when not the host", __FUNCTION__); 611af245d11STodd Fiala 612af245d11STodd Fiala // Launch it for debugging 613af245d11STodd Fiala return NativeProcessLinux::AttachToProcess (pid, native_delegate, process_sp); 614af245d11STodd Fiala #endif 615af245d11STodd Fiala } 616