From dd87426d535c3e9c6b5e8c86b01727f6d9ae0a0a Mon Sep 17 00:00:00 2001 From: Sebastian Reimers <hallo@studio-link.de> Date: Wed, 7 Feb 2024 21:31:35 +0100 Subject: [PATCH] trace: use heap json_arg --- src/trace/trace.c | 38 +++++++++++++++++++++++++++----------- test/trace.c | 2 ++ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/trace/trace.c b/src/trace/trace.c index 832d0f977..397d48989 100644 --- a/src/trace/trace.c +++ b/src/trace/trace.c @@ -164,7 +164,7 @@ int re_trace_init(const char *json_file) trace.event_buffer_flush = mem_zalloc( TRACE_BUFFER_SIZE * sizeof(struct trace_event), NULL); if (!trace.event_buffer_flush) { - mem_deref(trace.event_buffer); + trace.event_buffer = mem_deref(trace.event_buffer); return ENOMEM; } @@ -191,8 +191,8 @@ int re_trace_init(const char *json_file) out: if (err) { re_atomic_rlx_set(&trace.init, false); - mem_deref(trace.event_buffer); - mem_deref(trace.event_buffer_flush); + trace.event_buffer = mem_deref(trace.event_buffer); + trace.event_buffer_flush = mem_deref(trace.event_buffer_flush); } return err; @@ -245,12 +245,12 @@ int re_trace_close(void) int re_trace_flush(void) { #ifdef RE_TRACE_ENABLED - int i, flush_count; + int flush_count; struct trace_event *event_tmp; struct trace_event *e; - char json_arg[256] = {0}; - char name[128] = {0}; - char id_str[128] = {0}; + char *json_arg; + char name[128] = {0}; + char id_str[128] = {0}; if (!re_atomic_rlx(&trace.init)) return 0; @@ -264,7 +264,21 @@ int re_trace_flush(void) trace.event_count = 0; mtx_unlock(&trace.lock); - for (i = 0; i < flush_count; i++) + size_t json_arg_sz = 4096; + json_arg = mem_zalloc(json_arg_sz, NULL); + if (!json_arg) { + for (int i = 0; i < flush_count; i++) { + e = &trace.event_buffer_flush[i]; + if (e->arg_type == RE_TRACE_ARG_STRING_COPY) + mem_deref((void *)e->arg.a_str); + + if (e->id) + mem_deref(e->id); + } + return ENOMEM; + } + + for (int i = 0; i < flush_count; i++) { e = &trace.event_buffer_flush[i]; @@ -273,17 +287,17 @@ int re_trace_flush(void) json_arg[0] = '\0'; break; case RE_TRACE_ARG_INT: - (void)re_snprintf(json_arg, sizeof(json_arg), + (void)re_snprintf(json_arg, json_arg_sz, ", \"args\":{\"%s\":%i}", e->arg_name, e->arg.a_int); break; case RE_TRACE_ARG_STRING_CONST: - (void)re_snprintf(json_arg, sizeof(json_arg), + (void)re_snprintf(json_arg, json_arg_sz, ", \"args\":{\"%s\":\"%s\"}", e->arg_name, e->arg.a_str); break; case RE_TRACE_ARG_STRING_COPY: - (void)re_snprintf(json_arg, sizeof(json_arg), + (void)re_snprintf(json_arg, json_arg_sz, ", \"args\":{\"%s\":\"%s\"}", e->arg_name, e->arg.a_str); @@ -310,6 +324,8 @@ int re_trace_flush(void) trace.new = false; } + mem_deref(json_arg); + (void)fflush(trace.f); return 0; #else diff --git a/test/trace.c b/test/trace.c index be85c828e..6966be3e2 100644 --- a/test/trace.c +++ b/test/trace.c @@ -67,5 +67,7 @@ int test_trace(void) #endif out: + if (err) + re_trace_close(); return err; }