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]
# 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;")
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
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
else
self.add("fprintf(stderr, \"\\n\");")
end
- self.add("exit(1);")
+ self.add("show_backtrace(1);")
end
# Add a dynamic cast
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)))
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)
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)
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} */")
# 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
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
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)
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} */")
# 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
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