1 //===-- ProcessElfCore.cpp --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // C Includes
11 #include <stdlib.h>
12 
13 // C++ Includes
14 #include <mutex>
15 
16 // Other libraries and framework includes
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/ModuleSpec.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Core/Section.h"
21 #include "lldb/Core/State.h"
22 #include "lldb/Target/DynamicLoader.h"
23 #include "lldb/Target/MemoryRegionInfo.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Target/UnixSignals.h"
26 #include "lldb/Utility/DataBufferHeap.h"
27 #include "lldb/Utility/DataBufferLLVM.h"
28 #include "lldb/Utility/Log.h"
29 
30 #include "llvm/Support/ELF.h"
31 #include "llvm/Support/Threading.h"
32 
33 #include "Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h"
34 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
35 
36 // Project includes
37 #include "ProcessElfCore.h"
38 #include "ThreadElfCore.h"
39 
40 using namespace lldb_private;
41 
42 ConstString ProcessElfCore::GetPluginNameStatic() {
43   static ConstString g_name("elf-core");
44   return g_name;
45 }
46 
47 const char *ProcessElfCore::GetPluginDescriptionStatic() {
48   return "ELF core dump plug-in.";
49 }
50 
51 void ProcessElfCore::Terminate() {
52   PluginManager::UnregisterPlugin(ProcessElfCore::CreateInstance);
53 }
54 
55 lldb::ProcessSP ProcessElfCore::CreateInstance(lldb::TargetSP target_sp,
56                                                lldb::ListenerSP listener_sp,
57                                                const FileSpec *crash_file) {
58   lldb::ProcessSP process_sp;
59   if (crash_file) {
60     // Read enough data for a ELF32 header or ELF64 header
61     // Note: Here we care about e_type field only, so it is safe
62     // to ignore possible presence of the header extension.
63     const size_t header_size = sizeof(llvm::ELF::Elf64_Ehdr);
64 
65     auto data_sp =
66         DataBufferLLVM::CreateSliceFromPath(crash_file->GetPath(), header_size, 0);
67     if (data_sp && data_sp->GetByteSize() == header_size &&
68         elf::ELFHeader::MagicBytesMatch(data_sp->GetBytes())) {
69       elf::ELFHeader elf_header;
70       DataExtractor data(data_sp, lldb::eByteOrderLittle, 4);
71       lldb::offset_t data_offset = 0;
72       if (elf_header.Parse(data, &data_offset)) {
73         if (elf_header.e_type == llvm::ELF::ET_CORE)
74           process_sp.reset(
75               new ProcessElfCore(target_sp, listener_sp, *crash_file));
76       }
77     }
78   }
79   return process_sp;
80 }
81 
82 bool ProcessElfCore::CanDebug(lldb::TargetSP target_sp,
83                               bool plugin_specified_by_name) {
84   // For now we are just making sure the file exists for a given module
85   if (!m_core_module_sp && m_core_file.Exists()) {
86     ModuleSpec core_module_spec(m_core_file, target_sp->GetArchitecture());
87     Error error(ModuleList::GetSharedModule(core_module_spec, m_core_module_sp,
88                                             NULL, NULL, NULL));
89     if (m_core_module_sp) {
90       ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
91       if (core_objfile && core_objfile->GetType() == ObjectFile::eTypeCoreFile)
92         return true;
93     }
94   }
95   return false;
96 }
97 
98 //----------------------------------------------------------------------
99 // ProcessElfCore constructor
100 //----------------------------------------------------------------------
101 ProcessElfCore::ProcessElfCore(lldb::TargetSP target_sp,
102                                lldb::ListenerSP listener_sp,
103                                const FileSpec &core_file)
104     : Process(target_sp, listener_sp), m_core_module_sp(),
105       m_core_file(core_file), m_dyld_plugin_name(),
106       m_os(llvm::Triple::UnknownOS), m_thread_data_valid(false),
107       m_thread_data(), m_core_aranges() {}
108 
109 //----------------------------------------------------------------------
110 // Destructor
111 //----------------------------------------------------------------------
112 ProcessElfCore::~ProcessElfCore() {
113   Clear();
114   // We need to call finalize on the process before destroying ourselves
115   // to make sure all of the broadcaster cleanup goes as planned. If we
116   // destruct this class, then Process::~Process() might have problems
117   // trying to fully destroy the broadcaster.
118   Finalize();
119 }
120 
121 //----------------------------------------------------------------------
122 // PluginInterface
123 //----------------------------------------------------------------------
124 ConstString ProcessElfCore::GetPluginName() { return GetPluginNameStatic(); }
125 
126 uint32_t ProcessElfCore::GetPluginVersion() { return 1; }
127 
128 lldb::addr_t ProcessElfCore::AddAddressRangeFromLoadSegment(
129     const elf::ELFProgramHeader *header) {
130   const lldb::addr_t addr = header->p_vaddr;
131   FileRange file_range(header->p_offset, header->p_filesz);
132   VMRangeToFileOffset::Entry range_entry(addr, header->p_memsz, file_range);
133 
134   VMRangeToFileOffset::Entry *last_entry = m_core_aranges.Back();
135   if (last_entry && last_entry->GetRangeEnd() == range_entry.GetRangeBase() &&
136       last_entry->data.GetRangeEnd() == range_entry.data.GetRangeBase() &&
137       last_entry->GetByteSize() == last_entry->data.GetByteSize()) {
138     last_entry->SetRangeEnd(range_entry.GetRangeEnd());
139     last_entry->data.SetRangeEnd(range_entry.data.GetRangeEnd());
140   } else {
141     m_core_aranges.Append(range_entry);
142   }
143 
144   // Keep a separate map of permissions that that isn't coalesced so all ranges
145   // are maintained.
146   const uint32_t permissions =
147       ((header->p_flags & llvm::ELF::PF_R) ? lldb::ePermissionsReadable : 0u) |
148       ((header->p_flags & llvm::ELF::PF_W) ? lldb::ePermissionsWritable : 0u) |
149       ((header->p_flags & llvm::ELF::PF_X) ? lldb::ePermissionsExecutable : 0u);
150 
151   m_core_range_infos.Append(
152       VMRangeToPermissions::Entry(addr, header->p_memsz, permissions));
153 
154   return addr;
155 }
156 
157 //----------------------------------------------------------------------
158 // Process Control
159 //----------------------------------------------------------------------
160 Error ProcessElfCore::DoLoadCore() {
161   Error error;
162   if (!m_core_module_sp) {
163     error.SetErrorString("invalid core module");
164     return error;
165   }
166 
167   ObjectFileELF *core = (ObjectFileELF *)(m_core_module_sp->GetObjectFile());
168   if (core == NULL) {
169     error.SetErrorString("invalid core object file");
170     return error;
171   }
172 
173   const uint32_t num_segments = core->GetProgramHeaderCount();
174   if (num_segments == 0) {
175     error.SetErrorString("core file has no segments");
176     return error;
177   }
178 
179   SetCanJIT(false);
180 
181   m_thread_data_valid = true;
182 
183   bool ranges_are_sorted = true;
184   lldb::addr_t vm_addr = 0;
185   /// Walk through segments and Thread and Address Map information.
186   /// PT_NOTE - Contains Thread and Register information
187   /// PT_LOAD - Contains a contiguous range of Process Address Space
188   for (uint32_t i = 1; i <= num_segments; i++) {
189     const elf::ELFProgramHeader *header = core->GetProgramHeaderByIndex(i);
190     assert(header != NULL);
191 
192     DataExtractor data = core->GetSegmentDataByIndex(i);
193 
194     // Parse thread contexts and auxv structure
195     if (header->p_type == llvm::ELF::PT_NOTE) {
196       error = ParseThreadContextsFromNoteSegment(header, data);
197       if (error.Fail())
198         return error;
199     }
200     // PT_LOAD segments contains address map
201     if (header->p_type == llvm::ELF::PT_LOAD) {
202       lldb::addr_t last_addr = AddAddressRangeFromLoadSegment(header);
203       if (vm_addr > last_addr)
204         ranges_are_sorted = false;
205       vm_addr = last_addr;
206     }
207   }
208 
209   if (!ranges_are_sorted) {
210     m_core_aranges.Sort();
211     m_core_range_infos.Sort();
212   }
213 
214   // Even if the architecture is set in the target, we need to override
215   // it to match the core file which is always single arch.
216   ArchSpec arch(m_core_module_sp->GetArchitecture());
217   if (arch.IsValid())
218     GetTarget().SetArchitecture(arch);
219 
220   SetUnixSignals(UnixSignals::Create(GetArchitecture()));
221 
222   // Ensure we found at least one thread that was stopped on a signal.
223   bool siginfo_signal_found = false;
224   bool prstatus_signal_found = false;
225   // Check we found a signal in a SIGINFO note.
226   for (const auto &thread_data: m_thread_data) {
227     if (thread_data.signo != 0)
228       siginfo_signal_found = true;
229     if (thread_data.prstatus_sig != 0)
230       prstatus_signal_found = true;
231   }
232   if (!siginfo_signal_found) {
233     // If we don't have signal from SIGINFO use the signal from each threads
234     // PRSTATUS note.
235     if (prstatus_signal_found) {
236       for (auto &thread_data: m_thread_data)
237         thread_data.signo = thread_data.prstatus_sig;
238     } else if (m_thread_data.size() > 0) {
239       // If all else fails force the first thread to be SIGSTOP
240       m_thread_data.begin()->signo =
241           GetUnixSignals()->GetSignalNumberFromName("SIGSTOP");
242     }
243   }
244 
245   // Core files are useless without the main executable. See if we can locate
246   // the main
247   // executable using data we found in the core file notes.
248   lldb::ModuleSP exe_module_sp = GetTarget().GetExecutableModule();
249   if (!exe_module_sp) {
250     // The first entry in the NT_FILE might be our executable
251     if (!m_nt_file_entries.empty()) {
252       ModuleSpec exe_module_spec;
253       exe_module_spec.GetArchitecture() = arch;
254       exe_module_spec.GetFileSpec().SetFile(
255           m_nt_file_entries[0].path.GetCString(), false);
256       if (exe_module_spec.GetFileSpec()) {
257         exe_module_sp = GetTarget().GetSharedModule(exe_module_spec);
258         if (exe_module_sp)
259           GetTarget().SetExecutableModule(exe_module_sp, false);
260       }
261     }
262   }
263   return error;
264 }
265 
266 lldb_private::DynamicLoader *ProcessElfCore::GetDynamicLoader() {
267   if (m_dyld_ap.get() == NULL)
268     m_dyld_ap.reset(DynamicLoader::FindPlugin(
269         this, DynamicLoaderPOSIXDYLD::GetPluginNameStatic().GetCString()));
270   return m_dyld_ap.get();
271 }
272 
273 bool ProcessElfCore::UpdateThreadList(ThreadList &old_thread_list,
274                                       ThreadList &new_thread_list) {
275   const uint32_t num_threads = GetNumThreadContexts();
276   if (!m_thread_data_valid)
277     return false;
278 
279   for (lldb::tid_t tid = 0; tid < num_threads; ++tid) {
280     const ThreadData &td = m_thread_data[tid];
281     lldb::ThreadSP thread_sp(new ThreadElfCore(*this, td));
282     new_thread_list.AddThread(thread_sp);
283   }
284   return new_thread_list.GetSize(false) > 0;
285 }
286 
287 void ProcessElfCore::RefreshStateAfterStop() {}
288 
289 Error ProcessElfCore::DoDestroy() { return Error(); }
290 
291 //------------------------------------------------------------------
292 // Process Queries
293 //------------------------------------------------------------------
294 
295 bool ProcessElfCore::IsAlive() { return true; }
296 
297 //------------------------------------------------------------------
298 // Process Memory
299 //------------------------------------------------------------------
300 size_t ProcessElfCore::ReadMemory(lldb::addr_t addr, void *buf, size_t size,
301                                   Error &error) {
302   // Don't allow the caching that lldb_private::Process::ReadMemory does
303   // since in core files we have it all cached our our core file anyway.
304   return DoReadMemory(addr, buf, size, error);
305 }
306 
307 Error ProcessElfCore::GetMemoryRegionInfo(lldb::addr_t load_addr,
308                                           MemoryRegionInfo &region_info) {
309   region_info.Clear();
310   const VMRangeToPermissions::Entry *permission_entry =
311       m_core_range_infos.FindEntryThatContainsOrFollows(load_addr);
312   if (permission_entry) {
313     if (permission_entry->Contains(load_addr)) {
314       region_info.GetRange().SetRangeBase(permission_entry->GetRangeBase());
315       region_info.GetRange().SetRangeEnd(permission_entry->GetRangeEnd());
316       const Flags permissions(permission_entry->data);
317       region_info.SetReadable(permissions.Test(lldb::ePermissionsReadable)
318                                   ? MemoryRegionInfo::eYes
319                                   : MemoryRegionInfo::eNo);
320       region_info.SetWritable(permissions.Test(lldb::ePermissionsWritable)
321                                   ? MemoryRegionInfo::eYes
322                                   : MemoryRegionInfo::eNo);
323       region_info.SetExecutable(permissions.Test(lldb::ePermissionsExecutable)
324                                     ? MemoryRegionInfo::eYes
325                                     : MemoryRegionInfo::eNo);
326       region_info.SetMapped(MemoryRegionInfo::eYes);
327     } else if (load_addr < permission_entry->GetRangeBase()) {
328       region_info.GetRange().SetRangeBase(load_addr);
329       region_info.GetRange().SetRangeEnd(permission_entry->GetRangeBase());
330       region_info.SetReadable(MemoryRegionInfo::eNo);
331       region_info.SetWritable(MemoryRegionInfo::eNo);
332       region_info.SetExecutable(MemoryRegionInfo::eNo);
333       region_info.SetMapped(MemoryRegionInfo::eNo);
334     }
335     return Error();
336   }
337 
338   region_info.GetRange().SetRangeBase(load_addr);
339   region_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS);
340   region_info.SetReadable(MemoryRegionInfo::eNo);
341   region_info.SetWritable(MemoryRegionInfo::eNo);
342   region_info.SetExecutable(MemoryRegionInfo::eNo);
343   region_info.SetMapped(MemoryRegionInfo::eNo);
344   return Error();
345 }
346 
347 size_t ProcessElfCore::DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
348                                     Error &error) {
349   ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
350 
351   if (core_objfile == NULL)
352     return 0;
353 
354   // Get the address range
355   const VMRangeToFileOffset::Entry *address_range =
356       m_core_aranges.FindEntryThatContains(addr);
357   if (address_range == NULL || address_range->GetRangeEnd() < addr) {
358     error.SetErrorStringWithFormat("core file does not contain 0x%" PRIx64,
359                                    addr);
360     return 0;
361   }
362 
363   // Convert the address into core file offset
364   const lldb::addr_t offset = addr - address_range->GetRangeBase();
365   const lldb::addr_t file_start = address_range->data.GetRangeBase();
366   const lldb::addr_t file_end = address_range->data.GetRangeEnd();
367   size_t bytes_to_read = size; // Number of bytes to read from the core file
368   size_t bytes_copied = 0;   // Number of bytes actually read from the core file
369   size_t zero_fill_size = 0; // Padding
370   lldb::addr_t bytes_left =
371       0; // Number of bytes available in the core file from the given address
372 
373   // Figure out how many on-disk bytes remain in this segment
374   // starting at the given offset
375   if (file_end > file_start + offset)
376     bytes_left = file_end - (file_start + offset);
377 
378   // Figure out how many bytes we need to zero-fill if we are
379   // reading more bytes than available in the on-disk segment
380   if (bytes_to_read > bytes_left) {
381     zero_fill_size = bytes_to_read - bytes_left;
382     bytes_to_read = bytes_left;
383   }
384 
385   // If there is data available on the core file read it
386   if (bytes_to_read)
387     bytes_copied =
388         core_objfile->CopyData(offset + file_start, bytes_to_read, buf);
389 
390   assert(zero_fill_size <= size);
391   // Pad remaining bytes
392   if (zero_fill_size)
393     memset(((char *)buf) + bytes_copied, 0, zero_fill_size);
394 
395   return bytes_copied + zero_fill_size;
396 }
397 
398 void ProcessElfCore::Clear() {
399   m_thread_list.Clear();
400   m_os = llvm::Triple::UnknownOS;
401 
402   SetUnixSignals(std::make_shared<UnixSignals>());
403 }
404 
405 void ProcessElfCore::Initialize() {
406   static llvm::once_flag g_once_flag;
407 
408   llvm::call_once(g_once_flag, []() {
409     PluginManager::RegisterPlugin(GetPluginNameStatic(),
410                                   GetPluginDescriptionStatic(), CreateInstance);
411   });
412 }
413 
414 lldb::addr_t ProcessElfCore::GetImageInfoAddress() {
415   ObjectFile *obj_file = GetTarget().GetExecutableModule()->GetObjectFile();
416   Address addr = obj_file->GetImageInfoAddress(&GetTarget());
417 
418   if (addr.IsValid())
419     return addr.GetLoadAddress(&GetTarget());
420   return LLDB_INVALID_ADDRESS;
421 }
422 
423 /// Core files PT_NOTE segment descriptor types
424 enum {
425   NT_PRSTATUS = 1,
426   NT_FPREGSET,
427   NT_PRPSINFO,
428   NT_TASKSTRUCT,
429   NT_PLATFORM,
430   NT_AUXV,
431   NT_FILE = 0x46494c45,
432   NT_PRXFPREG = 0x46e62b7f,
433   NT_SIGINFO = 0x53494749,
434 };
435 
436 namespace FREEBSD {
437 
438 enum {
439   NT_PRSTATUS = 1,
440   NT_FPREGSET,
441   NT_PRPSINFO,
442   NT_THRMISC = 7,
443   NT_PROCSTAT_AUXV = 16,
444   NT_PPC_VMX = 0x100
445 };
446 }
447 
448 // Parse a FreeBSD NT_PRSTATUS note - see FreeBSD sys/procfs.h for details.
449 static void ParseFreeBSDPrStatus(ThreadData &thread_data, DataExtractor &data,
450                                  ArchSpec &arch) {
451   lldb::offset_t offset = 0;
452   bool lp64 = (arch.GetMachine() == llvm::Triple::aarch64 ||
453                arch.GetMachine() == llvm::Triple::mips64 ||
454                arch.GetMachine() == llvm::Triple::ppc64 ||
455                arch.GetMachine() == llvm::Triple::x86_64);
456   int pr_version = data.GetU32(&offset);
457 
458   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
459   if (log) {
460     if (pr_version > 1)
461       log->Printf("FreeBSD PRSTATUS unexpected version %d", pr_version);
462   }
463 
464   // Skip padding, pr_statussz, pr_gregsetsz, pr_fpregsetsz, pr_osreldate
465   if (lp64)
466     offset += 32;
467   else
468     offset += 16;
469 
470   thread_data.signo = data.GetU32(&offset); // pr_cursig
471   thread_data.tid = data.GetU32(&offset);   // pr_pid
472   if (lp64)
473     offset += 4;
474 
475   size_t len = data.GetByteSize() - offset;
476   thread_data.gpregset = DataExtractor(data, offset, len);
477 }
478 
479 static void ParseFreeBSDThrMisc(ThreadData &thread_data, DataExtractor &data) {
480   lldb::offset_t offset = 0;
481   thread_data.name = data.GetCStr(&offset, 20);
482 }
483 
484 /// Parse Thread context from PT_NOTE segment and store it in the thread list
485 /// Notes:
486 /// 1) A PT_NOTE segment is composed of one or more NOTE entries.
487 /// 2) NOTE Entry contains a standard header followed by variable size data.
488 ///   (see ELFNote structure)
489 /// 3) A Thread Context in a core file usually described by 3 NOTE entries.
490 ///    a) NT_PRSTATUS - Register context
491 ///    b) NT_PRPSINFO - Process info(pid..)
492 ///    c) NT_FPREGSET - Floating point registers
493 /// 4) The NOTE entries can be in any order
494 /// 5) If a core file contains multiple thread contexts then there is two data
495 /// forms
496 ///    a) Each thread context(2 or more NOTE entries) contained in its own
497 ///    segment (PT_NOTE)
498 ///    b) All thread context is stored in a single segment(PT_NOTE).
499 ///        This case is little tricker since while parsing we have to find where
500 ///        the
501 ///        new thread starts. The current implementation marks beginning of
502 ///        new thread when it finds NT_PRSTATUS or NT_PRPSINFO NOTE entry.
503 ///    For case (b) there may be either one NT_PRPSINFO per thread, or a single
504 ///    one that applies to all threads (depending on the platform type).
505 Error ProcessElfCore::ParseThreadContextsFromNoteSegment(
506     const elf::ELFProgramHeader *segment_header, DataExtractor segment_data) {
507   assert(segment_header && segment_header->p_type == llvm::ELF::PT_NOTE);
508 
509   lldb::offset_t offset = 0;
510   std::unique_ptr<ThreadData> thread_data(new ThreadData);
511   bool have_prstatus = false;
512   bool have_prpsinfo = false;
513 
514   ArchSpec arch = GetArchitecture();
515   ELFLinuxPrPsInfo prpsinfo;
516   ELFLinuxPrStatus prstatus;
517   ELFLinuxSigInfo siginfo;
518   size_t header_size;
519   size_t len;
520   Error error;
521 
522   // Loop through the NOTE entires in the segment
523   while (offset < segment_header->p_filesz) {
524     ELFNote note = ELFNote();
525     note.Parse(segment_data, &offset);
526 
527     // Beginning of new thread
528     if ((note.n_type == NT_PRSTATUS && have_prstatus) ||
529         (note.n_type == NT_PRPSINFO && have_prpsinfo)) {
530       assert(thread_data->gpregset.GetByteSize() > 0);
531       // Add the new thread to thread list
532       m_thread_data.push_back(*thread_data);
533       *thread_data = ThreadData();
534       have_prstatus = false;
535       have_prpsinfo = false;
536     }
537 
538     size_t note_start, note_size;
539     note_start = offset;
540     note_size = llvm::alignTo(note.n_descsz, 4);
541 
542     // Store the NOTE information in the current thread
543     DataExtractor note_data(segment_data, note_start, note_size);
544     note_data.SetAddressByteSize(
545         m_core_module_sp->GetArchitecture().GetAddressByteSize());
546     if (note.n_name == "FreeBSD") {
547       m_os = llvm::Triple::FreeBSD;
548       switch (note.n_type) {
549       case FREEBSD::NT_PRSTATUS:
550         have_prstatus = true;
551         ParseFreeBSDPrStatus(*thread_data, note_data, arch);
552         break;
553       case FREEBSD::NT_FPREGSET:
554         thread_data->fpregset = note_data;
555         break;
556       case FREEBSD::NT_PRPSINFO:
557         have_prpsinfo = true;
558         break;
559       case FREEBSD::NT_THRMISC:
560         ParseFreeBSDThrMisc(*thread_data, note_data);
561         break;
562       case FREEBSD::NT_PROCSTAT_AUXV:
563         // FIXME: FreeBSD sticks an int at the beginning of the note
564         m_auxv = DataExtractor(segment_data, note_start + 4, note_size - 4);
565         break;
566       case FREEBSD::NT_PPC_VMX:
567         thread_data->vregset = note_data;
568         break;
569       default:
570         break;
571       }
572     } else if (note.n_name == "CORE") {
573       switch (note.n_type) {
574       case NT_PRSTATUS:
575         have_prstatus = true;
576         error = prstatus.Parse(note_data, arch);
577         if (error.Fail())
578           return error;
579         thread_data->prstatus_sig = prstatus.pr_cursig;
580         thread_data->tid = prstatus.pr_pid;
581         header_size = ELFLinuxPrStatus::GetSize(arch);
582         len = note_data.GetByteSize() - header_size;
583         thread_data->gpregset = DataExtractor(note_data, header_size, len);
584         break;
585       case NT_FPREGSET:
586         // In a i386 core file NT_FPREGSET is present, but it's not the result
587         // of the FXSAVE instruction like in 64 bit files.
588         // The result from FXSAVE is in NT_PRXFPREG for i386 core files
589         if (arch.GetCore() == ArchSpec::eCore_x86_64_x86_64)
590           thread_data->fpregset = note_data;
591         break;
592       case NT_PRPSINFO:
593         have_prpsinfo = true;
594         error = prpsinfo.Parse(note_data, arch);
595         if (error.Fail())
596           return error;
597         thread_data->name = prpsinfo.pr_fname;
598         SetID(prpsinfo.pr_pid);
599         break;
600       case NT_AUXV:
601         m_auxv = DataExtractor(note_data);
602         break;
603       case NT_FILE: {
604         m_nt_file_entries.clear();
605         lldb::offset_t offset = 0;
606         const uint64_t count = note_data.GetAddress(&offset);
607         note_data.GetAddress(&offset); // Skip page size
608         for (uint64_t i = 0; i < count; ++i) {
609           NT_FILE_Entry entry;
610           entry.start = note_data.GetAddress(&offset);
611           entry.end = note_data.GetAddress(&offset);
612           entry.file_ofs = note_data.GetAddress(&offset);
613           m_nt_file_entries.push_back(entry);
614         }
615         for (uint64_t i = 0; i < count; ++i) {
616           const char *path = note_data.GetCStr(&offset);
617           if (path && path[0])
618             m_nt_file_entries[i].path.SetCString(path);
619         }
620       } break;
621       case NT_SIGINFO: {
622         error = siginfo.Parse(note_data, arch);
623         if (error.Fail())
624           return error;
625         thread_data->signo = siginfo.si_signo;
626       } break;
627       default:
628         break;
629       }
630     } else if (note.n_name == "LINUX") {
631       switch (note.n_type) {
632       case NT_PRXFPREG:
633         thread_data->fpregset = note_data;
634       }
635     }
636 
637     offset += note_size;
638   }
639   // Add last entry in the note section
640   if (thread_data && thread_data->gpregset.GetByteSize() > 0) {
641     m_thread_data.push_back(*thread_data);
642   }
643 
644   return error;
645 }
646 
647 uint32_t ProcessElfCore::GetNumThreadContexts() {
648   if (!m_thread_data_valid)
649     DoLoadCore();
650   return m_thread_data.size();
651 }
652 
653 ArchSpec ProcessElfCore::GetArchitecture() {
654   ObjectFileELF *core_file =
655       (ObjectFileELF *)(m_core_module_sp->GetObjectFile());
656   ArchSpec arch;
657   core_file->GetArchitecture(arch);
658   return arch;
659 }
660 
661 const lldb::DataBufferSP ProcessElfCore::GetAuxvData() {
662   const uint8_t *start = m_auxv.GetDataStart();
663   size_t len = m_auxv.GetByteSize();
664   lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(start, len));
665   return buffer;
666 }
667 
668 bool ProcessElfCore::GetProcessInfo(ProcessInstanceInfo &info) {
669   info.Clear();
670   info.SetProcessID(GetID());
671   info.SetArchitecture(GetArchitecture());
672   lldb::ModuleSP module_sp = GetTarget().GetExecutableModule();
673   if (module_sp) {
674     const bool add_exe_file_as_first_arg = false;
675     info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
676                            add_exe_file_as_first_arg);
677   }
678   return true;
679 }
680