Blame debug/tst-backtrace5.c

Packit 6c4009
/* Test backtrace and backtrace_symbols for signal frames, where a
Packit 6c4009
   system call was interrupted by a signal.
Packit 6c4009
   Copyright (C) 2011-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <execinfo.h>
Packit 6c4009
#include <search.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#include "tst-backtrace.h"
Packit 6c4009
Packit 6c4009
#ifndef SIGACTION_FLAGS
Packit 6c4009
# define SIGACTION_FLAGS 0
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* The backtrace should include at least handle_signal, a signal
Packit 6c4009
   trampoline, read, 3 * fn, and do_test.  */
Packit 6c4009
#define NUM_FUNCTIONS 7
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
handle_signal (int signum)
Packit 6c4009
{
Packit 6c4009
  void *addresses[NUM_FUNCTIONS];
Packit 6c4009
  char **symbols;
Packit 6c4009
  int n;
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  /* Get the backtrace addresses.  */
Packit 6c4009
  n = backtrace (addresses, sizeof (addresses) / sizeof (addresses[0]));
Packit 6c4009
  printf ("Obtained backtrace with %d functions\n", n);
Packit 6c4009
  /*  Check that there are at least seven functions.  */
Packit 6c4009
  if (n < NUM_FUNCTIONS)
Packit 6c4009
    {
Packit 6c4009
      FAIL ();
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
  /* Convert them to symbols.  */
Packit 6c4009
  symbols = backtrace_symbols (addresses, n);
Packit 6c4009
  /* Check that symbols were obtained.  */
Packit 6c4009
  if (symbols == NULL)
Packit 6c4009
    {
Packit 6c4009
      FAIL ();
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
  for (i = 0; i < n; ++i)
Packit 6c4009
    printf ("Function %d: %s\n", i, symbols[i]);
Packit 6c4009
  /* Check that the function names obtained are accurate.  */
Packit 6c4009
  if (!match (symbols[0], "handle_signal"))
Packit 6c4009
    {
Packit 6c4009
      FAIL ();
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
  /* Do not check name for signal trampoline.  */
Packit 6c4009
  i = 2;
Packit 6c4009
  if (!match (symbols[i++], "read"))
Packit 6c4009
    {
Packit 6c4009
      /* Perhaps symbols[2] is __kernel_vsyscall?  */
Packit 6c4009
      if (!match (symbols[i++], "read"))
Packit 6c4009
	{
Packit 6c4009
	  FAIL ();
Packit 6c4009
	  return;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  for (; i < n - 1; i++)
Packit 6c4009
    if (!match (symbols[i], "fn"))
Packit 6c4009
      {
Packit 6c4009
	FAIL ();
Packit 6c4009
	return;
Packit 6c4009
      }
Packit 6c4009
  /* Symbol names are not available for static functions, so we do not
Packit 6c4009
     check do_test.  */
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
NO_INLINE int
Packit 6c4009
fn (int c, int flags)
Packit 6c4009
{
Packit 6c4009
  pid_t parent_pid, child_pid;
Packit 6c4009
  int pipefd[2];
Packit 6c4009
  char r[1];
Packit 6c4009
  struct sigaction act;
Packit 6c4009
Packit 6c4009
  if (c > 0)
Packit 6c4009
    {
Packit 6c4009
      fn (c - 1, flags);
Packit 6c4009
      return x;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  memset (&act, 0, sizeof (act));
Packit 6c4009
  act.sa_handler = handle_signal;
Packit 6c4009
  act.sa_flags = flags;
Packit 6c4009
  sigemptyset (&act.sa_mask);
Packit 6c4009
  sigaction (SIGUSR1, &act, NULL);
Packit 6c4009
  parent_pid = getpid ();
Packit 6c4009
  if (pipe (pipefd) == -1)
Packit 6c4009
    abort ();
Packit 6c4009
Packit 6c4009
  child_pid = fork ();
Packit 6c4009
  if (child_pid == (pid_t) -1)
Packit 6c4009
    abort ();
Packit 6c4009
  else if (child_pid == 0)
Packit 6c4009
    {
Packit 6c4009
      sleep (1);
Packit 6c4009
      kill (parent_pid, SIGUSR1);
Packit 6c4009
      _exit (0);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* In the parent.  */
Packit 6c4009
  read (pipefd[0], r, 1);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
NO_INLINE int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  fn (2, SIGACTION_FLAGS);
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>