Merge "Consistently use %m rather than strerror() in logging." into main

This commit is contained in:
Elliott Hughes 2023-07-13 13:36:46 +00:00 committed by Gerrit Code Review
commit a4b7949aca
11 changed files with 40 additions and 52 deletions

View file

@ -150,7 +150,7 @@ extern "C" void __libc_init_main_thread_final() {
// stack.)
ThreadMapping mapping = __allocate_thread_mapping(0, PTHREAD_GUARD_SIZE);
if (mapping.mmap_base == nullptr) {
async_safe_fatal("failed to mmap main thread static TLS: %s", strerror(errno));
async_safe_fatal("failed to mmap main thread static TLS: %m");
}
const StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout;

View file

@ -126,15 +126,14 @@ static void HandleProfilingSignal(int /*signal_number*/, siginfo_t* info, void*
static void HandleTracedPerfSignal() {
ScopedFd sock_fd{ socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0 /*protocol*/) };
if (sock_fd.get() == -1) {
async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to create socket: %s", strerror(errno));
async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to create socket: %m");
return;
}
sockaddr_un saddr{ AF_UNIX, "/dev/socket/traced_perf" };
size_t addrlen = sizeof(sockaddr_un);
if (connect(sock_fd.get(), reinterpret_cast<const struct sockaddr*>(&saddr), addrlen) == -1) {
async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to connect to traced_perf socket: %s",
strerror(errno));
async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to connect to traced_perf socket: %m");
return;
}
@ -153,13 +152,11 @@ static void HandleTracedPerfSignal() {
}
if (maps_fd.get() == -1) {
async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to open /proc/self/maps: %s",
strerror(errno));
async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to open /proc/self/maps: %m");
return;
}
if (mem_fd.get() == -1) {
async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to open /proc/self/mem: %s",
strerror(errno));
async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to open /proc/self/mem: %m");
return;
}
@ -183,7 +180,7 @@ static void HandleTracedPerfSignal() {
memcpy(CMSG_DATA(cmsg), send_fds, num_fds * sizeof(int));
if (sendmsg(sock_fd.get(), &msg_hdr, 0) == -1) {
async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to sendmsg: %s", strerror(errno));
async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to sendmsg: %m");
}
}

View file

@ -158,7 +158,7 @@ void AtexitArray::set_writable(bool writable, size_t start_idx, size_t num_entri
const int prot = PROT_READ | (writable ? PROT_WRITE : 0);
if (mprotect(reinterpret_cast<char*>(array_) + start_byte, byte_len, prot) != 0) {
async_safe_fatal("mprotect failed on atexit array: %s", strerror(errno));
async_safe_fatal("mprotect failed on atexit array: %m");
}
}
@ -198,8 +198,8 @@ bool AtexitArray::expand_capacity() {
}
if (new_pages == MAP_FAILED) {
async_safe_format_log(ANDROID_LOG_WARN, "libc",
"__cxa_atexit: mmap/mremap failed to allocate %zu bytes: %s",
new_capacity_bytes, strerror(errno));
"__cxa_atexit: mmap/mremap failed to allocate %zu bytes: %m",
new_capacity_bytes);
} else {
result = true;
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, new_pages, new_capacity_bytes, "atexit handlers");

View file

@ -192,7 +192,7 @@ void BionicSmallObjectAllocator::alloc_page() {
void* const map_ptr =
mmap(nullptr, page_size(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (map_ptr == MAP_FAILED) {
async_safe_fatal("mmap failed: %s", strerror(errno));
async_safe_fatal("mmap failed: %m");
}
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, page_size(), "bionic_alloc_small_objects");
@ -273,7 +273,7 @@ void* BionicAllocator::alloc_mmap(size_t align, size_t size) {
-1, 0);
if (map_ptr == MAP_FAILED) {
async_safe_fatal("mmap failed: %s", strerror(errno));
async_safe_fatal("mmap failed: %m");
}
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, allocated_size, "bionic_alloc_lob");

View file

@ -87,7 +87,7 @@ FdEntry* FdTableImpl<inline_fds>::at(size_t idx) {
void* allocation =
mmap(nullptr, aligned_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (allocation == MAP_FAILED) {
async_safe_fatal("fdsan: mmap failed: %s", strerror(errno));
async_safe_fatal("fdsan: mmap failed: %m");
}
FdTableOverflow* new_overflow = reinterpret_cast<FdTableOverflow*>(allocation);

View file

@ -281,8 +281,7 @@ static void get_interface_flags_via_ioctl(ifaddrs** list) {
ScopedFd s(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
if (s.get() == -1) {
async_safe_format_log(ANDROID_LOG_ERROR, "libc",
"socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC) failed in ifaddrs: %s",
strerror(errno));
"socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC) failed in ifaddrs: %m");
return;
}
@ -294,8 +293,8 @@ static void get_interface_flags_via_ioctl(ifaddrs** list) {
addr->ifa.ifa_flags = ifr.ifr_flags;
} else {
async_safe_format_log(ANDROID_LOG_ERROR, "libc",
"ioctl(SIOCGIFFLAGS) for \"%s\" failed in ifaddrs: %s",
addr->ifa.ifa_name, strerror(errno));
"ioctl(SIOCGIFFLAGS) for \"%s\" failed in ifaddrs: %m",
addr->ifa.ifa_name);
}
}
}

View file

@ -340,11 +340,11 @@ static void __initialize_personality() {
#if !defined(__LP64__)
int old_value = personality(0xffffffff);
if (old_value == -1) {
async_safe_fatal("error getting old personality value: %s", strerror(errno));
async_safe_fatal("error getting old personality value: %m");
}
if (personality((static_cast<unsigned int>(old_value) & ~PER_MASK) | PER_LINUX32) == -1) {
async_safe_fatal("error setting PER_LINUX32 personality: %s", strerror(errno));
async_safe_fatal("error setting PER_LINUX32 personality: %m");
}
#endif
}

View file

@ -357,8 +357,7 @@ __attribute__((no_sanitize("hwaddress", "memtag"))) void __libc_init_mte(const v
void* pg_start =
reinterpret_cast<void*>(page_start(reinterpret_cast<uintptr_t>(stack_top)));
if (mprotect(pg_start, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_MTE | PROT_GROWSDOWN)) {
async_safe_fatal("error: failed to set PROT_MTE on main thread stack: %s\n",
strerror(errno));
async_safe_fatal("error: failed to set PROT_MTE on main thread stack: %m");
}
}

View file

@ -158,12 +158,12 @@ int pthread_attr_setstack(pthread_attr_t* attr, void* stack_base, size_t stack_s
static uintptr_t __get_main_stack_startstack() {
FILE* fp = fopen("/proc/self/stat", "re");
if (fp == nullptr) {
async_safe_fatal("couldn't open /proc/self/stat: %s", strerror(errno));
async_safe_fatal("couldn't open /proc/self/stat: %m");
}
char line[BUFSIZ];
if (fgets(line, sizeof(line), fp) == nullptr) {
async_safe_fatal("couldn't read /proc/self/stat: %s", strerror(errno));
async_safe_fatal("couldn't read /proc/self/stat: %m");
}
fclose(fp);
@ -205,7 +205,7 @@ static int __pthread_attr_getstack_main_thread(void** stack_base, size_t* stack_
// Hunt for the region that contains that address.
FILE* fp = fopen("/proc/self/maps", "re");
if (fp == nullptr) {
async_safe_fatal("couldn't open /proc/self/maps: %s", strerror(errno));
async_safe_fatal("couldn't open /proc/self/maps: %m");
}
char line[BUFSIZ];
while (fgets(line, sizeof(line), fp) != nullptr) {
@ -219,7 +219,7 @@ static int __pthread_attr_getstack_main_thread(void** stack_base, size_t* stack_
}
}
}
async_safe_fatal("Stack not found in /proc/self/maps");
async_safe_fatal("stack not found in /proc/self/maps");
}
__BIONIC_WEAK_FOR_NATIVE_BRIDGE

View file

@ -78,8 +78,7 @@ bionic_tls* __allocate_temp_bionic_tls() {
MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
if (allocation == MAP_FAILED) {
// Avoid strerror because it might need bionic_tls.
async_safe_fatal("failed to allocate bionic_tls: error %d", errno);
async_safe_fatal("failed to allocate bionic_tls: %m");
}
return static_cast<bionic_tls*>(allocation);
}
@ -138,8 +137,7 @@ static void __init_shadow_call_stack(pthread_internal_t* thread __unused) {
// stack pointer. This is deliberately the only place where the address is stored.
char* scs = scs_aligned_guard_region + scs_offset;
if (mprotect(scs, SCS_SIZE, PROT_READ | PROT_WRITE) == -1) {
async_safe_fatal("failed to mprotect() on shadow stack %p %d error %s", scs, SCS_SIZE,
strerror(errno));
async_safe_fatal("shadow stack read-write mprotect(%p, %d) failed: %m", scs, SCS_SIZE);
}
#if defined(__aarch64__)
__asm__ __volatile__("mov x18, %0" ::"r"(scs));
@ -174,12 +172,11 @@ int __init_thread(pthread_internal_t* thread) {
if (need_set) {
if (policy == -1) {
async_safe_format_log(ANDROID_LOG_WARN, "libc",
"pthread_create sched_getscheduler failed: %s", strerror(errno));
"pthread_create sched_getscheduler failed: %m");
return errno;
}
if (sched_getparam(0, &param) == -1) {
async_safe_format_log(ANDROID_LOG_WARN, "libc",
"pthread_create sched_getparam failed: %s", strerror(errno));
async_safe_format_log(ANDROID_LOG_WARN, "libc", "pthread_create sched_getparam failed: %m");
return errno;
}
}
@ -195,8 +192,8 @@ int __init_thread(pthread_internal_t* thread) {
if (need_set) {
if (sched_setscheduler(thread->tid, policy, &param) == -1) {
async_safe_format_log(ANDROID_LOG_WARN, "libc",
"pthread_create sched_setscheduler(%d, {%d}) call failed: %s", policy,
param.sched_priority, strerror(errno));
"pthread_create sched_setscheduler(%d, {%d}) call failed: %m", policy,
param.sched_priority);
#if defined(__LP64__)
// For backwards compatibility reasons, we only report failures on 64-bit devices.
return errno;
@ -231,10 +228,9 @@ ThreadMapping __allocate_thread_mapping(size_t stack_size, size_t stack_guard_si
const int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
char* const space = static_cast<char*>(mmap(nullptr, mmap_size, PROT_NONE, flags, -1, 0));
if (space == MAP_FAILED) {
async_safe_format_log(ANDROID_LOG_WARN,
"libc",
"pthread_create failed: couldn't allocate %zu-bytes mapped space: %s",
mmap_size, strerror(errno));
async_safe_format_log(ANDROID_LOG_WARN, "libc",
"pthread_create failed: couldn't allocate %zu-bytes mapped space: %m",
mmap_size);
return {};
}
const size_t writable_size = mmap_size - stack_guard_size - PTHREAD_GUARD_SIZE;
@ -249,8 +245,8 @@ ThreadMapping __allocate_thread_mapping(size_t stack_size, size_t stack_guard_si
if (mprotect(space + stack_guard_size, writable_size, prot) != 0) {
async_safe_format_log(
ANDROID_LOG_WARN, "libc",
"pthread_create failed: couldn't mprotect %s %zu-byte thread mapping region: %s", prot_str,
writable_size, strerror(errno));
"pthread_create failed: couldn't mprotect %s %zu-byte thread mapping region: %m", prot_str,
writable_size);
munmap(space, mmap_size);
return {};
}
@ -461,8 +457,7 @@ int pthread_create(pthread_t* thread_out, pthread_attr_t const* attr,
if (thread->mmap_size != 0) {
munmap(thread->mmap_base, thread->mmap_size);
}
async_safe_format_log(ANDROID_LOG_WARN, "libc", "pthread_create failed: clone failed: %s",
strerror(clone_errno));
async_safe_format_log(ANDROID_LOG_WARN, "libc", "pthread_create failed: clone failed: %m");
return clone_errno;
}

View file

@ -272,10 +272,9 @@ int __system_property_set(const char* key, const char* value) {
PropertyServiceConnection connection;
if (!connection.IsValid()) {
errno = connection.GetLastError();
async_safe_format_log(
ANDROID_LOG_WARN, "libc",
"Unable to set property \"%s\" to \"%s\": connection failed; errno=%d (%s)", key, value,
errno, strerror(errno));
async_safe_format_log(ANDROID_LOG_WARN, "libc",
"Unable to set property \"%s\" to \"%s\": connection failed: %m", key,
value);
return -1;
}
@ -283,8 +282,8 @@ int __system_property_set(const char* key, const char* value) {
if (!writer.WriteUint32(PROP_MSG_SETPROP2).WriteString(key).WriteString(value).Send()) {
errno = connection.GetLastError();
async_safe_format_log(ANDROID_LOG_WARN, "libc",
"Unable to set property \"%s\" to \"%s\": write failed; errno=%d (%s)",
key, value, errno, strerror(errno));
"Unable to set property \"%s\" to \"%s\": write failed: %m", key,
value);
return -1;
}
@ -292,8 +291,7 @@ int __system_property_set(const char* key, const char* value) {
if (!connection.RecvInt32(&result)) {
errno = connection.GetLastError();
async_safe_format_log(ANDROID_LOG_WARN, "libc",
"Unable to set property \"%s\" to \"%s\": recv failed; errno=%d (%s)",
key, value, errno, strerror(errno));
"Unable to set property \"%s\" to \"%s\": recv failed: %m", key, value);
return -1;
}