nitg: Added C stack trace when encountering an error
authorLucas Bajolet <r4pass@hotmail.com>
Mon, 27 Jan 2014 17:18:48 +0000 (12:18 -0500)
committerLucas Bajolet <r4pass@hotmail.com>
Mon, 27 Jan 2014 17:18:48 +0000 (12:18 -0500)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

src/abstract_compiler.nit
src/global_compiler.nit
src/separate_compiler.nit

index 6467c57..697ecb7 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
@@ -905,7 +943,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 +1591,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 +1641,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 +1673,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)
index 7c00d09..8d0290d 100644 (file)
@@ -278,7 +278,7 @@ class GlobalCompilerVisitor
                        var res = self.new_var(mtype)
                        if not compiler.runtime_type_analysis.live_types.has(valtype) then
                                self.add("/*no autobox from {value.mtype} to {mtype}: {value.mtype} is not live! */")
-                               self.add("printf(\"Dead code executed!\\n\"); exit(1);")
+                               self.add("printf(\"Dead code executed!\\n\"); show_backtrace(1);")
                                return res
                        end
                        self.add("{res} = BOX_{valtype.c_name}({value}); /* autobox from {value.mtype} to {mtype} */")
@@ -287,7 +287,7 @@ class GlobalCompilerVisitor
                        # Bad things will appen!
                        var res = self.new_var(mtype)
                        self.add("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
-                       self.add("printf(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); exit(1);")
+                       self.add("printf(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); show_backtrace(1);")
                        return res
                end
        end
@@ -462,7 +462,7 @@ class GlobalCompilerVisitor
        private fun finalize_call(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable
        do
                if args.length != m.msignature.arity + 1 then # because of self
-                       add("printf(\"NOT YET IMPLEMENTED: Invalid arity for {m}. {args.length} arguments given.\\n\"); exit(1);")
+                       add("printf(\"NOT YET IMPLEMENTED: Invalid arity for {m}. {args.length} arguments given.\\n\"); show_backtrace(1);")
                        debug("NOT YET IMPLEMENTED: Invalid arity for {m}. {args.length} arguments given.")
                        return null
                end
@@ -571,7 +571,7 @@ class GlobalCompilerVisitor
        do
                if recv.mtype.ctype != "val*" then return
                self.add("fprintf(stderr, \"BTD BUG: Dynamic type is %s, static type is %s\\n\", class_names[{recv}->classid], \"{recv.mcasttype}\");")
-               self.add("exit(1);")
+               self.add("show_backtrace(1);")
        end
 
        redef fun isset_attribute(a, recv)
index 0606c4a..0d2e061 100644 (file)
@@ -945,7 +945,7 @@ class SeparateCompilerVisitor
                        var res = self.new_var(mtype)
                        if not compiler.runtime_type_analysis.live_types.has(valtype) then
                                self.add("/*no autobox from {value.mtype} to {mtype}: {value.mtype} is not live! */")
-                               self.add("printf(\"Dead code executed!\\n\"); exit(1);")
+                               self.add("printf(\"Dead code executed!\\n\"); show_backtrace(1);")
                                return res
                        end
                        self.add("{res} = BOX_{valtype.c_name}({value}); /* autobox from {value.mtype} to {mtype} */")
@@ -954,7 +954,7 @@ class SeparateCompilerVisitor
                        # Bad things will appen!
                        var res = self.new_var(mtype)
                        self.add("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
-                       self.add("printf(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); exit(1);")
+                       self.add("printf(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); show_backtrace(1);")
                        return res
                end
        end
@@ -1349,7 +1349,7 @@ class SeparateCompilerVisitor
                                self.add("count_type_test_resolved_{tag}++;")
                        end
                else
-                       self.add("printf(\"NOT YET IMPLEMENTED: type_test(%s, {mtype}).\\n\", \"{value.inspect}\"); exit(1);")
+                       self.add("printf(\"NOT YET IMPLEMENTED: type_test(%s, {mtype}).\\n\", \"{value.inspect}\"); show_backtrace(1);")
                end
 
                # check color is in table