2020-11-11 22:05:49 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
2022-02-26 10:30:24 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-11-11 22:05:49 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-11 22:05:49 +02:00
|
|
|
*/
|
|
|
|
|
|
2022-01-27 13:22:45 +03:30
|
|
|
#include <AK/ByteReader.h>
|
2023-03-22 02:35:30 +11:00
|
|
|
#include <AK/Function.h>
|
2021-11-20 11:48:45 +02:00
|
|
|
#include <AK/HashTable.h>
|
2020-12-30 15:06:33 +01:00
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
|
#include <AK/JsonValue.h>
|
2022-02-07 05:42:05 +02:00
|
|
|
#include <AK/LexicalPath.h>
|
2021-03-26 18:46:20 +03:00
|
|
|
#include <LibCompress/Gzip.h>
|
2021-08-22 14:51:04 +02:00
|
|
|
#include <LibCoredump/Reader.h>
|
2023-03-22 02:35:30 +11:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2023-01-07 10:54:01 -07:00
|
|
|
#include <signal.h>
|
2020-11-11 22:05:49 +02:00
|
|
|
#include <string.h>
|
2022-02-07 05:42:05 +02:00
|
|
|
#include <unistd.h>
|
2020-11-11 22:05:49 +02:00
|
|
|
|
2021-08-22 14:51:04 +02:00
|
|
|
namespace Coredump {
|
2020-12-28 12:38:19 +01:00
|
|
|
|
2021-11-20 12:31:48 +02:00
|
|
|
OwnPtr<Reader> Reader::create(StringView path)
|
2020-11-11 22:05:49 +02:00
|
|
|
{
|
2021-11-23 11:32:25 +01:00
|
|
|
auto file_or_error = Core::MappedFile::map(path);
|
2021-01-10 15:55:54 +01:00
|
|
|
if (file_or_error.is_error())
|
2021-01-10 16:29:28 -07:00
|
|
|
return {};
|
2021-09-22 00:33:43 +02:00
|
|
|
|
|
|
|
|
if (!Compress::GzipDecompressor::is_likely_compressed(file_or_error.value()->bytes())) {
|
|
|
|
|
// It's an uncompressed coredump.
|
|
|
|
|
return AK::adopt_own_if_nonnull(new (nothrow) Reader(file_or_error.release_value()));
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-06 03:29:52 +04:30
|
|
|
auto decompressed_data = decompress_coredump(file_or_error.value()->bytes());
|
|
|
|
|
if (!decompressed_data.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
return adopt_own_if_nonnull(new (nothrow) Reader(decompressed_data.release_value()));
|
2020-11-11 22:05:49 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-22 00:33:43 +02:00
|
|
|
Reader::Reader(ByteBuffer buffer)
|
|
|
|
|
: Reader(buffer.bytes())
|
|
|
|
|
{
|
|
|
|
|
m_coredump_buffer = move(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 11:32:25 +01:00
|
|
|
Reader::Reader(NonnullRefPtr<Core::MappedFile> file)
|
2021-09-22 00:33:43 +02:00
|
|
|
: Reader(file->bytes())
|
|
|
|
|
{
|
|
|
|
|
m_mapped_file = move(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Reader::Reader(ReadonlyBytes coredump_bytes)
|
|
|
|
|
: m_coredump_bytes(coredump_bytes)
|
|
|
|
|
, m_coredump_image(m_coredump_bytes)
|
2020-11-11 22:05:49 +02:00
|
|
|
{
|
|
|
|
|
size_t index = 0;
|
|
|
|
|
m_coredump_image.for_each_program_header([this, &index](auto pheader) {
|
|
|
|
|
if (pheader.type() == PT_NOTE) {
|
|
|
|
|
m_notes_segment_index = index;
|
|
|
|
|
return IterationDecision::Break;
|
|
|
|
|
}
|
|
|
|
|
++index;
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_notes_segment_index != -1);
|
2020-11-11 22:05:49 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-11 01:06:34 +01:00
|
|
|
Optional<ByteBuffer> Reader::decompress_coredump(ReadonlyBytes raw_coredump)
|
2021-03-26 18:46:20 +03:00
|
|
|
{
|
|
|
|
|
auto decompressed_coredump = Compress::GzipDecompressor::decompress_all(raw_coredump);
|
2022-11-29 15:43:31 +01:00
|
|
|
if (!decompressed_coredump.is_error())
|
|
|
|
|
return decompressed_coredump.release_value();
|
2022-01-20 17:47:39 +00:00
|
|
|
|
|
|
|
|
// If we didn't manage to decompress it, try and parse it as decompressed coredump
|
|
|
|
|
auto bytebuffer = ByteBuffer::copy(raw_coredump);
|
|
|
|
|
if (bytebuffer.is_error())
|
|
|
|
|
return {};
|
|
|
|
|
return bytebuffer.release_value();
|
2021-03-26 18:46:20 +03:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Reader::NotesEntryIterator::NotesEntryIterator(u8 const* notes_data)
|
2022-01-27 04:51:17 +03:30
|
|
|
: m_current(bit_cast<const ELF::Core::NotesEntry*>(notes_data))
|
2020-11-11 22:05:49 +02:00
|
|
|
, start(notes_data)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:38:19 +01:00
|
|
|
ELF::Core::NotesEntryHeader::Type Reader::NotesEntryIterator::type() const
|
2020-11-11 22:05:49 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_current->header.type == ELF::Core::NotesEntryHeader::Type::ProcessInfo
|
2020-12-30 13:18:32 +01:00
|
|
|
|| m_current->header.type == ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo
|
2020-11-11 22:05:49 +02:00
|
|
|
|| m_current->header.type == ELF::Core::NotesEntryHeader::Type::ThreadInfo
|
2020-12-30 15:06:33 +01:00
|
|
|
|| m_current->header.type == ELF::Core::NotesEntryHeader::Type::Metadata
|
2020-11-11 22:05:49 +02:00
|
|
|
|| m_current->header.type == ELF::Core::NotesEntryHeader::Type::Null);
|
|
|
|
|
return m_current->header.type;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:38:19 +01:00
|
|
|
const ELF::Core::NotesEntry* Reader::NotesEntryIterator::current() const
|
2020-11-11 22:05:49 +02:00
|
|
|
{
|
|
|
|
|
return m_current;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:38:19 +01:00
|
|
|
void Reader::NotesEntryIterator::next()
|
2020-11-11 22:05:49 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!at_end());
|
2020-12-30 13:18:32 +01:00
|
|
|
switch (type()) {
|
|
|
|
|
case ELF::Core::NotesEntryHeader::Type::ProcessInfo: {
|
2022-04-01 20:58:27 +03:00
|
|
|
auto const* current = bit_cast<const ELF::Core::ProcessInfo*>(m_current);
|
2022-01-27 04:51:17 +03:30
|
|
|
m_current = bit_cast<const ELF::Core::NotesEntry*>(current->json_data + strlen(current->json_data) + 1);
|
2020-12-30 13:18:32 +01:00
|
|
|
break;
|
2020-11-11 22:05:49 +02:00
|
|
|
}
|
2020-12-30 13:18:32 +01:00
|
|
|
case ELF::Core::NotesEntryHeader::Type::ThreadInfo: {
|
2022-04-01 20:58:27 +03:00
|
|
|
auto const* current = bit_cast<const ELF::Core::ThreadInfo*>(m_current);
|
2022-01-27 04:51:17 +03:30
|
|
|
m_current = bit_cast<const ELF::Core::NotesEntry*>(current + 1);
|
2020-12-30 13:18:32 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo: {
|
2022-04-01 20:58:27 +03:00
|
|
|
auto const* current = bit_cast<const ELF::Core::MemoryRegionInfo*>(m_current);
|
2022-01-27 04:51:17 +03:30
|
|
|
m_current = bit_cast<const ELF::Core::NotesEntry*>(current->region_name + strlen(current->region_name) + 1);
|
2020-12-30 13:18:32 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2020-12-30 15:06:33 +01:00
|
|
|
case ELF::Core::NotesEntryHeader::Type::Metadata: {
|
2022-04-01 20:58:27 +03:00
|
|
|
auto const* current = bit_cast<const ELF::Core::Metadata*>(m_current);
|
2022-01-27 04:51:17 +03:30
|
|
|
m_current = bit_cast<const ELF::Core::NotesEntry*>(current->json_data + strlen(current->json_data) + 1);
|
2020-12-30 15:06:33 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2020-12-30 13:18:32 +01:00
|
|
|
default:
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-11-11 22:05:49 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:38:19 +01:00
|
|
|
bool Reader::NotesEntryIterator::at_end() const
|
2020-11-11 22:05:49 +02:00
|
|
|
{
|
|
|
|
|
return type() == ELF::Core::NotesEntryHeader::Type::Null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 17:48:01 +02:00
|
|
|
Optional<FlatPtr> Reader::peek_memory(FlatPtr address) const
|
2020-11-11 22:05:49 +02:00
|
|
|
{
|
2022-01-27 14:20:10 +03:30
|
|
|
auto region = region_containing(address);
|
|
|
|
|
if (!region.has_value())
|
2020-11-11 22:05:49 +02:00
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
FlatPtr offset_in_region = address - region->region_start;
|
2022-04-01 20:58:27 +03:00
|
|
|
auto* region_data = bit_cast<u8 const*>(image().program_header(region->program_header_index).raw_data());
|
2022-01-27 13:22:45 +03:30
|
|
|
FlatPtr value { 0 };
|
|
|
|
|
ByteReader::load(region_data + offset_in_region, value);
|
|
|
|
|
return value;
|
2020-11-11 22:05:49 +02:00
|
|
|
}
|
|
|
|
|
|
2021-01-13 23:59:22 +01:00
|
|
|
const JsonObject Reader::process_info() const
|
2020-12-30 13:18:32 +01:00
|
|
|
{
|
2021-01-13 23:59:22 +01:00
|
|
|
const ELF::Core::ProcessInfo* process_info_notes_entry = nullptr;
|
2022-04-01 20:58:27 +03:00
|
|
|
NotesEntryIterator it(bit_cast<u8 const*>(m_coredump_image.program_header(m_notes_segment_index).raw_data()));
|
2022-01-27 04:51:17 +03:30
|
|
|
for (; !it.at_end(); it.next()) {
|
2020-12-30 13:18:32 +01:00
|
|
|
if (it.type() != ELF::Core::NotesEntryHeader::Type::ProcessInfo)
|
|
|
|
|
continue;
|
2022-01-27 04:51:17 +03:30
|
|
|
process_info_notes_entry = bit_cast<const ELF::Core::ProcessInfo*>(it.current());
|
2021-01-13 23:59:22 +01:00
|
|
|
break;
|
2020-12-30 13:18:32 +01:00
|
|
|
}
|
2021-01-13 23:59:22 +01:00
|
|
|
if (!process_info_notes_entry)
|
|
|
|
|
return {};
|
2022-07-11 19:53:29 +00:00
|
|
|
auto const* json_data_ptr = process_info_notes_entry->json_data;
|
|
|
|
|
auto process_info_json_value = JsonValue::from_string({ json_data_ptr, strlen(json_data_ptr) });
|
2021-11-15 01:46:51 +01:00
|
|
|
if (process_info_json_value.is_error())
|
2021-01-13 23:59:22 +01:00
|
|
|
return {};
|
|
|
|
|
if (!process_info_json_value.value().is_object())
|
|
|
|
|
return {};
|
|
|
|
|
return process_info_json_value.value().as_object();
|
|
|
|
|
// FIXME: Maybe just cache this on the Reader instance after first access.
|
2020-12-30 13:18:32 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-27 14:20:10 +03:30
|
|
|
Optional<MemoryRegionInfo> Reader::first_region_for_object(StringView object_name) const
|
2021-07-27 17:01:29 +02:00
|
|
|
{
|
2022-01-27 14:20:10 +03:30
|
|
|
Optional<MemoryRegionInfo> ret;
|
2021-07-27 17:01:29 +02:00
|
|
|
for_each_memory_region_info([&ret, &object_name](auto& region_info) {
|
|
|
|
|
if (region_info.object_name() == object_name) {
|
2022-01-27 14:20:10 +03:30
|
|
|
ret = region_info;
|
2021-07-27 17:01:29 +02:00
|
|
|
return IterationDecision::Break;
|
|
|
|
|
}
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-27 14:20:10 +03:30
|
|
|
Optional<MemoryRegionInfo> Reader::region_containing(FlatPtr address) const
|
2020-11-11 22:05:49 +02:00
|
|
|
{
|
2022-01-27 14:20:10 +03:30
|
|
|
Optional<MemoryRegionInfo> ret;
|
2022-04-01 20:58:27 +03:00
|
|
|
for_each_memory_region_info([&ret, address](auto const& region_info) {
|
2020-12-28 16:26:03 +01:00
|
|
|
if (region_info.region_start <= address && region_info.region_end >= address) {
|
2022-01-27 14:20:10 +03:30
|
|
|
ret = region_info;
|
2020-11-11 22:05:49 +02:00
|
|
|
return IterationDecision::Break;
|
|
|
|
|
}
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2020-12-28 12:38:19 +01:00
|
|
|
|
2021-01-13 23:59:22 +01:00
|
|
|
int Reader::process_pid() const
|
|
|
|
|
{
|
|
|
|
|
auto process_info = this->process_info();
|
2022-12-21 17:26:59 +00:00
|
|
|
auto pid = process_info.get_integer<int>("pid"sv).value_or(0);
|
|
|
|
|
return pid;
|
2021-01-13 23:59:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u8 Reader::process_termination_signal() const
|
|
|
|
|
{
|
|
|
|
|
auto process_info = this->process_info();
|
2022-12-21 17:26:59 +00:00
|
|
|
auto termination_signal = process_info.get_u8("termination_signal"sv);
|
|
|
|
|
if (!termination_signal.has_value() || *termination_signal <= SIGINVAL || *termination_signal >= NSIG)
|
2021-01-13 23:59:22 +01:00
|
|
|
return SIGINVAL;
|
2022-12-21 17:26:59 +00:00
|
|
|
return *termination_signal;
|
2021-01-13 23:59:22 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString Reader::process_executable_path() const
|
2021-01-13 23:59:22 +01:00
|
|
|
{
|
|
|
|
|
auto process_info = this->process_info();
|
2022-12-21 17:26:59 +00:00
|
|
|
auto executable_path = process_info.get_deprecated_string("executable_path"sv);
|
|
|
|
|
return executable_path.value_or({});
|
2021-01-13 23:59:22 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> Reader::process_arguments() const
|
2021-01-15 20:49:32 +01:00
|
|
|
{
|
|
|
|
|
auto process_info = this->process_info();
|
2022-12-21 17:26:59 +00:00
|
|
|
auto arguments = process_info.get_array("arguments"sv);
|
|
|
|
|
if (!arguments.has_value())
|
2021-01-15 20:49:32 +01:00
|
|
|
return {};
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> vector;
|
2022-12-21 17:26:59 +00:00
|
|
|
arguments->for_each([&](auto& value) {
|
2021-01-15 20:49:32 +01:00
|
|
|
if (value.is_string())
|
|
|
|
|
vector.append(value.as_string());
|
|
|
|
|
});
|
|
|
|
|
return vector;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> Reader::process_environment() const
|
2021-01-15 20:49:32 +01:00
|
|
|
{
|
|
|
|
|
auto process_info = this->process_info();
|
2022-12-21 17:26:59 +00:00
|
|
|
auto environment = process_info.get_array("environment"sv);
|
|
|
|
|
if (!environment.has_value())
|
2021-01-15 20:49:32 +01:00
|
|
|
return {};
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> vector;
|
2022-12-21 17:26:59 +00:00
|
|
|
environment->for_each([&](auto& value) {
|
2021-01-15 20:49:32 +01:00
|
|
|
if (value.is_string())
|
|
|
|
|
vector.append(value.as_string());
|
|
|
|
|
});
|
|
|
|
|
return vector;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
HashMap<DeprecatedString, DeprecatedString> Reader::metadata() const
|
2020-12-30 15:06:33 +01:00
|
|
|
{
|
|
|
|
|
const ELF::Core::Metadata* metadata_notes_entry = nullptr;
|
2022-04-01 20:58:27 +03:00
|
|
|
NotesEntryIterator it(bit_cast<u8 const*>(m_coredump_image.program_header(m_notes_segment_index).raw_data()));
|
2022-01-27 04:51:17 +03:30
|
|
|
for (; !it.at_end(); it.next()) {
|
2020-12-30 15:06:33 +01:00
|
|
|
if (it.type() != ELF::Core::NotesEntryHeader::Type::Metadata)
|
|
|
|
|
continue;
|
2022-01-27 04:51:17 +03:30
|
|
|
metadata_notes_entry = bit_cast<const ELF::Core::Metadata*>(it.current());
|
2020-12-30 15:06:33 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (!metadata_notes_entry)
|
|
|
|
|
return {};
|
2022-07-11 19:53:29 +00:00
|
|
|
auto const* json_data_ptr = metadata_notes_entry->json_data;
|
|
|
|
|
auto metadata_json_value = JsonValue::from_string({ json_data_ptr, strlen(json_data_ptr) });
|
2021-11-15 01:46:51 +01:00
|
|
|
if (metadata_json_value.is_error())
|
2020-12-30 15:06:33 +01:00
|
|
|
return {};
|
|
|
|
|
if (!metadata_json_value.value().is_object())
|
|
|
|
|
return {};
|
2022-12-04 18:02:33 +00:00
|
|
|
HashMap<DeprecatedString, DeprecatedString> metadata;
|
2020-12-30 15:06:33 +01:00
|
|
|
metadata_json_value.value().as_object().for_each_member([&](auto& key, auto& value) {
|
|
|
|
|
metadata.set(key, value.as_string_or({}));
|
|
|
|
|
});
|
|
|
|
|
return metadata;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Reader::LibraryData const* Reader::library_containing(FlatPtr address) const
|
2021-01-09 11:05:08 +02:00
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
static HashMap<DeprecatedString, OwnPtr<LibraryData>> cached_libs;
|
2022-01-27 14:20:10 +03:30
|
|
|
auto region = region_containing(address);
|
|
|
|
|
if (!region.has_value())
|
2021-01-09 11:05:08 +02:00
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
auto name = region->object_name();
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString path = resolve_object_path(name);
|
2021-01-09 11:05:08 +02:00
|
|
|
|
|
|
|
|
if (!cached_libs.contains(path)) {
|
2021-11-23 11:32:25 +01:00
|
|
|
auto file_or_error = Core::MappedFile::map(path);
|
2021-01-10 15:55:54 +01:00
|
|
|
if (file_or_error.is_error())
|
2021-01-09 11:05:08 +02:00
|
|
|
return {};
|
2021-01-10 15:55:54 +01:00
|
|
|
auto image = ELF::Image(file_or_error.value()->bytes());
|
2022-01-27 04:51:17 +03:30
|
|
|
cached_libs.set(path, make<LibraryData>(name, static_cast<FlatPtr>(region->region_start), file_or_error.release_value(), move(image)));
|
2021-01-09 11:05:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto lib_data = cached_libs.get(path).value();
|
|
|
|
|
return lib_data;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString Reader::resolve_object_path(StringView name) const
|
2022-02-07 05:42:05 +02:00
|
|
|
{
|
|
|
|
|
// TODO: There are other places where similar method is implemented or would be useful.
|
|
|
|
|
// (e.g. UserspaceEmulator, LibSymbolication, Profiler, and DynamicLinker itself)
|
|
|
|
|
// We should consider creating unified implementation in the future.
|
|
|
|
|
|
2023-03-22 02:35:30 +11:00
|
|
|
if (name.starts_with('/') || !FileSystem::looks_like_shared_library(name)) {
|
2022-02-07 05:42:05 +02:00
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> library_search_directories;
|
2022-02-07 05:42:05 +02:00
|
|
|
|
|
|
|
|
// If LD_LIBRARY_PATH is present, check its folders first
|
|
|
|
|
for (auto& environment_variable : process_environment()) {
|
|
|
|
|
auto prefix = "LD_LIBRARY_PATH="sv;
|
|
|
|
|
if (environment_variable.starts_with(prefix)) {
|
|
|
|
|
auto ld_library_path = environment_variable.substring_view(prefix.length());
|
|
|
|
|
|
|
|
|
|
// FIXME: This code won't handle folders with ":" in the name correctly.
|
|
|
|
|
for (auto directory : ld_library_path.split_view(':')) {
|
|
|
|
|
library_search_directories.append(directory);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add default paths that DynamicLinker uses
|
|
|
|
|
library_search_directories.append("/usr/lib/"sv);
|
|
|
|
|
library_search_directories.append("/usr/local/lib/"sv);
|
|
|
|
|
|
|
|
|
|
// Search for the first readable library file
|
|
|
|
|
for (auto& directory : library_search_directories) {
|
|
|
|
|
auto full_path = LexicalPath::join(directory, name).string();
|
|
|
|
|
|
|
|
|
|
if (access(full_path.characters(), R_OK) != 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return full_path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-20 11:48:45 +02:00
|
|
|
void Reader::for_each_library(Function<void(LibraryInfo)> func) const
|
|
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
HashTable<DeprecatedString> libraries;
|
2022-01-27 14:20:10 +03:30
|
|
|
for_each_memory_region_info([&](auto const& region) {
|
2021-11-20 11:48:45 +02:00
|
|
|
auto name = region.object_name();
|
|
|
|
|
if (name.is_null() || libraries.contains(name))
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
|
|
|
|
|
libraries.set(name);
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString path = resolve_object_path(name);
|
2021-11-20 11:48:45 +02:00
|
|
|
|
2022-01-27 04:51:17 +03:30
|
|
|
func(LibraryInfo { name, path, static_cast<FlatPtr>(region.region_start) });
|
2021-11-20 11:48:45 +02:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:38:19 +01:00
|
|
|
}
|