Daily bump.
[gcc.git] / libbacktrace / btest.c
index df286dbeefe44e5866a145a75e0cd187c1ccb790..ff2eb7123f95e0b40e07d5be93c6fa49a1808b28 100644 (file)
@@ -1,5 +1,5 @@
 /* btest.c -- Test for libbacktrace library
-   Copyright (C) 2012 Free Software Foundation, Inc.
+   Copyright (C) 2012-2020 Free Software Foundation, Inc.
    Written by Ian Lance Taylor, Google.
 
 Redistribution and use in source and binary forms, with or without
@@ -7,13 +7,13 @@ modification, are permitted provided that the following conditions are
 met:
 
     (1) Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer. 
+    notice, this list of conditions and the following disclaimer.
 
     (2) Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
-    distribution.  
-    
+    distribution.
+
     (3) The name of the author may not be used to
     endorse or promote products derived from this software without
     specific prior written permission.
@@ -34,244 +34,23 @@ POSSIBILITY OF SUCH DAMAGE.  */
    libbacktrace library.  */
 
 #include <assert.h>
-#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #include "filenames.h"
 
 #include "backtrace.h"
 #include "backtrace-supported.h"
 
-/* Portable attribute syntax.  Actually some of these tests probably
-   won't work if the attributes are not recognized.  */
-
-#ifndef GCC_VERSION
-# define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
-#endif
-
-#if (GCC_VERSION < 2007)
-# define __attribute__(x)
-#endif
-
-#ifndef ATTRIBUTE_UNUSED
-# define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
-#endif
-
-/* Used to collect backtrace info.  */
-
-struct info
-{
-  char *filename;
-  int lineno;
-  char *function;
-};
-
-/* Passed to backtrace callback function.  */
-
-struct bdata
-{
-  struct info *all;
-  size_t index;
-  size_t max;
-  int failed;
-};
-
-/* Passed to backtrace_simple callback function.  */
-
-struct sdata
-{
-  uintptr_t *addrs;
-  size_t index;
-  size_t max;
-  int failed;
-};
-
-/* Passed to backtrace_syminfo callback function.  */
-
-struct symdata
-{
-  const char *name;
-  uintptr_t val;
-  int failed;
-};
-
-/* The backtrace state.  */
-
-static void *state;
-
-/* The number of failures.  */
-
-static int failures;
-
-/* Return the base name in a path.  */
-
-static const char *
-base (const char *p)
-{
-  const char *last;
-  const char *s;
-
-  last = NULL;
-  for (s = p; *s != '\0'; ++s)
-    {
-      if (IS_DIR_SEPARATOR (*s))
-       last = s + 1;
-    }
-  return last != NULL ? last : p;
-}
-
-/* Check an entry in a struct info array.  */
-
-static void
-check (const char *name, int index, const struct info *all, int want_lineno,
-       const char *want_function, int *failed)
-{
-  if (*failed)
-    return;
-  if (strcmp (base (all[index].filename), "btest.c") != 0)
-    {
-      fprintf (stderr, "%s: [%d]: got %s expected test.c\n", name, index,
-              all[index].filename);
-      *failed = 1;
-    }
-  if (all[index].lineno != want_lineno)
-    {
-      fprintf (stderr, "%s: [%d]: got %d expected %d\n", name, index,
-              all[index].lineno, want_lineno);
-      *failed = 1;
-    }
-  if (strcmp (all[index].function, want_function) != 0)
-    {
-      fprintf (stderr, "%s: [%d]: got %s expected %s\n", name, index,
-              all[index].function, want_function);
-      *failed = 1;
-    }
-}
-
-/* The backtrace callback function.  */
-
-static int
-callback_one (void *vdata, uintptr_t pc ATTRIBUTE_UNUSED,
-             const char *filename, int lineno, const char *function)
-{
-  struct bdata *data = (struct bdata *) vdata;
-  struct info *p;
-
-  if (data->index >= data->max)
-    {
-      fprintf (stderr, "callback_one: callback called too many times\n");
-      data->failed = 1;
-      return 1;
-    }
-
-  p = &data->all[data->index];
-  if (filename == NULL)
-    p->filename = NULL;
-  else
-    {
-      p->filename = strdup (filename);
-      assert (p->filename != NULL);
-    }
-  p->lineno = lineno;
-  if (function == NULL)
-    p->function = NULL;
-  else
-    {
-      p->function = strdup (function);
-      assert (p->function != NULL);
-    }
-  ++data->index;
-
-  return 0;
-}
-
-/* An error callback passed to backtrace.  */
-
-static void
-error_callback_one (void *vdata, const char *msg, int errnum)
-{
-  struct bdata *data = (struct bdata *) vdata;
-
-  fprintf (stderr, "%s", msg);
-  if (errnum > 0)
-    fprintf (stderr, ": %s", strerror (errnum));
-  fprintf (stderr, "\n");
-  data->failed = 1;
-}
-
-/* The backtrace_simple callback function.  */
-
-static int
-callback_two (void *vdata, uintptr_t pc)
-{
-  struct sdata *data = (struct sdata *) vdata;
-
-  if (data->index >= data->max)
-    {
-      fprintf (stderr, "callback_two: callback called too many times\n");
-      data->failed = 1;
-      return 1;
-    }
-
-  data->addrs[data->index] = pc;
-  ++data->index;
-
-  return 0;
-}
-
-/* An error callback passed to backtrace_simple.  */
-
-static void
-error_callback_two (void *vdata, const char *msg, int errnum)
-{
-  struct sdata *data = (struct sdata *) vdata;
-
-  fprintf (stderr, "%s", msg);
-  if (errnum > 0)
-    fprintf (stderr, ": %s", strerror (errnum));
-  fprintf (stderr, "\n");
-  data->failed = 1;
-}
-
-/* The backtrace_syminfo callback function.  */
-
-static void
-callback_three (void *vdata, uintptr_t pc ATTRIBUTE_UNUSED,
-               const char *symname, uintptr_t symval)
-{
-  struct symdata *data = (struct symdata *) vdata;
-
-  if (symname == NULL)
-    data->name = NULL;
-  else
-    {
-      data->name = strdup (symname);
-      assert (data->name != NULL);
-    }
-  data->val = symval;
-}
-
-/* The backtrace_syminfo error callback function.  */
-
-static void
-error_callback_three (void *vdata, const char *msg, int errnum)
-{
-  struct symdata *data = (struct symdata *) vdata;
-
-  fprintf (stderr, "%s", msg);
-  if (errnum > 0)
-    fprintf (stderr, ": %s", strerror (errnum));
-  fprintf (stderr, "\n");
-  data->failed = 1;
-}
+#include "testlib.h"
 
 /* Test the backtrace function with non-inlined functions.  */
 
-static int test1 (void) __attribute__ ((noinline, unused));
-static int f2 (int) __attribute__ ((noinline));
-static int f3 (int, int) __attribute__ ((noinline));
+static int test1 (void) __attribute__ ((noinline, noclone, unused));
+static int f2 (int) __attribute__ ((noinline, noclone));
+static int f3 (int, int) __attribute__ ((noinline, noclone));
 
 static int
 test1 (void)
@@ -309,9 +88,17 @@ f3 (int f1line, int f2line)
       data.failed = 1;
     }
 
-  check ("test1", 0, all, f3line, "f3", &data.failed);
-  check ("test1", 1, all, f2line, "f2", &data.failed);
-  check ("test1", 2, all, f1line, "test1", &data.failed);
+  if (data.index < 3)
+    {
+      fprintf (stderr,
+              "test1: not enough frames; got %zu, expected at least 3\n",
+              data.index);
+      data.failed = 1;
+    }
+
+  check ("test1", 0, all, f3line, "f3", "btest.c", &data.failed);
+  check ("test1", 1, all, f2line, "f2", "btest.c", &data.failed);
+  check ("test1", 2, all, f1line, "test1", "btest.c", &data.failed);
 
   printf ("%s: backtrace_full noinline\n", data.failed ? "FAIL" : "PASS");
 
@@ -361,9 +148,9 @@ f13 (int f1line, int f2line)
       data.failed = 1;
     }
 
-  check ("test2", 0, all, f3line, "f13", &data.failed);
-  check ("test2", 1, all, f2line, "f12", &data.failed);
-  check ("test2", 2, all, f1line, "test2", &data.failed);
+  check ("test2", 0, all, f3line, "f13", "btest.c", &data.failed);
+  check ("test2", 1, all, f2line, "f12", "btest.c", &data.failed);
+  check ("test2", 2, all, f1line, "test2", "btest.c", &data.failed);
 
   printf ("%s: backtrace_full inline\n", data.failed ? "FAIL" : "PASS");
 
@@ -375,9 +162,9 @@ f13 (int f1line, int f2line)
 
 /* Test the backtrace_simple function with non-inlined functions.  */
 
-static int test3 (void) __attribute__ ((noinline, unused));
-static int f22 (int) __attribute__ ((noinline));
-static int f23 (int, int) __attribute__ ((noinline));
+static int test3 (void) __attribute__ ((noinline, noclone, unused));
+static int f22 (int) __attribute__ ((noinline, noclone));
+static int f23 (int, int) __attribute__ ((noinline, noclone));
 
 static int
 test3 (void)
@@ -444,11 +231,11 @@ f23 (int f1line, int f2line)
                       (unsigned int) bdata.index, j + 1);
              bdata.failed = 1;
            }
-       }      
+       }
 
-      check ("test3", 0, all, f3line, "f23", &bdata.failed);
-      check ("test3", 1, all, f2line, "f22", &bdata.failed);
-      check ("test3", 2, all, f1line, "test3", &bdata.failed);
+      check ("test3", 0, all, f3line, "f23", "btest.c", &bdata.failed);
+      check ("test3", 1, all, f2line, "f22", "btest.c", &bdata.failed);
+      check ("test3", 2, all, f1line, "test3", "btest.c", &bdata.failed);
 
       if (bdata.failed)
        data.failed = 1;
@@ -459,6 +246,7 @@ f23 (int f1line, int f2line)
 
          symdata.name = NULL;
          symdata.val = 0;
+         symdata.size = 0;
          symdata.failed = 0;
 
          i = backtrace_syminfo (state, addrs[j], callback_three,
@@ -487,7 +275,7 @@ f23 (int f1line, int f2line)
                case 2:
                  expected = "test3";
                  break;
-               case 3:
+               default:
                  assert (0);
                }
 
@@ -583,9 +371,9 @@ f33 (int f1line, int f2line)
          bdata.failed = 1;
        }
 
-      check ("test4", 0, all, f3line, "f33", &bdata.failed);
-      check ("test4", 1, all, f2line, "f32", &bdata.failed);
-      check ("test4", 2, all, f1line, "test4", &bdata.failed);
+      check ("test4", 0, all, f3line, "f33", "btest.c", &bdata.failed);
+      check ("test4", 1, all, f2line, "f32", "btest.c", &bdata.failed);
+      check ("test4", 2, all, f1line, "test4", "btest.c", &bdata.failed);
 
       if (bdata.failed)
        data.failed = 1;
@@ -599,15 +387,94 @@ f33 (int f1line, int f2line)
   return failures;
 }
 
+static int test5 (void) __attribute__ ((unused));
+
+int global = 1;
+
+static int
+test5 (void)
+{
+  struct symdata symdata;
+  int i;
+  uintptr_t addr = (uintptr_t) &global;
+
+  if (sizeof (global) > 1)
+    addr += 1;
+
+  symdata.name = NULL;
+  symdata.val = 0;
+  symdata.size = 0;
+  symdata.failed = 0;
+
+  i = backtrace_syminfo (state, addr, callback_three,
+                        error_callback_three, &symdata);
+  if (i == 0)
+    {
+      fprintf (stderr,
+              "test5: unexpected return value from backtrace_syminfo %d\n",
+              i);
+      symdata.failed = 1;
+    }
+
+  if (!symdata.failed)
+    {
+      if (symdata.name == NULL)
+       {
+         fprintf (stderr, "test5: NULL syminfo name\n");
+         symdata.failed = 1;
+       }
+      else if (!(strncmp (symdata.name, "global", 6) == 0
+                && (symdata.name[6] == '\0'|| symdata.name[6] == '.')))
+       {
+         fprintf (stderr,
+                  "test5: unexpected syminfo name got %s expected %s\n",
+                  symdata.name, "global");
+         symdata.failed = 1;
+       }
+      else if (symdata.val != (uintptr_t) &global)
+       {
+         fprintf (stderr,
+                  "test5: unexpected syminfo value got %lx expected %lx\n",
+                  (unsigned long) symdata.val,
+                  (unsigned long) (uintptr_t) &global);
+         symdata.failed = 1;
+       }
+      else if (symdata.size != sizeof (global))
+       {
+         fprintf (stderr,
+                  "test5: unexpected syminfo size got %lx expected %lx\n",
+                  (unsigned long) symdata.size,
+                  (unsigned long) sizeof (global));
+         symdata.failed = 1;
+       }
+    }
+
+  printf ("%s: backtrace_syminfo variable\n",
+         symdata.failed ? "FAIL" : "PASS");
+
+  if (symdata.failed)
+    ++failures;
+
+  return failures;
+}
+
+/* Check that are no files left open.  */
+
 static void
-error_callback_create (void *data ATTRIBUTE_UNUSED, const char *msg,
-                      int errnum)
+check_open_files (void)
 {
-  fprintf (stderr, "%s", msg);
-  if (errnum > 0)
-    fprintf (stderr, ": %s", strerror (errnum));
-  fprintf (stderr, "\n");
-  exit (EXIT_FAILURE);
+  int i;
+
+  for (i = 3; i < 10; i++)
+    {
+      if (close (i) == 0)
+       {
+         fprintf (stderr,
+                  "ERROR: descriptor %d still open after tests complete\n",
+                  i);
+         ++failures;
+       }
+    }
 }
 
 /* Run all the tests.  */
@@ -623,7 +490,12 @@ main (int argc ATTRIBUTE_UNUSED, char **argv)
   test2 ();
   test3 ();
   test4 ();
+#if BACKTRACE_SUPPORTS_DATA
+  test5 ();
 #endif
+#endif
+
+  check_open_files ();
 
   exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);
 }