Skip to content

Commit 4b88da2

Browse files
authored
Use the debugger hook, reportToDebugger, for all calls to _swift_stdlib_reportFatalError[InFile]. Only do this when a hidden frontend flag, -report-errors-to-debugger, is used. (#10617)
1 parent 056254b commit 4b88da2

File tree

7 files changed

+64
-0
lines changed

7 files changed

+64
-0
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ namespace swift {
185185
/// accesses.
186186
bool DisableTsanInoutInstrumentation = false;
187187

188+
/// \brief Staging flag for reporting runtime issues to the debugger.
189+
bool ReportErrorsToDebugger = false;
190+
188191
/// \brief Staging flag for class resilience, which we do not want to enable
189192
/// fully until more code is in place, to allow the standard library to be
190193
/// tested with value type resilience only.

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ def disable_tsan_inout_instrumentation : Flag<["-"],
294294
"disable-tsan-inout-instrumentation">,
295295
HelpText<"Disable treatment of inout parameters as Thread Sanitizer accesses">;
296296

297+
def report_errors_to_debugger : Flag<["-"], "report-errors-to-debugger">,
298+
HelpText<"Invoke the debugger hook on fatalError calls">;
299+
297300
def enable_infer_import_as_member :
298301
Flag<["-"], "enable-infer-import-as-member">,
299302
HelpText<"Infer when a global could be imported as a member">;

include/swift/Runtime/Debug.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ enum: uintptr_t {
210210
void reportToDebugger(uintptr_t flags, const char *message,
211211
RuntimeErrorDetails *details = nullptr);
212212

213+
SWIFT_RUNTIME_EXPORT
214+
bool _swift_reportFatalErrorsToDebugger;
215+
213216
// namespace swift
214217
}
215218

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
964964
Opts.DisableTsanInoutInstrumentation |=
965965
Args.hasArg(OPT_disable_tsan_inout_instrumentation);
966966

967+
Opts.ReportErrorsToDebugger |=
968+
Args.hasArg(OPT_report_errors_to_debugger);
969+
967970
if (FrontendOpts.InputKind == InputFileKind::IFK_SIL)
968971
Opts.DisableAvailabilityChecking = true;
969972

lib/IRGen/IRGenModule.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "llvm/ADT/PointerUnion.h"
4545
#include "llvm/Support/ErrorHandling.h"
4646
#include "llvm/Support/MD5.h"
47+
#include "llvm/Transforms/Utils/ModuleUtils.h"
4748

4849
#include "GenEnum.h"
4950
#include "GenType.h"
@@ -1042,6 +1043,31 @@ void IRGenModule::emitAutolinkInfo() {
10421043
}
10431044
}
10441045

1046+
void IRGenModule::emitEnableReportErrorsToDebugger() {
1047+
if (!Context.LangOpts.ReportErrorsToDebugger)
1048+
return;
1049+
1050+
if (!getSwiftModule()->hasEntryPoint())
1051+
return;
1052+
1053+
llvm::Function *NewFn = llvm::Function::Create(
1054+
llvm::FunctionType::get(VoidTy, false), llvm::GlobalValue::PrivateLinkage,
1055+
"_swift_enable_report_errors_to_debugger");
1056+
IRGenFunction NewIGF(*this, NewFn);
1057+
NewFn->setAttributes(constructInitialAttributes());
1058+
Module.getFunctionList().push_back(NewFn);
1059+
NewFn->setCallingConv(DefaultCC);
1060+
1061+
llvm::Value *addr =
1062+
Module.getOrInsertGlobal("_swift_reportFatalErrorsToDebugger", Int1Ty);
1063+
llvm::Value *one = llvm::ConstantInt::get(Int1Ty, 1);
1064+
1065+
NewIGF.Builder.CreateStore(one, addr, Alignment(1));
1066+
NewIGF.Builder.CreateRetVoid();
1067+
1068+
llvm::appendToGlobalCtors(Module, NewFn, 0, nullptr);
1069+
}
1070+
10451071
void IRGenModule::cleanupClangCodeGenMetadata() {
10461072
// Remove llvm.ident that ClangCodeGen might have left in the module.
10471073
auto *LLVMIdent = Module.getNamedMetadata("llvm.ident");
@@ -1110,6 +1136,7 @@ bool IRGenModule::finalize() {
11101136
return false;
11111137

11121138
emitAutolinkInfo();
1139+
emitEnableReportErrorsToDebugger();
11131140
emitGlobalLists();
11141141
if (DebugInfo)
11151142
DebugInfo->finalize();

lib/IRGen/IRGenModule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ class IRGenModule {
795795

796796
void emitGlobalLists();
797797
void emitAutolinkInfo();
798+
void emitEnableReportErrorsToDebugger();
798799
void cleanupClangCodeGenMetadata();
799800

800801
//--- Remote reflection metadata --------------------------------------------

stdlib/public/stubs/Assert.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
using namespace swift;
2323

24+
bool swift::_swift_reportFatalErrorsToDebugger = false;
25+
2426
static int swift_asprintf(char **strp, const char *fmt, ...) {
2527
va_list args;
2628
va_start(args, fmt);
@@ -49,13 +51,33 @@ static int swift_asprintf(char **strp, const char *fmt, ...) {
4951
return result;
5052
}
5153

54+
static void logPrefixAndMessageToDebugger(
55+
const unsigned char *prefix, int prefixLength,
56+
const unsigned char *message, int messageLength
57+
) {
58+
if (!_swift_reportFatalErrorsToDebugger)
59+
return;
60+
61+
char *debuggerMessage;
62+
if (messageLength) {
63+
swift_asprintf(&debuggerMessage, "%.*s: %.*s", prefixLength, prefix,
64+
messageLength, message);
65+
} else {
66+
swift_asprintf(&debuggerMessage, "%.*s", prefixLength, prefix);
67+
}
68+
reportToDebugger(RuntimeErrorFlagFatal, debuggerMessage);
69+
free(debuggerMessage);
70+
}
71+
5272
void swift::_swift_stdlib_reportFatalErrorInFile(
5373
const unsigned char *prefix, int prefixLength,
5474
const unsigned char *message, int messageLength,
5575
const unsigned char *file, int fileLength,
5676
uint32_t line,
5777
uint32_t flags
5878
) {
79+
logPrefixAndMessageToDebugger(prefix, prefixLength, message, messageLength);
80+
5981
char *log;
6082
swift_asprintf(
6183
&log, "%.*s: %.*s%sfile %.*s, line %" PRIu32 "\n",
@@ -74,6 +96,8 @@ void swift::_swift_stdlib_reportFatalError(
7496
const unsigned char *message, int messageLength,
7597
uint32_t flags
7698
) {
99+
logPrefixAndMessageToDebugger(prefix, prefixLength, message, messageLength);
100+
77101
char *log;
78102
swift_asprintf(
79103
&log, "%.*s: %.*s\n",

0 commit comments

Comments
 (0)