1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-23 04:57:41 +00:00

Everywhere: Move shared library checks into a common function

While we're at it, unify the various different conditions that are
scattered accross the codebase.
This commit is contained in:
Tim Schumacher 2021-11-03 12:05:23 +01:00 committed by Andreas Kling
parent 31c634be5a
commit 80cb44afae
7 changed files with 25 additions and 6 deletions

View file

@ -5,6 +5,7 @@
*/ */
#include "Process.h" #include "Process.h"
#include <LibCore/File.h>
namespace Profiler { namespace Profiler {
@ -90,7 +91,7 @@ void LibraryMetadata::handle_mmap(FlatPtr base, size_t size, const String& name)
} else { } else {
String path_string = path.to_string(); String path_string = path.to_string();
String full_path; String full_path;
if (path_string.ends_with(".so"sv)) if (Core::File::looks_like_shared_library(path_string))
full_path = String::formatted("/usr/lib/{}", path); full_path = String::formatted("/usr/lib/{}", path);
else else
full_path = path_string; full_path = path_string;

View file

@ -15,6 +15,7 @@
#include <AK/LexicalPath.h> #include <AK/LexicalPath.h>
#include <AK/MappedFile.h> #include <AK/MappedFile.h>
#include <AK/StringUtils.h> #include <AK/StringUtils.h>
#include <LibCore/File.h>
#include <LibELF/AuxiliaryVector.h> #include <LibELF/AuxiliaryVector.h>
#include <LibELF/Image.h> #include <LibELF/Image.h>
#include <LibELF/Validation.h> #include <LibELF/Validation.h>
@ -395,7 +396,7 @@ MmapRegion const* Emulator::load_library_from_address(FlatPtr address)
return {}; return {};
String lib_path = lib_name; String lib_path = lib_name;
if (lib_name.ends_with(".so")) if (Core::File::looks_like_shared_library(lib_name))
lib_path = String::formatted("/usr/lib/{}", lib_path); lib_path = String::formatted("/usr/lib/{}", lib_path);
if (!m_dynamic_library_cache.contains(lib_path)) { if (!m_dynamic_library_cache.contains(lib_path)) {
@ -432,7 +433,10 @@ Optional<Emulator::SymbolInfo> Emulator::symbol_at(FlatPtr address)
auto lib_name = address_region->lib_name(); auto lib_name = address_region->lib_name();
auto const* first_region = (lib_name.is_null() || lib_name.is_empty()) ? address_region : first_region_for_object(lib_name); auto const* first_region = (lib_name.is_null() || lib_name.is_empty()) ? address_region : first_region_for_object(lib_name);
VERIFY(first_region); VERIFY(first_region);
auto lib_path = lib_name.ends_with(".so"sv) ? String::formatted("/usr/lib/{}", lib_name) : lib_name; auto lib_path = lib_name;
if (Core::File::looks_like_shared_library(lib_name)) {
lib_path = String::formatted("/usr/lib/{}", lib_name);
}
auto it = m_dynamic_library_cache.find(lib_path); auto it = m_dynamic_library_cache.find(lib_path);
auto const& elf = it->value.debug_info->elf(); auto const& elf = it->value.debug_info->elf();

View file

@ -147,6 +147,16 @@ bool File::is_link(String const& filename)
return S_ISLNK(st.st_mode); return S_ISLNK(st.st_mode);
} }
bool File::looks_like_shared_library() const
{
return File::looks_like_shared_library(m_filename);
}
bool File::looks_like_shared_library(const String& filename)
{
return filename.ends_with(".so"sv) || filename.contains(".so."sv);
}
bool File::exists(String const& filename) bool File::exists(String const& filename)
{ {
struct stat st; struct stat st;

View file

@ -32,6 +32,9 @@ public:
bool is_link() const; bool is_link() const;
static bool is_link(String const& filename); static bool is_link(String const& filename);
bool looks_like_shared_library() const;
static bool looks_like_shared_library(String const& filename);
static bool exists(String const& filename); static bool exists(String const& filename);
static ErrorOr<size_t> size(String const& filename); static ErrorOr<size_t> size(String const& filename);
static bool ensure_parent_directories(String const& path); static bool ensure_parent_directories(String const& path);

View file

@ -20,7 +20,7 @@ namespace Coredump {
ELFObjectInfo const* Backtrace::object_info_for_region(ELF::Core::MemoryRegionInfo const& region) ELFObjectInfo const* Backtrace::object_info_for_region(ELF::Core::MemoryRegionInfo const& region)
{ {
auto path = region.object_name(); auto path = region.object_name();
if (!path.starts_with('/') && (path.ends_with(".so"sv) || path.contains(".so."sv))) if (!path.starts_with('/') && Core::File::looks_like_shared_library(path))
path = LexicalPath::join("/usr/lib", path).string(); path = LexicalPath::join("/usr/lib", path).string();
auto maybe_ptr = m_debug_info_cache.get(path); auto maybe_ptr = m_debug_info_cache.get(path);

View file

@ -7,6 +7,7 @@
#include <AK/JsonObject.h> #include <AK/JsonObject.h>
#include <AK/JsonValue.h> #include <AK/JsonValue.h>
#include <LibCompress/Gzip.h> #include <LibCompress/Gzip.h>
#include <LibCore/File.h>
#include <LibCoredump/Reader.h> #include <LibCoredump/Reader.h>
#include <signal_numbers.h> #include <signal_numbers.h>
#include <string.h> #include <string.h>
@ -273,7 +274,7 @@ const Reader::LibraryData* Reader::library_containing(FlatPtr address) const
auto name = region->object_name(); auto name = region->object_name();
String path; String path;
if (name.ends_with(".so")) if (Core::File::looks_like_shared_library(name))
path = String::formatted("/usr/lib/{}", name); path = String::formatted("/usr/lib/{}", name);
else { else {
path = name; path = name;

View file

@ -433,7 +433,7 @@ void DebugSession::update_loaded_libs()
return IterationDecision::Continue; return IterationDecision::Continue;
String lib_name = object_path.value(); String lib_name = object_path.value();
if (lib_name.ends_with(".so")) if (Core::File::looks_like_shared_library(lib_name))
lib_name = LexicalPath::basename(object_path.value()); lib_name = LexicalPath::basename(object_path.value());
FlatPtr base_address = entry.as_object().get("address").to_addr(); FlatPtr base_address = entry.as_object().get("address").to_addr();