import breakpoint
intrude import naive_interpreter
import nitx
+intrude import local_var_init
+intrude import scope
+intrude import toolcontext
+
+redef class Model
+ # Cleans the model to remove a module and what it defines when semantic analysis fails on injected code
+ private fun try_remove_module(m: MModule): Bool
+ do
+ var index = -1
+ for i in [0 .. mmodules.length[ do
+ if mmodules[i] == m then
+ index = i
+ break
+ end
+ end
+ if index == -1 then return false
+ var mmodule = mmodules[index]
+ mmodules.remove_at(index)
+ for classdef in mmodule.mclassdefs do
+ var mclass = classdef.mclass
+ for i in [0 .. mclass.mclassdefs.length[ do
+ if mclass.mclassdefs[i] == classdef then
+ index = i
+ break
+ end
+ end
+ mclass.mclassdefs.remove_at(index)
+ var propdefs = classdef.mpropdefs
+ for propdef in propdefs do
+ var prop = propdef.mproperty
+ for i in [0..prop.mpropdefs.length[ do
+ if prop.mpropdefs[i] == propdef then
+ index = i
+ break
+ end
+ end
+ prop.mpropdefs.remove_at(index)
+ end
+ end
+ return true
+ end
+end
+
+redef class ScopeVisitor
+
+ redef init(toolcontext)
+ do
+ super
+ if toolcontext.dbg != null then
+ var localvars = toolcontext.dbg.frame.map
+ for i in localvars.keys do
+ scopes.first.variables[i.to_s] = i
+ end
+ end
+ end
+
+end
+
+redef class LocalVarInitVisitor
+ redef fun mark_is_unset(node: AExpr, variable: nullable Variable)
+ do
+ super
+ if toolcontext.dbg != null then
+ var varname = variable.to_s
+ var instmap = toolcontext.dbg.frame.map
+ for i in instmap.keys do
+ if i.to_s == varname then
+ mark_is_set(node, variable)
+ end
+ end
+ end
+ end
+
+end
redef class ToolContext
+ private var dbg: nullable Debugger = null
+
+ private var had_error: Bool = false
+
+ redef fun check_errors
+ do
+ if dbg == null then
+ super
+ else
+ if messages.length > 0 then
+ message_sorter.sort(messages)
+
+ for m in messages do
+ if "Warning".search_in(m.text, 0) == null then had_error = true
+ stderr.write("{m.to_color_string}\n")
+ end
+ end
+
+ messages.clear
+ end
+ end
+
# -d
var opt_debugger_mode: OptionBool = new OptionBool("Launches the target program with the debugger attached to it", "-d")
# -c
frame.current_node = old
end
+ # Does the same as an usual send, except it will modify the call chain on the first call when injecting code at Runtime using the debugger.
+ # Instead of creating a pristine Frame, it will copy the actual values of the frame, and re-inject them after execution in the current context.
+ fun rt_send(mproperty: MMethod, args: Array[Instance]): nullable Instance
+ do
+ var recv = args.first
+ var mtype = recv.mtype
+ var ret = send_commons(mproperty, args, mtype)
+ if ret != null then return ret
+ var propdef = mproperty.lookup_first_definition(self.mainmodule, mtype)
+ return self.rt_call(propdef, args)
+ end
+
+ # Same as a regular call but for a runtime injected module
+ #
+ fun rt_call(mpropdef: MMethodDef, args: Array[Instance]): nullable Instance
+ do
+ args = call_commons(mpropdef, args)
+ return rt_call_without_varargs(mpropdef, args)
+ end
+
+ # Common code to call and this function
+ #
+ # Call only executes the variadic part, this avoids
+ # double encapsulation of variadic parameters into an Array
+ fun rt_call_without_varargs(mpropdef: MMethodDef, args: Array[Instance]): nullable Instance
+ do
+ if self.modelbuilder.toolcontext.opt_discover_call_trace.value and not self.discover_call_trace.has(mpropdef) then
+ self.discover_call_trace.add mpropdef
+ self.debug("Discovered {mpropdef}")
+ end
+ if args.length < mpropdef.msignature.arity + 1 or args.length > mpropdef.msignature.arity + 1 then
+ fatal("NOT YET IMPLEMENTED: Invalid arity for {mpropdef}. {args.length} arguments given.")
+ end
+ if args.length < mpropdef.msignature.arity + 1 then
+ fatal("NOT YET IMPLEMENTED: default closures")
+ end
+
+ # Look for the AST node that implements the property
+ var mproperty = mpropdef.mproperty
+ if self.modelbuilder.mpropdef2npropdef.has_key(mpropdef) then
+ var npropdef = self.modelbuilder.mpropdef2npropdef[mpropdef]
+ self.parameter_check(npropdef, mpropdef, args)
+ if npropdef isa AConcreteMethPropdef then
+ return npropdef.rt_call(self, mpropdef, args)
+ else
+ print "Error, invalid propdef to call at runtime !"
+ return null
+ end
+ else if mproperty.name == "init" then
+ var nclassdef = self.modelbuilder.mclassdef2nclassdef[mpropdef.mclassdef]
+ self.parameter_check(nclassdef, mpropdef, args)
+ return nclassdef.call(self, mpropdef, args)
+ else
+ fatal("Fatal Error: method {mpropdef} not found in the AST")
+ abort
+ end
+ end
+
+ # Evaluates dynamically a snippet of Nit code
+ # `nit_code` : Nit code to be executed
+ fun eval(nit_code: String)
+ do
+ var local_toolctx = modelbuilder.toolcontext
+ local_toolctx.dbg = self
+ var e = local_toolctx.parse_something(nit_code)
+ if e isa AExpr then
+ nit_code = "print " + nit_code
+ e = local_toolctx.parse_something(nit_code)
+ end
+ if e isa AModule then
+ local_toolctx.had_error = false
+ modelbuilder.load_rt_module(self.mainmodule, e, "rt_module")
+ local_toolctx.run_phases([e])
+ if local_toolctx.had_error then
+ modelbuilder.model.try_remove_module(e.mmodule.as(not null))
+ local_toolctx.dbg = null
+ return
+ end
+ var mmod = e.mmodule
+ if mmod != null then
+ self.mainmodule = mmod
+ var local_classdefs = mmod.mclassdefs
+ var sys_type = mmod.sys_type
+ if sys_type == null then
+ print "Fatal error, cannot find Class Sys !\nAborting"
+ abort
+ end
+ var mobj = new MutableInstance(sys_type)
+ init_instance(mobj)
+ var initprop = mmod.try_get_primitive_method("init", sys_type.mclass)
+ if initprop != null then
+ self.send(initprop, [mobj])
+ end
+ self.check_init_instance(mobj)
+ var mainprop = mmod.try_get_primitive_method("main", sys_type.mclass)
+ if mainprop != null then
+ self.rt_send(mainprop, [mobj])
+ end
+ else
+ print "Error while loading_rt_module"
+ end
+ else
+ print "Error when parsing, e = {e.class_name}"
+ end
+ local_toolctx.dbg = null
+ end
+
# Encpasulates the behaviour for step over/out
private fun steps_fun_call(n: AExpr)
do
print "untrace variable_name : Removes the trace on the variable you chose to trace earlier in the program"
print "kill : kills the current program (Exits with an error and stack trace)\n"
end
+end
+
+redef class AConcreteMethPropdef
+ # Same as call except it will copy local variables of the parent frame to the frame defined in this call.
+ # Not supposed to be used by anyone else than the Debugger.
+ private fun rt_call(v: Debugger, mpropdef: MMethodDef, args: Array[Instance]): nullable Instance
+ do
+ var f = new Frame(self, self.mpropdef.as(not null), args)
+ var curr_instances = v.frame.map
+ for i in curr_instances.keys do
+ f.map[i] = curr_instances[i]
+ end
+ call_commons(v,mpropdef,args,f)
+ var currFra = v.frames.shift
+ for i in curr_instances.keys do
+ if currFra.map.keys.has(i) then
+ curr_instances[i] = currFra.map[i]
+ end
+ end
+ if v.returnmark == f then
+ v.returnmark = null
+ var res = v.escapevalue
+ v.escapevalue = null
+ return res
+ end
+ return null
+
+ end
end
# Traces the modifications of an object linked to a certain frame