12#include "llvm/Support/MathExtras.h"
13#include "llvm/Support/Threading.h"
55 return "Mach-O core file debugging plug-in.";
67 if (crash_file && !can_connect) {
68 const size_t header_size =
sizeof(llvm::MachO::mach_header);
70 crash_file->
GetPath(), header_size, 0);
71 if (data_sp && data_sp->GetByteSize() == header_size) {
75 llvm::MachO::mach_header mach_header;
77 if (mach_header.filetype == llvm::MachO::MH_CORE)
78 process_sp = std::make_shared<ProcessMachCore>(target_sp, listener_sp,
87 bool plugin_specified_by_name) {
88 if (plugin_specified_by_name)
99 nullptr,
nullptr,
nullptr));
115 m_core_range_infos(), m_core_module_sp(),
132 Log *log(
GetLog(LLDBLog::DynamicLoader | LLDBLog::Process));
133 llvm::MachO::mach_header header;
138 if (header.magic == llvm::MachO::MH_CIGAM ||
139 header.magic == llvm::MachO::MH_CIGAM_64) {
140 header.magic = llvm::byteswap<uint32_t>(header.magic);
141 header.cputype = llvm::byteswap<uint32_t>(header.cputype);
142 header.cpusubtype = llvm::byteswap<uint32_t>(header.cpusubtype);
143 header.filetype = llvm::byteswap<uint32_t>(header.filetype);
144 header.ncmds = llvm::byteswap<uint32_t>(header.ncmds);
145 header.sizeofcmds = llvm::byteswap<uint32_t>(header.sizeofcmds);
146 header.flags = llvm::byteswap<uint32_t>(header.flags);
149 if (header.magic == llvm::MachO::MH_MAGIC ||
150 header.magic == llvm::MachO::MH_MAGIC_64) {
155 switch (header.filetype) {
156 case llvm::MachO::MH_DYLINKER:
158 "ProcessMachCore::%s found a user "
159 "process dyld binary image at 0x%" PRIx64,
164 case llvm::MachO::MH_EXECUTE:
167 if ((header.flags & llvm::MachO::MH_DYLDLINK) == 0) {
169 "ProcessMachCore::%s found a mach "
170 "kernel binary image at 0x%" PRIx64,
187 bool ranges_are_sorted =
true;
189 for (uint32_t i = 0; i < num_sections; ++i) {
195 section_vm_addr, section->
GetByteSize(), file_range);
197 if (vm_addr > section_vm_addr)
198 ranges_are_sorted =
false;
204 last_entry->
data.GetRangeEnd() == range_entry.
data.GetRangeBase()) {
206 last_entry->
data.SetRangeEnd(range_entry.
data.GetRangeEnd());
215 if (permissions == 0)
216 permissions = lldb::ePermissionsReadable | lldb::ePermissionsExecutable;
218 section_vm_addr, section->
GetByteSize(), permissions));
221 if (!ranges_are_sorted) {
233 Log *log(
GetLog(LLDBLog::DynamicLoader | LLDBLog::Process));
236 uint64_t lowmem_uuid_addresses[] = {0x2000204, 0x1000204, 0x1000020, 0x4204,
237 0x1204, 0x1020, 0x4020, 0xc00,
240 for (uint64_t addr : lowmem_uuid_addresses) {
243 if (core_memory_entry) {
247 if (bytes_left >= 32) {
250 core_memory_entry->
data.GetRangeBase() + offset, 4, &strbuf) &&
251 strncmp(
"uuid", (
char *)&strbuf, 4) == 0) {
253 if (core_objfile->
CopyData(core_memory_entry->
data.GetRangeBase() +
255 sizeof(
uuid_t), uuid_bytes)) {
259 "ProcessMachCore::LoadBinaryViaLowmemUUID: found "
260 "binary uuid %s at low memory address 0x%" PRIx64,
264 const bool value_is_offset =
true;
265 const bool force_symbol_search =
true;
266 const bool notify =
true;
267 const bool set_address_in_target =
true;
268 const bool allow_memory_image_last_resort =
false;
270 this, llvm::StringRef(), uuid, 0, value_is_offset,
271 force_symbol_search, notify, set_address_in_target,
272 allow_memory_image_last_resort)) {
288 Log *log(
GetLog(LLDBLog::DynamicLoader | LLDBLog::Process));
291 addr_t objfile_binary_value;
292 bool objfile_binary_value_is_offset;
293 UUID objfile_binary_uuid;
299 bool found_binary_spec_in_metadata =
false;
302 objfile_binary_value_is_offset,
303 objfile_binary_uuid, type)) {
305 log->
Printf(
"ProcessMachCore::LoadBinariesViaMetadata: using binary hint "
306 "from 'main bin spec' "
307 "LC_NOTE with UUID %s value 0x%" PRIx64
308 " value is offset %d and type %d",
310 objfile_binary_value, objfile_binary_value_is_offset, type);
312 found_binary_spec_in_metadata =
true;
324 const bool force_symbol_search =
true;
325 const bool notify =
true;
326 const bool set_address_in_target =
true;
327 const bool allow_memory_image_last_resort =
false;
329 this, llvm::StringRef(), objfile_binary_uuid,
330 objfile_binary_value, objfile_binary_value_is_offset,
331 force_symbol_search, notify, set_address_in_target,
332 allow_memory_image_last_resort)) {
346 if (corefile_identifier.find(
"UUID=") != std::string::npos) {
347 size_t p = corefile_identifier.find(
"UUID=") + strlen(
"UUID=");
348 std::string uuid_str = corefile_identifier.substr(p, 36);
351 log->
Printf(
"Got a UUID from LC_IDENT/kern ver str LC_NOTE: %s",
353 found_binary_spec_in_metadata =
true;
355 if (corefile_identifier.find(
"stext=") != std::string::npos) {
356 size_t p = corefile_identifier.find(
"stext=") + strlen(
"stext=");
357 if (corefile_identifier[p] ==
'0' && corefile_identifier[p + 1] ==
'x') {
359 ::strtoul(corefile_identifier.c_str() + p,
nullptr, 16);
361 log->
Printf(
"Got a load address from LC_IDENT/kern ver str "
362 "LC_NOTE: 0x%" PRIx64,
364 found_binary_spec_in_metadata =
true;
370 if (corefile_identifier.find(
"Darwin Kernel") != std::string::npos &&
374 "ProcessMachCore::LoadBinariesViaMetadata: Found kernel binary via "
375 "LC_IDENT/kern ver str LC_NOTE");
377 found_binary_spec_in_metadata =
true;
378 }
else if (ident_uuid.
IsValid()) {
381 const bool value_is_offset =
false;
382 const bool force_symbol_search =
true;
383 const bool notify =
true;
384 const bool set_address_in_target =
true;
385 const bool allow_memory_image_last_resort =
false;
387 this, llvm::StringRef(), ident_uuid, ident_binary_addr,
388 value_is_offset, force_symbol_search, notify,
389 set_address_in_target, allow_memory_image_last_resort)) {
390 found_binary_spec_in_metadata =
true;
398 found_binary_spec_in_metadata =
true;
403 found_binary_spec_in_metadata =
true;
412 return found_binary_spec_in_metadata;
416 Log *log(
GetLog(LLDBLog::DynamicLoader | LLDBLog::Process));
422 std::vector<addr_t> dylds_found;
423 std::vector<addr_t> kernels_found;
426 for (
size_t i = 0; i < num_core_aranges; ++i) {
430 for (
lldb::addr_t section_vm_addr = section_vm_addr_start;
431 section_vm_addr < section_vm_addr_end; section_vm_addr += 0x1000) {
435 dylds_found.push_back(dyld);
437 kernels_found.push_back(kernel);
444 if (dylds_found.size() > 0)
446 if (kernels_found.size() > 0)
450 if (kernels_found.size() < 2)
470 addr_t better_kernel_address =
478 "ProcessMachCore::%s: Using "
479 "the kernel address "
480 "from DynamicLoaderDarwinKernel",
487 Log *log(
GetLog(LLDBLog::DynamicLoader | LLDBLog::Process));
490 if (!found_binary_spec_in_metadata)
499 "ProcessMachCore::%s: Using kernel "
506 "ProcessMachCore::%s: Using user process dyld "
507 "image at 0x%" PRIx64,
514 "ProcessMachCore::%s: Using user process dyld "
515 "image at 0x%" PRIx64,
520 "ProcessMachCore::%s: Using kernel "
541 for (
size_t i = 0; i < core_range_infos_size; i++) {
544 ent->
data = lldb::ePermissionsReadable | lldb::ePermissionsExecutable;
553 error.SetErrorString(
"invalid core module");
558 if (core_objfile ==
nullptr) {
559 error.SetErrorString(
"invalid core object file");
590 if (old_thread_list.
GetSize(
false) == 0) {
596 std::set<lldb::tid_t> used_tids;
598 std::vector<lldb::tid_t> tids;
600 assert(tids.size() == num_threads);
604 for (uint32_t i = 0; i < num_threads; i++) {
606 highest_tid = tids[i];
609 for (uint32_t i = 0; i < num_threads; i++) {
611 tids[i] = current_unused_tid++;
616 for (uint32_t i = 0; i < num_threads; i++) {
621 for (uint32_t i = 0; i < num_threads; i++) {
623 std::make_shared<ThreadMachCore>(*
this, tids[i], i);
628 const uint32_t num_threads = old_thread_list.
GetSize(
false);
629 for (uint32_t i = 0; i < num_threads; ++i)
632 return new_thread_list.
GetSize(
false) > 0;
661 size_t bytes_read = 0;
680 while (bytes_read < size) {
681 const addr_t curr_addr = addr + bytes_read;
685 if (core_memory_entry) {
688 const size_t bytes_to_read =
689 std::min(size - bytes_read, (
size_t)bytes_left);
690 const size_t curr_bytes_read = core_objfile->
CopyData(
691 core_memory_entry->
data.GetRangeBase() + offset, bytes_to_read,
692 (
char *)buf + bytes_read);
693 if (curr_bytes_read == 0)
695 bytes_read += curr_bytes_read;
699 error.SetErrorStringWithFormat(
700 "core file does not contain 0x%" PRIx64, curr_addr);
714 if (permission_entry) {
715 if (permission_entry->
Contains(load_addr)) {
718 const Flags permissions(permission_entry->
data);
729 }
else if (load_addr < permission_entry->GetRangeBase()) {
752 static llvm::once_flag g_once_flag;
754 llvm::call_once(g_once_flag, []() {
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
#define LLDB_PLUGIN_DEFINE(PluginName)
static llvm::StringRef GetPluginNameStatic()
static lldb::addr_t SearchForDarwinKernel(lldb_private::Process *process)
static llvm::StringRef GetPluginNameStatic()
static llvm::StringRef GetPluginNameStatic()
bool ParseHeader() override
Attempts to parse the object header.
bool WarnBeforeDetach() const override
Before lldb detaches from a process, it warns the user that they are about to lose their debug sessio...
static llvm::StringRef GetPluginDescriptionStatic()
size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size, lldb_private::Status &error) override
Read of memory from a process.
bool LoadBinaryViaLowmemUUID()
VMRangeToFileOffset m_core_aranges
bool LoadBinariesViaMetadata()
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.
CorefilePreference GetCorefilePreference()
If a core file can be interpreted multiple ways, this establishes which style wins.
bool CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) override
Check if a plug-in instance can debug the file in module.
void CleanupMemoryRegionPermissions()
lldb_private::ObjectFile * GetCoreObjectFile()
ProcessMachCore(lldb::TargetSP target_sp, lldb::ListenerSP listener, const lldb_private::FileSpec &core_file)
llvm::StringRef m_dyld_plugin_name
lldb_private::DynamicLoader * GetDynamicLoader() override
Get the dynamic loader plug-in for this process.
lldb_private::Status DoDestroy() override
static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener, const lldb_private::FileSpec *crash_file_path, bool can_connect)
void RefreshStateAfterStop() override
Currently called as part of ShouldStop.
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.
static llvm::StringRef GetPluginNameStatic()
bool IsAlive() override
Check if a process is still alive.
void CreateMemoryRegions()
bool CheckAddressForDyldOrKernel(lldb::addr_t addr, lldb::addr_t &dyld, lldb::addr_t &kernel)
VMRangeToPermissions m_core_range_infos
lldb_private::Status DoLoadCore() override
void LoadBinariesViaExhaustiveSearch()
void LoadBinariesAndSetDYLD()
lldb::addr_t m_mach_kernel_addr
lldb::addr_t GetImageInfoAddress() override
Get the image information address for the current process.
~ProcessMachCore() override
lldb::ModuleSP m_core_module_sp
lldb_private::Status DoGetMemoryRegionInfo(lldb::addr_t load_addr, lldb_private::MemoryRegionInfo ®ion_info) override
DoGetMemoryRegionInfo is called by GetMemoryRegionInfo after it has removed non address bits from loa...
A class which holds the metadata from a remote stub/corefile note about how many bits are used for ad...
An architecture specification class.
bool IsValid() const
Tests if this ArchSpec is valid.
A plug-in interface definition class for dynamic loaders.
static lldb::ModuleSP LoadBinaryWithUUIDAndAddress(Process *process, llvm::StringRef name, UUID uuid, lldb::addr_t value, bool value_is_offset, bool force_symbol_search, bool notify, bool set_address_in_target, bool allow_memory_image_last_resort)
Find/load a binary into lldb given a UUID and the address where it is loaded in memory,...
static DynamicLoader * FindPlugin(Process *process, llvm::StringRef plugin_name)
Find a dynamic loader plugin for a given process.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
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.
bool Test(ValueType bit) const
Test a single flag bit.
void void Printf(const char *format,...) __attribute__((format(printf
Prefer using LLDB_LOGF whenever possible.
void SetMapped(OptionalBool val)
void SetReadable(OptionalBool val)
void SetExecutable(OptionalBool val)
void SetWritable(OptionalBool val)
static Status GetSharedModule(const ModuleSpec &module_spec, lldb::ModuleSP &module_sp, const FileSpecList *module_search_paths_ptr, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr, bool always_create=false)
A plug-in interface definition class for object file parsers.
virtual bool GetCorefileThreadExtraInfos(std::vector< lldb::tid_t > &tids)
Get metadata about threads from the corefile.
virtual std::string GetIdentifierString()
Some object files may have an identifier string embedded in them, e.g.
virtual uint32_t GetNumThreadContexts()
virtual bool LoadCoreFileImages(lldb_private::Process &process)
Load binaries listed in a corefile.
@ eTypeCoreFile
A core file that has a checkpoint of a program's execution state.
virtual lldb_private::AddressableBits GetAddressableBits()
Some object files may have the number of bits used for addressing embedded in them,...
size_t CopyData(lldb::offset_t offset, size_t length, void *dst) const
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
virtual bool GetCorefileMainBinaryInfo(lldb::addr_t &value, bool &value_is_offset, UUID &uuid, ObjectFile::BinaryType &type)
When the ObjectFile is a core file, lldb needs to locate the "binary" in the core file.
BinaryType
If we have a corefile binary hint, this enum specifies the binary type which we can use to select the...
@ eBinaryTypeUser
kernel binary
virtual llvm::StringRef GetPluginName()=0
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
Base class for all processes that don't represent a live process, such as coredumps or processes trac...
void SetAddressableBitMasks(AddressableBits bit_masks)
void SetCanJIT(bool can_jit)
Sets whether executing JIT-compiled code in this process is possible.
lldb::DynamicLoaderUP m_dyld_up
lldb::addr_t FixAnyAddress(lldb::addr_t pc)
Use this method when you do not know, or do not care what kind of address you are fixing.
virtual void Finalize(bool destructing)
This object is about to be destroyed, do any necessary cleanup.
ThreadList m_thread_list
The threads for this process as the user will see them.
Target & GetTarget()
Get the target object pointer for this module.
const Entry * GetEntryAtIndex(size_t i) const
Entry * GetMutableEntryAtIndex(size_t i)
const Entry * FindEntryThatContainsOrFollows(B addr) const
void Append(const Entry &entry)
Entry * FindEntryThatContains(B addr)
size_t GetNumSections(uint32_t depth) const
lldb::SectionSP GetSectionAtIndex(size_t idx) const
uint32_t GetPermissions() const
Get the permissions as OR'ed bits from lldb::Permissions.
lldb::offset_t GetFileOffset() const
lldb::addr_t GetFileAddress() const
lldb::addr_t GetByteSize() const
lldb::offset_t GetFileSize() const
bool SetArchitecture(const ArchSpec &arch_spec, bool set_platform=false, bool merge=true)
Set the architecture for this target.
void AddThread(const lldb::ThreadSP &thread_sp)
uint32_t GetSize(bool can_update=true)
lldb::ThreadSP GetThreadAtIndex(uint32_t idx, bool can_update=true)
void RefreshStateAfterStop()
bool SetFromStringRef(llvm::StringRef str)
std::string GetAsString(llvm::StringRef separator="-") const
#define LLDB_INVALID_THREAD_ID
#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.
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::Listener > ListenerSP
std::shared_ptr< lldb_private::Target > TargetSP
bool Contains(BaseType r) const
BaseType GetRangeBase() const
void SetRangeEnd(BaseType end)
void SetRangeBase(BaseType b)
Set the start value for the range, and keep the same size.
BaseType GetRangeEnd() const