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 <cstdio> 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 void PlatformLinux::Initialize() { 93 PlatformPOSIX::Initialize(); 94 95 if (g_initialize_count++ == 0) { 96 #if defined(__linux__) && !defined(__ANDROID__) 97 PlatformSP default_platform_sp(new PlatformLinux(true)); 98 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 99 Platform::SetHostPlatform(default_platform_sp); 100 #endif 101 PluginManager::RegisterPlugin( 102 PlatformLinux::GetPluginNameStatic(false), 103 PlatformLinux::GetPluginDescriptionStatic(false), 104 PlatformLinux::CreateInstance, nullptr); 105 } 106 } 107 108 void PlatformLinux::Terminate() { 109 if (g_initialize_count > 0) { 110 if (--g_initialize_count == 0) { 111 PluginManager::UnregisterPlugin(PlatformLinux::CreateInstance); 112 } 113 } 114 115 PlatformPOSIX::Terminate(); 116 } 117 118 /// Default Constructor 119 PlatformLinux::PlatformLinux(bool is_host) 120 : PlatformPOSIX(is_host) // This is the local host platform 121 {} 122 123 bool PlatformLinux::GetSupportedArchitectureAtIndex(uint32_t idx, 124 ArchSpec &arch) { 125 if (IsHost()) { 126 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 127 if (hostArch.GetTriple().isOSLinux()) { 128 if (idx == 0) { 129 arch = hostArch; 130 return arch.IsValid(); 131 } else if (idx == 1) { 132 // If the default host architecture is 64-bit, look for a 32-bit 133 // variant 134 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) { 135 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32); 136 return arch.IsValid(); 137 } 138 } 139 } 140 } else { 141 if (m_remote_platform_sp) 142 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch); 143 144 llvm::Triple triple; 145 // Set the OS to linux 146 triple.setOS(llvm::Triple::Linux); 147 // Set the architecture 148 switch (idx) { 149 case 0: 150 triple.setArchName("x86_64"); 151 break; 152 case 1: 153 triple.setArchName("i386"); 154 break; 155 case 2: 156 triple.setArchName("arm"); 157 break; 158 case 3: 159 triple.setArchName("aarch64"); 160 break; 161 case 4: 162 triple.setArchName("mips64"); 163 break; 164 case 5: 165 triple.setArchName("hexagon"); 166 break; 167 case 6: 168 triple.setArchName("mips"); 169 break; 170 case 7: 171 triple.setArchName("mips64el"); 172 break; 173 case 8: 174 triple.setArchName("mipsel"); 175 break; 176 case 9: 177 triple.setArchName("s390x"); 178 break; 179 default: 180 return false; 181 } 182 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the 183 // vendor by calling triple.SetVendorName("unknown") so that it is a 184 // "unspecified unknown". This means when someone calls 185 // triple.GetVendorName() it will return an empty string which indicates 186 // that the vendor can be set when two architectures are merged 187 188 // Now set the triple into "arch" and return true 189 arch.SetTriple(triple); 190 return true; 191 } 192 return false; 193 } 194 195 void PlatformLinux::GetStatus(Stream &strm) { 196 Platform::GetStatus(strm); 197 198 #if LLDB_ENABLE_POSIX 199 // Display local kernel information only when we are running in host mode. 200 // Otherwise, we would end up printing non-Linux information (when running on 201 // Mac OS for example). 202 if (IsHost()) { 203 struct utsname un; 204 205 if (uname(&un)) 206 return; 207 208 strm.Printf(" Kernel: %s\n", un.sysname); 209 strm.Printf(" Release: %s\n", un.release); 210 strm.Printf(" Version: %s\n", un.version); 211 } 212 #endif 213 } 214 215 uint32_t 216 PlatformLinux::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) { 217 uint32_t resume_count = 0; 218 219 // Always resume past the initial stop when we use eLaunchFlagDebug 220 if (launch_info.GetFlags().Test(eLaunchFlagDebug)) { 221 // Resume past the stop for the final exec into the true inferior. 222 ++resume_count; 223 } 224 225 // If we're not launching a shell, we're done. 226 const FileSpec &shell = launch_info.GetShell(); 227 if (!shell) 228 return resume_count; 229 230 std::string shell_string = shell.GetPath(); 231 // We're in a shell, so for sure we have to resume past the shell exec. 232 ++resume_count; 233 234 // Figure out what shell we're planning on using. 235 const char *shell_name = strrchr(shell_string.c_str(), '/'); 236 if (shell_name == nullptr) 237 shell_name = shell_string.c_str(); 238 else 239 shell_name++; 240 241 if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 || 242 strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) { 243 // These shells seem to re-exec themselves. Add another resume. 244 ++resume_count; 245 } 246 247 return resume_count; 248 } 249 250 bool PlatformLinux::CanDebugProcess() { 251 if (IsHost()) { 252 return true; 253 } else { 254 // If we're connected, we can debug. 255 return IsConnected(); 256 } 257 } 258 259 void PlatformLinux::CalculateTrapHandlerSymbolNames() { 260 m_trap_handlers.push_back(ConstString("_sigtramp")); 261 m_trap_handlers.push_back(ConstString("__kernel_rt_sigreturn")); 262 m_trap_handlers.push_back(ConstString("__restore_rt")); 263 } 264 265 MmapArgList PlatformLinux::GetMmapArgumentList(const ArchSpec &arch, 266 addr_t addr, addr_t length, 267 unsigned prot, unsigned flags, 268 addr_t fd, addr_t offset) { 269 uint64_t flags_platform = 0; 270 uint64_t map_anon = arch.IsMIPS() ? 0x800 : MAP_ANON; 271 272 if (flags & eMmapFlagsPrivate) 273 flags_platform |= MAP_PRIVATE; 274 if (flags & eMmapFlagsAnon) 275 flags_platform |= map_anon; 276 277 MmapArgList args({addr, length, prot, flags_platform, fd, offset}); 278 return args; 279 } 280 281