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