forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrors.cpp
185 lines (152 loc) · 5.4 KB
/
Errors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//===--- Errors.cpp - Error reporting utilities ---------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Utilities for reporting errors to stderr, system console, and crash logs.
//
//===----------------------------------------------------------------------===//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <pthread.h>
#include <stdarg.h>
#include "swift/Runtime/Debug.h"
#ifdef __APPLE__
#include <asl.h>
#endif
#ifdef SWIFT_HAVE_CRASHREPORTERCLIENT
#include <malloc/malloc.h>
// Instead of linking to CrashReporterClient.a (because it complicates the
// build system), define the only symbol from that static archive ourselves.
//
// The layout of this struct is CrashReporter ABI, so there are no ABI concerns
// here.
extern "C" {
CRASH_REPORTER_CLIENT_HIDDEN
struct crashreporter_annotations_t gCRAnnotations
__attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) = {
CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0, 0};
}
// Report a message to any forthcoming crash log.
static void
reportOnCrash(const char *message)
{
static pthread_mutex_t crashlogLock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&crashlogLock);
char *oldMessage = (char *)CRGetCrashLogMessage();
char *newMessage;
if (oldMessage) {
asprintf(&newMessage, "%s%s", oldMessage, message);
if (malloc_size(oldMessage)) free(oldMessage);
} else {
newMessage = strdup(message);
}
CRSetCrashLogMessage(newMessage);
pthread_mutex_unlock(&crashlogLock);
}
#else
static void
reportOnCrash(const char *message)
{
// empty
}
#endif
// Report a message to system console and stderr.
static void
reportNow(const char *message)
{
write(STDERR_FILENO, message, strlen(message));
#ifdef __APPLE__
asl_log(NULL, NULL, ASL_LEVEL_ERR, "%s", message);
#endif
}
// Report a fatal error to system console, stderr, and crash logs, then abort.
LLVM_ATTRIBUTE_NORETURN
void
swift::fatalError(const char *format, ...)
{
va_list args;
va_start(args, format);
char *log;
vasprintf(&log, format, args);
reportNow(log);
reportOnCrash(log);
abort();
}
// Report a fatal error to system console, stderr, and crash logs.
// <prefix>: <message>: file <file>, line <line>\n
// The message may be omitted by passing messageLength=0.
extern "C" void
swift_reportFatalErrorInFile(const char *prefix, intptr_t prefixLength,
const char *message, intptr_t messageLength,
const char *file, intptr_t fileLength,
uintptr_t line) {
char *log;
asprintf(&log, "%.*s: %.*s%sfile %.*s, line %zu\n", (int)prefixLength, prefix,
(int)messageLength, message, (messageLength ? ": " : ""),
(int)fileLength, file, (size_t)line);
reportNow(log);
reportOnCrash(log);
free(log);
}
// Report a fatal error to system console, stderr, and crash logs.
// <prefix>: <message>: file <file>, line <line>\n
// The message may be omitted by passing messageLength=0.
extern "C" void swift_reportFatalError(const char *prefix,
intptr_t prefixLength,
const char *message,
intptr_t messageLength) {
char *log;
asprintf(&log, "%.*s: %.*s\n", (int)prefixLength, prefix,
(int)messageLength, message);
reportNow(log);
reportOnCrash(log);
free(log);
}
// Report a call to an unimplemented initializer.
// <file>: <line>: <column>: fatal error: use of unimplemented
// initializer '<initName>' for class 'className'
extern "C" void swift_reportUnimplementedInitializerInFile(
const char *className, intptr_t classNameLength, const char *initName,
intptr_t initNameLength, const char *file, intptr_t fileLength,
uintptr_t line, uintptr_t column) {
char *log;
asprintf(&log, "%.*s: %zu: %zu: fatal error: use of unimplemented "
"initializer '%.*s' for class '%.*s'\n",
(int)fileLength, file, (size_t)line, (size_t)column,
(int)initNameLength, initName, (int)classNameLength, className);
reportNow(log);
reportOnCrash(log);
free(log);
}
// Report a call to an unimplemented initializer.
// fatal error: use of unimplemented initializer '<initName>' for class
// 'className'
extern "C" void swift_reportUnimplementedInitializer(const char *className,
intptr_t classNameLength,
const char *initName,
intptr_t initNameLength) {
char *log;
asprintf(&log, "fatal error: use of unimplemented "
"initializer '%.*s' for class '%.*s'\n",
(int)initNameLength, initName, (int)classNameLength, className);
reportNow(log);
reportOnCrash(log);
free(log);
}
// Report a call to a removed method.
LLVM_ATTRIBUTE_NORETURN
extern "C" void
swift_reportMissingMethod() {
swift::fatalError("fatal error: call of removed method\n");
}