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 toolcontext
25 redef class ToolContext
26 private var dbg
: nullable Debugger = null
28 private var had_error
: Bool = false
30 redef fun check_errors
35 if messages
.length
> 0 then
36 message_sorter
.sort
(messages
)
39 if "Warning".search_in
(m
.text
, 0) == null then had_error
= true
40 stderr
.write
("{m.to_color_string}\n")
49 var opt_debugger_mode
: OptionBool = new OptionBool("Launches the target program with the debugger attached to it", "-d")
51 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")
56 self.option_context
.add_option
(self.opt_debugger_mode
)
57 self.option_context
.add_option
(self.opt_debugger_autorun
)
61 redef class ModelBuilder
62 # Execute the program from the entry point (Sys::main) of the `mainmodule`
63 # `arguments` are the command-line arguments in order
65 # 1. the AST is fully loaded.
66 # 2. the model is fully built.
67 # 3. the instructions are fully analysed.
68 fun run_debugger
(mainmodule
: MModule, arguments
: Array[String])
71 self.toolcontext
.info
("*** START INTERPRETING ***", 1)
73 var interpreter
= new Debugger(self, mainmodule
, arguments
)
75 init_naive_interpreter
(interpreter
, mainmodule
)
78 self.toolcontext
.info
("*** END INTERPRETING: {time1-time0} ***", 2)
81 fun run_debugger_autorun
(mainmodule
: MModule, arguments
: Array[String])
84 self.toolcontext
.info
("*** START INTERPRETING ***", 1)
86 var interpreter
= new Debugger(self, mainmodule
, arguments
)
87 interpreter
.autocontinue
= true
89 init_naive_interpreter
(interpreter
, mainmodule
)
92 self.toolcontext
.info
("*** END INTERPRETING: {time1-time0} ***", 2)
96 # The class extending `NaiveInterpreter` by adding debugging methods
98 super NaiveInterpreter
100 # Keeps the frame count in memory to find when to stop
101 # and launch the command prompt after a step out call
102 var step_stack_count
= 1
104 # Triggers a step over an instruction in a nit program
105 var stop_after_step_over_trigger
= true
107 # Triggers a step out of an instruction
108 var stop_after_step_out_trigger
= false
110 # Triggers a step in a instruction (enters a function
111 # if the instruction is a function call)
112 var step_in_trigger
= false
114 # HashMap containing the breakpoints bound to a file
115 var breakpoints
= new HashMap[String, HashSet[Breakpoint]]
117 # Contains the current file
120 # Aliases hashmap (maps an alias to a variable name)
121 var aliases
= new HashMap[String, String]
123 # Set containing all the traced variables and their related frame
124 private var traces
= new HashSet[TraceObject]
126 # Map containing all the positions for the positions of the arguments traced
128 private var fun_call_arguments_positions
= new HashMap[Int, TraceObject]
130 # Triggers the remapping of a trace object in the local context after a function call
131 var aftermath
= false
133 # Used to prevent the case when the body of the function called is empty
134 # If it is not, then, the remapping won't be happening
135 var frame_count_aftermath
= 1
137 # Auto continues the execution until the end or until an error is encountered
138 var autocontinue
= false
140 #######################################################################
141 ## Execution of statement function ##
142 #######################################################################
144 # Main loop, every call to a debug command is done here
145 redef fun stmt
(n
: nullable AExpr)
147 if n
== null then return
149 var frame
= self.frame
150 var old
= frame
.current_node
151 frame
.current_node
= n
153 if not self.autocontinue
then
154 if not n
isa ABlockExpr then
159 check_funcall_and_traced_args
(n
)
163 check_if_vars_are_traced
(n
)
168 frame
.current_node
= old
171 # Encpasulates the behaviour for step over/out
172 private fun steps_fun_call
(n
: AExpr)
174 if self.stop_after_step_over_trigger
then
175 if self.frames
.length
<= self.step_stack_count
then
176 n
.debug
("Execute stmt {n.to_s}")
177 while read_cmd
do end
179 else if self.stop_after_step_out_trigger
then
180 if frames
.length
< self.step_stack_count
then
181 n
.debug
("Execute stmt {n.to_s}")
182 while read_cmd
do end
184 else if step_in_trigger
then
185 n
.debug
("Execute stmt {n.to_s}")
186 while read_cmd
do end
190 # Checks if a breakpoint is encountered, and launches the debugging prompt if true
191 private fun breakpoint_check
(n
: AExpr)
193 var currFileNameSplit
= self.frame
.current_node
.location
.file
.filename
.to_s
.split_with
("/")
195 self.curr_file
= currFileNameSplit
[currFileNameSplit
.length-1
]
197 var breakpoint
= find_breakpoint
(curr_file
, n
.location
.line_start
)
199 if breakpoints
.keys
.has
(curr_file
) and breakpoint
!= null then
203 if not breakpoint
.is_valid
205 remove_breakpoint
(curr_file
, n
.location
.line_start
)
208 n
.debug
("Execute stmt {n.to_s}")
209 while read_cmd
do end
213 # Check if a variable of current expression is traced
214 # Then prints and/or breaks for command prompt
215 private fun check_if_vars_are_traced
(n
: AExpr)
217 var identifiers_in_instruction
= get_identifiers_in_current_instruction
(n
.location
.text
)
219 for i
in identifiers_in_instruction
do
220 var variable
= seek_variable
(i
, frame
)
221 for j
in self.traces
do
222 if j
.is_variable_traced_in_frame
(i
, frame
) then
223 n
.debug
("Traced variable {i} used")
224 if j
.break_on_encounter
then while read_cmd
do end
231 # Function remapping all the traced objects to match their name in the local context
232 private fun remap
(n
: AExpr)
234 if self.aftermath
then
236 # Trace every argument variable pre-specified
237 if self.frame_count_aftermath
< frames
.length
and fun_call_arguments_positions
.length
> 0 then
239 var ids_in_fun_def
= get_identifiers_in_current_instruction
(get_function_arguments
(frame
.mpropdef
.location
.text
))
241 for i
in fun_call_arguments_positions
.keys
do
242 self.fun_call_arguments_positions
[i
].add_frame_variable
(frame
, ids_in_fun_def
[i
])
246 self.aftermath
= false
250 # If the current instruction is a function call
251 # We analyse its signature and the position of traced arguments if the call
252 # For future remapping when inside the function
253 private fun check_funcall_and_traced_args
(n
: AExpr) do
254 # If we have a function call, we need to see if any of the arguments is traced (including the caller)
255 # if it is, next time we face an instruction, we'll trace the local version on the traced variable in the next frame
256 if n
isa ACallExpr then
257 self.aftermath
= true
258 self.frame_count_aftermath
= frames
.length
259 fun_call_arguments_positions
.clear
260 var fun_arguments
= get_identifiers_in_current_instruction
(get_function_arguments
(n
.location
.text
))
262 for i
in self.traces
do
263 for j
in [0 .. fun_arguments
.length
- 1] do
264 if i
.is_variable_traced_in_frame
(fun_arguments
[j
],frame
) then
265 fun_call_arguments_positions
[j
] = i
272 #######################################################################
273 ## Processing commands functions ##
274 #######################################################################
279 return process_debug_command
(gets
)
282 # Takes a user command as a parameter
284 # Returns a boolean value, representing whether or not to
285 # continue reading commands from the console input
286 fun process_debug_command
(command
:String): Bool
291 # Kills the current program
292 if command
== "kill" then
295 else if command
== "finish"
299 else if command
== "s"
303 else if command
== "n" then
305 # Opens a new NitIndex prompt on current model
306 else if command
== "nitx" then
307 new NitIndex.with_infos
(modelbuilder
, self.mainmodule
).prompt
309 # Continues execution until the end
310 else if command
== "c" then
313 var parts_of_command
= command
.split_with
(' ')
314 # Shows the value of a variable in the current frame
315 if parts_of_command
[0] == "p" or parts_of_command
[0] == "print" then
316 print_command
(parts_of_command
)
317 # Places a breakpoint on line x of file y
318 else if parts_of_command
[0] == "break" or parts_of_command
[0] == "b"
320 process_place_break_fun
(parts_of_command
)
321 # Places a temporary breakpoint on line x of file y
322 else if parts_of_command
[0] == "tbreak" and (parts_of_command
.length
== 2 or parts_of_command
.length
== 3)
324 process_place_tbreak_fun
(parts_of_command
)
325 # Removes a breakpoint on line x of file y
326 else if parts_of_command
[0] == "d" or parts_of_command
[0] == "delete" then
327 process_remove_break_fun
(parts_of_command
)
328 # Sets an alias for a variable
329 else if parts_of_command
.length
== 3 and parts_of_command
[1] == "as"
331 add_alias
(parts_of_command
[0], parts_of_command
[2])
332 # Modifies the value of a variable in the current frame
333 else if parts_of_command
.length
>= 3 and parts_of_command
[1] == "=" then
334 process_mod_function
(parts_of_command
)
335 # Traces the modifications on a variable
336 else if parts_of_command
.length
>= 2 and parts_of_command
[0] == "trace" then
337 process_trace_command
(parts_of_command
)
338 # Untraces the modifications on a variable
339 else if parts_of_command
.length
== 2 and parts_of_command
[0] == "untrace" then
340 process_untrace_command
(parts_of_command
)
341 # Lists all the commands available
349 #######################################################################
350 ## Processing specific command functions ##
351 #######################################################################
353 # Sets the flags to step-over an instruction in the current file
356 self.step_stack_count
= frames
.length
357 self.stop_after_step_over_trigger
= true
358 self.stop_after_step_out_trigger
= false
359 self.step_in_trigger
= false
363 #Sets the flags to step-out of a function
366 self.stop_after_step_over_trigger
= false
367 self.stop_after_step_out_trigger
= true
368 self.step_in_trigger
= false
369 self.step_stack_count
= frames
.length
373 # Sets the flags to step-in an instruction
376 self.step_in_trigger
= true
377 self.stop_after_step_over_trigger
= false
378 self.stop_after_step_out_trigger
= false
382 # Sets the flags to continue execution
383 fun continue_exec
: Bool
385 self.stop_after_step_over_trigger
= false
386 self.stop_after_step_out_trigger
= false
387 self.step_in_trigger
= false
391 # Prints the demanded variable in the command
393 # The name of the variable in in position 1 of the array 'parts_of_command'
394 fun print_command
(parts_of_command
: Array[String])
396 if parts_of_command
[1] == "*" then
397 var map_of_instances
= frame
.map
399 var keys
= map_of_instances
.iterator
401 print
"Variables collection : \n"
403 for instance
in map_of_instances
.keys
do
404 print
"Variable {instance.to_s}, Instance {map_of_instances[instance].to_s}"
407 print
"\nEnd of current instruction \n"
408 else if parts_of_command
[1] == "stack" then
409 print
self.stack_trace
410 else if parts_of_command
[1].has
('[') and parts_of_command
[1].has
(']') then
411 process_array_command
(parts_of_command
)
413 var instance
= seek_variable
(get_real_variable_name
(parts_of_command
[1]), frame
)
417 print_instance
(instance
)
419 print
"Cannot find variable {parts_of_command[1]}"
424 # Processes the input string to know where to put a breakpoint
425 fun process_place_break_fun
(parts_of_command
: Array[String])
427 var bp
= get_breakpoint_from_command
(parts_of_command
)
435 # Returns a breakpoint containing the informations stored in the command
436 fun get_breakpoint_from_command
(parts_of_command
: Array[String]): nullable Breakpoint
438 if parts_of_command
[1].is_numeric
then
439 return new Breakpoint(parts_of_command
[1].to_i
, curr_file
)
440 else if parts_of_command
.length
>= 3 and parts_of_command
[2].is_numeric
then
441 return new Breakpoint(parts_of_command
[2].to_i
, parts_of_command
[1])
447 # Processes the command of removing a breakpoint on specified line and file
448 fun process_remove_break_fun
(parts_of_command
: Array[String])
450 if parts_of_command
[1].is_numeric
then
451 remove_breakpoint
(self.curr_file
, parts_of_command
[1].to_i
)
452 else if parts_of_command
.length
>= 3 and parts_of_command
[2].is_numeric
then
453 remove_breakpoint
(parts_of_command
[1], parts_of_command
[2].to_i
)
459 # Processes an array print command
460 fun process_array_command
(parts_of_command
: Array[String])
462 var index_of_first_brace
= parts_of_command
[1].index_of
('[')
463 var variable_name
= get_real_variable_name
(parts_of_command
[1].substring
(0,index_of_first_brace
))
464 var braces
= parts_of_command
[1].substring_from
(index_of_first_brace
)
466 var indexes
= remove_braces
(braces
)
468 var index_array
= new Array[Array[Int]]
470 if indexes
!= null then
471 for index
in indexes
do
472 var temp_indexes_array
= process_index
(index
)
473 if temp_indexes_array
!= null then
474 index_array
.push
(temp_indexes_array
)
475 #print index_array.last
480 var instance
= seek_variable
(variable_name
, frame
)
482 if instance
!= null then
483 print_nested_collection
(instance
, index_array
, 0, variable_name
, "")
485 print
"Cannot find variable {variable_name}"
489 # Processes the modification function to modify a variable dynamically
491 # Command of type variable = value
492 fun process_mod_function
(parts_of_command
: Array[String])
494 parts_of_command
[0] = get_real_variable_name
(parts_of_command
[0])
495 var parts_of_variable
= parts_of_command
[0].split_with
(".")
497 if parts_of_variable
.length
> 1 then
498 var last_part
= parts_of_variable
.pop
499 var first_part
= parts_of_command
[0].substring
(0,parts_of_command
[0].length
- last_part
.length
- 1)
500 var papa
= seek_variable
(first_part
, frame
)
502 if papa
!= null and papa
isa MutableInstance then
503 var attribute
= get_attribute_in_mutable_instance
(papa
, last_part
)
505 if attribute
!= null then
506 modify_argument_of_complex_type
(papa
, attribute
, parts_of_command
[2])
510 var target
= seek_variable
(parts_of_variable
[0], frame
)
511 if target
!= null then
512 modify_in_frame
(target
, parts_of_command
[2])
517 # Processes the untrace variable command
519 # Command pattern : "untrace variable"
520 fun process_untrace_command
(parts_of_command
: Array[String])
522 var variable_name
= get_real_variable_name
(parts_of_command
[1])
523 if untrace_variable
(variable_name
) then
524 print
"Untraced variable {parts_of_command[1]}"
526 print
"{parts_of_command[1]} is not traced"
530 # Processes the trace variable command
532 # Command pattern : "trace variable [break/print]"
533 fun process_trace_command
(parts_of_command
: Array[String])
535 var variable_name
= get_real_variable_name
(parts_of_command
[1])
538 if seek_variable
(variable_name
, frame
) == null then
539 print
"Cannot find a variable called {parts_of_command[1]}"
543 if parts_of_command
.length
== 3 then
544 if parts_of_command
[2] == "break" then
553 trace_variable
(variable_name
, breaker
)
555 print
"Successfully tracing {parts_of_command[1]}"
558 #######################################################################
559 ## Trace Management functions ##
560 #######################################################################
562 # Effectively untraces the variable called *variable_name*
564 # Returns true if the variable exists, false otherwise
565 private fun untrace_variable
(variable_name
: String): Bool
567 var to_remove
: nullable TraceObject = null
568 for i
in self.traces
do
569 if i
.is_variable_traced_in_frame
(variable_name
, frame
) then
574 if to_remove
!= null then
575 self.traces
.remove
(to_remove
)
582 # Effectively traces the variable *variable_name* either in print or break mode depending on the value of breaker (break if true, print if false)
584 private fun trace_variable
(variable_name
: String, breaker
: Bool)
586 for i
in self.traces
do
587 if i
.is_variable_traced_in_frame
(variable_name
, frame
) then
588 print
"This variable is already traced"
593 var trace_object
: TraceObject
596 trace_object
= new TraceObject(true)
598 trace_object
= new TraceObject(false)
601 # We trace the current variable found for the current frame
602 trace_object
.add_frame_variable
(self.frame
, variable_name
)
604 var position_of_variable_in_arguments
= get_position_of_variable_in_arguments
(frame
, variable_name
)
606 # Start parsing the frames starting with the parent of the current one, until the highest
607 # When the variable traced is declared locally, the loop stops
608 for i
in [1 .. frames
.length-1
] do
610 # If the variable was reported to be an argument of the previous frame
611 if position_of_variable_in_arguments
!= -1 then
613 var local_name
= get_identifiers_in_current_instruction
(get_function_arguments
(frames
[i
].current_node
.location
.text
))[position_of_variable_in_arguments
]
615 position_of_variable_in_arguments
= get_position_of_variable_in_arguments
(frames
[i
], local_name
)
617 trace_object
.add_frame_variable
(frames
[i
], local_name
)
623 self.traces
.add
(trace_object
)
626 # If the variable *variable_name* is an argument of the function being executed in the frame *frame*
627 # The function returns its position in the arguments
628 # Else, it returns -1
629 private fun get_position_of_variable_in_arguments
(frame
: Frame, variable_name
: String): Int
631 var identifiers
= get_identifiers_in_current_instruction
(get_function_arguments
(frame
.mpropdef
.location
.text
))
632 for i
in [0 .. identifiers
.length-1
] do
633 # If the current traced variable is an argument of the current function, we trace its parent (at least)
634 if identifiers
[i
] == variable_name
then return i
639 # Gets all the identifiers of an instruction (uses the rules of Nit as of Mar 05 2013)
641 fun get_identifiers_in_current_instruction
(instruction
: AbstractString): Array[String]
643 var result_array
= new Array[String]
644 var instruction_buffer
= new Buffer
646 var trigger_char_escape
= false
647 var trigger_string_escape
= false
648 var trigger_concat_in_string
= false
650 for i
in instruction
do
651 if trigger_char_escape
then
652 if i
== '\'' then trigger_char_escape = false
653 else if trigger_string_escape then
655 trigger_concat_in_string = true
656 trigger_string_escape = false
657 else if i == '\
"' then trigger_string_escape = false
659 if i.is_alphanumeric or i == '_' then
660 instruction_buffer.add(i)
661 else if i == '.' then
662 if instruction_buffer.is_numeric or (instruction_buffer[0] >= 'A' and instruction_buffer[0] <= 'Z') then
663 instruction_buffer.clear
665 result_array.push(instruction_buffer.to_s)
666 instruction_buffer.add(i)
668 else if i == '\'' then
669 trigger_char_escape = true
670 else if i == '\"' then
671 trigger_string_escape = true
672 else if i == '}' then
673 trigger_concat_in_string = false
674 trigger_string_escape = true
676 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)
677 instruction_buffer.clear
682 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)
687 # Takes a function call or declaration and strips all but the arguments
689 fun get_function_arguments(function: AbstractString): String
692 var trigger_copy = false
695 if i == ')' then break
696 if trigger_copy then buf.add(i)
697 if i == '(' then trigger_copy = true
703 #######################################################################
704 ## Alias management functions ##
705 #######################################################################
707 # Adds a new alias to the tables
708 fun add_alias(var_represented: String, alias: String)
710 self.aliases[alias] = var_represented
713 # Gets the real name of a variable hidden by an alias
714 fun get_variable_name_by_alias(alias: String): nullable String
716 if self.aliases.keys.has(alias) then
717 return self.aliases[alias]
723 # Gets the variable named by name, whether it is an alias or not
724 fun get_real_variable_name(name: String): String
726 var explode_string = name.split_with(".")
727 var final_string = new Buffer
728 for i in explode_string do
729 var alias_resolved = get_variable_name_by_alias(i)
730 if alias_resolved != null then
731 final_string.append(get_real_variable_name(alias_resolved))
733 final_string.append(i)
735 final_string.append(".")
738 return final_string.substring(0,final_string.length-1).to_s
741 #######################################################################
742 ## Print functions ##
743 #######################################################################
745 # Prints an object instance and its attributes if it has some
747 # If it is a primitive type, its value is directly printed
748 fun print_instance(instance: Instance)
750 print "Printing innards of a variable"
752 if instance isa MutableInstance then
753 var attributes = instance.attributes
754 print "Object : {instance}"
756 for current_attribute in attributes.keys do
757 print "Attribute : {current_attribute.to_s} \nValeur : {attributes[current_attribute].to_s}"
760 print "Found variable {instance}"
763 print "Stopping printing innards of a variable"
766 # Prints the attributes demanded in a SequenceRead
767 # Used recursively to print nested collections
768 fun print_nested_collection(instance: Instance, indexes: Array[Array[Int]], depth: Int, variable_name: String, depth_string: String)
770 var collection: nullable SequenceRead[Object] = null
771 var real_collection_length: nullable Int = null
773 if instance isa MutableInstance then
774 real_collection_length = get_collection_instance_real_length(instance)
775 collection = get_primary_collection(instance)
778 if collection != null and real_collection_length != null then
779 for i in indexes[depth] do
780 if i >= 0 and i < real_collection_length then
781 if depth == indexes.length-1 then
782 print "{variable_name}{depth_string}[{i}] = {collection[i]}"
784 var item_i = collection[i]
786 if item_i isa MutableInstance then
787 print_nested_collection(item_i, indexes, depth+1, variable_name, depth_string+"[{i}]")
789 print "The item at {variable_name}{depth_string}[{i}] is not a collection"
794 print "Out of bounds exception : i = {i} and collection_length = {real_collection_length.to_s}"
798 else if i >= real_collection_length then
804 if collection == null then
805 print "Cannot find {variable_name}{depth_string}"
806 else if real_collection_length != null then
807 print "Cannot find attribute length in {instance}"
809 print "Unknown error."
815 #######################################################################
816 ## Variable Exploration functions ##
817 #######################################################################
819 # Seeks a variable from the current frame called 'variable_path
', can introspect complex objects using function get_variable_in_mutable_instance
820 private fun seek_variable(variable_path: String, frame: Frame): nullable Instance
822 var full_variable = variable_path.split_with(".")
824 var full_variable_iterator = full_variable.iterator
826 var first_instance = get_variable_in_frame(full_variable_iterator.item, frame)
828 if first_instance == null then return null
830 if full_variable.length <= 1 then return first_instance
832 full_variable_iterator.next
834 if not (first_instance isa MutableInstance and full_variable_iterator.is_ok) then return null
836 return get_variable_in_mutable_instance(first_instance, full_variable_iterator)
839 # Gets a variable 'variable_name
' contained in the frame 'frame
'
840 private fun get_variable_in_frame(variable_name: String, frame: Frame): nullable Instance
842 if variable_name == "self" then
843 if frame.arguments.length >= 1 then return frame.arguments.first
846 var map_of_instances = frame.map
848 for key in map_of_instances.keys do
849 if key.to_s == variable_name then
850 return map_of_instances[key]
857 # Gets an attribute 'attribute_name
' contained in variable 'variable
'
858 fun get_attribute_in_mutable_instance(variable: MutableInstance, attribute_name: String): nullable MAttribute
860 var map_of_attributes = variable.attributes
862 for key in map_of_attributes.keys do
863 if key.to_s.substring_from(1) == attribute_name then
871 # Recursive function, returns the variable described by 'total_chain
'
872 fun get_variable_in_mutable_instance(variable: MutableInstance, iterator: Iterator[String]): nullable Instance
874 var attribute = get_attribute_in_mutable_instance(variable, iterator.item)
876 if attribute == null then return null
880 if iterator.is_ok then
881 var new_variable = variable.attributes[attribute]
882 if new_variable isa MutableInstance then
883 return get_variable_in_mutable_instance(new_variable, iterator)
888 return variable.attributes[attribute]
892 #######################################################################
893 ## Array exploring functions ##
894 #######################################################################
896 # Gets the length of a collection
897 # Used by the debugger, else if we call Collection.length, it returns the capacity instead
898 fun get_collection_instance_real_length(collection: MutableInstance): nullable Int
900 var collection_length_attribute = get_attribute_in_mutable_instance(collection, "length")
902 var real_collection_length: nullable Int = null
904 if collection_length_attribute != null then
905 var primitive_length_instance = collection.attributes[collection_length_attribute]
906 if primitive_length_instance isa PrimitiveInstance[Int] then
907 return primitive_length_instance.val
914 # Processes the indexes of a print array call
915 # Returns an array containing all the indexes demanded
916 fun process_index(index_string: String): nullable Array[Int]
918 var from_end_index = index_string.index_of('.')
919 var to_start_index = index_string.last_index_of('.')
921 if from_end_index != -1 and to_start_index != -1 then
922 var index_from_string = index_string.substring(0,from_end_index)
923 var index_to_string = index_string.substring_from(to_start_index+1)
925 if index_from_string.is_numeric and index_to_string.is_numeric then
926 var result_array = new Array[Int]
928 var index_from = index_from_string.to_i
929 var index_to = index_to_string.to_i
931 for i in [index_from..index_to] do
938 if index_string.is_numeric
940 var result_array = new Array[Int]
942 result_array.push(index_string.to_i)
953 # Gets a collection in a MutableInstance
954 fun get_primary_collection(container: MutableInstance): nullable SequenceRead[Object]
956 var items_of_array = get_attribute_in_mutable_instance(container, "items")
957 if items_of_array != null then
958 var array = container.attributes[items_of_array]
960 if array isa PrimitiveInstance[Object] then
961 var sequenceRead_final = array.val
962 if sequenceRead_final isa SequenceRead[Object] then
963 return sequenceRead_final
971 # Removes the braces '[' ']' in a print array command
972 # Returns an array containing their content
973 fun remove_braces(braces: String): nullable Array[String]
975 var buffer = new Buffer
977 var result_array = new Array[String]
979 var number_of_opening_brackets = 0
980 var number_of_closing_brackets = 0
982 var last_was_opening_bracket = false
986 if last_was_opening_bracket then
990 number_of_opening_brackets += 1
991 last_was_opening_bracket = true
992 else if i == ']' then
993 if not last_was_opening_bracket then
997 result_array.push(buffer.to_s)
999 number_of_closing_brackets += 1
1000 last_was_opening_bracket = false
1001 else if i.is_numeric or i == '.' then
1002 buffer.append(i.to_s)
1003 else if not i == ' ' then
1008 if number_of_opening_brackets != number_of_closing_brackets then
1015 #######################################################################
1016 ## Breakpoint placing functions ##
1017 #######################################################################
1019 # Places a breakpoint on line 'line_to_break
' for file 'file_to_break
'
1020 fun place_breakpoint(breakpoint: Breakpoint)
1022 if not self.breakpoints.keys.has(breakpoint.file) then
1023 self.breakpoints[breakpoint.file] = new HashSet[Breakpoint]
1025 if find_breakpoint(breakpoint.file, breakpoint.line) == null then
1026 self.breakpoints[breakpoint.file].add(breakpoint)
1027 print "Breakpoint added on line {breakpoint.line} for file {breakpoint.file}"
1029 print "Breakpoint already present on line {breakpoint.line} for file {breakpoint.file}"
1033 #Places a breakpoint that will trigger once and be destroyed afterwards
1034 fun process_place_tbreak_fun(parts_of_command: Array[String])
1036 var bp = get_breakpoint_from_command(parts_of_command)
1039 bp.set_max_breaks(1)
1040 place_breakpoint(bp)
1046 #######################################################################
1047 ## Breakpoint removing functions ##
1048 #######################################################################
1050 # Removes a breakpoint on line 'line_to_break
' for file 'file_to_break
'
1051 fun remove_breakpoint(file_to_break: String, line_to_break: Int)
1053 if self.breakpoints.keys.has(file_to_break) then
1054 var bp = find_breakpoint(file_to_break, line_to_break)
1057 self.breakpoints[file_to_break].remove(bp)
1058 print "Breakpoint removed on line {line_to_break} for file {file_to_break}"
1063 print "No breakpoint existing on line {line_to_break} for file {file_to_break}"
1066 #######################################################################
1067 ## Breakpoint searching functions ##
1068 #######################################################################
1070 # Finds a breakpoint for 'file
' and 'line
' in the class HashMap
1071 fun find_breakpoint(file: String, line: Int): nullable Breakpoint
1073 if self.breakpoints.keys.has(file)
1075 for i in self.breakpoints[file]
1087 #######################################################################
1088 ## Runtime modification functions ##
1089 #######################################################################
1091 # Modifies the value of a variable contained in a frame
1092 fun modify_in_frame(variable: Instance, value: String)
1094 var new_variable = get_variable_of_type_with_value(variable.mtype.to_s, value)
1095 if new_variable != null
1097 var keys = frame.map.keys
1100 if frame.map[key] == variable
1102 frame.map[key] = new_variable
1108 # Modifies the value of a variable contained in a MutableInstance
1109 fun modify_argument_of_complex_type(papa: MutableInstance, attribute: MAttribute, value: String)
1111 var final_variable = papa.attributes[attribute]
1112 var type_of_variable = final_variable.mtype.to_s
1113 var new_variable = get_variable_of_type_with_value(type_of_variable, value)
1114 if new_variable != null
1116 papa.attributes[attribute] = new_variable
1120 #######################################################################
1121 ## Variable generator functions ##
1122 #######################################################################
1124 # Returns a new variable of the type 'type_of_variable
' with a value of 'value
'
1125 fun get_variable_of_type_with_value(type_of_variable: String, value: String): nullable Instance
1127 if type_of_variable == "Int" then
1128 return get_int(value)
1129 else if type_of_variable == "Float" then
1130 return get_float(value)
1131 else if type_of_variable == "Bool" then
1132 return get_bool(value)
1133 else if type_of_variable == "Char" then
1134 return get_char(value)
1140 # Returns a new int instance with value 'value
'
1141 fun get_int(value: String): nullable Instance
1143 if value.is_numeric then
1144 return int_instance(value.to_i)
1150 # Returns a new float instance with value 'value
'
1151 fun get_float(value:String): nullable Instance
1153 if value.is_numeric then
1154 return float_instance(value.to_f)
1160 # Returns a new char instance with value 'value
'
1161 fun get_char(value: String): nullable Instance
1163 if value.length >= 1 then
1164 return char_instance(value[0])
1170 # Returns a new bool instance with value 'value
'
1171 fun get_bool(value: String): nullable Instance
1173 if value.to_lower == "true" then
1174 return self.true_instance
1175 else if value.to_lower == "false" then
1176 return self.false_instance
1178 print "Invalid value, a boolean must be set at \"true\" or \"false\""
1183 #######################################################################
1184 ## Command listing function ##
1185 #######################################################################
1187 # Lists the commands available when using the debugger
1190 print "\nCommand not recognized\n"
1191 print "Commands accepted : \n"
1192 print "[break/b] line : Adds a breakpoint on line *line_nb* of the current file\n"
1193 print "[break/b] file_name line_nb : Adds a breakpoint on line *line_nb* of file *file_name* \n"
1194 print "[p/print] variable : [p/print] * shows the status of all the variables\n"
1195 print "[p/print] variable[i] : Prints the value of the variable contained at position *i* in SequenceRead collection *variable*\n"
1196 print "[p/print] variable[i..j]: Prints the value of all the variables contained between positions *i* and *j* in SequenceRead collection *variable*\n"
1197 print "[p/print] stack: Prints a stack trace at current instruction\n"
1198 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"
1199 print "s : steps in on the current function\n"
1200 print "n : steps-over the current instruction\n"
1201 print "finish : steps out of the current function\n"
1202 print "variable as alias : Adds an alias called *alias* for the variable *variable*"
1203 print "An alias can reference another alias\n"
1204 print "variable = value : Sets the value of *variable* to *value*\n"
1205 print "[d/delete] line_nb : Removes a breakpoint on line *line_nb* of the current file \n"
1206 print "[d/delete] file_name line_nb : Removes a breakpoint on line *line_nb* of file *file_name* \n"
1207 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."
1208 print "untrace variable_name : Removes the trace on the variable you chose to trace earlier in the program"
1209 print "kill : kills the current program (Exits with an error and stack trace)\n"
1214 # Traces the modifications of an object linked to a certain frame
1215 private class TraceObject
1217 # Map of the local names bound to a frame
1218 var trace_map: HashMap[Frame, String]
1219 # Decides if breaking or printing statement when the variable is encountered
1220 var break_on_encounter: Bool
1222 init(break_on_encounter: Bool)
1224 trace_map = new HashMap[Frame, String]
1225 self.break_on_encounter = break_on_encounter
1228 # Adds the local alias for a variable and the frame bound to it
1229 fun add_frame_variable(frame: Frame, variable_name: String)
1231 self.trace_map[frame] = variable_name
1234 # Checks if the prompted variable is traced in the specified frame
1235 fun is_variable_traced_in_frame(variable_name: String, frame: Frame): Bool
1237 if self.trace_map.has_key(frame) then
1238 if self.trace_map[frame] == variable_name then
1250 # Breaks automatically when encountering an error
1251 # Permits the injunction of commands before crashing
1252 redef private fun fatal(v: NaiveInterpreter, message: String)
1254 if v isa Debugger then
1255 print "An error was encountered, the program will stop now."
1257 while v.process_debug_command(gets) do end