Skip to content

Commit

Permalink
Some debug tools (#704)
Browse files Browse the repository at this point in the history
* machinarium: add machine_get_backtrace*

This functions will fix bugs by printing backtraces

Signed-off-by: rkhapov <[email protected]>

* 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 <[email protected]>

---------

Signed-off-by: rkhapov <[email protected]>
Co-authored-by: rkhapov <[email protected]>
  • Loading branch information
rkhapov and rkhapov authored Oct 21, 2024
1 parent 72e4543 commit 81c16a5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
16 changes: 11 additions & 5 deletions sources/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions third_party/machinarium/sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(machine_src
close.c
connect.c
bind.c
backtrace.c
eventfd.c
cond.c
read.c
Expand Down
58 changes: 58 additions & 0 deletions third_party/machinarium/sources/backtrace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <machinarium.h>

#include <stdio.h>
#include <dlfcn.h>
#include <execinfo.h>

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;
}
7 changes: 7 additions & 0 deletions third_party/machinarium/sources/machinarium.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 81c16a5

Please sign in to comment.