Initial version of JerryScript debugger (#1557)
The debugger supports setting breakpoints, execution control (step, next, continue) and getting backtrace. The communication is WebSocket-based, so a browser can communicate with JerryScript without any intermediate application. JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com JerryScript-DCO-1.0-Signed-off-by: Levente Orban orbanl@inf.u-szeged.hu
This commit is contained in:
committed by
Tilmann Scheller
parent
453066fcf1
commit
025a99ccbb
@@ -16,6 +16,8 @@
|
||||
#ifndef BYTE_CODE_H
|
||||
#define BYTE_CODE_H
|
||||
|
||||
#include "ecma-globals.h"
|
||||
|
||||
/** \addtogroup parser Parser
|
||||
* @{
|
||||
*
|
||||
@@ -204,6 +206,20 @@
|
||||
/* PARSER_TRY_CONTEXT_STACK_ALLOCATION must be <= 3 */
|
||||
#define PARSER_TRY_CONTEXT_STACK_ALLOCATION 2
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
|
||||
#define CBC_BREAKPOINT_OPCODES \
|
||||
CBC_OPCODE (CBC_BREAKPOINT_ENABLED, CBC_NO_FLAG, 0, \
|
||||
VM_OC_BREAKPOINT_ENABLED) \
|
||||
CBC_OPCODE (CBC_BREAKPOINT_DISABLED, CBC_NO_FLAG, 0, \
|
||||
VM_OC_BREAKPOINT_DISABLED) \
|
||||
|
||||
#else /* !JERRY_DEBUGGER */
|
||||
|
||||
#define CBC_BREAKPOINT_OPCODES
|
||||
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
/**
|
||||
* Opcode definitions.
|
||||
*/
|
||||
@@ -315,6 +331,7 @@
|
||||
VM_OC_RET) \
|
||||
CBC_OPCODE (CBC_RETURN_WITH_LITERAL, CBC_HAS_LITERAL_ARG, 0, \
|
||||
VM_OC_RET | VM_OC_GET_LITERAL) \
|
||||
CBC_BREAKPOINT_OPCODES \
|
||||
\
|
||||
/* Unary opcodes. */ \
|
||||
CBC_UNARY_OPERATION (CBC_PLUS, \
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "common.h"
|
||||
|
||||
#include "byte-code.h"
|
||||
#include "jerry-debugger.h"
|
||||
#include "js-parser.h"
|
||||
#include "js-parser-limits.h"
|
||||
#include "js-lexer.h"
|
||||
@@ -190,6 +191,16 @@ typedef struct parser_branch_node_t
|
||||
parser_branch_t branch; /**< branch */
|
||||
} parser_branch_node_t;
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
/**
|
||||
* Extra information for each breakpoint.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint32_t value; /**< line or offset of the breakpoint */
|
||||
} parser_breakpoint_info_t;
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
/**
|
||||
* Those members of a context which needs
|
||||
* to be saved when a sub-function is parsed.
|
||||
@@ -270,6 +281,12 @@ typedef struct
|
||||
int is_show_opcodes; /**< show opcodes */
|
||||
uint32_t total_byte_code_size; /**< total byte code size */
|
||||
#endif /* PARSER_DUMP_BYTE_CODE */
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
parser_breakpoint_info_t breakpoint_info[JERRY_DEBUGGER_MAX_SIZE (parser_list_t)]; /**< extra data for breakpoints */
|
||||
uint16_t breakpoint_info_count; /**< current breakpoint index */
|
||||
parser_line_counter_t last_breakpoint_line; /**< last line where breakpoint was inserted */
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
} parser_context_t;
|
||||
|
||||
/**
|
||||
@@ -428,6 +445,15 @@ ecma_compiled_code_t *parser_parse_function (parser_context_t *context_p, uint32
|
||||
|
||||
void parser_raise_error (parser_context_t *context_p, parser_error_t error);
|
||||
|
||||
/* Debug functions. */
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
|
||||
void parser_append_breakpoint_info (parser_context_t *context_p, jerry_debugger_header_type_t type, uint32_t value);
|
||||
void parser_send_breakpoints (parser_context_t *context_p, jerry_debugger_header_type_t type);
|
||||
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
|
||||
@@ -17,6 +17,10 @@
|
||||
|
||||
#ifdef JERRY_JS_PARSER
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
#include "jcontext.h"
|
||||
#endif /*JERRY_DEBUGGER */
|
||||
|
||||
/** \addtogroup parser Parser
|
||||
* @{
|
||||
*
|
||||
@@ -310,6 +314,10 @@ parser_parse_var_statement (parser_context_t *context_p) /**< context */
|
||||
JERRY_ASSERT (context_p->token.type == LEXER_LITERAL
|
||||
&& context_p->token.lit_location.type == LEXER_IDENT_LITERAL);
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
parser_line_counter_t ident_line_counter = context_p->line;
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
context_p->lit_object.literal_p->status_flags |= LEXER_FLAG_VAR;
|
||||
|
||||
parser_emit_cbc_literal_from_token (context_p, CBC_PUSH_LITERAL);
|
||||
@@ -318,6 +326,30 @@ parser_parse_var_statement (parser_context_t *context_p) /**< context */
|
||||
|
||||
if (context_p->token.type == LEXER_ASSIGN)
|
||||
{
|
||||
#ifdef JERRY_DEBUGGER
|
||||
if (JERRY_CONTEXT (jerry_init_flags) & JERRY_INIT_DEBUGGER)
|
||||
{
|
||||
if (ident_line_counter != context_p->last_breakpoint_line)
|
||||
{
|
||||
JERRY_DEBUG_MSG ("Insert var breakpoint: %d (%d)\n", ident_line_counter, context_p->last_breakpoint_line);
|
||||
JERRY_ASSERT (context_p->last_cbc_opcode == CBC_PUSH_LITERAL);
|
||||
|
||||
cbc_argument_t last_cbc = context_p->last_cbc;
|
||||
context_p->last_cbc_opcode = PARSER_CBC_UNAVAILABLE;
|
||||
|
||||
parser_emit_cbc (context_p, CBC_BREAKPOINT_DISABLED);
|
||||
parser_flush_cbc (context_p);
|
||||
|
||||
parser_append_breakpoint_info (context_p, JERRY_DEBUGGER_BREAKPOINT_LIST, ident_line_counter);
|
||||
|
||||
context_p->last_cbc_opcode = CBC_PUSH_LITERAL;
|
||||
context_p->last_cbc = last_cbc;
|
||||
|
||||
context_p->last_breakpoint_line = ident_line_counter;
|
||||
}
|
||||
}
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
parser_parse_expression (context_p,
|
||||
PARSE_EXPR_STATEMENT | PARSE_EXPR_NO_COMMA | PARSE_EXPR_HAS_LITERAL);
|
||||
}
|
||||
@@ -369,6 +401,14 @@ parser_parse_function_statement (parser_context_t *context_p) /**< context */
|
||||
status_flags |= PARSER_HAS_NON_STRICT_ARG;
|
||||
}
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
if (JERRY_CONTEXT (jerry_init_flags) & JERRY_INIT_DEBUGGER)
|
||||
{
|
||||
jerry_debugger_send_function_name ((jerry_char_t *) name_p->u.char_p,
|
||||
name_p->prop.length);
|
||||
}
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
if (name_p->status_flags & LEXER_FLAG_INITIALIZED)
|
||||
{
|
||||
if (!(name_p->status_flags & (LEXER_FLAG_FUNCTION_NAME | LEXER_FLAG_FUNCTION_ARGUMENT)))
|
||||
@@ -1567,6 +1607,14 @@ parser_parse_statements (parser_context_t *context_p) /**< context */
|
||||
parser_stack_push_uint8 (context_p, PARSER_STATEMENT_START);
|
||||
parser_stack_iterator_init (context_p, &context_p->last_statement);
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
/* Set lexical enviroment for the debugger. */
|
||||
if (JERRY_CONTEXT (jerry_init_flags) & JERRY_INIT_DEBUGGER)
|
||||
{
|
||||
context_p->status_flags |= PARSER_LEXICAL_ENV_NEEDED;
|
||||
}
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
while (context_p->token.type == LEXER_LITERAL
|
||||
&& context_p->token.lit_location.type == LEXER_STRING_LITERAL)
|
||||
{
|
||||
@@ -1654,6 +1702,28 @@ parser_parse_statements (parser_context_t *context_p) /**< context */
|
||||
JERRY_ASSERT (context_p->stack_depth == context_p->context_stack_depth);
|
||||
#endif /* !JERRY_NDEBUG */
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
if (JERRY_CONTEXT (jerry_init_flags) & JERRY_INIT_DEBUGGER)
|
||||
{
|
||||
if (context_p->line != context_p->last_breakpoint_line
|
||||
&& context_p->token.type != LEXER_SEMICOLON
|
||||
&& context_p->token.type != LEXER_LEFT_BRACE
|
||||
&& context_p->token.type != LEXER_RIGHT_BRACE
|
||||
&& context_p->token.type != LEXER_KEYW_VAR
|
||||
&& context_p->token.type != LEXER_KEYW_FUNCTION
|
||||
&& context_p->token.type != LEXER_KEYW_CASE
|
||||
&& context_p->token.type != LEXER_KEYW_DEFAULT)
|
||||
{
|
||||
parser_emit_cbc (context_p, CBC_BREAKPOINT_DISABLED);
|
||||
parser_flush_cbc (context_p);
|
||||
|
||||
parser_append_breakpoint_info (context_p, JERRY_DEBUGGER_BREAKPOINT_LIST, context_p->line);
|
||||
|
||||
context_p->last_breakpoint_line = context_p->line;
|
||||
}
|
||||
}
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
switch (context_p->token.type)
|
||||
{
|
||||
case LEXER_SEMICOLON:
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "ecma-helpers.h"
|
||||
#include "ecma-literal-storage.h"
|
||||
#include "jcontext.h"
|
||||
#include "jerry-debugger.h"
|
||||
#include "js-parser-internal.h"
|
||||
|
||||
#ifdef JERRY_JS_PARSER
|
||||
@@ -1271,6 +1272,15 @@ parser_post_processing (parser_context_t *context_p) /**< context */
|
||||
|
||||
JERRY_ASSERT (context_p->literal_count <= PARSER_MAXIMUM_NUMBER_OF_LITERALS);
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
if ((JERRY_CONTEXT (jerry_init_flags) & JERRY_INIT_DEBUGGER)
|
||||
&& context_p->breakpoint_info_count > 0)
|
||||
{
|
||||
parser_send_breakpoints (context_p, JERRY_DEBUGGER_BREAKPOINT_LIST);
|
||||
JERRY_ASSERT (context_p->breakpoint_info_count == 0);
|
||||
}
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
initializers_length = parser_compute_indicies (context_p,
|
||||
&ident_end,
|
||||
&uninitialized_var_end,
|
||||
@@ -1573,6 +1583,14 @@ parser_post_processing (parser_context_t *context_p) /**< context */
|
||||
PARSER_NEXT_BYTE_UPDATE (page_p, offset, real_offset);
|
||||
flags = cbc_flags[opcode];
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
if (opcode == CBC_BREAKPOINT_DISABLED)
|
||||
{
|
||||
uint32_t offset = (uint32_t) (((uint8_t *) dst_p) - ((uint8_t *) compiled_code_p) - 1);
|
||||
parser_append_breakpoint_info (context_p, JERRY_DEBUGGER_BREAKPOINT_OFFSET_LIST, offset);
|
||||
}
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
if (opcode == CBC_EXT_OPCODE)
|
||||
{
|
||||
cbc_ext_opcode_t ext_opcode;
|
||||
@@ -1668,6 +1686,15 @@ parser_post_processing (parser_context_t *context_p) /**< context */
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
if ((JERRY_CONTEXT (jerry_init_flags) & JERRY_INIT_DEBUGGER)
|
||||
&& context_p->breakpoint_info_count > 0)
|
||||
{
|
||||
parser_send_breakpoints (context_p, JERRY_DEBUGGER_BREAKPOINT_OFFSET_LIST);
|
||||
JERRY_ASSERT (context_p->breakpoint_info_count == 0);
|
||||
}
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
if (!(context_p->status_flags & PARSER_NO_END_LABEL))
|
||||
{
|
||||
*dst_p++ = CBC_RETURN_WITH_BLOCK;
|
||||
@@ -1770,6 +1797,13 @@ parser_post_processing (parser_context_t *context_p) /**< context */
|
||||
compiled_code_p);
|
||||
}
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
if (JERRY_CONTEXT (jerry_init_flags) & JERRY_INIT_DEBUGGER)
|
||||
{
|
||||
jerry_debugger_send_function_cp (JERRY_DEBUGGER_BYTE_CODE_CP, compiled_code_p);
|
||||
}
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
return compiled_code_p;
|
||||
} /* parser_post_processing */
|
||||
|
||||
@@ -1861,6 +1895,11 @@ parser_parse_source (const uint8_t *source_p, /**< valid UTF-8 source code */
|
||||
}
|
||||
#endif /* PARSER_DUMP_BYTE_CODE */
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
context.breakpoint_info_count = 0;
|
||||
context.last_breakpoint_line = 0;
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
PARSER_TRY (context.try_buffer)
|
||||
{
|
||||
/* Pushing a dummy value ensures the stack is never empty.
|
||||
@@ -1948,6 +1987,15 @@ parser_parse_function (parser_context_t *context_p, /**< context */
|
||||
|
||||
JERRY_ASSERT (context_p->last_cbc_opcode == PARSER_CBC_UNAVAILABLE);
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
if ((JERRY_CONTEXT (jerry_init_flags) & JERRY_INIT_DEBUGGER)
|
||||
&& context_p->breakpoint_info_count > 0)
|
||||
{
|
||||
parser_send_breakpoints (context_p, JERRY_DEBUGGER_BREAKPOINT_LIST);
|
||||
context_p->breakpoint_info_count = 0;
|
||||
}
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
/* Save private part of the context. */
|
||||
|
||||
saved_context.status_flags = context_p->status_flags;
|
||||
@@ -1998,6 +2046,21 @@ parser_parse_function (parser_context_t *context_p, /**< context */
|
||||
}
|
||||
#endif /* PARSER_DUMP_BYTE_CODE */
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
if (JERRY_CONTEXT (jerry_init_flags) & JERRY_INIT_DEBUGGER)
|
||||
{
|
||||
if (context_p->line != context_p->last_breakpoint_line)
|
||||
{
|
||||
parser_emit_cbc (context_p, CBC_BREAKPOINT_DISABLED);
|
||||
parser_flush_cbc (context_p);
|
||||
|
||||
parser_append_breakpoint_info (context_p, JERRY_DEBUGGER_BREAKPOINT_LIST, context_p->line);
|
||||
|
||||
context_p->last_breakpoint_line = context_p->line;
|
||||
}
|
||||
}
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
lexer_next_token (context_p);
|
||||
|
||||
if (context_p->status_flags & PARSER_IS_FUNC_EXPRESSION
|
||||
@@ -2008,6 +2071,14 @@ parser_parse_function (parser_context_t *context_p, /**< context */
|
||||
&context_p->token.lit_location,
|
||||
LEXER_IDENT_LITERAL);
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
if (JERRY_CONTEXT (jerry_init_flags) & JERRY_INIT_DEBUGGER)
|
||||
{
|
||||
jerry_debugger_send_function_name ((jerry_char_t *) context_p->lit_object.literal_p->u.char_p,
|
||||
context_p->lit_object.literal_p->prop.length);
|
||||
}
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
/* The arguments object is created later than the binding to the
|
||||
* function expression name, so there is no need to assign special flags. */
|
||||
if (context_p->lit_object.type != LEXER_LITERAL_OBJECT_ARGUMENTS)
|
||||
@@ -2025,6 +2096,13 @@ parser_parse_function (parser_context_t *context_p, /**< context */
|
||||
lexer_next_token (context_p);
|
||||
}
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
if (JERRY_CONTEXT (jerry_init_flags) & JERRY_INIT_DEBUGGER)
|
||||
{
|
||||
jerry_debugger_send_type (JERRY_DEBUGGER_PARSE_FUNCTION);
|
||||
}
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
if (context_p->token.type != LEXER_LEFT_PAREN)
|
||||
{
|
||||
parser_raise_error (context_p, PARSER_ERR_ARGUMENT_LIST_EXPECTED);
|
||||
@@ -2223,6 +2301,46 @@ parser_raise_error (parser_context_t *context_p, /**< context */
|
||||
JERRY_ASSERT (0);
|
||||
} /* parser_raise_error */
|
||||
|
||||
#ifdef JERRY_DEBUGGER
|
||||
|
||||
/**
|
||||
* Append a breakpoint info.
|
||||
*/
|
||||
void
|
||||
parser_append_breakpoint_info (parser_context_t *context_p, /**< context */
|
||||
jerry_debugger_header_type_t type, /**< message type */
|
||||
uint32_t value) /**< line or offset of the breakpoint */
|
||||
{
|
||||
JERRY_ASSERT (JERRY_CONTEXT (jerry_init_flags) & JERRY_INIT_DEBUGGER);
|
||||
|
||||
if (context_p->breakpoint_info_count >= JERRY_DEBUGGER_MAX_SIZE (parser_list_t))
|
||||
{
|
||||
parser_send_breakpoints (context_p, type);
|
||||
}
|
||||
|
||||
context_p->breakpoint_info[context_p->breakpoint_info_count].value = value;
|
||||
context_p->breakpoint_info_count = (uint16_t) (context_p->breakpoint_info_count + 1);
|
||||
} /* parser_append_breakpoint_info */
|
||||
|
||||
/**
|
||||
* Send current breakpoint list.
|
||||
*/
|
||||
void
|
||||
parser_send_breakpoints (parser_context_t *context_p, /**< context */
|
||||
jerry_debugger_header_type_t type) /**< message type */
|
||||
{
|
||||
JERRY_ASSERT (JERRY_CONTEXT (jerry_init_flags) & JERRY_INIT_DEBUGGER);
|
||||
JERRY_ASSERT (context_p->breakpoint_info_count > 0);
|
||||
|
||||
jerry_debugger_send_data (type,
|
||||
context_p->breakpoint_info,
|
||||
context_p->breakpoint_info_count * sizeof (parser_breakpoint_info_t));
|
||||
|
||||
context_p->breakpoint_info_count = 0;
|
||||
} /* parser_send_breakpoints */
|
||||
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
#define PARSE_ERR_POS_START " [line: "
|
||||
#define PARSE_ERR_POS_START_SIZE ((uint32_t) sizeof (PARSE_ERR_POS_START) - 1)
|
||||
#define PARSE_ERR_POS_MIDDLE ", column: "
|
||||
@@ -2253,6 +2371,13 @@ parser_parse_script (const uint8_t *source_p, /**< source code */
|
||||
|
||||
if (!*bytecode_data_p)
|
||||
{
|
||||
#ifdef JERRY_DEBUGGER
|
||||
if (JERRY_CONTEXT (jerry_init_flags) & JERRY_INIT_DEBUGGER)
|
||||
{
|
||||
jerry_debugger_send_type (JERRY_DEBUGGER_PARSE_ERROR);
|
||||
}
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
if (parser_error.error == PARSER_ERR_OUT_OF_MEMORY)
|
||||
{
|
||||
/* It is unlikely that memory can be allocated in an out-of-memory
|
||||
|
||||
Reference in New Issue
Block a user