1 /* ttest.c -- Test for libbacktrace library
2 Copyright (C) 2017-2022 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
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
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. */
42 #include "filenames.h"
44 #include "backtrace.h"
45 #include "backtrace-supported.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 *. */
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);
66 return f3 (f1line
, __LINE__
) + 2;
70 f3 (int f1line
, int f2line
)
82 f3line
= __LINE__
+ 1;
83 i
= backtrace_full (state
, 0, callback_one
, error_callback_one
, &data
);
87 fprintf (stderr
, "test1: unexpected return value %d\n", i
);
94 "test1: not enough frames; got %zu, expected at least 3\n",
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
);
106 /* Run the test with 10 threads simultaneously. */
108 #define THREAD_COUNT 10
110 static void test1 (void) __attribute__ ((unused
));
115 pthread_t atid
[THREAD_COUNT
];
121 for (i
= 0; i
< THREAD_COUNT
; i
++)
123 errnum
= pthread_create (&atid
[i
], NULL
, test1_thread
, NULL
);
126 fprintf (stderr
, "pthread_create %d: %s\n", i
, strerror (errnum
));
132 for (i
= 0; i
< THREAD_COUNT
; i
++)
134 errnum
= pthread_join (atid
[i
], &ret
);
137 fprintf (stderr
, "pthread_join %d: %s\n", i
, strerror (errnum
));
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
160 exit (failures
? EXIT_FAILURE
: EXIT_SUCCESS
);