connection: Add a thread ID to WAYLAND_DEBUG output.

If WAYLAND_DEBUG contains the token "thread_id", and gettid() is
available, then include the current thread ID in the output from
wl_closure_print.

If multiple threads are sending requests, then those requests can get
interleaved. That's usually fine, but for wl_surface requests and
commits, that can cause problems ranging from incorrect behavior to
protocol errors.

Being able to see which requests are sent by different threads would
make such problems much easier to diagnose.

Signed-off-by: Kyle Brenneman <kbrenneman@nvidia.com>
This commit is contained in:
Kyle Brenneman 2024-09-10 14:36:06 -06:00 committed by Daniel Stone
parent 77730f10a0
commit 4673ef7e9c
2 changed files with 18 additions and 0 deletions

View file

@ -46,6 +46,7 @@ have_funcs = [
'memfd_create', 'memfd_create',
'mremap', 'mremap',
'strndup', 'strndup',
'gettid',
] ]
foreach f: have_funcs foreach f: have_funcs
config_h.set('HAVE_' + f.underscorify().to_upper(), cc.has_function(f)) config_h.set('HAVE_' + f.underscorify().to_upper(), cc.has_function(f))

View file

@ -26,6 +26,8 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include "../config.h"
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
@ -1538,6 +1540,9 @@ wl_closure_print(struct wl_closure *closure, struct wl_object *target,
int send, int discarded, uint32_t (*n_parse)(union wl_argument *arg), int send, int discarded, uint32_t (*n_parse)(union wl_argument *arg),
const char *queue_name, int color) const char *queue_name, int color)
{ {
#if defined(HAVE_GETTID)
static int include_tid = -1;
#endif // defined(HAVE_GETTID)
int i; int i;
struct argument_details arg; struct argument_details arg;
const char *signature = closure->message->signature; const char *signature = closure->message->signature;
@ -1558,6 +1563,18 @@ wl_closure_print(struct wl_closure *closure, struct wl_object *target,
color ? WL_DEBUG_COLOR_GREEN : "", color ? WL_DEBUG_COLOR_GREEN : "",
time / 1000, time % 1000); time / 1000, time % 1000);
#if defined(HAVE_GETTID)
if (include_tid < 0) {
include_tid = wl_check_env_token(getenv("WAYLAND_DEBUG"), "thread_id");
}
if (include_tid) {
fprintf(f, "%sTID#%d ",
color ? WL_DEBUG_COLOR_CYAN : "",
(int) gettid());
}
#endif
if (queue_name) { if (queue_name) {
fprintf(f, "%s{%s} ", fprintf(f, "%s{%s} ",
color ? WL_DEBUG_COLOR_YELLOW : "", color ? WL_DEBUG_COLOR_YELLOW : "",