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