From c8d8ddd88b2b70f811953fa4bf19c2b0b40a613f Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Sat, 20 Feb 2021 22:54:46 +0000 Subject: [PATCH] better distinguish mmap errors & print errno Motivation: In case SwiftBacktrace fails, we get only very little output. Unfortunately, some crucial information (like the errno) is missing and in case `mmap` fails, we don't know if the problem is file I/O or allocating memory. Modification: - print errnos - distinguish the two mmaps Result: Easier debugging --- Sources/Backtrace/Backtrace.swift | 6 +++--- Sources/CBacktrace/mmap.c | 2 +- Sources/CBacktrace/mmapio.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Sources/Backtrace/Backtrace.swift b/Sources/Backtrace/Backtrace.swift index 30b5dfd..1e481cf 100644 --- a/Sources/Backtrace/Backtrace.swift +++ b/Sources/Backtrace/Backtrace.swift @@ -53,10 +53,10 @@ private let fullCallback: CBacktraceFullCallback? = { } private let errorCallback: CBacktraceErrorCallback? = { - _, msg, _ in + _, msg, errNo in if let msg = msg { - _ = withVaList([msg]) { vaList in - vfprintf(stderr, "%s\n", vaList) + _ = withVaList([msg, errNo]) { vaList in + vfprintf(stderr, "SwiftBacktrace ERROR: %s (errno: %d)\n", vaList) } } } diff --git a/Sources/CBacktrace/mmap.c b/Sources/CBacktrace/mmap.c index d17bc89..320f0a1 100644 --- a/Sources/CBacktrace/mmap.c +++ b/Sources/CBacktrace/mmap.c @@ -170,7 +170,7 @@ backtrace_alloc (struct backtrace_state *state, if (page == MAP_FAILED) { if (error_callback) - error_callback (data, "mmap", errno); + error_callback (data, "mmap for alloc", errno); } else { diff --git a/Sources/CBacktrace/mmapio.c b/Sources/CBacktrace/mmapio.c index 3834819..2f84cd7 100644 --- a/Sources/CBacktrace/mmapio.c +++ b/Sources/CBacktrace/mmapio.c @@ -71,7 +71,7 @@ backtrace_get_view (struct backtrace_state *state ATTRIBUTE_UNUSED, map = mmap (NULL, size, PROT_READ, MAP_PRIVATE, descriptor, pageoff); if (map == MAP_FAILED) { - error_callback (data, "mmap", errno); + error_callback (data, "mmap file i/o", errno); return 0; }