diff --git a/src/trace.c b/src/trace.c index f898bc0f..5a44dca9 100644 --- a/src/trace.c +++ b/src/trace.c @@ -34,6 +34,7 @@ typedef struct _tf_trace_stack_t typedef struct _tf_trace_thread_t { pthread_t id; + int index; tf_trace_stack_t* stack; char name[64]; } tf_trace_thread_t; @@ -208,6 +209,7 @@ static tf_trace_thread_t* _tf_trace_get_thread(tf_trace_t* trace, pthread_t self found = tf_malloc(sizeof(tf_trace_thread_t)); *found = (tf_trace_thread_t) { .id = self, + .index = trace->threads_count, }; #if defined(__linux__) && !defined(__ANDROID__) pthread_getname_np(self, found->name, sizeof(found->name)); @@ -221,7 +223,7 @@ static tf_trace_thread_t* _tf_trace_get_thread(tf_trace_t* trace, pthread_t self return found; } -static void _tf_push_stack(tf_trace_t* trace, pthread_t self, const char* name, void* tag) +static void _tf_push_stack(tf_trace_t* trace, pthread_t self, const char* name, void* tag, tf_trace_thread_t** out_thread) { tf_trace_thread_t* thread = _tf_trace_get_thread(trace, self); if (!thread->stack || thread->stack->count + 1 > tf_countof(thread->stack->names)) @@ -239,9 +241,10 @@ static void _tf_push_stack(tf_trace_t* trace, pthread_t self, const char* name, stack->names[stack->count] = name; stack->tags[stack->count] = tag; stack->count++; + *out_thread = thread; } -static const char* _tf_pop_stack(tf_trace_t* trace, pthread_t self, void* tag) +static const char* _tf_pop_stack(tf_trace_t* trace, pthread_t self, void* tag, tf_trace_thread_t** out_thread) { tf_trace_thread_t* thread = _tf_trace_get_thread(trace, self); tf_trace_stack_t* stack = thread->stack; @@ -255,6 +258,7 @@ static const char* _tf_pop_stack(tf_trace_t* trace, pthread_t self, void* tag) name = stack->names[stack->count - 1]; stack->count--; } + *out_thread = thread; return name; } @@ -265,11 +269,11 @@ static void _tf_trace_begin_tagged(tf_trace_t* trace, const char* name, void* ta return; } - pthread_t self = pthread_self(); - _tf_push_stack(trace, self, name, tag); + tf_trace_thread_t* thread = NULL; + _tf_push_stack(trace, pthread_self(), name, tag, &thread); char line[1024]; - int p = snprintf(line, sizeof(line), "{\"ph\": \"B\", \"pid\": %d, \"tid\": %" PRId64 ", \"ts\": %" PRId64 ", \"name\": \"", getpid(), (int64_t)self, _trace_ts()); + int p = snprintf(line, sizeof(line), "{\"ph\": \"B\", \"pid\": %d, \"tid\": %d, \"ts\": %" PRId64 ", \"name\": \"", getpid(), thread->index, _trace_ts()); p += _tf_trace_escape_name(line + p, sizeof(line) - p - 4, name); p += snprintf(line + p, sizeof(line) - p, "\"},"); p = tf_min(p, tf_countof(line)); @@ -288,15 +292,15 @@ static void _tf_trace_end_tagged(tf_trace_t* trace, void* tag) return; } - pthread_t self = pthread_self(); - const char* name = _tf_pop_stack(trace, self, tag); + tf_trace_thread_t* thread = NULL; + const char* name = _tf_pop_stack(trace, pthread_self(), tag, &thread); if (!name) { return; } char line[1024]; - int p = snprintf(line, sizeof(line), "{\"ph\": \"E\", \"pid\": %d, \"tid\": %" PRId64 ", \"ts\": %" PRId64 ", \"name\": \"", getpid(), (int64_t)pthread_self(), _trace_ts()); + int p = snprintf(line, sizeof(line), "{\"ph\": \"E\", \"pid\": %d, \"tid\": %d, \"ts\": %" PRId64 ", \"name\": \"", getpid(), thread->index, _trace_ts()); p += _tf_trace_escape_name(line + p, sizeof(line) - p - 4, name); p += snprintf(line + p, sizeof(line) - p, "\"},"); p = tf_min(p, tf_countof(line)); @@ -330,8 +334,8 @@ char* tf_trace_export(tf_trace_t* trace) for (int i = 0; i < trace->threads_count; i++) { tf_trace_thread_t* thread = trace->threads[i]; - size += snprintf(buffer + size, k_buffer_size - size, "{\"ph\":\"M\",\"pid\":%d,\"tid\":%" PRId64 ",\"name\":\"thread_name\",\"args\":{\"name\":\"%s\"}},\n", getpid(), - (uint64_t)thread->id, thread->name); + size += snprintf(buffer + size, k_buffer_size - size, "{\"ph\":\"M\",\"pid\":%d,\"tid\":%d,\"name\":\"thread_name\",\"args\":{\"name\":\"%s\"}},\n", getpid(), + thread->index, thread->name); } uv_rwlock_rdunlock(&trace->threads_lock); if (begin)