Python debugger source & display command (#1850)

Reworked source(src) command to display a given range of the code, defaults to 3.

Added display command which does the same as the source, except it displays the source code everytime a breakpoint is hit.

Made tests for display, extended tests for src.

JerryScript-DCO-1.0-Signed-off-by: Daniel Balla dballa@inf.u-szeged.hu
This commit is contained in:
Daniel Balla 2017-05-30 10:58:34 +02:00 committed by Zoltan Herczeg
parent ae60ff0aa3
commit 712d5ca8b7
7 changed files with 160 additions and 15 deletions

View File

@ -117,7 +117,7 @@ class JerryFunction(object):
def __init__(self, is_func, byte_code_cp, source, source_name, line, column, name, lines, offsets):
self.is_func = is_func
self.byte_code_cp = byte_code_cp
self.source = source
self.source = re.split("\r\n|[\r\n]", source)
self.source_name = source_name
self.name = name
self.lines = {}
@ -312,11 +312,29 @@ class DebuggerPrompt(Cmd):
def do_src(self, args):
""" Get current source code """
if self.disable_args(args):
return
if args:
line_num = src_check_args(args)
if line_num >= 0:
print_source(self.debugger, line_num)
elif line_num == 0:
print_source(self.debugger, self.debugger.default_viewrange)
else:
return
if self.debugger.last_breakpoint_hit:
print(self.debugger.last_breakpoint_hit.function.source)
do_source = do_src
def do_display(self, args):
""" Toggle source code display after breakpoints """
if args:
line_num = src_check_args(args)
if line_num >= 0:
self.debugger.display = line_num
else:
return
else:
print("Non-negative integer number expected, 0 turns off this function")
return
def do_dump(self, args):
""" Dump all of the debugger data """
@ -395,7 +413,7 @@ class DebuggerPrompt(Cmd):
def do_memstats(self, args):
""" Memory statistics """
self.exec_command(args, JERRY_DEBUGGER_MEMSTATS);
self.exec_command(args, JERRY_DEBUGGER_MEMSTATS)
return
do_ms = do_memstats
@ -448,6 +466,8 @@ class JerryDebugger(object):
self.active_breakpoint_list = {}
self.pending_breakpoint_list = []
self.line_list = Multimap()
self.display = 0
self.default_viewrange = 3
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client_socket.connect((self.host, self.port))
@ -729,6 +749,42 @@ def parse_source(debugger, data):
logging.debug("No pending breakpoints")
def src_check_args(args):
try:
line_num = int(args)
if line_num < 0:
print("Error: Non-negative integer number expected")
return -1
return line_num
except ValueError as val_errno:
print("Error: Non-negative integer number expected: %s" % (val_errno))
return -1
def print_source(debugger, line_num):
last_bp = debugger.last_breakpoint_hit
if not last_bp:
return
lines = last_bp.function.source
if last_bp.function.source_name:
print("Source: %s" % (last_bp.function.source_name))
if line_num == 0:
start = 0
end = len(last_bp.function.source) - 1
else:
start = max(last_bp.line - line_num, 0)
end = min(last_bp.line + line_num-1, len(last_bp.function.source)-1)
for i in range(start, end):
if i == last_bp.line - 1:
print("%4d > %s" % (i + 1, lines[i]))
else:
print("%4d %s" % (i + 1, lines[i]))
def release_function(debugger, data):
byte_code_cp = struct.unpack(debugger.byte_order + debugger.cp_format,
data[3: 3 + debugger.cp_size])[0]
@ -881,6 +937,8 @@ def main():
breakpoint_info += " breakpoint:%d" % (breakpoint[0].active_index)
print("Stopped %s %s" % (breakpoint_info, breakpoint[0]))
if debugger.display:
print_source(prompt.debugger, debugger.display)
prompt.cmdloop()
if prompt.quit:

View File

@ -0,0 +1,13 @@
b a
b b
b c
b d
display
c
display 2
c
display 5435
c
display 0
c
c

View File

@ -0,0 +1,53 @@
Connecting to: localhost:5001
Stopped at tests/debugger/do_display.js:20
(jerry-debugger) b a
Breakpoint 1 at tests/debugger/do_display.js:15 (in a() at line:15, col:1)
(jerry-debugger) b b
Breakpoint 2 at tests/debugger/do_display.js:16 (in b() at line:16, col:1)
(jerry-debugger) b c
Breakpoint 3 at tests/debugger/do_display.js:17 (in c() at line:17, col:1)
(jerry-debugger) b d
Breakpoint 4 at tests/debugger/do_display.js:18 (in d() at line:18, col:1)
(jerry-debugger) display
Non-negative integer number expected, 0 turns off this function
(jerry-debugger) c
Stopped at breakpoint:1 tests/debugger/do_display.js:15 (in a() at line:15, col:1)
(jerry-debugger) display 2
(jerry-debugger) c
Stopped at breakpoint:2 tests/debugger/do_display.js:16 (in b() at line:16, col:1)
Source: tests/debugger/do_display.js
15 function a() { print("hi"); }
16 > function b() { print("welcome"); }
17 function c() { print("hello"); }
(jerry-debugger) display 5435
(jerry-debugger) c
Stopped at breakpoint:3 tests/debugger/do_display.js:17 (in c() at line:17, col:1)
Source: tests/debugger/do_display.js
1 // Copyright JS Foundation and other contributors, http://js.foundation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 function a() { print("hi"); }
16 function b() { print("welcome"); }
17 > function c() { print("hello"); }
18 function d() { print("goodbye"); }
19
20 a();
21 b();
22 c();
23 d();
(jerry-debugger) display 0
(jerry-debugger) c
Stopped at breakpoint:4 tests/debugger/do_display.js:18 (in d() at line:18, col:1)
(jerry-debugger) c
Connection closed.

View File

@ -0,0 +1,23 @@
// Copyright JS Foundation and other contributors, http://js.foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
function a() { print("hi"); }
function b() { print("welcome"); }
function c() { print("hello"); }
function d() { print("goodbye"); }
a();
b();
c();
d();

View File

@ -4,8 +4,9 @@ Stopped at tests/debugger/do_help.js:15
Documented commands (type help <topic>):
========================================
b bt delete eval help ms pendingdel src
backtrace c dump exception list n quit step
break continue e fbreak memstats next s
b c dump fbreak ms quit step
backtrace continue e help n s
break delete eval list next source
bt display exception memstats pendingdel src
(jerry-debugger) quit

View File

@ -2,7 +2,7 @@ b f
n
next
s
src
source 0
n
step
src

View File

@ -8,15 +8,12 @@ Stopped at breakpoint:1 tests/debugger/do_src.js:16 (in f() at line:15, col:1)
Stopped at tests/debugger/do_src.js:20
(jerry-debugger) s
Stopped at <unknown>:1
(jerry-debugger) src
f = function f() {
print('F2') }
(jerry-debugger) source 0
1 > f = function f() {
(jerry-debugger) n
Stopped at tests/debugger/do_src.js:21
(jerry-debugger) step
Stopped at <unknown>:2 (in f() at line:1, col:5)
(jerry-debugger) src
f = function f() {
print('F2') }
(jerry-debugger) c
Connection closed.