Blame tests/backtrace-dwarf.c

Packit 032894
/* Test program for unwinding of complicated DWARF expressions.
Packit 032894
   Copyright (C) 2013, 2015, 2018 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of the GNU General Public License as published by
Packit 032894
   the Free Software Foundation; either version 3 of the License, or
Packit 032894
   (at your option) any later version.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 032894
   GNU General Public License for more details.
Packit 032894
Packit 032894
   You should have received a copy of the GNU General Public License
Packit 032894
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#include <config.h>
Packit 032894
#include <inttypes.h>
Packit 032894
#include <stdio_ext.h>
Packit 032894
#include <locale.h>
Packit 032894
#include <errno.h>
Packit 032894
#include <unistd.h>
Packit 032894
#include <sys/types.h>
Packit 032894
#include ELFUTILS_HEADER(dwfl)
Packit 032894
#include "system.h"
Packit 032894
Packit 032894
#ifndef __linux__
Packit 032894
Packit 032894
int
Packit 032894
main (int argc __attribute__ ((unused)), char **argv)
Packit 032894
{
Packit 032894
  fprintf (stderr, "%s: Unwinding not supported for this architecture\n",
Packit 032894
           argv[0]);
Packit 032894
  return 77;
Packit 032894
}
Packit 032894
Packit 032894
#else /* __linux__ */
Packit 032894
#include <sys/ptrace.h>
Packit 032894
#include <sys/wait.h>
Packit 032894
#include <signal.h>
Packit 032894
Packit 032894
#define main cleanup_13_main
Packit 032894
#include "cleanup-13.c"
Packit 032894
#undef main
Packit 032894
Packit 032894
static void
Packit 032894
report_pid (Dwfl *dwfl, pid_t pid)
Packit 032894
{
Packit 032894
  int result = dwfl_linux_proc_report (dwfl, pid);
Packit 032894
  if (result < 0)
Packit 032894
    error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1));
Packit 032894
  else if (result > 0)
Packit 032894
    error (2, result, "dwfl_linux_proc_report");
Packit 032894
Packit 032894
  if (dwfl_report_end (dwfl, NULL, NULL) != 0)
Packit 032894
    error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));
Packit 032894
Packit 032894
  result = dwfl_linux_proc_attach (dwfl, pid, true);
Packit 032894
  if (result < 0)
Packit 032894
    error (2, 0, "dwfl_linux_proc_attach: %s", dwfl_errmsg (-1));
Packit 032894
  else if (result > 0)
Packit 032894
    error (2, result, "dwfl_linux_proc_attach");
Packit 032894
}
Packit 032894
Packit 032894
static Dwfl *
Packit 032894
pid_to_dwfl (pid_t pid)
Packit 032894
{
Packit 032894
  static char *debuginfo_path;
Packit 032894
  static const Dwfl_Callbacks proc_callbacks =
Packit 032894
    {
Packit 032894
      .find_debuginfo = dwfl_standard_find_debuginfo,
Packit 032894
      .debuginfo_path = &debuginfo_path,
Packit 032894
Packit 032894
      .find_elf = dwfl_linux_proc_find_elf,
Packit 032894
    };
Packit 032894
  Dwfl *dwfl = dwfl_begin (&proc_callbacks);
Packit 032894
  if (dwfl == NULL)
Packit 032894
    error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
Packit 032894
  report_pid (dwfl, pid);
Packit 032894
  return dwfl;
Packit 032894
}
Packit 032894
Packit 032894
static int
Packit 032894
frame_callback (Dwfl_Frame *state, void *frame_arg)
Packit 032894
{
Packit 032894
  Dwarf_Addr pc;
Packit 032894
  bool isactivation;
Packit 032894
  if (! dwfl_frame_pc (state, &pc, &isactivation))
Packit 032894
    {
Packit 032894
      error (0, 0, "%s", dwfl_errmsg (-1));
Packit 032894
      return DWARF_CB_ABORT;
Packit 032894
    }
Packit 032894
  Dwarf_Addr pc_adjusted = pc - (isactivation ? 0 : 1);
Packit 032894
Packit 032894
  /* Get PC->SYMNAME.  */
Packit 032894
  Dwfl_Thread *thread = dwfl_frame_thread (state);
Packit 032894
  Dwfl *dwfl = dwfl_thread_dwfl (thread);
Packit 032894
  Dwfl_Module *mod = dwfl_addrmodule (dwfl, pc_adjusted);
Packit 032894
  const char *symname = NULL;
Packit 032894
  if (mod)
Packit 032894
    symname = dwfl_module_addrname (mod, pc_adjusted);
Packit 032894
Packit 032894
  printf ("%#" PRIx64 "\t%s\n", (uint64_t) pc, symname ?: "<null>");
Packit 032894
Packit 032894
  if (symname && (strcmp (symname, "main") == 0
Packit 032894
		  || strcmp (symname, ".main") == 0))
Packit 032894
    {
Packit 032894
      kill (dwfl_pid (dwfl), SIGKILL);
Packit 032894
      exit (0);
Packit 032894
    }
Packit 032894
Packit 032894
  return DWARF_CB_OK;
Packit 032894
}
Packit 032894
Packit 032894
static int
Packit 032894
thread_callback (Dwfl_Thread *thread, void *thread_arg)
Packit 032894
{
Packit 032894
  if (dwfl_thread_getframes (thread, frame_callback, NULL) == -1)
Packit 032894
    error (1, 0, "dwfl_thread_getframes: %s", dwfl_errmsg (-1));
Packit 032894
Packit 032894
  /* frame_callback shall exit (0) on success.  */
Packit 032894
  printf ("dwfl_thread_getframes returned, main not found\n");
Packit 032894
  return DWARF_CB_ABORT;
Packit 032894
}
Packit 032894
Packit 032894
int
Packit 032894
__attribute__((section(".main"))) /* Defeat -freorder-blocks-and-partition  */
Packit 032894
main (int argc __attribute__ ((unused)), char **argv)
Packit 032894
{
Packit 032894
  /* We use no threads here which can interfere with handling a stream.  */
Packit 032894
  __fsetlocking (stdin, FSETLOCKING_BYCALLER);
Packit 032894
  __fsetlocking (stdout, FSETLOCKING_BYCALLER);
Packit 032894
  __fsetlocking (stderr, FSETLOCKING_BYCALLER);
Packit 032894
Packit 032894
  /* Set locale.  */
Packit 032894
  (void) setlocale (LC_ALL, "");
Packit 032894
Packit 032894
  elf_version (EV_CURRENT);
Packit 032894
Packit 032894
  pid_t pid = fork ();
Packit 032894
  switch (pid)
Packit 032894
  {
Packit 032894
    case -1:
Packit 032894
      perror ("fork failed");
Packit 032894
      exit (-1);
Packit 032894
    case 0:;
Packit 032894
      long l = ptrace (PTRACE_TRACEME, 0, NULL, NULL);
Packit 032894
      if (l != 0)
Packit 032894
	{
Packit 032894
	  perror ("PTRACE_TRACEME failed");
Packit 032894
	  exit (-1);
Packit 032894
	}
Packit 032894
      cleanup_13_main ();
Packit 032894
      printf ("cleanup_13_main returned, impossible...\n");
Packit 032894
      exit (-1);
Packit 032894
    default:
Packit 032894
      break;
Packit 032894
  }
Packit 032894
Packit 032894
  errno = 0;
Packit 032894
  int status;
Packit 032894
  pid_t got = waitpid (pid, &status, 0);
Packit 032894
  if (got != pid)
Packit 032894
    error (1, errno, "waitpid returned %d", got);
Packit 032894
  if (!WIFSTOPPED (status))
Packit 032894
    error (1, 0, "unexpected wait status %u", status);
Packit 032894
  if (WSTOPSIG (status) != SIGABRT)
Packit 032894
    error (1, 0, "unexpected signal %u", WSTOPSIG (status));
Packit 032894
Packit 032894
  Dwfl *dwfl = pid_to_dwfl (pid);
Packit 032894
  if (dwfl_getthreads (dwfl, thread_callback, NULL) == -1)
Packit 032894
    error (1, 0, "dwfl_getthreads: %s", dwfl_errmsg (-1));
Packit 032894
Packit 032894
  /* There is an exit (0) call if we find the "main" frame,  */
Packit 032894
  printf ("dwfl_getthreads returned, main not found\n");
Packit 032894
  exit (-1);
Packit 032894
}
Packit 032894
Packit 032894
#endif /* ! __linux__ */
Packit 032894