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