Skip to content

Commit 9d1c2a5

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 [email protected]
1 parent ba22072 commit 9d1c2a5

File tree

6 files changed

+68
-100
lines changed

6 files changed

+68
-100
lines changed

jerry-core/debugger/debugger-ws.c

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
#include <fcntl.h>
2525
#include <unistd.h>
2626

27+
/**
28+
* Last fragment of a Websocket package.
29+
*/
30+
#define JERRY_DEBUGGER_WEBSOCKET_FIN_BIT 0x80
31+
2732
/**
2833
* Masking-key is available.
2934
*/
@@ -39,17 +44,33 @@
3944
*/
4045
#define JERRY_DEBUGGER_WEBSOCKET_LENGTH_MASK 0x7fu
4146

47+
/**
48+
* Size of websocket header size.
49+
*/
50+
#define JERRY_DEBUGGER_WEBSOCKET_HEADER_SIZE 2
51+
4252
/**
4353
* Payload mask size in bytes of a websocket package.
4454
*/
4555
#define JERRY_DEBUGGER_WEBSOCKET_MASK_SIZE 4
4656

4757
/**
48-
*
58+
* Waiting for data from the client.
4959
*/
5060
#define JERRY_DEBUGGER_RECEIVE_DATA_MODE \
5161
(JERRY_DEBUGGER_BREAKPOINT_MODE | JERRY_DEBUGGER_CLIENT_SOURCE_MODE)
5262

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

5475
/**
5576
* Header for incoming packets.
@@ -314,6 +335,25 @@ jerry_debugger_accept_connection (void)
314335
struct sockaddr_in addr;
315336
socklen_t sin_size = sizeof (struct sockaddr_in);
316337

338+
uint8_t *payload_p = JERRY_CONTEXT (debugger_send_buffer) + JERRY_DEBUGGER_WEBSOCKET_HEADER_SIZE;
339+
JERRY_CONTEXT (debugger_send_buffer_payload_p) = payload_p;
340+
341+
uint8_t max_send_size = (JERRY_DEBUGGER_MAX_BUFFER_SIZE - JERRY_DEBUGGER_WEBSOCKET_HEADER_SIZE);
342+
if (max_send_size > 125)
343+
{
344+
max_send_size = 125;
345+
}
346+
JERRY_CONTEXT (debugger_max_send_size) = max_send_size;
347+
348+
uint8_t receive_header_size = (JERRY_DEBUGGER_WEBSOCKET_HEADER_SIZE + JERRY_DEBUGGER_WEBSOCKET_MASK_SIZE);
349+
uint8_t max_receive_size = (uint8_t) (JERRY_DEBUGGER_MAX_BUFFER_SIZE - receive_header_size);
350+
351+
if (max_receive_size > 125)
352+
{
353+
max_receive_size = 125;
354+
}
355+
JERRY_CONTEXT (debugger_max_receive_size) = max_receive_size;
356+
317357
addr.sin_family = AF_INET;
318358
addr.sin_port = htons (JERRY_CONTEXT (debugger_port));
319359
addr.sin_addr.s_addr = INADDR_ANY;
@@ -377,7 +417,7 @@ jerry_debugger_accept_connection (void)
377417
return false;
378418
}
379419

380-
if (!jerry_debugger_send_configuration (JERRY_DEBUGGER_MAX_RECEIVE_SIZE))
420+
if (!jerry_debugger_send_configuration (max_receive_size))
381421
{
382422
return false;
383423
}
@@ -423,11 +463,14 @@ jerry_debugger_close_connection (void)
423463
inline bool __attr_always_inline___
424464
jerry_debugger_send (size_t data_size) /**< data size */
425465
{
426-
return jerry_debugger_send_tcp (JERRY_CONTEXT (debugger_send_buffer), data_size);
427-
} /* jerry_debugger_send */
466+
JERRY_ASSERT (data_size <= JERRY_CONTEXT (debugger_max_send_size));
428467

429-
JERRY_STATIC_ASSERT (JERRY_DEBUGGER_MAX_RECEIVE_SIZE < 126,
430-
maximum_debug_message_receive_size_must_be_smaller_than_126);
468+
uint8_t *header_p = JERRY_CONTEXT (debugger_send_buffer);
469+
header_p[0] = JERRY_DEBUGGER_WEBSOCKET_FIN_BIT | JERRY_DEBUGGER_WEBSOCKET_BINARY_FRAME;
470+
header_p[1] = (uint8_t) data_size;
471+
472+
return jerry_debugger_send_tcp (header_p, data_size + JERRY_DEBUGGER_WEBSOCKET_HEADER_SIZE);
473+
} /* jerry_debugger_send */
431474

432475
/**
433476
* Receive message from the client.
@@ -443,6 +486,7 @@ bool
443486
jerry_debugger_receive (jerry_debugger_uint8_data_t **message_data_p) /**< [out] data received from client */
444487
{
445488
JERRY_ASSERT (JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED);
489+
JERRY_ASSERT (JERRY_CONTEXT (debugger_max_receive_size) <= 125);
446490

447491
JERRY_ASSERT (message_data_p != NULL ? !!(JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_RECEIVE_DATA_MODE)
448492
: !(JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_RECEIVE_DATA_MODE));
@@ -487,7 +531,7 @@ jerry_debugger_receive (jerry_debugger_uint8_data_t **message_data_p) /**< [out]
487531
}
488532

489533
if ((recv_buffer_p[0] & ~JERRY_DEBUGGER_WEBSOCKET_OPCODE_MASK) != JERRY_DEBUGGER_WEBSOCKET_FIN_BIT
490-
|| (recv_buffer_p[1] & JERRY_DEBUGGER_WEBSOCKET_LENGTH_MASK) > JERRY_DEBUGGER_MAX_RECEIVE_SIZE
534+
|| (recv_buffer_p[1] & JERRY_DEBUGGER_WEBSOCKET_LENGTH_MASK) > JERRY_CONTEXT (debugger_max_receive_size)
491535
|| !(recv_buffer_p[1] & JERRY_DEBUGGER_WEBSOCKET_MASK_BIT))
492536
{
493537
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Unsupported Websocket message.\n");

jerry-core/debugger/debugger-ws.h

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +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-
* Last fragment of a Websocket package.
42-
*/
43-
#define JERRY_DEBUGGER_WEBSOCKET_FIN_BIT 0x80
44-
45-
/**
46-
* WebSocket opcode types.
47-
*/
48-
typedef enum
49-
{
50-
JERRY_DEBUGGER_WEBSOCKET_TEXT_FRAME = 1, /**< text frame */
51-
JERRY_DEBUGGER_WEBSOCKET_BINARY_FRAME = 2, /**< binary frame */
52-
JERRY_DEBUGGER_WEBSOCKET_CLOSE_CONNECTION = 8, /**< close connection */
53-
JERRY_DEBUGGER_WEBSOCKET_PING = 9, /**< ping (keep alive) frame */
54-
JERRY_DEBUGGER_WEBSOCKET_PONG = 10, /**< reply to ping frame */
55-
} jerry_websocket_opcode_type_t;
56-
57-
/**
58-
* Header for outgoing packets.
59-
*/
60-
typedef struct
61-
{
62-
uint8_t ws_opcode; /**< Websocket opcode */
63-
uint8_t size; /**< size of the message */
64-
} jerry_debugger_send_header_t;
65-
6630
/**
6731
* Incoming message: next message of string data.
6832
*/
@@ -80,24 +44,6 @@ typedef struct
8044
uint32_t uint8_offset; /**< current offset in the client source */
8145
} jerry_debugger_uint8_data_t;
8246

83-
/**
84-
* Initialize the header of an outgoing message.
85-
*/
86-
#define JERRY_DEBUGGER_INIT_SEND_MESSAGE(message_p) \
87-
(message_p)->header.ws_opcode = JERRY_DEBUGGER_WEBSOCKET_FIN_BIT | JERRY_DEBUGGER_WEBSOCKET_BINARY_FRAME
88-
89-
/**
90-
* Set the size of an outgoing message from type.
91-
*/
92-
#define JERRY_DEBUGGER_SET_SEND_MESSAGE_SIZE_FROM_TYPE(message_p, type) \
93-
(message_p)->header.size = (uint8_t) (sizeof (type) - sizeof (jerry_debugger_send_header_t))
94-
95-
/**
96-
* Set the size of an outgoing message.
97-
*/
98-
#define JERRY_DEBUGGER_SET_SEND_MESSAGE_SIZE(message_p, byte_size) \
99-
(message_p)->header.size = (uint8_t) (byte_size)
100-
10147
bool jerry_debugger_accept_connection (void);
10248
void jerry_debugger_close_connection (void);
10349

jerry-core/debugger/debugger.c

Lines changed: 10 additions & 27 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.
@@ -88,13 +88,12 @@ jerry_debugger_send_backtrace (uint8_t *recv_buffer_p) /**< pointer to the recei
8888

8989
JERRY_DEBUGGER_SEND_BUFFER_AS (jerry_debugger_send_backtrace_t, backtrace_p);
9090

91-
JERRY_DEBUGGER_INIT_SEND_MESSAGE (backtrace_p);
92-
JERRY_DEBUGGER_SET_SEND_MESSAGE_SIZE_FROM_TYPE (backtrace_p, jerry_debugger_send_backtrace_t);
9391
backtrace_p->type = JERRY_DEBUGGER_BACKTRACE;
9492

9593
vm_frame_ctx_t *frame_ctx_p = JERRY_CONTEXT (vm_top_context_p);
9694

9795
uint32_t current_frame = 0;
96+
uint32_t max_frame_count = (uint32_t) JERRY_DEBUGGER_SEND_MAX (jerry_debugger_frame_t);
9897

9998
while (frame_ctx_p != NULL && max_depth > 0)
10099
{
@@ -104,9 +103,9 @@ jerry_debugger_send_backtrace (uint8_t *recv_buffer_p) /**< pointer to the recei
104103
continue;
105104
}
106105

107-
if (current_frame >= JERRY_DEBUGGER_SEND_MAX (jerry_debugger_frame_t))
106+
if (current_frame >= max_frame_count)
108107
{
109-
if (!jerry_debugger_send (sizeof (jerry_debugger_send_backtrace_t)))
108+
if (!jerry_debugger_send (max_frame_count * sizeof (jerry_debugger_frame_t) + 1))
110109
{
111110
return;
112111
}
@@ -129,7 +128,6 @@ jerry_debugger_send_backtrace (uint8_t *recv_buffer_p) /**< pointer to the recei
129128

130129
size_t message_size = current_frame * sizeof (jerry_debugger_frame_t);
131130

132-
JERRY_DEBUGGER_SET_SEND_MESSAGE_SIZE (backtrace_p, 1 + message_size);
133131
backtrace_p->type = JERRY_DEBUGGER_BACKTRACE_END;
134132

135133
jerry_debugger_send (sizeof (jerry_debugger_send_type_t) + message_size);
@@ -536,7 +534,7 @@ jerry_debugger_process_message (uint8_t *recv_buffer_p, /**< pointer to the rece
536534
memcpy (&eval_size, eval_first_p->eval_size, sizeof (uint32_t));
537535
bool is_eval = (recv_buffer_p[0] == JERRY_DEBUGGER_EVAL);
538536

539-
if (eval_size <= JERRY_DEBUGGER_MAX_RECEIVE_SIZE - sizeof (jerry_debugger_receive_eval_first_t))
537+
if (eval_size <= JERRY_CONTEXT (debugger_max_receive_size) - sizeof (jerry_debugger_receive_eval_first_t))
540538
{
541539
if (eval_size != message_size - sizeof (jerry_debugger_receive_eval_first_t))
542540
{
@@ -593,8 +591,10 @@ jerry_debugger_process_message (uint8_t *recv_buffer_p, /**< pointer to the rece
593591
uint32_t client_source_size;
594592
memcpy (&client_source_size, client_source_first_p->code_size, sizeof (uint32_t));
595593

596-
if (client_source_size <= JERRY_DEBUGGER_MAX_RECEIVE_SIZE - sizeof (jerry_debugger_receive_client_source_first_t)
597-
&& client_source_size != message_size - sizeof (jerry_debugger_receive_client_source_first_t))
594+
uint32_t header_size = sizeof (jerry_debugger_receive_client_source_first_t);
595+
596+
if (client_source_size <= JERRY_CONTEXT (debugger_max_receive_size) - header_size
597+
&& client_source_size != message_size - header_size)
598598
{
599599
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Invalid message size\n");
600600
jerry_debugger_close_connection ();
@@ -686,8 +686,6 @@ jerry_debugger_breakpoint_hit (uint8_t message_type) /**< message type */
686686

687687
JERRY_DEBUGGER_SEND_BUFFER_AS (jerry_debugger_send_breakpoint_hit_t, breakpoint_hit_p);
688688

689-
JERRY_DEBUGGER_INIT_SEND_MESSAGE (breakpoint_hit_p);
690-
JERRY_DEBUGGER_SET_SEND_MESSAGE_SIZE_FROM_TYPE (breakpoint_hit_p, jerry_debugger_send_breakpoint_hit_t);
691689
breakpoint_hit_p->type = message_type;
692690

693691
vm_frame_ctx_t *frame_ctx_p = JERRY_CONTEXT (vm_top_context_p);
@@ -734,8 +732,6 @@ jerry_debugger_send_type (jerry_debugger_header_type_t type) /**< message type *
734732

735733
JERRY_DEBUGGER_SEND_BUFFER_AS (jerry_debugger_send_type_t, message_type_p);
736734

737-
JERRY_DEBUGGER_INIT_SEND_MESSAGE (message_type_p);
738-
JERRY_DEBUGGER_SET_SEND_MESSAGE_SIZE_FROM_TYPE (message_type_p, jerry_debugger_send_type_t);
739735
message_type_p->type = (uint8_t) type;
740736

741737
jerry_debugger_send (sizeof (jerry_debugger_send_type_t));
@@ -762,8 +758,6 @@ jerry_debugger_send_configuration (uint8_t max_message_size) /**< maximum messag
762758

763759
endian_data.uint16_value = 1;
764760

765-
JERRY_DEBUGGER_INIT_SEND_MESSAGE (configuration_p);
766-
JERRY_DEBUGGER_SET_SEND_MESSAGE_SIZE_FROM_TYPE (configuration_p, jerry_debugger_send_configuration_t);
767761
configuration_p->type = JERRY_DEBUGGER_CONFIGURATION;
768762
configuration_p->max_message_size = max_message_size;
769763
configuration_p->cpointer_size = sizeof (jmem_cpointer_t);
@@ -785,8 +779,6 @@ jerry_debugger_send_data (jerry_debugger_header_type_t type, /**< message type *
785779

786780
JERRY_DEBUGGER_SEND_BUFFER_AS (jerry_debugger_send_type_t, message_type_p);
787781

788-
JERRY_DEBUGGER_INIT_SEND_MESSAGE (message_type_p);
789-
JERRY_DEBUGGER_SET_SEND_MESSAGE_SIZE (message_type_p, 1 + size);
790782
message_type_p->type = type;
791783
memcpy (message_type_p + 1, data, size);
792784

@@ -811,15 +803,13 @@ jerry_debugger_send_string (uint8_t message_type, /**< message type */
811803

812804
JERRY_DEBUGGER_SEND_BUFFER_AS (jerry_debugger_send_string_t, message_string_p);
813805

814-
JERRY_DEBUGGER_INIT_SEND_MESSAGE (message_string_p);
815-
JERRY_DEBUGGER_SET_SEND_MESSAGE_SIZE_FROM_TYPE (message_string_p, jerry_debugger_send_string_t);
816806
message_string_p->type = message_type;
817807

818808
while (string_length > max_fragment_len)
819809
{
820810
memcpy (message_string_p->string, string_p, max_fragment_len);
821811

822-
if (!jerry_debugger_send (sizeof (jerry_debugger_send_string_t)))
812+
if (!jerry_debugger_send (sizeof (jerry_debugger_send_type_t) + max_fragment_len))
823813
{
824814
return false;
825815
}
@@ -833,7 +823,6 @@ jerry_debugger_send_string (uint8_t message_type, /**< message type */
833823
string_length += 1;
834824
}
835825

836-
JERRY_DEBUGGER_SET_SEND_MESSAGE_SIZE (message_string_p, 1 + string_length);
837826
message_string_p->type = (uint8_t) (message_type + 1);
838827

839828
memcpy (message_string_p->string, string_p, string_length);
@@ -859,8 +848,6 @@ jerry_debugger_send_function_cp (jerry_debugger_header_type_t type, /**< message
859848

860849
JERRY_DEBUGGER_SEND_BUFFER_AS (jerry_debugger_send_byte_code_cp_t, byte_code_cp_p);
861850

862-
JERRY_DEBUGGER_INIT_SEND_MESSAGE (byte_code_cp_p);
863-
JERRY_DEBUGGER_SET_SEND_MESSAGE_SIZE_FROM_TYPE (byte_code_cp_p, jerry_debugger_send_byte_code_cp_t);
864851
byte_code_cp_p->type = (uint8_t) type;
865852

866853
jmem_cpointer_t compiled_code_cp;
@@ -884,8 +871,6 @@ jerry_debugger_send_parse_function (uint32_t line, /**< line */
884871

885872
JERRY_DEBUGGER_SEND_BUFFER_AS (jerry_debugger_send_parse_function_t, message_parse_function_p);
886873

887-
JERRY_DEBUGGER_INIT_SEND_MESSAGE (message_parse_function_p);
888-
JERRY_DEBUGGER_SET_SEND_MESSAGE_SIZE_FROM_TYPE (message_parse_function_p, jerry_debugger_send_parse_function_t);
889874
message_parse_function_p->type = JERRY_DEBUGGER_PARSE_FUNCTION;
890875
memcpy (message_parse_function_p->line, &line, sizeof (uint32_t));
891876
memcpy (message_parse_function_p->column, &column, sizeof (uint32_t));
@@ -902,8 +887,6 @@ jerry_debugger_send_memstats (void)
902887
JERRY_ASSERT (JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED);
903888

904889
JERRY_DEBUGGER_SEND_BUFFER_AS (jerry_debugger_send_memstats_t, memstats_p);
905-
JERRY_DEBUGGER_INIT_SEND_MESSAGE (memstats_p);
906-
JERRY_DEBUGGER_SET_SEND_MESSAGE_SIZE_FROM_TYPE (memstats_p, jerry_debugger_send_memstats_t);
907890

908891
memstats_p->type = JERRY_DEBUGGER_MEMSTATS_RECEIVE;
909892

0 commit comments

Comments
 (0)