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