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" 11b2f1fb29SVirgile Bello #include "lldb/Host/Config.h" 12e996fd30SGreg Clayton 13e996fd30SGreg Clayton // C Includes 14ecc11474SStephen Wilson #include <stdio.h> 15b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX 16ecc11474SStephen Wilson #include <sys/utsname.h> 17b2f1fb29SVirgile Bello #endif 18ecc11474SStephen Wilson 19e996fd30SGreg Clayton // C++ Includes 20e996fd30SGreg Clayton // Other libraries and framework includes 21e996fd30SGreg Clayton // Project includes 222586e94bSJason Molenda #include "lldb/Breakpoint/BreakpointLocation.h" 2328041352SGreg Clayton #include "lldb/Core/Debugger.h" 24348fb385STodd Fiala #include "lldb/Core/Error.h" 25015d818bSTodd Fiala #include "lldb/Core/Log.h" 26e996fd30SGreg Clayton #include "lldb/Core/Module.h" 27e996fd30SGreg Clayton #include "lldb/Core/ModuleList.h" 281f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h" 29ecc11474SStephen Wilson #include "lldb/Core/PluginManager.h" 30348fb385STodd Fiala #include "lldb/Core/State.h" 31e996fd30SGreg Clayton #include "lldb/Core/StreamString.h" 32e996fd30SGreg Clayton #include "lldb/Host/FileSpec.h" 3313b18261SZachary Turner #include "lldb/Host/HostInfo.h" 34348fb385STodd Fiala #include "lldb/Interpreter/OptionValueProperties.h" 35348fb385STodd Fiala #include "lldb/Interpreter/Property.h" 36ecc11474SStephen Wilson #include "lldb/Target/Target.h" 37e996fd30SGreg Clayton #include "lldb/Target/Process.h" 38e996fd30SGreg Clayton 39a868c13cSBruce Mitchener // Define these constants from Linux mman.h for use when targeting 4096ad3de5SRobert Flack // remote linux systems even when host has different values. 4196ad3de5SRobert Flack #define MAP_PRIVATE 2 4296ad3de5SRobert Flack #define MAP_ANON 0x20 4396ad3de5SRobert Flack 44e996fd30SGreg Clayton using namespace lldb; 45e996fd30SGreg Clayton using namespace lldb_private; 46db264a6dSTamas Berghammer using namespace lldb_private::platform_linux; 47e996fd30SGreg Clayton 4828041352SGreg Clayton static uint32_t g_initialize_count = 0; 4928041352SGreg Clayton 50281961a8STodd Fiala //------------------------------------------------------------------ 51281961a8STodd Fiala /// Code to handle the PlatformLinux settings 52281961a8STodd Fiala //------------------------------------------------------------------ 53281961a8STodd Fiala 54348fb385STodd Fiala namespace 55348fb385STodd Fiala { 56db264a6dSTamas Berghammer class PlatformLinuxProperties : public Properties 57db264a6dSTamas Berghammer { 58db264a6dSTamas Berghammer public: 59db264a6dSTamas Berghammer PlatformLinuxProperties(); 60db264a6dSTamas Berghammer 61222b937cSEugene Zelenko ~PlatformLinuxProperties() override = default; 62222b937cSEugene Zelenko 63222b937cSEugene Zelenko static ConstString& 64222b937cSEugene Zelenko GetSettingName (); 65db264a6dSTamas Berghammer 66db264a6dSTamas Berghammer private: 67db264a6dSTamas Berghammer static const PropertyDefinition* 68db264a6dSTamas Berghammer GetStaticPropertyDefinitions(); 69db264a6dSTamas Berghammer }; 70db264a6dSTamas Berghammer 71db264a6dSTamas Berghammer typedef std::shared_ptr<PlatformLinuxProperties> PlatformLinuxPropertiesSP; 72db264a6dSTamas Berghammer 73db264a6dSTamas Berghammer } // anonymous namespace 74db264a6dSTamas Berghammer 75db264a6dSTamas Berghammer PlatformLinuxProperties::PlatformLinuxProperties() : 76db264a6dSTamas Berghammer Properties () 77db264a6dSTamas Berghammer { 78db264a6dSTamas Berghammer m_collection_sp.reset (new OptionValueProperties(GetSettingName ())); 79db264a6dSTamas Berghammer m_collection_sp->Initialize (GetStaticPropertyDefinitions ()); 80db264a6dSTamas Berghammer } 81db264a6dSTamas Berghammer 82db264a6dSTamas Berghammer ConstString& 83db264a6dSTamas Berghammer PlatformLinuxProperties::GetSettingName () 84db264a6dSTamas Berghammer { 85db264a6dSTamas Berghammer static ConstString g_setting_name("linux"); 86db264a6dSTamas Berghammer return g_setting_name; 87db264a6dSTamas Berghammer } 88db264a6dSTamas Berghammer 89348fb385STodd Fiala const PropertyDefinition* 90db264a6dSTamas Berghammer PlatformLinuxProperties::GetStaticPropertyDefinitions() 91348fb385STodd Fiala { 92281961a8STodd Fiala static PropertyDefinition 93281961a8STodd Fiala g_properties[] = 94281961a8STodd Fiala { 95281961a8STodd Fiala { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL } 96281961a8STodd Fiala }; 97281961a8STodd Fiala 98348fb385STodd Fiala return g_properties; 99348fb385STodd Fiala } 100281961a8STodd Fiala 101281961a8STodd Fiala static const PlatformLinuxPropertiesSP & 102281961a8STodd Fiala GetGlobalProperties() 103281961a8STodd Fiala { 104281961a8STodd Fiala static PlatformLinuxPropertiesSP g_settings_sp; 105281961a8STodd Fiala if (!g_settings_sp) 106281961a8STodd Fiala g_settings_sp.reset (new PlatformLinuxProperties ()); 107281961a8STodd Fiala return g_settings_sp; 108281961a8STodd Fiala } 109281961a8STodd Fiala 110281961a8STodd Fiala void 111db264a6dSTamas Berghammer PlatformLinux::DebuggerInitialize (Debugger &debugger) 112281961a8STodd Fiala { 113281961a8STodd Fiala if (!PluginManager::GetSettingForPlatformPlugin (debugger, PlatformLinuxProperties::GetSettingName())) 114281961a8STodd Fiala { 115281961a8STodd Fiala const bool is_global_setting = true; 116281961a8STodd Fiala PluginManager::CreateSettingForPlatformPlugin (debugger, 117281961a8STodd Fiala GetGlobalProperties()->GetValueProperties(), 118281961a8STodd Fiala ConstString ("Properties for the PlatformLinux plug-in."), 119281961a8STodd Fiala is_global_setting); 120281961a8STodd Fiala } 121281961a8STodd Fiala } 122281961a8STodd Fiala 123281961a8STodd Fiala //------------------------------------------------------------------ 124281961a8STodd Fiala 125615eb7e6SGreg Clayton PlatformSP 126b3a40ba8SGreg Clayton PlatformLinux::CreateInstance (bool force, const ArchSpec *arch) 127ecc11474SStephen Wilson { 128db264a6dSTamas Berghammer Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); 129015d818bSTodd Fiala if (log) 130015d818bSTodd Fiala { 131015d818bSTodd Fiala const char *arch_name; 132015d818bSTodd Fiala if (arch && arch->GetArchitectureName ()) 133015d818bSTodd Fiala arch_name = arch->GetArchitectureName (); 134015d818bSTodd Fiala else 135015d818bSTodd Fiala arch_name = "<null>"; 136015d818bSTodd Fiala 137015d818bSTodd Fiala const char *triple_cstr = arch ? arch->GetTriple ().getTriple ().c_str() : "<null>"; 138015d818bSTodd Fiala 139015d818bSTodd Fiala log->Printf ("PlatformLinux::%s(force=%s, arch={%s,%s})", __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr); 140015d818bSTodd Fiala } 141015d818bSTodd Fiala 142b3a40ba8SGreg Clayton bool create = force; 143b3a40ba8SGreg Clayton if (create == false && arch && arch->IsValid()) 144b3a40ba8SGreg Clayton { 145b3a40ba8SGreg Clayton const llvm::Triple &triple = arch->GetTriple(); 14670512317SGreg Clayton switch (triple.getOS()) 14770512317SGreg Clayton { 14870512317SGreg Clayton case llvm::Triple::Linux: 149b1da257aSTed Woodward create = true; 15070512317SGreg Clayton break; 15170512317SGreg Clayton 152dbc6c0bbSGreg Clayton #if defined(__linux__) 153dbc6c0bbSGreg Clayton // Only accept "unknown" for the OS if the host is linux and 1546a7f3338SBruce Mitchener // it "unknown" wasn't specified (it was just returned because it 155dbc6c0bbSGreg Clayton // was NOT specified) 156015d818bSTodd Fiala case llvm::Triple::OSType::UnknownOS: 15770512317SGreg Clayton create = !arch->TripleOSWasSpecified(); 15870512317SGreg Clayton break; 159dbc6c0bbSGreg Clayton #endif 16070512317SGreg Clayton default: 16170512317SGreg Clayton break; 16270512317SGreg Clayton } 16370512317SGreg Clayton } 164015d818bSTodd Fiala 165b3a40ba8SGreg Clayton if (create) 166015d818bSTodd Fiala { 167015d818bSTodd Fiala if (log) 168015d818bSTodd Fiala log->Printf ("PlatformLinux::%s() creating remote-linux platform", __FUNCTION__); 169615eb7e6SGreg Clayton return PlatformSP(new PlatformLinux(false)); 170015d818bSTodd Fiala } 171015d818bSTodd Fiala 172015d818bSTodd Fiala if (log) 173015d818bSTodd Fiala log->Printf ("PlatformLinux::%s() aborting creation of remote-linux platform", __FUNCTION__); 174015d818bSTodd Fiala 175615eb7e6SGreg Clayton return PlatformSP(); 176ecc11474SStephen Wilson } 177ecc11474SStephen Wilson 178db264a6dSTamas Berghammer ConstString 17957abc5d6SGreg Clayton PlatformLinux::GetPluginNameStatic (bool is_host) 180ecc11474SStephen Wilson { 18128041352SGreg Clayton if (is_host) 18257abc5d6SGreg Clayton { 18357abc5d6SGreg Clayton static ConstString g_host_name(Platform::GetHostPlatformName ()); 18457abc5d6SGreg Clayton return g_host_name; 18557abc5d6SGreg Clayton } 18628041352SGreg Clayton else 18757abc5d6SGreg Clayton { 18857abc5d6SGreg Clayton static ConstString g_remote_name("remote-linux"); 18957abc5d6SGreg Clayton return g_remote_name; 19057abc5d6SGreg Clayton } 19128041352SGreg Clayton } 19228041352SGreg Clayton 19328041352SGreg Clayton const char * 19428041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic (bool is_host) 19528041352SGreg Clayton { 19628041352SGreg Clayton if (is_host) 19728041352SGreg Clayton return "Local Linux user platform plug-in."; 19828041352SGreg Clayton else 19928041352SGreg Clayton return "Remote Linux user platform plug-in."; 200ecc11474SStephen Wilson } 201ecc11474SStephen Wilson 202db264a6dSTamas Berghammer ConstString 20357abc5d6SGreg Clayton PlatformLinux::GetPluginName() 20457abc5d6SGreg Clayton { 20557abc5d6SGreg Clayton return GetPluginNameStatic(IsHost()); 20657abc5d6SGreg Clayton } 20757abc5d6SGreg Clayton 208e996fd30SGreg Clayton void 209e996fd30SGreg Clayton PlatformLinux::Initialize () 210e996fd30SGreg Clayton { 2113c4f89d7STamas Berghammer PlatformPOSIX::Initialize (); 2123c4f89d7STamas Berghammer 21328041352SGreg Clayton if (g_initialize_count++ == 0) 214ecc11474SStephen Wilson { 2151c6a1ea9STamas Berghammer #if defined(__linux__) && !defined(__ANDROID__) 21628041352SGreg Clayton PlatformSP default_platform_sp (new PlatformLinux(true)); 21713b18261SZachary Turner default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 218615eb7e6SGreg Clayton Platform::SetHostPlatform (default_platform_sp); 21928041352SGreg Clayton #endif 22057abc5d6SGreg Clayton PluginManager::RegisterPlugin(PlatformLinux::GetPluginNameStatic(false), 22128041352SGreg Clayton PlatformLinux::GetPluginDescriptionStatic(false), 222281961a8STodd Fiala PlatformLinux::CreateInstance, 223281961a8STodd Fiala PlatformLinux::DebuggerInitialize); 224ecc11474SStephen Wilson } 225e996fd30SGreg Clayton } 226e996fd30SGreg Clayton 227e996fd30SGreg Clayton void 228e996fd30SGreg Clayton PlatformLinux::Terminate () 229e996fd30SGreg Clayton { 23028041352SGreg Clayton if (g_initialize_count > 0) 23128041352SGreg Clayton { 23228041352SGreg Clayton if (--g_initialize_count == 0) 23328041352SGreg Clayton { 23428041352SGreg Clayton PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance); 235e996fd30SGreg Clayton } 23628041352SGreg Clayton } 2373c4f89d7STamas Berghammer 2383c4f89d7STamas Berghammer PlatformPOSIX::Terminate (); 23928041352SGreg Clayton } 240e996fd30SGreg Clayton 241e996fd30SGreg Clayton Error 2428012cadbSGreg Clayton PlatformLinux::ResolveExecutable (const ModuleSpec &ms, 243ea5e0cc3SGreg Clayton lldb::ModuleSP &exe_module_sp, 244ea5e0cc3SGreg Clayton const FileSpecList *module_search_paths_ptr) 245e996fd30SGreg Clayton { 246e996fd30SGreg Clayton Error error; 247e996fd30SGreg Clayton // Nothing special to do here, just use the actual file and architecture 248e996fd30SGreg Clayton 24928041352SGreg Clayton char exe_path[PATH_MAX]; 2508012cadbSGreg Clayton ModuleSpec resolved_module_spec (ms); 251e996fd30SGreg Clayton 25228041352SGreg Clayton if (IsHost()) 25328041352SGreg Clayton { 25428041352SGreg Clayton // If we have "ls" as the exe_file, resolve the executable location based on 255e996fd30SGreg Clayton // the current path variables 2568012cadbSGreg Clayton if (!resolved_module_spec.GetFileSpec().Exists()) 25728041352SGreg Clayton { 2588012cadbSGreg Clayton resolved_module_spec.GetFileSpec().GetPath(exe_path, sizeof(exe_path)); 2598012cadbSGreg Clayton resolved_module_spec.GetFileSpec().SetFile(exe_path, true); 26028041352SGreg Clayton } 26128041352SGreg Clayton 2628012cadbSGreg Clayton if (!resolved_module_spec.GetFileSpec().Exists()) 2638012cadbSGreg Clayton resolved_module_spec.GetFileSpec().ResolveExecutableLocation (); 264e996fd30SGreg Clayton 2658012cadbSGreg Clayton if (resolved_module_spec.GetFileSpec().Exists()) 26628041352SGreg Clayton error.Clear(); 26728041352SGreg Clayton else 26828041352SGreg Clayton { 2698012cadbSGreg Clayton error.SetErrorStringWithFormat("unable to find executable for '%s'", resolved_module_spec.GetFileSpec().GetPath().c_str()); 27028041352SGreg Clayton } 27128041352SGreg Clayton } 27228041352SGreg Clayton else 27328041352SGreg Clayton { 27428041352SGreg Clayton if (m_remote_platform_sp) 27528041352SGreg Clayton { 276a3f245ffSOleksiy Vyalov error = GetCachedExecutable (resolved_module_spec, exe_module_sp, module_search_paths_ptr, *m_remote_platform_sp); 27728041352SGreg Clayton } 27828041352SGreg Clayton else 27928041352SGreg Clayton { 28028041352SGreg Clayton // We may connect to a process and use the provided executable (Don't use local $PATH). 281e996fd30SGreg Clayton 2828012cadbSGreg Clayton if (resolved_module_spec.GetFileSpec().Exists()) 28328041352SGreg Clayton error.Clear(); 28428041352SGreg Clayton else 28528041352SGreg Clayton error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path); 28628041352SGreg Clayton } 28728041352SGreg Clayton } 28828041352SGreg Clayton 28928041352SGreg Clayton if (error.Success()) 290e996fd30SGreg Clayton { 2918012cadbSGreg Clayton if (resolved_module_spec.GetArchitecture().IsValid()) 292e996fd30SGreg Clayton { 2938012cadbSGreg Clayton error = ModuleList::GetSharedModule (resolved_module_spec, 294e996fd30SGreg Clayton exe_module_sp, 295e996fd30SGreg Clayton NULL, 2960c90ef47SGreg Clayton NULL, 297e996fd30SGreg Clayton NULL); 2989f0013d8SMichael Sartain if (error.Fail()) 2999f0013d8SMichael Sartain { 3009f0013d8SMichael Sartain // If we failed, it may be because the vendor and os aren't known. If that is the 3019f0013d8SMichael Sartain // case, try setting them to the host architecture and give it another try. 3028012cadbSGreg Clayton llvm::Triple &module_triple = resolved_module_spec.GetArchitecture().GetTriple(); 3039f0013d8SMichael Sartain bool is_vendor_specified = (module_triple.getVendor() != llvm::Triple::UnknownVendor); 3049f0013d8SMichael Sartain bool is_os_specified = (module_triple.getOS() != llvm::Triple::UnknownOS); 3059f0013d8SMichael Sartain if (!is_vendor_specified || !is_os_specified) 3069f0013d8SMichael Sartain { 30713b18261SZachary Turner const llvm::Triple &host_triple = HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple(); 3089f0013d8SMichael Sartain 3099f0013d8SMichael Sartain if (!is_vendor_specified) 3109f0013d8SMichael Sartain module_triple.setVendorName (host_triple.getVendorName()); 3119f0013d8SMichael Sartain if (!is_os_specified) 3129f0013d8SMichael Sartain module_triple.setOSName (host_triple.getOSName()); 3139f0013d8SMichael Sartain 3148012cadbSGreg Clayton error = ModuleList::GetSharedModule (resolved_module_spec, 3159f0013d8SMichael Sartain exe_module_sp, 3169f0013d8SMichael Sartain NULL, 3179f0013d8SMichael Sartain NULL, 3189f0013d8SMichael Sartain NULL); 3199f0013d8SMichael Sartain } 3209f0013d8SMichael Sartain } 321e996fd30SGreg Clayton 322e635db49SSean Callanan // TODO find out why exe_module_sp might be NULL 323e635db49SSean Callanan if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL) 324e996fd30SGreg Clayton { 325e996fd30SGreg Clayton exe_module_sp.reset(); 326b5ad4ec7SGreg Clayton error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s", 3278012cadbSGreg Clayton resolved_module_spec.GetFileSpec().GetPath().c_str(), 3288012cadbSGreg Clayton resolved_module_spec.GetArchitecture().GetArchitectureName()); 329e996fd30SGreg Clayton } 330e996fd30SGreg Clayton } 331e996fd30SGreg Clayton else 332e996fd30SGreg Clayton { 333e996fd30SGreg Clayton // No valid architecture was specified, ask the platform for 334e996fd30SGreg Clayton // the architectures that we should be using (in the correct order) 335e996fd30SGreg Clayton // and see if we can find a match that way 336e996fd30SGreg Clayton StreamString arch_names; 3378012cadbSGreg Clayton for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, resolved_module_spec.GetArchitecture()); ++idx) 338e996fd30SGreg Clayton { 3398012cadbSGreg Clayton error = ModuleList::GetSharedModule (resolved_module_spec, 340e996fd30SGreg Clayton exe_module_sp, 341e996fd30SGreg Clayton NULL, 3420c90ef47SGreg Clayton NULL, 343e996fd30SGreg Clayton NULL); 344e996fd30SGreg Clayton // Did we find an executable using one of the 345e996fd30SGreg Clayton if (error.Success()) 346e996fd30SGreg Clayton { 347e996fd30SGreg Clayton if (exe_module_sp && exe_module_sp->GetObjectFile()) 348e996fd30SGreg Clayton break; 349e996fd30SGreg Clayton else 350e996fd30SGreg Clayton error.SetErrorToGenericError(); 351e996fd30SGreg Clayton } 352e996fd30SGreg Clayton 353e996fd30SGreg Clayton if (idx > 0) 354e996fd30SGreg Clayton arch_names.PutCString (", "); 3558012cadbSGreg Clayton arch_names.PutCString (resolved_module_spec.GetArchitecture().GetArchitectureName()); 356e996fd30SGreg Clayton } 357e996fd30SGreg Clayton 358e996fd30SGreg Clayton if (error.Fail() || !exe_module_sp) 359e996fd30SGreg Clayton { 3608012cadbSGreg Clayton if (resolved_module_spec.GetFileSpec().Readable()) 36139945dccSGreg Clayton { 362b5ad4ec7SGreg Clayton error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s", 3638012cadbSGreg Clayton resolved_module_spec.GetFileSpec().GetPath().c_str(), 36457abc5d6SGreg Clayton GetPluginName().GetCString(), 365e996fd30SGreg Clayton arch_names.GetString().c_str()); 366e996fd30SGreg Clayton } 36739945dccSGreg Clayton else 36839945dccSGreg Clayton { 3698012cadbSGreg Clayton error.SetErrorStringWithFormat("'%s' is not readable", resolved_module_spec.GetFileSpec().GetPath().c_str()); 37039945dccSGreg Clayton } 37139945dccSGreg Clayton } 372e996fd30SGreg Clayton } 373e996fd30SGreg Clayton } 374e996fd30SGreg Clayton 375e996fd30SGreg Clayton return error; 376e996fd30SGreg Clayton } 377e996fd30SGreg Clayton 378e996fd30SGreg Clayton Error 379fc995725SSteve Pucci PlatformLinux::GetFileWithUUID (const FileSpec &platform_file, 38028041352SGreg Clayton const UUID *uuid_ptr, FileSpec &local_file) 381e996fd30SGreg Clayton { 38228041352SGreg Clayton if (IsRemote()) 38328041352SGreg Clayton { 38428041352SGreg Clayton if (m_remote_platform_sp) 385fc995725SSteve Pucci return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file); 38628041352SGreg Clayton } 38728041352SGreg Clayton 388e996fd30SGreg Clayton // Default to the local case 389e996fd30SGreg Clayton local_file = platform_file; 390e996fd30SGreg Clayton return Error(); 391e996fd30SGreg Clayton } 392e996fd30SGreg Clayton 393e996fd30SGreg Clayton //------------------------------------------------------------------ 394e996fd30SGreg Clayton /// Default Constructor 395e996fd30SGreg Clayton //------------------------------------------------------------------ 39628041352SGreg Clayton PlatformLinux::PlatformLinux (bool is_host) : 397015d818bSTodd Fiala PlatformPOSIX(is_host) // This is the local host platform 398e996fd30SGreg Clayton { 399e996fd30SGreg Clayton } 400e996fd30SGreg Clayton 401e996fd30SGreg Clayton //------------------------------------------------------------------ 402e996fd30SGreg Clayton /// Destructor. 403e996fd30SGreg Clayton /// 404e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be 405e996fd30SGreg Clayton /// inherited from by the plug-in instance. 406e996fd30SGreg Clayton //------------------------------------------------------------------ 407222b937cSEugene Zelenko PlatformLinux::~PlatformLinux() = default; 408e996fd30SGreg Clayton 409e996fd30SGreg Clayton bool 41013e8e1c3SJohnny Chen PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) 411e996fd30SGreg Clayton { 41228041352SGreg Clayton bool success = false; 41328041352SGreg Clayton if (IsHost()) 41428041352SGreg Clayton { 41528041352SGreg Clayton success = Platform::GetProcessInfo (pid, process_info); 41628041352SGreg Clayton } 41728041352SGreg Clayton else 41828041352SGreg Clayton { 41928041352SGreg Clayton if (m_remote_platform_sp) 42028041352SGreg Clayton success = m_remote_platform_sp->GetProcessInfo (pid, process_info); 42128041352SGreg Clayton } 42228041352SGreg Clayton return success; 423e996fd30SGreg Clayton } 424e996fd30SGreg Clayton 4258e6ec453SStephane Sezer uint32_t 4268e6ec453SStephane Sezer PlatformLinux::FindProcesses (const ProcessInstanceInfoMatch &match_info, 4278e6ec453SStephane Sezer ProcessInstanceInfoList &process_infos) 4288e6ec453SStephane Sezer { 4298e6ec453SStephane Sezer uint32_t match_count = 0; 4308e6ec453SStephane Sezer if (IsHost()) 4318e6ec453SStephane Sezer { 4328e6ec453SStephane Sezer // Let the base class figure out the host details 4338e6ec453SStephane Sezer match_count = Platform::FindProcesses (match_info, process_infos); 4348e6ec453SStephane Sezer } 4358e6ec453SStephane Sezer else 4368e6ec453SStephane Sezer { 4378e6ec453SStephane Sezer // If we are remote, we can only return results if we are connected 4388e6ec453SStephane Sezer if (m_remote_platform_sp) 4398e6ec453SStephane Sezer match_count = m_remote_platform_sp->FindProcesses (match_info, process_infos); 4408e6ec453SStephane Sezer } 4418e6ec453SStephane Sezer return match_count; 4428e6ec453SStephane Sezer } 4438e6ec453SStephane Sezer 444e996fd30SGreg Clayton bool 445e996fd30SGreg Clayton PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch) 446e996fd30SGreg Clayton { 447e49b8e06SRobert Flack if (IsHost()) 448e49b8e06SRobert Flack { 449e49b8e06SRobert Flack ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 450e49b8e06SRobert Flack if (hostArch.GetTriple().isOSLinux()) 451e49b8e06SRobert Flack { 452e49b8e06SRobert Flack if (idx == 0) 453e49b8e06SRobert Flack { 454e49b8e06SRobert Flack arch = hostArch; 455e49b8e06SRobert Flack return arch.IsValid(); 456e49b8e06SRobert Flack } 457e49b8e06SRobert Flack else if (idx == 1) 458e49b8e06SRobert Flack { 459e49b8e06SRobert Flack // If the default host architecture is 64-bit, look for a 32-bit variant 460e49b8e06SRobert Flack if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) 461e49b8e06SRobert Flack { 462e49b8e06SRobert Flack arch = HostInfo::GetArchitecture(HostInfo::eArchKind32); 463e49b8e06SRobert Flack return arch.IsValid(); 464e49b8e06SRobert Flack } 465e49b8e06SRobert Flack } 466e49b8e06SRobert Flack } 467e49b8e06SRobert Flack } 468e49b8e06SRobert Flack else 469e49b8e06SRobert Flack { 470e49b8e06SRobert Flack if (m_remote_platform_sp) 471e49b8e06SRobert Flack return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch); 472bb1e283cSTed Woodward 473bb1e283cSTed Woodward llvm::Triple triple; 474bb1e283cSTed Woodward // Set the OS to linux 475bb1e283cSTed Woodward triple.setOS(llvm::Triple::Linux); 476bb1e283cSTed Woodward // Set the architecture 477bb1e283cSTed Woodward switch (idx) 478bb1e283cSTed Woodward { 479bb1e283cSTed Woodward case 0: triple.setArchName("x86_64"); break; 480bb1e283cSTed Woodward case 1: triple.setArchName("i386"); break; 481bb1e283cSTed Woodward case 2: triple.setArchName("arm"); break; 482bb1e283cSTed Woodward case 3: triple.setArchName("aarch64"); break; 483bb1e283cSTed Woodward case 4: triple.setArchName("mips64"); break; 484bb1e283cSTed Woodward case 5: triple.setArchName("hexagon"); break; 485bb1e283cSTed Woodward case 6: triple.setArchName("mips"); break; 486ce815e45SSagar Thakur case 7: triple.setArchName("mips64el"); break; 487ce815e45SSagar Thakur case 8: triple.setArchName("mipsel"); break; 488bb1e283cSTed Woodward default: return false; 489bb1e283cSTed Woodward } 490bb1e283cSTed Woodward // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the vendor by 491bb1e283cSTed Woodward // calling triple.SetVendorName("unknown") so that it is a "unspecified unknown". 492bb1e283cSTed Woodward // This means when someone calls triple.GetVendorName() it will return an empty string 493bb1e283cSTed Woodward // which indicates that the vendor can be set when two architectures are merged 494bb1e283cSTed Woodward 495bb1e283cSTed Woodward // Now set the triple into "arch" and return true 496bb1e283cSTed Woodward arch.SetTriple(triple); 497bb1e283cSTed Woodward return true; 498e49b8e06SRobert Flack } 499e996fd30SGreg Clayton return false; 500e996fd30SGreg Clayton } 501ecc11474SStephen Wilson 502ecc11474SStephen Wilson void 503ecc11474SStephen Wilson PlatformLinux::GetStatus (Stream &strm) 504ecc11474SStephen Wilson { 5053be69dacSDaniel Malea Platform::GetStatus(strm); 506ecc11474SStephen Wilson 507b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX 508b743a803SStephane Sezer // Display local kernel information only when we are running in host mode. 509b743a803SStephane Sezer // Otherwise, we would end up printing non-Linux information (when running 510b743a803SStephane Sezer // on Mac OS for example). 511b743a803SStephane Sezer if (IsHost()) 512b743a803SStephane Sezer { 513b2f1fb29SVirgile Bello struct utsname un; 514b2f1fb29SVirgile Bello 5153be69dacSDaniel Malea if (uname(&un)) 5163be69dacSDaniel Malea return; 5173be69dacSDaniel Malea 5183be69dacSDaniel Malea strm.Printf (" Kernel: %s\n", un.sysname); 5193be69dacSDaniel Malea strm.Printf (" Release: %s\n", un.release); 5203be69dacSDaniel Malea strm.Printf (" Version: %s\n", un.version); 521b743a803SStephane Sezer } 522b2f1fb29SVirgile Bello #endif 523ecc11474SStephen Wilson } 524ecc11474SStephen Wilson 525348fb385STodd Fiala int32_t 526348fb385STodd Fiala PlatformLinux::GetResumeCountForLaunchInfo (ProcessLaunchInfo &launch_info) 52728041352SGreg Clayton { 528348fb385STodd Fiala int32_t resume_count = 0; 52928041352SGreg Clayton 530348fb385STodd Fiala // Always resume past the initial stop when we use eLaunchFlagDebug 531348fb385STodd Fiala if (launch_info.GetFlags ().Test (eLaunchFlagDebug)) 53228041352SGreg Clayton { 533348fb385STodd Fiala // Resume past the stop for the final exec into the true inferior. 534348fb385STodd Fiala ++resume_count; 535348fb385STodd Fiala } 536015d818bSTodd Fiala 537348fb385STodd Fiala // If we're not launching a shell, we're done. 53810687b0eSZachary Turner const FileSpec &shell = launch_info.GetShell(); 53910687b0eSZachary Turner if (!shell) 540348fb385STodd Fiala return resume_count; 541348fb385STodd Fiala 54210687b0eSZachary Turner std::string shell_string = shell.GetPath(); 543348fb385STodd Fiala // We're in a shell, so for sure we have to resume past the shell exec. 544348fb385STodd Fiala ++resume_count; 545348fb385STodd Fiala 546348fb385STodd Fiala // Figure out what shell we're planning on using. 54710687b0eSZachary Turner const char *shell_name = strrchr (shell_string.c_str(), '/'); 548348fb385STodd Fiala if (shell_name == NULL) 54910687b0eSZachary Turner shell_name = shell_string.c_str(); 55028041352SGreg Clayton else 551348fb385STodd Fiala shell_name++; 552348fb385STodd Fiala 553348fb385STodd Fiala if (strcmp (shell_name, "csh") == 0 554348fb385STodd Fiala || strcmp (shell_name, "tcsh") == 0 555348fb385STodd Fiala || strcmp (shell_name, "zsh") == 0 556348fb385STodd Fiala || strcmp (shell_name, "sh") == 0) 55728041352SGreg Clayton { 558348fb385STodd Fiala // These shells seem to re-exec themselves. Add another resume. 559348fb385STodd Fiala ++resume_count; 560ecc11474SStephen Wilson } 56113e8e1c3SJohnny Chen 562348fb385STodd Fiala return resume_count; 563348fb385STodd Fiala } 564348fb385STodd Fiala 565348fb385STodd Fiala bool 566015d818bSTodd Fiala PlatformLinux::CanDebugProcess () 567015d818bSTodd Fiala { 568015d818bSTodd Fiala if (IsHost ()) 569348fb385STodd Fiala { 570b36f9178SPavel Labath return true; 571348fb385STodd Fiala } 572348fb385STodd Fiala else 573348fb385STodd Fiala { 574015d818bSTodd Fiala // If we're connected, we can debug. 575015d818bSTodd Fiala return IsConnected (); 576015d818bSTodd Fiala } 577348fb385STodd Fiala } 578015d818bSTodd Fiala 579348fb385STodd Fiala // For local debugging, Linux will override the debug logic to use llgs-launch rather than 580348fb385STodd Fiala // lldb-launch, llgs-attach. This differs from current lldb-launch, debugserver-attach 581348fb385STodd Fiala // approach on MacOSX. 58213e8e1c3SJohnny Chen lldb::ProcessSP 583348fb385STodd Fiala PlatformLinux::DebugProcess (ProcessLaunchInfo &launch_info, 58413e8e1c3SJohnny Chen Debugger &debugger, 585348fb385STodd Fiala Target *target, // Can be NULL, if NULL create a new target, else use existing one 58613e8e1c3SJohnny Chen Error &error) 58713e8e1c3SJohnny Chen { 588db264a6dSTamas Berghammer Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); 589348fb385STodd Fiala if (log) 590348fb385STodd Fiala log->Printf ("PlatformLinux::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target)); 59128041352SGreg Clayton 592348fb385STodd Fiala // If we're a remote host, use standard behavior from parent class. 593348fb385STodd Fiala if (!IsHost ()) 5948012cadbSGreg Clayton return PlatformPOSIX::DebugProcess (launch_info, debugger, target, error); 595348fb385STodd Fiala 596348fb385STodd Fiala // 597348fb385STodd Fiala // For local debugging, we'll insist on having ProcessGDBRemote create the process. 598348fb385STodd Fiala // 599348fb385STodd Fiala 600348fb385STodd Fiala ProcessSP process_sp; 601348fb385STodd Fiala 602348fb385STodd Fiala // Make sure we stop at the entry point 603348fb385STodd Fiala launch_info.GetFlags ().Set (eLaunchFlagDebug); 604348fb385STodd Fiala 605348fb385STodd Fiala // We always launch the process we are going to debug in a separate process 606348fb385STodd Fiala // group, since then we can handle ^C interrupts ourselves w/o having to worry 607348fb385STodd Fiala // about the target getting them as well. 608348fb385STodd Fiala launch_info.SetLaunchInSeparateProcessGroup(true); 609348fb385STodd Fiala 610348fb385STodd Fiala // Ensure we have a target. 611348fb385STodd Fiala if (target == nullptr) 612348fb385STodd Fiala { 613348fb385STodd Fiala if (log) 614348fb385STodd Fiala log->Printf ("PlatformLinux::%s creating new target", __FUNCTION__); 615348fb385STodd Fiala 616348fb385STodd Fiala TargetSP new_target_sp; 61728041352SGreg Clayton error = debugger.GetTargetList().CreateTarget (debugger, 618348fb385STodd Fiala nullptr, 619348fb385STodd Fiala nullptr, 62028041352SGreg Clayton false, 621348fb385STodd Fiala nullptr, 62228041352SGreg Clayton new_target_sp); 623348fb385STodd Fiala if (error.Fail ()) 624348fb385STodd Fiala { 625348fb385STodd Fiala if (log) 626348fb385STodd Fiala log->Printf ("PlatformLinux::%s failed to create new target: %s", __FUNCTION__, error.AsCString ()); 627348fb385STodd Fiala return process_sp; 628348fb385STodd Fiala } 629348fb385STodd Fiala 63028041352SGreg Clayton target = new_target_sp.get(); 631348fb385STodd Fiala if (!target) 632348fb385STodd Fiala { 633348fb385STodd Fiala error.SetErrorString ("CreateTarget() returned nullptr"); 634348fb385STodd Fiala if (log) 635348fb385STodd Fiala log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 636348fb385STodd Fiala return process_sp; 637348fb385STodd Fiala } 63828041352SGreg Clayton } 63928041352SGreg Clayton else 64028041352SGreg Clayton { 641348fb385STodd Fiala if (log) 642348fb385STodd Fiala log->Printf ("PlatformLinux::%s using provided target", __FUNCTION__); 643348fb385STodd Fiala } 644348fb385STodd Fiala 645348fb385STodd Fiala // Mark target as currently selected target. 64628041352SGreg Clayton debugger.GetTargetList().SetSelectedTarget(target); 64728041352SGreg Clayton 648348fb385STodd Fiala // Now create the gdb-remote process. 649348fb385STodd Fiala if (log) 650348fb385STodd Fiala log->Printf ("PlatformLinux::%s having target create process with gdb-remote plugin", __FUNCTION__); 6518012cadbSGreg Clayton process_sp = target->CreateProcess (launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr); 65228041352SGreg Clayton 653348fb385STodd Fiala if (!process_sp) 654348fb385STodd Fiala { 655348fb385STodd Fiala error.SetErrorString ("CreateProcess() failed for gdb-remote process"); 656348fb385STodd Fiala if (log) 657348fb385STodd Fiala log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 658348fb385STodd Fiala return process_sp; 659348fb385STodd Fiala } 660348fb385STodd Fiala else 661348fb385STodd Fiala { 662348fb385STodd Fiala if (log) 663348fb385STodd Fiala log->Printf ("PlatformLinux::%s successfully created process", __FUNCTION__); 664348fb385STodd Fiala } 665348fb385STodd Fiala 666348fb385STodd Fiala // Adjust launch for a hijacker. 667348fb385STodd Fiala ListenerSP listener_sp; 668348fb385STodd Fiala if (!launch_info.GetHijackListener ()) 669348fb385STodd Fiala { 670348fb385STodd Fiala if (log) 671348fb385STodd Fiala log->Printf ("PlatformLinux::%s setting up hijacker", __FUNCTION__); 672348fb385STodd Fiala 673*583bbb1dSJim Ingham listener_sp = Listener::MakeListener("lldb.PlatformLinux.DebugProcess.hijack"); 674348fb385STodd Fiala launch_info.SetHijackListener (listener_sp); 675*583bbb1dSJim Ingham process_sp->HijackProcessEvents (listener_sp); 676348fb385STodd Fiala } 677348fb385STodd Fiala 678348fb385STodd Fiala // Log file actions. 679348fb385STodd Fiala if (log) 680348fb385STodd Fiala { 681348fb385STodd Fiala log->Printf ("PlatformLinux::%s launching process with the following file actions:", __FUNCTION__); 682348fb385STodd Fiala 683348fb385STodd Fiala StreamString stream; 684348fb385STodd Fiala size_t i = 0; 685348fb385STodd Fiala const FileAction *file_action; 686348fb385STodd Fiala while ((file_action = launch_info.GetFileActionAtIndex (i++)) != nullptr) 687348fb385STodd Fiala { 688348fb385STodd Fiala file_action->Dump (stream); 689348fb385STodd Fiala log->PutCString (stream.GetString().c_str ()); 690348fb385STodd Fiala stream.Clear(); 691348fb385STodd Fiala } 692348fb385STodd Fiala } 693348fb385STodd Fiala 694348fb385STodd Fiala // Do the launch. 695348fb385STodd Fiala error = process_sp->Launch(launch_info); 696348fb385STodd Fiala if (error.Success ()) 697348fb385STodd Fiala { 698348fb385STodd Fiala // Handle the hijacking of process events. 699348fb385STodd Fiala if (listener_sp) 700348fb385STodd Fiala { 701*583bbb1dSJim Ingham const StateType state = process_sp->WaitForProcessToStop (NULL, NULL, false, listener_sp); 702348fb385STodd Fiala 703348fb385STodd Fiala if (state == eStateStopped) 704348fb385STodd Fiala { 705348fb385STodd Fiala if (log) 706348fb385STodd Fiala log->Printf ("PlatformLinux::%s pid %" PRIu64 " state %s\n", 707348fb385STodd Fiala __FUNCTION__, process_sp->GetID (), StateAsCString (state)); 708348fb385STodd Fiala } 709348fb385STodd Fiala else 710348fb385STodd Fiala { 711348fb385STodd Fiala if (log) 712348fb385STodd Fiala log->Printf ("PlatformLinux::%s pid %" PRIu64 " state is not stopped - %s\n", 713348fb385STodd Fiala __FUNCTION__, process_sp->GetID (), StateAsCString (state)); 714348fb385STodd Fiala } 715348fb385STodd Fiala } 716348fb385STodd Fiala 717348fb385STodd Fiala // Hook up process PTY if we have one (which we should for local debugging with llgs). 718348fb385STodd Fiala int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); 719348fb385STodd Fiala if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd) 720348fb385STodd Fiala { 721348fb385STodd Fiala process_sp->SetSTDIOFileDescriptor(pty_fd); 722348fb385STodd Fiala if (log) 723348fb385STodd Fiala log->Printf ("PlatformLinux::%s pid %" PRIu64 " hooked up STDIO pty to process", __FUNCTION__, process_sp->GetID ()); 724348fb385STodd Fiala } 725348fb385STodd Fiala else 726348fb385STodd Fiala { 727348fb385STodd Fiala if (log) 728348fb385STodd Fiala log->Printf ("PlatformLinux::%s pid %" PRIu64 " not using process STDIO pty", __FUNCTION__, process_sp->GetID ()); 72928041352SGreg Clayton } 73028041352SGreg Clayton } 73128041352SGreg Clayton else 73228041352SGreg Clayton { 733348fb385STodd Fiala if (log) 734348fb385STodd Fiala log->Printf ("PlatformLinux::%s process launch failed: %s", __FUNCTION__, error.AsCString ()); 735348fb385STodd Fiala // FIXME figure out appropriate cleanup here. Do we delete the target? Do we delete the process? Does our caller do that? 73628041352SGreg Clayton } 737348fb385STodd Fiala 73828041352SGreg Clayton return process_sp; 73913e8e1c3SJohnny Chen } 7402094dbf4SJason Molenda 7412094dbf4SJason Molenda void 7422094dbf4SJason Molenda PlatformLinux::CalculateTrapHandlerSymbolNames () 7432094dbf4SJason Molenda { 7442094dbf4SJason Molenda m_trap_handlers.push_back (ConstString ("_sigtramp")); 7452094dbf4SJason Molenda } 746af245d11STodd Fiala 74796ad3de5SRobert Flack uint64_t 748e0d8c422SMohit K. Bhakkad PlatformLinux::ConvertMmapFlagsToPlatform(const ArchSpec &arch, unsigned flags) 74996ad3de5SRobert Flack { 75096ad3de5SRobert Flack uint64_t flags_platform = 0; 751e0d8c422SMohit K. Bhakkad uint64_t map_anon = MAP_ANON; 752e0d8c422SMohit K. Bhakkad 753e0d8c422SMohit K. Bhakkad // To get correct flags for MIPS Architecture 754e0d8c422SMohit K. Bhakkad if (arch.GetTriple ().getArch () == llvm::Triple::mips64 755e0d8c422SMohit K. Bhakkad || arch.GetTriple ().getArch () == llvm::Triple::mips64el 756e0d8c422SMohit K. Bhakkad || arch.GetTriple ().getArch () == llvm::Triple::mips 757e0d8c422SMohit K. Bhakkad || arch.GetTriple ().getArch () == llvm::Triple::mipsel) 758e0d8c422SMohit K. Bhakkad map_anon = 0x800; 759e0d8c422SMohit K. Bhakkad 76096ad3de5SRobert Flack if (flags & eMmapFlagsPrivate) 76196ad3de5SRobert Flack flags_platform |= MAP_PRIVATE; 76296ad3de5SRobert Flack if (flags & eMmapFlagsAnon) 763e0d8c422SMohit K. Bhakkad flags_platform |= map_anon; 76496ad3de5SRobert Flack return flags_platform; 76596ad3de5SRobert Flack } 7666e25aeeaSEnrico Granata 7676e25aeeaSEnrico Granata ConstString 7686e25aeeaSEnrico Granata PlatformLinux::GetFullNameForDylib (ConstString basename) 7696e25aeeaSEnrico Granata { 7706e25aeeaSEnrico Granata if (basename.IsEmpty()) 7716e25aeeaSEnrico Granata return basename; 7726e25aeeaSEnrico Granata 7736e25aeeaSEnrico Granata StreamString stream; 7746e25aeeaSEnrico Granata stream.Printf("lib%s.so", basename.GetCString()); 7756e25aeeaSEnrico Granata return ConstString(stream.GetData()); 7766e25aeeaSEnrico Granata } 777