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