rta: use callsites to do the analysis
[nit.git] / src / debugger.nit
index eea8820..be9c26b 100644 (file)
@@ -241,6 +241,112 @@ class Debugger
                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
+                               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
@@ -358,14 +464,8 @@ class Debugger
        # continue reading commands from the console input
        fun process_debug_command(command:String): Bool
        do
-               # For lisibility
-               print "\n"
-
-               # Kills the current program
-               if command == "kill" then
-                       abort
                # Step-out command
-               else if command == "finish"
+               if command == "finish"
                then
                        return step_out
                # Step-in command
@@ -382,6 +482,23 @@ class Debugger
                # Continues execution until the end
                else if command == "c" then
                        return continue_exec
+               else if command == "nit" then
+                       printn "$~> "
+                       command = gets
+                       var nit_buf = new Buffer
+                       while not command == ":q" do
+                               nit_buf.append(command)
+                               nit_buf.append("\n")
+                               printn "$~> "
+                               command = gets
+                       end
+                       step_in
+                       eval(nit_buf.to_s)
+               else if command == "quit" then
+                       exit(0)
+               else if command == "abort" then
+                       print stack_trace
+                       exit(0)
                else
                        var parts_of_command = command.split_with(' ')
                        # Shows the value of a variable in the current frame
@@ -411,9 +528,8 @@ class Debugger
                        # Untraces the modifications on a variable
                        else if parts_of_command.length == 2 and parts_of_command[0] == "untrace" then
                                process_untrace_command(parts_of_command)
-                       # Lists all the commands available
                        else
-                               list_commands
+                               print "Unknown command \"{command}\""
                        end
                end
                return true
@@ -500,8 +616,6 @@ class Debugger
                var bp = get_breakpoint_from_command(parts_of_command)
                if bp != null then
                        place_breakpoint(bp)
-               else
-                       list_commands
                end
        end
 
@@ -524,8 +638,6 @@ class Debugger
                        remove_breakpoint(self.curr_file, parts_of_command[1].to_i)
                else if parts_of_command.length >= 3 and parts_of_command[2].is_numeric then
                        remove_breakpoint(parts_of_command[1], parts_of_command[2].to_i)
-               else
-                       list_commands
                end
        end
 
@@ -1111,8 +1223,6 @@ class Debugger
                then
                        bp.set_max_breaks(1)
                        place_breakpoint(bp)
-               else
-                       list_commands
                end
        end
 
@@ -1253,35 +1363,35 @@ class Debugger
                end
        end
 
-       #######################################################################
-       ##                     Command listing function                      ##
-       #######################################################################
+end
 
-       # Lists the commands available when using the debugger
-       fun list_commands
-       do
-               print "\nCommand not recognized\n"
-               print "Commands accepted : \n"
-               print "[break/b] line : Adds a breakpoint on line *line_nb* of the current file\n"
-               print "[break/b] file_name line_nb : Adds a breakpoint on line *line_nb* of file *file_name* \n"
-               print "[p/print] variable : [p/print] * shows the status of all the variables\n"
-               print "[p/print] variable[i] : Prints the value of the variable contained at position *i* in SequenceRead collection *variable*\n"
-               print "[p/print] variable[i..j]: Prints the value of all the variables contained between positions *i* and *j* in SequenceRead collection *variable*\n"
-               print "[p/print] stack: Prints a stack trace at current instruction\n"
-               print "Note : The arrays can be multi-dimensional (Ex : variable[i..j][k] will print all the values at position *k* of all the SequenceRead collections contained between positions *i* and *j* in SequenceRead collection *variable*)\n"
-               print "s : steps in on the current function\n"
-               print "n : steps-over the current instruction\n"
-               print "finish : steps out of the current function\n"
-               print "variable as alias : Adds an alias called *alias* for the variable *variable*"
-               print "An alias can reference another alias\n"
-               print "variable = value : Sets the value of *variable* to *value*\n"
-               print "[d/delete] line_nb : Removes a breakpoint on line *line_nb* of the current file \n"
-               print "[d/delete] file_name line_nb : Removes a breakpoint on line *line_nb* of file *file_name* \n"
-               print "trace variable_name [break/print] : Traces the uses of the variable you chose to trace by printing the statement it appears in or by breaking on each use."
-               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
+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