1 //===-- ThreadElfCore.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 "lldb/Target/RegisterContext.h" 10 #include "lldb/Target/StopInfo.h" 11 #include "lldb/Target/Target.h" 12 #include "lldb/Target/Unwind.h" 13 #include "lldb/Utility/DataExtractor.h" 14 #include "lldb/Utility/Log.h" 15 16 #include "Plugins/Process/Utility/RegisterContextFreeBSD_i386.h" 17 #include "Plugins/Process/Utility/RegisterContextFreeBSD_mips64.h" 18 #include "Plugins/Process/Utility/RegisterContextFreeBSD_powerpc.h" 19 #include "Plugins/Process/Utility/RegisterContextFreeBSD_x86_64.h" 20 #include "Plugins/Process/Utility/RegisterContextLinux_i386.h" 21 #include "Plugins/Process/Utility/RegisterContextLinux_mips.h" 22 #include "Plugins/Process/Utility/RegisterContextLinux_mips64.h" 23 #include "Plugins/Process/Utility/RegisterContextLinux_s390x.h" 24 #include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h" 25 #include "Plugins/Process/Utility/RegisterContextNetBSD_x86_64.h" 26 #include "Plugins/Process/Utility/RegisterContextOpenBSD_i386.h" 27 #include "Plugins/Process/Utility/RegisterContextOpenBSD_x86_64.h" 28 #include "Plugins/Process/Utility/RegisterInfoPOSIX_arm.h" 29 #include "Plugins/Process/Utility/RegisterInfoPOSIX_arm64.h" 30 #include "Plugins/Process/Utility/RegisterInfoPOSIX_ppc64le.h" 31 #include "ProcessElfCore.h" 32 #include "RegisterContextPOSIXCore_arm.h" 33 #include "RegisterContextPOSIXCore_arm64.h" 34 #include "RegisterContextPOSIXCore_mips64.h" 35 #include "RegisterContextPOSIXCore_powerpc.h" 36 #include "RegisterContextPOSIXCore_ppc64le.h" 37 #include "RegisterContextPOSIXCore_s390x.h" 38 #include "RegisterContextPOSIXCore_x86_64.h" 39 #include "ThreadElfCore.h" 40 41 #include <memory> 42 43 using namespace lldb; 44 using namespace lldb_private; 45 46 // Construct a Thread object with given data 47 ThreadElfCore::ThreadElfCore(Process &process, const ThreadData &td) 48 : Thread(process, td.tid), m_thread_name(td.name), m_thread_reg_ctx_sp(), 49 m_signo(td.signo), m_gpregset_data(td.gpregset), m_notes(td.notes) {} 50 51 ThreadElfCore::~ThreadElfCore() { DestroyThread(); } 52 53 void ThreadElfCore::RefreshStateAfterStop() { 54 GetRegisterContext()->InvalidateIfNeeded(false); 55 } 56 57 RegisterContextSP ThreadElfCore::GetRegisterContext() { 58 if (!m_reg_context_sp) { 59 m_reg_context_sp = CreateRegisterContextForFrame(nullptr); 60 } 61 return m_reg_context_sp; 62 } 63 64 RegisterContextSP 65 ThreadElfCore::CreateRegisterContextForFrame(StackFrame *frame) { 66 RegisterContextSP reg_ctx_sp; 67 uint32_t concrete_frame_idx = 0; 68 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 69 70 if (frame) 71 concrete_frame_idx = frame->GetConcreteFrameIndex(); 72 73 if (concrete_frame_idx == 0) { 74 if (m_thread_reg_ctx_sp) 75 return m_thread_reg_ctx_sp; 76 77 ProcessElfCore *process = static_cast<ProcessElfCore *>(GetProcess().get()); 78 ArchSpec arch = process->GetArchitecture(); 79 RegisterInfoInterface *reg_interface = nullptr; 80 81 switch (arch.GetTriple().getOS()) { 82 case llvm::Triple::FreeBSD: { 83 switch (arch.GetMachine()) { 84 case llvm::Triple::aarch64: 85 break; 86 case llvm::Triple::arm: 87 reg_interface = new RegisterInfoPOSIX_arm(arch); 88 break; 89 case llvm::Triple::ppc: 90 reg_interface = new RegisterContextFreeBSD_powerpc32(arch); 91 break; 92 case llvm::Triple::ppc64: 93 reg_interface = new RegisterContextFreeBSD_powerpc64(arch); 94 break; 95 case llvm::Triple::mips64: 96 reg_interface = new RegisterContextFreeBSD_mips64(arch); 97 break; 98 case llvm::Triple::x86: 99 reg_interface = new RegisterContextFreeBSD_i386(arch); 100 break; 101 case llvm::Triple::x86_64: 102 reg_interface = new RegisterContextFreeBSD_x86_64(arch); 103 break; 104 default: 105 break; 106 } 107 break; 108 } 109 110 case llvm::Triple::NetBSD: { 111 switch (arch.GetMachine()) { 112 case llvm::Triple::aarch64: 113 break; 114 case llvm::Triple::x86_64: 115 reg_interface = new RegisterContextNetBSD_x86_64(arch); 116 break; 117 default: 118 break; 119 } 120 break; 121 } 122 123 case llvm::Triple::Linux: { 124 switch (arch.GetMachine()) { 125 case llvm::Triple::arm: 126 reg_interface = new RegisterInfoPOSIX_arm(arch); 127 break; 128 case llvm::Triple::aarch64: 129 break; 130 case llvm::Triple::mipsel: 131 case llvm::Triple::mips: 132 reg_interface = new RegisterContextLinux_mips(arch); 133 break; 134 case llvm::Triple::mips64el: 135 case llvm::Triple::mips64: 136 reg_interface = new RegisterContextLinux_mips64(arch); 137 break; 138 case llvm::Triple::ppc64le: 139 reg_interface = new RegisterInfoPOSIX_ppc64le(arch); 140 break; 141 case llvm::Triple::systemz: 142 reg_interface = new RegisterContextLinux_s390x(arch); 143 break; 144 case llvm::Triple::x86: 145 reg_interface = new RegisterContextLinux_i386(arch); 146 break; 147 case llvm::Triple::x86_64: 148 reg_interface = new RegisterContextLinux_x86_64(arch); 149 break; 150 default: 151 break; 152 } 153 break; 154 } 155 156 case llvm::Triple::OpenBSD: { 157 switch (arch.GetMachine()) { 158 case llvm::Triple::aarch64: 159 break; 160 case llvm::Triple::arm: 161 reg_interface = new RegisterInfoPOSIX_arm(arch); 162 break; 163 case llvm::Triple::x86: 164 reg_interface = new RegisterContextOpenBSD_i386(arch); 165 break; 166 case llvm::Triple::x86_64: 167 reg_interface = new RegisterContextOpenBSD_x86_64(arch); 168 break; 169 default: 170 break; 171 } 172 break; 173 } 174 175 default: 176 break; 177 } 178 179 if (!reg_interface && arch.GetMachine() != llvm::Triple::aarch64) { 180 LLDB_LOGF(log, "elf-core::%s:: Architecture(%d) or OS(%d) not supported", 181 __FUNCTION__, arch.GetMachine(), arch.GetTriple().getOS()); 182 assert(false && "Architecture or OS not supported"); 183 } 184 185 switch (arch.GetMachine()) { 186 case llvm::Triple::aarch64: 187 m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_arm64>( 188 *this, std::make_unique<RegisterInfoPOSIX_arm64>(arch), 189 m_gpregset_data, m_notes); 190 break; 191 case llvm::Triple::arm: 192 m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_arm>( 193 *this, reg_interface, m_gpregset_data, m_notes); 194 break; 195 case llvm::Triple::mipsel: 196 case llvm::Triple::mips: 197 m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_mips64>( 198 *this, reg_interface, m_gpregset_data, m_notes); 199 break; 200 case llvm::Triple::mips64: 201 case llvm::Triple::mips64el: 202 m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_mips64>( 203 *this, reg_interface, m_gpregset_data, m_notes); 204 break; 205 case llvm::Triple::ppc: 206 case llvm::Triple::ppc64: 207 m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_powerpc>( 208 *this, reg_interface, m_gpregset_data, m_notes); 209 break; 210 case llvm::Triple::ppc64le: 211 m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_ppc64le>( 212 *this, reg_interface, m_gpregset_data, m_notes); 213 break; 214 case llvm::Triple::systemz: 215 m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_s390x>( 216 *this, reg_interface, m_gpregset_data, m_notes); 217 break; 218 case llvm::Triple::x86: 219 case llvm::Triple::x86_64: 220 m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_x86_64>( 221 *this, reg_interface, m_gpregset_data, m_notes); 222 break; 223 default: 224 break; 225 } 226 227 reg_ctx_sp = m_thread_reg_ctx_sp; 228 } else { 229 reg_ctx_sp = GetUnwinder().CreateRegisterContextForFrame(frame); 230 } 231 return reg_ctx_sp; 232 } 233 234 bool ThreadElfCore::CalculateStopInfo() { 235 ProcessSP process_sp(GetProcess()); 236 if (process_sp) { 237 SetStopInfo(StopInfo::CreateStopReasonWithSignal(*this, m_signo)); 238 return true; 239 } 240 return false; 241 } 242 243 // Parse PRSTATUS from NOTE entry 244 ELFLinuxPrStatus::ELFLinuxPrStatus() { 245 memset(this, 0, sizeof(ELFLinuxPrStatus)); 246 } 247 248 size_t ELFLinuxPrStatus::GetSize(const lldb_private::ArchSpec &arch) { 249 constexpr size_t mips_linux_pr_status_size_o32 = 96; 250 constexpr size_t mips_linux_pr_status_size_n32 = 72; 251 constexpr size_t num_ptr_size_members = 10; 252 if (arch.IsMIPS()) { 253 std::string abi = arch.GetTargetABI(); 254 assert(!abi.empty() && "ABI is not set"); 255 if (!abi.compare("n64")) 256 return sizeof(ELFLinuxPrStatus); 257 else if (!abi.compare("o32")) 258 return mips_linux_pr_status_size_o32; 259 // N32 ABI 260 return mips_linux_pr_status_size_n32; 261 } 262 switch (arch.GetCore()) { 263 case lldb_private::ArchSpec::eCore_x86_32_i386: 264 case lldb_private::ArchSpec::eCore_x86_32_i486: 265 return 72; 266 default: 267 if (arch.GetAddressByteSize() == 8) 268 return sizeof(ELFLinuxPrStatus); 269 else 270 return sizeof(ELFLinuxPrStatus) - num_ptr_size_members * 4; 271 } 272 } 273 274 Status ELFLinuxPrStatus::Parse(const DataExtractor &data, 275 const ArchSpec &arch) { 276 Status error; 277 if (GetSize(arch) > data.GetByteSize()) { 278 error.SetErrorStringWithFormat( 279 "NT_PRSTATUS size should be %zu, but the remaining bytes are: %" PRIu64, 280 GetSize(arch), data.GetByteSize()); 281 return error; 282 } 283 284 // Read field by field to correctly account for endianess of both the core 285 // dump and the platform running lldb. 286 offset_t offset = 0; 287 si_signo = data.GetU32(&offset); 288 si_code = data.GetU32(&offset); 289 si_errno = data.GetU32(&offset); 290 291 pr_cursig = data.GetU16(&offset); 292 offset += 2; // pad 293 294 pr_sigpend = data.GetAddress(&offset); 295 pr_sighold = data.GetAddress(&offset); 296 297 pr_pid = data.GetU32(&offset); 298 pr_ppid = data.GetU32(&offset); 299 pr_pgrp = data.GetU32(&offset); 300 pr_sid = data.GetU32(&offset); 301 302 pr_utime.tv_sec = data.GetAddress(&offset); 303 pr_utime.tv_usec = data.GetAddress(&offset); 304 305 pr_stime.tv_sec = data.GetAddress(&offset); 306 pr_stime.tv_usec = data.GetAddress(&offset); 307 308 pr_cutime.tv_sec = data.GetAddress(&offset); 309 pr_cutime.tv_usec = data.GetAddress(&offset); 310 311 pr_cstime.tv_sec = data.GetAddress(&offset); 312 pr_cstime.tv_usec = data.GetAddress(&offset); 313 314 return error; 315 } 316 317 // Parse PRPSINFO from NOTE entry 318 ELFLinuxPrPsInfo::ELFLinuxPrPsInfo() { 319 memset(this, 0, sizeof(ELFLinuxPrPsInfo)); 320 } 321 322 size_t ELFLinuxPrPsInfo::GetSize(const lldb_private::ArchSpec &arch) { 323 constexpr size_t mips_linux_pr_psinfo_size_o32_n32 = 128; 324 if (arch.IsMIPS()) { 325 uint8_t address_byte_size = arch.GetAddressByteSize(); 326 if (address_byte_size == 8) 327 return sizeof(ELFLinuxPrPsInfo); 328 return mips_linux_pr_psinfo_size_o32_n32; 329 } 330 331 switch (arch.GetCore()) { 332 case lldb_private::ArchSpec::eCore_s390x_generic: 333 case lldb_private::ArchSpec::eCore_x86_64_x86_64: 334 return sizeof(ELFLinuxPrPsInfo); 335 case lldb_private::ArchSpec::eCore_x86_32_i386: 336 case lldb_private::ArchSpec::eCore_x86_32_i486: 337 return 124; 338 default: 339 return 0; 340 } 341 } 342 343 Status ELFLinuxPrPsInfo::Parse(const DataExtractor &data, 344 const ArchSpec &arch) { 345 Status error; 346 ByteOrder byteorder = data.GetByteOrder(); 347 if (GetSize(arch) > data.GetByteSize()) { 348 error.SetErrorStringWithFormat( 349 "NT_PRPSINFO size should be %zu, but the remaining bytes are: %" PRIu64, 350 GetSize(arch), data.GetByteSize()); 351 return error; 352 } 353 size_t size = 0; 354 offset_t offset = 0; 355 356 pr_state = data.GetU8(&offset); 357 pr_sname = data.GetU8(&offset); 358 pr_zomb = data.GetU8(&offset); 359 pr_nice = data.GetU8(&offset); 360 if (data.GetAddressByteSize() == 8) { 361 // Word align the next field on 64 bit. 362 offset += 4; 363 } 364 365 pr_flag = data.GetAddress(&offset); 366 367 if (arch.IsMIPS()) { 368 // The pr_uid and pr_gid is always 32 bit irrespective of platforms 369 pr_uid = data.GetU32(&offset); 370 pr_gid = data.GetU32(&offset); 371 } else { 372 // 16 bit on 32 bit platforms, 32 bit on 64 bit platforms 373 pr_uid = data.GetMaxU64(&offset, data.GetAddressByteSize() >> 1); 374 pr_gid = data.GetMaxU64(&offset, data.GetAddressByteSize() >> 1); 375 } 376 377 pr_pid = data.GetU32(&offset); 378 pr_ppid = data.GetU32(&offset); 379 pr_pgrp = data.GetU32(&offset); 380 pr_sid = data.GetU32(&offset); 381 382 size = 16; 383 data.ExtractBytes(offset, size, byteorder, pr_fname); 384 offset += size; 385 386 size = 80; 387 data.ExtractBytes(offset, size, byteorder, pr_psargs); 388 offset += size; 389 390 return error; 391 } 392 393 // Parse SIGINFO from NOTE entry 394 ELFLinuxSigInfo::ELFLinuxSigInfo() { memset(this, 0, sizeof(ELFLinuxSigInfo)); } 395 396 size_t ELFLinuxSigInfo::GetSize(const lldb_private::ArchSpec &arch) { 397 if (arch.IsMIPS()) 398 return sizeof(ELFLinuxSigInfo); 399 switch (arch.GetCore()) { 400 case lldb_private::ArchSpec::eCore_x86_64_x86_64: 401 return sizeof(ELFLinuxSigInfo); 402 case lldb_private::ArchSpec::eCore_s390x_generic: 403 case lldb_private::ArchSpec::eCore_x86_32_i386: 404 case lldb_private::ArchSpec::eCore_x86_32_i486: 405 return 12; 406 default: 407 return 0; 408 } 409 } 410 411 Status ELFLinuxSigInfo::Parse(const DataExtractor &data, const ArchSpec &arch) { 412 Status error; 413 if (GetSize(arch) > data.GetByteSize()) { 414 error.SetErrorStringWithFormat( 415 "NT_SIGINFO size should be %zu, but the remaining bytes are: %" PRIu64, 416 GetSize(arch), data.GetByteSize()); 417 return error; 418 } 419 420 // Parsing from a 32 bit ELF core file, and populating/reusing the structure 421 // properly, because the struct is for the 64 bit version 422 offset_t offset = 0; 423 si_signo = data.GetU32(&offset); 424 si_code = data.GetU32(&offset); 425 si_errno = data.GetU32(&offset); 426 427 return error; 428 } 429