From 81c16a5df959a05eca88c55347aa102ba6e3ec8c Mon Sep 17 00:00:00 2001 From: Roman Khapov Date: Mon, 21 Oct 2024 14:06:22 +0500 Subject: [PATCH] Some debug tools (#704) * machinarium: add machine_get_backtrace* This functions will fix bugs by printing backtraces Signed-off-by: rkhapov * sources/console.c: clients table improvements 1) rename 'ptr' to 'id' which is more obvious 2) replace useless 'link' with 'ptr' which can be used for debugging 3) add column 'coro' with coroutine id of the client, which can be used for debugging too Signed-off-by: rkhapov --------- Signed-off-by: rkhapov Co-authored-by: rkhapov --- sources/console.c | 16 +++-- .../machinarium/sources/CMakeLists.txt | 1 + third_party/machinarium/sources/backtrace.c | 58 +++++++++++++++++++ third_party/machinarium/sources/machinarium.h | 7 +++ 4 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 third_party/machinarium/sources/backtrace.c diff --git a/sources/console.c b/sources/console.c index 527a60c4f..dba1fc523 100644 --- a/sources/console.c +++ b/sources/console.c @@ -1339,18 +1339,24 @@ static inline int od_console_show_clients_callback(od_client_t *client, rc = kiwi_be_write_data_row_add(stream, offset, data, data_len); if (rc == NOT_OK_RESPONSE) return NOT_OK_RESPONSE; - /* ptr */ + /* id */ data_len = od_snprintf(data, sizeof(data), "%s%.*s", client->id.id_prefix, (signed)sizeof(client->id.id), client->id.id); rc = kiwi_be_write_data_row_add(stream, offset, data, data_len); if (rc == NOT_OK_RESPONSE) return NOT_OK_RESPONSE; - /* link */ - data_len = od_snprintf(data, sizeof(data), "%s", ""); + /* ptr */ + data_len = od_snprintf(data, sizeof(data), "%p", client); rc = kiwi_be_write_data_row_add(stream, offset, data, data_len); if (rc == NOT_OK_RESPONSE) return NOT_OK_RESPONSE; + /* coro */ + data_len = od_snprintf(data, sizeof(data), "%d", client->coroutine_id); + rc = kiwi_be_write_data_row_add(stream, offset, data, data_len); + if (rc == NOT_OK_RESPONSE) { + return NOT_OK_RESPONSE; + } /* remote_pid */ data_len = od_snprintf(data, sizeof(data), "0"); rc = kiwi_be_write_data_row_add(stream, offset, data, data_len); @@ -1390,10 +1396,10 @@ static inline int od_console_show_clients(od_client_t *client, machine_msg_t *msg; msg = kiwi_be_write_row_descriptionf( - stream, "ssssssdsdssddssds", "type", "user", "database", + stream, "ssssssdsdssddssdds", "type", "user", "database", "state", "storage_user", "addr", "port", "local_addr", "local_port", "connect_time", "request_time", "wait", "wait_us", - "ptr", "link", "remote_pid", "tls"); + "id", "ptr", "coro", "remote_pid", "tls"); if (msg == NULL) return NOT_OK_RESPONSE; diff --git a/third_party/machinarium/sources/CMakeLists.txt b/third_party/machinarium/sources/CMakeLists.txt index a51f92722..d08c5672e 100644 --- a/third_party/machinarium/sources/CMakeLists.txt +++ b/third_party/machinarium/sources/CMakeLists.txt @@ -32,6 +32,7 @@ set(machine_src close.c connect.c bind.c + backtrace.c eventfd.c cond.c read.c diff --git a/third_party/machinarium/sources/backtrace.c b/third_party/machinarium/sources/backtrace.c new file mode 100644 index 000000000..ff00a34b2 --- /dev/null +++ b/third_party/machinarium/sources/backtrace.c @@ -0,0 +1,58 @@ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include + +#include +#include +#include + +MACHINE_API int machine_get_backtrace(void **entries, int max) +{ + return backtrace(entries, max); +} + +#define MM_BACKTRACE_STRING_N_ENTRIES 15 + +__thread char backtrace_string[MM_BACKTRACE_STRING_N_ENTRIES * 40]; + +MACHINE_API const char *machine_get_backtrace_string() +{ + void *bt[MM_BACKTRACE_STRING_N_ENTRIES]; + int nentries = machine_get_backtrace(bt, MM_BACKTRACE_STRING_N_ENTRIES); + + if (nentries <= 0) { + return NULL; + } + + char *wptr = backtrace_string; + for (int i = 0; i < nentries; ++i) { + wptr += sprintf(wptr, "%p ", bt[i]); + } + + wptr += sprintf(wptr, "("); + + for (int i = 0; i < nentries; ++i) { + void *addr = bt[i]; + + Dl_info info; + if (dladdr(addr, &info) == 0) { + wptr += sprintf(wptr, "[unknown]"); + } else { + void *calibrated = (void *)((uintptr_t)addr - + (uintptr_t)info.dli_fbase); + + wptr += sprintf(wptr, "%p", calibrated); + } + + if (i != nentries - 1) { + wptr += sprintf(wptr, " "); + } + } + + wptr += sprintf(wptr, ")"); + *wptr = '\0'; + + return backtrace_string; +} diff --git a/third_party/machinarium/sources/machinarium.h b/third_party/machinarium/sources/machinarium.h index c0c5b0a02..a79c12923 100644 --- a/third_party/machinarium/sources/machinarium.h +++ b/third_party/machinarium/sources/machinarium.h @@ -295,6 +295,13 @@ MACHINE_API ssize_t machine_tls_cert_hash( MACHINE_API char machine_compression_choose_alg(char *client_compression_algorithms); +/* debug tools */ + +// note: backtrace functions are currently slow +// if you want bt collection to be fast, impl should be rewritten +MACHINE_API const char *machine_get_backtrace_string(); +MACHINE_API int machine_get_backtrace(void **entries, int max); + #ifdef __cplusplus } #endif