diff --git a/jerry-debugger/jerry-client-ws.py b/jerry-debugger/jerry-client-ws.py index 6fa5ddb93..5a98ae664 100755 --- a/jerry-debugger/jerry-client-ws.py +++ b/jerry-debugger/jerry-client-ws.py @@ -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: diff --git a/tests/debugger/do_display.cmd b/tests/debugger/do_display.cmd new file mode 100644 index 000000000..9b85eafaa --- /dev/null +++ b/tests/debugger/do_display.cmd @@ -0,0 +1,13 @@ +b a +b b +b c +b d +display +c +display 2 +c +display 5435 +c +display 0 +c +c diff --git a/tests/debugger/do_display.expected b/tests/debugger/do_display.expected new file mode 100644 index 000000000..2fb9d2de9 --- /dev/null +++ b/tests/debugger/do_display.expected @@ -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. diff --git a/tests/debugger/do_display.js b/tests/debugger/do_display.js new file mode 100644 index 000000000..b57be6d26 --- /dev/null +++ b/tests/debugger/do_display.js @@ -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(); diff --git a/tests/debugger/do_help.expected b/tests/debugger/do_help.expected index 5ac4677ec..b8cf6c302 100644 --- a/tests/debugger/do_help.expected +++ b/tests/debugger/do_help.expected @@ -4,8 +4,9 @@ Stopped at tests/debugger/do_help.js:15 Documented commands (type help ): ======================================== -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 diff --git a/tests/debugger/do_src.cmd b/tests/debugger/do_src.cmd index ee1a60f28..38ab83e49 100644 --- a/tests/debugger/do_src.cmd +++ b/tests/debugger/do_src.cmd @@ -2,7 +2,7 @@ b f n next s -src +source 0 n step src diff --git a/tests/debugger/do_src.expected b/tests/debugger/do_src.expected index 8233ca492..7e7a417f5 100644 --- a/tests/debugger/do_src.expected +++ b/tests/debugger/do_src.expected @@ -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 :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 :2 (in f() at line:1, col:5) (jerry-debugger) src -f = function f() { -print('F2') } (jerry-debugger) c Connection closed.