Skip to content

Commit e326588

Browse files
knightburtonyichoi
authored andcommitted
Add restart command to the debugger (#2401)
With this feature the use can restart the actual debug session (similar to the multiple source context reset) within a client. JerryScript-DCO-1.0-Signed-off-by: Imre Kiss kissi.szeged@partner.samsung.com
1 parent 35ac0e0 commit e326588

File tree

9 files changed

+191
-80
lines changed

9 files changed

+191
-80
lines changed

jerry-core/debugger/debugger.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
JERRY_STATIC_ASSERT (JERRY_DEBUGGER_MESSAGES_OUT_MAX_COUNT == 27
3232
&& JERRY_DEBUGGER_MESSAGES_IN_MAX_COUNT == 19
33-
&& JERRY_DEBUGGER_VERSION == 3,
33+
&& JERRY_DEBUGGER_VERSION == 4,
3434
debugger_version_correlates_to_message_type_count);
3535

3636
/**

jerry-core/debugger/debugger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* JerryScript debugger protocol version.
2828
*/
29-
#define JERRY_DEBUGGER_VERSION (3)
29+
#define JERRY_DEBUGGER_VERSION (4)
3030

3131
/**
3232
* Frequency of calling jerry_debugger_receive() by the VM.

jerry-debugger/jerry-client-ws.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import time
2929

3030
# Expected debugger protocol version.
31-
JERRY_DEBUGGER_VERSION = 3
31+
JERRY_DEBUGGER_VERSION = 4
3232

3333
# Messages sent by the server to client.
3434
JERRY_DEBUGGER_CONFIGURATION = 1
@@ -513,6 +513,12 @@ def do_memstats(self, args):
513513

514514
do_ms = do_memstats
515515

516+
def do_restart(self, _):
517+
""" Restart the engine's debug session """
518+
self._send_string(JERRY_DEBUGGER_EVAL_ABORT + "\"r353t\"", JERRY_DEBUGGER_EVAL)
519+
520+
do_res = do_restart
521+
516522
def store_client_sources(self, args):
517523
self.client_sources = args
518524

jerry-main/main-unix.c

Lines changed: 124 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,25 @@ instance_alloc (size_t size,
412412

413413
#endif /* JERRY_ENABLE_EXTERNAL_CONTEXT */
414414

415+
/**
416+
* Inits the engine and the debugger
417+
*/
418+
static void
419+
init_engine (jerry_init_flag_t flags, /**< initialized flags for the engine */
420+
bool debug_server, /**< enable the debugger init or not */
421+
uint16_t debug_port) /**< the debugger port */
422+
{
423+
jerry_init (flags);
424+
if (debug_server)
425+
{
426+
jerry_debugger_init (debug_port);
427+
}
428+
429+
register_js_function ("assert", jerryx_handler_assert);
430+
register_js_function ("gc", jerryx_handler_gc);
431+
register_js_function ("print", jerryx_handler_print);
432+
} /* init_engine */
433+
415434
int
416435
main (int argc,
417436
char **argv)
@@ -591,15 +610,7 @@ main (int argc,
591610

592611
#endif /* JERRY_ENABLE_EXTERNAL_CONTEXT */
593612

594-
jerry_init (flags);
595-
if (start_debug_server)
596-
{
597-
jerry_debugger_init (debug_port);
598-
}
599-
600-
register_js_function ("assert", jerryx_handler_assert);
601-
register_js_function ("gc", jerryx_handler_gc);
602-
register_js_function ("print", jerryx_handler_print);
613+
init_engine (flags, start_debug_server, debug_port);
603614

604615
jerry_value_t ret_value = jerry_create_undefined ();
605616

@@ -629,99 +640,139 @@ main (int argc,
629640
}
630641
}
631642

632-
if (!jerry_value_is_error (ret_value))
643+
while (true)
633644
{
634-
for (int i = 0; i < files_counter; i++)
635-
{
636-
size_t source_size;
637-
const jerry_char_t *source_p = (jerry_char_t *) read_file (file_names[i], &source_size);
638645

639-
if (source_p == NULL)
646+
if (!jerry_value_is_error (ret_value))
647+
{
648+
for (int i = 0; i < files_counter; i++)
640649
{
641-
ret_value = jerry_create_error (JERRY_ERROR_COMMON, (jerry_char_t *) "Source file load error");
642-
break;
643-
}
650+
size_t source_size;
651+
const jerry_char_t *source_p = (jerry_char_t *) read_file (file_names[i], &source_size);
644652

645-
if (!jerry_is_valid_utf8_string (source_p, (jerry_size_t) source_size))
646-
{
647-
ret_value = jerry_create_error (JERRY_ERROR_COMMON, (jerry_char_t *) ("Input must be a valid UTF-8 string."));
648-
break;
649-
}
653+
if (source_p == NULL)
654+
{
655+
ret_value = jerry_create_error (JERRY_ERROR_COMMON, (jerry_char_t *) "Source file load error");
656+
break;
657+
}
650658

651-
ret_value = jerry_parse ((jerry_char_t *) file_names[i],
652-
strlen (file_names[i]),
653-
source_p,
654-
source_size,
655-
JERRY_PARSE_NO_OPTS);
659+
if (!jerry_is_valid_utf8_string (source_p, (jerry_size_t) source_size))
660+
{
661+
ret_value = jerry_create_error (JERRY_ERROR_COMMON, (jerry_char_t *) ("Input must be a valid UTF-8 string."));
662+
break;
663+
}
656664

657-
if (!jerry_value_is_error (ret_value) && !is_parse_only)
658-
{
659-
jerry_value_t func_val = ret_value;
660-
ret_value = jerry_run (func_val);
661-
jerry_release_value (func_val);
662-
}
665+
ret_value = jerry_parse ((jerry_char_t *) file_names[i],
666+
strlen (file_names[i]),
667+
source_p,
668+
source_size,
669+
JERRY_PARSE_NO_OPTS);
663670

664-
if (jerry_value_is_error (ret_value))
665-
{
666-
break;
667-
}
671+
if (!jerry_value_is_error (ret_value) && !is_parse_only)
672+
{
673+
jerry_value_t func_val = ret_value;
674+
ret_value = jerry_run (func_val);
675+
jerry_release_value (func_val);
676+
}
668677

669-
jerry_release_value (ret_value);
670-
ret_value = jerry_create_undefined ();
671-
}
672-
}
678+
if (jerry_value_is_error (ret_value))
679+
{
680+
break;
681+
}
673682

674-
if (is_wait_mode)
675-
{
676-
is_repl_mode = false;
683+
jerry_release_value (ret_value);
684+
ret_value = jerry_create_undefined ();
685+
}
686+
}
677687

678-
if (jerry_is_feature_enabled (JERRY_FEATURE_DEBUGGER))
688+
if (is_wait_mode)
679689
{
680-
while (true)
681-
{
682-
jerry_debugger_wait_for_source_status_t receive_status;
690+
is_repl_mode = false;
683691

684-
do
692+
if (jerry_is_feature_enabled (JERRY_FEATURE_DEBUGGER))
693+
{
694+
while (true)
685695
{
686-
jerry_value_t run_result;
687-
688-
receive_status = jerry_debugger_wait_for_client_source (wait_for_source_callback,
689-
NULL,
690-
&run_result);
696+
jerry_debugger_wait_for_source_status_t receive_status;
691697

692-
if (receive_status == JERRY_DEBUGGER_SOURCE_RECEIVE_FAILED)
698+
do
693699
{
694-
ret_value = jerry_create_error (JERRY_ERROR_COMMON,
695-
(jerry_char_t *) "Connection aborted before source arrived.");
700+
jerry_value_t run_result;
701+
702+
receive_status = jerry_debugger_wait_for_client_source (wait_for_source_callback,
703+
NULL,
704+
&run_result);
705+
706+
if (receive_status == JERRY_DEBUGGER_SOURCE_RECEIVE_FAILED)
707+
{
708+
ret_value = jerry_create_error (JERRY_ERROR_COMMON,
709+
(jerry_char_t *) "Connection aborted before source arrived.");
710+
}
711+
712+
if (receive_status == JERRY_DEBUGGER_SOURCE_END)
713+
{
714+
jerry_port_log (JERRY_LOG_LEVEL_DEBUG, "No more client source.\n");
715+
}
716+
717+
if (jerry_value_is_abort (run_result))
718+
{
719+
ret_value = jerry_acquire_value (run_result);
720+
}
721+
722+
jerry_release_value (run_result);
696723
}
724+
while (receive_status == JERRY_DEBUGGER_SOURCE_RECEIVED);
697725

698-
if (receive_status == JERRY_DEBUGGER_SOURCE_END)
726+
if (receive_status != JERRY_DEBUGGER_CONTEXT_RESET_RECEIVED)
699727
{
700-
jerry_port_log (JERRY_LOG_LEVEL_DEBUG, "No more client source.\n");
728+
break;
701729
}
702730

703-
jerry_release_value (run_result);
704-
}
705-
while (receive_status == JERRY_DEBUGGER_SOURCE_RECEIVED);
731+
init_engine (flags, true, debug_port);
706732

707-
if (receive_status != JERRY_DEBUGGER_CONTEXT_RESET_RECEIVED)
708-
{
709-
break;
733+
ret_value = jerry_create_undefined ();
710734
}
735+
}
711736

712-
jerry_cleanup ();
737+
}
713738

714-
jerry_init (flags);
715-
jerry_debugger_init (debug_port);
739+
bool restart = false;
716740

717-
register_js_function ("assert", jerryx_handler_assert);
718-
register_js_function ("gc", jerryx_handler_gc);
719-
register_js_function ("print", jerryx_handler_print);
741+
if (jerry_is_feature_enabled (JERRY_FEATURE_DEBUGGER) && jerry_value_is_abort (ret_value))
742+
{
743+
jerry_value_t abort_value = jerry_get_value_from_error (ret_value, false);
744+
if (jerry_value_is_string (abort_value))
745+
{
746+
jerry_char_t str_buf[5];
747+
jerry_value_t str_val = jerry_value_to_string (abort_value);
748+
jerry_size_t str_size = jerry_get_string_size (str_val);
720749

721-
ret_value = jerry_create_undefined ();
750+
if (str_size == 5)
751+
{
752+
jerry_string_to_char_buffer (str_val, str_buf, str_size);
753+
if (memcmp ("r353t", (char *) (str_buf), 5) == 0)
754+
{
755+
jerry_release_value (ret_value);
756+
restart = true;
757+
}
758+
}
759+
760+
jerry_release_value (str_val);
722761
}
762+
763+
jerry_release_value (abort_value);
723764
}
724765

766+
if (!restart)
767+
{
768+
break;
769+
}
770+
771+
jerry_cleanup ();
772+
773+
init_engine (flags, true, debug_port);
774+
775+
ret_value = jerry_create_undefined ();
725776
}
726777

727778
if (is_repl_mode)

tests/debugger/do_help.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ Stopped at tests/debugger/do_help.js:15
44

55
Documented commands (type help <topic>):
66
========================================
7-
abort bt display exception list next source
8-
b c dump f memstats quit src
9-
backtrace continue e finish ms s step
10-
break delete eval help n scroll throw
7+
abort bt display exception list next s step
8+
b c dump f memstats quit scroll throw
9+
backtrace continue e finish ms res source
10+
break delete eval help n restart src
1111

1212
(jerry-debugger) quit

tests/debugger/do_restart.cmd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
n
2+
n
3+
n
4+
restart

tests/debugger/do_restart.expected

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Connecting to: localhost:5001
2+
Stopped at tests/debugger/do_restart.js:23
3+
(jerry-debugger) n
4+
Stopped at tests/debugger/do_restart.js:24
5+
(jerry-debugger) n
6+
out: foo
7+
Stopped at tests/debugger/do_restart.js:25
8+
(jerry-debugger) n
9+
out: bar
10+
Stopped at tests/debugger/do_restart.js:24
11+
(jerry-debugger) restart
12+
Connecting to: localhost:5001
13+
Stopped at tests/debugger/do_restart.js:23
14+
(jerry-debugger) continue
15+
out: foo
16+
out: bar
17+
out: foo
18+
out: bar

tests/debugger/do_restart.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 foo() {
16+
print("foo");
17+
}
18+
19+
function bar() {
20+
print("bar");
21+
}
22+
23+
for (var i = 0; i < 2; i++) {
24+
foo();
25+
bar();
26+
}

tools/runners/run-debugger-test.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ sleep 1s
3737
RESULT_TEMP=`mktemp ${TEST_CASE}.out.XXXXXXXXXX`
3838

3939
(cat "${TEST_CASE}.cmd" | ${DEBUGGER_CLIENT} --non-interactive ${CLIENT_ARGS}) &> ${RESULT_TEMP}
40+
41+
if [[ $TEST_CASE == *"restart"* ]]; then
42+
CONTINUE_CASE=$(sed "s/restart/continue/g" <<< "$TEST_CASE")
43+
(cat "${CONTINUE_CASE}.cmd" | ${DEBUGGER_CLIENT} --non-interactive ${CLIENT_ARGS}) &>> ${RESULT_TEMP}
44+
fi
45+
4046
diff -U0 ${TEST_CASE}.expected ${RESULT_TEMP}
4147
STATUS_CODE=$?
4248

0 commit comments

Comments
 (0)