Skip to content

Commit 49d76c3

Browse files
authored
Merge pull request #31902 from compnerd/error-recording
runtime: improve error handling path for the runtime
2 parents ffca352 + 0637e17 commit 49d76c3

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

stdlib/public/LLVMSupport/ErrorHandling.cpp

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,50 @@
2121
# include <fcntl.h>
2222
#else
2323
# include <stdio.h>
24+
# include <unistd.h>
2425
#endif
2526

27+
#include <stdarg.h>
28+
29+
#if defined(__APPLE__)
30+
#include <asl.h>
31+
#elif defined(__ANDROID__)
32+
#include <android/log.h>
33+
#endif
34+
35+
namespace {
36+
void error(const char *fmt, ...) {
37+
char buffer[1024];
38+
va_list argp;
39+
40+
va_start(argp, fmt);
41+
vsnprintf(buffer, sizeof(buffer), fmt, argp);
42+
va_end(argp);
43+
44+
#if defined(__APPLE__)
45+
asl_log(nullptr, nullptr, ASL_LEVEL_ERR, "%s", buffer);
46+
#elif defined(__ANDROID__)
47+
__android_log_printf(ANDROID_LOG_FATAL, "SwiftRuntime", "%s", buffer);
48+
#elif defined(_WIN32)
49+
#define STDERR_FILENO 2
50+
_write(STDERR_FILENO, buffer, strlen(buffer));
51+
#else
52+
write(STDERR_FILENO, buffer, strlen(buffer));
53+
#endif
54+
}
55+
}
56+
2657
using namespace llvm;
2758

2859
void __swift::__runtime::llvm::report_fatal_error(const char *Reason,
2960
bool GenCrashDiag) {
30-
report_fatal_error(std::string(Reason), GenCrashDiag);
61+
error("LLVM ERROR: %s\n", Reason);
62+
abort();
3163
}
3264

3365
void __swift::__runtime::llvm::report_fatal_error(const std::string &Reason,
3466
bool GenCrashDiag) {
35-
// Blast the result out to stderr. We don't try hard to make sure this
36-
// succeeds (e.g. handling EINTR) and we can't use errs() here because
37-
// raw ostreams can call report_fatal_error.
38-
fprintf(stderr, "LLVM ERROR: %s\n", Reason.c_str());
39-
abort();
67+
report_fatal_error(Reason.c_str(), GenCrashDiag);
4068
}
4169

4270
void __swift::__runtime::llvm::report_fatal_error(StringRef Reason,
@@ -48,7 +76,7 @@ void __swift::__runtime::llvm::report_bad_alloc_error(const char *Reason,
4876
bool GenCrashDiag) {
4977
// Don't call the normal error handler. It may allocate memory. Directly write
5078
// an OOM to stderr and abort.
51-
fprintf(stderr, "LLVM ERROR: out of memory\n");
79+
error("LLVM ERROR: out of memory\n");
5280
abort();
5381
}
5482

@@ -58,11 +86,11 @@ void __swift::__runtime::llvm::llvm_unreachable_internal(
5886
// llvm_unreachable is intended to be used to indicate "impossible"
5987
// situations, and not legitimate runtime errors.
6088
if (msg)
61-
fprintf(stderr, "%s\n", msg);
62-
fprintf(stderr, "UNREACHABLE executed");
89+
error("%s\n", msg);
90+
error("UNREACHABLE executed");
6391
if (file)
64-
fprintf(stderr, " at %s:%u", file, line);
65-
fprintf(stderr, "!\n");
92+
error(" at %s:%u", file, line);
93+
error("!\n");
6694
abort();
6795
#ifdef LLVM_BUILTIN_UNREACHABLE
6896
// Windows systems and possibly others don't declare abort() to be noreturn,

0 commit comments

Comments
 (0)