Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/native/clr/include/runtime-base/android-system.hh
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include <array>
#include <cstring>
#include <limits>
#include <span>
#include <string>
#include <string_view>
#include <unordered_map>
Expand All @@ -18,6 +20,7 @@ struct BundledProperty;
namespace xamarin::android {
class AndroidSystem
{
#if !defined (XA_HOST_NATIVEAOT)
// This optimizes things a little bit. The array is allocated at build time, so we pay no cost for its
// allocation and at run time it allows us to skip dynamic memory allocation.
inline static std::array<std::string, 1> single_app_lib_directory{};
Expand All @@ -35,6 +38,7 @@ namespace xamarin::android {
std::string_view { "x86_64" }, // CPU_KIND_X86_64
std::string_view { "riscv" }, // CPU_KIND_RISCV
};
#endif

public:
static auto get_gref_gc_threshold () noexcept -> long
Expand All @@ -60,16 +64,28 @@ namespace xamarin::android {
running_in_emulator = yesno;
}

#if defined (XA_HOST_NATIVEAOT)
static auto get_primary_override_dir () noexcept -> const char*
{
return primary_override_dir;
}
#else
static auto get_primary_override_dir () noexcept -> std::string const&
{
return primary_override_dir;
}
#endif

static void set_primary_override_dir (jstring_wrapper& home) noexcept
{
#if defined (XA_HOST_NATIVEAOT)
determine_primary_override_dir (home, primary_override_dir, sizeof (primary_override_dir));
#else
primary_override_dir = determine_primary_override_dir (home);
#endif
}

#if !defined (XA_HOST_NATIVEAOT)
static auto get_app_code_cache_dir () noexcept -> std::string const&
{
return app_code_cache_dir;
Expand Down Expand Up @@ -104,6 +120,7 @@ namespace xamarin::android {
log_debug (LOG_DEFAULT, "Creating public update directory: `{}`", override_dir);
Util::create_public_directory (override_dir);
}
#endif

static auto is_embedded_dso_mode_enabled () noexcept -> bool
{
Expand Down Expand Up @@ -139,6 +156,19 @@ namespace xamarin::android {
embedded_dso_mode_enabled = yesno;
}

#if defined (XA_HOST_NATIVEAOT)
static void determine_primary_override_dir (jstring_wrapper &home, char *buffer, size_t buffer_size) noexcept
{
dynamic_local_string<SENSIBLE_PATH_MAX> name { home.get_cstr () };
name.append ("/")
.append (Constants::OVERRIDE_DIRECTORY_NAME)
.append ("/")
.append (Constants::android_lib_abi);

abort_unless (name.length () < buffer_size, "Primary override directory path is too long");
memcpy (buffer, name.get (), name.length () + 1);
}
#else
static auto determine_primary_override_dir (jstring_wrapper &home) noexcept -> std::string
{
dynamic_local_string<SENSIBLE_PATH_MAX> name { home.get_cstr () };
Expand All @@ -149,17 +179,22 @@ namespace xamarin::android {

return {name.get (), name.length ()};
}
#endif

private:
static inline long max_gref_count = 0;
static inline bool running_in_emulator = false;
static inline bool embedded_dso_mode_enabled = false;
#if defined (XA_HOST_NATIVEAOT)
static inline char primary_override_dir[Constants::SENSIBLE_PATH_MAX] {};
#else
static inline std::string primary_override_dir;
static inline std::string native_libraries_dir;
static inline std::string app_code_cache_dir;

#if defined (DEBUG)
static inline std::unordered_map<std::string, std::string> bundled_properties;
#endif
#endif
};
}
63 changes: 47 additions & 16 deletions src/native/clr/runtime-base/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <cstdlib>
#include <cstring>
#include <limits>
#include <string>

#include <strings.h>
#include <unistd.h>
Expand All @@ -22,10 +21,26 @@ using namespace xamarin::android;
using std::operator""sv;

namespace {
std::string gref_file{};
std::string lref_file{};
char *gref_file = nullptr;
char *lref_file = nullptr;
bool light_gref = false;
bool light_lref = false;

void set_log_file (char *&log_file, std::string_view path) noexcept
{
char *new_log_file = nullptr;
if (!path.empty ()) {
size_t allocation_size = Helpers::add_with_overflow_check<size_t> (path.length (), 1uz);
new_log_file = static_cast<char*> (std::malloc (allocation_size));
abort_unless (new_log_file != nullptr, "Failed to allocate reference log file path");

memcpy (new_log_file, path.data (), path.length ());
new_log_file [path.length ()] = '\0';
}

std::free (log_file);
log_file = new_log_file;
Comment thread
jonathanpeppers marked this conversation as resolved.
}
}

[[gnu::always_inline]]
Expand Down Expand Up @@ -62,30 +77,46 @@ auto Logger::open_file (LogCategories category, std::string_view const& custom_p
return log_and_return (ret, custom_path);
}

std::string p{};
Util::create_public_directory (override_dir);
p.assign (override_dir);
p.append ("/");
p.append (fallback_filename);
dynamic_local_string<Constants::SENSIBLE_PATH_MAX> p;
p.append (override_dir)
.append ("/")
.append (fallback_filename);

return log_and_return (open_file (p), p);
std::string_view path = p.as_string_view ();
return log_and_return (open_file (path), path);
}

void
Logger::init_reference_logging (std::string_view const& override_dir) noexcept
{
if ((log_categories & LOG_GREF) != 0 && !light_gref) {
_gref_log = open_file (LOG_GREF, gref_file, override_dir, "grefs.txt"sv);
_gref_log = open_file (
LOG_GREF,
gref_file == nullptr ? std::string_view {} : std::string_view { gref_file },
override_dir,
"grefs.txt"sv
);
}

if ((log_categories & LOG_LREF) != 0 && !light_lref) {
// if both lref & gref have files specified, and they're the same path, reuse the FILE*.
if (!lref_file.empty () && strcmp (lref_file.c_str (), !gref_file.empty () ? gref_file.c_str () : "") == 0) {
if (lref_file != nullptr && strcmp (lref_file, gref_file != nullptr ? gref_file : "") == 0) {
_lref_log = _gref_log;
} else {
_lref_log = open_file (LOG_LREF, lref_file, override_dir, "lrefs.txt"sv);
_lref_log = open_file (
LOG_LREF,
lref_file == nullptr ? std::string_view {} : std::string_view { lref_file },
override_dir,
"lrefs.txt"sv
);
}
}

std::free (gref_file);
gref_file = nullptr;
std::free (lref_file);
lref_file = nullptr;
}

[[gnu::always_inline]] bool
Expand Down Expand Up @@ -158,20 +189,20 @@ Logger::init_logging_categories () noexcept
continue;
}

auto get_log_file_name = [](std::string_view const& file_kind, string_segment const& segment, size_t offset) -> const char* {
auto get_log_file_name = [](std::string_view const& file_kind, string_segment const& segment, size_t offset) -> std::string_view {
auto file_name = segment.at (offset);

if (!file_name.has_value ()) {
log_warn (LOG_DEFAULT, "Unable to set path to {} log file: {}", file_kind, to_string (file_name.error ()));
return nullptr;
return {};
}

return file_name.value ();
return { file_name.value (), segment.length () - offset };
};

constexpr std::string_view CAT_GREF_EQUALS { "gref=" };
if (set_category (CAT_GREF_EQUALS, param, LOG_GREF, true /* arg_starts_with_name */)) {
gref_file = get_log_file_name ("gref"sv, param, CAT_GREF_EQUALS.length ());
set_log_file (gref_file, get_log_file_name ("gref"sv, param, CAT_GREF_EQUALS.length ()));
continue;
}

Expand All @@ -187,7 +218,7 @@ Logger::init_logging_categories () noexcept

constexpr std::string_view CAT_LREF_EQUALS { "lref=" };
if (set_category (CAT_LREF_EQUALS, param, LOG_LREF, true /* arg_starts_with_name */)) {
lref_file = get_log_file_name ("lref"sv, param, CAT_LREF_EQUALS.length ());
set_log_file (lref_file, get_log_file_name ("lref"sv, param, CAT_LREF_EQUALS.length ()));
continue;
}

Expand Down
Loading
Loading