Daily bump.
[official-gcc.git] / libbacktrace / ttest.c
blobca55e9b3722f555de94c6a6b691744e0d887c335
1 /* ttest.c -- Test for libbacktrace library
2 Copyright (C) 2017-2018 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
33 /* Test using the libbacktrace library from multiple threads. */
35 #include <assert.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
40 #include <pthread.h>
42 #include "filenames.h"
44 #include "backtrace.h"
45 #include "backtrace-supported.h"
47 #include "testlib.h"
49 static int f2 (int) __attribute__ ((noinline));
50 static int f3 (int, int) __attribute__ ((noinline));
52 /* Test that a simple backtrace works. This is called via
53 pthread_create. It returns the number of failures, as void *. */
55 static void *
56 test1_thread (void *arg ATTRIBUTE_UNUSED)
58 /* Returning a value here and elsewhere avoids a tailcall which
59 would mess up the backtrace. */
60 return (void *) (uintptr_t) (f2 (__LINE__) - 2);
63 static int
64 f2 (int f1line)
66 return f3 (f1line, __LINE__) + 2;
69 static int
70 f3 (int f1line, int f2line)
72 struct info all[20];
73 struct bdata data;
74 int f3line;
75 int i;
77 data.all = &all[0];
78 data.index = 0;
79 data.max = 20;
80 data.failed = 0;
82 f3line = __LINE__ + 1;
83 i = backtrace_full (state, 0, callback_one, error_callback_one, &data);
85 if (i != 0)
87 fprintf (stderr, "test1: unexpected return value %d\n", i);
88 data.failed = 1;
91 if (data.index < 3)
93 fprintf (stderr,
94 "test1: not enough frames; got %zu, expected at least 3\n",
95 data.index);
96 data.failed = 1;
99 check ("test1", 0, all, f3line, "f3", "ttest.c", &data.failed);
100 check ("test1", 1, all, f2line, "f2", "ttest.c", &data.failed);
101 check ("test1", 2, all, f1line, "test1_thread", "ttest.c", &data.failed);
103 return data.failed;
106 /* Run the test with 10 threads simultaneously. */
108 #define THREAD_COUNT 10
110 static void test1 (void) __attribute__ ((unused));
112 static void
113 test1 (void)
115 pthread_t atid[THREAD_COUNT];
116 int i;
117 int errnum;
118 int this_fail;
119 void *ret;
121 for (i = 0; i < THREAD_COUNT; i++)
123 errnum = pthread_create (&atid[i], NULL, test1_thread, NULL);
124 if (errnum != 0)
126 fprintf (stderr, "pthread_create %d: %s\n", i, strerror (errnum));
127 exit (EXIT_FAILURE);
131 this_fail = 0;
132 for (i = 0; i < THREAD_COUNT; i++)
134 errnum = pthread_join (atid[i], &ret);
135 if (errnum != 0)
137 fprintf (stderr, "pthread_join %d: %s\n", i, strerror (errnum));
138 exit (EXIT_FAILURE);
140 this_fail += (int) (uintptr_t) ret;
143 printf ("%s: threaded backtrace_full noinline\n", this_fail > 0 ? "FAIL" : "PASS");
145 failures += this_fail;
149 main (int argc ATTRIBUTE_UNUSED, char **argv)
151 state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
152 error_callback_create, NULL);
154 #if BACKTRACE_SUPPORTED
155 #if BACKTRACE_SUPPORTS_THREADS
156 test1 ();
157 #endif
158 #endif
160 exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);