This is a new interface for unwinding that doesn't require the Dwfl to
be attached to a live process (via ptrace) or via corefile. Instead,
data from a perf_events stack sample is provided along with an Elf
struct used to identify the architecture. Based on code from
eu-stacktrace.

* libdwfl/libdwfl.h (dwfl_perf_sample_getframes): New function.
* libdwfl/dwfl_perf_frame.c: New file.
  (struct __libdwfl_perf_sample_info): New struct, based on
  src/stacktrace.c struct sample_arg.
  (sample_next_thread): New function, based on src/stacktrace.c.
  (sample_getthread): Ditto.
  (copy_word_64): New macro, based on src/stacktrace.c.
  (copy_word_32): Ditto.
  (copy_word): Ditto.
  (elf_memory_read): New function, based on src/stacktrace.c.
  (sample_memory_read): Ditto.
  (sample_set_initial_registers): Ditto.
  (sample_detach): Ditto.
  (sample_thread_callbacks): New struct, set of callbacks based on
  src/stacktrace.c sample_thread_callbacks.
  (dwfl_perf_sample_getframes): New function, based on parts of
  src/stacktrace.c sysprof_find_dwfl. If the Dwfl is not attached,
  attaches it with sample_thread_callbacks and
  __libdwfl_perf_sample_info. Populates the __libdwfl_perf_sample_info
  with data from the stack sample and calls dwfl_getthread_frames to
  unwind it using the sample_thread_callbacks.
* libdw/libdw.map (ELFUTILS_0.193): Add dwfl_perf_sample_getframes.
* libdwfl/Makefile.am (libdwfl_a_SOURCES): Add dwfl_perf_frame.c.
---
 libdw/libdw.map           |   1 +
 libdwfl/dwfl_perf_frame.c | 193 +++++++++++++++++++++++++++++++++++++-
 libdwfl/libdwfl.h         |  15 ++-
 3 files changed, 207 insertions(+), 2 deletions(-)

diff --git a/libdw/libdw.map b/libdw/libdw.map
index 502be882..d92e5365 100644
--- a/libdw/libdw.map
+++ b/libdw/libdw.map
@@ -395,4 +395,5 @@ ELFUTILS_0.193 {
     dwfl_process_tracker_end;
     dwfl_process_tracker_find_elf;
     dwfl_process_tracker_find_pid;
+    dwfl_perf_sample_getframes;
 } ELFUTILS_0.192;
diff --git a/libdwfl/dwfl_perf_frame.c b/libdwfl/dwfl_perf_frame.c
index 87db97bc..b962ace3 100644
--- a/libdwfl/dwfl_perf_frame.c
+++ b/libdwfl/dwfl_perf_frame.c
@@ -58,4 +58,195 @@ uint64_t dwfl_perf_sample_preferred_regs_mask (GElf_Half 
machine)
   return 0;
 }
 
-/* XXX dwfl_perf_sample_getframes to be added in subsequent patch */
+struct __libdwfl_perf_sample_info {
+  pid_t pid;
+  pid_t tid;
+  Dwarf_Addr base_addr;
+  uint8_t *stack;
+  size_t stack_size;
+  const Dwarf_Word *regs;
+  uint n_regs;
+  uint64_t perf_regs_mask;
+  uint abi;
+  Dwarf_Addr pc;
+};
+
+/* The next few functions imitate the corefile interface for a single
+   stack sample, with very restricted access to registers and memory. */
+
+/* Just yield the single thread id matching the sample. */
+static pid_t
+sample_next_thread (Dwfl *dwfl __attribute__ ((unused)), void *dwfl_arg,
+                   void **thread_argp)
+{
+  struct __libdwfl_perf_sample_info *sample_arg =
+    (struct __libdwfl_perf_sample_info *)dwfl_arg;
+  if (*thread_argp == NULL)
+    {
+      *thread_argp = (void *)0xea7b3375;
+      return sample_arg->tid;
+    }
+  else
+    return 0;
+}
+
+/* Just check that the thread id matches the sample. */
+static bool
+sample_getthread (Dwfl *dwfl __attribute__ ((unused)), pid_t tid,
+                 void *dwfl_arg, void **thread_argp)
+{
+  struct __libdwfl_perf_sample_info *sample_arg =
+    (struct __libdwfl_perf_sample_info *)dwfl_arg;
+  *thread_argp = (void *)sample_arg;
+  if (sample_arg->tid != tid)
+    {
+      __libdwfl_seterrno(DWFL_E_INVALID_ARGUMENT);
+      return false;
+    }
+  return true;
+}
+
+#define copy_word_64(result, d) \
+  if ((((uintptr_t) (d)) & (sizeof (uint64_t) - 1)) == 0) \
+    *(result) = *(uint64_t *)(d); \
+  else \
+    memcpy ((result), (d), sizeof (uint64_t));
+
+#define copy_word_32(result, d) \
+  if ((((uintptr_t) (d)) & (sizeof (uint32_t) - 1)) == 0) \
+    *(result) = *(uint32_t *)(d); \
+  else \
+    memcpy ((result), (d), sizeof (uint32_t));
+
+#define copy_word(result, d, abi) \
+  if ((abi) == PERF_SAMPLE_REGS_ABI_64)        \
+    { copy_word_64((result), (d)); } \
+  else if ((abi) == PERF_SAMPLE_REGS_ABI_32) \
+    { copy_word_32((result), (d)); } \
+  else \
+    *(result) = 0;
+
+static bool
+elf_memory_read (Dwfl *dwfl, Dwarf_Addr addr, Dwarf_Word *result, void *arg)
+{
+  struct __libdwfl_perf_sample_info *sample_arg =
+    (struct __libdwfl_perf_sample_info *)arg;
+  Dwfl_Module *mod = INTUSE(dwfl_addrmodule) (dwfl, addr);
+  Dwarf_Addr bias;
+  Elf_Scn *section = INTUSE(dwfl_module_address_section) (mod, &addr, &bias);
+
+  if (!section)
+    {
+      __libdwfl_seterrno(DWFL_E_ADDR_OUTOFRANGE);
+      return false;
+    }
+
+  Elf_Data *data = elf_getdata(section, NULL);
+  if (data && data->d_buf && data->d_size > addr) {
+    uint8_t *d = ((uint8_t *)data->d_buf) + addr;
+    copy_word(result, d, sample_arg->abi);
+    return true;
+  }
+  __libdwfl_seterrno(DWFL_E_ADDR_OUTOFRANGE);
+  return false;
+}
+
+static bool
+sample_memory_read (Dwfl *dwfl, Dwarf_Addr addr, Dwarf_Word *result, void *arg)
+{
+  struct __libdwfl_perf_sample_info *sample_arg =
+    (struct __libdwfl_perf_sample_info *)arg;
+  /* Imitate read_cached_memory() with the stack sample data as the cache. */
+  if (addr < sample_arg->base_addr ||
+      addr - sample_arg->base_addr >= sample_arg->stack_size)
+    return elf_memory_read(dwfl, addr, result, arg);
+  uint8_t *d = &sample_arg->stack[addr - sample_arg->base_addr];
+  copy_word(result, d, sample_arg->abi);
+  return true;
+}
+
+static bool
+sample_set_initial_registers (Dwfl_Thread *thread, void *arg)
+{
+  struct __libdwfl_perf_sample_info *sample_arg =
+    (struct __libdwfl_perf_sample_info *)arg;
+  dwfl_thread_state_register_pc (thread, sample_arg->pc);
+  Dwfl_Process *process = thread->process;
+  Ebl *ebl = process->ebl;
+  /* XXX Sysprof provides exactly the required registers for unwinding: */
+  uint64_t regs_mask = ebl_perf_frame_regs_mask (ebl);
+  return ebl_set_initial_registers_sample
+    (ebl, sample_arg->regs, sample_arg->n_regs, regs_mask, sample_arg->abi,
+     dwfl_set_initial_registers_thread, thread);
+}
+
+static void
+sample_detach (Dwfl *dwfl __attribute__ ((unused)), void *dwfl_arg)
+{
+  struct __libdwfl_perf_sample_info *sample_arg =
+    (struct __libdwfl_perf_sample_info *)dwfl_arg;
+  free (sample_arg);
+}
+
+static const Dwfl_Thread_Callbacks sample_thread_callbacks =
+  {
+    sample_next_thread,
+    sample_getthread,
+    sample_memory_read,
+    sample_set_initial_registers,
+    sample_detach,
+    NULL, /* sample_thread_detach */
+  };
+
+int dwfl_perf_sample_getframes (Dwfl *dwfl, Elf *elf,
+                               pid_t pid, pid_t tid,
+                               void *stack, size_t stack_size,
+                               const Dwarf_Word *regs, uint n_regs,
+                               uint64_t perf_regs_mask, uint abi,
+                               int (*callback) (Dwfl_Frame *state, void *arg),
+                               void *arg)
+{
+  /* TODO: Lock the dwfl to ensure attach_state does not interfere
+     with other dwfl_perf_sample_getframes calls. */
+
+  struct __libdwfl_perf_sample_info *sample_arg;
+  bool attached = false;
+  if (dwfl->process != NULL)
+    {
+      sample_arg = dwfl->process->callbacks_arg;
+      attached = true;
+    }
+  else
+    {
+      sample_arg = malloc (sizeof *sample_arg);
+      if (sample_arg == NULL)
+       {
+         __libdwfl_seterrno(DWFL_E_NOMEM);
+         return -1;
+       }
+    }
+
+  sample_arg->pid = pid;
+  sample_arg->tid = tid;
+  sample_arg->stack = (uint8_t *)stack;
+  sample_arg->stack_size = stack_size;
+  sample_arg->regs = regs;
+  sample_arg->n_regs = n_regs;
+  sample_arg->perf_regs_mask = perf_regs_mask;
+  sample_arg->abi = abi;
+
+  if (! attached
+      && ! INTUSE(dwfl_attach_state) (dwfl, elf, pid,
+                                     &sample_thread_callbacks, sample_arg))
+      return -1;
+
+  /* Now that Dwfl is attached, we can access its Ebl: */
+  Dwfl_Process *process = dwfl->process;
+  Ebl *ebl = process->ebl;
+  sample_arg->base_addr = ebl_sample_base_addr(ebl, regs, n_regs,
+                                              perf_regs_mask, abi);
+  sample_arg->pc = ebl_sample_pc(ebl, regs, n_regs,
+                                perf_regs_mask, abi);
+
+  return INTUSE(dwfl_getthread_frames) (dwfl, tid, callback, arg);
+}
diff --git a/libdwfl/libdwfl.h b/libdwfl/libdwfl.h
index c6d0f02e..060f4ba6 100644
--- a/libdwfl/libdwfl.h
+++ b/libdwfl/libdwfl.h
@@ -858,7 +858,20 @@ int dwfl_getthread_frames (Dwfl *dwfl, pid_t tid,
                           void *arg)
   __nonnull_attribute__ (1, 3);
 
-/* XXX dwfl_perf_sample_getframes to be added in subsequent patch */
+/* Like dwfl_thread_getframes, but iterates through the frames for a
+   linux perf_events stack sample rather than a live thread.  Calls
+   dwfl_attach_state on DWFL, with architecture specified by ELF, ELF
+   must remain valid during Dwfl lifetime.  Returns zero if all frames
+   have been processed by the callback, returns -1 on error, or the
+   value of the callback when not DWARF_CB_OK.  -1 returned on error
+   will set dwfl_errno ().  */
+int dwfl_perf_sample_getframes (Dwfl *dwfl, Elf *elf, pid_t pid, pid_t tid,
+                                void *stack, size_t stack_size,
+                                const Dwarf_Word *regs, uint32_t n_regs,
+                                uint64_t perf_regs_mask, uint32_t abi,
+                                int (*callback) (Dwfl_Frame *state, void *arg),
+                                void *arg)
+  __nonnull_attribute__ (1, 5, 7, 11);
 
 /* Returns the linux perf_events register mask describing a set of
    registers sufficient for unwinding on MACHINE, or 0 if libdwfl does
-- 
2.47.0

Reply via email to