Blame debug/tst-backtrace3.c

Packit 6c4009
/* Test backtrace and backtrace_symbols for recursive calls.
Packit 6c4009
   Copyright (C) 2010-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
Packit 6c4009
#include "tst-backtrace.h"
Packit 6c4009
Packit 6c4009
/* The backtrace should include at least 3 * fn, and do_test.  */
Packit 6c4009
#define NUM_FUNCTIONS 4
Packit 6c4009
Packit 6c4009
NO_INLINE int
Packit 6c4009
fn (int c)
Packit 6c4009
{
Packit 6c4009
  void *addresses[NUM_FUNCTIONS];
Packit 6c4009
  char **symbols;
Packit 6c4009
  int n;
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  if (c > 0)
Packit 6c4009
    {
Packit 6c4009
      fn (c - 1);
Packit 6c4009
      return x;
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 four functions.  */
Packit 6c4009
  if (n < NUM_FUNCTIONS)
Packit 6c4009
    {
Packit 6c4009
      FAIL ();
Packit 6c4009
      return 1;
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 1;
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
  for (i = 0; i < n - 1; ++i)
Packit 6c4009
    if (!match (symbols[i], "fn"))
Packit 6c4009
      {
Packit 6c4009
	FAIL ();
Packit 6c4009
	return 1;
Packit 6c4009
      }
Packit 6c4009
  /* Symbol names are not available for static functions, so we do not
Packit 6c4009
     check do_test.  */
Packit 6c4009
  return x;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
NO_INLINE int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  fn (2);
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>