LLDB  mainline
ProcessElfCore.cpp
Go to the documentation of this file.
1 //===-- ProcessElfCore.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 
9 #include <stdlib.h>
10 
11 #include <memory>
12 #include <mutex>
13 
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/ModuleSpec.h"
17 #include "lldb/Core/Section.h"
20 #include "lldb/Target/Target.h"
23 #include "lldb/Utility/Log.h"
24 #include "lldb/Utility/State.h"
25 
26 #include "llvm/BinaryFormat/ELF.h"
27 #include "llvm/Support/Threading.h"
28 
32 #include "ProcessElfCore.h"
33 #include "ThreadElfCore.h"
34 
35 using namespace lldb_private;
36 
38  static ConstString g_name("elf-core");
39  return g_name;
40 }
41 
43  return "ELF core dump plug-in.";
44 }
45 
48 }
49 
50 lldb::ProcessSP ProcessElfCore::CreateInstance(lldb::TargetSP target_sp,
51  lldb::ListenerSP listener_sp,
52  const FileSpec *crash_file) {
53  lldb::ProcessSP process_sp;
54  if (crash_file) {
55  // Read enough data for a ELF32 header or ELF64 header Note: Here we care
56  // about e_type field only, so it is safe to ignore possible presence of
57  // the header extension.
58  const size_t header_size = sizeof(llvm::ELF::Elf64_Ehdr);
59 
60  auto data_sp = FileSystem::Instance().CreateDataBuffer(
61  crash_file->GetPath(), header_size, 0);
62  if (data_sp && data_sp->GetByteSize() == header_size &&
63  elf::ELFHeader::MagicBytesMatch(data_sp->GetBytes())) {
64  elf::ELFHeader elf_header;
65  DataExtractor data(data_sp, lldb::eByteOrderLittle, 4);
66  lldb::offset_t data_offset = 0;
67  if (elf_header.Parse(data, &data_offset)) {
68  if (elf_header.e_type == llvm::ELF::ET_CORE)
69  process_sp = std::make_shared<ProcessElfCore>(target_sp, listener_sp,
70  *crash_file);
71  }
72  }
73  }
74  return process_sp;
75 }
76 
77 bool ProcessElfCore::CanDebug(lldb::TargetSP target_sp,
78  bool plugin_specified_by_name) {
79  // For now we are just making sure the file exists for a given module
80  if (!m_core_module_sp && FileSystem::Instance().Exists(m_core_file)) {
81  ModuleSpec core_module_spec(m_core_file, target_sp->GetArchitecture());
82  Status error(ModuleList::GetSharedModule(core_module_spec, m_core_module_sp,
83  NULL, NULL, NULL));
84  if (m_core_module_sp) {
85  ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
86  if (core_objfile && core_objfile->GetType() == ObjectFile::eTypeCoreFile)
87  return true;
88  }
89  }
90  return false;
91 }
92 
93 // ProcessElfCore constructor
94 ProcessElfCore::ProcessElfCore(lldb::TargetSP target_sp,
95  lldb::ListenerSP listener_sp,
96  const FileSpec &core_file)
97  : Process(target_sp, listener_sp), m_core_file(core_file) {}
98 
99 // Destructor
101  Clear();
102  // We need to call finalize on the process before destroying ourselves to
103  // make sure all of the broadcaster cleanup goes as planned. If we destruct
104  // this class, then Process::~Process() might have problems trying to fully
105  // destroy the broadcaster.
106  Finalize();
107 }
108 
109 // PluginInterface
111 
113 
114 lldb::addr_t ProcessElfCore::AddAddressRangeFromLoadSegment(
115  const elf::ELFProgramHeader &header) {
116  const lldb::addr_t addr = header.p_vaddr;
117  FileRange file_range(header.p_offset, header.p_filesz);
118  VMRangeToFileOffset::Entry range_entry(addr, header.p_memsz, file_range);
119 
120  VMRangeToFileOffset::Entry *last_entry = m_core_aranges.Back();
121  if (last_entry && last_entry->GetRangeEnd() == range_entry.GetRangeBase() &&
122  last_entry->data.GetRangeEnd() == range_entry.data.GetRangeBase() &&
123  last_entry->GetByteSize() == last_entry->data.GetByteSize()) {
124  last_entry->SetRangeEnd(range_entry.GetRangeEnd());
125  last_entry->data.SetRangeEnd(range_entry.data.GetRangeEnd());
126  } else {
127  m_core_aranges.Append(range_entry);
128  }
129 
130  // Keep a separate map of permissions that that isn't coalesced so all ranges
131  // are maintained.
132  const uint32_t permissions =
133  ((header.p_flags & llvm::ELF::PF_R) ? lldb::ePermissionsReadable : 0u) |
134  ((header.p_flags & llvm::ELF::PF_W) ? lldb::ePermissionsWritable : 0u) |
135  ((header.p_flags & llvm::ELF::PF_X) ? lldb::ePermissionsExecutable : 0u);
136 
137  m_core_range_infos.Append(
138  VMRangeToPermissions::Entry(addr, header.p_memsz, permissions));
139 
140  return addr;
141 }
142 
143 // Process Control
145  Status error;
146  if (!m_core_module_sp) {
147  error.SetErrorString("invalid core module");
148  return error;
149  }
150 
151  ObjectFileELF *core = (ObjectFileELF *)(m_core_module_sp->GetObjectFile());
152  if (core == NULL) {
153  error.SetErrorString("invalid core object file");
154  return error;
155  }
156 
157  llvm::ArrayRef<elf::ELFProgramHeader> segments = core->ProgramHeaders();
158  if (segments.size() == 0) {
159  error.SetErrorString("core file has no segments");
160  return error;
161  }
162 
163  SetCanJIT(false);
164 
165  m_thread_data_valid = true;
166 
167  bool ranges_are_sorted = true;
168  lldb::addr_t vm_addr = 0;
169  /// Walk through segments and Thread and Address Map information.
170  /// PT_NOTE - Contains Thread and Register information
171  /// PT_LOAD - Contains a contiguous range of Process Address Space
172  for (const elf::ELFProgramHeader &H : segments) {
173  DataExtractor data = core->GetSegmentData(H);
174 
175  // Parse thread contexts and auxv structure
176  if (H.p_type == llvm::ELF::PT_NOTE) {
177  if (llvm::Error error = ParseThreadContextsFromNoteSegment(H, data))
178  return Status(std::move(error));
179  }
180  // PT_LOAD segments contains address map
181  if (H.p_type == llvm::ELF::PT_LOAD) {
182  lldb::addr_t last_addr = AddAddressRangeFromLoadSegment(H);
183  if (vm_addr > last_addr)
184  ranges_are_sorted = false;
185  vm_addr = last_addr;
186  }
187  }
188 
189  if (!ranges_are_sorted) {
190  m_core_aranges.Sort();
191  m_core_range_infos.Sort();
192  }
193 
194  // Even if the architecture is set in the target, we need to override it to
195  // match the core file which is always single arch.
196  ArchSpec arch(m_core_module_sp->GetArchitecture());
197 
198  ArchSpec target_arch = GetTarget().GetArchitecture();
199  ArchSpec core_arch(m_core_module_sp->GetArchitecture());
200  target_arch.MergeFrom(core_arch);
201  GetTarget().SetArchitecture(target_arch);
202 
203  SetUnixSignals(UnixSignals::Create(GetArchitecture()));
204 
205  // Ensure we found at least one thread that was stopped on a signal.
206  bool siginfo_signal_found = false;
207  bool prstatus_signal_found = false;
208  // Check we found a signal in a SIGINFO note.
209  for (const auto &thread_data : m_thread_data) {
210  if (thread_data.signo != 0)
211  siginfo_signal_found = true;
212  if (thread_data.prstatus_sig != 0)
213  prstatus_signal_found = true;
214  }
215  if (!siginfo_signal_found) {
216  // If we don't have signal from SIGINFO use the signal from each threads
217  // PRSTATUS note.
218  if (prstatus_signal_found) {
219  for (auto &thread_data : m_thread_data)
220  thread_data.signo = thread_data.prstatus_sig;
221  } else if (m_thread_data.size() > 0) {
222  // If all else fails force the first thread to be SIGSTOP
223  m_thread_data.begin()->signo =
224  GetUnixSignals()->GetSignalNumberFromName("SIGSTOP");
225  }
226  }
227 
228  // Core files are useless without the main executable. See if we can locate
229  // the main executable using data we found in the core file notes.
230  lldb::ModuleSP exe_module_sp = GetTarget().GetExecutableModule();
231  if (!exe_module_sp) {
232  // The first entry in the NT_FILE might be our executable
233  if (!m_nt_file_entries.empty()) {
234  ModuleSpec exe_module_spec;
235  exe_module_spec.GetArchitecture() = arch;
236  exe_module_spec.GetFileSpec().SetFile(
237  m_nt_file_entries[0].path.GetCString(), FileSpec::Style::native);
238  if (exe_module_spec.GetFileSpec()) {
239  exe_module_sp = GetTarget().GetOrCreateModule(exe_module_spec,
240  true /* notify */);
241  if (exe_module_sp)
243  }
244  }
245  }
246  return error;
247 }
248 
250  if (m_dyld_up.get() == NULL)
251  m_dyld_up.reset(DynamicLoader::FindPlugin(
253  return m_dyld_up.get();
254 }
255 
257  ThreadList &new_thread_list) {
258  const uint32_t num_threads = GetNumThreadContexts();
259  if (!m_thread_data_valid)
260  return false;
261 
262  for (lldb::tid_t tid = 0; tid < num_threads; ++tid) {
263  const ThreadData &td = m_thread_data[tid];
264  lldb::ThreadSP thread_sp(new ThreadElfCore(*this, td));
265  new_thread_list.AddThread(thread_sp);
266  }
267  return new_thread_list.GetSize(false) > 0;
268 }
269 
271 
273 
274 // Process Queries
275 
276 bool ProcessElfCore::IsAlive() { return true; }
277 
278 // Process Memory
279 size_t ProcessElfCore::ReadMemory(lldb::addr_t addr, void *buf, size_t size,
280  Status &error) {
281  // Don't allow the caching that lldb_private::Process::ReadMemory does since
282  // in core files we have it all cached our our core file anyway.
283  return DoReadMemory(addr, buf, size, error);
284 }
285 
287  MemoryRegionInfo &region_info) {
288  region_info.Clear();
289  const VMRangeToPermissions::Entry *permission_entry =
290  m_core_range_infos.FindEntryThatContainsOrFollows(load_addr);
291  if (permission_entry) {
292  if (permission_entry->Contains(load_addr)) {
293  region_info.GetRange().SetRangeBase(permission_entry->GetRangeBase());
294  region_info.GetRange().SetRangeEnd(permission_entry->GetRangeEnd());
295  const Flags permissions(permission_entry->data);
296  region_info.SetReadable(permissions.Test(lldb::ePermissionsReadable)
297  ? MemoryRegionInfo::eYes
298  : MemoryRegionInfo::eNo);
299  region_info.SetWritable(permissions.Test(lldb::ePermissionsWritable)
300  ? MemoryRegionInfo::eYes
301  : MemoryRegionInfo::eNo);
302  region_info.SetExecutable(permissions.Test(lldb::ePermissionsExecutable)
303  ? MemoryRegionInfo::eYes
304  : MemoryRegionInfo::eNo);
305  region_info.SetMapped(MemoryRegionInfo::eYes);
306  } else if (load_addr < permission_entry->GetRangeBase()) {
307  region_info.GetRange().SetRangeBase(load_addr);
308  region_info.GetRange().SetRangeEnd(permission_entry->GetRangeBase());
309  region_info.SetReadable(MemoryRegionInfo::eNo);
310  region_info.SetWritable(MemoryRegionInfo::eNo);
311  region_info.SetExecutable(MemoryRegionInfo::eNo);
312  region_info.SetMapped(MemoryRegionInfo::eNo);
313  }
314  return Status();
315  }
316 
317  region_info.GetRange().SetRangeBase(load_addr);
319  region_info.SetReadable(MemoryRegionInfo::eNo);
320  region_info.SetWritable(MemoryRegionInfo::eNo);
321  region_info.SetExecutable(MemoryRegionInfo::eNo);
322  region_info.SetMapped(MemoryRegionInfo::eNo);
323  return Status();
324 }
325 
326 size_t ProcessElfCore::DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
327  Status &error) {
328  ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
329 
330  if (core_objfile == NULL)
331  return 0;
332 
333  // Get the address range
334  const VMRangeToFileOffset::Entry *address_range =
335  m_core_aranges.FindEntryThatContains(addr);
336  if (address_range == NULL || address_range->GetRangeEnd() < addr) {
337  error.SetErrorStringWithFormat("core file does not contain 0x%" PRIx64,
338  addr);
339  return 0;
340  }
341 
342  // Convert the address into core file offset
343  const lldb::addr_t offset = addr - address_range->GetRangeBase();
344  const lldb::addr_t file_start = address_range->data.GetRangeBase();
345  const lldb::addr_t file_end = address_range->data.GetRangeEnd();
346  size_t bytes_to_read = size; // Number of bytes to read from the core file
347  size_t bytes_copied = 0; // Number of bytes actually read from the core file
348  size_t zero_fill_size = 0; // Padding
349  lldb::addr_t bytes_left =
350  0; // Number of bytes available in the core file from the given address
351 
352  // Don't proceed if core file doesn't contain the actual data for this
353  // address range.
354  if (file_start == file_end)
355  return 0;
356 
357  // Figure out how many on-disk bytes remain in this segment starting at the
358  // given offset
359  if (file_end > file_start + offset)
360  bytes_left = file_end - (file_start + offset);
361 
362  // Figure out how many bytes we need to zero-fill if we are reading more
363  // bytes than available in the on-disk segment
364  if (bytes_to_read > bytes_left) {
365  zero_fill_size = bytes_to_read - bytes_left;
366  bytes_to_read = bytes_left;
367  }
368 
369  // If there is data available on the core file read it
370  if (bytes_to_read)
371  bytes_copied =
372  core_objfile->CopyData(offset + file_start, bytes_to_read, buf);
373 
374  assert(zero_fill_size <= size);
375  // Pad remaining bytes
376  if (zero_fill_size)
377  memset(((char *)buf) + bytes_copied, 0, zero_fill_size);
378 
379  return bytes_copied + zero_fill_size;
380 }
381 
384 
385  SetUnixSignals(std::make_shared<UnixSignals>());
386 }
387 
389  static llvm::once_flag g_once_flag;
390 
391  llvm::call_once(g_once_flag, []() {
392  PluginManager::RegisterPlugin(GetPluginNameStatic(),
394  });
395 }
396 
398  ObjectFile *obj_file = GetTarget().GetExecutableModule()->GetObjectFile();
399  Address addr = obj_file->GetImageInfoAddress(&GetTarget());
400 
401  if (addr.IsValid())
402  return addr.GetLoadAddress(&GetTarget());
403  return LLDB_INVALID_ADDRESS;
404 }
405 
406 // Parse a FreeBSD NT_PRSTATUS note - see FreeBSD sys/procfs.h for details.
407 static void ParseFreeBSDPrStatus(ThreadData &thread_data,
408  const DataExtractor &data,
409  const ArchSpec &arch) {
410  lldb::offset_t offset = 0;
411  bool lp64 = (arch.GetMachine() == llvm::Triple::aarch64 ||
412  arch.GetMachine() == llvm::Triple::mips64 ||
413  arch.GetMachine() == llvm::Triple::ppc64 ||
414  arch.GetMachine() == llvm::Triple::x86_64);
415  int pr_version = data.GetU32(&offset);
416 
418  if (log) {
419  if (pr_version > 1)
420  log->Printf("FreeBSD PRSTATUS unexpected version %d", pr_version);
421  }
422 
423  // Skip padding, pr_statussz, pr_gregsetsz, pr_fpregsetsz, pr_osreldate
424  if (lp64)
425  offset += 32;
426  else
427  offset += 16;
428 
429  thread_data.signo = data.GetU32(&offset); // pr_cursig
430  thread_data.tid = data.GetU32(&offset); // pr_pid
431  if (lp64)
432  offset += 4;
433 
434  size_t len = data.GetByteSize() - offset;
435  thread_data.gpregset = DataExtractor(data, offset, len);
436 }
437 
439  uint32_t &cpi_nlwps,
440  uint32_t &cpi_signo,
441  uint32_t &cpi_siglwp,
442  uint32_t &cpi_pid) {
443  lldb::offset_t offset = 0;
444 
445  uint32_t version = data.GetU32(&offset);
446  if (version != 1)
447  return llvm::make_error<llvm::StringError>(
448  "Error parsing NetBSD core(5) notes: Unsupported procinfo version",
449  llvm::inconvertibleErrorCode());
450 
451  uint32_t cpisize = data.GetU32(&offset);
452  if (cpisize != NETBSD::NT_PROCINFO_SIZE)
453  return llvm::make_error<llvm::StringError>(
454  "Error parsing NetBSD core(5) notes: Unsupported procinfo size",
455  llvm::inconvertibleErrorCode());
456 
457  cpi_signo = data.GetU32(&offset); /* killing signal */
458 
464  cpi_pid = data.GetU32(&offset);
474  cpi_nlwps = data.GetU32(&offset); /* number of LWPs */
475 
477  cpi_siglwp = data.GetU32(&offset); /* LWP target of killing signal */
478 
479  return llvm::Error::success();
480 }
481 
482 static void ParseOpenBSDProcInfo(ThreadData &thread_data,
483  const DataExtractor &data) {
484  lldb::offset_t offset = 0;
485 
486  int version = data.GetU32(&offset);
487  if (version != 1)
488  return;
489 
490  offset += 4;
491  thread_data.signo = data.GetU32(&offset);
492 }
493 
494 llvm::Expected<std::vector<CoreNote>>
495 ProcessElfCore::parseSegment(const DataExtractor &segment) {
496  lldb::offset_t offset = 0;
497  std::vector<CoreNote> result;
498 
499  while (offset < segment.GetByteSize()) {
500  ELFNote note = ELFNote();
501  if (!note.Parse(segment, &offset))
502  return llvm::make_error<llvm::StringError>(
503  "Unable to parse note segment", llvm::inconvertibleErrorCode());
504 
505  size_t note_start = offset;
506  size_t note_size = llvm::alignTo(note.n_descsz, 4);
507  DataExtractor note_data(segment, note_start, note_size);
508 
509  result.push_back({note, note_data});
510  offset += note_size;
511  }
512 
513  return std::move(result);
514 }
515 
516 llvm::Error ProcessElfCore::parseFreeBSDNotes(llvm::ArrayRef<CoreNote> notes) {
517  bool have_prstatus = false;
518  bool have_prpsinfo = false;
519  ThreadData thread_data;
520  for (const auto &note : notes) {
521  if (note.info.n_name != "FreeBSD")
522  continue;
523 
524  if ((note.info.n_type == FREEBSD::NT_PRSTATUS && have_prstatus) ||
525  (note.info.n_type == FREEBSD::NT_PRPSINFO && have_prpsinfo)) {
526  assert(thread_data.gpregset.GetByteSize() > 0);
527  // Add the new thread to thread list
528  m_thread_data.push_back(thread_data);
529  thread_data = ThreadData();
530  have_prstatus = false;
531  have_prpsinfo = false;
532  }
533 
534  switch (note.info.n_type) {
536  have_prstatus = true;
537  ParseFreeBSDPrStatus(thread_data, note.data, GetArchitecture());
538  break;
540  have_prpsinfo = true;
541  break;
542  case FREEBSD::NT_THRMISC: {
543  lldb::offset_t offset = 0;
544  thread_data.name = note.data.GetCStr(&offset, 20);
545  break;
546  }
548  // FIXME: FreeBSD sticks an int at the beginning of the note
549  m_auxv = DataExtractor(note.data, 4, note.data.GetByteSize() - 4);
550  break;
551  default:
552  thread_data.notes.push_back(note);
553  break;
554  }
555  }
556  if (!have_prstatus) {
557  return llvm::make_error<llvm::StringError>(
558  "Could not find NT_PRSTATUS note in core file.",
559  llvm::inconvertibleErrorCode());
560  }
561  m_thread_data.push_back(thread_data);
562  return llvm::Error::success();
563 }
564 
565 /// NetBSD specific Thread context from PT_NOTE segment
566 ///
567 /// NetBSD ELF core files use notes to provide information about
568 /// the process's state. The note name is "NetBSD-CORE" for
569 /// information that is global to the process, and "NetBSD-CORE@nn",
570 /// where "nn" is the lwpid of the LWP that the information belongs
571 /// to (such as register state).
572 ///
573 /// NetBSD uses the following note identifiers:
574 ///
575 /// ELF_NOTE_NETBSD_CORE_PROCINFO (value 1)
576 /// Note is a "netbsd_elfcore_procinfo" structure.
577 /// ELF_NOTE_NETBSD_CORE_AUXV (value 2; since NetBSD 8.0)
578 /// Note is an array of AuxInfo structures.
579 ///
580 /// NetBSD also uses ptrace(2) request numbers (the ones that exist in
581 /// machine-dependent space) to identify register info notes. The
582 /// info in such notes is in the same format that ptrace(2) would
583 /// export that information.
584 ///
585 /// For more information see /usr/include/sys/exec_elf.h
586 ///
587 llvm::Error ProcessElfCore::parseNetBSDNotes(llvm::ArrayRef<CoreNote> notes) {
588  ThreadData thread_data;
589  bool had_nt_regs = false;
590 
591  // To be extracted from struct netbsd_elfcore_procinfo
592  // Used to sanity check of the LWPs of the process
593  uint32_t nlwps = 0;
594  uint32_t signo; // killing signal
595  uint32_t siglwp; // LWP target of killing signal
596  uint32_t pr_pid;
597 
598  for (const auto &note : notes) {
599  llvm::StringRef name = note.info.n_name;
600 
601  if (name == "NetBSD-CORE") {
602  if (note.info.n_type == NETBSD::NT_PROCINFO) {
603  llvm::Error error = ParseNetBSDProcInfo(note.data, nlwps, signo,
604  siglwp, pr_pid);
605  if (error)
606  return error;
607  SetID(pr_pid);
608  } else if (note.info.n_type == NETBSD::NT_AUXV) {
609  m_auxv = note.data;
610  }
611  } else if (name.consume_front("NetBSD-CORE@")) {
612  lldb::tid_t tid;
613  if (name.getAsInteger(10, tid))
614  return llvm::make_error<llvm::StringError>(
615  "Error parsing NetBSD core(5) notes: Cannot convert LWP ID "
616  "to integer",
617  llvm::inconvertibleErrorCode());
618 
619  switch (GetArchitecture().GetMachine()) {
620  case llvm::Triple::aarch64: {
621  // Assume order PT_GETREGS, PT_GETFPREGS
622  if (note.info.n_type == NETBSD::AARCH64::NT_REGS) {
623  // If this is the next thread, push the previous one first.
624  if (had_nt_regs) {
625  m_thread_data.push_back(thread_data);
626  thread_data = ThreadData();
627  had_nt_regs = false;
628  }
629 
630  thread_data.gpregset = note.data;
631  thread_data.tid = tid;
632  if (thread_data.gpregset.GetByteSize() == 0)
633  return llvm::make_error<llvm::StringError>(
634  "Could not find general purpose registers note in core file.",
635  llvm::inconvertibleErrorCode());
636  had_nt_regs = true;
637  } else if (note.info.n_type == NETBSD::AARCH64::NT_FPREGS) {
638  if (!had_nt_regs || tid != thread_data.tid)
639  return llvm::make_error<llvm::StringError>(
640  "Error parsing NetBSD core(5) notes: Unexpected order "
641  "of NOTEs PT_GETFPREG before PT_GETREG",
642  llvm::inconvertibleErrorCode());
643  thread_data.notes.push_back(note);
644  }
645  } break;
646  case llvm::Triple::x86_64: {
647  // Assume order PT_GETREGS, PT_GETFPREGS
648  if (note.info.n_type == NETBSD::AMD64::NT_REGS) {
649  // If this is the next thread, push the previous one first.
650  if (had_nt_regs) {
651  m_thread_data.push_back(thread_data);
652  thread_data = ThreadData();
653  had_nt_regs = false;
654  }
655 
656  thread_data.gpregset = note.data;
657  thread_data.tid = tid;
658  if (thread_data.gpregset.GetByteSize() == 0)
659  return llvm::make_error<llvm::StringError>(
660  "Could not find general purpose registers note in core file.",
661  llvm::inconvertibleErrorCode());
662  had_nt_regs = true;
663  } else if (note.info.n_type == NETBSD::AMD64::NT_FPREGS) {
664  if (!had_nt_regs || tid != thread_data.tid)
665  return llvm::make_error<llvm::StringError>(
666  "Error parsing NetBSD core(5) notes: Unexpected order "
667  "of NOTEs PT_GETFPREG before PT_GETREG",
668  llvm::inconvertibleErrorCode());
669  thread_data.notes.push_back(note);
670  }
671  } break;
672  default:
673  break;
674  }
675  }
676  }
677 
678  // Push the last thread.
679  if (had_nt_regs)
680  m_thread_data.push_back(thread_data);
681 
682  if (m_thread_data.empty())
683  return llvm::make_error<llvm::StringError>(
684  "Error parsing NetBSD core(5) notes: No threads information "
685  "specified in notes",
686  llvm::inconvertibleErrorCode());
687 
688  if (m_thread_data.size() != nlwps)
689  return llvm::make_error<llvm::StringError>(
690  "Error parsing NetBSD core(5) notes: Mismatch between the number "
691  "of LWPs in netbsd_elfcore_procinfo and the number of LWPs specified "
692  "by MD notes",
693  llvm::inconvertibleErrorCode());
694 
695  // Signal targeted at the whole process.
696  if (siglwp == 0) {
697  for (auto &data : m_thread_data)
698  data.signo = signo;
699  }
700  // Signal destined for a particular LWP.
701  else {
702  bool passed = false;
703 
704  for (auto &data : m_thread_data) {
705  if (data.tid == siglwp) {
706  data.signo = signo;
707  passed = true;
708  break;
709  }
710  }
711 
712  if (!passed)
713  return llvm::make_error<llvm::StringError>(
714  "Error parsing NetBSD core(5) notes: Signal passed to unknown LWP",
715  llvm::inconvertibleErrorCode());
716  }
717 
718  return llvm::Error::success();
719 }
720 
721 llvm::Error ProcessElfCore::parseOpenBSDNotes(llvm::ArrayRef<CoreNote> notes) {
722  ThreadData thread_data;
723  for (const auto &note : notes) {
724  // OpenBSD per-thread information is stored in notes named "OpenBSD@nnn" so
725  // match on the initial part of the string.
726  if (!llvm::StringRef(note.info.n_name).startswith("OpenBSD"))
727  continue;
728 
729  switch (note.info.n_type) {
731  ParseOpenBSDProcInfo(thread_data, note.data);
732  break;
733  case OPENBSD::NT_AUXV:
734  m_auxv = note.data;
735  break;
736  case OPENBSD::NT_REGS:
737  thread_data.gpregset = note.data;
738  break;
739  default:
740  thread_data.notes.push_back(note);
741  break;
742  }
743  }
744  if (thread_data.gpregset.GetByteSize() == 0) {
745  return llvm::make_error<llvm::StringError>(
746  "Could not find general purpose registers note in core file.",
747  llvm::inconvertibleErrorCode());
748  }
749  m_thread_data.push_back(thread_data);
750  return llvm::Error::success();
751 }
752 
753 /// A description of a linux process usually contains the following NOTE
754 /// entries:
755 /// - NT_PRPSINFO - General process information like pid, uid, name, ...
756 /// - NT_SIGINFO - Information about the signal that terminated the process
757 /// - NT_AUXV - Process auxiliary vector
758 /// - NT_FILE - Files mapped into memory
759 ///
760 /// Additionally, for each thread in the process the core file will contain at
761 /// least the NT_PRSTATUS note, containing the thread id and general purpose
762 /// registers. It may include additional notes for other register sets (floating
763 /// point and vector registers, ...). The tricky part here is that some of these
764 /// notes have "CORE" in their owner fields, while other set it to "LINUX".
765 llvm::Error ProcessElfCore::parseLinuxNotes(llvm::ArrayRef<CoreNote> notes) {
766  const ArchSpec &arch = GetArchitecture();
767  bool have_prstatus = false;
768  bool have_prpsinfo = false;
769  ThreadData thread_data;
770  for (const auto &note : notes) {
771  if (note.info.n_name != "CORE" && note.info.n_name != "LINUX")
772  continue;
773 
774  if ((note.info.n_type == LINUX::NT_PRSTATUS && have_prstatus) ||
775  (note.info.n_type == LINUX::NT_PRPSINFO && have_prpsinfo)) {
776  assert(thread_data.gpregset.GetByteSize() > 0);
777  // Add the new thread to thread list
778  m_thread_data.push_back(thread_data);
779  thread_data = ThreadData();
780  have_prstatus = false;
781  have_prpsinfo = false;
782  }
783 
784  switch (note.info.n_type) {
785  case LINUX::NT_PRSTATUS: {
786  have_prstatus = true;
787  ELFLinuxPrStatus prstatus;
788  Status status = prstatus.Parse(note.data, arch);
789  if (status.Fail())
790  return status.ToError();
791  thread_data.prstatus_sig = prstatus.pr_cursig;
792  thread_data.tid = prstatus.pr_pid;
793  uint32_t header_size = ELFLinuxPrStatus::GetSize(arch);
794  size_t len = note.data.GetByteSize() - header_size;
795  thread_data.gpregset = DataExtractor(note.data, header_size, len);
796  break;
797  }
798  case LINUX::NT_PRPSINFO: {
799  have_prpsinfo = true;
800  ELFLinuxPrPsInfo prpsinfo;
801  Status status = prpsinfo.Parse(note.data, arch);
802  if (status.Fail())
803  return status.ToError();
804  thread_data.name.assign (prpsinfo.pr_fname, strnlen (prpsinfo.pr_fname, sizeof (prpsinfo.pr_fname)));
805  SetID(prpsinfo.pr_pid);
806  break;
807  }
808  case LINUX::NT_SIGINFO: {
809  ELFLinuxSigInfo siginfo;
810  Status status = siginfo.Parse(note.data, arch);
811  if (status.Fail())
812  return status.ToError();
813  thread_data.signo = siginfo.si_signo;
814  break;
815  }
816  case LINUX::NT_FILE: {
817  m_nt_file_entries.clear();
818  lldb::offset_t offset = 0;
819  const uint64_t count = note.data.GetAddress(&offset);
820  note.data.GetAddress(&offset); // Skip page size
821  for (uint64_t i = 0; i < count; ++i) {
822  NT_FILE_Entry entry;
823  entry.start = note.data.GetAddress(&offset);
824  entry.end = note.data.GetAddress(&offset);
825  entry.file_ofs = note.data.GetAddress(&offset);
826  m_nt_file_entries.push_back(entry);
827  }
828  for (uint64_t i = 0; i < count; ++i) {
829  const char *path = note.data.GetCStr(&offset);
830  if (path && path[0])
831  m_nt_file_entries[i].path.SetCString(path);
832  }
833  break;
834  }
835  case LINUX::NT_AUXV:
836  m_auxv = note.data;
837  break;
838  default:
839  thread_data.notes.push_back(note);
840  break;
841  }
842  }
843  // Add last entry in the note section
844  if (have_prstatus)
845  m_thread_data.push_back(thread_data);
846  return llvm::Error::success();
847 }
848 
849 /// Parse Thread context from PT_NOTE segment and store it in the thread list
850 /// A note segment consists of one or more NOTE entries, but their types and
851 /// meaning differ depending on the OS.
852 llvm::Error ProcessElfCore::ParseThreadContextsFromNoteSegment(
853  const elf::ELFProgramHeader &segment_header, DataExtractor segment_data) {
854  assert(segment_header.p_type == llvm::ELF::PT_NOTE);
855 
856  auto notes_or_error = parseSegment(segment_data);
857  if(!notes_or_error)
858  return notes_or_error.takeError();
859  switch (GetArchitecture().GetTriple().getOS()) {
860  case llvm::Triple::FreeBSD:
861  return parseFreeBSDNotes(*notes_or_error);
862  case llvm::Triple::Linux:
863  return parseLinuxNotes(*notes_or_error);
864  case llvm::Triple::NetBSD:
865  return parseNetBSDNotes(*notes_or_error);
866  case llvm::Triple::OpenBSD:
867  return parseOpenBSDNotes(*notes_or_error);
868  default:
869  return llvm::make_error<llvm::StringError>(
870  "Don't know how to parse core file. Unsupported OS.",
871  llvm::inconvertibleErrorCode());
872  }
873 }
874 
875 uint32_t ProcessElfCore::GetNumThreadContexts() {
876  if (!m_thread_data_valid)
877  DoLoadCore();
878  return m_thread_data.size();
879 }
880 
882  ArchSpec arch = m_core_module_sp->GetObjectFile()->GetArchitecture();
883 
884  ArchSpec target_arch = GetTarget().GetArchitecture();
885  arch.MergeFrom(target_arch);
886 
887  // On MIPS there is no way to differentiate betwenn 32bit and 64bit core
888  // files and this information can't be merged in from the target arch so we
889  // fail back to unconditionally returning the target arch in this config.
890  if (target_arch.IsMIPS()) {
891  return target_arch;
892  }
893 
894  return arch;
895 }
896 
897 const lldb::DataBufferSP ProcessElfCore::GetAuxvData() {
898  const uint8_t *start = m_auxv.GetDataStart();
899  size_t len = m_auxv.GetByteSize();
900  lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(start, len));
901  return buffer;
902 }
903 
905  info.Clear();
906  info.SetProcessID(GetID());
908  lldb::ModuleSP module_sp = GetTarget().GetExecutableModule();
909  if (module_sp) {
910  const bool add_exe_file_as_first_arg = false;
911  info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
912  add_exe_file_as_first_arg);
913  }
914  return true;
915 }
static Status GetSharedModule(const ModuleSpec &module_spec, lldb::ModuleSP &module_sp, const FileSpecList *module_search_paths_ptr, lldb::ModuleSP *old_module_sp_ptr, bool *did_create_ptr, bool always_create=false)
Definition: ModuleList.cpp:765
static void Initialize()
An data extractor class.
Definition: DataExtractor.h:47
llvm::Error ToError() const
Definition: Status.cpp:88
virtual lldb_private::Address GetImageInfoAddress(Target *target)
Similar to Process::GetImageInfoAddress().
Definition: ObjectFile.h:458
lldb_private::DynamicLoader * GetDynamicLoader() override
Get the dynamic loader plug-in for this process.
#define LIBLLDB_LOG_PROCESS
Definition: Logging.h:15
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
void SetExecutable(OptionalBool val)
const ArchSpec & GetArchitecture() const
Definition: Target.h:941
static const char * GetPluginDescriptionStatic()
bool Contains(BaseType r) const
Definition: RangeMap.h:77
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
Definition: ProcessInfo.cpp:61
elf_word p_type
Type of program segment.
Definition: ELFHeader.h:193
static size_t GetSize(const lldb_private::ArchSpec &arch)
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
void SetArchitecture(const ArchSpec &arch)
Definition: ProcessInfo.h:66
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:292
static lldb_private::ConstString GetPluginNameStatic()
lldb_private::DataExtractor gpregset
llvm::Error Error
static llvm::Error ParseNetBSDProcInfo(const DataExtractor &data, uint32_t &cpi_nlwps, uint32_t &cpi_signo, uint32_t &cpi_siglwp, uint32_t &cpi_pid)
bool GetProcessInfo(lldb_private::ProcessInstanceInfo &info) override
lldb_private::ArchSpec GetArchitecture()
std::shared_ptr< DataBufferLLVM > CreateDataBuffer(const llvm::Twine &path, uint64_t size=0, uint64_t offset=0)
Create memory buffer from path.
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:49
void SetProcessID(lldb::pid_t pid)
Definition: ProcessInfo.h:70
A file utility class.
Definition: FileSpec.h:55
An architecture specification class.
Definition: ArchSpec.h:32
lldb::addr_t GetImageInfoAddress() override
Get the image information address for the current process.
lldb_private::DataExtractor GetSegmentData(const elf::ELFProgramHeader &H)
lldb_private::Status Parse(const lldb_private::DataExtractor &data, const lldb_private::ArchSpec &arch)
bool UpdateThreadList(lldb_private::ThreadList &old_thread_list, lldb_private::ThreadList &new_thread_list) override
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:58
static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const lldb_private::FileSpec *crash_file_path)
void SetMapped(OptionalBool val)
Generic representation of an ELF file header.
Definition: ELFHeader.h:56
bool SetArchitecture(const ArchSpec &arch_spec, bool set_platform=false)
Set the architecture for this target.
Definition: Target.cpp:1498
size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size, lldb_private::Status &error) override
Read of memory from a process.
void SetRangeEnd(BaseType end)
Definition: RangeMap.h:64
lldb_private::Status Parse(const lldb_private::DataExtractor &data, const lldb_private::ArchSpec &arch)
static FileSystem & Instance()
const lldb::DataBufferSP GetAuxvData() override
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::tid_t tid
size_t CopyData(lldb::offset_t offset, size_t length, void *dst) const
Definition: ObjectFile.cpp:481
llvm::ArrayRef< elf::ELFProgramHeader > ProgramHeaders()
elf_half e_type
Object file type.
Definition: ELFHeader.h:63
lldb_private::Status Parse(const lldb_private::DataExtractor &data, const lldb_private::ArchSpec &arch)
lldb_private::Status DoLoadCore() override
const Entry * FindEntryThatContainsOrFollows(B addr) const
Definition: RangeMap.h:788
const lldb::UnixSignalsSP & GetUnixSignals()
Definition: Process.cpp:3361
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
#define NT_PRPSINFO
bool Test(ValueType bit) const
Test a single flag bit.
Definition: Flags.h:107
uint64_t offset_t
Definition: lldb-types.h:87
Entry * FindEntryThatContains(B addr)
Definition: RangeMap.h:737
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:57
virtual void Finalize()
This object is about to be destroyed, do any necessary cleanup.
Definition: Process.cpp:615
Generic ELF object file reader.
Definition: ObjectFileELF.h:57
lldb::ModuleSP GetOrCreateModule(const ModuleSpec &module_spec, bool notify, Status *error_ptr=nullptr)
Find a binary on the system and return its Module, or return an existing Module that is already in th...
Definition: Target.cpp:1999
uint64_t tid_t
Definition: lldb-types.h:86
elf::elf_word n_descsz
Definition: ObjectFileELF.h:26
#define NT_SIGINFO
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:241
A plug-in interface definition class for debugging a process.
Definition: Process.h:353
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
Definition: Target.cpp:1380
~ProcessElfCore() override
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:343
A section + offset based address class.
Definition: Address.h:80
#define NT_PRSTATUS
FileSpec & GetFileSpec()
Definition: ModuleSpec.h:75
void SetUnixSignals(lldb::UnixSignalsSP &&signals_sp)
Definition: Process.cpp:3356
bool IsMIPS() const
if MIPS architecture return true.
Definition: ArchSpec.cpp:598
#define NT_AUXV
A plug-in interface definition class for dynamic loaders.
Definition: DynamicLoader.h:64
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
ArchSpec & GetArchitecture()
Definition: ModuleSpec.h:111
static void Terminate()
lldb_private::Status DoDestroy() override
A class to manage flags.
Definition: Flags.h:22
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1194
uint32_t GetPluginVersion() override
size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size, lldb_private::Status &error) override
Actually do the reading of memory from a process.
elf_xword p_filesz
Byte size of the segment in file.
Definition: ELFHeader.h:198
uint64_t addr_t
Definition: lldb-types.h:83
elf_xword p_memsz
Byte size of the segment in memory.
Definition: ELFHeader.h:199
void SetReadable(OptionalBool val)
static bool UnregisterPlugin(ABICreateInstance create_callback)
const uint8_t * GetDataStart() const
Get the data start pointer.
A uniqued constant string class.
Definition: ConstString.h:38
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:247
lldb_private::Status GetMemoryRegionInfo(lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &region_info) override
Locate the memory region that contains load_addr.
void RefreshStateAfterStop() override
Currently called as part of ShouldStop.
void MergeFrom(const ArchSpec &other)
Merges fields from another ArchSpec into this ArchSpec.
Definition: ArchSpec.cpp:859
void SetID(lldb::user_id_t uid)
Set accessor for the user ID.
Definition: UserID.h:55
bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
Parse an ELFNote entry from the given DataExtractor starting at position offset.
elf_off p_offset
Start of segment from beginning of file.
Definition: ELFHeader.h:195
ProcessElfCore(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const lldb_private::FileSpec &core_file)
lldb_private::ConstString GetPluginName() override
elf_word p_flags
Segment attributes.
Definition: ELFHeader.h:194
ThreadList m_thread_list
The threads for this process as the user will see them.
Definition: Process.h:2657
static void ParseFreeBSDPrStatus(ThreadData &thread_data, const DataExtractor &data, const ArchSpec &arch)
void SetRangeBase(BaseType b)
Definition: RangeMap.h:48
void SetWritable(OptionalBool val)
std::string name
BaseType GetRangeEnd() const
Definition: RangeMap.h:62
void AddThread(const lldb::ThreadSP &thread_sp)
uint32_t GetSize(bool can_update=true)
Definition: ThreadList.cpp:82
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:255
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:198
Generic representation of an ELF program header.
Definition: ELFHeader.h:192
elf_addr p_vaddr
Virtual address of segment in memory.
Definition: ELFHeader.h:196
static bool MagicBytesMatch(const uint8_t *magic)
Examines at most EI_NIDENT bytes starting from the given pointer and determines if the magic ELF iden...
Definition: ELFHeader.cpp:157
SizeType GetByteSize() const
Definition: RangeMap.h:71
static lldb_private::ConstString GetPluginNameStatic()
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:376
#define NT_FILE
void SetExecutableModule(lldb::ModuleSP &module_sp, LoadDependentFiles load_dependent_files=eLoadDependentsDefault)
Set the main executable module.
Definition: Target.cpp:1431
void Printf(const char *format,...) __attribute__((format(printf
Definition: Log.cpp:113
static void ParseOpenBSDProcInfo(ThreadData &thread_data, const DataExtractor &data)
void SetCanJIT(bool can_jit)
Sets whether executing JIT-compiled code in this process is possible.
Definition: Process.cpp:2406
bool CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) override
Check if a plug-in instance can debug the file in module.
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition: ArchSpec.cpp:726
lldb::DynamicLoaderUP m_dyld_up
Definition: Process.h:2677
std::vector< lldb_private::CoreNote > notes
bool IsAlive() override
Check if a process is still alive.
bool Parse(lldb_private::DataExtractor &data, lldb::offset_t *offset)
Parse an ELFHeader entry starting at position offset and update the data extractor with the address s...
Definition: ELFHeader.cpp:114
An error handling class.
Definition: Status.h:44
BaseType GetRangeBase() const
Definition: RangeMap.h:46
void Append(const Entry &entry)
Definition: RangeMap.h:635