LLDB mainline
ProcessElfCore.cpp
Go to the documentation of this file.
1//===-- ProcessElfCore.cpp ------------------------------------------------===//
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 <cstdlib>
10
11#include <memory>
12
13#include "lldb/Core/Module.h"
16#include "lldb/Core/Section.h"
17#include "lldb/Target/ABI.h"
20#include "lldb/Target/Target.h"
24#include "lldb/Utility/Log.h"
25#include "lldb/Utility/State.h"
26
27#include "llvm/BinaryFormat/ELF.h"
28
34#include "ProcessElfCore.h"
35#include "ThreadElfCore.h"
36
37using namespace lldb_private;
38namespace ELF = llvm::ELF;
39
41
43 return "ELF core dump plug-in.";
44}
45
49
51 lldb::ListenerSP listener_sp,
52 const FileSpec *crash_file,
53 bool can_connect) {
54 lldb::ProcessSP process_sp;
55 if (crash_file && !can_connect) {
56 // Read enough data for an ELF32 header or ELF64 header Note: Here we care
57 // about e_type field only, so it is safe to ignore possible presence of
58 // the header extension.
59 const size_t header_size = sizeof(llvm::ELF::Elf64_Ehdr);
60
62 crash_file->GetPath(), header_size, 0);
63 if (data_sp && data_sp->GetByteSize() == header_size &&
64 elf::ELFHeader::MagicBytesMatch(data_sp->GetBytes())) {
65 elf::ELFHeader elf_header;
66 DataExtractor data(data_sp, lldb::eByteOrderLittle, 4);
67 lldb::offset_t data_offset = 0;
68 if (elf_header.Parse(data, &data_offset)) {
69 // Check whether we're dealing with a raw FreeBSD "full memory dump"
70 // ELF vmcore that needs to be handled via FreeBSDKernel plugin instead.
71 if (elf_header.e_ident[7] == 0xFF && elf_header.e_version == 0)
72 return process_sp;
73 if (elf_header.e_type == llvm::ELF::ET_CORE)
74 process_sp = std::make_shared<ProcessElfCore>(target_sp, listener_sp,
75 *crash_file);
76 }
77 }
78 }
79 return process_sp;
80}
81
83 bool plugin_specified_by_name) {
84 // For now we are just making sure the file exists for a given module
86 ModuleSpec core_module_spec(m_core_file, target_sp->GetArchitecture());
87 core_module_spec.SetTarget(target_sp);
89 nullptr, nullptr));
90 if (m_core_module_sp) {
91 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
92 if (core_objfile && core_objfile->GetType() == ObjectFile::eTypeCoreFile)
93 return true;
94 }
95 }
96 return false;
97}
98
99// ProcessElfCore constructor
101 lldb::ListenerSP listener_sp,
102 const FileSpec &core_file)
103 : PostMortemProcess(target_sp, listener_sp, core_file), m_uuids() {}
104
105// Destructor
107 Clear();
108 // We need to call finalize on the process before destroying ourselves to
109 // make sure all of the broadcaster cleanup goes as planned. If we destruct
110 // this class, then Process::~Process() might have problems trying to fully
111 // destroy the broadcaster.
112 Finalize(true /* destructing */);
113}
114
116 const elf::ELFProgramHeader &header) {
117 const lldb::addr_t addr = header.p_vaddr;
118 FileRange file_range(header.p_offset, header.p_filesz);
119 VMRangeToFileOffset::Entry range_entry(addr, header.p_memsz, file_range);
120
121 // Only add to m_core_aranges if the file size is non zero. Some core files
122 // have PT_LOAD segments for all address ranges, but set f_filesz to zero for
123 // the .text sections since they can be retrieved from the object files.
124 if (header.p_filesz > 0) {
125 VMRangeToFileOffset::Entry *last_entry = m_core_aranges.Back();
126 if (last_entry && last_entry->GetRangeEnd() == range_entry.GetRangeBase() &&
127 last_entry->data.GetRangeEnd() == range_entry.data.GetRangeBase() &&
128 last_entry->GetByteSize() == last_entry->data.GetByteSize()) {
129 last_entry->SetRangeEnd(range_entry.GetRangeEnd());
130 last_entry->data.SetRangeEnd(range_entry.data.GetRangeEnd());
131 } else {
132 m_core_aranges.Append(range_entry);
133 }
134 }
135 // Keep a separate map of permissions that isn't coalesced so all ranges
136 // are maintained.
137 const uint32_t permissions =
138 ((header.p_flags & llvm::ELF::PF_R) ? lldb::ePermissionsReadable : 0u) |
139 ((header.p_flags & llvm::ELF::PF_W) ? lldb::ePermissionsWritable : 0u) |
140 ((header.p_flags & llvm::ELF::PF_X) ? lldb::ePermissionsExecutable : 0u);
141
142 m_core_range_infos.Append(
143 VMRangeToPermissions::Entry(addr, header.p_memsz, permissions));
144
145 return addr;
146}
147
149 const elf::ELFProgramHeader &header) {
150 // If lldb understood multiple kinds of tag segments we would record the type
151 // of the segment here also. As long as there is only 1 type lldb looks for,
152 // there is no need.
153 FileRange file_range(header.p_offset, header.p_filesz);
154 m_core_tag_ranges.Append(
155 VMRangeToFileOffset::Entry(header.p_vaddr, header.p_memsz, file_range));
156
157 return header.p_vaddr;
158}
159
160// Process Control
163 if (!m_core_module_sp) {
164 error = Status::FromErrorString("invalid core module");
165 return error;
166 }
167
168 ObjectFileELF *core = (ObjectFileELF *)(m_core_module_sp->GetObjectFile());
169 if (core == nullptr) {
170 error = Status::FromErrorString("invalid core object file");
171 return error;
172 }
173
174 llvm::ArrayRef<elf::ELFProgramHeader> segments = core->ProgramHeaders();
175 if (segments.size() == 0) {
176 error = Status::FromErrorString("core file has no segments");
177 return error;
178 }
179
180 // Even if the architecture is set in the target, we need to override it to
181 // match the core file which is always single arch.
182 ArchSpec arch(m_core_module_sp->GetArchitecture());
183
184 ArchSpec target_arch = GetTarget().GetArchitecture();
185 ArchSpec core_arch(m_core_module_sp->GetArchitecture());
186 target_arch.MergeFrom(core_arch);
187 GetTarget().SetArchitecture(target_arch, /*set_platform*/ true);
188
190
191 SetCanJIT(false);
192
193 m_thread_data_valid = true;
194
195 bool ranges_are_sorted = true;
196 lldb::addr_t vm_addr = 0;
197 lldb::addr_t tag_addr = 0;
198 /// Walk through segments and Thread and Address Map information.
199 /// PT_NOTE - Contains Thread and Register information
200 /// PT_LOAD - Contains a contiguous range of Process Address Space
201 /// PT_AARCH64_MEMTAG_MTE - Contains AArch64 MTE memory tags for a range of
202 /// Process Address Space.
203 for (const elf::ELFProgramHeader &H : segments) {
204
205 // Parse thread contexts and auxv structure
206 if (H.p_type == llvm::ELF::PT_NOTE) {
207 DataExtractor data = core->GetSegmentData(H);
208 if (llvm::Error error = ParseThreadContextsFromNoteSegment(H, data))
209 return Status::FromError(std::move(error));
210 }
211 // PT_LOAD segments contains address map
212 if (H.p_type == llvm::ELF::PT_LOAD) {
214 if (vm_addr > last_addr)
215 ranges_are_sorted = false;
216 vm_addr = last_addr;
217 } else if (H.p_type == llvm::ELF::PT_AARCH64_MEMTAG_MTE) {
219 if (tag_addr > last_addr)
220 ranges_are_sorted = false;
221 tag_addr = last_addr;
222 }
223 }
224
225 if (!ranges_are_sorted) {
226 m_core_aranges.Sort();
227 m_core_range_infos.Sort();
228 m_core_tag_ranges.Sort();
229 }
230
231 // Ensure we found at least one thread that was stopped on a signal.
232 bool siginfo_signal_found = false;
233 bool prstatus_signal_found = false;
234 // Check we found a signal in a SIGINFO note.
235 for (const auto &thread_data : m_thread_data) {
236 if (!thread_data.siginfo_bytes.empty() || thread_data.signo != 0)
237 siginfo_signal_found = true;
238 if (thread_data.prstatus_sig != 0)
239 prstatus_signal_found = true;
240 }
241 if (!siginfo_signal_found) {
242 // If we don't have signal from SIGINFO use the signal from each threads
243 // PRSTATUS note.
244 if (prstatus_signal_found) {
245 for (auto &thread_data : m_thread_data)
246 thread_data.signo = thread_data.prstatus_sig;
247 } else if (m_thread_data.size() > 0) {
248 // If all else fails force the first thread to be SIGSTOP
249 m_thread_data.begin()->signo =
250 GetUnixSignals()->GetSignalNumberFromName("SIGSTOP");
251 }
252 }
253
254 // Try to find gnu build id before we load the executable.
256
257 // Core files are useless without the main executable. See if we can locate
258 // the main executable using data we found in the core file notes.
259 lldb::ModuleSP exe_module_sp = GetTarget().GetExecutableModule();
260 if (!exe_module_sp) {
261 ModuleSpec exe_module_spec;
262 if (GetMainExecutableModuleSpec(exe_module_spec)) {
263 exe_module_sp =
264 GetTarget().GetOrCreateModule(exe_module_spec, true /* notify */);
265 if (!exe_module_sp) {
266 // Create an ELF file from memory for the main executable. The dynamic
267 // loader requires the main executable so that it can extract the
268 // DT_DEBUG key/value pair from the dynamic section and get the list
269 // of shared libraries.
270 std::optional<NT_FILE_Entry> exe_header =
272 if (exe_header) {
273 if (llvm::Expected<lldb::ModuleSP> module_sp_or_err =
274 ReadModuleFromMemory(exe_module_spec.GetFileSpec(),
275 exe_header->start,
276 exe_header->end - exe_header->start))
277 exe_module_sp = *module_sp_or_err;
278 else
279 llvm::consumeError(module_sp_or_err.takeError());
280 }
281 // Create a placeholder module for the main executable if we failed to
282 // create an ELF module from memory.
283 if (!exe_module_sp) {
284 lldb::addr_t load_addr =
285 exe_header ? exe_header->start : LLDB_INVALID_ADDRESS;
286 lldb::addr_t size =
287 exe_header ? (exe_header->end - exe_header->start) : 0;
288 exe_module_sp =
290 exe_module_spec, load_addr, size);
291 if (exe_module_spec.GetPlatformFileSpec())
292 exe_module_sp->SetPlatformFileSpec(
293 exe_module_spec.GetPlatformFileSpec());
294 }
295 }
296 if (exe_module_sp)
298 }
299 }
300 return error;
301}
302
305 m_uuids.clear();
306 for (NT_FILE_Entry &entry : m_nt_file_entries) {
307 UUID uuid = FindBuidIdInCoreMemory(entry.start);
308 if (uuid.IsValid()) {
309 // Assert that either the path is not in the map or the UUID matches
310 assert(m_uuids.count(entry.path) == 0 || m_uuids[entry.path] == uuid);
311 m_uuids[entry.path] = uuid;
312 LLDB_LOGF(log, "%s found UUID @ %16.16" PRIx64 ": %s \"%s\"",
313 __FUNCTION__, entry.start, uuid.GetAsString().c_str(),
314 entry.path.c_str());
315 }
316 }
317}
318
319/// Correctly create a FileSpec from a path found in a core file.
320///
321/// This method will guess the path style more intelligently that specifying
322/// a native path style since core files can contain paths from a different
323/// system than the host system.
324static FileSpec CreateFileSpecFromPath(llvm::StringRef path) {
325 FileSpec::Style path_style = FileSpec::Style::native;
326 if (auto guessed_style = FileSpec::GuessPathStyle(path))
327 path_style = *guessed_style;
328 return FileSpec(path, path_style);
329}
330
332 AuxVector aux_vector(m_auxv);
334
335 // Find the NT_FILE_Entry for the main executable's ELF header.
336 std::optional<NT_FILE_Entry> exe_header =
338 if (exe_header) {
339 exe_spec.GetFileSpec() = CreateFileSpecFromPath(exe_header->path);
340 exe_spec.GetUUID() = FindModuleUUID(exe_header->path);
341 }
342
343 // If we failed to find the executable program in the NT_FILE list with the
344 // program header address, then we can read the executable name from the value
345 // of the AUXV_AT_EXECFN in the AUX vector. The reason we don't use this file
346 // all of the time is if the program is launched using a symlink, the value of
347 // the AUXV_AT_EXECFN string will be the symlink itself. The same goes for the
348 // m_executable_name found in the NT_PRPSINFO section, it will be the name of
349 // the symlink. Even if we did find a path above, we want to fill in this path
350 // if it is different from main executable's path in the platform file name
351 // in case someone needs to know how the executable was launched.
352 if (auto execfn = aux_vector.GetAuxValue(AuxVector::AUXV_AT_EXECFN)) {
354 std::string execfn_str;
355 if (ReadCStringFromMemory(*execfn, execfn_str, error)) {
356 // This path can be a symlink path. Set it as the main file spec if one
357 // hasn't been set, else set the platform file spec.
358 FileSpec execfn_spec = CreateFileSpecFromPath(execfn_str);
359 if (exe_spec.GetFileSpec()) {
360 // Fill in the platform file spec if it differs from the main path from
361 // the resolved file info in the NT_FILE note.
362 if (exe_spec.GetFileSpec() != execfn_spec)
363 exe_spec.GetPlatformFileSpec() = execfn_spec;
364 } else {
365 // We don't have an executable file spec yet, lets set it.
366 exe_spec.GetFileSpec() = execfn_spec;
367 exe_spec.GetUUID() = FindModuleUUID(execfn_str);
368 }
369 }
370 }
371
372 // If we didn't set the executable file spec yet, lets set it from the info
373 // from the NT_PRPSINFO. This usually is just a basename of the actual path
374 // used to launch the binary, so this can be a symlink basename. But it will
375 // be better than nothing since we will create a placeholder module for any
376 // files that don't exist.
377 if (!exe_spec.GetFileSpec() && !m_executable_name.empty())
379
380 // We succeeded if we got a path.
381 return (bool)exe_spec.GetFileSpec();
382}
383
384UUID ProcessElfCore::FindModuleUUID(const llvm::StringRef path) {
385 // Lookup the UUID for the given path in the map.
386 // Note that this could be called by multiple threads so make sure
387 // we access the map in a thread safe way (i.e. don't use operator[]).
388 auto it = m_uuids.find(std::string(path));
389 if (it != m_uuids.end())
390 return it->second;
391 return UUID();
392}
393
395 if (!m_dyld_up) {
396 llvm::StringRef dyld_name;
397 if (GetTarget().GetArchitecture().GetMachine() == llvm::Triple::riscv32 &&
398 GetTarget().GetArchitecture().GetTriple().getOS() ==
399 llvm::Triple::UnknownOS)
401 else
403 m_dyld_up.reset(DynamicLoader::FindPlugin(this, dyld_name));
404 }
405 return m_dyld_up.get();
406}
407
409 ThreadList &new_thread_list) {
410 const uint32_t num_threads = GetNumThreadContexts();
412 return false;
413
414 for (lldb::tid_t tid = 0; tid < num_threads; ++tid) {
415 const ThreadData &td = m_thread_data[tid];
416 lldb::ThreadSP thread_sp(new ThreadElfCore(*this, td));
417 new_thread_list.AddThread(thread_sp);
418 }
419 return new_thread_list.GetSize(false) > 0;
420}
421
423
425
426// Process Queries
427
428bool ProcessElfCore::IsAlive() { return true; }
429
430// Process Memory
431size_t ProcessElfCore::ReadMemory(lldb::addr_t addr, void *buf, size_t size,
432 Status &error) {
433 if (lldb::ABISP abi_sp = GetABI())
434 addr = abi_sp->FixAnyAddress(addr);
435
436 // Don't allow the caching that lldb_private::Process::ReadMemory does since
437 // in core files we have it all cached our our core file anyway.
438 return DoReadMemory(addr, buf, size, error);
439}
440
442 MemoryRegionInfo &region_info) {
443 region_info.Clear();
444 const VMRangeToPermissions::Entry *permission_entry =
445 m_core_range_infos.FindEntryThatContainsOrFollows(load_addr);
446 if (permission_entry) {
447 if (permission_entry->Contains(load_addr)) {
448 region_info.GetRange().SetRangeBase(permission_entry->GetRangeBase());
449 region_info.GetRange().SetRangeEnd(permission_entry->GetRangeEnd());
450 const Flags permissions(permission_entry->data);
451 region_info.SetReadable(permissions.Test(lldb::ePermissionsReadable)
453 : eLazyBoolNo);
454 region_info.SetWritable(permissions.Test(lldb::ePermissionsWritable)
456 : eLazyBoolNo);
457 region_info.SetExecutable(permissions.Test(lldb::ePermissionsExecutable)
459 : eLazyBoolNo);
460 region_info.SetMapped(eLazyBoolYes);
461
462 // A region is memory tagged if there is a memory tag segment that covers
463 // the exact same range.
464 region_info.SetMemoryTagged(eLazyBoolNo);
465 const VMRangeToFileOffset::Entry *tag_entry =
466 m_core_tag_ranges.FindEntryStartsAt(permission_entry->GetRangeBase());
467 if (tag_entry &&
468 tag_entry->GetRangeEnd() == permission_entry->GetRangeEnd())
469 region_info.SetMemoryTagged(eLazyBoolYes);
470 } else if (load_addr < permission_entry->GetRangeBase()) {
471 region_info.GetRange().SetRangeBase(load_addr);
472 region_info.GetRange().SetRangeEnd(permission_entry->GetRangeBase());
473 region_info.SetReadable(eLazyBoolNo);
474 region_info.SetWritable(eLazyBoolNo);
475 region_info.SetExecutable(eLazyBoolNo);
476 region_info.SetMapped(eLazyBoolNo);
477 region_info.SetMemoryTagged(eLazyBoolNo);
478 }
479 return Status();
480 }
481
482 region_info.GetRange().SetRangeBase(load_addr);
484 region_info.SetReadable(eLazyBoolNo);
485 region_info.SetWritable(eLazyBoolNo);
486 region_info.SetExecutable(eLazyBoolNo);
487 region_info.SetMapped(eLazyBoolNo);
488 region_info.SetMemoryTagged(eLazyBoolNo);
489 return Status();
490}
491
492size_t ProcessElfCore::DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
493 Status &error) {
494 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
495
496 if (core_objfile == nullptr)
497 return 0;
498
499 // Get the address range
500 const VMRangeToFileOffset::Entry *address_range =
501 m_core_aranges.FindEntryThatContains(addr);
502 if (address_range == nullptr || address_range->GetRangeEnd() < addr) {
504 "core file does not contain 0x%" PRIx64, addr);
505 return 0;
506 }
507
508 // Convert the address into core file offset
509 const lldb::addr_t offset = addr - address_range->GetRangeBase();
510 const lldb::addr_t file_start = address_range->data.GetRangeBase();
511 const lldb::addr_t file_end = address_range->data.GetRangeEnd();
512 size_t bytes_to_read = size; // Number of bytes to read from the core file
513 size_t bytes_copied = 0; // Number of bytes actually read from the core file
514 lldb::addr_t bytes_left =
515 0; // Number of bytes available in the core file from the given address
516
517 // Don't proceed if core file doesn't contain the actual data for this
518 // address range.
519 if (file_start == file_end)
520 return 0;
521
522 // Figure out how many on-disk bytes remain in this segment starting at the
523 // given offset
524 if (file_end > file_start + offset)
525 bytes_left = file_end - (file_start + offset);
526
527 if (bytes_to_read > bytes_left)
528 bytes_to_read = bytes_left;
529
530 // If there is data available on the core file read it
531 if (bytes_to_read)
532 bytes_copied =
533 core_objfile->CopyData(offset + file_start, bytes_to_read, buf);
534
535 return bytes_copied;
536}
537
538llvm::Expected<std::vector<lldb::addr_t>>
540 ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
541 if (core_objfile == nullptr)
542 return llvm::createStringError(llvm::inconvertibleErrorCode(),
543 "No core object file.");
544
545 llvm::Expected<const MemoryTagManager *> tag_manager_or_err =
547 if (!tag_manager_or_err)
548 return tag_manager_or_err.takeError();
549
550 // LLDB only supports AArch64 MTE tag segments so we do not need to worry
551 // about the segment type here. If you got here then you must have a tag
552 // manager (meaning you are debugging AArch64) and all the segments in this
553 // list will have had type PT_AARCH64_MEMTAG_MTE.
554 const VMRangeToFileOffset::Entry *tag_entry =
555 m_core_tag_ranges.FindEntryThatContains(addr);
556 // If we don't have a tag segment or the range asked for extends outside the
557 // segment.
558 if (!tag_entry || (addr + len) >= tag_entry->GetRangeEnd())
559 return llvm::createStringError(llvm::inconvertibleErrorCode(),
560 "No tag segment that covers this range.");
561
562 const MemoryTagManager *tag_manager = *tag_manager_or_err;
563 return tag_manager->UnpackTagsFromCoreFileSegment(
564 [core_objfile](lldb::offset_t offset, size_t length, void *dst) {
565 return core_objfile->CopyData(offset, length, dst);
566 },
567 tag_entry->GetRangeBase(), tag_entry->data.GetRangeBase(), addr, len);
568}
569
571 m_thread_list.Clear();
572
573 SetUnixSignals(std::make_shared<UnixSignals>());
574}
575
580
582 ObjectFile *obj_file = GetTarget().GetExecutableModule()->GetObjectFile();
583 Address addr = obj_file->GetImageInfoAddress(&GetTarget());
584
585 if (addr.IsValid())
586 return addr.GetLoadAddress(&GetTarget());
588}
589
590// Parse a FreeBSD NT_PRSTATUS note - see FreeBSD sys/procfs.h for details.
591static void ParseFreeBSDPrStatus(ThreadData &thread_data,
592 const DataExtractor &data,
593 bool lp64) {
594 lldb::offset_t offset = 0;
595 int pr_version = data.GetU32(&offset);
596
598 if (pr_version > 1)
599 LLDB_LOGF(log, "FreeBSD PRSTATUS unexpected version %d", pr_version);
600
601 // Skip padding, pr_statussz, pr_gregsetsz, pr_fpregsetsz, pr_osreldate
602 if (lp64)
603 offset += 32;
604 else
605 offset += 16;
606
607 thread_data.signo = data.GetU32(&offset); // pr_cursig
608 thread_data.tid = data.GetU32(&offset); // pr_pid
609 if (lp64)
610 offset += 4;
611
612 size_t len = data.GetByteSize() - offset;
613 thread_data.gpregset = DataExtractor(data, offset, len);
614}
615
616// Parse a FreeBSD NT_PRPSINFO note - see FreeBSD sys/procfs.h for details.
618 const DataExtractor &data,
619 bool lp64) {
620 lldb::offset_t offset = 0;
621 int pr_version = data.GetU32(&offset);
622
624 if (pr_version > 1)
625 LLDB_LOGF(log, "FreeBSD PRPSINFO unexpected version %d", pr_version);
626
627 // Skip pr_psinfosz, pr_fname, pr_psargs
628 offset += 108;
629 if (lp64)
630 offset += 4;
631
632 process.SetID(data.GetU32(&offset)); // pr_pid
633}
634
635static llvm::Error ParseNetBSDProcInfo(const DataExtractor &data,
636 uint32_t &cpi_nlwps,
637 uint32_t &cpi_signo,
638 uint32_t &cpi_siglwp,
639 uint32_t &cpi_pid) {
640 lldb::offset_t offset = 0;
641
642 uint32_t version = data.GetU32(&offset);
643 if (version != 1)
644 return llvm::createStringError(
645 "Error parsing NetBSD core(5) notes: Unsupported procinfo version");
646
647 uint32_t cpisize = data.GetU32(&offset);
648 if (cpisize != NETBSD::NT_PROCINFO_SIZE)
649 return llvm::createStringError(
650 "Error parsing NetBSD core(5) notes: Unsupported procinfo size");
651
652 cpi_signo = data.GetU32(&offset); /* killing signal */
653
659 cpi_pid = data.GetU32(&offset);
669 cpi_nlwps = data.GetU32(&offset); /* number of LWPs */
670
672 cpi_siglwp = data.GetU32(&offset); /* LWP target of killing signal */
673
674 return llvm::Error::success();
675}
676
677static void ParseOpenBSDProcInfo(ThreadData &thread_data,
678 const DataExtractor &data) {
679 lldb::offset_t offset = 0;
680
681 int version = data.GetU32(&offset);
682 if (version != 1)
683 return;
684
685 offset += 4;
686 thread_data.signo = data.GetU32(&offset);
687}
688
689llvm::Expected<std::vector<CoreNote>>
691 lldb::offset_t offset = 0;
692 std::vector<CoreNote> result;
693
694 while (offset < segment.GetByteSize()) {
695 ELFNote note = ELFNote();
696 if (!note.Parse(segment, &offset))
697 return llvm::createStringError("unable to parse note segment");
698
699 size_t note_start = offset;
700 size_t note_size = llvm::alignTo(note.n_descsz, 4);
701
702 result.push_back({note, DataExtractor(segment, note_start, note_size)});
703 offset += note_size;
704 }
705
706 return std::move(result);
707}
708
709llvm::Error ProcessElfCore::parseFreeBSDNotes(llvm::ArrayRef<CoreNote> notes) {
710 ArchSpec arch = GetArchitecture();
711 bool lp64 = (arch.GetMachine() == llvm::Triple::aarch64 ||
712 arch.GetMachine() == llvm::Triple::ppc64 ||
713 arch.GetMachine() == llvm::Triple::x86_64);
714 bool have_prstatus = false;
715 bool have_prpsinfo = false;
716 ThreadData thread_data;
717 for (const auto &note : notes) {
718 if (note.info.n_name != "FreeBSD")
719 continue;
720
721 if ((note.info.n_type == ELF::NT_PRSTATUS && have_prstatus) ||
722 (note.info.n_type == ELF::NT_PRPSINFO && have_prpsinfo)) {
723 assert(thread_data.gpregset.GetByteSize() > 0);
724 // Add the new thread to thread list
725 m_thread_data.push_back(thread_data);
726 thread_data = ThreadData();
727 have_prstatus = false;
728 have_prpsinfo = false;
729 }
730
731 switch (note.info.n_type) {
732 case ELF::NT_PRSTATUS:
733 have_prstatus = true;
734 ParseFreeBSDPrStatus(thread_data, note.data, lp64);
735 break;
736 case ELF::NT_PRPSINFO:
737 have_prpsinfo = true;
738 ParseFreeBSDPrPsInfo(*this, note.data, lp64);
739 break;
740 case ELF::NT_FREEBSD_THRMISC: {
741 lldb::offset_t offset = 0;
742 thread_data.name = note.data.GetCStr(&offset, 20);
743 break;
744 }
745 case ELF::NT_FREEBSD_PROCSTAT_AUXV:
746 // FIXME: FreeBSD sticks an int at the beginning of the note
747 m_auxv = DataExtractor(note.data, 4, note.data.GetByteSize() - 4);
748 break;
749 default:
750 thread_data.notes.push_back(note);
751 break;
752 }
753 }
754 if (!have_prstatus) {
755 return llvm::createStringError(
756 "Could not find NT_PRSTATUS note in core file.");
757 }
758 m_thread_data.push_back(thread_data);
759 return llvm::Error::success();
760}
761
762/// NetBSD specific Thread context from PT_NOTE segment
763///
764/// NetBSD ELF core files use notes to provide information about
765/// the process's state. The note name is "NetBSD-CORE" for
766/// information that is global to the process, and "NetBSD-CORE@nn",
767/// where "nn" is the lwpid of the LWP that the information belongs
768/// to (such as register state).
769///
770/// NetBSD uses the following note identifiers:
771///
772/// ELF_NOTE_NETBSD_CORE_PROCINFO (value 1)
773/// Note is a "netbsd_elfcore_procinfo" structure.
774/// ELF_NOTE_NETBSD_CORE_AUXV (value 2; since NetBSD 8.0)
775/// Note is an array of AuxInfo structures.
776///
777/// NetBSD also uses ptrace(2) request numbers (the ones that exist in
778/// machine-dependent space) to identify register info notes. The
779/// info in such notes is in the same format that ptrace(2) would
780/// export that information.
781///
782/// For more information see /usr/include/sys/exec_elf.h
783///
784llvm::Error ProcessElfCore::parseNetBSDNotes(llvm::ArrayRef<CoreNote> notes) {
785 ThreadData thread_data;
786 bool had_nt_regs = false;
787
788 // To be extracted from struct netbsd_elfcore_procinfo
789 // Used to sanity check of the LWPs of the process
790 uint32_t nlwps = 0;
791 uint32_t signo = 0; // killing signal
792 uint32_t siglwp = 0; // LWP target of killing signal
793 uint32_t pr_pid = 0;
794
795 for (const auto &note : notes) {
796 llvm::StringRef name = note.info.n_name;
797
798 if (name == "NetBSD-CORE") {
799 if (note.info.n_type == NETBSD::NT_PROCINFO) {
800 llvm::Error error = ParseNetBSDProcInfo(note.data, nlwps, signo,
801 siglwp, pr_pid);
802 if (error)
803 return error;
804 SetID(pr_pid);
805 } else if (note.info.n_type == NETBSD::NT_AUXV) {
806 m_auxv = note.data;
807 }
808 } else if (name.consume_front("NetBSD-CORE@")) {
809 lldb::tid_t tid;
810 if (name.getAsInteger(10, tid))
811 return llvm::createStringError(
812 "Error parsing NetBSD core(5) notes: Cannot convert LWP ID "
813 "to integer");
814
815 switch (GetArchitecture().GetMachine()) {
816 case llvm::Triple::aarch64: {
817 // Assume order PT_GETREGS, PT_GETFPREGS
818 if (note.info.n_type == NETBSD::AARCH64::NT_REGS) {
819 // If this is the next thread, push the previous one first.
820 if (had_nt_regs) {
821 m_thread_data.push_back(thread_data);
822 thread_data = ThreadData();
823 had_nt_regs = false;
824 }
825
826 thread_data.gpregset = note.data;
827 thread_data.tid = tid;
828 if (thread_data.gpregset.GetByteSize() == 0)
829 return llvm::createStringError(
830 "Could not find general purpose registers note in core file.");
831 had_nt_regs = true;
832 } else if (note.info.n_type == NETBSD::AARCH64::NT_FPREGS) {
833 if (!had_nt_regs || tid != thread_data.tid)
834 return llvm::createStringError(
835 "Error parsing NetBSD core(5) notes: Unexpected order "
836 "of NOTEs PT_GETFPREG before PT_GETREG");
837 thread_data.notes.push_back(note);
838 }
839 } break;
840 case llvm::Triple::x86: {
841 // Assume order PT_GETREGS, PT_GETFPREGS
842 if (note.info.n_type == NETBSD::I386::NT_REGS) {
843 // If this is the next thread, push the previous one first.
844 if (had_nt_regs) {
845 m_thread_data.push_back(thread_data);
846 thread_data = ThreadData();
847 had_nt_regs = false;
848 }
849
850 thread_data.gpregset = note.data;
851 thread_data.tid = tid;
852 if (thread_data.gpregset.GetByteSize() == 0)
853 return llvm::createStringError(
854 "Could not find general purpose registers note in core file.");
855 had_nt_regs = true;
856 } else if (note.info.n_type == NETBSD::I386::NT_FPREGS) {
857 if (!had_nt_regs || tid != thread_data.tid)
858 return llvm::createStringError(
859 "Error parsing NetBSD core(5) notes: Unexpected order "
860 "of NOTEs PT_GETFPREG before PT_GETREG");
861 thread_data.notes.push_back(note);
862 }
863 } break;
864 case llvm::Triple::x86_64: {
865 // Assume order PT_GETREGS, PT_GETFPREGS
866 if (note.info.n_type == NETBSD::AMD64::NT_REGS) {
867 // If this is the next thread, push the previous one first.
868 if (had_nt_regs) {
869 m_thread_data.push_back(thread_data);
870 thread_data = ThreadData();
871 had_nt_regs = false;
872 }
873
874 thread_data.gpregset = note.data;
875 thread_data.tid = tid;
876 if (thread_data.gpregset.GetByteSize() == 0)
877 return llvm::createStringError(
878 "Could not find general purpose registers note in core file.");
879 had_nt_regs = true;
880 } else if (note.info.n_type == NETBSD::AMD64::NT_FPREGS) {
881 if (!had_nt_regs || tid != thread_data.tid)
882 return llvm::createStringError(
883 "Error parsing NetBSD core(5) notes: Unexpected order "
884 "of NOTEs PT_GETFPREG before PT_GETREG");
885 thread_data.notes.push_back(note);
886 }
887 } break;
888 default:
889 break;
890 }
891 }
892 }
893
894 // Push the last thread.
895 if (had_nt_regs)
896 m_thread_data.push_back(thread_data);
897
898 if (m_thread_data.empty())
899 return llvm::createStringError(
900 "Error parsing NetBSD core(5) notes: No threads information "
901 "specified in notes");
902
903 if (m_thread_data.size() != nlwps)
904 return llvm::createStringError(
905 "Error parsing NetBSD core(5) notes: Mismatch between the number "
906 "of LWPs in netbsd_elfcore_procinfo and the number of LWPs specified "
907 "by MD notes");
908
909 // Signal targeted at the whole process.
910 if (siglwp == 0) {
911 for (auto &data : m_thread_data)
912 data.signo = signo;
913 }
914 // Signal destined for a particular LWP.
915 else {
916 bool passed = false;
917
918 for (auto &data : m_thread_data) {
919 if (data.tid == siglwp) {
920 data.signo = signo;
921 passed = true;
922 break;
923 }
924 }
925
926 if (!passed)
927 return llvm::createStringError(
928 "Error parsing NetBSD core(5) notes: Signal passed to unknown LWP");
929 }
930
931 return llvm::Error::success();
932}
933
934llvm::Error ProcessElfCore::parseOpenBSDNotes(llvm::ArrayRef<CoreNote> notes) {
935 ThreadData thread_data = {};
936 for (const auto &note : notes) {
937 // OpenBSD per-thread information is stored in notes named "OpenBSD@nnn" so
938 // match on the initial part of the string.
939 if (!llvm::StringRef(note.info.n_name).starts_with("OpenBSD"))
940 continue;
941
942 switch (note.info.n_type) {
944 ParseOpenBSDProcInfo(thread_data, note.data);
945 break;
946 case OPENBSD::NT_AUXV:
947 m_auxv = note.data;
948 break;
949 case OPENBSD::NT_REGS:
950 thread_data.gpregset = note.data;
951 break;
952 default:
953 thread_data.notes.push_back(note);
954 break;
955 }
956 }
957 if (thread_data.gpregset.GetByteSize() == 0) {
958 return llvm::createStringError(
959 "Could not find general purpose registers note in core file.");
960 }
961 m_thread_data.push_back(thread_data);
962 return llvm::Error::success();
963}
964
965/// A description of a linux process usually contains the following NOTE
966/// entries:
967/// - NT_PRPSINFO - General process information like pid, uid, name, ...
968/// - NT_SIGINFO - Information about the signal that terminated the process
969/// - NT_AUXV - Process auxiliary vector
970/// - NT_FILE - Files mapped into memory
971///
972/// Additionally, for each thread in the process the core file will contain at
973/// least the NT_PRSTATUS note, containing the thread id and general purpose
974/// registers. It may include additional notes for other register sets (floating
975/// point and vector registers, ...). The tricky part here is that some of these
976/// notes have "CORE" in their owner fields, while other set it to "LINUX".
977llvm::Error ProcessElfCore::parseLinuxNotes(llvm::ArrayRef<CoreNote> notes) {
978 const ArchSpec &arch = GetArchitecture();
979 bool have_prstatus = false;
980 bool have_prpsinfo = false;
981 ThreadData thread_data;
982 for (const auto &note : notes) {
983 if (note.info.n_name != "CORE" && note.info.n_name != "LINUX")
984 continue;
985
986 if ((note.info.n_type == ELF::NT_PRSTATUS && have_prstatus) ||
987 (note.info.n_type == ELF::NT_PRPSINFO && have_prpsinfo)) {
988 assert(thread_data.gpregset.GetByteSize() > 0);
989 // Add the new thread to thread list
990 m_thread_data.push_back(thread_data);
991 thread_data = ThreadData();
992 have_prstatus = false;
993 have_prpsinfo = false;
994 }
995
996 switch (note.info.n_type) {
997 case ELF::NT_PRSTATUS: {
998 have_prstatus = true;
999 ELFLinuxPrStatus prstatus;
1000 Status status = prstatus.Parse(note.data, arch);
1001 if (status.Fail())
1002 return status.ToError();
1003 thread_data.prstatus_sig = prstatus.pr_cursig;
1004 thread_data.tid = prstatus.pr_pid;
1005 uint32_t header_size = ELFLinuxPrStatus::GetSize(arch);
1006 size_t len = note.data.GetByteSize() - header_size;
1007 thread_data.gpregset = DataExtractor(note.data, header_size, len);
1008 break;
1009 }
1010 case ELF::NT_PRPSINFO: {
1011 have_prpsinfo = true;
1012 ELFLinuxPrPsInfo prpsinfo;
1013 Status status = prpsinfo.Parse(note.data, arch);
1014 if (status.Fail())
1015 return status.ToError();
1016 thread_data.name.assign (prpsinfo.pr_fname, strnlen (prpsinfo.pr_fname, sizeof (prpsinfo.pr_fname)));
1017 SetID(prpsinfo.pr_pid);
1018 m_executable_name = thread_data.name;
1019 auto core_arg = llvm::StringRef(prpsinfo.pr_psargs,
1020 strnlen(prpsinfo.pr_psargs,
1021 sizeof(prpsinfo.pr_psargs)))
1022 .str();
1023 // pr_psargs's char array used to represent arguments is only 80 character
1024 // long (\0 included), for a total of 79.
1025 // We set core_arg's m_might_be_truncated = true if its size
1026 // is the maximum (79).
1028 CoreArgs(core_arg, /*might_be_truncated=*/core_arg.size() ==
1029 sizeof(prpsinfo.pr_psargs) - 1);
1030 break;
1031 }
1032 case ELF::NT_SIGINFO: {
1033 lldb::offset_t size = note.data.GetByteSize();
1034 lldb::offset_t offset = 0;
1035 const char *bytes =
1036 static_cast<const char *>(note.data.GetData(&offset, size));
1037 thread_data.siginfo_bytes = llvm::StringRef(bytes, size);
1038 break;
1039 }
1040 case ELF::NT_FILE: {
1041 m_nt_file_entries.clear();
1042 lldb::offset_t offset = 0;
1043 const uint64_t count = note.data.GetAddress(&offset);
1044 note.data.GetAddress(&offset); // Skip page size
1045 for (uint64_t i = 0; i < count; ++i) {
1046 NT_FILE_Entry entry;
1047 entry.start = note.data.GetAddress(&offset);
1048 entry.end = note.data.GetAddress(&offset);
1049 entry.file_ofs = note.data.GetAddress(&offset);
1050 m_nt_file_entries.push_back(entry);
1051 }
1052 for (uint64_t i = 0; i < count; ++i) {
1053 const char *path = note.data.GetCStr(&offset);
1054 if (path && path[0])
1055 m_nt_file_entries[i].path.assign(path);
1056 }
1057 break;
1058 }
1059 case ELF::NT_AUXV:
1060 m_auxv = note.data;
1061 break;
1062 default:
1063 thread_data.notes.push_back(note);
1064 break;
1065 }
1066 }
1067 // Add last entry in the note section
1068 if (have_prstatus)
1069 m_thread_data.push_back(thread_data);
1070 return llvm::Error::success();
1071}
1072
1073/// Parse Thread context from PT_NOTE segment and store it in the thread list
1074/// A note segment consists of one or more NOTE entries, but their types and
1075/// meaning differ depending on the OS.
1077 const elf::ELFProgramHeader &segment_header,
1078 const DataExtractor &segment_data) {
1079 assert(segment_header.p_type == llvm::ELF::PT_NOTE);
1080
1081 auto notes_or_error = parseSegment(segment_data);
1082 if(!notes_or_error)
1083 return notes_or_error.takeError();
1084 switch (GetArchitecture().GetTriple().getOS()) {
1085 case llvm::Triple::FreeBSD:
1086 return parseFreeBSDNotes(*notes_or_error);
1087 case llvm::Triple::Linux:
1088 return parseLinuxNotes(*notes_or_error);
1089 case llvm::Triple::NetBSD:
1090 return parseNetBSDNotes(*notes_or_error);
1091 case llvm::Triple::OpenBSD:
1092 return parseOpenBSDNotes(*notes_or_error);
1093 default:
1094 // Treat bare-metal 32-bit RISC-V like Linux.
1095 if (GetTarget().GetArchitecture().GetMachine() == llvm::Triple::riscv32 &&
1096 GetTarget().GetArchitecture().GetTriple().getOS() ==
1097 llvm::Triple::UnknownOS)
1098 return parseLinuxNotes(*notes_or_error);
1099 else
1100 return llvm::createStringError(
1101 "don't know how to parse core file: unsupported OS");
1102 }
1103}
1104
1106 UUID invalid_uuid;
1107 const uint32_t addr_size = GetAddressByteSize();
1108 const size_t elf_header_size = addr_size == 4 ? sizeof(llvm::ELF::Elf32_Ehdr)
1109 : sizeof(llvm::ELF::Elf64_Ehdr);
1110
1111 std::vector<uint8_t> elf_header_bytes;
1112 elf_header_bytes.resize(elf_header_size);
1113 Status error;
1114 size_t byte_read =
1115 ReadMemory(address, elf_header_bytes.data(), elf_header_size, error);
1116 if (byte_read != elf_header_size ||
1117 !elf::ELFHeader::MagicBytesMatch(elf_header_bytes.data()))
1118 return invalid_uuid;
1119 DataExtractor elf_header_data(elf_header_bytes.data(), elf_header_size,
1120 GetByteOrder(), addr_size);
1121 lldb::offset_t offset = 0;
1122
1123 elf::ELFHeader elf_header;
1124 elf_header.Parse(elf_header_data, &offset);
1125
1126 const lldb::addr_t ph_addr = address + elf_header.e_phoff;
1127
1128 std::vector<uint8_t> ph_bytes;
1129 ph_bytes.resize(elf_header.e_phentsize);
1130 lldb::addr_t base_addr = 0;
1131 bool found_first_load_segment = false;
1132 for (unsigned int i = 0; i < elf_header.e_phnum; ++i) {
1133 byte_read = ReadMemory(ph_addr + i * elf_header.e_phentsize,
1134 ph_bytes.data(), elf_header.e_phentsize, error);
1135 if (byte_read != elf_header.e_phentsize)
1136 break;
1137 DataExtractor program_header_data(ph_bytes.data(), elf_header.e_phentsize,
1138 GetByteOrder(), addr_size);
1139 offset = 0;
1140 elf::ELFProgramHeader program_header;
1141 program_header.Parse(program_header_data, &offset);
1142 if (program_header.p_type == llvm::ELF::PT_LOAD &&
1143 !found_first_load_segment) {
1144 base_addr = program_header.p_vaddr;
1145 found_first_load_segment = true;
1146 }
1147 if (program_header.p_type != llvm::ELF::PT_NOTE)
1148 continue;
1149
1150 std::vector<uint8_t> note_bytes;
1151 note_bytes.resize(program_header.p_memsz);
1152
1153 // We need to slide the address of the p_vaddr as these values don't get
1154 // relocated in memory.
1155 const lldb::addr_t vaddr = program_header.p_vaddr + address - base_addr;
1156 byte_read =
1157 ReadMemory(vaddr, note_bytes.data(), program_header.p_memsz, error);
1158 if (byte_read != program_header.p_memsz)
1159 continue;
1160 DataExtractor segment_data(note_bytes.data(), note_bytes.size(),
1161 GetByteOrder(), addr_size);
1162 auto notes_or_error = parseSegment(segment_data);
1163 if (!notes_or_error) {
1164 llvm::consumeError(notes_or_error.takeError());
1165 return invalid_uuid;
1166 }
1167 for (const CoreNote &note : *notes_or_error) {
1168 if (note.info.n_namesz == 4 &&
1169 note.info.n_type == llvm::ELF::NT_GNU_BUILD_ID &&
1170 "GNU" == note.info.n_name &&
1171 note.data.ValidOffsetForDataOfSize(0, note.info.n_descsz))
1172 return UUID(note.data.GetData().take_front(note.info.n_descsz));
1173 }
1174 }
1175 return invalid_uuid;
1176}
1177
1180 DoLoadCore();
1181 return m_thread_data.size();
1182}
1183
1185 ArchSpec arch = m_core_module_sp->GetObjectFile()->GetArchitecture();
1186
1187 ArchSpec target_arch = GetTarget().GetArchitecture();
1188 arch.MergeFrom(target_arch);
1189
1190 // On MIPS there is no way to differentiate betwenn 32bit and 64bit core
1191 // files and this information can't be merged in from the target arch so we
1192 // fail back to unconditionally returning the target arch in this config.
1193 if (target_arch.IsMIPS()) {
1194 return target_arch;
1195 }
1196
1197 return arch;
1198}
1199
1201 assert(m_auxv.GetByteSize() == 0 ||
1202 (m_auxv.GetByteOrder() == GetByteOrder() &&
1203 m_auxv.GetAddressByteSize() == GetAddressByteSize()));
1204 return DataExtractor(m_auxv);
1205}
1206std::optional<Process::CoreArgs> ProcessElfCore::GetCoreFileArgs() {
1207 if (m_process_args.empty())
1208 return std::nullopt;
1209 return m_process_args;
1210}
1211
1213 info.Clear();
1214 info.SetProcessID(GetID());
1217 if (module_sp) {
1218 const bool add_exe_file_as_first_arg = false;
1219 info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
1220 add_exe_file_as_first_arg);
1221 }
1222 info.SetArguments(m_process_args.as_args(), /*first_arg_is_executable=*/true);
1223 return true;
1224}
1225
1226/// Find the NT_FILE entry that contains an address.
1227std::optional<ProcessElfCore::NT_FILE_Entry>
1229 for (const NT_FILE_Entry &file_entry : m_nt_file_entries) {
1230 if (file_entry.start <= addr && addr < file_entry.end)
1231 return file_entry;
1232 }
1233 return std::nullopt;
1234}
1235
1236std::optional<ProcessElfCore::NT_FILE_Entry>
1238 /// This method will search for the first NT_FILE entry that contains the
1239 /// executable's ELF header. We use the AUXV_AT_PHDR from the aux vector to
1240 /// find the address of the main executable's program headers and then find
1241 /// the NT_FILE entry that contains this address.
1242 ///
1243 /// Previously we would try to find the first NT_FILE entry that had a path
1244 /// that ended with the executable name found in the NT_PRPSINFO note, but
1245 /// this basename can be the name of a symlink and not the actual resolved
1246 /// executable file found in the NT_FILE entry so this could fail for cases
1247 /// where a symlink was used to launch the program, and that symlink's
1248 /// base name was different from the resolved executable file's name in
1249 /// the NT_FILE entry.
1250 if (m_nt_file_entries.empty())
1251 return std::nullopt;
1252 // The AUX vector has the load address of the program headers from the main
1253 // executable as the value for AUXV_AT_PHDR. We can use this value to find
1254 // the NT_FILE entry that contains this address and this will locate the main
1255 // executable's mapping that contains the ELF header.
1256 AuxVector aux_vector(m_auxv);
1257 if (std::optional<uint64_t> opt_value =
1259 if (std::optional<NT_FILE_Entry> nt =
1261 return *nt;
1262 }
1263 // Fall back to trying to find the first NT_FILE entry that contains the entry
1264 // point address.
1265 if (std::optional<uint64_t> opt_value =
1267 if (std::optional<NT_FILE_Entry> nt =
1269 return *nt;
1270 }
1271 return std::nullopt;
1272}
static llvm::raw_ostream & error(Stream &strm)
static llvm::raw_ostream & note(Stream &strm)
#define LLDB_LOGF(log,...)
Definition Log.h:378
#define LLDB_PLUGIN_DEFINE(PluginName)
static FileSpec CreateFileSpecFromPath(llvm::StringRef path)
Correctly create a FileSpec from a path found in a core file.
static void ParseOpenBSDProcInfo(ThreadData &thread_data, const DataExtractor &data)
static void ParseFreeBSDPrPsInfo(ProcessElfCore &process, const DataExtractor &data, bool lp64)
static void ParseFreeBSDPrStatus(ThreadData &thread_data, const DataExtractor &data, bool lp64)
static llvm::Error ParseNetBSDProcInfo(const DataExtractor &data, uint32_t &cpi_nlwps, uint32_t &cpi_signo, uint32_t &cpi_siglwp, uint32_t &cpi_pid)
@ AUXV_AT_EXECFN
Filename of executable.
Definition AuxVector.h:61
@ AUXV_AT_PHDR
Program headers.
Definition AuxVector.h:30
@ AUXV_AT_ENTRY
Program entry point.
Definition AuxVector.h:36
std::optional< uint64_t > GetAuxValue(enum EntryType entry_type) const
Definition AuxVector.cpp:34
static llvm::StringRef GetPluginNameStatic()
static llvm::StringRef GetPluginNameStatic()
Generic COFF object file reader.
lldb_private::DataExtractor GetSegmentData(const elf::ELFProgramHeader &H)
llvm::ArrayRef< elf::ELFProgramHeader > ProgramHeaders()
std::vector< NT_FILE_Entry > m_nt_file_entries
std::optional< NT_FILE_Entry > GetNTFileEntryForExecutableELFHeader()
Intelligently find the NT_FILE entry for the executable's ELF header.
lldb_private::UUID FindModuleUUID(const llvm::StringRef path) override
lldb::addr_t GetImageInfoAddress() override
Get the image information address for the current process.
lldb::addr_t AddAddressRangeFromMemoryTagSegment(const elf::ELFProgramHeader &header)
lldb_private::DataExtractor m_auxv
llvm::Error parseLinuxNotes(llvm::ArrayRef< lldb_private::CoreNote > notes)
A description of a linux process usually contains the following NOTE entries:
llvm::Error ParseThreadContextsFromNoteSegment(const elf::ELFProgramHeader &segment_header, const lldb_private::DataExtractor &segment_data)
Parse Thread context from PT_NOTE segment and store it in the thread list A note segment consists of ...
void UpdateBuildIdForNTFileEntries()
std::vector< ThreadData > m_thread_data
lldb_private::Range< lldb::addr_t, lldb::addr_t > FileRange
static void Initialize()
bool DoUpdateThreadList(lldb_private::ThreadList &old_thread_list, lldb_private::ThreadList &new_thread_list) override
Update the thread list following process plug-in's specific logic.
size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size, lldb_private::Status &error) override
Read of memory from a process.
VMRangeToPermissions m_core_range_infos
static llvm::StringRef GetPluginDescriptionStatic()
std::unordered_map< std::string, lldb_private::UUID > m_uuids
llvm::Expected< std::vector< lldb_private::CoreNote > > parseSegment(const lldb_private::DataExtractor &segment)
lldb::addr_t AddAddressRangeFromLoadSegment(const elf::ELFProgramHeader &header)
~ProcessElfCore() override
bool GetMainExecutableModuleSpec(lldb_private::ModuleSpec &exe_spec)
llvm::Error parseFreeBSDNotes(llvm::ArrayRef< lldb_private::CoreNote > notes)
lldb_private::UUID FindBuidIdInCoreMemory(lldb::addr_t address)
VMRangeToFileOffset m_core_aranges
lldb_private::Status DoGetMemoryRegionInfo(lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &region_info) override
DoGetMemoryRegionInfo is called by GetMemoryRegionInfo after it has removed non address bits from loa...
Process::CoreArgs m_process_args
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.
VMRangeToFileOffset m_core_tag_ranges
llvm::Error parseNetBSDNotes(llvm::ArrayRef< lldb_private::CoreNote > notes)
NetBSD specific Thread context from PT_NOTE segment.
lldb_private::Status DoLoadCore() override
static void Terminate()
ProcessElfCore(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const lldb_private::FileSpec &core_file)
lldb_private::DynamicLoader * GetDynamicLoader() override
Get the dynamic loader plug-in for this process.
llvm::Expected< std::vector< lldb::addr_t > > ReadMemoryTags(lldb::addr_t addr, size_t len) override
Read memory tags for the range addr to addr+len.
lldb_private::DataExtractor GetAuxvData() override
bool IsAlive() override
Check if a process is still alive.
uint32_t GetNumThreadContexts()
std::string m_executable_name
static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const lldb_private::FileSpec *crash_file_path, bool can_connect)
llvm::Error parseOpenBSDNotes(llvm::ArrayRef< lldb_private::CoreNote > notes)
void RefreshStateAfterStop() override
Currently called as part of ShouldStop.
lldb_private::ArchSpec GetArchitecture()
bool CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) override
Check if a plug-in instance can debug the file in module.
std::optional< Process::CoreArgs > GetCoreFileArgs() override
Provide arguments of a command that triggered a core dump.
bool GetProcessInfo(lldb_private::ProcessInstanceInfo &info) override
static llvm::StringRef GetPluginNameStatic()
lldb::ModuleSP m_core_module_sp
std::optional< NT_FILE_Entry > GetNTFileEntryContainingAddress(lldb::addr_t addr)
Find the NT_FILE entry that contains an address.
lldb_private::Status DoDestroy() override
A section + offset based address class.
Definition Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition Address.cpp:301
bool IsValid() const
Check if the object state is valid.
Definition Address.h:355
An architecture specification class.
Definition ArchSpec.h:32
void MergeFrom(const ArchSpec &other)
Merges fields from another ArchSpec into this ArchSpec.
Definition ArchSpec.cpp:810
bool IsMIPS() const
if MIPS architecture return true.
Definition ArchSpec.cpp:564
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition ArchSpec.cpp:682
An data extractor class.
virtual uint64_t GetByteSize() const
Get the number of bytes contained in this object.
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
A plug-in interface definition class for dynamic loaders.
static DynamicLoader * FindPlugin(Process *process, llvm::StringRef plugin_name)
Find a dynamic loader plugin for a given process.
A file utility class.
Definition FileSpec.h:57
static std::optional< Style > GuessPathStyle(llvm::StringRef absolute_path)
Attempt to guess path style for a given path string.
Definition FileSpec.cpp:310
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:374
llvm::sys::path::Style Style
Definition FileSpec.h:59
static FileSystem & Instance()
std::shared_ptr< DataBuffer > CreateDataBuffer(const llvm::Twine &path, uint64_t size=0, uint64_t offset=0)
Create memory buffer from path.
A class to manage flags.
Definition Flags.h:22
bool Test(ValueType bit) const
Test a single flag bit.
Definition Flags.h:96
MemoryRegionInfo & SetMemoryTagged(LazyBool val)
virtual llvm::Expected< std::vector< lldb::addr_t > > UnpackTagsFromCoreFileSegment(CoreReaderFn reader, lldb::addr_t tag_segment_virtual_address, lldb::addr_t tag_segment_data_address, lldb::addr_t addr, size_t len) const =0
static Status GetSharedModule(const ModuleSpec &module_spec, lldb::ModuleSP &module_sp, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr, bool invoke_locate_callback=true)
FileSpec & GetPlatformFileSpec()
Definition ModuleSpec.h:69
FileSpec & GetFileSpec()
Definition ModuleSpec.h:57
ArchSpec & GetArchitecture()
Definition ModuleSpec.h:93
void SetTarget(lldb::TargetSP target)
Set the target to be used when resolving a module.
Definition ModuleSpec.h:141
static lldb::ModuleSP CreateModuleFromObjectFile(Args &&...args)
Definition Module.h:136
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
virtual lldb_private::Address GetImageInfoAddress(Target *target)
Similar to Process::GetImageInfoAddress().
Definition ObjectFile.h:442
@ eTypeCoreFile
A core file that has a checkpoint of a program's execution state.
Definition ObjectFile.h:53
size_t CopyData(lldb::offset_t offset, size_t length, void *dst) const
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
PostMortemProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const FileSpec &core_file)
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
void SetArchitecture(const ArchSpec &arch)
Definition ProcessInfo.h:64
void SetArguments(const Args &args, bool first_arg_is_executable)
void SetProcessID(lldb::pid_t pid)
Definition ProcessInfo.h:68
lldb::pid_t GetID() const
Returns the pid of the process or LLDB_INVALID_PROCESS_ID if there is no known pid.
Definition Process.h:541
void SetUnixSignals(lldb::UnixSignalsSP &&signals_sp)
Definition Process.cpp:3902
size_t ReadCStringFromMemory(lldb::addr_t vm_addr, char *cstr, size_t cstr_max_len, Status &error)
Read a NULL terminated C string from memory.
Definition Process.cpp:2328
void SetCanJIT(bool can_jit)
Sets whether executing JIT-compiled code in this process is possible.
Definition Process.cpp:2742
lldb::DynamicLoaderUP m_dyld_up
Definition Process.h:3517
llvm::Expected< const MemoryTagManager * > GetMemoryTagManager()
If this architecture and process supports memory tagging, return a tag manager that can be used to ma...
Definition Process.cpp:6722
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:3912
llvm::Expected< lldb::ModuleSP > ReadModuleFromMemory(const FileSpec &file_spec, lldb::addr_t header_addr, size_t size_to_read=512)
Creates and populates a module using an in-memory object file.
Definition Process.cpp:2779
void SetID(lldb::pid_t new_pid)
Sets the stored pid.
Definition Process.h:546
uint32_t GetAddressByteSize() const
Definition Process.cpp:3916
virtual void Finalize(bool destructing)
This object is about to be destroyed, do any necessary cleanup.
Definition Process.cpp:562
ThreadList m_thread_list
The threads for this process as the user will see them.
Definition Process.h:3490
const lldb::UnixSignalsSP & GetUnixSignals()
Definition Process.cpp:3907
const lldb::ABISP & GetABI()
Definition Process.cpp:1483
friend class ThreadList
Definition Process.h:364
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1256
RangeData< lldb::addr_t, lldb::addr_t, FileRange > Entry
Definition RangeMap.h:462
An error handling class.
Definition Status.h:118
llvm::Error ToError() const
FIXME: Replace all uses with takeError() instead.
Definition Status.cpp:138
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
bool Fail() const
Test for error condition.
Definition Status.cpp:293
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
Definition Status.cpp:136
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:2409
bool SetArchitecture(const ArchSpec &arch_spec, bool set_platform=false, bool merge=true)
Set the architecture for this target.
Definition Target.cpp:1755
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
Definition Target.cpp:1593
const ArchSpec & GetArchitecture() const
Definition Target.h:1283
void SetExecutableModule(lldb::ModuleSP &module_sp, LoadDependentFiles load_dependent_files=eLoadDependentsDefault)
Set the main executable module.
Definition Target.cpp:1626
void AddThread(const lldb::ThreadSP &thread_sp)
uint32_t GetSize(bool can_update=true)
Represents UUID's of various sizes.
Definition UUID.h:27
std::string GetAsString(llvm::StringRef separator="-") const
Definition UUID.cpp:54
bool IsValid() const
Definition UUID.h:69
static lldb::UnixSignalsSP Create(const ArchSpec &arch)
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:327
std::shared_ptr< lldb_private::ABI > ABISP
std::shared_ptr< lldb_private::Thread > ThreadSP
uint64_t offset_t
Definition lldb-types.h:85
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::Listener > ListenerSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP
uint64_t tid_t
Definition lldb-types.h:84
std::shared_ptr< lldb_private::Module > ModuleSP
lldb_private::Status Parse(const lldb_private::DataExtractor &data, const lldb_private::ArchSpec &arch)
static size_t GetSize(const lldb_private::ArchSpec &arch)
lldb_private::Status Parse(const lldb_private::DataExtractor &data, const lldb_private::ArchSpec &arch)
lldb::addr_t file_ofs
lldb::addr_t end
lldb::addr_t start
llvm::StringRef siginfo_bytes
lldb::tid_t tid
std::string name
lldb_private::DataExtractor gpregset
std::vector< lldb_private::CoreNote > notes
Generic representation of an ELF file header.
Definition ELFHeader.h:56
elf_off e_phoff
File offset of program header table.
Definition ELFHeader.h:59
elf_half e_phentsize
Size of a program header table entry.
Definition ELFHeader.h:66
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...
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...
elf_word e_phnum
Number of program header entries.
Definition ELFHeader.h:75
elf_word e_version
Version of object file (always 1).
Definition ELFHeader.h:62
unsigned char e_ident[llvm::ELF::EI_NIDENT]
ELF file identification.
Definition ELFHeader.h:57
elf_half e_type
Object file type.
Definition ELFHeader.h:63
Generic representation of an ELF program header.
Definition ELFHeader.h:192
bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
Parse an ELFProgramHeader entry from the given DataExtractor starting at position offset.
elf_word p_flags
Segment attributes.
Definition ELFHeader.h:194
elf_xword p_filesz
Byte size of the segment in file.
Definition ELFHeader.h:198
elf_off p_offset
Start of segment from beginning of file.
Definition ELFHeader.h:195
elf_addr p_vaddr
Virtual address of segment in memory.
Definition ELFHeader.h:196
elf_xword p_memsz
Byte size of the segment in memory.
Definition ELFHeader.h:199
elf_word p_type
Type of program segment.
Definition ELFHeader.h:193
bool Contains(BaseType r) const
Definition RangeMap.h:93
BaseType GetRangeBase() const
Definition RangeMap.h:45
void SetRangeEnd(BaseType end)
Definition RangeMap.h:80
SizeType GetByteSize() const
Definition RangeMap.h:87
void SetRangeBase(BaseType b)
Set the start value for the range, and keep the same size.
Definition RangeMap.h:48
BaseType GetRangeEnd() const
Definition RangeMap.h:78