Make LD_DEBUG more user-friendly.
This is more self-explanatory than arbitrary integers, actually does
explain itself if you typo or say LD_DEBUG=help, and makes statistics
and timing available without recompilation.
Some of the groups could probably use a little tweaking, and we could
probably use some more groups, but I think this is already better than
what we have, and the initial change moving LD_DEBUG from integers to
names should probably minimize the amount of change that regard.
There's also the continuing question of whether statistics and timing
should just be deleted. They're not super useful as-is, and we should
think about whether we want to make them more useful or just remove them.
The timing stuff in particular is probably questionable, because
systrace is probably a better way to do anything like that? But that too
sounds like a question for another day.
Bug: http://b/309528372
Test: adb shell LD_DEBUG=<various things> date
Change-Id: I52b96c4892a41f9d24873122aebe5c272f39cdae
diff --git a/linker/linker_main.cpp b/linker/linker_main.cpp
index 86b6509..6ccd75b 100644
--- a/linker/linker_main.cpp
+++ b/linker/linker_main.cpp
@@ -73,21 +73,6 @@
void __libc_init_mte(const memtag_dynamic_entries_t* memtag_dynamic_entries, const void* phdr_start,
size_t phdr_count, uintptr_t load_bias, void* stack_top);
-__printflike(1, 2) static void __linker_error(const char* fmt, ...) {
- va_list ap;
-
- va_start(ap, fmt);
- async_safe_format_fd_va_list(STDERR_FILENO, fmt, ap);
- write(STDERR_FILENO, "\n", 1);
- va_end(ap);
-
- va_start(ap, fmt);
- async_safe_format_log_va_list(ANDROID_LOG_FATAL, "linker", fmt, ap);
- va_end(ap);
-
- _exit(EXIT_FAILURE);
-}
-
static void __linker_cannot_link(const char* argv0) {
__linker_error("CANNOT LINK EXECUTABLE \"%s\": %s", argv0, linker_get_error_buffer());
}
@@ -119,7 +104,7 @@
if (trav == nullptr) {
// si was not in solist
- PRINT("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
+ DL_WARN("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
return false;
}
@@ -147,7 +132,6 @@
}
bool g_is_ldd;
-int g_ld_debug_verbosity;
static std::vector<std::string> g_ld_preload_names;
@@ -296,10 +280,8 @@
static ElfW(Addr) linker_main(KernelArgumentBlock& args, const char* exe_to_load) {
ProtectedDataGuard guard;
-#if TIMING
- struct timeval t0, t1;
- gettimeofday(&t0, 0);
-#endif
+ timeval t0, t1;
+ gettimeofday(&t0, nullptr);
// Sanitize the environment.
__libc_init_AT_SECURE(args.envp);
@@ -317,13 +299,11 @@
// Enable debugging logs?
const char* LD_DEBUG = getenv("LD_DEBUG");
- if (LD_DEBUG != nullptr) {
- g_ld_debug_verbosity = atoi(LD_DEBUG);
- }
+ if (LD_DEBUG != nullptr) init_LD_DEBUG(LD_DEBUG);
if (getenv("LD_SHOW_AUXV") != nullptr) ld_show_auxv(args.auxv);
- INFO("[ Android dynamic linker (" ABI_STRING ") ]");
+ LD_DEBUG(any, "[ Android dynamic linker (" ABI_STRING ") ]");
// These should have been sanitized by __libc_init_AT_SECURE, but the test
// doesn't cost us anything.
@@ -332,18 +312,18 @@
if (!getauxval(AT_SECURE)) {
ldpath_env = getenv("LD_LIBRARY_PATH");
if (ldpath_env != nullptr) {
- INFO("[ LD_LIBRARY_PATH set to \"%s\" ]", ldpath_env);
+ LD_DEBUG(any, "[ LD_LIBRARY_PATH set to \"%s\" ]", ldpath_env);
}
ldpreload_env = getenv("LD_PRELOAD");
if (ldpreload_env != nullptr) {
- INFO("[ LD_PRELOAD set to \"%s\" ]", ldpreload_env);
+ LD_DEBUG(any, "[ LD_PRELOAD set to \"%s\" ]", ldpreload_env);
}
}
const ExecutableInfo exe_info = exe_to_load ? load_executable(exe_to_load) :
get_executable_info(args.argv[0]);
- INFO("[ Linking executable \"%s\" ]", exe_info.path.c_str());
+ LD_DEBUG(any, "[ Linking executable \"%s\" ]", exe_info.path.c_str());
// Initialize the main exe's soinfo.
soinfo* si = soinfo_alloc(&g_default_namespace,
@@ -496,27 +476,22 @@
si->call_pre_init_constructors();
si->call_constructors();
-#if TIMING
- gettimeofday(&t1, nullptr);
- PRINT("LINKER TIME: %s: %d microseconds", g_argv[0],
- static_cast<int>(((static_cast<long long>(t1.tv_sec) * 1000000LL) +
- static_cast<long long>(t1.tv_usec)) -
- ((static_cast<long long>(t0.tv_sec) * 1000000LL) +
- static_cast<long long>(t0.tv_usec))));
-#endif
-#if STATS
- print_linker_stats();
-#endif
-#if TIMING || STATS
- fflush(stdout);
-#endif
+ if (g_linker_debug_config.timing) {
+ gettimeofday(&t1, nullptr);
+ long long t0_us = (t0.tv_sec * 1000000LL) + t0.tv_usec;
+ long long t1_us = (t1.tv_sec * 1000000LL) + t1.tv_usec;
+ LD_DEBUG(timing, "LINKER TIME: %s: %lld microseconds", g_argv[0], t1_us - t0_us);
+ }
+ if (g_linker_debug_config.statistics) {
+ print_linker_stats();
+ }
// We are about to hand control over to the executable loaded. We don't want
// to leave dirty pages behind unnecessarily.
purge_unused_memory();
ElfW(Addr) entry = exe_info.entry_point;
- TRACE("[ Ready to execute \"%s\" @ %p ]", si->get_realpath(), reinterpret_cast<void*>(entry));
+ LD_DEBUG(any, "[ Ready to execute \"%s\" @ %p ]", si->get_realpath(), reinterpret_cast<void*>(entry));
return entry;
}
@@ -833,7 +808,7 @@
ElfW(Addr) start_address = linker_main(args, exe_to_load);
- INFO("[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address));
+ LD_DEBUG(any, "[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address));
// Return the address that the calling assembly stub should jump to.
return start_address;