Enable source debugging feature for windows platform (#910)

- use platform independent data types in debug-engine library
- add os_socket APIs and provide windows and posix implementation
- avoid using platform related header files in non-platform layer
- use format specifiers macros for sprintf and sscanf
- change thread handle type from uint64 to korp_tid
- add lock when sending socket packet to avoid thread racing
This commit is contained in:
Xu Jun
2021-12-22 19:52:07 +08:00
committed by GitHub
parent af251e45ca
commit ccb2de35d7
21 changed files with 851 additions and 316 deletions

View File

@ -3,15 +3,12 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "bh_platform.h"
#include "packets.h"
#include <stdbool.h>
#include "bh_log.h"
#include "gdbserver.h"
void
pktbuf_insert(WASMGDBServer *gdbserver, const uint8_t *buf, ssize_t len)
pktbuf_insert(WASMGDBServer *gdbserver, const uint8 *buf, ssize_t len)
{
WasmDebugPacket *pkt = &gdbserver->pkt;
@ -45,13 +42,13 @@ pktbuf_clear(WASMGDBServer *gdbserver)
pkt->size = 0;
}
int
int32
read_data_once(WASMGDBServer *gdbserver)
{
ssize_t nread;
uint8_t buf[4096];
uint8 buf[4096];
nread = read(gdbserver->socket_fd, buf, sizeof(buf));
nread = os_socket_recv(gdbserver->socket_fd, buf, sizeof(buf));
if (nread <= 0) {
LOG_ERROR("Connection closed");
return -1;
@ -61,11 +58,11 @@ read_data_once(WASMGDBServer *gdbserver)
}
void
write_data_raw(WASMGDBServer *gdbserver, const uint8_t *data, ssize_t len)
write_data_raw(WASMGDBServer *gdbserver, const uint8 *data, ssize_t len)
{
ssize_t nwritten;
nwritten = write(gdbserver->socket_fd, data, len);
nwritten = os_socket_send(gdbserver->socket_fd, data, len);
if (nwritten < 0) {
LOG_ERROR("Write error\n");
exit(-2);
@ -79,21 +76,21 @@ write_hex(WASMGDBServer *gdbserver, unsigned long hex)
size_t len;
len = snprintf(buf, sizeof(buf) - 1, "%02lx", hex);
write_data_raw(gdbserver, (uint8_t *)buf, len);
write_data_raw(gdbserver, (uint8 *)buf, len);
}
void
write_packet_bytes(WASMGDBServer *gdbserver, const uint8_t *data,
write_packet_bytes(WASMGDBServer *gdbserver, const uint8 *data,
size_t num_bytes)
{
uint8_t checksum;
uint8 checksum;
size_t i;
write_data_raw(gdbserver, (uint8_t *)"$", 1);
write_data_raw(gdbserver, (uint8 *)"$", 1);
for (i = 0, checksum = 0; i < num_bytes; ++i)
checksum += data[i];
write_data_raw(gdbserver, (uint8_t *)data, num_bytes);
write_data_raw(gdbserver, (uint8_t *)"#", 1);
write_data_raw(gdbserver, (uint8 *)data, num_bytes);
write_data_raw(gdbserver, (uint8 *)"#", 1);
write_hex(gdbserver, checksum);
}
@ -101,17 +98,17 @@ void
write_packet(WASMGDBServer *gdbserver, const char *data)
{
LOG_VERBOSE("send replay:%s", data);
write_packet_bytes(gdbserver, (const uint8_t *)data, strlen(data));
write_packet_bytes(gdbserver, (const uint8 *)data, strlen(data));
}
void
write_binary_packet(WASMGDBServer *gdbserver, const char *pfx,
const uint8_t *data, ssize_t num_bytes)
const uint8 *data, ssize_t num_bytes)
{
uint8_t *buf;
uint8 *buf;
ssize_t pfx_num_chars = strlen(pfx);
ssize_t buf_num_bytes = 0, total_size;
int i;
int32 i;
total_size = 2 * num_bytes + pfx_num_chars;
buf = wasm_runtime_malloc(total_size);
@ -125,7 +122,7 @@ write_binary_packet(WASMGDBServer *gdbserver, const char *pfx,
buf_num_bytes += pfx_num_chars;
for (i = 0; i < num_bytes; ++i) {
uint8_t b = data[i];
uint8 b = data[i];
switch (b) {
case '#':
case '$':
@ -148,7 +145,7 @@ skip_to_packet_start(WASMGDBServer *gdbserver)
{
ssize_t start_index = -1, i;
for (i = 0; i < gdbserver->pkt.size; ++i) {
for (i = 0; i < (ssize_t)gdbserver->pkt.size; ++i) {
if (gdbserver->pkt.buf[i] == '$') {
start_index = i;
break;
@ -176,6 +173,6 @@ read_packet(WASMGDBServer *gdbserver)
return false;
}
if (!gdbserver->noack)
write_data_raw(gdbserver, (uint8_t *)"+", 1);
write_data_raw(gdbserver, (uint8 *)"+", 1);
return true;
}