1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013 Lucas Bajolet <lucas.bajolet@gmail.com>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Debugging of a nit program using the NaiveInterpreter
21 intrude import naive_interpreter
23 intrude import local_var_init
25 intrude import toolcontext
28 # Cleans the model to remove a module and what it defines when semantic analysis fails on injected code
29 private fun try_remove_module
(m
: MModule): Bool
32 for i
in [0 .. mmodules
.length
[ do
33 if mmodules
[i
] == m
then
38 if index
== -1 then return false
39 var mmodule
= mmodules
[index
]
40 mmodules
.remove_at
(index
)
41 for classdef
in mmodule
.mclassdefs
do
42 var mclass
= classdef
.mclass
43 for i
in [0 .. mclass
.mclassdefs
.length
[ do
44 if mclass
.mclassdefs
[i
] == classdef
then
49 mclass
.mclassdefs
.remove_at
(index
)
50 var propdefs
= classdef
.mpropdefs
51 for propdef
in propdefs
do
52 var prop
= propdef
.mproperty
53 for i
in [0..prop
.mpropdefs
.length
[ do
54 if prop
.mpropdefs
[i
] == propdef
then
59 prop
.mpropdefs
.remove_at
(index
)
66 redef class ScopeVisitor
68 redef init(toolcontext
)
71 if toolcontext
.dbg
!= null then
72 var localvars
= toolcontext
.dbg
.frame
.map
73 for i
in localvars
.keys
do
74 scopes
.first
.variables
[i
.to_s
] = i
81 redef class LocalVarInitVisitor
82 redef fun mark_is_unset
(node
: AExpr, variable
: nullable Variable)
85 if toolcontext
.dbg
!= null then
86 var varname
= variable
.to_s
87 var instmap
= toolcontext
.dbg
.frame
.map
88 for i
in instmap
.keys
do
89 if i
.to_s
== varname
then
90 mark_is_set
(node
, variable
)
98 redef class ToolContext
99 private var dbg
: nullable Debugger = null
101 private var had_error
: Bool = false
103 redef fun check_errors
108 if messages
.length
> 0 then
109 message_sorter
.sort
(messages
)
112 if "Warning".search_in
(m
.text
, 0) == null then had_error
= true
113 stderr
.write
("{m.to_color_string}\n")
122 var opt_debugger_mode
: OptionBool = new OptionBool("Launches the target program with the debugger attached to it", "-d")
124 var opt_debugger_autorun
: OptionBool = new OptionBool("Launches the target program with the interpreter, such as when the program fails, the debugging prompt is summoned", "-c")
129 self.option_context
.add_option
(self.opt_debugger_mode
)
130 self.option_context
.add_option
(self.opt_debugger_autorun
)
134 redef class ModelBuilder
135 # Execute the program from the entry point (Sys::main) of the `mainmodule`
136 # `arguments` are the command-line arguments in order
138 # 1. the AST is fully loaded.
139 # 2. the model is fully built.
140 # 3. the instructions are fully analysed.
141 fun run_debugger
(mainmodule
: MModule, arguments
: Array[String])
144 self.toolcontext
.info
("*** START INTERPRETING ***", 1)
146 var interpreter
= new Debugger(self, mainmodule
, arguments
)
148 init_naive_interpreter
(interpreter
, mainmodule
)
151 self.toolcontext
.info
("*** END INTERPRETING: {time1-time0} ***", 2)
154 fun run_debugger_autorun
(mainmodule
: MModule, arguments
: Array[String])
157 self.toolcontext
.info
("*** START INTERPRETING ***", 1)
159 var interpreter
= new Debugger(self, mainmodule
, arguments
)
160 interpreter
.autocontinue
= true
162 init_naive_interpreter
(interpreter
, mainmodule
)
165 self.toolcontext
.info
("*** END INTERPRETING: {time1-time0} ***", 2)
169 # The class extending `NaiveInterpreter` by adding debugging methods
171 super NaiveInterpreter
173 # Keeps the frame count in memory to find when to stop
174 # and launch the command prompt after a step out call
175 var step_stack_count
= 1
177 # Triggers a step over an instruction in a nit program
178 var stop_after_step_over_trigger
= true
180 # Triggers a step out of an instruction
181 var stop_after_step_out_trigger
= false
183 # Triggers a step in a instruction (enters a function
184 # if the instruction is a function call)
185 var step_in_trigger
= false
187 # HashMap containing the breakpoints bound to a file
188 var breakpoints
= new HashMap[String, HashSet[Breakpoint]]
190 # Contains the current file
193 # Aliases hashmap (maps an alias to a variable name)
194 var aliases
= new HashMap[String, String]
196 # Set containing all the traced variables and their related frame
197 private var traces
= new HashSet[TraceObject]
199 # Map containing all the positions for the positions of the arguments traced
201 private var fun_call_arguments_positions
= new HashMap[Int, TraceObject]
203 # Triggers the remapping of a trace object in the local context after a function call
204 var aftermath
= false
206 # Used to prevent the case when the body of the function called is empty
207 # If it is not, then, the remapping won't be happening
208 var frame_count_aftermath
= 1
210 # Auto continues the execution until the end or until an error is encountered
211 var autocontinue
= false
213 #######################################################################
214 ## Execution of statement function ##
215 #######################################################################
217 # Main loop, every call to a debug command is done here
218 redef fun stmt
(n
: nullable AExpr)
220 if n
== null then return
222 var frame
= self.frame
223 var old
= frame
.current_node
224 frame
.current_node
= n
226 if not self.autocontinue
then
227 if not n
isa ABlockExpr then
232 check_funcall_and_traced_args
(n
)
236 check_if_vars_are_traced
(n
)
241 frame
.current_node
= old
244 # Encpasulates the behaviour for step over/out
245 private fun steps_fun_call
(n
: AExpr)
247 if self.stop_after_step_over_trigger
then
248 if self.frames
.length
<= self.step_stack_count
then
249 n
.debug
("Execute stmt {n.to_s}")
250 while read_cmd
do end
252 else if self.stop_after_step_out_trigger
then
253 if frames
.length
< self.step_stack_count
then
254 n
.debug
("Execute stmt {n.to_s}")
255 while read_cmd
do end
257 else if step_in_trigger
then
258 n
.debug
("Execute stmt {n.to_s}")
259 while read_cmd
do end
263 # Checks if a breakpoint is encountered, and launches the debugging prompt if true
264 private fun breakpoint_check
(n
: AExpr)
266 var currFileNameSplit
= self.frame
.current_node
.location
.file
.filename
.to_s
.split_with
("/")
268 self.curr_file
= currFileNameSplit
[currFileNameSplit
.length-1
]
270 var breakpoint
= find_breakpoint
(curr_file
, n
.location
.line_start
)
272 if breakpoints
.keys
.has
(curr_file
) and breakpoint
!= null then
276 if not breakpoint
.is_valid
278 remove_breakpoint
(curr_file
, n
.location
.line_start
)
281 n
.debug
("Execute stmt {n.to_s}")
282 while read_cmd
do end
286 # Check if a variable of current expression is traced
287 # Then prints and/or breaks for command prompt
288 private fun check_if_vars_are_traced
(n
: AExpr)
290 var identifiers_in_instruction
= get_identifiers_in_current_instruction
(n
.location
.text
)
292 for i
in identifiers_in_instruction
do
293 var variable
= seek_variable
(i
, frame
)
294 for j
in self.traces
do
295 if j
.is_variable_traced_in_frame
(i
, frame
) then
296 n
.debug
("Traced variable {i} used")
297 if j
.break_on_encounter
then while read_cmd
do end
304 # Function remapping all the traced objects to match their name in the local context
305 private fun remap
(n
: AExpr)
307 if self.aftermath
then
309 # Trace every argument variable pre-specified
310 if self.frame_count_aftermath
< frames
.length
and fun_call_arguments_positions
.length
> 0 then
312 var ids_in_fun_def
= get_identifiers_in_current_instruction
(get_function_arguments
(frame
.mpropdef
.location
.text
))
314 for i
in fun_call_arguments_positions
.keys
do
315 self.fun_call_arguments_positions
[i
].add_frame_variable
(frame
, ids_in_fun_def
[i
])
319 self.aftermath
= false
323 # If the current instruction is a function call
324 # We analyse its signature and the position of traced arguments if the call
325 # For future remapping when inside the function
326 private fun check_funcall_and_traced_args
(n
: AExpr) do
327 # If we have a function call, we need to see if any of the arguments is traced (including the caller)
328 # if it is, next time we face an instruction, we'll trace the local version on the traced variable in the next frame
329 if n
isa ACallExpr then
330 self.aftermath
= true
331 self.frame_count_aftermath
= frames
.length
332 fun_call_arguments_positions
.clear
333 var fun_arguments
= get_identifiers_in_current_instruction
(get_function_arguments
(n
.location
.text
))
335 for i
in self.traces
do
336 for j
in [0 .. fun_arguments
.length
- 1] do
337 if i
.is_variable_traced_in_frame
(fun_arguments
[j
],frame
) then
338 fun_call_arguments_positions
[j
] = i
345 #######################################################################
346 ## Processing commands functions ##
347 #######################################################################
352 return process_debug_command
(gets
)
355 # Takes a user command as a parameter
357 # Returns a boolean value, representing whether or not to
358 # continue reading commands from the console input
359 fun process_debug_command
(command
:String): Bool
364 # Kills the current program
365 if command
== "kill" then
368 else if command
== "finish"
372 else if command
== "s"
376 else if command
== "n" then
378 # Opens a new NitIndex prompt on current model
379 else if command
== "nitx" then
380 new NitIndex.with_infos
(modelbuilder
, self.mainmodule
).prompt
382 # Continues execution until the end
383 else if command
== "c" then
386 var parts_of_command
= command
.split_with
(' ')
387 # Shows the value of a variable in the current frame
388 if parts_of_command
[0] == "p" or parts_of_command
[0] == "print" then
389 print_command
(parts_of_command
)
390 # Places a breakpoint on line x of file y
391 else if parts_of_command
[0] == "break" or parts_of_command
[0] == "b"
393 process_place_break_fun
(parts_of_command
)
394 # Places a temporary breakpoint on line x of file y
395 else if parts_of_command
[0] == "tbreak" and (parts_of_command
.length
== 2 or parts_of_command
.length
== 3)
397 process_place_tbreak_fun
(parts_of_command
)
398 # Removes a breakpoint on line x of file y
399 else if parts_of_command
[0] == "d" or parts_of_command
[0] == "delete" then
400 process_remove_break_fun
(parts_of_command
)
401 # Sets an alias for a variable
402 else if parts_of_command
.length
== 3 and parts_of_command
[1] == "as"
404 add_alias
(parts_of_command
[0], parts_of_command
[2])
405 # Modifies the value of a variable in the current frame
406 else if parts_of_command
.length
>= 3 and parts_of_command
[1] == "=" then
407 process_mod_function
(parts_of_command
)
408 # Traces the modifications on a variable
409 else if parts_of_command
.length
>= 2 and parts_of_command
[0] == "trace" then
410 process_trace_command
(parts_of_command
)
411 # Untraces the modifications on a variable
412 else if parts_of_command
.length
== 2 and parts_of_command
[0] == "untrace" then
413 process_untrace_command
(parts_of_command
)
414 # Lists all the commands available
422 #######################################################################
423 ## Processing specific command functions ##
424 #######################################################################
426 # Sets the flags to step-over an instruction in the current file
429 self.step_stack_count
= frames
.length
430 self.stop_after_step_over_trigger
= true
431 self.stop_after_step_out_trigger
= false
432 self.step_in_trigger
= false
436 #Sets the flags to step-out of a function
439 self.stop_after_step_over_trigger
= false
440 self.stop_after_step_out_trigger
= true
441 self.step_in_trigger
= false
442 self.step_stack_count
= frames
.length
446 # Sets the flags to step-in an instruction
449 self.step_in_trigger
= true
450 self.stop_after_step_over_trigger
= false
451 self.stop_after_step_out_trigger
= false
455 # Sets the flags to continue execution
456 fun continue_exec
: Bool
458 self.stop_after_step_over_trigger
= false
459 self.stop_after_step_out_trigger
= false
460 self.step_in_trigger
= false
464 # Prints the demanded variable in the command
466 # The name of the variable in in position 1 of the array 'parts_of_command'
467 fun print_command
(parts_of_command
: Array[String])
469 if parts_of_command
[1] == "*" then
470 var map_of_instances
= frame
.map
472 var keys
= map_of_instances
.iterator
474 print
"Variables collection : \n"
476 for instance
in map_of_instances
.keys
do
477 print
"Variable {instance.to_s}, Instance {map_of_instances[instance].to_s}"
480 print
"\nEnd of current instruction \n"
481 else if parts_of_command
[1] == "stack" then
482 print
self.stack_trace
483 else if parts_of_command
[1].has
('[') and parts_of_command
[1].has
(']') then
484 process_array_command
(parts_of_command
)
486 var instance
= seek_variable
(get_real_variable_name
(parts_of_command
[1]), frame
)
490 print_instance
(instance
)
492 print
"Cannot find variable {parts_of_command[1]}"
497 # Processes the input string to know where to put a breakpoint
498 fun process_place_break_fun
(parts_of_command
: Array[String])
500 var bp
= get_breakpoint_from_command
(parts_of_command
)
508 # Returns a breakpoint containing the informations stored in the command
509 fun get_breakpoint_from_command
(parts_of_command
: Array[String]): nullable Breakpoint
511 if parts_of_command
[1].is_numeric
then
512 return new Breakpoint(parts_of_command
[1].to_i
, curr_file
)
513 else if parts_of_command
.length
>= 3 and parts_of_command
[2].is_numeric
then
514 return new Breakpoint(parts_of_command
[2].to_i
, parts_of_command
[1])
520 # Processes the command of removing a breakpoint on specified line and file
521 fun process_remove_break_fun
(parts_of_command
: Array[String])
523 if parts_of_command
[1].is_numeric
then
524 remove_breakpoint
(self.curr_file
, parts_of_command
[1].to_i
)
525 else if parts_of_command
.length
>= 3 and parts_of_command
[2].is_numeric
then
526 remove_breakpoint
(parts_of_command
[1], parts_of_command
[2].to_i
)
532 # Processes an array print command
533 fun process_array_command
(parts_of_command
: Array[String])
535 var index_of_first_brace
= parts_of_command
[1].index_of
('[')
536 var variable_name
= get_real_variable_name
(parts_of_command
[1].substring
(0,index_of_first_brace
))
537 var braces
= parts_of_command
[1].substring_from
(index_of_first_brace
)
539 var indexes
= remove_braces
(braces
)
541 var index_array
= new Array[Array[Int]]
543 if indexes
!= null then
544 for index
in indexes
do
545 var temp_indexes_array
= process_index
(index
)
546 if temp_indexes_array
!= null then
547 index_array
.push
(temp_indexes_array
)
548 #print index_array.last
553 var instance
= seek_variable
(variable_name
, frame
)
555 if instance
!= null then
556 print_nested_collection
(instance
, index_array
, 0, variable_name
, "")
558 print
"Cannot find variable {variable_name}"
562 # Processes the modification function to modify a variable dynamically
564 # Command of type variable = value
565 fun process_mod_function
(parts_of_command
: Array[String])
567 parts_of_command
[0] = get_real_variable_name
(parts_of_command
[0])
568 var parts_of_variable
= parts_of_command
[0].split_with
(".")
570 if parts_of_variable
.length
> 1 then
571 var last_part
= parts_of_variable
.pop
572 var first_part
= parts_of_command
[0].substring
(0,parts_of_command
[0].length
- last_part
.length
- 1)
573 var papa
= seek_variable
(first_part
, frame
)
575 if papa
!= null and papa
isa MutableInstance then
576 var attribute
= get_attribute_in_mutable_instance
(papa
, last_part
)
578 if attribute
!= null then
579 modify_argument_of_complex_type
(papa
, attribute
, parts_of_command
[2])
583 var target
= seek_variable
(parts_of_variable
[0], frame
)
584 if target
!= null then
585 modify_in_frame
(target
, parts_of_command
[2])
590 # Processes the untrace variable command
592 # Command pattern : "untrace variable"
593 fun process_untrace_command
(parts_of_command
: Array[String])
595 var variable_name
= get_real_variable_name
(parts_of_command
[1])
596 if untrace_variable
(variable_name
) then
597 print
"Untraced variable {parts_of_command[1]}"
599 print
"{parts_of_command[1]} is not traced"
603 # Processes the trace variable command
605 # Command pattern : "trace variable [break/print]"
606 fun process_trace_command
(parts_of_command
: Array[String])
608 var variable_name
= get_real_variable_name
(parts_of_command
[1])
611 if seek_variable
(variable_name
, frame
) == null then
612 print
"Cannot find a variable called {parts_of_command[1]}"
616 if parts_of_command
.length
== 3 then
617 if parts_of_command
[2] == "break" then
626 trace_variable
(variable_name
, breaker
)
628 print
"Successfully tracing {parts_of_command[1]}"
631 #######################################################################
632 ## Trace Management functions ##
633 #######################################################################
635 # Effectively untraces the variable called *variable_name*
637 # Returns true if the variable exists, false otherwise
638 private fun untrace_variable
(variable_name
: String): Bool
640 var to_remove
: nullable TraceObject = null
641 for i
in self.traces
do
642 if i
.is_variable_traced_in_frame
(variable_name
, frame
) then
647 if to_remove
!= null then
648 self.traces
.remove
(to_remove
)
655 # Effectively traces the variable *variable_name* either in print or break mode depending on the value of breaker (break if true, print if false)
657 private fun trace_variable
(variable_name
: String, breaker
: Bool)
659 for i
in self.traces
do
660 if i
.is_variable_traced_in_frame
(variable_name
, frame
) then
661 print
"This variable is already traced"
666 var trace_object
: TraceObject
669 trace_object
= new TraceObject(true)
671 trace_object
= new TraceObject(false)
674 # We trace the current variable found for the current frame
675 trace_object
.add_frame_variable
(self.frame
, variable_name
)
677 var position_of_variable_in_arguments
= get_position_of_variable_in_arguments
(frame
, variable_name
)
679 # Start parsing the frames starting with the parent of the current one, until the highest
680 # When the variable traced is declared locally, the loop stops
681 for i
in [1 .. frames
.length-1
] do
683 # If the variable was reported to be an argument of the previous frame
684 if position_of_variable_in_arguments
!= -1 then
686 var local_name
= get_identifiers_in_current_instruction
(get_function_arguments
(frames
[i
].current_node
.location
.text
))[position_of_variable_in_arguments
]
688 position_of_variable_in_arguments
= get_position_of_variable_in_arguments
(frames
[i
], local_name
)
690 trace_object
.add_frame_variable
(frames
[i
], local_name
)
696 self.traces
.add
(trace_object
)
699 # If the variable *variable_name* is an argument of the function being executed in the frame *frame*
700 # The function returns its position in the arguments
701 # Else, it returns -1
702 private fun get_position_of_variable_in_arguments
(frame
: Frame, variable_name
: String): Int
704 var identifiers
= get_identifiers_in_current_instruction
(get_function_arguments
(frame
.mpropdef
.location
.text
))
705 for i
in [0 .. identifiers
.length-1
] do
706 # If the current traced variable is an argument of the current function, we trace its parent (at least)
707 if identifiers
[i
] == variable_name
then return i
712 # Gets all the identifiers of an instruction (uses the rules of Nit as of Mar 05 2013)
714 fun get_identifiers_in_current_instruction
(instruction
: AbstractString): Array[String]
716 var result_array
= new Array[String]
717 var instruction_buffer
= new Buffer
719 var trigger_char_escape
= false
720 var trigger_string_escape
= false
721 var trigger_concat_in_string
= false
723 for i
in instruction
do
724 if trigger_char_escape
then
725 if i
== '\'' then trigger_char_escape = false
726 else if trigger_string_escape then
728 trigger_concat_in_string = true
729 trigger_string_escape = false
730 else if i == '\
"' then trigger_string_escape = false
732 if i.is_alphanumeric or i == '_' then
733 instruction_buffer.add(i)
734 else if i == '.' then
735 if instruction_buffer.is_numeric or (instruction_buffer[0] >= 'A' and instruction_buffer[0] <= 'Z') then
736 instruction_buffer.clear
738 result_array.push(instruction_buffer.to_s)
739 instruction_buffer.add(i)
741 else if i == '\'' then
742 trigger_char_escape = true
743 else if i == '\"' then
744 trigger_string_escape = true
745 else if i == '}' then
746 trigger_concat_in_string = false
747 trigger_string_escape = true
749 if instruction_buffer.length > 0 and not instruction_buffer.is_numeric and not (instruction_buffer[0] >= 'A
' and instruction_buffer[0] <= 'Z
') then result_array.push(instruction_buffer.to_s)
750 instruction_buffer.clear
755 if instruction_buffer.length > 0 and not instruction_buffer.is_numeric and not (instruction_buffer[0] >= 'A
' and instruction_buffer[0] <= 'Z
') then result_array.push(instruction_buffer.to_s)
760 # Takes a function call or declaration and strips all but the arguments
762 fun get_function_arguments(function: AbstractString): String
765 var trigger_copy = false
768 if i == ')' then break
769 if trigger_copy then buf.add(i)
770 if i == '(' then trigger_copy = true
776 #######################################################################
777 ## Alias management functions ##
778 #######################################################################
780 # Adds a new alias to the tables
781 fun add_alias(var_represented: String, alias: String)
783 self.aliases[alias] = var_represented
786 # Gets the real name of a variable hidden by an alias
787 fun get_variable_name_by_alias(alias: String): nullable String
789 if self.aliases.keys.has(alias) then
790 return self.aliases[alias]
796 # Gets the variable named by name, whether it is an alias or not
797 fun get_real_variable_name(name: String): String
799 var explode_string = name.split_with(".")
800 var final_string = new Buffer
801 for i in explode_string do
802 var alias_resolved = get_variable_name_by_alias(i)
803 if alias_resolved != null then
804 final_string.append(get_real_variable_name(alias_resolved))
806 final_string.append(i)
808 final_string.append(".")
811 return final_string.substring(0,final_string.length-1).to_s
814 #######################################################################
815 ## Print functions ##
816 #######################################################################
818 # Prints an object instance and its attributes if it has some
820 # If it is a primitive type, its value is directly printed
821 fun print_instance(instance: Instance)
823 print "Printing innards of a variable"
825 if instance isa MutableInstance then
826 var attributes = instance.attributes
827 print "Object : {instance}"
829 for current_attribute in attributes.keys do
830 print "Attribute : {current_attribute.to_s} \nValeur : {attributes[current_attribute].to_s}"
833 print "Found variable {instance}"
836 print "Stopping printing innards of a variable"
839 # Prints the attributes demanded in a SequenceRead
840 # Used recursively to print nested collections
841 fun print_nested_collection(instance: Instance, indexes: Array[Array[Int]], depth: Int, variable_name: String, depth_string: String)
843 var collection: nullable SequenceRead[Object] = null
844 var real_collection_length: nullable Int = null
846 if instance isa MutableInstance then
847 real_collection_length = get_collection_instance_real_length(instance)
848 collection = get_primary_collection(instance)
851 if collection != null and real_collection_length != null then
852 for i in indexes[depth] do
853 if i >= 0 and i < real_collection_length then
854 if depth == indexes.length-1 then
855 print "{variable_name}{depth_string}[{i}] = {collection[i]}"
857 var item_i = collection[i]
859 if item_i isa MutableInstance then
860 print_nested_collection(item_i, indexes, depth+1, variable_name, depth_string+"[{i}]")
862 print "The item at {variable_name}{depth_string}[{i}] is not a collection"
867 print "Out of bounds exception : i = {i} and collection_length = {real_collection_length.to_s}"
871 else if i >= real_collection_length then
877 if collection == null then
878 print "Cannot find {variable_name}{depth_string}"
879 else if real_collection_length != null then
880 print "Cannot find attribute length in {instance}"
882 print "Unknown error."
888 #######################################################################
889 ## Variable Exploration functions ##
890 #######################################################################
892 # Seeks a variable from the current frame called 'variable_path
', can introspect complex objects using function get_variable_in_mutable_instance
893 private fun seek_variable(variable_path: String, frame: Frame): nullable Instance
895 var full_variable = variable_path.split_with(".")
897 var full_variable_iterator = full_variable.iterator
899 var first_instance = get_variable_in_frame(full_variable_iterator.item, frame)
901 if first_instance == null then return null
903 if full_variable.length <= 1 then return first_instance
905 full_variable_iterator.next
907 if not (first_instance isa MutableInstance and full_variable_iterator.is_ok) then return null
909 return get_variable_in_mutable_instance(first_instance, full_variable_iterator)
912 # Gets a variable 'variable_name
' contained in the frame 'frame
'
913 private fun get_variable_in_frame(variable_name: String, frame: Frame): nullable Instance
915 if variable_name == "self" then
916 if frame.arguments.length >= 1 then return frame.arguments.first
919 var map_of_instances = frame.map
921 for key in map_of_instances.keys do
922 if key.to_s == variable_name then
923 return map_of_instances[key]
930 # Gets an attribute 'attribute_name
' contained in variable 'variable
'
931 fun get_attribute_in_mutable_instance(variable: MutableInstance, attribute_name: String): nullable MAttribute
933 var map_of_attributes = variable.attributes
935 for key in map_of_attributes.keys do
936 if key.to_s.substring_from(1) == attribute_name then
944 # Recursive function, returns the variable described by 'total_chain
'
945 fun get_variable_in_mutable_instance(variable: MutableInstance, iterator: Iterator[String]): nullable Instance
947 var attribute = get_attribute_in_mutable_instance(variable, iterator.item)
949 if attribute == null then return null
953 if iterator.is_ok then
954 var new_variable = variable.attributes[attribute]
955 if new_variable isa MutableInstance then
956 return get_variable_in_mutable_instance(new_variable, iterator)
961 return variable.attributes[attribute]
965 #######################################################################
966 ## Array exploring functions ##
967 #######################################################################
969 # Gets the length of a collection
970 # Used by the debugger, else if we call Collection.length, it returns the capacity instead
971 fun get_collection_instance_real_length(collection: MutableInstance): nullable Int
973 var collection_length_attribute = get_attribute_in_mutable_instance(collection, "length")
975 var real_collection_length: nullable Int = null
977 if collection_length_attribute != null then
978 var primitive_length_instance = collection.attributes[collection_length_attribute]
979 if primitive_length_instance isa PrimitiveInstance[Int] then
980 return primitive_length_instance.val
987 # Processes the indexes of a print array call
988 # Returns an array containing all the indexes demanded
989 fun process_index(index_string: String): nullable Array[Int]
991 var from_end_index = index_string.index_of('.')
992 var to_start_index = index_string.last_index_of('.')
994 if from_end_index != -1 and to_start_index != -1 then
995 var index_from_string = index_string.substring(0,from_end_index)
996 var index_to_string = index_string.substring_from(to_start_index+1)
998 if index_from_string.is_numeric and index_to_string.is_numeric then
999 var result_array = new Array[Int]
1001 var index_from = index_from_string.to_i
1002 var index_to = index_to_string.to_i
1004 for i in [index_from..index_to] do
1005 result_array.push(i)
1011 if index_string.is_numeric
1013 var result_array = new Array[Int]
1015 result_array.push(index_string.to_i)
1026 # Gets a collection in a MutableInstance
1027 fun get_primary_collection(container: MutableInstance): nullable SequenceRead[Object]
1029 var items_of_array = get_attribute_in_mutable_instance(container, "items")
1030 if items_of_array != null then
1031 var array = container.attributes[items_of_array]
1033 if array isa PrimitiveInstance[Object] then
1034 var sequenceRead_final = array.val
1035 if sequenceRead_final isa SequenceRead[Object] then
1036 return sequenceRead_final
1044 # Removes the braces '[' ']' in a print array command
1045 # Returns an array containing their content
1046 fun remove_braces(braces: String): nullable Array[String]
1048 var buffer = new Buffer
1050 var result_array = new Array[String]
1052 var number_of_opening_brackets = 0
1053 var number_of_closing_brackets = 0
1055 var last_was_opening_bracket = false
1059 if last_was_opening_bracket then
1063 number_of_opening_brackets += 1
1064 last_was_opening_bracket = true
1065 else if i == ']' then
1066 if not last_was_opening_bracket then
1070 result_array.push(buffer.to_s)
1072 number_of_closing_brackets += 1
1073 last_was_opening_bracket = false
1074 else if i.is_numeric or i == '.' then
1075 buffer.append(i.to_s)
1076 else if not i == ' ' then
1081 if number_of_opening_brackets != number_of_closing_brackets then
1088 #######################################################################
1089 ## Breakpoint placing functions ##
1090 #######################################################################
1092 # Places a breakpoint on line 'line_to_break
' for file 'file_to_break
'
1093 fun place_breakpoint(breakpoint: Breakpoint)
1095 if not self.breakpoints.keys.has(breakpoint.file) then
1096 self.breakpoints[breakpoint.file] = new HashSet[Breakpoint]
1098 if find_breakpoint(breakpoint.file, breakpoint.line) == null then
1099 self.breakpoints[breakpoint.file].add(breakpoint)
1100 print "Breakpoint added on line {breakpoint.line} for file {breakpoint.file}"
1102 print "Breakpoint already present on line {breakpoint.line} for file {breakpoint.file}"
1106 #Places a breakpoint that will trigger once and be destroyed afterwards
1107 fun process_place_tbreak_fun(parts_of_command: Array[String])
1109 var bp = get_breakpoint_from_command(parts_of_command)
1112 bp.set_max_breaks(1)
1113 place_breakpoint(bp)
1119 #######################################################################
1120 ## Breakpoint removing functions ##
1121 #######################################################################
1123 # Removes a breakpoint on line 'line_to_break
' for file 'file_to_break
'
1124 fun remove_breakpoint(file_to_break: String, line_to_break: Int)
1126 if self.breakpoints.keys.has(file_to_break) then
1127 var bp = find_breakpoint(file_to_break, line_to_break)
1130 self.breakpoints[file_to_break].remove(bp)
1131 print "Breakpoint removed on line {line_to_break} for file {file_to_break}"
1136 print "No breakpoint existing on line {line_to_break} for file {file_to_break}"
1139 #######################################################################
1140 ## Breakpoint searching functions ##
1141 #######################################################################
1143 # Finds a breakpoint for 'file
' and 'line
' in the class HashMap
1144 fun find_breakpoint(file: String, line: Int): nullable Breakpoint
1146 if self.breakpoints.keys.has(file)
1148 for i in self.breakpoints[file]
1160 #######################################################################
1161 ## Runtime modification functions ##
1162 #######################################################################
1164 # Modifies the value of a variable contained in a frame
1165 fun modify_in_frame(variable: Instance, value: String)
1167 var new_variable = get_variable_of_type_with_value(variable.mtype.to_s, value)
1168 if new_variable != null
1170 var keys = frame.map.keys
1173 if frame.map[key] == variable
1175 frame.map[key] = new_variable
1181 # Modifies the value of a variable contained in a MutableInstance
1182 fun modify_argument_of_complex_type(papa: MutableInstance, attribute: MAttribute, value: String)
1184 var final_variable = papa.attributes[attribute]
1185 var type_of_variable = final_variable.mtype.to_s
1186 var new_variable = get_variable_of_type_with_value(type_of_variable, value)
1187 if new_variable != null
1189 papa.attributes[attribute] = new_variable
1193 #######################################################################
1194 ## Variable generator functions ##
1195 #######################################################################
1197 # Returns a new variable of the type 'type_of_variable
' with a value of 'value
'
1198 fun get_variable_of_type_with_value(type_of_variable: String, value: String): nullable Instance
1200 if type_of_variable == "Int" then
1201 return get_int(value)
1202 else if type_of_variable == "Float" then
1203 return get_float(value)
1204 else if type_of_variable == "Bool" then
1205 return get_bool(value)
1206 else if type_of_variable == "Char" then
1207 return get_char(value)
1213 # Returns a new int instance with value 'value
'
1214 fun get_int(value: String): nullable Instance
1216 if value.is_numeric then
1217 return int_instance(value.to_i)
1223 # Returns a new float instance with value 'value
'
1224 fun get_float(value:String): nullable Instance
1226 if value.is_numeric then
1227 return float_instance(value.to_f)
1233 # Returns a new char instance with value 'value
'
1234 fun get_char(value: String): nullable Instance
1236 if value.length >= 1 then
1237 return char_instance(value[0])
1243 # Returns a new bool instance with value 'value
'
1244 fun get_bool(value: String): nullable Instance
1246 if value.to_lower == "true" then
1247 return self.true_instance
1248 else if value.to_lower == "false" then
1249 return self.false_instance
1251 print "Invalid value, a boolean must be set at \"true\" or \"false\""
1256 #######################################################################
1257 ## Command listing function ##
1258 #######################################################################
1260 # Lists the commands available when using the debugger
1263 print "\nCommand not recognized\n"
1264 print "Commands accepted : \n"
1265 print "[break/b] line : Adds a breakpoint on line *line_nb* of the current file\n"
1266 print "[break/b] file_name line_nb : Adds a breakpoint on line *line_nb* of file *file_name* \n"
1267 print "[p/print] variable : [p/print] * shows the status of all the variables\n"
1268 print "[p/print] variable[i] : Prints the value of the variable contained at position *i* in SequenceRead collection *variable*\n"
1269 print "[p/print] variable[i..j]: Prints the value of all the variables contained between positions *i* and *j* in SequenceRead collection *variable*\n"
1270 print "[p/print] stack: Prints a stack trace at current instruction\n"
1271 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"
1272 print "s : steps in on the current function\n"
1273 print "n : steps-over the current instruction\n"
1274 print "finish : steps out of the current function\n"
1275 print "variable as alias : Adds an alias called *alias* for the variable *variable*"
1276 print "An alias can reference another alias\n"
1277 print "variable = value : Sets the value of *variable* to *value*\n"
1278 print "[d/delete] line_nb : Removes a breakpoint on line *line_nb* of the current file \n"
1279 print "[d/delete] file_name line_nb : Removes a breakpoint on line *line_nb* of file *file_name* \n"
1280 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."
1281 print "untrace variable_name : Removes the trace on the variable you chose to trace earlier in the program"
1282 print "kill : kills the current program (Exits with an error and stack trace)\n"
1287 # Traces the modifications of an object linked to a certain frame
1288 private class TraceObject
1290 # Map of the local names bound to a frame
1291 var trace_map: HashMap[Frame, String]
1292 # Decides if breaking or printing statement when the variable is encountered
1293 var break_on_encounter: Bool
1295 init(break_on_encounter: Bool)
1297 trace_map = new HashMap[Frame, String]
1298 self.break_on_encounter = break_on_encounter
1301 # Adds the local alias for a variable and the frame bound to it
1302 fun add_frame_variable(frame: Frame, variable_name: String)
1304 self.trace_map[frame] = variable_name
1307 # Checks if the prompted variable is traced in the specified frame
1308 fun is_variable_traced_in_frame(variable_name: String, frame: Frame): Bool
1310 if self.trace_map.has_key(frame) then
1311 if self.trace_map[frame] == variable_name then
1323 # Breaks automatically when encountering an error
1324 # Permits the injunction of commands before crashing
1325 redef private fun fatal(v: NaiveInterpreter, message: String)
1327 if v isa Debugger then
1328 print "An error was encountered, the program will stop now."
1330 while v.process_debug_command(gets) do end