]> git.ipfire.org Git - thirdparty/gcc.git/blob - libphobos/libdruntime/core/runtime.d
19bb61b0c220c6b70a4c1a7a0c5c21fab082218c
[thirdparty/gcc.git] / libphobos / libdruntime / core / runtime.d
1 /**
2 * The runtime module exposes information specific to the D runtime code.
3 *
4 * Copyright: Copyright Sean Kelly 2005 - 2009.
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
6 * Authors: Sean Kelly
7 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/druntime/src/core/runtime.d, _runtime.d)
8 * Documentation: https://dlang.org/phobos/core_runtime.html
9 */
10
11 /* NOTE: This file has been patched from the original DMD distribution to
12 * work with the GDC compiler.
13 */
14 module core.runtime;
15
16 version (OSX)
17 version = Darwin;
18 else version (iOS)
19 version = Darwin;
20 else version (TVOS)
21 version = Darwin;
22 else version (WatchOS)
23 version = Darwin;
24
25 version (GNU)
26 {
27 import gcc.backtrace;
28 // This shouldn't be necessary but ensure that code doesn't get mixed
29 // It does however prevent the unittest SEGV handler to be installed,
30 // which is desireable as it uses backtrace directly.
31 private enum hasExecinfo = false;
32 }
33 else version (DRuntime_Use_Libunwind)
34 {
35 import core.internal.backtrace.libunwind;
36 // This shouldn't be necessary but ensure that code doesn't get mixed
37 // It does however prevent the unittest SEGV handler to be installed,
38 // which is desireable as it uses backtrace directly.
39 private enum hasExecinfo = false;
40 }
41 else
42 import core.internal.execinfo;
43
44 /// C interface for Runtime.loadLibrary
45 extern (C) void* rt_loadLibrary(const char* name);
46 /// ditto
47 version (Windows) extern (C) void* rt_loadLibraryW(const wchar* name);
48
49 /// C interface for Runtime.unloadLibrary, returns 1/0 instead of bool
50 extern (C) int rt_unloadLibrary(void* ptr);
51
52 /// C interface for Runtime.initialize, returns 1/0 instead of bool
53 extern(C) int rt_init();
54 /// C interface for Runtime.terminate, returns 1/0 instead of bool
55 extern(C) int rt_term();
56
57 /**
58 * This type is returned by the module unit test handler to indicate testing
59 * results.
60 */
61 struct UnitTestResult
62 {
63 /**
64 * Number of modules which were tested
65 */
66 size_t executed;
67
68 /**
69 * Number of modules passed the unittests
70 */
71 size_t passed;
72
73 /**
74 * Should the main function be run or not? This is ignored if any tests
75 * failed.
76 */
77 bool runMain;
78
79 /**
80 * Should we print a summary of the results?
81 */
82 bool summarize;
83
84 /**
85 * Simple check for whether execution should continue after unit tests
86 * have been run. Works with legacy code that expected a bool return.
87 *
88 * Returns:
89 * true if execution should continue after testing is complete, false if
90 * not.
91 */
92 bool opCast(T : bool)() const
93 {
94 return runMain && (executed == passed);
95 }
96
97 /// Simple return code that says unit tests pass, and main should be run
98 enum UnitTestResult pass = UnitTestResult(0, 0, true, false);
99 /// Simple return code that says unit tests failed.
100 enum UnitTestResult fail = UnitTestResult(1, 0, false, false);
101 }
102
103 /// Legacy module unit test handler
104 alias bool function() ModuleUnitTester;
105 /// Module unit test handler
106 alias UnitTestResult function() ExtendedModuleUnitTester;
107 private
108 {
109 alias bool function(Object) CollectHandler;
110 alias Throwable.TraceInfo function( void* ptr ) TraceHandler;
111
112 alias void delegate( Throwable ) ExceptionHandler;
113 extern (C) void _d_print_throwable(Throwable t);
114
115 extern (C) void* thread_stackBottom() nothrow @nogc;
116 }
117
118
119 shared static this()
120 {
121 // NOTE: Some module ctors will run before this handler is set, so it's
122 // still possible the app could exit without a stack trace. If
123 // this becomes an issue, the handler could be set in C main
124 // before the module ctors are run.
125 Runtime.traceHandler(&defaultTraceHandler, &defaultTraceDeallocator);
126 }
127
128
129 ///////////////////////////////////////////////////////////////////////////////
130 // Runtime
131 ///////////////////////////////////////////////////////////////////////////////
132
133 /**
134 * Stores the unprocessed arguments supplied when the
135 * process was started.
136 */
137 struct CArgs
138 {
139 int argc; /// The argument count.
140 char** argv; /// The arguments as a C array of strings.
141 }
142
143 /**
144 * This struct encapsulates all functionality related to the underlying runtime
145 * module for the calling context.
146 */
147 struct Runtime
148 {
149 /**
150 * Initializes the runtime. This call is to be used in instances where the
151 * standard program initialization process is not executed. This is most
152 * often in shared libraries or in libraries linked to a C program.
153 * If the runtime was already successfully initialized this returns true.
154 * Each call to initialize must be paired by a call to $(LREF terminate).
155 *
156 * Returns:
157 * true if initialization succeeded or false if initialization failed.
158 */
159 static bool initialize()
160 {
161 return !!rt_init();
162 }
163
164 /**
165 * Terminates the runtime. This call is to be used in instances where the
166 * standard program termination process will not be not executed. This is
167 * most often in shared libraries or in libraries linked to a C program.
168 * If the runtime was not successfully initialized the function returns false.
169 *
170 * Returns:
171 * true if termination succeeded or false if termination failed.
172 */
173 static bool terminate()
174 {
175 return !!rt_term();
176 }
177
178 /**
179 * Returns the arguments supplied when the process was started.
180 *
181 * Returns:
182 * The arguments supplied when this process was started.
183 */
184 extern(C) pragma(mangle, "rt_args") static @property string[] args();
185
186 /**
187 * Returns the unprocessed C arguments supplied when the process was started.
188 * Use this when you need to supply argc and argv to C libraries.
189 *
190 * Returns:
191 * A $(LREF CArgs) struct with the arguments supplied when this process was started.
192 *
193 * Example:
194 * ---
195 * import core.runtime;
196 *
197 * // A C library function requiring char** arguments
198 * extern(C) void initLibFoo(int argc, char** argv);
199 *
200 * void main()
201 * {
202 * auto args = Runtime.cArgs;
203 * initLibFoo(args.argc, args.argv);
204 * }
205 * ---
206 */
207 extern(C) pragma(mangle, "rt_cArgs") static @property CArgs cArgs() @nogc;
208
209 /**
210 * Locates a dynamic library with the supplied library name and dynamically
211 * loads it into the caller's address space. If the library contains a D
212 * runtime it will be integrated with the current runtime.
213 *
214 * Params:
215 * name = The name of the dynamic library to load.
216 *
217 * Returns:
218 * A reference to the library or null on error.
219 */
220 static void* loadLibrary()(const scope char[] name)
221 {
222 import core.stdc.stdlib : free, malloc;
223 version (Windows)
224 {
225 import core.sys.windows.winnls : CP_UTF8, MultiByteToWideChar;
226 import core.sys.windows.winnt : WCHAR;
227
228 if (name.length == 0) return null;
229 // Load a DLL at runtime
230 auto len = MultiByteToWideChar(
231 CP_UTF8, 0, name.ptr, cast(int)name.length, null, 0);
232 if (len == 0)
233 return null;
234
235 auto buf = cast(WCHAR*)malloc((len+1) * WCHAR.sizeof);
236 if (buf is null) return null;
237 scope (exit) free(buf);
238
239 len = MultiByteToWideChar(
240 CP_UTF8, 0, name.ptr, cast(int)name.length, buf, len);
241 if (len == 0)
242 return null;
243
244 buf[len] = '\0';
245
246 return rt_loadLibraryW(buf);
247 }
248 else version (Posix)
249 {
250 /* Need a 0-terminated C string for the dll name
251 */
252 immutable len = name.length;
253 auto buf = cast(char*)malloc(len + 1);
254 if (!buf) return null;
255 scope (exit) free(buf);
256
257 buf[0 .. len] = name[];
258 buf[len] = 0;
259
260 return rt_loadLibrary(buf);
261 }
262 }
263
264
265 /**
266 * Unloads the dynamic library referenced by p. If this library contains a
267 * D runtime then any necessary finalization or cleanup of that runtime
268 * will be performed.
269 *
270 * Params:
271 * p = A reference to the library to unload.
272 */
273 static bool unloadLibrary()(void* p)
274 {
275 return !!rt_unloadLibrary(p);
276 }
277
278
279 /**
280 * Overrides the default trace mechanism with a user-supplied version. A
281 * trace represents the context from which an exception was thrown, and the
282 * trace handler will be called when this occurs. The pointer supplied to
283 * this routine indicates the base address from which tracing should occur.
284 * If the supplied pointer is null then the trace routine should determine
285 * an appropriate calling context from which to begin the trace.
286 *
287 * If the deallocator is set, then it is called with the traceinfo when the
288 * exception is finalized. The deallocator is only set in the exception if
289 * the default handler is used to generate the trace info.
290 *
291 * Params:
292 * h = The new trace handler. Set to null to disable exception backtracing.
293 * d = The new trace deallocator. If non-null, this will be called on
294 * exception destruction with the trace info, only when the trace
295 * handler is used to generate TraceInfo.
296 */
297 extern(C) pragma(mangle, "rt_setTraceHandler") static @property void traceHandler(TraceHandler h,
298 Throwable.TraceDeallocator d = null);
299
300 /**
301 * Gets the current trace handler.
302 *
303 * Returns:
304 * The current trace handler or null if none has been set.
305 */
306 extern(C) pragma(mangle, "rt_getTraceHandler") static @property TraceHandler traceHandler();
307
308 /**
309 * Gets the current trace deallocator.
310 *
311 * Returns:
312 * The current trace deallocator or null if none has been set.
313 */
314 extern(C) pragma(mangle, "rt_getTraceDeallocator") static @property Throwable.TraceDeallocator traceDeallocator();
315
316 /**
317 * Overrides the default collect hander with a user-supplied version. This
318 * routine will be called for each resource object that is finalized in a
319 * non-deterministic manner--typically during a garbage collection cycle.
320 * If the supplied routine returns true then the object's dtor will called
321 * as normal, but if the routine returns false than the dtor will not be
322 * called. The default behavior is for all object dtors to be called.
323 *
324 * Params:
325 * h = The new collect handler. Set to null to use the default handler.
326 */
327 extern(C) pragma(mangle, "rt_setCollectHandler") static @property void collectHandler( CollectHandler h );
328
329
330 /**
331 * Gets the current collect handler.
332 *
333 * Returns:
334 * The current collect handler or null if none has been set.
335 */
336 extern(C) pragma(mangle, "rt_getCollectHandler") static @property CollectHandler collectHandler();
337
338
339 /**
340 * Overrides the default module unit tester with a user-supplied version.
341 * This routine will be called once on program initialization. The return
342 * value of this routine indicates to the runtime whether the tests ran
343 * without error.
344 *
345 * There are two options for handlers. The `bool` version is deprecated but
346 * will be kept for legacy support. Returning `true` from the handler is
347 * equivalent to returning `UnitTestResult.pass` from the extended version.
348 * Returning `false` from the handler is equivalent to returning
349 * `UnitTestResult.fail` from the extended version.
350 *
351 * See the documentation for `UnitTestResult` to see how you should set up
352 * the return structure.
353 *
354 * See the documentation for `runModuleUnitTests` for how the default
355 * algorithm works, or read the example below.
356 *
357 * Params:
358 * h = The new unit tester. Set both to null to use the default unit
359 * tester.
360 *
361 * Example:
362 * ---------
363 * shared static this()
364 * {
365 * import core.runtime;
366 *
367 * Runtime.extendedModuleUnitTester = &customModuleUnitTester;
368 * }
369 *
370 * UnitTestResult customModuleUnitTester()
371 * {
372 * import std.stdio;
373 *
374 * writeln("Using customModuleUnitTester");
375 *
376 * // Do the same thing as the default moduleUnitTester:
377 * UnitTestResult result;
378 * foreach (m; ModuleInfo)
379 * {
380 * if (m)
381 * {
382 * auto fp = m.unitTest;
383 *
384 * if (fp)
385 * {
386 * ++result.executed;
387 * try
388 * {
389 * fp();
390 * ++result.passed;
391 * }
392 * catch (Throwable e)
393 * {
394 * writeln(e);
395 * }
396 * }
397 * }
398 * }
399 * if (result.executed != result.passed)
400 * {
401 * result.runMain = false; // don't run main
402 * result.summarize = true; // print failure
403 * }
404 * else
405 * {
406 * result.runMain = true; // all UT passed
407 * result.summarize = false; // be quiet about it.
408 * }
409 * return result;
410 * }
411 * ---------
412 */
413 static @property void extendedModuleUnitTester( ExtendedModuleUnitTester h )
414 {
415 sm_extModuleUnitTester = h;
416 }
417
418 /// Ditto
419 static @property void moduleUnitTester( ModuleUnitTester h )
420 {
421 sm_moduleUnitTester = h;
422 }
423
424 /**
425 * Gets the current legacy module unit tester.
426 *
427 * This property should not be used, but is supported for legacy purposes.
428 *
429 * Note that if the extended unit test handler is set, this handler will
430 * be ignored.
431 *
432 * Returns:
433 * The current legacy module unit tester handler or null if none has been
434 * set.
435 */
436 static @property ModuleUnitTester moduleUnitTester()
437 {
438 return sm_moduleUnitTester;
439 }
440
441 /**
442 * Gets the current module unit tester.
443 *
444 * This handler overrides any legacy module unit tester set by the
445 * moduleUnitTester property.
446 *
447 * Returns:
448 * The current module unit tester handler or null if none has been
449 * set.
450 */
451 static @property ExtendedModuleUnitTester extendedModuleUnitTester()
452 {
453 return sm_extModuleUnitTester;
454 }
455
456 private:
457
458 // NOTE: This field will only ever be set in a static ctor and should
459 // never occur within any but the main thread, so it is safe to
460 // make it __gshared.
461 __gshared ExtendedModuleUnitTester sm_extModuleUnitTester = null;
462 __gshared ModuleUnitTester sm_moduleUnitTester = null;
463 }
464
465 /**
466 * Set source file path for coverage reports.
467 *
468 * Params:
469 * path = The new path name.
470 * Note:
471 * This is a dmd specific setting.
472 */
473 extern (C) void dmd_coverSourcePath(string path);
474
475 /**
476 * Set output path for coverage reports.
477 *
478 * Params:
479 * path = The new path name.
480 * Note:
481 * This is a dmd specific setting.
482 */
483 extern (C) void dmd_coverDestPath(string path);
484
485 /**
486 * Enable merging of coverage reports with existing data.
487 *
488 * Params:
489 * flag = enable/disable coverage merge mode
490 * Note:
491 * This is a dmd specific setting.
492 */
493 extern (C) void dmd_coverSetMerge(bool flag);
494
495 /**
496 * Set the output file name for profile reports (-profile switch).
497 * An empty name will set the output to stdout.
498 *
499 * Params:
500 * name = file name
501 * Note:
502 * This is a dmd specific setting.
503 */
504 extern (C) void trace_setlogfilename(string name);
505
506 /**
507 * Set the output file name for the optimized profile linker DEF file (-profile switch).
508 * An empty name will set the output to stdout.
509 *
510 * Params:
511 * name = file name
512 * Note:
513 * This is a dmd specific setting.
514 */
515 extern (C) void trace_setdeffilename(string name);
516
517 /**
518 * Set the output file name for memory profile reports (-profile=gc switch).
519 * An empty name will set the output to stdout.
520 *
521 * Params:
522 * name = file name
523 * Note:
524 * This is a dmd specific setting.
525 */
526 extern (C) void profilegc_setlogfilename(string name);
527
528 ///////////////////////////////////////////////////////////////////////////////
529 // Overridable Callbacks
530 ///////////////////////////////////////////////////////////////////////////////
531
532
533 /**
534 * This routine is called by the runtime to run module unit tests on startup.
535 * The user-supplied unit tester will be called if one has been set,
536 * otherwise all unit tests will be run in sequence.
537 *
538 * If the extended unittest handler is registered, this function returns the
539 * result from that handler directly.
540 *
541 * If a legacy boolean returning custom handler is used, `false` maps to
542 * `UnitTestResult.fail`, and `true` maps to `UnitTestResult.pass`. This was
543 * the original behavior of the unit testing system.
544 *
545 * If no unittest custom handlers are registered, the following algorithm is
546 * executed (the behavior can be affected by the `--DRT-testmode` switch
547 * below):
548 * 1. Execute any unittests present. For each that fails, print the stack
549 * trace and continue.
550 * 2. If no unittests were present, set summarize to false, and runMain to
551 * true.
552 * 3. Otherwise, set summarize to true, and runMain to false.
553 *
554 * See the documentation for `UnitTestResult` for details on how the runtime
555 * treats the return value from this function.
556 *
557 * If the switch `--DRT-testmode` is passed to the executable, it can have
558 * one of 3 values:
559 * 1. "run-main": even if unit tests are run (and all pass), runMain is set
560 to true.
561 * 2. "test-or-main": any unit tests present will cause the program to
562 * summarize the results and exit regardless of the result. This is the
563 * default.
564 * 3. "test-only", runMain is set to false, even with no tests present.
565 *
566 * This command-line parameter does not affect custom unit test handlers.
567 *
568 * Returns:
569 * A `UnitTestResult` struct indicating the result of running unit tests.
570 */
571 extern (C) UnitTestResult runModuleUnitTests()
572 {
573 version (Windows)
574 import core.sys.windows.stacktrace;
575
576 static if (__traits(compiles, new LibBacktrace(0)))
577 {
578 import core.sys.posix.signal; // segv handler
579
580 static extern (C) void unittestSegvHandler(int signum, siginfo_t* info, void* ptr)
581 {
582 import core.stdc.stdio;
583 fprintf(stderr, "Segmentation fault while running unittests:\n");
584 fprintf(stderr, "----------------\n");
585
586 // First frame is LibBacktrace ctor. Second is signal handler,
587 // but include that for now
588 scope bt = new LibBacktrace(1);
589
590 foreach (size_t i, const(char[]) msg; bt)
591 fprintf(stderr, "%s\n", msg.ptr ? msg.ptr : "???");
592 }
593
594 sigaction_t action = void;
595 sigaction_t oldseg = void;
596 sigaction_t oldbus = void;
597
598 (cast(byte*) &action)[0 .. action.sizeof] = 0;
599 sigfillset(&action.sa_mask); // block other signals
600 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
601 action.sa_sigaction = &unittestSegvHandler;
602 sigaction(SIGSEGV, &action, &oldseg);
603 sigaction(SIGBUS, &action, &oldbus);
604 scope (exit)
605 {
606 sigaction(SIGSEGV, &oldseg, null);
607 sigaction(SIGBUS, &oldbus, null);
608 }
609 }
610 else static if (hasExecinfo)
611 {
612 import core.sys.posix.signal; // segv handler
613
614 static extern (C) void unittestSegvHandler( int signum, siginfo_t* info, void* ptr ) nothrow
615 {
616 static enum MAXFRAMES = 128;
617 void*[MAXFRAMES] callstack;
618
619 auto numframes = backtrace( callstack.ptr, MAXFRAMES );
620 backtrace_symbols_fd( callstack.ptr, numframes, 2 );
621 }
622
623 sigaction_t action = void;
624 sigaction_t oldseg = void;
625 sigaction_t oldbus = void;
626
627 (cast(byte*) &action)[0 .. action.sizeof] = 0;
628 sigfillset( &action.sa_mask ); // block other signals
629 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
630 action.sa_sigaction = &unittestSegvHandler;
631 sigaction( SIGSEGV, &action, &oldseg );
632 sigaction( SIGBUS, &action, &oldbus );
633 scope( exit )
634 {
635 sigaction( SIGSEGV, &oldseg, null );
636 sigaction( SIGBUS, &oldbus, null );
637 }
638 }
639
640 if (Runtime.sm_extModuleUnitTester !is null)
641 return Runtime.sm_extModuleUnitTester();
642 else if (Runtime.sm_moduleUnitTester !is null)
643 return Runtime.sm_moduleUnitTester() ? UnitTestResult.pass : UnitTestResult.fail;
644 UnitTestResult results;
645 foreach ( m; ModuleInfo )
646 {
647 if ( !m )
648 continue;
649 auto fp = m.unitTest;
650 if ( !fp )
651 continue;
652
653 import core.exception;
654 ++results.executed;
655 try
656 {
657 fp();
658 ++results.passed;
659 }
660 catch ( Throwable e )
661 {
662 if ( typeid(e) == typeid(AssertError) )
663 {
664 // Crude heuristic to figure whether the assertion originates in
665 // the unittested module. TODO: improve.
666 auto moduleName = m.name;
667 if (moduleName.length && e.file.length > moduleName.length
668 && e.file[0 .. moduleName.length] == moduleName)
669 {
670 import core.stdc.stdio;
671 printf("%.*s(%llu): [unittest] %.*s\n",
672 cast(int) e.file.length, e.file.ptr, cast(ulong) e.line,
673 cast(int) e.message.length, e.message.ptr);
674
675 // Exception originates in the same module, don't print
676 // the stack trace.
677 // TODO: omit stack trace only if assert was thrown
678 // directly by the unittest.
679 continue;
680 }
681 }
682 // TODO: perhaps indent all of this stuff.
683 _d_print_throwable(e);
684 }
685 }
686
687 import core.internal.parseoptions : rt_configOption;
688
689 if (results.passed != results.executed)
690 {
691 // by default, we always print a summary if there are failures.
692 results.summarize = true;
693 }
694 else switch (rt_configOption("testmode", null, false))
695 {
696 case "run-main":
697 results.runMain = true;
698 break;
699 case "test-only":
700 // Never run main, always summarize
701 results.summarize = true;
702 break;
703 case "":
704 // By default, do not run main if tests are present.
705 case "test-or-main":
706 // only run main if there were no tests. Only summarize if we are not
707 // running main.
708 results.runMain = (results.executed == 0);
709 results.summarize = !results.runMain;
710 break;
711 default:
712 assert(0, "Unknown --DRT-testmode option: " ~ rt_configOption("testmode", null, false));
713 }
714
715 return results;
716 }
717
718 /**
719 * Get the default `Throwable.TraceInfo` implementation for the platform
720 *
721 * This functions returns a trace handler, allowing to inspect the
722 * current stack trace.
723 *
724 * IMPORTANT NOTE! the returned trace is potentially not GC allocated, and so
725 * you must call `defaultTraceDeallocator` when you are finished with the
726 * `TraceInfo`
727 *
728 * Params:
729 * ptr = (Windows only) The context to get the stack trace from.
730 * When `null` (the default), start from the current frame.
731 *
732 * Returns:
733 * A `Throwable.TraceInfo` implementation suitable to iterate over the stack,
734 * or `null`. If called from a finalizer (destructor), always returns `null`
735 * as trace handlers allocate.
736 */
737 Throwable.TraceInfo defaultTraceHandler( void* ptr = null ) // @nogc
738 {
739 // NOTE: with traces now being allocated using C malloc, no need to worry
740 // about GC reentrancy. This code left commented out for reference.
741 //
742 // avoid recursive GC calls in finalizer, trace handlers should be made @nogc instead
743 /*import core.memory : GC;
744 if (GC.inFinalizer)
745 return null;*/
746
747 static T allocate(T, Args...)(auto ref Args args) @nogc
748 {
749 import core.lifetime : emplace;
750 import core.stdc.stdlib : malloc;
751 auto result = cast(T)malloc(__traits(classInstanceSize, T));
752 return emplace(result, args);
753 }
754 static if (__traits(compiles, allocate!LibBacktrace(0)))
755 {
756 version (Posix)
757 static enum FIRSTFRAME = 4;
758 else version (Win64)
759 static enum FIRSTFRAME = 4;
760 else
761 static enum FIRSTFRAME = 0;
762 return allocate!LibBacktrace(FIRSTFRAME);
763 }
764 else static if (__traits(compiles, allocate!UnwindBacktrace(0)))
765 {
766 version (Posix)
767 static enum FIRSTFRAME = 5;
768 else version (Win64)
769 static enum FIRSTFRAME = 4;
770 else
771 static enum FIRSTFRAME = 0;
772 return allocate!UnwindBacktrace(FIRSTFRAME);
773 }
774 else version (Windows)
775 {
776 import core.sys.windows.stacktrace;
777 static if (__traits(compiles, allocate!StackTrace(0, null)))
778 {
779 import core.sys.windows.winnt : CONTEXT;
780 version (Win64)
781 enum FIRSTFRAME = 4;
782 else version (Win32)
783 enum FIRSTFRAME = 0;
784 return allocate!StackTrace(FIRSTFRAME, cast(CONTEXT*)ptr);
785 }
786 else
787 return null;
788 }
789 else static if (__traits(compiles, allocate!DefaultTraceInfo()))
790 return allocate!DefaultTraceInfo();
791 else
792 return null;
793 }
794
795 /// Example of a simple program printing its stack trace
796 unittest
797 {
798 import core.runtime;
799 import core.stdc.stdio;
800
801 void main()
802 {
803 auto trace = defaultTraceHandler(null);
804 foreach (line; trace)
805 {
806 printf("%.*s\n", cast(int)line.length, line.ptr);
807 }
808 defaultTraceDeallocator(trace);
809 }
810 }
811
812 /***
813 * Deallocate a traceinfo generated by deaultTraceHander.
814 *
815 * Call this function on a TraceInfo generated via `defaultTraceHandler` when
816 * you are done with it. If necessary, this cleans up any manually managed
817 * resources from the `TraceInfo`, and invalidates it. After this, the object
818 * is no longer valid.
819 *
820 * Params:
821 * info = The `TraceInfo` to deallocate. This should only be a value that
822 * was returned by `defaultTraceHandler`.
823 */
824 void defaultTraceDeallocator(Throwable.TraceInfo info) nothrow
825 {
826 if (info is null)
827 return;
828 auto obj = cast(Object)info;
829 destroy(obj);
830 import core.stdc.stdlib : free;
831 free(cast(void *)obj);
832 }
833
834 version (DRuntime_Use_Libunwind)
835 {
836 import core.internal.backtrace.handler;
837
838 alias DefaultTraceInfo = LibunwindHandler;
839 }
840 /// Default implementation for most POSIX systems
841 else static if (hasExecinfo) private class DefaultTraceInfo : Throwable.TraceInfo
842 {
843 import core.demangle;
844 import core.stdc.stdlib : free;
845 import core.stdc.string : strlen, memchr, memmove;
846
847 this() @nogc
848 {
849 // it may not be 1 but it is good enough to get
850 // in CALL instruction address range for backtrace
851 enum CALL_INSTRUCTION_SIZE = 1;
852
853 static if (__traits(compiles, backtrace((void**).init, int.init)))
854 numframes = cast(int) backtrace(this.callstack.ptr, MAXFRAMES);
855 // Backtrace succeeded, adjust the frame to point to the caller
856 if (numframes >= 2)
857 foreach (ref elem; this.callstack)
858 elem -= CALL_INSTRUCTION_SIZE;
859 else // backtrace() failed, do it ourselves
860 {
861 static void** getBasePtr() @nogc
862 {
863 version (D_InlineAsm_X86)
864 asm @nogc { naked; mov EAX, EBP; ret; }
865 else
866 version (D_InlineAsm_X86_64)
867 asm @nogc { naked; mov RAX, RBP; ret; }
868 else
869 return null;
870 }
871
872 auto stackTop = getBasePtr();
873 auto stackBottom = cast(void**) thread_stackBottom();
874 void* dummy;
875
876 if ( stackTop && &dummy < stackTop && stackTop < stackBottom )
877 {
878 auto stackPtr = stackTop;
879
880 for ( numframes = 0; stackTop <= stackPtr &&
881 stackPtr < stackBottom &&
882 numframes < MAXFRAMES; )
883 {
884 callstack[numframes++] = *(stackPtr + 1) - CALL_INSTRUCTION_SIZE;
885 stackPtr = cast(void**) *stackPtr;
886 }
887 }
888 }
889 }
890
891 override int opApply( scope int delegate(ref const(char[])) dg ) const
892 {
893 return opApply( (ref size_t, ref const(char[]) buf)
894 {
895 return dg( buf );
896 } );
897 }
898
899 override int opApply( scope int delegate(ref size_t, ref const(char[])) dg ) const
900 {
901 version (linux) enum enableDwarf = true;
902 else version (FreeBSD) enum enableDwarf = true;
903 else version (DragonFlyBSD) enum enableDwarf = true;
904 else version (OpenBSD) enum enableDwarf = true;
905 else version (Darwin) enum enableDwarf = true;
906 else enum enableDwarf = false;
907
908 const framelist = backtrace_symbols( callstack.ptr, numframes );
909 scope(exit) free(cast(void*) framelist);
910
911 static if (enableDwarf)
912 {
913 import core.internal.backtrace.dwarf;
914 return traceHandlerOpApplyImpl(numframes,
915 i => callstack[i],
916 (i) { auto str = framelist[i][0 .. strlen(framelist[i])]; return getMangledSymbolName(str); },
917 dg);
918 }
919 else
920 {
921 int ret = 0;
922 for (size_t pos = 0; pos < numframes; ++pos)
923 {
924 char[4096] fixbuf = void;
925 auto buf = framelist[pos][0 .. strlen(framelist[pos])];
926 buf = fixline( buf, fixbuf );
927 ret = dg( pos, buf );
928 if ( ret )
929 break;
930 }
931 return ret;
932 }
933 }
934
935 override string toString() const
936 {
937 string buf;
938 foreach ( i, line; this )
939 buf ~= i ? "\n" ~ line : line;
940 return buf;
941 }
942
943 private:
944 int numframes;
945 static enum MAXFRAMES = 128;
946 void*[MAXFRAMES] callstack = void;
947
948 private:
949 const(char)[] fixline( const(char)[] buf, return ref char[4096] fixbuf ) const
950 {
951 size_t symBeg, symEnd;
952
953 getMangledSymbolName(buf, symBeg, symEnd);
954
955 enum min = (size_t a, size_t b) => a <= b ? a : b;
956 if (symBeg == symEnd || symBeg >= fixbuf.length)
957 {
958 immutable len = min(buf.length, fixbuf.length);
959 fixbuf[0 .. len] = buf[0 .. len];
960 return fixbuf[0 .. len];
961 }
962 else
963 {
964 fixbuf[0 .. symBeg] = buf[0 .. symBeg];
965
966 auto sym = demangle(buf[symBeg .. symEnd], fixbuf[symBeg .. $], getCXXDemangler());
967
968 if (sym.ptr !is fixbuf.ptr + symBeg)
969 {
970 // demangle reallocated the buffer, copy the symbol to fixbuf
971 immutable len = min(fixbuf.length - symBeg, sym.length);
972 memmove(fixbuf.ptr + symBeg, sym.ptr, len);
973 if (symBeg + len == fixbuf.length)
974 return fixbuf[];
975 }
976
977 immutable pos = symBeg + sym.length;
978 assert(pos < fixbuf.length);
979 immutable tail = buf.length - symEnd;
980 immutable len = min(fixbuf.length - pos, tail);
981 fixbuf[pos .. pos + len] = buf[symEnd .. symEnd + len];
982 return fixbuf[0 .. pos + len];
983 }
984 }
985 }