libsysprof-capture: add frame type for tracing

This is like sample but has an "enter/exit" flag with it. This can be
useful when you want to provide tracing instead of sampling. We use a
different frame type so that we can denote that this isn't traditional
sampling, and the flag can be used to find the next exit for the current
enter for calculating durations.

The entire stack trace is provided to make things easier on tools
which may want to deal with indirect functions that were not instrumented
but can be unwound. That may allow for tooling to give the user some
insight that it's not *just* this function entering, but some functions
before it were entered too.

This also adds a SysprofTracer instrument which will preload a
libsysprof-tracer-6.so into the process providing the
__cyg_profile_func_enter() and __cyg_profile_func_leave() hooks.
This commit is contained in:
Christian Hergert
2023-06-13 12:11:55 -07:00
parent 81eafb9232
commit 3a94170b0a
17 changed files with 369 additions and 9 deletions

View File

@ -175,6 +175,10 @@ sysprof_capture_cursor_foreach (SysprofCaptureCursor *self,
delegate = READ_DELEGATE (sysprof_capture_reader_read_sample);
break;
case SYSPROF_CAPTURE_FRAME_TRACE:
delegate = READ_DELEGATE (sysprof_capture_reader_read_trace);
break;
case SYSPROF_CAPTURE_FRAME_LOG:
delegate = READ_DELEGATE (sysprof_capture_reader_read_log);
break;

View File

@ -925,6 +925,52 @@ sysprof_capture_reader_read_sample (SysprofCaptureReader *self)
return sample;
}
const SysprofCaptureTrace *
sysprof_capture_reader_read_trace (SysprofCaptureReader *self)
{
SysprofCaptureTrace *trace;
assert (self != NULL);
assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
assert (self->pos <= self->bufsz);
if (!sysprof_capture_reader_ensure_space_for (self, sizeof *trace))
return NULL;
trace = (SysprofCaptureTrace *)(void *)&self->buf[self->pos];
sysprof_capture_reader_bswap_frame (self, &trace->frame);
if (trace->frame.type != SYSPROF_CAPTURE_FRAME_TRACE)
return NULL;
if (trace->frame.len < sizeof *trace)
return NULL;
if (self->endian != __BYTE_ORDER)
trace->n_addrs = bswap_16 (trace->n_addrs);
if (trace->frame.len < (sizeof *trace + (sizeof(SysprofCaptureAddress) * trace->n_addrs)))
return NULL;
if (!sysprof_capture_reader_ensure_space_for (self, trace->frame.len))
return NULL;
trace = (SysprofCaptureTrace *)(void *)&self->buf[self->pos];
if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER))
{
unsigned int i;
for (i = 0; i < trace->n_addrs; i++)
trace->addrs[i] = bswap_64 (trace->addrs[i]);
}
self->pos += trace->frame.len;
return trace;
}
const SysprofCaptureCounterDefine *
sysprof_capture_reader_read_counter_define (SysprofCaptureReader *self)
{

View File

@ -111,6 +111,8 @@ const SysprofCaptureProcess *sysprof_capture_reader_read_process (
SYSPROF_AVAILABLE_IN_ALL
const SysprofCaptureSample *sysprof_capture_reader_read_sample (SysprofCaptureReader *self);
SYSPROF_AVAILABLE_IN_ALL
const SysprofCaptureTrace *sysprof_capture_reader_read_trace (SysprofCaptureReader *self);
SYSPROF_AVAILABLE_IN_ALL
const SysprofCaptureJitmap *sysprof_capture_reader_read_jitmap (SysprofCaptureReader *self);
SYSPROF_AVAILABLE_IN_ALL
const SysprofCaptureCounterDefine *sysprof_capture_reader_read_counter_define (SysprofCaptureReader *self);

View File

@ -140,10 +140,11 @@ typedef enum
SYSPROF_CAPTURE_FRAME_FILE_CHUNK = 13,
SYSPROF_CAPTURE_FRAME_ALLOCATION = 14,
SYSPROF_CAPTURE_FRAME_OVERLAY = 15,
SYSPROF_CAPTURE_FRAME_TRACE = 16,
} SysprofCaptureFrameType;
/* Not part of ABI */
#define SYSPROF_CAPTURE_FRAME_LAST 16
#define SYSPROF_CAPTURE_FRAME_LAST 17
SYSPROF_ALIGNED_BEGIN(1)
typedef struct
@ -225,6 +226,18 @@ typedef struct
} SysprofCaptureSample
SYSPROF_ALIGNED_END(1);
SYSPROF_ALIGNED_BEGIN(1)
typedef struct
{
SysprofCaptureFrame frame;
uint32_t n_addrs : 16;
uint32_t entering : 1;
uint32_t padding1 : 15;
int32_t tid;
SysprofCaptureAddress addrs[0];
} SysprofCaptureTrace
SYSPROF_ALIGNED_END(1);
SYSPROF_ALIGNED_BEGIN(1)
typedef struct
{
@ -357,6 +370,7 @@ SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureMap) == 56, "SysprofCaptureMap chan
SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureJitmap) == 28, "SysprofCaptureJitmap changed size");
SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureProcess) == 24, "SysprofCaptureProcess changed size");
SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureSample) == 32, "SysprofCaptureSample changed size");
SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureTrace) == 32, "SysprofCaptureTrace changed size");
SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureFork) == 28, "SysprofCaptureFork changed size");
SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureExit) == 24, "SysprofCaptureExit changed size");
SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureTimestamp) == 24, "SysprofCaptureTimestamp changed size");
@ -373,6 +387,7 @@ SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureOverlay) == 32, "SysprofCaptureOver
SYSPROF_STATIC_ASSERT ((offsetof (SysprofCaptureAllocation, addrs) % SYSPROF_CAPTURE_ALIGN) == 0, "SysprofCaptureAllocation.addrs is not aligned");
SYSPROF_STATIC_ASSERT ((offsetof (SysprofCaptureSample, addrs) % SYSPROF_CAPTURE_ALIGN) == 0, "SysprofCaptureSample.addrs is not aligned");
SYSPROF_STATIC_ASSERT ((offsetof (SysprofCaptureTrace, addrs) % SYSPROF_CAPTURE_ALIGN) == 0, "SysprofCaptureTrace.addrs is not aligned");
static inline int
sysprof_capture_address_compare (SysprofCaptureAddress a,

View File

@ -416,6 +416,32 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self,
break;
}
case SYSPROF_CAPTURE_FRAME_TRACE:
{
const SysprofCaptureTrace *frame;
if (!(frame = sysprof_capture_reader_read_trace (reader)))
goto panic;
{
SysprofCaptureAddress addrs[frame->n_addrs];
for (unsigned int z = 0; z < frame->n_addrs; z++)
addrs[z] = translate_table_translate (tables, TRANSLATE_ADDR, frame->addrs[z]);
sysprof_capture_writer_add_trace (self,
frame->frame.time,
frame->frame.cpu,
frame->frame.pid,
frame->tid,
addrs,
frame->n_addrs,
frame->entering);
}
break;
}
case SYSPROF_CAPTURE_FRAME_CTRDEF:
{
const SysprofCaptureCounterDefine *frame;

View File

@ -798,6 +798,42 @@ sysprof_capture_writer_add_sample (SysprofCaptureWriter *self,
return true;
}
bool
sysprof_capture_writer_add_trace (SysprofCaptureWriter *self,
int64_t time,
int cpu,
int32_t pid,
int32_t tid,
const SysprofCaptureAddress *addrs,
unsigned int n_addrs,
bool entering)
{
SysprofCaptureTrace *ev;
size_t len;
assert (self != NULL);
len = sizeof *ev + (n_addrs * sizeof (SysprofCaptureAddress));
ev = (SysprofCaptureTrace *)sysprof_capture_writer_allocate (self, &len);
if (!ev)
return false;
sysprof_capture_writer_frame_init (&ev->frame,
len,
cpu,
pid,
time,
SYSPROF_CAPTURE_FRAME_SAMPLE);
ev->n_addrs = n_addrs;
ev->tid = tid;
ev->entering = !!entering;
memcpy (ev->addrs, addrs, (n_addrs * sizeof (SysprofCaptureAddress)));
return true;
}
bool
sysprof_capture_writer_add_fork (SysprofCaptureWriter *self,
int64_t time,
@ -1659,7 +1695,7 @@ _sysprof_capture_writer_add_raw (SysprofCaptureWriter *self,
return false;
assert (fr->len == len);
assert (fr->type < 16);
assert (fr->type < SYSPROF_CAPTURE_FRAME_LAST);
memcpy (begin, fr, fr->len);

View File

@ -143,6 +143,15 @@ bool sysprof_capture_writer_add_sample (Sy
const SysprofCaptureAddress *addrs,
unsigned int n_addrs);
SYSPROF_AVAILABLE_IN_ALL
bool sysprof_capture_writer_add_trace (SysprofCaptureWriter *self,
int64_t time,
int cpu,
int32_t pid,
int32_t tid,
const SysprofCaptureAddress *addrs,
unsigned int n_addrs,
bool entering);
SYSPROF_AVAILABLE_IN_ALL
bool sysprof_capture_writer_add_fork (SysprofCaptureWriter *self,
int64_t time,
int cpu,

View File

@ -576,6 +576,43 @@ sysprof_collector_sample (SysprofBacktraceFunc backtrace_func,
} COLLECTOR_END;
}
void
sysprof_collector_trace (SysprofBacktraceFunc backtrace_func,
void *backtrace_data,
bool entering)
{
COLLECTOR_BEGIN {
SysprofCaptureTrace *ev;
size_t len;
len = sizeof *ev + (sizeof (SysprofCaptureTrace) * MAX_UNWIND_DEPTH);
if ((ev = mapped_ring_buffer_allocate (collector->buffer, len)))
{
int n_addrs;
/* See comment from sysprof_collector_allocate(). */
if (backtrace_func)
n_addrs = backtrace_func (ev->addrs, MAX_UNWIND_DEPTH, backtrace_data);
else
n_addrs = 0;
ev->n_addrs = ((n_addrs < 0) ? 0 : (n_addrs > MAX_UNWIND_DEPTH) ? MAX_UNWIND_DEPTH : n_addrs);
ev->frame.len = sizeof *ev + sizeof (SysprofCaptureAddress) * ev->n_addrs;
ev->frame.type = SYSPROF_CAPTURE_FRAME_TRACE;
ev->frame.cpu = _do_getcpu ();
ev->frame.pid = collector->pid;
ev->frame.time = SYSPROF_CAPTURE_CURRENT_TIME;
ev->tid = collector->tid;
ev->entering = !!entering;
ev->padding1 = 0;
mapped_ring_buffer_advance (collector->buffer, ev->frame.len);
}
} COLLECTOR_END;
}
void
sysprof_collector_mark (int64_t time,
int64_t duration,

View File

@ -75,6 +75,10 @@ void sysprof_collector_allocate (SysprofCaptureAddress
SYSPROF_AVAILABLE_IN_3_36
void sysprof_collector_sample (SysprofBacktraceFunc backtrace_func,
void *backtrace_data);
SYSPROF_AVAILABLE_IN_ALL
void sysprof_collector_trace (SysprofBacktraceFunc backtrace_func,
void *backtrace_data,
bool entering);
SYSPROF_AVAILABLE_IN_3_36
void sysprof_collector_mark (int64_t time,
int64_t duration,