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/LLDBLog.h" 26 #include "lldb/Utility/Log.h" 27 #include "lldb/Utility/State.h" 28 #include "lldb/Utility/Status.h" 29 #include "lldb/Utility/StreamString.h" 30 31 // Define these constants from Linux mman.h for use when targeting remote linux 32 // systems even when host has different values. 33 #define MAP_PRIVATE 2 34 #define MAP_ANON 0x20 35 36 using namespace lldb; 37 using namespace lldb_private; 38 using namespace lldb_private::platform_linux; 39 40 LLDB_PLUGIN_DEFINE(PlatformLinux) 41 42 static uint32_t g_initialize_count = 0; 43 44 45 PlatformSP PlatformLinux::CreateInstance(bool force, const ArchSpec *arch) { 46 Log *log = GetLog(LLDBLog::Platform); 47 LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force, 48 arch ? arch->GetArchitectureName() : "<null>", 49 arch ? arch->GetTriple().getTriple() : "<null>"); 50 51 bool create = force; 52 if (!create && arch && arch->IsValid()) { 53 const llvm::Triple &triple = arch->GetTriple(); 54 switch (triple.getOS()) { 55 case llvm::Triple::Linux: 56 create = true; 57 break; 58 59 #if defined(__linux__) 60 // Only accept "unknown" for the OS if the host is linux and it "unknown" 61 // wasn't specified (it was just returned because it was NOT specified) 62 case llvm::Triple::OSType::UnknownOS: 63 create = !arch->TripleOSWasSpecified(); 64 break; 65 #endif 66 default: 67 break; 68 } 69 } 70 71 LLDB_LOG(log, "create = {0}", create); 72 if (create) { 73 return PlatformSP(new PlatformLinux(false)); 74 } 75 return PlatformSP(); 76 } 77 78 llvm::StringRef PlatformLinux::GetPluginDescriptionStatic(bool is_host) { 79 if (is_host) 80 return "Local Linux user platform plug-in."; 81 return "Remote Linux user platform plug-in."; 82 } 83 84 void PlatformLinux::Initialize() { 85 PlatformPOSIX::Initialize(); 86 87 if (g_initialize_count++ == 0) { 88 #if defined(__linux__) && !defined(__ANDROID__) 89 PlatformSP default_platform_sp(new PlatformLinux(true)); 90 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 91 Platform::SetHostPlatform(default_platform_sp); 92 #endif 93 PluginManager::RegisterPlugin( 94 PlatformLinux::GetPluginNameStatic(false), 95 PlatformLinux::GetPluginDescriptionStatic(false), 96 PlatformLinux::CreateInstance, nullptr); 97 } 98 } 99 100 void PlatformLinux::Terminate() { 101 if (g_initialize_count > 0) { 102 if (--g_initialize_count == 0) { 103 PluginManager::UnregisterPlugin(PlatformLinux::CreateInstance); 104 } 105 } 106 107 PlatformPOSIX::Terminate(); 108 } 109 110 /// Default Constructor 111 PlatformLinux::PlatformLinux(bool is_host) 112 : PlatformPOSIX(is_host) // This is the local host platform 113 { 114 if (is_host) { 115 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 116 m_supported_architectures.push_back(hostArch); 117 if (hostArch.GetTriple().isArch64Bit()) { 118 m_supported_architectures.push_back( 119 HostInfo::GetArchitecture(HostInfo::eArchKind32)); 120 } 121 } else { 122 m_supported_architectures = CreateArchList( 123 {llvm::Triple::x86_64, llvm::Triple::x86, llvm::Triple::arm, 124 llvm::Triple::aarch64, llvm::Triple::mips64, llvm::Triple::mips64, 125 llvm::Triple::hexagon, llvm::Triple::mips, llvm::Triple::mips64el, 126 llvm::Triple::mipsel, llvm::Triple::systemz}, 127 llvm::Triple::Linux); 128 } 129 } 130 131 std::vector<ArchSpec> PlatformLinux::GetSupportedArchitectures() { 132 if (m_remote_platform_sp) 133 return m_remote_platform_sp->GetSupportedArchitectures(); 134 return m_supported_architectures; 135 } 136 137 void PlatformLinux::GetStatus(Stream &strm) { 138 Platform::GetStatus(strm); 139 140 #if LLDB_ENABLE_POSIX 141 // Display local kernel information only when we are running in host mode. 142 // Otherwise, we would end up printing non-Linux information (when running on 143 // Mac OS for example). 144 if (IsHost()) { 145 struct utsname un; 146 147 if (uname(&un)) 148 return; 149 150 strm.Printf(" Kernel: %s\n", un.sysname); 151 strm.Printf(" Release: %s\n", un.release); 152 strm.Printf(" Version: %s\n", un.version); 153 } 154 #endif 155 } 156 157 uint32_t 158 PlatformLinux::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) { 159 uint32_t resume_count = 0; 160 161 // Always resume past the initial stop when we use eLaunchFlagDebug 162 if (launch_info.GetFlags().Test(eLaunchFlagDebug)) { 163 // Resume past the stop for the final exec into the true inferior. 164 ++resume_count; 165 } 166 167 // If we're not launching a shell, we're done. 168 const FileSpec &shell = launch_info.GetShell(); 169 if (!shell) 170 return resume_count; 171 172 std::string shell_string = shell.GetPath(); 173 // We're in a shell, so for sure we have to resume past the shell exec. 174 ++resume_count; 175 176 // Figure out what shell we're planning on using. 177 const char *shell_name = strrchr(shell_string.c_str(), '/'); 178 if (shell_name == nullptr) 179 shell_name = shell_string.c_str(); 180 else 181 shell_name++; 182 183 if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 || 184 strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) { 185 // These shells seem to re-exec themselves. Add another resume. 186 ++resume_count; 187 } 188 189 return resume_count; 190 } 191 192 bool PlatformLinux::CanDebugProcess() { 193 if (IsHost()) { 194 return true; 195 } else { 196 // If we're connected, we can debug. 197 return IsConnected(); 198 } 199 } 200 201 void PlatformLinux::CalculateTrapHandlerSymbolNames() { 202 m_trap_handlers.push_back(ConstString("_sigtramp")); 203 m_trap_handlers.push_back(ConstString("__kernel_rt_sigreturn")); 204 m_trap_handlers.push_back(ConstString("__restore_rt")); 205 } 206 207 static lldb::UnwindPlanSP GetAArch64TrapHanlderUnwindPlan(ConstString name) { 208 UnwindPlanSP unwind_plan_sp; 209 if (name != "__kernel_rt_sigreturn") 210 return unwind_plan_sp; 211 212 UnwindPlan::RowSP row = std::make_shared<UnwindPlan::Row>(); 213 row->SetOffset(0); 214 215 // In the signal trampoline frame, sp points to an rt_sigframe[1], which is: 216 // - 128-byte siginfo struct 217 // - ucontext struct: 218 // - 8-byte long (uc_flags) 219 // - 8-byte pointer (uc_link) 220 // - 24-byte stack_t 221 // - 128-byte signal set 222 // - 8 bytes of padding because sigcontext has 16-byte alignment 223 // - sigcontext/mcontext_t 224 // [1] 225 // https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/signal.c 226 int32_t offset = 128 + 8 + 8 + 24 + 128 + 8; 227 // Then sigcontext[2] is: 228 // - 8 byte fault address 229 // - 31 8 byte registers 230 // - 8 byte sp 231 // - 8 byte pc 232 // [2] 233 // https://github.com/torvalds/linux/blob/master/arch/arm64/include/uapi/asm/sigcontext.h 234 235 // Skip fault address 236 offset += 8; 237 row->GetCFAValue().SetIsRegisterPlusOffset(arm64_dwarf::sp, offset); 238 239 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x0, 0 * 8, false); 240 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x1, 1 * 8, false); 241 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x2, 2 * 8, false); 242 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x3, 3 * 8, false); 243 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x4, 4 * 8, false); 244 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x5, 5 * 8, false); 245 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x6, 6 * 8, false); 246 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x7, 7 * 8, false); 247 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x8, 8 * 8, false); 248 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x9, 9 * 8, false); 249 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x10, 10 * 8, false); 250 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x11, 11 * 8, false); 251 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x12, 12 * 8, false); 252 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x13, 13 * 8, false); 253 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x14, 14 * 8, false); 254 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x15, 15 * 8, false); 255 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x16, 16 * 8, false); 256 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x17, 17 * 8, false); 257 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x18, 18 * 8, false); 258 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x19, 19 * 8, false); 259 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x20, 20 * 8, false); 260 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x21, 21 * 8, false); 261 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x22, 22 * 8, false); 262 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x23, 23 * 8, false); 263 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x24, 24 * 8, false); 264 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x25, 25 * 8, false); 265 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x26, 26 * 8, false); 266 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x27, 27 * 8, false); 267 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x28, 28 * 8, false); 268 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::fp, 29 * 8, false); 269 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::x30, 30 * 8, false); 270 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::sp, 31 * 8, false); 271 row->SetRegisterLocationToAtCFAPlusOffset(arm64_dwarf::pc, 32 * 8, false); 272 273 // The sigcontext may also contain floating point and SVE registers. 274 // However this would require a dynamic unwind plan so they are not included 275 // here. 276 277 unwind_plan_sp = std::make_shared<UnwindPlan>(eRegisterKindDWARF); 278 unwind_plan_sp->AppendRow(row); 279 unwind_plan_sp->SetSourceName("AArch64 Linux sigcontext"); 280 unwind_plan_sp->SetSourcedFromCompiler(eLazyBoolYes); 281 // Because sp is the same throughout the function 282 unwind_plan_sp->SetUnwindPlanValidAtAllInstructions(eLazyBoolYes); 283 unwind_plan_sp->SetUnwindPlanForSignalTrap(eLazyBoolYes); 284 285 return unwind_plan_sp; 286 } 287 288 lldb::UnwindPlanSP 289 PlatformLinux::GetTrapHandlerUnwindPlan(const llvm::Triple &triple, 290 ConstString name) { 291 if (triple.isAArch64()) 292 return GetAArch64TrapHanlderUnwindPlan(name); 293 294 return {}; 295 } 296 297 MmapArgList PlatformLinux::GetMmapArgumentList(const ArchSpec &arch, 298 addr_t addr, addr_t length, 299 unsigned prot, unsigned flags, 300 addr_t fd, addr_t offset) { 301 uint64_t flags_platform = 0; 302 uint64_t map_anon = arch.IsMIPS() ? 0x800 : MAP_ANON; 303 304 if (flags & eMmapFlagsPrivate) 305 flags_platform |= MAP_PRIVATE; 306 if (flags & eMmapFlagsAnon) 307 flags_platform |= map_anon; 308 309 MmapArgList args({addr, length, prot, flags_platform, fd, offset}); 310 return args; 311 } 312 313 CompilerType PlatformLinux::GetSiginfoType(const llvm::Triple &triple) { 314 if (!m_type_system_up) 315 m_type_system_up.reset(new TypeSystemClang("siginfo", triple)); 316 TypeSystemClang *ast = m_type_system_up.get(); 317 318 bool si_errno_then_code = true; 319 320 switch (triple.getArch()) { 321 case llvm::Triple::mips: 322 case llvm::Triple::mipsel: 323 case llvm::Triple::mips64: 324 case llvm::Triple::mips64el: 325 // mips has si_code and si_errno swapped 326 si_errno_then_code = false; 327 break; 328 default: 329 break; 330 } 331 332 // generic types 333 CompilerType int_type = ast->GetBasicType(eBasicTypeInt); 334 CompilerType uint_type = ast->GetBasicType(eBasicTypeUnsignedInt); 335 CompilerType short_type = ast->GetBasicType(eBasicTypeShort); 336 CompilerType long_type = ast->GetBasicType(eBasicTypeLong); 337 CompilerType voidp_type = ast->GetBasicType(eBasicTypeVoid).GetPointerType(); 338 339 // platform-specific types 340 CompilerType &pid_type = int_type; 341 CompilerType &uid_type = uint_type; 342 CompilerType &clock_type = long_type; 343 CompilerType &band_type = long_type; 344 345 CompilerType sigval_type = ast->CreateRecordType( 346 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_sigval_t", 347 clang::TTK_Union, lldb::eLanguageTypeC); 348 ast->StartTagDeclarationDefinition(sigval_type); 349 ast->AddFieldToRecordType(sigval_type, "sival_int", int_type, 350 lldb::eAccessPublic, 0); 351 ast->AddFieldToRecordType(sigval_type, "sival_ptr", voidp_type, 352 lldb::eAccessPublic, 0); 353 ast->CompleteTagDeclarationDefinition(sigval_type); 354 355 CompilerType sigfault_bounds_type = ast->CreateRecordType( 356 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "", 357 clang::TTK_Union, lldb::eLanguageTypeC); 358 ast->StartTagDeclarationDefinition(sigfault_bounds_type); 359 ast->AddFieldToRecordType(sigfault_bounds_type, "_addr_bnd", 360 ast->CreateStructForIdentifier(ConstString(), 361 { 362 {"_lower", voidp_type}, 363 {"_upper", voidp_type}, 364 }), 365 lldb::eAccessPublic, 0); 366 ast->AddFieldToRecordType(sigfault_bounds_type, "_pkey", uint_type, 367 lldb::eAccessPublic, 0); 368 ast->CompleteTagDeclarationDefinition(sigfault_bounds_type); 369 370 // siginfo_t 371 CompilerType siginfo_type = ast->CreateRecordType( 372 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_siginfo_t", 373 clang::TTK_Struct, lldb::eLanguageTypeC); 374 ast->StartTagDeclarationDefinition(siginfo_type); 375 ast->AddFieldToRecordType(siginfo_type, "si_signo", int_type, 376 lldb::eAccessPublic, 0); 377 378 if (si_errno_then_code) { 379 ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type, 380 lldb::eAccessPublic, 0); 381 ast->AddFieldToRecordType(siginfo_type, "si_code", int_type, 382 lldb::eAccessPublic, 0); 383 } else { 384 ast->AddFieldToRecordType(siginfo_type, "si_code", int_type, 385 lldb::eAccessPublic, 0); 386 ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type, 387 lldb::eAccessPublic, 0); 388 } 389 390 // the structure is padded on 64-bit arches to fix alignment 391 if (triple.isArch64Bit()) 392 ast->AddFieldToRecordType(siginfo_type, "__pad0", int_type, 393 lldb::eAccessPublic, 0); 394 395 // union used to hold the signal data 396 CompilerType union_type = ast->CreateRecordType( 397 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "", 398 clang::TTK_Union, lldb::eLanguageTypeC); 399 ast->StartTagDeclarationDefinition(union_type); 400 401 ast->AddFieldToRecordType( 402 union_type, "_kill", 403 ast->CreateStructForIdentifier(ConstString(), 404 { 405 {"si_pid", pid_type}, 406 {"si_uid", uid_type}, 407 }), 408 lldb::eAccessPublic, 0); 409 410 ast->AddFieldToRecordType( 411 union_type, "_timer", 412 ast->CreateStructForIdentifier(ConstString(), 413 { 414 {"si_tid", int_type}, 415 {"si_overrun", int_type}, 416 {"si_sigval", sigval_type}, 417 }), 418 lldb::eAccessPublic, 0); 419 420 ast->AddFieldToRecordType( 421 union_type, "_rt", 422 ast->CreateStructForIdentifier(ConstString(), 423 { 424 {"si_pid", pid_type}, 425 {"si_uid", uid_type}, 426 {"si_sigval", sigval_type}, 427 }), 428 lldb::eAccessPublic, 0); 429 430 ast->AddFieldToRecordType( 431 union_type, "_sigchld", 432 ast->CreateStructForIdentifier(ConstString(), 433 { 434 {"si_pid", pid_type}, 435 {"si_uid", uid_type}, 436 {"si_status", int_type}, 437 {"si_utime", clock_type}, 438 {"si_stime", clock_type}, 439 }), 440 lldb::eAccessPublic, 0); 441 442 ast->AddFieldToRecordType( 443 union_type, "_sigfault", 444 ast->CreateStructForIdentifier(ConstString(), 445 { 446 {"si_addr", voidp_type}, 447 {"si_addr_lsb", short_type}, 448 {"_bounds", sigfault_bounds_type}, 449 }), 450 lldb::eAccessPublic, 0); 451 452 ast->AddFieldToRecordType( 453 union_type, "_sigpoll", 454 ast->CreateStructForIdentifier(ConstString(), 455 { 456 {"si_band", band_type}, 457 {"si_fd", int_type}, 458 }), 459 lldb::eAccessPublic, 0); 460 461 // NB: SIGSYS is not present on ia64 but we don't seem to support that 462 ast->AddFieldToRecordType( 463 union_type, "_sigsys", 464 ast->CreateStructForIdentifier(ConstString(), 465 { 466 {"_call_addr", voidp_type}, 467 {"_syscall", int_type}, 468 {"_arch", uint_type}, 469 }), 470 lldb::eAccessPublic, 0); 471 472 ast->CompleteTagDeclarationDefinition(union_type); 473 ast->AddFieldToRecordType(siginfo_type, "_sifields", union_type, 474 lldb::eAccessPublic, 0); 475 476 ast->CompleteTagDeclarationDefinition(siginfo_type); 477 return siginfo_type; 478 } 479