1 module core.internal.abort;
2 
3 /*
4  * Use instead of assert(0, msg), since this does not print a message for -release compiled
5  * code, and druntime is -release compiled.
6  */
7 void abort(string msg, string filename = __FILE__, size_t line = __LINE__) @nogc nothrow @safe
8 {
9     import core.stdc.stdlib: c_abort = abort;
10     // use available OS system calls to print the message to stderr
version(Posix)11     version (Posix)
12     {
13         import core.sys.posix.unistd: write;
14         static void writeStr(const(char)[][] m...) @nogc nothrow @trusted
15         {
16             foreach (s; m)
17                 write(2, s.ptr, s.length);
18         }
19     }
version(Windows)20     else version (Windows)
21     {
22         import core.sys.windows.windows: GetStdHandle, STD_ERROR_HANDLE, WriteFile, INVALID_HANDLE_VALUE;
23         auto h = (() @trusted => GetStdHandle(STD_ERROR_HANDLE))();
24         if (h == INVALID_HANDLE_VALUE)
25             // attempt best we can to print the message
26             assert(0, msg);
27         void writeStr(const(char)[][] m...) @nogc nothrow @trusted
28         {
29             foreach (s; m)
30             {
31                 assert(s.length <= uint.max);
32                 WriteFile(h, s.ptr, cast(uint)s.length, null, null);
33             }
34         }
35     }
36     else
37         static assert(0, "Unsupported OS");
38 
39     import core.internal.string;
40     UnsignedStringBuf strbuff;
41 
42     // write an appropriate message, then abort the program
43     writeStr("Aborting from ", filename, "(", line.unsignedToTempString(strbuff, 10), ") ", msg);
44     c_abort();
45 }
46