Merge main into dev/socket

This commit is contained in:
Wenyong Huang
2022-09-10 22:12:43 +08:00
74 changed files with 1478 additions and 646 deletions

View File

@ -11,17 +11,15 @@
#include "wasm_opcode.h"
#include "wasm_runtime.h"
static uint8 break_instr[] = { DEBUG_OP_BREAK };
static const uint8 break_instr[] = { DEBUG_OP_BREAK };
typedef struct WASMDebugEngine {
struct WASMDebugEngine *next;
WASMDebugControlThread *control_thread;
char ip_addr[128];
int32 platform_port;
int32 process_base_port;
bh_list debug_instance_list;
korp_mutex instance_list_lock;
bool active;
} WASMDebugEngine;
void
@ -81,10 +79,12 @@ control_thread_routine(void *arg)
control_thread->debug_instance = debug_inst;
bh_strcpy_s(control_thread->ip_addr, sizeof(control_thread->ip_addr),
g_debug_engine->ip_addr);
control_thread->port =
(g_debug_engine->process_base_port == 0)
? 0
: g_debug_engine->process_base_port + debug_inst->id;
if (control_thread->port == -1) {
control_thread->port =
(g_debug_engine->process_base_port == 0)
? 0
: g_debug_engine->process_base_port + debug_inst->id - 1;
}
LOG_WARNING("control thread of debug object %p start\n", debug_inst);
@ -93,6 +93,7 @@ control_thread_routine(void *arg)
if (!control_thread->server) {
LOG_ERROR("Failed to create debug server\n");
control_thread->port = 0;
os_cond_signal(&debug_inst->wait_cond);
os_mutex_unlock(&debug_inst->wait_lock);
return NULL;
@ -178,7 +179,7 @@ control_thread_routine(void *arg)
}
static WASMDebugControlThread *
wasm_debug_control_thread_create(WASMDebugInstance *debug_instance)
wasm_debug_control_thread_create(WASMDebugInstance *debug_instance, int32 port)
{
WASMDebugControlThread *control_thread;
@ -188,6 +189,7 @@ wasm_debug_control_thread_create(WASMDebugInstance *debug_instance)
return NULL;
}
memset(control_thread, 0, sizeof(WASMDebugControlThread));
control_thread->port = port;
if (os_mutex_init(&control_thread->wait_lock) != 0)
goto fail;
@ -198,7 +200,7 @@ wasm_debug_control_thread_create(WASMDebugInstance *debug_instance)
if (0
!= os_thread_create(&control_thread->tid, control_thread_routine,
debug_instance, APP_THREAD_STACK_SIZE_MAX)) {
debug_instance, APP_THREAD_STACK_SIZE_DEFAULT)) {
os_mutex_unlock(&debug_instance->wait_lock);
goto fail1;
}
@ -265,16 +267,6 @@ wasm_debug_engine_create()
/* reset current instance id */
current_instance_id = 1;
/* TODO: support Wasm platform in LLDB */
/*
engine->control_thread =
wasm_debug_control_thread_create((WASMDebugObject *)engine);
engine->control_thread->debug_engine = (WASMDebugObject *)engine;
engine->control_thread->debug_instance = NULL;
sprintf(engine->control_thread->ip_addr, "127.0.0.1");
engine->control_thread->port = 1234;
*/
bh_list_init(&engine->debug_instance_list);
return engine;
}
@ -291,7 +283,7 @@ wasm_debug_engine_destroy()
}
bool
wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port)
wasm_debug_engine_init(char *ip_addr, int32 process_port)
{
if (wasm_debug_handler_init() != 0) {
return false;
@ -302,9 +294,6 @@ wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port)
}
if (g_debug_engine) {
process_port -= 1;
g_debug_engine->platform_port =
platform_port > 0 ? platform_port : 1234;
g_debug_engine->process_base_port =
(process_port > 0) ? process_port : 0;
if (ip_addr)
@ -313,7 +302,6 @@ wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port)
else
snprintf(g_debug_engine->ip_addr, sizeof(g_debug_engine->ip_addr),
"%s", "127.0.0.1");
g_debug_engine->active = true;
}
else {
wasm_debug_handler_deinit();
@ -322,33 +310,16 @@ wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port)
return g_debug_engine != NULL ? true : false;
}
void
wasm_debug_set_engine_active(bool active)
{
if (g_debug_engine) {
g_debug_engine->active = active;
}
}
bool
wasm_debug_get_engine_active(void)
{
if (g_debug_engine) {
return g_debug_engine->active;
}
return false;
}
/* A debug Instance is a debug "process" in gdb remote protocol
and bound to a runtime cluster */
WASMDebugInstance *
wasm_debug_instance_create(WASMCluster *cluster)
wasm_debug_instance_create(WASMCluster *cluster, int32 port)
{
WASMDebugInstance *instance;
WASMExecEnv *exec_env = NULL;
wasm_module_inst_t module_inst = NULL;
if (!g_debug_engine || !g_debug_engine->active) {
if (!g_debug_engine) {
return NULL;
}
@ -392,7 +363,7 @@ wasm_debug_instance_create(WASMCluster *cluster)
}
instance->exec_mem_info.current_pos = instance->exec_mem_info.start_offset;
if (!wasm_debug_control_thread_create(instance)) {
if (!wasm_debug_control_thread_create(instance, port)) {
LOG_ERROR("WASM Debug Engine error: failed to create control thread");
goto fail3;
}

View File

@ -108,7 +108,7 @@ void
on_thread_stop_event(WASMDebugInstance *debug_inst, WASMExecEnv *exec_env);
WASMDebugInstance *
wasm_debug_instance_create(WASMCluster *cluster);
wasm_debug_instance_create(WASMCluster *cluster, int32 port);
void
wasm_debug_instance_destroy(WASMCluster *cluster);
@ -117,17 +117,11 @@ WASMDebugInstance *
wasm_exec_env_get_instance(WASMExecEnv *exec_env);
bool
wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port);
wasm_debug_engine_init(char *ip_addr, int32 process_port);
void
wasm_debug_engine_destroy();
void
wasm_debug_set_engine_active(bool active);
bool
wasm_debug_get_engine_active(void);
WASMExecEnv *
wasm_debug_instance_get_current_env(WASMDebugInstance *instance);

View File

@ -18,7 +18,7 @@ struct packet_handler_elem {
#define DEL_HANDLER(r, h) [r] = { .request = r, .handler = h }
static struct packet_handler_elem packet_handler_table[255] = {
static const struct packet_handler_elem packet_handler_table[255] = {
DEL_HANDLER('Q', handle_general_set),
DEL_HANDLER('q', handle_general_query),
DEL_HANDLER('v', handle_v_packet),

View File

@ -10,19 +10,51 @@
#include "utils.h"
#include "wasm_runtime.h"
#define MAX_PACKET_SIZE (0x20000)
static char tmpbuf[MAX_PACKET_SIZE];
/*
* Note: A moderate MAX_PACKET_SIZE is ok because
* LLDB queries our buffer size (via qSupported PacketSize)
* and limits packet sizes accordingly.
*/
#if defined(DEBUG_MAX_PACKET_SIZE)
#define MAX_PACKET_SIZE DEBUG_MAX_PACKET_SIZE
#else
#define MAX_PACKET_SIZE (4096)
#endif
/*
* Note: It's assumed that MAX_PACKET_SIZE is reasonably large.
* See GetWorkingDir, WasmCallStack, etc.
*/
#if MAX_PACKET_SIZE < PATH_MAX || MAX_PACKET_SIZE < (2048 + 1)
#error MAX_PACKET_SIZE is too small
#endif
static char *tmpbuf;
static korp_mutex tmpbuf_lock;
int
wasm_debug_handler_init()
{
return os_mutex_init(&tmpbuf_lock);
int ret;
tmpbuf = wasm_runtime_malloc(MAX_PACKET_SIZE);
if (tmpbuf == NULL) {
LOG_ERROR("debug-engine: Packet buffer allocation failure");
return BHT_ERROR;
}
ret = os_mutex_init(&tmpbuf_lock);
if (ret != BHT_OK) {
wasm_runtime_free(tmpbuf);
tmpbuf = NULL;
}
return ret;
}
void
wasm_debug_handler_deinit()
{
wasm_runtime_free(tmpbuf);
tmpbuf = NULL;
os_mutex_destroy(&tmpbuf_lock);
}
@ -76,14 +108,17 @@ process_xfer(WASMGDBServer *server, const char *name, char *args)
os_mutex_lock(&tmpbuf_lock);
#if WASM_ENABLE_LIBC_WASI != 0
char objname[128];
wasm_debug_instance_get_current_object_name(
(WASMDebugInstance *)server->thread->debug_instance, objname, 128);
snprintf(tmpbuf, sizeof(tmpbuf),
if (!wasm_debug_instance_get_current_object_name(
(WASMDebugInstance *)server->thread->debug_instance, objname,
128)) {
objname[0] = 0; /* use an empty string */
}
snprintf(tmpbuf, MAX_PACKET_SIZE,
"l<library-list><library name=\"%s\"><section "
"address=\"0x%" PRIx64 "\"/></library></library-list>",
objname, addr);
#else
snprintf(tmpbuf, sizeof(tmpbuf),
snprintf(tmpbuf, MAX_PACKET_SIZE,
"l<library-list><library name=\"%s\"><section "
"address=\"0x%" PRIx64 "\"/></library></library-list>",
"nobody.wasm", addr);
@ -103,7 +138,7 @@ process_wasm_local(WASMGDBServer *server, char *args)
bool ret;
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf), "E01");
snprintf(tmpbuf, MAX_PACKET_SIZE, "E01");
if (sscanf(args, "%" PRId32 ";%" PRId32, &frame_index, &local_index) == 2) {
ret = wasm_debug_instance_get_local(
(WASMDebugInstance *)server->thread->debug_instance, frame_index,
@ -126,7 +161,7 @@ process_wasm_global(WASMGDBServer *server, char *args)
bool ret;
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf), "E01");
snprintf(tmpbuf, MAX_PACKET_SIZE, "E01");
if (sscanf(args, "%" PRId32 ";%" PRId32, &frame_index, &global_index)
== 2) {
ret = wasm_debug_instance_get_global(
@ -161,14 +196,14 @@ handle_general_query(WASMGDBServer *server, char *payload)
(WASMDebugInstance *)server->thread->debug_instance);
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf), "QCp%" PRIx64 ".%" PRIx64 "", pid,
snprintf(tmpbuf, MAX_PACKET_SIZE, "QCp%" PRIx64 ".%" PRIx64 "", pid,
tid);
write_packet(server, tmpbuf);
os_mutex_unlock(&tmpbuf_lock);
}
if (!strcmp(name, "Supported")) {
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf),
snprintf(tmpbuf, MAX_PACKET_SIZE,
"qXfer:libraries:read+;PacketSize=%" PRIx32 ";",
MAX_PACKET_SIZE);
write_packet(server, tmpbuf);
@ -196,7 +231,7 @@ handle_general_query(WASMGDBServer *server, char *payload)
strlen("wasm32-wamr-wasi-wasm"));
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf),
snprintf(tmpbuf, MAX_PACKET_SIZE,
"vendor:wamr;ostype:wasi;arch:wasm32;"
"triple:%s;endian:little;ptrsize:4;",
triple);
@ -227,7 +262,7 @@ handle_general_query(WASMGDBServer *server, char *payload)
strlen("wasm32-wamr-wasi-wasm"));
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf),
snprintf(tmpbuf, MAX_PACKET_SIZE,
"pid:%" PRIx64 ";parent-pid:%" PRIx64
";vendor:wamr;ostype:wasi;arch:wasm32;"
"triple:%s;endian:little;ptrsize:4;",
@ -238,7 +273,7 @@ handle_general_query(WASMGDBServer *server, char *payload)
if (!strcmp(name, "RegisterInfo0")) {
os_mutex_lock(&tmpbuf_lock);
snprintf(
tmpbuf, sizeof(tmpbuf),
tmpbuf, MAX_PACKET_SIZE,
"name:pc;alt-name:pc;bitsize:64;offset:0;encoding:uint;format:hex;"
"set:General Purpose Registers;gcc:16;dwarf:16;generic:pc;");
write_packet(server, tmpbuf);
@ -260,7 +295,7 @@ handle_general_query(WASMGDBServer *server, char *payload)
mem2hex(mem_info->name, name_buf, strlen(mem_info->name));
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf),
snprintf(tmpbuf, MAX_PACKET_SIZE,
"start:%" PRIx64 ";size:%" PRIx64
";permissions:%s;name:%s;",
(uint64)mem_info->start, mem_info->size,
@ -339,7 +374,7 @@ send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid)
if (status == 0) {
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf), "W%02x", status);
snprintf(tmpbuf, MAX_PACKET_SIZE, "W%02x", status);
write_packet(server, tmpbuf);
os_mutex_unlock(&tmpbuf_lock);
return;
@ -355,16 +390,16 @@ send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid)
os_mutex_lock(&tmpbuf_lock);
// TODO: how name a wasm thread?
len += snprintf(tmpbuf, sizeof(tmpbuf), "T%02xthread:%" PRIx64 ";name:%s;",
len += snprintf(tmpbuf, MAX_PACKET_SIZE, "T%02xthread:%" PRIx64 ";name:%s;",
gdb_status, (uint64)(uintptr_t)tid, "nobody");
if (tids_count > 0) {
len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, "threads:");
len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "threads:");
while (i < tids_count) {
if (i == tids_count - 1)
len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
"%" PRIx64 ";", (uint64)(uintptr_t)tids[i]);
else
len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
"%" PRIx64 ",", (uint64)(uintptr_t)tids[i]);
i++;
}
@ -383,29 +418,29 @@ send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid)
* correctly processed by LLDB */
uint32 exception_len = strlen(exception);
len +=
snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
"thread-pcs:%" PRIx64 ";00:%s;reason:%s;description:", pc,
pc_string, "exception");
/* The description should be encoded as HEX */
for (i = 0; i < exception_len; i++) {
len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, "%02x",
len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "%02x",
exception[i]);
}
len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, ";");
len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, ";");
}
else {
if (status == WAMR_SIG_TRAP) {
len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
"thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
pc_string, "breakpoint");
}
else if (status == WAMR_SIG_SINGSTEP) {
len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
"thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
pc_string, "trace");
}
else if (status > 0) {
len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
"thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
pc_string, "signal");
}
@ -551,7 +586,7 @@ handle_get_read_memory(WASMGDBServer *server, char *payload)
bool ret;
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf), "%s", "");
snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "");
if (sscanf(payload, "%" SCNx64 ",%" SCNx64, &maddr, &mlen) == 2) {
char *buff;
@ -585,7 +620,7 @@ handle_get_write_memory(WASMGDBServer *server, char *payload)
bool ret;
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf), "%s", "");
snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "");
if (sscanf(payload, "%" SCNx64 ",%" SCNx64 ":%n", &maddr, &mlen, &offset)
== 2) {
payload += offset;
@ -599,7 +634,7 @@ handle_get_write_memory(WASMGDBServer *server, char *payload)
(WASMDebugInstance *)server->thread->debug_instance, maddr,
buff, &mlen);
if (ret) {
snprintf(tmpbuf, sizeof(tmpbuf), "%s", "OK");
snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "OK");
}
wasm_runtime_free(buff);
}
@ -681,7 +716,7 @@ handle_malloc(WASMGDBServer *server, char *payload)
}
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf), "%s", "E03");
snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "E03");
size = strtoll(payload, NULL, 16);
if (size > 0) {
@ -701,7 +736,7 @@ handle_malloc(WASMGDBServer *server, char *payload)
(WASMDebugInstance *)server->thread->debug_instance, size,
map_prot);
if (addr) {
snprintf(tmpbuf, sizeof(tmpbuf), "%" PRIx64, addr);
snprintf(tmpbuf, MAX_PACKET_SIZE, "%" PRIx64, addr);
}
}
write_packet(server, tmpbuf);
@ -715,13 +750,13 @@ handle_free(WASMGDBServer *server, char *payload)
bool ret;
os_mutex_lock(&tmpbuf_lock);
snprintf(tmpbuf, sizeof(tmpbuf), "%s", "E03");
snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "E03");
addr = strtoll(payload, NULL, 16);
ret = wasm_debug_instance_ummap(
(WASMDebugInstance *)server->thread->debug_instance, addr);
if (ret) {
snprintf(tmpbuf, sizeof(tmpbuf), "%s", "OK");
snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "OK");
}
write_packet(server, tmpbuf);