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