2 * Contains druntime startup and shutdown routines.
4 * Copyright: Copyright Digital Mars 2000 - 2018.
5 * License: Distributed under the
6 * $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
7 * (See accompanying file LICENSE)
8 * Authors: Walter Bright, Sean Kelly
9 * Source: $(DRUNTIMESRC rt/_dmain2.d)
12 /* NOTE: This file has been patched from the original DMD distribution to
13 * work with the GDC compiler.
20 import core.stdc.stddef;
21 import core.stdc.stdlib;
22 import core.stdc.string;
23 import core.stdc.stdio; // for printf()
24 import core.stdc.errno : errno;
28 import core.stdc.wchar_;
29 import core.sys.windows.basetsd : HANDLE;
30 import core.sys.windows.shellapi : CommandLineToArgvW;
31 import core.sys.windows.winbase : FreeLibrary, GetCommandLineW, GetProcAddress,
32 IsDebuggerPresent, LoadLibraryW, LocalFree, WriteFile;
33 import core.sys.windows.wincon : CONSOLE_SCREEN_BUFFER_INFO, GetConsoleOutputCP,
34 GetConsoleScreenBufferInfo;
35 import core.sys.windows.winnls : CP_UTF8, MultiByteToWideChar, WideCharToMultiByte;
36 import core.sys.windows.winnt : WCHAR;
37 import core.sys.windows.winuser : MB_ICONERROR, MessageBoxW;
39 pragma(lib, "shell32.lib"); // needed for CommandLineToArgvW
44 import core.stdc.fenv;
48 import core.stdc.fenv;
50 version (DragonFlyBSD)
52 import core.stdc.fenv;
55 // not sure why we can't define this in one place, but this is to keep this
56 // module from importing core.runtime.
65 extern (C) void _d_monitor_staticctor();
66 extern (C) void _d_monitor_staticdtor();
67 extern (C) void _d_critical_init();
68 extern (C) void _d_critical_term();
69 extern (C) void gc_init();
70 extern (C) void gc_term();
71 extern (C) void thread_init() @nogc;
72 extern (C) void thread_term() @nogc;
73 extern (C) void lifetime_init();
74 extern (C) void rt_moduleCtor();
75 extern (C) void rt_moduleTlsCtor();
76 extern (C) void rt_moduleDtor();
77 extern (C) void rt_moduleTlsDtor();
78 extern (C) void thread_joinAll();
79 extern (C) UnitTestResult runModuleUnitTests();
80 extern (C) void _d_initMonoTime();
82 version (CRuntime_Microsoft)
84 extern(C) void init_msvc();
87 /* To get out-of-band access to the args[] passed to main().
90 __gshared string[] _d_args = null;
92 extern (C) string[] rt_args()
97 // This variable is only ever set by a debugger on initialization so it should
98 // be fine to leave it as __gshared.
99 extern (C) __gshared bool rt_trapExceptions = true;
101 alias void delegate(Throwable) ExceptionHandler;
104 * Keep track of how often rt_init/rt_term were called.
106 shared size_t _initCount;
108 /**********************************************
109 * Initialize druntime.
110 * If a C program wishes to call D code, and there's no D main(), then it
111 * must call rt_init() and rt_term().
113 extern (C) int rt_init()
115 /* @@BUG 11380 @@ Need to synchronize rt_init/rt_term calls for
116 version (Shared) druntime, because multiple C threads might
117 initialize different D libraries without knowing about the
118 shared druntime. Also we need to attach any thread that calls
120 if (atomicOp!"+="(_initCount, 1) > 1) return 1;
122 version (CRuntime_Microsoft)
125 _d_monitor_staticctor();
131 // this initializes mono time before anything else to allow usage
132 // in other druntime systems.
135 // TODO: fixme - calls GC.addRange -> Initializes GC
144 atomicStore!(MemoryOrder.raw)(_initCount, 0);
145 _d_print_throwable(t);
148 _d_monitor_staticdtor();
152 /**********************************************
153 * Terminate use of druntime.
155 extern (C) int rt_term()
157 if (atomicLoad!(MemoryOrder.raw)(_initCount) == 0) return 0; // was never initialized
158 if (atomicOp!"-="(_initCount, 1)) return 1;
171 _d_print_throwable(t);
177 _d_monitor_staticdtor();
182 /**********************************************
185 alias Throwable.TraceInfo function(void* ptr) TraceHandler;
186 private __gshared TraceHandler traceHandler = null;
190 * Overrides the default trace hander with a user-supplied version.
193 * h = The new trace handler. Set to null to use the default handler.
195 extern (C) void rt_setTraceHandler(TraceHandler h)
201 * Return the current trace handler
203 extern (C) TraceHandler rt_getTraceHandler()
209 * This function will be called when an exception is constructed. The
210 * user-supplied trace handler will be called if one has been supplied,
211 * otherwise no trace will be generated.
214 * ptr = A pointer to the location from which to generate the trace, or null
215 * if the trace should be generated from within the trace handler
219 * An object describing the current calling context or null if no handler is
222 extern (C) Throwable.TraceInfo _d_traceContext(void* ptr = null)
224 if (traceHandler is null)
226 return traceHandler(ptr);
229 /***********************************
230 * Provide out-of-band access to the original C argc/argv
231 * passed to this program via main(argc,argv).
240 __gshared CArgs _cArgs;
242 extern (C) CArgs rt_cArgs() @nogc
247 /// Type of the D main() function (`_Dmain`).
248 private alias extern(C) int function(char[][] args) MainFunc;
251 * Sets up the D char[][] command-line args, initializes druntime,
252 * runs embedded unittests and then runs the given D main() function,
253 * optionally catching and printing any unhandled exceptions.
255 extern (C) int _d_run_main(int argc, char** argv, MainFunc mainFunc)
257 // Set up _cArgs and array of D char[] slices, then forward to _d_run_main2
259 // Remember the original C argc/argv
265 /* Because we want args[] to be UTF-8, and Windows doesn't guarantee that,
266 * we ignore argc/argv and go get the Windows command line again as UTF-16.
267 * Then, reparse into wargc/wargs, and then use Windows API to convert
270 const wCommandLine = GetCommandLineW();
271 immutable size_t wCommandLineLength = wcslen(wCommandLine);
273 auto wargs = CommandLineToArgvW(wCommandLine, &wargc);
274 // assert(wargc == argc); /* argc can be broken by Unicode arguments */
276 // Allocate args[] on the stack - use wargc
277 char[][] args = (cast(char[]*) alloca(wargc * (char[]).sizeof))[0 .. wargc];
279 // This is required because WideCharToMultiByte requires int as input.
280 assert(wCommandLineLength <= cast(size_t) int.max, "Wide char command line length must not exceed int.max");
282 immutable size_t totalArgsLength = WideCharToMultiByte(CP_UTF8, 0, wCommandLine, cast(int)wCommandLineLength, null, 0, null, null);
284 char* totalArgsBuff = cast(char*) alloca(totalArgsLength);
286 foreach (i; 0 .. wargc)
288 immutable size_t wlen = wcslen(wargs[i]);
289 assert(wlen <= cast(size_t) int.max, "wlen cannot exceed int.max");
290 immutable int len = WideCharToMultiByte(CP_UTF8, 0, &wargs[i][0], cast(int) wlen, null, 0, null, null);
291 args[i] = totalArgsBuff[j .. j + len];
295 assert(j <= totalArgsLength);
296 WideCharToMultiByte(CP_UTF8, 0, &wargs[i][0], cast(int) wlen, &args[i][0], len, null, null);
305 // Allocate args[] on the stack
306 char[][] args = (cast(char[]*) alloca(argc * (char[]).sizeof))[0 .. argc];
308 size_t totalArgsLength = 0;
309 foreach (i, ref arg; args)
311 arg = argv[i][0 .. strlen(argv[i])];
312 totalArgsLength += arg.length;
318 return _d_run_main2(args, totalArgsLength, mainFunc);
322 * Windows-specific version for wide command-line arguments, e.g.,
323 * from a wmain/wWinMain C entry point.
324 * This wide version uses the specified arguments, unlike narrow
325 * _d_run_main which uses the actual (wide) process arguments instead.
328 extern (C) int _d_wrun_main(int argc, wchar** wargv, MainFunc mainFunc)
330 // Allocate args[] on the stack
331 char[][] args = (cast(char[]*) alloca(argc * (char[]).sizeof))[0 .. argc];
333 // 1st pass: compute each argument's length as UTF-16 and UTF-8
334 size_t totalArgsLength = 0;
335 foreach (i; 0 .. argc)
337 const warg = wargv[i];
338 const size_t wlen = wcslen(warg) + 1; // incl. terminating null
339 assert(wlen <= cast(size_t) int.max, "wlen cannot exceed int.max");
340 const int len = WideCharToMultiByte(CP_UTF8, 0, warg, cast(int) wlen, null, 0, null, null);
341 args[i] = (cast(char*) wlen)[0 .. len]; // args[i].ptr = wlen, args[i].length = len
342 totalArgsLength += len;
345 // Allocate a single buffer for all (null-terminated) argument strings in UTF-8 on the stack
346 char* utf8Buffer = cast(char*) alloca(totalArgsLength);
348 // 2nd pass: convert to UTF-8 and finalize `args`
349 char* utf8 = utf8Buffer;
350 foreach (i; 0 .. argc)
352 const wlen = cast(int) args[i].ptr;
353 const len = cast(int) args[i].length;
354 WideCharToMultiByte(CP_UTF8, 0, wargv[i], wlen, utf8, len, null, null);
355 args[i] = utf8[0 .. len-1]; // excl. terminating null
359 // Set C argc/argv; argv is a new stack-allocated array of UTF-8 C strings
360 char*[] argv = (cast(char**) alloca(argc * (char*).sizeof))[0 .. argc];
361 foreach (i, ref arg; argv)
364 _cArgs.argv = argv.ptr;
366 totalArgsLength -= argc; // excl. null terminator per arg
367 return _d_run_main2(args, totalArgsLength, mainFunc);
370 private extern (C) int _d_run_main2(char[][] args, size_t totalArgsLength, MainFunc mainFunc)
374 version (FreeBSD) version (D_InlineAsm_X86)
377 * FreeBSD/i386 sets the FPU precision mode to 53 bit double.
378 * Make it 64 bit extended.
384 or fpucw, 0b11_00_111111; // 11: use 64 bit extended-precision
385 // 111111: mask all FP exceptions
389 version (CRuntime_Microsoft)
391 // enable full precision for reals
392 version (D_InlineAsm_X86_64)
397 fstcw word ptr [RSP];
398 or [RSP], 0b11_00_111111; // 11: use 64 bit extended-precision
399 // 111111: mask all FP exceptions
400 fldcw word ptr [RSP];
404 else version (D_InlineAsm_X86)
409 fstcw word ptr [ESP];
410 or [ESP], 0b11_00_111111; // 11: use 64 bit extended-precision
411 // 111111: mask all FP exceptions
412 fldcw word ptr [ESP];
416 else version (GNU_InlineAsm)
419 asm { "fstcw %0" : "=m" (fpu_cw); }
420 fpu_cw |= 0b11_00_111111; // 11: use 64 bit extended-precision
421 // 111111: mask all FP exceptions
422 asm { "fldcw %0" : "=m" (fpu_cw); }
426 /* Create a copy of args[] on the stack to be used for main, so that rt_args()
427 * cannot be modified by the user.
428 * Note that when this function returns, _d_args will refer to garbage.
431 _d_args = cast(string[]) args;
432 auto buff = cast(char[]*) alloca(args.length * (char[]).sizeof + totalArgsLength);
434 char[][] argsCopy = buff[0 .. args.length];
435 auto argBuff = cast(char*) (buff + args.length);
437 import rt.config : rt_cmdline_enabled;
438 bool parseOpts = rt_cmdline_enabled!();
441 // Do not pass Druntime options to the program
442 if (parseOpts && arg.length >= 6 && arg[0 .. 6] == "--DRT-")
444 // https://issues.dlang.org/show_bug.cgi?id=20459
447 argsCopy[j++] = (argBuff[0 .. arg.length] = arg[]);
448 argBuff += arg.length;
450 args = argsCopy[0..j];
453 auto useExceptionTrap = parseExceptionOptions();
457 if (IsDebuggerPresent())
458 useExceptionTrap = false;
461 void tryExec(scope void delegate() dg)
463 if (useExceptionTrap)
471 _d_print_throwable(t);
472 result = EXIT_FAILURE;
481 // NOTE: The lifetime of a process is much like the lifetime of an object:
482 // it is initialized, then used, then destroyed. If initialization
483 // fails, the successive two steps are never reached. However, if
484 // initialization succeeds, then cleanup will occur even if the use
485 // step fails in some way. Here, the use phase consists of running
486 // the user's main function. If main terminates with an exception,
487 // the exception is handled and then cleanup begins. An exception
488 // thrown during cleanup, however, will abort the cleanup process.
493 auto utResult = runModuleUnitTests();
494 assert(utResult.passed <= utResult.executed);
495 if (utResult.passed == utResult.executed)
497 if (utResult.summarize)
499 if (utResult.passed == 0)
500 .fprintf(.stderr, "No unittests run\n");
502 .fprintf(.stderr, "%d modules passed unittests\n",
503 cast(int)utResult.passed);
505 if (utResult.runMain)
506 tryExec({ result = mainFunc(args); });
508 result = EXIT_SUCCESS;
512 if (utResult.summarize)
513 .fprintf(.stderr, "%d/%d modules FAILED unittests\n",
514 cast(int)(utResult.executed - utResult.passed),
515 cast(int)utResult.executed);
516 result = EXIT_FAILURE;
520 result = EXIT_FAILURE;
523 result = (result == EXIT_SUCCESS) ? EXIT_FAILURE : result;
528 // Issue 10344: flush stdout and return nonzero on failure
529 if (.fflush(.stdout) != 0)
531 .fprintf(.stderr, "Failed to flush stdout: %s\n", .strerror(.errno));
534 result = EXIT_FAILURE;
541 private void formatThrowable(Throwable t, scope void delegate(const scope char[] s) nothrow sink)
545 u.toString(sink); sink("\n");
547 auto e = cast(Error)u;
548 if (e is null || e.bypassedException is null) continue;
550 sink("=== Bypassed ===\n");
551 foreach (t2; e.bypassedException)
553 t2.toString(sink); sink("\n");
555 sink("=== ~Bypassed ===\n");
559 private auto parseExceptionOptions()
561 import rt.config : rt_configOption;
562 import core.internal.parseoptions : rt_parseOption;
563 const optName = "trapExceptions";
564 auto option = rt_configOption(optName);
565 auto trap = rt_trapExceptions;
567 rt_parseOption(optName, option, trap, "");
571 extern (C) void _d_print_throwable(Throwable t)
573 // On Windows, a console may not be present to print the output to.
574 // Show a message box instead. If the console is present, convert to
575 // the correct encoding.
580 WCHAR* ptr; size_t len;
582 void sink(const scope char[] s) scope nothrow
584 if (!s.length) return;
585 int swlen = MultiByteToWideChar(
586 CP_UTF8, 0, s.ptr, cast(int)s.length, null, 0);
589 auto newPtr = cast(WCHAR*)realloc(ptr,
590 (this.len + swlen + 1) * WCHAR.sizeof);
593 auto written = MultiByteToWideChar(
594 CP_UTF8, 0, s.ptr, cast(int)s.length, ptr+len, swlen);
598 typeof(ptr) get() { if (ptr) ptr[len] = 0; return ptr; }
600 void free() { .free(ptr); }
603 HANDLE windowsHandle(int fd)
605 version (CRuntime_Microsoft)
606 return cast(HANDLE)_get_osfhandle(fd);
608 return _fdToHandle(fd);
611 auto hStdErr = windowsHandle(fileno(stderr));
612 CONSOLE_SCREEN_BUFFER_INFO sbi;
613 bool isConsole = GetConsoleScreenBufferInfo(hStdErr, &sbi) != 0;
615 // ensure the exception is shown at the beginning of the line, while also
616 // checking whether stderr is a valid file
617 int written = fprintf(stderr, "\n");
621 formatThrowable(t, &buf.sink);
627 caption.sink(t.classinfo.name);
629 // Avoid static user32.dll dependency for console applications
630 // by loading it dynamically as needed
631 auto user32 = LoadLibraryW("user32.dll");
634 alias typeof(&MessageBoxW) PMessageBoxW;
635 auto pMessageBoxW = cast(PMessageBoxW)
636 GetProcAddress(user32, "MessageBoxW");
638 pMessageBoxW(null, buf.get(), caption.get(), MB_ICONERROR);
649 formatThrowable(t, &buf.sink);
653 uint codepage = GetConsoleOutputCP();
654 int slen = WideCharToMultiByte(codepage, 0,
655 buf.ptr, cast(int)buf.len, null, 0, null, null);
656 auto sptr = cast(char*)malloc(slen * char.sizeof);
659 WideCharToMultiByte(codepage, 0,
660 buf.ptr, cast(int)buf.len, sptr, slen, null, null);
661 WriteFile(hStdErr, sptr, slen, null, null);
670 void sink(const scope char[] buf) scope nothrow
672 fprintf(stderr, "%.*s", cast(int)buf.length, buf.ptr);
674 formatThrowable(t, &sink);