1 //===-- PlatformLinux.cpp -------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "PlatformLinux.h" 10 #include "lldb/Host/Config.h" 11 12 #include <stdio.h> 13 #if LLDB_ENABLE_POSIX 14 #include <sys/utsname.h> 15 #endif 16 17 #include "lldb/Core/Debugger.h" 18 #include "lldb/Core/PluginManager.h" 19 #include "lldb/Host/HostInfo.h" 20 #include "lldb/Target/Process.h" 21 #include "lldb/Target/Target.h" 22 #include "lldb/Utility/FileSpec.h" 23 #include "lldb/Utility/Log.h" 24 #include "lldb/Utility/State.h" 25 #include "lldb/Utility/Status.h" 26 #include "lldb/Utility/StreamString.h" 27 28 // Define these constants from Linux mman.h for use when targeting remote linux 29 // systems even when host has different values. 30 #define MAP_PRIVATE 2 31 #define MAP_ANON 0x20 32 33 using namespace lldb; 34 using namespace lldb_private; 35 using namespace lldb_private::platform_linux; 36 37 LLDB_PLUGIN_DEFINE(PlatformLinux) 38 39 static uint32_t g_initialize_count = 0; 40 41 42 PlatformSP PlatformLinux::CreateInstance(bool force, const ArchSpec *arch) { 43 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); 44 LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force, 45 arch ? arch->GetArchitectureName() : "<null>", 46 arch ? arch->GetTriple().getTriple() : "<null>"); 47 48 bool create = force; 49 if (!create && arch && arch->IsValid()) { 50 const llvm::Triple &triple = arch->GetTriple(); 51 switch (triple.getOS()) { 52 case llvm::Triple::Linux: 53 create = true; 54 break; 55 56 #if defined(__linux__) 57 // Only accept "unknown" for the OS if the host is linux and it "unknown" 58 // wasn't specified (it was just returned because it was NOT specified) 59 case llvm::Triple::OSType::UnknownOS: 60 create = !arch->TripleOSWasSpecified(); 61 break; 62 #endif 63 default: 64 break; 65 } 66 } 67 68 LLDB_LOG(log, "create = {0}", create); 69 if (create) { 70 return PlatformSP(new PlatformLinux(false)); 71 } 72 return PlatformSP(); 73 } 74 75 ConstString PlatformLinux::GetPluginNameStatic(bool is_host) { 76 if (is_host) { 77 static ConstString g_host_name(Platform::GetHostPlatformName()); 78 return g_host_name; 79 } else { 80 static ConstString g_remote_name("remote-linux"); 81 return g_remote_name; 82 } 83 } 84 85 const char *PlatformLinux::GetPluginDescriptionStatic(bool is_host) { 86 if (is_host) 87 return "Local Linux user platform plug-in."; 88 else 89 return "Remote Linux user platform plug-in."; 90 } 91 92 ConstString PlatformLinux::GetPluginName() { 93 return GetPluginNameStatic(IsHost()); 94 } 95 96 void PlatformLinux::Initialize() { 97 PlatformPOSIX::Initialize(); 98 99 if (g_initialize_count++ == 0) { 100 #if defined(__linux__) && !defined(__ANDROID__) 101 PlatformSP default_platform_sp(new PlatformLinux(true)); 102 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 103 Platform::SetHostPlatform(default_platform_sp); 104 #endif 105 PluginManager::RegisterPlugin( 106 PlatformLinux::GetPluginNameStatic(false), 107 PlatformLinux::GetPluginDescriptionStatic(false), 108 PlatformLinux::CreateInstance, nullptr); 109 } 110 } 111 112 void PlatformLinux::Terminate() { 113 if (g_initialize_count > 0) { 114 if (--g_initialize_count == 0) { 115 PluginManager::UnregisterPlugin(PlatformLinux::CreateInstance); 116 } 117 } 118 119 PlatformPOSIX::Terminate(); 120 } 121 122 /// Default Constructor 123 PlatformLinux::PlatformLinux(bool is_host) 124 : PlatformPOSIX(is_host) // This is the local host platform 125 {} 126 127 PlatformLinux::~PlatformLinux() = default; 128 129 bool PlatformLinux::GetSupportedArchitectureAtIndex(uint32_t idx, 130 ArchSpec &arch) { 131 if (IsHost()) { 132 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 133 if (hostArch.GetTriple().isOSLinux()) { 134 if (idx == 0) { 135 arch = hostArch; 136 return arch.IsValid(); 137 } else if (idx == 1) { 138 // If the default host architecture is 64-bit, look for a 32-bit 139 // variant 140 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) { 141 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32); 142 return arch.IsValid(); 143 } 144 } 145 } 146 } else { 147 if (m_remote_platform_sp) 148 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch); 149 150 llvm::Triple triple; 151 // Set the OS to linux 152 triple.setOS(llvm::Triple::Linux); 153 // Set the architecture 154 switch (idx) { 155 case 0: 156 triple.setArchName("x86_64"); 157 break; 158 case 1: 159 triple.setArchName("i386"); 160 break; 161 case 2: 162 triple.setArchName("arm"); 163 break; 164 case 3: 165 triple.setArchName("aarch64"); 166 break; 167 case 4: 168 triple.setArchName("mips64"); 169 break; 170 case 5: 171 triple.setArchName("hexagon"); 172 break; 173 case 6: 174 triple.setArchName("mips"); 175 break; 176 case 7: 177 triple.setArchName("mips64el"); 178 break; 179 case 8: 180 triple.setArchName("mipsel"); 181 break; 182 case 9: 183 triple.setArchName("s390x"); 184 break; 185 default: 186 return false; 187 } 188 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the 189 // vendor by calling triple.SetVendorName("unknown") so that it is a 190 // "unspecified unknown". This means when someone calls 191 // triple.GetVendorName() it will return an empty string which indicates 192 // that the vendor can be set when two architectures are merged 193 194 // Now set the triple into "arch" and return true 195 arch.SetTriple(triple); 196 return true; 197 } 198 return false; 199 } 200 201 void PlatformLinux::GetStatus(Stream &strm) { 202 Platform::GetStatus(strm); 203 204 #if LLDB_ENABLE_POSIX 205 // Display local kernel information only when we are running in host mode. 206 // Otherwise, we would end up printing non-Linux information (when running on 207 // Mac OS for example). 208 if (IsHost()) { 209 struct utsname un; 210 211 if (uname(&un)) 212 return; 213 214 strm.Printf(" Kernel: %s\n", un.sysname); 215 strm.Printf(" Release: %s\n", un.release); 216 strm.Printf(" Version: %s\n", un.version); 217 } 218 #endif 219 } 220 221 int32_t 222 PlatformLinux::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) { 223 int32_t resume_count = 0; 224 225 // Always resume past the initial stop when we use eLaunchFlagDebug 226 if (launch_info.GetFlags().Test(eLaunchFlagDebug)) { 227 // Resume past the stop for the final exec into the true inferior. 228 ++resume_count; 229 } 230 231 // If we're not launching a shell, we're done. 232 const FileSpec &shell = launch_info.GetShell(); 233 if (!shell) 234 return resume_count; 235 236 std::string shell_string = shell.GetPath(); 237 // We're in a shell, so for sure we have to resume past the shell exec. 238 ++resume_count; 239 240 // Figure out what shell we're planning on using. 241 const char *shell_name = strrchr(shell_string.c_str(), '/'); 242 if (shell_name == nullptr) 243 shell_name = shell_string.c_str(); 244 else 245 shell_name++; 246 247 if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 || 248 strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) { 249 // These shells seem to re-exec themselves. Add another resume. 250 ++resume_count; 251 } 252 253 return resume_count; 254 } 255 256 bool PlatformLinux::CanDebugProcess() { 257 if (IsHost()) { 258 return true; 259 } else { 260 // If we're connected, we can debug. 261 return IsConnected(); 262 } 263 } 264 265 void PlatformLinux::CalculateTrapHandlerSymbolNames() { 266 m_trap_handlers.push_back(ConstString("_sigtramp")); 267 m_trap_handlers.push_back(ConstString("__kernel_rt_sigreturn")); 268 m_trap_handlers.push_back(ConstString("__restore_rt")); 269 } 270 271 MmapArgList PlatformLinux::GetMmapArgumentList(const ArchSpec &arch, 272 addr_t addr, addr_t length, 273 unsigned prot, unsigned flags, 274 addr_t fd, addr_t offset) { 275 uint64_t flags_platform = 0; 276 uint64_t map_anon = arch.IsMIPS() ? 0x800 : MAP_ANON; 277 278 if (flags & eMmapFlagsPrivate) 279 flags_platform |= MAP_PRIVATE; 280 if (flags & eMmapFlagsAnon) 281 flags_platform |= map_anon; 282 283 MmapArgList args({addr, length, prot, flags_platform, fd, offset}); 284 return args; 285 } 286 287