1 //===-- ProcessElfCore.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/ModuleSpec.h"
16 #include "lldb/Core/PluginManager.h"
17 #include "lldb/Core/Section.h"
18 #include "lldb/Target/DynamicLoader.h"
19 #include "lldb/Target/MemoryRegionInfo.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Target/UnixSignals.h"
22 #include "lldb/Utility/DataBufferHeap.h"
23 #include "lldb/Utility/Log.h"
24 #include "lldb/Utility/State.h"
26 #include "llvm/BinaryFormat/ELF.h"
27 #include "llvm/Support/Threading.h"
29 #include "Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h"
30 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
31 #include "Plugins/Process/elf-core/RegisterUtilities.h"
32 #include "ProcessElfCore.h"
33 #include "ThreadElfCore.h"
35 using namespace lldb_private;
37 ConstString ProcessElfCore::GetPluginNameStatic() {
38 static ConstString g_name("elf-core");
42 const char *ProcessElfCore::GetPluginDescriptionStatic() {
43 return "ELF core dump plug-in.";
46 void ProcessElfCore::Terminate() {
47 PluginManager::UnregisterPlugin(ProcessElfCore::CreateInstance);
50 lldb::ProcessSP ProcessElfCore::CreateInstance(lldb::TargetSP target_sp,
51 lldb::ListenerSP listener_sp,
52 const FileSpec *crash_file) {
53 lldb::ProcessSP process_sp;
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);
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)
70 new ProcessElfCore(target_sp, listener_sp, *crash_file));
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,
84 if (m_core_module_sp) {
85 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
86 if (core_objfile && core_objfile->GetType() == ObjectFile::eTypeCoreFile)
93 //----------------------------------------------------------------------
94 // ProcessElfCore constructor
95 //----------------------------------------------------------------------
96 ProcessElfCore::ProcessElfCore(lldb::TargetSP target_sp,
97 lldb::ListenerSP listener_sp,
98 const FileSpec &core_file)
99 : Process(target_sp, listener_sp), m_core_file(core_file) {}
101 //----------------------------------------------------------------------
103 //----------------------------------------------------------------------
104 ProcessElfCore::~ProcessElfCore() {
106 // We need to call finalize on the process before destroying ourselves to
107 // make sure all of the broadcaster cleanup goes as planned. If we destruct
108 // this class, then Process::~Process() might have problems trying to fully
109 // destroy the broadcaster.
113 //----------------------------------------------------------------------
115 //----------------------------------------------------------------------
116 ConstString ProcessElfCore::GetPluginName() { return GetPluginNameStatic(); }
118 uint32_t ProcessElfCore::GetPluginVersion() { return 1; }
120 lldb::addr_t ProcessElfCore::AddAddressRangeFromLoadSegment(
121 const elf::ELFProgramHeader &header) {
122 const lldb::addr_t addr = header.p_vaddr;
123 FileRange file_range(header.p_offset, header.p_filesz);
124 VMRangeToFileOffset::Entry range_entry(addr, header.p_memsz, file_range);
126 VMRangeToFileOffset::Entry *last_entry = m_core_aranges.Back();
127 if (last_entry && last_entry->GetRangeEnd() == range_entry.GetRangeBase() &&
128 last_entry->data.GetRangeEnd() == range_entry.data.GetRangeBase() &&
129 last_entry->GetByteSize() == last_entry->data.GetByteSize()) {
130 last_entry->SetRangeEnd(range_entry.GetRangeEnd());
131 last_entry->data.SetRangeEnd(range_entry.data.GetRangeEnd());
133 m_core_aranges.Append(range_entry);
136 // Keep a separate map of permissions that that isn't coalesced so all ranges
138 const uint32_t permissions =
139 ((header.p_flags & llvm::ELF::PF_R) ? lldb::ePermissionsReadable : 0u) |
140 ((header.p_flags & llvm::ELF::PF_W) ? lldb::ePermissionsWritable : 0u) |
141 ((header.p_flags & llvm::ELF::PF_X) ? lldb::ePermissionsExecutable : 0u);
143 m_core_range_infos.Append(
144 VMRangeToPermissions::Entry(addr, header.p_memsz, permissions));
149 //----------------------------------------------------------------------
151 //----------------------------------------------------------------------
152 Status ProcessElfCore::DoLoadCore() {
154 if (!m_core_module_sp) {
155 error.SetErrorString("invalid core module");
159 ObjectFileELF *core = (ObjectFileELF *)(m_core_module_sp->GetObjectFile());
161 error.SetErrorString("invalid core object file");
165 llvm::ArrayRef<elf::ELFProgramHeader> segments = core->ProgramHeaders();
166 if (segments.size() == 0) {
167 error.SetErrorString("core file has no segments");
173 m_thread_data_valid = true;
175 bool ranges_are_sorted = true;
176 lldb::addr_t vm_addr = 0;
177 /// Walk through segments and Thread and Address Map information.
178 /// PT_NOTE - Contains Thread and Register information
179 /// PT_LOAD - Contains a contiguous range of Process Address Space
180 for (const elf::ELFProgramHeader &H : segments) {
181 DataExtractor data = core->GetSegmentData(H);
183 // Parse thread contexts and auxv structure
184 if (H.p_type == llvm::ELF::PT_NOTE) {
185 if (llvm::Error error = ParseThreadContextsFromNoteSegment(H, data))
186 return Status(std::move(error));
188 // PT_LOAD segments contains address map
189 if (H.p_type == llvm::ELF::PT_LOAD) {
190 lldb::addr_t last_addr = AddAddressRangeFromLoadSegment(H);
191 if (vm_addr > last_addr)
192 ranges_are_sorted = false;
197 if (!ranges_are_sorted) {
198 m_core_aranges.Sort();
199 m_core_range_infos.Sort();
202 // Even if the architecture is set in the target, we need to override it to
203 // match the core file which is always single arch.
204 ArchSpec arch(m_core_module_sp->GetArchitecture());
206 ArchSpec target_arch = GetTarget().GetArchitecture();
207 ArchSpec core_arch(m_core_module_sp->GetArchitecture());
208 target_arch.MergeFrom(core_arch);
209 GetTarget().SetArchitecture(target_arch);
211 SetUnixSignals(UnixSignals::Create(GetArchitecture()));
213 // Ensure we found at least one thread that was stopped on a signal.
214 bool siginfo_signal_found = false;
215 bool prstatus_signal_found = false;
216 // Check we found a signal in a SIGINFO note.
217 for (const auto &thread_data : m_thread_data) {
218 if (thread_data.signo != 0)
219 siginfo_signal_found = true;
220 if (thread_data.prstatus_sig != 0)
221 prstatus_signal_found = true;
223 if (!siginfo_signal_found) {
224 // If we don't have signal from SIGINFO use the signal from each threads
226 if (prstatus_signal_found) {
227 for (auto &thread_data : m_thread_data)
228 thread_data.signo = thread_data.prstatus_sig;
229 } else if (m_thread_data.size() > 0) {
230 // If all else fails force the first thread to be SIGSTOP
231 m_thread_data.begin()->signo =
232 GetUnixSignals()->GetSignalNumberFromName("SIGSTOP");
236 // Core files are useless without the main executable. See if we can locate
237 // the main executable using data we found in the core file notes.
238 lldb::ModuleSP exe_module_sp = GetTarget().GetExecutableModule();
239 if (!exe_module_sp) {
240 // The first entry in the NT_FILE might be our executable
241 if (!m_nt_file_entries.empty()) {
242 ModuleSpec exe_module_spec;
243 exe_module_spec.GetArchitecture() = arch;
244 exe_module_spec.GetFileSpec().SetFile(
245 m_nt_file_entries[0].path.GetCString(), FileSpec::Style::native);
246 if (exe_module_spec.GetFileSpec()) {
247 exe_module_sp = GetTarget().GetSharedModule(exe_module_spec);
249 GetTarget().SetExecutableModule(exe_module_sp, eLoadDependentsNo);
256 lldb_private::DynamicLoader *ProcessElfCore::GetDynamicLoader() {
257 if (m_dyld_ap.get() == NULL)
258 m_dyld_ap.reset(DynamicLoader::FindPlugin(
259 this, DynamicLoaderPOSIXDYLD::GetPluginNameStatic().GetCString()));
260 return m_dyld_ap.get();
263 bool ProcessElfCore::UpdateThreadList(ThreadList &old_thread_list,
264 ThreadList &new_thread_list) {
265 const uint32_t num_threads = GetNumThreadContexts();
266 if (!m_thread_data_valid)
269 for (lldb::tid_t tid = 0; tid < num_threads; ++tid) {
270 const ThreadData &td = m_thread_data[tid];
271 lldb::ThreadSP thread_sp(new ThreadElfCore(*this, td));
272 new_thread_list.AddThread(thread_sp);
274 return new_thread_list.GetSize(false) > 0;
277 void ProcessElfCore::RefreshStateAfterStop() {}
279 Status ProcessElfCore::DoDestroy() { return Status(); }
281 //------------------------------------------------------------------
283 //------------------------------------------------------------------
285 bool ProcessElfCore::IsAlive() { return true; }
287 //------------------------------------------------------------------
289 //------------------------------------------------------------------
290 size_t ProcessElfCore::ReadMemory(lldb::addr_t addr, void *buf, size_t size,
292 // Don't allow the caching that lldb_private::Process::ReadMemory does since
293 // in core files we have it all cached our our core file anyway.
294 return DoReadMemory(addr, buf, size, error);
297 Status ProcessElfCore::GetMemoryRegionInfo(lldb::addr_t load_addr,
298 MemoryRegionInfo ®ion_info) {
300 const VMRangeToPermissions::Entry *permission_entry =
301 m_core_range_infos.FindEntryThatContainsOrFollows(load_addr);
302 if (permission_entry) {
303 if (permission_entry->Contains(load_addr)) {
304 region_info.GetRange().SetRangeBase(permission_entry->GetRangeBase());
305 region_info.GetRange().SetRangeEnd(permission_entry->GetRangeEnd());
306 const Flags permissions(permission_entry->data);
307 region_info.SetReadable(permissions.Test(lldb::ePermissionsReadable)
308 ? MemoryRegionInfo::eYes
309 : MemoryRegionInfo::eNo);
310 region_info.SetWritable(permissions.Test(lldb::ePermissionsWritable)
311 ? MemoryRegionInfo::eYes
312 : MemoryRegionInfo::eNo);
313 region_info.SetExecutable(permissions.Test(lldb::ePermissionsExecutable)
314 ? MemoryRegionInfo::eYes
315 : MemoryRegionInfo::eNo);
316 region_info.SetMapped(MemoryRegionInfo::eYes);
317 } else if (load_addr < permission_entry->GetRangeBase()) {
318 region_info.GetRange().SetRangeBase(load_addr);
319 region_info.GetRange().SetRangeEnd(permission_entry->GetRangeBase());
320 region_info.SetReadable(MemoryRegionInfo::eNo);
321 region_info.SetWritable(MemoryRegionInfo::eNo);
322 region_info.SetExecutable(MemoryRegionInfo::eNo);
323 region_info.SetMapped(MemoryRegionInfo::eNo);
328 region_info.GetRange().SetRangeBase(load_addr);
329 region_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS);
330 region_info.SetReadable(MemoryRegionInfo::eNo);
331 region_info.SetWritable(MemoryRegionInfo::eNo);
332 region_info.SetExecutable(MemoryRegionInfo::eNo);
333 region_info.SetMapped(MemoryRegionInfo::eNo);
337 size_t ProcessElfCore::DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
339 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
341 if (core_objfile == NULL)
344 // Get the address range
345 const VMRangeToFileOffset::Entry *address_range =
346 m_core_aranges.FindEntryThatContains(addr);
347 if (address_range == NULL || address_range->GetRangeEnd() < addr) {
348 error.SetErrorStringWithFormat("core file does not contain 0x%" PRIx64,
353 // Convert the address into core file offset
354 const lldb::addr_t offset = addr - address_range->GetRangeBase();
355 const lldb::addr_t file_start = address_range->data.GetRangeBase();
356 const lldb::addr_t file_end = address_range->data.GetRangeEnd();
357 size_t bytes_to_read = size; // Number of bytes to read from the core file
358 size_t bytes_copied = 0; // Number of bytes actually read from the core file
359 size_t zero_fill_size = 0; // Padding
360 lldb::addr_t bytes_left =
361 0; // Number of bytes available in the core file from the given address
363 // Don't proceed if core file doesn't contain the actual data for this
365 if (file_start == file_end)
368 // Figure out how many on-disk bytes remain in this segment starting at the
370 if (file_end > file_start + offset)
371 bytes_left = file_end - (file_start + offset);
373 // Figure out how many bytes we need to zero-fill if we are reading more
374 // bytes than available in the on-disk segment
375 if (bytes_to_read > bytes_left) {
376 zero_fill_size = bytes_to_read - bytes_left;
377 bytes_to_read = bytes_left;
380 // If there is data available on the core file read it
383 core_objfile->CopyData(offset + file_start, bytes_to_read, buf);
385 assert(zero_fill_size <= size);
386 // Pad remaining bytes
388 memset(((char *)buf) + bytes_copied, 0, zero_fill_size);
390 return bytes_copied + zero_fill_size;
393 void ProcessElfCore::Clear() {
394 m_thread_list.Clear();
396 SetUnixSignals(std::make_shared<UnixSignals>());
399 void ProcessElfCore::Initialize() {
400 static llvm::once_flag g_once_flag;
402 llvm::call_once(g_once_flag, []() {
403 PluginManager::RegisterPlugin(GetPluginNameStatic(),
404 GetPluginDescriptionStatic(), CreateInstance);
408 lldb::addr_t ProcessElfCore::GetImageInfoAddress() {
409 ObjectFile *obj_file = GetTarget().GetExecutableModule()->GetObjectFile();
410 Address addr = obj_file->GetImageInfoAddress(&GetTarget());
413 return addr.GetLoadAddress(&GetTarget());
414 return LLDB_INVALID_ADDRESS;
417 // Parse a FreeBSD NT_PRSTATUS note - see FreeBSD sys/procfs.h for details.
418 static void ParseFreeBSDPrStatus(ThreadData &thread_data,
419 const DataExtractor &data,
420 const ArchSpec &arch) {
421 lldb::offset_t offset = 0;
422 bool lp64 = (arch.GetMachine() == llvm::Triple::aarch64 ||
423 arch.GetMachine() == llvm::Triple::mips64 ||
424 arch.GetMachine() == llvm::Triple::ppc64 ||
425 arch.GetMachine() == llvm::Triple::x86_64);
426 int pr_version = data.GetU32(&offset);
428 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
431 log->Printf("FreeBSD PRSTATUS unexpected version %d", pr_version);
434 // Skip padding, pr_statussz, pr_gregsetsz, pr_fpregsetsz, pr_osreldate
440 thread_data.signo = data.GetU32(&offset); // pr_cursig
441 thread_data.tid = data.GetU32(&offset); // pr_pid
445 size_t len = data.GetByteSize() - offset;
446 thread_data.gpregset = DataExtractor(data, offset, len);
449 static void ParseNetBSDProcInfo(ThreadData &thread_data,
450 const DataExtractor &data) {
451 lldb::offset_t offset = 0;
453 int version = data.GetU32(&offset);
458 thread_data.signo = data.GetU32(&offset);
461 static void ParseOpenBSDProcInfo(ThreadData &thread_data,
462 const DataExtractor &data) {
463 lldb::offset_t offset = 0;
465 int version = data.GetU32(&offset);
470 thread_data.signo = data.GetU32(&offset);
473 llvm::Expected<std::vector<CoreNote>>
474 ProcessElfCore::parseSegment(const DataExtractor &segment) {
475 lldb::offset_t offset = 0;
476 std::vector<CoreNote> result;
478 while (offset < segment.GetByteSize()) {
479 ELFNote note = ELFNote();
480 if (!note.Parse(segment, &offset))
481 return llvm::make_error<llvm::StringError>(
482 "Unable to parse note segment", llvm::inconvertibleErrorCode());
484 size_t note_start = offset;
485 size_t note_size = llvm::alignTo(note.n_descsz, 4);
486 DataExtractor note_data(segment, note_start, note_size);
488 result.push_back({note, note_data});
492 return std::move(result);
495 llvm::Error ProcessElfCore::parseFreeBSDNotes(llvm::ArrayRef<CoreNote> notes) {
496 bool have_prstatus = false;
497 bool have_prpsinfo = false;
498 ThreadData thread_data;
499 for (const auto ¬e : notes) {
500 if (note.info.n_name != "FreeBSD")
503 if ((note.info.n_type == FREEBSD::NT_PRSTATUS && have_prstatus) ||
504 (note.info.n_type == FREEBSD::NT_PRPSINFO && have_prpsinfo)) {
505 assert(thread_data.gpregset.GetByteSize() > 0);
506 // Add the new thread to thread list
507 m_thread_data.push_back(thread_data);
508 thread_data = ThreadData();
509 have_prstatus = false;
510 have_prpsinfo = false;
513 switch (note.info.n_type) {
514 case FREEBSD::NT_PRSTATUS:
515 have_prstatus = true;
516 ParseFreeBSDPrStatus(thread_data, note.data, GetArchitecture());
518 case FREEBSD::NT_PRPSINFO:
519 have_prpsinfo = true;
521 case FREEBSD::NT_THRMISC: {
522 lldb::offset_t offset = 0;
523 thread_data.name = note.data.GetCStr(&offset, 20);
526 case FREEBSD::NT_PROCSTAT_AUXV:
527 // FIXME: FreeBSD sticks an int at the beginning of the note
528 m_auxv = DataExtractor(note.data, 4, note.data.GetByteSize() - 4);
531 thread_data.notes.push_back(note);
535 if (!have_prstatus) {
536 return llvm::make_error<llvm::StringError>(
537 "Could not find NT_PRSTATUS note in core file.",
538 llvm::inconvertibleErrorCode());
540 m_thread_data.push_back(thread_data);
541 return llvm::Error::success();
544 llvm::Error ProcessElfCore::parseNetBSDNotes(llvm::ArrayRef<CoreNote> notes) {
545 ThreadData thread_data;
546 for (const auto ¬e : notes) {
547 // NetBSD per-thread information is stored in notes named "NetBSD-CORE@nnn"
548 // so match on the initial part of the string.
549 if (!llvm::StringRef(note.info.n_name).startswith("NetBSD-CORE"))
552 switch (note.info.n_type) {
553 case NETBSD::NT_PROCINFO:
554 ParseNetBSDProcInfo(thread_data, note.data);
556 case NETBSD::NT_AUXV:
560 case NETBSD::NT_AMD64_REGS:
561 if (GetArchitecture().GetMachine() == llvm::Triple::x86_64)
562 thread_data.gpregset = note.data;
565 thread_data.notes.push_back(note);
569 if (thread_data.gpregset.GetByteSize() == 0) {
570 return llvm::make_error<llvm::StringError>(
571 "Could not find general purpose registers note in core file.",
572 llvm::inconvertibleErrorCode());
574 m_thread_data.push_back(thread_data);
575 return llvm::Error::success();
578 llvm::Error ProcessElfCore::parseOpenBSDNotes(llvm::ArrayRef<CoreNote> notes) {
579 ThreadData thread_data;
580 for (const auto ¬e : notes) {
581 // OpenBSD per-thread information is stored in notes named "OpenBSD@nnn" so
582 // match on the initial part of the string.
583 if (!llvm::StringRef(note.info.n_name).startswith("OpenBSD"))
586 switch (note.info.n_type) {
587 case OPENBSD::NT_PROCINFO:
588 ParseOpenBSDProcInfo(thread_data, note.data);
590 case OPENBSD::NT_AUXV:
593 case OPENBSD::NT_REGS:
594 thread_data.gpregset = note.data;
597 thread_data.notes.push_back(note);
601 if (thread_data.gpregset.GetByteSize() == 0) {
602 return llvm::make_error<llvm::StringError>(
603 "Could not find general purpose registers note in core file.",
604 llvm::inconvertibleErrorCode());
606 m_thread_data.push_back(thread_data);
607 return llvm::Error::success();
610 /// A description of a linux process usually contains the following NOTE
612 /// - NT_PRPSINFO - General process information like pid, uid, name, ...
613 /// - NT_SIGINFO - Information about the signal that terminated the process
614 /// - NT_AUXV - Process auxiliary vector
615 /// - NT_FILE - Files mapped into memory
617 /// Additionally, for each thread in the process the core file will contain at
618 /// least the NT_PRSTATUS note, containing the thread id and general purpose
619 /// registers. It may include additional notes for other register sets (floating
620 /// point and vector registers, ...). The tricky part here is that some of these
621 /// notes have "CORE" in their owner fields, while other set it to "LINUX".
622 llvm::Error ProcessElfCore::parseLinuxNotes(llvm::ArrayRef<CoreNote> notes) {
623 const ArchSpec &arch = GetArchitecture();
624 bool have_prstatus = false;
625 bool have_prpsinfo = false;
626 ThreadData thread_data;
627 for (const auto ¬e : notes) {
628 if (note.info.n_name != "CORE" && note.info.n_name != "LINUX")
631 if ((note.info.n_type == LINUX::NT_PRSTATUS && have_prstatus) ||
632 (note.info.n_type == LINUX::NT_PRPSINFO && have_prpsinfo)) {
633 assert(thread_data.gpregset.GetByteSize() > 0);
634 // Add the new thread to thread list
635 m_thread_data.push_back(thread_data);
636 thread_data = ThreadData();
637 have_prstatus = false;
638 have_prpsinfo = false;
641 switch (note.info.n_type) {
642 case LINUX::NT_PRSTATUS: {
643 have_prstatus = true;
644 ELFLinuxPrStatus prstatus;
645 Status status = prstatus.Parse(note.data, arch);
647 return status.ToError();
648 thread_data.prstatus_sig = prstatus.pr_cursig;
649 thread_data.tid = prstatus.pr_pid;
650 uint32_t header_size = ELFLinuxPrStatus::GetSize(arch);
651 size_t len = note.data.GetByteSize() - header_size;
652 thread_data.gpregset = DataExtractor(note.data, header_size, len);
655 case LINUX::NT_PRPSINFO: {
656 have_prpsinfo = true;
657 ELFLinuxPrPsInfo prpsinfo;
658 Status status = prpsinfo.Parse(note.data, arch);
660 return status.ToError();
661 thread_data.name.assign (prpsinfo.pr_fname, strnlen (prpsinfo.pr_fname, sizeof (prpsinfo.pr_fname)));
662 SetID(prpsinfo.pr_pid);
665 case LINUX::NT_SIGINFO: {
666 ELFLinuxSigInfo siginfo;
667 Status status = siginfo.Parse(note.data, arch);
669 return status.ToError();
670 thread_data.signo = siginfo.si_signo;
673 case LINUX::NT_FILE: {
674 m_nt_file_entries.clear();
675 lldb::offset_t offset = 0;
676 const uint64_t count = note.data.GetAddress(&offset);
677 note.data.GetAddress(&offset); // Skip page size
678 for (uint64_t i = 0; i < count; ++i) {
680 entry.start = note.data.GetAddress(&offset);
681 entry.end = note.data.GetAddress(&offset);
682 entry.file_ofs = note.data.GetAddress(&offset);
683 m_nt_file_entries.push_back(entry);
685 for (uint64_t i = 0; i < count; ++i) {
686 const char *path = note.data.GetCStr(&offset);
688 m_nt_file_entries[i].path.SetCString(path);
696 thread_data.notes.push_back(note);
700 // Add last entry in the note section
702 m_thread_data.push_back(thread_data);
703 return llvm::Error::success();
706 /// Parse Thread context from PT_NOTE segment and store it in the thread list
707 /// A note segment consists of one or more NOTE entries, but their types and
708 /// meaning differ depending on the OS.
709 llvm::Error ProcessElfCore::ParseThreadContextsFromNoteSegment(
710 const elf::ELFProgramHeader &segment_header, DataExtractor segment_data) {
711 assert(segment_header.p_type == llvm::ELF::PT_NOTE);
713 auto notes_or_error = parseSegment(segment_data);
715 return notes_or_error.takeError();
716 switch (GetArchitecture().GetTriple().getOS()) {
717 case llvm::Triple::FreeBSD:
718 return parseFreeBSDNotes(*notes_or_error);
719 case llvm::Triple::Linux:
720 return parseLinuxNotes(*notes_or_error);
721 case llvm::Triple::NetBSD:
722 return parseNetBSDNotes(*notes_or_error);
723 case llvm::Triple::OpenBSD:
724 return parseOpenBSDNotes(*notes_or_error);
726 return llvm::make_error<llvm::StringError>(
727 "Don't know how to parse core file. Unsupported OS.",
728 llvm::inconvertibleErrorCode());
732 uint32_t ProcessElfCore::GetNumThreadContexts() {
733 if (!m_thread_data_valid)
735 return m_thread_data.size();
738 ArchSpec ProcessElfCore::GetArchitecture() {
739 ArchSpec arch = m_core_module_sp->GetObjectFile()->GetArchitecture();
741 ArchSpec target_arch = GetTarget().GetArchitecture();
742 arch.MergeFrom(target_arch);
744 // On MIPS there is no way to differentiate betwenn 32bit and 64bit core
745 // files and this information can't be merged in from the target arch so we
746 // fail back to unconditionally returning the target arch in this config.
747 if (target_arch.IsMIPS()) {
754 const lldb::DataBufferSP ProcessElfCore::GetAuxvData() {
755 const uint8_t *start = m_auxv.GetDataStart();
756 size_t len = m_auxv.GetByteSize();
757 lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(start, len));
761 bool ProcessElfCore::GetProcessInfo(ProcessInstanceInfo &info) {
763 info.SetProcessID(GetID());
764 info.SetArchitecture(GetArchitecture());
765 lldb::ModuleSP module_sp = GetTarget().GetExecutableModule();
767 const bool add_exe_file_as_first_arg = false;
768 info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
769 add_exe_file_as_first_arg);