Merge branch 'github/fork/emersion/tracing' into 'master'

Add kernel tracing instrumentation

See merge request wlroots/wlroots!2791
This commit is contained in:
Simon Ser 2026-01-08 09:52:31 +00:00
commit fe99c730c8
5 changed files with 93 additions and 1 deletions

View file

@ -13,6 +13,7 @@ wlr_files += files(
'shm.c',
'time.c',
'token.c',
'trace.c',
'transform.c',
'utf8.c',
'version.c',

58
util/trace.c Normal file
View file

@ -0,0 +1,58 @@
#include <inttypes.h>
#include <stdio.h>
#include "util/trace.h"
static bool trace_initialized = false;
static FILE *trace_file = NULL;
static uint32_t prev_ctx = 0;
void wlr_vtrace(const char *fmt, va_list args) {
if (!trace_initialized) {
trace_initialized = true;
trace_file = fopen("/sys/kernel/tracing/trace_marker", "w");
if (trace_file != NULL) {
wlr_log(WLR_INFO, "Kernel tracing is enabled");
}
}
if (trace_file == NULL) {
return;
}
vfprintf(trace_file, fmt, args);
fprintf(trace_file, "\n");
fflush(trace_file);
}
void wlr_trace(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
wlr_vtrace(fmt, args);
va_end(args);
}
void wlr_trace_begin_ctx(struct wlr_trace_ctx *ctx, const char *fmt, ...) {
*ctx = (struct wlr_trace_ctx){
.seq = prev_ctx++,
};
static char new_fmt[512];
snprintf(new_fmt, sizeof(new_fmt), "%s (begin_ctx=%"PRIu32")", fmt, ctx->seq);
va_list args;
va_start(args, fmt);
wlr_vtrace(new_fmt, args);
va_end(args);
}
void wlr_trace_end_ctx(struct wlr_trace_ctx *ctx, const char *fmt, ...) {
static char new_fmt[512];
snprintf(new_fmt, sizeof(new_fmt), "%s (end_ctx=%"PRIu32")", fmt, ctx->seq);
va_list args;
va_start(args, fmt);
wlr_vtrace(new_fmt, args);
va_end(args);
ctx->seq = 0;
}