1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Johan Kayser <kayser.johan@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 sockets.
18 module debugger_socket
20 intrude import debugger
23 redef class ToolContext
25 var opt_socket_mode
= new OptionBool("Launches the target program with raw output on the network via sockets", "--socket")
28 var opt_websocket_mode
= new OptionBool("Launches the target program with output on the network via websockets", "--websocket")
31 var opt_debug_port
: OptionInt = new OptionInt("Sets the debug port (Defaults to 22125) - Must be contained between 0 and 65535", 22125, "--port")
36 self.option_context
.add_option
(self.opt_socket_mode
)
37 self.option_context
.add_option
(self.opt_websocket_mode
)
38 self.option_context
.add_option
(self.opt_debug_port
)
42 redef class ModelBuilder
43 # Execute the program from the entry point (Sys::main) of the `mainmodule`
44 # `arguments` are the command-line arguments in order
46 # 1. the AST is fully loaded.
47 # 2. the model is fully built.
48 # 3. the instructions are fully analysed.
49 redef fun run_debugger
(mainmodule
: MModule, arguments
: Array[String])
52 self.toolcontext
.info
("*** START INTERPRETING ***", 1)
54 var interpreter
= new Debugger(self, mainmodule
, arguments
)
58 init_naive_interpreter
(interpreter
, mainmodule
)
63 self.toolcontext
.info
("*** END INTERPRETING: {time1-time0} ***", 2)
66 redef fun run_debugger_autorun
(mainmodule
: MModule, arguments
: Array[String])
69 self.toolcontext
.info
("*** START INTERPRETING ***", 1)
71 var interpreter
= new Debugger(self, mainmodule
, arguments
)
72 interpreter
.autocontinue
= true
76 init_naive_interpreter
(interpreter
, mainmodule
)
81 self.toolcontext
.info
("*** END INTERPRETING: {time1-time0} ***", 2)
84 redef fun run_naive_interpreter
(mmod
, args
)
92 if self.toolcontext
.opt_socket_mode
.value
then
93 var sock
= new Socket.server
(toolcontext
.opt_debug_port
.value
, 1)
97 else if self.toolcontext
.opt_websocket_mode
.value
then
98 var websock
= new WebSocket(toolcontext
.opt_debug_port
.value
, 1)
100 sys
.set_io
(websock
,websock
,websock
)
106 if sys
.stdin
isa WebSocket or sys
.stdin
isa Socket then
115 private fun set_io
(istream
: PollableIStream, ostream
: OStream, errstream
: OStream)
118 self.stdout
= ostream
119 self.stderr
= ostream