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 "Utility/ARM64_DWARF_Registers.h" 18 #include "lldb/Core/Debugger.h" 19 #include "lldb/Core/PluginManager.h" 20 #include "lldb/Host/HostInfo.h" 21 #include "lldb/Symbol/UnwindPlan.h" 22 #include "lldb/Target/Process.h" 23 #include "lldb/Target/Target.h" 24 #include "lldb/Utility/FileSpec.h" 25 #include "lldb/Utility/Log.h" 26 #include "lldb/Utility/State.h" 27 #include "lldb/Utility/Status.h" 28 #include "lldb/Utility/StreamString.h" 29 30 // Define these constants from Linux mman.h for use when targeting remote linux 31 // systems even when host has different values. 32 #define MAP_PRIVATE 2 33 #define MAP_ANON 0x20 34 35 using namespace lldb; 36 using namespace lldb_private; 37 using namespace lldb_private::platform_linux; 38 39 LLDB_PLUGIN_DEFINE(PlatformLinux) 40 41 static uint32_t g_initialize_count = 0; 42 43 44 PlatformSP PlatformLinux::CreateInstance(bool force, const ArchSpec *arch) { 45 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); 46 LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force, 47 arch ? arch->GetArchitectureName() : "<null>", 48 arch ? arch->GetTriple().getTriple() : "<null>"); 49 50 bool create = force; 51 if (!create && arch && arch->IsValid()) { 52 const llvm::Triple &triple = arch->GetTriple(); 53 switch (triple.getOS()) { 54 case llvm::Triple::Linux: 55 create = true; 56 break; 57 58 #if defined(__linux__) 59 // Only accept "unknown" for the OS if the host is linux and it "unknown" 60 // wasn't specified (it was just returned because it was NOT specified) 61 case llvm::Triple::OSType::UnknownOS: 62 create = !arch->TripleOSWasSpecified(); 63 break; 64 #endif 65 default: 66 break; 67 } 68 } 69 70 LLDB_LOG(log, "create = {0}", create); 71 if (create) { 72 return PlatformSP(new PlatformLinux(false)); 73 } 74 return PlatformSP(); 75 } 76 77 llvm::StringRef PlatformLinux::GetPluginDescriptionStatic(bool is_host) { 78 if (is_host) 79 return "Local Linux user platform plug-in."; 80 return "Remote Linux user platform plug-in."; 81 } 82 83 void PlatformLinux::Initialize() { 84 PlatformPOSIX::Initialize(); 85 86 if (g_initialize_count++ == 0) { 87 #if defined(__linux__) && !defined(__ANDROID__) 88 PlatformSP default_platform_sp(new PlatformLinux(true)); 89 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 90 Platform::SetHostPlatform(default_platform_sp); 91 #endif 92 PluginManager::RegisterPlugin( 93 PlatformLinux::GetPluginNameStatic(false), 94 PlatformLinux::GetPluginDescriptionStatic(false), 95 PlatformLinux::CreateInstance, nullptr); 96 } 97 } 98 99 void PlatformLinux::Terminate() { 100 if (g_initialize_count > 0) { 101 if (--g_initialize_count == 0) { 102 PluginManager::UnregisterPlugin(PlatformLinux::CreateInstance); 103 } 104 } 105 106 PlatformPOSIX::Terminate(); 107 } 108 109 /// Default Constructor 110 PlatformLinux::PlatformLinux(bool is_host) 111 : PlatformPOSIX(is_host) // This is the local host platform 112 {} 113 114 bool PlatformLinux::GetSupportedArchitectureAtIndex(uint32_t idx, 115 ArchSpec &arch) { 116 if (IsHost()) { 117 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 118 if (hostArch.GetTriple().isOSLinux()) { 119 if (idx == 0) { 120 arch = hostArch; 121 return arch.IsValid(); 122 } else if (idx == 1) { 123 // If the default host architecture is 64-bit, look for a 32-bit 124 // variant 125 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) { 126 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32); 127 return arch.IsValid(); 128 } 129 } 130 } 131 } else { 132 if (m_remote_platform_sp) 133 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch); 134 135 llvm::Triple triple; 136 // Set the OS to linux 137 triple.setOS(llvm::Triple::Linux); 138 // Set the architecture 139 switch (idx) { 140 case 0: 141 triple.setArchName("x86_64"); 142 break; 143 case 1: 144 triple.setArchName("i386"); 145 break; 146 case 2: 147 triple.setArchName("arm"); 148 break; 149 case 3: 150 triple.setArchName("aarch64"); 151 break; 152 case 4: 153 triple.setArchName("mips64"); 154 break; 155 case 5: 156 triple.setArchName("hexagon"); 157 break; 158 case 6: 159 triple.setArchName("mips"); 160 break; 161 case 7: 162 triple.setArchName("mips64el"); 163 break; 164 case 8: 165 triple.setArchName("mipsel"); 166 break; 167 case 9: 168 triple.setArchName("s390x"); 169 break; 170 default: 171 return false; 172 } 173 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the 174 // vendor by calling triple.SetVendorName("unknown") so that it is a 175 // "unspecified unknown". This means when someone calls 176 // triple.GetVendorName() it will return an empty string which indicates 177 // that the vendor can be set when two architectures are merged 178 179 // Now set the triple into "arch" and return true 180 arch.SetTriple(triple); 181 return true; 182 } 183 return false; 184 } 185 186 void PlatformLinux::GetStatus(Stream &strm) { 187 Platform::GetStatus(strm); 188 189 #if LLDB_ENABLE_POSIX 190 // Display local kernel information only when we are running in host mode. 191 // Otherwise, we would end up printing non-Linux information (when running on 192 // Mac OS for example). 193 if (IsHost()) { 194 struct utsname un; 195 196 if (uname(&un)) 197 return; 198 199 strm.Printf(" Kernel: %s\n", un.sysname); 200 strm.Printf(" Release: %s\n", un.release); 201 strm.Printf(" Version: %s\n", un.version); 202 } 203 #endif 204 } 205 206 uint32_t 207 PlatformLinux::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) { 208 uint32_t resume_count = 0; 209 210 // Always resume past the initial stop when we use eLaunchFlagDebug 211 if (launch_info.GetFlags().Test(eLaunchFlagDebug)) { 212 // Resume past the stop for the final exec into the true inferior. 213 ++resume_count; 214 } 215 216 // If we're not launching a shell, we're done. 217 const FileSpec &shell = launch_info.GetShell(); 218 if (!shell) 219 return resume_count; 220 221 std::string shell_string = shell.GetPath(); 222 // We're in a shell, so for sure we have to resume past the shell exec. 223 ++resume_count; 224 225 // Figure out what shell we're planning on using. 226 const char *shell_name = strrchr(shell_string.c_str(), '/'); 227 if (shell_name == nullptr) 228 shell_name = shell_string.c_str(); 229 else 230 shell_name++; 231 232 if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 || 233 strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) { 234 // These shells seem to re-exec themselves. Add another resume. 235 ++resume_count; 236 } 237 238 return resume_count; 239 } 240 241 bool PlatformLinux::CanDebugProcess() { 242 if (IsHost()) { 243 return true; 244 } else { 245 // If we're connected, we can debug. 246 return IsConnected(); 247 } 248 } 249 250 void PlatformLinux::CalculateTrapHandlerSymbolNames() { 251 m_trap_handlers.push_back(ConstString("_sigtramp")); 252 m_trap_handlers.push_back(ConstString("__kernel_rt_sigreturn")); 253 m_trap_handlers.push_back(ConstString("__restore_rt")); 254 } 255 256 static lldb::UnwindPlanSP GetAArch64TrapHanlderUnwindPlan(ConstString name) { 257 UnwindPlanSP unwind_plan_sp; 258 if (name != "__kernel_rt_sigreturn") 259 return unwind_plan_sp; 260 261 UnwindPlan::RowSP row = std::make_shared<UnwindPlan::Row>(); 262 row->SetOffset(0); 263 264 // In the signal trampoline frame, sp points to an rt_sigframe[1], which is: 265 // - 128-byte siginfo struct 266 // - ucontext struct: 267 // - 8-byte long (uc_flags) 268 // - 8-byte pointer (uc_link) 269 // - 24-byte stack_t 270 // - 128-byte signal set 271 // - 8 bytes of padding because sigcontext has 16-byte alignment 272 // - sigcontext/mcontext_t 273 // [1] 274 // https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/signal.c 275 int32_t offset = 128 + 8 + 8 + 24 + 128 + 8; 276 // Then sigcontext[2] is: 277 // - 8 byte fault address 278 // - 31 8 byte registers 279 // - 8 byte sp 280 // - 8 byte pc 281 // [2] 282 // https://github.com/torvalds/linux/blob/master/arch/arm64/include/uapi/asm/sigcontext.h 283 284 // Skip fault address 285 offset += 8; 286 row->GetCFAValue().SetIsRegisterPlusOffset(arm64_dwarf::sp, offset); 287 288 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x0, 0 * 8, false); 289 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x1, 1 * 8, false); 290 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x2, 2 * 8, false); 291 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x3, 3 * 8, false); 292 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x4, 4 * 8, false); 293 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x5, 5 * 8, false); 294 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x6, 6 * 8, false); 295 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x7, 7 * 8, false); 296 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x8, 8 * 8, false); 297 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x9, 9 * 8, false); 298 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x10, 10 * 8, false); 299 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x11, 11 * 8, false); 300 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x12, 12 * 8, false); 301 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x13, 13 * 8, false); 302 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x14, 14 * 8, false); 303 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x15, 15 * 8, false); 304 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x16, 16 * 8, false); 305 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x17, 17 * 8, false); 306 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x18, 18 * 8, false); 307 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x19, 19 * 8, false); 308 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x20, 20 * 8, false); 309 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x21, 21 * 8, false); 310 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x22, 22 * 8, false); 311 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x23, 23 * 8, false); 312 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x24, 24 * 8, false); 313 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x25, 25 * 8, false); 314 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x26, 26 * 8, false); 315 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x27, 27 * 8, false); 316 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x28, 28 * 8, false); 317 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::fp, 29 * 8, false); 318 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x30, 30 * 8, false); 319 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::sp, 31 * 8, false); 320 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::pc, 32 * 8, false); 321 322 // The sigcontext may also contain floating point and SVE registers. 323 // However this would require a dynamic unwind plan so they are not included 324 // here. 325 326 unwind_plan_sp = std::make_shared<UnwindPlan>(eRegisterKindDWARF); 327 unwind_plan_sp->AppendRow(row); 328 unwind_plan_sp->SetSourceName("AArch64 Linux sigcontext"); 329 unwind_plan_sp->SetSourcedFromCompiler(eLazyBoolYes); 330 // Because sp is the same throughout the function 331 unwind_plan_sp->SetUnwindPlanValidAtAllInstructions(eLazyBoolYes); 332 unwind_plan_sp->SetUnwindPlanForSignalTrap(eLazyBoolYes); 333 334 return unwind_plan_sp; 335 } 336 337 lldb::UnwindPlanSP 338 PlatformLinux::GetTrapHandlerUnwindPlan(const llvm::Triple &triple, 339 ConstString name) { 340 if (triple.isAArch64()) 341 return GetAArch64TrapHanlderUnwindPlan(name); 342 343 return {}; 344 } 345 346 MmapArgList PlatformLinux::GetMmapArgumentList(const ArchSpec &arch, 347 addr_t addr, addr_t length, 348 unsigned prot, unsigned flags, 349 addr_t fd, addr_t offset) { 350 uint64_t flags_platform = 0; 351 uint64_t map_anon = arch.IsMIPS() ? 0x800 : MAP_ANON; 352 353 if (flags & eMmapFlagsPrivate) 354 flags_platform |= MAP_PRIVATE; 355 if (flags & eMmapFlagsAnon) 356 flags_platform |= map_anon; 357 358 MmapArgList args({addr, length, prot, flags_platform, fd, offset}); 359 return args; 360 } 361 362