• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- ErrorHandler.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lld/Common/ErrorHandler.h"
10 
11 #include "llvm/Support/Parallel.h"
12 
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/IR/DiagnosticInfo.h"
15 #include "llvm/IR/DiagnosticPrinter.h"
16 #include "llvm/Support/CrashRecoveryContext.h"
17 #include "llvm/Support/ManagedStatic.h"
18 #include "llvm/Support/Process.h"
19 #include "llvm/Support/Program.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <mutex>
22 #include <regex>
23 
24 using namespace llvm;
25 using namespace lld;
26 
27 // The functions defined in this file can be called from multiple threads,
28 // but lld::outs() or lld::errs() are not thread-safe. We protect them using a
29 // mutex.
30 static std::mutex mu;
31 
32 // We want to separate multi-line messages with a newline. `sep` is "\n"
33 // if the last messages was multi-line. Otherwise "".
34 static StringRef sep;
35 
getSeparator(const Twine & msg)36 static StringRef getSeparator(const Twine &msg) {
37   if (StringRef(msg.str()).contains('\n'))
38     return "\n";
39   return "";
40 }
41 
42 raw_ostream *lld::stdoutOS;
43 raw_ostream *lld::stderrOS;
44 
errorHandler()45 ErrorHandler &lld::errorHandler() {
46   static ErrorHandler handler;
47   return handler;
48 }
49 
outs()50 raw_ostream &lld::outs() {
51   if (errorHandler().disableOutput)
52     return llvm::nulls();
53   return stdoutOS ? *stdoutOS : llvm::outs();
54 }
55 
errs()56 raw_ostream &lld::errs() {
57   if (errorHandler().disableOutput)
58     return llvm::nulls();
59   return stderrOS ? *stderrOS : llvm::errs();
60 }
61 
exitLld(int val)62 void lld::exitLld(int val) {
63   // Delete any temporary file, while keeping the memory mapping open.
64   if (errorHandler().outputBuffer)
65     errorHandler().outputBuffer->discard();
66 
67   // Re-throw a possible signal or exception once/if it was catched by
68   // safeLldMain().
69   CrashRecoveryContext::throwIfCrash(val);
70 
71   // Dealloc/destroy ManagedStatic variables before calling _exit().
72   // In an LTO build, allows us to get the output of -time-passes.
73   // Ensures that the thread pool for the parallel algorithms is stopped to
74   // avoid intermittent crashes on Windows when exiting.
75   if (!CrashRecoveryContext::GetCurrent())
76     llvm_shutdown();
77 
78   {
79     std::lock_guard<std::mutex> lock(mu);
80     lld::outs().flush();
81     lld::errs().flush();
82   }
83   // When running inside safeLldMain(), restore the control flow back to the
84   // CrashRecoveryContext. Otherwise simply use _exit(), meanning no cleanup,
85   // since we want to avoid further crashes on shutdown.
86   llvm::sys::Process::Exit(val, /*NoCleanup=*/true);
87 }
88 
diagnosticHandler(const DiagnosticInfo & di)89 void lld::diagnosticHandler(const DiagnosticInfo &di) {
90   SmallString<128> s;
91   raw_svector_ostream os(s);
92   DiagnosticPrinterRawOStream dp(os);
93   di.print(dp);
94   switch (di.getSeverity()) {
95   case DS_Error:
96     error(s);
97     break;
98   case DS_Warning:
99     warn(s);
100     break;
101   case DS_Remark:
102   case DS_Note:
103     message(s);
104     break;
105   }
106 }
107 
checkError(Error e)108 void lld::checkError(Error e) {
109   handleAllErrors(std::move(e),
110                   [&](ErrorInfoBase &eib) { error(eib.message()); });
111 }
112 
113 // This is for --vs-diagnostics.
114 //
115 // Normally, lld's error message starts with argv[0]. Therefore, it usually
116 // looks like this:
117 //
118 //   ld.lld: error: ...
119 //
120 // This error message style is unfortunately unfriendly to Visual Studio
121 // IDE. VS interprets the first word of the first line as an error location
122 // and make it clickable, thus "ld.lld" in the above message would become a
123 // clickable text. When you click it, VS opens "ld.lld" executable file with
124 // a binary editor.
125 //
126 // As a workaround, we print out an error location instead of "ld.lld" if
127 // lld is running in VS diagnostics mode. As a result, error message will
128 // look like this:
129 //
130 //   src/foo.c(35): error: ...
131 //
132 // This function returns an error location string. An error location is
133 // extracted from an error message using regexps.
getLocation(const Twine & msg)134 std::string ErrorHandler::getLocation(const Twine &msg) {
135   if (!vsDiagnostics)
136     return std::string(logName);
137 
138   static std::regex regexes[] = {
139       std::regex(
140           R"(^undefined (?:\S+ )?symbol:.*\n)"
141           R"(>>> referenced by .+\((\S+):(\d+)\))"),
142       std::regex(
143           R"(^undefined (?:\S+ )?symbol:.*\n>>> referenced by (\S+):(\d+))"),
144       std::regex(R"(^undefined symbol:.*\n>>> referenced by (.*):)"),
145       std::regex(
146           R"(^duplicate symbol: .*\n>>> defined in (\S+)\n>>> defined in.*)"),
147       std::regex(
148           R"(^duplicate symbol: .*\n>>> defined at .+\((\S+):(\d+)\))"),
149       std::regex(R"(^duplicate symbol: .*\n>>> defined at (\S+):(\d+))"),
150       std::regex(
151           R"(.*\n>>> defined in .*\n>>> referenced by .+\((\S+):(\d+)\))"),
152       std::regex(R"(.*\n>>> defined in .*\n>>> referenced by (\S+):(\d+))"),
153       std::regex(R"((\S+):(\d+): unclosed quote)"),
154   };
155 
156   std::string str = msg.str();
157   for (std::regex &re : regexes) {
158     std::smatch m;
159     if (!std::regex_search(str, m, re))
160       continue;
161 
162     assert(m.size() == 2 || m.size() == 3);
163     if (m.size() == 2)
164       return m.str(1);
165     return m.str(1) + "(" + m.str(2) + ")";
166   }
167 
168   return std::string(logName);
169 }
170 
log(const Twine & msg)171 void ErrorHandler::log(const Twine &msg) {
172   if (!verbose || disableOutput)
173     return;
174   std::lock_guard<std::mutex> lock(mu);
175   lld::errs() << logName << ": " << msg << "\n";
176 }
177 
message(const Twine & msg)178 void ErrorHandler::message(const Twine &msg) {
179   if (disableOutput)
180     return;
181   std::lock_guard<std::mutex> lock(mu);
182   lld::outs() << msg << "\n";
183   lld::outs().flush();
184 }
185 
warn(const Twine & msg)186 void ErrorHandler::warn(const Twine &msg) {
187   if (fatalWarnings) {
188     error(msg);
189     return;
190   }
191 
192   std::lock_guard<std::mutex> lock(mu);
193   lld::errs() << sep << getLocation(msg) << ": " << Colors::MAGENTA
194               << "warning: " << Colors::RESET << msg << "\n";
195   sep = getSeparator(msg);
196 }
197 
error(const Twine & msg)198 void ErrorHandler::error(const Twine &msg) {
199   // If Visual Studio-style error message mode is enabled,
200   // this particular error is printed out as two errors.
201   if (vsDiagnostics) {
202     static std::regex re(R"(^(duplicate symbol: .*))"
203                          R"((\n>>> defined at \S+:\d+.*\n>>>.*))"
204                          R"((\n>>> defined at \S+:\d+.*\n>>>.*))");
205     std::string str = msg.str();
206     std::smatch m;
207 
208     if (std::regex_match(str, m, re)) {
209       error(m.str(1) + m.str(2));
210       error(m.str(1) + m.str(3));
211       return;
212     }
213   }
214 
215   bool exit = false;
216   {
217     std::lock_guard<std::mutex> lock(mu);
218 
219     if (errorLimit == 0 || errorCount < errorLimit) {
220       lld::errs() << sep << getLocation(msg) << ": " << Colors::RED
221                   << "error: " << Colors::RESET << msg << "\n";
222     } else if (errorCount == errorLimit) {
223       lld::errs() << sep << getLocation(msg) << ": " << Colors::RED
224                   << "error: " << Colors::RESET << errorLimitExceededMsg
225                   << "\n";
226       exit = exitEarly;
227     }
228 
229     sep = getSeparator(msg);
230     ++errorCount;
231   }
232 
233   if (exit)
234     exitLld(1);
235 }
236 
error(const Twine & msg,ErrorTag tag,ArrayRef<StringRef> args)237 void ErrorHandler::error(const Twine &msg, ErrorTag tag,
238                          ArrayRef<StringRef> args) {
239   if (errorHandlingScript.empty()) {
240     error(msg);
241     return;
242   }
243   SmallVector<StringRef, 4> scriptArgs;
244   scriptArgs.push_back(errorHandlingScript);
245   switch (tag) {
246   case ErrorTag::LibNotFound:
247     scriptArgs.push_back("missing-lib");
248     break;
249   case ErrorTag::SymbolNotFound:
250     scriptArgs.push_back("undefined-symbol");
251     break;
252   }
253   scriptArgs.insert(scriptArgs.end(), args.begin(), args.end());
254   int res = llvm::sys::ExecuteAndWait(errorHandlingScript, scriptArgs);
255   if (res == 0) {
256     return error(msg);
257   } else {
258     // Temporarily disable error limit to make sure the two calls to error(...)
259     // only count as one.
260     uint64_t currentErrorLimit = errorLimit;
261     errorLimit = 0;
262     error(msg);
263     errorLimit = currentErrorLimit;
264     --errorCount;
265 
266     switch (res) {
267     case -1:
268       error("error handling script '" + errorHandlingScript +
269             "' failed to execute");
270       break;
271     case -2:
272       error("error handling script '" + errorHandlingScript +
273             "' crashed or timeout");
274       break;
275     default:
276       error("error handling script '" + errorHandlingScript +
277             "' exited with code " + Twine(res));
278     }
279   }
280 }
281 
fatal(const Twine & msg)282 void ErrorHandler::fatal(const Twine &msg) {
283   error(msg);
284   exitLld(1);
285 }
286