Move low-level debugger connection handling into jerry-ext. (#2426)

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg 2018-07-19 03:14:43 +02:00 committed by yichoi
parent 88589902e2
commit 76ff084dc7
15 changed files with 686 additions and 520 deletions

View File

@ -16,8 +16,6 @@
#include "debugger.h"
#include "jcontext.h"
#include "jerryscript.h"
#include "debugger-tcp.h"
#include "debugger-ws.h"
/**
* Checks whether the debugger is connected.
@ -91,26 +89,6 @@ jerry_debugger_stop_at_breakpoint (bool enable_stop_at_breakpoint) /**< enable/d
#endif /* JERRY_DEBUGGER */
} /* jerry_debugger_stop_at_breakpoint */
/**
* Debugger server initialization. Must be called after jerry_init.
*/
void
jerry_debugger_init (uint16_t port) /**< server port number */
{
#ifdef JERRY_DEBUGGER
if (!jerry_debugger_tcp_create (port)
|| !jerry_debugger_ws_create ())
{
jerry_debugger_transport_close ();
return;
}
jerry_debugger_transport_start ();
#else /* !JERRY_DEBUGGER */
JERRY_UNUSED (port);
#endif /* JERRY_DEBUGGER */
} /* jerry_debugger_init */
/**
* Sets whether the engine should wait and run a source.
*

View File

@ -1,370 +0,0 @@
/* 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.
*/
/*
* FIPS-180-1 compliant SHA-1 implementation
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
/*
* The SHA-1 standard was published by NIST in 1993.
*
* http://www.itl.nist.gov/fipspubs/fip180-1.htm
*/
#include "debugger.h"
#ifdef JERRY_DEBUGGER
/**
* SHA-1 context structure.
*/
typedef struct
{
uint32_t total[2]; /**< number of bytes processed */
uint32_t state[5]; /**< intermediate digest state */
uint8_t buffer[64]; /**< data block being processed */
} jerry_sha1_context;
/* 32-bit integer manipulation macros (big endian). */
#define JERRY_SHA1_GET_UINT32_BE(n, b, i) \
{ \
(n) = (((uint32_t) (b)[(i) + 0]) << 24) \
| (((uint32_t) (b)[(i) + 1]) << 16) \
| (((uint32_t) (b)[(i) + 2]) << 8) \
| ((uint32_t) (b)[(i) + 3]); \
}
#define JERRY_SHA1_PUT_UINT32_BE(n, b, i) \
{ \
(b)[(i) + 0] = (uint8_t) ((n) >> 24); \
(b)[(i) + 1] = (uint8_t) ((n) >> 16); \
(b)[(i) + 2] = (uint8_t) ((n) >> 8); \
(b)[(i) + 3] = (uint8_t) ((n)); \
}
/**
* Initialize SHA-1 context.
*/
static void
jerry_sha1_init (jerry_sha1_context *sha1_context_p) /**< SHA-1 context */
{
memset (sha1_context_p, 0, sizeof (jerry_sha1_context));
sha1_context_p->total[0] = 0;
sha1_context_p->total[1] = 0;
sha1_context_p->state[0] = 0x67452301;
sha1_context_p->state[1] = 0xEFCDAB89;
sha1_context_p->state[2] = 0x98BADCFE;
sha1_context_p->state[3] = 0x10325476;
sha1_context_p->state[4] = 0xC3D2E1F0;
} /* jerry_sha1_init */
#define JERRY_SHA1_P(a, b, c, d, e, x) \
do { \
e += JERRY_SHA1_SHIFT (a, 5) + JERRY_SHA1_F (b, c, d) + K + x; \
b = JERRY_SHA1_SHIFT (b, 30); \
} while (0)
/**
* Update SHA-1 internal buffer status.
*/
static void
jerry_sha1_process (jerry_sha1_context *sha1_context_p, /**< SHA-1 context */
const uint8_t data[64]) /**< data buffer */
{
uint32_t temp, W[16], A, B, C, D, E;
JERRY_SHA1_GET_UINT32_BE (W[0], data, 0);
JERRY_SHA1_GET_UINT32_BE (W[1], data, 4);
JERRY_SHA1_GET_UINT32_BE (W[2], data, 8);
JERRY_SHA1_GET_UINT32_BE (W[3], data, 12);
JERRY_SHA1_GET_UINT32_BE (W[4], data, 16);
JERRY_SHA1_GET_UINT32_BE (W[5], data, 20);
JERRY_SHA1_GET_UINT32_BE (W[6], data, 24);
JERRY_SHA1_GET_UINT32_BE (W[7], data, 28);
JERRY_SHA1_GET_UINT32_BE (W[8], data, 32);
JERRY_SHA1_GET_UINT32_BE (W[9], data, 36);
JERRY_SHA1_GET_UINT32_BE (W[10], data, 40);
JERRY_SHA1_GET_UINT32_BE (W[11], data, 44);
JERRY_SHA1_GET_UINT32_BE (W[12], data, 48);
JERRY_SHA1_GET_UINT32_BE (W[13], data, 52);
JERRY_SHA1_GET_UINT32_BE (W[14], data, 56);
JERRY_SHA1_GET_UINT32_BE (W[15], data, 60);
#define JERRY_SHA1_SHIFT(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
#define JERRY_SHA1_R(t) \
( \
temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ W[(t - 14) & 0x0F] ^ W[t & 0x0F], \
W[t & 0x0F] = JERRY_SHA1_SHIFT (temp, 1) \
)
A = sha1_context_p->state[0];
B = sha1_context_p->state[1];
C = sha1_context_p->state[2];
D = sha1_context_p->state[3];
E = sha1_context_p->state[4];
uint32_t K = 0x5A827999;
#define JERRY_SHA1_F(x, y, z) (z ^ (x & (y ^ z)))
JERRY_SHA1_P (A, B, C, D, E, W[0]);
JERRY_SHA1_P (E, A, B, C, D, W[1]);
JERRY_SHA1_P (D, E, A, B, C, W[2]);
JERRY_SHA1_P (C, D, E, A, B, W[3]);
JERRY_SHA1_P (B, C, D, E, A, W[4]);
JERRY_SHA1_P (A, B, C, D, E, W[5]);
JERRY_SHA1_P (E, A, B, C, D, W[6]);
JERRY_SHA1_P (D, E, A, B, C, W[7]);
JERRY_SHA1_P (C, D, E, A, B, W[8]);
JERRY_SHA1_P (B, C, D, E, A, W[9]);
JERRY_SHA1_P (A, B, C, D, E, W[10]);
JERRY_SHA1_P (E, A, B, C, D, W[11]);
JERRY_SHA1_P (D, E, A, B, C, W[12]);
JERRY_SHA1_P (C, D, E, A, B, W[13]);
JERRY_SHA1_P (B, C, D, E, A, W[14]);
JERRY_SHA1_P (A, B, C, D, E, W[15]);
JERRY_SHA1_P (E, A, B, C, D, JERRY_SHA1_R (16));
JERRY_SHA1_P (D, E, A, B, C, JERRY_SHA1_R (17));
JERRY_SHA1_P (C, D, E, A, B, JERRY_SHA1_R (18));
JERRY_SHA1_P (B, C, D, E, A, JERRY_SHA1_R (19));
#undef JERRY_SHA1_F
K = 0x6ED9EBA1;
#define JERRY_SHA1_F(x, y, z) (x ^ y ^ z)
JERRY_SHA1_P (A, B, C, D, E, JERRY_SHA1_R (20));
JERRY_SHA1_P (E, A, B, C, D, JERRY_SHA1_R (21));
JERRY_SHA1_P (D, E, A, B, C, JERRY_SHA1_R (22));
JERRY_SHA1_P (C, D, E, A, B, JERRY_SHA1_R (23));
JERRY_SHA1_P (B, C, D, E, A, JERRY_SHA1_R (24));
JERRY_SHA1_P (A, B, C, D, E, JERRY_SHA1_R (25));
JERRY_SHA1_P (E, A, B, C, D, JERRY_SHA1_R (26));
JERRY_SHA1_P (D, E, A, B, C, JERRY_SHA1_R (27));
JERRY_SHA1_P (C, D, E, A, B, JERRY_SHA1_R (28));
JERRY_SHA1_P (B, C, D, E, A, JERRY_SHA1_R (29));
JERRY_SHA1_P (A, B, C, D, E, JERRY_SHA1_R (30));
JERRY_SHA1_P (E, A, B, C, D, JERRY_SHA1_R (31));
JERRY_SHA1_P (D, E, A, B, C, JERRY_SHA1_R (32));
JERRY_SHA1_P (C, D, E, A, B, JERRY_SHA1_R (33));
JERRY_SHA1_P (B, C, D, E, A, JERRY_SHA1_R (34));
JERRY_SHA1_P (A, B, C, D, E, JERRY_SHA1_R (35));
JERRY_SHA1_P (E, A, B, C, D, JERRY_SHA1_R (36));
JERRY_SHA1_P (D, E, A, B, C, JERRY_SHA1_R (37));
JERRY_SHA1_P (C, D, E, A, B, JERRY_SHA1_R (38));
JERRY_SHA1_P (B, C, D, E, A, JERRY_SHA1_R (39));
#undef JERRY_SHA1_F
K = 0x8F1BBCDC;
#define JERRY_SHA1_F(x, y, z) ((x & y) | (z & (x | y)))
JERRY_SHA1_P (A, B, C, D, E, JERRY_SHA1_R (40));
JERRY_SHA1_P (E, A, B, C, D, JERRY_SHA1_R (41));
JERRY_SHA1_P (D, E, A, B, C, JERRY_SHA1_R (42));
JERRY_SHA1_P (C, D, E, A, B, JERRY_SHA1_R (43));
JERRY_SHA1_P (B, C, D, E, A, JERRY_SHA1_R (44));
JERRY_SHA1_P (A, B, C, D, E, JERRY_SHA1_R (45));
JERRY_SHA1_P (E, A, B, C, D, JERRY_SHA1_R (46));
JERRY_SHA1_P (D, E, A, B, C, JERRY_SHA1_R (47));
JERRY_SHA1_P (C, D, E, A, B, JERRY_SHA1_R (48));
JERRY_SHA1_P (B, C, D, E, A, JERRY_SHA1_R (49));
JERRY_SHA1_P (A, B, C, D, E, JERRY_SHA1_R (50));
JERRY_SHA1_P (E, A, B, C, D, JERRY_SHA1_R (51));
JERRY_SHA1_P (D, E, A, B, C, JERRY_SHA1_R (52));
JERRY_SHA1_P (C, D, E, A, B, JERRY_SHA1_R (53));
JERRY_SHA1_P (B, C, D, E, A, JERRY_SHA1_R (54));
JERRY_SHA1_P (A, B, C, D, E, JERRY_SHA1_R (55));
JERRY_SHA1_P (E, A, B, C, D, JERRY_SHA1_R (56));
JERRY_SHA1_P (D, E, A, B, C, JERRY_SHA1_R (57));
JERRY_SHA1_P (C, D, E, A, B, JERRY_SHA1_R (58));
JERRY_SHA1_P (B, C, D, E, A, JERRY_SHA1_R (59));
#undef JERRY_SHA1_F
K = 0xCA62C1D6;
#define JERRY_SHA1_F(x, y, z) (x ^ y ^ z)
JERRY_SHA1_P (A, B, C, D, E, JERRY_SHA1_R (60));
JERRY_SHA1_P (E, A, B, C, D, JERRY_SHA1_R (61));
JERRY_SHA1_P (D, E, A, B, C, JERRY_SHA1_R (62));
JERRY_SHA1_P (C, D, E, A, B, JERRY_SHA1_R (63));
JERRY_SHA1_P (B, C, D, E, A, JERRY_SHA1_R (64));
JERRY_SHA1_P (A, B, C, D, E, JERRY_SHA1_R (65));
JERRY_SHA1_P (E, A, B, C, D, JERRY_SHA1_R (66));
JERRY_SHA1_P (D, E, A, B, C, JERRY_SHA1_R (67));
JERRY_SHA1_P (C, D, E, A, B, JERRY_SHA1_R (68));
JERRY_SHA1_P (B, C, D, E, A, JERRY_SHA1_R (69));
JERRY_SHA1_P (A, B, C, D, E, JERRY_SHA1_R (70));
JERRY_SHA1_P (E, A, B, C, D, JERRY_SHA1_R (71));
JERRY_SHA1_P (D, E, A, B, C, JERRY_SHA1_R (72));
JERRY_SHA1_P (C, D, E, A, B, JERRY_SHA1_R (73));
JERRY_SHA1_P (B, C, D, E, A, JERRY_SHA1_R (74));
JERRY_SHA1_P (A, B, C, D, E, JERRY_SHA1_R (75));
JERRY_SHA1_P (E, A, B, C, D, JERRY_SHA1_R (76));
JERRY_SHA1_P (D, E, A, B, C, JERRY_SHA1_R (77));
JERRY_SHA1_P (C, D, E, A, B, JERRY_SHA1_R (78));
JERRY_SHA1_P (B, C, D, E, A, JERRY_SHA1_R (79));
#undef JERRY_SHA1_F
sha1_context_p->state[0] += A;
sha1_context_p->state[1] += B;
sha1_context_p->state[2] += C;
sha1_context_p->state[3] += D;
sha1_context_p->state[4] += E;
#undef JERRY_SHA1_SHIFT
#undef JERRY_SHA1_R
} /* jerry_sha1_process */
#undef JERRY_SHA1_P
/**
* SHA-1 update buffer.
*/
static void
jerry_sha1_update (jerry_sha1_context *sha1_context_p, /**< SHA-1 context */
const uint8_t *source_p, /**< source buffer */
size_t source_length) /**< length of source buffer */
{
size_t fill;
uint32_t left;
if (source_length == 0)
{
return;
}
left = sha1_context_p->total[0] & 0x3F;
fill = 64 - left;
sha1_context_p->total[0] += (uint32_t) source_length;
/* Check overflow. */
if (sha1_context_p->total[0] < (uint32_t) source_length)
{
sha1_context_p->total[1]++;
}
if (left && source_length >= fill)
{
memcpy ((void *) (sha1_context_p->buffer + left), source_p, fill);
jerry_sha1_process (sha1_context_p, sha1_context_p->buffer);
source_p += fill;
source_length -= fill;
left = 0;
}
while (source_length >= 64)
{
jerry_sha1_process (sha1_context_p, source_p);
source_p += 64;
source_length -= 64;
}
if (source_length > 0)
{
memcpy ((void *) (sha1_context_p->buffer + left), source_p, source_length);
}
} /* jerry_sha1_update */
/**
* SHA-1 final digest.
*/
static void
jerry_sha1_finish (jerry_sha1_context *sha1_context_p, /**< SHA-1 context */
uint8_t destination_p[20]) /**< result */
{
uint8_t buffer[16];
uint32_t high = (sha1_context_p->total[0] >> 29) | (sha1_context_p->total[1] << 3);
uint32_t low = (sha1_context_p->total[0] << 3);
uint32_t last = sha1_context_p->total[0] & 0x3F;
uint32_t padn = (last < 56) ? (56 - last) : (120 - last);
memset (buffer, 0, sizeof (buffer));
buffer[0] = 0x80;
while (padn > sizeof (buffer))
{
jerry_sha1_update (sha1_context_p, buffer, sizeof (buffer));
buffer[0] = 0;
padn -= (uint32_t) sizeof (buffer);
}
jerry_sha1_update (sha1_context_p, buffer, padn);
JERRY_SHA1_PUT_UINT32_BE (high, buffer, 0);
JERRY_SHA1_PUT_UINT32_BE (low, buffer, 4);
jerry_sha1_update (sha1_context_p, buffer, 8);
JERRY_SHA1_PUT_UINT32_BE (sha1_context_p->state[0], destination_p, 0);
JERRY_SHA1_PUT_UINT32_BE (sha1_context_p->state[1], destination_p, 4);
JERRY_SHA1_PUT_UINT32_BE (sha1_context_p->state[2], destination_p, 8);
JERRY_SHA1_PUT_UINT32_BE (sha1_context_p->state[3], destination_p, 12);
JERRY_SHA1_PUT_UINT32_BE (sha1_context_p->state[4], destination_p, 16);
} /* jerry_sha1_finish */
#undef JERRY_SHA1_GET_UINT32_BE
#undef JERRY_SHA1_PUT_UINT32_BE
/**
* Computes the SHA-1 value of the combination of the two input buffers.
*/
void
jerry_debugger_compute_sha1 (const uint8_t *source1_p, /**< first part of the input */
size_t source1_length, /**< length of the first part */
const uint8_t *source2_p, /**< second part of the input */
size_t source2_length, /**< length of the second part */
uint8_t destination_p[20]) /**< result */
{
JMEM_DEFINE_LOCAL_ARRAY (sha1_context_p, 1, jerry_sha1_context);
jerry_sha1_init (sha1_context_p);
jerry_sha1_update (sha1_context_p, source1_p, source1_length);
jerry_sha1_update (sha1_context_p, source2_p, source2_length);
jerry_sha1_finish (sha1_context_p, destination_p);
JMEM_FINALIZE_LOCAL_ARRAY (sha1_context_p);
} /* jerry_debugger_compute_sha1 */
#endif /* JERRY_DEBUGGER */

View File

@ -16,7 +16,6 @@
#ifndef DEBUGGER_H
#define DEBUGGER_H
#include "debugger-ws.h"
#include "ecma-globals.h"
#include "jerryscript-debugger-transport.h"

View File

@ -53,7 +53,6 @@ typedef jerry_value_t (*jerry_debugger_wait_for_source_callback_t) (const jerry_
/**
* Engine debugger functions.
*/
void jerry_debugger_init (uint16_t port);
bool jerry_debugger_is_connected (void);
void jerry_debugger_stop (void);
void jerry_debugger_continue (void);

View File

@ -17,21 +17,25 @@ set(JERRY_EXT_NAME jerry-ext)
project (${JERRY_EXT_NAME} C)
# Include directories
set(INCLUDE_EXT "${CMAKE_CURRENT_SOURCE_DIR}/include")
set(INCLUDE_EXT "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_SOURCE_DIR}/common")
if(FEATURE_INIT_FINI)
set(DEFINES_EXT ${DEFINES_EXT} ENABLE_INIT_FINI)
endif()
# Source directories
file(GLOB SOURCE_EXT_ARG arg/*.c)
file(GLOB SOURCE_EXT_MODULE module/*.c)
file(GLOB SOURCE_EXT_HANDLER handler/*.c)
file(GLOB SOURCE_EXT_ARG arg/*.c)
file(GLOB SOURCE_EXT_COMMON common/*.c)
file(GLOB SOURCE_EXT_DEBUGGER debugger/*.c)
file(GLOB SOURCE_EXT_HANDLER handler/*.c)
file(GLOB SOURCE_EXT_MODULE module/*.c)
set(SOURCE_EXT
${SOURCE_EXT_ARG}
${SOURCE_EXT_MODULE}
${SOURCE_EXT_HANDLER})
${SOURCE_EXT_COMMON}
${SOURCE_EXT_DEBUGGER}
${SOURCE_EXT_HANDLER}
${SOURCE_EXT_MODULE})
add_library(${JERRY_EXT_NAME} ${SOURCE_EXT})

View File

@ -0,0 +1,101 @@
/* 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.
*/
#ifndef JEXT_COMMON_H
#define JEXT_COMMON_H
#if !defined (_XOPEN_SOURCE) || _XOPEN_SOURCE < 500
#undef _XOPEN_SOURCE
/* Required macro for sleep functions (nanosleep or usleep) */
#define _XOPEN_SOURCE 500
#endif
#include <stdio.h>
#include <string.h>
#include "jerryscript.h"
#include "jerryscript-port.h"
/*
* Make sure unused parameters, variables, or expressions trigger no compiler warning.
*/
#define JERRYX_UNUSED(x) ((void) (x))
/*
* Asserts
*
* Warning:
* Don't use JERRY_STATIC_ASSERT in headers, because
* __LINE__ may be the same for asserts in a header
* and in an implementation file.
*/
#define JERRYX_STATIC_ASSERT_GLUE_(a, b, c) a ## b ## _ ## c
#define JERRYX_STATIC_ASSERT_GLUE(a, b, c) JERRYX_STATIC_ASSERT_GLUE_ (a, b, c)
#define JERRYX_STATIC_ASSERT(x, msg) \
enum { JERRYX_STATIC_ASSERT_GLUE (static_assertion_failed_, __LINE__, msg) = 1 / (!!(x)) }
#ifndef JERRY_NDEBUG
void JERRY_ATTR_NORETURN
jerry_assert_fail (const char *assertion, const char *file, const char *function, const uint32_t line);
void JERRY_ATTR_NORETURN
jerry_unreachable (const char *file, const char *function, const uint32_t line);
#define JERRYX_ASSERT(x) \
do \
{ \
if (JERRY_UNLIKELY (!(x))) \
{ \
jerry_assert_fail (#x, __FILE__, __func__, __LINE__); \
} \
} while (0)
#define JERRYX_UNREACHABLE() \
do \
{ \
jerry_unreachable (__FILE__, __func__, __LINE__); \
} while (0)
#else /* JERRY_NDEBUG */
#define JERRYX_ASSERT(x) \
do \
{ \
if (false) \
{ \
JERRYX_UNUSED (x); \
} \
} while (0)
#ifdef __GNUC__
#define JERRYX_UNREACHABLE() __builtin_unreachable ()
#endif /* __GNUC__ */
#ifdef _MSC_VER
#define JERRYX_UNREACHABLE() _assume (0)
#endif /* _MSC_VER */
#ifndef JERRYX_UNREACHABLE
#define JERRYX_UNREACHABLE()
#endif /* !JERRYX_UNREACHABLE */
#endif /* !JERRY_NDEBUG */
/*
* Logging
*/
#define JERRYX_ERROR_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_ERROR, __VA_ARGS__)
#define JERRYX_WARNING_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_WARNING, __VA_ARGS__)
#define JERRYX_DEBUG_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_DEBUG, __VA_ARGS__)
#define JERRYX_TRACE_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_TRACE, __VA_ARGS__)
#endif /* !JEXT_COMMON_H */

View File

@ -0,0 +1,38 @@
/* 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.
*/
#include "jerryscript-ext/debugger.h"
#include "jext-common.h"
/**
* Must be called after the connection has been initialized.
*/
void
jerryx_debugger_after_connect (bool success) /**< tells whether the connection
* has been successfully established */
{
#ifdef JERRY_DEBUGGER
if (success)
{
jerry_debugger_transport_start ();
}
else
{
jerry_debugger_transport_close ();
}
#else /* !JERRY_DEBUGGER */
JERRYX_UNUSED (success);
#endif /* JERRY_DEBUGGER */
} /* jerryx_debugger_after_connect */

View File

@ -0,0 +1,369 @@
/* 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.
*/
/*
* FIPS-180-1 compliant SHA-1 implementation
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
/*
* The SHA-1 standard was published by NIST in 1993.
*
* http://www.itl.nist.gov/fipspubs/fip180-1.htm
*/
#include "debugger-ws.h"
#include "jext-common.h"
#ifdef JERRY_DEBUGGER
/**
* SHA-1 context structure.
*/
typedef struct
{
uint32_t total[2]; /**< number of bytes processed */
uint32_t state[5]; /**< intermediate digest state */
uint8_t buffer[64]; /**< data block being processed */
} jerryx_sha1_context;
/* 32-bit integer manipulation macros (big endian). */
#define JERRYX_SHA1_GET_UINT32_BE(n, b, i) \
{ \
(n) = (((uint32_t) (b)[(i) + 0]) << 24) \
| (((uint32_t) (b)[(i) + 1]) << 16) \
| (((uint32_t) (b)[(i) + 2]) << 8) \
| ((uint32_t) (b)[(i) + 3]); \
}
#define JERRYX_SHA1_PUT_UINT32_BE(n, b, i) \
{ \
(b)[(i) + 0] = (uint8_t) ((n) >> 24); \
(b)[(i) + 1] = (uint8_t) ((n) >> 16); \
(b)[(i) + 2] = (uint8_t) ((n) >> 8); \
(b)[(i) + 3] = (uint8_t) ((n)); \
}
/**
* Initialize SHA-1 context.
*/
static void
jerryx_sha1_init (jerryx_sha1_context *sha1_context_p) /**< SHA-1 context */
{
memset (sha1_context_p, 0, sizeof (jerryx_sha1_context));
sha1_context_p->total[0] = 0;
sha1_context_p->total[1] = 0;
sha1_context_p->state[0] = 0x67452301;
sha1_context_p->state[1] = 0xEFCDAB89;
sha1_context_p->state[2] = 0x98BADCFE;
sha1_context_p->state[3] = 0x10325476;
sha1_context_p->state[4] = 0xC3D2E1F0;
} /* jerryx_sha1_init */
#define JERRYX_SHA1_P(a, b, c, d, e, x) \
do { \
e += JERRYX_SHA1_SHIFT (a, 5) + JERRYX_SHA1_F (b, c, d) + K + x; \
b = JERRYX_SHA1_SHIFT (b, 30); \
} while (0)
/**
* Update SHA-1 internal buffer status.
*/
static void
jerryx_sha1_process (jerryx_sha1_context *sha1_context_p, /**< SHA-1 context */
const uint8_t data[64]) /**< data buffer */
{
uint32_t temp, W[16], A, B, C, D, E;
JERRYX_SHA1_GET_UINT32_BE (W[0], data, 0);
JERRYX_SHA1_GET_UINT32_BE (W[1], data, 4);
JERRYX_SHA1_GET_UINT32_BE (W[2], data, 8);
JERRYX_SHA1_GET_UINT32_BE (W[3], data, 12);
JERRYX_SHA1_GET_UINT32_BE (W[4], data, 16);
JERRYX_SHA1_GET_UINT32_BE (W[5], data, 20);
JERRYX_SHA1_GET_UINT32_BE (W[6], data, 24);
JERRYX_SHA1_GET_UINT32_BE (W[7], data, 28);
JERRYX_SHA1_GET_UINT32_BE (W[8], data, 32);
JERRYX_SHA1_GET_UINT32_BE (W[9], data, 36);
JERRYX_SHA1_GET_UINT32_BE (W[10], data, 40);
JERRYX_SHA1_GET_UINT32_BE (W[11], data, 44);
JERRYX_SHA1_GET_UINT32_BE (W[12], data, 48);
JERRYX_SHA1_GET_UINT32_BE (W[13], data, 52);
JERRYX_SHA1_GET_UINT32_BE (W[14], data, 56);
JERRYX_SHA1_GET_UINT32_BE (W[15], data, 60);
#define JERRYX_SHA1_SHIFT(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
#define JERRYX_SHA1_R(t) \
( \
temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ W[(t - 14) & 0x0F] ^ W[t & 0x0F], \
W[t & 0x0F] = JERRYX_SHA1_SHIFT (temp, 1) \
)
A = sha1_context_p->state[0];
B = sha1_context_p->state[1];
C = sha1_context_p->state[2];
D = sha1_context_p->state[3];
E = sha1_context_p->state[4];
uint32_t K = 0x5A827999;
#define JERRYX_SHA1_F(x, y, z) (z ^ (x & (y ^ z)))
JERRYX_SHA1_P (A, B, C, D, E, W[0]);
JERRYX_SHA1_P (E, A, B, C, D, W[1]);
JERRYX_SHA1_P (D, E, A, B, C, W[2]);
JERRYX_SHA1_P (C, D, E, A, B, W[3]);
JERRYX_SHA1_P (B, C, D, E, A, W[4]);
JERRYX_SHA1_P (A, B, C, D, E, W[5]);
JERRYX_SHA1_P (E, A, B, C, D, W[6]);
JERRYX_SHA1_P (D, E, A, B, C, W[7]);
JERRYX_SHA1_P (C, D, E, A, B, W[8]);
JERRYX_SHA1_P (B, C, D, E, A, W[9]);
JERRYX_SHA1_P (A, B, C, D, E, W[10]);
JERRYX_SHA1_P (E, A, B, C, D, W[11]);
JERRYX_SHA1_P (D, E, A, B, C, W[12]);
JERRYX_SHA1_P (C, D, E, A, B, W[13]);
JERRYX_SHA1_P (B, C, D, E, A, W[14]);
JERRYX_SHA1_P (A, B, C, D, E, W[15]);
JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (16));
JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (17));
JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (18));
JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (19));
#undef JERRYX_SHA1_F
K = 0x6ED9EBA1;
#define JERRYX_SHA1_F(x, y, z) (x ^ y ^ z)
JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (20));
JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (21));
JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (22));
JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (23));
JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (24));
JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (25));
JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (26));
JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (27));
JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (28));
JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (29));
JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (30));
JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (31));
JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (32));
JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (33));
JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (34));
JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (35));
JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (36));
JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (37));
JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (38));
JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (39));
#undef JERRYX_SHA1_F
K = 0x8F1BBCDC;
#define JERRYX_SHA1_F(x, y, z) ((x & y) | (z & (x | y)))
JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (40));
JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (41));
JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (42));
JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (43));
JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (44));
JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (45));
JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (46));
JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (47));
JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (48));
JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (49));
JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (50));
JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (51));
JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (52));
JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (53));
JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (54));
JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (55));
JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (56));
JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (57));
JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (58));
JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (59));
#undef JERRYX_SHA1_F
K = 0xCA62C1D6;
#define JERRYX_SHA1_F(x, y, z) (x ^ y ^ z)
JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (60));
JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (61));
JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (62));
JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (63));
JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (64));
JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (65));
JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (66));
JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (67));
JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (68));
JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (69));
JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (70));
JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (71));
JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (72));
JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (73));
JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (74));
JERRYX_SHA1_P (A, B, C, D, E, JERRYX_SHA1_R (75));
JERRYX_SHA1_P (E, A, B, C, D, JERRYX_SHA1_R (76));
JERRYX_SHA1_P (D, E, A, B, C, JERRYX_SHA1_R (77));
JERRYX_SHA1_P (C, D, E, A, B, JERRYX_SHA1_R (78));
JERRYX_SHA1_P (B, C, D, E, A, JERRYX_SHA1_R (79));
#undef JERRYX_SHA1_F
sha1_context_p->state[0] += A;
sha1_context_p->state[1] += B;
sha1_context_p->state[2] += C;
sha1_context_p->state[3] += D;
sha1_context_p->state[4] += E;
#undef JERRYX_SHA1_SHIFT
#undef JERRYX_SHA1_R
} /* jerryx_sha1_process */
#undef JERRYX_SHA1_P
/**
* SHA-1 update buffer.
*/
static void
jerryx_sha1_update (jerryx_sha1_context *sha1_context_p, /**< SHA-1 context */
const uint8_t *source_p, /**< source buffer */
size_t source_length) /**< length of source buffer */
{
size_t fill;
uint32_t left;
if (source_length == 0)
{
return;
}
left = sha1_context_p->total[0] & 0x3F;
fill = 64 - left;
sha1_context_p->total[0] += (uint32_t) source_length;
/* Check overflow. */
if (sha1_context_p->total[0] < (uint32_t) source_length)
{
sha1_context_p->total[1]++;
}
if (left && source_length >= fill)
{
memcpy ((void *) (sha1_context_p->buffer + left), source_p, fill);
jerryx_sha1_process (sha1_context_p, sha1_context_p->buffer);
source_p += fill;
source_length -= fill;
left = 0;
}
while (source_length >= 64)
{
jerryx_sha1_process (sha1_context_p, source_p);
source_p += 64;
source_length -= 64;
}
if (source_length > 0)
{
memcpy ((void *) (sha1_context_p->buffer + left), source_p, source_length);
}
} /* jerryx_sha1_update */
/**
* SHA-1 final digest.
*/
static void
jerryx_sha1_finish (jerryx_sha1_context *sha1_context_p, /**< SHA-1 context */
uint8_t destination_p[20]) /**< result */
{
uint8_t buffer[16];
uint32_t high = (sha1_context_p->total[0] >> 29) | (sha1_context_p->total[1] << 3);
uint32_t low = (sha1_context_p->total[0] << 3);
uint32_t last = sha1_context_p->total[0] & 0x3F;
uint32_t padn = (last < 56) ? (56 - last) : (120 - last);
memset (buffer, 0, sizeof (buffer));
buffer[0] = 0x80;
while (padn > sizeof (buffer))
{
jerryx_sha1_update (sha1_context_p, buffer, sizeof (buffer));
buffer[0] = 0;
padn -= (uint32_t) sizeof (buffer);
}
jerryx_sha1_update (sha1_context_p, buffer, padn);
JERRYX_SHA1_PUT_UINT32_BE (high, buffer, 0);
JERRYX_SHA1_PUT_UINT32_BE (low, buffer, 4);
jerryx_sha1_update (sha1_context_p, buffer, 8);
JERRYX_SHA1_PUT_UINT32_BE (sha1_context_p->state[0], destination_p, 0);
JERRYX_SHA1_PUT_UINT32_BE (sha1_context_p->state[1], destination_p, 4);
JERRYX_SHA1_PUT_UINT32_BE (sha1_context_p->state[2], destination_p, 8);
JERRYX_SHA1_PUT_UINT32_BE (sha1_context_p->state[3], destination_p, 12);
JERRYX_SHA1_PUT_UINT32_BE (sha1_context_p->state[4], destination_p, 16);
} /* jerryx_sha1_finish */
#undef JERRYX_SHA1_GET_UINT32_BE
#undef JERRYX_SHA1_PUT_UINT32_BE
/**
* Computes the SHA-1 value of the combination of the two input buffers.
*/
void
jerryx_debugger_compute_sha1 (const uint8_t *source1_p, /**< first part of the input */
size_t source1_length, /**< length of the first part */
const uint8_t *source2_p, /**< second part of the input */
size_t source2_length, /**< length of the second part */
uint8_t destination_p[20]) /**< result */
{
jerryx_sha1_context sha1_context;
jerryx_sha1_init (&sha1_context);
jerryx_sha1_update (&sha1_context, source1_p, source1_length);
jerryx_sha1_update (&sha1_context, source2_p, source2_length);
jerryx_sha1_finish (&sha1_context, destination_p);
} /* jerryx_debugger_compute_sha1 */
#endif /* JERRY_DEBUGGER */

View File

@ -13,8 +13,9 @@
* limitations under the License.
*/
#include "debugger-tcp.h"
#include "jrt.h"
#include "jerryscript-debugger-transport.h"
#include "jerryscript-ext/debugger.h"
#include "jext-common.h"
#ifdef JERRY_DEBUGGER
@ -30,33 +31,33 @@ typedef struct
{
jerry_debugger_transport_header_t header; /**< transport header */
int tcp_socket; /**< tcp socket */
} jerry_debugger_transport_tcp_t;
} jerryx_debugger_transport_tcp_t;
/**
* Log tcp error message.
*/
static void
jerry_debugger_tcp_log_error (void)
jerryx_debugger_tcp_log_error (void)
{
JERRY_ERROR_MSG ("Error: %s\n", strerror (errno));
} /* jerry_debugger_tcp_log_error */
JERRYX_ERROR_MSG ("Error: %s\n", strerror (errno));
} /* jerryx_debugger_tcp_log_error */
/**
* Close a tcp connection.
*/
static void
jerry_debugger_tcp_close (jerry_debugger_transport_header_t *header_p) /**< tcp implementation */
jerryx_debugger_tcp_close (jerry_debugger_transport_header_t *header_p) /**< tcp implementation */
{
JERRY_ASSERT (jerry_debugger_transport_is_connected ());
JERRYX_ASSERT (jerry_debugger_transport_is_connected ());
jerry_debugger_transport_tcp_t *tcp_p = (jerry_debugger_transport_tcp_t *) header_p;
jerryx_debugger_transport_tcp_t *tcp_p = (jerryx_debugger_transport_tcp_t *) header_p;
JERRY_DEBUG_MSG ("TCP connection closed.\n");
JERRYX_DEBUG_MSG ("TCP connection closed.\n");
close (tcp_p->tcp_socket);
jerry_debugger_transport_free ((void *) header_p, sizeof (jerry_debugger_transport_tcp_t));
} /* jerry_debugger_tcp_close */
jerry_debugger_transport_free ((void *) header_p, sizeof (jerryx_debugger_transport_tcp_t));
} /* jerryx_debugger_tcp_close */
/**
* Send data over a tcp connection.
@ -65,13 +66,13 @@ jerry_debugger_tcp_close (jerry_debugger_transport_header_t *header_p) /**< tcp
* false - otherwise
*/
static bool
jerry_debugger_tcp_send (jerry_debugger_transport_header_t *header_p, /**< tcp implementation */
uint8_t *message_p, /**< message to be sent */
size_t message_length) /**< message length in bytes */
jerryx_debugger_tcp_send (jerry_debugger_transport_header_t *header_p, /**< tcp implementation */
uint8_t *message_p, /**< message to be sent */
size_t message_length) /**< message length in bytes */
{
JERRY_ASSERT (jerry_debugger_transport_is_connected ());
JERRYX_ASSERT (jerry_debugger_transport_is_connected ());
jerry_debugger_transport_tcp_t *tcp_p = (jerry_debugger_transport_tcp_t *) header_p;
jerryx_debugger_transport_tcp_t *tcp_p = (jerryx_debugger_transport_tcp_t *) header_p;
do
{
@ -84,7 +85,7 @@ jerry_debugger_tcp_send (jerry_debugger_transport_header_t *header_p, /**< tcp i
continue;
}
jerry_debugger_tcp_log_error ();
jerryx_debugger_tcp_log_error ();
jerry_debugger_transport_close ();
return false;
}
@ -95,16 +96,16 @@ jerry_debugger_tcp_send (jerry_debugger_transport_header_t *header_p, /**< tcp i
while (message_length > 0);
return true;
} /* jerry_debugger_tcp_send */
} /* jerryx_debugger_tcp_send */
/**
* Receive data from a tcp connection.
*/
static bool
jerry_debugger_tcp_receive (jerry_debugger_transport_header_t *header_p, /**< tcp implementation */
jerry_debugger_transport_receive_context_t *receive_context_p) /**< receive context */
jerryx_debugger_tcp_receive (jerry_debugger_transport_header_t *header_p, /**< tcp implementation */
jerry_debugger_transport_receive_context_t *receive_context_p) /**< receive context */
{
jerry_debugger_transport_tcp_t *tcp_p = (jerry_debugger_transport_tcp_t *) header_p;
jerryx_debugger_transport_tcp_t *tcp_p = (jerryx_debugger_transport_tcp_t *) header_p;
uint8_t *buffer_p = receive_context_p->buffer_p + receive_context_p->received_length;
size_t buffer_size = JERRY_DEBUGGER_TRANSPORT_MAX_BUFFER_SIZE - receive_context_p->received_length;
@ -115,7 +116,7 @@ jerry_debugger_tcp_receive (jerry_debugger_transport_header_t *header_p, /**< tc
{
if (errno != EWOULDBLOCK)
{
jerry_debugger_tcp_log_error ();
jerryx_debugger_tcp_log_error ();
jerry_debugger_transport_close ();
return false;
}
@ -131,7 +132,7 @@ jerry_debugger_tcp_receive (jerry_debugger_transport_header_t *header_p, /**< tc
}
return true;
} /* jerry_debugger_tcp_receive */
} /* jerryx_debugger_tcp_receive */
/**
* Create a tcp connection.
@ -140,7 +141,7 @@ jerry_debugger_tcp_receive (jerry_debugger_transport_header_t *header_p, /**< tc
* false otherwise
*/
bool
jerry_debugger_tcp_create (uint16_t port) /**< listening port */
jerryx_debugger_tcp_create (uint16_t port) /**< listening port */
{
int server_socket;
struct sockaddr_in addr;
@ -152,7 +153,7 @@ jerry_debugger_tcp_create (uint16_t port) /**< listening port */
if ((server_socket = socket (AF_INET, SOCK_STREAM, 0)) == -1)
{
JERRY_ERROR_MSG ("Error: %s\n", strerror (errno));
JERRYX_ERROR_MSG ("Error: %s\n", strerror (errno));
return false;
}
@ -161,25 +162,25 @@ jerry_debugger_tcp_create (uint16_t port) /**< listening port */
if (setsockopt (server_socket, SOL_SOCKET, SO_REUSEADDR, &opt_value, sizeof (int)) == -1)
{
close (server_socket);
JERRY_ERROR_MSG ("Error: %s\n", strerror (errno));
JERRYX_ERROR_MSG ("Error: %s\n", strerror (errno));
return false;
}
if (bind (server_socket, (struct sockaddr *)&addr, sizeof (struct sockaddr)) == -1)
{
close (server_socket);
JERRY_ERROR_MSG ("Error: %s\n", strerror (errno));
JERRYX_ERROR_MSG ("Error: %s\n", strerror (errno));
return false;
}
if (listen (server_socket, 1) == -1)
{
close (server_socket);
JERRY_ERROR_MSG ("Error: %s\n", strerror (errno));
JERRYX_ERROR_MSG ("Error: %s\n", strerror (errno));
return false;
}
JERRY_DEBUG_MSG ("Waiting for client connection\n");
JERRYX_DEBUG_MSG ("Waiting for client connection\n");
int tcp_socket = accept (server_socket, (struct sockaddr *)&addr, &sin_size);
@ -187,7 +188,7 @@ jerry_debugger_tcp_create (uint16_t port) /**< listening port */
if (tcp_socket == -1)
{
JERRY_ERROR_MSG ("Error: %s\n", strerror (errno));
JERRYX_ERROR_MSG ("Error: %s\n", strerror (errno));
return false;
}
@ -206,9 +207,9 @@ jerry_debugger_tcp_create (uint16_t port) /**< listening port */
return false;
}
JERRY_DEBUG_MSG ("Connected from: %s\n", inet_ntoa (addr.sin_addr));
JERRYX_DEBUG_MSG ("Connected from: %s\n", inet_ntoa (addr.sin_addr));
size_t size = sizeof (jerry_debugger_transport_tcp_t);
size_t size = sizeof (jerryx_debugger_transport_tcp_t);
jerry_debugger_transport_header_t *header_p;
header_p = (jerry_debugger_transport_header_t *) jerry_debugger_transport_malloc (size);
@ -219,11 +220,11 @@ jerry_debugger_tcp_create (uint16_t port) /**< listening port */
return false;
}
header_p->close = jerry_debugger_tcp_close;
header_p->send = jerry_debugger_tcp_send;
header_p->receive = jerry_debugger_tcp_receive;
header_p->close = jerryx_debugger_tcp_close;
header_p->send = jerryx_debugger_tcp_send;
header_p->receive = jerryx_debugger_tcp_receive;
((jerry_debugger_transport_tcp_t *) header_p)->tcp_socket = tcp_socket;
((jerryx_debugger_transport_tcp_t *) header_p)->tcp_socket = tcp_socket;
jerry_debugger_transport_add (header_p,
0,
@ -232,6 +233,20 @@ jerry_debugger_tcp_create (uint16_t port) /**< listening port */
JERRY_DEBUGGER_TRANSPORT_MAX_BUFFER_SIZE);
return true;
} /* jerry_debugger_tcp_create */
} /* jerryx_debugger_tcp_create */
#else /* !JERRY_DEBUGGER */
/**
* Dummy function when debugger is disabled.
*
* @return false
*/
bool
jerryx_debugger_tcp_create (uint16_t port)
{
JERRYX_UNUSED (port);
return false;
} /* jerryx_debugger_tcp_create */
#endif /* JERRY_DEBUGGER */

View File

@ -14,7 +14,8 @@
*/
#include "debugger-ws.h"
#include "jrt.h"
#include "jerryscript-ext/debugger.h"
#include "jext-common.h"
#ifdef JERRY_DEBUGGER
@ -23,49 +24,49 @@
/**
* Last fragment of a Websocket package.
*/
#define JERRY_DEBUGGER_WEBSOCKET_FIN_BIT 0x80
#define JERRYX_DEBUGGER_WEBSOCKET_FIN_BIT 0x80
/**
* Masking-key is available.
*/
#define JERRY_DEBUGGER_WEBSOCKET_MASK_BIT 0x80
#define JERRYX_DEBUGGER_WEBSOCKET_MASK_BIT 0x80
/**
* Opcode type mask.
*/
#define JERRY_DEBUGGER_WEBSOCKET_OPCODE_MASK 0x0fu
#define JERRYX_DEBUGGER_WEBSOCKET_OPCODE_MASK 0x0fu
/**
* Packet length mask.
*/
#define JERRY_DEBUGGER_WEBSOCKET_LENGTH_MASK 0x7fu
#define JERRYX_DEBUGGER_WEBSOCKET_LENGTH_MASK 0x7fu
/**
* Size of websocket header size.
*/
#define JERRY_DEBUGGER_WEBSOCKET_HEADER_SIZE 2
#define JERRYX_DEBUGGER_WEBSOCKET_HEADER_SIZE 2
/**
* Payload mask size in bytes of a websocket package.
*/
#define JERRY_DEBUGGER_WEBSOCKET_MASK_SIZE 4
#define JERRYX_DEBUGGER_WEBSOCKET_MASK_SIZE 4
/**
* Maximum message size with 1 byte size field.
*/
#define JERRY_DEBUGGER_WEBSOCKET_ONE_BYTE_LEN_MAX 125
#define JERRYX_DEBUGGER_WEBSOCKET_ONE_BYTE_LEN_MAX 125
/**
* WebSocket opcode types.
*/
typedef enum
{
JERRY_DEBUGGER_WEBSOCKET_TEXT_FRAME = 1, /**< text frame */
JERRY_DEBUGGER_WEBSOCKET_BINARY_FRAME = 2, /**< binary frame */
JERRY_DEBUGGER_WEBSOCKET_CLOSE_CONNECTION = 8, /**< close connection */
JERRY_DEBUGGER_WEBSOCKET_PING = 9, /**< ping (keep alive) frame */
JERRY_DEBUGGER_WEBSOCKET_PONG = 10, /**< reply to ping frame */
} jerry_websocket_opcode_type_t;
JERRYX_DEBUGGER_WEBSOCKET_TEXT_FRAME = 1, /**< text frame */
JERRYX_DEBUGGER_WEBSOCKET_BINARY_FRAME = 2, /**< binary frame */
JERRYX_DEBUGGER_WEBSOCKET_CLOSE_CONNECTION = 8, /**< close connection */
JERRYX_DEBUGGER_WEBSOCKET_PING = 9, /**< ping (keep alive) frame */
JERRYX_DEBUGGER_WEBSOCKET_PONG = 10, /**< reply to ping frame */
} jerryx_websocket_opcode_type_t;
/**
* Header for incoming packets.
@ -75,7 +76,7 @@ typedef struct
uint8_t ws_opcode; /**< websocket opcode */
uint8_t size; /**< size of the message */
uint8_t mask[4]; /**< mask bytes */
} jerry_websocket_receive_header_t;
} jerryx_websocket_receive_header_t;
/**
* Convert a 6-bit value to a Base64 character.
@ -83,7 +84,7 @@ typedef struct
* @return Base64 character
*/
static uint8_t
jerry_to_base64_character (uint8_t value) /**< 6-bit value */
jerryx_to_base64_character (uint8_t value) /**< 6-bit value */
{
if (value < 26)
{
@ -106,35 +107,35 @@ jerry_to_base64_character (uint8_t value) /**< 6-bit value */
}
return (uint8_t) '/';
} /* jerry_to_base64_character */
} /* jerryx_to_base64_character */
/**
* Encode a byte sequence into Base64 string.
*/
static void
jerry_to_base64 (const uint8_t *source_p, /**< source data */
jerryx_to_base64 (const uint8_t *source_p, /**< source data */
uint8_t *destination_p, /**< destination buffer */
size_t length) /**< length of source, must be divisible by 3 */
{
while (length >= 3)
{
uint8_t value = (source_p[0] >> 2);
destination_p[0] = jerry_to_base64_character (value);
destination_p[0] = jerryx_to_base64_character (value);
value = (uint8_t) (((source_p[0] << 4) | (source_p[1] >> 4)) & 0x3f);
destination_p[1] = jerry_to_base64_character (value);
destination_p[1] = jerryx_to_base64_character (value);
value = (uint8_t) (((source_p[1] << 2) | (source_p[2] >> 6)) & 0x3f);
destination_p[2] = jerry_to_base64_character (value);
destination_p[2] = jerryx_to_base64_character (value);
value = (uint8_t) (source_p[2] & 0x3f);
destination_p[3] = jerry_to_base64_character (value);
destination_p[3] = jerryx_to_base64_character (value);
source_p += 3;
destination_p += 4;
length -= 3;
}
} /* jerry_to_base64 */
} /* jerryx_to_base64 */
/**
* Process WebSocket handshake.
@ -143,7 +144,7 @@ jerry_to_base64 (const uint8_t *source_p, /**< source data */
* false - otherwise
*/
static bool
jerry_process_handshake (uint8_t *request_buffer_p) /**< temporary buffer */
jerryx_process_handshake (uint8_t *request_buffer_p) /**< temporary buffer */
{
size_t request_buffer_size = 1024;
uint8_t *request_end_p = request_buffer_p;
@ -154,7 +155,7 @@ jerry_process_handshake (uint8_t *request_buffer_p) /**< temporary buffer */
jerry_debugger_transport_receive_context_t context;
if (!jerry_debugger_transport_receive (&context))
{
JERRY_ASSERT (!jerry_debugger_transport_is_connected ());
JERRYX_ASSERT (!jerry_debugger_transport_is_connected ());
return false;
}
@ -168,7 +169,7 @@ jerry_process_handshake (uint8_t *request_buffer_p) /**< temporary buffer */
if (length < context.message_length)
{
JERRY_ERROR_MSG ("Handshake buffer too small.\n");
JERRYX_ERROR_MSG ("Handshake buffer too small.\n");
return false;
}
@ -194,7 +195,7 @@ jerry_process_handshake (uint8_t *request_buffer_p) /**< temporary buffer */
if ((size_t) (request_end_p - request_buffer_p) < text_len
|| memcmp (request_buffer_p, text_p, text_len) != 0)
{
JERRY_ERROR_MSG ("Invalid handshake format.\n");
JERRYX_ERROR_MSG ("Invalid handshake format.\n");
return false;
}
@ -207,7 +208,7 @@ jerry_process_handshake (uint8_t *request_buffer_p) /**< temporary buffer */
{
if ((size_t) (request_end_p - websocket_key_p) < text_len)
{
JERRY_ERROR_MSG ("Sec-WebSocket-Key not found.\n");
JERRYX_ERROR_MSG ("Sec-WebSocket-Key not found.\n");
return false;
}
@ -242,17 +243,17 @@ jerry_process_handshake (uint8_t *request_buffer_p) /**< temporary buffer */
const size_t sha1_length = 20;
jerry_debugger_compute_sha1 (websocket_key_p,
jerryx_debugger_compute_sha1 (websocket_key_p,
(size_t) (websocket_key_end_p - websocket_key_p),
(const uint8_t *) "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
36,
request_buffer_p);
/* The SHA-1 key is 20 bytes long but jerry_to_base64 expects
/* The SHA-1 key is 20 bytes long but jerryx_to_base64 expects
* a length divisible by 3 so an extra 0 is appended at the end. */
request_buffer_p[sha1_length] = 0;
jerry_to_base64 (request_buffer_p, request_buffer_p + sha1_length + 1, sha1_length + 1);
jerryx_to_base64 (request_buffer_p, request_buffer_p + sha1_length + 1, sha1_length + 1);
/* Last value must be replaced by equal sign. */
@ -266,18 +267,18 @@ jerry_process_handshake (uint8_t *request_buffer_p) /**< temporary buffer */
text_p = "=\r\n\r\n";
return jerry_debugger_transport_send ((const uint8_t *) text_p, strlen (text_p));
} /* jerry_process_handshake */
} /* jerryx_process_handshake */
/**
* Close a tcp connection.
*/
static void
jerry_debugger_ws_close (jerry_debugger_transport_header_t *header_p) /**< tcp implementation */
jerryx_debugger_ws_close (jerry_debugger_transport_header_t *header_p) /**< tcp implementation */
{
JERRY_ASSERT (jerry_debugger_transport_is_connected ());
JERRYX_ASSERT (jerry_debugger_transport_is_connected ());
jerry_debugger_transport_free ((void *) header_p, sizeof (jerry_debugger_transport_header_t));
} /* jerry_debugger_ws_close */
} /* jerryx_debugger_ws_close */
/**
* Send data over a websocket connection.
@ -286,24 +287,24 @@ jerry_debugger_ws_close (jerry_debugger_transport_header_t *header_p) /**< tcp i
* false - otherwise
*/
static bool
jerry_debugger_ws_send (jerry_debugger_transport_header_t *header_p, /**< tcp implementation */
uint8_t *message_p, /**< message to be sent */
size_t message_length) /**< message length in bytes */
jerryx_debugger_ws_send (jerry_debugger_transport_header_t *header_p, /**< tcp implementation */
uint8_t *message_p, /**< message to be sent */
size_t message_length) /**< message length in bytes */
{
JERRY_ASSERT (message_length <= JERRY_DEBUGGER_WEBSOCKET_ONE_BYTE_LEN_MAX);
JERRYX_ASSERT (message_length <= JERRYX_DEBUGGER_WEBSOCKET_ONE_BYTE_LEN_MAX);
message_p[-2] = JERRY_DEBUGGER_WEBSOCKET_FIN_BIT | JERRY_DEBUGGER_WEBSOCKET_BINARY_FRAME;
message_p[-2] = JERRYX_DEBUGGER_WEBSOCKET_FIN_BIT | JERRYX_DEBUGGER_WEBSOCKET_BINARY_FRAME;
message_p[-1] = (uint8_t) message_length;
return header_p->next_p->send (header_p->next_p, message_p - 2, message_length + 2);
} /* jerry_debugger_ws_send */
} /* jerryx_debugger_ws_send */
/**
* Receive data from a websocket connection.
*/
static bool
jerry_debugger_ws_receive (jerry_debugger_transport_header_t *header_p, /**< tcp implementation */
jerry_debugger_transport_receive_context_t *receive_context_p) /**< receive context */
jerryx_debugger_ws_receive (jerry_debugger_transport_header_t *header_p, /**< tcp implementation */
jerry_debugger_transport_receive_context_t *receive_context_p) /**< receive context */
{
if (!header_p->next_p->receive (header_p->next_p, receive_context_p))
{
@ -320,7 +321,7 @@ jerry_debugger_ws_receive (jerry_debugger_transport_header_t *header_p, /**< tcp
if (message_total_length == 0)
{
/* Byte stream. */
if (receive_context_p->message_length < sizeof (jerry_websocket_receive_header_t))
if (receive_context_p->message_length < sizeof (jerryx_websocket_receive_header_t))
{
receive_context_p->message_p = NULL;
return true;
@ -329,32 +330,32 @@ jerry_debugger_ws_receive (jerry_debugger_transport_header_t *header_p, /**< tcp
else
{
/* Datagram packet. */
JERRY_ASSERT (receive_context_p->message_length >= sizeof (jerry_websocket_receive_header_t));
JERRYX_ASSERT (receive_context_p->message_length >= sizeof (jerryx_websocket_receive_header_t));
}
uint8_t *message_p = receive_context_p->message_p;
if ((message_p[0] & ~JERRY_DEBUGGER_WEBSOCKET_OPCODE_MASK) != JERRY_DEBUGGER_WEBSOCKET_FIN_BIT
|| (message_p[1] & JERRY_DEBUGGER_WEBSOCKET_LENGTH_MASK) > JERRY_DEBUGGER_WEBSOCKET_ONE_BYTE_LEN_MAX
|| !(message_p[1] & JERRY_DEBUGGER_WEBSOCKET_MASK_BIT))
if ((message_p[0] & ~JERRYX_DEBUGGER_WEBSOCKET_OPCODE_MASK) != JERRYX_DEBUGGER_WEBSOCKET_FIN_BIT
|| (message_p[1] & JERRYX_DEBUGGER_WEBSOCKET_LENGTH_MASK) > JERRYX_DEBUGGER_WEBSOCKET_ONE_BYTE_LEN_MAX
|| !(message_p[1] & JERRYX_DEBUGGER_WEBSOCKET_MASK_BIT))
{
JERRY_ERROR_MSG ("Unsupported Websocket message.\n");
JERRYX_ERROR_MSG ("Unsupported Websocket message.\n");
jerry_debugger_transport_close ();
return false;
}
if ((message_p[0] & JERRY_DEBUGGER_WEBSOCKET_OPCODE_MASK) != JERRY_DEBUGGER_WEBSOCKET_BINARY_FRAME)
if ((message_p[0] & JERRYX_DEBUGGER_WEBSOCKET_OPCODE_MASK) != JERRYX_DEBUGGER_WEBSOCKET_BINARY_FRAME)
{
JERRY_ERROR_MSG ("Unsupported Websocket opcode.\n");
JERRYX_ERROR_MSG ("Unsupported Websocket opcode.\n");
jerry_debugger_transport_close ();
return false;
}
size_t message_length = (size_t) (message_p[1] & JERRY_DEBUGGER_WEBSOCKET_LENGTH_MASK);
size_t message_length = (size_t) (message_p[1] & JERRYX_DEBUGGER_WEBSOCKET_LENGTH_MASK);
if (message_total_length == 0)
{
size_t new_total_length = message_length + sizeof (jerry_websocket_receive_header_t);
size_t new_total_length = message_length + sizeof (jerryx_websocket_receive_header_t);
/* Byte stream. */
if (receive_context_p->message_length < new_total_length)
@ -368,16 +369,16 @@ jerry_debugger_ws_receive (jerry_debugger_transport_header_t *header_p, /**< tcp
else
{
/* Datagram packet. */
JERRY_ASSERT (receive_context_p->message_length == (message_length + sizeof (jerry_websocket_receive_header_t)));
JERRYX_ASSERT (receive_context_p->message_length == (message_length + sizeof (jerryx_websocket_receive_header_t)));
}
message_p += sizeof (jerry_websocket_receive_header_t);
message_p += sizeof (jerryx_websocket_receive_header_t);
receive_context_p->message_p = message_p;
receive_context_p->message_length = message_length;
/* Unmask data bytes. */
const uint8_t *mask_p = message_p - JERRY_DEBUGGER_WEBSOCKET_MASK_SIZE;
const uint8_t *mask_p = message_p - JERRYX_DEBUGGER_WEBSOCKET_MASK_SIZE;
const uint8_t *mask_end_p = message_p;
const uint8_t *message_end_p = message_p + message_length;
@ -391,12 +392,12 @@ jerry_debugger_ws_receive (jerry_debugger_transport_header_t *header_p, /**< tcp
if (JERRY_UNLIKELY (mask_p >= mask_end_p))
{
mask_p -= JERRY_DEBUGGER_WEBSOCKET_MASK_SIZE;
mask_p -= JERRYX_DEBUGGER_WEBSOCKET_MASK_SIZE;
}
}
return true;
} /* jerry_debugger_ws_receive */
} /* jerryx_debugger_ws_receive */
/**
* Initialize the websocket transportation layer.
@ -405,7 +406,7 @@ jerry_debugger_ws_receive (jerry_debugger_transport_header_t *header_p, /**< tcp
* false - otherwise
*/
bool
jerry_debugger_ws_create (void)
jerryx_debugger_ws_create (void)
{
bool is_handshake_ok = false;
@ -417,7 +418,7 @@ jerry_debugger_ws_create (void)
return false;
}
is_handshake_ok = jerry_process_handshake (request_buffer_p);
is_handshake_ok = jerryx_process_handshake (request_buffer_p);
jerry_debugger_transport_free ((void *) request_buffer_p, buffer_size);
@ -435,17 +436,30 @@ jerry_debugger_ws_create (void)
return false;
}
header_p->close = jerry_debugger_ws_close;
header_p->send = jerry_debugger_ws_send;
header_p->receive = jerry_debugger_ws_receive;
header_p->close = jerryx_debugger_ws_close;
header_p->send = jerryx_debugger_ws_send;
header_p->receive = jerryx_debugger_ws_receive;
jerry_debugger_transport_add (header_p,
JERRY_DEBUGGER_WEBSOCKET_HEADER_SIZE,
JERRY_DEBUGGER_WEBSOCKET_ONE_BYTE_LEN_MAX,
JERRY_DEBUGGER_WEBSOCKET_HEADER_SIZE + JERRY_DEBUGGER_WEBSOCKET_MASK_SIZE,
JERRY_DEBUGGER_WEBSOCKET_ONE_BYTE_LEN_MAX);
JERRYX_DEBUGGER_WEBSOCKET_HEADER_SIZE,
JERRYX_DEBUGGER_WEBSOCKET_ONE_BYTE_LEN_MAX,
JERRYX_DEBUGGER_WEBSOCKET_HEADER_SIZE + JERRYX_DEBUGGER_WEBSOCKET_MASK_SIZE,
JERRYX_DEBUGGER_WEBSOCKET_ONE_BYTE_LEN_MAX);
return true;
} /* jerry_debugger_ws_create */
} /* jerryx_debugger_ws_create */
#else /* !JERRY_DEBUGGER */
/**
* Dummy function when debugger is disabled.
*
* @return false
*/
bool
jerryx_debugger_ws_create (void)
{
return false;
} /* jerryx_debugger_ws_create */
#endif /* JERRY_DEBUGGER */

View File

@ -22,11 +22,9 @@
/* JerryScript debugger protocol is a simplified version of RFC-6455 (WebSockets). */
bool jerry_debugger_ws_create (void);
void jerry_debugger_compute_sha1 (const uint8_t *input1, size_t input1_len,
const uint8_t *input2, size_t input2_len,
uint8_t output[20]);
void jerryx_debugger_compute_sha1 (const uint8_t *input1, size_t input1_len,
const uint8_t *input2, size_t input2_len,
uint8_t output[20]);
#endif /* JERRY_DEBUGGER */

View File

@ -13,15 +13,30 @@
* limitations under the License.
*/
#ifndef DEBUGGER_TCP_H
#define DEBUGGER_TCP_H
#ifndef JERRYX_DEBUGGER_H
#define JERRYX_DEBUGGER_H
#include "jerryscript.h"
#include "jerryscript-debugger-transport.h"
#ifdef JERRY_DEBUGGER
#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */
bool jerry_debugger_tcp_create (uint16_t port);
void jerryx_debugger_after_connect (bool success);
#endif /* JERRY_DEBUGGER */
/*
* Message transmission interfaces.
*/
bool jerryx_debugger_tcp_create (uint16_t port);
#endif /* !DEBUGGER_TCP_H */
/*
* Message encoding interfaces.
*/
bool jerryx_debugger_ws_create (void);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* !JERRYX_HANDLER_H */

View File

@ -19,6 +19,7 @@
#include <string.h>
#include "jerryscript.h"
#include "jerryscript-ext/debugger.h"
#include "jerryscript-ext/handler.h"
#include "jerryscript-port.h"
#include "jerryscript-port-default.h"
@ -423,7 +424,8 @@ init_engine (jerry_init_flag_t flags, /**< initialized flags for the engine */
jerry_init (flags);
if (debug_server)
{
jerry_debugger_init (debug_port);
jerryx_debugger_after_connect (jerryx_debugger_tcp_create (debug_port)
&& jerryx_debugger_ws_create ());
}
register_js_function ("assert", jerryx_handler_assert);

View File

@ -18,6 +18,7 @@
#include <stdlib.h>
#include "jerryscript.h"
#include "jerryscript-ext/debugger.h"
#include "jerryscript-ext/handler.h"
#include "jerryscript-port.h"
#include "setjmp.h"
@ -380,7 +381,8 @@ int jerry_main (int argc, char *argv[])
if (start_debug_server)
{
jerry_debugger_init (debug_port);
jerryx_debugger_after_connect (jerryx_debugger_tcp_create (debug_port)
&& jerryx_debugger_ws_create ());
}
register_js_function ("assert", jerryx_handler_assert);

View File

@ -19,6 +19,7 @@
#include <tinyara/fs/fs_utils.h>
#include "jerryscript.h"
#include "jerryscript-ext/debugger.h"
#include "jerryscript-ext/handler.h"
#include "jerryscript-port.h"
#include "setjmp.h"
@ -356,7 +357,8 @@ jerry_cmd_main (int argc, char *argv[])
if (start_debug_server)
{
jerry_debugger_init (debug_port);
jerryx_debugger_after_connect (jerryx_debugger_tcp_create (debug_port)
&& jerryx_debugger_ws_create ());
}
register_js_function ("assert", jerryx_handler_assert);