LLDB  mainline
ThreadElfCore.cpp
Go to the documentation of this file.
1 //===-- ThreadElfCore.cpp --------------------------------------*- C++ -*-===//
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 
10 #include "lldb/Target/StopInfo.h"
11 #include "lldb/Target/Target.h"
12 #include "lldb/Target/Unwind.h"
14 #include "lldb/Utility/Log.h"
15 
31 #include "ProcessElfCore.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
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 
52 
54  GetRegisterContext()->InvalidateIfNeeded(false);
55 }
56 
57 RegisterContextSP ThreadElfCore::GetRegisterContext() {
58  if (!m_reg_context_sp) {
60  }
61  return m_reg_context_sp;
62 }
63 
64 RegisterContextSP
66  RegisterContextSP reg_ctx_sp;
67  uint32_t concrete_frame_idx = 0;
69 
70  if (frame)
71  concrete_frame_idx = frame->GetConcreteFrameIndex();
72 
73  if (concrete_frame_idx == 0) {
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  reg_interface = new RegisterInfoPOSIX_arm64(arch);
86  break;
87  case llvm::Triple::arm:
88  reg_interface = new RegisterInfoPOSIX_arm(arch);
89  break;
90  case llvm::Triple::ppc:
91  reg_interface = new RegisterContextFreeBSD_powerpc32(arch);
92  break;
93  case llvm::Triple::ppc64:
94  reg_interface = new RegisterContextFreeBSD_powerpc64(arch);
95  break;
96  case llvm::Triple::mips64:
97  reg_interface = new RegisterContextFreeBSD_mips64(arch);
98  break;
99  case llvm::Triple::x86:
100  reg_interface = new RegisterContextFreeBSD_i386(arch);
101  break;
102  case llvm::Triple::x86_64:
103  reg_interface = new RegisterContextFreeBSD_x86_64(arch);
104  break;
105  default:
106  break;
107  }
108  break;
109  }
110 
111  case llvm::Triple::NetBSD: {
112  switch (arch.GetMachine()) {
113  case llvm::Triple::aarch64:
114  reg_interface = new RegisterInfoPOSIX_arm64(arch);
115  break;
116  case llvm::Triple::x86_64:
117  reg_interface = new RegisterContextNetBSD_x86_64(arch);
118  break;
119  default:
120  break;
121  }
122  break;
123  }
124 
125  case llvm::Triple::Linux: {
126  switch (arch.GetMachine()) {
127  case llvm::Triple::arm:
128  reg_interface = new RegisterInfoPOSIX_arm(arch);
129  break;
130  case llvm::Triple::aarch64:
131  reg_interface = new RegisterInfoPOSIX_arm64(arch);
132  break;
133  case llvm::Triple::mipsel:
134  case llvm::Triple::mips:
135  reg_interface = new RegisterContextLinux_mips(arch);
136  break;
137  case llvm::Triple::mips64el:
138  case llvm::Triple::mips64:
139  reg_interface = new RegisterContextLinux_mips64(arch);
140  break;
141  case llvm::Triple::ppc64le:
142  reg_interface = new RegisterInfoPOSIX_ppc64le(arch);
143  break;
144  case llvm::Triple::systemz:
145  reg_interface = new RegisterContextLinux_s390x(arch);
146  break;
147  case llvm::Triple::x86:
148  reg_interface = new RegisterContextLinux_i386(arch);
149  break;
150  case llvm::Triple::x86_64:
151  reg_interface = new RegisterContextLinux_x86_64(arch);
152  break;
153  default:
154  break;
155  }
156  break;
157  }
158 
159  case llvm::Triple::OpenBSD: {
160  switch (arch.GetMachine()) {
161  case llvm::Triple::aarch64:
162  reg_interface = new RegisterInfoPOSIX_arm64(arch);
163  break;
164  case llvm::Triple::arm:
165  reg_interface = new RegisterInfoPOSIX_arm(arch);
166  break;
167  case llvm::Triple::x86:
168  reg_interface = new RegisterContextOpenBSD_i386(arch);
169  break;
170  case llvm::Triple::x86_64:
171  reg_interface = new RegisterContextOpenBSD_x86_64(arch);
172  break;
173  default:
174  break;
175  }
176  break;
177  }
178 
179  default:
180  break;
181  }
182 
183  if (!reg_interface) {
184  if (log)
185  log->Printf("elf-core::%s:: Architecture(%d) or OS(%d) not supported",
186  __FUNCTION__, arch.GetMachine(), arch.GetTriple().getOS());
187  assert(false && "Architecture or OS not supported");
188  }
189 
190  switch (arch.GetMachine()) {
191  case llvm::Triple::aarch64:
192  m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_arm64>(
193  *this, reg_interface, m_gpregset_data, m_notes);
194  break;
195  case llvm::Triple::arm:
196  m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_arm>(
197  *this, reg_interface, m_gpregset_data, m_notes);
198  break;
199  case llvm::Triple::mipsel:
200  case llvm::Triple::mips:
201  m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_mips64>(
202  *this, reg_interface, m_gpregset_data, m_notes);
203  break;
204  case llvm::Triple::mips64:
205  case llvm::Triple::mips64el:
206  m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_mips64>(
207  *this, reg_interface, m_gpregset_data, m_notes);
208  break;
209  case llvm::Triple::ppc:
210  case llvm::Triple::ppc64:
211  m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_powerpc>(
212  *this, reg_interface, m_gpregset_data, m_notes);
213  break;
214  case llvm::Triple::ppc64le:
215  m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_ppc64le>(
216  *this, reg_interface, m_gpregset_data, m_notes);
217  break;
218  case llvm::Triple::systemz:
219  m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_s390x>(
220  *this, reg_interface, m_gpregset_data, m_notes);
221  break;
222  case llvm::Triple::x86:
223  case llvm::Triple::x86_64:
224  m_thread_reg_ctx_sp = std::make_shared<RegisterContextCorePOSIX_x86_64>(
225  *this, reg_interface, m_gpregset_data, m_notes);
226  break;
227  default:
228  break;
229  }
230 
231  reg_ctx_sp = m_thread_reg_ctx_sp;
232  } else {
233  Unwind *unwinder = GetUnwinder();
234  if (unwinder != nullptr)
235  reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
236  }
237  return reg_ctx_sp;
238 }
239 
241  ProcessSP process_sp(GetProcess());
242  if (process_sp) {
243  SetStopInfo(StopInfo::CreateStopReasonWithSignal(*this, m_signo));
244  return true;
245  }
246  return false;
247 }
248 
249 // Parse PRSTATUS from NOTE entry
251  memset(this, 0, sizeof(ELFLinuxPrStatus));
252 }
253 
255  constexpr size_t mips_linux_pr_status_size_o32 = 96;
256  constexpr size_t mips_linux_pr_status_size_n32 = 72;
257  constexpr size_t num_ptr_size_members = 10;
258  if (arch.IsMIPS()) {
259  std::string abi = arch.GetTargetABI();
260  assert(!abi.empty() && "ABI is not set");
261  if (!abi.compare("n64"))
262  return sizeof(ELFLinuxPrStatus);
263  else if (!abi.compare("o32"))
264  return mips_linux_pr_status_size_o32;
265  // N32 ABI
266  return mips_linux_pr_status_size_n32;
267  }
268  switch (arch.GetCore()) {
271  return 72;
272  default:
273  if (arch.GetAddressByteSize() == 8)
274  return sizeof(ELFLinuxPrStatus);
275  else
276  return sizeof(ELFLinuxPrStatus) - num_ptr_size_members * 4;
277  }
278 }
279 
281  const ArchSpec &arch) {
282  Status error;
283  if (GetSize(arch) > data.GetByteSize()) {
285  "NT_PRSTATUS size should be %zu, but the remaining bytes are: %" PRIu64,
286  GetSize(arch), data.GetByteSize());
287  return error;
288  }
289 
290  // Read field by field to correctly account for endianess of both the core
291  // dump and the platform running lldb.
292  offset_t offset = 0;
293  si_signo = data.GetU32(&offset);
294  si_code = data.GetU32(&offset);
295  si_errno = data.GetU32(&offset);
296 
297  pr_cursig = data.GetU16(&offset);
298  offset += 2; // pad
299 
300  pr_sigpend = data.GetPointer(&offset);
301  pr_sighold = data.GetPointer(&offset);
302 
303  pr_pid = data.GetU32(&offset);
304  pr_ppid = data.GetU32(&offset);
305  pr_pgrp = data.GetU32(&offset);
306  pr_sid = data.GetU32(&offset);
307 
308  pr_utime.tv_sec = data.GetPointer(&offset);
309  pr_utime.tv_usec = data.GetPointer(&offset);
310 
311  pr_stime.tv_sec = data.GetPointer(&offset);
312  pr_stime.tv_usec = data.GetPointer(&offset);
313 
314  pr_cutime.tv_sec = data.GetPointer(&offset);
315  pr_cutime.tv_usec = data.GetPointer(&offset);
316 
317  pr_cstime.tv_sec = data.GetPointer(&offset);
318  pr_cstime.tv_usec = data.GetPointer(&offset);
319 
320  return error;
321 }
322 
323 // Parse PRPSINFO from NOTE entry
325  memset(this, 0, sizeof(ELFLinuxPrPsInfo));
326 }
327 
329  constexpr size_t mips_linux_pr_psinfo_size_o32_n32 = 128;
330  if (arch.IsMIPS()) {
331  uint8_t address_byte_size = arch.GetAddressByteSize();
332  if (address_byte_size == 8)
333  return sizeof(ELFLinuxPrPsInfo);
334  return mips_linux_pr_psinfo_size_o32_n32;
335  }
336 
337  switch (arch.GetCore()) {
340  return sizeof(ELFLinuxPrPsInfo);
343  return 124;
344  default:
345  return 0;
346  }
347 }
348 
350  const ArchSpec &arch) {
351  Status error;
352  ByteOrder byteorder = data.GetByteOrder();
353  if (GetSize(arch) > data.GetByteSize()) {
355  "NT_PRPSINFO size should be %zu, but the remaining bytes are: %" PRIu64,
356  GetSize(arch), data.GetByteSize());
357  return error;
358  }
359  size_t size = 0;
360  offset_t offset = 0;
361 
362  pr_state = data.GetU8(&offset);
363  pr_sname = data.GetU8(&offset);
364  pr_zomb = data.GetU8(&offset);
365  pr_nice = data.GetU8(&offset);
366  if (data.GetAddressByteSize() == 8) {
367  // Word align the next field on 64 bit.
368  offset += 4;
369  }
370 
371  pr_flag = data.GetPointer(&offset);
372 
373  if (arch.IsMIPS()) {
374  // The pr_uid and pr_gid is always 32 bit irrespective of platforms
375  pr_uid = data.GetU32(&offset);
376  pr_gid = data.GetU32(&offset);
377  } else {
378  // 16 bit on 32 bit platforms, 32 bit on 64 bit platforms
379  pr_uid = data.GetMaxU64(&offset, data.GetAddressByteSize() >> 1);
380  pr_gid = data.GetMaxU64(&offset, data.GetAddressByteSize() >> 1);
381  }
382 
383  pr_pid = data.GetU32(&offset);
384  pr_ppid = data.GetU32(&offset);
385  pr_pgrp = data.GetU32(&offset);
386  pr_sid = data.GetU32(&offset);
387 
388  size = 16;
389  data.ExtractBytes(offset, size, byteorder, pr_fname);
390  offset += size;
391 
392  size = 80;
393  data.ExtractBytes(offset, size, byteorder, pr_psargs);
394  offset += size;
395 
396  return error;
397 }
398 
399 // Parse SIGINFO from NOTE entry
400 ELFLinuxSigInfo::ELFLinuxSigInfo() { memset(this, 0, sizeof(ELFLinuxSigInfo)); }
401 
403  if (arch.IsMIPS())
404  return sizeof(ELFLinuxSigInfo);
405  switch (arch.GetCore()) {
407  return sizeof(ELFLinuxSigInfo);
411  return 12;
412  default:
413  return 0;
414  }
415 }
416 
418  Status error;
419  if (GetSize(arch) > data.GetByteSize()) {
421  "NT_SIGINFO size should be %zu, but the remaining bytes are: %" PRIu64,
422  GetSize(arch), data.GetByteSize());
423  return error;
424  }
425 
426  // Parsing from a 32 bit ELF core file, and populating/reusing the structure
427  // properly, because the struct is for the 64 bit version
428  offset_t offset = 0;
429  si_signo = data.GetU32(&offset);
430  si_code = data.GetU32(&offset);
431  si_errno = data.GetU32(&offset);
432 
433  return error;
434 }
An data extractor class.
Definition: DataExtractor.h:47
Core GetCore() const
Definition: ArchSpec.h:410
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
static size_t GetSize(const lldb_private::ArchSpec &arch)
lldb_private::DataExtractor m_gpregset_data
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
Definition: ArchSpec.cpp:742
RegisterInfo interface to patch RegisterInfo structure for archs.
lldb_private::ArchSpec GetArchitecture()
An architecture specification class.
Definition: ArchSpec.h:32
lldb::RegisterContextSP GetRegisterContext() override
lldb_private::Status Parse(const lldb_private::DataExtractor &data, const lldb_private::ArchSpec &arch)
std::vector< lldb_private::CoreNote > m_notes
size_t ExtractBytes(lldb::offset_t offset, lldb::offset_t length, lldb::ByteOrder dst_byte_order, void *dst) const
Extract an arbitrary number of bytes in the specified byte order.
lldb::RegisterContextSP m_thread_reg_ctx_sp
void RefreshStateAfterStop() override
lldb::RegisterContextSP m_reg_context_sp
The register context for this thread&#39;s current register state.
Definition: Thread.h:1220
lldb_private::Status Parse(const lldb_private::DataExtractor &data, const lldb_private::ArchSpec &arch)
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:431
lldb_private::Status Parse(const lldb_private::DataExtractor &data, const lldb_private::ArchSpec &arch)
virtual lldb_private::Unwind * GetUnwinder()
Definition: Thread.cpp:2056
lldb::RegisterContextSP CreateRegisterContextForFrame(StackFrame *frame)
Definition: Unwind.h:55
static size_t GetSize(const lldb_private::ArchSpec &arch)
uint64_t offset_t
Definition: lldb-types.h:87
std::string GetTargetABI() const
Return a string representing target application ABI.
Definition: ArchSpec.cpp:604
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:57
void SetStopInfo(const lldb::StopInfoSP &stop_info_sp)
Definition: Thread.cpp:477
#define LIBLLDB_LOG_THREAD
Definition: Logging.h:16
lldb::ByteOrder GetByteOrder() const
Get the current byte order value.
A plug-in interface definition class for debugging a process.
Definition: Process.h:353
virtual void DestroyThread()
Definition: Thread.cpp:278
bool CalculateStopInfo() override
lldb::ProcessSP GetProcess() const
Definition: Thread.h:154
bool IsMIPS() const
if MIPS architecture return true.
Definition: ArchSpec.cpp:598
uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
~ThreadElfCore() override
static size_t GetSize(const lldb_private::ArchSpec &arch)
Definition: SBAddress.h:15
ThreadElfCore(lldb_private::Process &process, const ThreadData &td)
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:255
lldb::RegisterContextSP CreateRegisterContextForFrame(lldb_private::StackFrame *frame) override
uint32_t GetConcreteFrameIndex() const
Query this frame to find what frame it is in this Thread&#39;s StackFrameList, not counting inlined frame...
Definition: StackFrame.h:389
void Printf(const char *format,...) __attribute__((format(printf
Definition: Log.cpp:113
uint64_t GetPointer(lldb::offset_t *offset_ptr) const
Extract an pointer from *offset_ptr.
uint16_t GetU16(lldb::offset_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition: ArchSpec.cpp:726
This base class provides an interface to stack frames.
Definition: StackFrame.h:40
uint32_t GetAddressByteSize() const
Get the current address size.
An error handling class.
Definition: Status.h:44