libsysprof-capture: allow for backtrace skip optimization

We want to be backtracing directly into the capture buffer, but also need
to skip a small number of frames.

If we call the backtrace before filling in information, we can capture to
the position *before* ev->addrs and then overwrite that data right after.
This commit is contained in:
Christian Hergert
2020-02-18 13:35:18 -08:00
parent b6dc058d62
commit 70bea64f88
2 changed files with 26 additions and 10 deletions

View File

@ -384,6 +384,19 @@ sysprof_collector_allocate (SysprofCaptureAddress alloc_addr,
if ((ev = mapped_ring_buffer_allocate (collector->buffer, len)))
{
/* First take a backtrace, so that backtrace_func() can overwrite
* a little bit of data *BEFORE* ev->addrs as stratch space. This
* is useful to allow using unw_backtrace() or backtrace() to skip
* a small number of frames.
*
* We fill in all the other data afterwards which overwrites that
* scratch space anyway.
*/
if (backtrace_func)
ev->n_addrs = backtrace_func (ev->addrs, MAX_UNWIND_DEPTH, backtrace_data);
else
ev->n_addrs = 0;
ev->frame.type = SYSPROF_CAPTURE_FRAME_ALLOCATION;
ev->frame.len = len;
ev->frame.cpu = _do_getcpu ();
@ -392,10 +405,6 @@ sysprof_collector_allocate (SysprofCaptureAddress alloc_addr,
ev->tid = collector->tid;
ev->alloc_addr = alloc_addr;
ev->alloc_size = alloc_size;
ev->n_addrs = 0;
if (backtrace_func)
ev->n_addrs = backtrace_func (ev->addrs, MAX_UNWIND_DEPTH, backtrace_data);
len = sizeof *ev + sizeof (SysprofCaptureAddress) * ev->n_addrs;
_realign (&len);