Merge remote-tracking branch 'lucas/unwind_integration'
[nit.git] / src / abstract_compiler.nit
index 6467c57..0f71dff 100644 (file)
@@ -193,7 +193,7 @@ redef class ModelBuilder
                        p = orig_dir.join_path(p).simplify_path
                        cc_includes += " -I \"" + p + "\""
                end
-               makefile.write("CC = ccache cc\nCFLAGS = -g -O2\nCINCL = {cc_includes}\nLDFLAGS ?= \nLDLIBS  ?= -lm -lgc\n\n")
+               makefile.write("CC = ccache cc\nCFLAGS = -g -O2\nCINCL = {cc_includes}\nLDFLAGS ?= \nLDLIBS  ?= -lunwind -lm -lgc\n\n")
                makefile.write("all: {outpath}\n\n")
 
                var ofiles = new Array[String]
@@ -310,13 +310,19 @@ abstract class AbstractCompiler
        # This method call compile_header_strucs method that has to be refined
        fun compile_header do
                var v = self.header
+               self.header.add_decl("#define UNW_LOCAL_ONLY")
                self.header.add_decl("#include <stdlib.h>")
                self.header.add_decl("#include <stdio.h>")
                self.header.add_decl("#include <string.h>")
+               self.header.add_decl("#include <libunwind.h>")
+               self.header.add_decl("#include <signal.h>")
                self.header.add_decl("#include <gc_chooser.h>")
 
                compile_header_structs
 
+               # Signal handler function prototype
+               self.header.add_decl("void show_backtrace(int);")
+
                # Global variable used by the legacy native interface
                self.header.add_decl("extern int glob_argc;")
                self.header.add_decl("extern char **glob_argv;")
@@ -348,7 +354,38 @@ abstract class AbstractCompiler
                                v.compiler.header.add_decl("extern long count_type_test_skipped_{tag};")
                        end
                end
+
+               v.add_decl("void show_backtrace (int signo) \{")
+               v.add_decl("char* opt = getenv(\"NIT_NO_STACK\");")
+               v.add_decl("unw_cursor_t cursor;")
+               v.add_decl("if(opt==NULL)\{")
+               v.add_decl("unw_context_t uc;")
+               v.add_decl("unw_word_t ip;")
+               v.add_decl("char* procname = malloc(sizeof(char) * 100);")
+               v.add_decl("unw_getcontext(&uc);")
+               v.add_decl("unw_init_local(&cursor, &uc);")
+               v.add_decl("printf(\"-------------------------------------------------\\n\");")
+               v.add_decl("printf(\"-- C Stack Trace   ------------------------------\\n\");")
+               v.add_decl("printf(\"-------------------------------------------------\\n\");")
+               v.add_decl("while (unw_step(&cursor) > 0) \{")
+               v.add_decl("    unw_get_proc_name(&cursor, procname, 100, &ip);")
+               v.add_decl("    printf(\"` %s \\n\",procname);")
+               v.add_decl("\}")
+               v.add_decl("printf(\"-------------------------------------------------\\n\");")
+               v.add_decl("free(procname);")
+               v.add_decl("\}")
+               v.add_decl("exit(signo);")
+               v.add_decl("\}")
+
                v.add_decl("int main(int argc, char** argv) \{")
+
+               v.add("signal(SIGABRT, show_backtrace);")
+               v.add("signal(SIGFPE, show_backtrace);")
+               v.add("signal(SIGILL, show_backtrace);")
+               v.add("signal(SIGINT, show_backtrace);")
+               v.add("signal(SIGTERM, show_backtrace);")
+               v.add("signal(SIGSEGV, show_backtrace);")
+
                v.add("glob_argc = argc; glob_argv = argv;")
                v.add("initialize_gc_option();")
                var main_type = mainmodule.sys_type
@@ -391,6 +428,7 @@ abstract class AbstractCompiler
                                v.add("printf(\"\\t%ld (%.2f%%)\\n\", count_type_test_total_{tag}, 100.0*count_type_test_total_{tag}/count_type_test_total_total);")
                        end
                end
+
                v.add("return 0;")
                v.add("\}")
        end
@@ -677,6 +715,14 @@ abstract class AbstractCompilerVisitor
                return self.call(propdef, t, args)
        end
 
+       # Generate a monomorphic super send from the method `m`, the type `t` and the arguments `args`
+       fun monomorphic_super_send(m: MMethodDef, t: MType, args: Array[RuntimeVariable]): nullable RuntimeVariable
+       do
+               assert t isa MClassType
+               m = m.lookup_next_definition(self.compiler.mainmodule, t)
+               return self.call(m, t, args)
+       end
+
        # Attributes handling
 
        # Generate a polymorphic attribute is_set test
@@ -905,7 +951,7 @@ abstract class AbstractCompilerVisitor
                else
                        self.add("fprintf(stderr, \"\\n\");")
                end
-               self.add("exit(1);")
+               self.add("show_backtrace(1);")
        end
 
        # Add a dynamic cast
@@ -1553,7 +1599,7 @@ redef class AInternMethPropdef
                        return
                end
                if pname == "exit" then
-                       v.add("exit({arguments[1]});")
+                       v.add("show_backtrace({arguments[1]});")
                        return
                else if pname == "sys" then
                        v.ret(v.new_expr("glob_sys", ret.as(not null)))
@@ -1603,7 +1649,7 @@ redef class AExternMethPropdef
                var nextern = self.n_extern
                if nextern == null then
                        v.add("fprintf(stderr, \"NOT YET IMPLEMENTED nitni for {mpropdef} at {location.to_s}\\n\");")
-                       v.add("exit(1);")
+                       v.add("show_backtrace(1);")
                        return
                end
                externname = nextern.text.substring(1, nextern.text.length-2)
@@ -1635,7 +1681,7 @@ redef class AExternInitPropdef
                var nextern = self.n_extern
                if nextern == null then
                        v.add("printf(\"NOT YET IMPLEMENTED nitni for {mpropdef} at {location.to_s}\\n\");")
-                       v.add("exit(1);")
+                       v.add("show_backtrace(1);")
                        return
                end
                externname = nextern.text.substring(1, nextern.text.length-2)