Skip to content

Commit bc46a84

Browse files
committed
Simplify debugger-ws.h
Remove several macros and types from it. This change simplify the required debugger interface. JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
1 parent a0e3157 commit bc46a84

File tree

7 files changed

+90
-74
lines changed

7 files changed

+90
-74
lines changed

jerry-core/debugger/debugger-ws.c

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,33 @@
4646
*/
4747
#define JERRY_DEBUGGER_WEBSOCKET_LENGTH_MASK 0x7fu
4848

49+
/**
50+
* Size of websocket header size.
51+
*/
52+
#define JERRY_DEBUGGER_WEBSOCKET_HEADER_SIZE 2
53+
4954
/**
5055
* Payload mask size in bytes of a websocket package.
5156
*/
5257
#define JERRY_DEBUGGER_WEBSOCKET_MASK_SIZE 4
5358

5459
/**
55-
*
60+
* Waiting for data from the client.
5661
*/
5762
#define JERRY_DEBUGGER_RECEIVE_DATA_MODE \
5863
(JERRY_DEBUGGER_BREAKPOINT_MODE | JERRY_DEBUGGER_CLIENT_SOURCE_MODE)
5964

65+
/**
66+
* WebSocket opcode types.
67+
*/
68+
typedef enum
69+
{
70+
JERRY_DEBUGGER_WEBSOCKET_TEXT_FRAME = 1, /**< text frame */
71+
JERRY_DEBUGGER_WEBSOCKET_BINARY_FRAME = 2, /**< binary frame */
72+
JERRY_DEBUGGER_WEBSOCKET_CLOSE_CONNECTION = 8, /**< close connection */
73+
JERRY_DEBUGGER_WEBSOCKET_PING = 9, /**< ping (keep alive) frame */
74+
JERRY_DEBUGGER_WEBSOCKET_PONG = 10, /**< reply to ping frame */
75+
} jerry_websocket_opcode_type_t;
6076

6177
/**
6278
* Header for incoming packets.
@@ -321,6 +337,25 @@ jerry_debugger_accept_connection (void)
321337
struct sockaddr_in addr;
322338
socklen_t sin_size = sizeof (struct sockaddr_in);
323339

340+
uint8_t *payload_p = JERRY_CONTEXT (debugger_send_buffer) + JERRY_DEBUGGER_WEBSOCKET_HEADER_SIZE;
341+
JERRY_CONTEXT (debugger_send_buffer_payload_p) = payload_p;
342+
343+
uint8_t max_send_size = (JERRY_DEBUGGER_MAX_BUFFER_SIZE - JERRY_DEBUGGER_WEBSOCKET_HEADER_SIZE);
344+
if (max_send_size > 125)
345+
{
346+
max_send_size = 125;
347+
}
348+
JERRY_CONTEXT (debugger_max_send_size) = max_send_size;
349+
350+
uint8_t receive_header_size = (JERRY_DEBUGGER_WEBSOCKET_HEADER_SIZE + JERRY_DEBUGGER_WEBSOCKET_MASK_SIZE);
351+
uint8_t max_receive_size = (uint8_t) (JERRY_DEBUGGER_MAX_BUFFER_SIZE - receive_header_size);
352+
353+
if (max_receive_size > 125)
354+
{
355+
max_receive_size = 125;
356+
}
357+
JERRY_CONTEXT (debugger_max_receive_size) = max_receive_size;
358+
324359
addr.sin_family = AF_INET;
325360
addr.sin_port = htons (JERRY_CONTEXT (debugger_port));
326361
addr.sin_addr.s_addr = INADDR_ANY;
@@ -384,7 +419,7 @@ jerry_debugger_accept_connection (void)
384419
return false;
385420
}
386421

387-
if (!jerry_debugger_send_configuration (JERRY_DEBUGGER_MAX_RECEIVE_SIZE))
422+
if (!jerry_debugger_send_configuration (max_receive_size))
388423
{
389424
return false;
390425
}
@@ -430,14 +465,14 @@ jerry_debugger_close_connection (void)
430465
bool
431466
jerry_debugger_send (size_t data_size) /**< data size */
432467
{
433-
jerry_debugger_send_type_t *message_p = (jerry_debugger_send_type_t *) JERRY_CONTEXT (debugger_send_buffer);
434-
message_p->header.ws_opcode = JERRY_DEBUGGER_WEBSOCKET_FIN_BIT | JERRY_DEBUGGER_WEBSOCKET_BINARY_FRAME;
435-
message_p->header.size = (uint8_t) (data_size - sizeof (jerry_debugger_send_header_t));
436-
return jerry_debugger_send_tcp (JERRY_CONTEXT (debugger_send_buffer), data_size);
437-
} /* jerry_debugger_send */
468+
JERRY_ASSERT (data_size <= JERRY_CONTEXT (debugger_max_send_size));
438469

439-
JERRY_STATIC_ASSERT (JERRY_DEBUGGER_MAX_RECEIVE_SIZE < 126,
440-
maximum_debug_message_receive_size_must_be_smaller_than_126);
470+
uint8_t *header_p = JERRY_CONTEXT (debugger_send_buffer);
471+
header_p[0] = JERRY_DEBUGGER_WEBSOCKET_FIN_BIT | JERRY_DEBUGGER_WEBSOCKET_BINARY_FRAME;
472+
header_p[1] = (uint8_t) data_size;
473+
474+
return jerry_debugger_send_tcp (header_p, data_size + JERRY_DEBUGGER_WEBSOCKET_HEADER_SIZE);
475+
} /* jerry_debugger_send */
441476

442477
/**
443478
* Receive message from the client.
@@ -453,6 +488,7 @@ bool
453488
jerry_debugger_receive (jerry_debugger_uint8_data_t **message_data_p) /**< [out] data received from client */
454489
{
455490
JERRY_ASSERT (JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED);
491+
JERRY_ASSERT (JERRY_CONTEXT (debugger_max_receive_size) <= 125);
456492

457493
JERRY_ASSERT (message_data_p != NULL ? !!(JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_RECEIVE_DATA_MODE)
458494
: !(JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_RECEIVE_DATA_MODE));
@@ -497,7 +533,7 @@ jerry_debugger_receive (jerry_debugger_uint8_data_t **message_data_p) /**< [out]
497533
}
498534

499535
if ((recv_buffer_p[0] & ~JERRY_DEBUGGER_WEBSOCKET_OPCODE_MASK) != JERRY_DEBUGGER_WEBSOCKET_FIN_BIT
500-
|| (recv_buffer_p[1] & JERRY_DEBUGGER_WEBSOCKET_LENGTH_MASK) > JERRY_DEBUGGER_MAX_RECEIVE_SIZE
536+
|| (recv_buffer_p[1] & JERRY_DEBUGGER_WEBSOCKET_LENGTH_MASK) > JERRY_CONTEXT (debugger_max_receive_size)
501537
|| !(recv_buffer_p[1] & JERRY_DEBUGGER_WEBSOCKET_MASK_BIT))
502538
{
503539
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Unsupported Websocket message.\n");

jerry-core/debugger/debugger-ws.h

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,6 @@
2727
*/
2828
#define JERRY_DEBUGGER_MAX_BUFFER_SIZE 128
2929

30-
/**
31-
* Maximum number of bytes that can be sent in a single message.
32-
*/
33-
#define JERRY_DEBUGGER_MAX_SEND_SIZE (JERRY_DEBUGGER_MAX_BUFFER_SIZE - 1)
34-
35-
/**
36-
* Maximum number of bytes that can be received in a single message.
37-
*/
38-
#define JERRY_DEBUGGER_MAX_RECEIVE_SIZE (JERRY_DEBUGGER_MAX_BUFFER_SIZE - 6)
39-
40-
/**
41-
* WebSocket opcode types.
42-
*/
43-
typedef enum
44-
{
45-
JERRY_DEBUGGER_WEBSOCKET_TEXT_FRAME = 1, /**< text frame */
46-
JERRY_DEBUGGER_WEBSOCKET_BINARY_FRAME = 2, /**< binary frame */
47-
JERRY_DEBUGGER_WEBSOCKET_CLOSE_CONNECTION = 8, /**< close connection */
48-
JERRY_DEBUGGER_WEBSOCKET_PING = 9, /**< ping (keep alive) frame */
49-
JERRY_DEBUGGER_WEBSOCKET_PONG = 10, /**< reply to ping frame */
50-
} jerry_websocket_opcode_type_t;
51-
52-
/**
53-
* Header for outgoing packets.
54-
*/
55-
typedef struct
56-
{
57-
uint8_t ws_opcode; /**< Websocket opcode */
58-
uint8_t size; /**< size of the message */
59-
} jerry_debugger_send_header_t;
60-
6130
/**
6231
* Incoming message: next message of string data.
6332
*/

jerry-core/debugger/debugger.c

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ JERRY_STATIC_ASSERT (JERRY_DEBUGGER_MESSAGES_OUT_MAX_COUNT == 27
3737
* Type cast the debugger send buffer into a specific type.
3838
*/
3939
#define JERRY_DEBUGGER_SEND_BUFFER_AS(type, name_p) \
40-
type *name_p = (type *) (&JERRY_CONTEXT (debugger_send_buffer))
40+
type *name_p = (type *) (JERRY_CONTEXT (debugger_send_buffer_payload_p))
4141

4242
/**
4343
* Type cast the debugger receive buffer into a specific type.
@@ -92,7 +92,9 @@ jerry_debugger_send_backtrace (uint8_t *recv_buffer_p) /**< pointer to the recei
9292

9393
vm_frame_ctx_t *frame_ctx_p = JERRY_CONTEXT (vm_top_context_p);
9494

95-
uint32_t current_frame = 0;
95+
size_t current_frame = 0;
96+
const size_t max_frame_count = JERRY_DEBUGGER_SEND_MAX (jerry_debugger_frame_t);
97+
const size_t max_message_size = JERRY_DEBUGGER_SEND_SIZE (max_frame_count, jerry_debugger_frame_t);
9698

9799
while (frame_ctx_p != NULL && max_depth > 0)
98100
{
@@ -102,9 +104,9 @@ jerry_debugger_send_backtrace (uint8_t *recv_buffer_p) /**< pointer to the recei
102104
continue;
103105
}
104106

105-
if (current_frame >= JERRY_DEBUGGER_SEND_MAX (jerry_debugger_frame_t))
107+
if (current_frame >= max_frame_count)
106108
{
107-
if (!jerry_debugger_send (sizeof (jerry_debugger_send_backtrace_t)))
109+
if (!jerry_debugger_send (max_message_size))
108110
{
109111
return;
110112
}
@@ -533,7 +535,7 @@ jerry_debugger_process_message (uint8_t *recv_buffer_p, /**< pointer to the rece
533535
memcpy (&eval_size, eval_first_p->eval_size, sizeof (uint32_t));
534536
bool is_eval = (recv_buffer_p[0] == JERRY_DEBUGGER_EVAL);
535537

536-
if (eval_size <= JERRY_DEBUGGER_MAX_RECEIVE_SIZE - sizeof (jerry_debugger_receive_eval_first_t))
538+
if (eval_size <= JERRY_CONTEXT (debugger_max_receive_size) - sizeof (jerry_debugger_receive_eval_first_t))
537539
{
538540
if (eval_size != message_size - sizeof (jerry_debugger_receive_eval_first_t))
539541
{
@@ -590,8 +592,10 @@ jerry_debugger_process_message (uint8_t *recv_buffer_p, /**< pointer to the rece
590592
uint32_t client_source_size;
591593
memcpy (&client_source_size, client_source_first_p->code_size, sizeof (uint32_t));
592594

593-
if (client_source_size <= JERRY_DEBUGGER_MAX_RECEIVE_SIZE - sizeof (jerry_debugger_receive_client_source_first_t)
594-
&& client_source_size != message_size - sizeof (jerry_debugger_receive_client_source_first_t))
595+
uint32_t header_size = sizeof (jerry_debugger_receive_client_source_first_t);
596+
597+
if (client_source_size <= JERRY_CONTEXT (debugger_max_receive_size) - header_size
598+
&& client_source_size != message_size - header_size)
595599
{
596600
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Invalid message size\n");
597601
jerry_debugger_close_connection ();
@@ -796,37 +800,42 @@ jerry_debugger_send_string (uint8_t message_type, /**< message type */
796800
{
797801
JERRY_ASSERT (JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED);
798802

799-
const size_t max_fragment_len = JERRY_DEBUGGER_SEND_MAX (uint8_t);
803+
const size_t max_byte_count = JERRY_DEBUGGER_SEND_MAX (uint8_t);
804+
const size_t max_message_size = JERRY_DEBUGGER_SEND_SIZE (max_byte_count, uint8_t);
800805

801806
JERRY_DEBUGGER_SEND_BUFFER_AS (jerry_debugger_send_string_t, message_string_p);
802807

803808
message_string_p->type = message_type;
804809

805-
while (string_length > max_fragment_len)
810+
if (sub_type != JERRY_DEBUGGER_NO_SUBTYPE)
806811
{
807-
memcpy (message_string_p->string, string_p, max_fragment_len);
812+
string_length += 1;
813+
}
808814

809-
if (!jerry_debugger_send (sizeof (jerry_debugger_send_string_t)))
815+
while (string_length > max_byte_count)
816+
{
817+
memcpy (message_string_p->string, string_p, max_byte_count);
818+
819+
if (!jerry_debugger_send (max_message_size))
810820
{
811821
return false;
812822
}
813823

814-
string_length -= max_fragment_len;
815-
string_p += max_fragment_len;
816-
}
817-
818-
if (sub_type != JERRY_DEBUGGER_NO_SUBTYPE)
819-
{
820-
string_length += 1;
824+
string_length -= max_byte_count;
825+
string_p += max_byte_count;
821826
}
822827

823828
message_string_p->type = (uint8_t) (message_type + 1);
824829

825-
memcpy (message_string_p->string, string_p, string_length);
826830
if (sub_type != JERRY_DEBUGGER_NO_SUBTYPE)
827831
{
832+
memcpy (message_string_p->string, string_p, string_length - 1);
828833
message_string_p->string[string_length - 1] = sub_type;
829834
}
835+
else
836+
{
837+
memcpy (message_string_p->string, string_p, string_length);
838+
}
830839

831840
return jerry_debugger_send (sizeof (jerry_debugger_send_type_t) + string_length);
832841
} /* jerry_debugger_send_string */

jerry-core/debugger/debugger.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,16 @@
5353

5454
/**
5555
* Calculate the maximum number of items for a given type
56-
* which can be transmitted by one message.
56+
* which can be transmitted in one message.
5757
*/
5858
#define JERRY_DEBUGGER_SEND_MAX(type) \
59-
((JERRY_DEBUGGER_MAX_SEND_SIZE - sizeof (jerry_debugger_send_header_t) - 1) / sizeof (type))
59+
((size_t) ((JERRY_CONTEXT (debugger_max_send_size) - sizeof (jerry_debugger_send_type_t)) / sizeof (type)))
60+
61+
/**
62+
* Calculate the size of a message when a count number of items transmitted.
63+
*/
64+
#define JERRY_DEBUGGER_SEND_SIZE(count, type) \
65+
((size_t) ((count * sizeof (type)) + sizeof (jerry_debugger_send_type_t)))
6066

6167
/**
6268
* Debugger operation modes:
@@ -226,7 +232,6 @@ typedef struct
226232
*/
227233
typedef struct
228234
{
229-
jerry_debugger_send_header_t header; /**< message header */
230235
uint8_t type; /**< type of the message */
231236
uint8_t max_message_size; /**< maximum incoming message size */
232237
uint8_t cpointer_size; /**< size of compressed pointers */
@@ -239,7 +244,6 @@ typedef struct
239244
*/
240245
typedef struct
241246
{
242-
jerry_debugger_send_header_t header; /**< message header */
243247
uint8_t type; /**< type of the message */
244248
} jerry_debugger_send_type_t;
245249

@@ -256,17 +260,15 @@ typedef struct
256260
*/
257261
typedef struct
258262
{
259-
jerry_debugger_send_header_t header; /**< message header */
260263
uint8_t type; /**< type of the message */
261-
uint8_t string[JERRY_DEBUGGER_SEND_MAX (uint8_t)]; /**< string data */
264+
uint8_t string[1]; /**< string data */
262265
} jerry_debugger_send_string_t;
263266

264267
/**
265268
* Outgoing message: uint32 value.
266269
*/
267270
typedef struct
268271
{
269-
jerry_debugger_send_header_t header; /**< message header */
270272
uint8_t type; /**< type of the message */
271273
uint8_t line[sizeof (uint32_t)]; /**< value data */
272274
uint8_t column[sizeof (uint32_t)]; /**< value data */
@@ -277,7 +279,6 @@ typedef struct
277279
*/
278280
typedef struct
279281
{
280-
jerry_debugger_send_header_t header; /**< message header */
281282
uint8_t type; /**< type of the message */
282283
uint8_t byte_code_cp[sizeof (jmem_cpointer_t)]; /**< byte code compressed pointer */
283284
} jerry_debugger_send_byte_code_cp_t;
@@ -307,7 +308,6 @@ typedef struct
307308
*/
308309
typedef struct
309310
{
310-
jerry_debugger_send_header_t header; /**< message header */
311311
uint8_t type; /**< type of the message */
312312
uint8_t allocated_bytes[sizeof (uint32_t)]; /**< allocated bytes */
313313
uint8_t byte_code_bytes[sizeof (uint32_t)]; /**< byte code bytes */
@@ -321,7 +321,6 @@ typedef struct
321321
*/
322322
typedef struct
323323
{
324-
jerry_debugger_send_header_t header; /**< message header */
325324
uint8_t type; /**< type of the message */
326325
uint8_t byte_code_cp[sizeof (jmem_cpointer_t)]; /**< byte code compressed pointer */
327326
uint8_t offset[sizeof (uint32_t)]; /**< breakpoint offset */
@@ -341,9 +340,8 @@ typedef struct
341340
*/
342341
typedef struct
343342
{
344-
jerry_debugger_send_header_t header; /**< message header */
345343
uint8_t type; /**< type of the message */
346-
jerry_debugger_frame_t frames[JERRY_DEBUGGER_SEND_MAX (jerry_debugger_frame_t)]; /**< frames */
344+
jerry_debugger_frame_t frames[1]; /**< frames */
347345
} jerry_debugger_send_backtrace_t;
348346

349347
/**

jerry-core/jcontext/jcontext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,16 @@ typedef struct
111111
#ifdef JERRY_DEBUGGER
112112
uint8_t debugger_send_buffer[JERRY_DEBUGGER_MAX_BUFFER_SIZE]; /**< buffer for sending messages */
113113
uint8_t debugger_receive_buffer[JERRY_DEBUGGER_MAX_BUFFER_SIZE]; /**< buffer for receiving messages */
114+
uint8_t *debugger_send_buffer_payload_p; /**< start where the outgoing message can be written */
114115
vm_frame_ctx_t *debugger_stop_context; /**< stop only if the current context is equal to this context */
115116
jmem_cpointer_t debugger_byte_code_free_head; /**< head of byte code free linked list */
116117
jmem_cpointer_t debugger_byte_code_free_tail; /**< tail of byte code free linked list */
117118
uint32_t debugger_flags; /**< debugger flags */
118119
uint16_t debugger_receive_buffer_offset; /**< receive buffer offset */
119120
uint16_t debugger_port; /**< debugger socket communication port */
120121
uint8_t debugger_message_delay; /**< call receive message when reaches zero */
122+
uint8_t debugger_max_send_size; /**< maximum amount of data that can be written */
123+
uint8_t debugger_max_receive_size; /**< maximum amount of data that can be received */
121124
int debugger_connection; /**< holds the file descriptor of the socket communication */
122125
#endif /* JERRY_DEBUGGER */
123126

jerry-core/parser/js/js-parser-internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,8 @@ typedef struct
305305
#endif /* PARSER_DUMP_BYTE_CODE */
306306

307307
#ifdef JERRY_DEBUGGER
308-
parser_breakpoint_info_t breakpoint_info[JERRY_DEBUGGER_SEND_MAX (parser_list_t)]; /**< extra data for breakpoints */
308+
/** extra data for each breakpoint */
309+
parser_breakpoint_info_t breakpoint_info[JERRY_DEBUGGER_MAX_BUFFER_SIZE / sizeof (parser_breakpoint_info_t)];
309310
uint16_t breakpoint_info_count; /**< current breakpoint index */
310311
parser_line_counter_t last_breakpoint_line; /**< last line where breakpoint was inserted */
311312
#endif /* JERRY_DEBUGGER */

jerry-core/parser/js/js-parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2700,7 +2700,7 @@ parser_append_breakpoint_info (parser_context_t *context_p, /**< context */
27002700

27012701
context_p->status_flags |= PARSER_DEBUGGER_BREAKPOINT_APPENDED;
27022702

2703-
if (context_p->breakpoint_info_count >= JERRY_DEBUGGER_SEND_MAX (parser_list_t))
2703+
if (context_p->breakpoint_info_count >= JERRY_DEBUGGER_SEND_MAX (parser_breakpoint_info_t))
27042704
{
27052705
parser_send_breakpoints (context_p, type);
27062706
}

0 commit comments

Comments
 (0)