Skip to content

Commit 61677d6

Browse files
committed
Splitting the debugger and console part of the python debugger
Move DebuggerPrompt to jerry_client.py Implement JerryDebugger functions in the jerry_client_ws.py file Server response is displayed by jerry_client.py JerryScript-DCO-1.0-Signed-off-by: Tamas Zakor [email protected]
1 parent 5f4a220 commit 61677d6

File tree

3 files changed

+809
-663
lines changed

3 files changed

+809
-663
lines changed

jerry-debugger/jerry_client.py

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright JS Foundation and other contributors, http://js.foundation
4+
#
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
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
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.
16+
17+
from __future__ import print_function
18+
from cmd import Cmd
19+
from pprint import pprint
20+
import math
21+
import socket
22+
import sys
23+
import logging
24+
import time
25+
import jerry_client_ws
26+
27+
class DebuggerPrompt(Cmd):
28+
# pylint: disable=too-many-instance-attributes,too-many-arguments
29+
def __init__(self, debugger):
30+
Cmd.__init__(self)
31+
self.debugger = debugger
32+
self.stop = False
33+
self.quit = False
34+
self.backtrace = True
35+
self.debugger.non_interactive = False
36+
37+
def precmd(self, line):
38+
self.stop = False
39+
if self.debugger.non_interactive:
40+
print("%s" % line)
41+
return line
42+
43+
def postcmd(self, stop, line):
44+
return self.stop
45+
46+
def do_quit(self, _):
47+
""" Exit JerryScript debugger """
48+
self.debugger.quit()
49+
self.quit = True
50+
self.stop = True
51+
52+
def do_display(self, args):
53+
""" Toggle source code display after breakpoints """
54+
if args:
55+
line_num = src_check_args(args)
56+
if line_num >= 0:
57+
self.debugger.display = line_num
58+
else:
59+
print("Non-negative integer number expected, 0 turns off this function")
60+
61+
def do_break(self, args):
62+
""" Insert breakpoints on the given lines or functions """
63+
result = ""
64+
result = self.debugger.set_break(args)
65+
if self.debugger.not_empty(result):
66+
print(result.get_data())
67+
do_b = do_break
68+
69+
def do_list(self, _):
70+
""" Lists the available breakpoints """
71+
result = self.debugger.show_breakpoint_list()
72+
print(result.get_data())
73+
74+
def do_delete(self, args):
75+
""" Delete the given breakpoint, use 'delete all|active|pending' to clear all the given breakpoints """
76+
result = self.debugger.delete(args)
77+
if self.debugger.not_empty(result):
78+
print(result.get_data())
79+
80+
def do_next(self, args):
81+
""" Next breakpoint in the same context """
82+
self.stop = True
83+
if self.debugger.check_empty_data(args):
84+
args = 0
85+
self.debugger.next()
86+
else:
87+
try:
88+
args = int(args)
89+
if args <= 0:
90+
raise ValueError(args)
91+
else:
92+
while int(args) != 0:
93+
self.debugger.next()
94+
time.sleep(0.25)
95+
result = self.debugger.mainloop()
96+
if result[-1:] == '\n':
97+
result = result[:-1]
98+
if result:
99+
print(result.get_data())
100+
self.debugger.smessage = ''
101+
if self.debugger.display > 0:
102+
print(self.debugger.print_source(self.debugger.display,
103+
self.debugger.src_offset).get_data())
104+
args = int(args) - 1
105+
self.cmdloop()
106+
except ValueError as val_errno:
107+
print("Error: expected a positive integer: %s" % val_errno)
108+
self.cmdloop()
109+
do_n = do_next
110+
111+
def do_step(self, _):
112+
""" Next breakpoint, step into functions """
113+
self.debugger.step()
114+
self.stop = True
115+
do_s = do_step
116+
117+
def do_backtrace(self, args):
118+
""" Get backtrace data from debugger """
119+
result = self.debugger.backtrace(args)
120+
if self.debugger.not_empty(result):
121+
print(result.get_data())
122+
self.stop = True
123+
self.cmdloop()
124+
else:
125+
self.stop = True
126+
self.backtrace = True
127+
do_bt = do_backtrace
128+
129+
def do_src(self, args):
130+
""" Get current source code """
131+
if args:
132+
line_num = src_check_args(args)
133+
if line_num >= 0:
134+
print(self.debugger.print_source(line_num, 0).get_data())
135+
do_source = do_src
136+
137+
def do_scroll(self, _):
138+
""" Scroll the source up or down """
139+
while True:
140+
key = sys.stdin.readline()
141+
if key == 'w\n':
142+
_scroll_direction(self.debugger, "up")
143+
elif key == 's\n':
144+
_scroll_direction(self.debugger, "down")
145+
elif key == 'q\n':
146+
break
147+
else:
148+
print("Invalid key")
149+
150+
def do_continue(self, _):
151+
""" Continue execution """
152+
self.debugger.get_continue()
153+
self.stop = True
154+
if self.debugger.check_empty_data(self.debugger.non_interactive):
155+
print("Press enter to stop JavaScript execution.")
156+
do_c = do_continue
157+
158+
def do_finish(self, _):
159+
""" Continue running until the current function returns """
160+
self.debugger.finish()
161+
self.stop = True
162+
do_f = do_finish
163+
164+
def do_dump(self, args):
165+
""" Dump all of the debugger data """
166+
if args:
167+
print("Error: No argument expected")
168+
else:
169+
pprint(self.debugger.function_list)
170+
171+
def do_eval(self, args):
172+
""" Evaluate JavaScript source code """
173+
self.debugger.eval(args)
174+
self.stop = True
175+
do_e = do_eval
176+
177+
def do_memstats(self, _):
178+
""" Memory statistics """
179+
self.debugger.memstats()
180+
self.stop = True
181+
do_ms = do_memstats
182+
183+
def do_abort(self, args):
184+
""" Throw an exception """
185+
self.debugger.abort(args)
186+
self.stop = True
187+
188+
def do_restart(self, _):
189+
""" Restart the engine's debug session """
190+
self.debugger.restart()
191+
self.stop = True
192+
do_res = do_restart
193+
194+
def do_throw(self, args):
195+
""" Throw an exception """
196+
self.debugger.throw(args)
197+
self.stop = True
198+
199+
def do_exception(self, args):
200+
""" Config the exception handler module """
201+
result = self.debugger.exception(args)
202+
print(result.get_data())
203+
204+
def _scroll_direction(debugger, direction):
205+
""" Helper function for do_scroll """
206+
debugger.src_offset_diff = int(max(math.floor(debugger.display / 3), 1))
207+
if direction == "up":
208+
debugger.src_offset -= debugger.src_offset_diff
209+
else:
210+
debugger.src_offset += debugger.src_offset_diff
211+
print(debugger.print_source(debugger.display, debugger.src_offset)['value'])
212+
213+
def src_check_args(args):
214+
try:
215+
line_num = int(args)
216+
if line_num < 0:
217+
print("Error: Non-negative integer number expected")
218+
return -1
219+
220+
return line_num
221+
except ValueError as val_errno:
222+
print("Error: Non-negative integer number expected: %s" % (val_errno))
223+
return -1
224+
225+
# pylint: disable=too-many-branches,too-many-locals,too-many-statements
226+
def main():
227+
args = jerry_client_ws.arguments_parse()
228+
229+
debugger = jerry_client_ws.JerryDebugger(args.address)
230+
231+
logging.debug("Connected to JerryScript on %d port", debugger.port)
232+
233+
prompt = DebuggerPrompt(debugger)
234+
prompt.prompt = "(jerry-debugger) "
235+
prompt.debugger.non_interactive = args.non_interactive
236+
237+
if args.color:
238+
debugger.set_colors()
239+
240+
if args.display:
241+
prompt.debugger.display = args.display
242+
prompt.do_display(args.display)
243+
else:
244+
prompt.stop = False
245+
if prompt.debugger.check_empty_data(args.client_source):
246+
prompt.debugger.mainloop()
247+
result = prompt.debugger.smessage
248+
print(result)
249+
prompt.debugger.smessage = ''
250+
prompt.cmdloop()
251+
252+
if prompt.debugger.not_empty(args.exception):
253+
prompt.do_exception(str(args.exception))
254+
255+
if args.client_source:
256+
prompt.debugger.store_client_sources(args.client_source)
257+
258+
while True:
259+
if prompt.quit:
260+
break
261+
262+
result = prompt.debugger.mainloop().get_data()
263+
264+
if prompt.debugger.check_empty_data(result) and prompt.backtrace is False:
265+
break
266+
267+
if prompt.debugger.wait_data(result):
268+
continue
269+
270+
elif jerry_client_ws.JERRY_DEBUGGER_DATA_END in result:
271+
result = result.replace(jerry_client_ws.JERRY_DEBUGGER_DATA_END, '')
272+
if result.endswith('\n'):
273+
result = result.rstrip()
274+
if result:
275+
print(result)
276+
prompt.debugger.smessage = ''
277+
if prompt.debugger.display > 0:
278+
print(prompt.debugger.print_source(prompt.debugger.display, prompt.debugger.src_offset).get_data())
279+
prompt.backtrace = False
280+
break
281+
else:
282+
if result.endswith('\n'):
283+
result = result.rstrip()
284+
if result:
285+
print(result)
286+
prompt.debugger.smessage = ''
287+
if prompt.debugger.display > 0:
288+
print(prompt.debugger.print_source(prompt.debugger.display, prompt.debugger.src_offset).get_data())
289+
prompt.cmdloop()
290+
continue
291+
292+
if __name__ == "__main__":
293+
try:
294+
main()
295+
except socket.error as error_msg:
296+
ERRNO = error_msg.errno
297+
MSG = str(error_msg)
298+
if ERRNO == 111:
299+
sys.exit("Failed to connect to the JerryScript debugger.")
300+
elif ERRNO == 32 or ERRNO == 104:
301+
sys.exit("Connection closed.")
302+
else:
303+
sys.exit("Failed to connect to the JerryScript debugger.\nError: %s" % (MSG))

0 commit comments

Comments
 (0)