From d472d8734b187c0c23b84572c413cd38d1ecdd78 Mon Sep 17 00:00:00 2001 From: lixianjing Date: Sat, 22 Jan 2022 18:38:08 +0800 Subject: [PATCH] improve fscript debugger --- docs/changes.md | 1 + src/debugger/debugger_fscript.c | 16 +++--------- src/debugger/debugger_global.c | 9 ------- src/debugger/debugger_server.c | 35 ++++++++++++++++++-------- src/debugger/debugger_server.h | 8 ++++++ src/debugger/debugger_server_tcp.c | 25 ++++++++++++++++++ src/debugger/debugger_server_tcp.h | 9 +++++++ src/platforms/aworkslp/serial_helper.c | 23 ++++++++--------- tests/fdb.cpp | 8 +++--- tests/fscript_run.cpp | 16 +++--------- 10 files changed, 90 insertions(+), 60 deletions(-) diff --git a/docs/changes.md b/docs/changes.md index 5eb8942ff..75f6e398a 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -2,6 +2,7 @@ 2022/01/22 * line_number支持设置高亮行。 + * 完善fscript debugger 2022/01/21 * 完善fscript debugger并增加fdb命令行调试工具。 diff --git a/src/debugger/debugger_fscript.c b/src/debugger/debugger_fscript.c index fbdfa35f1..560b6d8bf 100644 --- a/src/debugger/debugger_fscript.c +++ b/src/debugger/debugger_fscript.c @@ -514,11 +514,9 @@ ret_t debugger_fscript_print_func(fscript_t* fscript, fscript_args_t* args, valu uint32_t i = 0; debugger_log_event_t event; debugger_t* debugger = NULL; + return_value_if_fail(fscript != NULL, RET_BAD_PARAMS); - if (fscript != NULL && fscript->code_id != NULL) { - debugger = debugger_server_find_debugger(fscript->code_id); - } - + debugger = debugger_server_find_debugger(fscript->code_id); if (debugger != NULL) { uint32_t line = fscript->curr->row; value_set_bool(result, TRUE); @@ -685,10 +683,7 @@ ret_t debugger_fscript_exec_func(fscript_t* fscript, const char* name, fscript_f debugger_t* debugger = NULL; return_value_if_fail(fscript != NULL, RET_BAD_PARAMS); - if (fscript != NULL && fscript->code_id != NULL) { - debugger = debugger_server_find_debugger(fscript->code_id); - } - + debugger = debugger_server_find_debugger(fscript->code_id); if (debugger == NULL) { return fscript_exec_func_default(fscript, iter, result); } else { @@ -729,10 +724,7 @@ ret_t debugger_fscript_set_var(fscript_t* fscript, const char* name, const value debugger_t* debugger = NULL; return_value_if_fail(fscript != NULL, RET_BAD_PARAMS); - if (fscript != NULL && fscript->code_id != NULL) { - debugger = debugger_server_find_debugger(fscript->code_id); - } - + debugger = debugger_server_find_debugger(fscript->code_id); if (debugger == NULL) { return fscript_set_var_default(fscript, name, v); } else { diff --git a/src/debugger/debugger_global.c b/src/debugger/debugger_global.c index d595d6a4c..466d844cc 100644 --- a/src/debugger/debugger_global.c +++ b/src/debugger/debugger_global.c @@ -27,9 +27,6 @@ static ret_t debugger_fscript_on_connect(fscript_t* fscript) { debugger_t* debugger = NULL; return_value_if_fail(fscript != NULL, RET_BAD_PARAMS); - if (fscript->code_id == NULL) { - return RET_OK; - } debugger = debugger_server_find_debugger(fscript->code_id); if (debugger != NULL) { @@ -45,9 +42,6 @@ static ret_t debugger_fscript_on_connect(fscript_t* fscript) { static ret_t debugger_fscript_on_disconnect(fscript_t* fscript) { debugger_t* debugger = NULL; return_value_if_fail(fscript != NULL, RET_BAD_PARAMS); - if (fscript->code_id == NULL) { - return RET_OK; - } debugger = debugger_server_find_debugger(fscript->code_id); if (debugger != NULL) { @@ -63,9 +57,6 @@ static ret_t debugger_fscript_on_disconnect(fscript_t* fscript) { static ret_t debugger_fscript_on_fscript_init(fscript_t* fscript, const char* code) { debugger_t* debugger = NULL; return_value_if_fail(fscript != NULL, RET_BAD_PARAMS); - if (fscript->code_id == NULL) { - return RET_OK; - } debugger = debugger_server_find_debugger(fscript->code_id); if (debugger != NULL) { diff --git a/src/debugger/debugger_server.c b/src/debugger/debugger_server.c index 9108b3865..92bdb5028 100644 --- a/src/debugger/debugger_server.c +++ b/src/debugger/debugger_server.c @@ -38,7 +38,7 @@ typedef struct _debugger_server_t { bool_t quited; /*启动完成*/ bool_t started; - + bool_t single_mode; tk_istream_t* in; tk_ostream_t* out; tk_iostream_t* io; @@ -53,13 +53,14 @@ static void* debugger_server_run(void* ctx); static ret_t debugger_server_send_object(debugger_server_t* server, debugger_resp_t* resp, tk_object_t* obj); -static debugger_server_t* debugger_server_create(tk_iostream_t* io) { +static debugger_server_t* debugger_server_create(tk_iostream_t* io, bool_t single_mode) { debugger_server_t* server = TKMEM_ZALLOC(debugger_server_t); return_value_if_fail(server != NULL, NULL); assert(io != NULL); server->io = io; + server->single_mode = single_mode; server->in = tk_iostream_get_istream(io); server->out = tk_iostream_get_ostream(io); darray_init(&(server->debuggers), 5, (tk_destroy_t)tk_object_unref, NULL); @@ -219,15 +220,18 @@ static ret_t debugger_server_get_debuggers(debugger_server_t* server, str_t* deb static debugger_t* debugger_server_find(debugger_server_t* server, const char* code_id) { uint32_t i = 0; debugger_t* debugger = NULL; - return_value_if_fail(code_id != NULL, NULL); return_value_if_fail(server != NULL, NULL); if (tk_mutex_nest_lock(server->mutex) == RET_OK) { - for (i = 0; i < server->debuggers.size; i++) { - debugger_t* iter = (debugger_t*)darray_get(&(server->debuggers), i); - if (debugger_match(iter, code_id)) { - debugger = iter; - break; + if (server->single_mode) { + debugger = server->debugger; + } else { + for (i = 0; i < server->debuggers.size; i++) { + debugger_t* iter = (debugger_t*)darray_get(&(server->debuggers), i); + if (debugger_match(iter, code_id)) { + debugger = iter; + break; + } } } tk_mutex_nest_unlock(server->mutex); @@ -542,13 +546,14 @@ static void* debugger_server_run(void* ctx) { return NULL; } +static bool_t s_single_mode = FALSE; static debugger_server_t* s_debugger_server = NULL; ret_t debugger_server_start(tk_iostream_t* io) { debugger_server_t* server = NULL; return_value_if_fail(io != NULL && s_debugger_server == NULL, RET_BAD_PARAMS); - server = debugger_server_create(io); + server = debugger_server_create(io, s_single_mode); return_value_if_fail(server != NULL, RET_BAD_PARAMS); s_debugger_server = server; @@ -567,7 +572,6 @@ ret_t debugger_server_stop(void) { debugger_t* debugger_server_find_debugger(const char* code_id) { debugger_server_t* server = s_debugger_server; - return_value_if_fail(code_id != NULL, NULL); return debugger_server_find(server, code_id); } @@ -575,3 +579,14 @@ debugger_t* debugger_server_find_debugger(const char* code_id) { bool_t debugger_server_is_running(void) { return s_debugger_server != NULL; } + +ret_t debugger_server_set_single_mode(bool_t single_mode) { + debugger_server_t* server = s_debugger_server; + + s_single_mode = single_mode; + if (server != NULL) { + server->single_mode = single_mode; + } + + return RET_OK; +} diff --git a/src/debugger/debugger_server.h b/src/debugger/debugger_server.h index 1fe86625f..2919a8096 100644 --- a/src/debugger/debugger_server.h +++ b/src/debugger/debugger_server.h @@ -50,6 +50,14 @@ ret_t debugger_server_start(tk_iostream_t* io); */ ret_t debugger_server_stop(void); +/** + * @method debugger_server_set_single_mode + * 设置为单一debugger模式。 + * @param {bool_t} single_mode 单一debugger模式 + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t debugger_server_set_single_mode(bool_t single_mode); + /** * @method debugger_server_is_running * 判断服务是否在运行。 diff --git a/src/debugger/debugger_server_tcp.c b/src/debugger/debugger_server_tcp.c index 59aa025e0..ef6301fc9 100644 --- a/src/debugger/debugger_server_tcp.c +++ b/src/debugger/debugger_server_tcp.c @@ -19,6 +19,7 @@ * */ +#include "tkc/thread.h" #include "tkc/socket_helper.h" #include "streams/inet/iostream_tcp.h" #include "debugger/debugger_server_tcp.h" @@ -58,10 +59,34 @@ ret_t debugger_server_tcp_start(void) { return RET_OK; } +static tk_thread_t* s_accept_thread = NULL; +static void* accept_thread(void* arg) { + debugger_server_tcp_start(); + s_accept_thread = NULL; + return NULL; +} + +ret_t debugger_server_tcp_start_async(void) { + return_value_if_fail(s_accept_thread == NULL, RET_BUSY); + + s_accept_thread = tk_thread_create(accept_thread, NULL); + return_value_if_fail(s_accept_thread != NULL, RET_OOM); + + tk_thread_start(s_accept_thread); + + return RET_OK; +} + ret_t debugger_server_tcp_deinit(void) { return_value_if_fail(s_server_sock >= 0, RET_BAD_PARAMS); socket_close(s_server_sock); s_server_sock = -1; + + if (s_accept_thread != NULL) { + tk_thread_destroy(s_accept_thread); + s_accept_thread = NULL; + } + debugger_server_stop(); return RET_OK; diff --git a/src/debugger/debugger_server_tcp.h b/src/debugger/debugger_server_tcp.h index 7ab174878..5f5b902c7 100644 --- a/src/debugger/debugger_server_tcp.h +++ b/src/debugger/debugger_server_tcp.h @@ -24,6 +24,7 @@ #include "tkc/iostream.h" #include "debugger/debugger.h" +#include "debugger/debugger_global.h" #include "debugger/debugger_server.h" BEGIN_C_DECLS @@ -51,6 +52,14 @@ ret_t debugger_server_tcp_init(uint32_t port); */ ret_t debugger_server_tcp_start(void); +/** + * @method debugger_server_tcp_start_async + * 启动调试器服务。 + * > 接收客户端请求,并启动服务。 + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t debugger_server_tcp_start_async(void); + /** * @method debugger_server_tcp_deinit * 停止调试器服务。 diff --git a/src/platforms/aworkslp/serial_helper.c b/src/platforms/aworkslp/serial_helper.c index 445bd5229..2c03ac55c 100644 --- a/src/platforms/aworkslp/serial_helper.c +++ b/src/platforms/aworkslp/serial_helper.c @@ -65,7 +65,6 @@ static ret_t da_serial_destroy(void* data) { return RET_OK; } - int serial_handle_get_fd(serial_handle_t handle) { return handle->dev; } @@ -114,19 +113,17 @@ ret_t serial_oflush(serial_handle_t handle) { } ret_t serial_wait_for_data(serial_handle_t handle, uint32_t timeout_ms) { - - int len = 0; - int fd = serial_handle_get_fd(handle); - while (timeout_ms) { - aw_ioctl(fd, AW_FIONREAD, &len); - if (len > 0) { - return RET_OK; - } - sleep_ms(10); - timeout_ms -= tk_min(timeout_ms, 10); + int len = 0; + int fd = serial_handle_get_fd(handle); + while (timeout_ms) { + aw_ioctl(fd, AW_FIONREAD, &len); + if (len > 0) { + return RET_OK; } - return RET_TIMEOUT; - + sleep_ms(10); + timeout_ms -= tk_min(timeout_ms, 10); + } + return RET_TIMEOUT; } int32_t serial_read(serial_handle_t handle, uint8_t* buff, uint32_t max_size) { diff --git a/tests/fdb.cpp b/tests/fdb.cpp index 0f898ecd4..497fc19b2 100644 --- a/tests/fdb.cpp +++ b/tests/fdb.cpp @@ -430,10 +430,10 @@ ret_t aw_flow_shell_run(debugger_t* debugger) { tk_object_t* obj = object_default_create(); return_value_if_fail(debugger != NULL, RET_BAD_PARAMS); - app.obj = obj; - app.break_at_line = 0xffff; - app.debugger = debugger; - app.completed = FALSE; + app.obj = obj; + app.break_at_line = 0xffff; + app.debugger = debugger; + app.completed = FALSE; aw_read_line_init(); register_functions(obj); diff --git a/tests/fscript_run.cpp b/tests/fscript_run.cpp index 4c6714057..b648d1c60 100644 --- a/tests/fscript_run.cpp +++ b/tests/fscript_run.cpp @@ -40,22 +40,14 @@ static ret_t run_fscript(const char* code, uint32_t times, bool_t debug) { fscript_t* fscript = NULL; if (debug) { - str_t str; debugger_global_init(); debugger_server_tcp_init(DEBUGGER_TCP_PORT); debugger_server_tcp_start(); + debugger_server_set_single_mode(TRUE); sleep_ms(1000); - - str_init(&str, 100); - str_set(&str, code); - str_append_more(&str, "//code_id(\"", DEBUGGER_DEFAULT_CODE_ID, "\")\n", NULL); - fscript = fscript_create(obj, str.str); - log_debug("%s\n", str.str); - str_reset(&str); - } else { - fscript = fscript_create(obj, code); } + fscript = fscript_create(obj, code); fscript_exec(fscript, &v); fscript_destroy(fscript); @@ -85,7 +77,7 @@ static ret_t run_fscript_file(const char* filename, uint32_t times, bool_t debug int main(int argc, char* argv[]) { platform_prepare(); - tk_socket_init(); + tk_socket_init(); tk_mem_dump(); fscript_global_init(); fscript_ext_init(); @@ -123,7 +115,7 @@ int main(int argc, char* argv[]) { data_writer_factory_set(NULL); data_reader_factory_set(NULL); fscript_global_deinit(); - tk_socket_deinit(); + tk_socket_deinit(); return 0; }